Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit a0aea0e

Browse files
committed
[WebAssembly] Fix ScopeTops info in CFGStackify for EH pads
Summary: When creating `ScopeTops` info for `try` ~ `catch` ~ `end_try`, we should create not only `end_try` -> `try` mapping but also `catch` -> `try` mapping as well. If this is not created, `block` and `end_block` markers later added may span across an existing `catch`, resulting in the incorrect code like: ``` try block --| (X) catch | end_block --| end_try ``` Reviewers: dschuff Subscribers: sunfish, sbc100, jgravelle-google, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58605 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@354945 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6187547 commit a0aea0e

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -594,11 +594,22 @@ void WebAssemblyCFGStackify::placeTryMarker(MachineBasicBlock &MBB) {
594594
TII.get(WebAssembly::END_TRY));
595595
registerTryScope(Begin, End, &MBB);
596596

597-
// Track the farthest-spanning scope that ends at this point.
598-
int Number = Cont->getNumber();
599-
if (!ScopeTops[Number] ||
600-
ScopeTops[Number]->getNumber() > Header->getNumber())
601-
ScopeTops[Number] = Header;
597+
// Track the farthest-spanning scope that ends at this point. We create two
598+
// mappings: (BB with 'end_try' -> BB with 'try') and (BB with 'catch' -> BB
599+
// with 'try'). We need to create 'catch' -> 'try' mapping here too because
600+
// markers should not span across 'catch'. For example, this should not
601+
// happen:
602+
//
603+
// try
604+
// block --| (X)
605+
// catch |
606+
// end_block --|
607+
// end_try
608+
for (int Number : {Cont->getNumber(), MBB.getNumber()}) {
609+
if (!ScopeTops[Number] ||
610+
ScopeTops[Number]->getNumber() > Header->getNumber())
611+
ScopeTops[Number] = Header;
612+
}
602613
}
603614

604615
void WebAssemblyCFGStackify::removeUnnecessaryInstrs(MachineFunction &MF) {

test/CodeGen/WebAssembly/cfg-stackify-eh.ll

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,44 @@ try.cont: ; preds = %entry, %catch, %cat
8888
; }
8989

9090
; CHECK-LABEL: test1
91-
; TODO Fill in CHECK lines once we fix ScopeTops info bug in D58605
91+
; CHECK: try
92+
; CHECK: call foo
93+
; CHECK: catch
94+
; CHECK: block
95+
; CHECK: block
96+
; CHECK: br_if 0, {{.*}} # 0: down to label7
97+
; CHECK: i32.call $drop=, __cxa_begin_catch
98+
; CHECK: try
99+
; CHECK: call foo
100+
; CHECK: br 2 # 2: down to label6
101+
; CHECK: catch
102+
; CHECK: try
103+
; CHECK: block
104+
; CHECK: br_if 0, {{.*}} # 0: down to label11
105+
; CHECK: i32.call $drop=, __cxa_begin_catch
106+
; CHECK: try
107+
; CHECK: call foo
108+
; CHECK: br 2 # 2: down to label10
109+
; CHECK: catch
110+
; CHECK: call __cxa_end_catch
111+
; CHECK: rethrow # down to catch3
112+
; CHECK: end_try
113+
; CHECK: end_block # label11:
114+
; CHECK: call __cxa_rethrow
115+
; CHECK: unreachable
116+
; CHECK: catch {{.*}} # catch3:
117+
; CHECK: call __cxa_end_catch
118+
; CHECK: rethrow # to caller
119+
; CHECK: end_try # label10:
120+
; CHECK: call __cxa_end_catch
121+
; CHECK: br 2 # 2: down to label6
122+
; CHECK: end_try
123+
; CHECK: end_block # label7:
124+
; CHECK: call __cxa_rethrow
125+
; CHECK: unreachable
126+
; CHECK: end_block # label6:
127+
; CHECK: call __cxa_end_catch
128+
; CHECK: end_try
92129
define void @test1() personality i8* bitcast (i32 (...)* @__gxx_wasm_personality_v0 to i8*) {
93130
entry:
94131
invoke void @foo()

0 commit comments

Comments
 (0)