Docsity

CS553 Lecture 1: Introduction to Compiler Construction - Prof. Michelle M. Strout, Study notes of Computer Science

Toggle sidebar
bg1
CS553 Lecture 1 Introduction 1
CS553 Compiler Construction
Instructor: Michelle Strout
Computer Science Building 342
Office hours: decide in class
URL: http://www.cs.colostate.edu/~cs553
CS553 Lecture 1 Introduction 2
Plan for Today
!Introductions
!Motivation
–!Why study compilers?
!Issues
–!Look at some sample program optimizations and assorted issues
!Administrivia
–!Course details
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download CS553 Lecture 1: Introduction to Compiler Construction - Prof. Michelle M. Strout and more Study notes Computer Science in PDF only on Docsity!

CS553 Lecture 1 Introduction 1

CS553 Compiler Construction

Instructor: Michelle Strout [email protected] Computer Science Building 342 Office hours: decide in class URL: http://www.cs.colostate.edu/~cs

Plan for Today

! Introductions ! Motivation

-! Why study compilers? ! Issues -! Look at some sample program optimizations and assorted issues ! Administrivia -! Course details

CS553 Lecture 1 Introduction 3

Motivation

! What is a compiler?

-! A translator that converts a source program into an target program ! What is an optimizing compiler? -! A translator that somehow improves the program ! Why study compilers? -! They are specifically important: Compilers provide a bridge between applications and architectures -! They are generally important: Compilers encapsulate techniques for reasoning about programs and their behavior -! They are cool: First major computer application

Prelude

! Q: Who wrote the first compiler, when, and for what language? ! A: Admiral Grace Murray Hopper in 1952 ! Q: What language did it compile? ! A: A-0 (similar to 3-address code) for the UNIVAC I at Eckert- Mauchly Computer Corporation ! Q: What other interesting things did Admiral Hopper accomplish? ! A: Helped develop COBOL for the UNIVAC ! A: In 1969, awarded the first ever Computer Science “Man-of-the- Year” Award from the Data Processing Management Association. ! A: Rear Admiral in the Navy (highest rank for a woman) ! A: In 1986, at 80, oldest active duty officer in the US. ! A: In 1991, the National Medal of Technology (first woman to win) ! Quote: “It's easier to ask forgiveness than it is to get permission.”

CS553 Lecture 1 Introduction 7

Modern View of Compilers

! Analysis and translation are useful everywhere

-! Analysis and transformations can be performed at run time and link time, not just at “compile time” -! Optimization can be applied to OS as well as applications -! Analysis can be used to improve security by finding bugs -! Analysis can be used in software engineering -! Program understanding, reverse engineering, refactoring -! Debugging and testing -! Increased interaction between hardware and compilers can improve performance -! Bottom line -! Analysis and transformation play essential roles in computer systems -! Computation important! understanding computation important

Some Exciting Current Research in PLDI Research

! PLDI

-! Programming language design and implementation -! Premier conference for dissemination of compiler and programming languages research ! Parallel Programming Languages -! Most common: C/C++ or Fortran 90+ combined with MPI and/or OpenMP -! How do you do data-flow analysis for MPI programs? -! Up and coming languages and programming models -! DARPA HPCS languages: Cray’s Chapel, IBM’s X10, Sun’s Fortress -! PGAS languages like UPC and CoArray FORTRAN -! CUDA and OpenCL for programming GPUs -! Concurrent Collections: Intel and Rice University collaboration -! Alphaz for expressing programs as equations, CSU project

Yes, but can it help me get a job?

! Summer internships in past 4 years

-! LLNL with ROSE compiler (2) -! Cray with Chapel group -! NCAR looking at optimizing look up tables in Fortran 90 code -! Intel working on hand-parallelization based on compiler feedback ! Check out compilerjobs.com ! Government labs often looking for research programmers who know about compilers. ! Remember all of those new languages being developed … CS553 Lecture 1 Introduction 9

Types of Optimizations

! Definition

-! An optimization is a transformation that is expected to improve the program in some way; often consists of analysis and transformation e.g., decreasing the running time or decreasing memory requirements ! Machine-independent optimizations -! Eliminate redundant computation -! Move computation to less frequently executed place -! Specialize some general purpose code -! Remove useless code

CS553 Lecture 1 Introduction 13

Sample Optimizations (cont)

! Common subexpression elimination (CSE)

-! e.g., x = a + b; y = a + b; ! Dead (unused) assignment elimination -! e.g., x = 3; ... x not used... x = 4; ! Dead (unreachable) code elimination -! e.g., if (false == true) { printf(“debugging...”); } t = a + b; x = t; y = t; this assignment is dead this statement is dead

Sample Optimizations (cont)

! Loop-invariant code motion

-! e.g., for i = 1 to 10 do x = 3; ... ! Induction variable elimination -! e.g., for i = 1 to 10 do a[i] = a[i] + 1; ! Loop unrolling -! e.g., **for i = 1 to 10 do a[i] = a[i] + 1; x = 3; for i = 1 to 10 do ... for p = &a[1] to &a[10] do p = p + 1 for i = 1 to 10 by 2 do a[i] = a[i] + 1; a[i+1] = a[i+1] + 1;

CS553 Lecture Compiling for Parallelism & Locality 15 ! Sample code: Assume Fortran’s Column Major Order array layout ! do j = 1, ! do i = 1, ! A(j,i) = A(j,i)+ ! enddo ! enddo

More examples: Loop Permutation for Improved Locality

do i = 1, ! do j = 1, ! A(j,i) = A(j,i)+ ! enddo enddo i j poor cache locality i j good cache locality 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 28 30 1 7 13 19 25 2 8 14 20 26 3 9 15 21 27 4 10 16 22 28 5 11 17 23 29 6 12 18 24 30 CS553 Lecture Compiling for Parallelism & Locality 16

More examples: Parallelization

! Can we parallelize the following loops? ! do i = 1, ! A(i) = A(i)+ ! enddo ! do i = 1, ! A(i) = A(i-1)+ ! enddo

i Yes 1 2 3 4 5 ... i No

CS553 Lecture 1 Introduction 19

Limits of Compiler Optimizations

! Fully Optimizing Compiler (FOC)

-! FOC(P) = Popt -! Popt is the smallest program with same I/O behavior as P ! Observe -! If program Q produces no output and never halts, FOC(Q) = L: goto L ! Aha! -! We’ve solved the halting problem?! ! Moral -! Cannot build FOC -! Can always build a better optimizing compiler ( full employment theorem for compiler writers!)

Optimizations Don’t Always Help

! Common Subexpression Elimination t = a + b x = t y = t !2 adds 1 add x = a + b y = a + b !4 variables 5 variables

CS553 Lecture 1 Introduction 21 ! for i = 1 to n T[i] = A[i] + B[i] for i = 1 to n C[i] = D[i] + T[i]

Optimizations Don’t Always Help (cont)

! Fusion and Contraction t fits in a register, so no loads or stores in this loop. Huge win on most machines. Degrades performance on machines with hardware managed stream buffers. for i = 1 to n t = A[i] + B[i] C[i] = D[i] + t In Java, the address of foo() is often not known until runtime (due to dynamic class loading), so the method call requires a table lookup. After the first execution of this statement, backpatching replaces the table lookup with a direct call to the proper function. ! o.foo();

Optimizations Don’t Always Help (cont)

! Backpatching Q: How could this optimization ever hurt? A: The Pentium 4 has a trace cache, when any instruction is modified, the entire trace cache has to be flushed.

CS553 Lecture 1 Introduction 25

Beyond Optimization

! Security and Correctness

-! Can we check whether pointers and addresses are valid? -! Can we detect when untrusted code accesses a sensitive part of a system? -! Can we detect whether locks are used properly? -! Can we use compilers to certify that code is correct? -! Can we use compilers to obfuscate code?

Administrative Matters

! Turn to your syllabus ! Discuss PA0 and PA

CS553 Lecture 1 Introduction 27

Expectations

! DO

-! Expect to spend more time on this course than on a challenging undergraduate course. -! Write more than one draft for your project reports. Spelling mistakes will be penalized. Correct grammar is also expected (for help use Word or even better the Writing Center). -! Make decisions when the project is underspecified. Describe the reasoning for your decisions in the project report. -! Read assigned reading. Much of it will take more than two readings and anything in the readings might be on the midterm or final. -! Implement your projects in small pieces, thus making them easier to debug. -! Use a debugger BEFORE coming to me about problems in your implementation. -! Ask questions and come to office hours sooner rather than later. ! Thinking is important and should be done frequently.

Next Time

! Reading

-! Chapters 1 and 2 in purple dragon book ! Projects -! Take a look at project 0 (no turn in required) -! Find a partner for project 1 and get started ! Lecture -! Undergrad compiler review