Skip to content

Commit bc3e9d1

Browse files
committed
don't initialize non-relocatible globals
1 parent acd969c commit bc3e9d1

3 files changed

Lines changed: 27 additions & 12 deletions

File tree

src/driver.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ const __llvm_initialized = Ref(false)
256256
dyn_ir, dyn_meta = codegen(:llvm, CompilerJob(dyn_job; config))
257257
dyn_entry_fn = LLVM.name(dyn_meta.entry)
258258
merge!(compiled, dyn_meta.compiled)
259+
merge!(gv_to_value, dyn_meta.gv_to_value)
259260
@assert context(dyn_ir) == context(ir)
260261
link!(ir, dyn_ir)
261262
changed = true
@@ -319,6 +320,17 @@ const __llvm_initialized = Ref(false)
319320
end
320321
end
321322

323+
# TODO: Move this to somewhere else?
324+
@tracepoint "Resolve relocations eagerly" for gv in globals(ir)
325+
name = LLVM.name(gv)
326+
init = get(gv_to_value, name, nothing)
327+
if init !== nothing
328+
@assert initializer(gv) === nothing
329+
val = const_inttoptr(ConstantInt(reinterpret(UInt, init)), LLVM.PointerType())
330+
initializer!(gv, val)
331+
end
332+
end
333+
322334
@tracepoint "IR post-processing" begin
323335
# mark the kernel entry-point functions (optimization may need it)
324336
if job.config.kernel

src/irgen.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ function irgen(@nospecialize(job::CompilerJob))
5555
new_name = safe_name(old_name)
5656
if old_name != new_name
5757
LLVM.name!(val, new_name)
58+
val = get(gv_to_value, old_name, nothing)
59+
if val !== nothing
60+
delete!(gv_to_value, old_name)
61+
gv_to_value[new_name] = val
62+
end
5863
end
5964
end
6065

@@ -120,8 +125,6 @@ function irgen(@nospecialize(job::CompilerJob))
120125
can_throw(job) || lower_throw!(mod)
121126
end
122127

123-
# TODO: should we filter out non-preserved_gvs?
124-
125128
return mod, compiled, gv_to_value
126129
end
127130

src/jlgen.jl

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,12 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
808808
cache_gbl = nothing
809809
end
810810

811-
gv_to_value = Dict{String, Any}()
811+
gv_to_value = Dict{String, Ptr{Cvoid}}()
812812

813+
# The caller is responsible for initializing global variables that
814+
# point to global values or bindings with their address in memory.
815+
# For Julia < v"1.13" to enable relocation we strip out the initializers here.
813816
if VERSION >= v"1.13.0-DEV.623"
814-
# Since Julia 1.13, the caller is responsible for initializing global variables that
815-
# point to global values or bindings with their address in memory.
816817
num_gvars = Ref{Csize_t}(0)
817818
@ccall jl_get_llvm_gvs(native_code::Ptr{Cvoid}, num_gvars::Ptr{Csize_t},
818819
C_NULL::Ptr{Cvoid})::Nothing
@@ -826,12 +827,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
826827

827828
for (gv_ref, init) in zip(gvs, inits)
828829
gv = GlobalVariable(gv_ref)
829-
val = const_inttoptr(ConstantInt(Int64(init)), LLVM.PointerType())
830-
initializer!(gv, val)
831-
832-
# TODO: jl_binding_t?
833-
@show LLVM.name(gv), val
834-
gv_to_value[LLVM.name(gv)] = Base.unsafe_pointer_to_objref(val)
830+
gv_to_value[LLVM.name(gv)] = init
835831
end
836832
else
837833
# Prior to this version of Julia we only had access to the values that the global variables
@@ -870,11 +866,15 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
870866
end
871867
ptr = reinterpret(Ptr{Cvoid}, convert(UInt, init))
872868
if ptr in gvalues
873-
gv_to_value[LLVM.name(gv)] = Base.unsafe_pointer_to_objref(ptr)
869+
gv_to_value[LLVM.name(gv)] = ptr
874870
end
871+
LLVM.initializer!(gv, nothing)
875872
end
876873
@assert length(gv_to_value) == length(gvalues)
877874
end
875+
# It's valid to call Base.unsafe_pointer_to_objref on values(gv_to_value),
876+
# but we may not be able to "easily" obtain the pointer back later.
877+
# (Types, etc, disallow Base.pointer_from_objref on them.)
878878

879879
if VERSION >= v"1.13.0-DEV.1120"
880880
# on sufficiently recent versions of Julia, we can query the CIs compiled.

0 commit comments

Comments
 (0)