Skip to content

[WebAssembly] Clang support for exception-based lookup paths - #185775

Merged
sbc100 merged 1 commit into
llvm:mainfrom
alexcrichton:eh-sysroots
Mar 11, 2026
Merged

sbc100 merged 1 commit into
llvm:mainfrom
alexcrichton:eh-sysroots

Conversation

@alexcrichton

Copy link
Copy Markdown
Contributor

This commit is an attempt to make progress on WebAssembly/wasi-sdk#565 where with wasi-sdk I'd like to ship a single toolchain which is capable of building binaries both with C++ exceptions and without. This means that there can't be a single set of precompiled libraries that are used because one set of libraries is wrong for the other mode. The support added here is to use -fwasm-exceptions to automatically select a lookup path in the sysroot. The intention is then that wasi-sdk will ship both a "eh" set of C++ libraries as well as a "noeh" set of C++ libraries too. Clang will automatically select the correct one based on compilation flags which means that the final distribution will be able to build both binaries with exceptions and without.

This commit is an attempt to make progress on WebAssembly/wasi-sdk#565
where with wasi-sdk I'd like to ship a single toolchain which is
capable of building binaries both with C++ exceptions and without. This
means that there can't be a single set of precompiled libraries that are
used because one set of libraries is wrong for the other mode. The
support added here is to use `-fwasm-exceptions` to automatically select
a lookup path in the sysroot. The intention is then that wasi-sdk will
ship both a "eh" set of C++ libraries as well as a "noeh" set of C++
libraries too. Clang will automatically select the correct one based on
compilation flags which means that the final distribution will be able
to build both binaries with exceptions and without.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Mar 10, 2026
@alexcrichton

Copy link
Copy Markdown
Contributor Author

@sbc100 in addition to #185770 would you be willing to review this too?

@llvmbot

llvmbot commented Mar 10, 2026 •

Copy link
Copy Markdown
Member

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Alex Crichton (alexcrichton)

Changes

This commit is an attempt to make progress on WebAssembly/wasi-sdk#565 where with wasi-sdk I'd like to ship a single toolchain which is capable of building binaries both with C++ exceptions and without. This means that there can't be a single set of precompiled libraries that are used because one set of libraries is wrong for the other mode. The support added here is to use -fwasm-exceptions to automatically select a lookup path in the sysroot. The intention is then that wasi-sdk will ship both a "eh" set of C++ libraries as well as a "noeh" set of C++ libraries too. Clang will automatically select the correct one based on compilation flags which means that the final distribution will be able to build both binaries with exceptions and without.


Full diff: https://github.com/llvm/llvm-project/pull/185775.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/WebAssembly.cpp (+35-16)
  • (modified) clang/test/Driver/wasm-toolchain.cpp (+35)
diff --git a/clang/lib/Driver/ToolChains/WebAssembly.cpp b/clang/lib/Driver/ToolChains/WebAssembly.cpp
index b5fa5760a46a0..e532ef0743cc2 100644
--- a/clang/lib/Driver/ToolChains/WebAssembly.cpp
+++ b/clang/lib/Driver/ToolChains/WebAssembly.cpp
@@ -34,6 +34,15 @@ std::string WebAssembly::getMultiarchTriple(const Driver &D,
             TargetTriple.getOSAndEnvironmentName()).str();
 }
 
+/// Returns a directory name in which separate objects compile with/without
+/// exceptions may lie. This is used both for `#include` paths as well as lib
+/// paths.
+static std::string GetCXXExceptionsDir(const ArgList &DriverArgs) {
+  if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions))
+    return "eh";
+  return "noeh";
+}
+
 std::string wasm::Linker::getLinkerPath(const ArgList &Args) const {
   const ToolChain &ToolChain = getToolChain();
   if (const Arg* A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
@@ -230,12 +239,16 @@ void wasm::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   }
 }
 
-/// Given a base library directory, append path components to form the
-/// LTO directory.
-static std::string AppendLTOLibDir(const std::string &Dir) {
+/// Append `Dir` to `Paths`, but also include the LTO directories before that if
+/// LTO is eanbled.
+static void AppendLibDirAndLTODir(ToolChain::path_list &Paths, const Driver &D,
+                                  const std::string &Dir) {
+  if (D.isUsingLTO()) {
     // The version allows the path to be keyed to the specific version of
     // LLVM in used, as the bitcode format is not stable.
-    return Dir + "/llvm-lto/" LLVM_VERSION_STRING;
+    Paths.push_back(Dir + "/llvm-lto/" LLVM_VERSION_STRING);
+  }
+  Paths.push_back(Dir);
 }
 
 WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
@@ -256,14 +269,15 @@ WebAssembly::WebAssembly(const Driver &D, const llvm::Triple &Triple,
   } else {
     const std::string MultiarchTriple =
         getMultiarchTriple(getDriver(), Triple, SysRoot);
-    if (D.isUsingLTO()) {
-      // For LTO, enable use of lto-enabled sysroot libraries too, if available.
-      // Note that the directory is keyed to the LLVM revision, as LLVM's
-      // bitcode format is not stable.
-      auto Dir = AppendLTOLibDir(SysRoot + "/lib/" + MultiarchTriple);
-      getFilePaths().push_back(Dir);
-    }
-    getFilePaths().push_back(SysRoot + "/lib/" + MultiarchTriple);
+    std::string TripleLibDir = SysRoot + "/lib/" + MultiarchTriple;
+    // Allow sysroots to segregate objects based on whether exceptions are
+    // enabled or not. This is intended to assist with distribution of pre-built
+    // sysroots that contain libraries that are capable of producing binaries
+    // entirely without exception-handling instructions but also with if
+    // exceptions are enabled, for example.
+    AppendLibDirAndLTODir(getFilePaths(), D,
+                          TripleLibDir + "/" + GetCXXExceptionsDir(Args));
+    AppendLibDirAndLTODir(getFilePaths(), D, TripleLibDir);
   }
 
   if (getTriple().getOS() == llvm::Triple::WASI) {
@@ -580,13 +594,18 @@ void WebAssembly::addLibCxxIncludePaths(
   if (Version.empty())
     return;
 
-  // First add the per-target include path if the OS is known.
+  // First add the per-target-per-exception-handling include path if the
+  // OS is known,  then second add the per-target include path.
   if (IsKnownOs) {
-    std::string TargetDir = LibPath + "/" + MultiarchTriple + "/c++/" + Version;
-    addSystemInclude(DriverArgs, CC1Args, TargetDir);
+    std::string TargetDir = LibPath + "/" + MultiarchTriple;
+    std::string Suffix = "/c++/" + Version;
+    addSystemInclude(DriverArgs, CC1Args,
+                     TargetDir + "/" + GetCXXExceptionsDir(DriverArgs) +
+                         Suffix);
+    addSystemInclude(DriverArgs, CC1Args, TargetDir + Suffix);
   }
 
-  // Second add the generic one.
+  // Third add the generic one.
   addSystemInclude(DriverArgs, CC1Args, LibPath + "/c++/" + Version);
 }
 
diff --git a/clang/test/Driver/wasm-toolchain.cpp b/clang/test/Driver/wasm-toolchain.cpp
index d7ff76cedfd10..30a2f9397e3f4 100644
--- a/clang/test/Driver/wasm-toolchain.cpp
+++ b/clang/test/Driver/wasm-toolchain.cpp
@@ -111,3 +111,38 @@
 // COMPILE_WALI_STDCXX: "-internal-isystem" "[[RESOURCE_DIR]]{{(/|\\\\)}}include"
 // COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-linux-muslwali"
 // COMPILE_WALI_STDCXX: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
+
+// With a known OS "eh" and "noeh" directories are added to enable segregating
+// object built with/without exception-handling
+
+// RUN: %clangxx -### --target=wasm32-wasi --stdlib=libc++ %s 2>&1 \
+// RUN:     --sysroot=%S/Inputs/basic_linux_libcxx_tree/usr \
+// RUN:   | FileCheck -check-prefix=EH_OFF %s
+// EH_OFF: "-cc1"
+// EH_OFF: "-isysroot" "[[SYSROOT:[^"]+]]"
+// EH_OFF: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi/noeh/c++/v1"
+// EH_OFF-NOT: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi/eh/c++/v1"
+// EH_OFF: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi/c++/v1"
+// EH_OFF: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/v1"
+// EH_OFF: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi"
+// EH_OFF: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
+
+// RUN: %clangxx -### --target=wasm32-wasi -fwasm-exceptions --stdlib=libc++ %s 2>&1 \
+// RUN:     --sysroot=%S/Inputs/basic_linux_libcxx_tree/usr \
+// RUN:   | FileCheck -check-prefix=EH_ON %s
+// EH_ON: "-cc1"
+// EH_ON: "-isysroot" "[[SYSROOT:[^"]+]]"
+// EH_ON: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi/eh/c++/v1"
+// EH_ON-NOT: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi/noeh/c++/v1"
+// EH_ON: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi/c++/v1"
+// EH_ON: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/c++/v1"
+// EH_ON: "-internal-isystem" "[[SYSROOT:[^"]+]]/include/wasm32-wasi"
+// EH_ON: "-internal-isystem" "[[SYSROOT:[^"]+]]/include"
+//
+// RUN: %clangxx -### --target=wasm32-wasi --sysroot=/foo --stdlib=libc++ %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=EH_OFF_LINK %s
+// EH_OFF_LINK: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi/noeh" "-L/foo/lib/wasm32-wasi"
+//
+// RUN: %clangxx -### --target=wasm32-wasi -fwasm-exceptions --sysroot=/foo --stdlib=libc++ %s 2>&1 \
+// RUN:   | FileCheck -check-prefix=EH_ON_LINK %s
+// EH_ON_LINK: wasm-ld{{.*}}" "-L/foo/lib/wasm32-wasi/eh" "-L/foo/lib/wasm32-wasi"

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm % comment

static std::string GetCXXExceptionsDir(const ArgList &DriverArgs) {
if (DriverArgs.getLastArg(options::OPT_fwasm_exceptions))
return "eh";
return "noeh";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we need both of there?

Isn't it enough perhaps to have the default libs be either eh or noeh? i.e. maybe in the default case we don't need an extra directory at all?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My thinking behind this was that if the no-eh libs are in the default location then there's a risk they're used if things are misconfigured and the eh path is lost. By separating these directories entirely it'd help catch misconfigurations because the libraries would be entirely missing as opposed to possibly overlapping.

For now with wasi-sdk only libcxx and friends are compiled twice, I was thinking that there'd be libc in the "default location" and then libcxx would be located both in the "eh" and "noeh" locations compiled separately

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sounds reasonable to me

@sbc100
sbc100 merged commit 78beeb7 into llvm:main Mar 11, 2026
11 checks passed
@alexcrichton
alexcrichton deleted the eh-sysroots branch March 11, 2026 00:24
@alexcrichton

Copy link
Copy Markdown
Contributor Author

Thanks so much for the quick review!

alexcrichton added a commit to alexcrichton/wasi-sdk that referenced this pull request Mar 11, 2026
This commit collects together some LLVM PRs, some changes in the build
configuration here, and some thoughts from WebAssembly#565 and related issues.
Specifically the changes here are:

* The patch for llvm/llvm-project#168449 is updated to its upstream
  (unlanded) form.
* Patches for the (landed) llvm/llvm-project#185770 and
  llvm/llvm-project#185775 are added.
* The `WASI_SDK_EXCEPTIONS` configuration is now either `ON`, `OFF`, or
  `DUAL`. The default depends on the version of Clang in use, where
  23.0.0+ (which isn't released officially yet) will be `DUAL` and
  otherwise it's `OFF`. CI for our custom-built patched toolchain
  defaults to `DUAL`.
* In `DUAL` mode libcxx is built twice into two different directories,
  once with exceptions and once without. This is supported by LLVM
  patches and means that Clang will select the right set of libraries
  based on compiler flags.

The end result here is that the produced toolchain from this repository,
by default, supports C++ exceptions. Additionally if exceptions-related
flags are not passed then the final binary will not use C++ exceptions
nor require the wasm exception-handling proposal.

There's still follow-up work from WebAssembly#565, such as:

* Subjectively it feels wordy to pass `-fwasm-exceptions` vs
  `-fexceptions`.
* Personally I think `-mllvm -wasm-use-legacy-eh=false` should become
  the default upstream.
* Subjectively I don't think that `-lunwind` should be necessary and it
  should be injected automatically with `-fwasm-exceptions` (or
  `-fexceptions`).
* Shared libraries for exceptions remain disabled due to build errors I
  do not personally know how to resolve.

I'll file follow-up issues for these once this has landed since they're
more minor compared to the main body of "anything works".

Closes WebAssembly#334
Closes WebAssembly#565
alexcrichton added a commit to alexcrichton/wasi-sdk that referenced this pull request Mar 30, 2026
This commit collects together some LLVM PRs, some changes in the build
configuration here, and some thoughts from WebAssembly#565 and related issues.
Specifically the changes here are:

* The patch for llvm/llvm-project#168449 is updated to its upstream
  (unlanded) form.
* Patches for the (landed) llvm/llvm-project#185770 and
  llvm/llvm-project#185775 are added.
* The `WASI_SDK_EXCEPTIONS` configuration is now either `ON`, `OFF`, or
  `DUAL`. The default depends on the version of Clang in use, where
  23.0.0+ (which isn't released officially yet) will be `DUAL` and
  otherwise it's `OFF`. CI for our custom-built patched toolchain
  defaults to `DUAL`.
* In `DUAL` mode libcxx is built twice into two different directories,
  once with exceptions and once without. This is supported by LLVM
  patches and means that Clang will select the right set of libraries
  based on compiler flags.

The end result here is that the produced toolchain from this repository,
by default, supports C++ exceptions. Additionally if exceptions-related
flags are not passed then the final binary will not use C++ exceptions
nor require the wasm exception-handling proposal.

There's still follow-up work from WebAssembly#565, such as:

* Subjectively it feels wordy to pass `-fwasm-exceptions` vs
  `-fexceptions`.
* Personally I think `-mllvm -wasm-use-legacy-eh=false` should become
  the default upstream.
* Subjectively I don't think that `-lunwind` should be necessary and it
  should be injected automatically with `-fwasm-exceptions` (or
  `-fexceptions`).
* Shared libraries for exceptions remain disabled due to build errors I
  do not personally know how to resolve.

I'll file follow-up issues for these once this has landed since they're
more minor compared to the main body of "anything works".

Closes WebAssembly#334
Closes WebAssembly#565
alexcrichton added a commit to WebAssembly/wasi-sdk that referenced this pull request Apr 8, 2026
This commit collects together some LLVM PRs, some changes in the build
configuration here, and some thoughts from #565 and related issues.
Specifically the changes here are:

* The patch for llvm/llvm-project#168449 is updated to its upstream
(unlanded) form.
* Patches for the (landed) llvm/llvm-project#185770 and
llvm/llvm-project#185775 are added.
* The `WASI_SDK_EXCEPTIONS` configuration is now either `ON`, `OFF`, or
`DUAL`. The default depends on the version of Clang in use, where
23.0.0+ (which isn't released officially yet) will be `DUAL` and
otherwise it's `OFF`. CI for our custom-built patched toolchain defaults
to `DUAL`.
* In `DUAL` mode libcxx is built twice into two different directories,
once with exceptions and once without. This is supported by LLVM patches
and means that Clang will select the right set of libraries based on
compiler flags.

The end result here is that the produced toolchain from this repository,
by default, supports C++ exceptions. Additionally if exceptions-related
flags are not passed then the final binary will not use C++ exceptions
nor require the wasm exception-handling proposal.

There's still follow-up work from #565, such as:

* Subjectively it feels wordy to pass `-fwasm-exceptions` vs
`-fexceptions`.
* Personally I think `-mllvm -wasm-use-legacy-eh=false` should become
the default upstream.
* Subjectively I don't think that `-lunwind` should be necessary and it
should be injected automatically with `-fwasm-exceptions` (or
`-fexceptions`).
* Shared libraries for exceptions remain disabled due to build errors I
do not personally know how to resolve.

I'll file follow-up issues for these once this has landed since they're
more minor compared to the main body of "anything works".

Closes #334
Closes #565

---------

Co-authored-by: Joel Dice <[email protected]>
nakedtatsuya added a commit to Progate/llvm-project that referenced this pull request Oct 14, 2026
[WebAssembly] Clang support for exception-based lookup paths(上流 llvm#185775 の取り込み)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants