I have a physics background and is not very well-versed in programming. Sorry for the basic question, I should have learn julia basics properly.
julia> const one = 1
1
julia> add1(x) = x + 1
add1 (generic function with 1 method)
julia> addone(x) = x + one
addone (generic function with 1 method)
julia> addvar(x) = x + y
addvar (generic function with 1 method)
julia> y = 1
1
julia> [add1(1) addone(1) addvar(1)]
1Ă—3 Matrix{Int64}:
2 2 2
julia> @code_llvm add1(1)
; @ REPL[75]:1 within `add1'
define i64 @julia_add1_2957(i64 signext %0) {
top:
; ┌ @ int.jl:87 within `+'
%1 = add i64 %0, 1
; â””
ret i64 %1
}
julia> @code_llvm addone(1)
; @ REPL[76]:1 within `addone'
define i64 @julia_addone_2959(i64 signext %0) {
top:
; ┌ @ int.jl:87 within `+'
%1 = add i64 %0, 1
; â””
ret i64 %1
}
julia> @code_llvm addvar(1)
; @ REPL[77]:1 within `addvar'
define nonnull {}* @julia_addvar_2961(i64 signext %0) {
top:
%1 = alloca [2 x {}*], align 8
%gcframe2 = alloca [4 x {}*], align 16
%gcframe2.sub = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 0
%.sub = getelementptr inbounds [2 x {}*], [2 x {}*]* %1, i64 0, i64 0
%2 = bitcast [4 x {}*]* %gcframe2 to i8*
call void @llvm.memset.p0i8.i32(i8* nonnull align 16 dereferenceable(32) %2, i8 0, i32 32, i1 false)
%3 = call {}*** inttoptr (i64 4348951440 to {}*** ()*)() #4
%4 = bitcast [4 x {}*]* %gcframe2 to i64*
store i64 8, i64* %4, align 16
%5 = bitcast {}*** %3 to i64*
%6 = load i64, i64* %5, align 8
%7 = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 1
%8 = bitcast {}** %7 to i64*
store i64 %6, i64* %8, align 8
%9 = bitcast {}*** %3 to {}***
store {}** %gcframe2.sub, {}*** %9, align 8
%10 = load atomic i64, i64* inttoptr (i64 5759690280 to i64*) unordered, align 8
%11 = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 2
%12 = bitcast {}** %11 to i64*
store i64 %10, i64* %12, align 16
%13 = call nonnull {}* @jl_box_int64(i64 signext %0)
%14 = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 3
store {}* %13, {}** %14, align 8
store {}* %13, {}** %.sub, align 8
%15 = getelementptr inbounds [2 x {}*], [2 x {}*]* %1, i64 0, i64 1
%16 = bitcast {}** %15 to i64*
store i64 %10, i64* %16, align 8
%17 = call nonnull {}* @jl_apply_generic({}* inttoptr (i64 4463799936 to {}*), {}** nonnull %.sub, i32 2)
%18 = load i64, i64* %8, align 8
store i64 %18, i64* %5, align 8
ret {}* %17
}
julia> z = 1
1
julia> typeof(z)
Int64
julia> addint(x) = x + z
addint (generic function with 1 method)
julia> @code_llvm addint(1)
; @ REPL[85]:1 within `addint'
define nonnull {}* @julia_addint_2963(i64 signext %0) {
top:
%1 = alloca [2 x {}*], align 8
%gcframe2 = alloca [4 x {}*], align 16
%gcframe2.sub = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 0
%.sub = getelementptr inbounds [2 x {}*], [2 x {}*]* %1, i64 0, i64 0
%2 = bitcast [4 x {}*]* %gcframe2 to i8*
call void @llvm.memset.p0i8.i32(i8* nonnull align 16 dereferenceable(32) %2, i8 0, i32 32, i1 false)
%3 = call {}*** inttoptr (i64 4348951440 to {}*** ()*)() #4
%4 = bitcast [4 x {}*]* %gcframe2 to i64*
store i64 8, i64* %4, align 16
%5 = bitcast {}*** %3 to i64*
%6 = load i64, i64* %5, align 8
%7 = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 1
%8 = bitcast {}** %7 to i64*
store i64 %6, i64* %8, align 8
%9 = bitcast {}*** %3 to {}***
store {}** %gcframe2.sub, {}*** %9, align 8
%10 = load atomic i64, i64* inttoptr (i64 5681719064 to i64*) unordered, align 8
%11 = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 2
%12 = bitcast {}** %11 to i64*
store i64 %10, i64* %12, align 16
%13 = call nonnull {}* @jl_box_int64(i64 signext %0)
%14 = getelementptr inbounds [4 x {}*], [4 x {}*]* %gcframe2, i64 0, i64 3
store {}* %13, {}** %14, align 8
store {}* %13, {}** %.sub, align 8
%15 = getelementptr inbounds [2 x {}*], [2 x {}*]* %1, i64 0, i64 1
%16 = bitcast {}** %15 to i64*
store i64 %10, i64* %16, align 8
%17 = call nonnull {}* @jl_apply_generic({}* inttoptr (i64 4463799936 to {}*), {}** nonnull %.sub, i32 2)
%18 = load i64, i64* %8, align 8
store i64 %18, i64* %5, align 8
ret {}* %17
}
julia> using BenchmarkTools
julia> @btime add1(1)
0.085 ns (0 allocations: 0 bytes)
2
julia> @btime addone(1)
0.078 ns (0 allocations: 0 bytes)
2
julia> @btime addvar(1)
26.882 ns (0 allocations: 0 bytes)
2
julia> @btime addint(1)
34.177 ns (0 allocations: 0 bytes)
2