Compiler Instruction Scheduling for a Multi-Issue VLIW Processor
System DesignModel answerLast updated
Design an instruction scheduler for a 4-issue VLIW core with mixed-latency functional units, covering hazard detection, register pressure, loop pipelining, and latency sensitivity.
You are building the scheduling pass of a compiler back end for a 4-issue VLIW processor. The machine has the following functional units and latencies:
- Two ALU units, each with 1-cycle latency.
- One load/store unit (LSU), fully pipelined, with 3-cycle latency for loads and 1-cycle effective latency for stores (a store writes no register).
- One special-function unit (SFU) with 2-cycle latency.
Each cycle the processor can issue at most 4 operations, respecting the per-unit limits above. The hardware provides no interlocks; correctness is entirely the compiler's responsibility. Empty issue slots become NOPs.
You are given a basic block represented as a dependency DAG where each node is an instruction tagged with its functional unit and latency, and each edge carries a dependency type (RAW, WAR, WAW, or memory ordering).
Your tasks:
- Produce a legal instruction schedule that maximizes parallel issue and minimizes stall cycles. Show the cycle-by-cycle bundle layout.
- Explain how your scheduler detects and handles each hazard type (RAW, WAR, WAW, structural, memory aliasing, and control flow across blocks).
- Discuss how register pressure interacts with your scheduling decisions and what strategies you use to manage it.
- Suppose the basic block is the body of a tight loop. Describe how you would apply software pipelining, including the initiation interval, stage count, and register management.
- If the average load latency increases from 3 to 4 cycles, explain how your straight-line schedule and pipelined loop schedule change.
Hints
Hint 1
Model each dependency edge with its latency so a consumer can only issue once the producer's result is available; with no interlocks, the scheduler must insert explicit idle slots or fill them with independent work.
Hint 2
Use a priority function like latency-weighted critical-path height and greedily fill each cycle's unit slots in priority order; this is a heuristic since optimal scheduling is NP-hard.
Hint 3
Operate on SSA virtual registers before physical allocation to eliminate WAR and WAW false dependencies, then track live-register counts during scheduling to avoid spills.
Hint 4
For loops, compute the resource-constrained initiation interval (ResMII) from unit utilization and the recurrence-constrained interval (RecMII) from loop-carried dependencies, then use modulo scheduling.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
A strong answer walks through five areas: machine model, scheduling algorithm, hazard handling, register pressure, and loop pipelining with a latency-sensitivity discussion.
1. Machine Model
State assumptions explicitly before scheduling:
- Bundle constraints: up to 4 ops per cycle, with at most 2 on ALU ports, 1 on the LSU, and 1 on the SFU. Unfilled slots are NOPs.
- Latency semantics: a result is consumable at
issue_cycle + latency. ALU results take 1 cycle, loads take 3, SFU operations take 2, and stores produce no destination register (useful only for ordering, so treat their effective latency as 1). - Pipeline behavior: units are fully pipelined, so the LSU can initiate a new load every cycle without waiting for a prior load to complete.
- Interlocks: none. The compiler guarantees that no consumer issues before a producer's result is ready. If a scoreboard existed, an illegal schedule would still be functionally safe but slow; clarify this distinction because it affects the latency question later.
- Register file: confirm the available register count and whether memory operations have known aliasing properties.
2. Scheduling Algorithm
Use critical-path list scheduling, a greedy heuristic that works well because optimal VLIW scheduling is NP-hard.
Build the DAG. Each edge carries a latency value:
- RAW edge: the producer's latency.
- WAR edge: 0 (or 1 on some architectures).
- WAW edge: the difference in producer latencies.
- Memory-ordering edge: 1 (or the relevant latency) for potentially aliasing load/store pairs.
Compute priorities. For each node, calculate its height — the longest latency-weighted path from that node to the end of the block:
height(n) = latency(n) + max(height(successor) for each successor)
Nodes on the critical path get the highest priority.
Greedily fill bundles. Each cycle, collect all ready operations (all predecessors have issued and their latencies have elapsed). Sort by descending height, breaking ties by number of successors, then by which op reduces live-register count. Fill available unit slots in that order. If no op is ready, emit a NOP cycle.
import collections
def list_schedule(ops, succs, lat):
"""
ops: {id: unit_name}
succs: {id: [successor_id, ...]}
lat: {id: latency_cycles}
Returns: (issue_times: {id: cycle}, bundles: [[op_id, ...], ...])
"""
unit_caps = {"alu": 2, "lsu": 1, "sfu": 1}
# Build predecessor map
preds = collections.defaultdict(list)
for u, vs in succs.items():
for v in vs:
preds[v].append(u)
# Compute height via memoized DFS
height = {}
def compute_height(n):
if n not in height:
height[n] = lat[n] + max(
(compute_height(s) for s in succs.get(n, [])), default=0
)
return height[n]
for n in ops:
compute_height(n)
issued, cycle, bundles = {}, 0, []
while len(issued) < len(ops):
# Ready: all predecessors issued and their latencies have elapsed
ready = sorted(
[n for n in ops if n not in issued
and all(p in issued and issued[p] + lat[p] <= cycle
for p in preds[n])],
key=lambda n: (-height[n], -len(succs.get(n, [])))
)
free = dict(unit_caps)
bundle = []
for n in ready:
if free.get(ops[n], 0) > 0 and len(bundle) < 4:
free[ops[n]] -= 1
issued[n] = cycle
bundle.append(n)
bundles.append(bundle)
cycle += 1
return issued, bundles
3. Worked Example
Block computing c = sqrt(a + b) + (x - 1):
| Op | Instruction | Unit | Latency | Height |
|---|---|---|---|---|
| I1 | ld r1, [a] | LSU | 3 | 8 |
| I2 | ld r2, [b] | LSU | 3 | 8 |
| I3 | add r3, r1, r2 | ALU | 1 | 5 |
| I4 | sqrt r4, r3 | SFU | 2 | 4 |
| I5 | sub r5, r0, 1 | ALU | 1 | 3 |
| I6 | add r6, r5, r4 | ALU | 1 | 2 |
| I7 | st [c], r6 | LSU | 1 | 1 |
Schedule:
| Cycle | ALU0 | ALU1 | LSU | SFU |
|---|---|---|---|---|
| 0 | I5 | NOP | I1 | NOP |
| 1 | NOP | NOP | I2 | NOP |
| 2–3 | NOP | NOP | NOP | NOP |
| 4 | I3 | NOP | NOP | NOP |
| 5 | NOP | NOP | NOP | I4 |
| 6 | NOP | NOP | NOP | NOP |
| 7 | I6 | NOP | NOP | NOP |
| 8 | NOP | NOP | I7 | NOP |
Total: 9 cycles. The critical path is 8, but the single LSU serializes I2 to cycle 1, adding one cycle. I5 has slack and fills a slot that would otherwise be wasted. The block has low ILP, which is precisely why loops benefit from software pipelining.
4. Hazard Handling
- RAW: Enforced by latency edges. Cycles 2–3 and 6 are explicit stalls (or filled with independent work from surrounding blocks).
- WAR / WAW: Schedule on SSA virtual registers so false dependencies vanish entirely. Physical register allocation happens afterward. Some architectures offer "equals" semantics where an old value remains readable until a new write lands, but this complicates exception handling. Prefer the safer model where a write invalidates the old value at issue time.
- Structural: A per-cycle reservation table tracks unit occupancy and the 4-issue limit. For pipelined loops this becomes a modulo reservation table indexed by
cycle % II. - Memory aliasing: A load may cross above a store only if alias analysis proves they access disjoint addresses. Without proof, add an ordering edge. Speculative loads (e.g., IA-64
ld.a) are an advanced option for hiding latency when aliasing is uncertain. - Control flow: Within a basic block there is none. Across blocks, use trace or superblock scheduling with compensation code at rejoin points.
5. Register Pressure
Hoisting loads early extends live ranges and increases the number of simultaneously live values. Two standard approaches:
- Prepass scheduling: Schedule first, then allocate. If spills appear, rerun the scheduler with pressure awareness.
- Pressure-aware priority: Track MaxLive during scheduling. When the live count approaches the physical register limit, boost the priority of operations that end live ranges (consumers) over those that start new ones (loads). In the example above, at most 3 values are live at once, so pressure is trivial. In pipelined loops it becomes the binding constraint.
6. Software Pipelining
If the block is a loop body over arrays, apply modulo scheduling to overlap consecutive iterations.
- ResMII (resource-constrained initiation interval): 3 memory ops on 1 LSU gives 3; 2 ALU ops on 2 ALU units gives 1; 1 SFU op on 1 SFU unit gives 1. So ResMII = 3.
- RecMII (recurrence-constrained interval): No loop-carried dependency here, so RecMII = 0. A reduction like
sum += xwould add latency/distance and raise RecMII. - II: max(ResMII, RecMII) = 3. Iterative modulo scheduling places operations in a modulo reservation table at II = 3, incrementing II if a legal placement is not found.
- Stage count: Schedule length is 9, so stages = ceil(9 / 3) = 3. Throughput improves from 9 cycles per iteration to 3. The LSU is 100% utilized, making the loop provably optimal for this hardware.
- Register management: A load result like
r1lives longer than II, so use rotating registers or modulo variable expansion (unroll the kernel and rename across stages). - Prologue / epilogue: Generate explicitly, or use predicated stage execution. Trip counts below the stage count need a scalar fallback path.
7. Increased Load Latency (4 cycles)
Straight-line schedule: The critical path grows to 9 and the LSU-constrained bound becomes 10. I3 shifts to cycle 5, I4 to cycle 6, I6 to cycle 8, and I7 to cycle 9 — total 10 cycles. The list scheduler handles this automatically because heights change and loads gain priority. The key opportunity is to schedule loads even earlier using alias analysis, speculation, or cross-block hoisting to hide the extra cycle.
Pipelined loop: II stays at 3 because the loop is resource-bound, not latency-bound. Schedule length grows to 10, so stages become ceil(10 / 3) = 4. The extra stage means one more concurrent iteration in flight, increasing register pressure and potentially requiring additional rotating registers or renaming slots. If the register file cannot sustain the extra stage, II must increase to 4, trading throughput for fewer spills.