Skip to content

Commit 3790bba

Browse files
committed
RequirementMachine: Verify loops immediately when they're added to aid debugging
1 parent 6895fa0 commit 3790bba

File tree

4 files changed

+30
-22
lines changed

4 files changed

+30
-22
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,6 @@ void RewriteSystem::minimizeRewriteSystem() {
475475
assert(!Minimized);
476476
Minimized = 1;
477477

478-
// Check invariants before homotopy reduction.
479-
verifyRewriteLoops();
480-
481478
propagateExplicitBits();
482479

483480
// First pass:
@@ -623,24 +620,7 @@ RewriteSystem::getMinimizedGenericSignatureRules() const {
623620
void RewriteSystem::verifyRewriteLoops() const {
624621
#ifndef NDEBUG
625622
for (const auto &loop : Loops) {
626-
RewritePathEvaluator evaluator(loop.Basepoint);
627-
628-
for (const auto &step : loop.Path) {
629-
evaluator.apply(step, *this);
630-
}
631-
632-
if (evaluator.getCurrentTerm() != loop.Basepoint) {
633-
llvm::errs() << "Not a loop: ";
634-
loop.dump(llvm::errs(), *this);
635-
llvm::errs() << "\n";
636-
abort();
637-
}
638-
639-
if (evaluator.isInContext()) {
640-
llvm::errs() << "Leftover terms on evaluator stack\n";
641-
evaluator.dump(llvm::errs());
642-
abort();
643-
}
623+
loop.verify(*this);
644624
}
645625
#endif
646626
}

lib/AST/RequirementMachine/RewriteLoop.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,29 @@ void RewritePath::dump(llvm::raw_ostream &out,
109109
}
110110
}
111111

112+
void RewriteLoop::verify(const RewriteSystem &system) const {
113+
#ifndef NDEBUG
114+
RewritePathEvaluator evaluator(Basepoint);
115+
116+
for (const auto &step : Path) {
117+
evaluator.apply(step, system);
118+
}
119+
120+
if (evaluator.getCurrentTerm() != Basepoint) {
121+
llvm::errs() << "Not a loop: ";
122+
dump(llvm::errs(), system);
123+
llvm::errs() << "\n";
124+
abort();
125+
}
126+
127+
if (evaluator.isInContext()) {
128+
llvm::errs() << "Leftover terms on evaluator stack\n";
129+
evaluator.dump(llvm::errs());
130+
abort();
131+
}
132+
#endif
133+
}
134+
112135
void RewriteLoop::dump(llvm::raw_ostream &out,
113136
const RewriteSystem &system) const {
114137
out << Basepoint << ": ";

lib/AST/RequirementMachine/RewriteLoop.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ class RewriteLoop {
286286
ProtocolConformanceRules, 2> &result,
287287
const RewriteSystem &system) const;
288288

289+
void verify(const RewriteSystem &system) const;
290+
289291
void dump(llvm::raw_ostream &out, const RewriteSystem &system) const;
290292
};
291293

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,14 +628,17 @@ bool RewriteSystem::isInMinimizationDomain(
628628

629629
void RewriteSystem::recordRewriteLoop(MutableTerm basepoint,
630630
RewritePath path) {
631+
RewriteLoop loop(basepoint, path);
632+
loop.verify(*this);
633+
631634
if (!RecordLoops)
632635
return;
633636

634637
// Ignore the rewrite rule if it is not part of our minimization domain.
635638
if (!isInMinimizationDomain(basepoint.getRootProtocols()))
636639
return;
637640

638-
Loops.emplace_back(basepoint, path);
641+
Loops.push_back(loop);
639642
}
640643

641644
void RewriteSystem::verifyRewriteRules(ValidityPolicy policy) const {

0 commit comments

Comments
 (0)