Skip to content

[clang][bytecode] Not all null pointers are 0 #118601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -3814,7 +3816,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);
}
Expand Down Expand Up @@ -3854,11 +3856,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: {
Expand Down Expand Up @@ -4418,7 +4421,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);
Expand All @@ -4428,7 +4431,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;
Expand Down Expand Up @@ -4780,7 +4783,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>
Expand Down Expand Up @@ -5330,7 +5334,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.
Expand Down Expand Up @@ -6477,7 +6481,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) {
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/AST/ByteCode/Interp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Opcodes.td
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def ZeroIntAPS : Opcode {
// [] -> [Pointer]
def Null : Opcode {
let Types = [PtrTypeClass];
let Args = [ArgDesc];
let Args = [ArgUint64, ArgDesc];
let HasGroup = 1;
}

Expand Down
12 changes: 12 additions & 0 deletions clang/test/AST/ByteCode/amdgpu-nullptr.cl
Original file line number Diff line number Diff line change
@@ -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;


Loading