Skip to content

Commit 2773ec1

Browse files
committed
Enable users to map from GV to Julia value
1 parent 531dccd commit 2773ec1

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/driver.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ const __llvm_initialized = Ref(false)
197197
end
198198

199199
@tracepoint "IR generation" begin
200-
ir, compiled = irgen(job)
200+
ir, compiled, gv_to_value = irgen(job)
201201
if job.config.entry_abi === :specfunc
202202
entry_fn = compiled[job.source].specfunc
203203
else
@@ -422,7 +422,7 @@ const __llvm_initialized = Ref(false)
422422
@tracepoint "verification" verify(ir)
423423
end
424424

425-
return ir, (; entry, compiled)
425+
return ir, (; entry, compiled, gv_to_value)
426426
end
427427

428428
@locked function emit_asm(@nospecialize(job::CompilerJob), ir::LLVM.Module,

src/irgen.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# LLVM IR generation
22

33
function irgen(@nospecialize(job::CompilerJob))
4-
mod, compiled = @tracepoint "emission" compile_method_instance(job)
4+
mod, compiled, gv_to_value = @tracepoint "emission" compile_method_instance(job)
55
if job.config.entry_abi === :specfunc
66
entry_fn = compiled[job.source].specfunc
77
else
@@ -120,7 +120,9 @@ function irgen(@nospecialize(job::CompilerJob))
120120
can_throw(job) || lower_throw!(mod)
121121
end
122122

123-
return mod, compiled
123+
# TODO: should we filter out non-preserved_gvs?
124+
125+
return mod, compiled, gv_to_value
124126
end
125127

126128

src/jlgen.jl

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -786,15 +786,17 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
786786
cache_gbl = nothing
787787
end
788788

789+
gv_to_value = Dict{String, Any}()
790+
num_gvars = Ref{Csize_t}(0)
791+
@ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
792+
C_NULL::Ptr{Cvoid})::Nothing
793+
gvs = Vector{Ptr{LLVM.API.LLVMOpaqueValue}}(undef, num_gvars[])
794+
@ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
795+
gvs::Ptr{LLVM.API.LLVMOpaqueValue})::Nothing
796+
789797
if VERSION >= v"1.13.0-DEV.623"
790798
# Since Julia 1.13, the caller is responsible for initializing global variables that
791799
# point to global values or bindings with their address in memory.
792-
num_gvars = Ref{Csize_t}(0)
793-
@ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
794-
C_NULL::Ptr{Cvoid})::Nothing
795-
gvs = Vector{Ptr{LLVM.API.LLVMOpaqueValue}}(undef, num_gvars[])
796-
@ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
797-
gvs::Ptr{LLVM.API.LLVMOpaqueValue})::Nothing
798800
inits = Vector{Ptr{Cvoid}}(undef, num_gvars[])
799801
@ccall jl_get_llvm_gv_inits(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
800802
inits::Ptr{Cvoid})::Nothing
@@ -803,6 +805,16 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
803805
gv = GlobalVariable(gv_ref)
804806
val = const_inttoptr(ConstantInt(Int64(init)), LLVM.PointerType())
805807
initializer!(gv, val)
808+
809+
# TODO: jl_binding_t?
810+
gv_to_value[LLVM.name(gv)] = Base.unsafe_pointer_to_objref(val)
811+
end
812+
else
813+
for gv_ref in gvs
814+
gv = GlobalVariable(gv_ref)
815+
val = reinterpret(Ptr{Cvoid}, initializer(gv))
816+
# TODO: jl_binding_t?
817+
gv_to_value[LLVM.name(gv)] = Base.unsafe_pointer_to_objref(val)
806818
end
807819
end
808820

@@ -874,7 +886,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
874886
# ensure that the requested method instance was compiled
875887
@assert haskey(compiled, job.source)
876888

877-
return llvm_mod, compiled
889+
return llvm_mod, compiled, gv_to_value
878890
end
879891

880892
# partially revert JuliaLangjulia#49391

0 commit comments

Comments
 (0)