@@ -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