[WebAssembly] Exception handling: Switch to the new proposal
Summary:
This switches the EH implementation to the new proposal:
https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md
(The previous proposal was
https://github.com/WebAssembly/exception-handling/blob/master/proposals/old/Exceptions.md)
- Instruction changes
- Now we have one single `catch` instruction that returns a except_ref
value
- `throw` now can take variable number of operations
- `rethrow` does not have 'depth' argument anymore
- `br_on_exn` queries an except_ref to see if it matches the tag and
branches to the given label if true.
- `extract_exception` is a pseudo instruction that simulates popping
values from wasm stack. This is to make `br_on_exn`, a very special
instruction, work: `br_on_exn` puts values onto the stack only if it
is taken, and the # of values can vay depending on the tag.
- Now there's only one `catch` per `try`, this patch removes all special
handling for terminate pad with a call to `__clang_call_terminate`.
Before it was the only case there are two catch clauses (a normal
`catch` and `catch_all` per `try`).
- Make `rethrow` act as a terminator like `throw`. This splits BB after
`rethrow` in WasmEHPrepare, and deletes an unnecessary `unreachable`
after `rethrow` in LateEHPrepare.
- Now we stop at all catchpads (because we add wasm `catch` instruction
that catches all exceptions), this creates new
`findWasmUnwindDestinations` function in SelectionDAGBuilder.
- Now we use `br_on_exn` instrution to figure out if an except_ref
matches the current tag or not, LateEHPrepare generates this sequence
for catch pads:
```
catch
block i32
br_on_exn $__cpp_exception
end_block
extract_exception
```
- Branch analysis for `br_on_exn` in WebAssemblyInstrInfo
- Other various misc. changes to switch to the new proposal.
Reviewers: dschuff
Subscribers: sbc100, jgravelle-google, sunfish, llvm-commits
Differential Revision: https://reviews.llvm.org/D57134
llvm-svn: 352598
diff --git a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
index e3c34f8..0377301 100644
--- a/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
+++ b/llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll
@@ -7,21 +7,25 @@
@_ZTId = external constant i8*
; Simple test case with two catch clauses
+; void test0() {
+; try {
+; foo();
+; } catch (int n) {
+; bar();
+; } catch (double d) {
+; }
+; }
; CHECK-LABEL: test0
+; CHECK: try
; CHECK: call foo@FUNCTION
-; CHECK: .LBB0_1:
-; CHECK: i32.catch
+; CHECK: catch $[[EXCEPT_REF:[0-9]+]]=
+; CHECK: block i32
+; CHECK: br_on_exn 0, __cpp_exception@EVENT, $[[EXCEPT_REF]]
+; CHECK: rethrow
+; CHECK: end_block
; CHECK: i32.call $drop=, _Unwind_CallPersonality@FUNCTION
-; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION
-; CHECK: call bar@FUNCTION
-; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB0_3:
-; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION
-; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB0_5:
-; CHECK: call __cxa_rethrow@FUNCTION
-; CHECK: .LBB0_6:
+; CHECK: end_try
; CHECK: return
define void @test0() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
entry:
@@ -68,39 +72,45 @@
}
; Nested try-catches within a catch
+; void test1() {
+; try {
+; foo();
+; } catch (int n) {
+; try {
+; foo();
+; } catch (int n) {
+; foo();
+; }
+; }
+; }
; CHECK-LABEL: test1
+; CHECK: try
; CHECK: call foo@FUNCTION
-; CHECK: .LBB1_1:
-; CHECK: i32.catch $0=, 0
-; CHECK: i32.call $drop=, _Unwind_CallPersonality@FUNCTION, $0
-; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION, $0
+; CHECK: catch
+; CHECK: br_on_exn 0, __cpp_exception@EVENT
+; CHECK: rethrow
+; CHECK: i32.call $drop=, _Unwind_CallPersonality@FUNCTION
+; CHECK: try
; CHECK: call foo@FUNCTION
-; CHECK: .LBB1_3:
-; CHECK: i32.catch $0=, 0
-; CHECK: i32.call $drop=, _Unwind_CallPersonality@FUNCTION, $0
-; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION, $0
+; CHECK: catch
+; CHECK: br_on_exn 0, __cpp_exception@EVENT
+; CHECK: rethrow
+; CHECK: i32.call $drop=, _Unwind_CallPersonality@FUNCTION
+; CHECK: try
+; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION
+; CHECK: try
; CHECK: call foo@FUNCTION
-; CHECK: .LBB1_5:
-; CHECK: catch_all
-; CHECK: call __cxa_end_catch@FUNCTION
+; CHECK: catch $drop=
; CHECK: rethrow
-; CHECK: .LBB1_6:
-; CHECK: call __cxa_rethrow@FUNCTION
+; CHECK: end_try
+; CHECK: catch $drop=
; CHECK: rethrow
-; CHECK: .LBB1_7:
-; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB1_8:
-; CHECK: catch_all
-; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB1_9:
-; CHECK: call __cxa_rethrow@FUNCTION
-; CHECK: rethrow
-; CHECK: .LBB1_10:
-; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB1_11:
+; CHECK: end_try
+; CHECK: end_try
+; CHECK: end_try
; CHECK: return
-define hidden void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
+define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
entry:
invoke void @foo()
to label %try.cont11 unwind label %catch.dispatch
@@ -175,30 +185,38 @@
}
; Nested loop within a catch clause
+; void test2() {
+; try {
+; foo();
+; } catch (...) {
+; for (int i = 0; i < 50; i++)
+; foo();
+; }
+; }
; CHECK-LABEL: test2
+; CHECK: try
; CHECK: call foo@FUNCTION
-; CHECK: .LBB2_1:
-; CHECK: i32.catch
-; CHECK: i32.call $drop=, __cxa_begin_catch@FUNCTION
-; CHECK: .LBB2_2:
+; CHECK: catch
+; CHECK: br_on_exn 0, __cpp_exception@EVENT
+; CHECK: rethrow
+; CHECK: loop
+; CHECK: try
; CHECK: call foo@FUNCTION
-; CHECK: .LBB2_4:
-; CHECK: catch_all
+; CHECK: catch $drop=
+; CHECK: try
; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB2_5:
-; CHECK: i32.catch
+; CHECK: catch
+; CHECK: br_on_exn 0, __cpp_exception@EVENT
+; CHECK: call __clang_call_terminate@FUNCTION, 0
+; CHECK: unreachable
; CHECK: call __clang_call_terminate@FUNCTION
; CHECK: unreachable
-; CHECK: .LBB2_6:
-; CHECK: catch_all
-; CHECK: call _ZSt9terminatev@FUNCTION
-; CHECK: unreachable
-; CHECK: .LBB2_7:
+; CHECK: end_try
; CHECK: rethrow
-; CHECK: .LBB2_8:
-; CHECK: call __cxa_end_catch@FUNCTION
-; CHECK: .LBB2_10:
+; CHECK: end_try
+; CHECK: end_loop
+; CHECK: end_try
; CHECK: return
define void @test2() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
entry: