Skip to content

Commit 985a203

Browse files
committed
[Distributed] Add a new access strategy - DispatchToDistributedThunk
This strategy is used to dispatch accesses to 'distributed' computed property to distributed thunk accessor instead of a regular getter when access happen outside actor isolation context.
1 parent b2db4ad commit 985a203

File tree

5 files changed

+46
-31
lines changed

5 files changed

+46
-31
lines changed

include/swift/AST/StorageImpl.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ class AccessStrategy {
111111
/// a Write access back into the storage.
112112
MaterializeToTemporary,
113113

114-
// /// The access is to a computed distributed property, and thus the
115-
// /// get-accessor is a distributed thunk which may perform a remote call.
116-
// DirectToDistributedThunkAccessor,
114+
/// The access is to a computed distributed property, and thus the
115+
/// get-accessor is a distributed thunk which may perform a remote call.
116+
DispatchToDistributedThunk,
117117
};
118118

119119
private:
@@ -153,10 +153,10 @@ class AccessStrategy {
153153
return { dispatched ? DispatchToAccessor : DirectToAccessor, accessor };
154154
}
155155

156-
// static AccessStrategy getDistributedGetAccessor(AccessorKind accessor) {
157-
// assert(accessor == AccessorKind::Get);
158-
// return { DirectToDistributedThunkAccessor, accessor };
159-
// }
156+
static AccessStrategy getDistributedThunkDispatchStrategy() {
157+
return {DispatchToDistributedThunk, AccessorKind::Get};
158+
}
159+
160160
static AccessStrategy getMaterializeToTemporary(AccessStrategy read,
161161
AccessStrategy write) {
162162
return { read, write };
@@ -165,7 +165,8 @@ class AccessStrategy {
165165
Kind getKind() const { return TheKind; }
166166

167167
bool hasAccessor() const {
168-
return TheKind == DirectToAccessor || TheKind == DispatchToAccessor;
168+
return TheKind == DirectToAccessor || TheKind == DispatchToAccessor ||
169+
TheKind == DispatchToDistributedThunk;
169170
}
170171

171172
AccessorKind getAccessor() const {

lib/AST/Decl.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,10 +2283,8 @@ AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
22832283
assert(hasStorage());
22842284
return AccessStrategy::getStorage();
22852285

2286-
// FIXME: For now `DistributedThunk` behaves just like `Ordinary` but it
2287-
// needs a new strategy to be useful.
22882286
case AccessSemantics::DistributedThunk:
2289-
LLVM_FALLTHROUGH;
2287+
return AccessStrategy::getDistributedThunkDispatchStrategy();
22902288

22912289
case AccessSemantics::Ordinary:
22922290
// Skip these checks for local variables, both because they're unnecessary

lib/SILGen/SILGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1833,7 +1833,7 @@ SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
18331833
case AccessStrategy::DirectToAccessor:
18341834
case AccessStrategy::DispatchToAccessor:
18351835
case AccessStrategy::MaterializeToTemporary:
1836-
// case AccessStrategy::DirectToDistributedThunkAccessor:
1836+
case AccessStrategy::DispatchToDistributedThunk:
18371837
return false;
18381838
}
18391839
llvm_unreachable("unhandled strategy");

lib/SILGen/SILGenExpr.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,27 +3504,18 @@ getIdForKeyPathComponentComputedProperty(SILGenModule &SGM,
35043504
// stable identifier.
35053505
return SGM.getFunction(getterRef, NotForDefinition);
35063506
}
3507-
// case AccessStrategy::DirectToDistributedThunkAccessor: {
3508-
// assert(false && "dont need this");
3509-
// // Locate the distributed thunk for the getter of this property
3510-
// fprintf(stderr, "[%s:%d] (%s) CHECKING.... DirectToDistributedThunkAccessor\n", __FILE__, __LINE__, __FUNCTION__);
3511-
// auto representativeDecl = getRepresentativeAccessorForKeyPath(storage);
3512-
// assert(representativeDecl->isDistributed());
3513-
// fprintf(stderr, "[%s:%d] (%s) OK, representative is DIST\n", __FILE__, __LINE__, __FUNCTION__);
3514-
//
3515-
// auto getterThunkRef = SILDeclRef(representativeDecl->getDistributedThunk(),
3516-
// SILDeclRef::Kind::Func,
3517-
// /*isForeign=*/false,
3518-
// /*isDistributed=*/true);
3519-
// fprintf(stderr, "[%s:%d] (%s) THUNK\n", __FILE__, __LINE__, __FUNCTION__);
3520-
// getterThunkRef.dump();
3521-
//
3522-
// return SGM.getFunction(getterThunkRef, NotForDefinition);
3523-
// }
35243507
case AccessStrategy::DispatchToAccessor: {
35253508
// Identify the property by its vtable or wtable slot.
35263509
return SGM.getAccessorDeclRef(getRepresentativeAccessorForKeyPath(storage));
35273510
}
3511+
3512+
case AccessStrategy::DispatchToDistributedThunk: {
3513+
auto thunkRef = SILDeclRef(cast<VarDecl>(storage)->getDistributedThunk(),
3514+
SILDeclRef::Kind::Func,
3515+
/*isForeign=*/false,
3516+
/*isDistributed=*/true);
3517+
return SGM.getFunction(thunkRef, NotForDefinition);
3518+
}
35283519
}
35293520
llvm_unreachable("unhandled access strategy");
35303521
}

lib/SILGen/SILGenLValue.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2693,6 +2693,9 @@ namespace {
26932693
strategy.getWriteStrategy(),
26942694
typeData);
26952695
}
2696+
2697+
case AccessStrategy::DispatchToDistributedThunk:
2698+
return asImpl().emitUsingDistributedThunk();
26962699
}
26972700
llvm_unreachable("unknown kind");
26982701
}
@@ -2898,6 +2901,11 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
28982901
if (address.getType().is<ReferenceStorageType>())
28992902
LV.add<OwnershipComponent>(typeData);
29002903
}
2904+
2905+
void emitUsingDistributedThunk() {
2906+
llvm_unreachable("cannot dispatch non-member var via distributed thunk");
2907+
}
2908+
29012909
} emitter(SGF, loc, var, subs, accessKind, formalRValueType, options,
29022910
actorIso, *this);
29032911

@@ -3193,11 +3201,11 @@ static SGFAccessKind getBaseAccessKind(SILGenModule &SGM,
31933201
}
31943202

31953203
case AccessStrategy::DirectToAccessor:
3196-
case AccessStrategy::DispatchToAccessor: {
3204+
case AccessStrategy::DispatchToAccessor:
3205+
case AccessStrategy::DispatchToDistributedThunk: {
31973206
auto accessor = member->getOpaqueAccessor(strategy.getAccessor());
31983207
return getBaseAccessKindForAccessor(SGM, accessor, baseFormalType);
31993208
}
3200-
32013209
}
32023210
llvm_unreachable("bad access strategy");
32033211
}
@@ -3405,6 +3413,19 @@ void LValue::addMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
34053413
LV.add<OwnershipComponent>(typeData);
34063414
}
34073415
}
3416+
3417+
void emitUsingDistributedThunk() {
3418+
auto *var = cast<VarDecl>(Storage);
3419+
SILDeclRef accessor(var->getDistributedThunk(), SILDeclRef::Kind::Func,
3420+
/*isForeign=*/false, /*isDistributed=*/true);
3421+
3422+
auto typeData = getLogicalStorageTypeData(
3423+
SGF.getTypeExpansionContext(), SGF.SGM, AccessKind, FormalRValueType);
3424+
3425+
asImpl().emitUsingGetterSetter(accessor, /*isDirect=*/false,
3426+
/*isDistributed=*/true, typeData);
3427+
}
3428+
34083429
} emitter(SGF, loc, var, subs, isSuper, accessKind,
34093430
formalRValueType, options, *this,
34103431
/*indices for diags*/ nullptr, /*indices*/ PreparedArguments(),
@@ -3580,6 +3601,10 @@ void LValue::addMemberSubscriptComponent(SILGenFunction &SGF, SILLocation loc,
35803601
void emitUsingStorage(LValueTypeData typeData) {
35813602
llvm_unreachable("subscripts never have storage");
35823603
}
3604+
3605+
void emitUsingDistributedThunk() {
3606+
llvm_unreachable("subscripts cannot be dispatch via distributed thunk");
3607+
}
35833608
} emitter(SGF, loc, decl, subs, isSuper, accessKind, formalRValueType,
35843609
options, *this, argListForDiagnostics, std::move(indices),
35853610
isOnSelfParameter, actorIso);

0 commit comments

Comments
 (0)