Skip to content

Commit 6ab7e52

Browse files
authored
WebAssembly: Move validation of EH flags to TargetMachine construct time (#146634)
1 parent d433134 commit 6ab7e52

File tree

7 files changed

+63
-68
lines changed

7 files changed

+63
-68
lines changed

clang/test/CodeGen/WebAssembly/wasm-exception-model-flag-parse-ir-input.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

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

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

1111
; CHECK-LABEL: define void @test(
1212

1313
; ERR: error: invalid value 'invalid' in '-exception-model=invalid'
14+
; ERR-BE: fatal error: error in backend: -exception-model should be either 'none' or 'wasm'
1415
define void @test() {
1516
ret void
1617
}

clang/test/CodeGenCXX/builtins-eh-wasm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// 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
1+
// 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
22

33
// Check if __builtin_wasm_throw and __builtin_wasm_rethrow are correctly
44
// invoked when placed in try-catch.

clang/test/CodeGenCXX/wasm-eh.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,9 @@ void noexcept_throw() noexcept {
393393
// CHECK-NEXT: call void @_ZSt9terminatev()
394394

395395

396-
// 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
397-
// 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
398-
// 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
396+
// 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
397+
// 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
398+
// 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
399399
// 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
400400

401401
// Wasm EH ignores dynamic exception specifications with types at the moment.

llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,57 @@ static Reloc::Model getEffectiveRelocModel(std::optional<Reloc::Model> RM,
111111
return *RM;
112112
}
113113

114+
using WebAssembly::WasmEnableEH;
115+
using WebAssembly::WasmEnableEmEH;
116+
using WebAssembly::WasmEnableEmSjLj;
117+
using WebAssembly::WasmEnableSjLj;
118+
119+
static void basicCheckForEHAndSjLj(TargetMachine *TM) {
120+
121+
// You can't enable two modes of EH at the same time
122+
if (WasmEnableEmEH && WasmEnableEH)
123+
report_fatal_error(
124+
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
125+
// You can't enable two modes of SjLj at the same time
126+
if (WasmEnableEmSjLj && WasmEnableSjLj)
127+
report_fatal_error(
128+
"-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
129+
// You can't mix Emscripten EH with Wasm SjLj.
130+
if (WasmEnableEmEH && WasmEnableSjLj)
131+
report_fatal_error(
132+
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");
133+
134+
if (TM->Options.ExceptionModel == ExceptionHandling::None) {
135+
// FIXME: These flags should be removed in favor of directly using the
136+
// generically configured ExceptionsType
137+
if (WebAssembly::WasmEnableEH || WebAssembly::WasmEnableSjLj)
138+
TM->Options.ExceptionModel = ExceptionHandling::Wasm;
139+
}
140+
141+
// Basic Correctness checking related to -exception-model
142+
if (TM->Options.ExceptionModel != ExceptionHandling::None &&
143+
TM->Options.ExceptionModel != ExceptionHandling::Wasm)
144+
report_fatal_error("-exception-model should be either 'none' or 'wasm'");
145+
if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
146+
report_fatal_error("-exception-model=wasm not allowed with "
147+
"-enable-emscripten-cxx-exceptions");
148+
if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
149+
report_fatal_error(
150+
"-wasm-enable-eh only allowed with -exception-model=wasm");
151+
if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
152+
report_fatal_error(
153+
"-wasm-enable-sjlj only allowed with -exception-model=wasm");
154+
if ((!WasmEnableEH && !WasmEnableSjLj) &&
155+
TM->Options.ExceptionModel == ExceptionHandling::Wasm)
156+
report_fatal_error(
157+
"-exception-model=wasm only allowed with at least one of "
158+
"-wasm-enable-eh or -wasm-enable-sjlj");
159+
160+
// Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
161+
// measure, but some code will error out at compile time in this combination.
162+
// See WebAssemblyLowerEmscriptenEHSjLj pass for details.
163+
}
164+
114165
/// Create an WebAssembly architecture model.
115166
///
116167
WebAssemblyTargetMachine::WebAssemblyTargetMachine(
@@ -149,7 +200,7 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
149200
this->Options.UniqueSectionNames = true;
150201

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

403-
using WebAssembly::WasmEnableEH;
404-
using WebAssembly::WasmEnableEmEH;
405-
using WebAssembly::WasmEnableEmSjLj;
406-
using WebAssembly::WasmEnableSjLj;
407-
408-
static void basicCheckForEHAndSjLj(TargetMachine *TM) {
409-
410-
// You can't enable two modes of EH at the same time
411-
if (WasmEnableEmEH && WasmEnableEH)
412-
report_fatal_error(
413-
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-eh");
414-
// You can't enable two modes of SjLj at the same time
415-
if (WasmEnableEmSjLj && WasmEnableSjLj)
416-
report_fatal_error(
417-
"-enable-emscripten-sjlj not allowed with -wasm-enable-sjlj");
418-
// You can't mix Emscripten EH with Wasm SjLj.
419-
if (WasmEnableEmEH && WasmEnableSjLj)
420-
report_fatal_error(
421-
"-enable-emscripten-cxx-exceptions not allowed with -wasm-enable-sjlj");
422-
423-
// Here we make sure TargetOptions.ExceptionModel is the same as
424-
// MCAsmInfo.ExceptionsType. Normally these have to be the same, because clang
425-
// stores the exception model info in LangOptions, which is later transferred
426-
// to TargetOptions and MCAsmInfo. But when clang compiles bitcode directly,
427-
// clang's LangOptions is not used and thus the exception model info is not
428-
// correctly transferred to TargetOptions and MCAsmInfo, so we make sure we
429-
// have the correct exception model in WebAssemblyMCAsmInfo constructor. But
430-
// in this case TargetOptions is still not updated, so we make sure they are
431-
// the same.
432-
TM->Options.ExceptionModel = TM->getMCAsmInfo()->getExceptionHandlingType();
433-
434-
// Basic Correctness checking related to -exception-model
435-
if (TM->Options.ExceptionModel != ExceptionHandling::None &&
436-
TM->Options.ExceptionModel != ExceptionHandling::Wasm)
437-
report_fatal_error("-exception-model should be either 'none' or 'wasm'");
438-
if (WasmEnableEmEH && TM->Options.ExceptionModel == ExceptionHandling::Wasm)
439-
report_fatal_error("-exception-model=wasm not allowed with "
440-
"-enable-emscripten-cxx-exceptions");
441-
if (WasmEnableEH && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
442-
report_fatal_error(
443-
"-wasm-enable-eh only allowed with -exception-model=wasm");
444-
if (WasmEnableSjLj && TM->Options.ExceptionModel != ExceptionHandling::Wasm)
445-
report_fatal_error(
446-
"-wasm-enable-sjlj only allowed with -exception-model=wasm");
447-
if ((!WasmEnableEH && !WasmEnableSjLj) &&
448-
TM->Options.ExceptionModel == ExceptionHandling::Wasm)
449-
report_fatal_error(
450-
"-exception-model=wasm only allowed with at least one of "
451-
"-wasm-enable-eh or -wasm-enable-sjlj");
452-
453-
// Currently it is allowed to mix Wasm EH with Emscripten SjLj as an interim
454-
// measure, but some code will error out at compile time in this combination.
455-
// See WebAssemblyLowerEmscriptenEHSjLj pass for details.
456-
}
457-
458454
//===----------------------------------------------------------------------===//
459455
// The following functions are called from lib/CodeGen/Passes.cpp to modify
460456
// the CodeGen pass sequence.
@@ -475,8 +471,6 @@ void WebAssemblyPassConfig::addIRPasses() {
475471
if (getOptLevel() != CodeGenOptLevel::None)
476472
addPass(createWebAssemblyOptimizeReturned());
477473

478-
basicCheckForEHAndSjLj(TM);
479-
480474
// If exception handling is not enabled and setjmp/longjmp handling is
481475
// enabled, we lower invokes into calls and delete unreachable landingpad
482476
// blocks. Lowering invokes when there is no EH support is done in

llvm/test/CodeGen/WebAssembly/cfg-stackify-eh-legacy.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 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
1+
# 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
22

33
--- |
44
target triple = "wasm32-unknown-unknown"

llvm/test/CodeGen/WebAssembly/exception-legacy.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 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
1+
# 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
22

33
--- |
44
target triple = "wasm32-unknown-unknown"

llvm/test/CodeGen/WebAssembly/function-info.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 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
1+
# 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
22

33
--- |
44
target triple = "wasm32-unknown-unknown"

0 commit comments

Comments
 (0)