mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[SE-0371] Back-deploy support for main-actor-isolated deinit
When targeting a platform that predates the introduction of isolated deinit, make a narrow exception that allows main-actor-isolated deinit to work through a special, inlineable entrypoint that is back-deployed. This implementation 1. Calls into the real implementation when available, otherwise 2. Checks if we're on the main thread, destroying immediately when we are, otherwise 3. Creates a new task on the main actor to handle destruction. This implementation is less efficient than the implementation in the runtime, but allows us to back-deploy this functionality as far back as concurrency goes. Fixes rdar://151029118.
This commit is contained in:
@@ -2396,13 +2396,25 @@ FUNCTION(TaskSwitchFunc,
|
||||
// size_t flags);
|
||||
FUNCTION(DeinitOnExecutorFunc,
|
||||
_Concurrency, swift_task_deinitOnExecutor, SwiftCC,
|
||||
ConcurrencyAvailability,
|
||||
IsolatedDeinitAvailability,
|
||||
RETURNS(VoidTy),
|
||||
ARGS(Int8PtrTy, Int8PtrTy, ExecutorFirstTy, ExecutorSecondTy, SizeTy),
|
||||
ATTRS(NoUnwind),
|
||||
EFFECT(RuntimeEffect::Concurrency),
|
||||
UNKNOWN_MEMEFFECTS)
|
||||
|
||||
// void swift_task_deinitOnExecutorMainActorBackDeploy(void *object,
|
||||
// DeinitWorkFunction *work,
|
||||
// SerialExecutorRef newExecutor,
|
||||
// size_t flags);
|
||||
FUNCTION(DeinitOnExecutorMainActorBackDeployFunc,
|
||||
_Concurrency, deinitOnExecutorMainActorBackDeploy, SwiftCC,
|
||||
ConcurrencyAvailability,
|
||||
RETURNS(VoidTy),
|
||||
ARGS(Int8PtrTy, Int8PtrTy, ExecutorFirstTy, ExecutorSecondTy, SizeTy),
|
||||
ATTRS(NoUnwind),
|
||||
EFFECT(RuntimeEffect::Concurrency),
|
||||
UNKNOWN_MEMEFFECTS)
|
||||
|
||||
// AsyncTask *swift_continuation_init(AsyncContext *continuationContext,
|
||||
// AsyncContinuationFlags);
|
||||
|
||||
@@ -982,6 +982,14 @@ namespace RuntimeConstants {
|
||||
return RuntimeAvailability::AlwaysAvailable;
|
||||
}
|
||||
|
||||
RuntimeAvailability IsolatedDeinitAvailability(ASTContext &context) {
|
||||
auto featureAvailability = context.getIsolatedDeinitAvailability();
|
||||
if (!isDeploymentAvailabilityContainedIn(context, featureAvailability)) {
|
||||
return RuntimeAvailability::ConditionallyAvailable;
|
||||
}
|
||||
return RuntimeAvailability::AlwaysAvailable;
|
||||
}
|
||||
|
||||
RuntimeAvailability
|
||||
MultiPayloadEnumTagSinglePayloadAvailability(ASTContext &context) {
|
||||
auto featureAvailability = context.getMultiPayloadEnumTagSinglePayload();
|
||||
|
||||
@@ -462,6 +462,15 @@ FuncDecl *SILGenModule::getDeinitOnExecutor() {
|
||||
return lookupConcurrencyIntrinsic(getASTContext(), "_deinitOnExecutor");
|
||||
}
|
||||
|
||||
FuncDecl *SILGenModule::getDeinitOnExecutorMainActorBackDeploy() {
|
||||
auto found = lookupConcurrencyIntrinsic(getASTContext(),
|
||||
"_deinitOnExecutorMainActorBackDeploy");
|
||||
if (found)
|
||||
return found;
|
||||
|
||||
return getDeinitOnExecutor();
|
||||
}
|
||||
|
||||
FuncDecl *SILGenModule::getCreateExecutors() {
|
||||
return lookupConcurrencyIntrinsic(getASTContext(), "_createExecutors");
|
||||
}
|
||||
|
||||
@@ -557,6 +557,8 @@ public:
|
||||
FuncDecl *getSwiftJobRun();
|
||||
/// Retrieve the _Concurrency._deinitOnExecutor intrinsic.
|
||||
FuncDecl *getDeinitOnExecutor();
|
||||
/// Retrieve the _Concurrency._deinitOnExecutorMainActorBackDeploy intrinsic.
|
||||
FuncDecl *getDeinitOnExecutorMainActorBackDeploy();
|
||||
// Retrieve the _SwiftConcurrencyShims.exit intrinsic.
|
||||
FuncDecl *getExit();
|
||||
|
||||
|
||||
@@ -339,6 +339,17 @@ void SILGenFunction::emitDeallocatingMoveOnlyDestructor(DestructorDecl *dd) {
|
||||
B.createReturn(loc, emitEmptyTuple(loc));
|
||||
}
|
||||
|
||||
/// Determine whether the availability for the body the given destructor predates the introduction of
|
||||
/// support for isolated deinit.
|
||||
static bool availabilityPredatesIsolatedDeinit(DestructorDecl *dd) {
|
||||
ASTContext &ctx = dd->getASTContext();
|
||||
if (ctx.LangOpts.DisableAvailabilityChecking)
|
||||
return false;
|
||||
|
||||
auto deploymentAvailability = AvailabilityRange::forDeploymentTarget(ctx);
|
||||
return !deploymentAvailability.isContainedIn(ctx.getIsolatedDeinitAvailability());
|
||||
}
|
||||
|
||||
void SILGenFunction::emitIsolatingDestructor(DestructorDecl *dd) {
|
||||
MagicFunctionName = DeclName(SGM.M.getASTContext().getIdentifier("deinit"));
|
||||
|
||||
@@ -372,8 +383,15 @@ void SILGenFunction::emitIsolatingDestructor(DestructorDecl *dd) {
|
||||
executor = B.createExtractExecutor(loc, actor);
|
||||
}
|
||||
|
||||
// Determine whether we need the main-actor back-deployment version of
|
||||
// this function.
|
||||
bool useMainActorBackDeploy =
|
||||
ai.isMainActor() && availabilityPredatesIsolatedDeinit(dd);
|
||||
|
||||
// Get deinitOnExecutor
|
||||
FuncDecl *swiftDeinitOnExecutorDecl = SGM.getDeinitOnExecutor();
|
||||
FuncDecl *swiftDeinitOnExecutorDecl =
|
||||
useMainActorBackDeploy ? SGM.getDeinitOnExecutorMainActorBackDeploy()
|
||||
: SGM.getDeinitOnExecutor();
|
||||
if (!swiftDeinitOnExecutorDecl) {
|
||||
dd->diagnose(diag::missing_deinit_on_executor_function);
|
||||
return;
|
||||
|
||||
@@ -123,15 +123,17 @@ public:
|
||||
diagnoseAndRemoveAttr(attr, diag::isolated_deinit_on_value_type);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
TypeChecker::checkAvailability(
|
||||
attr->getRange(), C.getIsolatedDeinitAvailability(),
|
||||
D->getDeclContext(),
|
||||
[&](AvailabilityDomain domain, AvailabilityRange range) {
|
||||
return diagnoseAndRemoveAttr(
|
||||
attr, diag::isolated_deinit_unavailable, domain, range);
|
||||
});
|
||||
if (!getActorIsolation(nominal).isMainActor()) {
|
||||
TypeChecker::checkAvailability(
|
||||
attr->getRange(), C.getIsolatedDeinitAvailability(),
|
||||
D->getDeclContext(),
|
||||
[&](AvailabilityDomain domain, AvailabilityRange range) {
|
||||
return diagnoseAndRemoveAttr(
|
||||
attr, diag::isolated_deinit_unavailable, domain, range);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -164,4 +164,37 @@ extension MainActor {
|
||||
}
|
||||
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
|
||||
|
||||
|
||||
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) || os(visionOS)
|
||||
@_silgen_name("pthread_main_np")
|
||||
@usableFromInline
|
||||
internal func pthread_main_np() -> CInt
|
||||
|
||||
@available(SwiftStdlib 5.1, *)
|
||||
@_alwaysEmitIntoClient
|
||||
@_silgen_name("swift_task_deinitOnExecutorMainActorBackDeploy")
|
||||
public func _deinitOnExecutorMainActorBackDeploy(
|
||||
_ object: __owned AnyObject,
|
||||
_ work: @convention(thin) (__owned AnyObject) -> Void,
|
||||
_ executor: Builtin.Executor,
|
||||
_ flags: Builtin.Word) {
|
||||
if #available(macOS 15.4, iOS 18.4, watchOS 11.4, tvOS 18.4, visionOS 2.4, *) {
|
||||
// On new-enough platforms, use the runtime functionality, which allocates
|
||||
// the task more efficiently.
|
||||
_deinitOnExecutor(object, work, executor, flags)
|
||||
} else if pthread_main_np() == 1 {
|
||||
// Using "main thread" as a proxy for "main actor", immediately destroy
|
||||
// the object.
|
||||
work(consume object)
|
||||
} else {
|
||||
// Steal the local object so that the reference count stays at 1 even when
|
||||
// the object is captured.
|
||||
var stolenObject: AnyObject? = consume object
|
||||
Task.detached { @MainActor in
|
||||
work(stolenObject.take()!)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !$Embedded
|
||||
|
||||
15
test/Concurrency/deinit_isolation_backdeploy.swift
Normal file
15
test/Concurrency/deinit_isolation_backdeploy.swift
Normal file
@@ -0,0 +1,15 @@
|
||||
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -parse-as-library -emit-silgen -DSILGEN %s | %FileCheck %s
|
||||
|
||||
// REQUIRES: concurrency
|
||||
// REQUIRES: OS=macosx
|
||||
|
||||
|
||||
@MainActor
|
||||
class C {
|
||||
// CHECK-LABEL: sil hidden [ossa] @$s27deinit_isolation_backdeploy1CCfD : $@convention(method) (@owned C) -> ()
|
||||
// CHECK: function_ref @swift_task_deinitOnExecutorMainActorBackDeploy
|
||||
isolated deinit { }
|
||||
}
|
||||
|
||||
// Make sure this function is available
|
||||
// CHECK: sil hidden_external [serialized] [available 12.0.0] @swift_task_deinitOnExecutorMainActorBackDeploy : $@convention(thin) (@owned AnyObject, @convention(thin) (@owned AnyObject) -> (), Builtin.Executor, Builtin.Word) -> ()
|
||||
@@ -13,11 +13,24 @@ class NotSendable {}
|
||||
}
|
||||
}
|
||||
|
||||
@globalActor
|
||||
actor SomeGlobalActor {
|
||||
static let shared = SomeGlobalActor()
|
||||
}
|
||||
|
||||
// expected-note@+1{{add '@available' attribute to enclosing class}}
|
||||
@MainActor class C2 {
|
||||
@SomeGlobalActor class C2 {
|
||||
var x: Int = 0
|
||||
|
||||
isolated deinit { // expected-error{{isolated deinit is only available in macOS 15.4.0 or newer}}
|
||||
print(x)
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor class C3 {
|
||||
var x: Int = 0
|
||||
|
||||
isolated deinit { // okay, this back-deploys
|
||||
print(x)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user