Docsity

Control Flow III: Control Flow Optimizations - Study Notes | EECS 483, Study Guides, Projects, Research of Electrical and Electronics Engineering

Partial preview of the text

Download Control Flow III: Control Flow Optimizations - Study Notes | EECS 483 and more Study Guides, Projects, Research Electrical and Electronics Engineering in PDF only on Docsity!

Control Flow III: Control Flow Optimizations

EECS 483 – Lecture 21 University of Michigan Monday, November 20, 2006

  • 1 -

Announcements and Reading^ ™^ Project 3 – Assigned today

» Due Dec 13 - midnight » Grading will occur Dec 14/15 (thurs/fri) » Openimpact source code will be availabletonight ™ We will have class Wednes (Nov 22) ™ Exam 2 – Mon Dec 11 in class ™ Reading » Todays material not in book

  • 3 -

Compilation Steps^ ™^ 1. EDG frontend –

»^ Lexical/syntax/semantic analysis »^ Pcode is created ™ 2. Pcode flattening »^ remove complex control flow (ie &&,|| operators) ™ 3. Pcode profiling »^ Note – compilation not halted if profile fails ™ 4. Pcode inlining (disabled by default) »^ Ask me if you want to enable it ™ 5. Interprocedural pointer analysis ™ 6. Lcode generation (PtoL) » Generates *.lc files, textual assembly, this is the inputto your optimizations

  • 4 -

Compilation Steps - Continued ™^ 7. Lcode optimization, lc to O (Lopti)^ » This is what you will be changing for P3 ™^ 8.^ Lcode profiling, O to O_p ™^ 9a. Scheduling/register allocation, O_p to O_im_p^ » This essentially generates code for a “fake” machine, it’s a 1-issue Lcode machine with 64 int/64 fp registers^ » Generates cycle stats – estimate of number of cycles toexecute benchmark ignoring cache misses (see *.sumview) ™^ 9b. Lemulate – take any .lc, .O, .O_p and generate Ccode. From this compile with gcc to test functionalcorrectioness

  • 6 -

Project 3 – 3 Parts^ ™^ 0. Install and run openimpact^ ™^ 1. Implement backedge coalescing on all loops

» Loop detection provided » Many other utilities available to create blocks,^ y^

See l_code.h, l_lcode.h, l_loop.h

™^ 2. Implement 2 optimizations of your choice – tryto pick ones that you think will yield the largestperformance gains^ » You are given a baseline compiler that performs dead

code elimination, constant propagation, copypropagation, and constant folding

  • 7 -

Backedge Coalescing Example

BB2 BB4^ BB
BB
BB

BB1 b < 0 b >= 0^ BB3c <= 0^ BB c > 0

b <= 13

c <= 25^

c > 25 e < 34 d++ b++^

c++

b > 13

e >= 34 a++

e++^ BB

BB2 BB4^ BB
BB
BB

BB1 b < 0 b >= 0^ BB3c <= 0^ BB c > 0 b <= 13

c > 25c <= 25

b++^

c++

b > 13

e < 34

d++^ a++^ e >= 34

e++

  • 9 -

Loop Induction Variables^ ™^ Induction variables are variables such that everytime they changes value, they areincremented/decremented by some constant^ ™^ Basic induction variable – induction variablewhose only assignments within a loop are of theform j = j +/- C, where C is a constant^ ™^ Primary induction variable – basic inductionvariable that controls the loop execution (for i=0;i<100; i++), i (virtual register holding i) is theprimary induction variable^ ™^ Derived induction variable – variable that is alinear function of a basic induction variable

  • 10 -

Class Problem

r1 = 0r7 = &A r2 = r1 * 4r4 = r7 + 3r7 = r7 + 1r1 = load(r2)r3 = load(r4)r9 = r1 * r3r10 = r9 >> 4store (r10, r2)r1 = r1 + 4blt r1 100 Loop
Loop:

Identify the basic, primary, and derived inductions variables in this loop.

  • 12 -

Irreducible Flow Graph Example

bb1 bb2 bb

Non-reducible!

  • In C/C++, its not possible to create an irreducible flow graph without using goto’s * Cyclic graphs that are NOT natural loops cannot be optimized by the compiler L1: x = x + 1 if (x) {^ L2: y = y + 1^ if (y > 10) goto L3 } else {^ L3: z = z + 1^ if (z > 0) goto L2 }
  • 13 -

Control Flow Optis: Loop Unrolling^ ™^ Most renowned control flowopti^ ™^ Replicate the body of a loop N-1times (giving N total copies)

»^ Loop unrolled N times or Nxunrolled»^ Enable overlap of operations fromdifferent iterations»^ Increase potential for ILP(instruction level parallelism) ™ 3 variants»^ Unroll multiple of known tripcount»^ Unroll with remainder loop»^ While loop unroll

Loop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = r2 + 1blt r2 100 Loop

  • 15 -

Loop Unroll – Type 2^ r2 is the loop variable,^ Increment is?^ Initial value is?^ Final value is?^ Trip count is?^ Loop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = r2 + Xblt r2 Y Loop

Remainder loop executes the “leftover” iterations

Unrolled loop same as Type 1, and is guaranteed to execute a multiple of N times

Counted loop Some parms unknown

tc = final – initialtc = tc / incrementrem = tc % Nfin = rem * increment^ RemLoop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = r2 + Xblt r2 fin RemLoop

Loop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6 r1 = MEM[r2 + X]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = r2 + (N*X)blt r2 Y Loop

  • 16 -

Loop Unroll – Type 3^ Non-counted loop^ Some parms unknown^ pointer chasing, loop^ var modified in a strange^ way, etc.^ Loop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = MEM[r2 + 0]bne r2 0 Loop

Loop:r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = MEM[r2 + 0]beq r2 0 Exit r1 = MEM[r2 + 0]r4 = r1 * r5r6 = r4 << 2MEM[r3 + 0] = r6r2 = MEM[r2 + 0]bne r2 0 LoopExit:

Just duplicate the body, none of the loop branches can be removed. Instead they are converted into conditional breaks^ Can apply this^ to any loop!

  • 18 -

Loop Unroll Summary (2)^ ™^ Type 2 is almost as effective

» All intermediate branches removed » Remainder loop is required since trip count not known atcompile time » Need to make sure don’t spend much time in rem loop ™ Type 3 can be effective » No branches eliminated » But iteration overlap still possible » Always applicable (most loops fall into this category!) » Use average trip count to guide unroll amount

  • 19 -

Class Problem^ for (i=0; i<100; i++) {^ j = i;^ while (j < 100) {

A[j]--; } B[i] = 0;

Unroll both the outer loop and inner loop 2x. Apply the most aggressive style unrolling that you can, e.g., Type 1 if possible, else Type 2, else Type 3^ }