Skip to content

[SandboxIR] Implement ConstantPointerNull #107320

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
Sep 4, 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
31 changes: 31 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class BasicBlock;
class ConstantInt;
class ConstantFP;
class ConstantAggregateZero;
class ConstantPointerNull;
class Context;
class Function;
class Instruction;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/SandboxIR/SandboxIRValues.def
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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));
Expand Down
27 changes: 27 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading