From 7441e039d0a6c0b4faa8ba1ab586219f232ba75a Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 29 Feb 2024 13:37:08 +0000 Subject: [PATCH] wasm: Add "_multithreaded" as valid `_runtime` argument 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. --- lib/Basic/LangOptions.cpp | 16 ++++++++++++++++ .../ConditionalCompilation/noneOSTarget.swift | 8 ++++++++ .../ConditionalCompilation/wasm32Target.swift | 4 ++++ .../wasm32ThreadsTarget.swift | 8 ++++++++ 4 files changed, 36 insertions(+) create mode 100644 test/Parse/ConditionalCompilation/noneOSTarget.swift create mode 100644 test/Parse/ConditionalCompilation/wasm32ThreadsTarget.swift diff --git a/lib/Basic/LangOptions.cpp b/lib/Basic/LangOptions.cpp index 13441dc76ae50..53a0e327dcd5b 100644 --- a/lib/Basic/LangOptions.cpp +++ b/lib/Basic/LangOptions.cpp @@ -111,6 +111,7 @@ static const SupportedConditionalValue SupportedConditionalCompilationPointerBit static const SupportedConditionalValue SupportedConditionalCompilationRuntimes[] = { "_ObjC", "_Native", + "_multithreaded", }; static const SupportedConditionalValue SupportedConditionalCompilationTargetEnvironments[] = { @@ -400,6 +401,16 @@ void LangOptions::setHasAtomicBitWidth(llvm::Triple triple) { } } +static bool isMultiThreadedRuntime(llvm::Triple triple) { + if (triple.getOS() == llvm::Triple::WASI) { + return triple.getEnvironmentName() == "threads"; + } + if (triple.getOSName() == "none") { + return false; + } + return true; +} + std::pair LangOptions::setTarget(llvm::Triple triple) { clearAllPlatformConditionValues(); clearAtomicBitWidths(); @@ -572,6 +583,11 @@ std::pair LangOptions::setTarget(llvm::Triple triple) { addPlatformConditionValue(PlatformConditionKind::TargetEnvironment, "macabi"); + if (isMultiThreadedRuntime(Target)) { + addPlatformConditionValue(PlatformConditionKind::Runtime, + "_multithreaded"); + } + // Set the "_hasHasAtomicBitWidth" platform condition. setHasAtomicBitWidth(triple); diff --git a/test/Parse/ConditionalCompilation/noneOSTarget.swift b/test/Parse/ConditionalCompilation/noneOSTarget.swift new file mode 100644 index 0000000000000..c17cfe8b3feb6 --- /dev/null +++ b/test/Parse/ConditionalCompilation/noneOSTarget.swift @@ -0,0 +1,8 @@ +// RUN: %swift -typecheck %s -verify -target arm64-apple-none-macho -parse-stdlib +// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target arm64-apple-none-macho + +#if !_runtime(_multithreaded) + func underNoThreads() { + foo() // expected-error {{cannot find 'foo' in scope}} + } +#endif diff --git a/test/Parse/ConditionalCompilation/wasm32Target.swift b/test/Parse/ConditionalCompilation/wasm32Target.swift index 8ca38d67f14ae..4ff8f5d9a2884 100644 --- a/test/Parse/ConditionalCompilation/wasm32Target.swift +++ b/test/Parse/ConditionalCompilation/wasm32Target.swift @@ -8,3 +8,7 @@ var x = C() #endif #endif var y = x + +#if !_runtime(_multithreaded) + let z = xx // expected-error {{cannot find 'xx' in scope}} +#endif diff --git a/test/Parse/ConditionalCompilation/wasm32ThreadsTarget.swift b/test/Parse/ConditionalCompilation/wasm32ThreadsTarget.swift new file mode 100644 index 0000000000000..a61b548facb2a --- /dev/null +++ b/test/Parse/ConditionalCompilation/wasm32ThreadsTarget.swift @@ -0,0 +1,8 @@ +// RUN: %swift -typecheck %s -verify -target wasm32-unknown-wasip1-threads -parse-stdlib +// RUN: %swift-ide-test -test-input-complete -source-filename=%s -target wasm32-unknown-wasip1-threads + +#if _runtime(_multithreaded) + func underThreads() { + foo() // expected-error {{cannot find 'foo' in scope}} + } +#endif