Skip to content

Commit 8fbe93c

Browse files
committed
RequirementMachine: Rename Rule::isDeleted() => isSimplified(), and add isPermanent(), isRedundant() bits
1 parent 1fa36c7 commit 8fbe93c

File tree

4 files changed

+60
-20
lines changed

4 files changed

+60
-20
lines changed

lib/AST/RequirementMachine/PropertyMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ RewriteSystem::buildPropertyMap(PropertyMap &map,
10401040
SmallVector<std::vector<std::pair<Term, Symbol>>, 4> properties;
10411041

10421042
for (const auto &rule : Rules) {
1043-
if (rule.isDeleted())
1043+
if (rule.isSimplified())
10441044
continue;
10451045

10461046
// Collect all rules of the form T.[p] => T where T is canonical.

lib/AST/RequirementMachine/RewriteSystem.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ bool Rule::isProtocolConformanceRule() const {
4747

4848
void Rule::dump(llvm::raw_ostream &out) const {
4949
out << LHS << " => " << RHS;
50-
if (deleted)
51-
out << " [deleted]";
50+
if (Permanent)
51+
out << " [permanent]";
52+
if (Simplified)
53+
out << " [simplified]";
54+
if (Redundant)
55+
out << " [redundant]";
5256
}
5357

5458
RewriteSystem::RewriteSystem(RewriteContext &ctx)
@@ -221,7 +225,7 @@ bool RewriteSystem::simplify(MutableTerm &term, RewritePath *path) const {
221225
auto ruleID = Trie.find(from, end);
222226
if (ruleID) {
223227
const auto &rule = getRule(*ruleID);
224-
if (!rule.isDeleted()) {
228+
if (!rule.isSimplified()) {
225229
auto to = from + rule.getLHS().size();
226230
assert(std::equal(from, to, rule.getLHS().begin()));
227231

@@ -271,7 +275,7 @@ bool RewriteSystem::simplify(MutableTerm &term, RewritePath *path) const {
271275
void RewriteSystem::simplifyRewriteSystem() {
272276
for (unsigned ruleID = 0, e = Rules.size(); ruleID < e; ++ruleID) {
273277
auto &rule = getRule(ruleID);
274-
if (rule.isDeleted())
278+
if (rule.isSimplified())
275279
continue;
276280

277281
// First, see if the left hand side of this rule can be reduced using
@@ -286,7 +290,7 @@ void RewriteSystem::simplifyRewriteSystem() {
286290
continue;
287291

288292
// Ignore other deleted rules.
289-
if (getRule(*otherRuleID).isDeleted())
293+
if (getRule(*otherRuleID).isSimplified())
290294
continue;
291295

292296
if (Debug.contains(DebugFlags::Completion)) {
@@ -296,13 +300,13 @@ void RewriteSystem::simplifyRewriteSystem() {
296300
<< "\n";
297301
}
298302

299-
rule.markDeleted();
303+
rule.markSimplified();
300304
break;
301305
}
302306
}
303307

304308
// If the rule was deleted above, skip the rest.
305-
if (rule.isDeleted())
309+
if (rule.isSimplified())
306310
continue;
307311

308312
// Now, try to reduce the right hand side.
@@ -312,7 +316,7 @@ void RewriteSystem::simplifyRewriteSystem() {
312316
continue;
313317

314318
// We're adding a new rule, so the old rule won't apply anymore.
315-
rule.markDeleted();
319+
rule.markSimplified();
316320

317321
unsigned newRuleID = Rules.size();
318322

@@ -359,7 +363,7 @@ void RewriteSystem::verifyRewriteRules() const {
359363
}
360364

361365
for (const auto &rule : Rules) {
362-
if (rule.isDeleted())
366+
if (rule.isSimplified())
363367
continue;
364368

365369
const auto &lhs = rule.getLHS();

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,37 @@ class RewriteSystem;
4141
class Rule final {
4242
Term LHS;
4343
Term RHS;
44-
bool deleted;
44+
45+
/// Associated type introduction rules are 'permanent', meaning they cannot
46+
/// be deleted by homotopy reduction. This is because they do not correspond
47+
/// to generic requirements and are re-added when the rewrite system is
48+
/// built, so by leaving them in place we can find other redundancies
49+
/// instead.
50+
unsigned Permanent : 1;
51+
52+
/// A 'simplified' rule was eliminated by simplifyRewriteSystem() if one of two
53+
/// things happen:
54+
/// - The rule's left hand side can be reduced via some other rule, in which
55+
/// case completion will have filled in the missing edge if necessary.
56+
/// - The rule's right hand side can be reduced, in which case the reduced
57+
/// rule is added when simplifying the rewrite system.
58+
///
59+
/// Simplified rules do not participate in term rewriting, because other rules
60+
/// can be used to derive an equivalent rewrite path.
61+
unsigned Simplified : 1;
62+
63+
/// A 'redundant' rule was eliminated by homotopy reduction. Redundant rules
64+
/// still participate in term rewriting, but they are not part of the minimal
65+
/// set of requirements in a generic signature.
66+
unsigned Redundant : 1;
4567

4668
public:
4769
Rule(Term lhs, Term rhs)
48-
: LHS(lhs), RHS(rhs), deleted(false) {}
70+
: LHS(lhs), RHS(rhs) {
71+
Permanent = false;
72+
Simplified = false;
73+
Redundant = false;
74+
}
4975

5076
const Term &getLHS() const { return LHS; }
5177
const Term &getRHS() const { return RHS; }
@@ -54,19 +80,29 @@ class Rule final {
5480

5581
bool isProtocolConformanceRule() const;
5682

57-
/// Returns if the rule was deleted.
58-
bool isDeleted() const {
59-
return deleted;
83+
/// See above for an explanation.
84+
bool isPermanent() const {
85+
return Permanent;
86+
}
87+
88+
/// See above for an explanation.
89+
bool isSimplified() const {
90+
return Simplified;
91+
}
92+
93+
/// See above for an explanation.
94+
bool isRedundant() const {
95+
return Redundant;
6096
}
6197

6298
/// Deletes the rule, which removes it from consideration in term
6399
/// simplification and completion. Deleted rules are simply marked as
64100
/// such instead of being physically removed from the rules vector
65101
/// in the rewrite system, to ensure that indices remain valid across
66102
/// deletion.
67-
void markDeleted() {
68-
assert(!deleted);
69-
deleted = true;
103+
void markSimplified() {
104+
assert(!Simplified);
105+
Simplified = true;
70106
}
71107

72108
/// Returns the length of the left hand side.

lib/AST/RequirementMachine/RewriteSystemCompletion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
501501
// For every rule, looking for other rules that overlap with this rule.
502502
for (unsigned i = 0, e = Rules.size(); i < e; ++i) {
503503
const auto &lhs = getRule(i);
504-
if (lhs.isDeleted())
504+
if (lhs.isSimplified())
505505
continue;
506506

507507
// Look up every suffix of this rule in the trie using findAll(). This
@@ -519,7 +519,7 @@ RewriteSystem::computeConfluentCompletion(unsigned maxIterations,
519519
return;
520520

521521
const auto &rhs = getRule(j);
522-
if (rhs.isDeleted())
522+
if (rhs.isSimplified())
523523
return;
524524

525525
if (from == lhs.getLHS().begin()) {

0 commit comments

Comments
 (0)