1 cs 201 compiler construction lecture 13 instruction scheduling: trace scheduler

24
1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Post on 20-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

1

CS 201Compiler Construction

Lecture 13Instruction Scheduling:

Trace Scheduler

Page 2: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

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.

2

Page 3: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

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.

3

Page 4: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

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.

4

Page 5: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Trace Scheduling

A trace is a sequence of basic blocks that does not extend across loop boundaries.

5

• 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

Page 6: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

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.

6

Page 7: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Picking Traces

O – operation/instructionCount(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)

o Counts are estimated using profiling – measure counts by running the program on a representative input.

7

Page 8: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Algorithm for Trace Construction

1. Pick an operation with the largest execution count as the seed of the trace.

2. Grow the trace backward from the seed.3. Grow the trace forward from the seed.

8

Given that p is in the trace, include s in the trace iff:1.Of all edges leaving p, e has the largest execution count.2.Of all edges entering s, e has the highest execution count.Same approach taken to grow the trace backward.

Page 9: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Algorithm Contd..

Trace stops growing forward when:Count(e1) < count(e2)

9

Premature termination of trace can occur in the above algorithm. To prevent this, a slight modification is required.

Page 10: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Algorithm Contd..

Lets say A-B-C-D has been included in the current trace.

Count(D-E) > Count(D-F) => add ECount(C-E) > Count(D-E) => do not add

E

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.

Modification: consider only edges P-E st P is not already part of he current trace.

Page 11: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Algorithm Contd..

Trace cannot cross loop boundaries:• if the edge encountered is a loop back edge; or• if edge enters into a loopthen stop growing the trace.

11

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.

Page 12: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

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.

12

Page 13: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

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:

13

Page 14: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

2. Movement of a statement above a Join:

14

Page 15: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

15

3. Movement of a statement above a Split:

No compensation code introduced – speculation.Note that i<-i+2 can be moved above spilt if i is dead along the off-trace path.

Page 16: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

16

4. Movement of a statement below a Join:

This case will not arise assuming dead code has been removed.

Page 17: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

17

5. Movement of a branch across a split.

Page 18: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

18

6. Movement of a branch above a join.

Page 19: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

19

6. Movement of a branch above a join.

Page 20: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Compensation Code Contd..

20

7. Packing multiple branches in a long instruction.

Page 21: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Code Explosion

21

Page 22: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Code Explosion Contd..

22

Page 23: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Building a DAG for Scheduling

DAG contains the following edges:1.Write-After-Read data dependence2.Write-After-Write data dependence3.Read-After-Write data dependence4.Conditional jumps: introduce write-after-conditional-read edge between IF e & x=c+d to prevent movement of x=c+d above IF e.

23

Page 24: 1 CS 201 Compiler Construction Lecture 13 Instruction Scheduling: Trace Scheduler

Building a DAG Contd..

5. Condition jumps:– Introduce off-live edge

between x=a+b nd 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.

24