Skip to content

Commit 2b068b3

Browse files
committed
RequirementMachine: Encapsulate 3-cells in a new HomotopyGenerator data type
1 parent 5210e5d commit 2b068b3

File tree

4 files changed

+51
-29
lines changed

4 files changed

+51
-29
lines changed

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,12 @@ void RewriteSystem::minimizeRewriteSystem() {
505505
const auto &loop = HomotopyGenerators[loopID];
506506

507507
SmallVector<unsigned> redundancyCandidates =
508-
loop.second.findRulesAppearingOnceInEmptyContext();
508+
loop.Path.findRulesAppearingOnceInEmptyContext();
509509
if (redundancyCandidates.empty())
510510
continue;
511511

512512
auto ruleID = redundancyCandidates.front();
513-
RewritePath replacementPath = loop.second.splitCycleAtRule(ruleID);
513+
RewritePath replacementPath = loop.Path.splitCycleAtRule(ruleID);
514514

515515
deletedRules.insert(ruleID);
516516
deletedHomotopyGenerators.insert(loopID);
@@ -538,30 +538,30 @@ void RewriteSystem::minimizeRewriteSystem() {
538538
continue;
539539

540540
auto &loop = HomotopyGenerators[loopID];
541-
bool changed = loop.second.replaceRuleWithPath(ruleID, replacementPath);
541+
bool changed = loop.Path.replaceRuleWithPath(ruleID, replacementPath);
542542

543543
if (changed) {
544-
unsigned size = loop.second.size();
544+
unsigned size = loop.Path.size();
545545

546546
bool changed;
547547
do {
548548
changed = false;
549-
changed |= loop.second.computeFreelyReducedPath();
550-
changed |= loop.second.computeCyclicallyReducedLoop(loop.first, *this);
551-
changed |= loop.second.computeLeftCanonicalForm(*this);
549+
changed |= loop.Path.computeFreelyReducedPath();
550+
changed |= loop.Path.computeCyclicallyReducedLoop(loop.Basepoint, *this);
551+
changed |= loop.Path.computeLeftCanonicalForm(*this);
552552
} while (changed);
553553

554554
if (Debug.contains(DebugFlags::HomotopyReduction)) {
555-
if (size != loop.second.size()) {
556-
llvm::dbgs() << "** Note: Cyclically reduced the loop to eliminate "
557-
<< (size - loop.second.size()) << " steps\n";
555+
if (size != loop.Path.size()) {
556+
llvm::dbgs() << "** Note: Reducing the loop eliminated "
557+
<< (size - loop.Path.size()) << " steps\n";
558558
}
559559
}
560560

561561
if (Debug.contains(DebugFlags::HomotopyReduction)) {
562562
llvm::dbgs() << "** Updated homotopy generator: ";
563-
llvm::dbgs() << "- " << loop.first << ": ";
564-
loop.second.dump(llvm::dbgs(), loop.first, *this);
563+
llvm::dbgs() << "- " << loop.Basepoint << ": ";
564+
loop.Path.dump(llvm::dbgs(), loop.Basepoint, *this);
565565
llvm::dbgs() << "\n";
566566
}
567567
}
@@ -587,16 +587,16 @@ void RewriteSystem::minimizeRewriteSystem() {
587587
continue;
588588

589589
const auto &loop = HomotopyGenerators[loopID];
590-
if (loop.second.empty())
590+
if (loop.Path.empty())
591591
continue;
592592

593593
llvm::dbgs() << "(#" << loopID << ") ";
594-
llvm::dbgs() << loop.first << ": ";
595-
loop.second.dump(llvm::dbgs(), loop.first, *this);
594+
llvm::dbgs() << loop.Basepoint << ": ";
595+
loop.Path.dump(llvm::dbgs(), loop.Basepoint, *this);
596596
llvm::dbgs() << "\n";
597597

598-
MutableTerm basepoint = loop.first;
599-
for (auto step : loop.second) {
598+
MutableTerm basepoint = loop.Basepoint;
599+
for (auto step : loop.Path) {
600600
step.apply(basepoint, *this);
601601
llvm::dbgs() << "- " << basepoint << "\n";
602602
}
@@ -608,15 +608,15 @@ void RewriteSystem::minimizeRewriteSystem() {
608608
void RewriteSystem::verifyHomotopyGenerators() const {
609609
#ifndef NDEBUG
610610
for (const auto &loop : HomotopyGenerators) {
611-
auto term = loop.first;
611+
auto term = loop.Basepoint;
612612

613-
for (const auto &step : loop.second) {
613+
for (const auto &step : loop.Path) {
614614
step.apply(term, *this);
615615
}
616616

617-
if (term != loop.first) {
617+
if (term != loop.Basepoint) {
618618
llvm::errs() << "Not a loop: ";
619-
loop.second.dump(llvm::errs(), loop.first, *this);
619+
loop.Path.dump(llvm::errs(), loop.Basepoint, *this);
620620
llvm::errs() << "\n";
621621
abort();
622622
}

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ void RewriteSystem::dump(llvm::raw_ostream &out) const {
428428
out << "}\n";
429429
out << "Homotopy generators: {\n";
430430
for (const auto &loop : HomotopyGenerators) {
431-
out << "- " << loop.first << ": ";
432-
loop.second.dump(out, loop.first, *this);
431+
out << "- " << loop.Basepoint << ": ";
432+
loop.Path.dump(out, loop.Basepoint, *this);
433433
out << "\n";
434434
}
435435
out << "}\n";

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,29 @@ struct RewritePath {
266266
const RewriteSystem &system) const;
267267
};
268268

269+
/// A loop (3-cell) that rewrites the basepoint back to the basepoint.
270+
class HomotopyGenerator {
271+
public:
272+
MutableTerm Basepoint;
273+
RewritePath Path;
274+
275+
private:
276+
bool Deleted;
277+
278+
public:
279+
HomotopyGenerator(MutableTerm basepoint, RewritePath path)
280+
: Basepoint(basepoint), Path(path), Deleted(false) {}
281+
282+
bool isDeleted() const {
283+
return Deleted;
284+
}
285+
286+
void markDeleted() {
287+
assert(!Deleted);
288+
Deleted = true;
289+
}
290+
};
291+
269292
/// A term rewrite system for working with types in a generic signature.
270293
///
271294
/// Out-of-line methods are documented in RewriteSystem.cpp.
@@ -320,7 +343,7 @@ class RewriteSystem final {
320343
/// loop around a base point.
321344
///
322345
/// This data informs the generic signature minimization algorithm.
323-
std::vector<std::pair<MutableTerm, RewritePath>> HomotopyGenerators;
346+
std::vector<HomotopyGenerator> HomotopyGenerators;
324347

325348
DebugOptions Debug;
326349

@@ -401,7 +424,7 @@ class RewriteSystem final {
401424
const Rule &lhs, const Rule &rhs,
402425
std::vector<std::pair<MutableTerm, MutableTerm>> &pairs,
403426
std::vector<RewritePath> &paths,
404-
std::vector<std::pair<MutableTerm, RewritePath>> &loops) const;
427+
std::vector<HomotopyGenerator> &loops) const;
405428

406429
void processMergedAssociatedTypes();
407430

lib/AST/RequirementMachine/RewriteSystemCompletion.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,7 @@ RewriteSystem::computeCriticalPair(ArrayRef<Symbol>::const_iterator from,
369369
std::vector<std::pair<MutableTerm,
370370
MutableTerm>> &pairs,
371371
std::vector<RewritePath> &paths,
372-
std::vector<std::pair<MutableTerm,
373-
RewritePath>> &loops) const {
372+
std::vector<HomotopyGenerator> &loops) const {
374373
auto end = lhs.getLHS().end();
375374
if (from + rhs.getLHS().size() < end) {
376375
// lhs == TUV -> X, rhs == U -> Y.
@@ -495,7 +494,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
495494

496495
std::vector<std::pair<MutableTerm, MutableTerm>> resolvedCriticalPairs;
497496
std::vector<RewritePath> resolvedPaths;
498-
std::vector<std::pair<MutableTerm, RewritePath>> resolvedLoops;
497+
std::vector<HomotopyGenerator> resolvedLoops;
499498

500499
do {
501500
// For every rule, looking for other rules that overlap with this rule.
@@ -571,7 +570,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
571570
llvm::dbgs() << rhs << ":\n";
572571

573572
llvm::dbgs() << "$$ Loop: ";
574-
loop.second.dump(llvm::dbgs(), loop.first, *this);
573+
loop.Path.dump(llvm::dbgs(), loop.Basepoint, *this);
575574
llvm::dbgs() << "\n\n";
576575

577576
// Record the trivial loop.

0 commit comments

Comments
 (0)