CS153: Compilers Lecture 15: Local Optimization

Similar documents
CS153: Compilers Lecture 16: Local Optimization II

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

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

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

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

Compiler Construction Lent Term 2013 Lectures 9 & 10 (of 16)

Compiler Construction Lent Term 2015 Lectures 10, 11 (of 16)

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

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

Compiler Code Generation COMP360

CS153: Compilers Lecture 14: Type Checking

Programming Languages Lecture 15: Recursive Types & Subtyping

Values (a.k.a. data) representation. Advanced Compiler Construction Michel Schinz

Values (a.k.a. data) representation. The problem. Values representation. The problem. Advanced Compiler Construction Michel Schinz

To figure this out we need a more precise understanding of how ML works

Running class Timing on Java HotSpot VM, 1

CS153: Compilers Lecture 7: Simple Code Generation

Programming in Haskell Aug Nov 2015

Introduction to OCaml

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CS Lecture 7: The dynamic environment. Prof. Clarkson Spring Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Compiler Optimization

Compiler Optimization Intermediate Representation

To figure this out we need a more precise understanding of how ML works

Mini-ML. CS 502 Lecture 2 8/28/08

Multi-dimensional Arrays

CSE3322 Programming Languages and Implementation

CSCI-GA Scripting Languages

Compiler construction 2009

MODEL ANSWERS COMP36512, May 2016

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

Types and Static Type Checking (Introducing Micro-Haskell)

A Functional Evaluation Model

CS 4110 Programming Languages & Logics. Lecture 35 Typed Assembly Language

A New Verified Compiler Backend for CakeML. Yong Kiam Tan* Magnus O. Myreen Ramana Kumar Anthony Fox Scott Owens Michael Norrish

CS 403 Compiler Construction Lecture 10 Code Optimization [Based on Chapter 8.5, 9.1 of Aho2]

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

Verification of an ML compiler. Lecture 3: Closures, closure conversion and call optimisations

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

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

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

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Lecture 2: SML Basics

CSE 341 Section 5. Winter 2018

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS553 Lecture Generalizing Data-flow Analysis 3

CS143 - Written Assignment 4 Reference Solutions

CS558 Programming Languages

CS 11 Ocaml track: lecture 2

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Fall Lecture 3 September 4. Stephen Brookes

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

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

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

The role of semantic analysis in a compiler

CS 406/534 Compiler Construction Putting It All Together

CS558 Programming Languages

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

CS 242. Fundamentals. Reading: See last slide

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

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

Fall 2018 Lecture N

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Intermediate Code & Local Optimizations. Lecture 20

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

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

Intermediate Code Generation

CS558 Programming Languages

LECTURE 3. Compiler Phases

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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?

CS558 Programming Languages

LECTURE 17. Expressions and Assignment

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

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

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

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

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CS558 Programming Languages

CS 4110 Programming Languages & Logics

The structure of a compiler

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS143 Compilers - Written Assignment 4

Fall 2018 Stephen Brookes. LECTURE 2 Thursday, August 30

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

CS153: Compilers Lecture 9: Data Representation and Memory Management

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Compilers. Lecture 2 Overview. (original slides by Sam

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CODE GENERATION Monday, May 31, 2010

Transcription:

CS153: Compilers Lecture 15: Local Optimization Stephen Chong https://www.seas.harvard.edu/courses/cs153

Announcements Project 4 out Due Thursday Oct 25 (2 days) Project 5 out Due Tuesday Nov 13 (21 days) Project 6 will be released today Due Tuesday Nov 20 (28 days) 2

Today Tour of many optimizations Algebraic simplification Constant folding Strength reduction Constant propagation Copy propagation Dead-code elimination Common sub-expression elimination Loop fusion, deforestation Flattening/unboxings 3

Optimization Want to rewrite code so that it's: faster, smaller, consumes less power, etc. while retaining the "observable behavior" usually: input/output behavior often need analysis to determine that a given optimization preserves behavior. often need profile information to determine that a given optimization is actually an improvement. Often have two flavors of optimization: high-level: e.g., at the AST-level (e.g., inlining) low-level: e.g., right before instruction selection (e.g., register allocation) 4

Basic Architecture Source Code Parsing Front end Elaboration Lowering Optimization Back end Code Generation Target Code 5

Tour of Optimizations Local optimizations Algebraic simplification Constant folding Strength reduction Constant propagation Copy propagation Dead-code elimination Common sub-expression elimination Loop fusion, deforestation Additional optimizations Inlining Flattening/unboxings Uncurrying... 6

Algebraic Simplification Use algebraic arithmetic identities E.g., e+0 becomes e e*1 becomes e e*0 becomes 0 etc. 7

Strength Reduction Replace powerful /expensive operations with cheaper ones E.g., x*2 becomes x+x x div 8 becomes x>>3 On many machines bit shifting is faster than multiplication and division x*15 becomes let t = x<<4 in t-x 8

Constant Folding aka delta reductions Operations on constants can be done at compile time! E.g., 3+4 becomes 7 if true then s else t becomes s 9

Copy and Constant Propagation If variable x is defined as a constant or another variable, can replace x with its definition E.g., constant propagation let x = 3 in x + x becomes 3 + 3 let foo = 4 in foo + bar becomes 4 + bar E.g., copy propagation let x = y in x + x becomes y + y 10

Dead Code Elimination Dead code = code that doesn t contribute to the program s result E.g., let x = e1 in e2 becomes e2 (if x doesn t appear in e2) 11

Common Sub-Expression Elimination Don t need to recompute the same thing multiple times! Identify and remove common subexpressions e.g., f(x+y,8+x+y) becomes let t = x+y in f(t, 8+t) 12

Deforestation Think about the execution of map g (map f l) The first map produces a new list that is consumed by the second map memory allocation, pressure on the memory bandwidth, garbage collection What if we could do map (compose f g) l instead? In general, functional programming produces lots of intermediate terms (trees) Deforestation is the removal of these intermediate trees aka fusion 13

Unboxing For uniformity, we often represent all data as pointers Allows functions like map: a list -> ( a -> b) -> b list to work on all data types, including ints, records, etc. Data represented by a pointer is called boxed Data represented directly in registers is unboxed Unboxing changes representation from pointer to value In Java this is the difference between, e.g., Integer and int What is the benefit? More efficient access to data! Can store in register rather than memory When is it applicable? Not applicable? So long as value doesn t escape (i.e., need to be passed in memory to other function, caller,...) 14

Unboxing Example function foo(x) = let y = (x, 13) in let z = (y, 14) in (bar y) + #2 z Function constructs 2 pairs, y and z y escapes (as argument to function bar) z does not escape Could unbox z to the following (enabling further optimizations) function foo(x) = let y = (x, 13) in let z1 = y in let z2 = 14 in (bar y) + z2 15

Monomorphization Polymorphic code works for many types E.g., map: a list -> ( a -> b) -> b list works for all types a and b But code could be more efficient if it were specialized for a specific type and then optimized When is it applicable? When we have polymorphic code E.g., C++ templates What are the benefits? In presence of dynamic dispatch, may be able to turn some into static dispatch May enable optimizations like unboxing What are the drawbacks? Potential for code bloat! 16

Local Optimizations Most of the optimizations we ve just seen are local optimizations They can be applied just looking locally at computation No need to understand control flow Applying one local optimization may enable more optimizations! Can just keep applying local optimizations until we can t apply any more... 17

Optimization Example let a = x ** 2 in let b = 3 in let c = x in let d = c * c in let e = b * 2 in let f = a + d in e * f let a = x * x in let d = x * x in let e = 6 in let f = a + d in e * f let a = x * x in let d = a in let e = 6 in let f = a + d in e * f Common sub-expression elimination Copy and constant propagation Strength reduction Copy and constant propagation let a = x ** 2 in let d = x * x in let e = 3 * 2 in let f = a + d in e * f Constant folding let a = x ** 2 in let d = x * x in let e = 6 in let f = a + d in e * f let a = x * x in let f = a + a in 6 * f 18

When to Perform Local Optimization? Can be done at intermediate language representation and at assembly level Local optimizations at assembly level called peephole optimizations Examine some small set of instructions and replace with different set Often very machine specific 19

When is it Safe to Rewrite? When can we safely replace e1 with e2? 1. when e1 == e2 from an input/output point of view AND 2. when e1 e2 from our improvement metrics (e.g., performance, space, power) Optimization is a misnomer; not producing optimal program. Improving program... 20

I/O Equivalence Consider let-reduction: (let x = e1 in e2) =?= (e2[x e1]) where e2[x e1] is e2 with e1 substituted for x When does this equation hold? 21

Non-Examples let x = print "hello ;2 in x+x let x = print "hello" in 3 let x = raise Foo in 3 let x = ref 3 in x :=!x + 1;!x let x = print hello in print world ; let x = foo() in x + x 22

For ML (let x = e1 in e2) =?= (e2[x e1]) Holds for sure when e1 has no observable effects. Observable effects include: diverging input/output allocating or reading/writing refs & arrays raising an exception 23

Side-Effect Free by Construction Define a syntax for expressions that are guaranteed to be sideeffect free So we can guarantee that (let x = v in e) == (e[x v]) when v is drawn from the subset of expressions: v ::= i (* constants *) x (* variables *) v op v (* binops of vals *) (v,,v) (* tuples of vals *) #i v (* select of a val *) D v (* constructors *) fun x -> e (* functions *) let x = v in v What expressions are missing from here? 24

Another Problem Variable names! Consider the following program Let s replace y with x + x... Uh oh... let x = foo() in let y = x+x in let x = bar() in y * y let x = foo() in let x = bar() in (x+x) * (x+x) 25

Variable Capture When substituting a value v for a variable x, we must make sure that none of the free variables in v are accidentally captured. A simple solution is to just rename all the variables so they are unique (throughout the program) before doing any reductions. Must be sure to preserve uniqueness. 26

Avoiding Caputre Returning to previous example Rename variables to be unique let x = foo() in let y = x+x in let x = bar() in y * y let x = foo() in let y = x+x in let z = bar() in y * y Now replacing y with x + x avoids variable capture let x = foo() in let y = x+x in let z = bar() in (x+x) * (x+x) 27

Monadic Form We will put programs into monadic form A syntactic form that lets us easily distinguish sideeffecting expressions from pure expressions Enable simpler implementations of optimizations Take CS152 to find out why it s called monadic form! Next lecture... 28