Skip to content

Commit 9115a46

Browse files
committed
Sema: Record favored constraints in the trail
1 parent b2adf51 commit 9115a46

File tree

4 files changed

+28
-19
lines changed

4 files changed

+28
-19
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ class SolverTrail {
8484
RecordedKeyPathComponentType,
8585
/// Disabled a constraint.
8686
DisabledConstraint,
87+
/// Favored a constraint.
88+
FavoredConstraint,
8789
};
8890

8991
/// A change made to the constraint system.
@@ -248,6 +250,9 @@ class SolverTrail {
248250
/// Create a change that disabled a constraint.
249251
static Change disabledConstraint(Constraint *constraint);
250252

253+
/// Create a change that favored a constraint.
254+
static Change favoredConstraint(Constraint *constraint);
255+
251256
/// Undo this change, reverting the constraint graph to the state it
252257
/// had prior to this change.
253258
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,15 +2631,6 @@ class ConstraintSystem {
26312631
}
26322632

26332633
generatedConstraints.erase(genStart, genEnd);
2634-
2635-
for (unsigned constraintIdx :
2636-
range(scope->numFavoredConstraints, favoredConstraints.size())) {
2637-
if (favoredConstraints[constraintIdx]->isFavored())
2638-
favoredConstraints[constraintIdx]->setFavored(false);
2639-
}
2640-
favoredConstraints.erase(
2641-
favoredConstraints.begin() + scope->numFavoredConstraints,
2642-
favoredConstraints.end());
26432634
}
26442635

26452636
/// Check whether constraint system is allowed to form solutions
@@ -2651,21 +2642,18 @@ class ConstraintSystem {
26512642
/// Disable the given constraint; this change will be rolled back
26522643
/// when we exit the current solver scope.
26532644
void disableConstraint(Constraint *constraint) {
2645+
ASSERT(!constraint->isDisabled());
26542646
constraint->setDisabled();
26552647
Trail.recordChange(SolverTrail::Change::disabledConstraint(constraint));
26562648
}
26572649

2658-
unsigned getNumFavoredConstraints() const {
2659-
return favoredConstraints.size();
2660-
}
2661-
26622650
/// Favor the given constraint; this change will be rolled back
26632651
/// when we exit the current solver scope.
26642652
void favorConstraint(Constraint *constraint) {
26652653
assert(!constraint->isFavored());
26662654

26672655
constraint->setFavored();
2668-
favoredConstraints.push_back(constraint);
2656+
Trail.recordChange(SolverTrail::Change::favoredConstraint(constraint));
26692657
}
26702658

26712659
private:
@@ -2689,8 +2677,7 @@ class ConstraintSystem {
26892677
llvm::SmallVector<
26902678
std::tuple<SolverScope *, ConstraintList::iterator, unsigned>, 4> scopes;
26912679

2692-
SmallVector<Constraint *, 4> favoredConstraints;
2693-
2680+
26942681
/// Depth of the solution stack.
26952682
unsigned depth = 0;
26962683
};
@@ -2871,8 +2858,6 @@ class ConstraintSystem {
28712858
/// FIXME: Remove this.
28722859
unsigned numFixes;
28732860

2874-
unsigned numFavoredConstraints;
2875-
28762861
unsigned numResultBuilderTransformed;
28772862

28782863
/// The length of \c appliedPropertyWrappers

lib/Sema/CSSolver.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
664664
numTypeVariables = cs.TypeVariables.size();
665665
numFixes = cs.Fixes.size();
666666
numKeyPaths = cs.KeyPaths.size();
667-
numFavoredConstraints = cs.solverState->getNumFavoredConstraints();
668667
numResultBuilderTransformed = cs.resultBuilderTransformed.size();
669668
numAppliedPropertyWrappers = cs.appliedPropertyWrappers.size();
670669
numResolvedOverloads = cs.ResolvedOverloads.size();

lib/Sema/CSTrail.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ SolverTrail::Change::disabledConstraint(Constraint *constraint) {
254254
return result;
255255
}
256256

257+
SolverTrail::Change
258+
SolverTrail::Change::favoredConstraint(Constraint *constraint) {
259+
Change result;
260+
result.Kind = ChangeKind::FavoredConstraint;
261+
result.TheConstraint.Constraint = constraint;
262+
return result;
263+
}
264+
257265
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
258266
auto &cg = cs.getConstraintGraph();
259267

@@ -355,6 +363,11 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
355363
if (TheConstraint.Constraint->isDisabled())
356364
TheConstraint.Constraint->setEnabled();
357365
break;
366+
367+
case ChangeKind::FavoredConstraint:
368+
if (TheConstraint.Constraint->isFavored())
369+
TheConstraint.Constraint->setFavored(false);
370+
break;
358371
}
359372
}
360373

@@ -550,6 +563,13 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
550563
indent + 2);
551564
out << ")\n";
552565
break;
566+
567+
case ChangeKind::FavoredConstraint:
568+
out << "(favored constraint ";
569+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
570+
indent + 2);
571+
out << ")\n";
572+
break;
553573
}
554574
}
555575

0 commit comments

Comments
 (0)