Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 201 Compiler Construction

Similar presentations


Presentation on theme: "CS 201 Compiler Construction"— Presentation transcript:

1 CS 201 Compiler Construction
Instruction Scheduling: Trace Scheduler

2 Instruction Scheduling
Modern processors can exploit Instruction Level Parallelism (ILP) by simultaneously executing multiple instructions. Instruction scheduling influences effectiveness with which ILP is exploited. Pipelined processors (e.g., ARM): reordering of instructions avoids delays due hazards. EPIC/VLIW processors (e.g. Itanium): a single long instruction is packed with multiple operations (conventional instructions) that can be simultaneously executed.

3 Compiler Support Analyze dependences and rearrange the order of instructions, i.e. perform instruction scheduling. Pipelined: limited amount of ILP is required -- can be uncovered by reordering instructions within each basic block. EPIC/VLIW: much more ILP is required -- can be uncovered by examining code from multiple basic blocks.

4 Compiler Support Two techniques that go beyond basic block boundaries to uncover ILP: (Acyclic Schedulers) Trace Scheduling: examines a trace – a sequence of basic blocks along an acyclic program path; instruction scheduling can result in movement of instructions across basic block boundaries. (Cyclic Schedulers) Software Pipelining: examines basic blocks corresponding to consecutive loop iterations; instruction scheduling can result in movement of instructions across loop iterations.

5 Trace Scheduling A trace is a sequence of basic blocks that does not extend across loop boundaries. Select a trace Determine the instruction schedule for the trace Introduce compensation code to preserve program semantics Repeat the above steps till some part of the program is yet to be scheduled

6 Trace Selection Selection of traces is extremely important for overall performance – traces should represent paths that are executed frequently. A fast instruction schedule for one path is obtained at the expense of a slower schedule for the other path due to speculative code motion.

7 Picking Traces O – operation/instruction
Count(o) – number of times o is expected to be executed during an entire program run. Prob(e) – probability that an edge e will be executed -- important for conditional branches. Count(e) = Count(branch) x Prob(e) Counts are estimated using profiling – measure counts by running the program on a representative input.

8 Algorithm for Trace Construction
Pick an operation with the largest execution count as the seed of the trace. Grow the trace backward from the seed. Grow the trace forward from the seed. Given that p is in the trace, include s in the trace iff: Of all the edges leaving p, e has the largest execution count. Of all the edges entering s, e has the highest execution count. Same approach taken to grow the trace backward.

9 Algorithm Contd.. Trace stops growing forward when: Count(e1) < count(e2) Premature termination of trace can occur in the above algorithm. To prevent this, a slight modification is required.

10 Algorithm Contd.. Lets say A-B-C-D has been included in the current trace. Count(D-E) > Count(D-F) => add E Count(C-E) > Count(D-E) => do not add E 15 10 Premature termination occurs because the trace that can include C-E can no longer be formed because C is already in the current trace. 9 6 Modification: consider only edges P-E st P is not already part of the current trace.

11 Algorithm Contd.. Trace cannot cross loop boundaries:
if the edge encountered is a loop back edge; or if edge enters/leaves a loop then stop growing the trace. 1 & 2 cannot be placed in the same trace because the edge directly connecting them is a loop back edge and edges indirectly connecting them cross loop boundaries.

12 Instruction Scheduling
Construct a DAG for the selected trace. Generate an instruction schedule using a scheduling heuristic: list scheduling with critical path first. Following generation of the instruction schedule introduction of compensation code may be required to preserve program semantics.

13 DAG and List Scheduling
DAG – nodes are statements, edges dependences List Scheduling – critical (longest) path first i = j / 2 k = i + 4 If i<3 n = m+ 1 m = x +4 k live i = j / 2 m = x + 4 n = m+ 1 If i<3 k = i + 4 m = x + 4 i = j / 2 If i<3 k = i + 4 n = m+ 1

14 Compensation Code Consider movement of instructions across basic block boundaries, i.e. past splits and merges in the control flow graph. 1. Movement of a statement past/below a Split: compensation code i = n + 1 k = j + 4 If e k = i + 1 i = n + 1 k = j + 4 If e k = i + 1

15 Compensation Code Contd..
2. Movement of a statement above a Join: i = i + 1 d = c - 2 c = a + b c = a /2 i = i + 1 d = c - 2 c = a + b c = a / 2 compensation code

16 Compensation Code Contd..
3. Movement of a statement above a Split: i = j + 1 i = i + 2 If e k = j + 1 i = j + 1 i = i + 2 If e k = j + 1 no compensation code No compensation code introduced – speculation. Note that i=i+2 can be moved above spilt if i is dead along the off-trace path.

17 Compensation Code Contd..
4. Movement of a statement below a Join: i = i + 1 d = c - 2 c = a + b c = a /2 i = i + 1 d = c - 2 c = a + b c = a /2 illegal to move unless i=i+1 is deadcode This case will not arise assuming dead code has been removed.

18 Compensation Code Contd..
5. Movement of a branch below a split. i = i + 1 If e1 C If e2 D If e2 C i = i + 1 If e1 i = i + 1 If e1 D

19 Compensation Code Contd..
6. Movement of a branch above a join. i = j + 1 x = y + z If e D C i = j + 1 x = y + z If e D C

20 Negatives: Redundant Code
A=B+C A=B+C A=B+C

21 Negatives: Code Explosion
B1 C2 A2 B2 Cn An Bn Cn An Cn-1 An-1 C1 A1 Order of instructions along the trace after scheduling

22 Code Explosion Contd.. O(n2)  1 step O(nn)  after processing the
off-trace paths Cn An Cn-1 An-1 C2 A2 C1 A1 Bn B1 B2 C3 A3 Cn-2 An-2 Bn-1 1 trace of length n n more traces of length (n-1) created each trace will give rise to (n-1) traces of size (n-2) n + n(n-1) + n(n-1)(n-2) + …. O(nn)

23 Building a DAG for Scheduling
DAG contains the following edges: Write-After-Read data dependence Write-After-Write data dependence Read-After-Write data dependence Write-after-conditional-read edge between IF e & x=c+d to prevent movement of x=c+d above IF e. x = a-b x = c + d If e z = x+1

24 Building a DAG Contd.. 5. Condition jumps:
Introduce off-live edge between x=a-b and IF e. This edge does not constrain movement past IF e; it indicates that if x=a-b is moved past IF e then it can be eliminated from the trace but a copy must be placed along the off-trace path. x = a-b y = c+d If e z = x+1

25 Sample Problem: Introduce Compensation Code
A=B+C D=C+1 X=Y+1 If () A=A+1 Z=D+1 P=Q+1 ????? S A=B+C A=A+1 If () D=C+1 Z=D+1 P=Q+1 X=Y+1 S


Download ppt "CS 201 Compiler Construction"

Similar presentations


Ads by Google