Skip to content

Commit 3e228bd

Browse files
authored
Merge pull request #41056 from ktoso/wip-roundtrip
2 parents 9eb9df8 + 6a0164d commit 3e228bd

File tree

41 files changed

+581
-1475
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+581
-1475
lines changed

include/swift/AST/ASTContext.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -670,38 +670,38 @@ class ASTContext final {
670670
NominalTypeDecl *actorOrSystem,
671671
bool isVoidReturn) const;
672672

673+
/// Retrieve the declaration of DistributedActorSystem.make().
674+
///
675+
/// \param actorOrSystem distributed actor or actor system to get the
676+
/// remoteCall function for. Since the method we're looking for is an ad-hoc
677+
/// requirement, a specific type MUST be passed here as it is not possible
678+
/// to obtain the decl from just the `DistributedActorSystem` protocol type.
679+
FuncDecl *getMakeInvocationEncoderOnDistributedActorSystem(
680+
NominalTypeDecl *actorOrSystem) const;
681+
673682
// Retrieve the declaration of DistributedInvocationEncoder.recordArgument(_:).
674683
//
675684
// \param nominal optionally provide a 'NominalTypeDecl' from which the
676685
// function decl shall be extracted. This is useful to avoid witness calls
677686
// through the protocol which is looked up when nominal is null.
678687
FuncDecl *getRecordArgumentOnDistributedInvocationEncoder(
679-
NominalTypeDecl *nominal = nullptr) const;
688+
NominalTypeDecl *nominal) const;
680689

681-
// Retrieve the declaration of DistributedInvocationEncoder.recordErrorType().
682-
//
683-
// \param nominal optionally provide a 'NominalTypeDecl' from which the
684-
// function decl shall be extracted. This is useful to avoid witness calls
685-
// through the protocol which is looked up when nominal is null.
690+
// Retrieve the declaration of DistributedInvocationEncoder.recordErrorType(_:).
686691
FuncDecl *getRecordErrorTypeOnDistributedInvocationEncoder(
687-
NominalTypeDecl *nominal = nullptr) const;
692+
NominalTypeDecl *nominal) const;
688693

689-
// Retrieve the declaration of DistributedInvocationEncoder.recordReturnType().
690-
//
691-
// \param nominal optionally provide a 'NominalTypeDecl' from which the
692-
// function decl shall be extracted. This is useful to avoid witness calls
693-
// through the protocol which is looked up when nominal is null.
694+
// Retrieve the declaration of DistributedInvocationEncoder.recordReturnType(_:).
694695
FuncDecl *getRecordReturnTypeOnDistributedInvocationEncoder(
695-
NominalTypeDecl *nominal = nullptr) const;
696+
NominalTypeDecl *nominal) const;
696697

697698
// Retrieve the declaration of DistributedInvocationEncoder.doneRecording().
698699
//
699700
// \param nominal optionally provide a 'NominalTypeDecl' from which the
700701
// function decl shall be extracted. This is useful to avoid witness calls
701702
// through the protocol which is looked up when nominal is null.
702703
FuncDecl *getDoneRecordingOnDistributedInvocationEncoder(
703-
NominalTypeDecl *nominal = nullptr) const;
704-
704+
NominalTypeDecl *nominal) const;
705705

706706
/// Look for the declaration with the given name within the
707707
/// passed in module.

include/swift/AST/Decl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3425,9 +3425,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
34253425
/// Find, or potentially synthesize, the implicit 'id' property of this actor.
34263426
VarDecl *getDistributedActorIDProperty() const;
34273427

3428-
/// Find the 'makeInvocation' function.
3429-
AbstractFunctionDecl* getDistributedActorSystemMakeInvocationEncoderFunction() const;
3430-
34313428
/// Find the 'RemoteCallTarget.init(_mangledName:)' initializer function
34323429
ConstructorDecl* getDistributedRemoteCallTargetInitFunction() const;
34333430

lib/AST/ASTContext.cpp

Lines changed: 49 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,6 @@ struct ASTContext::Implementation {
267267
/// -> Builtin.Int1
268268
FuncDecl *IsOSVersionAtLeastDecl = nullptr;
269269

270-
/// func recordArgument(_:) throws
271-
FuncDecl *RecordArgumentDistributedInvocationEncoderDecl = nullptr;
272-
273-
/// func recordErrorType(_:) throws
274-
FuncDecl *RecordErrorTypeDistributedInvocationEncoderDecl = nullptr;
275-
276-
/// func recordReturnType(_:) throws
277-
FuncDecl *RecordReturnTypeDistributedInvocationEncoderDecl = nullptr;
278-
279-
/// func doneRecording() throws
280-
FuncDecl *DoneRecordingDistributedInvocationEncoderDecl = nullptr;
281-
282270
/// The set of known protocols, lazily populated as needed.
283271
ProtocolDecl *KnownProtocols[NumKnownProtocols] = { };
284272

@@ -1296,7 +1284,7 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
12961284
NominalTypeDecl *actorOrSystem, bool isVoidReturn) const {
12971285
assert(actorOrSystem && "distributed actor (or system) decl must be provided");
12981286
const NominalTypeDecl *system = actorOrSystem;
1299-
if (actorOrSystem && actorOrSystem->isDistributedActor()) {
1287+
if (actorOrSystem->isDistributedActor()) {
13001288
auto var = actorOrSystem->getDistributedActorSystemProperty();
13011289
system = var->getInterfaceType()->getAnyNominal();
13021290
}
@@ -1311,113 +1299,99 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
13111299
nullptr);
13121300
}
13131301

1314-
FuncDecl *ASTContext::getRecordArgumentOnDistributedInvocationEncoder(
1315-
NominalTypeDecl *nominal) const {
1316-
if (getImpl().RecordArgumentDistributedInvocationEncoderDecl) {
1317-
return getImpl().RecordArgumentDistributedInvocationEncoderDecl;
1302+
FuncDecl *ASTContext::getMakeInvocationEncoderOnDistributedActorSystem(
1303+
NominalTypeDecl *actorOrSystem) const {
1304+
NominalTypeDecl *system = actorOrSystem;
1305+
assert(actorOrSystem && "distributed actor (or system) decl must be provided");
1306+
if (actorOrSystem->isDistributedActor()) {
1307+
auto var = actorOrSystem->getDistributedActorSystemProperty();
1308+
system = var->getInterfaceType()->getAnyNominal();
13181309
}
13191310

1320-
NominalTypeDecl *encoderProto = nominal ?
1321-
nominal :
1322-
getProtocol(KnownProtocolKind::DistributedTargetInvocationEncoder);
1323-
assert(encoderProto && "Missing DistributedTargetInvocationEncoder protocol");
1324-
for (auto result : encoderProto->lookupDirect(Id_recordArgument)) {
1311+
for (auto result : system->lookupDirect(Id_makeInvocationEncoder)) {
13251312
auto *fd = dyn_cast<FuncDecl>(result);
13261313
if (!fd)
13271314
continue;
1315+
if (fd->getParameters()->size() != 0)
1316+
continue;
1317+
if (fd->hasAsync())
1318+
continue;
1319+
if (fd->hasThrows())
1320+
continue;
1321+
// TODO(distributed): more checks, return type etc
13281322

1323+
return fd;
1324+
}
1325+
1326+
return nullptr;
1327+
}
1328+
1329+
FuncDecl *ASTContext::getRecordArgumentOnDistributedInvocationEncoder(
1330+
NominalTypeDecl *nominal) const {
1331+
for (auto result : nominal->lookupDirect(Id_recordArgument)) {
1332+
auto *fd = dyn_cast<FuncDecl>(result);
1333+
if (!fd)
1334+
continue;
13291335
if (fd->getParameters()->size() != 1)
13301336
continue;
1331-
1337+
if (fd->hasAsync())
1338+
continue;
1339+
if (!fd->hasThrows())
1340+
continue;
13321341
// TODO(distributed): more checks
13331342

1334-
if (fd->getResultInterfaceType()->isVoid() &&
1335-
fd->hasThrows() &&
1336-
!fd->hasAsync()) {
1337-
getImpl().RecordArgumentDistributedInvocationEncoderDecl = fd;
1343+
if (fd->getResultInterfaceType()->isVoid())
13381344
return fd;
1339-
}
13401345
}
13411346

13421347
return nullptr;
13431348
}
13441349

13451350
FuncDecl *ASTContext::getRecordErrorTypeOnDistributedInvocationEncoder(
13461351
NominalTypeDecl *nominal) const {
1347-
if (getImpl().RecordErrorTypeDistributedInvocationEncoderDecl) {
1348-
return getImpl().RecordErrorTypeDistributedInvocationEncoderDecl;
1349-
}
1350-
1351-
NominalTypeDecl *encoderProto =
1352-
nominal
1353-
? nominal
1354-
: getProtocol(KnownProtocolKind::DistributedTargetInvocationEncoder);
1355-
assert(encoderProto && "Missing DistributedTargetInvocationEncoder protocol");
1356-
for (auto result : encoderProto->lookupDirect(Id_recordErrorType)) {
1352+
for (auto result : nominal->lookupDirect(Id_recordErrorType)) {
13571353
auto *fd = dyn_cast<FuncDecl>(result);
13581354
if (!fd)
13591355
continue;
1360-
13611356
if (fd->getParameters()->size() != 1)
13621357
continue;
1358+
if (fd->hasAsync())
1359+
continue;
1360+
if (!fd->hasThrows())
1361+
continue;
1362+
// TODO(distributed): more checks
13631363

1364-
// TODO(distributed): more checks that the arg type matches (!!!)
1365-
1366-
if (fd->getResultInterfaceType()->isVoid() &&
1367-
fd->hasThrows() &&
1368-
!fd->hasAsync()) {
1369-
getImpl().RecordErrorTypeDistributedInvocationEncoderDecl = fd;
1364+
if (fd->getResultInterfaceType()->isVoid())
13701365
return fd;
1371-
}
13721366
}
13731367

13741368
return nullptr;
13751369
}
13761370

13771371
FuncDecl *ASTContext::getRecordReturnTypeOnDistributedInvocationEncoder(
13781372
NominalTypeDecl *nominal) const {
1379-
if (getImpl().RecordReturnTypeDistributedInvocationEncoderDecl) {
1380-
return getImpl().RecordReturnTypeDistributedInvocationEncoderDecl;
1381-
}
1382-
1383-
NominalTypeDecl *encoderProto =
1384-
nominal
1385-
? nominal
1386-
: getProtocol(KnownProtocolKind::DistributedTargetInvocationEncoder);
1387-
assert(encoderProto && "Missing DistributedTargetInvocationEncoder protocol");
1388-
for (auto result : encoderProto->lookupDirect(Id_recordReturnType)) {
1373+
for (auto result : nominal->lookupDirect(Id_recordReturnType)) {
13891374
auto *fd = dyn_cast<FuncDecl>(result);
13901375
if (!fd)
13911376
continue;
1392-
13931377
if (fd->getParameters()->size() != 1)
13941378
continue;
1379+
if (fd->hasAsync())
1380+
continue;
1381+
if (!fd->hasThrows())
1382+
continue;
1383+
// TODO(distributed): more checks
13951384

1396-
// TODO(distributed): more checks that the arg type matches (!!!)
1397-
1398-
if (fd->getResultInterfaceType()->isVoid() &&
1399-
fd->hasThrows() &&
1400-
!fd->hasAsync()) {
1401-
getImpl().RecordReturnTypeDistributedInvocationEncoderDecl = fd;
1385+
if (fd->getResultInterfaceType()->isVoid())
14021386
return fd;
1403-
}
14041387
}
14051388

14061389
return nullptr;
14071390
}
14081391

14091392
FuncDecl *ASTContext::getDoneRecordingOnDistributedInvocationEncoder(
14101393
NominalTypeDecl *nominal) const {
1411-
if (getImpl().DoneRecordingDistributedInvocationEncoderDecl) {
1412-
return getImpl().DoneRecordingDistributedInvocationEncoderDecl;
1413-
}
1414-
1415-
NominalTypeDecl *encoderProto =
1416-
nominal
1417-
? nominal
1418-
: getProtocol(KnownProtocolKind::DistributedTargetInvocationEncoder);
1419-
assert(encoderProto && "Missing DistributedTargetInvocationEncoder protocol");
1420-
for (auto result : encoderProto->lookupDirect(Id_doneRecording)) {
1394+
for (auto result : nominal->lookupDirect(Id_doneRecording)) {
14211395
auto *fd = dyn_cast<FuncDecl>(result);
14221396
if (!fd)
14231397
continue;
@@ -1427,10 +1401,8 @@ FuncDecl *ASTContext::getDoneRecordingOnDistributedInvocationEncoder(
14271401

14281402
if (fd->getResultInterfaceType()->isVoid() &&
14291403
fd->hasThrows() &&
1430-
!fd->hasAsync()) {
1431-
getImpl().DoneRecordingDistributedInvocationEncoderDecl = fd;
1404+
!fd->hasAsync())
14321405
return fd;
1433-
}
14341406
}
14351407

14361408
return nullptr;

lib/AST/Decl.cpp

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7428,17 +7428,14 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)
74287428
auto callId = isVoidReturn ? C.Id_remoteCallVoid : C.Id_remoteCall;
74297429

74307430
// Check the name
7431-
if (this->getBaseName() != callId)
7431+
if (getBaseName() != callId)
74327432
return false;
74337433

7434-
auto params = this->getParameters();
7434+
auto params = getParameters();
7435+
unsigned int expectedParamNum = isVoidReturn ? 4 : 5;
74357436

7436-
// Check the expected argument count
7437-
// - for value returning remoteCall:
7438-
if (!params || (!isVoidReturn && params->size() != 5))
7439-
return false;
7440-
// - for void returning remoteCallVoid:
7441-
if (!params || (isVoidReturn && params->size() != 4))
7437+
// Check the expected argument count:
7438+
if (!params || params->size() != expectedParamNum)
74427439
return false;
74437440

74447441
// Check API names of the arguments
@@ -7448,7 +7445,7 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)
74487445
auto thrownTypeParam = params->get(3);
74497446
if (actorParam->getArgumentName() != C.Id_on ||
74507447
targetParam->getArgumentName() != C.Id_target ||
7451-
invocationParam->getArgumentName() != C.Id_invocationDecoder ||
7448+
invocationParam->getArgumentName() != C.Id_invocation ||
74527449
thrownTypeParam->getArgumentName() != C.Id_throwing)
74537450
return false;
74547451

@@ -7458,41 +7455,34 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)
74587455
return false;
74597456
}
74607457

7461-
// FIXME(distributed): check the right types of the args and generics...
7462-
// FIXME(distributed): check access level actually is ok, i.e. not private etc
7463-
7464-
return true;
7465-
}
7458+
if (!isGeneric())
7459+
return false;
74667460

7467-
bool AbstractFunctionDecl::isDistributed() const {
7468-
return this->getAttrs().hasAttribute<DistributedActorAttr>();
7469-
}
7461+
auto genericParams = getGenericParams();
7462+
unsigned int expectedGenericParamNum = isVoidReturn ? 2 : 3;
74707463

7471-
AbstractFunctionDecl*
7472-
NominalTypeDecl::getDistributedActorSystemMakeInvocationEncoderFunction() const {
7473-
auto &C = this->getASTContext();
7474-
NominalTypeDecl *system = const_cast<NominalTypeDecl *>(this);
7475-
if (this->isDistributedActor()) {
7476-
auto var = this->getDistributedActorSystemProperty();
7477-
system = var->getInterfaceType()->getAnyNominal();
7464+
// We expect: Act, Err, Res?
7465+
if (genericParams->size() != expectedGenericParamNum) {
7466+
return false;
74787467
}
74797468

7480-
// FIXME(distributed): implement more properly...
7481-
for (auto value : system->lookupDirect(C.Id_makeInvocationEncoder)) {
7482-
auto func = dyn_cast<AbstractFunctionDecl>(value);
7483-
if (!func)
7484-
continue;
7469+
// FIXME(distributed): check the exact generic requirements
74857470

7486-
if (func->getParameters()->size() != 0)
7487-
continue;
7471+
// === check the return type
7472+
if (isVoidReturn) {
7473+
if (auto func = dyn_cast<FuncDecl>(this))
7474+
if (!func->getResultInterfaceType()->isVoid())
7475+
return false;
7476+
}
74887477

7489-
// TODO(distriuted): return type must conform to our expected protocol
7478+
// FIXME(distributed): check the right types of the args and generics...
7479+
// FIXME(distributed): check access level actually is ok, i.e. not private etc
74907480

7491-
return func;
7492-
}
7481+
return true;
7482+
}
74937483

7494-
// TODO(distributed): make a Request for it?
7495-
return nullptr;
7484+
bool AbstractFunctionDecl::isDistributed() const {
7485+
return getAttrs().hasAttribute<DistributedActorAttr>();
74967486
}
74977487

74987488
ConstructorDecl*

lib/SILGen/SILGenDistributed.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,8 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
724724

725725
// === `InvocationEncoder` types
726726
AbstractFunctionDecl *makeInvocationEncoderFnDecl =
727-
selfTyDecl->getDistributedActorSystemMakeInvocationEncoderFunction();
728-
assert(makeInvocationEncoderFnDecl && "no remoteCall func found!");
727+
ctx.getMakeInvocationEncoderOnDistributedActorSystem(selfTyDecl);
728+
assert(makeInvocationEncoderFnDecl && "no 'makeInvocationEncoder' func found!");
729729
auto makeInvocationEncoderFnRef = SILDeclRef(makeInvocationEncoderFnDecl);
730730

731731
ProtocolDecl *invocationEncoderProto =
@@ -882,7 +882,7 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
882882

883883
// function_ref FakeActorSystem.makeInvocationEncoder()
884884
// %19 = function_ref @$s27FakeDistributedActorSystems0aC6SystemV21makeInvocationEncoderAA0aG0VyF : $@convention(method) (@guaranteed FakeActorSystem) -> FakeInvocation // user: %20
885-
auto makeInvocationEncoderFnSIL =
885+
SILFunction *makeInvocationEncoderFnSIL =
886886
builder.getOrCreateFunction(loc, makeInvocationEncoderFnRef, NotForDefinition);
887887
SILValue makeInvocationEncoderFn =
888888
B.createFunctionRefFor(loc, makeInvocationEncoderFnSIL);
@@ -1395,7 +1395,7 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
13951395
}
13961396
assert(returnMetatypeValue);
13971397

1398-
// function_ref FakeActorSystem.remoteCall<A, B, C>(on:target:invocationDecoder:throwing:returning:)
1398+
// function_ref FakeActorSystem.remoteCall<A, B, C>(on:target:invocation:throwing:returning:)
13991399
// %49 = function_ref @$s27FakeDistributedActorSystems0aC6SystemV10remoteCall2on6target17invocationDecoder8throwing9returningq0_x_01_B006RemoteG6TargetVAA0A10InvocationVzq_mq0_mSgtYaKAJ0bC0RzSeR0_SER0_AA0C7AddressV2IDRtzr1_lF : $@convention(method) @async <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : DistributedActor, τ_0_2 : Decodable, τ_0_2 : Encodable, τ_0_0.ID == ActorAddress> (@guaranteed τ_0_0, @in_guaranteed RemoteCallTarget, @inout FakeInvocation, @thick τ_0_1.Type, Optional<@thick τ_0_2.Type>, @guaranteed FakeActorSystem) -> (@out τ_0_2, @error Error) // user: %50
14001400
auto remoteCallFnDecl =
14011401
ctx.getRemoteCallOnDistributedActorSystem(selfTyDecl, /*isVoid=*/resultType.isVoid());

0 commit comments

Comments
 (0)