[llvm] [SandboxIR] Add debug checker to compare IR before/after a revert (PR #115968)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 12 18:08:45 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";
----------------
vporpo wrote:

This is good for now, but my concern is that these module dumps may become really huge when compiling large applications, so getting these textual dumps will become problematic. Could we perhaps do a per-function check, or per-BB check instead?

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


More information about the llvm-commits mailing list