Skip to content

Commit 8e93ae9

Browse files
committed
[SILGen] Don't emit executor references when '_checkExpectedExecutor' is unavailable
Avoid emitting an unused executor reference for dynamic actor isolation checking.
1 parent f977996 commit 8e93ae9

File tree

6 files changed

+38
-18
lines changed

6 files changed

+38
-18
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,9 +1619,7 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
16191619
// executor, since the task will end after we invoke the completion handler.
16201620
emitPrologGlobalActorHop(loc, isolation->getGlobalActor());
16211621
} else {
1622-
auto executor =
1623-
emitLoadGlobalActorExecutor(isolation->getGlobalActor());
1624-
emitPreconditionCheckExpectedExecutor(loc, executor);
1622+
emitPreconditionCheckExpectedExecutor(loc, *isolation, std::nullopt);
16251623
}
16261624
}
16271625

lib/SILGen/SILGenConcurrency.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,10 @@ void SILGenFunction::emitHopToActorValue(SILLocation loc, ManagedValue actor) {
590590
executor, /*mandatory*/ true);
591591
}
592592

593-
void SILGenFunction::emitPreconditionCheckExpectedExecutor(
594-
SILLocation loc, SILValue executorOrActor) {
593+
static bool isCheckExpectedExecutorIntrinsicAvailable(SILGenModule &SGM) {
595594
auto checkExecutor = SGM.getCheckExpectedExecutor();
596595
if (!checkExecutor)
597-
return;
596+
return false;
598597

599598
// Forego a check if instrinsic is unavailable, this could happen
600599
// in main-actor context.
@@ -603,10 +602,28 @@ void SILGenFunction::emitPreconditionCheckExpectedExecutor(
603602
auto deploymentAvailability = AvailabilityContext::forDeploymentTarget(C);
604603
auto declAvailability =
605604
AvailabilityInference::availableRange(checkExecutor, C);
606-
if (!deploymentAvailability.isContainedIn(declAvailability))
607-
return;
605+
return deploymentAvailability.isContainedIn(declAvailability);
608606
}
609607

608+
return true;
609+
}
610+
611+
void SILGenFunction::emitPreconditionCheckExpectedExecutor(
612+
SILLocation loc, ActorIsolation isolation,
613+
std::optional<ManagedValue> actorSelf) {
614+
if (!isCheckExpectedExecutorIntrinsicAvailable(SGM))
615+
return;
616+
617+
auto executor = emitExecutor(loc, isolation, actorSelf);
618+
assert(executor);
619+
emitPreconditionCheckExpectedExecutor(loc, *executor);
620+
}
621+
622+
void SILGenFunction::emitPreconditionCheckExpectedExecutor(
623+
SILLocation loc, SILValue executorOrActor) {
624+
if (!isCheckExpectedExecutorIntrinsicAvailable(SGM))
625+
return;
626+
610627
// We don't want the debugger to step into these.
611628
loc.markAutoGenerated();
612629

@@ -617,7 +634,7 @@ void SILGenFunction::emitPreconditionCheckExpectedExecutor(
617634
auto args = emitSourceLocationArgs(loc.getSourceLoc(), loc);
618635

619636
emitApplyOfLibraryIntrinsic(
620-
loc, checkExecutor, SubstitutionMap(),
637+
loc, SGM.getCheckExpectedExecutor(), SubstitutionMap(),
621638
{args.filenameStartPointer, args.filenameLength, args.filenameIsAscii,
622639
args.line, ManagedValue::forObjectRValueWithoutOwnership(executor)},
623640
SGFContext());

lib/SILGen/SILGenFunction.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,13 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
11651165
ActorIsolation isolation,
11661166
std::optional<ManagedValue> maybeSelf);
11671167

1168+
/// Emit a precondition check to ensure that the function is executing in
1169+
/// the expected isolation context.
1170+
void
1171+
emitPreconditionCheckExpectedExecutor(SILLocation loc,
1172+
ActorIsolation isolation,
1173+
std::optional<ManagedValue> actorSelf);
1174+
11681175
/// Emit a precondition check to ensure that the function is executing in
11691176
/// the expected isolation context.
11701177
void emitPreconditionCheckExpectedExecutor(

lib/SILGen/SILGenPoly.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7021,8 +7021,7 @@ void SILGenFunction::emitProtocolWitness(
70217021

70227022
if (!F.isAsync()) {
70237023
assert(isPreconcurrency);
7024-
auto executor = emitExecutor(loc, *enterIsolation, actorSelf);
7025-
emitPreconditionCheckExpectedExecutor(loc, *executor);
7024+
emitPreconditionCheckExpectedExecutor(loc, *enterIsolation, actorSelf);
70267025
} else {
70277026
emitHopToTargetActor(loc, enterIsolation, actorSelf);
70287027
}
@@ -7247,12 +7246,10 @@ ManagedValue SILGenFunction::emitActorIsolationErasureThunk(
72477246

72487247
buildThunkBody(
72497248
thunkSGF, loc, AbstractionPattern(isolatedType), isolatedType,
7250-
AbstractionPattern(nonIsolatedType), nonIsolatedType,
7251-
expectedType, dynamicSelfType,
7252-
[&loc, &globalActor](SILGenFunction &thunkSGF) {
7253-
auto expectedExecutor =
7254-
thunkSGF.emitLoadGlobalActorExecutor(globalActor);
7255-
thunkSGF.emitPreconditionCheckExpectedExecutor(loc, expectedExecutor);
7249+
AbstractionPattern(nonIsolatedType), nonIsolatedType, expectedType,
7250+
dynamicSelfType, [&loc, &globalActor](SILGenFunction &thunkSGF) {
7251+
thunkSGF.emitPreconditionCheckExpectedExecutor(
7252+
loc, ActorIsolation::forGlobalActor(globalActor), std::nullopt);
72567253
});
72577254

72587255
SGM.emitLazyConformancesForFunction(thunk);

test/Concurrency/dynamic_checks_for_func_refs_in_preconcurrency_apis_objc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -enable-experimental-feature DynamicActorIsolation -emit-silgen -verify %s | %FileCheck %s
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -target arm64-apple-macosx10.15 -enable-experimental-feature DynamicActorIsolation -emit-silgen -verify %s | %FileCheck %s
22
// REQUIRES: objc_interop
33
// REQUIRES: asserts
44

test/SILGen/preconcurrency_conformances_backdeploy.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ struct IsolatedType<T> : @preconcurrency P {
1616
}
1717

1818
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s38preconcurrency_conformances_backdeploy12IsolatedTypeVyxGAA1PA2aEP2fn1TQzSgyFTW
19+
// CHECK-NOT: function_ref @$sScM6sharedScMvgZ
1920
// CHECK-NOT: function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF

0 commit comments

Comments
 (0)