yes! Julia does compile this way. Julia emphasizes JIT compilation based on runtime types rather than runtime values. I couldn't get runtime captures to work, e.g.
a = 2
function f(n)
return n * a
end
println(f(4))
@code_llvm f(4)
@code_native f(4)
That's because defining `a = 2` at the top level is a global constant, which can be changed at any time. Hence, the compiler can't optimize it out. If you tell the compiler it's a `const` though, it'll indeed do the optimization you were hoping for
const a = 2
f(n) = n * a
julia> @code_llvm f(4)
; @ REPL[2]:1 within `f'
define i64 @julia_f_290(i64 signext %0) {
top:
; @ REPL[2]:2 within `f'
; ┌ @ int.jl:88 within `*'
%1 = shl i64 %0, 1
; └
ret i64 %1
}
julia> @code_native f(4)
.text
; ┌ @ REPL[2]:2 within `f'
; │┌ @ int.jl:88 within `*'
leaq (%rdi,%rdi), %rax
; │└
retq
nopw %cs:(%rax,%rax)
; └