Algorithm Search⌘K

Problemcore

Instruction scheduling

Also called instruction scheduling problem.

Order the instructions of a block or region so that dependences and machine resources are respected and the run time is least.

Instruction scheduling chooses the order, and on a machine that issues several instructions per cycle the grouping, of the instructions of a program. The order must keep every dependence, and each instruction must wait until the results it reads are ready. In each cycle, the machine resources must suffice. The goal is the fewest cycles. Modern processors are pipelined: a load can take three cycles before its result is ready, while the processor could start other instructions in the meantime. A good order fills these waiting cycles with independent work. This page states the problem and proves that it is NP-complete. The scheduler pages give the methods.

In plain words

A processor works like an assembly line. It can start a new instruction every cycle, but some results take several cycles to come out. If the next instruction needs a result that is not ready, the processor waits and does nothing. The compiler can avoid many of these waits by moving instructions that do not depend on each other into the gaps. It must never move an instruction before one whose result it needs. It must also not use more adders or memory ports in one cycle than the processor has. Instruction scheduling chooses such an order with as few cycles as possible. For more than a few instructions, no fast method is known that always finds the best order.

Definition

The dependence graph of a basic block is a directed acyclic graph G=(V,A)G = (V, A) with one vertex for each instruction. There is an arc (u,v)(u, v) when vv must come after uu (see data dependence):

  • a true dependence: vv reads a value that uu writes;
  • an anti dependence: vv writes a value or a memory cell that uu reads;
  • an output dependence: both write the same value or cell.

Each arc has a delay δ(u,v)≥0\delta(u, v) \ge 0. Each instruction ii has a latency di≥1d_i \ge 1, the number of cycles until its result is ready. For a true dependence δ(u,v)=du\delta(u, v) = d_u. For anti and output dependences the delay is 0 or 1, as the machine needs.

The machine issues at most WW instructions per cycle, its issue width. Each instruction has a class c(i)c(i), such as memory or arithmetic, and at most cap(c)\mathrm{cap}(c) instructions of class cc can issue in one cycle. The units are pipelined: an instruction uses its unit only in the cycle where it issues.

A schedule gives each instruction an issue cycle ti∈{0,1,2,…}t_i \in \{0, 1, 2, \ldots\} such that:

  1. tv≥tu+δ(u,v)t_v \ge t_u + \delta(u, v) for every arc (u,v)(u, v);
  2. at most WW instructions have the same tit_i, and at most cap(c)\mathrm{cap}(c) of them have class cc.

Its length is max⁡i(ti+di)\max_i (t_i + d_i), the cycle when the last result is ready. The problem is to find a schedule of least length. In the decision form, an integer TT is also given, and the question is whether a schedule of length at most TT exists.

Unit latencies, one class and width mm give exactly the scheduling problem P∣prec,pj=1∣Cmax⁡P \mid \mathrm{prec}, p_j = 1 \mid C_{\max} of precedence-constrained scheduling: an instruction is a unit job, an issue cycle is a time slot, and the length is the makespan.

Variants

  • Local scheduling orders the instructions of one basic block, the form defined above.
  • Global or regional scheduling moves instructions across block boundaries. The region is a likely path (trace scheduling), a path with one entry (superblock scheduling), or a tree of blocks. Moving an instruction above a branch or below a join needs extra care so that the other paths still compute the same values.
  • Loop scheduling overlaps the iterations of a loop (software pipelining). The goal is a short interval between the starts of two iterations, not a short block. Iterative modulo scheduling is the main algorithm.
  • Scheduling with register pressure. Moving loads early makes more values live at once and can force spills. A scheduler before register allocation often limits the number of live values. A scheduler after allocation must also respect the anti and output dependences that register reuse adds. This is the phase ordering problem between the two.
  • Out-of-order processors reorder instructions in hardware within a window. A compiler scheduler still helps for long latencies and for in-order cores, such as many embedded and VLIW processors, where the order in the code is the order of execution.

Examples

Filling load delays. The block below reads three values. Loads have latency 3, multiplications 2 and additions 1. The machine issues one instruction per cycle.

a = load x
b = a * 3
c = load y
d = c + 1
e = b + d
f = load z
g = e * f

The arcs are a→ba \to b (delay 3), c→dc \to d (3), b→eb \to e (2), d→ed \to e (1), e→ge \to g (1) and f→gf \to g (3). An in-order processor that runs the instructions as written stalls whenever an operand is not ready. It issues a at cycle 0, b at 3, c at 4, d at 7, e at 8, f at 9 and g at 12, so the length is 12+2=1412 + 2 = 14.

A list scheduler gives each instruction its height, the longest sum of delays on a path from it to the end, plus the latency of the last instruction. The heights are aa: 8, cc: 7, bb: 5, ff: 5, dd: 4, ee: 3, gg: 2. In each cycle it issues the ready instruction of greatest height:

cycle ready issue
0 a,c,fa, c, f aa
1 c,fc, f cc
2 ff ff
3 bb bb
4 dd dd
5 ee ee
6 gg gg

The length is 6+2=86 + 2 = 8. No schedule is shorter, because the path a→b→e→ga \to b \to e \to g alone needs 3+2+1+2=83 + 2 + 1 + 2 = 8 cycles. So this order is optimal, and it is almost twice as fast as the order in the code. A machine with width 2 and one memory port gains nothing more here, for the same reason. A script found the same length 8 by trying every schedule.

The longest path rule can lose. Take six instructions on the same machine, with loads of latency 3 and additions of latency 1:

i = j + 1
u = load i
v = u + 1
w = load k
y = w + 1
z = w * 2

Here the multiplication has latency 1. The arcs are i→ui \to u (delay 1), u→vu \to v (3), w→yw \to y (3) and w→zw \to z (3). The heights are ii: 5, uu: 4, ww: 4, and 1 for the rest. The list scheduler issues ii at cycle 0, then uu and ww at cycles 1 and 2 in either order. At cycle 3 nothing is ready, so it waits, and it ends at length 7. The schedule w,i,u,y,z,vw, i, u, y, z, v at cycles 0 to 5 keeps every delay (uu at 2 and vv at 5, ww at 0 and yy, zz at 3 and 4) and has length 6, which is optimal because six instructions need six cycles. The instruction with the longest path should not go first here, because ww has two successors that can fill the gap.

Known algorithms

  • List scheduling. Build the dependence graph, give each instruction a priority, and go cycle by cycle, issuing the ready instructions of highest priority that the resources allow. It is the method of almost every compiler, often with the height as the main priority and the number of successors or the register pressure to break ties (Gibbons and Muchnick 1986). It is the list scheduling of Graham with three changes. Time goes in cycles, and an instruction is ready δ\delta cycles after its predecessor issues, not when that predecessor ends. A unit is busy for one cycle even when the result comes later. And there are several classes of units. So a cycle can be empty while every unit is free, because each ready instruction waits for a latency, and the proof of Graham's bound, which counts busy machines, does not apply as written.
  • Optimal methods. Branch and bound over the list orders, or an integer linear program with a 0-1 variable for each instruction and cycle, find the optimum for blocks of a few dozen instructions. They are used for hot code and to judge heuristics.
  • Polynomial special cases. Unit latencies on one issue slot need no scheduling at all, since any topological order has length ∣V∣|V|. With delays of 0 or 1 on one issue slot, an optimal schedule can be found in polynomial time (Bernstein and Gertner 1989). With unit latencies, one class and two issue slots, the Coffman-Graham algorithm is optimal. With unit latencies and an in-tree, Hu's algorithm is optimal for any width.
  • Regional schedulers. Trace scheduling and superblock scheduling list-schedule a long path and repair the other paths. Iterative modulo scheduling schedules loops.
AlgorithmTimeSpacePropertiesCode
Iterative modulo schedulingO(n^3 log D + I (n e + B n (n + e + P w))) worstO(n^2 + e + n w) extradeterministicnot exactofflineiterative1 language
Superblock schedulingO(m (s N + n^2)) worstO(s N + n^2) extradeterministicofflineiterative1 language
Trace schedulingO(m (s N + n^2)) worstO(s N + n^2) extradeterministicofflineiterative1 language

Bounds

Theorem 1. The decision form is in NP.

Proof. A schedule with length at most TT has every ti<Tt_i < T, so it is a certificate of polynomial size. Conditions 1 and 2 and the length are checked in time O(∣V∣+∣A∣)O(|V| + |A|) with one counter per cycle and class. □\square

Theorem 2 (Ullman 1975). The decision form is NP-complete when the issue width is part of the input, even with one class and all latencies 1.

Proof. With these restrictions, a schedule is a map from the instructions to cycles where each arc goes to a later cycle and each cycle holds at most WW instructions, and its length is the last cycle plus 1. This is word for word a schedule of unit jobs on WW identical machines with precedence constraints, and its makespan. So the two decision problems are the same. The second is NP-complete, and the reduction from clique of Lenstra and Rinnooy Kan is given on the page on precedence-constrained scheduling. □\square

Real processors have a fixed width, so Theorem 2 does not settle the case that matters. The next theorem does: one issue slot is enough for hardness once the latencies are part of the input. Hennessy and Gross (1983) stated this; here is a complete proof by a reduction from clique.

Theorem 3. The decision form is strongly NP-complete for a machine with issue width 1 and one class, when the latencies are part of the input.

Proof. Take a graph HH with nn vertices and ee edges and an integer kk with 2≤k≤n2 \le k \le n. The question is whether HH has a clique of kk vertices. Let l=k(k−1)/2l = k(k-1)/2. If l>el > e the answer is no, and we output a fixed instance with answer no. Otherwise let

L=n+e,h2=l+n−k,h3=e−l,T=2L+n+e.L = n + e, \quad h_2 = l + n - k, \quad h_3 = e - l, \quad T = 2L + n + e.

The instance has three kinds of instructions, with every delay equal to the latency of the source:

  • a vertex instruction xax_a for each vertex aa, with latency LL;
  • an edge instruction yaby_{ab} for each edge abab, with latency 1, and the arcs xa→yabx_a \to y_{ab} and xb→yabx_b \to y_{ab};
  • a chain s0→s1→⋯→s2L−1s_0 \to s_1 \to \cdots \to s_{2L-1} of 2L2L frame instructions. The delays along the chain are k+1k + 1 from s0s_0, then 1 up to sL−1s_{L-1}, then h2+1h_2 + 1 from sL−1s_{L-1}, then 1 up to s2L−2s_{2L-2}, then h3+1h_3 + 1 from s2L−2s_{2L-2}. Each frame instruction has the latency of its outgoing arc, and s2L−1s_{2L-1} has latency 1.

There are n+e+2L=Tn + e + 2L = T instructions. The delays along the chain add up to (k+1)+(L−2)+(h2+1)+(L−2)+(h3+1)=2L+k+h2+h3−1=T−1(k + 1) + (L - 2) + (h_2 + 1) + (L - 2) + (h_3 + 1) = 2L + k + h_2 + h_3 - 1 = T - 1, since k+h2+h3=n+ek + h_2 + h_3 = n + e.

Frames are fixed. In a schedule of length at most TT, every ti≤T−1t_i \le T - 1, since di≥1d_i \ge 1. With one issue slot, the TT instructions fill the cycles 00 to T−1T - 1 exactly. The chain needs t(s2L−1)−t(s0)≥T−1t(s_{2L-1}) - t(s_0) \ge T - 1, so s0s_0 is at cycle 0, s2L−1s_{2L-1} is at cycle T−1T - 1, and every chain arc is tight. So the frame takes cycle 0, the L−1L - 1 cycles from k+1k + 1 up to k+L−1k + L - 1, the L−1L - 1 cycles from k+L+h2k + L + h_2 up to k+2L+h2−2k + 2L + h_2 - 2, and cycle T−1T - 1. The free cycles form three holes: H1=[1,k]H_1 = [1, k] with kk cycles, H2=[k+L,k+L+h2−1]H_2 = [k + L, k + L + h_2 - 1] with h2h_2 cycles, and H3H_3 with h3h_3 cycles just before T−1T - 1.

No edge in H1H_1. An edge instruction issues at least LL cycles after a vertex instruction, so at cycle LL or later, and L=n+e>kL = n + e > k because n≥kn \ge k and e≥l≥1e \ge l \ge 1. So H1H_1 holds kk vertex instructions. Call their vertices KK.

Edges in H2H_2 have both ends in KK. If an end of yaby_{ab} is not in KK, its vertex instruction is at cycle k+Lk + L or later, since the cycles between H1H_1 and H2H_2 belong to the frame. Then yaby_{ab} is at cycle k+2Lk + 2L or later, which is after H2H_2, because L≥h2L \ge h_2.

A schedule gives a clique. H2H_2 holds h2=l+(n−k)h_2 = l + (n - k) instructions. Each is one of the n−kn - k vertex instructions outside KK, or an edge instruction with both ends in KK. A set of kk vertices spans at most ll edges. So H2H_2 holds exactly ll edges inside KK, and KK is a clique of size kk.

A clique gives a schedule. Put the frame as above, the clique KK in H1H_1, the other n−kn - k vertices and the ll edges inside KK in H2H_2, and the other e−le - l edges in H3H_3, each hole in any order. An edge in H2H_2 issues at k+Lk + L or later and its ends at kk or earlier. An edge in H3H_3 issues at k+2L+h2−1k + 2L + h_2 - 1 or later and its ends at k+L+h2−1k + L + h_2 - 1 or earlier. So every delay holds. The last result of a vertex instruction is ready at k+2L+h2−1≤Tk + 2L + h_2 - 1 \le T at most, since l≤e+1l \le e + 1. Every frame instruction is ready at the issue cycle of the next one, and every other instruction issues by T−2T - 2 with latency 1. So the length is at most TT.

The instance has 3(n+e)3(n + e) instructions and all numbers are at most TT, so the reduction is polynomial even when the numbers are written in unary. With Theorem 1, the problem is strongly NP-complete. □\square

A script built this instance for 43 random graphs with 3 to 5 vertices and kk from 2 to 4, and solved each with an exact search over all schedules that did not use the fixed frame. The schedule existed exactly for the 33 graphs with a clique.

Open cases. With a fixed width and latencies bounded by a constant, the complexity is not known in general. The case of width 3, one class and unit latencies is the scheduling problem P3∣prec,pj=1∣Cmax⁡P3 \mid \mathrm{prec}, p_j = 1 \mid C_{\max}, a famous open question.

Lower bounds on the length. Simple bounds still help to judge a schedule:

  • Path bound. Every schedule is at least as long as the longest path, where a path counts the delays of its arcs and the latency of its last instruction. In the first example this bound is 8 and is reached.
  • Resource bound. Every schedule has length at least ⌈∣V∣/W⌉\lceil |V| / W \rceil, and at least ⌈nc/cap(c)⌉\lceil n_c / \mathrm{cap}(c) \rceil for each class cc with ncn_c instructions. In the second example ∣V∣=6|V| = 6 gives the bound 6.
  • List schedules. A list schedule leaves no cycle empty while an instruction is ready and a unit for it is free, but it can still be longer than the optimum, as the second example shows. By Theorem 3, no rule that runs in polynomial time gives an optimal schedule on every block unless P = NP.

Reductions

  • From precedence-constrained scheduling. Unit latencies, one class and width mm give P∣prec,pj=1∣Cmax⁡P \mid \mathrm{prec}, p_j = 1 \mid C_{\max}, as in Theorem 2. Resource-constrained scheduling in high-level synthesis is the same problem with operations in place of instructions and functional units in place of issue slots.
  • From clique. Theorem 3 above.
  • To integer programming. A 0-1 variable zi,τz_{i,\tau} says that ii issues at cycle τ\tau. Each instruction has exactly one cycle, each cycle and class have a capacity, and each arc gives a linear constraint on ∑ττzi,τ\sum_\tau \tau z_{i,\tau}. Exact schedulers solve this model.

Applications

Every compiler for a pipelined processor has an instruction scheduler. It matters most for in-order and VLIW processors, where the code order is the run order, and for long-latency operations such as loads and floating-point arithmetic on any processor. The same problem appears in high-level synthesis, where operations are scheduled on hardware units, and in the scheduling of tasks with communication delays on parallel machines.

References

  1. John L. Hennessy and Thomas R. Gross, Postpass code optimization of pipeline constraints, ACM TOPLAS 5(3), 1983, 422-448, the model of a pipeline that stalls when an instruction reads a result too early, the statement that finding an optimal order with arbitrary latencies is NP-complete, and a heuristic that reorders the instructions of a basic block
  2. Philip B. Gibbons and Steven S. Muchnick, Efficient instruction scheduling for a pipelined architecture, Proceedings of the SIGPLAN '86 Symposium on Compiler Construction, 1986, 11-16, list scheduling of the dependence DAG of a basic block for a pipelined machine, with priorities from stalls, the number of successors and the length of the longest path
  3. Jeffrey D. Ullman, NP-complete scheduling problems, Journal of Computer and System Sciences 10(3), 1975, 384-393, the NP-completeness of scheduling unit-time jobs with precedence constraints on a number of processors that is part of the input
  4. Jan Karel Lenstra and Alexander H. G. Rinnooy Kan, Complexity of scheduling under precedence constraints, Operations Research 26(1), 1978, 22-35, the reduction from clique that makes it NP-complete to decide whether unit-time jobs with precedence constraints fit in three time slots
  5. David Bernstein and Izidor Gertner, Scheduling expressions on a pipelined processor with a maximal delay of one cycle, ACM TOPLAS 11(1), 1989, 57-66, an optimal polynomial algorithm for one pipelined processor when every delay is 0 or 1 cycle
  6. Krishna V. Palem and Barbara B. Simons, Scheduling time-critical instructions on RISC machines, ACM TOPLAS 15(4), 1993, 632-658, scheduling instructions with latencies and deadlines on pipelined processors, with an optimal algorithm for small latencies and bounds for greedy schedules
  7. Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 12 (Instruction Scheduling), the dependence graph, list scheduling and regional scheduling
  8. Steven S. Muchnick, Advanced Compiler Design and Implementation, Morgan Kaufmann, 1997, Chapter 17 (Code Scheduling), basic block scheduling, cross-block scheduling and software pipelining

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a contract.yaml