# Lab Talk ## Escape Analysis - Overview: - introduce brand new analysis to Julia compiler, to analyze dynamic scope and lifetime of variables - use escape information to optimize Julia's memory management - heap to stack allocation - elide `finalizer` calls - more scalar replacements - and ... ```julia ret = let # <= we can allocate `ary` on stack instead of heap ? ary = CuArray(...) r = ... # some "safe" operations on `ary` r end # <= we can call `finalize(ary)` here to free resoures earlier instead of letting GC to manage it ? ``` - Long-term potential: "**two-way abstract interpretation**" - Julia's type inference: _forward_ data-flow analysis - escape analysis: _backward_ data-flow analysis - both analysis can be combined to feedback and improve each other ! ```julia function foo(x::Ref{Union{Nothing,Int}}) if !isnothing(x[]) # using escape information, type inference can # understand `x[]::Int` in this pass return sin(x[]) end return 0 end ``` - Implementation: [EscapeAnalysis.jl](https://github.com/aviatesk/EscapeAnalysis.jl) ## compiler-plugin - Overview - bring yet more powerful and extensible staged programming interface to Julia - Everyone agrees that Julia's metaprogramming is very powerful, what more do you want ... ? - The issues with `@generated`: 1. _compile-time latency_ 2. _limited inference_ 3. _limited customization_ - Goals: allows Julia to express advanced computations much effectively - auto-differentiation: e.g. [Diffractor.jl](https://github.com/JuliaDiff/Diffractor.jl) - CAS application ? - prototype optimizations based on escape analysis as a compiler-plugin - many ideas ... - Links: - [Design document](https://hackmd.io/bVhb97Q4QTWeBQw8Rq4IFw?view) - PR: [wip: compiler-plugin: prototype builtin Cassette.jl-like overdubbing mechanism #41632](https://github.com/JuliaLang/julia/pull/41632) ## Some side works Random choices from my recent PRs: - [introduce @noinfer macro to tell the compiler to avoid excess inference #41931](https://github.com/JuliaLang/julia/pull/41931) - [compiler: speed up bootstrapping time by 25% #41794](https://github.com/JuliaLang/julia/pull/41794) - [optimizer: supports callsite annotations of inlining, fixes #18773 #41328](https://github.com/JuliaLang/julia/pull/41328) - [inference: simple back-propagation of constraints imposed on aliased field #41199 ](https://github.com/JuliaLang/julia/pull/41199) => Please tell my any questions or problems you have about Julia's type inference and optimization :) ## Toward my ideal Julia programming ... - we Julia programmers are greedy :p - we want to type checking w/o type annotations - we want better completions - we want to rename struct field with a single command - we want ... - insight: decent IDE features become very important as a language grows - what happens for Python and Ruby ... ? - gradual typing: successful, but often denies the beauty of generic programming... - [mypy](https://github.com/python/mypy) - [steep](https://github.com/soutaro/steep) - inter-procedural analysis based approach: not successful yet - [ruby/typeprof](https://github.com/ruby/typeprof) - => Let's leverage Julia compiler's decent analysis to realize next-generation developer tools for Julia ! - [JET.jl](https://github.com/aviatesk/JET.jl): JuliaCon2021 [talk](https://www.youtube.com/watch?v=7eOiGc8wfE0&t=1125s) ([slide](https://docs.google.com/presentation/d/16rwAf7m32NrdsV1WkkhAvCcZ8GuGA085nRQ47Ltol7I/edit#slide=id.ge18a4831d8_0_548)), [workshop](https://www.youtube.com/watch?v=wXRMwJdEjX4&t=1797s) - JET is already "useful" - e.g. JET-driven improvements on Julia Base: - [optimize optimizer #41580](https://github.com/JuliaLang/julia/pull/41580) - [more type-stable type-inference #41697](https://github.com/JuliaLang/julia/pull/41697) - A lot of stuff on the way: - useful frontend: e.g. [VSCode integration #227](https://github.com/aviatesk/JET.jl/pull/227) - moar performance ! - discard similar errors - introduce parallelism to type inference ? - moar accuracy ! - switch to new lattice design - introduce backward (two-way) abstract interpretation - implement memory-effect analysis - Julia Base integration: to help the community write more type-stable code - **LSP integration**: basis of new ideal IDE for Julia