where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · where are you going with...
TRANSCRIPT
![Page 1: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/1.jpg)
Where are you going with those
types?
Vincent St-Amour, Sam Tobin-Hochstadt,Matthew Flatt, Matthias Felleisen
PLT / Northeastern UniversityBoston, MA, USA
PLT / University of UtahSalt Lake City, UT, USA
IFL 2010 - September 3rd, 2010
![Page 2: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/2.jpg)
Generating fast code in the
presence of ad-hoc polymorphism
is hard.
![Page 3: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/3.jpg)
Case study: generic arithmetic
(+ 2 2)
![Page 4: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/4.jpg)
Case study: generic arithmetic
(+ 2 2)
(+ 2.3 2.4)
![Page 5: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/5.jpg)
Case study: generic arithmetic
(+ 2 2)
(+ 2.3 2.4)
(+ 2.3 2)
![Page 6: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/6.jpg)
Case study: generic arithmetic
(+ 2 2)
(+ 2.3 2.4)
(+ 2.3 2)
(+ 2+3i 2+4i)
![Page 7: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/7.jpg)
Case study: generic arithmetic
(+ 2 2)
(+ 2.3 2.4)
(+ 2.3 2)
(+ 2+3i 2+4i)
Types of arguments are not known statically.
![Page 8: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/8.jpg)
Case study: generic arithmetic
#lang racket(+ 2.3 2.4)
![Page 9: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/9.jpg)
Case study: generic arithmetic
#lang racket(+ 2.3 2.4)
(define (add x y) (cond ((and (float? x) (float? y))
(let* ([val-x (strip-type-tag x)][val-y (strip-type-tag y)][result (add-floats val-x val-y)])
(tag-as-float result))) ((and (integer? x) (integer? y))...) ((and (complex? x) (complex? y))...) (else (error))))
![Page 10: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/10.jpg)
Case study: generic arithmetic
#lang racket(+ 2.3 2.4)
(define (add x y) (cond ((and (float? x) (float? y))
(let* ([val-x (strip-type-tag x)][val-y (strip-type-tag y)][result (add-floats val-x val-y)])
(tag-as-float result))) ((and (integer? x) (integer? y))...) ((and (complex? x) (complex? y))...) (else (error))))
![Page 11: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/11.jpg)
Our solution
• Type-specialized primitives
• Composition of:
• Type-driven rewriting
• Primitives drive optimization
Typechecker
Rewriting
Compiler
Primitives
τ
![Page 12: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/12.jpg)
Implementation
• Typed Racket
• Higher-order functional language
• Generic arithmetic (and complexes)
![Page 13: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/13.jpg)
Implementation
• Typed Racket
• Higher-order functional language
• Generic arithmetic (and complexes)
Applicable to other languages
![Page 14: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/14.jpg)
Type-specialized primitives
![Page 15: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/15.jpg)
#lang racket(fl+ x y)
![Page 16: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/16.jpg)
#lang racket(fl+ 2.3 2.4)
![Page 17: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/17.jpg)
#lang racket(fl+ 2.3 2.4)
4.7
![Page 18: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/18.jpg)
#lang racket(fl+ 2 2)
![Page 19: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/19.jpg)
#lang racket(fl+ 2 2)
segmentation fault
![Page 20: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/20.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (fl+ x y))
![Page 21: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/21.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (fl+ x y))
![Page 22: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/22.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (fl+ x y))
4.7
![Page 23: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/23.jpg)
#lang typed/racket(let: ([x : Integer 2]
[y : Integer 2]) (fl+ x y))
![Page 24: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/24.jpg)
#lang typed/racket(let: ([x : Integer 2]
[y : Integer 2]) (fl+ x y))
![Page 25: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/25.jpg)
#lang typed/racket(let: ([x : Integer 2]
[y : Integer 2]) (fl+ x y))
Type Checker: No function domains matched infunction application:Domains: Float FloatArguments: Integer Integer in: (fl+ x y)
![Page 26: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/26.jpg)
Type-driven rewriting
![Page 27: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/27.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (+ x y))
![Page 28: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/28.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (+ x y))
![Page 29: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/29.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (fl+ x y))
![Page 30: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/30.jpg)
#lang typed/racket(let: ([x : Float 2.3]
[y : Float 2.4]) (fl+ x y))
4.7
![Page 31: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/31.jpg)
Primitives drive optimization
![Page 32: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/32.jpg)
#lang typed/racket(* (+ x y)
(+ z w))
![Page 33: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/33.jpg)
#lang typed/racket(* (+ x y)
(+ z w))
load $x $r1load $y $r2...fadd $r1 $r2 $r3...sto $r3 $tmp1load $z $r4load $w $r5...fadd $r4 $r5 $r6...sto $r6 $tmp2load $tmp1 $r7load $tmp2 $r8...fmul $r7 $r8 $r9...sto $r9 $tmp3
![Page 34: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/34.jpg)
#lang typed/racket(* (+ x y)
(+ z w))
load $x $r1load $y $r2...fadd $r1 $r2 $r3...sto $r3 $tmp1load $z $r4load $w $r5...fadd $r4 $r5 $r6...sto $r6 $tmp2load $tmp1 $r7load $tmp2 $r8...fmul $r3 $r6 $r9...sto $r9 $tmp3
![Page 35: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/35.jpg)
#lang typed/racket(* (+ x y)
(+ z w))
#lang typed/racket(fl* (fl+ x y)
(fl+ z w))
load $x $r1load $y $r2...fadd $r1 $r2 $r3...sto $r3 $tmp1load $z $r4load $w $r5...fadd $r4 $r5 $r6...sto $r6 $tmp2load $tmp1 $r7load $tmp2 $r8...fmul $r3 $r6 $r9...sto $r9 $tmp3
![Page 36: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/36.jpg)
#lang typed/racket(* (+ x y)
(+ z w))
#lang typed/racket(fl* (fl+ x y)
(fl+ z w))
load $x $r1load $y $r2...fadd $r1 $r2 $r3...sto $r3 $tmp1load $z $r4load $w $r5...fadd $r4 $r5 $r6...sto $r6 $tmp2load $tmp1 $r7load $tmp2 $r8...fmul $r3 $r6 $r9...sto $r9 $tmp3
![Page 37: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/37.jpg)
#lang typed/racket(let ([a (+ x y)]) (* a (- z a)))
![Page 38: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/38.jpg)
#lang typed/racket(let ([a (+ x y)]) (* a (- z a)))
load $x $r1 load $y $r2...fadd $r1 $r2 $r3...sto $r3 $aload $z $r4load $a $r5...fsub $r4 $r5 $r6...sto $r6 $tmp1load $a $r7load $tmp1 $r8...fmul $r7 $r8 $r9...sto $r9 $tmp2
![Page 39: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/39.jpg)
#lang typed/racket(let ([a (+ x y)]) (* a (- z a)))
load $x $r1 load $y $r2...fadd $r1 $r2 $r3...sto $r3 $aload $z $r4load $a $r5...fsub $r4 $r3 $r6...sto $r6 $tmp1load $a $r7load $tmp1 $r8...fmul $r3 $r6 $r9...sto $r9 $tmp2
![Page 40: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/40.jpg)
#lang typed/racket(let ([a (+ x y)]) (* a (- z a)))
#lang typed/racket(let ([a (fl+ x y)]) (fl* a (fl- z a)))
load $x $r1 load $y $r2...fadd $r1 $r2 $r3...sto $r3 $aload $z $r4load $a $r5...fsub $r4 $r3 $r6...sto $r6 $tmp1load $a $r7load $tmp1 $r8...fmul $r3 $r6 $r9...sto $r9 $tmp2
![Page 41: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/41.jpg)
#lang typed/racket(let loop ([acc 0.0]) (if (> acc x)
acc(loop (+ y acc))))
![Page 42: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/42.jpg)
#lang typed/racket(let loop ([acc 0.0]) (if (> acc x)
acc(loop (+ y acc))))
mov 0.0 $r1 ... sto $r1 $acc
loop: load $acc $r2 load $x $r3 ... flcmp $r2 $r3 jgt end load $y $r4 load $acc $r5 ... fadd $r4 $r5 $r6 ... sto $r6 $acc jmp loopend:
![Page 43: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/43.jpg)
#lang typed/racket(let loop ([acc 0.0]) (if (> acc x)
acc(loop (+ y acc))))
mov 0.0 $r1 ... sto $r1 $acc
loop: load $acc $r2 load $x $r3 ... flcmp $r2 $r3 jgt end load $y $r4 load $acc $r5 ... fadd $r4 $r5 $r6 ... sto $r6 $acc jmp loopend:
![Page 44: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/44.jpg)
#lang typed/racket(let loop ([acc 0.0]) (if (> acc x)
acc(loop (+ y acc))))
#lang typed/racket(let loop ([acc 0.0]) (if (fl> acc x)
acc(loop (fl+ y acc))))
mov 0.0 $r1 ... sto $r1 $acc
loop: load $acc $r2 load $x $r3 ... flcmp $r1 $r3 jgt end load $y $r4 load $acc $r5 ... fadd $r4 $r1 $r6 ... sto $r6 $acc jmp loopend: sto $r1 $acc
![Page 45: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/45.jpg)
#lang typed/racket(let loop ([acc 0.0]) (if (> acc x)
acc(loop (+ y acc))))
#lang typed/racket(let loop ([acc 0.0]) (if (fl> acc x)
acc(loop (fl+ y acc))))
mov 0.0 $r1 ... sto $r1 $acc
loop: load $acc $r2 load $x $r3 ... flcmp $r1 $r3 jgt end load $y $r4 load $acc $r5 ... fadd $r4 $r1 $r6 ... sto $r6 $acc jmp loopend: sto $r1 $acc
![Page 46: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/46.jpg)
#lang typed/racket(let loop ([acc 0.0]) (if (> acc x)
acc(loop (+ y acc))))
#lang typed/racket(let loop ([acc 0.0]) (if (fl> acc x)
acc(loop (fl+ y acc))))
mov 0.0 $r1 ... sto $r1 $acc load $x $r3 load $y $r4loop: load $acc $r2 load $x $r3 ... flcmp $r1 $r3 jgt end load $y $r4 load $acc $r5 ... fadd $r4 $r1 $r6 ... sto $r6 $acc jmp loopend: sto $r1 $acc
![Page 47: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/47.jpg)
Results
![Page 48: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/48.jpg)
Speedup
benchmarks pseudoknot 2.50mandelbrot 15.72
nbody 2.16takl 1.24
FFT 15.91
data structures banker's queue 1.60leftist heap 1.34
industrial application FFT 2.78
Bigger is better
Average of 5 runs on x86
![Page 49: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/49.jpg)
In-depth look: Industrial FFT
![Page 50: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/50.jpg)
#lang racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i])
(- (+ x y) z))
![Page 51: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/51.jpg)
#lang racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i][x-real (real-part x)][x-imag (imag-part x)][y-real (real-part y)][y-imag (imag-part y)][z-real (real-part z)][z-imag (imag-part z)])
(make-rectangular(- (+ x-real y-real) z-real)(- (+ x-imag y-imag) z-imag)))
![Page 52: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/52.jpg)
#lang racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i][x-real (real-part x)][x-imag (imag-part x)][y-real (real-part y)][y-imag (imag-part y)][z-real (real-part z)][z-imag (imag-part z)])
(make-rectangular(fl- (fl+ x-real y-real) z-real)(fl- (fl+ x-imag y-imag) z-imag)))
![Page 53: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/53.jpg)
#lang racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i][x-real (real-part x)][x-imag (imag-part x)][y-real (real-part y)][y-imag (imag-part y)][z-real (real-part z)][z-imag (imag-part z)])
(make-rectangular(fl- (fl+ x-real y-real) z-real)(fl- (fl+ x-imag y-imag) z-imag)))
Significant manual labor
![Page 54: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/54.jpg)
#lang racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i][x-real (real-part x)][x-imag (imag-part x)][y-real (real-part y)][y-imag (imag-part y)][z-real (real-part z)][z-imag (imag-part z)])
(make-rectangular(fl- (fl+ x-real y-real) z-real)(fl- (fl+ x-imag y-imag) z-imag)))
Significant manual labor
Error prone
![Page 55: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/55.jpg)
#lang typed/racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i])
(- (+ x y) z))
![Page 56: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/56.jpg)
#lang typed/racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i])
(- (+ x y) z))
Unboxed intermediate results
![Page 57: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/57.jpg)
#lang typed/racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i])
(- (+ x y) z))
Unboxed intermediate results
Unboxed let bindings
![Page 58: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/58.jpg)
#lang typed/racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i])
(- (+ x y) z))
Unboxed intermediate results
Unboxed let bindings
Unboxed loop variables
![Page 59: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/59.jpg)
#lang typed/racket(let* ([x 2.3+2.4i]
[y 2.5+2.6i][z 2.7+2.8i])
(- (+ x y) z))
Unboxed intermediate results
Unboxed let bindings
Unboxed loop variables
Faster than hand-optimized code
![Page 60: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/60.jpg)
Related Work
![Page 61: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/61.jpg)
Gallium / FLINT
typechecker
code types
CPS
code
λ-lifting
code
...
code
optimization
Typed Racket
typechecker
code types
CPS
code
λ-lifting
code
...
code
optimization
![Page 62: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/62.jpg)
Gallium / FLINT
typechecker
code types
CPS
code
CPS
types
λ-lifting
code
λ-lifting
types
...
code
...
types
optimization
Typed Racket
typechecker
code types
CPS
code
λ-lifting
code
...
code
optimization
![Page 63: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/63.jpg)
Gallium / FLINT
typechecker
code types
CPS
code
CPS
types
λ-lifting
code
λ-lifting
types
...
code
...
types
optimization
Typed Racket
typechecker
code types
rewriting
code
CPS
code
λ-lifting
code
...
code
optimization
![Page 64: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/64.jpg)
OCaml
2 + 2
2.3 +. 2.4
Typed Racket
(+ 2 2)
(+ 2.3 2.4)
![Page 65: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/65.jpg)
OCaml
2 + 2
2.3 +. 2.4
Typed Racket
(+ 2 2)
(+ 2.3 2.4)
OCaml limits specialization to integer vs float.
![Page 66: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/66.jpg)
Common LISP
(defun f (x) (declare (type function x)
(optimize (speed 3)(safety 0)))
(funcall x 3)) (defun g (fun) (declare (type function fun)) (funcall fun "a")) (g #'f)
Typed Racket
#lang typed/racket (define: (f (x : (Integer -> Integer))) : Integer (x 3)) (define: (g (fun : (String -> String))) : String (fun "a")) (g f)
![Page 67: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/67.jpg)
Common LISP
(defun f (x) (declare (type function x)
(optimize (speed 3)(safety 0)))
(funcall x 3)) (defun g (fun) (declare (type function fun)) (funcall fun "a")) (g #'f)
Typed Racket
#lang typed/racket (define: (f (x : (Integer -> Integer))) : Integer (x 3)) (define: (g (fun : (String -> String))) : String (fun "a")) (g f)
![Page 68: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/68.jpg)
Common LISP
(defun f (x) (declare (type function x)
(optimize (speed 3)(safety 0)))
(funcall x 3)) (defun g (fun) (declare (type function fun)) (funcall fun "a")) (g #'f)
Unhandled memory fault at #x610000.
Typed Racket
#lang typed/racket (define: (f (x : (Integer -> Integer))) : Integer (x 3)) (define: (g (fun : (String -> String))) : String (fun "a")) (g f)
Type Checker:Expected (String -> String),but got ((Integer -> Integer) -> Integer)in: f
![Page 69: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/69.jpg)
Our solution
• Type-specialized primitives
• Composition of:
• Type-driven rewriting
• Primitives drive optimization
Typechecker
Rewriting
Compiler
Primitives
τ
![Page 70: Where are you going with those types?stamourv/slides/tr-opt-ifl10.pdf · Where are you going with those types? Vincent St-Amour, Sam Tobin-Hochstadt, Matthew Flatt, Matthias Felleisen](https://reader034.vdocument.in/reader034/viewer/2022052101/603b71517fa15c67f32aaf0f/html5/thumbnails/70.jpg)
racket-lang.org