Skip to content

Commit 85d6aa6

Browse files
authored
Fix typed pointer gv initialization (#758)
1 parent 161bc3e commit 85d6aa6

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/jlgen.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,20 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
846846
continue
847847
end
848848
gv_to_value[LLVM.name(gv)] = C_NULL
849+
val = initializer(gv)
850+
if val === nothing
851+
continue
852+
end
853+
while isa(val, LLVM.ConstantExpr)
854+
if in(opcode(val), (LLVM.API.LLVMBitCast, LLVM.API.LLVMPtrToInt, LLVM.API.LLVMAddrSpaceCast, LLVM.API.LLVMIntToPtr))
855+
val = operands(val)[1]
856+
continue
857+
end
858+
break
859+
end
860+
if isa(val, LLVM.ConstantInt)
861+
gv_to_value[LLVM.name(gv)] = reinterpret(Ptr{Cvoid}, convert(UInt, val))
862+
end
849863
end
850864
else
851865
@assert inits !== nothing
@@ -855,7 +869,7 @@ function compile_method_instance(@nospecialize(job::CompilerJob))
855869
# set the initializer
856870
# TODO(vc): To enable full relocation we should actually strip out the initializers here.
857871
if LLVM.isnull(initializer(gv))
858-
val = const_inttoptr(ConstantInt(Int64(init)), LLVM.PointerType())
872+
val = const_inttoptr(ConstantInt(Int64(init)), value_type(initializer(gv)))
859873
initializer!(gv, val)
860874
end
861875
end

test/native.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ end
5959

6060
if VERSION >= v"1.12"
6161
@test length(meta.gv_to_value) == 1
62+
for (k, v) in meta.gv_to_value
63+
@test v != C_NULL
64+
end
6265
end
6366
# TODO: Global values get privatized, so we can't find them by name anymore.
6467
# %.not = icmp eq ptr %"sym::Symbol", inttoptr (i64 140096668482288 to ptr), !dbg !38

0 commit comments

Comments
 (0)