Skip to content

[SandboxIR] Implement FuncletPadInst, CatchPadInst and CleanupInst #105294

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
75 changes: 75 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIR.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class CallBase;
class CallInst;
class InvokeInst;
class CallBrInst;
class FuncletPadInst;
class CatchPadInst;
class CleanupPadInst;
class GetElementPtrInst;
class CastInst;
class PtrToIntInst;
Expand Down Expand Up @@ -256,6 +259,9 @@ class Value {
friend class CallInst; // For getting `Val`.
friend class InvokeInst; // For getting `Val`.
friend class CallBrInst; // For getting `Val`.
friend class FuncletPadInst; // For getting `Val`.
friend class CatchPadInst; // For getting `Val`.
friend class CleanupPadInst; // 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 @@ -679,6 +685,8 @@ class Instruction : public sandboxir::User {
friend class CallInst; // For getTopmostLLVMInstruction().
friend class InvokeInst; // For getTopmostLLVMInstruction().
friend class CallBrInst; // For getTopmostLLVMInstruction().
friend class CatchPadInst; // For getTopmostLLVMInstruction().
friend class CleanupPadInst; // For getTopmostLLVMInstruction().
friend class GetElementPtrInst; // For getTopmostLLVMInstruction().
friend class CatchSwitchInst; // For getTopmostLLVMInstruction().
friend class SwitchInst; // For getTopmostLLVMInstruction().
Expand Down Expand Up @@ -845,6 +853,7 @@ template <typename LLVMT> class SingleLLVMInstructionImpl : public Instruction {
#include "llvm/SandboxIR/SandboxIRValues.def"
friend class UnaryInstruction;
friend class CallBase;
friend class FuncletPadInst;

Use getOperandUseInternal(unsigned OpIdx, bool Verify) const final {
return getOperandUseDefault(OpIdx, Verify);
Expand Down Expand Up @@ -1843,6 +1852,68 @@ class CallBrInst final : public CallBase {
}
};

class FuncletPadInst : public SingleLLVMInstructionImpl<llvm::FuncletPadInst> {
FuncletPadInst(ClassID SubclassID, Opcode Opc, llvm::Instruction *I,
Context &Ctx)
: SingleLLVMInstructionImpl(SubclassID, Opc, I, Ctx) {}
friend class CatchPadInst; // For constructor.
friend class CleanupPadInst; // For constructor.

public:
/// Return the number of funcletpad arguments.
unsigned arg_size() const {
return cast<llvm::FuncletPadInst>(Val)->arg_size();
}
/// Return the outer EH-pad this funclet is nested within.
///
/// Note: This returns the associated CatchSwitchInst if this FuncletPadInst
/// is a CatchPadInst.
Value *getParentPad() const;
void setParentPad(Value *ParentPad);
/// Return the Idx-th funcletpad argument.
Value *getArgOperand(unsigned Idx) const;
/// Set the Idx-th funcletpad argument.
void setArgOperand(unsigned Idx, Value *V);

// TODO: Implement missing functions: arg_operands().
static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::CatchPad ||
From->getSubclassID() == ClassID::CleanupPad;
}
};

class CatchPadInst : public FuncletPadInst {
CatchPadInst(llvm::CatchPadInst *CPI, Context &Ctx)
: FuncletPadInst(ClassID::CatchPad, Opcode::CatchPad, CPI, Ctx) {}
friend class Context; // For constructor.

public:
CatchSwitchInst *getCatchSwitch() const;
// TODO: We have not implemented setCatchSwitch() because we can't revert it
// for now, as there is no CatchPadInst member function that can undo it.

static CatchPadInst *create(Value *ParentPad, ArrayRef<Value *> Args,
BBIterator WhereIt, BasicBlock *WhereBB,
Context &Ctx, const Twine &Name = "");
static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::CatchPad;
}
};

class CleanupPadInst : public FuncletPadInst {
CleanupPadInst(llvm::CleanupPadInst *CPI, Context &Ctx)
: FuncletPadInst(ClassID::CleanupPad, Opcode::CleanupPad, CPI, Ctx) {}
friend class Context; // For constructor.

public:
static CleanupPadInst *create(Value *ParentPad, ArrayRef<Value *> Args,
BBIterator WhereIt, BasicBlock *WhereBB,
Context &Ctx, const Twine &Name = "");
static bool classof(const Value *From) {
return From->getSubclassID() == ClassID::CleanupPad;
}
};

class GetElementPtrInst final
: public SingleLLVMInstructionImpl<llvm::GetElementPtrInst> {
/// Use Context::createGetElementPtrInst(). Don't call
Expand Down Expand Up @@ -2745,6 +2816,10 @@ class Context {
friend InvokeInst; // For createInvokeInst()
CallBrInst *createCallBrInst(llvm::CallBrInst *I);
friend CallBrInst; // For createCallBrInst()
CatchPadInst *createCatchPadInst(llvm::CatchPadInst *I);
friend CatchPadInst; // For createCatchPadInst()
CleanupPadInst *createCleanupPadInst(llvm::CleanupPadInst *I);
friend CleanupPadInst; // For createCleanupPadInst()
GetElementPtrInst *createGetElementPtrInst(llvm::GetElementPtrInst *I);
friend GetElementPtrInst; // For createGetElementPtrInst()
CatchSwitchInst *createCatchSwitchInst(llvm::CatchSwitchInst *I);
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/SandboxIR/SandboxIRValues.def
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ DEF_INSTR(Ret, OP(Ret), ReturnInst)
DEF_INSTR(Call, OP(Call), CallInst)
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(GetElementPtr, OP(GetElementPtr), GetElementPtrInst)
DEF_INSTR(CatchSwitch, OP(CatchSwitch), CatchSwitchInst)
DEF_INSTR(Switch, OP(Switch), SwitchInst)
Expand Down
83 changes: 82 additions & 1 deletion llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,68 @@ BasicBlock *CallBrInst::getSuccessor(unsigned Idx) const {
Ctx.getValue(cast<llvm::CallBrInst>(Val)->getSuccessor(Idx)));
}

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

void FuncletPadInst::setParentPad(Value *ParentPad) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetter<&FuncletPadInst::getParentPad,
&FuncletPadInst::setParentPad>>(this);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add tests for save/revert of these setters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops good catch, there are no tests for any of these setters.

cast<llvm::FuncletPadInst>(Val)->setParentPad(ParentPad->Val);
}

Value *FuncletPadInst::getArgOperand(unsigned Idx) const {
return Ctx.getValue(cast<llvm::FuncletPadInst>(Val)->getArgOperand(Idx));
}

void FuncletPadInst::setArgOperand(unsigned Idx, Value *V) {
Ctx.getTracker()
.emplaceIfTracking<GenericSetterWithIdx<&FuncletPadInst::getArgOperand,
&FuncletPadInst::setArgOperand>>(
this, Idx);
cast<llvm::FuncletPadInst>(Val)->setArgOperand(Idx, V->Val);
}

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

CatchPadInst *CatchPadInst::create(Value *ParentPad, ArrayRef<Value *> Args,
BBIterator WhereIt, BasicBlock *WhereBB,
Context &Ctx, const Twine &Name) {
auto &Builder = Ctx.getLLVMIRBuilder();
if (WhereIt != WhereBB->end())
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
else
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (auto *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::CatchPadInst *LLVMI =
Builder.CreateCatchPad(ParentPad->Val, LLVMArgs, Name);
return Ctx.createCatchPadInst(LLVMI);
}

CleanupPadInst *CleanupPadInst::create(Value *ParentPad, ArrayRef<Value *> Args,
BBIterator WhereIt, BasicBlock *WhereBB,
Context &Ctx, const Twine &Name) {
auto &Builder = Ctx.getLLVMIRBuilder();
if (WhereIt != WhereBB->end())
Builder.SetInsertPoint((*WhereIt).getTopmostLLVMInstruction());
else
Builder.SetInsertPoint(cast<llvm::BasicBlock>(WhereBB->Val));
SmallVector<llvm::Value *> LLVMArgs;
LLVMArgs.reserve(Args.size());
for (auto *Arg : Args)
LLVMArgs.push_back(Arg->Val);
llvm::CleanupPadInst *LLVMI =
Builder.CreateCleanupPad(ParentPad->Val, LLVMArgs, Name);
return Ctx.createCleanupPadInst(LLVMI);
}

Value *GetElementPtrInst::create(Type *Ty, Value *Ptr,
ArrayRef<Value *> IdxList,
BasicBlock::iterator WhereIt,
Expand Down Expand Up @@ -2064,6 +2126,18 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
It->second = std::unique_ptr<CallBrInst>(new CallBrInst(LLVMCallBr, *this));
return It->second.get();
}
case llvm::Instruction::CatchPad: {
auto *LLVMCPI = cast<llvm::CatchPadInst>(LLVMV);
It->second =
std::unique_ptr<CatchPadInst>(new CatchPadInst(LLVMCPI, *this));
return It->second.get();
}
case llvm::Instruction::CleanupPad: {
auto *LLVMCPI = cast<llvm::CleanupPadInst>(LLVMV);
It->second =
std::unique_ptr<CleanupPadInst>(new CleanupPadInst(LLVMCPI, *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 @@ -2240,7 +2314,14 @@ UnreachableInst *Context::createUnreachableInst(llvm::UnreachableInst *UI) {
std::unique_ptr<UnreachableInst>(new UnreachableInst(UI, *this));
return cast<UnreachableInst>(registerValue(std::move(NewPtr)));
}

CatchPadInst *Context::createCatchPadInst(llvm::CatchPadInst *I) {
auto NewPtr = std::unique_ptr<CatchPadInst>(new CatchPadInst(I, *this));
return cast<CatchPadInst>(registerValue(std::move(NewPtr)));
}
CleanupPadInst *Context::createCleanupPadInst(llvm::CleanupPadInst *I) {
auto NewPtr = std::unique_ptr<CleanupPadInst>(new CleanupPadInst(I, *this));
return cast<CleanupPadInst>(registerValue(std::move(NewPtr)));
}
GetElementPtrInst *
Context::createGetElementPtrInst(llvm::GetElementPtrInst *I) {
auto NewPtr =
Expand Down
90 changes: 90 additions & 0 deletions llvm/unittests/SandboxIR/SandboxIRTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,96 @@ define void @foo(i8 %arg) {
}
}

TEST_F(SandboxIRTest, FuncletPadInst_CatchPadInst_CleanupPadInst) {
parseIR(C, R"IR(
define void @foo() {
dispatch:
%cs = catchswitch within none [label %handler0] unwind to caller
handler0:
%catchpad = catchpad within %cs [ptr @foo]
ret void
handler1:
%cleanuppad = cleanuppad within %cs [ptr @foo]
ret void
bb:
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
BasicBlock *LLVMDispatch = getBasicBlockByName(LLVMF, "dispatch");
BasicBlock *LLVMHandler0 = getBasicBlockByName(LLVMF, "handler0");
BasicBlock *LLVMHandler1 = getBasicBlockByName(LLVMF, "handler1");
auto *LLVMCP = cast<llvm::CatchPadInst>(&*LLVMHandler0->begin());
auto *LLVMCLP = cast<llvm::CleanupPadInst>(&*LLVMHandler1->begin());

sandboxir::Context Ctx(C);
[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *Dispatch = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMDispatch));
auto *Handler0 = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMHandler0));
auto *Handler1 = cast<sandboxir::BasicBlock>(Ctx.getValue(LLVMHandler1));
auto *BB = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "bb")));
auto *BBRet = cast<sandboxir::ReturnInst>(&*BB->begin());
auto *CS = cast<sandboxir::CatchSwitchInst>(&*Dispatch->begin());
[[maybe_unused]] auto *CP =
cast<sandboxir::CatchPadInst>(&*Handler0->begin());
[[maybe_unused]] auto *CLP =
cast<sandboxir::CleanupPadInst>(&*Handler1->begin());

// Check getCatchSwitch().
EXPECT_EQ(CP->getCatchSwitch(), CS);
EXPECT_EQ(CP->getCatchSwitch(), Ctx.getValue(LLVMCP->getCatchSwitch()));

for (llvm::FuncletPadInst *LLVMFPI :
{static_cast<llvm::FuncletPadInst *>(LLVMCP),
static_cast<llvm::FuncletPadInst *>(LLVMCLP)}) {
auto *FPI = cast<sandboxir::FuncletPadInst>(Ctx.getValue(LLVMFPI));
// Check arg_size().
EXPECT_EQ(FPI->arg_size(), LLVMFPI->arg_size());
// Check getParentPad().
EXPECT_EQ(FPI->getParentPad(), Ctx.getValue(LLVMFPI->getParentPad()));
// Check setParentPad().
auto *OrigParentPad = FPI->getParentPad();
auto *NewParentPad = Dispatch;
EXPECT_NE(NewParentPad, OrigParentPad);
FPI->setParentPad(NewParentPad);
EXPECT_EQ(FPI->getParentPad(), NewParentPad);
FPI->setParentPad(OrigParentPad);
EXPECT_EQ(FPI->getParentPad(), OrigParentPad);
// Check getArgOperand().
for (auto Idx : seq<unsigned>(0, FPI->arg_size()))
EXPECT_EQ(FPI->getArgOperand(Idx),
Ctx.getValue(LLVMFPI->getArgOperand(Idx)));
// Check setArgOperand().
auto *OrigArgOperand = FPI->getArgOperand(0);
auto *NewArgOperand = Dispatch;
EXPECT_NE(NewArgOperand, OrigArgOperand);
FPI->setArgOperand(0, NewArgOperand);
EXPECT_EQ(FPI->getArgOperand(0), NewArgOperand);
FPI->setArgOperand(0, OrigArgOperand);
EXPECT_EQ(FPI->getArgOperand(0), OrigArgOperand);
}
// Check CatchPadInst::create().
auto *NewCPI = cast<sandboxir::CatchPadInst>(sandboxir::CatchPadInst::create(
CS, {}, BBRet->getIterator(), BB, Ctx, "NewCPI"));
EXPECT_EQ(NewCPI->getCatchSwitch(), CS);
EXPECT_EQ(NewCPI->arg_size(), 0u);
EXPECT_EQ(NewCPI->getNextNode(), BBRet);
#ifndef NDEBUG
EXPECT_EQ(NewCPI->getName(), "NewCPI");
#endif // NDEBUG
// Check CleanupPadInst::create().
auto *NewCLPI =
cast<sandboxir::CleanupPadInst>(sandboxir::CleanupPadInst::create(
CS, {}, BBRet->getIterator(), BB, Ctx, "NewCLPI"));
EXPECT_EQ(NewCLPI->getParentPad(), CS);
EXPECT_EQ(NewCLPI->arg_size(), 0u);
EXPECT_EQ(NewCLPI->getNextNode(), BBRet);
#ifndef NDEBUG
EXPECT_EQ(NewCLPI->getName(), "NewCLPI");
#endif // NDEBUG
}

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 @@ -1033,6 +1033,57 @@ define void @foo(i8 %arg) {
EXPECT_EQ(CallBr->getIndirectDest(0), OrigIndirectDest);
}

TEST_F(TrackerTest, FuncletPadInstSetters) {
parseIR(C, R"IR(
define void @foo() {
dispatch:
%cs = catchswitch within none [label %handler0] unwind to caller
handler0:
%catchpad = catchpad within %cs [ptr @foo]
ret void
handler1:
%cleanuppad = cleanuppad within %cs [ptr @foo]
ret void
bb:
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);
[[maybe_unused]] auto &F = *Ctx.createFunction(&LLVMF);
auto *Dispatch = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "dispatch")));
auto *Handler0 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "handler0")));
auto *Handler1 = cast<sandboxir::BasicBlock>(
Ctx.getValue(getBasicBlockByName(LLVMF, "handler1")));
auto *CP = cast<sandboxir::CatchPadInst>(&*Handler0->begin());
auto *CLP = cast<sandboxir::CleanupPadInst>(&*Handler1->begin());

for (auto *FPI : {static_cast<sandboxir::FuncletPadInst *>(CP),
static_cast<sandboxir::FuncletPadInst *>(CLP)}) {
// Check setParentPad().
auto *OrigParentPad = FPI->getParentPad();
auto *NewParentPad = Dispatch;
EXPECT_NE(NewParentPad, OrigParentPad);
Ctx.save();
FPI->setParentPad(NewParentPad);
EXPECT_EQ(FPI->getParentPad(), NewParentPad);
Ctx.revert();
EXPECT_EQ(FPI->getParentPad(), OrigParentPad);

// Check setArgOperand().
auto *OrigArgOperand = FPI->getArgOperand(0);
auto *NewArgOperand = Dispatch;
EXPECT_NE(NewArgOperand, OrigArgOperand);
Ctx.save();
FPI->setArgOperand(0, NewArgOperand);
EXPECT_EQ(FPI->getArgOperand(0), NewArgOperand);
Ctx.revert();
EXPECT_EQ(FPI->getArgOperand(0), OrigArgOperand);
}
}

TEST_F(TrackerTest, PHINodeSetters) {
parseIR(C, R"IR(
define void @foo(i8 %arg0, i8 %arg1, i8 %arg2) {
Expand Down
Loading