Skip to content

Commit d5b2b2a

Browse files
maleadtKristofferC
authored andcommitted
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 ec1115d commit d5b2b2a

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/intrinsics.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,24 @@ static jl_cgval_t generic_bitcast(jl_codectx_t &ctx, const jl_cgval_t *argv)
629629
setName(ctx.emission_context, vx, "bitcast_coercion");
630630
} else if (!vxt->isPointerTy() && llvmt->isPointerTy()) {
631631
vx = emit_inttoptr(ctx, vx, llvmt);
632-
setName(ctx.emission_context, vx, "bitcast_coercion");
632+
if (isa<Instruction>(vx) && !vx->hasName())
633+
// emit_inttoptr may undo an PtrToInt
634+
setName(ctx.emission_context, vx, "bitcast_coercion");
635+
} else if (vxt->isPointerTy() && llvmt->isPointerTy()) {
636+
// emit_bitcast preserves the origin address space, which we can't have here
637+
#if JL_LLVM_VERSION >= 170000
638+
vx = ctx.builder.CreateAddrSpaceCast(vx, llvmt);
639+
#else
640+
vx = ctx.builder.CreatePointerBitCastOrAddrSpaceCast(vx, llvmt);
641+
#endif
642+
if (isa<Instruction>(vx) && !vx->hasName())
643+
// cast may have been folded
644+
setName(ctx.emission_context, vx, "bitcast_coercion");
633645
} else {
634646
vx = emit_bitcast(ctx, vx, llvmt);
635-
setName(ctx.emission_context, vx, "bitcast_coercion");
647+
if (isa<Instruction>(vx) && !vx->hasName())
648+
// emit_bitcast may undo another bitcast
649+
setName(ctx.emission_context, vx, "bitcast_coercion");
636650
}
637651
}
638652

0 commit comments

Comments
 (0)