Skip to content
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

[SandboxIR] Implement ConstantTokenNone #108106

Merged
merged 1 commit into from
Sep 12, 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
32 changes: 32 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class ConstantAggregateZero;
class ConstantPointerNull;
class PoisonValue;
class BlockAddress;
class ConstantTokenNone;
class Context;
class Function;
class Instruction;
Expand Down Expand Up @@ -1141,6 +1142,37 @@ class BlockAddress final : public Constant {
}
};

// TODO: This should inherit from ConstantData.
class ConstantTokenNone final : public Constant {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Inherit from ConstantData when that becomes available

ConstantTokenNone(llvm::ConstantTokenNone *C, Context &Ctx)
: Constant(ClassID::ConstantTokenNone, C, Ctx) {}
friend class Context; // For constructor.

public:
/// Return the ConstantTokenNone.
static ConstantTokenNone *get(Context &Ctx);

/// For isa/dyn_cast.
static bool classof(const sandboxir::Value *From) {
return From->getSubclassID() == ClassID::ConstantTokenNone;
}

unsigned getUseOperandNo(const Use &Use) const final {
llvm_unreachable("ConstantTokenNone has no operands!");
}

#ifndef NDEBUG
void verify() const override {
assert(isa<llvm::ConstantTokenNone>(Val) &&
"Expected a ConstantTokenNone!");
}
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 @@ -35,6 +35,7 @@ DEF_CONST(ConstantPointerNull, ConstantPointerNull)
DEF_CONST(UndefValue, UndefValue)
DEF_CONST(PoisonValue, PoisonValue)
DEF_CONST(BlockAddress, BlockAddress)
DEF_CONST(ConstantTokenNone, ConstantTokenNone)

#ifndef DEF_INSTR
#define DEF_INSTR(ID, OPCODE, CLASS)
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,11 @@ BasicBlock *BlockAddress::getBasicBlock() const {
Ctx.getValue(cast<llvm::BlockAddress>(Val)->getBasicBlock()));
}

ConstantTokenNone *ConstantTokenNone::get(Context &Ctx) {
auto *LLVMC = llvm::ConstantTokenNone::get(Ctx.LLVMCtx);
return cast<ConstantTokenNone>(Ctx.getOrCreateConstant(LLVMC));
}

FunctionType *Function::getFunctionType() const {
return cast<FunctionType>(
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
Expand Down Expand Up @@ -2621,6 +2626,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
It->second = std::unique_ptr<BlockAddress>(
new BlockAddress(cast<llvm::BlockAddress>(C), *this));
return It->second.get();
case llvm::Value::ConstantTokenNoneVal:
It->second = std::unique_ptr<ConstantTokenNone>(
new ConstantTokenNone(cast<llvm::ConstantTokenNone>(C), *this));
return It->second.get();
case llvm::Value::ConstantAggregateZeroVal: {
auto *CAZ = cast<llvm::ConstantAggregateZero>(C);
It->second = std::unique_ptr<ConstantAggregateZero>(
Expand Down
24 changes: 24 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,30 @@ define void @foo(ptr %ptr) {
EXPECT_EQ(LookupBB2Addr, nullptr);
}

TEST_F(SandboxIRTest, ConstantTokenNone) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
bb0:
%cs = catchswitch within none [label %handler] unwind to caller
handler:
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);

[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *BB0 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "bb0")));
auto *CS = cast<sandboxir::CatchSwitchInst>(&*BB0->begin());

// Check classof(), creation, getFunction(), getBasicBlock().
auto *CTN = cast<sandboxir::ConstantTokenNone>(CS->getParentPad());
// Check get().
auto *NewCTN = sandboxir::ConstantTokenNone::get(Ctx);
EXPECT_EQ(NewCTN, CTN);
}

TEST_F(SandboxIRTest, Use) {
parseIR(C, R"IR(
define i32 @foo(i32 %v0, i32 %v1) {
Expand Down
Loading