Author: Timm Baeder Date: 2024-12-05T06:03:50+01:00 New Revision: 44be794658f9cd477ffd718b0322d1970c534274
URL: https://github.com/llvm/llvm-project/commit/44be794658f9cd477ffd718b0322d1970c534274 DIFF: https://github.com/llvm/llvm-project/commit/44be794658f9cd477ffd718b0322d1970c534274.diff LOG: [clang][bytecode] Not all null pointers are 0 (#118601) Get the Value from the ASTContext instead. Added: clang/test/AST/ByteCode/amdgpu-nullptr.cl Modified: clang/lib/AST/ByteCode/Compiler.cpp clang/lib/AST/ByteCode/Interp.h clang/lib/AST/ByteCode/Opcodes.td Removed: ################################################################################ diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index eb102f1e5c7f22..900312401bbda0 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -355,7 +355,9 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) { std::nullopt, true, false, /*IsMutable=*/false, nullptr); } - return this->emitNull(classifyPrim(CE->getType()), Desc, CE); + + uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(CE->getType()); + return this->emitNull(classifyPrim(CE->getType()), Val, Desc, CE); } case CK_PointerToIntegral: { @@ -3817,7 +3819,7 @@ template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) { // Convert pointers to bool. if (T == PT_Ptr || T == PT_FnPtr) { - if (!this->emitNull(*T, nullptr, E)) + if (!this->emitNull(*T, 0, nullptr, E)) return false; return this->emitNE(*T, E); } @@ -3857,11 +3859,12 @@ bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT, case PT_IntAPS: return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E); case PT_Ptr: - return this->emitNullPtr(nullptr, E); + return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT), + nullptr, E); case PT_FnPtr: - return this->emitNullFnPtr(nullptr, E); + return this->emitNullFnPtr(0, nullptr, E); case PT_MemberPtr: - return this->emitNullMemberPtr(nullptr, E); + return this->emitNullMemberPtr(0, nullptr, E); case PT_Float: return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E); case PT_FixedPoint: { @@ -4421,7 +4424,7 @@ bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType, if (Val.isLValue()) { if (Val.isNullPointer()) - return this->emitNull(ValType, nullptr, E); + return this->emitNull(ValType, 0, nullptr, E); APValue::LValueBase Base = Val.getLValueBase(); if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>()) return this->visit(BaseExpr); @@ -4431,7 +4434,7 @@ bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType, } else if (Val.isMemberPointer()) { if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) return this->emitGetMemberPtr(MemberDecl, E); - return this->emitNullMemberPtr(nullptr, E); + return this->emitNullMemberPtr(0, nullptr, E); } return false; @@ -4783,7 +4786,8 @@ bool Compiler<Emitter>::VisitCXXNullPtrLiteralExpr( if (DiscardResult) return true; - return this->emitNullPtr(nullptr, E); + uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType()); + return this->emitNullPtr(Val, nullptr, E); } template <class Emitter> @@ -5333,7 +5337,7 @@ bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) { // one here, and we don't need one either because the lambda cannot have // any captures, as verified above. Emit a null pointer. This is then // special-cased when interpreting to not emit any misleading diagnostics. - if (!this->emitNullPtr(nullptr, MD)) + if (!this->emitNullPtr(0, nullptr, MD)) return false; // Forward all arguments from the static invoker to the lambda call operator. @@ -6483,7 +6487,7 @@ bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) { if (!this->discard(SubExpr)) return false; - return this->emitNullPtr(nullptr, E); + return this->emitNullPtr(0, nullptr, E); } if (FromType->isNullPtrType() && ToT) { diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 25740c90d240ab..c5c2a5ef19cc4d 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -2432,9 +2432,11 @@ static inline bool ZeroIntAPS(InterpState &S, CodePtr OpPC, uint32_t BitWidth) { } template <PrimType Name, class T = typename PrimConv<Name>::T> -inline bool Null(InterpState &S, CodePtr OpPC, const Descriptor *Desc) { - // Note: Desc can be null. - S.Stk.push<T>(0, Desc); +inline bool Null(InterpState &S, CodePtr OpPC, uint64_t Value, + const Descriptor *Desc) { + // FIXME(perf): This is a somewhat often-used function and the value of a + // null pointer is almost always 0. + S.Stk.push<T>(Value, Desc); return true; } diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td index 26d5f70b44396b..0638f8249805f8 100644 --- a/clang/lib/AST/ByteCode/Opcodes.td +++ b/clang/lib/AST/ByteCode/Opcodes.td @@ -281,7 +281,7 @@ def ZeroIntAPS : Opcode { // [] -> [Pointer] def Null : Opcode { let Types = [PtrTypeClass]; - let Args = [ArgDesc]; + let Args = [ArgUint64, ArgDesc]; let HasGroup = 1; } diff --git a/clang/test/AST/ByteCode/amdgpu-nullptr.cl b/clang/test/AST/ByteCode/amdgpu-nullptr.cl new file mode 100644 index 00000000000000..d79f1cd4a237b4 --- /dev/null +++ b/clang/test/AST/ByteCode/amdgpu-nullptr.cl @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -no-enable-noundef-analysis %s -cl-std=CL2.0 -triple amdgcn -emit-llvm -o - | FileCheck %s + +// RUN: %clang_cc1 -no-enable-noundef-analysis %s -cl-std=CL2.0 -triple amdgcn -emit-llvm -fexperimental-new-constant-interpreter -o - | FileCheck %s + + +// CHECK: @fold_priv ={{.*}} local_unnamed_addr addrspace(1) global ptr addrspace(5) addrspacecast (ptr addrspace(1) null to ptr addrspace(5)), align 4 +private short *fold_priv = (private short*)(generic int*)(global void*)0; + +// CHECK: @fold_priv_arith ={{.*}} local_unnamed_addr addrspace(1) global ptr addrspace(5) inttoptr (i32 9 to ptr addrspace(5)), align 4 +private char *fold_priv_arith = (private char*)0 + 10; + + _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits