Skip to content

[concurrency] SILGen: emit hop_to_executor instructions in actor-isolated closures. #34824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
Type resultType, DeclContext *DC,
bool throws, SourceLoc throwsLoc);

/// Initializes 'actor' with the loaded shared instance of the \p globalActor
/// type.
void loadGlobalActor(Type globalActor);

/// Create SILArguments in the entry block that bind a single value
/// of the given parameter suitably for being forwarded.
void bindParameterForForwarding(ParamDecl *param,
Expand Down
67 changes: 45 additions & 22 deletions lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
if (!F.isAsync())
return;

// Initialize 'actor' if the function is an actor-isolated function or
// closure.

if (auto *funcDecl =
dyn_cast_or_null<AbstractFunctionDecl>(FunctionDC->getAsDecl())) {
auto actorIsolation = getActorIsolation(funcDecl);
Expand All @@ -465,38 +468,58 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
case ActorIsolation::ActorInstance: {
assert(selfParam && "no self parameter for ActorInstance isolation");
ManagedValue selfArg = ManagedValue::forUnmanaged(F.getSelfArgument());
ManagedValue borrowedSelf = selfArg.borrow(*this, F.getLocation());
actor = borrowedSelf.getValue();
actor = selfArg.borrow(*this, F.getLocation()).getValue();
break;
}
case ActorIsolation::GlobalActor: {
CanType actorType = CanType(actorIsolation.getGlobalActor());
NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal();
VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance();
assert(sharedInstanceDecl && "no shared actor field in global actor");
SubstitutionMap subs =
actorType->getContextSubstitutionMap(SGM.SwiftModule, nominal);
SILLocation loc = F.getLocation();
Type instanceType =
actorType->getTypeOfMember(SGM.SwiftModule, sharedInstanceDecl);

ManagedValue actorMetaType =
ManagedValue::forUnmanaged(B.createMetatype(loc,
SILType::getPrimitiveObjectType(
CanMetatypeType::get(actorType, MetatypeRepresentation::Thin))));

RValue actorInstanceRV = emitRValueForStorageLoad(loc, actorMetaType,
actorType, /*isSuper*/ false, sharedInstanceDecl, PreparedArguments(),
subs, AccessSemantics::Ordinary, instanceType, SGFContext());
case ActorIsolation::GlobalActor:
loadGlobalActor(actorIsolation.getGlobalActor());
break;
}
} else if (auto *closureExpr = dyn_cast<AbstractClosureExpr>(FunctionDC)) {
auto actorIsolation = closureExpr->getActorIsolation();
switch (actorIsolation.getKind()) {
case ClosureActorIsolation::Independent:
break;
case ClosureActorIsolation::ActorInstance: {
VarDecl *actorDecl = actorIsolation.getActorInstance();
RValue actorInstanceRV = emitRValueForDecl(F.getLocation(),
actorDecl, actorDecl->getType(), AccessSemantics::Ordinary);
ManagedValue actorInstance = std::move(actorInstanceRV).getScalarValue();
actor = actorInstance.borrow(*this, loc).getValue();
actor = actorInstance.borrow(*this, F.getLocation()).getValue();
break;
}
case ClosureActorIsolation::GlobalActor:
loadGlobalActor(actorIsolation.getGlobalActor());
break;
}
}
emitHopToCurrentExecutor(F.getLocation());
}

void SILGenFunction::loadGlobalActor(Type globalActor) {
assert(F.isAsync());
CanType actorType = CanType(globalActor);
NominalTypeDecl *nominal = actorType->getNominalOrBoundGenericNominal();
VarDecl *sharedInstanceDecl = nominal->getGlobalActorInstance();
assert(sharedInstanceDecl && "no shared actor field in global actor");
SubstitutionMap subs =
actorType->getContextSubstitutionMap(SGM.SwiftModule, nominal);
SILLocation loc = F.getLocation();
Type instanceType =
actorType->getTypeOfMember(SGM.SwiftModule, sharedInstanceDecl);

ManagedValue actorMetaType =
ManagedValue::forUnmanaged(B.createMetatype(loc,
SILType::getPrimitiveObjectType(
CanMetatypeType::get(actorType, MetatypeRepresentation::Thin))));

RValue actorInstanceRV = emitRValueForStorageLoad(loc, actorMetaType,
actorType, /*isSuper*/ false, sharedInstanceDecl, PreparedArguments(),
subs, AccessSemantics::Ordinary, instanceType, SGFContext());
ManagedValue actorInstance = std::move(actorInstanceRV).getScalarValue();
actor = actorInstance.borrow(*this, loc).getValue();
}

static void emitIndirectResultParameters(SILGenFunction &SGF, Type resultType,
DeclContext *DC) {
// Expand tuples.
Expand Down
25 changes: 25 additions & 0 deletions test/SILGen/hop_to_executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ actor class MyActor {
await callee(p)
}


// CHECK-LABEL: sil private [ossa] @$s4test7MyActorC0A7ClosureSiyYFSiyYXEfU_ : $@convention(thin) @async (@guaranteed MyActor) -> Int {
// CHECK: [[COPIED_SELF:%[0-9]+]] = copy_value %0 : $MyActor
// CHECK: [[BORROWED_SELF:%[0-9]+]] = begin_borrow [[COPIED_SELF]] : $MyActor
// CHECK: hop_to_executor [[BORROWED_SELF]] : $MyActor
// CHECK: = apply
// CHECK: } // end sil function '$s4test7MyActorC0A7ClosureSiyYFSiyYXEfU_'
func testClosure() async -> Int {
return await { () async in p }()
}

// CHECK-LABEL: sil hidden [ossa] @$s4test7MyActorC13dontInsertHTESiyF : $@convention(method) (@guaranteed MyActor) -> Int {
// CHECK-NOT: hop_to_executor
// CHECK: } // end sil function '$s4test7MyActorC13dontInsertHTESiyF'
Expand Down Expand Up @@ -77,6 +88,20 @@ struct GlobalActor {
func testGlobalActor() async {
}

// CHECK-LABEL: sil private [ossa] @$s4test0A22GlobalActorWithClosureyyYFyyYXEfU_ : $@convention(thin) @async () -> () {
// CHECK: [[F:%[0-9]+]] = function_ref @$s4test11GlobalActorV6sharedAA02MyC0Cvau : $@convention(thin) () -> Builtin.RawPointer
// CHECK: [[P:%[0-9]+]] = apply [[F]]() : $@convention(thin) () -> Builtin.RawPointer
// CHECK: [[A:%[0-9]+]] = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*MyActor
// CHECK: [[ACC:%[0-9]+]] = begin_access [read] [dynamic] [[A]] : $*MyActor
// CHECK: [[L:%[0-9]+]] = load [copy] [[ACC]] : $*MyActor
// CHECK: [[B:%[0-9]+]] = begin_borrow [[L]] : $MyActor
// CHECK: hop_to_executor [[B]] : $MyActor
// CHECK: } // end sil function '$s4test0A22GlobalActorWithClosureyyYFyyYXEfU_'
@GlobalActor
func testGlobalActorWithClosure() async {
await { () async in }()
}

@globalActor
struct GenericGlobalActorWithGetter<T> {
static var shared: MyActor { return MyActor() }
Expand Down