This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Add Wasm SjLj support
ClosedPublic

Authored by aheejin on Aug 30 2021, 5:23 PM.

Details

Summary

This add support for SjLj using Wasm exception handling instructions:
https://github.com/WebAssembly/exception-handling/blob/master/proposals/exception-handling/Exceptions.md

This does not yet support the mixed use of EH and SjLj within a
function. It will be added in a follow-up CL.

This currently passes all SjLj Emscripten tests for wasm0/1/2/3/s,
except for the below:

  • test_longjmp_standalone: Uses Node
  • test_dlfcn_longjmp: Uses NodeRAWFS
  • test_longjmp_throw: Mixes EH and SjLj
  • test_exceptions_longjmp1: Mixes EH and SjLj
  • test_exceptions_longjmp2: Mixes EH and SjLj
  • test_exceptions_longjmp3: Mixes EH and SjLj

Diff Detail

Event Timeline

aheejin created this revision.Aug 30 2021, 5:23 PM
aheejin requested review of this revision.Aug 30 2021, 5:23 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 30 2021, 5:23 PM
aheejin updated this revision to Diff 369589.Aug 30 2021, 5:24 PM

Remove debug print

dschuff accepted this revision.Aug 31 2021, 11:41 AM
dschuff added inline comments.
llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
11

Eventually it might be nice to make it possible to use wasm SJLJ without needing emscripten, which should be feasible since it doesn't need JS. I guess that would mean moving or copying some library functions out of emscripten proper, and maybe refactoring the code or something. It doesn't have to be soon though.

192

or perhaps just "If there are calls to longjmp()"

199
240
This revision is now accepted and ready to land.Aug 31 2021, 11:41 AM
aheejin updated this revision to Diff 369810.Aug 31 2021, 3:47 PM
aheejin marked 3 inline comments as done.

Address comments

llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
11

What this uses from Emscripten is just a couple of C library functions, mainly saveSetjmp and testSetjmp in https://github.com/emscripten-core/emscripten/blob/main/system/lib/compiler-rt/emscripten_setjmp.c. It doesn't need JS. Also it needs __wasm_longjmp which calls our Clang builtin. These functions have to be somewhere. If not Emscripten, where do you suggest? We can construct these functions in LLVM but not sure what the benefit for that is..?

199

Changed to "If there are calls to setjmp()", to be consistent with your comment above.

dschuff added inline comments.Aug 31 2021, 4:23 PM
llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
11

I guess I'm not saying we should move it "out" of emscripten, per se. Probably the "right" place for this kind of thing would be a language runtime library. e.g. if this were for C++, libc++abi would probably be the right place. Unfortunately C doesn't have a "low-level" library like C++ does, so in a standalone toolchain (i.e. other than emscripten) it might likely go in libc, or in some other toolchain-specific library that gets linked into every build. In emscripten, it looks like we just add an extra file to compiler-rt, and compiler-rt is in the emscripten repo. If e.g. wasi wanted to support SJLJ they might put it in the lower layer of wasi-libc or something. In that case it might make sense to move those functions into their own file. Then wasi-libc could just copy that whole file without getting the other emscripten code.

aheejin added inline comments.Aug 31 2021, 5:28 PM
llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
11

Yeah that makes sense. By the way that emscripten_setjmp.c is already pretty an isolated addition to compiler-rt; that single file contains all SjLj related stuff. It both contains Emscripten SjLj stuff that calls a JS function (emscripten_longjmp) and Wasm SjLj stuff like __wasm_longjmp, but I think fixing that and moving that file to somewhere else wouldn't be a huge problem.

tlively accepted this revision.Sep 1 2021, 12:40 PM
This revision was landed with ongoing or failed builds.Sep 2 2021, 10:51 AM
This revision was automatically updated to reflect the committed changes.