From 1a3e23f72ac94179afc5ec183d3acf711f15ef7a Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Mon, 30 Jun 2025 15:26:44 +0900 Subject: [PATCH] WebAssembly: Stop changing MCAsmInfo's ExceptionsType based on flags Currently wasm adds an extra level of options that work backwards from the standard options, and overwrites them. The ExceptionModel field in TM->Options is the standard user configuration option for the exception model to use. MCAsmInfo's ExceptionsType is a constant for the default to use for the triple if not explicitly set in the TargetOptions ExceptionModel. This was adding 2 custom flags, changing the MCAsmInfo default, and overwriting the ExceptionModel from the custom flags. These comments about compiling bitcode with clang are describing a toolchain bug or user error. TargetOptions is bad, and we should move to eliminating it. It is module state not captured in the IR. Ideally the exception model should either come implied from the triple, or a module flag and not depend on this side state. Currently it is the responsibility of the toolchain and/or user to ensure the same command line flags are used at each phase of the compilation. It is not the backend's responsibilty to try to second guess these options. -wasm-enable-eh and -wasm-enable-sjlj should also be removed in favor of the standard exception control. I'm a bit confused by how all of these fields are supposed to interact, but there are a few uses in the backend that are directly looking at these flags instead of the already parsed ExceptionModel which need to be cleaned up. --- .../MCTargetDesc/WebAssemblyMCAsmInfo.cpp | 9 +----- .../MCTargetDesc/WebAssemblyMCTargetDesc.cpp | 29 ------------------- .../MCTargetDesc/WebAssemblyMCTargetDesc.h | 7 ----- .../WebAssembly/WebAssemblyAsmPrinter.cpp | 13 +++++---- .../WebAssembly/WebAssemblyAsmPrinter.h | 2 +- .../WebAssembly/WebAssemblyCFGStackify.cpp | 2 +- .../WebAssembly/WebAssemblyLateEHPrepare.cpp | 2 +- .../WebAssembly/WebAssemblyMCInstLower.cpp | 4 +-- .../WebAssembly/WebAssemblyTargetMachine.cpp | 29 +++++++++++++++++++ .../WebAssembly/WebAssemblyTargetMachine.h | 9 ++++++ 10 files changed, 50 insertions(+), 56 deletions(-) diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCAsmInfo.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCAsmInfo.cpp index 3987b086195a4..a783e5fd8521a 100644 --- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCAsmInfo.cpp +++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCAsmInfo.cpp @@ -55,14 +55,7 @@ WebAssemblyMCAsmInfo::WebAssemblyMCAsmInfo(const Triple &T, LCOMMDirectiveAlignmentType = LCOMM::Log2Alignment; SupportsDebugInformation = true; - - // When compilation is done on a cpp file by clang, the exception model info - // is stored in LangOptions, which is later used to set the info in - // TargetOptions and then MCAsmInfo in CodeGenTargetMachine::initAsmInfo(). - // But this process does not happen when compiling bitcode directly with - // clang, so we make sure this info is set correctly. - if (WebAssembly::WasmEnableEH || WebAssembly::WasmEnableSjLj) - ExceptionsType = ExceptionHandling::Wasm; + ExceptionsType = ExceptionHandling::None; initializeAtSpecifiers(atSpecifiers); } diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp index 6c0031f429c6d..8019ae165ec8c 100644 --- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp +++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.cpp @@ -36,35 +36,6 @@ using namespace llvm; #define GET_REGINFO_MC_DESC #include "WebAssemblyGenRegisterInfo.inc" -// Exception handling & setjmp-longjmp handling related options. - -// Emscripten's asm.js-style exception handling -cl::opt WebAssembly::WasmEnableEmEH( - "enable-emscripten-cxx-exceptions", - cl::desc("WebAssembly Emscripten-style exception handling"), - cl::init(false)); -// Emscripten's asm.js-style setjmp/longjmp handling -cl::opt WebAssembly::WasmEnableEmSjLj( - "enable-emscripten-sjlj", - cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"), - cl::init(false)); -// Exception handling using wasm EH instructions -cl::opt - WebAssembly::WasmEnableEH("wasm-enable-eh", - cl::desc("WebAssembly exception handling")); -// setjmp/longjmp handling using wasm EH instrutions -cl::opt WebAssembly::WasmEnableSjLj( - "wasm-enable-sjlj", cl::desc("WebAssembly setjmp/longjmp handling")); -// If true, use the legacy Wasm EH proposal: -// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/legacy/Exceptions.md -// And if false, use the standardized Wasm EH proposal: -// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md -// Currently set to true by default because not all major web browsers turn on -// the new standard proposal by default, but will later change to false. -cl::opt WebAssembly::WasmUseLegacyEH( - "wasm-use-legacy-eh", cl::desc("WebAssembly exception handling (legacy)"), - cl::init(true)); - static MCAsmInfo *createMCAsmInfo(const MCRegisterInfo & /*MRI*/, const Triple &TT, const MCTargetOptions &Options) { diff --git a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h index d6a2fe4c7839d..fe9a4bada2430 100644 --- a/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h +++ b/llvm/lib/Target/WebAssembly/MCTargetDesc/WebAssemblyMCTargetDesc.h @@ -39,13 +39,6 @@ createWebAssemblyWasmObjectWriter(bool Is64Bit, bool IsEmscripten); namespace WebAssembly { -// Exception handling / setjmp-longjmp handling command-line options -extern cl::opt WasmEnableEmEH; // asm.js-style EH -extern cl::opt WasmEnableEmSjLj; // asm.js-style SjLJ -extern cl::opt WasmEnableEH; // EH using Wasm EH instructions -extern cl::opt WasmEnableSjLj; // SjLj using Wasm EH instructions -extern cl::opt WasmUseLegacyEH; // Legacy Wasm EH - enum OperandType { /// Basic block label in a branch construct. OPERAND_BASIC_BLOCK = MCOI::OPERAND_FIRST_TARGET, diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp index 44a19e4baaf62..1bf070e9ec9c8 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp @@ -24,6 +24,7 @@ #include "WebAssemblyMachineFunctionInfo.h" #include "WebAssemblyRegisterInfo.h" #include "WebAssemblyRuntimeLibcallSignatures.h" +#include "WebAssemblyTargetMachine.h" #include "WebAssemblyUtilities.h" #include "llvm/ADT/MapVector.h" #include "llvm/ADT/SmallSet.h" @@ -155,9 +156,11 @@ static std::string getEmscriptenInvokeSymbolName(wasm::WasmSignature *Sig) { //===----------------------------------------------------------------------===// MCSymbolWasm *WebAssemblyAsmPrinter::getMCSymbolForFunction( - const Function *F, bool EnableEmEH, wasm::WasmSignature *Sig, - bool &InvokeDetected) { + const Function *F, wasm::WasmSignature *Sig, bool &InvokeDetected) { MCSymbolWasm *WasmSym = nullptr; + + const bool EnableEmEH = + WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj; if (EnableEmEH && isEmscriptenInvokeName(F->getName())) { assert(Sig); InvokeDetected = true; @@ -344,9 +347,7 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) { // will discard it later if it turns out not to be necessary. auto Signature = signatureFromMVTs(OutContext, Results, Params); bool InvokeDetected = false; - auto *Sym = getMCSymbolForFunction( - &F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj, - Signature, InvokeDetected); + auto *Sym = getMCSymbolForFunction(&F, Signature, InvokeDetected); // Multiple functions can be mapped to the same invoke symbol. For // example, two IR functions '__invoke_void_i8*' and '__invoke_void_i32' @@ -404,7 +405,7 @@ void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) { if (!F.isIntrinsic() && F.hasAddressTaken()) { MCSymbolWasm *FunctionTable = WebAssembly::getOrCreateFunctionTableSymbol(OutContext, Subtarget); - OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip); + OutStreamer->emitSymbolAttribute(FunctionTable, MCSA_NoDeadStrip); break; } } diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h index 46063bbe0fba1..aef562e1dd684 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.h @@ -73,7 +73,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyAsmPrinter final : public AsmPrinter { MVT getRegType(unsigned RegNo) const; std::string regToString(const MachineOperand &MO); WebAssemblyTargetStreamer *getTargetStreamer(); - MCSymbolWasm *getMCSymbolForFunction(const Function *F, bool EnableEmEH, + MCSymbolWasm *getMCSymbolForFunction(const Function *F, wasm::WasmSignature *Sig, bool &InvokeDetected); MCSymbol *getOrCreateWasmSymbol(StringRef Name); diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp index 640be5fe8e8c9..acb889f00a48c 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp @@ -21,13 +21,13 @@ /// //===----------------------------------------------------------------------===// -#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" #include "Utils/WebAssemblyTypeUtilities.h" #include "WebAssembly.h" #include "WebAssemblyExceptionInfo.h" #include "WebAssemblyMachineFunctionInfo.h" #include "WebAssemblySortRegion.h" #include "WebAssemblySubtarget.h" +#include "WebAssemblyTargetMachine.h" #include "WebAssemblyUtilities.h" #include "llvm/ADT/Statistic.h" #include "llvm/BinaryFormat/Wasm.h" diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp index 254ad5c4f2beb..8ac32f939c5f2 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyLateEHPrepare.cpp @@ -11,9 +11,9 @@ /// //===----------------------------------------------------------------------===// -#include "MCTargetDesc/WebAssemblyMCTargetDesc.h" #include "WebAssembly.h" #include "WebAssemblySubtarget.h" +#include "WebAssemblyTargetMachine.h" #include "WebAssemblyUtilities.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/CodeGen/MachineFunctionPass.h" diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp index d5240959e765e..cc36244e63ff5 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyMCInstLower.cpp @@ -77,9 +77,7 @@ WebAssemblyMCInstLower::GetGlobalAddressSymbol(const MachineOperand &MO) const { auto Signature = signatureFromMVTs(Ctx, ResultMVTs, ParamMVTs); bool InvokeDetected = false; - auto *WasmSym = Printer.getMCSymbolForFunction( - F, WebAssembly::WasmEnableEmEH || WebAssembly::WasmEnableEmSjLj, - Signature, InvokeDetected); + auto *WasmSym = Printer.getMCSymbolForFunction(F, Signature, InvokeDetected); WasmSym->setSignature(Signature); WasmSym->setType(wasm::WASM_SYMBOL_TYPE_FUNCTION); return WasmSym; diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp index 3e964be202956..ad47cb8ea2fe1 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp +++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp @@ -54,6 +54,35 @@ static cl::opt WasmDisableFixIrreducibleControlFlowPass( " irreducible control flow optimization pass"), cl::init(false)); +// Exception handling & setjmp-longjmp handling related options. + +// Emscripten's asm.js-style exception handling +cl::opt WebAssembly::WasmEnableEmEH( + "enable-emscripten-cxx-exceptions", + cl::desc("WebAssembly Emscripten-style exception handling"), + cl::init(false)); +// Emscripten's asm.js-style setjmp/longjmp handling +cl::opt WebAssembly::WasmEnableEmSjLj( + "enable-emscripten-sjlj", + cl::desc("WebAssembly Emscripten-style setjmp/longjmp handling"), + cl::init(false)); +// Exception handling using wasm EH instructions +cl::opt + WebAssembly::WasmEnableEH("wasm-enable-eh", + cl::desc("WebAssembly exception handling")); +// setjmp/longjmp handling using wasm EH instrutions +cl::opt WebAssembly::WasmEnableSjLj( + "wasm-enable-sjlj", cl::desc("WebAssembly setjmp/longjmp handling")); +// If true, use the legacy Wasm EH proposal: +// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/legacy/Exceptions.md +// And if false, use the standardized Wasm EH proposal: +// https://github.com/WebAssembly/exception-handling/blob/main/proposals/exception-handling/Exceptions.md +// Currently set to true by default because not all major web browsers turn on +// the new standard proposal by default, but will later change to false. +cl::opt WebAssembly::WasmUseLegacyEH( + "wasm-use-legacy-eh", cl::desc("WebAssembly exception handling (legacy)"), + cl::init(true)); + extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeWebAssemblyTarget() { // Register the target. diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h index 1d8197d02588a..6f97f1302189a 100644 --- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h +++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h @@ -21,6 +21,15 @@ namespace llvm { +namespace WebAssembly { +// Exception handling / setjmp-longjmp handling command-line options +extern cl::opt WasmEnableEmEH; // asm.js-style EH +extern cl::opt WasmEnableEmSjLj; // asm.js-style SjLJ +extern cl::opt WasmEnableEH; // EH using Wasm EH instructions +extern cl::opt WasmEnableSjLj; // SjLj using Wasm EH instructions +extern cl::opt WasmUseLegacyEH; // Legacy Wasm EH +} // namespace WebAssembly + class WebAssemblyTargetMachine final : public CodeGenTargetMachineImpl { std::unique_ptr TLOF; mutable StringMap> SubtargetMap;