Skip to content

Commit 8f10dd8

Browse files
authored
Merge pull request swiftlang#175 from kateinoigakukun/fix-tests-part2
Fix tests part2
2 parents 7489bab + 1614c73 commit 8f10dd8

File tree

11 files changed

+328
-44
lines changed

11 files changed

+328
-44
lines changed

include/swift/Basic/Lazy.h

+15-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@
2525
#include "swift/Basic/type_traits.h"
2626

2727
#ifdef __wasi__
28-
void wasi_polyfill_call_once(int *flag, void *context, void (*func)(void *));
28+
inline void wasi_call_once(int *flag, void *context, void (*func)(void *)) {
29+
switch (*flag) {
30+
case 0:
31+
*flag = 1;
32+
func(context);
33+
return;
34+
case 1:
35+
return;
36+
default:
37+
assert(false && "wasi_call_once got invalid flag");
38+
abort();
39+
}
40+
}
41+
2942
#endif
3043

3144
namespace swift {
@@ -45,7 +58,7 @@ namespace swift {
4558
#elif defined(__wasi__)
4659
using OnceToken_t = int;
4760
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \
48-
::wasi_polyfill_call_once(&TOKEN, CONTEXT, FUNC)
61+
::wasi_call_once(&TOKEN, CONTEXT, FUNC)
4962
#else
5063
using OnceToken_t = std::once_flag;
5164
# define SWIFT_ONCE_F(TOKEN, FUNC, CONTEXT) \

lib/Driver/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(swiftDriver_sources
1515
ToolChains.cpp
1616
UnixToolChains.cpp
1717
WindowsToolChains.cpp
18+
WebAssemblyToolChains.cpp
1819
)
1920

2021
set(swiftDriver_targetDefines)

lib/Driver/Driver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ Driver::buildToolChain(const llvm::opt::InputArgList &ArgList) {
274274
case llvm::Triple::Haiku:
275275
return std::make_unique<toolchains::GenericUnix>(*this, target);
276276
case llvm::Triple::WASI:
277-
return std::make_unique<toolchains::GenericUnix>(*this, target);
277+
return std::make_unique<toolchains::WebAssembly>(*this, target);
278278
default:
279279
Diags.diagnose(SourceLoc(), diag::error_unknown_target,
280280
ArgList.getLastArg(options::OPT_target)->getValue());

lib/Driver/ToolChains.h

+17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ class LLVM_LIBRARY_VISIBILITY Windows : public ToolChain {
9292
bool shared = true) const override;
9393
};
9494

95+
class LLVM_LIBRARY_VISIBILITY WebAssembly : public ToolChain {
96+
protected:
97+
InvocationInfo constructInvocation(const AutolinkExtractJobAction &job,
98+
const JobContext &context) const override;
99+
InvocationInfo constructInvocation(const DynamicLinkJobAction &job,
100+
const JobContext &context) const override;
101+
InvocationInfo constructInvocation(const StaticLinkJobAction &job,
102+
const JobContext &context) const override;
103+
104+
public:
105+
WebAssembly(const Driver &D, const llvm::Triple &Triple) : ToolChain(D, Triple) {}
106+
~WebAssembly() = default;
107+
std::string sanitizerRuntimeLibName(StringRef Sanitizer,
108+
bool shared = true) const override;
109+
};
110+
111+
95112
class LLVM_LIBRARY_VISIBILITY GenericUnix : public ToolChain {
96113
protected:
97114
InvocationInfo constructInvocation(const InterpretJobAction &job,

lib/Driver/UnixToolChains.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,7 @@ toolchains::GenericUnix::constructInvocation(const StaticLinkJobAction &job,
346346
ArgStringList Arguments;
347347

348348
// Configure the toolchain.
349-
const char *AR;
350-
if (getTriple().isOSBinFormatWasm()) {
351-
AR = "llvm-ar";
352-
} else {
353-
AR = "ar";
354-
}
349+
const char *AR = "ar";
355350
Arguments.push_back("crs");
356351

357352
Arguments.push_back(

lib/Driver/WebAssemblyToolChains.cpp

+232
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
//===---- WebAssemblyToolChains.cpp - Job invocations (WebAssembly-specific) ------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "ToolChains.h"
14+
15+
#include "swift/Basic/Dwarf.h"
16+
#include "swift/Basic/LLVM.h"
17+
#include "swift/Basic/Platform.h"
18+
#include "swift/Basic/Range.h"
19+
#include "swift/Basic/TaskQueue.h"
20+
#include "swift/Config.h"
21+
#include "swift/Driver/Compilation.h"
22+
#include "swift/Driver/Driver.h"
23+
#include "swift/Driver/Job.h"
24+
#include "swift/Option/Options.h"
25+
#include "swift/Option/SanitizerOptions.h"
26+
#include "clang/Basic/Version.h"
27+
#include "clang/Driver/Util.h"
28+
#include "llvm/ADT/StringSwitch.h"
29+
#include "llvm/Option/Arg.h"
30+
#include "llvm/Option/ArgList.h"
31+
#include "llvm/ProfileData/InstrProf.h"
32+
#include "llvm/Support/FileSystem.h"
33+
#include "llvm/Support/Path.h"
34+
#include "llvm/Support/Process.h"
35+
#include "llvm/Support/Program.h"
36+
37+
using namespace swift;
38+
using namespace swift::driver;
39+
using namespace llvm::opt;
40+
41+
std::string toolchains::WebAssembly::sanitizerRuntimeLibName(StringRef Sanitizer,
42+
bool shared) const {
43+
return (Twine("clang_rt.") + Sanitizer + "-" +
44+
this->getTriple().getArchName() + ".lib")
45+
.str();
46+
}
47+
48+
ToolChain::InvocationInfo toolchains::WebAssembly::constructInvocation(
49+
const AutolinkExtractJobAction &job, const JobContext &context) const {
50+
assert(context.Output.getPrimaryOutputType() == file_types::TY_AutolinkFile);
51+
52+
InvocationInfo II{"swift-autolink-extract"};
53+
ArgStringList &Arguments = II.Arguments;
54+
II.allowsResponseFiles = true;
55+
56+
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
57+
file_types::TY_Object);
58+
addInputsOfType(Arguments, context.InputActions, file_types::TY_Object);
59+
60+
Arguments.push_back("-o");
61+
Arguments.push_back(
62+
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
63+
64+
return II;
65+
}
66+
67+
ToolChain::InvocationInfo
68+
toolchains::WebAssembly::constructInvocation(const DynamicLinkJobAction &job,
69+
const JobContext &context) const {
70+
assert(context.Output.getPrimaryOutputType() == file_types::TY_Image &&
71+
"Invalid linker output type.");
72+
73+
ArgStringList Arguments;
74+
75+
std::string Target = getTriple().str();
76+
if (!Target.empty()) {
77+
Arguments.push_back("-target");
78+
Arguments.push_back(context.Args.MakeArgString(Target));
79+
}
80+
81+
switch (job.getKind()) {
82+
case LinkKind::None:
83+
llvm_unreachable("invalid link kind");
84+
case LinkKind::Executable:
85+
// Default case, nothing extra needed.
86+
break;
87+
case LinkKind::DynamicLibrary:
88+
llvm_unreachable("WebAssembly doesn't support dynamic library yet");
89+
case LinkKind::StaticLibrary:
90+
llvm_unreachable("invalid link kind");
91+
}
92+
93+
// Select the linker to use.
94+
std::string Linker;
95+
if (const Arg *A = context.Args.getLastArg(options::OPT_use_ld)) {
96+
Linker = A->getValue();
97+
}
98+
if (!Linker.empty())
99+
Arguments.push_back(context.Args.MakeArgString("-fuse-ld=" + Linker));
100+
101+
102+
const char *Clang = "clang";
103+
if (const Arg *A = context.Args.getLastArg(options::OPT_tools_directory)) {
104+
StringRef toolchainPath(A->getValue());
105+
106+
// If there is a clang in the toolchain folder, use that instead.
107+
if (auto tool = llvm::sys::findProgramByName("clang", {toolchainPath}))
108+
Clang = context.Args.MakeArgString(tool.get());
109+
}
110+
111+
SmallVector<std::string, 4> RuntimeLibPaths;
112+
getRuntimeLibraryPaths(RuntimeLibPaths, context.Args, context.OI.SDKPath,
113+
/*Shared=*/false);
114+
115+
SmallString<128> SharedResourceDirPath;
116+
getResourceDirPath(SharedResourceDirPath, context.Args, /*Shared=*/false);
117+
118+
SmallString<128> swiftrtPath = SharedResourceDirPath;
119+
llvm::sys::path::append(swiftrtPath,
120+
swift::getMajorArchitectureName(getTriple()));
121+
llvm::sys::path::append(swiftrtPath, "swiftrt.o");
122+
Arguments.push_back(context.Args.MakeArgString(swiftrtPath));
123+
124+
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
125+
file_types::TY_Object);
126+
addInputsOfType(Arguments, context.InputActions, file_types::TY_Object);
127+
128+
if (!context.OI.SDKPath.empty()) {
129+
Arguments.push_back("--sysroot");
130+
Arguments.push_back(context.Args.MakeArgString(context.OI.SDKPath));
131+
}
132+
133+
// Add any autolinking scripts to the arguments
134+
for (const Job *Cmd : context.Inputs) {
135+
auto &OutputInfo = Cmd->getOutput();
136+
if (OutputInfo.getPrimaryOutputType() == file_types::TY_AutolinkFile)
137+
Arguments.push_back(context.Args.MakeArgString(
138+
Twine("@") + OutputInfo.getPrimaryOutputFilename()));
139+
}
140+
141+
// Add the runtime library link paths.
142+
for (auto path : RuntimeLibPaths) {
143+
Arguments.push_back("-L");
144+
Arguments.push_back(context.Args.MakeArgString(path));
145+
}
146+
// Link the standard library. In two paths, we do this using a .lnk file;
147+
// if we're going that route, we'll set `linkFilePath` to the path to that
148+
// file.
149+
SmallString<128> linkFilePath;
150+
getResourceDirPath(linkFilePath, context.Args, /*Shared=*/false);
151+
llvm::sys::path::append(linkFilePath, "static-executable-args.lnk");
152+
153+
auto linkFile = linkFilePath.str();
154+
if (llvm::sys::fs::is_regular_file(linkFile)) {
155+
Arguments.push_back(context.Args.MakeArgString(Twine("@") + linkFile));
156+
} else {
157+
llvm::report_fatal_error(linkFile + " not found");
158+
}
159+
160+
// Explicitly pass the target to the linker
161+
Arguments.push_back(
162+
context.Args.MakeArgString("--target=" + getTriple().str()));
163+
164+
// Delegate to Clang for sanitizers. It will figure out the correct linker
165+
// options.
166+
if (job.getKind() == LinkKind::Executable && context.OI.SelectedSanitizers) {
167+
Arguments.push_back(context.Args.MakeArgString(
168+
"-fsanitize=" + getSanitizerList(context.OI.SelectedSanitizers)));
169+
170+
// The TSan runtime depends on the blocks runtime and libdispatch.
171+
if (context.OI.SelectedSanitizers & SanitizerKind::Thread) {
172+
Arguments.push_back("-lBlocksRuntime");
173+
Arguments.push_back("-ldispatch");
174+
}
175+
}
176+
177+
if (context.Args.hasArg(options::OPT_profile_generate)) {
178+
SmallString<128> LibProfile(SharedResourceDirPath);
179+
llvm::sys::path::remove_filename(LibProfile); // remove platform name
180+
llvm::sys::path::append(LibProfile, "clang", "lib");
181+
182+
llvm::sys::path::append(LibProfile, getTriple().getOSName(),
183+
Twine("libclang_rt.profile-") +
184+
getTriple().getArchName() + ".a");
185+
Arguments.push_back(context.Args.MakeArgString(LibProfile));
186+
Arguments.push_back(context.Args.MakeArgString(
187+
Twine("-u", llvm::getInstrProfRuntimeHookVarName())));
188+
}
189+
190+
// Run clang++ in verbose mode if "-v" is set
191+
if (context.Args.hasArg(options::OPT_v)) {
192+
Arguments.push_back("-v");
193+
}
194+
195+
// These custom arguments should be right before the object file at the end.
196+
context.Args.AddAllArgs(Arguments, options::OPT_linker_option_Group);
197+
context.Args.AddAllArgs(Arguments, options::OPT_Xlinker);
198+
context.Args.AddAllArgValues(Arguments, options::OPT_Xclang_linker);
199+
200+
// This should be the last option, for convenience in checking output.
201+
Arguments.push_back("-o");
202+
Arguments.push_back(
203+
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
204+
205+
InvocationInfo II{Clang, Arguments};
206+
II.allowsResponseFiles = true;
207+
208+
return II;
209+
}
210+
211+
ToolChain::InvocationInfo
212+
toolchains::WebAssembly::constructInvocation(const StaticLinkJobAction &job,
213+
const JobContext &context) const {
214+
assert(context.Output.getPrimaryOutputType() == file_types::TY_Image &&
215+
"Invalid linker output type.");
216+
217+
ArgStringList Arguments;
218+
219+
const char *AR = "llvm-ar";
220+
Arguments.push_back("crs");
221+
222+
Arguments.push_back(
223+
context.Args.MakeArgString(context.Output.getPrimaryOutputFilename()));
224+
225+
addPrimaryInputsOfType(Arguments, context.Inputs, context.Args,
226+
file_types::TY_Object);
227+
addInputsOfType(Arguments, context.InputActions, file_types::TY_Object);
228+
229+
InvocationInfo II{AR, Arguments};
230+
231+
return II;
232+
}

stdlib/public/runtime/ThreadLocalStorage.h

+4-7
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ typedef unsigned long __swift_thread_key_t;
8282
# elif defined(__HAIKU__)
8383
typedef int __swift_thread_key_t;
8484
# elif defined(__wasi__)
85-
typedef unsigned int __swift_thread_key_t;
85+
typedef unsigned long __swift_thread_key_t;
8686
# else
8787
typedef unsigned long __swift_thread_key_t;
8888
# endif
@@ -99,12 +99,9 @@ static_assert(std::is_same<__swift_thread_key_t, DWORD>::value,
9999
# define SWIFT_THREAD_GETSPECIFIC FlsGetValue
100100
# define SWIFT_THREAD_SETSPECIFIC(key, value) (FlsSetValue(key, value) == FALSE)
101101
# elif defined(__wasi__)
102-
int wasi_polyfill_pthread_key_create(__swift_thread_key_t *key, void (*destructor)(void*));
103-
void *wasi_polyfill_pthread_getspecific(__swift_thread_key_t key);
104-
int wasi_polyfill_pthread_setspecific(__swift_thread_key_t key, const void *value);
105-
# define SWIFT_THREAD_KEY_CREATE wasi_polyfill_pthread_key_create
106-
# define SWIFT_THREAD_GETSPECIFIC wasi_polyfill_pthread_getspecific
107-
# define SWIFT_THREAD_SETSPECIFIC wasi_polyfill_pthread_setspecific
102+
# define SWIFT_THREAD_KEY_CREATE _stdlib_thread_key_create
103+
# define SWIFT_THREAD_GETSPECIFIC _stdlib_thread_getspecific
104+
# define SWIFT_THREAD_SETSPECIFIC _stdlib_thread_setspecific
108105
# else
109106
// Otherwise use the pthread API.
110107
# include <pthread.h>

0 commit comments

Comments
 (0)