---
title: High-level synthesis
heading: Scheduling loops into hardware, initiation interval, unrolling, memory ports and recurrences
eyebrow: S3 · Lesson 33 · High-level synthesis
xp: 50
---

::: hook
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.
:::

::: play
- The line is the number of cycles a pipelined loop takes against its trip count N: $(N - 1)\cdot\text{II} + D$, 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.
:::

```sim plot
view: graph
x: N = 1..4096
y: C = 0..20000
slider: I = 1..8 step 1 = 2
slider: D = 1..40 step 1 = 8
plot: (N - 1)*I + D | label: pipelined loop
plot: N + 0*I | dashed | label: one per cycle
readout: cycles for 1024 iterations = 1023*I + D | digits: 5
```

::: 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?

- [x] 1031 and 2054 cycles
- [ ] 8192 cycles both ways
- [ ] 1024 and 2048 cycles
- [ ] 1031 and 1039 cycles

::: reveal
A pipelined loop starts an iteration every II cycles and finishes the last one D cycles after it starts: $(1024 - 1) \times 1 + 8 = 1031$ and $(1024 - 1) \times 2 + 8 = 2054$. Without pipelining it would take $1024 \times 8 = 8192$. The interval, not the depth, sets the throughput.
:::
:::

::: nameit
## 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:
$$\text{cycles} = (N - 1)\,\text{II} + D, \qquad \text{throughput} = \frac{f_\text{clk}}{\text{II}}$$

## 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 $\text{II} \ge \lceil u/p\rceil$. 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 $\text{II} \ge \lceil L/d\rceil$. 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.
:::

::: 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?

::: solution
1. **As written.** Each addition needs the previous sum: a recurrence of 4 cycles at distance 1, so II = 4 and about
   $$1023 \times 4 + D\ \text{cycles}$$
   over 4000.
2. **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.
3. **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).
:::
:::

::: 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?

::: solution
1. **Ports needed.** Four reads through two ports need
   $$\lceil 4/2 \rceil = 2$$
   cycles per iteration, so II = 2.
2. **Cyclic partitioning.** Splitting the array into two memories (even and odd elements) gives four ports, enough for II = 1.
3. **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.
:::
:::

::: 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?

::: solution
1. **Iterations.** Unrolled by 8, the loop runs 128 iterations:
   $$127 + 10 = 137\ \text{cycles}$$
   which is 0.55 µs at 250 MHz.
2. **Without unrolling.** It would take 1033 cycles.
3. **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
kind: parts
given: N = 256 | 1024 | 4096; D = 5 | 12; I = 1 | 2 | 4; f = 200 | 300

## A loop of {N} iterations, each {D} cycles deep, is pipelined at II = {I} on a {f} MHz clock.

::: part
answer: (N - 1)*I + D
unit: cycles
tolerance: 0

### How many cycles does it take?

::: misses
- [N*D] That's without pipelining.
- [any] $(N - 1)\text{II} + D$.
:::
:::

::: part
answer: f/I
unit: MHz
tolerance: 0.5%

### At what rate does it complete iterations, in millions per second?

::: misses
- [any] $f_\text{clk}/\text{II}$.
:::
:::
:::

::: check
kind: parts
given: r = 3 | 4 | 6 | 8; p = 1 | 2; k = 2 | 4

## A loop body reads an array {r} times per iteration; the array's memory has {p} port(s).

::: part
answer: ceil(r/p)
tolerance: 0

### What is the smallest initiation interval the memory allows?

::: misses
- [any] $\lceil u/p\rceil$.
:::
:::

::: part
answer: ceil(r/(p*k))
tolerance: 0

### And after partitioning the array into {k} memories, if the reads spread evenly?

::: misses
- [any] Partitioning multiplies the ports.
:::
:::
:::

::: check
kind: parts
given: L = 3 | 4 | 7; d = 2 | 3

## A loop-carried dependence passes through a chain of operations {L} cycles long to an iteration {d} later. The loop runs 1000 iterations, each 10 cycles deep.

::: part
answer: ceil(L/d)
tolerance: 0

### What is the smallest initiation interval?

::: misses
- [L] The dependence spans d iterations: divide by d.
- [any] $\lceil L/d\rceil$.
:::
:::

::: part
answer: 999*ceil(L/d) + 10
unit: cycles
tolerance: 0

### How many cycles does the loop take at that interval?

::: misses
- [any] $(N - 1)\text{II} + D$.
:::
:::
:::

::: check
kind: parts
given: N = 1024 | 4096; U = 2 | 4 | 8; D = 6 | 10

## A loop of {N} iterations, {D} cycles deep, is unrolled by {U} and pipelined at II = 1.

::: part
answer: N/U - 1 + D
unit: cycles
tolerance: 0

### How many cycles does it take?

::: misses
- [N - 1 + D] Unrolling cuts the iterations to N/U.
- [any] $(N/U - 1) + D$.
:::
:::

::: part
answer: (N - 1 + D)/(N/U - 1 + D)
tolerance: 0.5%

### How much faster is that than without unrolling?

::: misses
- [any] Compare with $(N - 1) + D$.
:::
:::
:::

::: check
kind: choice

## 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?

- [ ] The multiplier needs four cycles per operation
- [x] Each addition needs the previous iteration's sum, and the adder takes four cycles, so a new addition can start only every four cycles
- [ ] The arrays are too small to pipeline
- [ ] Floating point can't be pipelined at all

::: misses
- [1] A pipelined multiplier accepts a new operation every cycle.
- [3] Array size doesn't set the interval; ports and dependences do.
- [4] The units are pipelined; the recurrence is the limit.
:::
:::

::: check
kind: derivation
vars: N = 2..1e5, I = 1..8, D = 1..50, U = 1..16

## 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
answer: (N - 1)*I + D

### How many cycles does the pipelined loop take?
:::

::: step
answer: N*D/((N - 1)*I + D)

### How much faster is that than running iterations one after another?
:::

::: step
answer: N/U - 1 + D

### How many cycles does the unrolled loop take?
:::

::: right
Right. For long loops the speedup from pipelining approaches D/II, and unrolling multiplies the rate by U.
:::
:::

::: check
kind: python

## 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 starter
def min_ii(uses, units, recurrences):
    return 1

def loop_cycles(n, ii, depth):
    return n * depth
```

```python solution
def min_ii(uses, units, recurrences):
    ii = 1
    for r, u in uses.items():
        ii = max(ii, -(-u // units[r]))
    for lat, dist in recurrences:
        ii = max(ii, -(-lat // dist))
    return ii

def loop_cycles(n, ii, depth):
    return (n - 1) * ii + depth
```

```python tests
assert min_ii({'bram': 4}, {'bram': 2}, []) == 2, "Four reads through two ports: II = 2."
assert min_ii({'bram': 4, 'mul': 1}, {'bram': 4, 'mul': 1}, [(4, 1)]) == 4, "The accumulator recurrence: II = 4."
assert min_ii({'bram': 4, 'mul': 1}, {'bram': 4, 'mul': 1}, [(4, 4)]) == 1, "Four partial sums: II = 1."
assert loop_cycles(1024, 2, 8) == 2054, "The predict: 2054 cycles."
```

::: right
Right. These are the two lower bounds an HLS scheduler computes first; the schedule it finds can only be at or above them.
:::
:::

::: explain
## Why does unrolling a loop not always speed it up in high-level synthesis?

::: model
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
::: check
given: N = 500 | 2000; I = 1 | 3; D = 4 | 9
answer: (N - 1)*I + D
unit: cycles
tolerance: 0

## Cycles for {N} iterations at II = {I}, depth {D}?

::: misses
- [any] $(N - 1)\text{II} + D$.
:::
:::

::: check
given: r = 5 | 7; p = 2 | 3
answer: ceil(r/p)
tolerance: 0

## Interval for {r} reads per iteration through {p} ports?

::: misses
- [any] $\lceil r/p\rceil$.
:::
:::

::: check
given: L = 5 | 9; d = 2 | 3
answer: ceil(L/d)
tolerance: 0

## Interval for a {L}-cycle recurrence at distance {d}?

::: misses
- [any] $\lceil L/d\rceil$.
:::
:::
:::
