[llvm] 950bb68 - [SandboxIR] Implement ConstantPointerNull (#107320)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 4 15:55:12 PDT 2024
Author: vporpo
Date: 2024-09-04T15:55:08-07:00
New Revision: 950bb68516eb564c29815997450bdb6516ffdcec
URL: https://github.com/llvm/llvm-project/commit/950bb68516eb564c29815997450bdb6516ffdcec
DIFF: https://github.com/llvm/llvm-project/commit/950bb68516eb564c29815997450bdb6516ffdcec.diff
LOG: [SandboxIR] Implement ConstantPointerNull (#107320)
This patch implements sandboxir::ConstantPointerNull mirroring
llvm::ConstantPointerNull.
Added:
Modified:
llvm/include/llvm/SandboxIR/SandboxIR.h
llvm/include/llvm/SandboxIR/SandboxIRValues.def
llvm/lib/SandboxIR/SandboxIR.cpp
llvm/unittests/SandboxIR/SandboxIRTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h
index 89bc9c46581fcd..4db4fae24b4b34 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -121,6 +121,7 @@ class BasicBlock;
class ConstantInt;
class ConstantFP;
class ConstantAggregateZero;
+class ConstantPointerNull;
class Context;
class Function;
class Instruction;
@@ -318,6 +319,7 @@ class Value {
friend class ConstantArray; // For `Val`.
friend class ConstantStruct; // For `Val`.
friend class ConstantAggregateZero; // For `Val`.
+ friend class ConstantPointerNull; // For `Val`.
/// All values point to the context.
Context &Ctx;
@@ -987,6 +989,35 @@ class ConstantAggregateZero final : public Constant {
#endif
};
+// TODO: Inherit from ConstantData.
+class ConstantPointerNull final : public Constant {
+ ConstantPointerNull(llvm::ConstantPointerNull *C, Context &Ctx)
+ : Constant(ClassID::ConstantPointerNull, C, Ctx) {}
+ friend class Context; // For constructor.
+
+public:
+ static ConstantPointerNull *get(PointerType *Ty);
+
+ PointerType *getType() const;
+
+ /// For isa/dyn_cast.
+ static bool classof(const sandboxir::Value *From) {
+ return From->getSubclassID() == ClassID::ConstantPointerNull;
+ }
+ unsigned getUseOperandNo(const Use &Use) const final {
+ llvm_unreachable("ConstantPointerNull has no operands!");
+ }
+#ifndef NDEBUG
+ void verify() const override {
+ assert(isa<llvm::ConstantPointerNull>(Val) && "Expected a CPNull!");
+ }
+ void dumpOS(raw_ostream &OS) const override {
+ dumpCommonPrefix(OS);
+ dumpCommonSuffix(OS);
+ }
+#endif
+};
+
/// Iterator for `Instruction`s in a `BasicBlock.
/// \Returns an sandboxir::Instruction & when derereferenced.
class BBIterator {
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index b2180ba58afcc0..fce5aacc8c86d4 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -31,6 +31,7 @@ DEF_CONST(ConstantArray, ConstantArray)
DEF_CONST(ConstantStruct, ConstantStruct)
DEF_CONST(ConstantVector, ConstantVector)
DEF_CONST(ConstantAggregateZero, ConstantAggregateZero)
+DEF_CONST(ConstantPointerNull, ConstantPointerNull)
#ifndef DEF_INSTR
#define DEF_INSTR(ID, OPCODE, CLASS)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index 89bbdf575c2458..acbc5c17ab2568 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -2426,6 +2426,17 @@ Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
cast<llvm::ConstantAggregateZero>(Val)->getElementValue(Idx)));
}
+ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
+ auto *LLVMC =
+ llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty->LLVMTy));
+ return cast<ConstantPointerNull>(Ty->getContext().getOrCreateConstant(LLVMC));
+}
+
+PointerType *ConstantPointerNull::getType() const {
+ return cast<PointerType>(
+ Ctx.getType(cast<llvm::ConstantPointerNull>(Val)->getType()));
+}
+
FunctionType *Function::getFunctionType() const {
return cast<FunctionType>(
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
@@ -2535,6 +2546,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
}
return Ret;
}
+ case llvm::Value::ConstantPointerNullVal:
+ It->second = std::unique_ptr<ConstantPointerNull>(
+ new ConstantPointerNull(cast<llvm::ConstantPointerNull>(C), *this));
+ return It->second.get();
case llvm::Value::ConstantArrayVal:
It->second = std::unique_ptr<ConstantArray>(
new ConstantArray(cast<llvm::ConstantArray>(C), *this));
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index ebb127915ba852..2f5ef92578e775 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -589,6 +589,33 @@ define void @foo(ptr %ptr, {i32, i8} %v1, <2 x i8> %v2) {
EXPECT_EQ(NewVectorCAZ->getElementCount(), ElementCount::getFixed(4));
}
+TEST_F(SandboxIRTest, ConstantPointerNull) {
+ parseIR(C, R"IR(
+define ptr @foo() {
+ ret ptr null
+}
+)IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto &BB = *F.begin();
+ auto It = BB.begin();
+ auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
+ // Check classof() and creation.
+ auto *CPNull = cast<sandboxir::ConstantPointerNull>(Ret->getReturnValue());
+ // Check get().
+ auto *NewCPNull =
+ sandboxir::ConstantPointerNull::get(sandboxir::PointerType::get(Ctx, 0u));
+ EXPECT_EQ(NewCPNull, CPNull);
+ auto *NewCPNull2 =
+ sandboxir::ConstantPointerNull::get(sandboxir::PointerType::get(Ctx, 1u));
+ EXPECT_NE(NewCPNull2, CPNull);
+ // Check getType().
+ EXPECT_EQ(CPNull->getType(), sandboxir::PointerType::get(Ctx, 0u));
+ EXPECT_EQ(NewCPNull2->getType(), sandboxir::PointerType::get(Ctx, 1u));
+}
+
TEST_F(SandboxIRTest, Use) {
parseIR(C, R"IR(
define i32 @foo(i32 %v0, i32 %v1) {
More information about the llvm-commits
mailing list