CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Similar documents
Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

USC 227 Office hours: 3-4 Monday and Wednesday CS553 Lecture 1 Introduction 4

Office Hours: Mon/Wed 3:30-4:30 GDC Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Tour of common optimizations

7. Optimization! Prof. O. Nierstrasz! Lecture notes by Marcus Denker!

CS153: Compilers Lecture 15: Local Optimization

Goals of Program Optimization (1 of 2)

Compilers and Interpreters

Code Optimization. Code Optimization

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Introduction to Compilers

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

What Do Compilers Do? How Can the Compiler Improve Performance? What Do We Mean By Optimization?

COMS W4115 Programming Languages and Translators Lecture 21: Code Optimization April 15, 2013

CS 701. Class Meets. Instructor. Teaching Assistant. Key Dates. Charles N. Fischer. Fall Tuesdays & Thursdays, 11:00 12: Engineering Hall

Usually, target code is semantically equivalent to source code, but not always!

Undergraduate Compilers in a Day

Introduction to Machine-Independent Optimizations - 1

Compiler Optimization

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

More Code Generation and Optimization. Pat Morin COMP 3002

Overview of a Compiler

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

The View from 35,000 Feet

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Advanced Compiler Design. CSE 231 Instructor: Sorin Lerner

What do Compilers Produce?

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

Optimization. ASU Textbook Chapter 9. Tsan-sheng Hsu.

Compiler Optimization Intermediate Representation

Building a Runnable Program and Code Improvement. Dario Marasco, Greg Klepic, Tess DiStefano

Introduction to Code Optimization. Lecture 36: Local Optimization. Basic Blocks. Basic-Block Example

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Field Analysis. Last time Exploit encapsulation to improve memory system performance

Intermediate Code & Local Optimizations

Machine-Independent Optimizations

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

CS 406/534 Compiler Construction Putting It All Together

Administration. Prerequisites. Meeting times. CS 380C: Advanced Topics in Compilers

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Intermediate Code, Object Representation, Type-Based Optimization

Programming Language Processor Theory

Compiling Techniques

COP5621 Exam 4 - Spring 2005

Semantic Analysis. Lecture 9. February 7, 2018

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Formats of Translated Programs

CS553 Lecture Dynamic Optimizations 2

Runtime. The optimized program is ready to run What sorts of facilities are available at runtime

MIT Introduction to Program Analysis and Optimization. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Under the Compiler's Hood: Supercharge Your PLAYSTATION 3 (PS3 ) Code. Understanding your compiler is the key to success in the gaming world.

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

Supercomputing in Plain English Part IV: Henry Neeman, Director

Intermediate Code & Local Optimizations. Lecture 20

History of Compilers The term

Advanced Compiler Design. CSE 231 Instructor: Sorin Lerner

Interprocedural Analysis with Data-Dependent Calls. Circularity dilemma. A solution: optimistic iterative analysis. Example

Code Merge. Flow Analysis. bookkeeping

Compilers Crash Course

Compiler Code Generation COMP360

Compilers. Lecture 2 Overview. (original slides by Sam

Compiler Design (40-414)

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

CS 4120 and 5120 are really the same course. CS 4121 (5121) is required! Outline CS 4120 / 4121 CS 5120/ = 5 & 0 = 1. Course Information

Optimization Prof. James L. Frankel Harvard University

Review. Pat Morin COMP 3002

Loop Optimizations. Outline. Loop Invariant Code Motion. Induction Variables. Loop Invariant Code Motion. Loop Invariant Code Motion

Compiler Optimization and Code Generation

LECTURE 17. Expressions and Assignment

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

Control Flow Analysis

Advanced Compiler Construction

CPSC 510 (Fall 2007, Winter 2008) Compiler Construction II

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

The role of semantic analysis in a compiler

Interprocedural Analysis with Data-Dependent Calls. Circularity dilemma. A solution: optimistic iterative analysis. Example

Group B Assignment 8. Title of Assignment: Problem Definition: Code optimization using DAG Perquisite: Lex, Yacc, Compiler Construction

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Overview of a Compiler

Principles of Programming Languages. Lecture Outline

Outline. Programming Languages 1/16/18 PROGRAMMING LANGUAGE FOUNDATIONS AND HISTORY. Current

CS 132 Compiler Construction

ECE 486/586. Computer Architecture. Lecture # 7

Course introduction. Advanced Compiler Construction Michel Schinz

Control flow graphs and loop optimizations. Thursday, October 24, 13

Introduction to optimizations. CS Compiler Design. Phases inside the compiler. Optimization. Introduction to Optimizations. V.

Advanced Compiler Design. CSE 231 Instructor: Sorin Lerner

Just-In-Time Compilers & Runtime Optimizers

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

IR Optimization. May 15th, Tuesday, May 14, 13

Learning from Executions

Final Exam Review Questions

CS 360 Programming Languages Interpreters

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

CODE GENERATION Monday, May 31, 2010

Appendix A The DL Language

Transcription:

CSE 501: Compiler Construction Course outline Main focus: program analysis and transformation how to represent programs? how to analyze programs? what to analyze? how to transform programs? what transformations to apply? Study imperative, functional, and object-oriented languages Prerequisites: CSE 401 or equivalent CSE 505 or equivalent Reading: Appel s Modern Compiler Implementation + ~20 papers from literature Compilers: Principles, Techniques, & Tools, a.k.a. the Dragon Book, as a reference Coursework: periodic homework assignments major course project midterm + final Models of compilation Standard transformations Basic representations and analyses Fancier representations and analyses Interprocedural representations, analyses, and transformations for imperative, functional, and OO languages Representations, analyses, and transformations for parallel machines Compiler back-end issues register allocation instruction scheduling Run-time system issues garbage collection compiling dynamic dispatch, first-class functions,... Craig Chambers 1 CSE 501 Craig Chambers 2 CSE 501 Why study compilers? Goals for language implementation Meeting area of programming languages, architecture capabilities of compilers greatly influences design of these others Program representation and analysis is widely useful software engineering tools DB query optimizers programmable graphics renderers safety checking of code, e.g. in programmable/extensible systems, networks, databases Cool theoretical aspects, too lattice domains, graph algorithms, computability/complexity Opportunity for AI? Correctness Efficiency of: time, data space, code space at: compile-time, run-time Support expressive language features first-class, higher-order functions dynamic dispatching exceptions, continuations reflection, dynamic code loading... Support desirable programming environment features fast turnaround separate compilation, shared libraries source-level debugging profiling garbage collection... Craig Chambers 3 CSE 501 Craig Chambers 4 CSE 501

Standard compiler organization Analysis of input program (front-end) character stream Lexical Analysis Synthesis of output program (back-end) intermediate form Compiling to a portable intermediate language Define portable intermediate language (e.g. Java bytecode, MSIL, SUIF, WIL, C,...) Compile multiple languages into it each such compiler may not be much more than a front-end token stream Syntactic Analysis abstract syntax tree Semantic Analysis annotated AST Interpreter Optimization intermediate form Code Generation target language Compile to multiple targets from it may not be much more than back-end Maybe interpret/execute directly Advantages: reuse of front-ends and back-ends portable compiled code Interpreter Intermediate Code Generation intermediate form Design of portable intermediate language is hard how universal? across input language models? target machine models? fast interpretation and simple compilation at odds Craig Chambers 5 CSE 501 Craig Chambers 6 CSE 501 Key questions Overview of optimizations How are programs represented in the compiler? How are analyses organized/structured? Over what region of the program are analyses performed? What analysis algorithms are used? What kinds of optimizations can be performed? Which are profitable in practice? How should analyses/optimizations be sequenced/combined? How best to compile in face of: pointers, arrays first-class functions inheritance & message passing parallel target machines Other issues: speeding compilation making compilers portable, table-driven supporting tools like debuggers, profilers, garbage collect rs First analyze program to learn things about it Then transform the program based on info Repeat... Requirement: don t change the semantics! transform input program into semantically equivalent but better output program Analysis determines when transformations are: legal profitable Caveat: optimize a misnomer almost never optimal sometimes slow down some programs on some inputs (although hope to speed up most programs on most inputs) Craig Chambers 7 CSE 501 Craig Chambers 8 CSE 501

Semantics Scope of analysis Exactly what are the semantics that are to be preserved? Subtleties: evaluation order arithmetic properties like associativity, commutativity behavior in error cases Peephole: across a small number of adjacent instructions [adjacent in space or time] trivial analysis Local: within a basic block simple analysis Some languages very precise programmers always know what they re getting Others weaker allow better performance (but how much?) Semantics selected by compiler option? Intraprocedural (a.k.a. global): across basic blocks, within a procedure analysis more complex: branches, merges, loops Interprocedural: across procedures, within a whole program analysis even more complex: calls, returns sometimes useful more useful for higher-level languages hard with separate compilation Whole-program: analysis examines whole program in order to prove safety Craig Chambers 9 CSE 501 Craig Chambers 10 CSE 501 Catalog of optimizations/transformations arithmetic simplifications: constant folding x := 3 + 4 x := 7 strength reduction x := y * 4 x := y << 2 constant propagation x := 5 x := 5 x := 5 y := x + 2 y := 5 + 2 y := 7 copy propagation x := y x := y w := w + x w := w + y common subexpression elimination (CSE) x := a + b x := a + b...... y := a + b y := x can also eliminate redundant memory references, branch tests partial redundancy elimination (PRE) like CSE, but with earlier expression only available along subset of possible paths if... then if... then...... x := a + b t := a + b; x := t end else t := a + b end...... y := a + b y := x pointer/alias analysis p := &x p := &x p := &x *p := 5 *p := 5 *p := 5 y := x + 1 y := 5 + 1 y := 6 Craig Chambers 11 CSE 501 Craig Chambers 12 CSE 501

dead (unused) assignment elimination x := y ** z... // no use of x x := 6 partial dead assignment elimination like DAE, except assignment only used on some later paths Loop optimizations loop-invariant code motion for j := 1 to 10 for j := 1 to 10 for i := 1 to 10 t := b[j] a[i] := a[i] + b[j] for i := 1 to 10 a[i] := a[i] + t dead (unreachable) code elimination if false goto _else... goto _done _else:... _done: integer range analysis fold comparisons based on range analysis eliminate unreachable code for(index = 0; index < 10; index ++) { if index >= 10 goto _error a[index] := 0 } induction variable elimination for i := 1 to 10 for p := &a[1] to &a[10] a[i] := a[i] + 1 *p := *p + 1 a[i] is several instructions, *p is one loop unrolling for i := 1 to N for i := 1 to N by 4 a[i] := a[i] + 1 a[i] := a[i] + 1 a[i+1] := a[i+1] + 1 a[i+2] := a[i+2] + 1 a[i+3] := a[i+3] + 1 Craig Chambers 13 CSE 501 Craig Chambers 14 CSE 501 parallelization for i := 1 to 1000 forall i := 1 to 1000 a[i] := a[i] + 1 a[i] := a[i] + 1 loop interchange, skewing, reversal,... blocking/tiling restructuring loops for better data cache locality Call optimizations inlining l :=... l :=... l :=... w := 4 w := 4 w := 4 a := area(l,w) a := l * w a := l << 2 lots of silly optimizations become important after inlining interprocedural constant propagation, alias analysis, etc. static binding of dynamic calls in imperative languages, for call of a function pointer: if can compute unique target of pointer, can replace with direct call in functional languages, for call of a computed function: if can compute unique value of function expression, can replace with direct call in OO languages, for dynamically dispatched message: if can deduce class of receiver, can replace with direct call other possible optimizations even if several possible targets procedure specialization more generally, partial evaluation Craig Chambers 15 CSE 501 Craig Chambers 16 CSE 501

Machine-dependent optimizations The phase ordering problem register allocation instruction selection p1 := p + 4 ld %g3, [%g1 + 4] x := *p1 particularly important on CISCs instruction scheduling ld %g2, [%g1 + 0] ld %g2, [%g1 + 0] add %g3, %g2, 1 ld %g5, [%g1 + 4] ld %g2, [%g1 + 4] add %g3, %g2, 1 add %g4, %g2, 1 add %g4, %g5, 1 particularly important with instructions that have delayed results, and on wide-issue machines vs. dynamically scheduled machines? Typically, want to perform a number of optimizations; in what order should the transformations be performed? some optimizations create opportunities for other optimizations order optimizations using this dependence some optimizations simplified if can assume another opt will run later & clean up but what about cyclic dependencies? e.g. constant folding constant propagation what about adverse interactions? e.g. common subexpression elimination register allocation e.g. register allocation instruction scheduling Craig Chambers 17 CSE 501 Craig Chambers 18 CSE 501 Compilation models Separate compilation compile source files independently trivial link, load, run stages + quick recompilation after program changes poor interprocedural optimization Link-time compilation delay bulk of compilation until link-time then perform whole-program optimizations + allow interprocedural & whole-program optimizations quick recompilation? shared precompiled libraries? Examples: Vortex, some research optimizers/parallelizers,... Run-time compilation (a.k.a. dynamic, just-in-time compilation) delay bulk of compilation until run-time can perform whole-program optimizations + optimizations based on run-time program state, execution environment + best optimization potential + can handle run-time changes/extensions to the program severe pressure to limit run-time compilation overhead Examples: Java JITs, Dynamo, FX-32, Transmeta Selective run-time compilation choose what part of compilation to delay to run-time + can balance compile-time/benefit trade-offs Examples: DyC,... Hybrids of all the above spread compilation arbitrarily across stages + all the advantages, and none of the disadvantages!! Example: Whirlwind Craig Chambers 19 CSE 501 Craig Chambers 20 CSE 501

Engineering Building a compiler is an engineering activity balance complexity of implementation, speed-up of typical programs, compilation speed,... Near infinite number of special cases for optimization can be identified can t implement them all Good compiler design, like good language design, seeks small set of powerful, general analyses and transformations, to minimize implementation complexity while maximizing effectiveness reality isn t always this pure... Craig Chambers 21 CSE 501