Skip to content

Commit b961a7e

Browse files
committed
Sema: Record isolated parameters in the trail
1 parent 1d177d0 commit b961a7e

File tree

8 files changed

+54
-19
lines changed

8 files changed

+54
-19
lines changed

include/swift/Sema/CSTrail.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ CHANGE(RecordedContextualInfo)
6666
CHANGE(RecordedTarget)
6767
CHANGE(RecordedCaseLabelItemInfo)
6868
CHANGE(RecordedPotentialThrowSite)
69+
CHANGE(RecordedIsolatedParam)
6970

70-
LAST_CHANGE(RecordedPotentialThrowSite)
71+
LAST_CHANGE(RecordedIsolatedParam)
7172

7273
#undef LOCATOR_CHANGE
7374
#undef EXPR_CHANGE

include/swift/Sema/CSTrail.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class SolverTrail {
134134
DeclContext *TheDeclContext;
135135
CaseLabelItem *TheItem;
136136
CatchNode TheCatchNode;
137+
ParamDecl *TheParam;
137138
};
138139

139140
Change() : Kind(ChangeKind::AddedTypeVariable), TypeVar(nullptr) { }
@@ -228,6 +229,9 @@ class SolverTrail {
228229
/// Create a change that recorded a potential throw site.
229230
static Change RecordedPotentialThrowSite(CatchNode catchNode);
230231

232+
/// Create a change that recorded an isolated parameter.
233+
static Change RecordedIsolatedParam(ParamDecl *param);
234+
231235
/// Undo this change, reverting the constraint graph to the state it
232236
/// had prior to this change.
233237
///

include/swift/Sema/ConstraintSystem.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,7 +1594,7 @@ class Solution {
15941594
llvm::DenseMap<Expr *, ExprPattern *> exprPatterns;
15951595

15961596
/// The set of parameters that have been inferred to be 'isolated'.
1597-
std::vector<ParamDecl *> isolatedParams;
1597+
llvm::DenseSet<ParamDecl *> isolatedParams;
15981598

15991599
/// The set of closures that have been inferred to be "isolated by
16001600
/// preconcurrency".
@@ -2292,7 +2292,7 @@ class ConstraintSystem {
22922292
llvm::SmallDenseMap<Expr *, ExprPattern *, 2> exprPatterns;
22932293

22942294
/// The set of parameters that have been inferred to be 'isolated'.
2295-
llvm::SmallSetVector<ParamDecl *, 2> isolatedParams;
2295+
llvm::SmallDenseSet<ParamDecl *, 2> isolatedParams;
22962296

22972297
/// The set of closures that have been inferred to be "isolated by
22982298
/// preconcurrency".
@@ -2855,9 +2855,6 @@ class ConstraintSystem {
28552855
/// FIXME: Remove this.
28562856
unsigned numFixes;
28572857

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

@@ -4215,6 +4212,13 @@ class ConstraintSystem {
42154212
bool resolveClosure(TypeVariableType *typeVar, Type contextualType,
42164213
ConstraintLocatorBuilder locator);
42174214

4215+
/// Used by the above to update isolatedParams and record a change in
4216+
/// the trail.
4217+
void recordIsolatedParam(ParamDecl *param);
4218+
4219+
/// Undo the above change.
4220+
void removeIsolatedParam(ParamDecl *param);
4221+
42184222
/// Given the fact that contextual type is now available for the type
42194223
/// variable representing a pack expansion type, let's resolve the expansion.
42204224
///

lib/Sema/CSApply.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8539,11 +8539,9 @@ bool ExprRewriter::isDistributedThunk(ConcreteDeclRef ref, Expr *context) {
85398539
// If this is a method reference on an potentially isolated
85408540
// actor then it cannot be a remote thunk.
85418541
bool isPotentiallyIsolated = isPotentiallyIsolatedActor(
8542-
actor,
8543-
[&](ParamDecl *P) {
8544-
return P->isIsolated() ||
8545-
llvm::is_contained(solution.isolatedParams, P);
8546-
});
8542+
actor, [&](ParamDecl *P) {
8543+
return P->isIsolated() || solution.isolatedParams.count(P);
8544+
});
85478545

85488546
// Adjust the declaration context to the innermost context that is neither
85498547
// a local function nor a closure, so that the actor reference is checked

lib/Sema/CSSimplify.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11513,6 +11513,19 @@ static Type getOpenedResultBuilderTypeFor(ConstraintSystem &cs,
1151311513
return builderType;
1151411514
}
1151511515

11516+
void ConstraintSystem::recordIsolatedParam(ParamDecl *param) {
11517+
bool inserted = isolatedParams.insert(param).second;
11518+
ASSERT(inserted);
11519+
11520+
if (solverState)
11521+
recordChange(SolverTrail::Change::RecordedIsolatedParam(param));
11522+
}
11523+
11524+
void ConstraintSystem::removeIsolatedParam(ParamDecl *param) {
11525+
bool erased = isolatedParams.erase(param);
11526+
ASSERT(erased);
11527+
}
11528+
1151611529
bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1151711530
Type contextualType,
1151811531
ConstraintLocatorBuilder locator) {
@@ -11658,7 +11671,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1165811671

1165911672
// Note when a parameter is inferred to be isolated.
1166011673
if (contextualParam->isIsolated() && !flags.isIsolated() && paramDecl)
11661-
isolatedParams.insert(paramDecl);
11674+
recordIsolatedParam(paramDecl);
1166211675

1166311676
// Carry-over the ownership specifier from the contextual parameter.
1166411677
auto paramOwnership =

lib/Sema/CSSolver.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ Solution ConstraintSystem::finalize() {
231231
solution.exprPatterns.insert(pattern);
232232

233233
for (const auto &param : isolatedParams)
234-
solution.isolatedParams.push_back(param);
234+
solution.isolatedParams.insert(param);
235235

236236
for (const auto &closure : preconcurrencyClosures)
237237
solution.preconcurrencyClosures.push_back(closure);
@@ -406,7 +406,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
406406
}
407407

408408
for (auto param : solution.isolatedParams) {
409-
isolatedParams.insert(param);
409+
if (isolatedParams.count(param) == 0)
410+
recordIsolatedParam(param);
410411
}
411412

412413
for (auto &pair : solution.exprPatterns) {
@@ -697,7 +698,6 @@ ConstraintSystem::SolverScope::SolverScope(ConstraintSystem &cs)
697698
numTypeVariables = cs.TypeVariables.size();
698699
numFixes = cs.Fixes.size();
699700
numKeyPaths = cs.KeyPaths.size();
700-
numIsolatedParams = cs.isolatedParams.size();
701701
numPreconcurrencyClosures = cs.preconcurrencyClosures.size();
702702
numImplicitValueConversions = cs.ImplicitValueConversions.size();
703703
numArgumentLists = cs.ArgumentLists.size();
@@ -738,9 +738,6 @@ ConstraintSystem::SolverScope::~SolverScope() {
738738
/// Remove any key path expressions.
739739
truncate(cs.KeyPaths, numKeyPaths);
740740

741-
// Remove any isolated parameters.
742-
truncate(cs.isolatedParams, numIsolatedParams);
743-
744741
// Remove any preconcurrency closures.
745742
truncate(cs.preconcurrencyClosures, numPreconcurrencyClosures);
746743

lib/Sema/CSTrail.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,14 @@ SolverTrail::Change::RecordedPotentialThrowSite(CatchNode catchNode) {
319319
return result;
320320
}
321321

322+
SolverTrail::Change
323+
SolverTrail::Change::RecordedIsolatedParam(ParamDecl *param) {
324+
Change result;
325+
result.Kind = ChangeKind::RecordedIsolatedParam;
326+
result.TheParam = param;
327+
return result;
328+
}
329+
322330
SyntacticElementTargetKey
323331
SolverTrail::Change::getSyntacticElementTargetKey() const {
324332
ASSERT(Kind == ChangeKind::RecordedTarget);
@@ -493,6 +501,10 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
493501
case ChangeKind::RecordedExprPattern:
494502
cs.removeExprPatternFor(TheExpr);
495503
break;
504+
505+
case ChangeKind::RecordedIsolatedParam:
506+
cs.removeIsolatedParam(TheParam);
507+
break;
496508
}
497509
}
498510

@@ -707,6 +719,12 @@ void SolverTrail::Change::dump(llvm::raw_ostream &out,
707719
// FIXME: Print something here
708720
out << "(RecordedPotentialThrowSite)\n";
709721
break;
722+
723+
case ChangeKind::RecordedIsolatedParam:
724+
out << "(RecordedIsolatedParam ";
725+
TheParam->dumpRef(out);
726+
out << ")\n";
727+
break;
710728
}
711729
}
712730

lib/Sema/ConstraintSystem.cpp

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

0 commit comments

Comments
 (0)