last time staging basics e
play

Last time: staging basics . < e > . 1/ 54 Staging recap Goal - PowerPoint PPT Presentation

Last time: staging basics . < e > . 1/ 54 Staging recap Goal : specialise with available data to improve future performance New constructs : a code . < e > . !. e . < x > . Binding-time analysis : what is available


  1. Last time: staging basics . < e > . 1/ 54

  2. Staging recap Goal : specialise with available data to improve future performance New constructs : ’a code . < e > . !. e . < x > . Binding-time analysis : what is available statically? Idealized staging process : annotate, close, apply. Examples : pow, dot Improvements : unrolling loops, eliminating unnecessary work 2/ 54

  3. Partially-static data structures 3/ 54

  4. Stack machines again c y x y x x+y c (y,x)[c] . . . . . . . . . . . . . . . . . . Add If PushConst 4/ 54

  5. Stack machines: higher-order vs first-order type ( ’ s , ’ t ) t = ’ s → ’ t l e t add ( x , ( y , s )) = ( x + y , s ) type ( ’ s , ’ t ) t = ( ’ s , ’ t ) i n s t r s l e t add = Add : : Stop 5/ 54

  6. Recap: optimising stack machines v a l ( > =) : ’ a t → ( ’ a → ’b t ) → ’b t > v a l ( ⊗ ) : ( ’ a → ’b) t → ’ a t → ’b t b y y y x x x y . . . . . . . . . . . . . . . . . . . . . PushConst x PushConst y PushConst y PushConst t r u e I f 6/ 54

  7. Stack machines: basic interface module type STACKM = s i g type ( ’ s , ’ t ) t v a l nothing : ( ’ s , ’ s ) t v a l ( ⊗ ) : ( ’ r , ’ s ) t → ( ’ s , ’ t ) t → ( ’ r , ’ t ) t v a l add : ( i n t ∗ ( i n t ∗ ’ s ) , i n t ∗ ’ s ) t v a l i f : ( bool ∗ ( ’ a ∗ ( ’ a ∗ ’ s )) , ’ a ∗ ’ s ) t v a l push const : ’ a → ( ’ s , ’ a ∗ ’ s ) t v a l execute : ( ’ s , ’ t ) t → ’ s → ’ t end 7/ 54

  8. Higher-order stack machines module StackM : STACKM = s t r u c t type ( ’ s , ’ t ) t = ’ s → ’ t l e t nothing s = s l e t ( ⊗ ) f x s = x ( f s ) l e t add ( x , ( y , s )) = (( x + y , s )) l e t i f ( c , ( x , ( y , s ) ) ) = (( i f c then x e l s e y ) , s ) l e t push const v s = ( v , s ) l e t execute f s = f s end 8/ 54

  9. Optimising higher-order stack machines Why are the higher-order machines hard to optimize? l e t ( ⊗ ) f x s = x ( f s ) l e t push const v s = ( v , s ) l e t add ( x , ( y , s )) = (( x + y , s )) push const 3 ⊗ push const 4 ⊗ add 9/ 54

  10. Optimising higher-order stack machines Why are the higher-order machines hard to optimize? l e t ( ⊗ ) f x s = x ( f s ) l e t push const v s = ( v , s ) l e t add ( x , ( y , s )) = (( x + y , s )) Inlining push const, add: ( fun s → (3 , s )) ⊗ ( fun s → (4 , s )) ⊗ ( fun ( x , ( y , s )) → (( x + y , s ) ) ) 10/ 54

  11. Optimising higher-order stack machines Why are the higher-order machines hard to optimize? l e t ( ⊗ ) f x s = x ( f s ) l e t push const v s = ( v , s ) l e t add ( x , ( y , s )) = (( x + y , s )) Inlining ⊗ : ( fun s → ( fun ( x , ( y , s )) → (( x + y , s ) ) ) (( fun s → ( fun s → (4 , s )) (( fun s → (3 , s )) s )) s ) 11/ 54

  12. Optimising higher-order stack machines Why are the higher-order machines hard to optimize? l e t ( ⊗ ) f x s = x ( f s ) l e t push const v s = ( v , s ) l e t add ( x , ( y , s )) = (( x + y , s )) Inlining ⊗ : ( fun s → ( fun ( x , ( y , s )) → (( x + y , s ) ) ) (( fun s → ( fun s → (4 , s )) (( fun s → (3 , s )) s )) s ) Difficulty: evaluating under lambda 11/ 54

  13. Stack machines: higher-order vs first-order vs staged type ( ’ s , ’ t ) t = ’ s → ’ t l e t add ( x , ( y , s )) = ( x + y , s ) type ( ’ s , ’ t ) t = ( ’ s , ’ t ) i n s t r s l e t add = Add : : Stop type ( ’ s , ’ t ) t = ’ s code → ’ t code l e t add p = . < l e t ( x , ( y , s )) = .˜p in ( x + y , s ) > . 12/ 54

  14. Staging the higher-order stack machine module type STACKM staged = s i g i n c l u d e STACKM v a l compile : ( ’ s , ’ t ) t → ( ’ s → ’ t ) code end 13/ 54

  15. Staging the higher-order stack machine module StackM staged : STACKM staged = s t r u c t type ( ’ s , ’ t ) t = ’ s code → ’ t code l e t nothing s = s l e t ( ⊗ ) f x s = x ( f s ) l e t add p = ( . < l e t ( x , ( y , s )) = .˜p in ( x + y , s ) > . ) l e t i f p = . < l e t ( c , ( x , ( y , s ) ) ) = .˜p in (( i f c then x e l s e y ) , s ) > . l e t push const v s = . < ( v , .˜s ) > . l e t compile f = . < fun s → .˜( f . < s > . ) > . l e t execute f s = ! . ( compile f ) s end 14/ 54

  16. Staging the higher-order stack machine: output # compile ( push const true ⊗ i f ) ; ; − : ( ’ a ∗ ( ’ a ∗ ’ b ) → ’ a ∗ ’ b ) code = . < fun s 59 → l e t ( c , ( x , ( y , s ) ) ) = ( true , s ) in (( i f c then x e l s e y ) , s ) > . # compile ( push const 3 ⊗ push const 4 ⊗ push const f a l s e ⊗ i f ) ; ; − : ( ’ a → i n t ∗ ’ a ) code = . < fun s → l e t ( c , ( x , ( y , s ) ) ) = ( f a l s e , (4 , (3 , s ) ) ) in (( i f c then x e l s e y ) , s ) > . # compile ( push const 3 ⊗ push const 4 ⊗ push const f a l s e ⊗ i f ) ; ; − : ( ’ a → i n t ∗ ’ a ) code = . < fun s → l e t ( c , ( x , ( y , s ) ) ) = ( f a l s e , (4 , (3 , s ) ) ) in (( i f c then x e l s e y ) , s ) > . 15/ 54

  17. Possibly-static values type ’ a sd = | Sta : ’ a → ’ a sd | Dyn : ’ a code → ’ a sd l e t unsd : ’ a . ’ a sd → ’ a code = f u n c t i o n Sta v → . < v > . | Dyn v → v 16/ 54

  18. Partially-static stacks type ’ a stack = T a i l : ’ a code → ’ a stack | : : : ’ a sd ∗ ’b stack → ( ’ a ∗ ’b) stack l e t rec unsd stack : type s . s stack → s code = f u n c t i o n T a i l s → s | c : : s → . < (.˜( unsd c ) , .˜( unsd stack s )) > . 17/ 54

  19. Stack machine: binding-time analysis type ( ’ s , ’ t ) t = ’ s → ’ t l e t add ( x , ( y , s )) = ( x + y , s ) type ( ’ s , ’ t ) t = ( ’ s , ’ t ) i n s t r s l e t add = Add : : Stop type ( ’ s , ’ t ) t = ’ s code → ’ t code l e t add p = . < l e t ( x , ( y , s )) = .˜p in ( x + y , s ) > . type ( ’ s , ’ t ) t = ’ s stack → ’ t stack l e t rec add : type s . ( i n t ∗ ( i n t ∗ s ) , i n t ∗ s ) t = f u n c t i o n Sta x : : Sta y : : s → Sta ( x + y ) : : s | . . . 18/ 54

  20. Stack machine: optimising add l e t extend : ’ a ’b . ( ’ a ∗ ’b) stack → ( ’ a ∗ ’b) stack = f u n c t i o n T a i l s → Dyn . < f s t .˜s > . : : T a i l . < snd .˜s > . | : : as s → s l e t rec add : type s . ( i n t ∗ ( i n t ∗ s ) , i n t ∗ s ) t = f u n c t i o n Sta x : : Sta y : : s → Sta ( x + y ) : : s | x : : y : : s → Dyn . < .˜( unsd x ) + .˜( unsd y ) > . : : s | ( T a i l as s ) → add ( extend s ) | c : : ( T a i l as s ) → add ( c : : extend s ) 19/ 54

  21. Stack machine: optimising branches l e t rec i f : type s a . ( bool ∗ ( a ∗ ( a ∗ s )) , a ∗ s ) t = f u n c t i o n | Sta true : : x : : y : : s → x : : s | Sta f a l s e : : x : : y : : s → y : : s | Dyn c : : x : : y : : s → Dyn . < i f .˜c then .˜( unsd y ) e l s e .˜( unsd x ) > . : : s | ( T a i l as s ) → i f ( extend s ) | c : : ( T a i l as s ) → i f ( c : : extend s ) | c : : x : : ( T a i l as s ) → i f ( c : : x : : extend s ) 20/ 54

  22. Stack machine: top-level compilation v a l compile : ( ’ s , ’ t ) t → ( ’ s → ’ t ) code l e t compile f = . < fun s → .˜( unsd stack ( f ( T a i l . < s > . )) ) > . 21/ 54

  23. Stack machine: flexible optimisation # compile add ; ; − : ( i n t ∗ ( i n t ∗ ’ a ) → i n t ∗ ’ a ) code = . < fun s → (( f s t s + f s t ( snd s )) , snd ( snd s )) > . # compile i f ; ; − : ( bool ∗ ( ’ a ∗ ( ’ a ∗ ’ b )) → ’ a ∗ ’ b ) code = . < fun s → (( i f f s t s then f s t ( snd ( snd s )) e l s e f s t ( snd s )) , ( snd ( snd ( snd s ) ) ) ) > . # compile ( push const true ⊗ i f ) ; ; − : ( ’ a ∗ ( ’ a ∗ ’ b ) → ’ a ∗ ’ b ) code = . < fun s → ( f s t s , snd ( snd s )) > . 22/ 54

  24. Stack machine: flexible optimisation # compile ( push const f a l s e ⊗ i f ) ; ; − : ( ’ a ∗ ( ’ a ∗ ’ b ) → ’ a ∗ ’ b ) code = . < fun s → ( f s t ( snd s ) , snd ( snd s )) > . # compile ( push const 3 ⊗ push const 4 ⊗ push const f a l s e ⊗ i f ) ; ; − : ( ’ a → i n t ∗ ’ a ) code = . < fun s → (3 , s ) > . # compile ( push const 3 ⊗ push const 4 ⊗ add ⊗ push const 2 ⊗ push const f a l s e ⊗ i f ) ; ; − : ( ’ a → i n t ∗ ’ a ) code = . < fun s → (7 , s ) > . 23/ 54

  25. Staged generic programming val gshow : ’a data → (’a → string) code 24/ 54

  26. Generic programming: binding-time analysis gshow ( l i s t ( i n t ∗ bool )) [ ( 1 , true ) ; ( 2 ; f a l s e ) ] Type representations are static Values are dynamic . We’ve used type representations to traverse values. Now we’ll use type representations to generate code. Goal: generate code without any type equality tests. 25/ 54

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend