What is a compiler?
15-745 A compiler translates source programs Optimizing Compilers into target programs Source programs are usually in a high- Peter Lee level, human-readable form that specifies both static (compile-time) and dynamic Spring 2006 (run-time) computations Target programs are usually machine executable and specify only run-time computations
Focus of this course source compiler program
static computations This course is about optimizing compilers
target machine methods for automatically improving program the quality of the target programs, usually for faster execution dynamic computations usually based on a semantics-based results analysis of the program Compilers are fundamental “Thompson and Ritchie were among the first to A very long history of study in CS realize that hardware and compiler technology had become good enough that an entire operating Every new machine architecture defines system could be written in C, and by 1978 the standard calling conventions and comes whole environment had been successfully ported with an optimizing compiler to several machines of different types.” Chip performance is measured, in large part, by performance on programs written in C — Eric S. Raymond, “The Cathedral and the Bazaar” So as a practical matter, the compiler is an integral part of the machine architecture
You will probably write a compiler
Compilers make big, amazing, direct use of theory ideas “I’d rather have a search engine or a compiler on a deserted island than a game.” Almost all of the key ideas in their design are important in other problem domains - John Carmack, You will end up using compiler design Co-founder, id Software principles in almost every research or software development project Compilers are also fun! Compiler structure The UNCOL argument
string of characters
lexical SML x86 analyzer
sequence of tokens Java Sparc parser
symbol abstract syntax tree C MIPS table semantic analyzer OCaml PPC intermediate code
code C# ARM generator relocatable object code n!m compilers!
The UNCOL argument More realistically...
SML x86 parsing semantic lex parse actions analysis Tokens Translate Java Sparc Reductions Source program Abstract syntax
control C IR MIPS instruction translate canonicalize flow IR IR selection
Assem analysis Translate Flow Graph OCaml PPC
register code C# ARM assembler linker
allocation emission code Register Flow Graph assignment Relocatable Machine code vs n+m compilers Assembly code This course Optimization
Theory and practice of modern optimizing The most important function of a compiler compilers is static code checking/analysis major focus: analysis and optimizing But almost as important is optimization transformations of the intermediate representation Optimization was the driving force behind the modern RISC microprocessors... some on target code transformations ...and today the language+compiler is the some on run-time systems driving force behind architecture developments such as multi-core processors no lexing, parsing, typechecking
What is optimization? Our focus: Run-time performance
Informally: Transform a program into an How to improve run-time performance? equivalent but better form Reduce the number of instructions [Note: Red denotes dangerous hand-waving!] Replace “expensive” instructions with “Optimize” is a bit of a misnomer “cheap” ones the results are almost never optimal Reduce memory costs the Full Employment Theorem anyway, what is meant by “better”? Increase parallelism virtual cache memory source
front end IR
optimizer IR y e r o 8B h 32B 8KB
c main optimizer’ IR m CPU a disk c
regs me memory optimizer’’ IR 3ns 6ns 60ns Most optimizations performed on an intermediate form
Eases retargeting to multiple source and target languages larger, slower, cheaper 8ms
Ingredients for an optimization Most optimizations we can imagine or desire are not susceptible to this recipe Identify an opportunity applicable to many programs SumFrom1toN (int max) { affects key parts of programs sum = 0; amenable to “efficient enough” algorithm for (i=1; i
machine independent algebraic simplification machine dependent constant propagation jump optimization constant folding register allocation common subexpression elimination instruction scheduling dead-code elimination loop-invariant code motion strength reduction induction-variable elimination
Local optimizations Algebraic simplifications
a*1 + a Some optimizations are local, meaning a/1 + a that the legality and desirability for a a*0 + 0 statement or expression can be a+0 + a determined in isolation from the rest of the program a-0 + a a = b+1 + c=b c = a-1
Use algebraic identities to simplify arithmetic Global optimizations Dead-code elimination
The most important optimizations are debug = false; global ... if (debug) { ... } They typically require a semantics-based ... analysis of the entire procedure (or program) dataflow analysis If code will never be executed or its result and abstract interpretation effect will never be used, eliminate it
Constant propagation Constant folding
a = 5; a = 5; a = 5; b = 3; b = 3; b = 3; ...... n = a + b; n = 5 + 3; n = 5 + 3; n = 8; for (i=0; i If a and b can be determined to be constants, Local and global optimizations can trigger additional then replace them local and global optimization opportunities Redundant computations Common subexpression elimination a = c*d; a = c*d; ...... The detection and elimination of (fully or ...... partially) redundant computations is d = (c*d + t) * u; d = (a + t) * u; perhaps the major goal of an optimizer If an expression’s evaluation is redundant, then reuse the previous result Loop-invariant code motion Code motion is tricky int *a; for (i=0; i<100; ++i) int n; for (j=0; j<100; ++j) ... scanf(“%d”, &n); for (k=0; k<100; ++k) for (i=0; i<100; ++i) a[i][j][k] = i*j*k; for (i=0; i Semantics-based analyses Code-level optimizations We will spend considerable time on the theory and practice of dataflow analysis This is the major semantics-based analysis used by most optimizing compilers Some optimizations are code-level, i.e., performed on the target code (or some Some particularly difficult problems, such machine-dependent intermediate form) as predicting aliasing or heap pointer structure, seem less susceptible to dataflow analysis and often use other methods Jump optimizations Strength reduction cmp d0,d1 cmp d0,d1 beq L1 bne L2 br L2 b * 2 + b + b + lsh(b) L1: ... L1: ...... L2: ... L2: ... -1 * b + -b ...... On some processors, some operations are significantly less expensive than others Simplify jump and branch instructions Memory optimizations and parallelization Cache optimizations for (j=0; j Loop permutation can sometimes improve the spatial locality of memory accesses Course staff Peter Lee http://www.cs.cmu.edu/~petel How this course works Mike DeRosa http://www.cs.cmu.edu/~mderosa Angie Miller [email protected] Major activities Prerequisites (tentative) undergraduate architecture course attendance at lectures e.g., 15-213 ~3 optimizer tasks (teams of 2) undergraduate compiler design course significant project (teams of 2), with proposal e.g., 15-411 textbook readings proficiency in SML, OCaml, or Java research paper readings and in-class presentations programming in-class proposal and project presentations basic understanding of architecture, especially x86 either an exam or take-home worksheet Tentative grading scheme Tasks We will assign tasks to implement specific Tasks: 30% optimizations in a compiler this guarantees that you will definitely Project: 30% understand and gain experience with Exam: 20% some basics We will give you a basic compiler Participation: 20% a toy compiler, but the optimization in class; paper presentations; project tasks will be realistic presentations You will evaluate your own compiler, submit the results, and this is your grade Readings Getting the textbook Textbook: It may be possible to get through the course without the Muchnick textbook, or Steven Muchnick, “Advanced Compiler with a different textbook Design and Implementation”, Morgan Kaufmann, 1997 But we recommend that you buy a copy Reading list assignments: a few copies in the bookstore In-class presentations, as time allows also on sale on-line Reading list is forthcoming Course schedule Communication Web site forthcoming will contain schedule of lectures, tasks, etc Please send email to Mike (mderosa@cs) to get put onto the class mailing list also lots of support material and writeups, including task information