grin: dead data elimination - in the context of ... · grin:deaddataelimination...

45
GRIN: Dead data elimination in the context of dependently typed languages Péter Podlovics, Csaba Hruska Eötvös Loránd University (ELTE), Budapest, Hungary EUTypes-2019

Upload: others

Post on 27-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

GRIN: Dead data eliminationin the context of dependently typed languages

Péter Podlovics, Csaba Hruska

Eötvös Loránd University (ELTE),Budapest, Hungary

EUTypes-2019

Page 2: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Overview

Introduction

Extensions

Dead Data Elimination

Results

Page 3: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Introduction

Page 4: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Why functional?

Declarativeness

pro: can program on a higher abstraction level

Composability

pro: can easily piece together smaller programs

con: results in a lot of function calls

Functions are first class citizens

pro: higher order functions

con: unknown function calls

Page 5: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Graph Reduction Intermediate Notation

HaskellIdris Agda

GRIN

Machine Code

Page 6: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Front end code

main = sum (upto 0 10)

upto n m| n > m = []| otherwise = n : upto (n+1) m

sum [] = 0sum (x:xs) = x + sum xs

main

eval

sum upto

Page 7: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Front end code

main = sum (upto 0 10)

upto n m| n > m = []| otherwise = n : upto (n+1) m

sum [] = 0sum (x:xs) = x + sum xs

main

eval

sum upto

Page 8: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

GRIN code

grinMain =t1 <- store (CInt 1)t2 <- store (CInt 10)t3 <- store (Fupto t1 t2)t4 <- store (Fsum t3)(CInt r) <- eval t4_prim_int_print r

eval p =v <- fetch pcase v of

(CInt n) -> pure v(CNil) -> pure v(CCons y ys) -> pure v(Fupto a b) ->

zs <- upto a bupdate p zspure zs

(Fsum c) ->s <- sum cupdate p spure s

Page 9: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Transformation machinery

Inline calls to eval

Run dataflow analyses:

Heap points-to analysis

Sharing analysis

Run transformations until we reach a fixed-point:

Sparse Case Optimization

Common Subexpression Elimination

Generalized Unboxing

etc . . .

Page 10: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Extensions

Page 11: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Extending Heap points-to

BAS ∈ {Int64,Float,Bool, String,Char}

indexArray# :: Array# a -> Int# -> (# a #)newMutVar# :: a -> s -> (# s, MutVar# s a #)

Page 12: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Extending Heap points-to

BAS ∈ {Int64,Float,Bool, String,Char}

indexArray# :: Array# a -> Int# -> (# a #)newMutVar# :: a -> s -> (# s, MutVar# s a #)

Page 13: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Extending Heap points-to

BAS ∈ {Int64,Float,Bool, String,Char}

indexArray# :: Array# a -> Int# -> (# a #)newMutVar# :: a -> s -> (# s, MutVar# s a #)

Page 14: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

LLVM back end

grinMain =t1 <- store (CInt 1)t2 <- store (CInt 10)t3 <- store (Fupto t1 t2)t4 <- store (Fsum t3)(CInt r') <- eval t4_prim_int_print r'

upto m n =(CInt m') <- eval m(CInt n') <- eval nb' <- _prim_int_gt m' n'case b' of

#True -> pure (CNil)

sum l = ...

eval p = ...

grinMain =n1 <- sum 0 1 10_prim_int_print n1

sum s lo hi =b <- _prim_int_gt lo hiif b thenpure s

elselo' <- _prim_int_add lo 1s' <- _prim_int_add s losum s' lo' hi

grinMain:# BB#0:

movabsq $55, %rdijmp _prim_int_print

Page 15: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

LLVM back end

grinMain =t1 <- store (CInt 1)t2 <- store (CInt 10)t3 <- store (Fupto t1 t2)t4 <- store (Fsum t3)(CInt r') <- eval t4_prim_int_print r'

upto m n =(CInt m') <- eval m(CInt n') <- eval nb' <- _prim_int_gt m' n'case b' of

#True -> pure (CNil)

sum l = ...

eval p = ...

grinMain =n1 <- sum 0 1 10_prim_int_print n1

sum s lo hi =b <- _prim_int_gt lo hiif b thenpure s

elselo' <- _prim_int_add lo 1s' <- _prim_int_add s losum s' lo' hi

grinMain:# BB#0:

movabsq $55, %rdijmp _prim_int_print

Page 16: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

LLVM back end

grinMain =t1 <- store (CInt 1)t2 <- store (CInt 10)t3 <- store (Fupto t1 t2)t4 <- store (Fsum t3)(CInt r') <- eval t4_prim_int_print r'

upto m n =(CInt m') <- eval m(CInt n') <- eval nb' <- _prim_int_gt m' n'case b' of

#True -> pure (CNil)

sum l = ...

eval p = ...

grinMain =n1 <- sum 0 1 10_prim_int_print n1

sum s lo hi =b <- _prim_int_gt lo hiif b thenpure s

elselo' <- _prim_int_add lo 1s' <- _prim_int_add s losum s' lo' hi

grinMain:# BB#0:

movabsq $55, %rdijmp _prim_int_print

Page 17: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Dead Data Elimination

Page 18: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Dead data elimination I.

length : List a -> Natlength Nil = Zlength (Cons x xs)

= S (length xs)

DDE==⇒

length p =xs <- fetch pcase xs of(Cons ys) ->l1 <- length ysl2 <- _prim_int_add l1 1pure l2

(Nil) ->pure 0

Page 19: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Dead data elimination II.

data Bin : Nat -> Type whereN : Bin 0O : {n : Nat} -> Bin n -> Bin (2*n + 0)I : {n : Nat} -> Bin n -> Bin (2*n + 1)

binToNat : Bin n -> NatbinToNat N = 0binToNat (O {n} _) = 2*nbinToNat (I {n} _) = 2*n + 1

Page 20: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Dead data elimination II.

data Bin : Nat -> Type whereN : Bin 0O : {n : Nat} -> Bin n -> Bin (2*n + 0)I : {n : Nat} -> Bin n -> Bin (2*n + 1)

binToNat : Bin n -> NatbinToNat N = 0binToNat (O {n} _) = 2*nbinToNat (I {n} _) = 2*n + 1

Page 21: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Applications

Map → Set

Type class dictionaries

Type erasure for dependently typed languages

Page 22: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

What do we need?

Producers & consumers

Detect dead fields

Connect consumers to producer

Remove or transform dead fields

Page 23: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Created-by

null xs =y <- case xs of(CNil) ->a <- pure (CTrue)pure a

(CCons z zs) ->b <- pure (CFalse)pure b

pure y

Var Producers

xs CNil [. . . ],CCons[. . . ]

a CTrue[a]b CFalse[b]y CTrue[a],CFalse[b]

Page 24: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

P1 P2

C2C1 C3

upto

Page 25: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto upto

lenlen sum

Page 26: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto upto

sumlenlen sum

Page 27: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto upto

sumlenlen

Page 28: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto

sumlenlen

upto

Page 29: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto

lenlen

upto

sum

Page 30: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto

lenlen

upto

sum

upto

Page 31: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto

lenlen

upto

sum

upto

len

Page 32: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

upto

lenlen

upto

sum

upto

lenE

Page 33: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

uptoupto upto

lenlen sum

Page 34: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

uptoupto

lenlen sum

upto

Page 35: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Producers and consumers

uptoupto upto

lenlen sum

Page 36: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Results

Page 37: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Setup

Small Idris code snippets from:Type-driven Development with Idris by Edwin BradyOnly interpreted codeCompile- & runtime measurementsPipeline setup:

Code gen. Regular Opts. DDE Regular Opts.

Page 38: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Length

Page 39: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Exact length

Page 40: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Reverse

Page 41: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Type level functions

Page 42: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Conclusions

The optimizer works well:

the number of stores, fetches, function calls and patternmatches significantly decreased

the structure of the code resembles that of an imperativelanguage

Dead Data Elimination:

is a bit costly

is a specific optimization

can completely transform data structures

can trigger further transformations

Page 43: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

THANK YOUFOR YOURATTENTION!

Page 44: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Sparse case optimization

<m0>v <- eval lcase v ofCNil -> <m1>CCons x xs -> <m2>

v∈{CCons}======⇒

<m0>v <- eval lcase v ofCCons x xs -> <m2>

Page 45: GRIN: Dead data elimination - in the context of ... · GRIN:Deaddataelimination inthecontextofdependentlytypedlanguages PéterPodlovics,CsabaHruska Eötvös Loránd University (ELTE),

Compiled data flow analysis

Analyzing the syntax tree has an interpretation overhead

We can work around this by "compiling" our analysis intoan executable program

The compiled abstract program is independent of the AST

It can be executed in a different context (ie.: by anotherprogram or on GPU)

After run (iteratively), it produces the result of the givenanalysis