Skip to content

Instantly share code, notes, and snippets.

@ChrisRackauckas
Last active October 28, 2017 04:56
Show Gist options
  • Save ChrisRackauckas/7f3b1664c17ed8350888e217a8721e2f to your computer and use it in GitHub Desktop.
Save ChrisRackauckas/7f3b1664c17ed8350888e217a8721e2f to your computer and use it in GitHub Desktop.
Julia v0.7's stack-allocation optimizations and escape analysis
https://github.com/JuliaLang/julia/pull/22684
Watch it in action: stack-allocated mutable static sized arrays. Example thanks to Valentin Churavy.
julia> function f(x::Int64)
A = MArray{Tuple{3}}(1, 2, 3)
A[2] += x
return A[2]-A[1]
end
f (generic function with 1 method)
julia> @code_llvm f(2)
; Function f
; Location: REPL[13]
define i64 @julia_f_63256(i64) #0 {
L23:
; Location: REPL[13]:4
%1 = add i64 %0, 1
ret i64 %1
}
################################################
v0.6
julia> function f(x::Int64)
A = MArray{Tuple{3}}(1, 2, 3)
A[2] += x
return A[2]-A[1]
end
f (generic function with 1 method)
julia> @code_llvm f(2)
define i64 @julia_f_60721(i64) #0 !dbg !5 {
L25:
%ptls_i8 = call i8* asm "movq %fs:0, $0;\0Aaddq $$-10888, $0", "=r,~{dirflag},~{fpsr},~{flags}"() #5
%ptls = bitcast i8* %ptls_i8 to i8****
%1 = alloca [4 x i8**], align 8
%.sub = getelementptr inbounds [4 x i8**], [4 x i8**]* %1, i64 0, i64 0
%2 = getelementptr [4 x i8**], [4 x i8**]* %1, i64 0, i64 2
%3 = bitcast [4 x i8**]* %1 to i64*
%4 = bitcast i8*** %2 to i8*
call void @llvm.memset.p0i8.i64(i8* %4, i8 0, i64 16, i32 8, i1 false)
store i64 4, i64* %3, align 8
%5 = getelementptr [4 x i8**], [4 x i8**]* %1, i64 0, i64 1
%6 = bitcast i8* %ptls_i8 to i64*
%7 = load i64, i64* %6, align 8
%8 = bitcast i8*** %5 to i64*
store i64 %7, i64* %8, align 8
store i8*** %.sub, i8**** %ptls, align 8
%9 = call i8** @jl_gc_pool_alloc(i8* %ptls_i8, i32 1456, i32 32)
%10 = getelementptr i8*, i8** %9, i64 -1
%11 = bitcast i8** %10 to i8***
store i8** inttoptr (i64 140077945663888 to i8**), i8*** %11, align 8
store i8** %9, i8*** %2, align 8
%.repack = bitcast i8** %9 to i64*
store i64 1, i64* %.repack, align 16
%.repack2 = getelementptr i8*, i8** %9, i64 1
%12 = bitcast i8** %.repack2 to i64*
%.repack3 = getelementptr inbounds i8*, i8** %9, i64 2
%13 = bitcast i8** %.repack3 to i64*
store i64 3, i64* %13, align 16
%14 = add i64 %0, 2
store i64 %14, i64* %12, align 1
%15 = add i64 %0, 1
%16 = load i64, i64* %8, align 8
store i64 %16, i64* %6, align 8
ret i64 %15
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment