Skip to content

Commit 8ab1c1f

Browse files
committed
RequirementMachine: Remove one of the three homotopy reduction passes
This was a hack from before the change to minimize away the least canonical rules first. Now, there's no reason to perform a separate pass over rules containing unresolved name symbols.
1 parent 15bf001 commit 8ab1c1f

File tree

2 files changed

+18
-43
lines changed

2 files changed

+18
-43
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,6 @@ bool RewriteLoop::isInContext(const RewriteSystem &system) const {
474474
/// minimization algorithm.
475475
bool RewriteSystem::
476476
isCandidateForDeletion(unsigned ruleID,
477-
bool firstPass,
478477
const llvm::DenseSet<unsigned> *redundantConformances) const {
479478
const auto &rule = getRule(ruleID);
480479

@@ -493,22 +492,15 @@ isCandidateForDeletion(unsigned ruleID,
493492
// Other rules involving unresolved name symbols are derived from an
494493
// associated type introduction rule together with a conformance rule.
495494
// They are eliminated in the first pass.
496-
if (firstPass)
497-
return rule.getLHS().containsUnresolvedSymbols();
498-
499-
// In the second and third pass we should not have any rules involving
500-
// unresolved name symbols, except for permanent rules which were
501-
// already skipped above.
502-
//
503-
// FIXME: This isn't true with invalid code.
504-
assert(!rule.getLHS().containsUnresolvedSymbols());
495+
if (rule.getLHS().containsUnresolvedSymbols())
496+
return true;
505497

506498
// Protocol conformance rules are eliminated via a different
507499
// algorithm which computes "generating conformances".
508500
//
509-
// The first and second passes skip protocol conformance rules.
501+
// The first pass skips protocol conformance rules.
510502
//
511-
// The third pass eliminates any protocol conformance rule which is
503+
// The second pass eliminates any protocol conformance rule which is
512504
// redundant according to both homotopy reduction and the generating
513505
// conformances algorithm.
514506
//
@@ -532,25 +524,19 @@ isCandidateForDeletion(unsigned ruleID,
532524
/// once in empty context. Returns a redundant rule to delete if one was found,
533525
/// otherwise returns None.
534526
///
535-
/// Minimization performs three passes over the rewrite system, with the
536-
/// \p firstPass and \p redundantConformances parameters as follows:
527+
/// Minimization performs three passes over the rewrite system.
537528
///
538-
/// 1) First, rules involving unresolved name symbols are deleted, with
539-
/// \p firstPass equal to true and \p redundantConformances equal to nullptr.
529+
/// 1) First, rules that are not conformance rules are deleted, with
530+
/// \p redundantConformances equal to nullptr.
540531
///
541-
/// 2) Second, rules that are not conformance rules are deleted, with
542-
/// \p firstPass equal to false and \p redundantConformances equal to nullptr.
532+
/// 2) Second, generating conformances are computed.
543533
///
544-
/// 3) Finally, conformance rules are deleted after computing a minimal set of
545-
/// generating conformances, with \p firstPass equal to false and
546-
/// \p redundantConformances equal to the set of conformance rules that are
534+
/// 3) Finally, redundant conformance rules are deleted, with
535+
/// \p redundantConformances equal to the set of conformance rules that are
547536
/// not generating conformances.
548537
Optional<unsigned> RewriteSystem::
549-
findRuleToDelete(bool firstPass,
550-
const llvm::DenseSet<unsigned> *redundantConformances,
538+
findRuleToDelete(const llvm::DenseSet<unsigned> *redundantConformances,
551539
RewritePath &replacementPath) {
552-
assert(!firstPass || redundantConformances == nullptr);
553-
554540
SmallVector<std::pair<unsigned, unsigned>, 2> redundancyCandidates;
555541
for (unsigned loopID : indices(Loops)) {
556542
const auto &loop = Loops[loopID];
@@ -566,7 +552,7 @@ findRuleToDelete(bool firstPass,
566552

567553
for (const auto &pair : redundancyCandidates) {
568554
unsigned ruleID = pair.second;
569-
if (!isCandidateForDeletion(ruleID, firstPass, redundantConformances))
555+
if (!isCandidateForDeletion(ruleID, redundantConformances))
570556
continue;
571557

572558
if (!found) {
@@ -665,12 +651,10 @@ void RewriteSystem::deleteRule(unsigned ruleID,
665651
}
666652

667653
void RewriteSystem::performHomotopyReduction(
668-
bool firstPass,
669654
const llvm::DenseSet<unsigned> *redundantConformances) {
670655
while (true) {
671656
RewritePath replacementPath;
672-
auto optRuleID = findRuleToDelete(firstPass,
673-
redundantConformances,
657+
auto optRuleID = findRuleToDelete(redundantConformances,
674658
replacementPath);
675659

676660
// If no redundant rules remain which can be eliminated by this pass, stop.
@@ -703,13 +687,8 @@ void RewriteSystem::minimizeRewriteSystem() {
703687
// Check invariants before homotopy reduction.
704688
verifyRewriteLoops();
705689

706-
// First pass: Eliminate all redundant rules involving unresolved types.
707-
performHomotopyReduction(/*firstPass=*/true,
708-
/*redundantConformances=*/nullptr);
709-
710-
// Second pass: Eliminate all redundant rules that are not conformance rules.
711-
performHomotopyReduction(/*firstPass=*/false,
712-
/*redundantConformances=*/nullptr);
690+
// First pass: Eliminate all redundant rules that are not conformance rules.
691+
performHomotopyReduction(/*redundantConformances=*/nullptr);
713692

714693
// Now find a minimal set of generating conformances.
715694
//
@@ -719,9 +698,8 @@ void RewriteSystem::minimizeRewriteSystem() {
719698
llvm::DenseSet<unsigned> redundantConformances;
720699
computeGeneratingConformances(redundantConformances);
721700

722-
// Third pass: Eliminate all redundant conformance rules.
723-
performHomotopyReduction(/*firstPass=*/false,
724-
/*redundantConformances=*/&redundantConformances);
701+
// Second pass: Eliminate all redundant conformance rules.
702+
performHomotopyReduction(/*redundantConformances=*/&redundantConformances);
725703

726704
// Check invariants after homotopy reduction.
727705
verifyRewriteLoops();

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,15 @@ class RewriteSystem final {
324324

325325
bool
326326
isCandidateForDeletion(unsigned ruleID,
327-
bool firstPass,
328327
const llvm::DenseSet<unsigned> *redundantConformances) const;
329328

330329
Optional<unsigned>
331-
findRuleToDelete(bool firstPass,
332-
const llvm::DenseSet<unsigned> *redundantConformances,
330+
findRuleToDelete(const llvm::DenseSet<unsigned> *redundantConformances,
333331
RewritePath &replacementPath);
334332

335333
void deleteRule(unsigned ruleID, const RewritePath &replacementPath);
336334

337335
void performHomotopyReduction(
338-
bool firstPass,
339336
const llvm::DenseSet<unsigned> *redundantConformances);
340337

341338
void minimizeRewriteSystem();

0 commit comments

Comments
 (0)