strategies for typecase optimization
play

Strategies for typecase optimization Jim Newton 11th European Lisp - PowerPoint PPT Presentation

Strategies for typecase optimization Jim Newton 11th European Lisp Symposium 16-17 April 2017 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 1 / 41 Motivation and Background 1 Intro to Common


  1. Strategies for typecase optimization Jim Newton 11th European Lisp Symposium 16-17 April 2017 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 1 / 41

  2. Motivation and Background 1 Intro to Common Lisp Types and typecase 2 Optimization by s-expression transformation 3 Optimization using decision diagrams 4 Conclusion 5 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 2 / 41

  3. Motivation and Background Table of Contents Motivation and Background 1 Intro to Common Lisp Types and typecase 2 Optimization by s-expression transformation 3 Optimization using decision diagrams 4 Conclusion 5 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 3 / 41

  4. Motivation and Background Background Rational Type Expressions (RTE) recognize sequences based on element type. Code gen for RTE: excessive use of typecase with complex, machine generated type specifiers. Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 4 / 41

  5. Motivation and Background Code generated from RTE state machine number (tagbody 0 number 2 (unless seq (return nil)) (typecase (pop seq) (symbol (go 1)) symbol symbol 0 1 (t (return nil))) symbol 1 (unless seq (return nil)) string 3 (typecase (pop seq) (number (go 2)) (string (go 3)) string (t (return nil))) 2 3 (unless seq (return t)) (unless seq (return t)) (typecase (pop seq) (typecase (pop seq) (number (go 2)) (string (go 3)) (symbol (go 1)) (symbol (go 1)) (t (return nil))) (t (return nil))))) Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 5 / 41

  6. Motivation and Background More complicated State Machine T1 19 T20 20 T9 18 T6 T10 T17 8 T4 21 T1 T3 16 T21 T10 T4 7 9 T1 6 17 T8 T8 T3 T7 T3 0 1 2 T9 T6 T16 T19 3 4 23 5 T10 T1 T1 T10 22 25 T9 12 T1 11 T3 T6 T5 24 10 T4 T3 28 T8 T4 T15 T6 13 14 T9 T1 29 26 T8 T1 15 27 T18 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 6 / 41

  7. Motivation and Background Background Problem: how to order the type specifiers and minimize redundancy. Two approaches S-expression manipulation and heuristics. 1 Binary Decision Diagrams (BDD) 2 Original hope was that the BDD approach would be superior. I now believe both approaches have merits. Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 7 / 41

  8. Intro to Common Lisp Types and typecase Table of Contents Motivation and Background 1 Intro to Common Lisp Types and typecase 2 Optimization by s-expression transformation 3 Optimization using decision diagrams 4 Conclusion 5 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 8 / 41

  9. Intro to Common Lisp Types and typecase What is a Common Lisp type? A type is a set of Lisp objects. Type operations are set operations. A B Subtypes are subsets. Intersecting types are intersecting sets. Disjoint types are disjoint sets. The empty type is the empty set. Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 9 / 41

  10. Intro to Common Lisp Types and typecase Some types can be identified Boolean operations integer ⊂ rational ratio = rational ∩ integer ratio = (and rational (not integer)) rational integer float ratio Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 10 / 41

  11. Intro to Common Lisp Types and typecase Some types can be identified Boolean operations integer ⊂ rational ratio = rational ∩ integer ratio = (and rational (not integer)) float ⊂ rational ∅ = rational ∩ float nil = (and rational float) rational integer float ratio Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 10 / 41

  12. Intro to Common Lisp Types and typecase What is typecase ? Simple example of typecase ( typecase expr ( fixnum body − forms − 1 . . . ) ( number body − forms − 2 . . . ) ( s t r i n g body − forms − 3 . . . ) ) Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 11 / 41

  13. Intro to Common Lisp Types and typecase What is typecase ? Simple example of typecase ( typecase expr ( fixnum body − forms − 1 . . . ) ( number body − forms − 2 . . . ) ( s t r i n g body − forms − 3 . . . ) ) typecase may use any valid type specifier. ( typecase expr (( and fixnum ( not ( e q l 0 ) ) ) body − forms − 1 . . . ) (( or fixnum s t r i n g ) body − forms − 2 . . . ) (( member − 1 − 2) body − forms − 3 . . . ) (( s a t i s f i e s MY − FUN) body − forms − 4 . . . ) . . . ) Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 11 / 41

  14. Intro to Common Lisp Types and typecase What is typecase ? Simple example of typecase ( typecase expr ( fixnum body − forms − 1 . . . ) ( number body − forms − 2 . . . ) ( s t r i n g body − forms − 3 . . . ) ) typecase may use any valid type specifier. ( typecase expr (( and fixnum ( not ( e q l 0 ) ) ) body − forms − 1 . . . ) (( or fixnum s t r i n g ) body − forms − 2 . . . ) (( member − 1 − 2) body − forms − 3 . . . ) (( s a t i s f i e s MY − FUN) body − forms − 4 . . . ) . . . ) Rich built-in syntax for specifying lots of exotic types Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 11 / 41

  15. Optimization by s-expression transformation Table of Contents Motivation and Background 1 Intro to Common Lisp Types and typecase 2 Optimization by s-expression transformation 3 Optimization using decision diagrams 4 Conclusion 5 Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 12 / 41

  16. Optimization by s-expression transformation Macro expansion of typecase We can use macroexpand-1 from SBCL. ( typecase x (( and fixnum ( not ( e q l 0 ) ) ) ( f1 )) (( e q l 0) ( f2 )) ( symbol ( f3 )) ( t ( f4 ) ) ) Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 13 / 41

  17. Optimization by s-expression transformation Macro expansion of typecase We can use macroexpand-1 from SBCL. ( typecase x (( and fixnum ( not ( e q l 0 ) ) ) ( f1 )) (( e q l 0) ( f2 )) ( symbol ( f3 )) ( t ( f4 ) ) ) The expansion essentially involves cond and typep . ( cond (( typep x ’( and fixnum ( not ( e q l 0 ) ) ) ) ( f1 )) (( typep x ’( e q l 0)) ( f2 )) (( typep x ’ symbol ) ( f3 )) ( t ( f4 ) ) ) Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 13 / 41

  18. Optimization by s-expression transformation Issues we wish to address Redundant type checks Unreachable code Exhaustiveness Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 14 / 41

  19. Optimization by s-expression transformation Redundant type checks Redundant type checks Unreachable code Exhaustiveness ( typecase obj (( and fixnum ( not bignum )) ( f1 )) (( and bignum ( not unsigned − byte )) ( f2 )) ( bignum ( f3 ) ) ) The type check for bignum might be executed multiple times. Perhaps not an enormous problem... Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 15 / 41

  20. Optimization by s-expression transformation Redundant type checks Redundant type checks Unreachable code Exhaustiveness ( typecase obj (( and fixnum ( not bignum )) ( f1 )) (( and bignum ( not unsigned − byte )) ( f2 )) ( bignum ( f3 ) ) ) The type check for bignum might be executed multiple times. Perhaps not an enormous problem... But satisfies types and consequently user defined types may be arbitrarily complex. Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 15 / 41

  21. Optimization by s-expression transformation Redundant type checks Redundant type checks Unreachable code Exhaustiveness ( typecase obj (( and fixnum ( not bignum )) ( f1 )) (( and bignum ( not unsigned − byte )) ( f2 )) ( bignum ( f3 ) ) ) The type check for bignum might be executed multiple times. Perhaps not an enormous problem... But satisfies types and consequently user defined types may be arbitrarily complex. Especially in machine generated code. Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 15 / 41

  22. Optimization by s-expression transformation Unreachable code Redundant type checks Unreachable code Exhaustiveness ( typecase obj (( or number s t r i n g symbol ) ( f1 )) (( and ( s a t i s f i e s slow − predicate ) number ) ( f2 )) (( and ( s a t i s f i e s slow − predicate ) ( or symbol s t r i n g )) ( f3 ) ) ) The function calls, (f2) and (f3) , are unreachable. Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 16 / 41

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