Skip to content

Commit f0a5e48

Browse files
authored
[Wasm EH] Optimize values flowing out of TryTable (#6997)
This allows (block $out (result i32) (try_table (catch..) .. (br $out (i32.const 42) ) ) ) => (block $out (result i32) (try_table (result i32) (catch..) ;; add a result .. (i32.const 42) ;; remove the br around the value ) )
1 parent a8aa660 commit f0a5e48

File tree

2 files changed

+52
-25
lines changed

2 files changed

+52
-25
lines changed

src/passes/RemoveUnusedBrs.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,15 +260,16 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
260260
}
261261
}
262262
} else if (curr->is<Nop>()) {
263-
// ignore (could be result of a previous cycle)
263+
// Ignore (could be result of a previous cycle).
264264
self->stopValueFlow();
265-
} else if (curr->is<Loop>()) { // TODO: eh
266-
// do nothing - it's ok for values to flow out
265+
} else if (curr->is<Loop>() || curr->is<TryTable>()) {
266+
// Do nothing - it's ok for values to flow out.
267+
// TODO: Legacy Try as well?
267268
} else if (auto* sw = curr->dynCast<Switch>()) {
268269
self->stopFlow();
269270
self->optimizeSwitch(sw);
270271
} else {
271-
// anything else stops the flow
272+
// Anything else stops the flow.
272273
self->stopFlow();
273274
}
274275
}

test/lit/passes/remove-unused-brs-eh.wast

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,36 @@
1212
;; CHECK: (func $throw-caught-all (type $0)
1313
;; CHECK-NEXT: (block $catch
1414
;; CHECK-NEXT: (try_table (catch_all $catch)
15-
;; CHECK-NEXT: (br $catch)
15+
;; CHECK-NEXT: (nop)
1616
;; CHECK-NEXT: )
1717
;; CHECK-NEXT: )
1818
;; CHECK-NEXT: )
1919
(func $throw-caught-all
2020
(block $catch
2121
(try_table (catch_all $catch)
22-
;; This throw can be a br.
22+
;; This throw can be a br. After that, it can also be removed, as we
23+
;; flow to that block anyhow.
24+
(throw $e)
25+
)
26+
)
27+
)
28+
29+
;; CHECK: (func $throw-caught-all-no-flow (type $0)
30+
;; CHECK-NEXT: (block $catch
31+
;; CHECK-NEXT: (try_table (catch_all $catch)
32+
;; CHECK-NEXT: (br $catch)
33+
;; CHECK-NEXT: )
34+
;; CHECK-NEXT: (unreachable)
35+
;; CHECK-NEXT: )
36+
;; CHECK-NEXT: )
37+
(func $throw-caught-all-no-flow
38+
(block $catch
39+
(try_table (catch_all $catch)
2340
(throw $e)
2441
)
42+
;; Block the flow, so that after the throw is optimized to a br, the br
43+
;; remains.
44+
(unreachable)
2545
)
2646
)
2747

@@ -52,7 +72,7 @@
5272
;; CHECK: (func $throw-caught-precise (type $0)
5373
;; CHECK-NEXT: (block $catch
5474
;; CHECK-NEXT: (try_table (catch $e $catch)
55-
;; CHECK-NEXT: (br $catch)
75+
;; CHECK-NEXT: (nop)
5676
;; CHECK-NEXT: )
5777
;; CHECK-NEXT: )
5878
;; CHECK-NEXT: )
@@ -70,7 +90,7 @@
7090
;; CHECK-NEXT: (block $fail
7191
;; CHECK-NEXT: (block $catch
7292
;; CHECK-NEXT: (try_table (catch $f $fail) (catch $e $catch)
73-
;; CHECK-NEXT: (br $catch)
93+
;; CHECK-NEXT: (nop)
7494
;; CHECK-NEXT: )
7595
;; CHECK-NEXT: )
7696
;; CHECK-NEXT: (call $throw-caught-precise-later)
@@ -94,7 +114,7 @@
94114
;; CHECK-NEXT: (block $fail
95115
;; CHECK-NEXT: (block $catch
96116
;; CHECK-NEXT: (try_table (catch $f $fail) (catch_all $catch)
97-
;; CHECK-NEXT: (br $catch)
117+
;; CHECK-NEXT: (nop)
98118
;; CHECK-NEXT: )
99119
;; CHECK-NEXT: )
100120
;; CHECK-NEXT: (call $throw-caught-precise-later)
@@ -138,6 +158,7 @@
138158
;; CHECK-NEXT: (br $catch)
139159
;; CHECK-NEXT: )
140160
;; CHECK-NEXT: )
161+
;; CHECK-NEXT: (unreachable)
141162
;; CHECK-NEXT: )
142163
;; CHECK-NEXT: (call $throw-caught-precise-later)
143164
;; CHECK-NEXT: )
@@ -151,6 +172,8 @@
151172
(throw $e)
152173
)
153174
)
175+
;; Block the flow, so that the br above remains.
176+
(unreachable)
154177
)
155178
;; Add an effect here, so the two blocks are not mergeable.
156179
(call $throw-caught-precise-later)
@@ -195,6 +218,7 @@
195218
;; CHECK-NEXT: (try_table (catch_ref $e $outer) (catch_all $catch)
196219
;; CHECK-NEXT: (throw $e)
197220
;; CHECK-NEXT: )
221+
;; CHECK-NEXT: (unreachable)
198222
;; CHECK-NEXT: )
199223
;; CHECK-NEXT: (unreachable)
200224
;; CHECK-NEXT: )
@@ -207,6 +231,7 @@
207231
;; not optimize.
208232
(throw $e)
209233
)
234+
(unreachable)
210235
)
211236
(unreachable)
212237
)
@@ -220,6 +245,7 @@
220245
;; CHECK-NEXT: (br $outer)
221246
;; CHECK-NEXT: (br $middle)
222247
;; CHECK-NEXT: (br $inner)
248+
;; CHECK-NEXT: (unreachable)
223249
;; CHECK-NEXT: )
224250
;; CHECK-NEXT: )
225251
;; CHECK-NEXT: (call $throw-caught-precise-later)
@@ -236,6 +262,9 @@
236262
(throw $e)
237263
(throw $f)
238264
(throw $g)
265+
;; Prevent the br we optimize to at the end from getting optimized
266+
;; out.
267+
(unreachable)
239268
)
240269
)
241270
;; Add an effect here, so the two blocks are not mergeable.
@@ -338,7 +367,7 @@
338367
)
339368

340369
(module
341-
;; CHECK: (import "a" "b" (func $effect (type $1) (result i32)))
370+
;; CHECK: (import "a" "b" (func $effect (type $2) (result i32)))
342371
(import "a" "b" (func $effect (result i32)))
343372

344373
;; CHECK: (tag $e (param i32))
@@ -347,7 +376,7 @@
347376
;; CHECK: (tag $multi (param i32 f64))
348377
(tag $multi (param i32 f64))
349378

350-
;; CHECK: (func $throw-caught-all (type $0) (param $x i32)
379+
;; CHECK: (func $throw-caught-all (type $1) (param $x i32)
351380
;; CHECK-NEXT: (block $catch
352381
;; CHECK-NEXT: (try_table (catch_all $catch)
353382
;; CHECK-NEXT: (drop
@@ -368,35 +397,32 @@
368397
)
369398
)
370399

371-
;; CHECK: (func $throw-br-contents (type $1) (result i32)
400+
;; CHECK: (func $throw-br-contents (type $2) (result i32)
372401
;; CHECK-NEXT: (block $catch (result i32)
373-
;; CHECK-NEXT: (try_table (catch $e $catch)
374-
;; CHECK-NEXT: (br $catch
375-
;; CHECK-NEXT: (i32.const 42)
376-
;; CHECK-NEXT: )
402+
;; CHECK-NEXT: (try_table (result i32) (catch $e $catch)
403+
;; CHECK-NEXT: (i32.const 42)
377404
;; CHECK-NEXT: )
378405
;; CHECK-NEXT: )
379406
;; CHECK-NEXT: )
380407
(func $throw-br-contents (result i32)
381408
(block $catch (result i32)
382409
(try_table (catch $e $catch)
383410
;; This throw is not caught by catch_all as above, so the value must be
384-
;; sent as a value on the br we optimize it to.
411+
;; sent as a value on the br we optimize it to. That br can also be
412+
;; optimized away by letting the value flow out.
385413
(throw $e
386414
(i32.const 42)
387415
)
388416
)
389417
)
390418
)
391419

392-
;; CHECK: (func $throw-br-contents-multi (type $2) (result i32 f64)
393-
;; CHECK-NEXT: (block $catch (type $2) (result i32 f64)
394-
;; CHECK-NEXT: (try_table (catch $multi $catch)
395-
;; CHECK-NEXT: (br $catch
396-
;; CHECK-NEXT: (tuple.make 2
397-
;; CHECK-NEXT: (i32.const 42)
398-
;; CHECK-NEXT: (f64.const 3.14159)
399-
;; CHECK-NEXT: )
420+
;; CHECK: (func $throw-br-contents-multi (type $0) (result i32 f64)
421+
;; CHECK-NEXT: (block $catch (type $0) (result i32 f64)
422+
;; CHECK-NEXT: (try_table (type $0) (result i32 f64) (catch $multi $catch)
423+
;; CHECK-NEXT: (tuple.make 2
424+
;; CHECK-NEXT: (i32.const 42)
425+
;; CHECK-NEXT: (f64.const 3.14159)
400426
;; CHECK-NEXT: )
401427
;; CHECK-NEXT: )
402428
;; CHECK-NEXT: )

0 commit comments

Comments
 (0)