From 2358cc973d2f7851a66924468fd4d75ccb11d8d6 Mon Sep 17 00:00:00 2001 From: Kuba Mracek Date: Tue, 15 Oct 2024 12:59:00 -0700 Subject: [PATCH] [Concurrency] Split out the default cooperative executor into a separate opt-in library --- stdlib/public/Concurrency/CMakeLists.txt | 34 ++++++++- test/embedded/concurrency-actors.swift | 3 +- test/embedded/concurrency-async-let.swift | 3 +- test/embedded/concurrency-continuations.swift | 3 +- test/embedded/concurrency-simple.swift | 3 +- test/embedded/concurrency-stream.swift | 3 +- test/embedded/concurrency-taskgroup.swift | 3 +- test/embedded/concurrency-taskgroup2.swift | 3 +- test/embedded/dependencies-concurrency.swift | 2 +- test/embedded/dependencies-concurrency2.swift | 72 +++++++++++++++++++ 10 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 test/embedded/dependencies-concurrency2.swift diff --git a/stdlib/public/Concurrency/CMakeLists.txt b/stdlib/public/Concurrency/CMakeLists.txt index 2b86e1cd4e5bb..f12ff3c171002 100644 --- a/stdlib/public/Concurrency/CMakeLists.txt +++ b/stdlib/public/Concurrency/CMakeLists.txt @@ -256,6 +256,8 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded") set(SWIFT_SDK_embedded_ARCH_${mod}_TRIPLE "${triple}") + # lib/swift/embedded/_Concurrency.swiftmodule + # lib/swift/embedded//libswift_Concurrency.a add_swift_target_library_single( embedded-concurrency-${mod} swift_Concurrency @@ -264,7 +266,6 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC ${SWIFT_RUNTIME_CONCURRENCY_C_SOURCES} ${SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES} - CooperativeGlobalExecutor.cpp SWIFT_COMPILE_FLAGS ${extra_swift_compile_flags} -enable-experimental-feature Embedded @@ -291,8 +292,37 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE ) set_property(TARGET embedded-concurrency-${mod} PROPERTY OSX_ARCHITECTURES "${arch}") - add_dependencies(embedded-concurrency embedded-concurrency-${mod}) + + # lib/swift/embedded//libswift_ConcurrencyDefaultExecutor.a + add_swift_target_library_single( + embedded-concurrency-default-executor-${mod} + swift_ConcurrencyDefaultExecutor + STATIC + IS_FRAGILE + + CooperativeGlobalExecutor.cpp + + C_COMPILE_FLAGS ${extra_c_compile_flags} + MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded" + SDK "embedded" + ARCHITECTURE "${mod}" + DEPENDS embedded-stdlib-${mod} + INSTALL_IN_COMPONENT stdlib + ) + swift_install_in_component( + TARGETS embedded-concurrency-default-executor-${mod} + DESTINATION "lib/swift/embedded/${mod}" + COMPONENT "stdlib" + ) + swift_install_in_component( + FILES "${SWIFTLIB_DIR}/embedded/${mod}/libswift_ConcurrencyDefaultExecutor.a" + DESTINATION "lib/swift/embedded/${mod}/" + COMPONENT "stdlib" + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE + ) + set_property(TARGET embedded-concurrency-default-executor-${mod} PROPERTY OSX_ARCHITECTURES "${arch}") + add_dependencies(embedded-concurrency embedded-concurrency-default-executor-${mod}) endforeach() # Copy the ExecutorImpl.h header into the local include directory diff --git a/test/embedded/concurrency-actors.swift b/test/embedded/concurrency-actors.swift index 53d3b4ea8a3a2..517a61770cd2d 100644 --- a/test/embedded/concurrency-actors.swift +++ b/test/embedded/concurrency-actors.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/concurrency-async-let.swift b/test/embedded/concurrency-async-let.swift index 10869bf25c22a..b84b6accb3705 100644 --- a/test/embedded/concurrency-async-let.swift +++ b/test/embedded/concurrency-async-let.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/concurrency-continuations.swift b/test/embedded/concurrency-continuations.swift index 04bfd9a848fcd..3de06a15c0d0b 100644 --- a/test/embedded/concurrency-continuations.swift +++ b/test/embedded/concurrency-continuations.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/concurrency-simple.swift b/test/embedded/concurrency-simple.swift index 7561ae13a0d09..ee4dff08c7423 100644 --- a/test/embedded/concurrency-simple.swift +++ b/test/embedded/concurrency-simple.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/concurrency-stream.swift b/test/embedded/concurrency-stream.swift index cc4538e3873d5..95585f64931c5 100644 --- a/test/embedded/concurrency-stream.swift +++ b/test/embedded/concurrency-stream.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -target %target-cpu-apple-macos14 -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/concurrency-taskgroup.swift b/test/embedded/concurrency-taskgroup.swift index 24809c35aba0e..1f8851db6358a 100644 --- a/test/embedded/concurrency-taskgroup.swift +++ b/test/embedded/concurrency-taskgroup.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -target %target-cpu-apple-macos14 -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/concurrency-taskgroup2.swift b/test/embedded/concurrency-taskgroup2.swift index dad3138be1690..9ee439d01b900 100644 --- a/test/embedded/concurrency-taskgroup2.swift +++ b/test/embedded/concurrency-taskgroup2.swift @@ -1,7 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -enable-experimental-feature Embedded -target %target-cpu-apple-macos14 -parse-as-library %s -c -o %t/a.o -// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o -// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: %target-run %t/a.out | %FileCheck %s // REQUIRES: executable_test diff --git a/test/embedded/dependencies-concurrency.swift b/test/embedded/dependencies-concurrency.swift index 5c2bd687eecd1..d284bb524be5f 100644 --- a/test/embedded/dependencies-concurrency.swift +++ b/test/embedded/dependencies-concurrency.swift @@ -1,6 +1,6 @@ // RUN: %empty-directory(%t) // RUN: %target-swift-frontend -target %target-cpu-apple-macos14 -disable-availability-checking -parse-as-library -enable-experimental-feature Embedded %s -c -o %t/a.o -// RUN: %target-clang %t/a.o -o %t/a.out %swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos/libswift_Concurrency.a -dead_strip +// 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 // RUN: grep DEP\: %s | sed 's#// DEP\: ##' | sort > %t/allowed-dependencies.txt diff --git a/test/embedded/dependencies-concurrency2.swift b/test/embedded/dependencies-concurrency2.swift new file mode 100644 index 0000000000000..257c7872ce907 --- /dev/null +++ b/test/embedded/dependencies-concurrency2.swift @@ -0,0 +1,72 @@ +// RUN: %empty-directory(%t) +// RUN: %target-swift-frontend -target %target-cpu-apple-macos14 -disable-availability-checking -parse-as-library -enable-experimental-feature Embedded %s -c -o %t/a.o +// RUN: %target-clang -nostdlib -lSystem %t/a.o -o %t/a.out -L%swift_obj_root/lib/swift/embedded/%target-cpu-apple-macos -lswift_Concurrency -dead_strip -Wl,-undefined,dynamic_lookup + +// RUN: grep DEP\: %s | sed 's#// DEP\: ##' | sort > %t/allowed-dependencies.txt + +// RUN: %llvm-nm --undefined-only --format=just-symbols %t/a.out | sort | tee %t/actual-dependencies.txt + +// Fail if there is any entry in actual-dependencies.txt that's not in allowed-dependencies.txt +// RUN: test -z "`comm -13 %t/allowed-dependencies.txt %t/actual-dependencies.txt`" + +// DEP: ___assert_rtn +// DEP: ___stack_chk_fail +// DEP: ___stack_chk_guard +// DEP: _abort +// DEP: _exit +// DEP: _free +// DEP: _malloc +// DEP: _memmove +// DEP: _memset +// DEP: _memset_s +// DEP: _posix_memalign +// DEP: _putchar +// DEP: _puts +// DEP: _strlen +// DEP: _swift_task_asyncMainDrainQueueImpl +// DEP: _swift_task_enqueueGlobalImpl +// DEP: _swift_task_getMainExecutorImpl +// DEP: _swift_task_asyncMainDrainQueueImpl +// DEP: _swift_task_checkIsolatedImpl +// DEP: _swift_task_donateThreadToGlobalExecutorUntilImpl +// DEP: _swift_task_enqueueGlobalImpl +// DEP: _swift_task_enqueueGlobalWithDeadlineImpl +// DEP: _swift_task_enqueueGlobalWithDelayImpl +// DEP: _swift_task_enqueueMainExecutorImpl +// DEP: _swift_task_getMainExecutorImpl +// DEP: _swift_task_isMainExecutorImpl +// DEP: _vprintf +// DEP: _vsnprintf +// DEP: dyld_stub_binder + +// REQUIRES: swift_in_compiler +// REQUIRES: optimized_stdlib +// REQUIRES: OS=macosx + +import _Concurrency + +public func test() async -> Int { + print("test") + let t = Task { + print("return 42") + return 42 + } + print("await") + let v = await t.value + print("return") + return v +} + +@main +struct Main { + static func main() async { + print("main") + let t = Task { + print("task") + let x = await test() + print(x == 42 ? "42" : "???") + } + print("after task") + await t.value + } +}