Skip to content

[embedded] Default Embedded Concurrency to SWIFT_THREADING_PACKAGE=none #72769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 5, 2024

Conversation

kubamracek
Copy link
Contributor

@kubamracek kubamracek commented Apr 2, 2024

The Concurrency runtime (including Embedded Concurrency) can use different underlaying threading API via the SWIFT_THREADING_PACKAGE setting. The different options (C11 threads, PThreads, Darwin, etc.) have a large effect on what dependencies are expected to be provided by the platform. Given that Embedded Swift needs to support a variety of environments, including ones that do not have any threading APIs, the choice of the threading API should probably be a user-configurable option. Until we flesh that out, let's in the meantime default to building Embedded Concurrency as SWIFT_THREADING_PACKAGE=none -- which does not require any threading API (and e.g. locks are simply no-ops) and assumes we have a single-threaded environment.

@rauhul
Copy link
Member

rauhul commented Apr 2, 2024

Does this imply import _Concurrency would work?

@kubamracek
Copy link
Contributor Author

import _Concurrency works with a lot of caveats today, see https://github.com/apple/swift/blob/main/test/embedded/concurrency-actors.swift -- the biggest caveat is that Concurrency is only provided for macOS. This change simplifies the way to get import _Concurrency to also work on real embedded setups, but doesn't directly achieve that, there's still more work needed.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@rauhul
Copy link
Member

rauhul commented Apr 2, 2024

interesting, I do really want to explore the space of no-allocation _Concurrency support looks like but thats unrelated to this change

Copy link
Member

@rauhul rauhul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, but I also dont understand cmake

@kubamracek kubamracek changed the title [embedded] Default Embedded Concurrency to single-threaded [embedded] Default Embedded Concurrency to SWIFT_THREADING_PACKAGE=none Apr 2, 2024
@kubamracek
Copy link
Contributor Author

@swift-ci please test macOS platform

@@ -199,6 +199,8 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
set(SWIFT_CONCURRENCY_USES_DISPATCH FALSE)
set(SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY TRUE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this change? We should want to eventually get to being able to specify SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default = hooked so that SWIFT_CONCURRENCY_GLOBAL_EXECUTOR is set to hooked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do it as a follow-up. If I just naively remove this line, I get...

Undefined symbols for architecture arm64:
  "_swift_reportError", referenced from:
      _swift_task_enqueueGlobal in libswift_Concurrency.a[7](GlobalExecutor.cpp.o)
      swift_task_enqueueGlobalImpl(swift::Job*) in libswift_Concurrency.a[7](GlobalExecutor.cpp.o)
      swift_task_asyncMainDrainQueueImpl() in libswift_Concurrency.a[11](Task.cpp.o)
ld: symbol(s) not found for architecture arm64

...because the hooked version (SWIFT_CONCURRENCY_GLOBAL_EXECUTOR_default = hooked) of Concurrency still brings in the stub implementations from NonDispatchGlobalExecutor.inc, and we should instead really just have these symbols undefined in the runtime, and expect the user to provide them and link them.

Copy link
Contributor

@rokhinip rokhinip Apr 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we should instead really just have these symbols undefined in the runtime, and expect the user to provide them and link them.

Don't we just want to funnel this to swift_fatal_error which is already hooked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems like the wrong solution -- it would certainly work, but we are going to need to keep cutting down dependencies and code in the Embedded Swift Concurrency runtime, and IIUC, in hooked mode, we expect the user/platform to provide swift_task_enqueueGlobal, and therefore the two symbols from the listing above (_swift_task_enqueueGlobal, swift_task_enqueueGlobalImpl ) should really just not be part of the hooked build of Concurrency. Doing that sounds like a double win (besides resolving this undefined symbol problem): (1) less code = better codesize, (2) turns a runtime failure into a link time error.

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize we made progress towards SWIFT_THREADING_PACKAGE that's good. I suspect we'd make use of that if we want to do a pure pthreads default executor etc.

@kubamracek
Copy link
Contributor Author

@swift-ci please test macOS platform

@kubamracek kubamracek force-pushed the embedded-concurrency-none branch from 32f5f2d to dcb60ab Compare April 4, 2024 22:38
@kubamracek
Copy link
Contributor Author

@swift-ci please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants