Skip to content

Commit 9111406

Browse files
[backports-release-1.11] Backport #59259 ("Fix compile time regression for null ptr comparisons") to Julia 1.11.x (#59446)
Backports #59259 to 1.11 (cherry picked from commit 920df7a). For more details, see #59445 (comment). Targets `backports-release-1.11` (#59336). --------- Co-authored-by: Gabriel Baraldi <[email protected]>
1 parent 8230e8b commit 9111406

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/cgutils.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,10 +1395,30 @@ static void undef_var_error_ifnot(jl_codectx_t &ctx, Value *ok, jl_sym_t *name,
13951395
ctx.builder.SetInsertPoint(ifok);
13961396
}
13971397

1398+
1399+
static bool has_known_null_nullptr(Type *T)
1400+
{
1401+
if (auto PT = dyn_cast<PointerType>(T)) {
1402+
auto addrspace = PT->getAddressSpace();
1403+
if (addrspace == AddressSpace::Generic || (AddressSpace::FirstSpecial <= addrspace && addrspace <= AddressSpace::LastSpecial)) {
1404+
return true;
1405+
}
1406+
}
1407+
return false;
1408+
}
1409+
1410+
// ctx.builder.CreateIsNotNull(v) lowers incorrectly in non-standard
1411+
// address spaces where null is not zero
1412+
// TODO: adapt to https://github.com/llvm/llvm-project/pull/131557 once merged
13981413
static Value *null_pointer_cmp(jl_codectx_t &ctx, Value *v)
13991414
{
14001415
++EmittedNullchecks;
1401-
return ctx.builder.CreateIsNotNull(v);
1416+
Type *T = v->getType();
1417+
if (has_known_null_nullptr(T) || !isa<PointerType>(T)) // i64/i32 are considered pointer like here
1418+
return ctx.builder.CreateIsNotNull(v);
1419+
else
1420+
return ctx.builder.CreateICmpNE(v, ctx.builder.CreateAddrSpaceCast(
1421+
Constant::getNullValue(ctx.builder.getPtrTy(0)), T));
14021422
}
14031423

14041424

0 commit comments

Comments
 (0)