|
| 1 | +//===- Tracker.h ------------------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This file is the component of SandboxIR that tracks all changes made to its |
| 10 | +// state, such that we can revert the state when needed. |
| 11 | +// |
| 12 | +// Tracking changes |
| 13 | +// ---------------- |
| 14 | +// The user needs to call `Tracker::save()` to enable tracking changes |
| 15 | +// made to SandboxIR. From that point on, any change made to SandboxIR, will |
| 16 | +// automatically create a change tracking object and register it with the |
| 17 | +// tracker. IR-change objects are subclasses of `IRChangeBase` and get |
| 18 | +// registered with the `Tracker::track()` function. The change objects |
| 19 | +// are saved in the order they are registered with the tracker and are stored in |
| 20 | +// the `Tracker::Changes` vector. All of this is done transparently to |
| 21 | +// the user. |
| 22 | +// |
| 23 | +// Reverting changes |
| 24 | +// ----------------- |
| 25 | +// Calling `Tracker::revert()` will restore the state saved when |
| 26 | +// `Tracker::save()` was called. Internally this goes through the |
| 27 | +// change objects in `Tracker::Changes` in reverse order, calling their |
| 28 | +// `IRChangeBase::revert()` function one by one. |
| 29 | +// |
| 30 | +// Accepting changes |
| 31 | +// ----------------- |
| 32 | +// The user needs to either revert or accept changes before the tracker object |
| 33 | +// is destroyed. This is enforced in the tracker's destructor. |
| 34 | +// This is the job of `Tracker::accept()`. Internally this will go |
| 35 | +// through the change objects in `Tracker::Changes` in order, calling |
| 36 | +// `IRChangeBase::accept()`. |
| 37 | +// |
| 38 | +//===----------------------------------------------------------------------===// |
| 39 | + |
| 40 | +#ifndef LLVM_SANDBOXIR_TRACKER_H |
| 41 | +#define LLVM_SANDBOXIR_TRACKER_H |
| 42 | + |
| 43 | +#include "llvm/ADT/SmallVector.h" |
| 44 | +#include "llvm/IR/IRBuilder.h" |
| 45 | +#include "llvm/IR/Instruction.h" |
| 46 | +#include "llvm/IR/Module.h" |
| 47 | +#include "llvm/SandboxIR/Use.h" |
| 48 | +#include "llvm/Support/Debug.h" |
| 49 | +#include <memory> |
| 50 | +#include <regex> |
| 51 | + |
| 52 | +namespace llvm::sandboxir { |
| 53 | + |
| 54 | +class BasicBlock; |
| 55 | +class Tracker; |
| 56 | + |
| 57 | +/// The base class for IR Change classes. |
| 58 | +class IRChangeBase { |
| 59 | +protected: |
| 60 | + Tracker &Parent; |
| 61 | + |
| 62 | +public: |
| 63 | + IRChangeBase(Tracker &Parent); |
| 64 | + /// This runs when changes get reverted. |
| 65 | + virtual void revert() = 0; |
| 66 | + /// This runs when changes get accepted. |
| 67 | + virtual void accept() = 0; |
| 68 | + virtual ~IRChangeBase() = default; |
| 69 | +#ifndef NDEBUG |
| 70 | + /// \Returns the index of this change by iterating over all changes in the |
| 71 | + /// tracker. This is only used for debugging. |
| 72 | + unsigned getIdx() const; |
| 73 | + void dumpCommon(raw_ostream &OS) const { OS << getIdx() << ". "; } |
| 74 | + virtual void dump(raw_ostream &OS) const = 0; |
| 75 | + LLVM_DUMP_METHOD virtual void dump() const = 0; |
| 76 | + friend raw_ostream &operator<<(raw_ostream &OS, const IRChangeBase &C) { |
| 77 | + C.dump(OS); |
| 78 | + return OS; |
| 79 | + } |
| 80 | +#endif |
| 81 | +}; |
| 82 | + |
| 83 | +/// Tracks the change of the source Value of a sandboxir::Use. |
| 84 | +class UseSet : public IRChangeBase { |
| 85 | + Use U; |
| 86 | + Value *OrigV = nullptr; |
| 87 | + |
| 88 | +public: |
| 89 | + UseSet(const Use &U, Tracker &Tracker) |
| 90 | + : IRChangeBase(Tracker), U(U), OrigV(U.get()) {} |
| 91 | + void revert() final { U.set(OrigV); } |
| 92 | + void accept() final {} |
| 93 | +#ifndef NDEBUG |
| 94 | + void dump(raw_ostream &OS) const final { |
| 95 | + dumpCommon(OS); |
| 96 | + OS << "UseSet"; |
| 97 | + } |
| 98 | + LLVM_DUMP_METHOD void dump() const final; |
| 99 | +#endif |
| 100 | +}; |
| 101 | + |
| 102 | +/// The tracker collects all the change objects and implements the main API for |
| 103 | +/// saving / reverting / accepting. |
| 104 | +class Tracker { |
| 105 | +public: |
| 106 | + enum class TrackerState { |
| 107 | + Disabled, ///> Tracking is disabled |
| 108 | + Record, ///> Tracking changes |
| 109 | + }; |
| 110 | + |
| 111 | +private: |
| 112 | + /// The list of changes that are being tracked. |
| 113 | + SmallVector<std::unique_ptr<IRChangeBase>> Changes; |
| 114 | +#ifndef NDEBUG |
| 115 | + friend unsigned IRChangeBase::getIdx() const; // For accessing `Changes`. |
| 116 | +#endif |
| 117 | + /// The current state of the tracker. |
| 118 | + TrackerState State = TrackerState::Disabled; |
| 119 | + |
| 120 | +public: |
| 121 | +#ifndef NDEBUG |
| 122 | + /// Helps catch bugs where we are creating new change objects while in the |
| 123 | + /// middle of creating other change objects. |
| 124 | + bool InMiddleOfCreatingChange = false; |
| 125 | +#endif // NDEBUG |
| 126 | + |
| 127 | + Tracker() = default; |
| 128 | + ~Tracker(); |
| 129 | + /// Record \p Change and take ownership. This is the main function used to |
| 130 | + /// track Sandbox IR changes. |
| 131 | + void track(std::unique_ptr<IRChangeBase> &&Change); |
| 132 | + /// \Returns true if the tracker is recording changes. |
| 133 | + bool isTracking() const { return State == TrackerState::Record; } |
| 134 | + /// \Returns the current state of the tracker. |
| 135 | + TrackerState getState() const { return State; } |
| 136 | + /// Turns on IR tracking. |
| 137 | + void save(); |
| 138 | + /// Stops tracking and accept changes. |
| 139 | + void accept(); |
| 140 | + /// Stops tracking and reverts to saved state. |
| 141 | + void revert(); |
| 142 | + |
| 143 | +#ifndef NDEBUG |
| 144 | + void dump(raw_ostream &OS) const; |
| 145 | + LLVM_DUMP_METHOD void dump() const; |
| 146 | + friend raw_ostream &operator<<(raw_ostream &OS, const Tracker &Tracker) { |
| 147 | + Tracker.dump(OS); |
| 148 | + return OS; |
| 149 | + } |
| 150 | +#endif // NDEBUG |
| 151 | +}; |
| 152 | + |
| 153 | +} // namespace llvm::sandboxir |
| 154 | + |
| 155 | +#endif // LLVM_SANDBOXIR_TRACKER_H |
0 commit comments