llvm / llvm/llvm-project

wasm-exceptions in clang 21 vs clang 20

Open
#175,717 2 comments 0 reactions 0 assignees View on GitHub
clang-repl
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Hey @sbc100 @aheejin

I would like some help from your end. I maintain clang-repl and the clang-repl in the browser through wasm usecase (which allows writing C++ in the browser in a repl format)

Can be tried out here : https://compiler-research.org/xeus-cpp/lab/index.html

The above link is already using `-fwasm-exceptions` & `-mllvm -wasm-enable-sjlj` with the CompilerBuilder as can be seen through the clang cc1 command. This is clang 20.1.8 based.

`
"" -cc1 -triple wasm32-unknown-emscripten ....... -target-feature +exception-handling -target-feature +multivalue -target-feature +reference-types -exception-model=wasm -mllvm -wasm-enable-eh ....... -fcxx-exceptions -fexceptions -exception-model=wasm -iwithsysroot/include/compat -fincremental-extensions -mllvm -wasm-enable-sjlj -o "<<< inputs >>>.o" -x c++ "<<< inputs >>>"
`

And hence you can do exception handling using the same

Image

**But what we notice is as we're migrating to clang 21.1.8, wasm-exceptions doesn't work the same.**

Let me take this out of Jupyter and demonstrate it through a toy project. A standard clang-repl in the browser workflow looks like this

code in cell x -> PTU (partial translation unit as code in a repl can be incomplete -> LLVM IR -> incr_module_x.o where x is cell number -> incr_module_x.wasm which is built as a standlone side module and loaded on top of a main module (from where we can parse and execute code)

So it's one of these steps, where things can go wrong. In our case I suspect the LLVM IR being built is wrong and hence the wasm generated is also wrong.

Taking the minimalistic example of
```
try { throw 1; } catch (...) { 0; }
```
note that we're using only `-fwasm-exceptions`. our compiler builder args look something like
```
CB.SetCompilerArgs({
"-v",
"-xc++",
"-std=c++23",
"-Xclang", "-iwithsysroot/include/compat",
"-fwasm-exceptions"
});
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the minimal C++ exception example with clang 20 and clang 21 using -fwasm-exceptions, then compare the generated LLVM IR and wasm output. Trace the clang-repl browser pipeline from the partial translation unit through the incremental object and standalone wasm module. Done means the clang 21 regression is localized and its expected exception-handling behavior is covered by an appropriate compiler test.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, wasm
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.