Skip to content

Commit 4d94f34

Browse files
committed
Take BrOn into account in CodeFolding
CodeFolding previously did not consider br_on_* instructions at all, so it would happily merge tails even if there were br_on_* branches to the same label with non-matching tails. Fix the bug by making any label targeted by a br_on_* branch unoptimizable. Folding these branches properly is left as future work. Also rename the test file from code-folding_enable-threads.wast to just code-folding.wast and enable all features instead of just threads. The old name was left over from when the test was originally ported to lit, and the new feature is necessary because the new test uses GC instructions.
1 parent 485ac7d commit 4d94f34

File tree

2 files changed

+60
-13
lines changed

2 files changed

+60
-13
lines changed

src/passes/CodeFolding.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,14 @@ struct CodeFolding : public WalkerPass<ControlFlowWalker<CodeFolding>> {
155155
}
156156
}
157157

158+
void visitBrOn(BrOn* curr) {
159+
// TODO: Handle folding br_on* instructions. br_on_null could be folded with
160+
// other kinds of branches and br_on_non_null, br_on_cast, and
161+
// br_on_cast_fail instructions could be folded with other copies of
162+
// themselves.
163+
unoptimizables.insert(curr->name);
164+
}
165+
158166
void visitSwitch(Switch* curr) {
159167
for (auto target : curr->targets) {
160168
unoptimizables.insert(target);

test/lit/passes/code-folding_enable-threads.wast renamed to test/lit/passes/code-folding.wast

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited.
22
;; NOTE: This test was ported using port_passes_tests_to_lit.py and could be cleaned up.
33

4-
;; RUN: foreach %s %t wasm-opt --code-folding --enable-threads -S -o - | filecheck %s
4+
;; RUN: foreach %s %t wasm-opt -all --code-folding -S -o - | filecheck %s
55

66
(module
77
;; CHECK: (type $0 (func))
@@ -15,13 +15,13 @@
1515
(memory $0 1 1)
1616
;; CHECK: (table $0 282 282 funcref)
1717

18-
;; CHECK: (func $0
18+
;; CHECK: (func $0 (type $0)
1919
;; CHECK-NEXT: (block $label$1
2020
;; CHECK-NEXT: (if
2121
;; CHECK-NEXT: (i32.const 1)
2222
;; CHECK-NEXT: (then
2323
;; CHECK-NEXT: (block $label$3
24-
;; CHECK-NEXT: (call_indirect (type $13)
24+
;; CHECK-NEXT: (call_indirect $0 (type $13)
2525
;; CHECK-NEXT: (block $label$4
2626
;; CHECK-NEXT: (br $label$3)
2727
;; CHECK-NEXT: )
@@ -52,7 +52,7 @@
5252
)
5353
)
5454
)
55-
;; CHECK: (func $negative-zero (result f32)
55+
;; CHECK: (func $negative-zero (type $1) (result f32)
5656
;; CHECK-NEXT: (if (result f32)
5757
;; CHECK-NEXT: (i32.const 0)
5858
;; CHECK-NEXT: (then
@@ -82,7 +82,7 @@
8282
)
8383
)
8484
)
85-
;; CHECK: (func $negative-zero-b (result f32)
85+
;; CHECK: (func $negative-zero-b (type $1) (result f32)
8686
;; CHECK-NEXT: (if (result f32)
8787
;; CHECK-NEXT: (i32.const 0)
8888
;; CHECK-NEXT: (then
@@ -112,7 +112,7 @@
112112
)
113113
)
114114
)
115-
;; CHECK: (func $negative-zero-c (result f32)
115+
;; CHECK: (func $negative-zero-c (type $1) (result f32)
116116
;; CHECK-NEXT: (if (result f32)
117117
;; CHECK-NEXT: (i32.const 0)
118118
;; CHECK-NEXT: (then
@@ -142,7 +142,7 @@
142142
)
143143
)
144144
)
145-
;; CHECK: (func $break-target-outside-of-return-merged-code
145+
;; CHECK: (func $break-target-outside-of-return-merged-code (type $0)
146146
;; CHECK-NEXT: (block $label$A
147147
;; CHECK-NEXT: (if
148148
;; CHECK-NEXT: (unreachable)
@@ -216,7 +216,7 @@
216216
)
217217
)
218218
)
219-
;; CHECK: (func $break-target-inside-all-good
219+
;; CHECK: (func $break-target-inside-all-good (type $0)
220220
;; CHECK-NEXT: (block $folding-inner0
221221
;; CHECK-NEXT: (block $label$A
222222
;; CHECK-NEXT: (if
@@ -283,7 +283,7 @@
283283
)
284284
)
285285
)
286-
;; CHECK: (func $leave-inner-block-type
286+
;; CHECK: (func $leave-inner-block-type (type $0)
287287
;; CHECK-NEXT: (block $label$1
288288
;; CHECK-NEXT: (drop
289289
;; CHECK-NEXT: (block $label$2
@@ -326,7 +326,7 @@
326326
(memory $0 1 1 shared)
327327
;; CHECK: (export "func_2224" (func $0))
328328
(export "func_2224" (func $0))
329-
;; CHECK: (func $0 (result i32)
329+
;; CHECK: (func $0 (type $0) (result i32)
330330
;; CHECK-NEXT: (local $var$0 i32)
331331
;; CHECK-NEXT: (if (result i32)
332332
;; CHECK-NEXT: (i32.const 0)
@@ -366,7 +366,7 @@
366366

367367
;; CHECK: (global $global$0 (mut i32) (i32.const 10))
368368
(global $global$0 (mut i32) (i32.const 10))
369-
;; CHECK: (func $determinism
369+
;; CHECK: (func $determinism (type $0)
370370
;; CHECK-NEXT: (block $folding-inner0
371371
;; CHECK-NEXT: (block
372372
;; CHECK-NEXT: (block $label$1
@@ -453,7 +453,7 @@
453453
)
454454
(unreachable)
455455
)
456-
;; CHECK: (func $careful-of-the-switch (param $0 i32)
456+
;; CHECK: (func $careful-of-the-switch (type $1) (param $0 i32)
457457
;; CHECK-NEXT: (block $label$1
458458
;; CHECK-NEXT: (block $label$3
459459
;; CHECK-NEXT: (block $label$5
@@ -500,7 +500,7 @@
500500
(module
501501
;; CHECK: (type $0 (func))
502502

503-
;; CHECK: (func $unreachable-if-concrete-arms
503+
;; CHECK: (func $unreachable-if-concrete-arms (type $0)
504504
;; CHECK-NEXT: (drop
505505
;; CHECK-NEXT: (if (result i32)
506506
;; CHECK-NEXT: (unreachable)
@@ -529,3 +529,42 @@
529529
(unreachable)
530530
)
531531
)
532+
533+
(module
534+
;; CHECK: (type $0 (func))
535+
536+
;; CHECK: (func $br-on-null (type $0)
537+
;; CHECK-NEXT: (block $block
538+
;; CHECK-NEXT: (drop
539+
;; CHECK-NEXT: (br_on_null $block
540+
;; CHECK-NEXT: (ref.null none)
541+
;; CHECK-NEXT: )
542+
;; CHECK-NEXT: )
543+
;; CHECK-NEXT: (drop
544+
;; CHECK-NEXT: (block (result i32)
545+
;; CHECK-NEXT: (call $br-on-null)
546+
;; CHECK-NEXT: (br $block)
547+
;; CHECK-NEXT: )
548+
;; CHECK-NEXT: )
549+
;; CHECK-NEXT: (call $br-on-null)
550+
;; CHECK-NEXT: )
551+
;; CHECK-NEXT: )
552+
(func $br-on-null
553+
(block $block
554+
(drop
555+
;; The other two tails are the same, but this br_on_null should inhibit code
556+
;; folding.
557+
(br_on_null $block
558+
(ref.null none)
559+
)
560+
)
561+
(drop
562+
(block (result i32)
563+
(call $br-on-null)
564+
(br $block)
565+
)
566+
)
567+
(call $br-on-null)
568+
)
569+
)
570+
)

0 commit comments

Comments
 (0)