Skip to content

[SandboxIR] Implement CleanupReturnInst #105750

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 24, 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
35 changes: 35 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class FuncletPadInst;
class CatchPadInst;
class CleanupPadInst;
class CatchReturnInst;
class CleanupReturnInst;
class GetElementPtrInst;
class CastInst;
class PtrToIntInst;
Expand Down Expand Up @@ -266,6 +267,7 @@ class Value {
friend class CatchReturnInst; // For getting `Val`.
friend class GetElementPtrInst; // For getting `Val`.
friend class CatchSwitchInst; // For getting `Val`.
friend class CleanupReturnInst; // For getting `Val`.
friend class SwitchInst; // For getting `Val`.
friend class UnaryOperator; // For getting `Val`.
friend class BinaryOperator; // For getting `Val`.
Expand Down Expand Up @@ -690,6 +692,7 @@ class Instruction : public sandboxir::User {
friend class CatchPadInst; // For getTopmostLLVMInstruction().
friend class CleanupPadInst; // For getTopmostLLVMInstruction().
friend class CatchReturnInst; // For getTopmostLLVMInstruction().
friend class CleanupReturnInst; // For getTopmostLLVMInstruction().
friend class GetElementPtrInst; // For getTopmostLLVMInstruction().
friend class CatchSwitchInst; // For getTopmostLLVMInstruction().
friend class SwitchInst; // For getTopmostLLVMInstruction().
Expand Down Expand Up @@ -1941,6 +1944,36 @@ class CatchReturnInst
}
};

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

public:
static CleanupReturnInst *create(CleanupPadInst *CleanupPad,
BasicBlock *UnwindBB, BBIterator WhereIt,
BasicBlock *WhereBB, Context &Ctx);
bool hasUnwindDest() const {
return cast<llvm::CleanupReturnInst>(Val)->hasUnwindDest();
}
bool unwindsToCaller() const {
return cast<llvm::CleanupReturnInst>(Val)->unwindsToCaller();
}
CleanupPadInst *getCleanupPad() const;
void setCleanupPad(CleanupPadInst *CleanupPad);
unsigned getNumSuccessors() const {
return cast<llvm::CleanupReturnInst>(Val)->getNumSuccessors();
}
BasicBlock *getUnwindDest() const;
void setUnwindDest(BasicBlock *NewDest);

static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::CleanupRet;
}
};

class GetElementPtrInst final
: public SingleLLVMInstructionImpl<llvm::GetElementPtrInst> {
/// Use Context::createGetElementPtrInst(). Don't call
Expand Down Expand Up @@ -2849,6 +2882,8 @@ class Context {
friend CleanupPadInst; // For createCleanupPadInst()
CatchReturnInst *createCatchReturnInst(llvm::CatchReturnInst *I);
friend CatchReturnInst; // For createCatchReturnInst()
CleanupReturnInst *createCleanupReturnInst(llvm::CleanupReturnInst *I);
friend CleanupReturnInst; // For createCleanupReturnInst()
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 @@ -49,6 +49,7 @@ 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(CleanupRet, OP(CleanupRet), CleanupReturnInst)
DEF_INSTR(GetElementPtr, OP(GetElementPtr), GetElementPtrInst)
DEF_INSTR(CatchSwitch, OP(CatchSwitch), CatchSwitchInst)
DEF_INSTR(Switch, OP(Switch), SwitchInst)
Expand Down
57 changes: 57 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,51 @@ Value *CatchReturnInst::getCatchSwitchParentPad() const {
cast<llvm::CatchReturnInst>(Val)->getCatchSwitchParentPad());
}

CleanupReturnInst *CleanupReturnInst::create(CleanupPadInst *CleanupPad,
BasicBlock *UnwindBB,
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));
auto *LLVMUnwindBB =
UnwindBB != nullptr ? cast<llvm::BasicBlock>(UnwindBB->Val) : nullptr;
llvm::CleanupReturnInst *LLVMI = Builder.CreateCleanupRet(
cast<llvm::CleanupPadInst>(CleanupPad->Val), LLVMUnwindBB);
return Ctx.createCleanupReturnInst(LLVMI);
}

CleanupPadInst *CleanupReturnInst::getCleanupPad() const {
return cast<CleanupPadInst>(
Ctx.getValue(cast<llvm::CleanupReturnInst>(Val)->getCleanupPad()));
}

void CleanupReturnInst::setCleanupPad(CleanupPadInst *CleanupPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CleanupReturnInst::getCleanupPad,
&CleanupReturnInst::setCleanupPad>>(
this);
cast<llvm::CleanupReturnInst>(Val)->setCleanupPad(
cast<llvm::CleanupPadInst>(CleanupPad->Val));
}

BasicBlock *CleanupReturnInst::getUnwindDest() const {
return cast_or_null<BasicBlock>(
Ctx.getValue(cast<llvm::CleanupReturnInst>(Val)->getUnwindDest()));
}

void CleanupReturnInst::setUnwindDest(BasicBlock *NewDest) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&CleanupReturnInst::getUnwindDest,
&CleanupReturnInst::setUnwindDest>>(
this);
cast<llvm::CleanupReturnInst>(Val)->setUnwindDest(
cast<llvm::BasicBlock>(NewDest->Val));
}

Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
ArrayRef<Value *> IdxList,
BasicBlock::iterator WhereIt,
Expand Down Expand Up @@ -2188,6 +2233,12 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
std::unique_ptr<CatchReturnInst>(new CatchReturnInst(LLVMCRI, *this));
return It->second.get();
}
case llvm::Instruction::CleanupRet: {
auto *LLVMCRI = cast<llvm::CleanupReturnInst>(LLVMV);
It->second = std::unique_ptr<CleanupReturnInst>(
new CleanupReturnInst(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 @@ -2376,6 +2427,12 @@ CatchReturnInst *Context::createCatchReturnInst(llvm::CatchReturnInst *I) {
auto NewPtr = std::unique_ptr<CatchReturnInst>(new CatchReturnInst(I, *this));
return cast<CatchReturnInst>(registerValue(std::move(NewPtr)));
}
CleanupReturnInst *
Context::createCleanupReturnInst(llvm::CleanupReturnInst *I) {
auto NewPtr =
std::unique_ptr<CleanupReturnInst>(new CleanupReturnInst(I, *this));
return cast<CleanupReturnInst>(registerValue(std::move(NewPtr)));
}
GetElementPtrInst *
Context::createGetElementPtrInst(llvm::GetElementPtrInst *I) {
auto NewPtr =
Expand Down
71 changes: 71 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,77 @@ define void @foo() {
EXPECT_EQ(CRI->getSuccessor(), Catch);
}

TEST_F(SandboxIRTest, CleanupReturnInst) {
parseIR(C, R"IR(
define void @foo() {
dispatch:
invoke void @foo()
to label %throw unwind label %cleanup
throw:
ret void
cleanup:
%cleanuppad = cleanuppad within none []
cleanupret from %cleanuppad unwind label %cleanup2
cleanup2:
%cleanuppad2 = cleanuppad within none []
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
BasicBlock *LLVMCleanup = getBasicBlockByName(LLVMF, "cleanup");
auto LLVMIt = LLVMCleanup->begin();
[[maybe_unused]] auto *LLVMCP = cast<llvm::CleanupPadInst>(&*LLVMIt++);
auto *LLVMCRI = cast<llvm::CleanupReturnInst>(&*LLVMIt++);

sandboxir::Context Ctx(C);
[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *Throw = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "throw")));
auto *Cleanup = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMCleanup));
auto *Cleanup2 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "cleanup2")));
auto It = Cleanup->begin();
[[maybe_unused]] auto *CP = cast<sandboxir::CleanupPadInst>(&*It++);
auto *CRI = cast<sandboxir::CleanupReturnInst>(&*It++);
It = Cleanup2->begin();
auto *CP2 = cast<sandboxir::CleanupPadInst>(&*It++);
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);

// Check hasUnwindDest().
EXPECT_EQ(CRI->hasUnwindDest(), LLVMCRI->hasUnwindDest());
// Check unwindsToCaller().
EXPECT_EQ(CRI->unwindsToCaller(), LLVMCRI->unwindsToCaller());
// Check getCleanupPad().
EXPECT_EQ(CRI->getCleanupPad(), Ctx.getValue(LLVMCRI->getCleanupPad()));
// Check setCleanupPad().
auto *OrigCleanupPad = CRI->getCleanupPad();
auto *NewCleanupPad = CP2;
EXPECT_NE(NewCleanupPad, OrigCleanupPad);
CRI->setCleanupPad(NewCleanupPad);
EXPECT_EQ(CRI->getCleanupPad(), NewCleanupPad);
CRI->setCleanupPad(OrigCleanupPad);
EXPECT_EQ(CRI->getCleanupPad(), OrigCleanupPad);
// Check setNumSuccessors().
EXPECT_EQ(CRI->getNumSuccessors(), LLVMCRI->getNumSuccessors());
// Check getUnwindDest().
EXPECT_EQ(CRI->getUnwindDest(), Ctx.getValue(LLVMCRI->getUnwindDest()));
// Check setUnwindDest().
auto *OrigUnwindDest = CRI->getUnwindDest();
auto *NewUnwindDest = Throw;
EXPECT_NE(NewUnwindDest, OrigUnwindDest);
CRI->setUnwindDest(NewUnwindDest);
EXPECT_EQ(CRI->getUnwindDest(), NewUnwindDest);
CRI->setUnwindDest(OrigUnwindDest);
EXPECT_EQ(CRI->getUnwindDest(), OrigUnwindDest);
// Check create().
auto *UnwindBB = Cleanup;
auto *NewCRI = sandboxir::CleanupReturnInst::create(
CP2, UnwindBB, Ret->getIterator(), Ret->getParent(), Ctx);
EXPECT_EQ(NewCRI->getCleanupPad(), CP2);
EXPECT_EQ(NewCRI->getUnwindDest(), UnwindBB);
EXPECT_EQ(NewCRI->getNextNode(), Ret);
}

TEST_F(SandboxIRTest, GetElementPtrInstruction) {
parseIR(C, R"IR(
define void @foo(ptr %ptr, <2 x ptr> %ptrs) {
Expand Down
51 changes: 51 additions & 0 deletions llvm/unittests/SandboxIR/TrackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,57 @@ define void @foo() {
EXPECT_EQ(CR->getSuccessor(), OrigSucc);
}

TEST_F(TrackerTest, CleanupReturnInstSetters) {
parseIR(C, R"IR(
define void @foo() {
dispatch:
invoke void @foo()
to label %throw unwind label %cleanup
throw:
ret void
cleanup:
%cleanuppad = cleanuppad within none []
cleanupret from %cleanuppad unwind label %cleanup2
cleanup2:
%cleanuppad2 = cleanuppad within none []
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
BasicBlock *LLVMCleanup = getBasicBlockByName(LLVMF, "cleanup");

sandboxir::Context Ctx(C);
[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *Throw = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "throw")));
auto *Cleanup = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMCleanup));
auto *Cleanup2 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "cleanup2")));
auto It = Cleanup->begin();
[[maybe_unused]] auto *CP = cast<sandboxir::CleanupPadInst>(&*It++);
auto *CRI = cast<sandboxir::CleanupReturnInst>(&*It++);
auto *CP2 = cast<sandboxir::CleanupPadInst>(&*Cleanup2->begin());

// Check setCleanupPad().
auto *OrigCleanupPad = CRI->getCleanupPad();
auto *NewCleanupPad = CP2;
EXPECT_NE(NewCleanupPad, OrigCleanupPad);
Ctx.save();
CRI->setCleanupPad(NewCleanupPad);
EXPECT_EQ(CRI->getCleanupPad(), NewCleanupPad);
Ctx.revert();
EXPECT_EQ(CRI->getCleanupPad(), OrigCleanupPad);
// Check setUnwindDest().
auto *OrigUnwindDest = CRI->getUnwindDest();
auto *NewUnwindDest = Throw;
EXPECT_NE(NewUnwindDest, OrigUnwindDest);
Ctx.save();
CRI->setUnwindDest(NewUnwindDest);
EXPECT_EQ(CRI->getUnwindDest(), NewUnwindDest);
Ctx.revert();
EXPECT_EQ(CRI->getUnwindDest(), OrigUnwindDest);
}

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