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

strategies for typecase optimization
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 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

slide-2
SLIDE 2

1

Motivation and Background

2

Intro to Common Lisp Types and typecase

3

Optimization by s-expression transformation

4

Optimization using decision diagrams

5

Conclusion

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

slide-3
SLIDE 3

Motivation and Background

Table of Contents

1

Motivation and Background

2

Intro to Common Lisp Types and typecase

3

Optimization by s-expression transformation

4

Optimization using decision diagrams

5

Conclusion

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

slide-4
SLIDE 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

slide-5
SLIDE 5

Motivation and Background

Code generated from RTE state machine

(tagbody (unless seq (return nil)) (typecase (pop seq) (symbol (go 1)) (t (return nil))) 1 (unless seq (return nil)) (typecase (pop seq) (number (go 2)) (string (go 3)) (t (return nil))) 2 (unless seq (return t)) (typecase (pop seq) (number (go 2)) (symbol (go 1)) (t (return nil)))

1 2 3 symbol number string symbol number symbol string

3 (unless seq (return t)) (typecase (pop seq) (string (go 3)) (symbol (go 1)) (t (return nil)))))

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

slide-6
SLIDE 6

Motivation and Background

More complicated State Machine

1 T3 2 T7 16 T21 17 T8 3 T9 25 T10 T1 18 T4 4 T6 26 T3 5 T19 6 T8 12 T10 T1 7 T4 13 T3 8 T17 9 T10 T1 10 T3 11 T5 T1 14 T15 15 T8 T1 T4 19 T20 20 T9 21 T10 T1 T6 22 T3 23 T16 24 T9 T1 T6 27 T18 28 T8 29 T9 T1 T4 T6

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

slide-7
SLIDE 7

Motivation and Background

Background

Problem: how to order the type specifiers and minimize redundancy. Two approaches

1

S-expression manipulation and heuristics.

2

Binary Decision Diagrams (BDD)

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

slide-8
SLIDE 8

Intro to Common Lisp Types and typecase

Table of Contents

1

Motivation and Background

2

Intro to Common Lisp Types and typecase

3

Optimization by s-expression transformation

4

Optimization using decision diagrams

5

Conclusion

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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 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

slide-14
SLIDE 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

slide-15
SLIDE 15

Optimization by s-expression transformation

Table of Contents

1

Motivation and Background

2

Intro to Common Lisp Types and typecase

3

Optimization by s-expression transformation

4

Optimization using decision diagrams

5

Conclusion

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

slide-16
SLIDE 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

slide-17
SLIDE 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

slide-18
SLIDE 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

slide-19
SLIDE 19

Optimization by s-expression transformation

Redundant type checks

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( 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

slide-20
SLIDE 20

Optimization by s-expression transformation

Redundant type checks

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( 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

slide-21
SLIDE 21

Optimization by s-expression transformation

Redundant type checks

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( 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

slide-22
SLIDE 22

Optimization by s-expression transformation

Unreachable code

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( 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

slide-23
SLIDE 23

Optimization by s-expression transformation

Unreachable code

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( 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. Perhaps programmer error

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

slide-24
SLIDE 24

Optimization by s-expression transformation

Unreachable code

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( 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. Perhaps programmer error However, your lisp compiler might not warn.

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

slide-25
SLIDE 25

Optimization by s-expression transformation

Exhaustiveness

Redundant type checks Unreachable code Exhaustiveness

( typecase

  • bj

(( not ( or number symbol )) ( f1 )) ( number ( f2 )) ( symbol ( f3 ) ) )

The final symbol check is unnecessary, can be replaced with T.

( typecase

  • bj

(( not ( or number symbol )) ( f1 )) ( number ( f2 )) ( t ( f3 ) ) )

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

slide-26
SLIDE 26

Optimization by s-expression transformation

Issues

Redundant type checks Unreachable code Exhaustiveness How to address these issues? Introducing: rewriting/forward-substitution/simplification according to heuristics.

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

slide-27
SLIDE 27

Optimization by s-expression transformation

Forward substitution

If line 3 is reached, then we know that (or number string symbol) failed.

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: (( and ( s a t i s f i e s p1 ) number ) ( f2 )) 4: (( and ( s a t i s f i e s p1 ) ( or symbol s t r i n g )) ( f3 ) ) )

Forward substitution: number ← nil string ← nil symbol ← nil

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: (( and ( s a t i s f i e s p1 ) n i l ) ( f2 )) 4: (( and ( s a t i s f i e s p1 ) ( or n i l n i l )) ( f3 ) ) )

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

slide-28
SLIDE 28

Optimization by s-expression transformation

... and Simplification

Forward substitution results expression which can be simplified.

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: ( ( and ( s a t i s f i e s p1 ) n i l ) ( f2 )) 4: ( ( and ( s a t i s f i e s p1 ) ( or n i l n i l )) ( f3 ) ) )

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

slide-29
SLIDE 29

Optimization by s-expression transformation

... and Simplification

Forward substitution results expression which can be simplified.

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: ( ( and ( s a t i s f i e s p1 ) n i l ) ( f2 )) 4: ( ( and ( s a t i s f i e s p1 ) ( or n i l n i l )) ( f3 ) ) )

After simplification via type-simplify

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: ( n i l ( f2 )) ; unreachable code detected 4: ( n i l ( f3 ) ) ) ; unreachable code detected

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

slide-30
SLIDE 30

Optimization by s-expression transformation

... and Simplification

Forward substitution results expression which can be simplified.

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: ( ( and ( s a t i s f i e s p1 ) n i l ) ( f2 )) 4: ( ( and ( s a t i s f i e s p1 ) ( or n i l n i l )) ( f3 ) ) )

After simplification via type-simplify

1: ( typecase

  • bj

2: (( or number s t r i n g symbol ) ( f1 )) 3: ( n i l ( f2 )) ; unreachable code detected 4: ( n i l ( f3 ) ) ) ; unreachable code detected

Your compiler will warn about unreachable code.

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

slide-31
SLIDE 31

Optimization by s-expression transformation

Order dependent clauses

Semantics of typecase depends on order of clauses. E.g., obj=2

( typecase

  • bj

( number ( f1 )) ( fixnum ( f2 )) ; f2 unreachable ( t ( f3 ) ) )

vs.

( typecase

  • bj

( fixnum ( f2 )) ; f2 r e a c h a b l e ( number ( f1 )) ( t ( f3 ) ) )

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

slide-32
SLIDE 32

Optimization by s-expression transformation

Order dependent clauses

Semantics of typecase depends on order of clauses. E.g., obj=2

( typecase

  • bj

( number ( f1 )) ( fixnum ( f2 )) ; f2 unreachable ( t ( f3 ) ) )

vs.

( typecase

  • bj

( fixnum ( f2 )) ; f2 r e a c h a b l e ( number ( f1 )) ( t ( f3 ) ) )

Unreachable code, but forward substitution does not find it.

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

slide-33
SLIDE 33

Optimization by s-expression transformation

Order dependent clauses

Semantics of typecase depends on order of clauses. E.g., obj=2

( typecase

  • bj

( number ( f1 )) ( fixnum ( f2 )) ; f2 unreachable ( t ( f3 ) ) )

vs.

( typecase

  • bj

( fixnum ( f2 )) ; f2 r e a c h a b l e ( number ( f1 )) ( t ( f3 ) ) )

Unreachable code, but forward substitution does not find it.

(f2) unreachable because fixnum ⊂ number

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

slide-34
SLIDE 34

Optimization by s-expression transformation

Rewriting

But, we can rewrite the type checks...

1: ( typecase

  • bj

2: ( number ( f1 )) 3: ( fixnum ( f2 )) 4: ( t ( f3 ) ) )

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

slide-35
SLIDE 35

Optimization by s-expression transformation

Rewriting

But, we can rewrite the type checks...

1: ( typecase

  • bj

2: ( number ( f1 )) 3: ( fixnum ( f2 )) 4: ( t ( f3 ) ) )

... to make previous failed clauses explicit.

1: ( typecase

  • bj

2: ( number ( f1 )) 3: (( and fixnum ( not number )) ( f2 )) 4: (( and t ( not ( or number fixnum ) ) ) ( f3 ) ) )

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

slide-36
SLIDE 36

Optimization by s-expression transformation

Rewriting

But, we can rewrite the type checks...

1: ( typecase

  • bj

2: ( number ( f1 )) 3: ( fixnum ( f2 )) 4: ( t ( f3 ) ) )

... to make previous failed clauses explicit.

1: ( typecase

  • bj

2: ( number ( f1 )) 3: (( and fixnum ( not number )) ( f2 )) 4: (( and t ( not ( or number fixnum ) ) ) ( f3 ) ) )

Simplify to find unreachable code (intersection of disjoint sets).

1: ( typecase

  • bj

2: ( number ( f1 )) 3: ( n i l ( f2 )) ; ; unreachable 4: (( not number ) ( f3 ) ) )

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

slide-37
SLIDE 37

Optimization by s-expression transformation

Rewriting

But, we can rewrite the type checks...

1: ( typecase

  • bj

2: ( number ( f1 )) 3: ( fixnum ( f2 )) 4: ( t ( f3 ) ) )

... to make previous failed clauses explicit.

1: ( typecase

  • bj

2: ( number ( f1 )) 3: (( and fixnum ( not number )) ( f2 )) 4: (( and t ( not ( or number fixnum ) ) ) ( f3 ) ) )

Simplify to find unreachable code (intersection of disjoint sets).

1: ( typecase

  • bj

2: ( number ( f1 )) 3: ( n i l ( f2 )) ; ; unreachable 4: (( not number ) ( f3 ) ) )

Moreover, the clauses can be reordered.

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

slide-38
SLIDE 38

Optimization by s-expression transformation

auto-permute-typecase macro

Clauses can be reordered after rewriting, maintaining semantics.

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

slide-39
SLIDE 39

Optimization by s-expression transformation

auto-permute-typecase macro

Clauses can be reordered after rewriting, maintaining semantics. Result of simplification depends on order of clauses.

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

slide-40
SLIDE 40

Optimization by s-expression transformation

auto-permute-typecase macro

Clauses can be reordered after rewriting, maintaining semantics. Result of simplification depends on order of clauses. Using a heuristic-cost function we can compare semantically equivalent expansions.

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

slide-41
SLIDE 41

Optimization by s-expression transformation

auto-permute-typecase macro

Clauses can be reordered after rewriting, maintaining semantics. Result of simplification depends on order of clauses. Using a heuristic-cost function we can compare semantically equivalent expansions. Implementation of auto-permute-typecase macro.

( defmacro auto−permute−typecase ( obj &r e s t c l a u s e s ) ( l e t (( best−order ( h e u r i s t i c − c o s t c l a u s e s )) ( c l a u s e s ( s i m p l i f y ( r e w r i t e c l a u s e s ) ) ) ) ( map−permutations ( perm c l a u s e s ) ( l e t (( candidate ( s i m p l i f y ( f or w a r d − s u b s t i t u t e perm ) ) ) ( when (< ( h e u r i s t i c − c o s t candidate ) ( h e u r i s t i c − c o s t best−order )) ( s e t f best−order candidate ) ) ) ) ( l i s t ∗ ’ typecase

  • bj

best−order ) ) )

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

slide-42
SLIDE 42

Optimization by s-expression transformation

auto-permute-typecase macro

Clauses can be reordered after rewriting, maintaining semantics. Result of simplification depends on order of clauses. Using a heuristic-cost function we can compare semantically equivalent expansions. Implementation of auto-permute-typecase macro.

( defmacro auto−permute−typecase ( obj &r e s t c l a u s e s ) ( l e t (( best−order ( h e u r i s t i c − c o s t c l a u s e s )) ( c l a u s e s ( s i m p l i f y ( r e w r i t e c l a u s e s ) ) ) ) ( map−permutations ( perm c l a u s e s ) ( l e t (( candidate ( s i m p l i f y ( f or w a r d − s u b s t i t u t e perm ) ) ) ( when (< ( h e u r i s t i c − c o s t candidate ) ( h e u r i s t i c − c o s t best−order )) ( s e t f best−order candidate ) ) ) ) ( l i s t ∗ ’ typecase

  • bj

best−order ) ) )

Finds permutation of clauses with minimum cost

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

slide-43
SLIDE 43

Optimization by s-expression transformation

Putting it together with auto-permute-typecase

Macro expansion example of auto-permute-typecase

( auto−permute−typecase

  • bj

(( or bignum unsigned−byte ) ( f1 )) ( s t r i n g ( f2 )) ( fixnum ( f3 )) (( or ( not s t r i n g ) ( not number )) ( f4 ) ) )

Macro expansion example of auto-permute-typecase

( typecase

  • bj

( s t r i n g ( f2 )) (( or bignum unsigned−byte ) ( f1 )) ( fixnum ( f3 )) ( t ( f4 ) ) )

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

slide-44
SLIDE 44

Optimization using decision diagrams

Table of Contents

1

Motivation and Background

2

Intro to Common Lisp Types and typecase

3

Optimization by s-expression transformation

4

Optimization using decision diagrams

5

Conclusion

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

slide-45
SLIDE 45

Optimization using decision diagrams

Re-ordering sometimes fails to eliminate redundancy

Sometimes no re-ordering of the typecase allows simplification.

( typecase

  • bj

(( and unsigned−byte ( not bignum )) body−forms−1 . . . ) (( and bignum ( not unsigned−byte )) body−forms−2 . . . ) )

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

slide-46
SLIDE 46

Optimization using decision diagrams

Re-ordering sometimes fails to eliminate redundancy

Sometimes no re-ordering of the typecase allows simplification.

( typecase

  • bj

(( and unsigned−byte ( not bignum )) body−forms−1 . . . ) (( and bignum ( not unsigned−byte )) body−forms−2 . . . ) )

Consider expanding typecase to if/then/else

( i f ( typep

  • bj

’ unsigned−byte ) ( i f ( typep

  • bj

’ bignum ) n i l ( progn body−forms−1 . . . ) ) ( i f ( typep

  • bj

’ bignum ) ( progn body−forms−2 . . . ) n i l ))

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

slide-47
SLIDE 47

Optimization using decision diagrams

Decision Diagram representing irreducible typecase

unsigned-byte bignum bignum ⊥ (progn body-forms-2...) (progn body-forms-1...)

This code flow diagram represents the calculation we want.

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

slide-48
SLIDE 48

Optimization using decision diagrams

Decision Diagram representing irreducible typecase

unsigned-byte bignum bignum ⊥ (progn body-forms-2...) (progn body-forms-1...)

This code flow diagram represents the calculation we want. It is similar to an ROBDD.

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

slide-49
SLIDE 49

Optimization using decision diagrams

What is an ROBDD?

Reduced Ordered Binary Decision Diagram, a data structure for representing an manipulating Boolean expressions. Using Boolean algebra notation AC D + ABC D + A B D

A C B ⊥ D T Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 28 / 41

slide-50
SLIDE 50

Optimization using decision diagrams

What is an ROBDD?

Reduced Ordered Binary Decision Diagram, a data structure for representing an manipulating Boolean expressions. Using Boolean algebra notation AC D + ABC D + A B D Using Common Lisp type specifier notation

( or ( and A ( not C) ( not D)) ( and ( not A) B ( not C) ( not D)) ( and ( not A) ( not B) ( not D) ) )

A C B ⊥ D T Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 28 / 41

slide-51
SLIDE 51

Optimization using decision diagrams

Type specifier as ROBDD

CL-ROBDD Can create and manipulate ROBDDs which correspond to Common Lisp type specifiers.

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

slide-52
SLIDE 52

Optimization using decision diagrams

Type specifier as ROBDD

CL-ROBDD Can create and manipulate ROBDDs which correspond to Common Lisp type specifiers. Adapted to accommodate subtype relations.

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

slide-53
SLIDE 53

Optimization using decision diagrams

Type specifier as ROBDD

CL-ROBDD Can create and manipulate ROBDDs which correspond to Common Lisp type specifiers. Adapted to accommodate subtype relations. Can serialize such ROBDDs to efficient Common Lisp code.

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

slide-54
SLIDE 54

Optimization using decision diagrams

Type specifier as ROBDD

CL-ROBDD Can create and manipulate ROBDDs which correspond to Common Lisp type specifiers. Adapted to accommodate subtype relations. Can serialize such ROBDDs to efficient Common Lisp code. Question: Can we convert typecase into a type specifier?

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

slide-55
SLIDE 55

Optimization using decision diagrams

Type specifier as ROBDD

CL-ROBDD Can create and manipulate ROBDDs which correspond to Common Lisp type specifiers. Adapted to accommodate subtype relations. Can serialize such ROBDDs to efficient Common Lisp code. Question: Can we convert typecase into a type specifier? Answer: Yes.

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

slide-56
SLIDE 56

Optimization using decision diagrams

Transform body-forms into predicates

We’d like to build an ROBDD to represent a typecase (typecase

  • bj

(T.1 body-forms-1 ...) (T.2 body-forms-2 ...) ... (T.n body-forms-n ...))

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

slide-57
SLIDE 57

Optimization using decision diagrams

Transform body-forms into predicates

We’d like to build an ROBDD to represent a typecase (typecase

  • bj

(T.1 body-forms-1 ...) (T.2 body-forms-2 ...) ... (T.n body-forms-n ...)) Encapsulate body-forms into named predicate functions. P1 ← (encapsulate-as-predicate body-forms-1...) P2 ← (encapsulate-as-predicate body-forms-2...) ... Pn ← (encapsulate-as-predicate body-forms-n...)

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

slide-58
SLIDE 58

Optimization using decision diagrams

Transform typecase into type specifier

(typecase

  • bj

(T.1 body-forms-1 ...) (T.2 body-forms-2 ...) ... (T.n body-forms-n ...)) Convert typecase to disjunctive normal form (DNF). (or (and T.1 (satisfies P1)) (and T.2 (not T.1) (satisfies P2)) ... (and T.n (not (or T.1 T.2 ... T.n-1)) (satisfies Pn)))

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

slide-59
SLIDE 59

Optimization using decision diagrams

ROBDD with temporary valid satisfies types

( bdd−typecase

  • bj

(( and unsigned−byte ( not bignum )) body−forms−1 . . . ) (( and bignum ( not unsigned−byte )) body−forms−2 . . . ) )

unsigned-byte bignum bignum ⊥ (satisfies P2) (satisfies P1) T

Now we can represent a difficult typecase as an ROBDD.

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

slide-60
SLIDE 60

Optimization using decision diagrams

Advantages of ROBDD representation of typecase

unsigned-byte bignum bignum ⊥ (satisfies P2) (satisfies P1) T

No type check is done twice. Missing (satisfies P...) corresponds to unreachable code. If a path to ⊥ avoids (satisfies

P...), then the typecase is not

exhaustive. Serializable to efficient Common Lisp code.

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

slide-61
SLIDE 61

Optimization using decision diagrams

Bigger bdd-typecase example

Invocation of bdd-typecase (bdd-typecase

  • bj

((and unsigned-byte (not (eql 42))) body-forms-1 ...) ((eql 42) body-forms-2 ...) ((and number (not (eql 42)) (not fixnum )) body-forms-3 ...) (fixnum body-forms-4 ...))

fixnum unsigned-byte number (eql 42) (satisfies P4) unsigned-byte ⊥ (satisfies P2) (satisfies P1) T (satisfies P3)

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

slide-62
SLIDE 62

Optimization using decision diagrams

Bigger bdd-typecase example

fixnum unsigned-byte number (eql 42) (satisfies P4) unsigned-byte ⊥ (satisfies P2) (satisfies P1) T (satisfies P3)

No duplicate type checks.

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

slide-63
SLIDE 63

Optimization using decision diagrams

Bigger bdd-typecase example

fixnum unsigned-byte number (eql 42) (satisfies P4) unsigned-byte ⊥ (satisfies P2) (satisfies P1) T (satisfies P3)

No duplicate type checks. No super-type checks.

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

slide-64
SLIDE 64

Optimization using decision diagrams

Bigger bdd-typecase simplified example with tagbody/go.

( l e t ( ( obj

  • bj ))

( tagbody L1 ( i f ( typep

  • bj

’ fixnum ) ( go L2 ) ( go L4 ) ) L2 ( i f ( typep

  • bj

’ unsigned−byte ) ( go L3 ) ( go P4 )) L3 ( i f ( typep

  • bj

’( e q l 42)) ( go P2) ( go P1 )) L4 ( i f ( typep

  • bj

’ number ) ( go L5 ) ( r e t u r n n i l ) ) L5 ( i f ( typep

  • bj

’ unsigned−byte ) ( go P1) ( go P3 )) P1 ( r e t u r n ( progn body−forms−1 . . . ) ) P2 ( r e t u r n ( progn body−forms−2 . . . ) ) P3 ( r e t u r n ( progn body−forms−3 . . . ) ) P4 ( r e t u r n ( progn body−forms−4 . . . ) ) ) )

fixnum unsigned-byte number (eql 42) (satisfies P4) unsigned-byte ⊥ (satisfies P2) (satisfies P1) T (satisfies P3)

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

slide-65
SLIDE 65

Optimization using decision diagrams

Bigger bdd-typecase example with labels.

( l e t ( ( obj

  • bj ))

( l a b e l s (( L1 () ( i f ( typep

  • bj

’ fixnum ) ( L2 ) ( L4 ) ) ) ( L2 () ( i f ( typep

  • bj

’ unsigned−byte ) ( L3 ) (P4 ) ) ) ( L3 () ( i f ( typep

  • bj

’( e q l 42)) (P2) (P1 ) ) ) ( L4 () ( i f ( typep

  • bj

’ number ) ( L5 ) n i l ) ) ( L5 () ( i f ( typep

  • bj

’ unsigned−byte ) (P1) (P3 ) ) ) (P1 ( ) body−forms−1 . . . ) (P2 ( ) body−forms−2 . . . ) (P3 ( ) body−forms−3 . . . ) (P4 ( ) body−forms−4 . . . ) ) ( L1 ) ) )

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

slide-66
SLIDE 66

Optimization using decision diagrams

ROBDD worst case size

N |ROBDDN| 1 3 2 5 3 7 4 11 5 19 6 31 7 47 8 79 9 143 10 271 11 511 12 767 13 1279 14 2303 15 4351 Number of labels is number of nodes in the ROBDD.

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

slide-67
SLIDE 67

Optimization using decision diagrams

ROBDD worst case size

N |ROBDDN| 1 3 2 5 3 7 4 11 5 19 6 31 7 47 8 79 9 143 10 271 11 511 12 767 13 1279 14 2303 15 4351 Number of labels is number of nodes in the ROBDD. Worst case code size for N type checks (including pseudo-predicates), proportional to full ROBDD size for N variables.

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

slide-68
SLIDE 68

Optimization using decision diagrams

ROBDD worst case size

N |ROBDDN| 1 3 2 5 3 7 4 11 5 19 6 31 7 47 8 79 9 143 10 271 11 511 12 767 13 1279 14 2303 15 4351 Number of labels is number of nodes in the ROBDD. Worst case code size for N type checks (including pseudo-predicates), proportional to full ROBDD size for N variables. Worst case size is calculable. |ROBDDN| = (2N−θ − 1) + 22θ where ⌈log2(N − 2 − log2 N)⌉ − 2 ≤ θ ≤ ⌊log2 N⌋

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

slide-69
SLIDE 69

Optimization using decision diagrams

ROBDD worst case size

N |ROBDDN| 1 3 2 5 3 7 4 11 5 19 6 31 7 47 8 79 9 143 10 271 11 511 12 767 13 1279 14 2303 15 4351 Number of labels is number of nodes in the ROBDD. Worst case code size for N type checks (including pseudo-predicates), proportional to full ROBDD size for N variables. Worst case size is calculable. |ROBDDN| = (2N−θ − 1) + 22θ where ⌈log2(N − 2 − log2 N)⌉ − 2 ≤ θ ≤ ⌊log2 N⌋ But our ROBDD is never worst-case.

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

slide-70
SLIDE 70

Conclusion

Table of Contents

1

Motivation and Background

2

Intro to Common Lisp Types and typecase

3

Optimization by s-expression transformation

4

Optimization using decision diagrams

5

Conclusion

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

slide-71
SLIDE 71

Conclusion

Summary

auto-permute-typecase : find best simplification by exhaustive

search

Combinatorical compile-time complexity Sometimes fails to remove duplicate checks. Difficult to implement a good/fast type-simplify function, (subtypep et.al.). Heuristic function, topic for more research.

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

slide-72
SLIDE 72

Conclusion

Summary

auto-permute-typecase : find best simplification by exhaustive

search

Combinatorical compile-time complexity Sometimes fails to remove duplicate checks. Difficult to implement a good/fast type-simplify function, (subtypep et.al.). Heuristic function, topic for more research. bdd-typecase : expand typecase into inline state machine. Eliminates duplicate checks Exponential code size Always removes duplicate type checks Ongoing research to optimize CL-ROBDD.

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

slide-73
SLIDE 73

Conclusion

Summary

auto-permute-typecase : find best simplification by exhaustive

search

Combinatorical compile-time complexity Sometimes fails to remove duplicate checks. Difficult to implement a good/fast type-simplify function, (subtypep et.al.). Heuristic function, topic for more research. bdd-typecase : expand typecase into inline state machine. Eliminates duplicate checks Exponential code size Always removes duplicate type checks Ongoing research to optimize CL-ROBDD.

Both approaches

Find unreachable code Find non-exhaustive cases

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

slide-74
SLIDE 74

Conclusion

Questions/Answers

Questions?

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

slide-75
SLIDE 75

Examples of some Common Lisp types, and their intersections

unsigned-byte bit fixnum rational float number

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

slide-76
SLIDE 76

Type specifiers are powerful and intuitive

Homoiconicity makes type specifiers intuitive and flexible. Simple

integer

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

slide-77
SLIDE 77

Type specifiers are powerful and intuitive

Homoiconicity makes type specifiers intuitive and flexible. Simple

integer

Compound type specifiers

(satisfies oddp) (float (0.0) 1.0) (member 2 5 7 11)

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

slide-78
SLIDE 78

Type specifiers are powerful and intuitive

Homoiconicity makes type specifiers intuitive and flexible. Simple

integer

Compound type specifiers

(satisfies oddp) (float (0.0) 1.0) (member 2 5 7 11)

Logical combinations

(and (or number string) (not (satisfies MY-FUN)))

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

slide-79
SLIDE 79

Type specifiers are powerful and intuitive

Homoiconicity makes type specifiers intuitive and flexible. Simple

integer

Compound type specifiers

(satisfies oddp) (float (0.0) 1.0) (member 2 5 7 11)

Logical combinations

(and (or number string) (not (satisfies MY-FUN)))

Specifiers for the empty type

nil (and number string)

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

slide-80
SLIDE 80

Macro expansion of typecase

Example macroexpand-1 from SBCL.

( typecase x (( and fixnum ( not ( e q l 0 ) ) ) ( f1 )) (( e q l 0) ( f2 )) ( symbol ( f3 )) ( t ( f4 ) ) ) ; ; macro expansion ( l e t ((\#: g604 x )) ( d e c l a r e ( i g n o r a b l e \#:g604 )) ( cond (( typep \#:g604 ’( and fixnum ( not ( e q l 0 ) ) ) ) n i l ( f1 )) (( typep \#:g604 ’( e q l 0)) n i l ( f2 )) (( typep \#:g604 ’ symbol ) n i l ( f3 )) ( t n i l ( f4 ) ) ) )

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

slide-81
SLIDE 81

Macro expansion of typecase

Example macroexpand-1 from SBCL.

( typecase x (( and fixnum ( not ( e q l 0 ) ) ) ( f1 )) (( e q l 0) ( f2 )) ( symbol ( f3 )) ( t ( f4 ) ) ) ; ; macro expansion ( l e t ((\#: g604 x )) ( d e c l a r e ( i g n o r a b l e \#:g604 )) ( cond (( typep \#:g604 ’( and fixnum ( not ( e q l 0 ) ) ) ) n i l ( f1 )) (( typep \#:g604 ’( e q l 0)) n i l ( f2 )) (( typep \#:g604 ’ symbol ) n i l ( f3 )) ( t n i l ( f4 ) ) ) )

We can clean up the expansion to make it easier to understand.

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

slide-82
SLIDE 82

Macro expansion of typecase

( typecase x (( and fixnum ( not ( e q l 0 ) ) ) ( f1 )) (( e q l 0) ( f2 )) ( symbol ( f3 )) ( t ( f4 ) ) )

Temporary variable because x might be an expression.

( l e t ((\#: g604 x )) ( d e c l a r e ( i g n o r a b l e \#:g604 )) ( cond (( typep \#:g604 ’( and fixnum ( not ( e q l 0 ) ) ) ) n i l ( f1 )) (( typep \#:g604 ’( e q l 0)) n i l ( f2 )) (( typep \#:g604 ’ symbol ) n i l ( f3 )) ( t n i l ( f4 ) ) ) )

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

slide-83
SLIDE 83

Macro expansion of typecase

( typecase x (( and fixnum ( not ( e q l 0 ) ) ) ( f1 )) (( e q l 0) ( f2 )) ( symbol ( f3 )) ( t ( f4 ) ) )

Protection against certain trivial/degenerate cases.

( l e t ((\#: g604 x )) ( d e c l a r e ( i g n o r a b l e \#:g604 )) ( cond (( typep \#:g604 ’( and fixnum ( not ( e q l 0 ) ) ) ) n i l ( f1 )) (( typep \#:g604 ’( e q l 0)) n i l ( f2 )) (( typep \#:g604 ’ symbol ) n i l ( f3 )) ( t n i l ( f4 ) ) ) )

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

slide-84
SLIDE 84

Machine generated, redundant checks

Redundant type checks Unreachable code Exhaustiveness

( typecase ( prog1 ( a r e f seq i ) ( i n c f i )) ( fixnum ( go 7)) (( and r e a l ( not fixnum ) ( not r a t i o )) ( go 11)) (( or r a t i o ( and number ( not r e a l ) ) ) ( go 10)) ( t ( return−from check n i l ) ) )

Example of machine-generated code containing repeated type checks:

fixnum and ratio.

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

slide-85
SLIDE 85

Reorderable clauses

( typecase

  • bj

( fixnum ( f1 )) (( and number ( not fixnum )) ( f2 )) (( and t ( not ( or fixnum number ) ) ) ( f3 ) ) )

Now the clauses can be reordered.

( typecase

  • bj

(( and number ( not fixnum )) ( f2 )) ( fixnum ( f1 )) (( and t ( not ( or fixnum number ) ) ) ( f3 ) ) )

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

slide-86
SLIDE 86

Heuristics for code cost

When comparing two type specifiers:

built-in types are cheap satisfies is expensive and, or, not cost depend on the tree size

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

slide-87
SLIDE 87

Heuristics for code cost

When comparing two type specifiers:

built-in types are cheap satisfies is expensive and, or, not cost depend on the tree size

When comparing two typecase expressions:

Better to have simple expressions early

( typecase

  • bj

( fixnum ( f1 )) (( and number ( not r a t i o )) ( f2 ) ) )

Than to have complex expressions early

( typecase

  • bj

(( and number ( not r a t i o )) ( f2 )) ( fixnum ( f1 ) ) )

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