Loop Optimization

Hardware pays off most when the same work is repeated many times: a loop, in software. A processor runs one iteration after another. Custom hardware can do better in two ways, and this demo measures both, on the polynomial from the lecture evaluated over an array of samples:

y[i] = c0 + c1*x[i] + c2*x[i]^2 + c3*x[i]^3
  1. Pipelining: start the next iteration before the last one has finished. One evaluation takes 30 clock cycles, but a pipelined loop starts a new one every cycle, so 30 are in progress at once.
  2. Unrolling: build several copies of the loop body, so several samples start in each cycle. That only pays if the memories holding x and y can deliver that many words per cycle, which is what array partitioning is for.

The timing demo did this for a single computation with no loop. Here the loop is the point.

Learning Objectives

In going through this demo, you will:

  • Use the PIPELINE pragma to pipeline a loop in Vitis HLS, and switch pipelining off to compare
  • Read a loop’s initiation interval (II), iteration latency and trip count from the synthesis report, and from them the cycles for the whole call
  • Read the schedule of a pipelined loop: which operation is in which cycle of an iteration, and how the iterations overlap
  • See pipelining in an RTL simulation: a new address on the x memory every cycle, instead of one every 30
  • Use the UNROLL pragma, and the ARRAY_PARTITION pragma to give the unrolled loop the memory ports it needs
  • Measure the trade-off: throughput against DSP blocks, LUTs and flip-flops, as the unroll factor grows
  • See why unrolling a sum needs the loop body rewritten, and unroll a dot product by hand

The pages

Part 1, pipelining:

  1. The kernel: the polynomial in C++, its memory interface, and the pragmas, switched on and off by macros so one source builds every variant.
  2. Pipelining the loop: the build steps, the synthesis report with and without the PIPELINE pragma, and the two RTL simulations.
  3. The pipeline schedule: which operation Vitis put in which cycle, and why a new iteration can start every cycle.
  4. In-class exercise: a loop that asks for II = 1 and gets 2. You rewrite it, in its own folder, demos/loopopt_inclass.

Part 2, unrolling (unit 8):

  1. Unrolling the loop: unroll factors 2, 4 and 8, the memory bottleneck without partitioning, and throughput against resources.
  2. Unrolling a sum: a float sum unrolled four ways, from demos/fsum: running it, and which report holds each number.
  3. Reading the II from the stages: the rolled loop stage by stage, and why the UNROLL pragma alone makes it no faster.
  4. Rewriting the body: a tree, and four lanes, each four times faster, in the lecture’s model and in Vitis’s schedule.
  5. How many adders?: adds per iteration ÷ II, the adder cycle by cycle, and where lanes’ extra hardware comes from.
  6. In-class exercise: unroll a dot product by hand, partition its arrays, and measure each step, in its own folder, demos/unroll_inclass.

Nested loops are the FIR demo.

The code

Everything is in hwdesign/demos/pipeline/poly:

File What it is
poly.cpp, poly.h the kernel; its pragmas are switched by the POLY_* macros
tb_poly.cpp the testbench, for C simulation and co-simulation
run.tcl the Vitis HLS script: csim, synth or cosim per invocation
golden.py the test vector and an independent FP32 Python model
poly_analysis.py reads the reports and schedules, and plots them and the waveforms
poly_build.py the build script: which tool runs when, and what is checked

To run all of it, in hwdesign-venv with Vitis installed:

(env) python poly_build.py --through pipeline    # part 1
(env) python poly_build.py                       # part 2 as well

Table of contents


This site uses Just the Docs, a documentation theme for Jekyll.