[llvm] [SandboxIR] Add callbacks for instruction insert/remove/move ops (PR #112965)
Jorge Gorbe Moya via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 25 17:27:01 PDT 2024
================
@@ -5962,3 +5963,87 @@ TEST_F(SandboxIRTest, CheckClassof) {
EXPECT_NE(&sandboxir::CLASS::classof, &sandboxir::Instruction::classof);
#include "llvm/SandboxIR/Values.def"
}
+
+TEST_F(SandboxIRTest, InstructionCallbacks) {
+ parseIR(C, R"IR(
+ define void @foo(ptr %ptr, i8 %val) {
+ ret void
+ }
+ )IR");
+ Function &LLVMF = *M->getFunction("foo");
+ sandboxir::Context Ctx(C);
+
+ auto &F = *Ctx.createFunction(&LLVMF);
+ auto &BB = *F.begin();
+ sandboxir::Argument *Ptr = F.getArg(0);
+ sandboxir::Argument *Val = F.getArg(1);
+ sandboxir::Instruction *Ret = &BB.front();
+
+ SmallVector<sandboxir::Instruction *> Inserted;
+ auto InsertCbId = Ctx.registerInsertInstrCallback(
+ [&Inserted](sandboxir::Instruction *I) { Inserted.push_back(I); });
+
+ SmallVector<sandboxir::Instruction *> Removed;
+ auto RemoveCbId = Ctx.registerRemoveInstrCallback(
+ [&Removed](sandboxir::Instruction *I) { Removed.push_back(I); });
+
+ // Keep the moved instruction and the instruction pointed by the Where
+ // iterator so we can check both callback arguments work as expected.
+ SmallVector<std::pair<sandboxir::Instruction *, sandboxir::Instruction *>>
+ Moved;
+ auto MoveCbId = Ctx.registerMoveInstrCallback(
+ [&Moved](sandboxir::Instruction *I, const sandboxir::BBIterator &Where) {
+ // Use a nullptr to signal "move to end" to keep it single. We only
+ // have a basic block in this test case anyway.
+ if (Where == Where.getNodeParent()->end())
+ Moved.push_back(std::make_pair(I, nullptr));
+ else
+ Moved.push_back(std::make_pair(I, &*Where));
+ });
+
+ Ctx.save();
+ auto *NewI = sandboxir::StoreInst::create(Val, Ptr, /*Align=*/std::nullopt,
+ Ret->getIterator(), Ctx);
+ EXPECT_THAT(Inserted, testing::ElementsAre(NewI));
+ EXPECT_THAT(Removed, testing::IsEmpty());
+ EXPECT_THAT(Moved, testing::IsEmpty());
+
----------------
slackito wrote:
Discussed this offline because @vporpo's prototype only called them on create/erase, but called them insert/remove. We only need create/erase/move callbacks for the vectorizer, so we're only adding those for now, and I have renamed them to create/erase callbacks. This leaves space to add proper insert/remove callbacks in the future if we (or some future user of Sandbox IR) needs them.
Thus, no need to test those here as they have no associated callbacks for now.
https://github.com/llvm/llvm-project/pull/112965
More information about the llvm-commits
mailing list