Skip to content

WebAssembly: Move validation of EH flags to TargetMachine construct time #146634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

; Check all the options parse
; RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=none %s | FileCheck %s
; RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=wasm %s | FileCheck %s
; RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=dwarf %s | FileCheck %s
; RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=sjlj %s | FileCheck %s
; RUN: %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=wasm -mllvm -wasm-enable-eh %s | FileCheck %s

; RUN: not %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=invalid %s 2>&1 | FileCheck -check-prefix=ERR %s
; RUN: not %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=dwarf %s 2>&1 | FileCheck -check-prefix=ERR-BE %s
; RUN: not %clang_cc1 -triple wasm32 -o - -emit-llvm -exception-model=sjlj %s 2>&1 | FileCheck -check-prefix=ERR-BE %s

; CHECK-LABEL: define void @test(

; ERR: error: invalid value 'invalid' in '-exception-model=invalid'
; ERR-BE: fatal error: error in backend: -exception-model should be either 'none' or 'wasm'
define void @test() {
ret void
}
2 changes: 1 addition & 1 deletion clang/test/CodeGenCXX/builtins-eh-wasm.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fexceptions -fcxx-exceptions -target-feature +reference-types -target-feature +exception-handling -target-feature +multivalue -exception-model=wasm -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -triple wasm32-unknown-unknown -fexceptions -fcxx-exceptions -target-feature +reference-types -target-feature +exception-handling -target-feature +multivalue -mllvm -wasm-enable-eh -exception-model=wasm -emit-llvm -o - %s | FileCheck %s

// Check if __builtin_wasm_throw and __builtin_wasm_rethrow are correctly
// invoked when placed in try-catch.
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CodeGenCXX/wasm-eh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ void noexcept_throw() noexcept {
// CHECK-NEXT: call void @_ZSt9terminatev()


// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-DEFAULT
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wwasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-ON
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -exception-model=wasm -target-feature +exception-handling -Wno-wasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-OFF
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-DEFAULT
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -Wwasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-ON
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fms-extensions -fexceptions -fcxx-exceptions -mllvm -wasm-enable-eh -exception-model=wasm -target-feature +exception-handling -Wno-wasm-exception-spec -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=WARNING-OFF
// RUN: %clang_cc1 %s -triple wasm32-unknown-unknown -fexceptions -fcxx-exceptions -emit-llvm -o - -std=c++11 2>&1 | FileCheck %s --check-prefix=EM-EH-WARNING

// Wasm EH ignores dynamic exception specifications with types at the moment.
Expand Down
110 changes: 52 additions & 58 deletions llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,57 @@ static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM,
return *RM;
}

using WebAssembly::WasmEnableEH;
using WebAssembly::WasmEnableEmEH;
using WebAssembly::WasmEnableEmSjLj;
using WebAssembly::WasmEnableSjLj;

static void basicCheckForEHAndSjLj(TargetMachine *TM) {

// You can't enable two modes of EH at the same time
if (WasmEnableEmEH && WasmEnableEH)
report_fatal_error(
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
// You can't enable two modes of SjLj at the same time
if (WasmEnableEmSjLj && WasmEnableSjLj)
report_fatal_error(
"-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
// You can't mix Emscripten EH with Wasm SjLj.
if (WasmEnableEmEH && WasmEnableSjLj)
report_fatal_error(
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");

if (TM->Options.ExceptionModel == ExceptionHandling::None) {
// FIXME: These flags should be removed in favor of directly using the
// generically configured ExceptionsType
if (WebAssembly::WasmEnableEH || WebAssembly::WasmEnableSjLj)
TM->Options.ExceptionModel = ExceptionHandling::Wasm;
}

// Basic Correctness checking related to -exception-model
if (TM->Options.ExceptionModel != ExceptionHandling::None &&
TM->Options.ExceptionModel != ExceptionHandling::Wasm)
report_fatal_error("-exception-model should be either 'none' or 'wasm'");
if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
report_fatal_error("-exception-model=wasm not allowed with "
"-enable-emscripten-cxx-exceptions");
if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
report_fatal_error(
"-wasm-enable-eh only allowed with -exception-model=wasm");
if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
report_fatal_error(
"-wasm-enable-sjlj only allowed with -exception-model=wasm");
if ((!WasmEnableEH && !WasmEnableSjLj) &&
TM->Options.ExceptionModel == ExceptionHandling::Wasm)
report_fatal_error(
"-exception-model=wasm only allowed with at least one of "
"-wasm-enable-eh or -wasm-enable-sjlj");

// Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
// measure, but some code will error out at compile time in this combination.
// See WebAssemblyLowerEmscriptenEHSjLj pass for details.
}

/// Create an WebAssembly architecture model.
///
WebAssemblyTargetMachine::WebAssemblyTargetMachine(
Expand Down Expand Up @@ -149,7 +200,7 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
this->Options.UniqueSectionNames = true;

initAsmInfo();

basicCheckForEHAndSjLj(this);
// Note that we don't use setRequiresStructuredCFG(true). It disables
// optimizations than we're ok with, and want, such as critical edge
// splitting and tail merging.
Expand Down Expand Up @@ -400,61 +451,6 @@ FunctionPass *WebAssemblyPassConfig::createTargetRegisterAllocator(bool) {
return nullptr; // No reg alloc
}

using WebAssembly::WasmEnableEH;
using WebAssembly::WasmEnableEmEH;
using WebAssembly::WasmEnableEmSjLj;
using WebAssembly::WasmEnableSjLj;

static void basicCheckForEHAndSjLj(TargetMachine *TM) {

// You can't enable two modes of EH at the same time
if (WasmEnableEmEH && WasmEnableEH)
report_fatal_error(
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
// You can't enable two modes of SjLj at the same time
if (WasmEnableEmSjLj && WasmEnableSjLj)
report_fatal_error(
"-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
// You can't mix Emscripten EH with Wasm SjLj.
if (WasmEnableEmEH && WasmEnableSjLj)
report_fatal_error(
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");

// Here we make sure TargetOptions.ExceptionModel is the same as
// MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang
// stores the exception model info in LangOptions, which is later transferred
// to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly,
// clang's LangOptions is not used and thus the exception model info is not
// correctly transferred to TargetOptions and MCAsmInfo, so we make sure we
// have the correct exception model in WebAssemblyMCAsmInfo constructor. But
// in this case TargetOptions is still not updated, so we make sure they are
// the same.
TM->Options.ExceptionModel = TM->getMCAsmInfo()->getExceptionHandlingType();

// Basic Correctness checking related to -exception-model
if (TM->Options.ExceptionModel != ExceptionHandling::None &&
TM->Options.ExceptionModel != ExceptionHandling::Wasm)
report_fatal_error("-exception-model should be either 'none' or 'wasm'");
if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
report_fatal_error("-exception-model=wasm not allowed with "
"-enable-emscripten-cxx-exceptions");
if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
report_fatal_error(
"-wasm-enable-eh only allowed with -exception-model=wasm");
if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
report_fatal_error(
"-wasm-enable-sjlj only allowed with -exception-model=wasm");
if ((!WasmEnableEH && !WasmEnableSjLj) &&
TM->Options.ExceptionModel == ExceptionHandling::Wasm)
report_fatal_error(
"-exception-model=wasm only allowed with at least one of "
"-wasm-enable-eh or -wasm-enable-sjlj");

// Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
// measure, but some code will error out at compile time in this combination.
// See WebAssemblyLowerEmscriptenEHSjLj pass for details.
}

//===----------------------------------------------------------------------===//
// The following functions are called from lib/CodeGen/Passes.cpp to modify
// the CodeGen pass sequence.
Expand All @@ -475,8 +471,6 @@ void WebAssemblyPassConfig::addIRPasses() {
if (getOptLevel() != CodeGenOptLevel::None)
addPass(createWebAssemblyOptimizeReturned());

basicCheckForEHAndSjLj(TM);

// If exception handling is not enabled and setjmp/longjmp handling is
// enabled, we lower invokes into calls and delete unreachable landingpad
// blocks. Lowering invokes when there is no EH support is done in
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: llc -mtriple=wasm32-unknown-unknown -wasm-use-legacy-eh -exception-model=wasm -mattr=+exception-handling -run-pass wasm-cfg-stackify %s -o - | FileCheck %s
# RUN: llc -mtriple=wasm32-unknown-unknown -wasm-use-legacy-eh -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -run-pass wasm-cfg-stackify %s -o - | FileCheck %s

--- |
target triple = "wasm32-unknown-unknown"
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/WebAssembly/exception-legacy.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: llc -mtriple=wasm32-unknown-unknown -wasm-use-legacy-eh -exception-model=wasm -mattr=+exception-handling -run-pass wasm-late-eh-prepare -run-pass wasm-cfg-stackify %s -o - | FileCheck %s
# RUN: llc -mtriple=wasm32-unknown-unknown -wasm-use-legacy-eh -wasm-enable-eh -exception-model=wasm -mattr=+exception-handling -run-pass wasm-late-eh-prepare -run-pass wasm-cfg-stackify %s -o - | FileCheck %s

--- |
target triple = "wasm32-unknown-unknown"
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/CodeGen/WebAssembly/function-info.mir
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RUN: llc -mtriple=wasm32-unknown-unknown -exception-model=wasm -mattr=+exception-handling,+multivalue,+simd128 -run-pass wasm-cfg-sort -run-pass wasm-cfg-stackify %s -o - | FileCheck %s
# RUN: llc -mtriple=wasm32-unknown-unknown -exception-model=wasm -wasm-enable-sjlj -mattr=+exception-handling,+multivalue,+simd128 -run-pass wasm-cfg-sort -run-pass wasm-cfg-stackify %s -o - | FileCheck %s

--- |
target triple = "wasm32-unknown-unknown"
Expand Down
Loading