Skip to content

Commit 4ed6f68

Browse files
authored
Merge pull request #59514 from DougGregor/distributed-witness-mark-2-5.7
2 parents 489508f + e66df34 commit 4ed6f68

File tree

4 files changed

+62
-27
lines changed

4 files changed

+62
-27
lines changed

lib/SIL/IR/SILDeclRef.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,8 +1045,6 @@ bool SILDeclRef::requiresNewVTableEntry() const {
10451045
return true;
10461046
if (!hasDecl())
10471047
return false;
1048-
if (isDistributedThunk())
1049-
return false;
10501048
if (isBackDeploymentThunk())
10511049
return false;
10521050
auto fnDecl = dyn_cast<AbstractFunctionDecl>(getDecl());

lib/SILGen/SILGenPoly.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4430,41 +4430,45 @@ void SILGenFunction::emitProtocolWitness(
44304430
FullExpr scope(Cleanups, cleanupLoc);
44314431
FormalEvaluationScope formalEvalScope(*this);
44324432

4433-
auto witnessKind = getWitnessDispatchKind(witness, isSelfConformance);
44344433
auto thunkTy = F.getLoweredFunctionType();
44354434

44364435
SmallVector<ManagedValue, 8> origParams;
44374436
collectThunkParams(loc, origParams);
44384437

4439-
// If we are supposed to enter the actor, do so now.
4440-
if (enterIsolation) {
4441-
if (enterIsolation->isDistributedActor()) {
4442-
// For a distributed actor, call through the distributed thunk.
4443-
witness = witness.asDistributed();
4444-
} else {
4445-
// For a non-distributed actor, hop to the actor.
4446-
Optional<ManagedValue> actorSelf;
4447-
4448-
// For an instance actor, get the actor 'self'.
4449-
if (*enterIsolation == ActorIsolation::ActorInstance) {
4450-
auto actorSelfVal = origParams.back();
4451-
4452-
if (actorSelfVal.getType().isAddress()) {
4453-
auto &actorSelfTL = getTypeLowering(actorSelfVal.getType());
4454-
if (!actorSelfTL.isAddressOnly()) {
4455-
actorSelfVal = emitManagedLoad(
4456-
*this, loc, actorSelfVal, actorSelfTL);
4457-
}
4438+
// If the witness is isolated to a distributed actor, but the requirement is
4439+
// not, go through the distributed thunk.
4440+
if (witness.hasDecl() &&
4441+
getActorIsolation(witness.getDecl()).isDistributedActor() &&
4442+
requirement.hasDecl() &&
4443+
!getActorIsolation(requirement.getDecl()).isDistributedActor()) {
4444+
witness = SILDeclRef(
4445+
cast<AbstractFunctionDecl>(witness.getDecl())->getDistributedThunk())
4446+
.asDistributed();
4447+
} else if (enterIsolation) {
4448+
// If we are supposed to enter the actor, do so now by hopping to the
4449+
// actor.
4450+
Optional<ManagedValue> actorSelf;
4451+
4452+
// For an instance actor, get the actor 'self'.
4453+
if (*enterIsolation == ActorIsolation::ActorInstance) {
4454+
auto actorSelfVal = origParams.back();
4455+
4456+
if (actorSelfVal.getType().isAddress()) {
4457+
auto &actorSelfTL = getTypeLowering(actorSelfVal.getType());
4458+
if (!actorSelfTL.isAddressOnly()) {
4459+
actorSelfVal = emitManagedLoad(
4460+
*this, loc, actorSelfVal, actorSelfTL);
44584461
}
4459-
4460-
actorSelf = actorSelfVal;
44614462
}
44624463

4463-
emitHopToTargetActor(loc, enterIsolation, actorSelf);
4464+
actorSelf = actorSelfVal;
44644465
}
4466+
4467+
emitHopToTargetActor(loc, enterIsolation, actorSelf);
44654468
}
44664469

44674470
// Get the type of the witness.
4471+
auto witnessKind = getWitnessDispatchKind(witness, isSelfConformance);
44684472
auto witnessInfo = getConstantInfo(getTypeExpansionContext(), witness);
44694473
CanAnyFunctionType witnessSubstTy = witnessInfo.LoweredType;
44704474
if (auto genericFnType = dyn_cast<GenericFunctionType>(witnessSubstTy)) {

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,10 @@ static FuncDecl *createDistributedThunkFunction(FuncDecl *func) {
664664
func->getResultInterfaceType(), DC);
665665
thunk->setSynthesized(true);
666666
thunk->getAttrs().add(new (C) NonisolatedAttr(/*isImplicit=*/true));
667+
668+
if (isa<ClassDecl>(DC))
669+
thunk->getAttrs().add(new (C) FinalAttr(/*isImplicit=*/true));
670+
667671
thunk->setGenericSignature(baseSignature);
668672
thunk->copyFormalAccessFrom(func, /*sourceIsParentContext=*/false);
669673
thunk->setBodySynthesizer(deriveBodyDistributed_thunk, func);

test/SILGen/distributed_thunk.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,37 @@ protocol ServerProto {
2525
extension DA: ServerProto {
2626
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s17distributed_thunk2DACAA11ServerProtoA2aDP11doSomethingyyYaKFTW : $@convention(witness_method: ServerProto) @async (@in_guaranteed DA) -> @error Error
2727
// CHECK-NOT: hop_to_executor
28-
// CHECK-NOT: return
29-
// CHECK: function_ref @$s17distributed_thunk2DAC11doSomethingyyFTE
28+
// CHECK: function_ref @$s17distributed_thunk2DAC11doSomethingyyYaKFTE
3029
// CHECK: return
3130
distributed func doSomething() { }
3231
}
32+
33+
distributed actor DA2: ServerProto {
34+
typealias ActorSystem = LocalTestingDistributedActorSystem
35+
36+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s17distributed_thunk3DA2CAA11ServerProtoA2aDP11doSomethingyyYaKFTW : $@convention(witness_method: ServerProto) @async (@in_guaranteed DA2) -> @error Error
37+
// CHECK-NOT: hop_to_executor
38+
// CHECK-NOT: return
39+
// CHECK: function_ref @$s17distributed_thunk3DA2C11doSomethingyyYaKFTE
40+
distributed func doSomething() async { }
41+
}
42+
43+
distributed actor DA3: ServerProto {
44+
typealias ActorSystem = LocalTestingDistributedActorSystem
45+
46+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s17distributed_thunk3DA3CAA11ServerProtoA2aDP11doSomethingyyYaKFTW
47+
// CHECK-NOT: hop_to_executor
48+
// CHECK-NOT: return
49+
// CHECK: function_ref @$s17distributed_thunk3DA3C11doSomethingyyYaKFTE
50+
distributed func doSomething() async throws { }
51+
}
52+
53+
distributed actor DA4: ServerProto {
54+
typealias ActorSystem = LocalTestingDistributedActorSystem
55+
56+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s17distributed_thunk3DA4CAA11ServerProtoA2aDP11doSomethingyyYaKFTW
57+
// CHECK-NOT: hop_to_executor
58+
// CHECK-NOT: return
59+
// CHECK: function_ref @$s17distributed_thunk3DA4C11doSomethingyyYaKFTE
60+
distributed func doSomething() throws { }
61+
}

0 commit comments

Comments
 (0)