[PATCH] D108945: [WebAssembly] Share rethrowing BBs in LowerEmscriptenEHSjLj

Heejin Ahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 30 13:33:54 PDT 2021


aheejin created this revision.
aheejin added a reviewer: dschuff.
Herald added subscribers: wingo, ecnelises, sunfish, hiraditya, jgravelle-google, sbc100.
aheejin requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

There are three kinds of "rethrowing" BBs in this pass:

1. In Emscripten SjLj, after a possibly longjmping function call, we check if the thrown longjmp corresponds to one of setjmps within the current function. If not, we rethrow the longjmp by calling `emscripten_longjmp`.
2. In Emscripten EH, after a possibly throwing function call, we check if the thrown exception corresponds to the current `catch` clauses. If not, we rethrow the exception by calling `__resumeException`.
3. When both Emscripten EH and SjLj are used, when we check for an exception after a possibly throwing function call, it is possible that we get not an exception but a longjmp. In this case, we shouldn't swallow it; we should rethrow the exception by calling `__resumeException`.
4. When both Emscripten EH and SjLj are used, when we check for a longjmp after a possibly longjmping function call, it is possible that we get not a longjmp but an exception. In this case, we shouldn't swallot it; we should rethrow the longjmp by calling `emscripten_longjmp`.

Case 1 is in Emscripten SjLj, 2 is in Emscripten EH, and 3 and 4 are
relevant when both Emscripten EH and SjLj are used. 3 and 4 were first
implemented in D106525 <https://reviews.llvm.org/D106525>.

We create BBs for 1, 3, and 4 in this pass. We create those BBs for
every throwing/longjmping function call, along with other BBs that
contain condition checks. What this CL does is to create a single BB
within a function for each of 1, 3, and 4 cases. These BBs are exiting
BBs in the function and thus don't have successors, so easy to be shared
between calls.

The names of BBs created are:
Case 1: `call.em.longjmp`
Case 3: `rethrow.exn`
Case 4: `rethrow.longjmp`

For the case 2 we don't currently create BBs; we only replace the
existing `resume` instruction with `call @__resumeException`. And Clang
already creates only a single `resume` BB per function and reuses it,
so we don't need to optimize this case.

Not sure what are good benchmarks for EH/SjLj, but this decreases the
size of the object file for `grfmt_jpeg.bc` (presumably from opencv) we
got from one of our users by 8.9%. Even after running `wasm-opt -O4` on
them, there is still 4.8% improvement.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108945

Files:
  llvm/lib/Target/WebAssembly/WebAssemblyLowerEmscriptenEHSjLj.cpp
  llvm/test/CodeGen/WebAssembly/lower-em-ehsjlj.ll
  llvm/test/CodeGen/WebAssembly/lower-em-sjlj-debuginfo.ll
  llvm/test/CodeGen/WebAssembly/lower-em-sjlj.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108945.369541.patch
Type: text/x-patch
Size: 18824 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210830/640748dc/attachment.bin>


More information about the llvm-commits mailing list