Skip to content

Commit e85bd1f

Browse files
committed
[Sema] Distributed: Adjust distributed thunk synthesis to use witnesses for actorSystem references
Things like `makeInvocationEncoder()` and all of the `record*` methods can now be called through witnesses.
1 parent a2caaa3 commit e85bd1f

File tree

1 file changed

+92
-113
lines changed

1 file changed

+92
-113
lines changed

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 92 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ static std::pair<BraceStmt *, bool>
197197
deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
198198
auto implicit = true;
199199
ASTContext &C = thunk->getASTContext();
200-
auto module = thunk->getParentModule();
201200

202201
// mock locations, we're a thunk and don't really need detailed locations
203202
const SourceLoc sloc = SourceLoc();
@@ -216,21 +215,6 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
216215
Type returnTy = func->getResultInterfaceType();
217216
auto isVoidReturn = returnTy->isVoid();
218217

219-
// === self.actorSystem
220-
ProtocolDecl *DAS = C.getDistributedActorSystemDecl();
221-
Type systemTy = getConcreteReplacementForProtocolActorSystemType(thunk);
222-
assert(systemTy && "distributed thunk can only be synthesized with concrete "
223-
"actor system types");
224-
NominalTypeDecl *systemDecl = systemTy->getAnyNominal();
225-
assert(systemDecl);
226-
auto systemConfRef = module->lookupConformance(systemTy, DAS);
227-
assert(systemConfRef && "ActorSystem must conform to DistributedActorSystem");
228-
229-
// === ActorSystem.InvocationEncoder
230-
Type invocationEncoderTy =
231-
getDistributedActorSystemInvocationEncoderType(systemDecl);
232-
NominalTypeDecl *invocationEncoderDecl = invocationEncoderTy->getAnyNominal();
233-
234218
// === Type:
235219
StructDecl *RCT = C.getRemoteCallTargetDecl();
236220
Type remoteCallTargetTy = RCT->getDeclaredInterfaceType();
@@ -293,17 +277,13 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
293277
// === remote branch --------------------------------------------------------
294278
SmallVector<ASTNode, 8> remoteBranchStmts;
295279
// --- self.actorSystem
296-
auto systemProperty = nominal->getDistributedActorSystemProperty();
297-
assert(systemProperty && "Unable to find 'actorSystem' property");
298280
auto systemRefExpr =
299281
UnresolvedDotExpr::createImplicit(
300282
C, new (C) DeclRefExpr(selfDecl, dloc, implicit), // TODO: make createImplicit
301283
C.Id_actorSystem);
302284

303-
auto *systemVar =
304-
new (C) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Let, sloc,
305-
C.Id_system, thunk);
306-
systemVar->setInterfaceType(systemProperty->getInterfaceType());
285+
auto *systemVar = new (C) VarDecl(
286+
/*isStatic=*/false, VarDecl::Introducer::Let, sloc, C.Id_system, thunk);
307287
systemVar->setImplicit();
308288
systemVar->setSynthesized();
309289

@@ -320,18 +300,15 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
320300
auto *invocationVar =
321301
new (C) VarDecl(/*isStatic=*/false, VarDecl::Introducer::Var, sloc,
322302
C.Id_invocation, thunk);
323-
invocationVar->setInterfaceType(invocationEncoderTy);
324303
invocationVar->setImplicit();
325304
invocationVar->setSynthesized();
326305

327306
{
328307
Pattern *invocationPattern = NamedPattern::createImplicit(C, invocationVar);
329308

330-
FuncDecl *makeInvocationEncoderDecl =
331-
C.getMakeInvocationEncoderOnDistributedActorSystem(func);
332309
auto makeInvocationExpr = UnresolvedDotExpr::createImplicit(
333310
C, new (C) DeclRefExpr(ConcreteDeclRef(systemVar), dloc, implicit),
334-
makeInvocationEncoderDecl->getName());
311+
DeclName(C.Id_makeInvocationEncoder));
335312
auto *makeInvocationArgs = ArgumentList::createImplicit(C, {});
336313
auto makeInvocationCallExpr =
337314
CallExpr::createImplicit(C, makeInvocationExpr, makeInvocationArgs);
@@ -347,12 +324,11 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
347324
// --- Recording invocation details
348325
// -- recordGenericSubstitution(s)
349326
if (auto genEnv = thunk->getGenericEnvironment()) {
350-
auto recordGenericSubstitutionDecl =
351-
C.getRecordGenericSubstitutionOnDistributedInvocationEncoder(invocationEncoderDecl);
352-
assert(recordGenericSubstitutionDecl);
327+
auto recordGenericSubstitutionName =
328+
DeclName(C, C.Id_recordGenericSubstitution,
329+
/*labels=*/{Identifier()});
353330
auto recordGenericSubstitutionDeclRef =
354-
UnresolvedDeclRefExpr::createImplicit(
355-
C, recordGenericSubstitutionDecl->getName());
331+
UnresolvedDeclRefExpr::createImplicit(C, recordGenericSubstitutionName);
356332

357333
for (auto genParamType : genEnv->getGenericParams()) {
358334
auto tyExpr = TypeExpr::createImplicit(genEnv->mapTypeIntoContext(genParamType), C);
@@ -366,12 +342,14 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
366342
{subTypeExpr},
367343
C);
368344

369-
Expr *recordGenericSub =
370-
CallExpr::createImplicit(C,
371-
UnresolvedDotExpr::createImplicit(C,
372-
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc, implicit, AccessSemantics::Ordinary),
373-
recordGenericSubstitutionDecl->getName()),
374-
recordGenericSubArgsList);
345+
Expr *recordGenericSub = CallExpr::createImplicit(
346+
C,
347+
UnresolvedDotExpr::createImplicit(
348+
C,
349+
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc,
350+
implicit, AccessSemantics::Ordinary),
351+
recordGenericSubstitutionName),
352+
recordGenericSubArgsList);
375353
recordGenericSub = TryExpr::createImplicit(C, sloc, recordGenericSub);
376354

377355
remoteBranchStmts.push_back(recordGenericSub);
@@ -380,14 +358,9 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
380358

381359
// -- recordArgument(s)
382360
{
383-
auto recordArgumentDecl =
384-
C.getRecordArgumentOnDistributedInvocationEncoder(invocationEncoderDecl);
385-
assert(recordArgumentDecl);
386-
361+
auto recordArgumentName = DeclName(C, C.Id_recordArgument,
362+
/*labels=*/{Identifier()});
387363
for (auto param : *thunk->getParameters()) {
388-
auto recordArgumentDeclRef = UnresolvedDeclRefExpr::createImplicit(
389-
C, recordArgumentDecl->getName());
390-
391364
auto argumentName = param->getArgumentName().str();
392365
LiteralExpr *argumentLabelArg;
393366
if (argumentName.empty()) {
@@ -444,100 +417,96 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
444417

445418
/// --- Pass the argumentRepr to the recordArgument function
446419
auto recordArgArgsList = ArgumentList::forImplicitCallTo(
447-
recordArgumentDeclRef->getName(),
448-
{
449-
new (C) DeclRefExpr(
450-
ConcreteDeclRef(callArgVar), dloc, implicit,
451-
AccessSemantics::Ordinary)
452-
}, C);
453-
454-
auto tryRecordArgExpr = TryExpr::createImplicit(C, sloc,
455-
CallExpr::createImplicit(C,
456-
UnresolvedDotExpr::createImplicit(C,
457-
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc, implicit, AccessSemantics::Ordinary),
458-
recordArgumentDecl->getName()),
459-
recordArgArgsList));
420+
DeclNameRef(recordArgumentName),
421+
{new (C) DeclRefExpr(ConcreteDeclRef(callArgVar), dloc, implicit,
422+
AccessSemantics::Ordinary)},
423+
C);
424+
425+
auto tryRecordArgExpr = TryExpr::createImplicit(
426+
C, sloc,
427+
CallExpr::createImplicit(
428+
C,
429+
UnresolvedDotExpr::createImplicit(
430+
C,
431+
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc,
432+
implicit, AccessSemantics::Ordinary),
433+
recordArgumentName),
434+
recordArgArgsList));
460435

461436
remoteBranchStmts.push_back(tryRecordArgExpr);
462437
}
463438
}
464439

465440
// -- recordErrorType
466441
if (func->hasThrows()) {
442+
auto recordErrorTypeName = DeclName(C, C.Id_recordErrorType,
443+
/*labels=*/{Identifier()});
467444
// Error.self
468445
auto errorDecl = C.getErrorDecl();
469446
auto *errorTypeExpr = new (C) DotSelfExpr(
470447
UnresolvedDeclRefExpr::createImplicit(C, errorDecl->getName()), sloc,
471448
sloc, errorDecl->getDeclaredInterfaceType());
472449

473-
auto recordErrorDecl = C.getRecordErrorTypeOnDistributedInvocationEncoder(
474-
invocationEncoderDecl);
475-
assert(recordErrorDecl);
476-
auto recordErrorDeclRef =
477-
UnresolvedDeclRefExpr::createImplicit(C, recordErrorDecl->getName());
478-
479450
auto recordArgsList = ArgumentList::forImplicitCallTo(
480-
recordErrorDeclRef->getName(),
481-
{errorTypeExpr},
482-
C);
483-
auto tryRecordErrorTyExpr = TryExpr::createImplicit(C, sloc,
484-
CallExpr::createImplicit(C,
485-
UnresolvedDotExpr::createImplicit(C,
486-
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc, implicit, AccessSemantics::Ordinary),
487-
recordErrorDecl->getName()),
488-
recordArgsList));
451+
DeclNameRef(recordErrorTypeName), {errorTypeExpr}, C);
452+
auto tryRecordErrorTyExpr = TryExpr::createImplicit(
453+
C, sloc,
454+
CallExpr::createImplicit(
455+
C,
456+
UnresolvedDotExpr::createImplicit(
457+
C,
458+
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc,
459+
implicit, AccessSemantics::Ordinary),
460+
recordErrorTypeName),
461+
recordArgsList));
489462

490463
remoteBranchStmts.push_back(tryRecordErrorTyExpr);
491464
}
492465

493466
// -- recordReturnType
494467
if (!isVoidReturn) {
468+
auto recordReturnTypeName = DeclName(C, C.Id_recordReturnType,
469+
/*labels=*/{Identifier()});
470+
495471
// Result.self
496472
// Watch out and always map into thunk context
497473
auto resultType = thunk->mapTypeIntoContext(func->getResultInterfaceType());
498474
auto *metaTypeRef = TypeExpr::createImplicit(resultType, C);
499475
auto *resultTypeExpr =
500476
new (C) DotSelfExpr(metaTypeRef, sloc, sloc, resultType);
501477

502-
auto recordReturnTypeDecl =
503-
C.getRecordReturnTypeOnDistributedInvocationEncoder(
504-
invocationEncoderDecl);
505-
auto recordReturnTypeDeclRef =
506-
UnresolvedDeclRefExpr::createImplicit(C, recordReturnTypeDecl->getName());
507-
508478
auto recordArgsList = ArgumentList::forImplicitCallTo(
509-
recordReturnTypeDeclRef->getName(),
510-
{resultTypeExpr},
511-
C);
512-
auto tryRecordReturnTyExpr = TryExpr::createImplicit(C, sloc,
513-
CallExpr::createImplicit(C,
514-
UnresolvedDotExpr::createImplicit(C,
515-
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc, implicit,
516-
AccessSemantics::Ordinary),
517-
recordReturnTypeDecl->getName()),
518-
recordArgsList));
479+
DeclNameRef(recordReturnTypeName), {resultTypeExpr}, C);
480+
auto tryRecordReturnTyExpr = TryExpr::createImplicit(
481+
C, sloc,
482+
CallExpr::createImplicit(
483+
C,
484+
UnresolvedDotExpr::createImplicit(
485+
C,
486+
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc,
487+
implicit, AccessSemantics::Ordinary),
488+
recordReturnTypeName),
489+
recordArgsList));
519490

520491
remoteBranchStmts.push_back(tryRecordReturnTyExpr);
521492
}
522493

523494
// -- doneRecording
524495
{
525-
auto doneRecordingDecl =
526-
C.getDoneRecordingOnDistributedInvocationEncoder(invocationEncoderDecl);
527-
assert(doneRecordingDecl && "Could not find 'doneRecording' ad-hoc requirement witness.");
528-
auto doneRecordingDeclRef =
529-
UnresolvedDeclRefExpr::createImplicit(C, doneRecordingDecl->getName());
530-
496+
DeclName doneRecordingName(C.Id_doneRecording);
531497
auto argsList =
532-
ArgumentList::forImplicitCallTo(doneRecordingDeclRef->getName(), {}, C);
533-
auto tryDoneRecordingExpr = TryExpr::createImplicit(C, sloc,
534-
CallExpr::createImplicit(C,
535-
UnresolvedDotExpr::createImplicit(C,
536-
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc, implicit,
537-
AccessSemantics::Ordinary,
538-
invocationVar->getInterfaceType()),
539-
doneRecordingDecl->getName()),
540-
argsList));
498+
ArgumentList::forImplicitCallTo(DeclNameRef(doneRecordingName), {}, C);
499+
auto tryDoneRecordingExpr = TryExpr::createImplicit(
500+
C, sloc,
501+
CallExpr::createImplicit(
502+
C,
503+
UnresolvedDotExpr::createImplicit(
504+
C,
505+
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc,
506+
implicit, AccessSemantics::Ordinary,
507+
invocationVar->getInterfaceType()),
508+
doneRecordingName),
509+
argsList));
541510

542511
remoteBranchStmts.push_back(tryDoneRecordingExpr);
543512
}
@@ -584,12 +553,20 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
584553

585554
// === Make the 'remoteCall(Void)(...)'
586555
{
587-
auto remoteCallDecl =
588-
C.getRemoteCallOnDistributedActorSystem(systemDecl, isVoidReturn);
589-
auto systemRemoteCallRef =
590-
UnresolvedDotExpr::createImplicit(
591-
C, new (C) DeclRefExpr(ConcreteDeclRef(systemVar), dloc, implicit),
592-
remoteCallDecl->getName());
556+
DeclName remoteCallName;
557+
if (isVoidReturn) {
558+
remoteCallName =
559+
DeclName(C, C.Id_remoteCallVoid,
560+
{C.Id_on, C.Id_target, C.Id_invocation, C.Id_throwing});
561+
} else {
562+
remoteCallName = DeclName(C, C.Id_remoteCall,
563+
{C.Id_on, C.Id_target, C.Id_invocation,
564+
C.Id_throwing, C.Id_returning});
565+
}
566+
567+
auto systemRemoteCallRef = UnresolvedDotExpr::createImplicit(
568+
C, new (C) DeclRefExpr(ConcreteDeclRef(systemVar), dloc, implicit),
569+
remoteCallName);
593570

594571
SmallVector<Expr *, 5> args;
595572
// -- on actor: Act
@@ -601,9 +578,11 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
601578
AccessSemantics::Ordinary,
602579
RCT->getDeclaredInterfaceType()));
603580
// -- invocation: inout InvocationEncoder
604-
args.push_back(new (C) InOutExpr(sloc,
605-
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc,
606-
implicit, AccessSemantics::Ordinary, invocationEncoderTy), invocationEncoderTy, implicit));
581+
args.push_back(new (C) InOutExpr(
582+
sloc,
583+
new (C) DeclRefExpr(ConcreteDeclRef(invocationVar), dloc, implicit,
584+
AccessSemantics::Ordinary),
585+
Type(), implicit));
607586

608587
// -- throwing: Err.Type
609588
if (func->hasThrows()) {
@@ -635,7 +614,7 @@ deriveBodyDistributed_thunk(AbstractFunctionDecl *thunk, void *context) {
635614
args.push_back(resultTypeExpr);
636615
}
637616

638-
assert(args.size() == remoteCallDecl->getParameters()->size());
617+
assert(args.size() == (isVoidReturn ? 4 : 5));
639618
auto remoteCallArgs = ArgumentList::forImplicitCallTo(
640619
systemRemoteCallRef->getName(), args, C);
641620

0 commit comments

Comments
 (0)