Skip to content

Commit 9858f39

Browse files
authored
Merge pull request #70472 from tshortli/back-deploy-assertassume-isolation
[SE-0392] Back-deploy assertIsolation/assumeIsolation
2 parents ac13ef5 + 0fbbc6a commit 9858f39

File tree

8 files changed

+50
-63
lines changed

8 files changed

+50
-63
lines changed

lib/SILGen/SILGen.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,6 @@ FuncDecl *SILGenModule::getAsyncMainDrainQueue() {
503503
"_asyncMainDrainQueue");
504504
}
505505

506-
FuncDecl *SILGenModule::getGetMainExecutor() {
507-
return lookupConcurrencyIntrinsic(getASTContext(), GetMainExecutor,
508-
"_getMainExecutor");
509-
}
510-
511506
FuncDecl *SILGenModule::getSwiftJobRun() {
512507
return lookupConcurrencyIntrinsic(getASTContext(), SwiftJobRun,
513508
"_swiftJobRun");

lib/SILGen/SILGen.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
583583

584584
/// Retrieve the _Concurrency._asyncMainDrainQueue intrinsic.
585585
FuncDecl *getAsyncMainDrainQueue();
586-
/// Retrieve the _Concurrency._getMainExecutor intrinsic.
587-
FuncDecl *getGetMainExecutor();
588586
/// Retrieve the _Concurrency._swiftJobRun intrinsic.
589587
FuncDecl *getSwiftJobRun();
590588
// Retrieve the _SwiftConcurrencyShims.exit intrinsic.

lib/SILGen/SILGenProlog.cpp

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,36 +1449,12 @@ void SILGenFunction::emitProlog(
14491449
}
14501450

14511451
SILValue SILGenFunction::emitMainExecutor(SILLocation loc) {
1452-
// Get main executor
1453-
FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor();
1454-
if (!getMainExecutorFuncDecl) {
1455-
// If it doesn't exist due to an SDK-compiler mismatch, we can conjure one
1456-
// up instead of crashing:
1457-
// @available(SwiftStdlib 5.1, *)
1458-
// @_silgen_name("swift_task_getMainExecutor")
1459-
// internal func _getMainExecutor() -> Builtin.Executor
1460-
auto &ctx = getASTContext();
1461-
1462-
ParameterList *emptyParams = ParameterList::createEmpty(ctx);
1463-
getMainExecutorFuncDecl = FuncDecl::createImplicit(
1464-
ctx, StaticSpellingKind::None,
1465-
DeclName(
1466-
ctx,
1467-
DeclBaseName(ctx.getIdentifier("_getMainExecutor")),
1468-
/*Arguments*/ emptyParams),
1469-
{}, /*async*/ false, /*throws*/ false, /*thrownType*/Type(), {},
1470-
emptyParams, ctx.TheExecutorType,
1471-
getModule().getSwiftModule());
1472-
getMainExecutorFuncDecl->getAttrs().add(
1473-
new (ctx) SILGenNameAttr("swift_task_getMainExecutor", /*raw*/ false,
1474-
/*implicit*/ true));
1475-
}
1452+
auto &ctx = getASTContext();
1453+
auto builtinName = ctx.getIdentifier(
1454+
getBuiltinName(BuiltinValueKind::BuildMainActorExecutorRef));
1455+
auto resultType = SILType::getPrimitiveObjectType(ctx.TheExecutorType);
14761456

1477-
auto fn = SGM.getFunction(
1478-
SILDeclRef(getMainExecutorFuncDecl, SILDeclRef::Kind::Func),
1479-
NotForDefinition);
1480-
SILValue fnRef = B.createFunctionRefFor(loc, fn);
1481-
return B.createApply(loc, fnRef, {}, {});
1457+
return B.createBuiltin(loc, builtinName, resultType, {}, {});
14821458
}
14831459

14841460
SILValue SILGenFunction::emitGenericExecutor(SILLocation loc) {

stdlib/public/Concurrency/ExecutorAssertions.swift

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SwiftShims
1818
// ==== -----------------------------------------------------------------------
1919
// MARK: Precondition executors
2020

21-
@available(SwiftStdlib 5.9, *)
21+
@available(SwiftStdlib 5.1, *)
2222
extension SerialExecutor {
2323
/// Unconditionally if the current task is executing on the expected serial executor,
2424
/// and if not crash the program offering information about the executor mismatch.
@@ -35,7 +35,10 @@ extension SerialExecutor {
3535
/// * In `-Ounchecked` builds, the optimizer may assume that this function is
3636
/// never called. Failure to satisfy that assumption is a serious
3737
/// programming error.
38-
@available(SwiftStdlib 5.9, *)
38+
@available(SwiftStdlib 5.1, *)
39+
#if !$Embedded
40+
@backDeployed(before: SwiftStdlib 5.9)
41+
#endif
3942
@_unavailableInEmbedded
4043
public func preconditionIsolated(
4144
_ message: @autoclosure () -> String = String(),
@@ -55,7 +58,7 @@ extension SerialExecutor {
5558
}
5659
}
5760

58-
@available(SwiftStdlib 5.9, *)
61+
@available(SwiftStdlib 5.1, *)
5962
extension Actor {
6063
/// Unconditionally if the current task is executing on the serial executor of the passed in `actor`,
6164
/// and if not crash the program offering information about the executor mismatch.
@@ -72,7 +75,10 @@ extension Actor {
7275
/// * In `-Ounchecked` builds, the optimizer may assume that this function is
7376
/// never called. Failure to satisfy that assumption is a serious
7477
/// programming error.
75-
@available(SwiftStdlib 5.9, *)
78+
@available(SwiftStdlib 5.1, *)
79+
#if !$Embedded
80+
@backDeployed(before: SwiftStdlib 5.9)
81+
#endif
7682
@_unavailableInEmbedded
7783
public nonisolated func preconditionIsolated(
7884
_ message: @autoclosure () -> String = String(),
@@ -92,7 +98,7 @@ extension Actor {
9298
}
9399
}
94100

95-
@available(SwiftStdlib 5.9, *)
101+
@available(SwiftStdlib 5.1, *)
96102
extension GlobalActor {
97103
/// Unconditionally if the current task is executing on the serial executor of the passed in `actor`,
98104
/// and if not crash the program offering information about the executor mismatch.
@@ -109,7 +115,10 @@ extension GlobalActor {
109115
/// * In `-Ounchecked` builds, the optimizer may assume that this function is
110116
/// never called. Failure to satisfy that assumption is a serious
111117
/// programming error.
112-
@available(SwiftStdlib 5.9, *)
118+
@available(SwiftStdlib 5.1, *)
119+
#if !$Embedded
120+
@backDeployed(before: SwiftStdlib 5.9)
121+
#endif
113122
@_unavailableInEmbedded
114123
public static func preconditionIsolated(
115124
_ message: @autoclosure () -> String = String(),
@@ -122,7 +131,7 @@ extension GlobalActor {
122131
// ==== -----------------------------------------------------------------------
123132
// MARK: Assert executors
124133

125-
@available(SwiftStdlib 5.9, *)
134+
@available(SwiftStdlib 5.1, *)
126135
extension SerialExecutor {
127136
/// Performs an executor check in debug builds.
128137
///
@@ -136,7 +145,10 @@ extension SerialExecutor {
136145
/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer
137146
/// may assume that it *always* evaluates to `true`. Failure to satisfy that
138147
/// assumption is a serious programming error.
139-
@available(SwiftStdlib 5.9, *)
148+
@available(SwiftStdlib 5.1, *)
149+
#if !$Embedded
150+
@backDeployed(before: SwiftStdlib 5.9)
151+
#endif
140152
@_unavailableInEmbedded
141153
public func assertIsolated(
142154
_ message: @autoclosure () -> String = String(),
@@ -156,7 +168,7 @@ extension SerialExecutor {
156168
}
157169
}
158170

159-
@available(SwiftStdlib 5.9, *)
171+
@available(SwiftStdlib 5.1, *)
160172
extension Actor {
161173
/// Performs an executor check in debug builds.
162174
///
@@ -170,7 +182,10 @@ extension Actor {
170182
/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer
171183
/// may assume that it *always* evaluates to `true`. Failure to satisfy that
172184
/// assumption is a serious programming error.
173-
@available(SwiftStdlib 5.9, *)
185+
@available(SwiftStdlib 5.1, *)
186+
#if !$Embedded
187+
@backDeployed(before: SwiftStdlib 5.9)
188+
#endif
174189
@_unavailableInEmbedded
175190
public nonisolated func assertIsolated(
176191
_ message: @autoclosure () -> String = String(),
@@ -191,7 +206,7 @@ extension Actor {
191206
}
192207
}
193208

194-
@available(SwiftStdlib 5.9, *)
209+
@available(SwiftStdlib 5.1, *)
195210
extension GlobalActor {
196211
/// Performs an executor check in debug builds.
197212
///
@@ -205,7 +220,10 @@ extension GlobalActor {
205220
/// * In `-Ounchecked` builds, `condition` is not evaluated, but the optimizer
206221
/// may assume that it *always* evaluates to `true`. Failure to satisfy that
207222
/// assumption is a serious programming error.
208-
@available(SwiftStdlib 5.9, *)
223+
@available(SwiftStdlib 5.1, *)
224+
#if !$Embedded
225+
@backDeployed(before: SwiftStdlib 5.9)
226+
#endif
209227
@_unavailableInEmbedded
210228
public static func assertIsolated(
211229
_ message: @autoclosure () -> String = String(),
@@ -218,7 +236,7 @@ extension GlobalActor {
218236
// ==== -----------------------------------------------------------------------
219237
// MARK: Assume Executor
220238

221-
@available(SwiftStdlib 5.9, *)
239+
@available(SwiftStdlib 5.1, *)
222240
extension Actor {
223241
/// A safe way to synchronously assume that the current execution context belongs to the passed in actor.
224242
///
@@ -233,7 +251,10 @@ extension Actor {
233251
/// if another actor uses the same serial executor--by using that actor's ``Actor/unownedExecutor``
234252
/// as its own ``Actor/unownedExecutor``--this check will succeed, as from a concurrency safety
235253
/// perspective, the serial executor guarantees mutual exclusion of those two actors.
236-
@available(SwiftStdlib 5.9, *)
254+
@available(SwiftStdlib 5.1, *)
255+
#if !$Embedded
256+
@backDeployed(before: SwiftStdlib 5.9)
257+
#endif
237258
@_unavailableFromAsync(message: "express the closure as an explicit function declared on the specified 'actor' instead")
238259
@_unavailableInEmbedded
239260
public nonisolated func assumeIsolated<T>(

stdlib/public/Concurrency/MainActor.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ extension MainActor {
9999
}
100100
}
101101

102-
@available(SwiftStdlib 5.9, *)
102+
@available(SwiftStdlib 5.1, *)
103103
extension MainActor {
104104
/// A safe way to synchronously assume that the current execution context belongs to the MainActor.
105105
///
@@ -114,7 +114,8 @@ extension MainActor {
114114
/// if another actor uses the same serial executor--by using ``MainActor/sharedUnownedExecutor``
115115
/// as its own ``Actor/unownedExecutor``--this check will succeed, as from a concurrency safety
116116
/// perspective, the serial executor guarantees mutual exclusion of those two actors.
117-
@available(SwiftStdlib 5.9, *)
117+
@available(SwiftStdlib 5.1, *)
118+
@backDeployed(before: SwiftStdlib 5.9)
118119
@_unavailableFromAsync(message: "await the call to the @MainActor closure directly")
119120
public static func assumeIsolated<T>(
120121
_ operation: @MainActor () throws -> T,

test/Concurrency/Runtime/actor_assume_executor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ final class MainActorEcho {
100100

101101
let echo = MainActorEcho()
102102

103-
if #available(SwiftStdlib 5.9, *) {
103+
if #available(SwiftStdlib 5.1, *) {
104104
// === MainActor --------------------------------------------------------
105105

106106
tests.test("MainActor.assumeIsolated: assume the main executor, from 'main() async'") {

test/Concurrency/async_main.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,10 @@ func asyncFunc() async {
8080
// CHECK-SIL-NEXT: %12 = function_ref @swift_job_run : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
8181
// CHECK-SIL-NEXT: %13 = builtin "convertTaskToJob"(%11 : $Builtin.NativeObject) : $Builtin.Job
8282
// CHECK-SIL-NEXT: %14 = struct $UnownedJob (%13 : $Builtin.Job)
83-
// CHECK-SIL-NEXT: // function_ref swift_task_getMainExecutor
84-
// CHECK-SIL-NEXT: %15 = function_ref @swift_task_getMainExecutor : $@convention(thin) () -> Builtin.Executor
85-
// CHECK-SIL-NEXT: %16 = apply %15() : $@convention(thin) () -> Builtin.Executor
86-
// CHECK-SIL-NEXT: %17 = struct $UnownedSerialExecutor (%16 : $Builtin.Executor)
87-
// CHECK-SIL-NEXT: %18 = apply %12(%14, %17) : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
83+
// CHECK-SIL-NEXT: %15 = builtin "buildMainActorExecutorRef"() : $Builtin.Executor
84+
// CHECK-SIL-NEXT: %16 = struct $UnownedSerialExecutor (%15 : $Builtin.Executor)
85+
// CHECK-SIL-NEXT: %17 = apply %12(%14, %16) : $@convention(thin) (UnownedJob, UnownedSerialExecutor) -> ()
8886
// CHECK-SIL-NEXT: // function_ref swift_task_asyncMainDrainQueue
89-
// CHECK-SIL-NEXT: %19 = function_ref @swift_task_asyncMainDrainQueue : $@convention(thin) () -> Never
90-
// CHECK-SIL-NEXT: %20 = apply %19() : $@convention(thin) () -> Never
87+
// CHECK-SIL-NEXT: %18 = function_ref @swift_task_asyncMainDrainQueue : $@convention(thin) () -> Never
88+
// CHECK-SIL-NEXT: %19 = apply %18() : $@convention(thin) () -> Never
9189
// CHECK-SIL-NEXT: unreachable

test/SILGen/toplevel_globalactorvars.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
// CHECK-LABEL: sil private [ossa] @async_Main
77
// CHECK: bb0:
8-
// CHECK-NEXT: // function_ref
9-
// CHECK-NEXT: [[GET_MAIN:%.*]] = function_ref @swift_task_getMainExecutor
10-
// CHECK-NEXT: [[MAIN:%.*]] = apply [[GET_MAIN]]()
8+
// CHECK-NEXT: [[MAIN:%.*]] = builtin "buildMainActorExecutorRef"() : $Builtin.Executor
119
// CHECK-NEXT: [[MAIN_OPTIONAL:%[0-9]+]] = enum $Optional<Builtin.Executor>, #Optional.some!enumelt, [[MAIN]]
1210

1311
actor MyActorImpl {}

0 commit comments

Comments
 (0)