This is an archive of the discontinued LLVM Phabricator instance.

[WebAssembly] Support clang -fwasm-exceptions for bitcode
ClosedPublic

Authored by aheejin on Dec 16 2021, 11:50 AM.

Details

Summary

This supports bitcode compilation using clang -fwasm-exceptions.


The current situation:

Currently the backend requires two options for Wasm EH:
-wasm-enable-eh and -exception-model=wasm. Wasm SjLj requires two
options as well: -wasm-enable-sjlj and -exception-model=wasm. When
using Wasm EH via Emscripten, you only need to pass -fwasm-exceptions,
and these options will be added within the clang driver. This
description will focus on the case of Wasm EH going forward, but Wasm
SjLj's case is similar.

When you pass -fwasm-exceptions to emcc and clang driver, the clang
driver adds these options to the command line that calls the clang
frontend (clang -cc1): -mllvm -wasm-enable-eh and
-exception-model=wasm. -wasm-enable-eh is prefixed with -mllvm, so
it is passed as is to the backend. But -exception-model is parsed and
processed within the clang frontend and stored in LangOptions class.
This info is later transferred to TargetOptions class, and then
eventually passed to MCAsmInfo class. All LLVM code queries this
MCAsmInfo to get the exception model.

All LLVM code queries this MCAsmInfo to get the exception model. (
(e.g., here)


Problem:

The problem is the whole LangOptions processing is bypassed when
compiling bitcode, so the information transfer of LangOptions ->
TargetOptions -> MCAsmInfo does not happen. They are all set to
ExceptionHandling::None, which is the default value.


What other targets do, and why we can't do the same:

Other targets support bitcode compilation by the clang driver, but they
can do that by using different triples. For example, X86 target supports
multiple triples, each of which has its own subclass of MCAsmInfo, so
it can hardcode the appropriate exception model within those subclasses'
constructors. But we don't have separate triples for each exception
mode: none, emscripten, and wasm.


What this CL does:

If we can figure out whether -wasm-enable-eh is passed to the backend,
we can programatically set the exception model from the backend, rather
than requiring it to be passed.

So we check WasmEnableEH and WasmEnableSjLj variables, which are
cl::opt for -wasm-enable-eh and -wasm-enable-sjlj, in
WebAssemblyMCAsmInfo constructor, and if either of them is set, we set
MCAsmInfo.ExceptionType to Wasm. TargetOptions cannot be updated
there, so we make sure they are the same later.

Fixes https://github.com/emscripten-core/emscripten/issues/15712.

Diff Detail

Event Timeline

aheejin created this revision.Dec 16 2021, 11:50 AM
aheejin requested review of this revision.Dec 16 2021, 11:50 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptDec 16 2021, 11:50 AM
aheejin edited the summary of this revision. (Show Details)Dec 16 2021, 11:55 AM
aheejin updated this revision to Diff 394951.Dec 16 2021, 12:02 PM

Revert a function name change

dschuff accepted this revision.Dec 16 2021, 3:29 PM
dschuff added inline comments.
llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
37–38

We could put these declarations in WebAssemblyUtilities.h; then they wouldn't have to be duplicated here and in WebAssemblyMCAsmInfo.cpp

353
This revision is now accepted and ready to land.Dec 16 2021, 3:29 PM
aheejin edited the summary of this revision. (Show Details)Dec 16 2021, 3:41 PM
aheejin updated this revision to Diff 395024.Dec 16 2021, 4:12 PM
aheejin marked 2 inline comments as done.

Address comments

aheejin added inline comments.Dec 16 2021, 4:14 PM
llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
37–38

Done. But as a result they are not within WebAssembly namespace, which I think is actually better because we have been cluttering the llvm namespace so far. And I needed to attach WebAssembly:: to all the usages of these variables in other places. In this file I just used using WebAssembly::***, because there are too many usages of these options.

aheejin updated this revision to Diff 395025.Dec 16 2021, 4:14 PM

Remove an unnecessary include

This revision was landed with ongoing or failed builds.Dec 16 2021, 4:50 PM
This revision was automatically updated to reflect the committed changes.
dschuff added inline comments.Dec 16 2021, 5:11 PM
llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
37–38

👍