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
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
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
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
Motivation and Background
Code generated from RTE state machine
(tagbody
0
(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)))
0 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
Motivation and Background
More complicated State Machine
0 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
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
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
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
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
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
Intro to Common Lisp Types and typecase
What is typecase ?
Simple example of typecase
( t y p e c a s e e x p r
( fix nu m bodyforms1 . . . )
( number bodyforms2 . . . )
( s t r i n g bodyforms3 . . . ) )
typecase may use any valid type specifier.
( t y p e c a s e e x p r
( ( and f ix nu m ( not ( e q l 0 ) ) ) bodyforms1 . . . )
( ( o r f ix nu m s t r i n g ) bodyforms2 . . . )
( ( me mber 1 2) bodyforms3 . . . )
( ( s a t i s f i e s MYFUN) bodyforms4 . . . )
. . . )
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
Intro to Common Lisp Types and typecase
What is typecase ?
Simple example of typecase
( t y p e c a s e e x p r
( f ix nu m bodyforms1 . . . )
( number bodyforms2 . . . )
( s t r i n g bodyforms3 . . . ) )
typecase may use any valid type specifier.
( t y p e c a s e e x p r
( ( and f ix nu m ( no t ( e q l 0 ) ) ) bodyforms1 . . . )
( ( o r f ix nu m s t r i n g ) bodyforms2 . . . )
( ( me mber 1 2) bodyforms3 . . . )
( ( s a t i s f i e s MYFUN) bodyforms4 . . . )
. . . )
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
Intro to Common Lisp Types and typecase
What is typecase ?
Simple example of typecase
( t y p e c a s e e x p r
( f ix nu m bodyforms1 . . . )
( number bodyforms2 . . . )
( s t r i n g bodyforms3 . . . ) )
typecase may use any valid type specifier.
( t y p e c a s e e x p r
( ( and f ix nu m ( no t ( e q l 0 ) ) ) bodyforms1 . . . )
( ( o r f ix nu m s t r i n g ) bodyforms2 . . . )
( ( me mber 1 2) bodyforms3 . . . )
( ( s a t i s f i e s MYFUN) bodyforms4 . . . )
. . . )
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
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
Optimization by s-expression transformation
Macro expansion of typecase
We can use macroexpand-1 from SBCL.
( t y p e c a s e x
( ( and f ix nu m ( no t ( e q l 0 ) ) ) ( f 1 ) )
( ( e q l 0) ( f 2 ) )
( symb ol ( f 3 ) )
( t ( f 4 ) ) )
The expansion essentially involves cond and typep.
( cond ( ( ty p ep x ( and f ix nu m ( no t ( e q l 0 ) ) ) )
( f 1 ) )
( ( t y p e p x ( e q l 0 ) )
( f 2 ) )
( ( t y p e p x symb ol )
( f 3 ) )
( t
( f 4 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 13 / 41
Optimization by s-expression transformation
Macro expansion of typecase
We can use macroexpand-1 from SBCL.
( t y p e c a s e x
( ( and f ix nu m ( no t ( e q l 0 ) ) ) ( f 1 ) )
( ( e q l 0) ( f 2 ) )
( symb ol ( f 3 ) )
( t ( f 4 ) ) )
The expansion essentially involves cond and typep.
( cond ( ( ty p ep x ( and f ix nu m ( no t ( e q l 0 ) ) ) )
( f 1 ) )
( ( t y p e p x ( e q l 0 ) )
( f 2 ) )
( ( t y p e p x symb ol )
( f 3 ) )
( t
( f 4 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 13 / 41
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
Optimization by s-expression transformation
Redundant type checks
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( and f ix nu m ( no t bignum ) ) ( f 1 ) )
( ( and bignum ( not u n si g ne d b y te ) ) ( f 2 ) )
( bignum ( f 3 ) ) )
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
Optimization by s-expression transformation
Redundant type checks
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( and f ix nu m ( no t bignum ) ) ( f 1 ) )
( ( and bignum ( not u n si g ne d b y te ) ) ( f 2 ) )
( bignum ( f 3 ) ) )
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
Optimization by s-expression transformation
Redundant type checks
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( and f ix nu m ( no t bignum ) ) ( f 1 ) )
( ( and bignum ( not u n si g ne d b y te ) ) ( f 2 ) )
( bignum ( f 3 ) ) )
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
Optimization by s-expression transformation
Unreachable code
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( o r number s t r i n g sy mbol ) ( f 1 ) )
( ( and ( s a t i s f i e s s l o w p r e d i c a t e ) number ) ( f 2 ) )
( ( and ( s a t i s f i e s s l o w p r e d i c a t e ) ( o r sy m bol s t r i n g ) ) ( f 3 ) ) )
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
Optimization by s-expression transformation
Unreachable code
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( o r number s t r i n g sy mbol ) ( f 1 ) )
( ( and ( s a t i s f i e s s l o w p r e d i c a t e ) number ) ( f 2 ) )
( ( and ( s a t i s f i e s s l o w p r e d i c a t e ) ( o r sy m bol s t r i n g ) ) ( f 3 ) ) )
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
Optimization by s-expression transformation
Unreachable code
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( o r number s t r i n g sy mbol ) ( f 1 ) )
( ( and ( s a t i s f i e s s l o w p r e d i c a t e ) number ) ( f 2 ) )
( ( and ( s a t i s f i e s s l o w p r e d i c a t e ) ( o r sy m bol s t r i n g ) ) ( f 3 ) ) )
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
Optimization by s-expression transformation
Exhaustiveness
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e o b j
( ( not ( o r number symbol ) ) ( f 1 ) )
( number ( f 2 ) )
( symb ol ( f 3 ) ) )
The final symbol check is unnecessary, can be replaced with T.
( t y p e c a s e o b j
( ( not ( o r number symbol ) ) ( f 1 ) )
( number ( f 2 ) )
( t ( f 3 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 17 / 41
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
Optimization by s-expression transformation
Forward substitution
If line 3 is reached, then we know that (or number string symbol) failed.
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( ( and ( s a t i s f i e s p1 ) number ) ( f 2 ) )
4 : ( ( and ( s a t i s f i e s p1 ) ( o r symbo l s t r i n g ) ) ( f 3 ) ) )
Forward substitution:
number nil
string nil
symbol nil
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( ( and ( s a t i s f i e s p1 ) n i l ) ( f 2 ) )
4 : ( ( and ( s a t i s f i e s p1 ) ( o r n i l n i l ) ) ( f 3 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 19 / 41
Optimization by s-expression transformation
... and Simplification
Forward substitution results expression which can be simplified.
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( ( and ( s a t i s f i e s p1 ) n i l ) ( f 2 ) )
4 : ( ( and ( s a t i s f i e s p1 ) ( o r n i l n i l ) ) ( f 3 ) ) )
After simplification via type-simplify
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; u n r e a c h a b l e code d e t e c t e d
4 : ( n i l ( f 3 ) ) ) ; u n r e a c h a b l e co d e d e t e c t e d
Your compiler will warn about unreachable code.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 20 / 41
Optimization by s-expression transformation
... and Simplification
Forward substitution results expression which can be simplified.
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( ( and ( s a t i s f i e s p1 ) n i l ) ( f 2 ) )
4 : ( ( and ( s a t i s f i e s p1 ) ( o r n i l n i l ) ) ( f 3 ) ) )
After simplification via type-simplify
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; u n r e a c h a b l e code d e t e c t e d
4 : ( n i l ( f 3 ) ) ) ; u n r e a c h a b l e co d e d e t e c t e d
Your compiler will warn about unreachable code.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 20 / 41
Optimization by s-expression transformation
... and Simplification
Forward substitution results expression which can be simplified.
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( ( and ( s a t i s f i e s p1 ) n i l ) ( f 2 ) )
4 : ( ( and ( s a t i s f i e s p1 ) ( o r n i l n i l ) ) ( f 3 ) ) )
After simplification via type-simplify
1 : ( t y p e c a s e o b j
2 : ( ( o r number s t r i n g sy mbol ) ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; u n r e a c h a b l e code d e t e c t e d
4 : ( n i l ( f 3 ) ) ) ; u n r e a c h a b l e co d e d e t e c t e d
Your compiler will warn about unreachable code.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 20 / 41
Optimization by s-expression transformation
Order dependent clauses
Semantics of typecase depends on order of clauses. E.g., obj=2
( t y p e c a s e o b j
( number ( f 1 ) )
( f ix nu m ( f 2 ) ) ; f 2 u n r e a c h a b l e
( t ( f 3 ) ) )
vs.
( t y p e c a s e o b j
( f ix nu m ( f 2 ) ) ; f 2 r e a c h a b l e
( number ( f 1 ) )
( t ( f 3 ) ) )
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
Optimization by s-expression transformation
Order dependent clauses
Semantics of typecase depends on order of clauses. E.g., obj=2
( t y p e c a s e o b j
( number ( f 1 ) )
( f ix nu m ( f 2 ) ) ; f 2 u n r e a c h a b l e
( t ( f 3 ) ) )
vs.
( t y p e c a s e o b j
( f ix nu m ( f 2 ) ) ; f 2 r e a c h a b l e
( number ( f 1 ) )
( t ( f 3 ) ) )
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
Optimization by s-expression transformation
Order dependent clauses
Semantics of typecase depends on order of clauses. E.g., obj=2
( t y p e c a s e o b j
( number ( f 1 ) )
( f ix nu m ( f 2 ) ) ; f 2 u n r e a c h a b l e
( t ( f 3 ) ) )
vs.
( t y p e c a s e o b j
( f ix nu m ( f 2 ) ) ; f 2 r e a c h a b l e
( number ( f 1 ) )
( t ( f 3 ) ) )
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
Optimization by s-expression transformation
Rewriting
But, we can rewrite the type checks...
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( f ix nu m ( f 2 ) )
4 : ( t ( f 3 ) ) )
... to make previous failed clauses explicit.
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( ( and f ix nu m ( no t number ) ) ( f 2 ) )
4 : ( ( and t ( not ( o r number f ix nu m ) ) ) ( f 3 ) ) )
Simplify to find unreachable code (intersection of disjoint sets).
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; ; u n r e a c h a b l e
4 : ( ( not number ) ( f 3 ) ) )
Moreover, the clauses can be reordered.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 22 / 41
Optimization by s-expression transformation
Rewriting
But, we can rewrite the type checks...
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( f ix nu m ( f 2 ) )
4 : ( t ( f 3 ) ) )
... to make previous failed clauses explicit.
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( ( and f ix nu m ( no t number ) ) ( f 2 ) )
4 : ( ( and t ( not ( o r number f ix nu m ) ) ) ( f 3 ) ) )
Simplify to find unreachable code (intersection of disjoint sets).
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; ; u n r e a c h a b l e
4 : ( ( not number ) ( f 3 ) ) )
Moreover, the clauses can be reordered.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 22 / 41
Optimization by s-expression transformation
Rewriting
But, we can rewrite the type checks...
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( f ix nu m ( f 2 ) )
4 : ( t ( f 3 ) ) )
... to make previous failed clauses explicit.
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( ( and f ix nu m ( no t number ) ) ( f 2 ) )
4 : ( ( and t ( not ( o r number f ix nu m ) ) ) ( f 3 ) ) )
Simplify to find unreachable code (intersection of disjoint sets).
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; ; u n r e a c h a b l e
4 : ( ( not number ) ( f 3 ) ) )
Moreover, the clauses can be reordered.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 22 / 41
Optimization by s-expression transformation
Rewriting
But, we can rewrite the type checks...
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( f ix nu m ( f 2 ) )
4 : ( t ( f 3 ) ) )
... to make previous failed clauses explicit.
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( ( and f ix nu m ( no t number ) ) ( f 2 ) )
4 : ( ( and t ( not ( o r number f ix nu m ) ) ) ( f 3 ) ) )
Simplify to find unreachable code (intersection of disjoint sets).
1 : ( t y p e c a s e o b j
2 : ( number ( f 1 ) )
3 : ( n i l ( f 2 ) ) ; ; u n r e a c h a b l e
4 : ( ( not number ) ( f 3 ) ) )
Moreover, the clauses can be reordered.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 22 / 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.
( de fm ac ro auto perm ute t ypec ase ( o b j &r e s t c l a u s e s )
( l e t (( b e s t o r d e r ( 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 ) ) ) )
( mappermutations ( perm c l a u s e s )
( l e t (( c a n d i d a t e ( s i m p l i f y ( f o r 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 c a n d i d a t e )
( h e u r i s t i c c o s t b e s t o r d e r ) )
( s e t f b e s t o r d e r c a n d i d a t e ) ) ) )
( l i s t t y p e c a s e o b j b e s t o r d e r ) ) )
Finds permutation of clauses with minimum cost
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 23 / 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.
( de fm ac ro auto perm ute t ypec ase ( o b j &r e s t c l a u s e s )
( l e t (( b e s t o r d e r ( 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 ) ) ) )
( mappermutations ( perm c l a u s e s )
( l e t (( c a n d i d a t e ( s i m p l i f y ( f o r 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 c a n d i d a t e )
( h e u r i s t i c c o s t b e s t o r d e r ) )
( s e t f b e s t o r d e r c a n d i d a t e ) ) ) )
( l i s t t y p e c a s e o b j b e s t o r d e r ) ) )
Finds permutation of clauses with minimum cost
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 23 / 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.
( de fm ac ro auto perm ute t ypec ase ( o b j &r e s t c l a u s e s )
( l e t (( b e s t o r d e r ( 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 ) ) ) )
( mappermutations ( perm c l a u s e s )
( l e t (( c a n d i d a t e ( s i m p l i f y ( f o r 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 c a n d i d a t e )
( h e u r i s t i c c o s t b e s t o r d e r ) )
( s e t f b e s t o r d e r c a n d i d a t e ) ) ) )
( l i s t t y p e c a s e o b j b e s t o r d e r ) ) )
Finds permutation of clauses with minimum cost
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 23 / 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.
( de fm ac ro auto perm ute t ypec ase ( o b j &r e s t c l a u s e s )
( l e t (( b e s t o r d e r ( 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 ) ) ) )
( mappermutations ( perm c l a u s e s )
( l e t (( c a n d i d a t e ( s i m p l i f y ( f o r 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 c a n d i d a t e )
( h e u r i s t i c c o s t b e s t o r d e r ) )
( s e t f b e s t o r d e r c a n d i d a t e ) ) ) )
( l i s t t y p e c a s e o b j b e s t o r d e r ) ) )
Finds permutation of clauses with minimum cost
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 23 / 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.
( de fm ac ro auto perm ute t ypec ase ( o b j &r e s t c l a u s e s )
( l e t (( b e s t o r d e r ( 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 ) ) ) )
( mappermutations ( perm c l a u s e s )
( l e t (( c a n d i d a t e ( s i m p l i f y ( f o r 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 c a n d i d a t e )
( h e u r i s t i c c o s t b e s t o r d e r ) )
( s e t f b e s t o r d e r c a n d i d a t e ) ) ) )
( l i s t t y p e c a s e o b j b e s t o r d e r ) ) )
Finds permutation of clauses with minimum cost
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 23 / 41
Optimization by s-expression transformation
Putting it together with auto-permute-typecase
Macro expansion example of auto-permute-typecase
( auto perm ute t ypec ase o b j
( ( o r bignum un s ig n ed by t e ) ( f 1 ) )
( s t r i n g ( f 2 ) )
( f ix nu m ( f 3 ) )
( ( o r ( not s t r i n g ) ( no t number ) ) ( f 4 ) ) )
Macro expansion example of auto-permute-typecase
( t y p e c a s e o b j
( s t r i n g ( f 2 ) )
( ( o r bignum un s ig n ed by t e ) ( f 1 ) )
( f ix nu m ( f 3 ) )
( t ( f 4 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 24 / 41
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
Optimization using decision diagrams
Re-ordering sometimes fails to eliminate redundancy
Sometimes no re-ordering of the typecase allows simplification.
( t y p e c a s e o b j
( ( and u n si g ne d b y te ( not bignum ) )
bodyforms1 . . . )
( ( and bignum ( not u n si g ne d b y te ) )
bodyforms2 . . . ) )
Consider expanding typecase to if/then/else
( i f ( t y pe p ob j un s ig n ed by t e )
( i f ( t y pe p ob j bignum )
n i l
( pr o gn bodyforms1 . . . ) )
( i f ( t y pe p ob j bignum )
( pr o gn bodyforms2 . . . )
n i l ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 26 / 41
Optimization using decision diagrams
Re-ordering sometimes fails to eliminate redundancy
Sometimes no re-ordering of the typecase allows simplification.
( t y p e c a s e o b j
( ( and u n si g ne d b y te ( not bignum ) )
bodyforms1 . . . )
( ( and bignum ( not u n si g ne d b y te ) )
bodyforms2 . . . ) )
Consider expanding typecase to if/then/else
( i f ( t y pe p ob j un s ig n ed by t e )
( i f ( t y pe p ob j bignum )
n i l
( pr o gn bodyforms1 . . . ) )
( i f ( t y pe p ob j bignum )
( pr o gn bodyforms2 . . . )
n i l ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 26 / 41
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
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
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
( o r ( and A ( n ot C) ( n ot D) )
( and ( not A) B ( not C) ( no t D) )
( and ( not A) ( no t B) ( not D) ) )
A
C
B
D
T
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 28 / 41
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
( o r ( and A ( n ot C) ( n ot D) )
( and ( not A) B ( not C) ( no t D) )
( and ( not A) ( no t B) ( not D) ) )
A
C
B
D
T
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 28 / 41
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
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
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
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
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
Optimization using decision diagrams
Transform body-forms into predicates
We’d like to build an ROBDD to represent a typecase
( typecase obj
(T .1 body-forms-1 ...)
(T .2 body-forms-2 ...)
...
(T.n body-forms-n ...))
Encapsulate body-forms into named predicate functions.
P
1
(encapsulate-as-predicate body-forms-1...)
P
2
(encapsulate-as-predicate body-forms-2...)
...
P
n
(encapsulate-as-predicate body-forms-n...)
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 30 / 41
Optimization using decision diagrams
Transform body-forms into predicates
We’d like to build an ROBDD to represent a typecase
( typecase obj
(T .1 body-forms-1 ...)
(T .2 body-forms-2 ...)
...
(T.n body-forms-n ...))
Encapsulate body-forms into named predicate functions.
P
1
(encapsulate-as-predicate body-forms-1...)
P
2
(encapsulate-as-predicate body-forms-2...)
...
P
n
(encapsulate-as-predicate body-forms-n...)
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 30 / 41
Optimization using decision diagrams
Transform typecase into type specifier
( typecase obj
(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
( satis fies P1 ))
( and T.2 ( not T .1)
( satis fies P2 ))
...
( and T. n ( not ( or T .1 T .2 ... T. n-1 ))
( satis fies Pn )))
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 31 / 41
Optimization using decision diagrams
ROBDD with temporary valid satisfies types
( bdd typ ecas e o b j
( ( and u n si g ne d b y te
( no t bignum ) )
bodyforms1 . . . )
( ( and bignum
( no t un s ig n ed by t e ) )
bodyforms2 . . . ) )
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
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
Optimization using decision diagrams
Bigger bdd-typecase example
Invocation of bdd-typecase
( bdd-typecase obj
(( and unsi gned-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
nil
(satisfies P2)(satisfies P1)
T
(satisfies P3)
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 34 / 41
Optimization using decision diagrams
Bigger bdd-typecase example
fixnum
unsigned-byte
number
(eql 42)
(satisfies P4)
unsigned-byte
nil
(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
Optimization using decision diagrams
Bigger bdd-typecase example
fixnum
unsigned-byte
number
(eql 42)
(satisfies P4)
unsigned-byte
nil
(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
Optimization using decision diagrams
Bigger bdd-typecase simplified example with tagbody/go.
( l e t ( ( o b j o b j ) )
( tagbo d y
L1 ( i f ( t y p e p o b j f i x nu m )
( go L2 )
( go L4 ) )
L2 ( i f ( t y p e p o b j u ns i gn e d b y t e )
( go L3 )
( go P4 ) )
L3 ( i f ( t y p e p o b j ( e q l 4 2 ) )
( go P2 )
( go P1 ) )
L4 ( i f ( t y p e p o b j number )
( go L5 )
( r e t u r n n i l ) )
L5 ( i f ( t y p e p o b j u ns i gn e d b y t e )
( go P1 )
( go P3 ) )
P1 ( r e t u r n ( p r o g n body forms 1 . . . ) )
P2 ( r e t u r n ( p r o g n body forms 2 . . . ) )
P3 ( r e t u r n ( p r o g n body forms 3 . . . ) )
P4 ( r e t u r n ( p r o g n body forms 4 . . . ) ) ) )
fixnum
unsigned-byte
number
(eql 42)
(satisfies P4)
unsigned-byte
nil
(satisfies P2)(satisfies P1)
T
(satisfies P3)
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 36 / 41
Optimization using decision diagrams
Bigger bdd-typecase example with labels.
( l e t ( ( o b j o b j ) )
( l a b e l s ( ( L1 ( ) ( i f ( ty p e p o b j f i x num )
( L2 )
( L4 ) ) )
( L2 ( ) ( i f ( ty p e p o b j u n s i gn e d b yt e )
( L3 )
( P4 ) ) )
( L3 ( ) ( i f ( ty p e p o b j ( e q l 4 2 ) )
( P2 )
( P1 ) ) )
( L4 ( ) ( i f ( ty p e p o b j number )
( L5 )
n i l ) )
( L5 ( ) ( i f ( ty p e p o b j u n s i gn e d b yt e )
( 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
Optimization using decision diagrams
ROBDD worst case size
N |ROBDD
N
|
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.
|ROBDD
N
| = (2
Nθ
1) + 2
2
θ
where
dlog
2
(N 2 log
2
N)e 2 θ blog
2
Nc
But our ROBDD is never worst-case.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 38 / 41
Optimization using decision diagrams
ROBDD worst case size
N |ROBDD
N
|
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.
|ROBDD
N
| = (2
Nθ
1) + 2
2
θ
where
dlog
2
(N 2 log
2
N)e 2 θ blog
2
Nc
But our ROBDD is never worst-case.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 38 / 41
Optimization using decision diagrams
ROBDD worst case size
N |ROBDD
N
|
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.
|ROBDD
N
| = (2
Nθ
1) + 2
2
θ
where
dlog
2
(N 2 log
2
N)e 2 θ blog
2
Nc
But our ROBDD is never worst-case.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 38 / 41
Optimization using decision diagrams
ROBDD worst case size
N |ROBDD
N
|
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.
|ROBDD
N
| = (2
Nθ
1) + 2
2
θ
where
dlog
2
(N 2 log
2
N)e 2 θ blog
2
Nc
But our ROBDD is never worst-case.
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 38 / 41
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
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
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
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
Conclusion
Questions/Answers
Questions?
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 41 / 41
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
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
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
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
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
Macro expansion of typecase
Example macroexpand-1 from SBCL.
( t y p e c a s e x
( ( and f ix nu m ( no t ( e q l 0 ) ) ) ( f 1 ) )
( ( e q l 0) ( f 2 ) )
( symb ol ( f 3 ) )
( t ( f 4 ) ) )
; ; macro e x p a n s i o n
( l e t ((\#: g604 x ) )
( d e c l a r e ( i g n o r a b l e \#: g604 ) )
( cond ( ( ty p ep \#:g604 ( and fixn um ( not ( e q l 0 ) ) ) ) n i l ( f 1 ) )
( ( t y p e p \#: g604 ( e q l 0) ) n i l ( f 2 ) )
( ( t y p e p \#: g604 sy mbol ) n i l ( f 3 ) )
( t n i l ( f 4 ) ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 44 / 41
Macro expansion of typecase
Example macroexpand-1 from SBCL.
( t y p e c a s e x
( ( and f ix nu m ( no t ( e q l 0 ) ) ) ( f 1 ) )
( ( e q l 0) ( f 2 ) )
( symb ol ( f 3 ) )
( t ( f 4 ) ) )
; ; macro e x p a n s i o n
( l e t ((\#: g604 x ) )
( d e c l a r e ( i g n o r a b l e \#: g604 ) )
( cond ( ( ty p ep \#:g604 ( and fixn um ( not ( e q l 0 ) ) ) ) n i l ( f 1 ) )
( ( t y p e p \#: g604 ( e q l 0) ) n i l ( f 2 ) )
( ( t y p e p \#: g604 sy mbol ) n i l ( f 3 ) )
( t n i l ( f 4 ) ) ) )
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
Macro expansion of typecase
( t y p e c a s e x
( ( and f ix nu m ( no t ( e q l 0 ) ) ) ( f 1 ) )
( ( e q l 0) ( f 2 ) )
( symb ol ( f 3 ) )
( t ( f 4 ) ) )
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 ( ( ty p ep \#:g604 ( and fixn um ( not ( e q l 0 ) ) ) ) n i l ( f 1 ) )
( ( t y p e p \#: g604 ( e q l 0) ) n i l ( f 2 ) )
( ( t y p e p \#: g604 sy mbol ) n i l ( f 3 ) )
( t n i l ( f 4 ) ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 45 / 41
Macro expansion of typecase
( t y p e c a s e x
( ( and f ix nu m ( no t ( e q l 0 ) ) ) ( f 1 ) )
( ( e q l 0) ( f 2 ) )
( symb ol ( f 3 ) )
( t ( f 4 ) ) )
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 ( ( ty p ep \#:g604 ( and fixn um ( not ( e q l 0 ) ) ) ) n i l ( f 1 ) )
( ( t y p e p \#: g604 ( e q l 0) ) n i l ( f 2 ) )
( ( t y p e p \#: g604 sy mbol ) n i l ( f 3 ) )
( t n i l ( f 4 ) ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 46 / 41
Machine generated, redundant checks
Redundant type checks
Unreachable code
Exhaustiveness
( t y p e c a s e ( prog 1 ( a r e f s eq i ) ( i n c f i ) )
( f ix nu m
( go 7 ) )
( ( and r e a l ( not f ix nu m ) ( n o t r a t i o ) )
( go 1 1 ) )
( ( o r r a t i o ( and number ( not r e a l ) ) )
( go 1 0 ) )
( t ( re tu r n fr o m ch ec k 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
Reorderable clauses
( t y p e c a s e o b j
( f ix nu m ( f 1 ) )
( ( and number ( not f ix nu m ) ) ( f 2 ) )
( ( and t ( not ( o r fix nu m number ) ) ) ( f 3 ) ) )
Now the clauses can be reordered.
( t y p e c a s e o b j
( ( and number ( not f ix nu m ) ) ( f 2 ) )
( f ix nu m ( f 1 ) )
( ( and t ( not ( o r fix nu m number ) ) ) ( f 3 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 48 / 41
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
( t y p e c a s e o b j
( f ix nu m ( f 1 ) )
( ( and number ( not r a t i o ) ) ( f 2 ) ) )
Than to have complex expressions early
( t y p e c a s e o b j
( ( and number ( not r a t i o ) ) ( f 2 ) )
( f ix nu m ( f 1 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 49 / 41
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
( t y p e c a s e o b j
( f ix nu m ( f 1 ) )
( ( and number ( not r a t i o ) ) ( f 2 ) ) )
Than to have complex expressions early
( t y p e c a s e o b j
( ( and number ( not r a t i o ) ) ( f 2 ) )
( f ix nu m ( f 1 ) ) )
Jim Newton (11th European Lisp Symposium) Strategies for typecase optimization 16-17 April 2017 49 / 41