Compiler Optimizations ECE 454 Computer Systems Programming Cristiana

  • Slides: 18
Download presentation
Compiler Optimizations ECE 454 Computer Systems Programming Cristiana Amza Topics: n The Role of

Compiler Optimizations ECE 454 Computer Systems Programming Cristiana Amza Topics: n The Role of the Compiler n Common Compiler (Automatic) Code Optimizations

Inside a Basic Compiler High-level language Low-level language Code Generator Front End (IA 64)

Inside a Basic Compiler High-level language Low-level language Code Generator Front End (IA 64) (C, C++, Java) HLL IR Intermediate Representation (similar to assembly) – 2– LLL

Inside an optimizing compiler High-level language Front End Optimizer Low-level language Code Generator (IA

Inside an optimizing compiler High-level language Front End Optimizer Low-level language Code Generator (IA 32) (C, C++, Java) HLL – 3– IR IR (Improved) LLL

Control Flow Graph: (how a compiler sees your program) Example IR: add … L

Control Flow Graph: (how a compiler sees your program) Example IR: add … L 1: add … branch L 2 add … L 2: add … branch L 1 return … Basic Blocks: add … Basic Block: a group of consecutive instructions with a single entry point and a single exit point – 4– L 1: add … branch L 2 add … L 2: add … branch L 1 return …

Performance Optimization: 3 Requirements 1) Preserve correctness n the speed of an incorrect program

Performance Optimization: 3 Requirements 1) Preserve correctness n the speed of an incorrect program is irrelevant 2) On average improve performance n Optimized may be worse than original if unlucky 3) Be “worth the effort” n Is this example worth it? l 1 person-year of work to implement compiler optimization l 2 x increase in compilation time l 0. 1% improvement in speed – 5–

How do optimizations improve performance? Execution_time = num_instructions * CPI n Fewer cycles per

How do optimizations improve performance? Execution_time = num_instructions * CPI n Fewer cycles per instruction ¨ Schedule instructions to avoid dependencies ¨ Improve cache/memory behavior n n – 6– Eg. , locality Fewer instructions ¨ Eg: Target special/new instructions

Role of Optimizing Compilers Provide efficient mapping of program to machine n eliminating minor

Role of Optimizing Compilers Provide efficient mapping of program to machine n eliminating minor inefficiencies n code selection and ordering register allocation n Don’t (usually) improve asymptotic efficiency n n up to programmer to select best overall algorithm big-O savings are (often) more important than constant factors l but constant factors also matter – 7–

Limitations of Optimizing Compilers Operate Under Fundamental Constraints n Must not cause any change

Limitations of Optimizing Compilers Operate Under Fundamental Constraints n Must not cause any change in program behavior under any possible condition Most analysis is performed only within procedures n inter-procedural analysis is too expensive in most cases Most analysis is based only on static information n compiler has difficulty anticipating run-time inputs When in doubt, the compiler must be conservative – 8–

Role of the Programmer How should I write my programs, given that I have

Role of the Programmer How should I write my programs, given that I have a good, optimizing compiler? Don’t: Smash Code into Oblivion n Hard to read, maintain, & assure correctness n Select best algorithm Write code that’s readable & maintainable Do: n l Procedures, recursion l Even though these factors can slow down code n Eliminate optimization blockers l Allows compiler to do its job Focus on Inner Loops n n – 9– Do detailed optimizations where code will be executed repeatedly Will get most performance gain here

Compiler Optimizations Machine independent (apply equally well to most CPUs) • • • Constant

Compiler Optimizations Machine independent (apply equally well to most CPUs) • • • Constant propagation Constant folding Common Subexpression Elimination Dead Code Elimination Loop Invariant Code Motion Function Inlining Machine dependent (apply differently to different CPUs) • • • Instruction Scheduling Loop unrolling Parallel unrolling Could do these manually, better if compiler does them • – 10 – Many optimizations make code less readable/maintainable

Constant Propagation (CP) a = 5; b = 3; : : n=5+3 n =

Constant Propagation (CP) a = 5; b = 3; : : n=5+3 n = a + b; for (i = 0 ; i < n ; ++i) { : } Replace variables with constants when possible – 11 –

Constant Folding (CF) : : n = 5 + 3; n=8 for (i =

Constant Folding (CF) : : n = 5 + 3; n=8 for (i = 0 ; i < n ; ++i) { : } 8 n Evaluate expressions containing constants n Can lead to further optimization ¨ Eg. , another round of constant propagation – 12 –

Common Sub-expression Elimination (CSE) a = c * d; : : d = (c

Common Sub-expression Elimination (CSE) a = c * d; : : d = (c * d + t) * u Þ a = c * d; : d = (a + t) * u Try to only compute a given expression once (assuming the variables have not been modified) – 13 –

Dead Code Elimination (DCE) debug = 0; // set to False : if (debug)

Dead Code Elimination (DCE) debug = 0; // set to False : if (debug) { : Þ : } a = f(b); § Compiler can determine if certain code will never execute: § Compiler will remove that code § You don’t have to worry about such code impacting performance §i. e. , – 14 – debug = 0; : : a = f(b); you are more free to have readable/debugable programs!

Loop Invariant Code Motion (LICM) for (i = 0; i < 100 ; ++i)

Loop Invariant Code Motion (LICM) for (i = 0; i < 100 ; ++i) { t 1 = a[i]; for (j = 0; j < 100 ; ++j) { tmp = i * j; t 2 = t 1[j]; for (k = 0 ; k < 100 ; ++k) { t 2[k] = tmp * k; } } } Loop invariant: value does not change across iterations LICM: move invariant code out of the loop Big performance wins: ¨ Inner loop will execute 1, 000 times ¨ Moving code out of inner loop results in big savings for (i=0; i < 100 ; ++i) { for (j=0; j < 100 ; ++j) { for (k=0 ; k < 100 ; ++k) { a[i][j][k] = i*j*k; } } } n n n – 15 – Þ

Function Inlining foo(int z){ int m = 5; return z + m; } main(){

Function Inlining foo(int z){ int m = 5; return z + m; } main(){ … x = foo(x); … } main(){ … { int foo_z = x; int m = 5; int foo_return = foo_z + m; x = foo_return; } … } main(){ … x = x + 5; … } Code size • can decrease if small procedure body and few calls • can increase if big procedure body and many calls Performance • eliminates call/return overhead • can expose potential optimizations • can be hard on instruction-cache if many copies made As a Programmer: • a good compiler should inline for best performance • feel free to use procedure calls to make your code readable!

Loop Unrolling j = 0; while (j < 100){ a[j] = b[j+1]; j +=

Loop Unrolling j = 0; while (j < 100){ a[j] = b[j+1]; j += 1; } j = 0; while (j < 99){ a[j] = b[j+1]; a[j+1] = b[j+2]; j += 2; } reduces loop overhead n n Fewer adds to update j Fewer loop condition tests enables more aggressive instruction scheduling n – 17 – more instructions for scheduler to move around

Summary: gcc Optimization Levels -g: n -O 0: n -O 1: n n -O

Summary: gcc Optimization Levels -g: n -O 0: n -O 1: n n -O 2: n -O 3: n -Os: n – 18 – Include debug information, no optimization Default, no optimization Do optimizations that don’t take too long CP, CF, CSE, DCE, LICM, inlining small functions Take longer optimizing, more aggressive scheduling Make space/speed trade-offs: loop unrolling, more inlining Optimize program size