-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SandboxIR] IR Tracker #99238
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
[SandboxIR] IR Tracker #99238
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
//===- Tracker.h ------------------------------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file is the component of SandboxIR that tracks all changes made to its | ||
// state, such that we can revert the state when needed. | ||
// | ||
// Tracking changes | ||
// ---------------- | ||
// The user needs to call `Tracker::save()` to enable tracking changes | ||
// made to SandboxIR. From that point on, any change made to SandboxIR, will | ||
// automatically create a change tracking object and register it with the | ||
// tracker. IR-change objects are subclasses of `IRChangeBase` and get | ||
// registered with the `Tracker::track()` function. The change objects | ||
// are saved in the order they are registered with the tracker and are stored in | ||
// the `Tracker::Changes` vector. All of this is done transparently to | ||
// the user. | ||
// | ||
// Reverting changes | ||
// ----------------- | ||
// Calling `Tracker::revert()` will restore the state saved when | ||
// `Tracker::save()` was called. Internally this goes through the | ||
// change objects in `Tracker::Changes` in reverse order, calling their | ||
// `IRChangeBase::revert()` function one by one. | ||
// | ||
// Accepting changes | ||
// ----------------- | ||
// The user needs to either revert or accept changes before the tracker object | ||
// is destroyed. This is enforced in the tracker's destructor. | ||
// This is the job of `Tracker::accept()`. Internally this will go | ||
// through the change objects in `Tracker::Changes` in order, calling | ||
// `IRChangeBase::accept()`. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_SANDBOXIR_TRACKER_H | ||
#define LLVM_SANDBOXIR_TRACKER_H | ||
|
||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/IR/IRBuilder.h" | ||
#include "llvm/IR/Instruction.h" | ||
#include "llvm/IR/Module.h" | ||
#include "llvm/SandboxIR/Use.h" | ||
#include "llvm/Support/Debug.h" | ||
#include <memory> | ||
#include <regex> | ||
|
||
namespace llvm::sandboxir { | ||
|
||
class BasicBlock; | ||
class Tracker; | ||
|
||
/// The base class for IR Change classes. | ||
class IRChangeBase { | ||
protected: | ||
Tracker &Parent; | ||
|
||
public: | ||
IRChangeBase(Tracker &Parent); | ||
/// This runs when changes get reverted. | ||
virtual void revert() = 0; | ||
/// This runs when changes get accepted. | ||
virtual void accept() = 0; | ||
virtual ~IRChangeBase() = default; | ||
#ifndef NDEBUG | ||
/// \Returns the index of this change by iterating over all changes in the | ||
/// tracker. This is only used for debugging. | ||
unsigned getIdx() const; | ||
void dumpCommon(raw_ostream &OS) const { OS << getIdx() << ". "; } | ||
virtual void dump(raw_ostream &OS) const = 0; | ||
LLVM_DUMP_METHOD virtual void dump() const = 0; | ||
friend raw_ostream &operator<<(raw_ostream &OS, const IRChangeBase &C) { | ||
C.dump(OS); | ||
return OS; | ||
} | ||
#endif | ||
}; | ||
|
||
/// Tracks the change of the source Value of a sandboxir::Use. | ||
class UseSet : public IRChangeBase { | ||
Use U; | ||
Value *OrigV = nullptr; | ||
|
||
public: | ||
UseSet(const Use &U, Tracker &Tracker) | ||
: IRChangeBase(Tracker), U(U), OrigV(U.get()) {} | ||
void revert() final { U.set(OrigV); } | ||
void accept() final {} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final { | ||
dumpCommon(OS); | ||
OS << "UseSet"; | ||
} | ||
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 { | ||
public: | ||
enum class TrackerState { | ||
Disabled, ///> Tracking is disabled | ||
Record, ///> Tracking changes | ||
}; | ||
|
||
private: | ||
/// The list of changes that are being tracked. | ||
SmallVector<std::unique_ptr<IRChangeBase>> Changes; | ||
#ifndef NDEBUG | ||
friend unsigned IRChangeBase::getIdx() const; // For accessing `Changes`. | ||
#endif | ||
/// The current state of the tracker. | ||
TrackerState State = TrackerState::Disabled; | ||
|
||
public: | ||
#ifndef NDEBUG | ||
/// Helps catch bugs where we are creating new change objects while in the | ||
/// middle of creating other change objects. | ||
bool InMiddleOfCreatingChange = false; | ||
#endif // NDEBUG | ||
|
||
Tracker() = default; | ||
~Tracker(); | ||
/// Record \p Change and take ownership. This is the main function used to | ||
/// track Sandbox IR changes. | ||
void track(std::unique_ptr<IRChangeBase> &&Change); | ||
/// \Returns true if the tracker is recording changes. | ||
bool isTracking() const { return State == TrackerState::Record; } | ||
/// \Returns the current state of the tracker. | ||
TrackerState getState() const { return State; } | ||
/// Turns on IR tracking. | ||
void save(); | ||
/// Stops tracking and accept changes. | ||
void accept(); | ||
/// Stops tracking and reverts to saved state. | ||
void revert(); | ||
|
||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const; | ||
LLVM_DUMP_METHOD void dump() const; | ||
friend raw_ostream &operator<<(raw_ostream &OS, const Tracker &Tracker) { | ||
Tracker.dump(OS); | ||
return OS; | ||
} | ||
#endif // NDEBUG | ||
}; | ||
|
||
} // namespace llvm::sandboxir | ||
|
||
#endif // LLVM_SANDBOXIR_TRACKER_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//===- Tracker.cpp --------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/SandboxIR/Tracker.h" | ||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/IR/BasicBlock.h" | ||
#include "llvm/IR/Instruction.h" | ||
#include "llvm/SandboxIR/SandboxIR.h" | ||
#include <sstream> | ||
|
||
using namespace llvm::sandboxir; | ||
|
||
IRChangeBase::IRChangeBase(Tracker &Parent) : Parent(Parent) { | ||
#ifndef NDEBUG | ||
assert(!Parent.InMiddleOfCreatingChange && | ||
"We are in the middle of creating another change!"); | ||
if (Parent.isTracking()) | ||
Parent.InMiddleOfCreatingChange = true; | ||
#endif // NDEBUG | ||
} | ||
|
||
#ifndef NDEBUG | ||
unsigned IRChangeBase::getIdx() const { | ||
auto It = | ||
find_if(Parent.Changes, [this](auto &Ptr) { return Ptr.get() == this; }); | ||
return It - Parent.Changes.begin(); | ||
} | ||
|
||
void UseSet::dump() const { | ||
dump(dbgs()); | ||
dbgs() << "\n"; | ||
} | ||
#endif // NDEBUG | ||
|
||
Tracker::~Tracker() { | ||
assert(Changes.empty() && "You must accept or revert changes!"); | ||
} | ||
|
||
void Tracker::track(std::unique_ptr<IRChangeBase> &&Change) { | ||
assert(State == TrackerState::Record && "The tracker should be tracking!"); | ||
Changes.push_back(std::move(Change)); | ||
|
||
#ifndef NDEBUG | ||
InMiddleOfCreatingChange = false; | ||
#endif | ||
} | ||
|
||
void Tracker::save() { State = TrackerState::Record; } | ||
|
||
void Tracker::revert() { | ||
assert(State == TrackerState::Record && "Forgot to save()!"); | ||
State = TrackerState::Disabled; | ||
for (auto &Change : reverse(Changes)) | ||
Change->revert(); | ||
Changes.clear(); | ||
} | ||
|
||
void Tracker::accept() { | ||
assert(State == TrackerState::Record && "Forgot to save()!"); | ||
State = TrackerState::Disabled; | ||
for (auto &Change : Changes) | ||
Change->accept(); | ||
Changes.clear(); | ||
} | ||
|
||
#ifndef NDEBUG | ||
void Tracker::dump(raw_ostream &OS) const { | ||
for (const auto &ChangePtr : Changes) { | ||
ChangePtr->dump(OS); | ||
OS << "\n"; | ||
} | ||
} | ||
void Tracker::dump() const { | ||
dump(dbgs()); | ||
dbgs() << "\n"; | ||
} | ||
#endif // NDEBUG |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ set(LLVM_LINK_COMPONENTS | |
|
||
add_llvm_unittest(SandboxIRTests | ||
SandboxIRTest.cpp | ||
TrackerTest.cpp | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.