Skip to content

Commit d63bbb9

Browse files
authored
Merge pull request #83456 from MaxDesiatov/enqueue-existential
Embedded WASI: fix some concurrency tests The test was crashing due to `swift_unreachable("custom executors not supported in embedded Swift")` line in `swift_task_enqueueImpl`, as the corresponding non-embedded codepath was relying on an unspecialized generic function `_swift_task_enqueueOnExecutor` defined in `Executor.swift`. Unspecialized generics are unavailable in Embedded Swift, and such `@silgen_name` function can't be specialized when used from concurrency runtime code written in C/C++. We can redefine this function for Embedded Swift using a class-bound existential instead, and re-enable this codepath with a slightly different call that avoids the use of unavailable `swift_getObjectType` function from the non-embedded runtime. rdar://156996468
2 parents 37ae8a2 + 78c8594 commit d63bbb9

File tree

8 files changed

+39
-12
lines changed

8 files changed

+39
-12
lines changed

lib/SILOptimizer/UtilityPasses/Link.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ class SILLinker : public SILModuleTransform {
103103

104104
linkUsedFunctionByName("swift_task_asyncMainDrainQueueImpl",
105105
SILLinkage::HiddenExternal);
106+
linkUsedFunctionByName("_swift_task_enqueueOnExecutor",
107+
SILLinkage::HiddenExternal);
106108
linkUsedFunctionByName("swift_createDefaultExecutors",
107109
SILLinkage::HiddenExternal);
108110
linkUsedFunctionByName("swift_getDefaultExecutor",

stdlib/public/Concurrency/Actor.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,15 @@ extern "C" SWIFT_CC(swift) void _swift_task_enqueueOnTaskExecutor(
485485
// Implemented in Swift to avoid some annoying hard-coding about
486486
// SerialExecutor's protocol witness table. We could inline this
487487
// with effort, though.
488+
#if SWIFT_CONCURRENCY_EMBEDDED
489+
extern "C" SWIFT_CC(swift) void _swift_task_enqueueOnExecutor(
490+
Job *job, HeapObject *executor,
491+
const SerialExecutorWitnessTable *wtable);
492+
#else
488493
extern "C" SWIFT_CC(swift) void _swift_task_enqueueOnExecutor(
489494
Job *job, HeapObject *executor, const Metadata *executorType,
490495
const SerialExecutorWitnessTable *wtable);
496+
#endif // #if SWIFT_CONCURRENCY_EMBEDDED
491497

492498
SWIFT_CC(swift)
493499
static bool swift_task_isCurrentExecutorWithFlagsImpl(
@@ -2704,7 +2710,11 @@ static void swift_task_enqueueImpl(Job *job, SerialExecutorRef serialExecutorRef
27042710
return swift_defaultActor_enqueue(job, serialExecutorRef.getDefaultActor());
27052711
}
27062712

2707-
#if SWIFT_CONCURRENCY_EMBEDDED
2713+
#if SWIFT_CONCURRENCY_EMBEDDED && defined(__wasi__)
2714+
auto serialExecutorIdentity = serialExecutorRef.getIdentity();
2715+
auto serialExecutorWtable = serialExecutorRef.getSerialExecutorWitnessTable();
2716+
_swift_task_enqueueOnExecutor(job, serialExecutorIdentity, serialExecutorWtable);
2717+
#elif SWIFT_CONCURRENCY_EMBEDDED
27082718
swift_unreachable("custom executors not supported in embedded Swift");
27092719
#else
27102720
// For main actor or actors with custom executors

stdlib/public/Concurrency/Executor.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,20 @@ internal func _task_taskExecutor_getTaskExecutorRef<E>(_ taskExecutor: E) -> Bui
937937
return taskExecutor.asUnownedTaskExecutor().executor
938938
}
939939

940+
#if $Embedded
941+
@_silgen_name("_swift_task_enqueueOnExecutor")
942+
internal func _enqueueOnExecutor(job unownedJob: UnownedJob, executor: any SerialExecutor) {
943+
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
944+
if #available(StdlibDeploymentTarget 5.9, *) {
945+
executor.enqueue(ExecutorJob(context: unownedJob._context))
946+
} else {
947+
fatalError("we shouldn't get here; if we have, availability is broken")
948+
}
949+
#else // SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
950+
executor.enqueue(unownedJob)
951+
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
952+
}
953+
#else
940954
// Used by the concurrency runtime
941955
@available(SwiftStdlib 5.1, *)
942956
@_silgen_name("_swift_task_enqueueOnExecutor")
@@ -952,6 +966,7 @@ where E: SerialExecutor {
952966
executor.enqueue(unownedJob)
953967
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
954968
}
969+
#endif // #if $Embedded
955970

956971
@_unavailableInEmbedded
957972
@available(SwiftStdlib 6.0, *)

test/embedded/concurrency-continuations.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o
3-
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos -lswift_Concurrency -lswift_ConcurrencyDefaultExecutor -dead_strip
3+
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%module-target-triple %target-clang-resource-dir-opt -lswift_Concurrency %target-swift-default-executor-opt -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

66
// REQUIRES: executable_test
77
// REQUIRES: swift_in_compiler
88
// REQUIRES: optimized_stdlib
9-
// REQUIRES: OS=macosx
9+
// REQUIRES: OS=macosx || OS=wasip1
1010
// REQUIRES: swift_feature_Embedded
1111

1212
import _Concurrency

test/embedded/concurrency-deleted-method.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library -module-name main %s -emit-ir | %FileCheck --check-prefix=CHECK-IR %s
33
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library -module-name main %s -c -o %t/a.o
44
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%module-target-triple %target-clang-resource-dir-opt -lswift_Concurrency %target-swift-default-executor-opt -dead_strip
5-
// RUN: if [ %target-os != "wasip1" ]; then %target-run %t/a.out | %FileCheck %s; fi
5+
// RUN: %target-run %t/a.out | %FileCheck %s
66

77
// REQUIRES: executable_test
88
// REQUIRES: swift_in_compiler

test/embedded/concurrency-modules.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
55
// RUN: %target-swift-frontend -c -I %t %t/Main.swift -enable-experimental-feature Embedded -o %t/a.o -parse-as-library
6-
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos -lswift_Concurrency -lswift_ConcurrencyDefaultExecutor -dead_strip
6+
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%module-target-triple %target-clang-resource-dir-opt -lswift_Concurrency %target-swift-default-executor-opt -dead_strip
77
// RUN: %target-run %t/a.out | %FileCheck %s
88

99
// REQUIRES: swift_in_compiler
1010
// REQUIRES: executable_test
1111
// REQUIRES: optimized_stdlib
12-
// REQUIRES: OS=macosx
12+
// REQUIRES: OS=macosx || OS=wasip1
1313
// REQUIRES: swift_feature_Embedded
1414

1515
// BEGIN MyModule.swift

test/embedded/concurrency-stream.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -target %target-cpu-apple-macos14 -parse-as-library %s -c -o %t/a.o
3-
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos -lswift_Concurrency -lswift_ConcurrencyDefaultExecutor -dead_strip
2+
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o
3+
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%module-target-triple %target-clang-resource-dir-opt -lswift_Concurrency %target-swift-default-executor-opt -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

66
// REQUIRES: executable_test
77
// REQUIRES: swift_in_compiler
88
// REQUIRES: optimized_stdlib
9-
// REQUIRES: OS=macosx
9+
// REQUIRES: OS=macosx || OS=wasip1
1010
// REQUIRES: swift_feature_Embedded
1111

1212
import _Concurrency

test/embedded/concurrency-typed-throws.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -target %target-cpu-apple-macos14 -parse-as-library %s -c -o %t/a.o
3-
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos -lswift_Concurrency -lswift_ConcurrencyDefaultExecutor -dead_strip
2+
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o
3+
// RUN: %target-clang %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%module-target-triple %target-clang-resource-dir-opt -lswift_Concurrency %target-swift-default-executor-opt -dead_strip
44
// RUN: %target-run %t/a.out | %FileCheck %s
55

66
// REQUIRES: executable_test
77
// REQUIRES: swift_in_compiler
88
// REQUIRES: optimized_stdlib
9-
// REQUIRES: OS=macosx
9+
// REQUIRES: OS=macosx || OS=wasip1
1010
// REQUIRES: swift_feature_Embedded
1111

1212
import _Concurrency

0 commit comments

Comments
 (0)