improving compiler heuristics with machine learning mark stephenson una-may o’reilly martin c....

46
Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Upload: ethan-tyler

Post on 18-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Improving Compiler Heuristics with

Machine Learning

Mark Stephenson

Una-May O’Reilly

Martin C. Martin

Saman Amarasinghe

Massachusetts Institute of Technology

Page 2: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

System Complexities

Compiler complexity Open Research Compiler

~3.5 million lines of C/C++ code

Trimaran’s compiler

~ 800,000 lines of C code

Architecture Complexity

Features Pentium® (3M)

Pentium 4 (55M)

Superscalar Branch prediction

Speculative execution

MMU and improved FPU

Page 3: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

NP-Completeness

Many compiler problems are NP-complete Thus, implementations can’t be optimal

Compiler writers rely on heuristics In practice, heuristics perform well …but, require a lot of tweaking

Heuristics often have a focal point Rely on a single priority function

Page 4: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Priority Functions

A heuristic’s Achilles heel A single priority or cost function often dictates the

efficacy of a heuristic Priority functions rank the options available to a

compiler heuristic List scheduling (identifying instructions in worklist to

schedule first) Graph coloring register allocation (selecting nodes to

spill) Hyperblock formation (selecting paths to include)

Any priority function is legal

Page 5: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Our Proposal

Use machine-learning techniques to automatically search the priority function space Increases compiler’s performance Reduces compiler design complexity

Page 6: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Can focus on a small portion of an optimization algorithm Don’t need to worry about legality checking

Small change can yield big payoffsClear specification in terms of input/outputPrevalent in compiler heuristics

Qualities of Priority Functions

Page 7: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

An Example OptimizationHyperblock Scheduling

Conditional execution is potentially very expensive on a modern architecture

Modern processors try to dynamically predict the outcome of the condition This works great for predictable branches… But some conditions can’t be predicted

If they don’t predict correctly you waste a lot of time

Page 8: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Example OptimizationHyperblock Scheduling

if (a[1] == 0)

else

Assume a[1] is 0 time

reso

urce

s

Misprediction

Page 9: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Example OptimizationHyperblock Scheduling

if (a[1] == 0)

else

Assume a[1] is 0 time

reso

urce

s

Page 10: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Example OptimizationHyperblock Scheduling (using predication)

if (a[1] == 0)

else

Assume a[1] is 0 time

reso

urce

s

Processor simply discards results of instructions that weren’t supposed to be run

Page 11: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Example OptimizationHyperblock Scheduling

There are unclear tradeoffs In some situations, hyperblocks are faster than traditional

execution In others, hyperblocks impair performance

Many factors affect this decision Accuracy of branch predictor Availability of parallel execution resources Effectiveness of the compiler’s scheduler Parallelizability and predictability of the program

Hard to model

Page 12: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Example OptimizationHyperblock Scheduling [Mahlke]

Find predicatable regions of control flowEnumerate paths of control in region

Exponential, but in practice it’s okay

Prioritize paths based on four path characteristics The priority function we want to optimize

Add paths to hyperblock in priority order

Page 13: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Trimaran’s Priority Function

free hazard is if :1

hazard has if :25.0

i

ii segment

segmenthazard

jNj

ii heightdep

heightdepratiodep

_max

__

1

)__1.2(_ iiiii ratioopratiodephazardratioexecpriority

jNj

ii heightop

heightopratioop

_max

__

1

Favor frequentlyexecuted paths

Favor short paths

Penalize paths with hazards Favor parallel paths

Page 14: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Our Approach

Trimaran uses four characteristics

What are the important characteristics of a hyperblock formation priority function?

Our approach: Extract all the characteristics you can think of and let a learning technique find the priority function

Page 15: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Genetic Programming

Searching algorithm analogous to Darwinian evolution Maintain a population of expressions

num_ops 2.3 predictability 4.1

- /

*

Page 16: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Genetic Programming

Searching algorithm analogous to Darwinian evolution Maintain a population of expressions Selection

The fittest expressions in the population are more likely to reproduce

Reproduction Crossing over subexpressions of two expressions

Mutation

Page 17: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

General Flow

Create initial population(initial solutions)

Evaluation

Selection

Randomly generated initial population seeded with the compiler writer’s best guess

Create Variants

done?

Page 18: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

General Flow

Create initial population(initial solutions)

Evaluation

Selection

Create Variants

done?

Compiler is modified to use the given expression as a priority function

Each expression is evaluated by compiling and running the benchmark(s)

Fitness is the relative speedup over Trimaran’s priority function on the benchmark(s)

Page 19: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

General Flow

Create initial population(initial solutions)

Evaluation

Selection

Create Variants

done?

Just as with Natural Selection, the fittest individuals are more likely to survive

Page 20: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

General Flow

Create initial population(initial solutions)

Evaluation

Selection

Create Variants

done?

Use crossover and mutation to generate new expressions

And thus, generate new compilers

Page 21: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Experimental Setup

Collect results using Trimaran Simulate a VLIW machine

64 GPRs, 64 FPRs, 128 PRs 4 fully pipelined integer FUs 2 fully pipelined floating point FUs 2 memory units (L1:2, L2:7, L3:35)

Replace priority functions in IMPACT with our GP expression parser and evaluator

Page 22: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Outline of Results

High-water mark Create compilers specific to a given application

and a given data set Essentially partially evaluating the application

Application-specific compilers Compiler trained for a given application and data

set, but run with an alternate data setGeneral-purpose compiler

Compiler trained on multiple applications and tested on an unrelated set of applications

Page 23: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Training the Priority Function

Compiler

A.c B.c C.c D.c

A B C D

1 2

Page 24: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Training the Priority FunctionApplication-Specific Compilers

Compiler

A.c B.c C.c D.c

A B C D

1 2

Page 25: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Hyperblock ResultsApplication-Specific Compilers (High-Water Mark)

1.5

4

1.2

3

0

0.5

1

1.5

2

2.5

3

3.51

29

.co

mp

ress

g7

21

en

cod

e

g7

21

de

cod

e

hu

ff_

de

c

hu

ff_

en

c

raw

cau

dio

raw

da

ud

io

toa

st

mp

eg

2d

ec

Av

era

ge

Sp

ee

du

p

Training input Novel input

(add (sub (cmul (gt (cmul $b0 0.8982 $d17)…$d7)) (cmul $b0 0.6183 $d28)))

(add (div $d20 $d5) (tern $b2 $d0 $d9))

Page 26: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Training the Priority FunctionGeneral-Purpose Compilers

Compiler

A.c B.c C.c D.c

A B C D

1

Page 27: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Hyperblock ResultsGeneral-Purpose Compiler

1.44

1.25

0.0

0.5

1.0

1.5

2.0

2.5

3.0

3.5

deco

drle

4

codr

le4

g721

deco

de

g721

enco

de

raw

daud

io

raw

caud

io

toas

t

mpe

g2de

c

124.

m88

ksim

129.

com

pres

s

huff

_enc

huff

_dec

Ave

rag

e

Sp

eed

up

Train data set Novel data set

Page 28: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Validation of GeneralityTesting General-Purpose Applicability

1.0

9

0.0

0.2

0.4

0.6

0.8

1.0

1.2

1.4

1.6

une

pic

djpe

g

rast

a

023

.eq

nto

tt

132

.ijp

eg

052

.alv

inn

147

.vor

tex

085

.cc1 ar

t

130

.li

osde

mo

mip

map

Ave

rag

e

Spe

edup

Page 29: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Running Time

Application specific compilers ~1 day using 15 processors

General-purpose compilers Dynamic Subset Selection [Gathercole]

Run on a subset of the training benchmarks at a time Memoize fitnesses ~1 week using 15 processors

This is a one time process! Performed by the compiler vendor

Page 30: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

(add (sub (mul exec_ratio_mean 0.8720) 0.9400) (mul 0.4762 (cmul (not has_pointer_deref)

(mul 0.6727 num_paths) (mul 1.1609

(add (sub (mul (div num_ops dependence_height) 10.8240) exec_ratio) (sub (mul (cmul has_unsafe_jsr predict_product_mean 0.9838)

(sub 1.1039 num_ops_max)) (sub (mul dependence_height_mean num_branches_max) num_paths)))))))

GP Hyperblock SolutionsGeneral Purpose

Intron that doesn’t affect solution

Page 31: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

(add (sub (mul exec_ratio_mean 0.8720) 0.9400) (mul 0.4762 (cmul (not has_pointer_deref)

(mul 0.6727 num_paths) (mul 1.1609

(add (sub (mul (div num_ops dependence_height) 10.8240) exec_ratio) (sub (mul (cmul has_unsafe_jsr predict_product_mean 0.9838)

(sub 1.1039 num_ops_max)) (sub (mul dependence_height_mean num_branches_max) num_paths)))))))

GP Hyperblock SolutionsGeneral Purpose

Favor paths that don’t have pointer dereferences

Page 32: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

GP Hyperblock SolutionsGeneral Purpose

(add (sub (mul exec_ratio_mean 0.8720) 0.9400) (mul 0.4762 (cmul (not has_pointer_deref)

(mul 0.6727 num_paths) (mul 1.1609

(add (sub (mul (div num_ops dependence_height) 10.8240) exec_ratio) (sub (mul (cmul has_unsafe_jsr predict_product_mean 0.9838)

(sub 1.1039 num_ops_max)) (sub (mul dependence_height_mean num_branches_max) num_paths)))))))

Favor highly parallel (fat) paths

Page 33: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

GP Hyperblock SolutionsGeneral Purpose

(add (sub (mul exec_ratio_mean 0.8720) 0.9400) (mul 0.4762 (cmul (not has_pointer_deref)

(mul 0.6727 num_paths) (mul 1.1609

(add (sub (mul (div num_ops dependence_height) 10.8240) exec_ratio) (sub (mul (cmul has_unsafe_jsr predict_product_mean 0.9838)

(sub 1.1039 num_ops_max)) (sub (mul dependence_height_mean num_branches_max) num_paths)))))))

If a path calls a subroutine that may have side effects, penalize it

Page 34: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Our Proposal

Use machine-learning techniques to automatically search the priority function space Increases compiler’s performance Reduces compiler design complexity

Page 35: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Eliminate the Human from the Loop

So far we have tried to improve existing priority functions Still a lot of person-hours were spent creating the

initial priority functions

Observation: the human-created priority functions are often eliminated in the 1st generation

What if we start from a completely random population (no human-generated seed)?

Page 36: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Another ExampleRegister Allocation

An old, established problem Hundreds of papers on the subject

Priority-Based Register Allocation [Chow,Hennessey]

Uses a priority function to determine the worth of allocating a register

Let’s throw our GP system at the problem and see what it comes up with

Page 37: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Register Allocation ResultsGeneral-Purpose Compiler

1.0

3

1.0

3

0.90

0.95

1.00

1.05

1.10

1.15

1.20

raw

caud

io

raw

daud

io

huff

_enc

huff

_dec

129.

com

pres

s

mpe

g2de

c

g721

enco

de

g721

deco

de

aver

age

Sp

ee

du

p

Train data set Novel data set

Page 38: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Validation of GeneralityTesting General-Purpose Applicability

1.02

0.94

0.96

0.98

1.00

1.02

1.04

1.06

1.08de

codr

le4

codr

le4

130.

li

djpe

g

unep

ic

124.

m88

ksim

023.

eqnt

ott

132.

ijpeg

085.

cc1

147.

vort

ex

aver

age

Sp

eed

up

Page 39: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Importance of Priority FunctionsSpeedup over a constant priority function

1.32 1.

36

0.80

1.00

1.20

1.40

1.60

1.80

2.00

2.20ra

wca

ud

io

raw

da

ud

io

hu

ff_

en

c

hu

ff_

de

c

12

9.c

om

pre

ss

mp

eg

2d

ec

g7

21

en

cod

e

g7

21

de

cod

e

ave

rag

e

Sp

ee

du

p (

ov

er

no

fu

nc

tio

n)

Trimaran's function GP's function

Page 40: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Advantages our System Provides

Engineers can focus their energy on more important tasks

Can quickly retune for architectural changesCan quickly retune when compiler changesCan provide compilers catered toward

specific application suites e.g., consumer may want a compiler that excels

on scientific benchmarks

Page 41: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Related Work

Calder et al. [TOPLAS-19] Fine tuned static branch prediction heuristics Requires a priori classification by a supervisor

Monsifrot et al. [AIMSA-02] Classify loops based on amenability to unrolling Also used a priori classification

Cooper et al. [Journal of Supercomputing-02] Use GAs to solve phase ordering problems

Page 42: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Conclusion

Performance talk and a complexity talkTake a huge compiler, optimize one priority

function with GP and get exciting speedupsTake a well-known heuristic and create

priority functions for it from scratchThere’s a lot left to do

Page 43: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology
Page 44: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Why Genetic Programming?

Many learning techniques rely on having pre-classified data (labels) e.g., statistical learning, neural networks, decision

treesPriority functions require another approach

Reinforcement learning Unsupervised learning

Several techniques that might work well e.g., hill climbing, active learning, simulated

annealing

Page 45: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Why Genetic Programming

Benefits of GP Capable of searching high-dimensional spaces It is a distributed algorithm The solutions are human readable

Nevertheless…there are other learning techniques that may also perform well

Page 46: Improving Compiler Heuristics with Machine Learning Mark Stephenson Una-May O’Reilly Martin C. Martin Saman Amarasinghe Massachusetts Institute of Technology

Genetic ProgrammingReproduction

num_ops 2.3 predictability 4.1

- /

*

branches 7.5

* 1.1

/

branches 7.5

*

predictability 4.1

/

*

1.1

/

num_ops 2.3

-