Skip to content

Commit 6441b8b

Browse files
committed
RequirementMachine: Extract some verification logic out of minimizeRewriteSystem()
1 parent a6cd78e commit 6441b8b

File tree

3 files changed

+68
-52
lines changed

3 files changed

+68
-52
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 63 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ void RewriteSystem::minimizeRewriteSystem() {
778778
loop.normalize(*this);
779779
}
780780

781+
// Check invariants before homotopy reduction.
782+
verifyHomotopyGenerators();
783+
781784
// First pass: Eliminate all redundant rules involving unresolved types.
782785
performHomotopyReduction(/*firstPass=*/true,
783786
/*redundantConformances=*/nullptr);
@@ -798,57 +801,10 @@ void RewriteSystem::minimizeRewriteSystem() {
798801
performHomotopyReduction(/*firstPass=*/false,
799802
/*redundantConformances=*/&redundantConformances);
800803

801-
// Assert if homotopy reduction failed to eliminate a redundant conformance,
802-
// since this suggests a misunderstanding on my part.
803-
for (unsigned ruleID : redundantConformances) {
804-
const auto &rule = getRule(ruleID);
805-
assert(rule.isProtocolConformanceRule() &&
806-
"Redundant conformance is not a conformance rule?");
807-
808-
if (!rule.isRedundant()) {
809-
llvm::errs() << "Homotopy reduction did not eliminate redundant "
810-
<< "conformance?\n";
811-
llvm::errs() << "(#" << ruleID << ") " << rule << "\n\n";
812-
dump(llvm::errs());
813-
abort();
814-
}
815-
}
816-
817-
// Assert if homotopy reduction failed to eliminate a rewrite rule which was
818-
// deleted because either it's left hand side can be reduced by some other
819-
// rule, or because it's right hand side can be reduced further.
820-
for (const auto &rule : Rules) {
821-
// Note that sometimes permanent rules can be simplified, but they can never
822-
// be redundant.
823-
if (rule.isPermanent()) {
824-
if (rule.isRedundant()) {
825-
llvm::errs() << "Permanent rule is redundant: " << rule << "\n\n";
826-
dump(llvm::errs());
827-
abort();
828-
}
829-
830-
continue;
831-
}
832-
833-
if (rule.isRedundant())
834-
continue;
835-
836-
// Simplified rules should be redundant.
837-
if (rule.isSimplified()) {
838-
llvm::errs() << "Simplified rule is not redundant: " << rule << "\n\n";
839-
dump(llvm::errs());
840-
abort();
841-
}
842-
843-
// Rules with unresolved name symbols (other than permanent rules for
844-
// associated type introduction) should be redundant.
845-
if (rule.getLHS().containsUnresolvedSymbols() ||
846-
rule.getRHS().containsUnresolvedSymbols()) {
847-
llvm::errs() << "Unresolved rule is not redundant: " << rule << "\n\n";
848-
dump(llvm::errs());
849-
abort();
850-
}
851-
}
804+
// Check invariants after homotopy reduction.
805+
verifyHomotopyGenerators();
806+
verifyRedundantConformances(redundantConformances);
807+
verifyMinimizedRules();
852808
}
853809

854810
/// Collect all non-permanent, non-redundant rules whose domain is equal to
@@ -897,4 +853,60 @@ void RewriteSystem::verifyHomotopyGenerators() const {
897853
}
898854
}
899855
#endif
856+
}
857+
858+
/// Assert if homotopy reduction failed to eliminate a redundant conformance,
859+
/// since this suggests a misunderstanding on my part.
860+
void RewriteSystem::verifyRedundantConformances(
861+
llvm::DenseSet<unsigned> redundantConformances) const {
862+
for (unsigned ruleID : redundantConformances) {
863+
const auto &rule = getRule(ruleID);
864+
assert(rule.isProtocolConformanceRule() &&
865+
"Redundant conformance is not a conformance rule?");
866+
867+
if (!rule.isRedundant()) {
868+
llvm::errs() << "Homotopy reduction did not eliminate redundant "
869+
<< "conformance?\n";
870+
llvm::errs() << "(#" << ruleID << ") " << rule << "\n\n";
871+
dump(llvm::errs());
872+
abort();
873+
}
874+
}
875+
}
876+
877+
// Assert if homotopy reduction failed to eliminate a rewrite rule it was
878+
// supposed to delete.
879+
void RewriteSystem::verifyMinimizedRules() const {
880+
for (const auto &rule : Rules) {
881+
// Note that sometimes permanent rules can be simplified, but they can never
882+
// be redundant.
883+
if (rule.isPermanent()) {
884+
if (rule.isRedundant()) {
885+
llvm::errs() << "Permanent rule is redundant: " << rule << "\n\n";
886+
dump(llvm::errs());
887+
abort();
888+
}
889+
890+
continue;
891+
}
892+
893+
if (rule.isRedundant())
894+
continue;
895+
896+
// Simplified rules should be redundant.
897+
if (rule.isSimplified()) {
898+
llvm::errs() << "Simplified rule is not redundant: " << rule << "\n\n";
899+
dump(llvm::errs());
900+
abort();
901+
}
902+
903+
// Rules with unresolved name symbols (other than permanent rules for
904+
// associated type introduction) should be redundant.
905+
if (rule.getLHS().containsUnresolvedSymbols() ||
906+
rule.getRHS().containsUnresolvedSymbols()) {
907+
llvm::errs() << "Unresolved rule is not redundant: " << rule << "\n\n";
908+
dump(llvm::errs());
909+
abort();
910+
}
911+
}
900912
}

lib/AST/RequirementMachine/RequirementMachine.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,6 @@ void RequirementMachine::computeCompletion(RewriteSystem::ValidityPolicy policy)
502502

503503
// Check invariants.
504504
System.verifyRewriteRules(policy);
505-
System.verifyHomotopyGenerators();
506505

507506
// Build the property map, which also performs concrete term
508507
// unification; if this added any new rules, run the completion

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,11 @@ class RewriteSystem final {
509509

510510
void verifyHomotopyGenerators() const;
511511

512+
void verifyRedundantConformances(
513+
llvm::DenseSet<unsigned> redundantConformances) const;
514+
515+
void verifyMinimizedRules() const;
516+
512517
//////////////////////////////////////////////////////////////////////////////
513518
///
514519
/// Generating conformances

0 commit comments

Comments
 (0)