Skip to content

Commit 4bc3d07

Browse files
maleadtKristofferC
authored and
KristofferC
committed
Fixes for bitcast bugs with LLVM 17 / opaque pointers (#54548)
Skip setName on folded inputs, and ensure the correct pointer address space is used. (cherry picked from commit baca8ba)
1 parent ddd5c28 commit 4bc3d07

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

src/intrinsics.cpp

+13-2
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,21 @@ static jl_cgval_t generic_bitcast(jl_codectx_t &ctx, ArrayRef<jl_cgval_t> argv)
628628
setName(ctx.emission_context, vx, "bitcast_coercion");
629629
} else if (!vxt->isPointerTy() && llvmt->isPointerTy()) {
630630
vx = emit_inttoptr(ctx, vx, llvmt);
631-
setName(ctx.emission_context, vx, "bitcast_coercion");
631+
} else if (vxt->isPointerTy() && llvmt->isPointerTy()) {
632+
// emit_bitcast preserves the origin address space, which we can't have here
633+
#if JL_LLVM_VERSION >= 170000
634+
vx = ctx.builder.CreateAddrSpaceCast(vx, llvmt);
635+
#else
636+
vx = ctx.builder.CreatePointerBitCastOrAddrSpaceCast(vx, llvmt);
637+
#endif
638+
if (isa<Instruction>(vx) && !vx->hasName())
639+
// cast may have been folded
640+
setName(ctx.emission_context, vx, "bitcast_coercion");
632641
} else {
633642
vx = emit_bitcast(ctx, vx, llvmt);
634-
setName(ctx.emission_context, vx, "bitcast_coercion");
643+
if (isa<Instruction>(vx) && !vx->hasName())
644+
// emit_bitcast may undo another bitcast
645+
setName(ctx.emission_context, vx, "bitcast_coercion");
635646
}
636647
}
637648

test/intrinsics.jl

+13
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,16 @@ Base.show(io::IO, a::IntWrap) = print(io, "IntWrap(", a.x, ")")
345345
@test r2 isa IntWrap && r2.x === 103 === r[].x && r2 !== r[]
346346
end
347347
end)()
348+
349+
@testset "issue #54548" begin
350+
@inline passthrough(ptr::Core.LLVMPtr{T,A}) where {T,A} = Base.llvmcall(("""
351+
define ptr addrspace(1) @entry(ptr addrspace(1) %0) #0 {
352+
entry:
353+
ret ptr addrspace(1) %0
354+
}
355+
356+
attributes #0 = { alwaysinline }""", "entry"),
357+
Core.LLVMPtr{T,A}, Tuple{Core.LLVMPtr{T,A}}, ptr)
358+
f(gws) = passthrough(Core.bitcast(Core.LLVMPtr{UInt32,1}, gws))
359+
f(C_NULL)
360+
end

0 commit comments

Comments
 (0)