Skip to content

Commit 06a25fa

Browse files
committed
RequirementMachine: Don't normalize replacement paths for redundant rules
1 parent d8215bb commit 06a25fa

File tree

2 files changed

+35
-62
lines changed

2 files changed

+35
-62
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -459,60 +459,6 @@ void RewriteSystem::performHomotopyReduction(
459459
propagateRedundantRequirementIDs();
460460
}
461461

462-
void RewriteSystem::normalizeRedundantRules() {
463-
llvm::DenseMap<unsigned, unsigned> RedundantRuleMap;
464-
465-
// A redundant path in the range [0, i-1] might contain rewrite steps naming
466-
// rules that subsequently became redundant in the range [i, e-1].
467-
//
468-
// We back-substitute later rules into earlier paths here.
469-
for (unsigned i = 0, e = RedundantRules.size(); i < e; ++i) {
470-
// Pre-condition: Redundant paths in the range [i+1, e-1] do not involve
471-
// any other redundant rules.
472-
unsigned j = e - i - 1;
473-
474-
// Replace all occurrences of redundant rules with their path at
475-
// RedundantRules[i].
476-
auto &pair = RedundantRules[j];
477-
pair.second.replaceRulesWithPaths(
478-
[&](unsigned ruleID) -> RewritePath * {
479-
auto found = RedundantRuleMap.find(ruleID);
480-
if (found != RedundantRuleMap.end())
481-
return &RedundantRules[found->second].second;
482-
483-
return nullptr;
484-
});
485-
pair.second.computeNormalForm(*this);
486-
487-
RedundantRuleMap[RedundantRules[j].first] = j;
488-
489-
// Post-condition: the path for RedundantRules[i] does not contain any
490-
// redundant rules.
491-
}
492-
493-
if (Debug.contains(DebugFlags::RedundantRules)) {
494-
llvm::dbgs() << "\nRedundant rules:\n";
495-
for (const auto &pair : RedundantRules) {
496-
const auto &rule = getRule(pair.first);
497-
llvm::dbgs() << "- ("
498-
<< rule.getLHS() << " => "
499-
<< rule.getRHS() << ") ::== ";
500-
501-
MutableTerm lhs(rule.getLHS());
502-
pair.second.dump(llvm::dbgs(), lhs, *this);
503-
504-
llvm::dbgs() << "\n";
505-
506-
if (Debug.contains(DebugFlags::RedundantRulesDetail)) {
507-
llvm::dbgs() << "\n";
508-
pair.second.dumpLong(llvm::dbgs(), lhs, *this);
509-
510-
llvm::dbgs() << "\n\n";
511-
}
512-
}
513-
}
514-
}
515-
516462
/// Use the loops to delete redundant rewrite rules via a series of Tietze
517463
/// transformations, updating and simplifying existing loops as each rule
518464
/// is deleted.
@@ -644,12 +590,32 @@ void RewriteSystem::minimizeRewriteSystem() {
644590
return false;
645591
});
646592

647-
normalizeRedundantRules();
648-
649593
// Check invariants after homotopy reduction.
650594
verifyRewriteLoops();
651595
verifyRedundantConformances(redundantConformances);
652596
verifyMinimizedRules(redundantConformances);
597+
598+
if (Debug.contains(DebugFlags::RedundantRules)) {
599+
llvm::dbgs() << "\nRedundant rules:\n";
600+
for (const auto &pair : RedundantRules) {
601+
const auto &rule = getRule(pair.first);
602+
llvm::dbgs() << "- ("
603+
<< rule.getLHS() << " => "
604+
<< rule.getRHS() << ") ::== ";
605+
606+
MutableTerm lhs(rule.getLHS());
607+
pair.second.dump(llvm::dbgs(), lhs, *this);
608+
609+
llvm::dbgs() << "\n";
610+
611+
if (Debug.contains(DebugFlags::RedundantRulesDetail)) {
612+
llvm::dbgs() << "\n";
613+
pair.second.dumpLong(llvm::dbgs(), lhs, *this);
614+
615+
llvm::dbgs() << "\n\n";
616+
}
617+
}
618+
}
653619
}
654620

655621
/// Returns flags indicating if the rewrite system has unresolved or
@@ -849,7 +815,11 @@ void RewriteSystem::verifyMinimizedRules(
849815
abort();
850816
}
851817

852-
for (const auto &pair : RedundantRules) {
818+
// Replacement paths for redundant rules can only reference other redundant
819+
// rules if those redundant rules were made redundant later, ie if they
820+
// appear later in the array.
821+
llvm::DenseSet<unsigned> laterRedundantRules;
822+
for (const auto &pair : llvm::reverse(RedundantRules)) {
853823
const auto &rule = getRule(pair.first);
854824
if (!rule.isRedundant()) {
855825
llvm::errs() << "Recorded replacement path for non-redundant rule "
@@ -860,14 +830,19 @@ void RewriteSystem::verifyMinimizedRules(
860830

861831
for (const auto &step : pair.second) {
862832
if (step.Kind == RewriteStep::Rule) {
863-
const auto &rule = getRule(step.getRuleID());
864-
if (rule.isRedundant()) {
833+
unsigned otherRuleID = step.getRuleID();
834+
const auto &otherRule = getRule(otherRuleID);
835+
if (otherRule.isRedundant() &&
836+
!laterRedundantRules.count(otherRuleID)) {
865837
llvm::errs() << "Redundant requirement path contains a redundant "
866-
"rule " << rule << "\n";
838+
"rule " << otherRule << "\n";
867839
dump(llvm::errs());
868840
abort();
869841
}
870842
}
871843
}
844+
845+
laterRedundantRules.insert(pair.first);
872846
}
847+
873848
}

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,6 @@ class RewriteSystem final {
367367
void computeMinimalConformances(
368368
llvm::DenseSet<unsigned> &redundantConformances);
369369

370-
void normalizeRedundantRules();
371-
372370
public:
373371
void recordRewriteLoop(MutableTerm basepoint,
374372
RewritePath path);

0 commit comments

Comments
 (0)