Skip to content

Commit b1268a1

Browse files
committed
wip
1 parent c04d298 commit b1268a1

6 files changed

Lines changed: 150 additions & 24 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ on:
66
- 'wasi-sdk-*'
77
branches:
88
- main
9+
- exceptions
910

1011
pull_request:
1112
workflow_dispatch:

cmake/wasi-sdk-sysroot.cmake

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ option(WASI_SDK_DEBUG_PREFIX_MAP "Pass `-fdebug-prefix-map` for built artifacts"
2424
option(WASI_SDK_INCLUDE_TESTS "Whether or not to build tests by default" OFF)
2525
option(WASI_SDK_INSTALL_TO_CLANG_RESOURCE_DIR "Whether or not to modify the compiler's resource directory" OFF)
2626
option(WASI_SDK_LTO "Whether or not to build LTO assets" ON)
27-
option(WASI_SDK_EXCEPTIONS "Whether or not C++ exceptions are enabled" OFF)
27+
option(WASI_SDK_EXCEPTIONS "Whether or not C++ exceptions are enabled" ON)
2828
set(WASI_SDK_CPU_CFLAGS "-mcpu=lime1" CACHE STRING "CFLAGS to specify wasm features to enable")
2929

3030
set(wasi_tmp_install ${CMAKE_CURRENT_BINARY_DIR}/install)
@@ -225,7 +225,7 @@ execute_process(
225225
OUTPUT_VARIABLE llvm_version
226226
OUTPUT_STRIP_TRAILING_WHITESPACE)
227227

228-
function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_suffix)
228+
function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_suffix exceptions)
229229
if(${target} MATCHES threads)
230230
set(pic OFF)
231231
set(target_flags -pthread)
@@ -251,7 +251,9 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
251251
--sysroot ${wasi_sysroot}
252252
-resource-dir ${wasi_resource_dir})
253253

254-
if (WASI_SDK_EXCEPTIONS)
254+
set(exnsuffix "")
255+
256+
if (exceptions)
255257
# TODO: lots of builds fail with shared libraries and `-fPIC`. Looks like
256258
# things are maybe changing in llvm/llvm-project#159143 but otherwise I'm at
257259
# least not really sure what the state of shared libraries and exceptions
@@ -260,6 +262,13 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
260262
set(pic OFF)
261263
set(runtimes "libunwind;${runtimes}")
262264
list(APPEND extra_flags -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false)
265+
if (WASI_SDK_EXCEPTIONS)
266+
set(exnsuffix "/eh")
267+
endif()
268+
else()
269+
if (WASI_SDK_EXCEPTIONS)
270+
set(exnsuffix "/noeh")
271+
endif()
263272
endif()
264273

265274
# The `wasm32-wasi` target is deprecated in clang, so ignore the deprecation
@@ -279,7 +288,7 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
279288
${default_cmake_args}
280289
# Ensure headers are installed in a target-specific path instead of a
281290
# target-generic path.
282-
-DCMAKE_INSTALL_INCLUDEDIR=${wasi_sysroot}/include/${target}
291+
-DCMAKE_INSTALL_INCLUDEDIR=${wasi_sysroot}/include/${target}${exnsuffix}
283292
-DCMAKE_STAGING_PREFIX=${wasi_sysroot}
284293
-DCMAKE_POSITION_INDEPENDENT_CODE=${pic}
285294
-DLIBCXX_ENABLE_THREADS:BOOL=ON
@@ -288,20 +297,20 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
288297
-DLIBCXX_HAS_WIN32_THREAD_API:BOOL=OFF
289298
-DLLVM_COMPILER_CHECKED=ON
290299
-DLIBCXX_ENABLE_SHARED:BOOL=${pic}
291-
-DLIBCXX_ENABLE_EXCEPTIONS:BOOL=${WASI_SDK_EXCEPTIONS}
300+
-DLIBCXX_ENABLE_EXCEPTIONS:BOOL=${exceptions}
292301
-DLIBCXX_ENABLE_FILESYSTEM:BOOL=ON
293302
-DLIBCXX_ENABLE_ABI_LINKER_SCRIPT:BOOL=OFF
294303
-DLIBCXX_CXX_ABI=libcxxabi
295304
-DLIBCXX_HAS_MUSL_LIBC:BOOL=OFF
296305
-DLIBCXX_ABI_VERSION=2
297-
-DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=${WASI_SDK_EXCEPTIONS}
306+
-DLIBCXXABI_ENABLE_EXCEPTIONS:BOOL=${exceptions}
298307
-DLIBCXXABI_ENABLE_SHARED:BOOL=${pic}
299308
-DLIBCXXABI_SILENT_TERMINATE:BOOL=ON
300309
-DLIBCXXABI_ENABLE_THREADS:BOOL=ON
301310
-DLIBCXXABI_HAS_PTHREAD_API:BOOL=ON
302311
-DLIBCXXABI_HAS_EXTERNAL_THREAD_API:BOOL=OFF
303312
-DLIBCXXABI_HAS_WIN32_THREAD_API:BOOL=OFF
304-
-DLIBCXXABI_USE_LLVM_UNWINDER:BOOL=${WASI_SDK_EXCEPTIONS}
313+
-DLIBCXXABI_USE_LLVM_UNWINDER:BOOL=${exceptions}
305314
-DLIBUNWIND_ENABLE_SHARED:BOOL=${pic}
306315
-DLIBUNWIND_ENABLE_THREADS:BOOL=ON
307316
-DLIBUNWIND_USE_COMPILER_RT:BOOL=ON
@@ -310,9 +319,9 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
310319
-DCMAKE_C_FLAGS=${extra_cflags}
311320
-DCMAKE_ASM_FLAGS=${extra_cflags}
312321
-DCMAKE_CXX_FLAGS=${extra_cxxflags}
313-
-DLIBCXX_LIBDIR_SUFFIX=/${target}${extra_libdir_suffix}
314-
-DLIBCXXABI_LIBDIR_SUFFIX=/${target}${extra_libdir_suffix}
315-
-DLIBUNWIND_LIBDIR_SUFFIX=/${target}${extra_libdir_suffix}
322+
-DLIBCXX_LIBDIR_SUFFIX=/${target}${exnsuffix}${extra_libdir_suffix}
323+
-DLIBCXXABI_LIBDIR_SUFFIX=/${target}${exnsuffix}${extra_libdir_suffix}
324+
-DLIBUNWIND_LIBDIR_SUFFIX=/${target}${exnsuffix}${extra_libdir_suffix}
316325
-DLIBCXX_INCLUDE_TESTS=OFF
317326
-DLIBCXX_INCLUDE_BENCHMARKS=OFF
318327

@@ -327,27 +336,31 @@ function(define_libcxx_sub target target_suffix extra_target_flags extra_libdir_
327336
USES_TERMINAL_CONFIGURE ON
328337
USES_TERMINAL_BUILD ON
329338
USES_TERMINAL_INSTALL ON
330-
PATCH_COMMAND
331-
${CMAKE_COMMAND} -E chdir .. bash -c
332-
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch -R --check"
333339
)
340+
add_dependencies(libcxx-${target} libcxx-${target}${target_suffix}-build)
334341
endfunction()
335342

336343
function(define_libcxx target)
337-
define_libcxx_sub(${target} "" "" "")
344+
add_custom_target(libcxx-${target})
345+
define_libcxx_sub(${target} "" "" "" OFF)
346+
if (WASI_SDK_EXCEPTIONS)
347+
define_libcxx_sub(${target} "-exn" "" "" ON)
348+
endif()
338349
if(WASI_SDK_LTO)
339350
# Note: clang knows this /llvm-lto/${llvm_version} convention.
340351
# https://github.com/llvm/llvm-project/blob/llvmorg-18.1.8/clang/lib/Driver/ToolChains/WebAssembly.cpp#L204-L210
341-
define_libcxx_sub(${target} "-lto" "-flto=full" "/llvm-lto/${llvm_version}")
352+
define_libcxx_sub(${target} "-lto" "-flto=full" "/llvm-lto/${llvm_version}" OFF)
353+
if (WASI_SDK_EXCEPTIONS)
354+
define_libcxx_sub(${target} "-lto-exn" "-flto=full" "/llvm-lto/${llvm_version}" ON)
355+
endif()
342356
endif()
343357

344358
# As of this writing, `clang++` will ignore the target-specific include dirs
345359
# unless this one also exists:
346360
add_custom_target(libcxx-${target}-extra-dir
347361
COMMAND ${CMAKE_COMMAND} -E make_directory ${wasi_sysroot}/include/c++/v1
348362
COMMENT "creating libcxx-specific header file folder")
349-
add_custom_target(libcxx-${target}
350-
DEPENDS libcxx-${target}-build $<$<BOOL:${WASI_SDK_LTO}>:libcxx-${target}-lto-build> libcxx-${target}-extra-dir)
363+
add_dependencies(libcxx-${target} libcxx-${target}-extra-dir)
351364
endfunction()
352365

353366
foreach(target IN LISTS WASI_SDK_TARGETS)

cmake/wasi-sdk-toolchain.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ ExternalProject_Add(llvm-build
182182
USES_TERMINAL_CONFIGURE ON
183183
USES_TERMINAL_BUILD ON
184184
USES_TERMINAL_INSTALL ON
185+
PATCH_COMMAND
186+
${CMAKE_COMMAND} -E chdir .. bash -c
187+
"git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch || git apply ${CMAKE_SOURCE_DIR}/src/llvm-pr-168449.patch -R --check"
185188
)
186189

187190
add_custom_target(build ALL DEPENDS llvm-build)

src/llvm-pr-168449.patch

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,116 @@
1+
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
2+
index b5fa5760a46a..a8687ef88878 100644
3+
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
4+
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
5+
@@ -34,6 +34,12 @@ std::string WebAssembly::getMultiarchTriple(const Driver &D,
6+
TargetTriple.getOSAndEnvironmentName()).str();
7+
}
8+
9+
+static std::string GetCXXExceptionsDir(const ArgList &DriverArgs) {
10+
+ if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions))
11+
+ return "eh";
12+
+ return "noeh";
13+
+}
14+
+
15+
std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
16+
const ToolChain &ToolChain = getToolChain();
17+
if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
18+
@@ -232,10 +238,13 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
19+
20+
/// Given a base library directory, append path components to form the
21+
/// LTO directory.
22+
-static std::string AppendLTOLibDir(const std::string &Dir) {
23+
+static void AppendLibDirAndLTODir(ToolChain::path_list &Paths, const Driver &D, const std::string &Dir) {
24+
+ if (D.isUsingLTO()) {
25+
// The version allows the path to be keyed to the specific version of
26+
// LLVM in used, as the bitcode format is not stable.
27+
- return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
28+
+ Paths.push_back(Dir + "/llvm-lto/" LLVM_VERSION_STRING);
29+
+ }
30+
+ Paths.push_back(Dir);
31+
}
32+
33+
WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
34+
@@ -256,14 +265,9 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
35+
} else {
36+
const std::string MultiarchTriple =
37+
getMultiarchTriple(getDriver(), Triple, SysRoot);
38+
- if (D.isUsingLTO()) {
39+
- // For LTO, enable use of lto-enabled sysroot libraries too, if available.
40+
- // Note that the directory is keyed to the LLVM revision, as LLVM's
41+
- // bitcode format is not stable.
42+
- auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
43+
- getFilePaths().push_back(Dir);
44+
- }
45+
- getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
46+
+ std::string TripleLibDir = SysRoot + "/lib/" + MultiarchTriple;
47+
+ AppendLibDirAndLTODir(getFilePaths(), D, TripleLibDir + "/" + GetCXXExceptionsDir(Args));
48+
+ AppendLibDirAndLTODir(getFilePaths(), D, TripleLibDir);
49+
}
50+
51+
if (getTriple().getOS() == llvm::Triple::WASI) {
52+
@@ -580,13 +584,16 @@ void WebAssembly::addLibCxxIncludePaths(
53+
if (Version.empty())
54+
return;
55+
56+
- // First add the per-target include path if the OS is known.
57+
+ // First add the per-target-per-exception-handling include path if the
58+
+ // OS is known, then second add the per-target include path.
59+
if (IsKnownOs) {
60+
- std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
61+
- addSystemInclude(DriverArgs, CC1Args, TargetDir);
62+
+ std::string TargetDir = LibPath + "/" + MultiarchTriple;
63+
+ std::string Suffix = "/c++/" + Version;
64+
+ addSystemInclude(DriverArgs, CC1Args, TargetDir + "/" + GetCXXExceptionsDir(DriverArgs) + Suffix);
65+
+ addSystemInclude(DriverArgs, CC1Args, TargetDir + Suffix);
66+
}
67+
68+
- // Second add the generic one.
69+
+ // Third add the generic one.
70+
addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
71+
}
72+
73+
@@ -630,8 +637,10 @@ void WebAssembly::addLibStdCXXIncludePaths(
74+
75+
// First add the per-target include path if the OS is known.
76+
if (IsKnownOs) {
77+
- std::string TargetDir = LibPath + "/c++/" + Version + "/" + MultiarchTriple;
78+
- addSystemInclude(DriverArgs, CC1Args, TargetDir);
79+
+ std::string TargetDir = LibPath + "/" + MultiarchTriple;
80+
+ std::string Suffix = "/c++/" + Version;
81+
+ addSystemInclude(DriverArgs, CC1Args, TargetDir + "/" + GetCXXExceptionsDir(DriverArgs) + Suffix);
82+
+ addSystemInclude(DriverArgs, CC1Args, TargetDir + Suffix);
83+
}
84+
85+
// Second add the generic one.
86+
diff --git a/libunwind/src/Unwind-wasm.c b/libunwind/src/Unwind-wasm.c
87+
index b0d6cd2d00fc..7218f78d6c9d 100644
88+
--- a/libunwind/src/Unwind-wasm.c
89+
+++ b/libunwind/src/Unwind-wasm.c
90+
@@ -69,6 +69,18 @@ _Unwind_RaiseException(_Unwind_Exception *exception_object) {
91+
__builtin_wasm_throw(0, exception_object);
92+
}
93+
94+
+__asm__(
95+
+".globl __cpp_exception\n"
96+
+#if defined(__wasm32__)
97+
+".tagtype __cpp_exception i32\n"
98+
+#elif defined(__wasm64__)
99+
+".tagtype __cpp_exception i64\n"
100+
+#else
101+
+# error "Unsupported Wasm architecture"
102+
+#endif
103+
+"__cpp_exception:\n"
104+
+);
105+
+
106+
/// Called by __cxa_end_catch.
107+
_LIBUNWIND_EXPORT void
108+
_Unwind_DeleteException(_Unwind_Exception *exception_object) {
1109
diff --git a/libunwind/src/assembly.h b/libunwind/src/assembly.h
2-
index f8e83e138eff..c5097d25b0c6 100644
110+
index 84c9d526f1d7..2167326605b8 100644
3111
--- a/libunwind/src/assembly.h
4112
+++ b/libunwind/src/assembly.h
5-
@@ -249,6 +249,9 @@ aliasname: \
113+
@@ -253,6 +253,9 @@ aliasname: \
6114
#define WEAK_ALIAS(name, aliasname)
7115
#define NO_EXEC_STACK_DIRECTIVE
8116

@@ -13,10 +121,10 @@ index f8e83e138eff..c5097d25b0c6 100644
13121
#else
14122

15123
diff --git a/libunwind/src/config.h b/libunwind/src/config.h
16-
index deb5a4d4d73d..23c9f012cbcf 100644
124+
index f017403fa223..6014a37e2721 100644
17125
--- a/libunwind/src/config.h
18126
+++ b/libunwind/src/config.h
19-
@@ -66,7 +66,8 @@
127+
@@ -75,7 +75,8 @@
20128
#define _LIBUNWIND_EXPORT
21129
#define _LIBUNWIND_HIDDEN
22130
#else

tests/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ function(add_testcase runwasi test)
6767

6868
# Apply language-specific options and dependencies.
6969
if(test MATCHES "cc$")
70-
if(WASI_SDK_EXCEPTIONS)
70+
if(test MATCHES "exceptions")
7171
target_compile_options(${target_name} PRIVATE -fwasm-exceptions -mllvm -wasm-use-legacy-eh=false)
72-
target_link_options(${target_name} PRIVATE -lunwind)
72+
target_link_options(${target_name} PRIVATE -fwasm-exceptions -lunwind)
7373
else()
7474
target_compile_options(${target_name} PRIVATE -fno-exceptions)
7575
endif()

tests/general/exceptions.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ int main() {
1212
}
1313
abort();
1414
#else
15-
return 0;
15+
abort();
16+
return 1;
1617
#endif
1718
}

0 commit comments

Comments
 (0)