So far we have assumed that we only rethrow the exception caught in the
innermost EH pad. This is true in code we directly generate, but after
inlining this may not be the case. For example, consider this code:
```ll
ehcleanup:
%0 = cleanuppad ...
call @destructor
cleanupret from %0 unwind label %catch.dispatch
```
If `destructor` gets inlined into this function, the code can be like
```ll
ehcleanup:
%0 = cleanuppad ...
invoke @throwing_func
to label %unreachale unwind label %catch.dispatch.i
catch.dispatch.i:
catchswitch ... [ label %catch.start.i ]
catch.start.i:
%1 = catchpad ...
invoke @some_function
to label %invoke.cont.i unwind label %terminate.i
invoke.cont.i:
catchret from %1 to label %destructor.exit
destructor.exit:
cleanupret from %0 unwind label %catch.dispatch
```
We lower a `cleanupret` into `rethrow`, which assumes it rethrows the
exception caught by the nearest dominating EH pad. But after the
inlining, the nearest dominating EH pad is not `ehcleanup` but
`catch.start.i`.
The problem exists in the same manner in the new (exnref) EH, because it
assumes the exception comes from the nearest EH pad and saves an exnref
from that EH pad and rethrows it (using `throw_ref`).
This problem can be fixed easily if `cleanupret` has the basic block
where its matching `cleanuppad` is. The bitcode instruction `cleanupret`
kind of has that info (it has a token from the `cleanuppad`), but that
info is lost when when we enter ISel, because `TargetSelectionDAG.td`'s
`cleanupret` node does not have any arguments:
5091a359d9/llvm/include/llvm/Target/TargetSelectionDAG.td (L700)
Note that `catchret` already has two basic block arguments, even though
neither of them means `catchpad`'s BB.
This PR adds the `cleanuppad`'s BB as an argument to `cleanupret` node
in ISel and uses it in the Wasm backend. Because this node is also used
in X86 backend we need to note its argument there too but nothing more
needs to change there as long as X86 doesn't need it.
---
- Details about changes in the Wasm backend:
After this PR, our pseudo `RETHROW` instruction takes a BB, which means
the EH pad whose exception it needs to rethrow. There are currently two
ways to generate a `RETHROW`: one is from `llvm.wasm.rethrow` intrinsic
and the other is from `CLEANUPRET` we discussed above. In case of
`llvm.wasm.rethrow`, we add a '0' as a placeholder argument when it is
lowered to a `RETHROW`, and change it to a BB in LateEHPrepare. As
written in the comments, this PR doesn't change how this BB is computed.
The BB argument will be converted to an immediate argument as with other
control flow instructions in CFGStackify.
In case of `CLEANUPRET`, it already has a BB argument pointing to an EH
pad, so it is just converted to a `RETHROW` with the same BB argument in
LateEHPrepare. This will also be lowered to an immediate in CFGStackify
with other control flow instructions.
---
Fixes #114600.
113 lines
3.1 KiB
YAML
113 lines
3.1 KiB
YAML
# RUN: llc -mtriple=wasm32-unknown-unknown -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"
|
|
|
|
declare i32 @__gxx_wasm_personality_v0(...)
|
|
declare void @foo()
|
|
define void @eh_label_test() personality ptr @__gxx_wasm_personality_v0 {
|
|
ret void
|
|
}
|
|
define void @unreachable_ehpad_test() personality ptr @__gxx_wasm_personality_v0 {
|
|
ret void
|
|
}
|
|
define void @empty_cleanuppad_test() personality ptr @__gxx_wasm_personality_v0 {
|
|
ret void
|
|
}
|
|
...
|
|
|
|
---
|
|
# This tests 'try' and 'catch' instructions are correctly placed with respect to
|
|
# EH_LABEL instructions.
|
|
# CHECK-LABEL: name: eh_label_test
|
|
name: eh_label_test
|
|
liveins:
|
|
- { reg: '$arguments' }
|
|
frameInfo:
|
|
hasCalls: true
|
|
body: |
|
|
bb.0:
|
|
; TRY should be before EH_LABEL wrappers of throwing calls
|
|
; CHECK: bb.0
|
|
; CHECK: TRY
|
|
; CHECK-NEXT: EH_LABEL
|
|
; CHECK-NEXT: CALL @foo
|
|
; CHECK-NEXT: EH_LABEL
|
|
successors: %bb.1, %bb.2
|
|
EH_LABEL <mcsymbol .Ltmp0>
|
|
CALL @foo, implicit-def dead $arguments, implicit $sp32, implicit $sp64
|
|
EH_LABEL <mcsymbol .Ltmp1>
|
|
BR %bb.2, implicit-def dead $arguments
|
|
|
|
bb.1 (landing-pad):
|
|
; predecessors: %bb.0
|
|
successors: %bb.2
|
|
; CATCH_ALL_LEGACY should be after EH_LABELs in the beginning of an EH pad.
|
|
; (Sometimes there are multiple EH_LABELs in an EH pad. This test tests
|
|
; that.) GLOBAL_SET should follow right after that.
|
|
; CHECK: bb.1
|
|
; CHECK: EH_LABEL
|
|
; CHECK: EH_LABEL
|
|
; CHECK-NEXT: CATCH_ALL_LEGACY
|
|
; CHECK-NEXT: GLOBAL_SET_I32
|
|
EH_LABEL <mcsymbol .Ltmp2>
|
|
EH_LABEL <mcsymbol .Ltmp2>
|
|
CATCHRET %bb.2, %bb.1, implicit-def dead $arguments
|
|
|
|
bb.2:
|
|
; predecessors: %bb.0, %bb.1
|
|
RETURN implicit-def dead $arguments
|
|
...
|
|
|
|
---
|
|
# Unreachable EH pads should be removed by LateEHPrepare.
|
|
# CHECK-LABEL: name: unreachable_ehpad_test
|
|
name: unreachable_ehpad_test
|
|
liveins:
|
|
- { reg: '$arguments' }
|
|
body: |
|
|
; CHECK: bb.0
|
|
bb.0:
|
|
successors: %bb.2
|
|
BR %bb.2, implicit-def dead $arguments
|
|
|
|
; This EH pad is unreachable, so it should be removed by LateEHPrepare
|
|
; CHECK-NOT: bb.1 (landing-pad)
|
|
bb.1 (landing-pad):
|
|
successors: %bb.2
|
|
EH_LABEL <mcsymbol .Ltmp2>
|
|
CATCHRET %bb.2, %bb.1, implicit-def dead $arguments
|
|
|
|
; CHECK: bb.2
|
|
bb.2:
|
|
; predecessors: %bb.0, %bb.1
|
|
RETURN implicit-def dead $arguments
|
|
...
|
|
|
|
---
|
|
# Regression test for a bug that LateEHPrepare::addCatchAll didn't handle empty
|
|
# cleanup pads. (It tried to get debug info from end() iterator.)
|
|
name: empty_cleanuppad_test
|
|
liveins:
|
|
- { reg: '$arguments' }
|
|
body: |
|
|
bb.0:
|
|
successors: %bb.1, %bb.3
|
|
EH_LABEL <mcsymbol .Ltmp0>
|
|
CALL @foo, implicit-def dead $arguments, implicit $sp32, implicit $sp64
|
|
EH_LABEL <mcsymbol .Ltmp1>
|
|
BR %bb.3, implicit-def dead $arguments
|
|
|
|
;; Empty cleanuppad
|
|
bb.1 (landing-pad):
|
|
successors: %bb.2
|
|
EH_LABEL <mcsymbol .Ltmp2>
|
|
|
|
bb.2:
|
|
successors: %bb.3
|
|
CLEANUPRET %bb.1, implicit-def dead $arguments
|
|
|
|
bb.3:
|
|
RETURN implicit-def dead $arguments
|
|
...
|