[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 16:59:06 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;
----------------
slackito wrote:
I was trying to check we ran `save()` before `expectNoDiff` (that is, an invariant at the IRSnapshotChecker level, not at the Context level). But removing it also works, so I just did that.
https://github.com/llvm/llvm-project/pull/115968
More information about the llvm-commits
mailing list