[llvm] c9ab697 - [SandboxIR] Implement ConstantTokenNone (#108106)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 11 18:55:11 PDT 2024
Author: vporpo
Date: 2024-09-11T18:55:08-07:00
New Revision: c9ab69798ff92f1fcd150a0e1988d08fb8c2a59d
URL: https://github.com/llvm/llvm-project/commit/c9ab69798ff92f1fcd150a0e1988d08fb8c2a59d
DIFF: https://github.com/llvm/llvm-project/commit/c9ab69798ff92f1fcd150a0e1988d08fb8c2a59d.diff
LOG: [SandboxIR] Implement ConstantTokenNone (#108106)
This patch implements sandboxir::ConstantTokenNone mirroring
llvm::ConstantTokenNone.
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 88884683f591a4..95fe239555fb41 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIR.h
+++ b/llvm/include/llvm/SandboxIR/SandboxIR.h
@@ -124,6 +124,7 @@ class ConstantAggregateZero;
class ConstantPointerNull;
class PoisonValue;
class BlockAddress;
+class ConstantTokenNone;
class Context;
class Function;
class Instruction;
@@ -1141,6 +1142,37 @@ class BlockAddress final : public Constant {
}
};
+// TODO: This should inherit from ConstantData.
+class ConstantTokenNone final : public Constant {
+ 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 {
diff --git a/llvm/include/llvm/SandboxIR/SandboxIRValues.def b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
index c29e8be24ea754..bd2f533e880ac6 100644
--- a/llvm/include/llvm/SandboxIR/SandboxIRValues.def
+++ b/llvm/include/llvm/SandboxIR/SandboxIRValues.def
@@ -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)
diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp
index df3839518c9d08..05d05f7ed10fb9 100644
--- a/llvm/lib/SandboxIR/SandboxIR.cpp
+++ b/llvm/lib/SandboxIR/SandboxIR.cpp
@@ -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()));
@@ -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>(
diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
index 148afd9483d568..6280963d588fac 100644
--- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp
+++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp
@@ -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) {
More information about the llvm-commits
mailing list