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 with one vertex for each instruction. There is an arc when must come after (see data dependence):
- a true dependence: reads a value that writes;
- an anti dependence: writes a value or a memory cell that reads;
- an output dependence: both write the same value or cell.
Each arc has a delay . Each instruction has a latency , the number of cycles until its result is ready. For a true dependence . For anti and output dependences the delay is 0 or 1, as the machine needs.
The machine issues at most instructions per cycle, its issue width. Each instruction has a class , such as memory or arithmetic, and at most instructions of class 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 such that:
- for every arc ;
- at most instructions have the same , and at most of them have class .
Its length is , the cycle when the last result is ready. The problem is to find a schedule of least length. In the decision form, an integer is also given, and the question is whether a schedule of length at most exists.
Unit latencies, one class and width give exactly the scheduling problem 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 (delay 3), (3), (2), (1), (1) and (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 .
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 : 8, : 7, : 5, : 5, : 4, : 3, : 2. In each cycle it issues the ready instruction of greatest height:
| cycle | ready | issue |
|---|---|---|
| 0 | ||
| 1 | ||
| 2 | ||
| 3 | ||
| 4 | ||
| 5 | ||
| 6 |
The length is . No schedule is shorter, because the path alone needs 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 (delay 1), (3), (3) and (3). The heights are : 5, : 4, : 4, and 1 for the rest. The list scheduler issues at cycle 0, then and 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 at cycles 0 to 5 keeps every delay ( at 2 and at 5, at 0 and , 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 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 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 . 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.
| Algorithm | Time | Space | Properties | Code |
|---|---|---|---|---|
| Iterative modulo scheduling | O(n^3 log D + I (n e + B n (n + e + P w))) worst | O(n^2 + e + n w) extra | deterministicnot exactofflineiterative | 1 language |
| Superblock scheduling | O(m (s N + n^2)) worst | O(s N + n^2) extra | deterministicofflineiterative | 1 language |
| Trace scheduling | O(m (s N + n^2)) worst | O(s N + n^2) extra | deterministicofflineiterative | 1 language |
Bounds
Theorem 1. The decision form is in NP.
Proof. A schedule with length at most has every , so it is a certificate of polynomial size. Conditions 1 and 2 and the length are checked in time with one counter per cycle and class.
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 instructions, and its length is the last cycle plus 1. This is word for word a schedule of unit jobs on 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.
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 with vertices and edges and an integer with . The question is whether has a clique of vertices. Let . If the answer is no, and we output a fixed instance with answer no. Otherwise let
The instance has three kinds of instructions, with every delay equal to the latency of the source:
- a vertex instruction for each vertex , with latency ;
- an edge instruction for each edge , with latency 1, and the arcs and ;
- a chain of frame instructions. The delays along the chain are from , then 1 up to , then from , then 1 up to , then from . Each frame instruction has the latency of its outgoing arc, and has latency 1.
There are instructions. The delays along the chain add up to , since .
Frames are fixed. In a schedule of length at most , every , since . With one issue slot, the instructions fill the cycles to exactly. The chain needs , so is at cycle 0, is at cycle , and every chain arc is tight. So the frame takes cycle 0, the cycles from up to , the cycles from up to , and cycle . The free cycles form three holes: with cycles, with cycles, and with cycles just before .
No edge in . An edge instruction issues at least cycles after a vertex instruction, so at cycle or later, and because and . So holds vertex instructions. Call their vertices .
Edges in have both ends in . If an end of is not in , its vertex instruction is at cycle or later, since the cycles between and belong to the frame. Then is at cycle or later, which is after , because .
A schedule gives a clique. holds instructions. Each is one of the vertex instructions outside , or an edge instruction with both ends in . A set of vertices spans at most edges. So holds exactly edges inside , and is a clique of size .
A clique gives a schedule. Put the frame as above, the clique in , the other vertices and the edges inside in , and the other edges in , each hole in any order. An edge in issues at or later and its ends at or earlier. An edge in issues at or later and its ends at or earlier. So every delay holds. The last result of a vertex instruction is ready at at most, since . Every frame instruction is ready at the issue cycle of the next one, and every other instruction issues by with latency 1. So the length is at most .
The instance has instructions and all numbers are at most , so the reduction is polynomial even when the numbers are written in unary. With Theorem 1, the problem is strongly NP-complete.
A script built this instance for 43 random graphs with 3 to 5 vertices and 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 , 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 , and at least for each class with instructions. In the second example 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 give , 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 says that issues at cycle . Each instruction has exactly one cycle, each cycle and class have a capacity, and each arc gives a linear constraint on . 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag JX5Y00 Edit this page History Report an error Markdown