|
10 | 10 | #include "llvm/ADT/STLExtras.h" |
11 | 11 | #include "llvm/IR/BasicBlock.h" |
12 | 12 | #include "llvm/IR/Instruction.h" |
| 13 | +#include "llvm/IR/Module.h" |
| 14 | +#include "llvm/IR/StructuralHash.h" |
13 | 15 | #include "llvm/SandboxIR/Instruction.h" |
14 | 16 | #include <sstream> |
15 | 17 |
|
16 | 18 | using namespace llvm::sandboxir; |
17 | 19 |
|
18 | 20 | #ifndef NDEBUG |
| 21 | + |
| 22 | +std::string IRSnapshotChecker::dumpIR(const llvm::Function &F) const { |
| 23 | + std::string Result; |
| 24 | + raw_string_ostream SS(Result); |
| 25 | + F.print(SS, /*AssemblyAnnotationWriter=*/nullptr); |
| 26 | + return Result; |
| 27 | +} |
| 28 | + |
| 29 | +IRSnapshotChecker::ContextSnapshot IRSnapshotChecker::takeSnapshot() const { |
| 30 | + ContextSnapshot Result; |
| 31 | + for (const auto &Entry : Ctx.LLVMModuleToModuleMap) |
| 32 | + for (const auto &F : *Entry.first) { |
| 33 | + FunctionSnapshot Snapshot; |
| 34 | + Snapshot.Hash = StructuralHash(F, /*DetailedHash=*/true); |
| 35 | + Snapshot.TextualIR = dumpIR(F); |
| 36 | + Result[&F] = Snapshot; |
| 37 | + } |
| 38 | + return Result; |
| 39 | +} |
| 40 | + |
| 41 | +bool IRSnapshotChecker::diff(const ContextSnapshot &Orig, |
| 42 | + const ContextSnapshot &Curr) const { |
| 43 | + bool DifferenceFound = false; |
| 44 | + for (const auto &[F, OrigFS] : Orig) { |
| 45 | + auto CurrFSIt = Curr.find(F); |
| 46 | + if (CurrFSIt == Curr.end()) { |
| 47 | + DifferenceFound = true; |
| 48 | + dbgs() << "Function " << F->getName() << " not found in current IR.\n"; |
| 49 | + dbgs() << OrigFS.TextualIR << "\n"; |
| 50 | + continue; |
| 51 | + } |
| 52 | + const FunctionSnapshot &CurrFS = CurrFSIt->second; |
| 53 | + if (OrigFS.Hash != CurrFS.Hash) { |
| 54 | + DifferenceFound = true; |
| 55 | + dbgs() << "Found IR difference in Function " << F->getName() << "\n"; |
| 56 | + dbgs() << "Original:\n" << OrigFS.TextualIR << "\n"; |
| 57 | + dbgs() << "Current:\n" << CurrFS.TextualIR << "\n"; |
| 58 | + } |
| 59 | + } |
| 60 | + // Check that Curr doesn't contain any new functions. |
| 61 | + for (const auto &[F, CurrFS] : Curr) { |
| 62 | + if (!Orig.contains(F)) { |
| 63 | + DifferenceFound = true; |
| 64 | + dbgs() << "Function " << F->getName() |
| 65 | + << " found in current IR but not in original snapshot.\n"; |
| 66 | + dbgs() << CurrFS.TextualIR << "\n"; |
| 67 | + } |
| 68 | + } |
| 69 | + return DifferenceFound; |
| 70 | +} |
| 71 | + |
| 72 | +void IRSnapshotChecker::save() { OrigContextSnapshot = takeSnapshot(); } |
| 73 | + |
| 74 | +void IRSnapshotChecker::expectNoDiff() { |
| 75 | + ContextSnapshot CurrContextSnapshot = takeSnapshot(); |
| 76 | + if (diff(OrigContextSnapshot, CurrContextSnapshot)) { |
| 77 | + llvm_unreachable( |
| 78 | + "Original and current IR differ! Probably a checkpointing bug."); |
| 79 | + } |
| 80 | +} |
| 81 | + |
19 | 82 | void UseSet::dump() const { |
20 | 83 | dump(dbgs()); |
21 | 84 | dbgs() << "\n"; |
@@ -275,14 +338,22 @@ void CmpSwapOperands::dump() const { |
275 | 338 | } |
276 | 339 | #endif |
277 | 340 |
|
278 | | -void Tracker::save() { State = TrackerState::Record; } |
| 341 | +void Tracker::save() { |
| 342 | + State = TrackerState::Record; |
| 343 | +#if !defined(NDEBUG) && defined(EXPENSIVE_CHECKS) |
| 344 | + SnapshotChecker.save(); |
| 345 | +#endif |
| 346 | +} |
279 | 347 |
|
280 | 348 | void Tracker::revert() { |
281 | 349 | assert(State == TrackerState::Record && "Forgot to save()!"); |
282 | 350 | State = TrackerState::Disabled; |
283 | 351 | for (auto &Change : reverse(Changes)) |
284 | 352 | Change->revert(*this); |
285 | 353 | Changes.clear(); |
| 354 | +#if !defined(NDEBUG) && defined(EXPENSIVE_CHECKS) |
| 355 | + SnapshotChecker.expectNoDiff(); |
| 356 | +#endif |
286 | 357 | } |
287 | 358 |
|
288 | 359 | void Tracker::accept() { |
|
0 commit comments