@@ -420,11 +420,17 @@ findRuleToDelete(llvm::function_ref<bool(unsigned)> isRedundantRuleFn) {
420420 // Homotopy reduction runs multiple passes with different filters to
421421 // prioritize the deletion of certain rules ahead of others. Apply
422422 // the filter now.
423- if (!isRedundantRuleFn (ruleID))
423+ if (!isRedundantRuleFn (ruleID)) {
424+ if (Debug.contains (DebugFlags::HomotopyReductionDetail)) {
425+ llvm::dbgs () << " ** Skipping rule " << rule << " from loop #"
426+ << pair.first << " \n " ;
427+ }
428+
424429 continue ;
430+ }
425431
426432 if (Debug.contains (DebugFlags::HomotopyReductionDetail)) {
427- llvm::dbgs () << " ** Candidate " << rule << " from loop #"
433+ llvm::dbgs () << " ** Candidate rule " << rule << " from loop #"
428434 << pair.first << " \n " ;
429435 }
430436
@@ -552,6 +558,12 @@ findRuleToDelete(llvm::function_ref<bool(unsigned)> isRedundantRuleFn) {
552558// / occurrences of the rule in all loops with the replacement path.
553559void RewriteSystem::deleteRule (unsigned ruleID,
554560 const RewritePath &replacementPath) {
561+ // Replace all occurrences of the rule with the replacement path in
562+ // all redundant rule paths recorded so far.
563+ for (auto &pair : RedundantRules) {
564+ (void ) pair.second .replaceRuleWithPath (ruleID, replacementPath);
565+ }
566+
555567 // Replace all occurrences of the rule with the replacement path in
556568 // all remaining rewrite loops.
557569 for (unsigned loopID : indices (Loops)) {
@@ -573,6 +585,9 @@ void RewriteSystem::deleteRule(unsigned ruleID,
573585 llvm::dbgs () << " \n " ;
574586 }
575587 }
588+
589+ // Record the redundant rule along with its replacement path.
590+ RedundantRules.emplace_back (ruleID, replacementPath);
576591}
577592
578593void RewriteSystem::performHomotopyReduction (
@@ -702,6 +717,26 @@ void RewriteSystem::minimizeRewriteSystem() {
702717 verifyRewriteLoops ();
703718 verifyRedundantConformances (redundantConformances);
704719 verifyMinimizedRules (redundantConformances);
720+
721+ if (Debug.contains (DebugFlags::RedundantRules)) {
722+ llvm::dbgs () << " \n Redundant rules:\n " ;
723+ for (const auto &pair : RedundantRules) {
724+ const auto &rule = getRule (pair.first );
725+ llvm::dbgs () << " - " << rule << " ::== " ;
726+
727+ MutableTerm lhs (rule.getLHS ());
728+ pair.second .dump (llvm::dbgs (), lhs, *this );
729+
730+ llvm::dbgs () << " \n " ;
731+
732+ if (Debug.contains (DebugFlags::RedundantRulesDetail)) {
733+ llvm::dbgs () << " \n " ;
734+ pair.second .dumpLong (llvm::dbgs (), lhs, *this );
735+
736+ llvm::dbgs () << " \n\n " ;
737+ }
738+ }
739+ }
705740}
706741
707742// / In a conformance-valid rewrite system, any rule with unresolved symbols on
@@ -826,6 +861,8 @@ void RewriteSystem::verifyRedundantConformances(
826861void RewriteSystem::verifyMinimizedRules (
827862 const llvm::DenseSet<unsigned > &redundantConformances) const {
828863#ifndef NDEBUG
864+ unsigned redundantRuleCount = 0 ;
865+
829866 for (unsigned ruleID : indices (Rules)) {
830867 const auto &rule = getRule (ruleID);
831868
@@ -845,8 +882,11 @@ void RewriteSystem::verifyMinimizedRules(
845882 continue ;
846883 }
847884
885+ if (rule.isRedundant ())
886+ ++redundantRuleCount;
887+
848888 // LHS-simplified rules should be redundant, unless they're protocol
849- // conformance rules, which unfortunately might no be redundant, because
889+ // conformance rules, which unfortunately might not be redundant, because
850890 // we try to keep them in the original protocol definition for
851891 // compatibility with the GenericSignatureBuilder's minimization algorithm.
852892 if (rule.isLHSSimplified () &&
@@ -877,5 +917,22 @@ void RewriteSystem::verifyMinimizedRules(
877917 abort ();
878918 }
879919 }
920+
921+ if (RedundantRules.size () != redundantRuleCount) {
922+ llvm::errs () << " Expected " << RedundantRules.size () << " redundant rules "
923+ << " but counted " << redundantRuleCount << " \n " ;
924+ dump (llvm::errs ());
925+ abort ();
926+ }
927+
928+ for (const auto &pair : RedundantRules) {
929+ const auto &rule = getRule (pair.first );
930+ if (!rule.isRedundant ()) {
931+ llvm::errs () << " Recorded replacement path for non-redundant rule "
932+ << rule << " \n " ;
933+ dump (llvm::errs ());
934+ abort ();
935+ }
936+ }
880937#endif
881938}
0 commit comments