Skip to content

Commit 5fdc1a8

Browse files
committed
Sema: Record retired constraints in the trail
1 parent 9017540 commit 5fdc1a8

File tree

5 files changed

+14
-76
lines changed

5 files changed

+14
-76
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ CLOSURE_CHANGE(RecordedPreconcurrencyClosure)
6161
CONSTRAINT_CHANGE(DisabledConstraint)
6262
CONSTRAINT_CHANGE(FavoredConstraint)
6363
CONSTRAINT_CHANGE(GeneratedConstraint)
64+
CONSTRAINT_CHANGE(RetiredConstraint)
6465

6566
CHANGE(AddedTypeVariable)
6667
CHANGE(AddedConstraint)

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,26 +2540,11 @@ class ConstraintSystem {
25402540
#define CS_STATISTIC(Name, Description) unsigned Name = 0;
25412541
#include "ConstraintSolverStats.def"
25422542

2543-
/// Check whether there are any retired constraints present.
2544-
bool hasRetiredConstraints() const {
2545-
return !retiredConstraints.empty();
2546-
}
2547-
25482543
/// Mark given constraint as retired along current solver path.
25492544
///
25502545
/// \param constraint The constraint to retire temporarily.
25512546
void retireConstraint(Constraint *constraint) {
2552-
retiredConstraints.push_front(constraint);
2553-
}
2554-
2555-
/// Iterate over all of the retired constraints registered with
2556-
/// current solver state.
2557-
///
2558-
/// \param processor The processor function to be applied to each of
2559-
/// the constraints retrieved.
2560-
void forEachRetired(llvm::function_ref<void(Constraint &)> processor) {
2561-
for (auto &constraint : retiredConstraints)
2562-
processor(constraint);
2547+
Trail.recordChange(SolverTrail::Change::RetiredConstraint(constraint));
25632548
}
25642549

25652550
/// Add new "generated" constraint along the current solver path.
@@ -2569,49 +2554,26 @@ class ConstraintSystem {
25692554
Trail.recordChange(SolverTrail::Change::GeneratedConstraint(constraint));
25702555
}
25712556

2572-
/// Register given scope to be tracked by the current solver state,
2573-
/// this helps to make sure that all of the retired/generated constraints
2574-
/// are dealt with correctly when the life time of the scope ends.
2557+
/// Update statistics when a scope begins.
25752558
///
25762559
/// \param scope The scope to associate with current solver state.
2577-
void registerScope(SolverScope *scope) {
2560+
void beginScope(SolverScope *scope) {
25782561
++depth;
25792562
maxDepth = std::max(maxDepth, depth);
25802563
scope->scopeNumber = NumStatesExplored++;
25812564

25822565
CS.incrementScopeCounter();
2583-
auto scopeInfo =
2584-
std::make_tuple(scope, retiredConstraints.begin());
2585-
scopes.push_back(scopeInfo);
25862566
}
25872567

2588-
/// Restore all of the retired/generated constraints to the state
2589-
/// before given scope. This is required because retired constraints have
2590-
/// to be re-introduced to the system in order of arrival (LIFO) and list
2591-
/// of the generated constraints has to be truncated back to the
2592-
/// original size.
2568+
/// Update statistics when a scope ends.
25932569
///
25942570
/// \param scope The solver scope to rollback.
2595-
void rollback(SolverScope *scope) {
2571+
void endScope(SolverScope *scope) {
25962572
--depth;
25972573

25982574
unsigned countScopesExplored = NumStatesExplored - scope->scopeNumber;
25992575
if (countScopesExplored == 1)
26002576
CS.incrementLeafScopes();
2601-
2602-
SolverScope *savedScope;
2603-
// The position of last retired constraint before given scope.
2604-
ConstraintList::iterator lastRetiredPos;
2605-
2606-
std::tie(savedScope, lastRetiredPos) =
2607-
scopes.pop_back_val();
2608-
2609-
assert(savedScope == scope && "Scope rollback not in LIFO order!");
2610-
2611-
// Restore all of the retired constraints.
2612-
CS.InactiveConstraints.splice(CS.InactiveConstraints.end(),
2613-
retiredConstraints,
2614-
retiredConstraints.begin(), lastRetiredPos);
26152577
}
26162578

26172579
/// Check whether constraint system is allowed to form solutions
@@ -2638,25 +2600,12 @@ class ConstraintSystem {
26382600
}
26392601

26402602
private:
2641-
/// The list of constraints that have been retired along the
2642-
/// current path, this list is used in LIFO fashion when
2643-
/// constraints are added back to the circulation.
2644-
ConstraintList retiredConstraints;
2603+
/// Depth of the solution stack.
2604+
unsigned depth = 0;
26452605

26462606
/// The set of constraints which were active at the time of this state
26472607
/// creating, it's used to re-activate them on destruction.
26482608
SmallVector<Constraint *, 4> activeConstraints;
2649-
2650-
/// The collection which holds association between solver scope
2651-
/// and position of the last retired constraint and number of
2652-
/// constraints generated before registration of given scope,
2653-
/// this helps to rollback all of the constraints retired/generated
2654-
/// each of the registered scopes correct (LIFO) order.
2655-
llvm::SmallVector<
2656-
std::tuple<SolverScope *, ConstraintList::iterator>, 4> scopes;
2657-
2658-
/// Depth of the solution stack.
2659-
unsigned depth = 0;
26602609
};
26612610

26622611
class CacheExprTypes : public ASTWalker {

lib/Sema/CSSolver.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -654,10 +654,6 @@ ConstraintSystem::SolverState::~SolverState() {
654654
if (CS.inInvalidState())
655655
return;
656656

657-
// Make sure that all of the retired constraints have been returned
658-
// to constraint system.
659-
assert(!hasRetiredConstraints());
660-
661657
// Re-activate constraints which were initially marked as "active"
662658
// to restore original state of the constraint system.
663659
for (auto *constraint : activeConstraints) {
@@ -715,7 +711,7 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
715711
numTypeVariables = cs.TypeVariables.size();
716712
numFixes = cs.Fixes.size();
717713

718-
cs.solverState->registerScope(this);
714+
cs.solverState->beginScope(this);
719715
assert(!cs.failedConstraint && "Unexpected failed constraint!");
720716
}
721717

@@ -739,10 +735,7 @@ ConstraintSystem::SolverScope::~SolverScope() {
739735
// Roll back changes to the constraint system.
740736
cs.solverState->Trail.undo(numTrailChanges);
741737

742-
// Rollback all of the changes done to constraints by the current scope,
743-
// e.g. add retired constraints back to the circulation and remove generated
744-
// constraints introduced by the current scope.
745-
cs.solverState->rollback(this);
738+
cs.solverState->endScope(this);
746739

747740
// Clear out other "failed" state.
748741
cs.failedConstraint = nullptr;

lib/Sema/CSTrail.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
520520
cs.InactiveConstraints.erase(iter);
521521
break;
522522
}
523+
524+
case ChangeKind::RetiredConstraint:
525+
cs.InactiveConstraints.push_back(TheConstraint.Constraint);
526+
break;
523527
}
524528
}
525529

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,15 +1508,6 @@ void ConstraintSystem::print(raw_ostream &out) const {
15081508
}
15091509
}
15101510

1511-
if (solverState && solverState->hasRetiredConstraints()) {
1512-
out.indent(indent) << "Retired Constraints:\n";
1513-
solverState->forEachRetired([&](Constraint &constraint) {
1514-
out.indent(indent + 2);
1515-
constraint.print(out, &getASTContext().SourceMgr);
1516-
out << "\n";
1517-
});
1518-
}
1519-
15201511
if (!ResolvedOverloads.empty()) {
15211512
out.indent(indent) << "Resolved overloads:\n";
15221513

0 commit comments

Comments
 (0)