SlideShare a Scribd company logo
1 of 52
Download to read offline
Optimizing with
persistent data
structures
Adventures in CPS soup
Andy Wingo ~ wingo@igalia.com
wingolog.org ~ @andywingo
Agenda
SSA and CPS: Tribal lore
A modern CPS
Programs as values: structure
Programs as values: transformation
Evaluation
How we got here
1928 Hilbert: Can has
Entscheidungsproblem?
1936 Church: Nope!
Also here is the lambda calculus
For identifiers x and terms t and s, a term is
either
A variable reference: x❧
A lambda abstraction: λx. t❧
An application: (t s)❧
Computing with lambda
Lambda abstractions bind variables lexically
To compute with the lambda calculus:
take a term and reduce it, exhaustively❧
Sounds like compilation, right?
GOTO?
1958 McCarthy: Hey the lambda calculus is
not bad for performing computation!
1965 Landin: Hey we can understand ALGOL
60 using the lambda calculus!
What about GOTO?
Landin: J operator captures state of SECD
machine that can be returned to later
To J or not to J
1964 van Wijngaarden: Not to J!
Just transform your program
1970 F. Lockwood Morris: Re-discovers
program transformation
(Iinspired by LISP 1.5 code!)
function f()
local x = foo() ? y() : z();
return x
end
function f(k)
function ktest(val)
function kt() return y(kret) end
function kf() return z(kret) end
if val then return kt() else return kf()
end
function kret(x) return k(x) end
return foo(ktest)
end
Nota bene
function kt() return y(kret) end
All calls are tail calls
1970 Chris Wadsworth: Hey! Result of the
Morris transformation is the continuation:
the meaning of the rest of the program
Function calls are passed an extra argument:
the continuation
Variables bound by continuations
Compiling with CPS
1977 Guy Steele: Hey we can compile with
this!
Tail calls are literally GOTO, potentially passing
values.
1978 Guy Steele: RABBIT Scheme compiler
using CPS as IL
Rewrite so all calls are tail calls, compile as
jumps
1984 David Kranz: ORBIT Scheme compiler
using CPS, even for register allocation
What’s missing?
1970 Fran Allen and John Cocke: Flow
analysis
Both Turing award winners!
Range checking, GCSE, DCE, code motion,
strength reduction, constant propagation,
scheduling
Flow analysis for CPS
1984 Shivers: Whoops this is hard
Flow analysis in CPS: given (f x), what values
flow to f and x?
For data-flow analysis, you need control-flow
analysis
For control-flow analysis, you need data-flow
analysis
Solution 1: k-CFA
Solve both problems at once
1991 Shivers: k-CFA family of higher-order
flow analysis
Based on CPS
Parameterized by precision
0-CFA: first order, quadratic...❧
1-CFA: second order, exponential!❧
k-CFA: order k, exponential❧
2009 Van Horn: k > 0 intractable
Solution 2: Some conts are
labels
Observation: Lambda terms in CPS are of
three kinds
Procs
Entry points to functions of source program
function f(k)
function ktest(val)
function kt() return y(kret) end
function kf() return z(kret) end
if val then return kt() else return kf()
end
function kret(x) return k(x) end
return foo(ktest)
end
Conts
Return points from calls; synthetic
function f(k)
function ktest(val)
function kt() return y(kret) end
function kf() return z(kret) end
if val then return kt() else return kf()
end
function kret(x) return k(x) end
return foo(ktest)
end
Jumps
Jump targets; synthetic
function f(k)
function ktest(val)
function kt() return y(kret) end
function kf() return z(kret) end
if val then return kt() else return kf()
end
function kret(x) return k(x) end
return foo(ktest)
end
Solution 2: Some conts are
labels
1995 Kelsey: “In terms of compilation
strategy, conts are return points, jumps can
be compiled as gotos, and procs require a
complete procedure-call mechanism.”
Separate control and data flow
1992 Appel, “Compiling with Continuations”
(ML)
What about SSA?
1986-1988 Rosen, Wegman, Ferrante, Cytron,
Zadeck: “Binding, not assignment”
“The right number of names”
Better notation makes it easier to transform
programs
Initial application of SSA was GVN
SSA and CPS
1995 Kelsey: “Making [continuation uses]
syntactically distinct restricts how
continuations are used and makes CPS and
SSA entirely equivalent.”
SSA: Definitions must dominate uses
CPS embeds static proof of SSA condition: all
uses must be in scope
1998 Appel: “SSA is Functional Programming”
Modern CPS
2007 Kennedy: Compiling with
Continuations, Continued
Nested scope
Syntactic difference between continuations
(control) and variables (data)
Why CPS in 2016?
SSA: How do I compile loops?
CPS: How do I compile functions?
“Get you a compiler that can do both”
Example: Contification
A function or clique of functions that always
continues to the same label (calls the same
continuation) can be integrated into the
caller
Like inlining, widens first-order flow graph: a
mother optimization
Unlike inlining, always a good idea: always a
reduction
CPS facilitates contification
Concept of continuation❧
Globally unique labels and variable names❧
Interprocedural scope❧
Single term for program❧
Possible in SSA too of course
And yet
CPS: all uses must be in scope... but not all
dominating definitions are in scope
Transformations can corrupt scope tree
function b0(k)
function k1(v1) return k2() end
function k2() return k(v1) end # XX
k1(42)
end
1999 Fluet and Weeks: MLton switches to
SSA
Alternate solution: CPS
without nesting
Values in scope are values that dominate
Program is soup of continuations
“CPS soup”
CPS in Guile
(define-type Label Natural)
(struct Program
([entry : Label]
[conts : (Map Label Cont)]))
Conts
(define-type Var Natural)
(define-type Vars (Listof Var))
(struct KEntry
([body : Label] [exit : Label]))
(struct KExpr
([vars : Vars] [k : Label] [exp : Exp]))
(struct KExit)
(define-type Cont (U KEntry KExpr KExit))
Exps
(define-type Op (U 'lookup 'add1 ...))
(struct Primcall ([op : Op] [args : Vars]))
(struct Branch ([kt : Label] [exp : Expr]))
(struct Call ([proc : Var] [args : Vars]))
(struct Const ([val : Literal]))
(struct Func ([entry : Label]))
(struct Values ([args : Vars]))
(define-type Exp
(U Primcall Branch Call Const Func Values))
See language/cps.scm for full details
;; (lambda () (if (foo) (y) #f))
(Map
(k0 (KEntry k1 k10))
(k1 (KExpr () k2 (Const 'foo)))
(k2 (KExpr (v0) k3 (Primcall 'lookup (v0)))
(k3 (KExpr (v1) k4 (Call v1 ())))
(k4 (KExpr (v2) k5 (Branch k8 (Values (v1))
(k5 (KExpr () k6 (Const 'y)))
(k6 (KExpr (v3) k7 (Primcall 'lookup (v3)))
(k7 (KExpr (v4) k10 (Call v4 ())))
(k8 (KExpr () k9 (Const #f)))
(k9 (KExpr (v5) k10 (Values (v5))))
(k10 (KExit)))
Salient details
Variables available for use a flow property
Variables bound by KExpr; values given by
predecessors
Expressions have labels and continue to
other labels
Return by continuing to the label identifying
function’s KExit
Orders of CPS
Two phases in Guile
Higher-order: Variables in “outer”
functions may be referenced directly by
“inner” functions; primitive support for
recursive function binding forms
❧
First-order: Closure representations
chosen, free variables (if any) accessed
through closure
❧
“[Interprocedural] binding is better than
assignment”
About those maps
(struct (v) IntMap
([min : Natural]
[shift : Natural]
[root : (U (Maybe v) (Branch v))]))
(define-type (Branch v)
(U (Vectorof (Maybe Branch))
(Vectorof (Maybe v))))
Shift 0 and root empty? {}
Shift 0? {min: valueof(root)}
Otherwise element i of root[i] is root for min
+i*2^(shift-5), at shift-5.
Bagwell AMTs
Array Mapped Trie
Clojure-inspired data structures invented by
Phil Bagwell
O(n log n) in size
Ref and update O(log n)
Visit-each near-linear
Unions and intersections very cheap
Clojure innovation
clojure.org/transients: Principled in-place
mutation
(define (intmap-map proc map)
(persistent-intmap
(intmap-fold
(lambda (k v out)
(intmap-add! out k (proc k v)))
map
(transient-intmap empty-intmap))))
Still O(n log n) but significant constant factor
savings
Intsets
“Which labels are in this function?”
(struct IntSet
([min : Natural]
[shift : Natural]
[root : (U Leaf Branch)]))
(define-type Leaf UInt32)
(define-type Branch
(U (Vectorof (Maybe Branch))
(Vectorof Leaf)))
Transient variants as well
Optimizing with persistent
data structures
Example optimization: “Unboxing”
Objective: use specific limited-precision
machine numbers instead of arbitrary-
precision polymorphic numbers
function unbox_pass(conts):
let out = conts
for entry, body in conts.functions():
let types = infer_types(conts, entry,
body)
for label in body:
match conts[label]:
KExpr vars k (Primcall 'add1 (a)):
if can_unbox?(label, k, a,
types, conts):
out = unbox(label, vars, k, a,
out)
_: pass
return out
function can_unbox?(label, k, arg,
types, conts):
match conts[k]:
KExpr (result) _ _:
let rtype, rmin, rmax =
lookup_post_type(label, result)
let atype, amin, amax =
lookup_pre_type(label, a)
return unboxable?(rtype, rmin, rmax)
and unboxable?(atype, amin, amax)
function unbox(label, vars, k, arg, conts):
let uarg, res = fresh_vars(conts, 2)
let kbox, kop = fresh_labels(conts, 2)
conts = conts.replace(label,
KEntry vars kop (Primcall 'unbox (a)))
conts = conts.add(kop,
KEntry (ua) kbox (Primcall 'uadd1 (ua)))
return conts.add(kbox,
KEntry (res) k (Primcall 'box (res)))
Salient points
To get name of result(s), have to look at
continuation
No easy way to get predecessors (without
building predecessors map)
No easy way to know if output var has
other definitions
❧
On the other hand... no easy way to write
local-only passes
Backwards flow
y = x & 0xffffffff
We only need low 32 bits from x; can allow x
to unbox...
...but can’t reach through from & to x.
Solution: solve a flow problem (bits needed
for each variable)
Also works globally!❧
Whither yon basic block?
Not necessary; get in the way sometimes
Need globally unique names for terms
anyway
Guile has terms that can bail out, unlike llvm;
have to do big flow graph anyway
Odd: almost never need dominators! Full
flow analysis instead.
Strengths
Simple – few moving parts
Immutability helps fit more of the problem
into your head
Interprocedural bindings pre-closure-
conversion easier to reason about than
locations in global heap
Good space complexity for complicated flow
analysis (type,range of all vars at all labels: n
log n)
Compared to SSA (1)
Just as rigid scheduling-wise (compare to
sea-of-nodes)
Flow analysis over cont graph has more
nodes than over basic block graph
Additional log n factor for most operations
Names as graph edges means lots of pointer
chasing
Compared to SSA (2)
Sometimes have to renumber graph if pass
wants specific ordering (usually topological)
Values that flow into phi vars have no names!
Lots of allocation (mitigate with zones?)
Always throwing away analysis
Summary
Better notation makes it easier to transform
programs
If SSA + basic block graph works for you,
great
If not, map to a notation that is more
tractable for you, transform there, and come
back
CPS name graph on persistent data
structures seems to work for Guile; perhaps
for you too?
Summary
Happy hacking!
wingolog.org
@andywingo
wingo@igalia.com

More Related Content

What's hot

Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufRomain Francois
 
Async await in C++
Async await in C++Async await in C++
Async await in C++cppfrug
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16Max Kleiner
 
Juan josefumeroarray14
Juan josefumeroarray14Juan josefumeroarray14
Juan josefumeroarray14Juan Fumero
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local namesVarsha Kumar
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide showMax Kleiner
 
Open cl programming using python syntax
Open cl programming using python syntaxOpen cl programming using python syntax
Open cl programming using python syntaxcsandit
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkAlexey Smirnov
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3Linaro
 
A Verified Decision Procedure for Pseudo-Boolean Formulas
A Verified Decision Procedure for Pseudo-Boolean FormulasA Verified Decision Procedure for Pseudo-Boolean Formulas
A Verified Decision Procedure for Pseudo-Boolean FormulasTobias Philipp
 
St Petersburg R user group meetup 2, Parallel R
St Petersburg R user group meetup 2, Parallel RSt Petersburg R user group meetup 2, Parallel R
St Petersburg R user group meetup 2, Parallel RAndrew Bzikadze
 
Computational Techniques for the Statistical Analysis of Big Data in R
Computational Techniques for the Statistical Analysis of Big Data in RComputational Techniques for the Statistical Analysis of Big Data in R
Computational Techniques for the Statistical Analysis of Big Data in Rherbps10
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
What make Swift Awesome
What make Swift AwesomeWhat make Swift Awesome
What make Swift AwesomeSokna Ly
 

What's hot (20)

Integrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBufIntegrating R with C++: Rcpp, RInside and RProtoBuf
Integrating R with C++: Rcpp, RInside and RProtoBuf
 
Async await in C++
Async await in C++Async await in C++
Async await in C++
 
A closure ekon16
A closure ekon16A closure ekon16
A closure ekon16
 
Python to scala
Python to scalaPython to scala
Python to scala
 
Juan josefumeroarray14
Juan josefumeroarray14Juan josefumeroarray14
Juan josefumeroarray14
 
Access to non local names
Access to non local namesAccess to non local names
Access to non local names
 
Arduino C maXbox web of things slide show
Arduino C maXbox web of things slide showArduino C maXbox web of things slide show
Arduino C maXbox web of things slide show
 
Extending ns
Extending nsExtending ns
Extending ns
 
Open cl programming using python syntax
Open cl programming using python syntaxOpen cl programming using python syntax
Open cl programming using python syntax
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
 
HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3HKG15-207: Advanced Toolchain Usage Part 3
HKG15-207: Advanced Toolchain Usage Part 3
 
A Verified Decision Procedure for Pseudo-Boolean Formulas
A Verified Decision Procedure for Pseudo-Boolean FormulasA Verified Decision Procedure for Pseudo-Boolean Formulas
A Verified Decision Procedure for Pseudo-Boolean Formulas
 
St Petersburg R user group meetup 2, Parallel R
St Petersburg R user group meetup 2, Parallel RSt Petersburg R user group meetup 2, Parallel R
St Petersburg R user group meetup 2, Parallel R
 
Rcpp
RcppRcpp
Rcpp
 
Computational Techniques for the Statistical Analysis of Big Data in R
Computational Techniques for the Statistical Analysis of Big Data in RComputational Techniques for the Statistical Analysis of Big Data in R
Computational Techniques for the Statistical Analysis of Big Data in R
 
Kotlin
KotlinKotlin
Kotlin
 
Run time
Run timeRun time
Run time
 
Wepwhacker !
Wepwhacker !Wepwhacker !
Wepwhacker !
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
What make Swift Awesome
What make Swift AwesomeWhat make Swift Awesome
What make Swift Awesome
 

Similar to Optimizing with persistent data structures (LLVM Cauldron 2016)

Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and FuturePushkar Kulkarni
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present FutureIndicThreads
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Leonardo Borges
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptAnkush138
 
Libxc a library of exchange and correlation functionals
Libxc a library of exchange and correlation functionalsLibxc a library of exchange and correlation functionals
Libxc a library of exchange and correlation functionalsABDERRAHMANE REGGAD
 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationEelco Visser
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants Hemantha Kulathilake
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.Ruslan Shevchenko
 
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)Matthew Lease
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsPhilip Schwarz
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointerskirthika jeyenth
 
Automatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELAutomatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELJoel Falcou
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29Bilal Ahmed
 
C standard library functions
C standard library functionsC standard library functions
C standard library functionsVaishnavee Sharma
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsEelco Visser
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyTypesafe
 

Similar to Optimizing with persistent data structures (LLVM Cauldron 2016) (20)

Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
 
C language
C languageC language
C language
 
C language
C languageC language
C language
 
Relational Algebra and Calculus.ppt
Relational Algebra and Calculus.pptRelational Algebra and Calculus.ppt
Relational Algebra and Calculus.ppt
 
Libxc a library of exchange and correlation functionals
Libxc a library of exchange and correlation functionalsLibxc a library of exchange and correlation functionals
Libxc a library of exchange and correlation functionals
 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code Generation
 
Java 8
Java 8Java 8
Java 8
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.SE 20016 - programming languages landscape.
SE 20016 - programming languages landscape.
 
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
 
Clanguage
ClanguageClanguage
Clanguage
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
 
Unit 4 functions and pointers
Unit 4 functions and pointersUnit 4 functions and pointers
Unit 4 functions and pointers
 
Automatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSELAutomatic Task-based Code Generation for High Performance DSEL
Automatic Task-based Code Generation for High Performance DSEL
 
CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29CS101- Introduction to Computing- Lecture 29
CS101- Introduction to Computing- Lecture 29
 
C standard library functions
C standard library functionsC standard library functions
C standard library functions
 
TI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class FunctionsTI1220 Lecture 6: First-class Functions
TI1220 Lecture 6: First-class Functions
 
ScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin OderskyScalaDays 2013 Keynote Speech by Martin Odersky
ScalaDays 2013 Keynote Speech by Martin Odersky
 

More from Igalia

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEIgalia
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesIgalia
 
Embedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceEmbedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceIgalia
 
Optimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfOptimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfIgalia
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JITIgalia
 
To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!Igalia
 
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerImplementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerIgalia
 
8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in MesaIgalia
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIgalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera LinuxIgalia
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVMIgalia
 
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsturnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsIgalia
 
Graphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesGraphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesIgalia
 
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSDelegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSIgalia
 
MessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webMessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webIgalia
 
Replacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersReplacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersIgalia
 
I'm not an AMD expert, but...
I'm not an AMD expert, but...I'm not an AMD expert, but...
I'm not an AMD expert, but...Igalia
 
Status of Vulkan on Raspberry
Status of Vulkan on RaspberryStatus of Vulkan on Raspberry
Status of Vulkan on RaspberryIgalia
 

More from Igalia (20)

A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Building End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPEBuilding End-user Applications on Embedded Devices with WPE
Building End-user Applications on Embedded Devices with WPE
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Automated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded DevicesAutomated Testing for Web-based Systems on Embedded Devices
Automated Testing for Web-based Systems on Embedded Devices
 
Embedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to MaintenanceEmbedding WPE WebKit - from Bring-up to Maintenance
Embedding WPE WebKit - from Bring-up to Maintenance
 
Optimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdfOptimizing Scheduler for Linux Gaming.pdf
Optimizing Scheduler for Linux Gaming.pdf
 
Running JS via WASM faster with JIT
Running JS via WASM      faster with JITRunning JS via WASM      faster with JIT
Running JS via WASM faster with JIT
 
To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!To crash or not to crash: if you do, at least recover fast!
To crash or not to crash: if you do, at least recover fast!
 
Implementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamerImplementing a Vulkan Video Encoder From Mesa to GStreamer
Implementing a Vulkan Video Encoder From Mesa to GStreamer
 
8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa8 Years of Open Drivers, including the State of Vulkan in Mesa
8 Years of Open Drivers, including the State of Vulkan in Mesa
 
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por IgaliaIntroducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
Introducción a Mesa. Caso específico dos dispositivos Raspberry Pi por Igalia
 
2023 in Chimera Linux
2023 in Chimera                    Linux2023 in Chimera                    Linux
2023 in Chimera Linux
 
Building a Linux distro with LLVM
Building a Linux distro        with LLVMBuilding a Linux distro        with LLVM
Building a Linux distro with LLVM
 
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUsturnip: Update on Open Source Vulkan Driver for Adreno GPUs
turnip: Update on Open Source Vulkan Driver for Adreno GPUs
 
Graphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devicesGraphics stack updates for Raspberry Pi devices
Graphics stack updates for Raspberry Pi devices
 
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOSDelegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
Delegated Compositing - Utilizing Wayland Protocols for Chromium on ChromeOS
 
MessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the webMessageFormat: The future of i18n on the web
MessageFormat: The future of i18n on the web
 
Replacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shadersReplacing the geometry pipeline with mesh shaders
Replacing the geometry pipeline with mesh shaders
 
I'm not an AMD expert, but...
I'm not an AMD expert, but...I'm not an AMD expert, but...
I'm not an AMD expert, but...
 
Status of Vulkan on Raspberry
Status of Vulkan on RaspberryStatus of Vulkan on Raspberry
Status of Vulkan on Raspberry
 

Recently uploaded

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

Optimizing with persistent data structures (LLVM Cauldron 2016)

  • 1. Optimizing with persistent data structures Adventures in CPS soup Andy Wingo ~ wingo@igalia.com wingolog.org ~ @andywingo
  • 2. Agenda SSA and CPS: Tribal lore A modern CPS Programs as values: structure Programs as values: transformation Evaluation
  • 3. How we got here 1928 Hilbert: Can has Entscheidungsproblem? 1936 Church: Nope! Also here is the lambda calculus For identifiers x and terms t and s, a term is either A variable reference: x❧ A lambda abstraction: λx. t❧ An application: (t s)❧
  • 4. Computing with lambda Lambda abstractions bind variables lexically To compute with the lambda calculus: take a term and reduce it, exhaustively❧ Sounds like compilation, right?
  • 5. GOTO? 1958 McCarthy: Hey the lambda calculus is not bad for performing computation! 1965 Landin: Hey we can understand ALGOL 60 using the lambda calculus! What about GOTO? Landin: J operator captures state of SECD machine that can be returned to later
  • 6. To J or not to J 1964 van Wijngaarden: Not to J! Just transform your program 1970 F. Lockwood Morris: Re-discovers program transformation (Iinspired by LISP 1.5 code!)
  • 7. function f() local x = foo() ? y() : z(); return x end function f(k) function ktest(val) function kt() return y(kret) end function kf() return z(kret) end if val then return kt() else return kf() end function kret(x) return k(x) end return foo(ktest) end
  • 8. Nota bene function kt() return y(kret) end All calls are tail calls 1970 Chris Wadsworth: Hey! Result of the Morris transformation is the continuation: the meaning of the rest of the program Function calls are passed an extra argument: the continuation Variables bound by continuations
  • 9. Compiling with CPS 1977 Guy Steele: Hey we can compile with this! Tail calls are literally GOTO, potentially passing values. 1978 Guy Steele: RABBIT Scheme compiler using CPS as IL Rewrite so all calls are tail calls, compile as jumps 1984 David Kranz: ORBIT Scheme compiler using CPS, even for register allocation
  • 10. What’s missing? 1970 Fran Allen and John Cocke: Flow analysis Both Turing award winners! Range checking, GCSE, DCE, code motion, strength reduction, constant propagation, scheduling
  • 11. Flow analysis for CPS 1984 Shivers: Whoops this is hard Flow analysis in CPS: given (f x), what values flow to f and x? For data-flow analysis, you need control-flow analysis For control-flow analysis, you need data-flow analysis
  • 12. Solution 1: k-CFA Solve both problems at once 1991 Shivers: k-CFA family of higher-order flow analysis Based on CPS Parameterized by precision 0-CFA: first order, quadratic...❧ 1-CFA: second order, exponential!❧ k-CFA: order k, exponential❧ 2009 Van Horn: k > 0 intractable
  • 13. Solution 2: Some conts are labels Observation: Lambda terms in CPS are of three kinds
  • 14. Procs Entry points to functions of source program function f(k) function ktest(val) function kt() return y(kret) end function kf() return z(kret) end if val then return kt() else return kf() end function kret(x) return k(x) end return foo(ktest) end
  • 15. Conts Return points from calls; synthetic function f(k) function ktest(val) function kt() return y(kret) end function kf() return z(kret) end if val then return kt() else return kf() end function kret(x) return k(x) end return foo(ktest) end
  • 16. Jumps Jump targets; synthetic function f(k) function ktest(val) function kt() return y(kret) end function kf() return z(kret) end if val then return kt() else return kf() end function kret(x) return k(x) end return foo(ktest) end
  • 17. Solution 2: Some conts are labels 1995 Kelsey: “In terms of compilation strategy, conts are return points, jumps can be compiled as gotos, and procs require a complete procedure-call mechanism.” Separate control and data flow 1992 Appel, “Compiling with Continuations” (ML)
  • 18. What about SSA? 1986-1988 Rosen, Wegman, Ferrante, Cytron, Zadeck: “Binding, not assignment” “The right number of names” Better notation makes it easier to transform programs Initial application of SSA was GVN
  • 19. SSA and CPS 1995 Kelsey: “Making [continuation uses] syntactically distinct restricts how continuations are used and makes CPS and SSA entirely equivalent.” SSA: Definitions must dominate uses CPS embeds static proof of SSA condition: all uses must be in scope 1998 Appel: “SSA is Functional Programming”
  • 20. Modern CPS 2007 Kennedy: Compiling with Continuations, Continued Nested scope Syntactic difference between continuations (control) and variables (data)
  • 21. Why CPS in 2016? SSA: How do I compile loops? CPS: How do I compile functions? “Get you a compiler that can do both”
  • 22. Example: Contification A function or clique of functions that always continues to the same label (calls the same continuation) can be integrated into the caller Like inlining, widens first-order flow graph: a mother optimization Unlike inlining, always a good idea: always a reduction
  • 23. CPS facilitates contification Concept of continuation❧ Globally unique labels and variable names❧ Interprocedural scope❧ Single term for program❧ Possible in SSA too of course
  • 24. And yet CPS: all uses must be in scope... but not all dominating definitions are in scope Transformations can corrupt scope tree function b0(k) function k1(v1) return k2() end function k2() return k(v1) end # XX k1(42) end 1999 Fluet and Weeks: MLton switches to SSA
  • 25. Alternate solution: CPS without nesting Values in scope are values that dominate Program is soup of continuations “CPS soup”
  • 26. CPS in Guile (define-type Label Natural) (struct Program ([entry : Label] [conts : (Map Label Cont)]))
  • 27. Conts (define-type Var Natural) (define-type Vars (Listof Var)) (struct KEntry ([body : Label] [exit : Label])) (struct KExpr ([vars : Vars] [k : Label] [exp : Exp])) (struct KExit) (define-type Cont (U KEntry KExpr KExit))
  • 28. Exps (define-type Op (U 'lookup 'add1 ...)) (struct Primcall ([op : Op] [args : Vars])) (struct Branch ([kt : Label] [exp : Expr])) (struct Call ([proc : Var] [args : Vars])) (struct Const ([val : Literal])) (struct Func ([entry : Label])) (struct Values ([args : Vars])) (define-type Exp (U Primcall Branch Call Const Func Values)) See language/cps.scm for full details
  • 29. ;; (lambda () (if (foo) (y) #f)) (Map (k0 (KEntry k1 k10)) (k1 (KExpr () k2 (Const 'foo))) (k2 (KExpr (v0) k3 (Primcall 'lookup (v0))) (k3 (KExpr (v1) k4 (Call v1 ()))) (k4 (KExpr (v2) k5 (Branch k8 (Values (v1)) (k5 (KExpr () k6 (Const 'y))) (k6 (KExpr (v3) k7 (Primcall 'lookup (v3))) (k7 (KExpr (v4) k10 (Call v4 ()))) (k8 (KExpr () k9 (Const #f))) (k9 (KExpr (v5) k10 (Values (v5)))) (k10 (KExit)))
  • 30. Salient details Variables available for use a flow property Variables bound by KExpr; values given by predecessors Expressions have labels and continue to other labels Return by continuing to the label identifying function’s KExit
  • 31. Orders of CPS Two phases in Guile Higher-order: Variables in “outer” functions may be referenced directly by “inner” functions; primitive support for recursive function binding forms ❧ First-order: Closure representations chosen, free variables (if any) accessed through closure ❧ “[Interprocedural] binding is better than assignment”
  • 32. About those maps (struct (v) IntMap ([min : Natural] [shift : Natural] [root : (U (Maybe v) (Branch v))])) (define-type (Branch v) (U (Vectorof (Maybe Branch)) (Vectorof (Maybe v)))) Shift 0 and root empty? {} Shift 0? {min: valueof(root)} Otherwise element i of root[i] is root for min +i*2^(shift-5), at shift-5.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38. Bagwell AMTs Array Mapped Trie Clojure-inspired data structures invented by Phil Bagwell O(n log n) in size Ref and update O(log n) Visit-each near-linear Unions and intersections very cheap
  • 39. Clojure innovation clojure.org/transients: Principled in-place mutation (define (intmap-map proc map) (persistent-intmap (intmap-fold (lambda (k v out) (intmap-add! out k (proc k v))) map (transient-intmap empty-intmap)))) Still O(n log n) but significant constant factor savings
  • 40. Intsets “Which labels are in this function?” (struct IntSet ([min : Natural] [shift : Natural] [root : (U Leaf Branch)])) (define-type Leaf UInt32) (define-type Branch (U (Vectorof (Maybe Branch)) (Vectorof Leaf))) Transient variants as well
  • 41. Optimizing with persistent data structures Example optimization: “Unboxing” Objective: use specific limited-precision machine numbers instead of arbitrary- precision polymorphic numbers
  • 42. function unbox_pass(conts): let out = conts for entry, body in conts.functions(): let types = infer_types(conts, entry, body) for label in body: match conts[label]: KExpr vars k (Primcall 'add1 (a)): if can_unbox?(label, k, a, types, conts): out = unbox(label, vars, k, a, out) _: pass return out
  • 43. function can_unbox?(label, k, arg, types, conts): match conts[k]: KExpr (result) _ _: let rtype, rmin, rmax = lookup_post_type(label, result) let atype, amin, amax = lookup_pre_type(label, a) return unboxable?(rtype, rmin, rmax) and unboxable?(atype, amin, amax)
  • 44. function unbox(label, vars, k, arg, conts): let uarg, res = fresh_vars(conts, 2) let kbox, kop = fresh_labels(conts, 2) conts = conts.replace(label, KEntry vars kop (Primcall 'unbox (a))) conts = conts.add(kop, KEntry (ua) kbox (Primcall 'uadd1 (ua))) return conts.add(kbox, KEntry (res) k (Primcall 'box (res)))
  • 45. Salient points To get name of result(s), have to look at continuation No easy way to get predecessors (without building predecessors map) No easy way to know if output var has other definitions ❧ On the other hand... no easy way to write local-only passes
  • 46. Backwards flow y = x & 0xffffffff We only need low 32 bits from x; can allow x to unbox... ...but can’t reach through from & to x. Solution: solve a flow problem (bits needed for each variable) Also works globally!❧
  • 47. Whither yon basic block? Not necessary; get in the way sometimes Need globally unique names for terms anyway Guile has terms that can bail out, unlike llvm; have to do big flow graph anyway Odd: almost never need dominators! Full flow analysis instead.
  • 48. Strengths Simple – few moving parts Immutability helps fit more of the problem into your head Interprocedural bindings pre-closure- conversion easier to reason about than locations in global heap Good space complexity for complicated flow analysis (type,range of all vars at all labels: n log n)
  • 49. Compared to SSA (1) Just as rigid scheduling-wise (compare to sea-of-nodes) Flow analysis over cont graph has more nodes than over basic block graph Additional log n factor for most operations Names as graph edges means lots of pointer chasing
  • 50. Compared to SSA (2) Sometimes have to renumber graph if pass wants specific ordering (usually topological) Values that flow into phi vars have no names! Lots of allocation (mitigate with zones?) Always throwing away analysis
  • 51. Summary Better notation makes it easier to transform programs If SSA + basic block graph works for you, great If not, map to a notation that is more tractable for you, transform there, and come back CPS name graph on persistent data structures seems to work for Guile; perhaps for you too?