Skip to content

Commit febfef9

Browse files
ktosoxedin
authored andcommitted
[Distributed] Skeleton implementation of distributed computed properties
1 parent 5c05da0 commit febfef9

20 files changed

+376
-87
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class ASTMangler : public Mangler {
197197
Type GlobalActorBound,
198198
ModuleDecl *Module);
199199

200-
std::string mangleDistributedThunk(const FuncDecl *thunk);
200+
std::string mangleDistributedThunk(const AbstractFunctionDecl *thunk);
201201

202202
/// Mangle a completion handler block implementation function, used for importing ObjC
203203
/// APIs as async.

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ SIMPLE_DECL_ATTR(_inheritActorContext, InheritActorContext,
664664
// 117 was 'spawn' and is now unused
665665

666666
CONTEXTUAL_SIMPLE_DECL_ATTR(distributed, DistributedActor,
667-
DeclModifier | OnClass | OnFunc | OnVar |
667+
DeclModifier | OnClass | OnFunc | OnAccessor | OnVar |
668668
ABIBreakingToAdd | ABIBreakingToRemove |
669669
APIBreakingToAdd | APIBreakingToRemove,
670670
118)

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4804,6 +4804,9 @@ ERROR(distributed_property_cannot_be_static,none,
48044804
ERROR(distributed_property_can_only_be_computed,none,
48054805
"%0 %1 cannot be 'distributed', only computed properties can",
48064806
(DescriptiveDeclKind, DeclName))
4807+
ERROR(distributed_property_accessor_only_get_can_be_distributed,none,
4808+
"only 'get' accessors may be 'distributed'",
4809+
())
48074810
NOTE(distributed_actor_isolated_property,none,
48084811
"access to %0 %1 is only permitted within distributed actor %2",
48094812
(DescriptiveDeclKind, DeclName, DeclName))

include/swift/AST/Expr.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
155155

156156
SWIFT_INLINE_BITFIELD_EMPTY(LiteralExpr, Expr);
157157
SWIFT_INLINE_BITFIELD_EMPTY(IdentityExpr, Expr);
158-
SWIFT_INLINE_BITFIELD(LookupExpr, Expr, 1+1+1,
158+
SWIFT_INLINE_BITFIELD(LookupExpr, Expr, 1+1+1+1,
159159
IsSuper : 1,
160160
IsImplicitlyAsync : 1,
161-
IsImplicitlyThrows : 1
161+
IsImplicitlyThrows : 1,
162+
ShouldApplyDistributedThunk: 1
162163
);
163164
SWIFT_INLINE_BITFIELD_EMPTY(DynamicLookupExpr, LookupExpr);
164165

@@ -1655,6 +1656,18 @@ class LookupExpr : public Expr {
16551656
Bits.LookupExpr.IsImplicitlyThrows = isImplicitlyThrows;
16561657
}
16571658

1659+
/// Informs IRGen to that this expression should be applied as its distributed
1660+
/// thunk, rather than invoking the function directly.
1661+
///
1662+
/// Only intended to be set on distributed get-only computed properties.
1663+
bool shouldApplyLookupDistributedThunk() const {
1664+
return Bits.LookupExpr.ShouldApplyDistributedThunk;
1665+
}
1666+
1667+
void setShouldApplyLookupDistributedThunk(bool flag) {
1668+
Bits.LookupExpr.ShouldApplyDistributedThunk = flag;
1669+
}
1670+
16581671
static bool classof(const Expr *E) {
16591672
return E->getKind() >= ExprKind::First_LookupExpr &&
16601673
E->getKind() <= ExprKind::Last_LookupExpr;

include/swift/AST/StorageImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ class AccessStrategy {
110110
/// separately performing a Read into a temporary variable followed by
111111
/// a Write access back into the storage.
112112
MaterializeToTemporary,
113+
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,
113117
};
114118

115119
private:
@@ -149,6 +153,10 @@ class AccessStrategy {
149153
return { dispatched ? DispatchToAccessor : DirectToAccessor, accessor };
150154
}
151155

156+
// static AccessStrategy getDistributedGetAccessor(AccessorKind accessor) {
157+
// assert(accessor == AccessorKind::Get);
158+
// return { DirectToDistributedThunkAccessor, accessor };
159+
// }
152160
static AccessStrategy getMaterializeToTemporary(AccessStrategy read,
153161
AccessStrategy write) {
154162
return { read, write };

lib/AST/ASTDumper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ namespace {
745745

746746
void visitVarDecl(VarDecl *VD) {
747747
printCommon(VD, "var_decl");
748+
if (VD->isDistributed())
749+
PrintWithColorRAII(OS, DeclModifierColor) << " distributed";
748750
if (VD->isLet())
749751
PrintWithColorRAII(OS, DeclModifierColor) << " let";
750752
if (VD->getAttrs().hasAttribute<LazyAttr>())
@@ -2033,6 +2035,9 @@ class PrintExpr : public ExprVisitor<PrintExpr> {
20332035

20342036
void visitMemberRefExpr(MemberRefExpr *E) {
20352037
printCommon(E, "member_ref_expr");
2038+
if (E->shouldApplyLookupDistributedThunk()) {
2039+
OS << " distributed";
2040+
}
20362041
PrintWithColorRAII(OS, DeclColor) << " decl=";
20372042
printDeclRef(E->getMember());
20382043
if (E->getAccessSemantics() != AccessSemantics::Ordinary)

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3486,7 +3486,7 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
34863486
return finalize();
34873487
}
34883488

3489-
std::string ASTMangler::mangleDistributedThunk(const FuncDecl *thunk) {
3489+
std::string ASTMangler::mangleDistributedThunk(const AbstractFunctionDecl *thunk) {
34903490
// Marker protocols cannot be checked at runtime, so there is no point
34913491
// in recording them for distributed thunks.
34923492
llvm::SaveAndRestore<bool> savedAllowMarkerProtocols(AllowMarkerProtocols,

lib/AST/Decl.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,17 @@ getDirectReadAccessStrategy(const AbstractStorageDecl *storage) {
21512151
llvm_unreachable("bad impl kind");
21522152
}
21532153

2154+
//static AccessStrategy
2155+
//getDirectToDistributedThunkAccessorStrategy(const AbstractStorageDecl *storage) {
2156+
// switch (storage->getReadImpl()) {
2157+
// case ReadImplKind::Get:
2158+
// return AccessStrategy::getDistributedGetAccessor(AccessorKind::Get);
2159+
// default:
2160+
// llvm_unreachable("bad impl kind for distributed property accessor");
2161+
// }
2162+
// llvm_unreachable("bad impl kind for distributed property accessor");
2163+
//}
2164+
21542165
static AccessStrategy
21552166
getDirectWriteAccessStrategy(const AbstractStorageDecl *storage) {
21562167
switch (storage->getWriteImpl()) {
@@ -2284,6 +2295,14 @@ AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
22842295
if (shouldUseNativeDynamicDispatch())
22852296
return getOpaqueAccessStrategy(this, accessKind, /*dispatch*/ false);
22862297

2298+
// if (auto var = dyn_cast<VarDecl>(this)) {
2299+
// if (var->isDistributed()) {
2300+
// fprintf(stderr, "[%s:%d] (%s) DIST STRATEGY!!\n", __FILE__, __LINE__, __FUNCTION__);
2301+
// var->dump();
2302+
// return getDirectToDistributedThunkAccessorStrategy(this);
2303+
// }
2304+
// }
2305+
22872306
// If the storage is resilient from the given module and resilience
22882307
// expansion, we cannot use direct access.
22892308
//

lib/SILGen/SILGen.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,9 @@ void SILGenModule::visitVarDecl(VarDecl *vd) {
17731773
if (vd->hasStorage())
17741774
addGlobalVariable(vd);
17751775

1776+
fprintf(stderr, "[%s:%d] (%s) VISIT VAR DECL\n", __FILE__, __LINE__, __FUNCTION__);
1777+
vd->dump();
1778+
17761779
vd->visitEmittedAccessors([&](AccessorDecl *accessor) {
17771780
emitFunction(accessor);
17781781
});
@@ -1839,6 +1842,7 @@ SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
18391842
case AccessStrategy::DirectToAccessor:
18401843
case AccessStrategy::DispatchToAccessor:
18411844
case AccessStrategy::MaterializeToTemporary:
1845+
// case AccessStrategy::DirectToDistributedThunkAccessor:
18421846
return false;
18431847
}
18441848
llvm_unreachable("unhandled strategy");

lib/SILGen/SILGenApply.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
10901090

10911091
// If this is a direct reference to a vardecl, just emit its value directly.
10921092
// Recursive references to callable declarations are allowed.
1093-
if (isa<VarDecl>(e->getDecl())) {
1093+
if (auto var = dyn_cast<VarDecl>(e->getDecl())) {
10941094
visitExpr(e);
10951095
return;
10961096
}
@@ -1126,6 +1126,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
11261126

11271127
/// Some special handling may be necessary for thunks:
11281128
if (callSite && callSite->shouldApplyDistributedThunk()) {
1129+
fprintf(stderr, "[%s:%d] (%s) SHOULD APPLY DIST THUNK\n", __FILE__, __LINE__, __FUNCTION__);
1130+
11291131
if (auto distributedThunk = cast<AbstractFunctionDecl>(e->getDecl())->getDistributedThunk()) {
11301132
constant = SILDeclRef(distributedThunk).asDistributed();
11311133
}
@@ -5375,6 +5377,7 @@ static Callee getBaseAccessorFunctionRef(SILGenFunction &SGF,
53755377
ArgumentSource &selfValue,
53765378
bool isSuper,
53775379
bool isDirectUse,
5380+
bool isDistributed,
53785381
SubstitutionMap subs,
53795382
bool isOnSelfParameter) {
53805383
auto *decl = cast<AbstractFunctionDecl>(constant.getDecl());
@@ -5454,12 +5457,13 @@ emitSpecializedAccessorFunctionRef(SILGenFunction &SGF,
54545457
ArgumentSource &selfValue,
54555458
bool isSuper,
54565459
bool isDirectUse,
5460+
bool isDistributed,
54575461
bool isOnSelfParameter)
54585462
{
54595463
// Get the accessor function. The type will be a polymorphic function if
54605464
// the Self type is generic.
54615465
Callee callee = getBaseAccessorFunctionRef(SGF, loc, constant, selfValue,
5462-
isSuper, isDirectUse,
5466+
isSuper, isDirectUse, isDistributed,
54635467
substitutions, isOnSelfParameter);
54645468

54655469
// Collect captures if the accessor has them.
@@ -5771,15 +5775,23 @@ SILDeclRef SILGenModule::getAccessorDeclRef(AccessorDecl *accessor) {
57715775
RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
57725776
SubstitutionMap substitutions,
57735777
ArgumentSource &&selfValue, bool isSuper,
5774-
bool isDirectUse,
5778+
bool isDirectUse, bool isDistributed,
57755779
PreparedArguments &&subscriptIndices,
5776-
SGFContext c, bool isOnSelfParameter) {
5780+
SGFContext c,
5781+
bool isOnSelfParameter) {
57775782
// Scope any further writeback just within this operation.
57785783
FormalEvaluationScope writebackScope(*this);
57795784

5785+
auto constant = get;
5786+
5787+
// if (isDistributed) {
5788+
// get.dump();
5789+
// assert(false && "should use dist thunk");
5790+
// }
5791+
57805792
Callee getter = emitSpecializedAccessorFunctionRef(
5781-
*this, loc, get, substitutions, selfValue, isSuper, isDirectUse,
5782-
isOnSelfParameter);
5793+
*this, loc, constant, substitutions, selfValue, isSuper, isDirectUse,
5794+
isDistributed, isOnSelfParameter);
57835795
bool hasSelf = (bool)selfValue;
57845796
CanAnyFunctionType accessType = getter.getSubstFormalType();
57855797

@@ -5812,7 +5824,7 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
58125824

58135825
Callee setter = emitSpecializedAccessorFunctionRef(
58145826
*this, loc, set, substitutions, selfValue, isSuper, isDirectUse,
5815-
isOnSelfParameter);
5827+
/*isDistributed=*/false, isOnSelfParameter);
58165828
bool hasSelf = (bool)selfValue;
58175829
CanAnyFunctionType accessType = setter.getSubstFormalType();
58185830

@@ -5847,14 +5859,14 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
58475859
ManagedValue SILGenFunction::emitAddressorAccessor(
58485860
SILLocation loc, SILDeclRef addressor, SubstitutionMap substitutions,
58495861
ArgumentSource &&selfValue, bool isSuper, bool isDirectUse,
5850-
PreparedArguments &&subscriptIndices, SILType addressType,
5851-
bool isOnSelfParameter) {
5862+
bool isDistributed, PreparedArguments &&subscriptIndices,
5863+
SILType addressType, bool isOnSelfParameter) {
58525864
// Scope any further writeback just within this operation.
58535865
FormalEvaluationScope writebackScope(*this);
58545866

58555867
Callee callee = emitSpecializedAccessorFunctionRef(
58565868
*this, loc, addressor, substitutions, selfValue, isSuper, isDirectUse,
5857-
isOnSelfParameter);
5869+
isDistributed, isOnSelfParameter);
58585870
bool hasSelf = (bool)selfValue;
58595871
CanAnyFunctionType accessType = callee.getSubstFormalType();
58605872

@@ -5909,7 +5921,9 @@ SILGenFunction::emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,
59095921
Callee callee =
59105922
emitSpecializedAccessorFunctionRef(*this, loc, accessor,
59115923
substitutions, selfValue,
5912-
isSuper, isDirectUse, isOnSelfParameter);
5924+
isSuper, isDirectUse,
5925+
/*isDistributed=*/false,
5926+
isOnSelfParameter);
59135927

59145928
// We're already in a full formal-evaluation scope.
59155929
// Make a dead writeback scope; applyCoroutine won't try to pop this.

0 commit comments

Comments
 (0)