Skip to content

Commit 420859c

Browse files
authored
Merge pull request #71906 from hborla/extract-function-isolation
[Concurrency] Add an API in the Concurrency library for extracting isolation from a dynamically isolated function value.
2 parents 1c652ef + 59507ba commit 420859c

File tree

7 files changed

+34
-1
lines changed

7 files changed

+34
-1
lines changed

SwiftCompilerSources/Sources/SIL/ForwardingInstruction.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ extension DestructureStructInst : ForwardingInstruction {
347347
public var canForwardOwnedValues: Bool { true }
348348
}
349349

350+
extension FunctionExtractIsolationInst : ForwardingInstruction {
351+
public var preservesIdentity: Bool { false }
352+
public var preservesRepresentation: Bool { true }
353+
public var canForwardGuaranteedValues: Bool { true }
354+
public var canForwardOwnedValues: Bool { false }
355+
}
356+
350357
extension InitExistentialRefInst : ForwardingInstruction {
351358
public var preservesIdentity: Bool { false }
352359
public var preservesRepresentation: Bool { true }

include/swift/Basic/Features.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ EXPERIMENTAL_FEATURE(DynamicActorIsolation, false)
290290
EXPERIMENTAL_FEATURE(BorrowingSwitch, true)
291291

292292
// Enable isolated(any) attribute on function types.
293-
EXPERIMENTAL_FEATURE(IsolatedAny, false)
293+
EXPERIMENTAL_FEATURE(IsolatedAny, true)
294294

295295
#undef EXPERIMENTAL_FEATURE_EXCLUDED_FROM_MODULE_INTERFACE
296296
#undef EXPERIMENTAL_FEATURE

lib/SILOptimizer/Mandatory/OwnershipModelEliminator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ struct OwnershipModelEliminatorVisitor
219219
HANDLE_FORWARDING_INST(LinearFunctionExtract)
220220
HANDLE_FORWARDING_INST(DifferentiableFunctionExtract)
221221
HANDLE_FORWARDING_INST(MarkUninitialized)
222+
HANDLE_FORWARDING_INST(FunctionExtractIsolation)
222223
#undef HANDLE_FORWARDING_INST
223224
};
224225

lib/SILOptimizer/SemanticARC/SemanticARCOptVisitor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ struct LLVM_LIBRARY_VISIBILITY SemanticARCOptVisitor
196196
FORWARDING_INST(LinearFunction)
197197
FORWARDING_INST(DifferentiableFunctionExtract)
198198
FORWARDING_INST(LinearFunctionExtract)
199+
FORWARDING_INST(FunctionExtractIsolation)
199200
#undef FORWARDING_INST
200201

201202
bool processWorklist();

stdlib/public/Concurrency/Actor.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,11 @@ internal func _enqueueOnMain(_ job: UnownedJob)
8585
public macro isolation<T>() -> T = Builtin.IsolationMacro
8686
#endif
8787

88+
#if $IsolatedAny
89+
@available(SwiftStdlib 5.1, *)
90+
public func extractIsolation<each Arg, Result>(
91+
_ fn: @escaping @isolated(any) (repeat each Arg) async throws -> Result
92+
) -> (any Actor)? {
93+
return Builtin.extractFunctionIsolation(fn)
94+
}
95+
#endif

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ else()
6060
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS "-fswift-async-fp=never")
6161
endif()
6262

63+
list(APPEND SWIFT_RUNTIME_CONCURRENCY_SWIFT_FLAGS
64+
"-enable-experimental-feature"
65+
"IsolatedAny"
66+
)
6367

6468
list(APPEND SWIFT_RUNTIME_CONCURRENCY_C_FLAGS
6569
"-D__STDC_WANT_LIB_EXT1__=1")

test/Concurrency/isolated_any.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ func globalNonisolatedFunction() {}
77

88
actor A {
99
func actorFunction() {}
10+
func asyncActorFunction() async {}
11+
func asyncThrowsActorFunction() async throws {}
12+
func actorFunctionWithArgs(value: Int) async -> String { "" }
1013
}
1114

1215
func testBasic_sync() {
@@ -71,3 +74,12 @@ func testConvertIsolatedAnyToMainActor(fn: @Sendable @isolated(any) () -> ()) {
7174
// expected-error @+1 {{cannot convert value of type '@isolated(any) @Sendable () -> ()' to expected argument type '@MainActor @Sendable () -> ()'}}
7275
requireSendableGlobalActor(fn)
7376
}
77+
78+
func extractFunctionIsolation(_ fn: @isolated(any) @escaping () async -> Void) {
79+
let _: (any Actor)? = extractIsolation(fn)
80+
81+
let myActor = A()
82+
let _: (any Actor)? = extractIsolation(myActor.asyncActorFunction)
83+
let _: (any Actor)? = extractIsolation(myActor.asyncThrowsActorFunction)
84+
let _: (any Actor)? = extractIsolation(myActor.actorFunctionWithArgs(value:))
85+
}

0 commit comments

Comments
 (0)