Skip to content

[SandboxIR][Tracker] Track creation of instructions #102013

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 5, 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
17 changes: 17 additions & 0 deletions llvm/include/llvm/SandboxIR/Tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,23 @@ class InsertIntoBB final : public IRChangeBase {
#endif // NDEBUG
};

class CreateAndInsertInst final : public IRChangeBase {
Instruction *NewI = nullptr;

public:
CreateAndInsertInst(Instruction *NewI, Tracker &Tracker)
: IRChangeBase(Tracker), NewI(NewI) {}
void revert() final;
void accept() final {}
#ifndef NDEBUG
void dump(raw_ostream &OS) const final {
dumpCommon(OS);
OS << "CreateAndInsertInst";
}
LLVM_DUMP_METHOD void dump() const final;
#endif
};

/// The tracker collects all the change objects and implements the main API for
/// saving / reverting / accepting.
class Tracker {
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/SandboxIR/SandboxIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,16 @@ std::unique_ptr<Value> Context::detach(Value *V) {
Value *Context::registerValue(std::unique_ptr<Value> &&VPtr) {
assert(VPtr->getSubclassID() != Value::ClassID::User &&
"Can't register a user!");

// Track creation of instructions.
// Please note that we don't allow the creation of detached instructions,
// meaning that the instructions need to be inserted into a block upon
// creation. This is why the tracker class combines creation and insertion.
auto &Tracker = getTracker();
if (Tracker.isTracking())
if (auto *I = dyn_cast<Instruction>(VPtr.get()))
Tracker.track(std::make_unique<CreateAndInsertInst>(I, Tracker));

Value *V = VPtr.get();
[[maybe_unused]] auto Pair =
LLVMValueToValueMap.insert({VPtr->Val, std::move(VPtr)});
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/SandboxIR/Tracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ void InsertIntoBB::dump() const {
}
#endif

void CreateAndInsertInst::revert() { NewI->eraseFromParent(); }

#ifndef NDEBUG
void CreateAndInsertInst::dump() const {
dump(dbgs());
dbgs() << "\n";
}
#endif

void Tracker::track(std::unique_ptr<IRChangeBase> &&Change) {
assert(State == TrackerState::Record && "The tracker should be tracking!");
Changes.push_back(std::move(Change));
Expand Down
36 changes: 36 additions & 0 deletions llvm/unittests/SandboxIR/TrackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,42 @@ define void @foo(i32 %arg) {
Add0->insertBefore(Ret);
}

// TODO: Test multi-instruction patterns.
TEST_F(TrackerTest, CreateAndInsertInst) {
parseIR(C, R"IR(
define void @foo(ptr %ptr) {
%ld = load i8, ptr %ptr, align 64
ret void
}
)IR");
Function &LLVMF = *M->getFunction("foo");
sandboxir::Context Ctx(C);

auto *F = Ctx.createFunction(&LLVMF);
auto *Ptr = F->getArg(0);
auto *BB = &*F->begin();
auto It = BB->begin();
auto *Ld = cast<sandboxir::LoadInst>(&*It++);
auto *Ret = &*It++;

Ctx.save();
// Check create(InsertBefore) with tracking enabled.
sandboxir::LoadInst *NewLd =
sandboxir::LoadInst::create(Ld->getType(), Ptr, Align(8),
/*InsertBefore=*/Ld, Ctx, "NewLd");
It = BB->begin();
EXPECT_EQ(&*It++, NewLd);
EXPECT_EQ(&*It++, Ld);
EXPECT_EQ(&*It++, Ret);
EXPECT_EQ(It, BB->end());
// Check revert().
Ctx.revert();
It = BB->begin();
EXPECT_EQ(&*It++, Ld);
EXPECT_EQ(&*It++, Ret);
EXPECT_EQ(It, BB->end());
}

TEST_F(TrackerTest, CallBaseSetters) {
parseIR(C, R"IR(
declare void @bar1(i8)
Expand Down
Loading