From 7679289e17511c76f729eef3608c23f4ee296430 Mon Sep 17 00:00:00 2001 From: gbaraldi Date: Tue, 12 Aug 2025 11:16:55 -0300 Subject: [PATCH] Fix compile time regression for null ptr comparisons --- src/cgutils.cpp | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cgutils.cpp b/src/cgutils.cpp index 97a58f8aa419c..e9b2bc3aa2ed0 100644 --- a/src/cgutils.cpp +++ b/src/cgutils.cpp @@ -1601,6 +1601,18 @@ static void undef_var_error_ifnot(jl_codectx_t &ctx, Value *ok, jl_sym_t *name, ctx.builder.SetInsertPoint(ifok); } + +static bool has_known_null_nullptr(Type *T) +{ + if (auto PT = cast(T)) { + auto addrspace = PT->getAddressSpace(); + if (addrspace == AddressSpace::Generic || (AddressSpace::FirstSpecial <= addrspace && addrspace <= AddressSpace::LastSpecial)) { + return true; + } + } + return false; +} + // ctx.builder.CreateIsNotNull(v) lowers incorrectly in non-standard // address spaces where null is not zero // TODO: adapt to https://github.com/llvm/llvm-project/pull/131557 once merged @@ -1608,10 +1620,11 @@ static Value *null_pointer_cmp(jl_codectx_t &ctx, Value *v) { ++EmittedNullchecks; Type *T = v->getType(); - return ctx.builder.CreateICmpNE( - v, - ctx.builder.CreateAddrSpaceCast( - Constant::getNullValue(ctx.builder.getPtrTy(0)), T)); + if (has_known_null_nullptr(T)) + return ctx.builder.CreateIsNotNull(v); + else + return ctx.builder.CreateICmpNE(v, ctx.builder.CreateAddrSpaceCast( + Constant::getNullValue(ctx.builder.getPtrTy(0)), T)); }