Instruction Selection, II Tree-pattern matching

Slides:



Advertisements
Similar presentations
Tiling Examples for X86 ISA Slides Selected from Radu Ruginas CS412/413 Lecture on Instruction Selection at Cornell.
Advertisements

Intermediate Code Generation
1 Code generation Our book's target machine (appendix A): opcode source1, source2, destination add r1, r2, r3 addI r1, c, r2 loadI c, r2 load r1, r2 loadAI.
Code Generation Steve Johnson. May 23, 2005Copyright (c) Stephen C. Johnson The Problem Given an expression tree and a machine architecture, generate.
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Intermediate Representations Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
11/19/2002© 2002 Hal Perkins & UW CSEN-1 CSE 582 – Compilers Instruction Selection Hal Perkins Autumn 2002.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
Instruction Selection Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in.
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Selection Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Instruction Selection II CS 671 February 26, 2008.
Spring 2014Jim Hogg - UW - CSE - P501N-1 CSE P501 – Compiler Construction Compiler Backend Organization Instruction Selection Instruction Scheduling Registers.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Local Register Allocation & Lab Exercise 1 Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp.
Instruction Selection and Scheduling. The Problem Writing a compiler is a lot of work Would like to reuse components whenever possible Would like to automate.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Local Instruction Scheduling — A Primer for Lab 3 — Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
COMPILERS Instruction Selection hussein suleman uct csc305h 2005.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
COMPILERS Instruction Selection hussein suleman uct csc3003s 2007.
Instruction Selection, Part I Selection via Peephole Optimization Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Instruction Scheduling Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Selection CS 671 February 19, CS 671 – Spring The Back End Essential tasks: Instruction selection Map low-level IR to actual.
1 March 16, March 16, 2016March 16, 2016March 16, 2016 Azusa, CA Sheldon X. Liang Ph. D. Azusa Pacific University, Azusa, CA 91702, Tel: (800)
Introduction to Parsing
Introduction to Optimization
Instruction Selection II: Tree-pattern Matching Comp 412
COMPILERS Instruction Selection
Parsing — Part II (Top-down parsing, left-recursion removal)
Parsing III (Top-down parsing: recursive descent & LL(1) )
Constructing Precedence Table
Local Register Allocation & Lab Exercise 1
CS416 Compiler Design lec00-outline September 19, 2018
Local Instruction Scheduling
Introduction CI612 Compiler Design CI612 Compiler Design.
Instruction Selection Hal Perkins Autumn 2011
Context-sensitive Analysis
Lexical Analysis - An Introduction
Introduction to Optimization
Intermediate Representations
Introduction to Code Generation
Wrapping Up Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit.
Chapter 6 Intermediate-Code Generation
Local Instruction Scheduling — A Primer for Lab 3 —
Lecture 7: Introduction to Parsing (Syntax Analysis)
Code Shape III Booleans, Relationals, & Control flow
Intermediate Representations
The Last Lecture COMP 512 Rice University Houston, Texas Fall 2003
Introduction to Parsing
Introduction to Parsing
Context-sensitive Analysis, III Ad-hoc syntax-directed translation
Instruction Selection Hal Perkins Autumn 2005
Parsing — Part II (Top-down parsing, left-recursion removal)
Local Register Allocation & Lab Exercise 1
Introduction to Optimization
CS416 Compiler Design lec00-outline February 23, 2019
Lexical Analysis - An Introduction
Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Presentation transcript:

Instruction Selection, II Tree-pattern matching Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

The Concept Many compilers use tree-structured IRs Abstract syntax trees generated in the parser Trees or DAGs for expressions These systems might well use trees to represent target ISA Consider the ILOC add operators If we can match these “pattern trees” against IR trees, … + ri rj add ri,rj  rk + ri cj addI ri,cj  rk Operation trees

The Concept Low-level AST for w  x - 2 * y ARP: rarp NUM: constant LAB: ASM label  + VAL ARP NUM 4 - REF -26 * 2 LAB @G 12 w: at ARP+4 x: at ARP-26 Y: at @G+12

The Concept Low-level AST for w  x - 2 * y ARP: rarp NUM: constant LAB: ASM label  + - w: at ARP+4 x: at ARP-26 Y: at @G+12 VAL ARP NUM 4 REF * REF NUM 2 REF + + VAL ARP NUM -26 LAB @G NUM 12

Notation To describe these trees, we need a concise notation + ri cj Linear prefix form +(ri,cj) + ri rj +(ri,rj)

Notation To describe these trees, we need a concise notation GETS + - -(REF(REF(+(VAL2,NUM2))), *(NUM3,(REF(+(LAB1,NUM3)))))) + - *(NUM3,(REF(+(LAB1,NUM3)))))) VAL ARP NUM 4 REF * (+(VAL1,NUM1) REF NUM 2 REF (REF(REF(+(VAL2,NUM2))) + + VAL ARP NUM -26 LAB @G NUM 12 GETS(+(VAL1,NUM1), -(REF(REF(+(VAL2,NUM2))), *(NUM3,(REF(+(LAB1,NUM3))))))

Tree-pattern matching Goal is to “tile” AST with operation trees A tiling is collection of <ast,op > pairs ast is a node in the AST op is an operation tree <ast, op > means that op could implement the subtree at ast A tiling ‘implements” an AST if it covers every node in the AST and the overlap between any two trees is limited to a single node <ast, op>  tiling means ast is also covered by a leaf in another operation tree in the tiling, unless it is the root Where two operation trees meet, they must be compatible (expect the value in the same location)

Tiling the Tree Tile 6 Each tile corresponds to a sequence of operations Emitting those operations in an appropriate order implements the tree. GETS Tile 5 + - Tile 1 Tile 2 Tile 4 VAL ARP NUM 4 REF * Tile 3 REF NUM 2 REF + + VAL ARP NUM -26 LAB @G NUM 12

Generating Code Given a tiled tree Postorder treewalk, with node-dependent order for children Right child of GETS before its left child Might impose “most demanding first” rule … (Sethi ) Emit code sequence for tiles, in order Tie boundaries together with register names Tile 6 uses registers produced by tiles 1 & 5 Tile 6 emits “store rtile 5  rtile 1” Can incorporate a “real” allocator or can use “NextRegister++”

So, What’s Hard About This? Finding the matches to tile the tree Compiler writer connects operation trees to AST subtrees Provides a set of rewrite rules Encode tree syntax, in linear form Associated with each is a code template

Rewrite rules: LL Integer AST into ILOC Cost Template 1 Goal  Assign 2 Assign  GETS(Reg1,Reg2) store r2  r1 3 Assign  GETS(+(Reg1,Reg2),Reg3) storeAO r3  r1,r2 4 Assign  GETS(+(Reg1,NUM2),Reg3) storeAI r3  r1,n2 5 Assign  GETS(+(NUM1,Reg2),Reg3) storeAI r3  r2,n1 6 Reg  LAB1 loadI l1  rnew 7 Reg  VAL1 8 Reg  NUM1 loadI n1  rnew 9 Reg  REF(Reg1) load r1  rnew 10 Reg  REF(+ (Reg1,Reg2)) loadAO r1,r2  rnew 11 Reg  REF(+ (Reg1,NUM2)) loadAI r1,n2  rnew 12 Reg  REF(+ (NUM1,Reg2)) loadAI r2,n1  rnew

Rewrite rules: LL Integer AST into ILOC (part II) Cost Template 13 Reg  + (Reg1,Reg2) 1 add r1,r2  rnew 14 Reg  + (Reg1,NUM2) addI r1,n2  rnew 15 Reg  + (NUM1,Reg2) addI r2,n1  rnew 16 Reg  - (Reg1,Reg2) sub r1,r2  rnew 17 Reg  - (Reg1,NUM2) subI r1,n2  rnew 18 Reg  - (NUM1,Reg2) rsubI r2,n1  rnew 19 Reg  x (Reg1,Reg2) mult r1,r2  rnew 20 Reg  x (Reg1,NUM2) multI r1,n2  rnew 21 Reg  x (NUM1,Reg2) multI r2,n1  rnew A real set of rules would cover more than signed integers …

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example Tile 3 REF LAB @G NUM 12 +

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example What rules match tile 3? REF + LAB @G NUM 12

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example What rules match tile 3? 6: Reg  LAB1 tiles the lower left node REF + 6 LAB @G NUM 12

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example What rules match tile 3? 6: Reg  LAB1 tiles the lower left node 8: Reg  NUM1 tiles the bottom right node REF + 6 8 LAB @G NUM 12

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example What rules match tile 3? 6: Reg  LAB1 tiles the lower left node 8: Reg  NUM1 tiles the bottom right node 13: Reg  + (Reg1,Reg2) tiles the + node REF 13 + 6 8 LAB @G NUM 12

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example What rules match tile 3? 6: Reg  LAB1 tiles the lower left node 8: Reg  NUM1 tiles the bottom right node 13: Reg  + (Reg1,Reg2) tiles the + node 9: Reg  REF(Reg1) tiles the REF 9 REF 13 + 6 8 LAB @G NUM 12

So, What’s Hard About This? Need an algorithm to AST subtrees with the rules Consider tile 3 in our example What rules match tile 3? 6: Reg  LAB1 tiles the lower left node 8: Reg  NUM1 tiles the bottom right node 13: Reg  + (Reg1,Reg2) tiles the + node 9: Reg  REF(Reg1) tiles the REF 9 REF 13 + 6 8 LAB @G NUM 12 We denote this match as <6,8,13,9> Of course, it implies <8,6,13,9> Both have a cost of 4

Finding matches Many Sequences Match Our Subtree Cost Sequences 2 6,11 8,12 3 6,8,10 8,6,10 6,14,9 8,15,9 4 6,8,13,9 8,6,13,9 REF + LAB @G NUM 12 In general, we want the low cost sequence Each unit of cost is an operation (1 cycle) We should favor short sequences

Finding matches Low Cost Matches Sequences with Cost of 2 REF + LAB 6: Reg  LAB1 11: Reg  REF(+(Reg1,NUM2)) loadI @G  ri loadAI ri,12  rj 8: Reg  NUM1 12: Reg  REF(+(NUM1,Reg2)) loadI 12  ri loadAI ri,@G  rj + LAB @G NUM 12 These two are equivalent in cost 6,11 might be better, because @G may be longer than the immediate field

Tiling the Tree Still need an algorithm Assume each rule implements one operator Assume operator takes 0, 1, or 2 operands Now, …

Tiling the Tree Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) if (left(r)  Label(child(n)) else /* n is a leaf */ Label(n)  {all rules that implement n } Match binary nodes against binary rules Match unary nodes against unary rules Handle leaves with lookup in rule table

Tiling the Tree This algorithm Finds all matches in rule set Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) if (left(r)  Label(child(n)) else /* n is a leaf */ Label(n)  {all rules that implement n } This algorithm Finds all matches in rule set Labels node n with that set Can keep lowest cost match at each point Leads to a notion of local optimality — lowest cost at each point Spends its time in the two matching loops

Tiling the Tree Oversimplifications Only handles 1 storage class Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) if (left(r)  Label(child(n)) else /* n is a leaf */ Label(n)  {all rules that implement n } Oversimplifications Only handles 1 storage class Must track low cost sequence in each class Must choose lowest cost for subtree, across all classes The extensions to handle these complications are pretty straightforward.

Tiling the Tree Tile(n) Label(n)  Ø if n has two children then Tile (left child of n) Tile (right child of n) for each rule r that implements n if (left(r)  Label(left(n)) and (right(r)  Label(right(n)) then Label(n)  Label(n)  { r } else if n has one child Tile(child of n) if (left(r)  Label(child(n)) else /* n is a leaf */ Label(n)  {all rules that implement n } Can turn matching code (inner loop) into a table lookup Table can get huge and sparse |op trees| x |labels| x |labels| 200 x 1000 x 1000 leads to 200,000,000 entries Fortunately, they are quite sparse & have reasonable encodings (e.g., Chase’s work)

The Big Picture Tree patterns represent AST and ASM Can use matching algorithms to find low-cost tiling of AST Can turn a tiling into code using templates for matched rules Techniques (& tools) exist to do this efficiently Hand-coded matcher like Tile Avoids large sparse table Lots of work Encode matching as an automaton O(1) cost per node Tools like BURS, BURG Use parsing techniques Uses known technology Very ambiguous grammars Linearize tree into string and use Aho-Corasick Finds all matches

Extra Slides Start Here

Other Sequences 6,11 Two operator rule REF 6: Reg  LAB1 11: Reg  REF( + (Reg1,NUM2)) Two operator rule REF + 6 LAB @G NUM 12

Other Sequences 8,12 REF 8: Reg  NUM1 Two operator rule 12: Reg  REF( + (NUM1,Reg2)) REF Two operator rule + 8 LAB @G NUM 12

Other Sequences 10 6,8,10 6: Reg  LAB1 8: Reg  NUM1 11: Reg  REF( + (Reg1,Reg2)) REF Two operator rule + 6 8 LAB @G NUM 12 8,6,10 looks the same

Other Sequences 9 6,14,9 REF 6: Reg  LAB1 All single operator rules 14: Reg  + (Reg1,NUM2) 9: Reg  REF(Reg1) REF All single operator rules 14 + 6 LAB @G NUM 12

Other Sequences 9 8,15,9 REF 8: Reg  NUM1 All single operator rules 15: Reg  + (NUM1,Reg2) 9: Reg  REF(Reg1) REF All single operator rules 15 + 8 LAB @G NUM 12

Other Sequences 9 6,8,13,9 6: Reg  LAB1 8: Reg  NUM1 13: Reg  + (Reg1,Reg2) 9: Reg  REF(Reg1) REF All single operator rules 13 + 6 8 LAB @G NUM 12 8,6,13,9 looks the same