Optimizing Compilers. Vineeth Kashyap Department of Computer Science, UCSB. SIAM Algorithms Seminar, 2014

Similar documents
Introduction to Compilers

Evolution of Fortran. Presented by: Tauqeer Ahmad. Seminar on Languages for Scientific Computing

Appendix Set Notation and Concepts

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

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

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

CS 406/534 Compiler Construction Putting It All Together

Abstract Interpretation Continued

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

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

Introduction to Programming Languages and Compilers. CS164 11:00-12:30 TT 10 Evans. UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164)

Two hours UNIVERSITY OF MANCHESTER SCHOOL OF COMPUTER SCIENCE. Date: Friday 20th May 2016 Time: 14:00-16:00

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

CSC D70: Compiler Optimization

Formal Languages and Compilers Lecture I: Introduction to Compilers

Compiler Construction

CS341l Fall 2009 Test #1

Compilers and Interpreters

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

4. An interpreter is a program that

CST-402(T): Language Processors

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compilers. Optimization. Yannis Smaragdakis, U. Athens

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

CSC D70: Compiler Optimization Register Allocation

Appendix A The DL Language

CS 242. Fundamentals. Reading: See last slide

Compilers. Lecture 2 Overview. (original slides by Sam

CS187 - Science Gateway Seminar for CS and Math

Principles of Compiler Construction ( )

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

CS153: Compilers Lecture 15: Local Optimization

CS426 Compiler Construction Fall 2006

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

Static Program Analysis Part 1 the TIP language

Stating the obvious, people and computers do not speak the same language.

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

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

CSE 582 Autumn 2002 Exam 11/26/02

The View from 35,000 Feet

Pioneering Compiler Design

CS5363 Final Review. cs5363 1

Register Allocation. Preston Briggs Reservoir Labs

Lecture 3 Local Optimizations, Intro to SSA

CS 415 Midterm Exam Spring 2002

P423/P523 Compilers Register Allocation

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

CS 132 Compiler Construction

CS 44 Exam #2 February 14, 2001

An Elegant Weapon for a More Civilized Age

Thursday, December 23, The attack model: Static Program Analysis

15-411/ Compiler Design

Introduction. Compilers and Interpreters

Who are we? Andre Platzer Out of town the first week GHC TAs Alex Crichton, senior in CS and ECE Ian Gillis, senior in CS

Redundant Computation Elimination Optimizations. Redundancy Elimination. Value Numbering CS2210

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

Compiler Construction 2010/2011 Loop Optimizations

Welcome to CSE131b: Compiler Construction

Overview of a Compiler

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

CS553 Lecture Generalizing Data-flow Analysis 3

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

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

Data-Flow Analysis Foundations

Theorem proving. PVS theorem prover. Hoare style verification PVS. More on embeddings. What if. Abhik Roychoudhury CS 6214

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

Compilers for Modern Architectures Course Syllabus, Spring 2015

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Instruction Set Architectures. Part 1

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Compiler Construction 2016/2017 Loop Optimizations

Compilers. Introduction. Yannis Smaragdakis, U. Athens (original slides by Sam

Programming Language Design and Implementation. Cunning Plan. Your Host For The Semester. Wes Weimer TR 9:30-10:45 MEC 214. Who Are We?

Principles of Compiler Construction ( )

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Programming Languages Third Edition

DEPARTMENT OF COMPUTER SCIENCE

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

PROGRAMMING LANGUAGE PARADIGMS & THE MAIN PRINCIPLES OF OBJECT-ORIENTED PROGRAMMING

Compiler Construction

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Virtual Machine. Part I: Stack Arithmetic. Building a Modern Computer From First Principles.

Lecture 1: Course Introduction

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

A Gentle Introduction to Program Analysis

Program Static Analysis. Overview

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

Control in Sequential Languages

Types and Type Inference

Fall Compiler Principles Lecture 12: Register Allocation. Roman Manevich Ben-Gurion University

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

CS 352: Compilers: Principles and Practice

CS202 Compiler Construction

CS4215 Programming Language Implementation

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Principles of Program Analysis. Lecture 1 Harry Xu Spring 2013

CS131: Programming Languages and Compilers. Spring 2017

Transcription:

Optimizing Compilers Vineeth Kashyap Department of Computer Science, UCSB SIAM Algorithms Seminar, 2014

Warning!! Optimizing compiler is a misnomer

My Background Worked on a compiler team for 2 years at National Instruments R&D Worked on multiple compilers during my PhD for languages we designed Will be working on compiler-related research at GrammaTech Research starting January 2015

Credit to xkcd.com

The Beginnings [.... ] Programmers wrote assembly [1952] Grace Hopper wrote the first compiler [1957] John W. Backus created an optimizing compiler

The Success of Fortran Optimizing Compiler Programs took hours instead of weeks to write Programs became portable! Decisively won the battle against assembly Space and military projects used it extensively

FizzBuzz Credit to Rosetta Code

FizzBuzz Credit to Rosetta Code

Source Code Compilers 101

Compilers 101 Source Code Parser

Compilers 101 Source Code Parser Optimizer

Compilers 101 Source Code Parser Optimizer Code Generator

Compilers 101 Source Code Parser Optimizer Code Generator Machine Code

related.classes@cs.ucsb Compilers (CS 160) Programming Languages (CS 162) Program Analysis (CS 260) Modern Programming Languages (CS 263)

Rest of the Talk A brief bit on parsers Source Code Data flow analysis Parser Optimizer Some interesting high-level optimizations Code Generator One low level optimization Machine Code A couple of research ideas

Parser Converts text (usually) to a structured representation Also recognizes a formal language Regular languages (regular expressions) Deterministic context-free grammars

HTML cannot be parsed using regular expressions Regular expressions can only match regular languages but HTML is a context-free language

Source Code Parser Optimizer Code Generator Machine Code

Compiler Optimizations Finding equivalent programs that are hopefully better in some way It is undecidable what effect an optimization has on performance of a program It is undecidable whether an optimization is applicable! We opt for safety instead of missed optimization opportunity

Data Flow Analysis Figure out how a program manipulates its data Ex: Does a variable always hold the same constant value at a given program point? Ex: Where are the given definition of a variable used? Important enabler for a lot of optimizations

Conservative Analysis Over-approximation: can an optimization be safely performed? (no is acceptable, unless absolutely sure) False positives rather than false negatives Finds program invariants that hold across all possible executions of the program

Data Flow Analysis Primer Operates on elements of a lattice algebraic structure Solves simultaneous data flow equations Equations involve monotone computations over lattice elements Can be solved iteratively, guaranteed to terminate

Constant Propagation > 0 1 2?

Constant Propagation > a 0 1 2 b? c d

Constant Propagation > a 0 1 2 b? c d f out (n) =f(t (n), {f out (k) k 2 pred(n)})

Abstract Interpretation Can do whatever data flow analysis can do Is theoretically elegant, can provide proof of safety Mathematically relates the analysis with all executions What I use in my dissertation

Loop Invariant Code Motion while ( ) { constant := x/y; // other irrelevant code }

Loop Invariant Code Motion while ( ) { constant := x/y; // other irrelevant code } constant := x/y; while ( ) { // other irrelevant code }

Loop Invariant Code Motion while ( ) { constant := x/y; // other irrelevant code } if ( ) constant := x/y; while ( ) { // other irrelevant code }

Dead Code Elimination var x := 0; var y := 0; if (x > 0) { // dead code } print x;

Tail Call Optimization Turn recursive calls into loops! Avoids costs associated with function calls and stack allocations

What are Tail Calls? int sumton(int n) { if (n == 1) return 1; else return n + sum(n-1); } // sumton(100);

What are Tail Calls? int sumton(int n) { if (n == 1) return 1; else return n + sum(n-1); } // sumton(100); Not a tail call

What are Tail Calls? int sumton(int n) { if (n == 1) return 1; else return n + sum(n-1); } // sumton(100); Not a tail call int sumton(int n, int acc) { if (n == 1) return acc; else return sum(n-1, acc+n); } // sumton(100, 1);

What are Tail Calls? int sumton(int n) { if (n == 1) return 1; else return n + sum(n-1); } // sumton(100); Not a tail call Tail call int sumton(int n, int acc) { if (n == 1) return acc; else return sum(n-1, acc+n); } // sumton(100, 1);

Tail Call Optimized Code int sumton(int n, int acc) { if (n == 1) return acc; else return sum(n-1, acc+n); } // sumton(100, 1); int sumton(int n, int acc) { } BEGIN: if (n == 1) return acc; else { } acc = acc + n; n = n - 1; goto BEGIN;

Tail Call Optimized Code int sumton(int n, int acc) { if (n == 1) return acc; else return sum(n-1, acc+n); } // sumton(100, 1); Reassign Parameters int sumton(int n, int acc) { } BEGIN: if (n == 1) return acc; else { } acc = acc + n; n = n - 1; goto BEGIN;

Source Code Parser Optimizer Code Generator Machine Code

Register Allocation Considered the most important optimization Minimize traffic between CPU registers and memory Usually requires a low level representation

What is Register Allocation Determine which values should be in which register Registers are scarce resource RISC: almost all operations are register based CISC: register operations are much faster

Note on Register Assignment RISC: usually trivial CISC: must take into account special registers

Using Graph Coloring! Register allocation can be viewed as graph-coloring [Cocke1971] Designed and implemented at IBM [Chaitin1981]

Example Allocation x := 2 y := 4 w := x + y z := x + 1 u := x * y x := z * 2

Symbolic Registers x := 2 y := 4 w := x + y z := x + 1 u := x * y x := z * 2 s1 := 2 s2 := 4 s3 := s1 + s2 s4 := s1 + 1 s5 := s1 * s2 s6 := s4 * 2

Interference Graphs s3 s1 s5 r3 s2 s4 r1 r2 s1 := 2 s2 := 4 s3 := s1 + s2 s4 := s1 + 1 s5 := s1 * s2 s6 := s4 * 2 s6

Graph Coloring s3 s1 s5 r3 s2 s4 r1 r2 s1 := 2 s2 := 4 s3 := s1 + s2 s4 := s1 + 1 s5 := s1 * s2 s6 := s4 * 2 s6

Allocate Registers s1 := 2 s2 := 4 s3 := s1 + s2 s4 := s1 + 1 s5 := s1 * s2 s6 := s4 * 2 r1 := 2 r2 := 4 r3 := r1 + r2 r3 := r1 + 1 r1 := r1 * r2 r2 := r3 * 2 s3 s1 s5 r3 s2 s4 r1 r2 s6

Register Spilling What if the graph has a chromatic number k > number of registers?

Interesting Research Ideas Superoptimization Optimizations for reducing energy consumption

Ping me! Twitter: @vineethkashyap Email: vineeth@cs.ucsb.edu Website: http://www.cs.ucsb.edu/~vineeth/