Top Banner
Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masa ki Kohama (Tokyo Institute of Technology) Takeaki Fukuoka, Masahito Takahashi an d Ikuo Nakata
43

Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Jan 04, 2016

Download

Documents

Lynette Goodwin
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Static Single Assignment Form in the COINS Compiler Infrastructure

- Current Status and Background -

Masataka Sassa, Toshiharu Nakaya, Masaki Kohama

(Tokyo Institute of Technology)

Takeaki Fukuoka, Masahito Takahashi and Ikuo Nakata

Page 2: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

0. COINS infrastructure and the SSA form1. Current optimization using SSA form in COINS2. A comparison of SSA translation algorithms3. A comparison of SSA back translation algorithms4. A survey of compiler infrastructures

Outline

Background

Static single assignment (SSA) form facilitates compiler optimizations.Compiler infrastructure facilitates compiler development.

Page 3: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

0. COINS infrastructure andStatic Single Assignment Form (SSA Form)

Page 4: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

COINS compiler infrastructure

• Multiple source languages• Retargetable• Two intermediate form, HI

R and LIR• Optimizations• Parallelization• C generation, source-to- source translation• Written in Java• 2000~ developed by Japa

nese institutions under Grant of the Ministry

High Level Intermediate Representation (HIR)

Basic analyzer &optimizer

HIRto

LIR

Low Level Intermediate Representation (LIR) 

SSAoptimizer

Code generator

Cfrontend

Advanced optimizer

Basicparallelizer

frontend C generation

SPARC

SIMD parallelizer

x86New

machine

FortranC New language

C OpenMP

Fortran frontend

C generation

C

Page 5: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

1: a = x + y2: a = a + 33: b = x + y       

Static Single Assignment (SSA) Form

(a) Normal (conventional) form (source program or internal form)

1: a1 = x0 + y0 2: a2 = a1 + 3 3: b1 = x0 + y0

       (b) SSA form

SSA form is a recently proposed internal representation where each use of a variable has a single definition point.

Indices are attached to variables so that their definitions become unique.

Page 6: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

1: a = x + y2: a = a + 33: b = x + y       

Optimization in Static Single Assignment (SSA) Form

(a) Normal form

1: a1 = x0 + y0 2: a2 = a1 + 3 3: b1 = x0 + y0

       (b) SSA form

1: a1 = x0 + y0 2: a2 = a1 + 3 3: b1 = a1

(c) After SSA form optimization

1: a1 = x0 + y0 2: a2 = a1 + 3 3: b1 = a1

(d) Optimized normal form

SSA translation

Optimization in SSA form (common subexpression elimination)

SSA back translation

SSA form is becoming increasingly popular in compilers, since it is suited for clear handling of dataflow analysis and optimization.

Page 7: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x1 = 1 x2 = 2

x3 = (x1;L1, x2:L2)… = x3

x = 1 x = 2

… = xL3

L2L1 L1 L2

L3

(b) SSA form(a) Normal form

Translating into SSA form (SSA translation)

Page 8: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x1 = 1 x2 = 2

x3 = (x1;L1, x2:L2)… = x3

x1 = 1x3 = x1

x2 = 2x3 = x2

… = x3L3

L2L1L1 L2

L3

(a) SSA form (b) Normal form

Translating back from SSA form (SSA back translation)

Page 9: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

1. SSA form module in the COINS compiler infrastructure

Page 10: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

High Level Intermediate Representation (HIR)

Basic analyzer &optimizer

HIRto

LIR

Low Level Intermediate Representation (LIR) 

SSAoptimizer

Code generator

Cfrontend

Advanced optimizer

Basicparallelizer

frontend C generation

SPARC

SIMD parallelizer

x86 Newmachine

FortranC New language

C OpenMP

Fortran frontend

COINS compiler infrastructure

C generation

C

Page 11: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Low level Intermediate Representation (LIR)

SSA optimization module

Code generation

Source program

object code

LIR to SSAtranslation

(3 variations)

LIR in SSA

SSA basic optimization com subexp elimination copy propagation cond const propagation dead code elimination

Optimized LIR in SSA

SSA to LIR back translation

(2 variations)+ 2 coalescing 12,000 lines

transformation on SSA copy folding dead phi elim edge splitting

SSA optimization module in COINS

Page 12: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Outline of SSA module in COINS• Translation into and back from SSA form on Low Level

Intermediate Representation (LIR)‐ SSA translation: Use dominance frontier [Cytron et al. 91]‐ SSA back translation: [Sreedhar et al. 99]‐ Basic optimization on SSA form: dead code elimination, copy prop

agation, common subexpression elimination, conditional constant propagation

• Useful transformation as an infrastructure for SSA form optimization

‐ Copy folding at SSA translation time, critical edge removal on control flow graph …

‐ Each variation and transformation can be made selectively• Preliminary result

‐ 1.43 times faster than COINS w/o optimization‐ 1.25 times faster than gcc w/o optimization

Page 13: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

2. A comparison of two major algorithms for SSA translation

•Algorithm by Cytron [1991] Dominance frontier•Algorithm by Sreedhar [1995] DJ-graph

Comparison made to decide the algorithm to be included in COINS

Page 14: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x1 = 1 x2 = 2

x3 = (x1;L1, x2:L2)… = x3

x = 1 x = 2

… = xL3

L2L1 L1 L2

L3

(b) SSA form(a) Normal form

Translating into SSA form (SSA translation)

Page 15: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

0

100

200

300

400

500

600

700

800

900

0 1000 2000 3000 4000No. of nodes of control flow graph

Tra

nsl

atio

n t

ime

(mil

li s

ec)

CytronSreedhar

SSA translation time (usual programs)

(The gap is due to the garbage collection)

Page 16: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

(b) ladder graph(a) nested loop

Peculiar programs

Page 17: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

0 1000 2000 3000 4000

No. of nodes of control flow graph

Tra

nsl

atio

n t

ime

(m

illi

sec

)

CytronSreedhar

SSA translation time (nested loop programs)

Page 18: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

0

500

1000

1500

2000

2500

3000

3500

0 1000 2000 3000 4000

No. of nodes of control flow graph

Tra

nsl

atio

n t

ime

(mil

li s

ec)

CytronSreedhar

SSA translation time (ladder graph programs)

Page 19: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

3. A comparison of two major algorithms for SSA back translation

• Algorithm by Briggs [1998] Insert copy statements• Algorithm by Sreedhar [1999] Eliminate interference

There have been no studies of comparisonComparison made on COINS

Page 20: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x1 = 1 x2 = 2

x3 = (x1;L1, x2:L2)… = x3

x1 = 1x3 = x1

x2 = 2x3 = x2

… = x3L3

L2L1L1 L2

L3

(a) SSA form (b) Normal form

Translating back from SSA form (SSA back translation)

Page 21: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x0 = 1

x1 =  (x0, x2)y = x1x2 = 2

return y

x0 = 1

x1 =  (x0, x2)

x2 = 2

return x1

x0 = 1x1 = x0

x2 = 2

x1 = x2

return x1

not correct

block1

block3

block2

block1

block3

block2

block1

block3

block2

Copy propagation Back translation by naïve method

Problems of naïve SSA back translation(lost copy problem)

Page 22: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x0 = 1

x1 = (x0, x2)

x2 = 2

return x1

x0 = 1x1 = x0

x2 = 2x1 = x2

return temp

block1

block3

block2

block1

block3

block2

temp = x1

(a) SSA form (b) normal form after back translation

liverangeof x1

liverangeof temp

To remedy these problems...(i) SSA back translation algorithm by Briggs

Page 23: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x0 = 1

x1 = (x0, x2)

x2 = 2

return x1

live range ofx0 x1 x2

x0 = 1

x1’ = (x0, x2)x1 = x1’x2 = 2

return x1

{x0, x1’, x2} A

x1 = AA = 2

A = 1

block1

block3

block2

block1

block3

block2

return x1

(a) SSA form (b) eliminating interference

(c) normal form after back translation

live range ofx0 x1' x2

block1

block3

block2

(ii) SSA back translation algorithm by Sreedhar

Page 24: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

SSA form

Briggs Briggs +

Coalescing

Sreedhar

Lost copy 0 3 1 [1] 1 [1]

Simple ordering 0 5 2 [2] 2 [2]

Swap 0 7 5 [5] 3 [3]

Swap-lost 0 10 7 [7] 4 [4]

do 0 9 6 [4] 4 [2]

fib 0 4 0 [0] 0 [0]

GCD 0 9 5 [2] 5 [2]

Selection Sort 0 9 0 [0] 0 [0]

Hige Swap 0 8 3 [3] 4 [4]

No. of copies [no. of copies in loops]

Empirical comparison of SSA back translation

Page 25: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Summary

• SSA form module of the COINS infrastructure

• Empirical comparison of algorithms for SSA translation gave criterion to make a good choice

• Empirical comparison of algorithms for SSA back translation clarified there is no single algorithm which gives optimal result

Page 26: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

4. A Survey of Compiler Infrastructures

• SUIF *• Machine SUIF *• Zephyr *• Scale• gcc• COINS• Saiki & Gondow * National Compiler Infrastructure (NCI) project

Page 27: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

An Overview of the SUIF2 System

Monica LamStanford University

http://suif.stanford.edu/

[PLDI 2000 tutorial]

Page 28: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

 OSUIF

The SUIF System

InterproceduralAnalysisParallelizationLocality Opt Scalar opt

Inst. SchedulingRegister Allocation

  Alpha

   x86

      C MachSUIF

PGI Fortran

 SUIF2

  JavaEDG C++

* C++ OSUIF to SUIF is incomplete

EDG C

*

Page 29: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Overview of SUIF Components (I)Basic Infrastructure Extensible IR and utilities Hoof: Suif object specification lang Standard IR Modular compiler system Pass submodule Data structures (e.g. hash tables)

FE: PGI Fortran, EDG C/C++, JavaSUIF1 / SUIF2 translators, S2cInteractive compilation: suifdriverStatement dismantlersSUIF IR consistency checkersSuifbrowser, TCL visual shellLinker

Backend Infrastructure MachSUIF program representation Optimization framework

Scalar optimizations common subexpression elimination deadcode elimination peephole optimizations Graph coloring register allocationAlpha and x86 backends

Object-oriented Infrastructure    OSUIF representation Java OSUIF -> SUIF lowering

   object layout and method dispatch

Page 30: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Overview of SUIF Components (II)High-Level Analysis Infrastructure Graphs, sccs Iterated dominance frontier Dot graph output

Region framework Interprocedural analysis framework

Presburger arithmetic (omega) Farkas lemma Gaussian elimination package

Intraprocedural analyses copy propagation deadcode eliminationSteensgaard’s alias analysis Call graph

Control flow graphsInterprocedural region-based analyses: array dependence & privatization scalar reduction & privatizationInterprocedural parallelizationAffine partitioning for parallelism & locality unifies: unimodular transform (interchange, reversal, skewing) fusion, fission statement reindexing and scalingBlocking for nonperfectly nested loops

Page 31: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Memory/Memory vs File/File Passes

Suif-file1

Suif-file2

Suif-file3

Suif-file4

driver+module1

driver+module2

driver+module3

Suif-file1

Suif-file4

Suifdriverimports/executes module1 module2 module3

A series of stand-alone programsA driver that imports & applies modules to program in memory

COMPILER

Page 32: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Machine SUIF

Michael D. Smith

Harvard UniversityDivision of Engineering and Applied

Sciences

June 2000© Copyright by Michael D. Smith 2000

All rights reserved.

Page 33: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Typical Backend Flow

lowerlower

optimizeoptimize

realizerealize

optimizeoptimize

finalizefinalize

layoutlayout

outputoutputObject, assembly, or C code

SUIF intermediate form

Parameter bindings from

dynamically-linked target libraries

Machine-SUIF IR forreal machine

Machine-SUIF IR foridealized machine (suifvm)

Page 34: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

 parameterization 

libcfg libutil

libbvd

dce cse

layout

alphalib

x86lib

alphalib

libcfg libutil

libbvd

dce cse

layout

 parameterization 

x86lib

alphalib

libcfg libutil

libbvd

dce cse

layout

 parameterization 

x86lib

yfc

yfcmlib

suif

machsuifmlib

suifvmlib

deco

decomlib

 opi 

Target Parameterization

• Analysis/optimization passes written without direct encoding of target details

• Target details encapsulated in OPI functions and data structures

• Machine-SUIF passes work without modification on disparate targets

Page 35: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

suif

machsuifmlib

 opi 

 parameterization 

alphalib

suifvmlib

x86lib

libcfg libutil

libbvd

yfc

yfcmlib

deco

decomlib

dce cse

layout

Substrate Independence• Optimizations, analyses,

and target libraries are substrate-independent

•   Machine SUIF is built    on top of SUIF

•   You could replace SUIF    with Your Favorite Compiler

•   Deco project at Harvard    uses this approach

Page 36: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Fortran program

C analyzer Java analyzer

High level Intermediate Representation (HIR)

Basic optimizer

Data flow analyzerCommon subexp elim

Dead code elim

HIRto

LIR

Low level intermediate representation (LIR)

SSA optimizer

LIR-SSA transformationBasic optimizer

Advanced optimizer

Code generator

Sparc codegenerator

New language analyzer

C program

Advanced optimizer

Alias analyzerLoop optimizer

Parallelization

Java programNew language

program C programOpenMPprogram

SPARCcode

SIMD parallelizer

SIMD instruction selection based on machi

ne descr

x86description

Machine dependent optimizer SPARC

descriptionPowerPC

descriptionRegister allocatorInstruction scheduler

X86code

New machine

code

Com

piler control (schedule module

invocation)

Symbol table

X86description for code generation

SPARCdescription for code

generation

New machine description for code

generation

Fortran analyzer

Loop analyzerCoarse grain parallelizer

Loop parallelizer

C generation

Phase 12000-2002

Phase 22003-2004

by COINS’s users

source/target

Code generation based on machine description

Overall structure of COINS

Page 37: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Features of COINSMultiple source languagesMultiple target architectures HIR: abstract syntax tree with attributesLIR: register transfer level with formal specificationEnabling source-to-source translation and application to

software engineeringScalar analysis & optimization (in usual form and in SS

A form)Basic parallelization (e.g. OpenMP)SIMD parallelizationCode generators generated from machine descriptionWritten in Java (early error detection), publicly available [http://www.coins-project.org/]

Page 38: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.
Page 39: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

x1=…   = x1y1=…z1=…

x2=…   = x2y2=…z2=…

   = y1    = y2

x3= (x1,x2)y3= (y1,y2)z3= (z1,z2)   = z3

x1=…   = x1y1=…z1=…

x2=…   = x2y2=…z2=…

   = y1    = y2

y3= (y1,y2)z3= (z1,z2)   = z3

x1=…   = x1y1=…z1=…

x2=…   = x2y2=…z2=…

   = y1    = y2

z3= (z1,z2)   = z3

x =…  = xy =…z =…

x =…   = xy =…z =…

   = y    = y

   = z

Minimal SSA form Semi-pruned SSA form Pruned SSA form

Normal form

Translating into SSA form (SSA translation)

Page 40: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Previous work: SSA form in compiler infrastructure

SUIF (Stanford Univ.): no SSA formmachine SUIF (Harvard Univ.): only one optimization

in SSA formScale (Univ. Massachusetts): a couple of SSA form

optimizations. But it generates only C programs, and cannot generate machine code like in COINS.

GCC: some attempts but experimental

Only COINS will have full support of SSA form as a compiler infrastructure

Page 41: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Example of a Hoof Definition

• Uniform data access functions (get_ & set_)• Automatic generation of meta class information etc.

class New : public SuifObject{

public:int get_x();void set_x(int the_value); ~New();void print(…);static const Lstring get_class_name()

; …

}

concrete New{ int x; }

hoof

C++

Page 42: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

Input program

for (i=0; i<10; i=i+1) { a[i]=i; ...}

HIR (abstract syntax tree with attributes)

(for (assign <var i int> <const 0 int>) (cmpLT <var i int> <const 10 int>) (block (assign (subs <var a <VECT 10 int>> <var i int>) <var i int>) .... ) (assign <var i int> (add <var i int> <const i int>) ) )

HIR (high-level intermediate representation)

Page 43: Static Single Assignment Form in the COINS Compiler Infrastructure - Current Status and Background - Masataka Sassa, Toshiharu Nakaya, Masaki Kohama (Tokyo.

LIR

(set (mem (static (var i))) (const 0))(labeldef _lab5)(jumpc (tstlt (mem (static (var i))) (const 10)) (list (label _lab6) (label _lab4))))(labeldef _lab6)(set (mem (add (static (var a)) (mul (mem (static (var i))) (const 4)))) (mem (static (var i))) . . .

Source program

for (i=0; i<10; i=i+1){ a[i]=i; ...}

LIR (low-level intermediate representation)