Skip to content

Commit 105b6b3

Browse files
committed
Sema: Remove ConstraintGraphScope
1 parent 8ee4eee commit 105b6b3

File tree

8 files changed

+33
-120
lines changed

8 files changed

+33
-120
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class TypeVariableType;
3131
namespace constraints {
3232

3333
class Constraint;
34-
class ConstraintGraphScope;
3534

3635
class SolverTrail {
3736
public:
@@ -134,6 +133,7 @@ class SolverTrail {
134133
void recordChange(Change change);
135134

136135
void dumpActiveScopeChanges(llvm::raw_ostream &out,
136+
unsigned fromIndex,
137137
unsigned indent = 0);
138138

139139
unsigned size() const {
@@ -148,7 +148,7 @@ class SolverTrail {
148148
/// The list of changes made to this constraint system.
149149
std::vector<Change> Changes;
150150

151-
friend class ConstraintGraphScope;
151+
bool UndoActive = false;
152152
};
153153

154154
} // namespace constraints

include/swift/Sema/ConstraintGraph.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ namespace constraints {
4242

4343
class Constraint;
4444
class ConstraintGraph;
45-
class ConstraintGraphScope;
4645
class ConstraintSystem;
4746
class TypeVariableBinding;
4847

@@ -440,11 +439,6 @@ class ConstraintGraph {
440439
/// to contract constraint graph edges.
441440
void incrementConstraintsPerContractionCounter();
442441

443-
/// The currently active scope, or null if we aren't tracking changes made
444-
/// to the constraint graph.
445-
ConstraintGraphScope *ActiveScope = nullptr;
446-
447-
friend class ConstraintGraphScope;
448442
friend class SolverTrail;
449443
};
450444

include/swift/Sema/ConstraintGraphScope.h

Lines changed: 0 additions & 63 deletions
This file was deleted.

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include "swift/Sema/CSTrail.h"
3636
#include "swift/Sema/Constraint.h"
3737
#include "swift/Sema/ConstraintGraph.h"
38-
#include "swift/Sema/ConstraintGraphScope.h"
3938
#include "swift/Sema/ConstraintLocator.h"
4039
#include "swift/Sema/OverloadChoice.h"
4140
#include "swift/Sema/SolutionResult.h"
@@ -2852,8 +2851,8 @@ class ConstraintSystem {
28522851
/// The length of \c TypeVariables.
28532852
unsigned numTypeVariables;
28542853

2855-
/// The length of \c SavedBindings.
2856-
unsigned numSavedBindings;
2854+
/// The length of \c Trail.
2855+
unsigned numTrailChanges;
28572856

28582857
/// The length of \c ConstraintRestrictions.
28592858
unsigned numConstraintRestrictions;
@@ -2958,9 +2957,6 @@ class ConstraintSystem {
29582957
/// The scope number of this scope. Set when the scope is registered.
29592958
unsigned scopeNumber = 0;
29602959

2961-
/// Constraint graph scope associated with this solver scope.
2962-
ConstraintGraphScope CGScope;
2963-
29642960
SolverScope(const SolverScope &) = delete;
29652961
SolverScope &operator=(const SolverScope &) = delete;
29662962

lib/Sema/CSSolver.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,9 @@ ConstraintSystem::SolverState::~SolverState() {
648648
}
649649

650650
ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
651-
: cs(cs), CGScope(cs.CG)
652-
{
651+
: cs(cs) {
652+
numTrailChanges = cs.solverState->Trail.size();
653+
653654
numTypeVariables = cs.TypeVariables.size();
654655
numConstraintRestrictions = cs.ConstraintRestrictions.size();
655656
numFixes = cs.Fixes.size();
@@ -711,6 +712,9 @@ ConstraintSystem::SolverScope::~SolverScope() {
711712
cs.ActiveConstraints);
712713
}
713714

715+
// Roll back changes to the constraint system.
716+
cs.solverState->Trail.undo(numTrailChanges);
717+
714718
// Rollback all of the changes done to constraints by the current scope,
715719
// e.g. add retired constraints back to the circulation and remove generated
716720
// constraints introduced by the current scope.

lib/Sema/CSStep.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,13 +545,16 @@ template <typename P> class BindingStep : public SolverStep {
545545
}
546546

547547
{
548+
auto &trail = CS.solverState->Trail;
549+
unsigned size = trail.size();
550+
548551
auto scope = std::make_unique<Scope>(CS);
549552
if (attempt(*choice)) {
550553
ActiveChoice.emplace(std::move(scope), *choice);
551554

552555
if (CS.isDebugMode()) {
553-
CS.solverState->Trail.dumpActiveScopeChanges(
554-
llvm::errs(), CS.solverState->getCurrentIndent());
556+
trail.dumpActiveScopeChanges(
557+
llvm::errs(), size, CS.solverState->getCurrentIndent());
555558
}
556559

557560
return suspend(std::make_unique<SplitterStep>(CS, Solutions));

lib/Sema/CSTrail.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "swift/Basic/Defer.h"
1919
#include "swift/Basic/Statistic.h"
2020
#include "swift/Sema/ConstraintGraph.h"
21-
#include "swift/Sema/ConstraintGraphScope.h"
2221
#include "swift/Sema/ConstraintSystem.h"
2322
#include "swift/Sema/CSTrail.h"
2423
#include "swift/Basic/Assertions.h"
@@ -102,11 +101,6 @@ SolverTrail::Change::updatedTypeVariable(
102101
void SolverTrail::Change::undo(ConstraintSystem &cs) {
103102
auto &cg = cs.getConstraintGraph();
104103

105-
/// Temporarily change the active scope to null, so we don't record
106-
/// any changes made while performing the undo operation.
107-
llvm::SaveAndRestore<ConstraintGraphScope *> prevActiveScope(cg.ActiveScope,
108-
nullptr);
109-
110104
switch (Kind) {
111105
case ChangeKind::AddedTypeVariable:
112106
cg.removeNode(TypeVar);
@@ -138,33 +132,40 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) {
138132
}
139133

140134
void SolverTrail::recordChange(Change change) {
135+
if (UndoActive)
136+
return;
137+
141138
Changes.push_back(change);
142139
}
143140

144-
void SolverTrail::undo(unsigned upTo) {
141+
void SolverTrail::undo(unsigned toIndex) {
145142
// Don't attempt to rollback if constraint system ended up
146143
// in an invalid state.
147144
if (CS.inInvalidState())
148145
return;
149146

150-
ASSERT(Changes.size() >= upTo && "Trail corrupted");
147+
ASSERT(Changes.size() >= toIndex && "Trail corrupted");
148+
ASSERT(!UndoActive);
149+
UndoActive = true;
151150

152-
for (unsigned i = Changes.size(); i > upTo; i--) {
151+
for (unsigned i = Changes.size(); i > toIndex; i--) {
153152
auto change = Changes[i - 1];
154153
if (change.Kind == ChangeKind::UpdatedTypeVariable)
155154
change.undo(CS);
156155
}
157156

158-
for (unsigned i = Changes.size(); i > upTo; i--) {
157+
for (unsigned i = Changes.size(); i > toIndex; i--) {
159158
auto change = Changes[i - 1];
160159
if (change.Kind != ChangeKind::UpdatedTypeVariable)
161160
change.undo(CS);
162161
}
163162

164-
Changes.resize(upTo);
163+
Changes.resize(toIndex);
164+
UndoActive = false;
165165
}
166166

167167
void SolverTrail::dumpActiveScopeChanges(llvm::raw_ostream &out,
168+
unsigned fromIndex,
168169
unsigned indent) {
169170
if (Changes.empty())
170171
return;
@@ -175,8 +176,7 @@ void SolverTrail::dumpActiveScopeChanges(llvm::raw_ostream &out,
175176
std::vector<TypeVariableType *> equivTypeVars;
176177
std::set<Constraint *> addedConstraints;
177178
std::set<Constraint *> removedConstraints;
178-
for (unsigned int i = CS.getConstraintGraph().ActiveScope->getStartIdx();
179-
i < Changes.size(); i++) {
179+
for (unsigned int i = fromIndex; i < Changes.size(); i++) {
180180
auto change = Changes[i];
181181
switch (change.Kind) {
182182
case ChangeKind::BoundTypeVariable:

lib/Sema/ConstraintGraph.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "swift/Basic/Defer.h"
1919
#include "swift/Basic/Statistic.h"
2020
#include "swift/Sema/ConstraintGraph.h"
21-
#include "swift/Sema/ConstraintGraphScope.h"
2221
#include "swift/Sema/ConstraintSystem.h"
2322
#include "swift/Sema/CSTrail.h"
2423
#include "swift/Basic/Assertions.h"
@@ -69,7 +68,7 @@ ConstraintGraph::lookupNode(TypeVariableType *typeVar) {
6968
TypeVariables.push_back(typeVar);
7069

7170
// Record the change, if there are active scopes.
72-
if (ActiveScope)
71+
if (CS.solverState)
7372
CS.solverState->recordChange(SolverTrail::Change::addedTypeVariable(typeVar));
7473

7574
// If this type variable is not the representative of its equivalence class,
@@ -405,26 +404,6 @@ void ConstraintGraphNode::resetBindingSet() {
405404
}
406405
}
407406

408-
#pragma mark Graph scope management
409-
ConstraintGraphScope::ConstraintGraphScope(ConstraintGraph &CG)
410-
: CG(CG), ParentScope(CG.ActiveScope),
411-
NumChanges(CG.getConstraintSystem().solverState->Trail.size())
412-
{
413-
CG.ActiveScope = this;
414-
}
415-
416-
ConstraintGraphScope::~ConstraintGraphScope() {
417-
// Don't attempt to rollback if constraint system ended up
418-
// in an invalid state.
419-
if (CG.CS.inInvalidState())
420-
return;
421-
422-
CG.CS.solverState->Trail.undo(NumChanges);
423-
424-
// The active scope is now the parent scope.
425-
CG.ActiveScope = ParentScope;
426-
}
427-
428407
#pragma mark Graph mutation
429408

430409
void ConstraintGraph::removeNode(TypeVariableType *typeVar) {
@@ -459,7 +438,7 @@ void ConstraintGraph::addConstraint(Constraint *constraint) {
459438
}
460439

461440
// Record the change, if there are active scopes.
462-
if (ActiveScope)
441+
if (CS.solverState)
463442
CS.solverState->recordChange(SolverTrail::Change::addedConstraint(constraint));
464443
}
465444

@@ -485,7 +464,7 @@ void ConstraintGraph::removeConstraint(Constraint *constraint) {
485464
}
486465

487466
// Record the change, if there are active scopes.
488-
if (ActiveScope)
467+
if (CS.solverState)
489468
CS.solverState->recordChange(SolverTrail::Change::removedConstraint(constraint));
490469
}
491470

@@ -504,7 +483,7 @@ void ConstraintGraph::mergeNodes(TypeVariableType *typeVar1,
504483
auto typeVarNonRep = typeVar1 == typeVarRep? typeVar2 : typeVar1;
505484

506485
// Record the change, if there are active scopes.
507-
if (ActiveScope) {
486+
if (CS.solverState) {
508487
CS.solverState->recordChange(
509488
SolverTrail::Change::extendedEquivalenceClass(
510489
typeVarRep,
@@ -521,7 +500,7 @@ void ConstraintGraph::bindTypeVariable(TypeVariableType *typeVar, Type fixed) {
521500
"Cannot bind to type variable; merge equivalence classes instead");
522501

523502
// Record the change, if there are active scopes.
524-
if (ActiveScope) {
503+
if (CS.solverState) {
525504
CS.solverState->recordChange(
526505
SolverTrail::Change::boundTypeVariable(typeVar, fixed));
527506
}

0 commit comments

Comments
 (0)