Skip to content

Commit d887249

Browse files
authored
Merge pull request #72173 from xedin/dont-emit-executor-check-if-target-is-too-old
[SILGen] Emit `_checkExpectedExecutor` only if its available on a dep…
2 parents 8801658 + 8e93ae9 commit d887249

File tree

6 files changed

+66
-15
lines changed

6 files changed

+66
-15
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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "RValue.h"
1616
#include "Scope.h"
1717
#include "swift/AST/ASTContext.h"
18+
#include "swift/AST/Availability.h"
1819
#include "swift/AST/ProtocolConformance.h"
1920
#include "swift/Basic/Range.h"
2021

@@ -589,10 +590,38 @@ void SILGenFunction::emitHopToActorValue(SILLocation loc, ManagedValue actor) {
589590
executor, /*mandatory*/ true);
590591
}
591592

592-
void SILGenFunction::emitPreconditionCheckExpectedExecutor(
593-
SILLocation loc, SILValue executorOrActor) {
593+
static bool isCheckExpectedExecutorIntrinsicAvailable(SILGenModule &SGM) {
594594
auto checkExecutor = SGM.getCheckExpectedExecutor();
595595
if (!checkExecutor)
596+
return false;
597+
598+
// Forego a check if instrinsic is unavailable, this could happen
599+
// in main-actor context.
600+
auto &C = checkExecutor->getASTContext();
601+
if (!C.LangOpts.DisableAvailabilityChecking) {
602+
auto deploymentAvailability = AvailabilityContext::forDeploymentTarget(C);
603+
auto declAvailability =
604+
AvailabilityInference::availableRange(checkExecutor, C);
605+
return deploymentAvailability.isContainedIn(declAvailability);
606+
}
607+
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))
596625
return;
597626

598627
// We don't want the debugger to step into these.
@@ -605,7 +634,7 @@ void SILGenFunction::emitPreconditionCheckExpectedExecutor(
605634
auto args = emitSourceLocationArgs(loc.getSourceLoc(), loc);
606635

607636
emitApplyOfLibraryIntrinsic(
608-
loc, checkExecutor, SubstitutionMap(),
637+
loc, SGM.getCheckExpectedExecutor(), SubstitutionMap(),
609638
{args.filenameStartPointer, args.filenameLength, args.filenameIsAscii,
610639
args.line, ManagedValue::forObjectRValueWithoutOwnership(executor)},
611640
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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -primary-file %s -target %target-cpu-apple-macosx10.14 -enable-experimental-feature DynamicActorIsolation -emit-silgen -o - | %FileCheck %s
2+
3+
// REQUIRES: asserts
4+
// REQUIRES: concurrency
5+
// REQUIRES: OS=macosx
6+
7+
protocol P {
8+
associatedtype T
9+
10+
func fn() -> T?
11+
}
12+
13+
@MainActor
14+
struct IsolatedType<T> : @preconcurrency P {
15+
func fn() -> T? { nil }
16+
}
17+
18+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s38preconcurrency_conformances_backdeploy12IsolatedTypeVyxGAA1PA2aEP2fn1TQzSgyFTW
19+
// CHECK-NOT: function_ref @$sScM6sharedScMvgZ
20+
// CHECK-NOT: function_ref @$ss22_checkExpectedExecutor14_filenameStart01_D6Length01_D7IsASCII5_line9_executoryBp_BwBi1_BwBetF

0 commit comments

Comments
 (0)