S3 · Lesson 33 · High-level synthesis
Scheduling loops into hardware, initiation interval, unrolling, memory ports and recurrences
High-level synthesis compiles C or C++ into RTL. It decides which clock cycle each operation runs in (scheduling) and which hardware unit runs it (binding), and its most important output is a single number per loop: the initiation interval, how many cycles pass between starting one iteration and the next. A filter written as a loop can run one sample per cycle or one per eight, depending on memory ports, loop-carried dependences and a few pragmas. Reading those limits from the code is what lets a designer get hardware speed out of software syntax.
- The line is the number of cycles a pipelined loop takes against its trip count N: , for the initiation interval II and the iteration depth D set by the sliders.
- The dashed line is one iteration per cycle, the ideal of II = 1.
- The depth only adds a constant; the initiation interval multiplies everything, which is why HLS reports put II first.
Predict
A loop of 1024 iterations, each 8 cycles deep, is pipelined at II = 1 and then at II = 2. How many cycles does it take each way?
A pipelined loop starts an iteration every II cycles and finishes the last one D cycles after it starts: and . Without pipelining it would take . The interval, not the depth, sets the throughput.
Name it
Scheduling and pipelining
HLS turns each operation into a node of a dataflow graph, assigns it a cycle within the iteration (its depth D cycles in all), and with loop pipelining overlaps iterations, starting one every II cycles:
What limits II
The achievable interval is the larger of two bounds.
- Resources: an iteration that uses a resource u times, with p copies (or memory ports) available, needs . A block RAM has two ports; array partitioning splits an array across several memories to multiply the ports.
- Recurrences: if an iteration needs a result from d iterations earlier, produced by a chain of operations L cycles long, then . A floating-point accumulation with a 4-cycle adder (d = 1) can't go faster than II = 4 as written; four partial sums, combined at the end, make d = 4 and II = 1.
Unrolling
Unrolling by U copies the loop body U times: N/U iterations, U times the hardware, and with II = 1 about U results per cycle, as long as memory ports and recurrences allow it.
Dataflow
Functions connected by streams or ping-pong buffers can run concurrently, each at its own interval; the slowest sets the throughput, like stages of any pipeline.
Worked example
A dot product of 1024 floating-point pairs uses a 4-cycle adder to accumulate. What interval does HLS reach as written, and with four partial sums?
Show the solution
- As written. Each addition needs the previous sum: a recurrence of 4 cycles at distance 1, so II = 4 and about over 4000.
- With four partial sums. Each accumulator is needed again only every fourth iteration, so II = 1: about 1031 cycles, plus a few to add the four sums.
- Compare. The same arithmetic, reassociated, runs four times faster.
Answer II = 4 as written (over 4000 cycles), and II = 1 with four partial sums (about 1031 cycles).
Worked example
A loop body reads a[i], a[i + 1], a[i + 2] and a[i + 3] from an array held in one dual-port block RAM. What interval can it reach, and how does partitioning help?
Show the solution
- Ports needed. Four reads through two ports need cycles per iteration, so II = 2.
- Cyclic partitioning. Splitting the array into two memories (even and odd elements) gives four ports, enough for II = 1.
- Complete partitioning. Putting the array in registers removes the limit entirely but costs flip-flops for every element.
Answer II = 2 as it stands; partitioning into two memories reaches II = 1.
Worked example
A 1024-iteration multiply-accumulate loop with depth 10 is unrolled by 8 and pipelined at II = 1 on a 250 MHz clock. How long does it take, and what does it cost?
Show the solution
- Iterations. Unrolled by 8, the loop runs 128 iterations: which is 0.55 µs at 250 MHz.
- Without unrolling. It would take 1033 cycles.
- The cost. It needs 8 multipliers instead of one and 8 array elements per cycle, so the array must be partitioned by 8 (or held in a memory 8 words wide).
Answer 137 cycles (0.55 µs), at the cost of 8 multipliers and an array partitioned by 8.
Check
A loop of 1024 iterations, each 5 cycles deep, is pipelined at II = 4 on a 300 MHz clock.
(a)
How many cycles does it take?
(b)
At what rate does it complete iterations, in millions per second?
Write units the way you normally would: 1.5 MHz, 1500 kHz and 0.0015 GHz are the same answer.
Check
A loop body reads an array 4 times per iteration; the array's memory has 1 port(s).
(a)
What is the smallest initiation interval the memory allows?
(b)
And after partitioning the array into 4 memories, if the reads spread evenly?
Check
A loop-carried dependence passes through a chain of operations 4 cycles long to an iteration 2 later. The loop runs 1000 iterations, each 10 cycles deep.
(a)
What is the smallest initiation interval?
(b)
How many cycles does the loop take at that interval?
Check
A loop of 1024 iterations, 10 cycles deep, is unrolled by 2 and pipelined at II = 1.
(a)
How many cycles does it take?
(b)
How much faster is that than without unrolling?
Check
A loop accumulates sum += a[i] * b[i] in floating point, and HLS reports II = 4 though the multiplier and adder are each fully pipelined. Why?
Check
A loop of N iterations has depth D and is pipelined at interval I; separately, it can be unrolled by U at II = 1.
Step 1
How many cycles does the pipelined loop take?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
How much faster is that than running iterations one after another?
Step 3
How many cycles does the unrolled loop take?
Check
Write min_ii(uses, units, recurrences): uses and units are dicts from resource name to uses per iteration and copies available; recurrences is a list of (latency, distance) pairs. Return the smallest initiation interval allowed by both bounds. Then write loop_cycles(n, ii, depth).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does unrolling a loop not always speed it up in high-level synthesis?
Unrolling makes copies of the loop body so that several iterations' work happens each cycle, but every copy still needs its operands. If the arrays live in memories with only two ports, the extra reads can't all happen in the same cycle, so the scheduler stretches the initiation interval and the unrolled loop runs no faster. Likewise, if each iteration depends on the previous one's result through a multi-cycle operation, copying the body doesn't break the chain of dependences. Unrolling pays off only together with enough memory bandwidth, through array partitioning or wider memories, and with independent work in the copies.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.