Skip to content

Commit ac17292

Browse files
committed
Sema: Record preconcurrency closures in the trail
1 parent b961a7e commit ac17292

File tree

8 files changed

+60
-33
lines changed

8 files changed

+60
-33
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
#define EXPR_CHANGE(Name) CHANGE(Name)
2727
#endif
2828

29+
#ifndef CLOSURE_CHANGE
30+
#define CLOSURE_CHANGE(Name) CHANGE(Name)
31+
#endif
32+
2933
#ifndef LAST_CHANGE
3034
#define LAST_CHANGE(Name)
3135
#endif
@@ -42,6 +46,9 @@ EXPR_CHANGE(AppliedPropertyWrapper)
4246
EXPR_CHANGE(RecordedImpliedResult)
4347
EXPR_CHANGE(RecordedExprPattern)
4448

49+
CLOSURE_CHANGE(RecordedClosureType)
50+
CLOSURE_CHANGE(RecordedPreconcurrencyClosure)
51+
4552
CHANGE(AddedTypeVariable)
4653
CHANGE(AddedConstraint)
4754
CHANGE(RemovedConstraint)
@@ -61,7 +68,6 @@ CHANGE(RecordedKeyPathComponentType)
6168
CHANGE(DisabledConstraint)
6269
CHANGE(FavoredConstraint)
6370
CHANGE(RecordedResultBuilderTransform)
64-
CHANGE(RecordedClosureType)
6571
CHANGE(RecordedContextualInfo)
6672
CHANGE(RecordedTarget)
6773
CHANGE(RecordedCaseLabelItemInfo)
@@ -72,5 +78,6 @@ LAST_CHANGE(RecordedIsolatedParam)
7278

7379
#undef LOCATOR_CHANGE
7480
#undef EXPR_CHANGE
81+
#undef CLOSURE_CHANGE
7582
#undef LAST_CHANGE
7683
#undef CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ class SolverTrail {
141141

142142
#define LOCATOR_CHANGE(Name) static Change Name(ConstraintLocator *locator);
143143
#define EXPR_CHANGE(Name) static Change Name(Expr *expr);
144+
#define CLOSURE_CHANGE(Name) static Change Name(ClosureExpr *closure);
144145
#include "swift/Sema/CSTrail.def"
145146

146147
/// Create a change that added a type variable.
@@ -214,9 +215,6 @@ class SolverTrail {
214215
/// Create a change that recorded a result builder transform.
215216
static Change RecordedResultBuilderTransform(AnyFunctionRef fn);
216217

217-
/// Create a change that recorded a closure type.
218-
static Change RecordedClosureType(ClosureExpr *closure);
219-
220218
/// Create a change that recorded the contextual type of an AST node.
221219
static Change RecordedContextualInfo(ASTNode node);
222220

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@ class Solution {
15981598

15991599
/// The set of closures that have been inferred to be "isolated by
16001600
/// preconcurrency".
1601-
std::vector<const ClosureExpr *> preconcurrencyClosures;
1601+
llvm::DenseSet<const ClosureExpr *> preconcurrencyClosures;
16021602

16031603
/// The set of functions that have been transformed by a result builder.
16041604
llvm::MapVector<AnyFunctionRef, AppliedBuilderTransform>
@@ -2296,7 +2296,7 @@ class ConstraintSystem {
22962296

22972297
/// The set of closures that have been inferred to be "isolated by
22982298
/// preconcurrency".
2299-
llvm::SmallSetVector<const ClosureExpr *, 2> preconcurrencyClosures;
2299+
llvm::SmallDenseSet<const ClosureExpr *, 2> preconcurrencyClosures;
23002300

23012301
/// Maps closure parameters to type variables.
23022302
llvm::DenseMap<const ParamDecl *, TypeVariableType *>
@@ -2855,9 +2855,6 @@ class ConstraintSystem {
28552855
/// FIXME: Remove this.
28562856
unsigned numFixes;
28572857

2858-
/// The length of \c PreconcurrencyClosures.
2859-
unsigned numPreconcurrencyClosures;
2860-
28612858
/// The length of \c ImplicitValueConversions.
28622859
unsigned numImplicitValueConversions;
28632860

@@ -4219,6 +4216,13 @@ class ConstraintSystem {
42194216
/// Undo the above change.
42204217
void removeIsolatedParam(ParamDecl *param);
42214218

4219+
/// Used by the above to update preconcurrencyClosures and record a change in
4220+
/// the trail.
4221+
void recordPreconcurrencyClosure(const ClosureExpr *closure);
4222+
4223+
/// Undo the above change.
4224+
void removePreconcurrencyClosure(const ClosureExpr *closure);
4225+
42224226
/// Given the fact that contextual type is now available for the type
42234227
/// variable representing a pack expansion type, let's resolve the expansion.
42244228
///

lib/Sema/CSSimplify.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11526,6 +11526,23 @@ void ConstraintSystem::removeIsolatedParam(ParamDecl *param) {
1152611526
ASSERT(erased);
1152711527
}
1152811528

11529+
void ConstraintSystem::recordPreconcurrencyClosure(
11530+
const ClosureExpr *closure) {
11531+
bool inserted = preconcurrencyClosures.insert(closure).second;
11532+
ASSERT(inserted);
11533+
11534+
if (solverState) {
11535+
recordChange(SolverTrail::Change::RecordedPreconcurrencyClosure(
11536+
const_cast<ClosureExpr *>(closure)));
11537+
}
11538+
}
11539+
11540+
void ConstraintSystem::removePreconcurrencyClosure(
11541+
const ClosureExpr *closure) {
11542+
bool erased = preconcurrencyClosures.erase(closure);
11543+
ASSERT(erased);
11544+
}
11545+
1152911546
bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1153011547
Type contextualType,
1153111548
ConstraintLocatorBuilder locator) {
@@ -11535,7 +11552,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1153511552

1153611553
// Note if this closure is isolated by preconcurrency.
1153711554
if (hasPreconcurrencyCallee(locator))
11538-
preconcurrencyClosures.insert(closure);
11555+
recordPreconcurrencyClosure(closure);
1153911556

1154011557
// Let's look through all optionals associated with contextual
1154111558
// type to make it possible to infer parameter/result type of

lib/Sema/CSSolver.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ Solution ConstraintSystem::finalize() {
233233
for (const auto &param : isolatedParams)
234234
solution.isolatedParams.insert(param);
235235

236-
for (const auto &closure : preconcurrencyClosures)
237-
solution.preconcurrencyClosures.push_back(closure);
236+
for (auto closure : preconcurrencyClosures)
237+
solution.preconcurrencyClosures.insert(closure);
238238

239239
for (const auto &transformed : resultBuilderTransformed) {
240240
solution.resultBuilderTransformed.insert(transformed);
@@ -416,7 +416,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
416416
}
417417

418418
for (auto closure : solution.preconcurrencyClosures) {
419-
preconcurrencyClosures.insert(closure);
419+
if (preconcurrencyClosures.count(closure) == 0)
420+
recordPreconcurrencyClosure(closure);
420421
}
421422

422423
for (const auto &transformed : solution.resultBuilderTransformed) {
@@ -698,7 +699,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
698699
numTypeVariables = cs.TypeVariables.size();
699700
numFixes = cs.Fixes.size();
700701
numKeyPaths = cs.KeyPaths.size();
701-
numPreconcurrencyClosures = cs.preconcurrencyClosures.size();
702702
numImplicitValueConversions = cs.ImplicitValueConversions.size();
703703
numArgumentLists = cs.ArgumentLists.size();
704704
numImplicitCallAsFunctionRoots = cs.ImplicitCallAsFunctionRoots.size();
@@ -738,9 +738,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
738738
/// Remove any key path expressions.
739739
truncate(cs.KeyPaths, numKeyPaths);
740740

741-
// Remove any preconcurrency closures.
742-
truncate(cs.preconcurrencyClosures, numPreconcurrencyClosures);
743-
744741
// Remove any implicit value conversions.
745742
truncate(cs.ImplicitValueConversions, numImplicitValueConversions);
746743

lib/Sema/CSSyntacticElement.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2581,7 +2581,7 @@ bool ConstraintSystem::applySolution(AnyFunctionRef fn,
25812581
param->setIsolated(true);
25822582
}
25832583

2584-
if (llvm::is_contained(solution.preconcurrencyClosures, closure))
2584+
if (solution.preconcurrencyClosures.count(closure))
25852585
closure->setIsolatedByPreconcurrency();
25862586

25872587
// Coerce the result type, if it was written explicitly.

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ SolverTrail::~SolverTrail() {
6464
result.TheExpr = expr; \
6565
return result; \
6666
}
67+
#define CLOSURE_CHANGE(Name) \
68+
SolverTrail::Change \
69+
SolverTrail::Change::Name(ClosureExpr *closure) { \
70+
Change result; \
71+
result.Kind = ChangeKind::Name; \
72+
result.TheClosure = closure; \
73+
return result; \
74+
}
6775
#include "swift/Sema/CSTrail.def"
6876

6977
SolverTrail::Change
@@ -247,14 +255,6 @@ SolverTrail::Change::RecordedResultBuilderTransform(AnyFunctionRef fn) {
247255
return result;
248256
}
249257

250-
SolverTrail::Change
251-
SolverTrail::Change::RecordedClosureType(ClosureExpr *closure) {
252-
Change result;
253-
result.Kind = ChangeKind::RecordedClosureType;
254-
result.TheClosure = closure;
255-
return result;
256-
}
257-
258258
SolverTrail::Change
259259
SolverTrail::Change::RecordedContextualInfo(ASTNode node) {
260260
Change result;
@@ -505,6 +505,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
505505
case ChangeKind::RecordedIsolatedParam:
506506
cs.removeIsolatedParam(TheParam);
507507
break;
508+
509+
case ChangeKind::RecordedPreconcurrencyClosure:
510+
cs.removePreconcurrencyClosure(TheClosure);
511+
break;
508512
}
509513
}
510514

@@ -530,6 +534,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
530534
simple_display(out, TheExpr); \
531535
out << ")\n"; \
532536
break;
537+
#define CLOSURE_CHANGE(Name) \
538+
case ChangeKind::Name: \
539+
out << "(" << #Name << " "; \
540+
simple_display(out, TheClosure); \
541+
out << ")\n"; \
542+
break;
533543
#include "swift/Sema/CSTrail.def"
534544

535545
case ChangeKind::AddedTypeVariable:
@@ -693,12 +703,6 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
693703
out << ")\n";
694704
break;
695705

696-
case ChangeKind::RecordedClosureType:
697-
out << "(RecordedClosureType ";
698-
simple_display(out, TheClosure);
699-
out << ")\n";
700-
break;
701-
702706
case ChangeKind::RecordedContextualInfo:
703707
// FIXME: Print short form of ASTNode
704708
out << "(RecordedContextualInfo)\n";

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4496,7 +4496,7 @@ size_t Solution::getTotalMemory() const {
44964496
size_in_bytes(caseLabelItems) +
44974497
size_in_bytes(exprPatterns) +
44984498
size_in_bytes(isolatedParams) +
4499-
(preconcurrencyClosures.size() * sizeof(void *)) +
4499+
size_in_bytes(preconcurrencyClosures) +
45004500
size_in_bytes(resultBuilderTransformed) +
45014501
size_in_bytes(appliedPropertyWrappers) +
45024502
size_in_bytes(argumentLists) +

0 commit comments

Comments
 (0)