welcome to comp 512 comp 512, rice university1 copyright 2011, keith d. cooper & linda torczon,...

23
Welcome to COMP 512 COMP 512, Rice University 1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Comp 512 Spring 2011

Upload: sherman-elliott

Post on 04-Jan-2016

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

Welcome to COMP 512

COMP 512, Rice University

1

Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved.

Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use.

Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved.

Comp 512Spring 2011

Page 2: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

2

COMP 512

This is COMP 512 — “Advanced Compiler Construction”

• Subject Matter Compiler-based code improvement techniques

Sometimes called “optimization” Analysis required to support them No vector or multiprocessor parallelism

See COMP 515, taught by Vivek Sarkar

• Required Work Mid-term (25%), Final (25%), & Project (50%) Details of project will depend on class size

Notice: Any student with a disability requiring accommodations in this class is encouraged to contact me after class or during office hours. Students should also contact Rice’s Coordinator for Disabled Student Services

Page 3: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

3

What About Reading Materials?

• We will use many different sources Chapters 8, 9, & 10 of “Engineering a Compiler” The original papers

You will learn more if you actually read the papers Part of your education is learning to read technical papers

and think critically about their contents

• Slides from lecture will be available on the web site http://www.cs.rice.edu/~keith/512 I will try to post them before class

Your part is to read the material before coming to class

Page 4: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

4

COMP 512

My goals

• Convey a fundamental understanding of the current state-of-the-art in code optimization and code generation

• Develop a mental framework for approaching these techniques

• Differentiate between the past & the present

• Motivate current research areas (and expose dead problems)

Explicit non-goals

• Cover every transformation in the “catalog”

• Teach every data-flow analysis algorithm

• Cover issues related to multiprocessor parallelism

||’ism

Page 5: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

5

COMP 512

Rough syllabus

• Introduction to optimization Motivation & history An example compiler (Fortran H) Some simple examples (new Chapter 8, EaC)

• Data-flow analysis (Chapter 9, EaC) Iterative algorithm SSA construction

• Classical scalar optimization (Chapter 10) Taxonomy for transformations Populate the taxonomy (papers)

• Combining optimizations (papers)

• Analyzing and improving whole programs (papers)

Page 6: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

6

COMP 512

For next class read R.G. Scarborough and H.G. Kolsky, “Improved Optimization of

FORTRAN Object Programs”, IBM Journal of Research and Development, November, 1980, pages 660-676.

The point of this particular reading is to show you that a legendary optimizing compiler does not need to do every known transformation.

Instead, it can do a handful of things and do them well.

Page 7: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

7

COMP 512

Many educated computer scientists have serious misperceptions about compiler-based code optimization

• For example, see the Wikipedia entry “Compiler Optimization” Look in the history section for the page as of roughly

January 2009; it may change & should only get more accurate

You may feel compelled to provide edits to it Please register if you do; anonymous edits lack authority

• By the end of COMP 512, you will be literate in the field of scalar code optimization, to the point where you will be capable of writing a much better entry than the current one (1/1/09)

Keith – Switch to Firefox.

Page 8: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

8

COMP 512

How does optimization change the program?

Optimizer tries to

1. Eliminate overhead from language abstractions

2. Map source program onto hardware efficiently Hide hardware weaknesses, utilize hardware strengths

3. Equal the efficiency of a good assembly programmer

CompilerSource

Program

Target

Program

Page 9: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

9

COMP 512

What does optimization do?

• The compiler can produce many outputs for a given input The user might want the fastest code The user might want the smallest code The user might want the code that pages least The user might want the code that …

• Optimization tries to reshape the code to better fit the user’s goal

CompilerInput 1

Output 1

Output 3

Output 2

Output 4

Page 10: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

10

COMP 512

• Some inputs have always produced good code First Fortran compiler focused on loops PCC did well on assembly-like programs

• The compiler should provide robust optimization Small changes in the input should not produce wild changes

in the output Create (& fulfill) an expectation of excellent code quality Broaden the set of inputs that produce good code

• Routinely attain large fraction of peak performance (not 5%)

Compiler

Output 1

Output 3

Output 2

Output 4

Input 1

Input 3

Input 2

Input 4

Page 11: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

11

COMP 512

Good optimizing compilers are crafted, not assembled

• Consistent philosophy

• Careful selection of transformations

• Thorough application of those transformations

• Careful use of algorithms and data structures

• Attention to the output code

Compilers are engineered objects

• Try to minimize running time of compiled code

• Try to minimize compile time

• Try to limit use of compile-time space

• With all these constraints, results are sometimes unexpected

Page 12: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

12

COMP 512

One strategy may not work for all applications

• Compiler may need to adapt its strategies to fit specific programs Choice and order of optimizations Parameters that control decisions & transformations

• Emerging field of “autotuning” or “adaptive compilation” Compiler writer cannot predict a single answer for all

possible programs Use learning, models, or search to find good strategies

• Currently, lots of people are working in this area Many submissions to CGO & PLDI We’ll talk about some of the problems & solutions

Page 13: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

13

A quick look at real compilers

Consider inline substitution

• Replace procedure call with body of called procedure Rename to handle naming issues Widely used in optimizing OOPs

• How well do compilers handle inlined code?

Characteristics of inline substitution

• Safety: almost always safe

• Profitability: expect improvement from avoiding the overhead of a procedure call and from specialization of the code

• Opportunity: inline leaf procedures, procedures called once, others where specialization seems likely

We will talk about inlining later. For now, focus on how compilers handle inlined code.

Page 14: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

14

A quick look at real compilers

The study

• Eight programs, five compilers, five processors

• Eliminated over 99% of dynamic calls in 5 of programs

• Measured speed of original versus transformed code

• We expected uniform speed up, at least from call overhead

• What really happened?

SourceProgram Compiler

Inliner CompilerExecut

e & time

Experimental Setup

Execute & time

Five good compilers!

Page 15: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

15

A quick look at real compilers

Do you see a pattern in this data?

Page 16: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

16

A quick look at real compilers

And this happened with good compilers!

What happened?

• Input code violated assumptions made by compiler writers Longer procedures More names Different code shapes

• Exacerbated problems that are unimportant on “normal” code Imprecise analysis Algorithms that scale poorly Tradeoffs between global and local speed Limitations in the implementations

The compiler writers were surprised (most of them)

Page 17: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

17

A quick look at real compilers

One standout story

• MIPS M120/5, 16 MB of memory

• Running standalone, wanal1 took > 95 hours to compile Original code, not the transformed code 1,252 lines of Fortran (not a large

program) COMP 512 met twice during the compilation

• Running standalone with 48 MB of memory, it took < 9 minutes

• The compiler swapped for over 95 hours !?!

• For several years, wanal1 was a popular benchmark Compiler writers included it to show their compile times!

Page 18: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

18

COMP 512

Intent of this class

• Theory & practice of scalar optimization The underpinning for all modern compilers Influences the practice of computer architecture

• Learn not only “what” but also “how” and “why”

• Provide a framework for thinking about compilation

• Class will emphasize transformations

• Analysis should be driven by needs of transformations

Role of the lab

• Critically important to provide hands-on experience

• Little time pressure

Remember register windows?

Page 19: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

19

Disclaimer

Disclaimer:

The following slides contain a rough history of code optimization from 1955 to 2000. They are intended to convey to you my own impressions of what was happening in the field. They are quite subjective. They are quite incomplete. (Hundreds of papers were published during each five year period. I cannot, and did not, try to be comprehensive.) They are based on perusing conference proceedings for the various periods.

Events are listed when (in my perception) the subject came to the fore. In some cases, this is different than when the idea first appeared. For example, software pipelining was clearly invented by Glaser & Rau in 1981. That notwithstanding, the technique became widely known and understood in the latter half of the 1980’s, which is why I cited the two PLDI 88 papers.

Again, this history is neither definitive or objective.

- Keith

Page 20: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

20

A Sense of History

1955-1959

Fortran

Cobol

1960–1964

Algol 60

1965-1969

PL/I

Algol 68

Simula 67

Commercial compilers generated good code

Separation of concerns (Backus, 1956)

Control-flow graph, register allocation (Haibt, 1957)

Academics try to catch up with industrial trade secrets

Early algorithms for “code generation” (1960, 1961)

Relating theory to practice (Lavrov, 1962)

Alpha project at Novosibirsk (Ershov, 1963 & 1965)

Technology begins to spread

Fortran H (Medlock & Lowry, 1967)

Value numbering (Balke, 1967 ?)

Literature begins to emerge (Allen, 1969)

Page 21: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

21

A Sense of History

1970-1974

SETL

Smalltalk

Lisp

APL

1975-1979

Pascal

CLU

Alphard

Com. Lisp

The literature explodes and optimization grows up

Cocke & Schwartz, Allen-Cocke Catalog, 1971

Theory of analysis (Kildall, 1971, Allen & Cocke, 1972)

Interprocedural analysis (Spillman,1972)

Strength reduction, dead code elimination, Live (SETL)

Expression tree algorithms (Sethi, Aho & Ullman)

Global optimization comes of age

Full literature of data-flow analysis

Strength reduction (Cocke & Kennedy, 1977)

Partial redundancy elimination (Morel & Renvoise, 1979)

Inline substitution studies (Scheiffler, 1977, Ball, 1979)

Tail recursion elimination (Steele, 1978)

Data dependence analysis (Bannerjee, 1979)

Page 22: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

22

A Sense of History

1980-1984

Smalltalk80

ADA

Scheme

1985–1989

C++

ML

Modula-3

Programming environments and new architectures

Incremental analysis (Reps, 1982; Ryder, Zadeck, 1983)

Incremental compilation (Schwartz et al., 1984)

Interprocedural analysis (Myers, 1981; Cooper, 1984)

RISC compilers (PL.8, 1980; MIPS, 1983)

Graph coloring allocation (Chaitin, 1981; Chow, 1983)

Vectorization (Wolfe, 1982; R. Allen, 1983)

Resurgence of interest in classical optimization

Constant propagation (Wegman & Zadeck,Torczon,1985)

Code motion of control structures (Cytron et al., 1986)

Value numbering (Alpern et al., Rosen et al., 1988)

Software pipelining (Lam, Aiken & Nicolau, 1988)

Pointer analysis (Ruggeri, 1988)

SSA-form (Cytron et al., 1989)

Page 23: Welcome to COMP 512 COMP 512, Rice University1 Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice

COMP 512, Rice University

23

A Sense of History

1990-1994

Fortran 90

1995–1999

Java

Perl (?)

Architects (and memory speed) drive the process

Hierarchical allocation (Koblenz & Callahan,1991)

Scalar replacement (Carr 1991)

Cache blocking (Wolf, 1991)

Prefetch placement (Mowry, 1992)

Commercial interprocedural compilers (Convex, 1992)

The internet & SSA both come of age

JIT compilers (Everyone, from 1996 to present)

Code compression (Franz, 1995; Frasier et al., 1997; ...)

SSA-based formulations of old methods (lots of papers)

Compile to VM (Java, 1995; Bernstein, 1998; … )

Memory layout optimizations (Smith, 19??; others …)

Widespread use of analysis (pointers, omega test, …)It’s still too early for an epitaph !

*