-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the first patch in a series of patches for the IR change tracking component of SandboxIR. The tracker collects changes in a vector of `IRChangeBase` objects and provides a `save()`/`accept()`/`revert()` API. Each type of IR changing event is captured by a dedicated subclass of `IRChangeBase`. This patch implements only one of them, that for updating a `sandboxir::Use` source value, named `UseSet`.
- Loading branch information
Showing
9 changed files
with
470 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,181 @@ | ||
//===- SandboxIRTracker.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 `SandboxIRTracker::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 `SandboxIRTracker::track()` function. The change objects | ||
// are saved in the order they are registered with the tracker and are stored in | ||
// the `SandboxIRTracker::Changes` vector. All of this is done transparently to | ||
// the user. | ||
// | ||
// Reverting changes | ||
// ----------------- | ||
// Calling `SandboxIRTracker::revert()` will restore the state saved when | ||
// `SandboxIRTracker::save()` was called. Internally this goes through the | ||
// change objects in `SandboxIRTracker::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, or else the tracker destructor will cause a crash. | ||
// This is the job of `SandboxIRTracker::accept()`. Internally this will go | ||
// through the change objects in `SandboxIRTracker::Changes` in order, calling | ||
// `IRChangeBase::accept()`. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_SANDBOXIR_SANDBOXIRTRACKER_H | ||
#define LLVM_SANDBOXIR_SANDBOXIRTRACKER_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; | ||
|
||
/// Each IR change type has an ID. | ||
enum class TrackID { | ||
UseSet, | ||
}; | ||
|
||
#ifndef NDEBUG | ||
static const char *trackIDToStr(TrackID ID) { | ||
switch (ID) { | ||
case TrackID::UseSet: | ||
return "UseSet"; | ||
} | ||
llvm_unreachable("Unimplemented ID"); | ||
} | ||
#endif // NDEBUG | ||
|
||
class SandboxIRTracker; | ||
|
||
/// The base class for IR Change classes. | ||
class IRChangeBase { | ||
protected: | ||
#ifndef NDEBUG | ||
unsigned Idx = 0; | ||
#endif | ||
const TrackID ID; | ||
SandboxIRTracker &Parent; | ||
|
||
public: | ||
IRChangeBase(TrackID ID, SandboxIRTracker &Parent); | ||
TrackID getTrackID() const { return ID; } | ||
/// 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 | ||
void dumpCommon(raw_ostream &OS) const { | ||
OS << Idx << ". " << trackIDToStr(ID); | ||
} | ||
virtual void dump(raw_ostream &OS) const = 0; | ||
LLVM_DUMP_METHOD virtual void dump() const = 0; | ||
#endif | ||
}; | ||
|
||
/// Change the source Value of a sandboxir::Use. | ||
class UseSet : public IRChangeBase { | ||
Use U; | ||
Value *OrigV = nullptr; | ||
|
||
public: | ||
UseSet(const Use &U, SandboxIRTracker &Tracker) | ||
: IRChangeBase(TrackID::UseSet, Tracker), U(U), OrigV(U.get()) {} | ||
// For isa<> etc. | ||
static bool classof(const IRChangeBase *Other) { | ||
return Other->getTrackID() == TrackID::UseSet; | ||
} | ||
void revert() final { U.set(OrigV); } | ||
void accept() final {} | ||
#ifndef NDEBUG | ||
void dump(raw_ostream &OS) const final { dumpCommon(OS); } | ||
LLVM_DUMP_METHOD void dump() const final; | ||
friend raw_ostream &operator<<(raw_ostream &OS, const UseSet &C) { | ||
C.dump(OS); | ||
return OS; | ||
} | ||
#endif | ||
}; | ||
|
||
/// The tracker collects all the change objects and implements the main API for | ||
/// saving / reverting / accepting. | ||
class SandboxIRTracker { | ||
public: | ||
enum class TrackerState { | ||
Disabled, ///> Tracking is disabled | ||
Record, ///> Tracking changes | ||
Revert, ///> Undoing changes | ||
Accept, ///> Accepting changes | ||
}; | ||
|
||
private: | ||
/// The list of changes that are being tracked. | ||
SmallVector<std::unique_ptr<IRChangeBase>> Changes; | ||
/// 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 | ||
|
||
SandboxIRTracker() = default; | ||
~SandboxIRTracker(); | ||
/// 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 tracking() 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(); | ||
/// \Returns the number of change entries recorded so far. | ||
unsigned size() const { return Changes.size(); } | ||
/// \Returns true if there are no change entries recorded so far. | ||
bool empty() const { return Changes.empty(); } | ||
|
||
#ifndef NDEBUG | ||
/// \Returns the \p Idx'th change. This is used for testing. | ||
IRChangeBase *getChange(unsigned Idx) const { return Changes[Idx].get(); } | ||
void dump(raw_ostream &OS) const; | ||
LLVM_DUMP_METHOD void dump() const; | ||
friend raw_ostream &operator<<(raw_ostream &OS, const SandboxIRTracker &C) { | ||
C.dump(OS); | ||
return OS; | ||
} | ||
#endif // NDEBUG | ||
}; | ||
|
||
} // namespace llvm::sandboxir | ||
|
||
#endif // LLVM_SANDBOXIR_SANDBOXIRTRACKER_H |
This file contains 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 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 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 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,84 @@ | ||
//===- SandboxIRTracker.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/SandboxIRTracker.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(TrackID ID, SandboxIRTracker &Parent) | ||
: ID(ID), Parent(Parent) { | ||
#ifndef NDEBUG | ||
Idx = Parent.size(); | ||
|
||
assert(!Parent.InMiddleOfCreatingChange && | ||
"We are in the middle of creating another change!"); | ||
if (Parent.tracking()) | ||
Parent.InMiddleOfCreatingChange = true; | ||
#endif // NDEBUG | ||
} | ||
|
||
#ifndef NDEBUG | ||
void UseSet::dump() const { | ||
dump(dbgs()); | ||
dbgs() << "\n"; | ||
} | ||
#endif // NDEBUG | ||
|
||
SandboxIRTracker::~SandboxIRTracker() { | ||
assert(Changes.empty() && "You must accept or revert changes!"); | ||
} | ||
|
||
void SandboxIRTracker::track(std::unique_ptr<IRChangeBase> &&Change) { | ||
#ifndef NDEBUG | ||
assert(State != TrackerState::Revert && | ||
"No changes should be tracked during revert()!"); | ||
#endif // NDEBUG | ||
Changes.push_back(std::move(Change)); | ||
|
||
#ifndef NDEBUG | ||
InMiddleOfCreatingChange = false; | ||
#endif | ||
} | ||
|
||
void SandboxIRTracker::save() { State = TrackerState::Record; } | ||
|
||
void SandboxIRTracker::revert() { | ||
auto SavedState = State; | ||
State = TrackerState::Revert; | ||
for (auto &Change : reverse(Changes)) | ||
Change->revert(); | ||
Changes.clear(); | ||
State = SavedState; | ||
} | ||
|
||
void SandboxIRTracker::accept() { | ||
auto SavedState = State; | ||
State = TrackerState::Accept; | ||
for (auto &Change : Changes) | ||
Change->accept(); | ||
Changes.clear(); | ||
State = SavedState; | ||
} | ||
|
||
#ifndef NDEBUG | ||
void SandboxIRTracker::dump(raw_ostream &OS) const { | ||
for (const auto &ChangePtr : Changes) { | ||
ChangePtr->dump(OS); | ||
OS << "\n"; | ||
} | ||
} | ||
void SandboxIRTracker::dump() const { | ||
dump(dbgs()); | ||
dbgs() << "\n"; | ||
} | ||
#endif // NDEBUG |
This file contains 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 | ||
SandboxIRTrackerTest.cpp | ||
) |
Oops, something went wrong.