Skip to content

Commit 079bbcf

Browse files
ktosoxedin
authored andcommitted
wip
1 parent a38fc3d commit 079bbcf

17 files changed

+179
-12
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
@@ -4801,6 +4801,9 @@ ERROR(distributed_property_cannot_be_static,none,
48014801
ERROR(distributed_property_can_only_be_computed,none,
48024802
"%0 %1 cannot be 'distributed', only computed properties can",
48034803
(DescriptiveDeclKind, DeclName))
4804+
ERROR(distributed_property_accessor_only_get_can_be_distributed,none,
4805+
"only 'get' accessors may be 'distributed'",
4806+
())
48044807
NOTE(distributed_actor_isolated_property,none,
48054808
"access to %0 %1 is only permitted within distributed actor %2",
48064809
(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
@@ -1764,6 +1764,9 @@ void SILGenModule::visitVarDecl(VarDecl *vd) {
17641764
if (vd->hasStorage())
17651765
addGlobalVariable(vd);
17661766

1767+
fprintf(stderr, "[%s:%d] (%s) VISIT VAR DECL\n", __FILE__, __LINE__, __FUNCTION__);
1768+
vd->dump();
1769+
17671770
vd->visitEmittedAccessors([&](AccessorDecl *accessor) {
17681771
emitFunction(accessor);
17691772
});
@@ -1830,6 +1833,7 @@ SILGenModule::canStorageUseStoredKeyPathComponent(AbstractStorageDecl *decl,
18301833
case AccessStrategy::DirectToAccessor:
18311834
case AccessStrategy::DispatchToAccessor:
18321835
case AccessStrategy::MaterializeToTemporary:
1836+
case AccessStrategy::DirectToDistributedThunkAccessor:
18331837
return false;
18341838
}
18351839
llvm_unreachable("unhandled strategy");

lib/SILGen/SILGenApply.cpp

Lines changed: 13 additions & 3 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
}
@@ -5773,12 +5775,20 @@ RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
57735775
ArgumentSource &&selfValue, bool isSuper,
57745776
bool isDirectUse,
57755777
PreparedArguments &&subscriptIndices,
5776-
SGFContext c, bool isOnSelfParameter) {
5778+
SGFContext c,
5779+
bool isOnSelfParameter,
5780+
bool shouldUseDistributedThunk) {
57775781
// Scope any further writeback just within this operation.
57785782
FormalEvaluationScope writebackScope(*this);
57795783

5784+
auto constant = get;
5785+
if (shouldUseDistributedThunk) {
5786+
get.dump();
5787+
assert(false && "should use dist thunk");
5788+
}
5789+
57805790
Callee getter = emitSpecializedAccessorFunctionRef(
5781-
*this, loc, get, substitutions, selfValue, isSuper, isDirectUse,
5791+
*this, loc, constant, substitutions, selfValue, isSuper, isDirectUse,
57825792
isOnSelfParameter);
57835793
bool hasSelf = (bool)selfValue;
57845794
CanAnyFunctionType accessType = getter.getSubstFormalType();

0 commit comments

Comments
 (0)