[llvm] [SandboxIR] Add debug checker to compare IR before/after a revert (PR #115968)
Jorge Gorbe Moya via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 15 17:00:03 PST 2024
================
@@ -67,6 +67,60 @@ class CmpInst;
class Module;
class GlobalVariable;
+#ifndef NDEBUG
+
+/// A class that saves hashes and textual IR snapshots of modules in a
+/// SandboxIR Context, and does hash comparison when `expectNoDiff` is called.
+/// If hashes differ, it prints textual IR for both old and new versions to
+/// aid debugging.
+///
+/// This is used as an additional debug check when reverting changes to
+/// SandboxIR, to verify the reverted state matches the initial state.
+class IRSnapshotChecker {
+ Context &Ctx;
+
+ // A snapshot of textual IR for a module, with a hash for quick comparison.
+ struct ModuleSnapshot {
+ llvm::stable_hash Hash;
+ std::string TextualIR;
+ };
+
+ // A snapshot for each llvm::Module found in the SandboxIR Context. In
+ // practice there will always be one module, but sandbox IR save/restore ops
+ // work at the Context level, so we must take the full state into account.
+ using ContextSnapshot = DenseMap<llvm::Module *, ModuleSnapshot>;
+
+ ContextSnapshot OrigContextSnapshot;
+
+ // True if save() was previously called. This helps us distinguish between
+ // "expectNoDiff was called without calling save" and "save was called but
+ // the saved snapshot is empty".
+ bool HasSavedState = false;
+
+ // Dumps to a string the textual IR for a single Module.
+ std::string dumpIR(llvm::Module *F) const;
+
+ // Returns a snapshot of all the modules in the sandbox IR context.
+ ContextSnapshot takeSnapshot() const;
+
+ // Compares two snapshots and returns true if they differ.
+ bool diff(const ContextSnapshot &Orig, const ContextSnapshot &Curr) const;
+
+public:
+ IRSnapshotChecker(Context &Ctx) : Ctx(Ctx) {}
+
+ /// Saves the current state.
+ void save();
+
+ /// Checks current state against saved state, crashes if different.
+ void expectNoDiff();
+
+ /// Empties saved state.
+ void reset();
----------------
slackito wrote:
No need to call it from `save`, as it will replace the old snapshot with the new one. I've just removed it.
https://github.com/llvm/llvm-project/pull/115968
More information about the llvm-commits
mailing list