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
- 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.
- Unrolling: build several copies of the loop body, so several
samples start in each cycle. That only pays if the memories holding
xandycan 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
PIPELINEpragma 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
xmemory every cycle, instead of one every 30 - Use the
UNROLLpragma, and theARRAY_PARTITIONpragma 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:
- The kernel: the polynomial in C++, its memory interface, and the pragmas, switched on and off by macros so one source builds every variant.
- Pipelining the loop: the build steps, the synthesis
report with and without the
PIPELINEpragma, and the two RTL simulations. - The pipeline schedule: which operation Vitis put in which cycle, and why a new iteration can start every cycle.
- 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):
- Unrolling the loop: unroll factors 2, 4 and 8, the memory bottleneck without partitioning, and throughput against resources.
- Unrolling a sum: a float sum unrolled four ways, from
demos/fsum: running it, and which report holds each number. - Reading the II from the stages: the rolled loop
stage by stage, and why the
UNROLLpragma alone makes it no faster. - Rewriting the body: a tree, and four lanes, each four times faster, in the lecture’s model and in Vitis’s schedule.
- How many adders?: adds per iteration ÷ II, the adder cycle by cycle, and where lanes’ extra hardware comes from.
- 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