Skip to content

Commit c9a149b

Browse files
authored
Merge pull request #38424 from JuliaLang/tb/inttoptr_as
Don't create invalid bitcasts.
2 parents 85000b1 + 40631ed commit c9a149b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/codegen.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,8 +1214,13 @@ static Value *emit_inttoptr(jl_codectx_t &ctx, Value *v, Type *ty)
12141214
{
12151215
// Almost all of our inttoptr are generated due to representing `Ptr` with `T_size`
12161216
// in LLVM and most of these integers are generated from `ptrtoint` in the first place.
1217-
if (auto I = dyn_cast<PtrToIntInst>(v))
1218-
return ctx.builder.CreateBitCast(I->getOperand(0), ty);
1217+
if (auto I = dyn_cast<PtrToIntInst>(v)) {
1218+
auto ptr = I->getOperand(0);
1219+
if (ty->getPointerAddressSpace() == ptr->getType()->getPointerAddressSpace())
1220+
return ctx.builder.CreateBitCast(ptr, ty);
1221+
else if (ty->getPointerElementType() == ptr->getType()->getPointerElementType())
1222+
return ctx.builder.CreateAddrSpaceCast(ptr, ty);
1223+
}
12191224
return ctx.builder.CreateIntToPtr(v, ty);
12201225
}
12211226

0 commit comments

Comments
 (0)