[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:58:51 PST 2024


================
@@ -10,12 +10,81 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/IR/BasicBlock.h"
 #include "llvm/IR/Instruction.h"
+#include "llvm/IR/StructuralHash.h"
 #include "llvm/SandboxIR/Instruction.h"
 #include <sstream>
 
 using namespace llvm::sandboxir;
 
 #ifndef NDEBUG
+
+std::string IRSnapshotChecker::dumpIR(llvm::Module *M) const {
+  std::string Result;
+  raw_string_ostream SS(Result);
+  M->print(SS, /*AssemblyAnnotationWriter=*/nullptr);
+  return Result;
+}
+
+IRSnapshotChecker::ContextSnapshot IRSnapshotChecker::takeSnapshot() const {
+  ContextSnapshot Result;
+  for (const auto &Entry : Ctx.LLVMModuleToModuleMap) {
+    llvm::Module *M = Entry.first;
+    ModuleSnapshot MS;
+    MS.Hash = StructuralHash(*M, /*DetailedHash=*/true);
+    MS.TextualIR = dumpIR(M);
+    Result[M] = MS;
+  }
+  return Result;
+}
+
+bool IRSnapshotChecker::diff(const ContextSnapshot &Orig,
+                             const ContextSnapshot &Curr) const {
+  bool DifferenceFound = false;
+  for (const auto &[M, OrigMS] : Orig) {
+    auto CurrMSIt = Curr.find(M);
+    if (CurrMSIt == Curr.end()) {
+      DifferenceFound = true;
+      dbgs() << "Module " << M->getName() << " not found in current IR.\n";
+      continue;
+    }
+    const ModuleSnapshot &CurrMS = CurrMSIt->second;
+    if (OrigMS.Hash != CurrMS.Hash) {
+      DifferenceFound = true;
+      dbgs() << "Found IR difference in module " << M->getName() << ".\n";
+      dbgs() << "Original:\n" << OrigMS.TextualIR << "\n";
----------------
slackito wrote:

Switched to per-function snapshots. I looked into using BBs instead but StructuralHash only takes Module or Function.

https://github.com/llvm/llvm-project/pull/115968


More information about the llvm-commits mailing list