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

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

Computer Science Building 342 Office hours: decide in class

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

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

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

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

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

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

Undergraduate Compilers in a Day

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

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

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

Tour of common optimizations

Code Optimization. Code Optimization

Introduction to Compilers

Machine-Independent Optimizations

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

CSC D70: Compiler Optimization

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

Languages and Compiler Design II IR Code Optimization

16.10 Exercises. 372 Chapter 16 Code Improvement. be translated as

ECE 486/586. Computer Architecture. Lecture # 7

Module 13: INTRODUCTION TO COMPILERS FOR HIGH PERFORMANCE COMPUTERS Lecture 25: Supercomputing Applications. The Lecture Contains: Loop Unswitching

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

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Advanced Compiler Construction

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

Intermediate Code & Local Optimizations. Lecture 20

Intermediate Code & Local Optimizations

Programming Language Processor Theory

Goals of Program Optimization (1 of 2)

CS553 Lecture Dynamic Optimizations 2

Simone Campanoni Loop transformations

Compiler Optimization and Code Generation

Loops. Announcements. Loop fusion. Loop unrolling. Code motion. Array. Good targets for optimization. Basic loop optimizations:

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Code Merge. Flow Analysis. bookkeeping

CS553 Lecture Profile-Guided Optimizations 3

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Compiler Construction 2010/2011 Loop Optimizations

We will focus on data dependencies: when an operand is written at some point and read at a later point. Example:!

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

Lecture 9 Dynamic Compilation

Running class Timing on Java HotSpot VM, 1

Compiler Construction 2016/2017 Loop Optimizations

Overview of a Compiler

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

Compiler Optimization

Other Forms of Intermediate Code. Local Optimizations. Lecture 34

Administrative. Other Forms of Intermediate Code. Local Optimizations. Lecture 34. Code Generation Summary. Why Intermediate Languages?

More Code Generation and Optimization. Pat Morin COMP 3002

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

CS 553: Algorithmic Language Compilers (PLDI) Graduate Students and Super Undergraduates... Logistics. Plan for Today

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

Local Optimization: Value Numbering The Desert Island Optimization. Comp 412 COMP 412 FALL Chapter 8 in EaC2e. target code

Compiler construction 2009

CS 406/534 Compiler Construction Putting It All Together

Compilers. Lecture 2 Overview. (original slides by Sam

Multi-dimensional Arrays

CS 132 Compiler Construction

The View from 35,000 Feet

Compiler Optimization Techniques

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

Loop Transformations! Part II!

Control Flow Analysis

Introduction to Optimization Local Value Numbering

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

Supercomputing in Plain English Part IV: Henry Neeman, Director

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CprE 488 Embedded Systems Design. Lecture 6 Software Optimization

Register Allocation. CS 502 Lecture 14 11/25/08

Compilers and Code Optimization EDOARDO FUSELLA

CS153: Compilers Lecture 15: Local Optimization

Program Optimization

Acknowledgements These slides are based on Kathryn McKinley s slides on garbage collection as well as E Christopher Lewis s slides

Enhancing Parallelism

Optimization Prof. James L. Frankel Harvard University

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

Outline. Issues with the Memory System Loop Transformations Data Transformations Prefetching Alias Analysis

Preface. Intel Technology Journal Q4, Lin Chao Editor Intel Technology Journal

Compilers and Interpreters

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

A Framework for Safe Automatic Data Reorganization

CS 2461: Computer Architecture 1

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

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

Soot A Java Bytecode Optimization Framework. Sable Research Group School of Computer Science McGill University

Calvin Lin The University of Texas at Austin

Operational Semantics of Cool

Review. Pat Morin COMP 3002

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

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

Just-In-Time Compilers & Runtime Optimizers

Systems I. Code Optimization I: Machine Independent Optimizations

Autotuning. John Cavazos. University of Delaware UNIVERSITY OF DELAWARE COMPUTER & INFORMATION SCIENCES DEPARTMENT

Code generation and local optimization

Code generation and local optimization

Compiler Optimization Intermediate Representation

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

Lecture Notes on Loop Optimizations

Transcription:

CS380C Compilers Instructor: TA: lin@cs.utexas.edu Office Hours: Mon/Wed 3:30-4:30 GDC 5.512 Jia Chen jchen@cs.utexas.edu Office Hours: Tue 3:30-4:30 Thu 3:30-4:30 GDC 5.440 January 21, 2015 Introduction 1 Today s Plan Motivation Why study compilers? Let s get started Look at some sample optimizations and assorted issues A few administrative matters Course details January 21, 2015 Introduction 2 CS380C Compilers 1

Motivation Q: Why study compilers? January 21, 2015 Introduction 3 Life B.C. Before compilers Machine Code Hardware January 21, 2015 Introduction 4 CS380C Compilers 2

Liberation Along came Backus High-level Code Hardware Compilers liberate the programmer from the machine January 21, 2015 Introduction 5 Traditional View of Compilers Translate high-level language to machine code High-level programming languages Increase programmer productivity Improve program maintenance Improve portability Low-level architectural details Instruction set Addressing modes Registers, cache, and the rest of the memory hierarchy Pipelines, instruction-level parallelism January 21, 2015 Introduction 6 CS380C Compilers 3

Optimization Translation is not enough Backus recognized the importance of obtaining good performance Can perform tedious optimizations that programmers won t do January 21, 2015 Introduction 7 Consider Matrix Multiplication Obvious code for i = 1 to n for j = 1 to n for k = 1 to n c[i,j] = c[i, j] + a[i, k]* b[k,j] Tiled code can be significantly faster for it = 1 to n by t Why don t we want for jt = 1 to n by programmers t to write this code? for kt = 1 to n by t for i = it to it+t-1 for j = jt to jt+t-1 for k = kt to kt+t-1 c[i,j] = c[i, j] + a[i, k]* b[k,j] January 21, 2015 Introduction 8 CS380C Compilers 4

Translation + Optimization Enable language design to flourish Functional languages Object oriented languages... Compilers liberate language designers Logic languages January 21, 2015 Introduction 9 Isn t Compilation A Solved Problem? Optimization for scalar machines is a problem that was solved ten years ago -- David Kuck, 1990 Languages keep changing Wacky ideas (e.g., OOP and GC) have gone mainstream Machines keep changing New features present new problems (e.g., MMX, IA64, trace caches) Changing costs lead to different concerns (e.g., loads) Applications keep changing Interactive, real-time, mobile January 21, 2015 Introduction 10 CS380C Compilers 5

Isn t Compilation A Solved Problem? (cont) Values keep changing We used to just care about run-time performance Now? Compile-time performance Code size Correctness Energy consumption Security Fault tolerance January 21, 2015 Introduction 11 Value-Added Compilation The more we rely on software, the more we demand more of it Compilers can help treat code as data Analyze the code Correctness Security January 21, 2015 Introduction 12 CS380C Compilers 6

Correctness and Security 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 verify that a given compiler transformation is correct? January 21, 2015 Introduction 13 Value-Added Compilation The more we rely on software, the more we demand more of it Compilers can help treat code as data Analyze the code Correctness Security Reliability Program understanding Program evolution Software testing Reverse engineering Program obfuscation Code compaction Energy efficiency Computation important understanding computation important January 21, 2015 Introduction 14 CS380C Compilers 7

Freedom Cuts Both Ways Just as compilers liberate the language designer, they also liberate the computer architect Can we change the ISA from one generation to the next? Yes, if we trust our compilers Enables richer design space VLIW Heterogeneous multi-core IA64 Reconfigurable architectures TRIPS Multicore January 21, 2015 Introduction 15 Benefits to the Architect (cont) Two benefits of the compiler Can simplify the hardware by shifting burden to the compiler VLIW, IA64, TRIPS, software controlled caches, Cell Can let the compiler inform the hardware Bias bits Prefetch instructions January 21, 2015 Introduction 16 CS380C Compilers 8

Virtualization is a Virtue High-level languages provide virtualization Why is virtualization good? We can virtualize at many levels Transmeta: dynamically compile x86 to VLIW GPUs rely on dynamic compilation JVMs and JITs January 21, 2015 Introduction 17 The Point Compilers are a fundamental building block of modern systems We need to understand their power and limitations Computer architects Language designers Software engineers OS/Runtime system researchers Security researchers Formal methods researchers (model checking, automated theorem proving) January 21, 2015 Introduction 18 CS380C Compilers 9

Plan For Today Motivation Why study compilers? Let s get started Look at some sample optimizations and assorted issues A few administrative matters Course details January 21, 2015 Introduction 19 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 January 21, 2015 Introduction 20 CS380C Compilers 10

Types of Optimizations (cont) Machine-dependent optimizations Replace a costly operation with a cheaper one Replace a sequence of operations with a cheaper one Hide latency Improve locality Reduce power consumption Enabling transformations Expose opportunities for other optimizations Help structure optimizations January 21, 2015 Introduction 21 Sample Optimizations Arithmetic simplification Constant folding e.g., x = 8/2; x = 4; Strength reduction e.g., x = y * 4; x = y << 2; January 21, 2015 Introduction 22 CS380C Compilers 11

Sample Optimizations (cont) Constant propagation e.g., x = 3; x = 3; x = 3; y = 4+x; y = 4+3; y = 7; Copy propagation e.g., x = z; x = z; y = 4+x; y = 4+z; January 21, 2015 Introduction 23 Sample Optimizations (cont) Common subexpression elimination (CSE) e.g., x = a + b; t = a + b; y = a + b; x = t; y = t; January 21, 2015 Introduction 24 CS380C Compilers 12

Sample Optimizations (cont) Dead (unused) assignment elimination e.g., x = 3;... x not used... This assignment is dead x = 4; Dead (unreachable) code elimination e.g., if (false == true) { printf( debugging... ); } This statement is dead January 21, 2015 Introduction 25 Sample Optimizations (cont) Loop-invariant code motion e.g., for i = 1 to 10 do x = 3;... x = 3; for i = 1 to 10 do... January 21, 2015 Introduction 26 CS380C Compilers 13

Sample Optimizations (cont) Induction variable elimination e.g., for i = 1 to 10 do a[i] = a[i] + 1; for p = &a[1] to &a[10] do *p = *p + 1 January 21, 2015 Introduction 27 Sample Optimizations (cont) Loop unrolling e.g., for i = 1 to 10 do a[i] = a[i] + 1; for i = 1 to 10 by 2 do a[i] = a[i] + 1; a[i+1] = a[i+1] + 1; January 21, 2015 Introduction 28 CS380C Compilers 14

Is an Optimization Worthwhile? Criteria for evaluating optimizations Safety: Does it preserve behavior? Profitability: Does it actually improve the code? Opportunity: Is it widely applicable? Cost (compilation time): Can it be practically performed? Cost (complexity): Can it be practically implemented? January 21, 2015 Introduction 29 Scope of Analysis/Optimizations Peephole Local Consider a small window of instructions Usually machine-specific Consider blocks of straight line code (no control flow) Simple to analyze January 21, 2015 Introduction 30 CS380C Compilers 15

Scope of Analysis/Optimizations (cont) Global (intraprocedural) Consider entire procedures Must consider branches, loops, merging of control flow Use data-flow analysis Make simplifying assumptions at procedure calls Whole program (interprocedural) Consider multiple procedures Analysis even more complex (calls, returns) Hard with separate compilation January 21, 2015 Introduction 31 Time of Optimization Compile time Link time Configuration time Runtime January 21, 2015 Introduction 32 CS380C Compilers 16

Optimization Dimensions: A Rich Space Abstraction level Machine-dependent, machine-independent Goal Performance, correctness, etc Enabling transformation Scope Peephole, local, global, interprocedural Timing Compile time, link time, configuration time,run time January 21, 2015 Introduction 33 Limits of Compiler Optimizations Fully Optimizing Compiler (FOC) FOC(P) = P opt P opt 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!) January 21, 2015 Introduction 34 CS380C Compilers 17

Optimizations Don t Always Help Common Sub-expression Elimination x = a + b y = a + b t = a + b x = t y = t 2 adds 1 add 4 variables 5 variables January 21, 2015 Introduction 35 Optimizations Don t Always Help (cont) Fusion and Contraction for i = 1 to n T[i] = A[i] + B[i] for i = 1 to n C[i] = D[i] + T[i] for i = 1 to n t = A[i] + B[i] C[i] = D[i] + t 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. January 21, 2015 Introduction 36 CS380C Compilers 18

Optimizations Don t Always Help (cont) Backpatching o.foo(); 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. Q: How could this optimization ever hurt? January 21, 2015 Introduction 37 Phase Ordering Problem In what order should optimizations be performed? Simple dependences One optimization creates opportunity for another e.g., copy propagation and dead code elimination Cyclic dependences e.g., constant folding and constant propagation Adverse interactions e.g., common sub-expression elimination and register allocation e.g., register allocation and instruction scheduling January 21, 2015 Introduction 38 CS380C Compilers 19

Engineering Issues Building a compiler is an engineering activity Balance multiple goals Benefit for typical programs Complexity of implementation Compilation speed Overall Goal Identify a small set of general analyses and optimization Easier said than done: just one more... January 21, 2015 Introduction 39 Two Approaches Which is Better? Build a compiler from scratch Clean and simple Typically implement a toy language Can only build the basics Extend an existing compiler Stand on the shoulders of others (can build more complex and complete solutions) Can do more interesting things More complex and more to learn January 21, 2015 Introduction 40 CS380C Compilers 20

Administrative Matters Turn to your syllabus January 21, 2015 Introduction 41 Next Time Reading Syllabus Lecture Undergraduate compilers in a day! January 21, 2015 Introduction 42 CS380C Compilers 21