Skip to content

Commit 8799596

Browse files
committed
Sema: Record disjunction choices in the trail
1 parent 49487b8 commit 8799596

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

include/swift/Sema/CSTrail.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class SolverTrail {
5959
AddedFix,
6060
/// Recorded a fixed requirement.
6161
AddedFixedRequirement,
62+
/// Recorded a disjunction choice.
63+
RecordedDisjunctionChoice,
6264
};
6365

6466
/// A change made to the constraint system.
@@ -122,6 +124,8 @@ class SolverTrail {
122124
GenericTypeParamType *GP;
123125
Type ReqTy;
124126
} FixedRequirement;
127+
128+
ConstraintLocator *Locator;
125129
};
126130

127131
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -169,6 +173,10 @@ class SolverTrail {
169173
unsigned reqKind,
170174
Type requirementTy);
171175

176+
/// Create a change that recorded a disjunction choice.
177+
static Change recordedDisjunctionChoice(ConstraintLocator *locator,
178+
unsigned index);
179+
172180
/// Undo this change, reverting the constraint graph to the state it
173181
/// had prior to this change.
174182
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,7 +2328,7 @@ class ConstraintSystem {
23282328

23292329
/// The set of remembered disjunction choices used to reach
23302330
/// the current constraint system.
2331-
llvm::MapVector<ConstraintLocator *, unsigned> DisjunctionChoices;
2331+
llvm::SmallDenseMap<ConstraintLocator *, unsigned, 4> DisjunctionChoices;
23322332

23332333
/// A map from applied disjunction constraints to the corresponding
23342334
/// argument function type.
@@ -2879,9 +2879,6 @@ class ConstraintSystem {
28792879
/// FIXME: Remove this.
28802880
unsigned numFixes;
28812881

2882-
/// The length of \c DisjunctionChoices.
2883-
unsigned numDisjunctionChoices;
2884-
28852882
/// The length of \c AppliedDisjunctions.
28862883
unsigned numAppliedDisjunctions;
28872884

@@ -5319,15 +5316,16 @@ class ConstraintSystem {
53195316
/// Collect the current inactive disjunction constraints.
53205317
void collectDisjunctions(SmallVectorImpl<Constraint *> &disjunctions);
53215318

5322-
/// Record a particular disjunction choice of
5323-
void recordDisjunctionChoice(ConstraintLocator *disjunctionLocator,
5324-
unsigned index) {
5325-
// We shouldn't ever register disjunction choices multiple times.
5326-
assert(!DisjunctionChoices.count(disjunctionLocator) ||
5327-
DisjunctionChoices[disjunctionLocator] == index);
5328-
DisjunctionChoices.insert({disjunctionLocator, index});
5319+
/// Record a particular disjunction choice and add a change to the trail.
5320+
void recordDisjunctionChoice(ConstraintLocator *locator, unsigned index);
5321+
5322+
/// Undo the above change.
5323+
void removeDisjunctionChoice(ConstraintLocator *locator) {
5324+
bool erased = DisjunctionChoices.erase(locator);
5325+
ASSERT(erased);
53295326
}
53305327

5328+
53315329
/// Filter the set of disjunction terms, keeping only those where the
53325330
/// predicate returns \c true.
53335331
///

lib/Sema/CSSolver.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ void ConstraintSystem::applySolution(const Solution &solution) {
302302

303303
// Register the solution's disjunction choices.
304304
for (auto &choice : solution.DisjunctionChoices) {
305-
DisjunctionChoices.insert(choice);
305+
recordDisjunctionChoice(choice.first, choice.second);
306306
}
307307

308308
// Remember all of the argument/parameter matching choices we made.
@@ -662,7 +662,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
662662

663663
numTypeVariables = cs.TypeVariables.size();
664664
numFixes = cs.Fixes.size();
665-
numDisjunctionChoices = cs.DisjunctionChoices.size();
666665
numAppliedDisjunctions = cs.AppliedDisjunctions.size();
667666
numArgumentMatchingChoices = cs.argumentMatchingChoices.size();
668667
numOpenedTypes = cs.OpenedTypes.size();
@@ -727,9 +726,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
727726
// constraints introduced by the current scope.
728727
cs.solverState->rollback(this);
729728

730-
// Remove any disjunction choices.
731-
truncate(cs.DisjunctionChoices, numDisjunctionChoices);
732-
733729
// Remove any applied disjunctions.
734730
truncate(cs.AppliedDisjunctions, numAppliedDisjunctions);
735731

lib/Sema/CSTrail.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ SolverTrail::Change::addedFixedRequirement(GenericTypeParamType *GP,
151151
return result;
152152
}
153153

154+
SolverTrail::Change
155+
SolverTrail::Change::recordedDisjunctionChoice(ConstraintLocator *locator,
156+
unsigned index) {
157+
Change result;
158+
result.Kind = ChangeKind::RecordedDisjunctionChoice;
159+
result.Locator = locator;
160+
result.Options = index;
161+
return result;
162+
}
163+
154164
void SolverTrail::Change::undo(ConstraintSystem &cs) const {
155165
auto &cg = cs.getConstraintGraph();
156166

@@ -203,6 +213,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
203213
cs.removeFixedRequirement(FixedRequirement.GP, Options,
204214
FixedRequirement.ReqTy);
205215
break;
216+
217+
case ChangeKind::RecordedDisjunctionChoice:
218+
cs.removeDisjunctionChoice(Locator);
219+
break;
206220
}
207221
}
208222

@@ -313,6 +327,13 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
313327
FixedRequirement.ReqTy->print(out, PO);
314328
out << ")\n";
315329
break;
330+
331+
case ChangeKind::RecordedDisjunctionChoice:
332+
out << "(recorded disjunction choice at ";
333+
Locator->dump(&cs.getASTContext().SourceMgr, out);
334+
out << " index ";
335+
out << Options << ")\n";
336+
break;
316337
}
317338
}
318339

lib/Sema/ConstraintSystem.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,23 @@ void ConstraintSystem::removeFix(ConstraintFix *fix) {
293293
Fixes.pop_back();
294294
}
295295

296+
void ConstraintSystem::recordDisjunctionChoice(
297+
ConstraintLocator *locator,
298+
unsigned index) {
299+
// We shouldn't ever register disjunction choices multiple times.
300+
auto inserted = DisjunctionChoices.insert(
301+
std::make_pair(locator, index));
302+
if (!inserted.second) {
303+
ASSERT(inserted.first->second == index);
304+
return;
305+
}
306+
307+
if (isRecordingChanges()) {
308+
recordChange(SolverTrail::Change::recordedDisjunctionChoice(
309+
locator, index));
310+
}
311+
}
312+
296313
/// Retrieve a dynamic result signature for the given declaration.
297314
static std::tuple<char, ObjCSelector, CanType>
298315
getDynamicResultSignature(ValueDecl *decl) {

0 commit comments

Comments
 (0)