https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/202356
>From c8c676e3900ebf00f9b407140ed4ee70ed70d12f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Mon, 8 Jun 2026 15:05:35 +0200 Subject: [PATCH] as --- clang/lib/AST/ByteCode/Compiler.cpp | 24 ++++++++++++++++++++++-- clang/lib/AST/ByteCode/Interp.h | 24 ++++++++++++++++++++---- clang/lib/AST/ByteCode/Opcodes.td | 6 +++++- clang/lib/AST/ByteCode/Pointer.cpp | 9 ++++++--- clang/lib/AST/ByteCode/Pointer.h | 11 +++++++---- clang/test/AST/ByteCode/codegen.cl | 17 +++++++++++++++++ 6 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 clang/test/AST/ByteCode/codegen.cl diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index 8ea42fea03bee..8e284ef1acc86 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -472,7 +472,8 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) { // FIXME: I think the discard is wrong since the int->ptr cast might cause a // diagnostic. PrimType T = classifyPrim(IntType); - if (!this->emitGetIntPtr(T, E->getType().getTypePtr(), E)) + uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType()); + if (!this->emitGetIntPtr(T, E->getType().getTypePtr(), Val, E)) return false; QualType PtrType = E->getType(); @@ -490,10 +491,29 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *E) { case CK_NonAtomicToAtomic: case CK_NoOp: case CK_UserDefinedConversion: - case CK_AddressSpaceConversion: case CK_CPointerToObjCPointerCast: return this->delegate(SubExpr); + case CK_AddressSpaceConversion: { + if (E->containsErrors()) + return false; + + if (!this->visit(SubExpr)) + return false; + + uint64_t Val; + if (E->getType()->isPointerType()) + Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType()); + else + Val = 0; + + if (!this->emitCastAddressSpace(Val, E->getType().getTypePtr(), E)) + return false; + if (DiscardResult) + return this->emitPopPtr(E); + return true; + } + case CK_BitCast: { if (E->containsErrors()) return false; diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index 9d3d3b449bea3..126657bd5542d 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -3127,7 +3127,20 @@ template <PrimType Name, class T = typename PrimConv<Name>::T> inline bool Null(InterpState &S, CodePtr OpPC, uint64_t Value, const Type *Ty) { // 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, Ty); + if constexpr (std::is_same_v<T, Pointer>) + S.Stk.push<T>(Value, Ty, /*Offset=*/0, /*IsNull=*/true); + else + S.Stk.push<T>(Value, Ty); + return true; +} + +inline bool CastAddressSpace(InterpState &S, CodePtr OpPC, uint64_t Value, + const Type *Ty) { + const Pointer Ptr = S.Stk.pop<Pointer>(); + if (Ptr.isZero()) + S.Stk.push<Pointer>(Value, Ty); + else + S.Stk.push<Pointer>(Ptr); return true; } @@ -3567,7 +3580,8 @@ inline bool GetFnPtr(InterpState &S, CodePtr OpPC, const Function *Func) { } template <PrimType Name, class T = typename PrimConv<Name>::T> -inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Type *Ty) { +inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Type *Ty, + uint64_t NullValue) { const T &IntVal = S.Stk.pop<T>(); S.CCEDiag(S.Current->getSource(OpPC), diag::note_constexpr_invalid_cast) @@ -3592,7 +3606,8 @@ inline bool GetIntPtr(InterpState &S, CodePtr OpPC, const Type *Ty) { S.P.getFunction((const FunctionDecl *)IntVal.getPtr()); S.Stk.push<Pointer>(F, IntVal.getOffset()); } else { - S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Ty); + S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Ty, 0, + static_cast<uint64_t>(IntVal) == NullValue); } } else { S.Stk.push<Pointer>(static_cast<uint64_t>(IntVal), Ty); @@ -3919,7 +3934,8 @@ inline bool AllocCN(InterpState &S, CodePtr OpPC, const Descriptor *ElementDesc, return false; // If this failed and is nothrow, just return a null ptr. - S.Stk.push<Pointer>(0, ElementDesc->getType().getTypePtr()); + S.Stk.push<Pointer>(0, ElementDesc->getType().getTypePtr(), 0, + /*IsNull=*/true); return true; } if (NumElements.isNegative()) { diff --git a/clang/lib/AST/ByteCode/Opcodes.td b/clang/lib/AST/ByteCode/Opcodes.td index 09c616aa2ff1d..a60e298a553b8 100644 --- a/clang/lib/AST/ByteCode/Opcodes.td +++ b/clang/lib/AST/ByteCode/Opcodes.td @@ -315,6 +315,10 @@ def Null : Opcode { let HasGroup = 1; } +def CastAddressSpace : Opcode { + let Args = [ArgUint64, ArgTypePtr]; +} + //===----------------------------------------------------------------------===// // Pointer generation //===----------------------------------------------------------------------===// @@ -596,7 +600,7 @@ def GetFnPtr : Opcode { def GetIntPtr : Opcode { let Types = [AluTypeClass]; - let Args = [ArgTypePtr]; + let Args = [ArgTypePtr, ArgUint64]; let HasGroup = 1; } diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp index 1b15c59c9fbff..91d55c4f82f7a 100644 --- a/clang/lib/AST/ByteCode/Pointer.cpp +++ b/clang/lib/AST/ByteCode/Pointer.cpp @@ -337,7 +337,8 @@ void Pointer::print(llvm::raw_ostream &OS) const { OS << "}"; } break; case Storage::Int: - OS << "(Int) {" << Int.Value << " + " << Offset << ", " << Int.Ty << "}"; + OS << "(Int) {" << Int.Value << " + " << Offset << ", " << Int.Ty << ", " + << (Int.IsNull ? "null" : "nonnull") << '}'; break; case Storage::Fn: OS << "(Fn) { " << Fn.Func << " + " << Offset << " }"; @@ -1029,7 +1030,8 @@ std::optional<IntPointer> IntPointer::atOffset(const interp::Context &Ctx, ASTCtx.toCharUnitsFromBits(Layout.getFieldOffset(FieldIndex)) .getQuantity(); - return IntPointer{FD->getType().getTypePtr(), this->Value + FieldOffset}; + uint64_t NewValue = this->Value + FieldOffset; + return IntPointer{FD->getType().getTypePtr(), NewValue, NewValue == 0}; } IntPointer IntPointer::baseCast(const interp::Context &Ctx, @@ -1063,5 +1065,6 @@ IntPointer IntPointer::baseCast(const interp::Context &Ctx, const RecordDecl *RD = BaseDesc->ElemRecord->getDecl(); QualType T = RD->getASTContext().getTagType(ElaboratedTypeKeyword::None, std::nullopt, RD, false); - return {T.getTypePtr(), Value + BaseLayoutOffset.getQuantity()}; + uint64_t NewValue = Value + BaseLayoutOffset.getQuantity(); + return {T.getTypePtr(), NewValue, NewValue == 0}; } diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h index 2b455ebc5efb5..9d4f40d8e3c43 100644 --- a/clang/lib/AST/ByteCode/Pointer.h +++ b/clang/lib/AST/ByteCode/Pointer.h @@ -47,6 +47,7 @@ struct BlockPointer { struct IntPointer { const Type *Ty; uint64_t Value; + bool IsNull = false; std::optional<IntPointer> atOffset(const Context &Ctx, unsigned Offset) const; IntPointer baseCast(const Context &Ctx, unsigned BaseOffset) const; @@ -112,15 +113,17 @@ class Pointer { static constexpr unsigned RootPtrMark = ~0u; public: - Pointer() : StorageKind(Storage::Int), Int{nullptr, 0} {} + Pointer() : StorageKind(Storage::Int), Int{nullptr, 0, true} {} Pointer(IntPointer &&IntPtr) : StorageKind(Storage::Int), Int(std::move(IntPtr)) {} Pointer(Block *B); Pointer(Block *B, uint64_t BaseAndOffset); Pointer(const Pointer &P); Pointer(Pointer &&P); - Pointer(uint64_t Address, const Type *Ty, uint64_t Offset = 0) - : Offset(Offset), StorageKind(Storage::Int), Int{Ty, Address} {} + Pointer(uint64_t Address, const Type *Ty, uint64_t Offset = 0, + std::optional<bool> IsNull = std::nullopt) + : Offset(Offset), StorageKind(Storage::Int), + Int{Ty, Address, IsNull.value_or(Address == 0)} {} Pointer(const Function *F, uint64_t Offset = 0) : Offset(Offset), StorageKind(Storage::Fn), Fn{F} {} Pointer(const Type *TypePtr, const Type *TypeInfoType, uint64_t Offset = 0) @@ -276,7 +279,7 @@ class Pointer { bool isZero() const { switch (StorageKind) { case Storage::Int: - return Int.Value == 0 && Offset == 0; + return Int.IsNull; case Storage::Block: return BS.Pointee == nullptr; case Storage::Fn: diff --git a/clang/test/AST/ByteCode/codegen.cl b/clang/test/AST/ByteCode/codegen.cl new file mode 100644 index 0000000000000..d2e331905e02d --- /dev/null +++ b/clang/test/AST/ByteCode/codegen.cl @@ -0,0 +1,17 @@ +// RUN: %clang_cc1 -no-enable-noundef-analysis %s -cl-std=CL2.0 -triple amdgcn -fcommon -O0 -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 -no-enable-noundef-analysis %s -cl-std=CL2.0 -triple amdgcn -fcommon -O0 -emit-llvm -o - -fexperimental-new-constant-interpreter | FileCheck %s + +// CHECK: @fold_int_local ={{.*}} addrspace(1) global i32 13, align 4 +int fold_int_local = (int)(local void*)(generic char*)(global int*)0 + 14; + +// CHECK: @fold_int ={{.*}} addrspace(1) global i32 13, align 4 +int fold_int = (int)(private void*)(generic char*)(global int*)0 + 14; + +// CHECK: @test_static_var_private.sp4 = internal addrspace(1) global ptr addrspace(5) null, align 4 +// CHECK: @test_static_var_private.sp5 = internal addrspace(1) global ptr addrspace(5) addrspacecast (ptr null to ptr addrspace(5)), align 4 + +void test_static_var_private(void) { + static private char *sp4 = (private char*)((void)0, 0); + const int x = 0; + static private char *sp5 = (private char*)x; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
