Skip to content

Commit 9017540

Browse files
committed
Sema: Record generated constraints in the trail
1 parent 1e2d4fb commit 9017540

File tree

5 files changed

+36
-63
lines changed

5 files changed

+36
-63
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
#define CLOSURE_CHANGE(Name) CHANGE(Name)
3131
#endif
3232

33+
#ifndef CONSTRAINT_CHANGE
34+
#define CONSTRAINT_CHANGE(Name) CHANGE(Name)
35+
#endif
36+
3337
#ifndef LAST_CHANGE
3438
#define LAST_CHANGE(Name)
3539
#endif
@@ -54,6 +58,10 @@ EXPR_CHANGE(RecordedExprPattern)
5458
CLOSURE_CHANGE(RecordedClosureType)
5559
CLOSURE_CHANGE(RecordedPreconcurrencyClosure)
5660

61+
CONSTRAINT_CHANGE(DisabledConstraint)
62+
CONSTRAINT_CHANGE(FavoredConstraint)
63+
CONSTRAINT_CHANGE(GeneratedConstraint)
64+
5765
CHANGE(AddedTypeVariable)
5866
CHANGE(AddedConstraint)
5967
CHANGE(RemovedConstraint)
@@ -69,8 +77,6 @@ CHANGE(RecordedOpenedPackExpansionType)
6977
CHANGE(RecordedPackEnvironment)
7078
CHANGE(RecordedNodeType)
7179
CHANGE(RecordedKeyPathComponentType)
72-
CHANGE(DisabledConstraint)
73-
CHANGE(FavoredConstraint)
7480
CHANGE(RecordedResultBuilderTransform)
7581
CHANGE(RecordedContextualInfo)
7682
CHANGE(RecordedTarget)
@@ -86,5 +92,6 @@ LAST_CHANGE(DecreasedScore)
8692
#undef LOCATOR_CHANGE
8793
#undef EXPR_CHANGE
8894
#undef CLOSURE_CHANGE
95+
#undef CONSTRAINT_CHANGE
8996
#undef LAST_CHANGE
9097
#undef CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class SolverTrail {
142142
#define LOCATOR_CHANGE(Name, _) static Change Name(ConstraintLocator *locator);
143143
#define EXPR_CHANGE(Name) static Change Name(Expr *expr);
144144
#define CLOSURE_CHANGE(Name) static Change Name(ClosureExpr *closure);
145+
#define CONSTRAINT_CHANGE(Name) static Change Name(Constraint *constraint);
145146
#include "swift/Sema/CSTrail.def"
146147

147148
/// Create a change that added a type variable.
@@ -202,12 +203,6 @@ class SolverTrail {
202203
unsigned component,
203204
Type oldType);
204205

205-
/// Create a change that disabled a constraint.
206-
static Change DisabledConstraint(Constraint *constraint);
207-
208-
/// Create a change that favored a constraint.
209-
static Change FavoredConstraint(Constraint *constraint);
210-
211206
/// Create a change that recorded a result builder transform.
212207
static Change RecordedResultBuilderTransform(AnyFunctionRef fn);
213208

include/swift/Sema/ConstraintSystem.h

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2566,8 +2566,7 @@ class ConstraintSystem {
25662566
///
25672567
/// \param constraint The newly generated constraint.
25682568
void addGeneratedConstraint(Constraint *constraint) {
2569-
assert(constraint && "Null generated constraint?");
2570-
generatedConstraints.push_back(constraint);
2569+
Trail.recordChange(SolverTrail::Change::GeneratedConstraint(constraint));
25712570
}
25722571

25732572
/// Register given scope to be tracked by the current solver state,
@@ -2582,8 +2581,7 @@ class ConstraintSystem {
25822581

25832582
CS.incrementScopeCounter();
25842583
auto scopeInfo =
2585-
std::make_tuple(scope, retiredConstraints.begin(),
2586-
generatedConstraints.size());
2584+
std::make_tuple(scope, retiredConstraints.begin());
25872585
scopes.push_back(scopeInfo);
25882586
}
25892587

@@ -2604,10 +2602,8 @@ class ConstraintSystem {
26042602
SolverScope *savedScope;
26052603
// The position of last retired constraint before given scope.
26062604
ConstraintList::iterator lastRetiredPos;
2607-
// The original number of generated constraints before given scope.
2608-
unsigned numGenerated;
26092605

2610-
std::tie(savedScope, lastRetiredPos, numGenerated) =
2606+
std::tie(savedScope, lastRetiredPos) =
26112607
scopes.pop_back_val();
26122608

26132609
assert(savedScope == scope && "Scope rollback not in LIFO order!");
@@ -2616,15 +2612,6 @@ class ConstraintSystem {
26162612
CS.InactiveConstraints.splice(CS.InactiveConstraints.end(),
26172613
retiredConstraints,
26182614
retiredConstraints.begin(), lastRetiredPos);
2619-
2620-
// And remove all of the generated constraints.
2621-
auto genStart = generatedConstraints.begin() + numGenerated,
2622-
genEnd = generatedConstraints.end();
2623-
for (auto genI = genStart; genI != genEnd; ++genI) {
2624-
CS.InactiveConstraints.erase(ConstraintList::iterator(*genI));
2625-
}
2626-
2627-
generatedConstraints.erase(genStart, genEnd);
26282615
}
26292616

26302617
/// Check whether constraint system is allowed to form solutions
@@ -2660,17 +2647,13 @@ class ConstraintSystem {
26602647
/// creating, it's used to re-activate them on destruction.
26612648
SmallVector<Constraint *, 4> activeConstraints;
26622649

2663-
/// The current set of generated constraints.
2664-
SmallVector<Constraint *, 4> generatedConstraints;
2665-
26662650
/// The collection which holds association between solver scope
26672651
/// and position of the last retired constraint and number of
26682652
/// constraints generated before registration of given scope,
26692653
/// this helps to rollback all of the constraints retired/generated
26702654
/// each of the registered scopes correct (LIFO) order.
26712655
llvm::SmallVector<
2672-
std::tuple<SolverScope *, ConstraintList::iterator, unsigned>, 4> scopes;
2673-
2656+
std::tuple<SolverScope *, ConstraintList::iterator>, 4> scopes;
26742657

26752658
/// Depth of the solution stack.
26762659
unsigned depth = 0;

lib/Sema/CSSolver.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,6 @@ ConstraintSystem::SolverState::~SolverState() {
658658
// to constraint system.
659659
assert(!hasRetiredConstraints());
660660

661-
// Make sure that all of the generated constraints have been handled.
662-
assert(generatedConstraints.empty());
663-
664661
// Re-activate constraints which were initially marked as "active"
665662
// to restore original state of the constraint system.
666663
for (auto *constraint : activeConstraints) {

lib/Sema/CSTrail.cpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ SolverTrail::~SolverTrail() {
7272
result.TheClosure = closure; \
7373
return result; \
7474
}
75+
#define CONSTRAINT_CHANGE(Name) \
76+
SolverTrail::Change \
77+
SolverTrail::Change::Name(Constraint *constraint) { \
78+
Change result; \
79+
result.Kind = ChangeKind::Name; \
80+
result.TheConstraint.Constraint = constraint; \
81+
return result; \
82+
}
7583
#include "swift/Sema/CSTrail.def"
7684

7785
SolverTrail::Change
@@ -221,22 +229,6 @@ SolverTrail::Change::RecordedKeyPathComponentType(const KeyPathExpr *expr,
221229
return result;
222230
}
223231

224-
SolverTrail::Change
225-
SolverTrail::Change::DisabledConstraint(Constraint *constraint) {
226-
Change result;
227-
result.Kind = ChangeKind::DisabledConstraint;
228-
result.TheConstraint.Constraint = constraint;
229-
return result;
230-
}
231-
232-
SolverTrail::Change
233-
SolverTrail::Change::FavoredConstraint(Constraint *constraint) {
234-
Change result;
235-
result.Kind = ChangeKind::FavoredConstraint;
236-
result.TheConstraint.Constraint = constraint;
237-
return result;
238-
}
239-
240232
SolverTrail::Change
241233
SolverTrail::Change::RecordedResultBuilderTransform(AnyFunctionRef fn) {
242234
Change result;
@@ -522,6 +514,12 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
522514
cs.CurrentScore.Data[kind] += value;
523515
break;
524516
}
517+
518+
case ChangeKind::GeneratedConstraint: {
519+
auto iter = ConstraintList::iterator(TheConstraint.Constraint);
520+
cs.InactiveConstraints.erase(iter);
521+
break;
522+
}
525523
}
526524
}
527525

@@ -553,6 +551,13 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
553551
simple_display(out, TheClosure); \
554552
out << ")\n"; \
555553
break;
554+
#define CONSTRAINT_CHANGE(Name) \
555+
case ChangeKind::Name: \
556+
out << "(" << #Name << " "; \
557+
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr, \
558+
indent + 2); \
559+
out << ")\n"; \
560+
break;
556561
#include "swift/Sema/CSTrail.def"
557562

558563
case ChangeKind::AddedTypeVariable:
@@ -689,20 +694,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
689694
out << " for component " << Options << ")\n";
690695
break;
691696

692-
case ChangeKind::DisabledConstraint:
693-
out << "(DisabledConstraint ";
694-
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
695-
indent + 2);
696-
out << ")\n";
697-
break;
698-
699-
case ChangeKind::FavoredConstraint:
700-
out << "(FavoredConstraint ";
701-
TheConstraint.Constraint->print(out, &cs.getASTContext().SourceMgr,
702-
indent + 2);
703-
out << ")\n";
704-
break;
705-
706697
case ChangeKind::RecordedResultBuilderTransform:
707698
out << "(RecordedResultBuilderTransform ";
708699
simple_display(out, TheRef);
@@ -823,4 +814,4 @@ void SolverTrail::dumpActiveScopeChanges(llvm::raw_ostream &out,
823814

824815
out.indent(indent);
825816
out << ")\n";
826-
}
817+
}

0 commit comments

Comments
 (0)