Skip to content
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

Add "_multithreaded" as valid _runtime argument #72649

Conversation

kateinoigakukun
Copy link
Member

This patch adds "threads" as a valid targetEnvironment argument and sets it when the target is wasm32-unknown-wasi-threads.

@kateinoigakukun kateinoigakukun marked this pull request as draft March 28, 2024 03:01
@kateinoigakukun kateinoigakukun force-pushed the pr-4bfcd7ce97f156f7d629719f05149ee051ecf670 branch from 7efe712 to e85f246 Compare March 28, 2024 03:16
@kateinoigakukun kateinoigakukun marked this pull request as ready for review March 28, 2024 03:16
@kateinoigakukun
Copy link
Member Author

@swift-ci Please smoke test

@@ -8,3 +8,7 @@ var x = C()
#endif
#endif
var y = x

#if !targetEnvironment(threads)
Copy link
Contributor

@MaxDesiatov MaxDesiatov Mar 28, 2024

Choose a reason for hiding this comment

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

I'm still wondering whether #if !canImport(Synchronization) would be a better approach for something like this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I feel targetEnvironment(threads) is a very straightforward representation considering threads is the environment part of target triple and other environments like simulator in arm64-apple-ios-simulator or macabi in x86_64-apple-ios-macabi. Also I think it's not desirable situation to recommend people use canImport for detecting target, which is beyond the original meaning of canImport detecting module importability.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the confusion is that "threads" is not really an "environment", it's a platform feature. "The iOS simulator" is an environment, as is "Mac Catalyst". canImport() does seem closer to me to the right mechanism because the question you're asking is, approximately, "can I import the threading library?"

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, perhaps _runtime(_multithreaded)?

Copy link
Member Author

@kateinoigakukun kateinoigakukun Mar 28, 2024

Choose a reason for hiding this comment

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

-threads implies not only the ability to import the threading library but also different ABI and CPU features. But I agree that "threads" is not an actual environment as others, thus I prefer _runtime(_multithreaded) now. WDYT @MaxDesiatov ?

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW _runtime(_multithreaded), however we precisely spell it, would also be useful on other platforms (e.g. Embedded Swift) where the existence of multithreading is not otherwise knowable. I think for Darwin, Linux, and Windows, it would just need to be set to true.

Copy link
Contributor

Choose a reason for hiding this comment

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

would also be useful on other platforms (e.g. Embedded Swift)

+1

Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC in general we now prefer experimental feature flags to underscored symbols, any preferences on this one specifically?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think the feature is very trivial and it's not difficult to keep compatibility with future evolution. So adding a new "experimental feature" sounds too much for me.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think for Darwin, Linux, and Windows, it would just need to be set to true.

Agreed, it should just be true in all of those cases.

I'm still wondering whether #if !canImport(Synchronization) would be a better approach for something like this?

I would say not. There's no reason you shouldn't be able to e.g. import Synchronization on a system that has no threads; IMO synchronization primitives in that case should probably just do nothing, rather than being unimplemented.

@CodaFi
Copy link
Contributor

CodaFi commented Mar 29, 2024

I agree this should be a _runtime condition. Also, please be sure to update swift-syntax!

@kateinoigakukun kateinoigakukun force-pushed the pr-4bfcd7ce97f156f7d629719f05149ee051ecf670 branch from e85f246 to 10431a4 Compare March 29, 2024 06:27
This patch adds "_multithreaded" as a valid `_runtime` argument and
sets it when the target is `wasm32-unknown-wasi-threads` or other
non-none OS targets.
@kateinoigakukun kateinoigakukun force-pushed the pr-4bfcd7ce97f156f7d629719f05149ee051ecf670 branch from 10431a4 to 7441e03 Compare March 29, 2024 06:30
@kateinoigakukun
Copy link
Member Author

@swift-ci Please smoke test

@kateinoigakukun kateinoigakukun changed the title wasm: Add "threads" as valid targetEnvironment argument Add "_multithreaded" as valid _runtime argument Mar 29, 2024
@kateinoigakukun
Copy link
Member Author

Updated with _runtime(_multithreaded) approach.

@kateinoigakukun kateinoigakukun merged commit 706f065 into swiftlang:main Mar 29, 2024
3 checks passed
@kateinoigakukun kateinoigakukun deleted the pr-4bfcd7ce97f156f7d629719f05149ee051ecf670 branch March 29, 2024 15:53
@@ -111,6 +111,7 @@ static const SupportedConditionalValue SupportedConditionalCompilationPointerBit
static const SupportedConditionalValue SupportedConditionalCompilationRuntimes[] = {
"_ObjC",
"_Native",
"_multithreaded",
Copy link
Contributor

Choose a reason for hiding this comment

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

If this is the spelling we want, it should probably match the casing of the other valid values.

@grynspan
Copy link
Contributor

Is a swift-syntax change also needed?

@kateinoigakukun
Copy link
Member Author

As far as I checked, we don't need swift-syntax side support because it's handled as DeclReferenceExpr inside FunctionCallExpr.

grynspan added a commit to swiftlang/swift-testing that referenced this pull request Mar 31, 2024
This PR opts WASI builds into using `pthread_mutex_t` in `Locked` when the WASI environment supports threading.

`_runtime(_multithreaded)` was added very recently with swiftlang/swift#72649, so we need an additional compiler version check before testing the runtime flag.

WASI with Swift 5.10, as well as WASI without threading, will continue to stub out `Locked`.
grynspan added a commit to swiftlang/swift-testing that referenced this pull request Apr 1, 2024
)

This PR opts WASI builds into using `pthread_mutex_t` in `Locked` when
the WASI environment supports threading.

`_runtime(_multithreaded)` was added very recently with
swiftlang/swift#72649, so we need an additional
compiler version check before testing the runtime flag. That change has
not been cherry-picked to Swift 6.0, so assume 6.1 or later is needed.

WASI with Swift 5.10, as well as WASI without threading, will continue
to stub out `Locked`.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
@kateinoigakukun kateinoigakukun added the WebAssembly Platform: WebAssembly label Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WebAssembly Platform: WebAssembly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants