Skip to content

[SandboxIR] Implement CatchReturnInst #105605

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
Aug 22, 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
29 changes: 29 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class CallBrInst;
class FuncletPadInst;
class CatchPadInst;
class CleanupPadInst;
class CatchReturnInst;
class GetElementPtrInst;
class CastInst;
class PtrToIntInst;
Expand Down Expand Up @@ -262,6 +263,7 @@ class Value {
friend class FuncletPadInst; // For getting `Val`.
friend class CatchPadInst; // For getting `Val`.
friend class CleanupPadInst; // For getting `Val`.
friend class CatchReturnInst; // For getting `Val`.
friend class GetElementPtrInst; // For getting `Val`.
friend class CatchSwitchInst; // For getting `Val`.
friend class SwitchInst; // For getting `Val`.
Expand Down Expand Up @@ -687,6 +689,7 @@ class Instruction : public sandboxir::User {
friend class CallBrInst; // For getTopmostLLVMInstruction().
friend class CatchPadInst; // For getTopmostLLVMInstruction().
friend class CleanupPadInst; // For getTopmostLLVMInstruction().
friend class CatchReturnInst; // For getTopmostLLVMInstruction().
friend class GetElementPtrInst; // For getTopmostLLVMInstruction().
friend class CatchSwitchInst; // For getTopmostLLVMInstruction().
friend class SwitchInst; // For getTopmostLLVMInstruction().
Expand Down Expand Up @@ -1914,6 +1917,30 @@ class CleanupPadInst : public FuncletPadInst {
}
};

class CatchReturnInst
: public SingleLLVMInstructionImpl<llvm::CatchReturnInst> {
CatchReturnInst(llvm::CatchReturnInst *CRI, Context &Ctx)
: SingleLLVMInstructionImpl(ClassID::CatchRet, Opcode::CatchRet, CRI,
Ctx) {}
friend class Context; // For constructor.

public:
static CatchReturnInst *create(CatchPadInst *CatchPad, BasicBlock *BB,
BBIterator WhereIt, BasicBlock *WhereBB,
Context &Ctx);
CatchPadInst *getCatchPad() const;
void setCatchPad(CatchPadInst *CatchPad);
BasicBlock *getSuccessor() const;
void setSuccessor(BasicBlock *NewSucc);
unsigned getNumSuccessors() {
return cast<llvm::CatchReturnInst>(Val)->getNumSuccessors();
}
Value *getCatchSwitchParentPad() const;
static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::CatchRet;
}
};

class GetElementPtrInst final
: public SingleLLVMInstructionImpl<llvm::GetElementPtrInst> {
/// Use Context::createGetElementPtrInst(). Don't call
Expand Down Expand Up @@ -2820,6 +2847,8 @@ class Context {
friend CatchPadInst; // For createCatchPadInst()
CleanupPadInst *createCleanupPadInst(llvm::CleanupPadInst *I);
friend CleanupPadInst; // For createCleanupPadInst()
CatchReturnInst *createCatchReturnInst(llvm::CatchReturnInst *I);
friend CatchReturnInst; // For createCatchReturnInst()
GetElementPtrInst *createGetElementPtrInst(llvm::GetElementPtrInst *I);
friend GetElementPtrInst; // For createGetElementPtrInst()
CatchSwitchInst *createCatchSwitchInst(llvm::CatchSwitchInst *I);
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 @@ -48,6 +48,7 @@ DEF_INSTR(Invoke, OP(Invoke), InvokeInst)
DEF_INSTR(CallBr, OP(CallBr), CallBrInst)
DEF_INSTR(CatchPad, OP(CatchPad), CatchPadInst)
DEF_INSTR(CleanupPad, OP(CleanupPad), CleanupPadInst)
DEF_INSTR(CatchRet, OP(CatchRet), CatchReturnInst)
DEF_INSTR(GetElementPtr, OP(GetElementPtr), GetElementPtrInst)
DEF_INSTR(CatchSwitch, OP(CatchSwitch), CatchSwitchInst)
DEF_INSTR(Switch, OP(Switch), SwitchInst)
Expand Down
54 changes: 54 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,50 @@ CleanupPadInst *CleanupPadInst::create(Value *ParentPad, ArrayRef<Value *> Args,
return Ctx.createCleanupPadInst(LLVMI);
}

CatchReturnInst *CatchReturnInst::create(CatchPadInst *CatchPad, BasicBlock *BB,
BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx) {
auto &Builder = Ctx.getLLVMIRBuilder();
if (WhereIt != WhereBB->end())
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
else
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
llvm::CatchReturnInst *LLVMI = Builder.CreateCatchRet(
cast<llvm::CatchPadInst>(CatchPad->Val), cast<llvm::BasicBlock>(BB->Val));
return Ctx.createCatchReturnInst(LLVMI);
}

CatchPadInst *CatchReturnInst::getCatchPad() const {
return cast<CatchPadInst>(
Ctx.getValue(cast<llvm::CatchReturnInst>(Val)->getCatchPad()));
}

void CatchReturnInst::setCatchPad(CatchPadInst *CatchPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CatchReturnInst::getCatchPad,
&CatchReturnInst::setCatchPad>>(this);
cast<llvm::CatchReturnInst>(Val)->setCatchPad(
cast<llvm::CatchPadInst>(CatchPad->Val));
}

BasicBlock *CatchReturnInst::getSuccessor() const {
return cast<BasicBlock>(
Ctx.getValue(cast<llvm::CatchReturnInst>(Val)->getSuccessor()));
}

void CatchReturnInst::setSuccessor(BasicBlock *NewSucc) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CatchReturnInst::getSuccessor,
&CatchReturnInst::setSuccessor>>(this);
cast<llvm::CatchReturnInst>(Val)->setSuccessor(
cast<llvm::BasicBlock>(NewSucc->Val));
}

Value *CatchReturnInst::getCatchSwitchParentPad() const {
return Ctx.getValue(
cast<llvm::CatchReturnInst>(Val)->getCatchSwitchParentPad());
}

Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
ArrayRef<Value *> IdxList,
BasicBlock::iterator WhereIt,
Expand Down Expand Up @@ -2138,6 +2182,12 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
std::unique_ptr<CleanupPadInst>(new CleanupPadInst(LLVMCPI, *this));
return It->second.get();
}
case llvm::Instruction::CatchRet: {
auto *LLVMCRI = cast<llvm::CatchReturnInst>(LLVMV);
It->second =
std::unique_ptr<CatchReturnInst>(new CatchReturnInst(LLVMCRI, *this));
return It->second.get();
}
case llvm::Instruction::GetElementPtr: {
auto *LLVMGEP = cast<llvm::GetElementPtrInst>(LLVMV);
It->second = std::unique_ptr<GetElementPtrInst>(
Expand Down Expand Up @@ -2322,6 +2372,10 @@ CleanupPadInst *Context::createCleanupPadInst(llvm::CleanupPadInst *I) {
auto NewPtr = std::unique_ptr<CleanupPadInst>(new CleanupPadInst(I, *this));
return cast<CleanupPadInst>(registerValue(std::move(NewPtr)));
}
CatchReturnInst *Context::createCatchReturnInst(llvm::CatchReturnInst *I) {
auto NewPtr = std::unique_ptr<CatchReturnInst>(new CatchReturnInst(I, *this));
return cast<CatchReturnInst>(registerValue(std::move(NewPtr)));
}
GetElementPtrInst *
Context::createGetElementPtrInst(llvm::GetElementPtrInst *I) {
auto NewPtr =
Expand Down
65 changes: 65 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,71 @@ define void @foo() {
#endif // NDEBUG
}

TEST_F(SandboxIRTest, CatchReturnInst) {
parseIR(C, R"IR(
define void @foo() {
dispatch:
%cs = catchswitch within none [label %catch] unwind to caller
catch:
%catchpad = catchpad within %cs [ptr @foo]
catchret from %catchpad to label %continue
continue:
ret void
catch2:
%catchpad2 = catchpad within %cs [ptr @foo]
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
BasicBlock *LLVMCatch = getBasicBlockByName(LLVMF, "catch");
auto LLVMIt = LLVMCatch->begin();
[[maybe_unused]] auto *LLVMCP = cast<llvm::CatchPadInst>(&*LLVMIt++);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to keep those [[maybe_unused]] locals around? (same for the test in TrackerTest.cpp)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two reasons:

  1. I wouldn't want to diverge from the Instruction casting pattern, regardless of whether the value is used or not:
auto *I0 = cast<...>(&*LLVMIt++);
auto *I1 = cast<...>(&*LLVMIt++);
...
  1. You have a handle for it to dump it in the debugger. This is especially true for the function F, but not so much for the instructions.

auto *LLVMCR = cast<llvm::CatchReturnInst>(&*LLVMIt++);

sandboxir::Context Ctx(C);
[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *Catch = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMCatch));
auto *Catch2 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "catch2")));
auto It = Catch->begin();
[[maybe_unused]] auto *CP = cast<sandboxir::CatchPadInst>(&*It++);
auto *CR = cast<sandboxir::CatchReturnInst>(&*It++);
auto *CP2 = cast<sandboxir::CatchPadInst>(&*Catch2->begin());

// Check getCatchPad().
EXPECT_EQ(CR->getCatchPad(), Ctx.getValue(LLVMCR->getCatchPad()));
// Check setCatchPad().
auto *OrigCP = CR->getCatchPad();
auto *NewCP = CP2;
EXPECT_NE(NewCP, OrigCP);
CR->setCatchPad(NewCP);
EXPECT_EQ(CR->getCatchPad(), NewCP);
CR->setCatchPad(OrigCP);
EXPECT_EQ(CR->getCatchPad(), OrigCP);
// Check getSuccessor().
EXPECT_EQ(CR->getSuccessor(), Ctx.getValue(LLVMCR->getSuccessor()));
// Check setSuccessor().
auto *OrigSucc = CR->getSuccessor();
auto *NewSucc = Catch;
EXPECT_NE(NewSucc, OrigSucc);
CR->setSuccessor(NewSucc);
EXPECT_EQ(CR->getSuccessor(), NewSucc);
CR->setSuccessor(OrigSucc);
EXPECT_EQ(CR->getSuccessor(), OrigSucc);
// Check getNumSuccessors().
EXPECT_EQ(CR->getNumSuccessors(), LLVMCR->getNumSuccessors());
// Check getCatchSwitchParentPad().
EXPECT_EQ(CR->getCatchSwitchParentPad(),
Ctx.getValue(LLVMCR->getCatchSwitchParentPad()));
// Check create().
auto *CRI =
cast<sandboxir::CatchReturnInst>(sandboxir::CatchReturnInst::create(
CP, Catch, CP->getIterator(), Catch, Ctx));
EXPECT_EQ(CRI->getNextNode(), CP);
EXPECT_EQ(CRI->getCatchPad(), CP);
EXPECT_EQ(CRI->getSuccessor(), Catch);
}

TEST_F(SandboxIRTest, GetElementPtrInstruction) {
parseIR(C, R"IR(
define void @foo(ptr %ptr, <2 x ptr> %ptrs) {
Expand Down
50 changes: 50 additions & 0 deletions llvm/unittests/SandboxIR/TrackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,56 @@ define void @foo(i32 %cond0, i32 %cond1) {
EXPECT_EQ(*HIt++, Handler1);
}

TEST_F(TrackerTest, CatchReturnInstSetters) {
parseIR(C, R"IR(
define void @foo() {
dispatch:
%cs = catchswitch within none [label %catch] unwind to caller
catch:
%catchpad = catchpad within %cs [ptr @foo]
catchret from %catchpad to label %continue
continue:
ret void
catch2:
%catchpad2 = catchpad within %cs [ptr @foo]
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
BasicBlock *LLVMCatch = getBasicBlockByName(LLVMF, "catch");
auto LLVMIt = LLVMCatch->begin();
[[maybe_unused]] auto *LLVMCP = cast<llvm::CatchPadInst>(&*LLVMIt++);

sandboxir::Context Ctx(C);
[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *Catch = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMCatch));
auto *Catch2 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "catch2")));
auto It = Catch->begin();
[[maybe_unused]] auto *CP = cast<sandboxir::CatchPadInst>(&*It++);
auto *CR = cast<sandboxir::CatchReturnInst>(&*It++);
auto *CP2 = cast<sandboxir::CatchPadInst>(&*Catch2->begin());

// Check setCatchPad().
auto *OrigCP = CR->getCatchPad();
auto *NewCP = CP2;
EXPECT_NE(NewCP, OrigCP);
Ctx.save();
CR->setCatchPad(NewCP);
EXPECT_EQ(CR->getCatchPad(), NewCP);
Ctx.revert();
EXPECT_EQ(CR->getCatchPad(), OrigCP);
// Check setSuccessor().
auto *OrigSucc = CR->getSuccessor();
auto *NewSucc = Catch;
EXPECT_NE(NewSucc, OrigSucc);
Ctx.save();
CR->setSuccessor(NewSucc);
EXPECT_EQ(CR->getSuccessor(), NewSucc);
Ctx.revert();
EXPECT_EQ(CR->getSuccessor(), OrigSucc);
}

TEST_F(TrackerTest, SwitchInstSetters) {
parseIR(C, R"IR(
define void @foo(i32 %cond0, i32 %cond1) {
Expand Down
Loading