@@ -788,23 +788,76 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
788788 cache_gbl = nothing
789789 end
790790
791- if VERSION >= v " 1.13.0-DEV.623"
792- # Since Julia 1.13, the caller is responsible for initializing global variables that
793- # point to global values or bindings with their address in memory.
791+ # Since Julia 1.13, the caller is responsible for initializing global variables that
792+ # point to global values or bindings with their address in memory.
793+ # Similarly on previous versions when imaging=true, it is also the caller's responsibility
794+ # (see https://github.com/JuliaGPU/GPUCompiler.jl/issues/753), but we can support this on versions
795+ # that have HAS_LLVM_GVS_GLOBALS.
796+ gvs = nothing
797+ inits = nothing
798+ @static if VERSION >= v " 1.13.0-DEV.623"
794799 num_gvars = Ref {Csize_t} (0 )
795800 @ccall jl_get_llvm_gvs (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
796- C_NULL :: Ptr{Cvoid} ):: Nothing
801+ C_NULL :: Ptr{Cvoid}
802+ ):: Nothing
797803 gvs = Vector {Ptr{LLVM.API.LLVMOpaqueValue}} (undef, num_gvars[])
798804 @ccall jl_get_llvm_gvs (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
799- gvs:: Ptr{LLVM.API.LLVMOpaqueValue} ):: Nothing
805+ gvs:: Ptr{LLVM.API.LLVMOpaqueValue}
806+ ):: Nothing
807+
800808 inits = Vector {Ptr{Cvoid}} (undef, num_gvars[])
801809 @ccall jl_get_llvm_gv_inits (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
802810 inits:: Ptr{Cvoid} ):: Nothing
811+ elseif HAS_LLVM_GVS_GLOBALS
812+ if VERSION >= v " 1.12.0-DEV.1703"
813+ num_gvars = Ref {Csize_t} (0 )
814+ @ccall jl_get_llvm_gvs (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
815+ C_NULL :: Ptr{Cvoid}
816+ ):: Nothing
817+ gvs = Vector {Ptr{LLVM.API.LLVMOpaqueValue}} (undef, num_gvars[])
818+ @ccall jl_get_llvm_gvs_globals (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
819+ gvs:: Ptr{LLVM.API.LLVMOpaqueValue}
820+ ):: Nothing
821+ inits = Vector {Ptr{Cvoid}} (undef, num_gvars[])
822+ @ccall jl_get_llvm_gvs (native_code:: Ptr{Cvoid} , num_gvars:: Ptr{Csize_t} ,
823+ inits:: Ptr{Cvoid}
824+ ):: Nothing
825+ else
826+ gvs = get_llvm_global_vars (native_code)
827+ inits = get_llvm_global_inits (native_code)
828+ end
829+ end
803830
831+ # Maintain a map from global variables to their initialized Julia values.
832+ # The objects pointed to are perma-rooted, during codegen.
833+ # It is legal to call `Base.unsafe_pointer_to_objref` on `values(gv_to_value)`,
834+ # but x->pointer_from_objref(Base.unsafe_pointer_to_objref(x)) is not idempotent,
835+ # thus we store raw pointers here.
836+ # Currently GVs are privatized, so users may have to handle embedded pointers,
837+ # but this dictionary provides a clear indication that the embedded pointer is
838+ # indeed avalid Julia object.
839+ gv_to_value = Dict {String, Ptr{Cvoid}} ()
840+
841+ # On certain version of Julia we have no reliable way to match the `gvs` to their initializers `inits`.
842+ if gvs === nothing
843+ # global variables here properly.
844+ for gv in globals (llvm_mod)
845+ if ! haskey (metadata (gv), " julia.constgv" )
846+ continue
847+ end
848+ gv_to_value[LLVM. name (gv)] = C_NULL
849+ end
850+ else
851+ @assert inits != = nothing
804852 for (gv_ref, init) in zip (gvs, inits)
805853 gv = GlobalVariable (gv_ref)
806- val = const_inttoptr (ConstantInt (Int64 (init)), LLVM. PointerType ())
807- initializer! (gv, val)
854+ gv_to_value[LLVM. name (gv)] = init
855+ # set the initializer
856+ # TODO (vc): To enable full relocation we should actually strip out the initializers here.
857+ if LLVM. isnull (initializer (gv))
858+ val = const_inttoptr (ConstantInt (Int64 (init)), LLVM. PointerType ())
859+ initializer! (gv, val)
860+ end
808861 end
809862 end
810863
@@ -918,7 +971,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
918971 # ensure that the requested method instance was compiled
919972 @assert haskey (compiled, job. source)
920973
921- return llvm_mod, compiled
974+ return llvm_mod, compiled, gv_to_value
922975end
923976
924977# partially revert JuliaLangjulia#49391
0 commit comments