@@ -454,6 +454,9 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
454454 if (!F.isAsync ())
455455 return ;
456456
457+ // Initialize 'actor' if the function is an actor-isolated function or
458+ // closure.
459+
457460 if (auto *funcDecl =
458461 dyn_cast_or_null<AbstractFunctionDecl>(FunctionDC->getAsDecl ())) {
459462 auto actorIsolation = getActorIsolation (funcDecl);
@@ -465,38 +468,58 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
465468 case ActorIsolation::ActorInstance: {
466469 assert (selfParam && " no self parameter for ActorInstance isolation" );
467470 ManagedValue selfArg = ManagedValue::forUnmanaged (F.getSelfArgument ());
468- ManagedValue borrowedSelf = selfArg.borrow (*this , F.getLocation ());
469- actor = borrowedSelf.getValue ();
471+ actor = selfArg.borrow (*this , F.getLocation ()).getValue ();
470472 break ;
471473 }
472- case ActorIsolation::GlobalActor: {
473- CanType actorType = CanType (actorIsolation.getGlobalActor ());
474- NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal ();
475- VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance ();
476- assert (sharedInstanceDecl && " no shared actor field in global actor" );
477- SubstitutionMap subs =
478- actorType->getContextSubstitutionMap (SGM.SwiftModule , nominal);
479- SILLocation loc = F.getLocation ();
480- Type instanceType =
481- actorType->getTypeOfMember (SGM.SwiftModule , sharedInstanceDecl);
482-
483- ManagedValue actorMetaType =
484- ManagedValue::forUnmanaged (B.createMetatype (loc,
485- SILType::getPrimitiveObjectType (
486- CanMetatypeType::get (actorType, MetatypeRepresentation::Thin))));
487-
488- RValue actorInstanceRV = emitRValueForStorageLoad (loc, actorMetaType,
489- actorType, /* isSuper*/ false , sharedInstanceDecl, PreparedArguments (),
490- subs, AccessSemantics::Ordinary, instanceType, SGFContext ());
474+ case ActorIsolation::GlobalActor:
475+ loadGlobalActor (actorIsolation.getGlobalActor ());
476+ break ;
477+ }
478+ } else if (auto *closureExpr = dyn_cast<AbstractClosureExpr>(FunctionDC)) {
479+ auto actorIsolation = closureExpr->getActorIsolation ();
480+ switch (actorIsolation.getKind ()) {
481+ case ClosureActorIsolation::Independent:
482+ break ;
483+ case ClosureActorIsolation::ActorInstance: {
484+ VarDecl *actorDecl = actorIsolation.getActorInstance ();
485+ RValue actorInstanceRV = emitRValueForDecl (F.getLocation (),
486+ actorDecl, actorDecl->getType (), AccessSemantics::Ordinary);
491487 ManagedValue actorInstance = std::move (actorInstanceRV).getScalarValue ();
492- actor = actorInstance.borrow (*this , loc ).getValue ();
488+ actor = actorInstance.borrow (*this , F. getLocation () ).getValue ();
493489 break ;
494490 }
491+ case ClosureActorIsolation::GlobalActor:
492+ loadGlobalActor (actorIsolation.getGlobalActor ());
493+ break ;
495494 }
496495 }
497496 emitHopToCurrentExecutor (F.getLocation ());
498497}
499498
499+ void SILGenFunction::loadGlobalActor (Type globalActor) {
500+ assert (F.isAsync ());
501+ CanType actorType = CanType (globalActor);
502+ NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal ();
503+ VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance ();
504+ assert (sharedInstanceDecl && " no shared actor field in global actor" );
505+ SubstitutionMap subs =
506+ actorType->getContextSubstitutionMap (SGM.SwiftModule , nominal);
507+ SILLocation loc = F.getLocation ();
508+ Type instanceType =
509+ actorType->getTypeOfMember (SGM.SwiftModule , sharedInstanceDecl);
510+
511+ ManagedValue actorMetaType =
512+ ManagedValue::forUnmanaged (B.createMetatype (loc,
513+ SILType::getPrimitiveObjectType (
514+ CanMetatypeType::get (actorType, MetatypeRepresentation::Thin))));
515+
516+ RValue actorInstanceRV = emitRValueForStorageLoad (loc, actorMetaType,
517+ actorType, /* isSuper*/ false , sharedInstanceDecl, PreparedArguments (),
518+ subs, AccessSemantics::Ordinary, instanceType, SGFContext ());
519+ ManagedValue actorInstance = std::move (actorInstanceRV).getScalarValue ();
520+ actor = actorInstance.borrow (*this , loc).getValue ();
521+ }
522+
500523static void emitIndirectResultParameters (SILGenFunction &SGF, Type resultType,
501524 DeclContext *DC) {
502525 // Expand tuples.
0 commit comments