Skip to content

Commit d212041

Browse files
committed
RequirementMachine: Fold RewriteSystem::processConflicts() into recordConflict() and add debug output
1 parent 75161ce commit d212041

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

lib/AST/RequirementMachine/Debug.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ enum class DebugFlags : unsigned {
7272

7373
/// Print a trace of requirement machines constructed and how long each took.
7474
Timers = (1<<16),
75+
76+
/// Print conflicting rules.
77+
ConflictingRules = (1<<17),
7578
};
7679

7780
using DebugOptions = OptionSet<DebugFlags>;

lib/AST/RequirementMachine/HomotopyReduction.cpp

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -167,30 +167,6 @@ void RewriteSystem::propagateRedundantRequirementIDs() {
167167
}
168168
}
169169

170-
/// Process pairs of conflicting rules, marking the more specific rule as
171-
/// conflicting, which instructs minimization to drop this rule.
172-
void RewriteSystem::processConflicts() {
173-
for (auto pair : ConflictingRules) {
174-
auto *existingRule = &getRule(pair.first);
175-
auto *newRule = &getRule(pair.second);
176-
177-
// The identity conformance rule ([P].[P] => [P]) will conflict with
178-
// a concrete type requirement in an invalid protocol declaration
179-
// where 'Self' is constrained to a type that does not conform to
180-
// the protocol. This rule is permanent, so don't mark it as
181-
// conflicting in this case.
182-
183-
if (!existingRule->isIdentityConformanceRule() &&
184-
existingRule->getRHS().size() >= newRule->getRHS().size())
185-
existingRule->markConflicting();
186-
if (!newRule->isIdentityConformanceRule() &&
187-
newRule->getRHS().size() >= existingRule->getRHS().size())
188-
newRule->markConflicting();
189-
190-
// FIXME: Diagnose the conflict later.
191-
}
192-
}
193-
194170
/// Find a rule to delete by looking through all loops for rewrite rules appearing
195171
/// once in empty context. Returns a pair consisting of a loop ID and a rule ID,
196172
/// otherwise returns None.
@@ -477,7 +453,6 @@ void RewriteSystem::minimizeRewriteSystem() {
477453
Minimized = 1;
478454

479455
propagateExplicitBits();
480-
processConflicts();
481456

482457
if (Context.getASTContext().LangOpts.EnableRequirementMachineLoopNormalization) {
483458
for (auto &loop : Loops) {

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,37 @@ static void recordRelation(Term key,
119119
(void) system.addRule(lhs, rhs, &path);
120120
}
121121

122+
/// Given two property rules that conflict because no concrete type
123+
/// can satisfy both, mark one or both rules conflicting.
124+
///
125+
/// The right hand side of one rule must be a suffix of the other
126+
/// (in which case the longer of the two rules is conflicting) or
127+
/// the right hand sides are equal (in which case both will be
128+
/// conflicting).
122129
void RewriteSystem::recordConflict(unsigned existingRuleID,
123130
unsigned newRuleID) {
124131
ConflictingRules.emplace_back(existingRuleID, newRuleID);
132+
133+
auto &existingRule = getRule(existingRuleID);
134+
auto &newRule = getRule(newRuleID);
135+
136+
if (Debug.contains(DebugFlags::ConflictingRules)) {
137+
llvm::dbgs() << "Conflicting rules:\n";
138+
llvm::dbgs() << "- " << existingRule << "\n";
139+
llvm::dbgs() << "- " << newRule << "\n";
140+
}
141+
142+
// The identity conformance rule ([P].[P] => [P]) will conflict with
143+
// a concrete type requirement in an invalid protocol declaration
144+
// where 'Self' is constrained to a type that does not conform to
145+
// the protocol. This rule is permanent, so don't mark it as
146+
// conflicting in this case.
147+
if (!existingRule.isIdentityConformanceRule() &&
148+
existingRule.getRHS().size() >= newRule.getRHS().size())
149+
existingRule.markConflicting();
150+
if (!newRule.isIdentityConformanceRule() &&
151+
newRule.getRHS().size() >= existingRule.getRHS().size())
152+
newRule.markConflicting();
125153
}
126154

127155
void PropertyMap::addConformanceProperty(

lib/AST/RequirementMachine/RewriteContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ static DebugOptions parseDebugFlags(StringRef debugFlags) {
177177
.Case("concrete-contraction", DebugFlags::ConcreteContraction)
178178
.Case("propagate-requirement-ids", DebugFlags::PropagateRequirementIDs)
179179
.Case("timers", DebugFlags::Timers)
180+
.Case("conflicting-rules", DebugFlags::ConflictingRules)
180181
.Default(None);
181182
if (!flag) {
182183
llvm::errs() << "Unknown debug flag in -debug-requirement-machine "

lib/AST/RequirementMachine/RewriteSystem.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,6 @@ class RewriteSystem final {
361361

362362
void propagateRedundantRequirementIDs();
363363

364-
void processConflicts();
365-
366364
using EliminationPredicate = llvm::function_ref<bool(unsigned loopID,
367365
unsigned ruleID)>;
368366

0 commit comments

Comments
 (0)