Compiler Optimisation

Similar documents
Compiler Optimisation

Compiler Optimisation

CS415 Compilers. Lexical Analysis

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Front End. Hwansoo Han

Compiler Optimisation

A simple syntax-directed

CS 406/534 Compiler Construction Putting It All Together

CS5363 Final Review. cs5363 1

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Compiler Optimisation

Lexical Analysis. Introduction

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Intermediate Representations

CS415 Compilers. Intermediate Represeation & Code Generation

Parsing II Top-down parsing. Comp 412

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

Compiler Code Generation COMP360

2068 (I) Attempt all questions.

Semantic Analysis. Lecture 9. February 7, 2018

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CSE 582 Autumn 2002 Exam 11/26/02

A Simple Syntax-Directed Translator

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Compilers. Lecture 2 Overview. (original slides by Sam

CMSC 330: Organization of Programming Languages. Context Free Grammars

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

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

CS415 Compilers Overview of the Course. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CMSC 330: Organization of Programming Languages

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Undergraduate Compilers in a Day

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

Formal Languages and Compilers Lecture VI: Lexical Analysis

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Introduction to Compiler

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

Question Bank. 10CS63:Compiler Design

Building Compilers with Phoenix

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

List of Figures. About the Authors. Acknowledgments

COP 3402 Systems Software Syntax Analysis (Parser)

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Code Shape II Expressions & Assignment

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

CS 406/534 Compiler Construction Parsing Part I

Syntax-Directed Translation. Lecture 14

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Building Compilers with Phoenix

Intermediate Representations

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Some Thoughts on Grad School. Undergraduate Compilers Review

Time : 1 Hour Max Marks : 30

Alternatives for semantic processing

Appendix A The DL Language

CMSC 330: Organization of Programming Languages

3. Parsing. Oscar Nierstrasz

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

EECS 6083 Intro to Parsing Context Free Grammars

Top down vs. bottom up parsing

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

CS 132 Compiler Construction

CMPT 379 Compilers. Parse trees

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

Introduction to Parsing

Compiling Regular Expressions COMP360

Grammars and Parsing. Paul Klint. Grammars and Parsing

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

Introduction to Parsing. Lecture 8

CMSC 330: Organization of Programming Languages

A programming language requires two major definitions A simple one pass compiler

CMSC 330: Organization of Programming Languages. Context Free Grammars

CSCI 565 Compiler Design and Implementation Spring 2014

Compiling Techniques

The View from 35,000 Feet

Context-Free Grammars

An Overview to Compiler Design. 2008/2/14 \course\cpeg421-08s\topic-1a.ppt 1

CST-402(T): Language Processors

Intermediate Representations

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CMSC 330: Organization of Programming Languages. Context Free Grammars

Compilers and Interpreters

Project Compiler. CS031 TA Help Session November 28, 2011

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

CS 432 Fall Mike Lam, Professor. Compilers. Advanced Systems Elective

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Lexical and Syntax Analysis. Top-Down Parsing

LECTURE 7. Lex and Intro to Parsing

Parsing and Pattern Recognition

Abstract Syntax. Mooly Sagiv. html://

Formal Languages and Compilers Lecture I: Introduction to Compilers

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

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

Transcription:

Compiler Optimisation 1 Introductory Lecture Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018

Textbooks Engineering a Compiler EaC by K. D. Cooper and L. Torczon. Published by Morgan Kaufmann 2003 Optimizing Compilers for Modern Architectures: A Dependence-based Approach CMA by R. Allen and K. Kennedy. Published Morgan Kaufmann 2001 Advanced Compiler Design and Implementation by Steven S. Muchnick, published by Morgan Kaufmann. (extra reading - not required) Plus research papers in last part of course Note: Slides do not replace books. Provide motivation, concepts and examples not details.

How to get the most out of the course Read ahead including exam questions and use lectures to ask questions L1 is a recap and sets the stage. Check you are comfortable Take notes Do the course work and write well. Straightforward - schedule smartly Exam results tend to be highly bi-modal If you are struggling, ask earlier rather than later If you dont understand - its probably my fault - so ask!

Course structure L1 Introduction and Recap L2 Course Work - again updated from last year 4-5 lectures on classical optimisation (Based on EaC) 5-6 lectures on high level/parallel (Based on CMA + papers) 4-5 lectures on adaptive compilation (Based on papers) Additional lectures on course work/ revision/ external talks/ research directions

What is a compiler? Translates a program from source language to target language Often target is assembly If target is a source language then source-to-source compiler

What is a compiler? Translates a program from source language to target language Often target is assembly If target is a source language then source-to-source compiler Compare this to an interpreter

Optimisation Just translating not enough - must optimise! Not just performance - also code size, power, energy Generally undecidable, often NP-complete Gap between potential performance and actual widening Many architectural issues to think about Exploiting parallelism: instruction, thread, multi-core, accelerators Effective management of memory hierarchy registers,li,l2,l3,mem,disk Small architectural changes have big impact - hard to reason about Program optimised for CPU with Random cache replacement. What do you change for new machine with LRU?

Typical compiler structure Front end takes string of characters into abstract syntax tree Optimiser does machine independent optimisations Back end does machine dependent optimisation and code generation

Typical compiler structure Work broken into small passes or phases Different IRs used - choice affects later analysis/optimisation

Front end Front end stages Lexical Analysis - Scanner Finds and verifies basic syntactic items - lexemes, tokens using finite state automata Syntax Analysis - Parser Checks tokens follow a grammar based on a context free grammar and builds an Abstract Syntax Tree (AST) Semantic Analysis - Parser Checks all names are consistently used. Various type checking schemes employed. Attribute grammar to Milner type inference. Builds a symbol table

Lexical analysis Find keywords, identifiers, constants, etc. - these are tokens A set of rules are expressed as regular expressions (RE) Scanner automatically generated from rules 1 Transform RE NFA DFA Scanner table Example scanner rules l ( a b... z A B... Z ) digit ( 0 1... 9 ) integer digit digit real digit digit. digit digit exp digit digit. digit digit ( e E ) digit digit 1 Except in practically every real compiler, where all of this is hand coded

Lexical analysis Token scanning example How are the following classified? 0, 01, 2.6, 2., 2.6E2, and 2E20

Lexical analysis Each token has at least: Type (Keyword, LBracket, RBracket, Number, Identifier, String, etc.) Text value (and number value etc.) Source file, line number, position White space and comments are typically stripped out Error tokens may be returned

Syntactic analysis REs not powerful enough (matched parentheses, operator precedence, etc) Syntax parser described by context free grammar (often BNF) Care must be taken to avoid ambiguity Generators (YACC, BISON, ANTLR) will complain Example grammar Parse x 2 y expr term op expr term term number id op +

Syntactic analysis Parse tree for x 2 y Notice this is parsed as x (2 y) What about x 2 y?

Syntactic analysis Parse trees have irrelevant intermediate nodes Removing them gives AST Simplified parse tree for x 2 y

Syntactic analysis Arbitrary CFGs can be expensive to parse Simple dynamic programming T (n) = O(n 3 ) Restricted classes of CFG with more efficient parsers CFG classes LR(1) Left to right scan, Rightmost derivation with 1 symbol lookahead LL(1) Left to right scan, Leftmost derivation with 1 symbol lookahead; cannot handle left-recursive grammars Others a LR(k), LL(k), SLR(k), LALR(k), LR(k), IELR(k), GLR(k), LL(*), etc a Some represent the same langauges

Semantic analysis Syntactic analysis produces abstract syntax tree Program may still be invalid Semantic analysis checks correct meaning and decorates AST Symbol tables record what names refer to at different scopes Semantic actions embedded in grammar allow arbitrary code during parsing Attribute grammars propagate information around AST

Semantic analysis - symbol tables Symbol tables provide two operations lookup(name) retrieve record associated with name insert(name, record) associate record with name Stack of symbol tables manages lexical scopes Lookup searches stack recursively for name Scope example (0) char* n = "N"; (0) char* fmt = "%d"; (0) void foo() { (1) int n = 10; (2) for( int i = 0; i < n; ++i ) { (3) printf(fmt, n); (2) } (0) }

Semantic analysis - semantic actions Semantic actions allow arbitrary code to be executed during parsing Action executed only on successful parse of rule or Action provides conditional check to help parser choose between rules Side effects can cause trouble with back tracking Semantic actions decl var id = expr expr number id {symtab.insert(id.name)} {assert(symtab.exists(id.name)}

Semantic analysis - attribute grammars Attribute grammar is a CFG with: Attributes associated with each symbol Semantic rules per production to move attributes Attributes can be inherited or synthesised Semantic rules can access global data structures, such as a symbol table Attribute grammar example - types expr term op expr expr.type = F op ( term.type, expr.type ) term num id term.type = num.type id.type op + F op = F F + F

Semantic analysis - attribute grammars Attribute grammar example - x 2 y x:int, y:real, int < real F int real int int real real real real Type matrices can encode errors Example F int real double int int real double real real real double double real

Basic Code Generation Translate AST in to assembler - walk through the tree and emit code based on node type ILOC instruction set 2,3 Load constant 2 into r 2 loadi 2 r 2 Load value x into r 1 loadi @x r 1 @x is offset of x loada0 r 0, r 1 r 1 Mem[r 0 + r 1 ] r 1 Add integers r 1 = r 2 + r 3 add r 2, r 3 r 1 3 EaC Appendix A 3 Assume activation record pointer in r 0

Basic Code Generation Typical top down generator - left to right - for simple expressions Assume activation record pointer in register r 0 function gen( node ) : Register

Basic Code Generation Typical top down generator - left to right - for simple expressions Assume activation record pointer in register r 0 function gen( node ) : Register case num r = nextreg() emit(loadi value( node ) r) return r

Basic Code Generation Typical top down generator - left to right - for simple expressions Assume activation record pointer in register r 0 function gen( node ) : Register case num r = nextreg() emit(loadi value( node ) r) return r case id r = nextreg() emit( loadi offset( node ) r) emit( loada r 0, r r) return r

Basic Code Generation Typical top down generator - left to right - for simple expressions Assume activation record pointer in register r 0 function gen( node ) : Register case num r = nextreg() emit(loadi value( node ) r) return r case id r = nextreg() emit( loadi offset( node ) r) emit( loada r 0, r r) return r case binop( left, +, right ) r L = gen( left ); r R = gen( right ) emit( add r L, r R r R ) return r R

Basic Code Generation Generate code for x 2 y

Basic Code Generation Generate code for x 2 y loadi @x r 1 loada0 r 0, r 1 r 1

Basic Code Generation Generate code for x 2 y loadi @x r 1 loada0 r 0, r 1 r 1 loadi 2 r 2

Basic Code Generation Generate code for x 2 y loadi @x r 1 loada0 r 0, r 1 r 1 loadi 2 r 2 loadi @y r 3 loada0 r 0, r 3 r 3

Basic Code Generation Generate code for x 2 y loadi @x r 1 loada0 r 0, r 1 r 1 loadi 2 r 2 loadi @y r 3 loada0 r 0, r 3 r 3 mult r 2, r 3 r 3

Basic Code Generation Generate code for x 2 y loadi @x r 1 loada0 r 0, r 1 r 1 loadi 2 r 2 loadi @y r 3 loada0 r 0, r 3 r 3 mult r 2, r 3 r 3 sub r 1, r 3 r 3

Basic Code Generation Generate code for x 2 y 3 registers used loadi @x r 1 loada0 r 0, r 1 r 1 loadi 2 r 2 loadi @y r 3 loada0 r 0, r 3 r 3 mult r 2, r 3 r 3 sub r 1, r 3 r 3

Optimisation Reducing number of registers used usually good Current traversal order left to right (r L = gen( left ); r R = gen( right )) Instead traverse child needing most registers first nextreg() must know which regs unused Most registers first traversal order 2 registers used loadi @y r 1 loada0 r 0, r 1 r 1 loadi 2 r 2 mult r 2, r 1 r 1 loadi @x r 2 loada0 r 0, r 2 r 2 sub r 2, r 1 r 2

Optimisation Expression, x 2 y will have context Subtrees of expression already evaluated? Common subexpression elimination a = 2 y z b = x 2 y t = 2 y a = t z b = x t

Machine models In first part of course Assume uni-processor with instruction level parallelism, registers and memory Generated assembler should not perform any redundant computation Should utilise all available functional units and minimise impact of latency Register access is fast compared to memory but limited in number. Use wisely Two flavours considered superscalar out-of-order vs VLIW: Dynamic vs static scheduling Later consider multi-core architecture

Summary Compilation as translation and optimisation Compiler structure Phase order lexical, syntactic, semantic analysis Naive code generation and optimisation Next lecture course work Then scalar optimisation - middle end

PPar CDT Advert 4-year programme: MSc by Research + PhD Research-focused: Work on your thesis topic from the start Collaboration between: University of Edinburgh s School of Informatics Ranked top in the UK by 2014 REF Edinburgh Parallel Computing Centre UK s largest supercomputing centre Research topics in software, hardware, theory and application of: Parallelism Concurrency Distribution Full funding available Industrial engagement programme includes internships at leading companies Now accepting applications! Find out more and apply at: pervasiveparallelism.inf.ed.ac.uk The biggest revolution in the technological landscape for fifty years