@@ -629,10 +629,24 @@ static jl_cgval_t generic_bitcast(jl_codectx_t &ctx, const jl_cgval_t *argv)
629
629
setName (ctx.emission_context , vx, " bitcast_coercion" );
630
630
} else if (!vxt->isPointerTy () && llvmt->isPointerTy ()) {
631
631
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" );
633
645
} else {
634
646
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" );
636
650
}
637
651
}
638
652
0 commit comments