From 8b5a6171af0f9e434711b8a9bd2f217994a810de Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Wed, 4 Jun 2025 11:28:12 +0800 Subject: [PATCH 01/27] opt for #7637 --- src/passes/RemoveUnusedBrs.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 6f20cfd63af..159197a9cd1 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -24,6 +24,7 @@ #include "ir/effects.h" #include "ir/gc-type-utils.h" #include "ir/literal-utils.h" +#include "ir/properties.h" #include "ir/utils.h" #include "parsing.h" #include "pass.h" @@ -1881,6 +1882,23 @@ struct RemoveUnusedBrs : public WalkerPass> { start = end; } } + + void visitBreak(Break* curr) { + if (!curr->condition) { + return; + } + // optimize if condition's fallthrough is a constant + auto* value = Properties::getFallthrough( + curr->condition, passOptions, *getModule()); + if (auto* c = value->dynCast()) { + curr->condition = getDroppedChildrenAndAppend( + curr->condition, + *getModule(), + passOptions, + Builder(*getModule()).makeConst(c->value), + DropMode::NoticeParentEffects); + } + } }; FinalOptimizer finalOptimizer(getPassOptions()); finalOptimizer.setModule(getModule()); From b9db9cf1a52e65ab7c57fe7763854f0b852b7570 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Wed, 4 Jun 2025 11:28:20 +0800 Subject: [PATCH 02/27] tests --- test/lit/passes/remove-unused-brs.wast | 7 ++++++- test/passes/remove-unused-brs_enable-multivalue.txt | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index ed7fd168148..9b0a12063bd 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -472,7 +472,12 @@ ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (br_if $block ;; CHECK-NEXT: (local.get $temp) - ;; CHECK-NEXT: (local.tee $temp + ;; CHECK-NEXT: (block (result i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.tee $temp + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: (i32.const 1) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) diff --git a/test/passes/remove-unused-brs_enable-multivalue.txt b/test/passes/remove-unused-brs_enable-multivalue.txt index a20f2905bf0..d3c5c40d464 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.txt +++ b/test/passes/remove-unused-brs_enable-multivalue.txt @@ -2897,7 +2897,12 @@ (i32.const 1026) (then (br_if $label$1 - (local.tee $0 + (block (result i32) + (drop + (local.tee $0 + (i32.const -7) + ) + ) (i32.const -7) ) ) From 223bfc0607a9c74da747f1a461da2bd7ff45126f Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Wed, 4 Jun 2025 13:23:36 +0800 Subject: [PATCH 03/27] add a test --- test/lit/passes/remove-unused-brs.wast | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index 9b0a12063bd..aa067049ff6 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -340,6 +340,31 @@ ) ) + ;; CHECK: (func $restructure-br_if-value-effectful-corner-case-5 (type $5) (param $x i32) + ;; CHECK-NEXT: (block $x + ;; CHECK-NEXT: (br_if $x + ;; CHECK-NEXT: (block (result i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (local.tee $x + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $restructure-br_if-value-effectful-corner-case-5 (param $x i32) + (block $x + (br_if $x + (local.tee $x + (i32.const 1) + ) + ) + ) + ) + + ;; CHECK: (func $restructure-br_if-value-redundant-in-block-tail-1 (type $2) (result i32) ;; CHECK-NEXT: (block $parent (result i32) ;; CHECK-NEXT: (call $nothing) From 1f44ad6757213555dbe66762f18654bc3cf5de5c Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Wed, 4 Jun 2025 15:24:36 +0800 Subject: [PATCH 04/27] Add comments --- test/lit/passes/remove-unused-brs.wast | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index aa067049ff6..481b429660c 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -357,6 +357,8 @@ (func $restructure-br_if-value-effectful-corner-case-5 (param $x i32) (block $x (br_if $x + ;; the fallthrough of the condition is constant, + ;; we can infer how the branch happens and apply that (local.tee $x (i32.const 1) ) From 1feb9ca812746981349502c4dcc55f5a6106158d Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 5 Jun 2025 11:40:23 +0800 Subject: [PATCH 05/27] Use ChildLocalizer if there is no curr->value --- src/passes/RemoveUnusedBrs.cpp | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 159197a9cd1..8f758183171 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -24,6 +24,7 @@ #include "ir/effects.h" #include "ir/gc-type-utils.h" #include "ir/literal-utils.h" +#include "ir/localize.h" #include "ir/properties.h" #include "ir/utils.h" #include "parsing.h" @@ -1887,16 +1888,32 @@ struct RemoveUnusedBrs : public WalkerPass> { if (!curr->condition) { return; } - // optimize if condition's fallthrough is a constant auto* value = Properties::getFallthrough( curr->condition, passOptions, *getModule()); + // optimize if condition's fallthrough is a constant if (auto* c = value->dynCast()) { - curr->condition = getDroppedChildrenAndAppend( - curr->condition, - *getModule(), - passOptions, - Builder(*getModule()).makeConst(c->value), - DropMode::NoticeParentEffects); + curr->dump(); + if (curr->value) { + curr->condition = getDroppedChildrenAndAppend( + curr->condition, + *getModule(), + passOptions, + Builder(*getModule()).makeConst(c->value), + DropMode::NoticeParentEffects); + } else { + ChildLocalizer localizer( + curr, getFunction(), *getModule(), passOptions); + auto* block = localizer.getChildrenReplacement(); + if (c->value.geti32()) { + // the branch is always taken, make it unconditional + curr->condition = nullptr; + block->list.push_back(curr); + replaceCurrent(block); + } else { + // the branch is never taken, allow control flow to fall through + replaceCurrent(block); + } + } } } }; From df10dcee303aecdb5bc0b2df695003c7796870ee Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 5 Jun 2025 11:40:38 +0800 Subject: [PATCH 06/27] Update test --- test/lit/passes/remove-unused-brs.wast | 58 ++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index 481b429660c..c5c18c54391 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -340,30 +340,74 @@ ) ) - ;; CHECK: (func $restructure-br_if-value-effectful-corner-case-5 (type $5) (param $x i32) - ;; CHECK-NEXT: (block $x + ;; CHECK: (func $restructure-br_if-constant-branch-1 (type $0) (param $x i32) (result i32) + ;; CHECK-NEXT: (block $x (result i32) ;; CHECK-NEXT: (br_if $x + ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: (block (result i32) ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (local.tee $x - ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: (i32.const 42) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: (i32.const 42) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - (func $restructure-br_if-value-effectful-corner-case-5 (param $x i32) + (func $restructure-br_if-constant-branch-1 (param $x i32) (result i32) + (block $x (result i32) + (br_if $x + (unreachable) + (local.tee $x + (i32.const 42) + ) + ) + (unreachable) + ) + ) + ;; CHECK: (func $restructure-br_if-constant-branch-2 (type $5) (param $x i32) + ;; CHECK-NEXT: (local $1 i32) + ;; CHECK-NEXT: (local $2 i32) + ;; CHECK-NEXT: (block $x + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.tee $x + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (br $x) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (block $x0 + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.tee $x + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $restructure-br_if-constant-branch-2 (param $x i32) + ;; there is no value, thus it can be simpler as below (block $x (br_if $x - ;; the fallthrough of the condition is constant, - ;; we can infer how the branch happens and apply that + ;; the branch is always taken, make it unconditional (local.tee $x (i32.const 1) ) ) ) + (block $x + (br_if $x + ;; the branch is never taken, allow control flow to fall through + (local.tee $x + (i32.const 0) + ) + ) + ) ) From bae974971d79424fa55e9d5eede4bb32cfc5bea8 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 5 Jun 2025 14:17:09 +0800 Subject: [PATCH 07/27] NFC --- src/passes/RemoveUnusedBrs.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 8f758183171..f41ed2913bc 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -1892,7 +1892,6 @@ struct RemoveUnusedBrs : public WalkerPass> { curr->condition, passOptions, *getModule()); // optimize if condition's fallthrough is a constant if (auto* c = value->dynCast()) { - curr->dump(); if (curr->value) { curr->condition = getDroppedChildrenAndAppend( curr->condition, From 6eafa8c390b183910f34b8abd13312cc9efc4925 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 5 Jun 2025 14:17:21 +0800 Subject: [PATCH 08/27] Bugfix --- src/passes/RemoveUnusedBrs.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index f41ed2913bc..dd8da4efd37 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -1906,7 +1906,9 @@ struct RemoveUnusedBrs : public WalkerPass> { if (c->value.geti32()) { // the branch is always taken, make it unconditional curr->condition = nullptr; + curr->type = Type::unreachable; block->list.push_back(curr); + block->finalize(); replaceCurrent(block); } else { // the branch is never taken, allow control flow to fall through From 3dacea0bf769dbb8a6215c29be8d545d291a838d Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Fri, 6 Jun 2025 16:57:02 +0800 Subject: [PATCH 09/27] refinalize the type --- src/passes/RemoveUnusedBrs.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index dd8da4efd37..2c911f41239 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -1909,6 +1909,8 @@ struct RemoveUnusedBrs : public WalkerPass> { curr->type = Type::unreachable; block->list.push_back(curr); block->finalize(); + // the type is changed , so refinalize + refinalize = true; replaceCurrent(block); } else { // the branch is never taken, allow control flow to fall through From e41f3443616c2686f60e965af5901d8305339ca7 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Fri, 6 Jun 2025 17:30:17 +0800 Subject: [PATCH 10/27] Add test of two-branches-unreachable --- test/lit/passes/O1.wast | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/test/lit/passes/O1.wast b/test/lit/passes/O1.wast index 49271699d73..846686dd5cc 100644 --- a/test/lit/passes/O1.wast +++ b/test/lit/passes/O1.wast @@ -4,6 +4,8 @@ ;; RUN: foreach %s %t wasm-opt -O1 -S -o - | filecheck %s (module + + (import "env" "fimport$0" (func $fimport$0 (param i32))) ;; CHECK: (type $0 (func (result i32))) ;; CHECK: (memory $0 1 1) @@ -38,5 +40,37 @@ (i32.const -2147483648) ) ) + (func $two-branches-unreachable + ;; remove-unused-brs makes the break unconditional, + ;; thus in this case the two branches are unreachable, + ;; refinalization is required. + (local $0 i32) + (block $label + (block $block + (if + (local.get $0) + (then + (if + (local.tee $0 + (i32.const 1) + ) + (then + (br $label) + ) + (else + (br $block) + ) + ) + ) + (else + (unreachable) + ) + ) + ) + (call $fimport$0 + (local.get $0) + ) + ) + ) ) From abbe4ce63e15e467ec82609aa060bbadf9c1c9dd Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Fri, 6 Jun 2025 17:52:46 +0800 Subject: [PATCH 11/27] Use auto_update script --- .../remove-unused-brs_enable-multivalue.txt | 141 +++++++----------- .../remove-unused-brs_shrink-level=1.txt | 75 +++++----- ...s_shrink-level=1_ignore-implicit-traps.txt | 69 ++++----- 3 files changed, 123 insertions(+), 162 deletions(-) diff --git a/test/passes/remove-unused-brs_enable-multivalue.txt b/test/passes/remove-unused-brs_enable-multivalue.txt index d3c5c40d464..8e7881785c1 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.txt +++ b/test/passes/remove-unused-brs_enable-multivalue.txt @@ -62,8 +62,8 @@ ) (func $b6 (param $i1 i32) (block $topmost - (br_if $topmost - (i32.const 1) + (block + (br $topmost) ) ) ) @@ -73,8 +73,8 @@ (drop (i32.const 0) ) - (br_if $topmost - (i32.const 1) + (block + (br $topmost) ) ) ) @@ -82,8 +82,8 @@ (func $b8 (param $i1 i32) (block $topmost (block $inner - (br_if $topmost - (i32.const 1) + (block + (br $topmost) ) ) ) @@ -91,8 +91,8 @@ (func $b9 (param $i1 i32) (block $topmost (block $inner - (br_if $topmost - (i32.const 1) + (block + (br $topmost) ) ) ) @@ -104,8 +104,8 @@ (drop (i32.const 0) ) - (br_if $topmost - (i32.const 1) + (block + (br $topmost) ) ) ) @@ -118,8 +118,8 @@ (drop (i32.const 0) ) - (br_if $topmost - (i32.const 1) + (block + (br $topmost) ) ) ) @@ -225,8 +225,8 @@ ) (func $b15 (block $topmost - (br_if $topmost - (i32.const 17) + (block + (br $topmost) ) ) ) @@ -516,15 +516,13 @@ ) (loop (block $out0 - (br_if $out0 - (i32.const 0) + (block ) ) ) (loop $in1 (block $out1 - (br_if $out1 - (i32.const 0) + (block ) ) ) @@ -535,24 +533,17 @@ ) ) (loop $in4 - (if - (i32.eqz - (i32.const 0) + (block $out3 + (block ) - (then - (block $out3 - (nop) - (br_if $in4 - (i32.const 1) - ) - ) + (block + (br $in4) ) ) ) (loop $in5 (block $out4 - (br_if $in5 - (i32.const 0) + (block ) ) ) @@ -763,19 +754,17 @@ ) (loop $in19 (block $out18 - (drop - (i32.const 0) + (block ) (br $in19) ) ) (loop $in-not (block $out-not - (br_if $out-not - (i32.const -1) + (block + (br $out-not) ) - (br_if $out-not - (i32.const 0) + (block ) (call $loops) (br $in-not) @@ -838,11 +827,11 @@ (block $value-in (result i32) (block $out (block $in - (br_if $out - (i32.const 1) + (block + (br $out) ) - (br_if $out - (i32.const 2) + (block + (br $out) ) (br $value-in (i32.const 3) @@ -857,8 +846,8 @@ (block $stack2 (block $stack3 (block $stack4 - (br_if $stack1 - (i32.const 1) + (block + (br $stack1) ) (unreachable) ) @@ -883,9 +872,7 @@ ) ) (else - (br_if $leave - (i32.const 1) - ) + (br $leave) ) ) (unreachable) @@ -904,9 +891,7 @@ (if (local.get $x) (then - (br_if $leave - (i32.const 1) - ) + (br $leave) ) (else (br $out @@ -993,25 +978,18 @@ ) ) (func $iffify - (if - (i32.eqz - (i32.const 0) + (block $yes + (block ) - (then - (block $yes - (nop) - (drop - (i32.const 1) - ) - (drop - (i32.const 2) - ) - ) + (drop + (i32.const 1) + ) + (drop + (i32.const 2) ) ) (block $no - (br_if $no - (i32.const 0) + (block ) (drop (i32.const 1) @@ -1022,8 +1000,7 @@ ) ) (block $no2 - (br_if $no2 - (i32.const 0) + (block ) ) (block $no3 @@ -1037,8 +1014,7 @@ ) (block $no5 (block $no4 - (br_if $no5 - (i32.const 0) + (block ) (drop (i32.const 1) @@ -1094,22 +1070,24 @@ (func $untaken-brs-might-prevent-block-removal (param $0 f32) (param $1 i32) (param $2 f32) (param $3 i32) (param $4 i32) (param $5 f64) (param $6 f32) (result i32) (block $label$0 (result i32) (block $label$1 - (br_if $label$1 - (i32.const 607395945) + (block + (br $label$1) ) (br_if $label$1 (i32.load offset=3 align=1 (select (call $untaken-brs-might-prevent-block-removal (f32.const 1.4904844647389837e-07) - (br_if $label$0 + (block (i32.store16 offset=4 align=1 (i32.const 1900641) (br $label$0 (i32.const 1628075109) ) ) - (i32.const 1764950569) + (drop + (i32.const 1764950569) + ) ) (f32.const 1.1910939690100655e-32) (i32.const 1628057906) @@ -1925,8 +1903,7 @@ ) (func $same-target-br_if-and-br (block $x - (drop - (i32.const 0) + (block ) (br $x) (unreachable) @@ -2313,8 +2290,8 @@ ) (func $if-block-br (block $label - (br_if $label - (i32.const 1) + (block + (br $label) ) ) ) @@ -2538,8 +2515,7 @@ (block $label$503 (br_if $label$3 (block $label$530 (result i32) - (br_if $label$503 - (i32.const 0) + (block ) (i32.const 0) ) @@ -2861,6 +2837,7 @@ ) (func $if_br_if (local $0 i32) + (local $1 i32) (block $label$1 (br_if $label$1 (select @@ -2896,16 +2873,12 @@ (if (i32.const 1026) (then - (br_if $label$1 - (block (result i32) - (drop - (local.tee $0 - (i32.const -7) - ) - ) + (local.set $1 + (local.tee $0 (i32.const -7) ) ) + (br $label$1) ) ) (i32.store diff --git a/test/passes/remove-unused-brs_shrink-level=1.txt b/test/passes/remove-unused-brs_shrink-level=1.txt index 24696cb8355..c3d7fc4e6c5 100644 --- a/test/passes/remove-unused-brs_shrink-level=1.txt +++ b/test/passes/remove-unused-brs_shrink-level=1.txt @@ -54,78 +54,73 @@ ) (func $join-br_ifs (block $out - (br_if $out - (i32.or - (i32.const 1) - (i32.const 2) - ) + (block + (br $out) ) - (nop) - (br_if $out - (i32.const 3) + (block + (br $out) + ) + (block + (br $out) ) ) (block $out2 (block $out3 - (br_if $out2 - (i32.const 1) + (block + (br $out2) ) - (br_if $out3 - (i32.const 2) + (block + (br $out3) ) - (br_if $out2 - (i32.const 3) + (block + (br $out2) ) ) (unreachable) ) (block $out4 (block $out5 - (br_if $out4 - (i32.const 1) + (block + (br $out4) ) - (br_if $out5 - (i32.or - (i32.const 2) - (i32.const 3) - ) + (block + (br $out5) + ) + (block + (br $out5) ) - (nop) ) (unreachable) ) (block $out6 (block $out7 - (br_if $out6 - (i32.or - (i32.const 1) - (i32.const 2) - ) + (block + (br $out6) + ) + (block + (br $out6) ) - (nop) - (br_if $out7 - (i32.const 3) + (block + (br $out7) ) ) (unreachable) ) (if (i32.eqz - (i32.or - (call $b14) - (i32.const 0) - ) + (call $b14) ) (then (block $out8 (nop) - (nop) + (block + ) ) ) ) (block $out80 - (br_if $out80 - (i32.const 1) + (block + (br $out80) ) (br_if $out80 (call $b14) @@ -153,15 +148,13 @@ (br_if $label$14 (unreachable) ) - (br_if $label$14 - (i32.const 0) + (block ) ) ) (func $br-if-unreachable-pair2 (block $label$14 - (br_if $label$14 - (i32.const 0) + (block ) (br_if $label$14 (unreachable) diff --git a/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt b/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt index 82f76d359c7..01c913eacab 100644 --- a/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt +++ b/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt @@ -46,78 +46,73 @@ ) (func $join-br_ifs (block $out - (br_if $out - (i32.or - (i32.const 1) - (i32.const 2) - ) + (block + (br $out) + ) + (block + (br $out) ) - (nop) - (br_if $out - (i32.const 3) + (block + (br $out) ) ) (block $out2 (block $out3 - (br_if $out2 - (i32.const 1) + (block + (br $out2) ) - (br_if $out3 - (i32.const 2) + (block + (br $out3) ) - (br_if $out2 - (i32.const 3) + (block + (br $out2) ) ) (unreachable) ) (block $out4 (block $out5 - (br_if $out4 - (i32.const 1) + (block + (br $out4) ) - (br_if $out5 - (i32.or - (i32.const 2) - (i32.const 3) - ) + (block + (br $out5) + ) + (block + (br $out5) ) - (nop) ) (unreachable) ) (block $out6 (block $out7 - (br_if $out6 - (i32.or - (i32.const 1) - (i32.const 2) - ) + (block + (br $out6) + ) + (block + (br $out6) ) - (nop) - (br_if $out7 - (i32.const 3) + (block + (br $out7) ) ) (unreachable) ) (if (i32.eqz - (i32.or - (call $b14) - (i32.const 0) - ) + (call $b14) ) (then (block $out8 (nop) - (nop) + (block + ) ) ) ) (block $out80 - (br_if $out80 - (i32.const 1) + (block + (br $out80) ) (br_if $out80 (call $b14) From 08234b37a1d7a27ff8a9d1fee38e813c36473cc5 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 12 Jun 2025 08:58:18 +0800 Subject: [PATCH 12/27] Add export to make CHECK lines appear --- test/lit/passes/O1.wast | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/test/lit/passes/O1.wast b/test/lit/passes/O1.wast index 846686dd5cc..099942b2cbf 100644 --- a/test/lit/passes/O1.wast +++ b/test/lit/passes/O1.wast @@ -8,11 +8,15 @@ (import "env" "fimport$0" (func $fimport$0 (param i32))) ;; CHECK: (type $0 (func (result i32))) + ;; CHECK: (type $1 (func)) + ;; CHECK: (memory $0 1 1) (memory $0 1 1) (global $global$0 (mut i32) (i32.const 10)) ;; CHECK: (export "foo" (func $foo)) + ;; CHECK: (export "two-branches-unreachable" (func $two-branches-unreachable)) + ;; CHECK: (func $foo (result i32) ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (i32.const 0) @@ -40,7 +44,18 @@ (i32.const -2147483648) ) ) - (func $two-branches-unreachable + ;; CHECK: (func $two-branches-unreachable + ;; CHECK-NEXT: (local $0 i32) + ;; CHECK-NEXT: (if + ;; CHECK-NEXT: (i32.eqz + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (then + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + (func $two-branches-unreachable (export "two-branches-unreachable") ;; remove-unused-brs makes the break unconditional, ;; thus in this case the two branches are unreachable, ;; refinalization is required. From 05f8a1ad60e0f46ca0a76d7cbcdb56736630bf65 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 12 Jun 2025 10:52:54 +0800 Subject: [PATCH 13/27] work --- src/passes/RemoveUnusedBrs.cpp | 38 +++++++++++++++------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 2c911f41239..54a151a557d 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -27,6 +27,7 @@ #include "ir/localize.h" #include "ir/properties.h" #include "ir/utils.h" +#include "literal.h" #include "parsing.h" #include "pass.h" #include "support/small_set.h" @@ -1892,30 +1893,25 @@ struct RemoveUnusedBrs : public WalkerPass> { curr->condition, passOptions, *getModule()); // optimize if condition's fallthrough is a constant if (auto* c = value->dynCast()) { - if (curr->value) { - curr->condition = getDroppedChildrenAndAppend( - curr->condition, - *getModule(), - passOptions, - Builder(*getModule()).makeConst(c->value), - DropMode::NoticeParentEffects); + ChildLocalizer localizer( + curr, getFunction(), *getModule(), passOptions); + auto* block = localizer.getChildrenReplacement(); + if (c->value.geti32()) { + // the branch is always taken, make it unconditional + curr->condition = nullptr; + curr->type = Type::unreachable; + block->list.push_back(curr); + block->finalize(); + // the type is changed , so refinalize + refinalize = true; + replaceCurrent(block); } else { - ChildLocalizer localizer( - curr, getFunction(), *getModule(), passOptions); - auto* block = localizer.getChildrenReplacement(); - if (c->value.geti32()) { - // the branch is always taken, make it unconditional - curr->condition = nullptr; - curr->type = Type::unreachable; - block->list.push_back(curr); + if (curr->value) { + block->list.push_back(curr->value); block->finalize(); - // the type is changed , so refinalize - refinalize = true; - replaceCurrent(block); - } else { - // the branch is never taken, allow control flow to fall through - replaceCurrent(block); } + // the branch is never taken, allow control flow to fall through + replaceCurrent(block); } } } From ed25e753342794e6a81699cce4bde3242bad6e04 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 12 Jun 2025 10:54:25 +0800 Subject: [PATCH 14/27] Update tests --- test/lit/passes/remove-unused-brs.wast | 108 ++++++++++++------ .../remove-unused-brs_all-features.wast | 24 ++-- 2 files changed, 84 insertions(+), 48 deletions(-) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index c5c18c54391..e73b3bfae42 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -5,7 +5,7 @@ (module ;; Regression test in which we need to calculate a proper LUB. - ;; CHECK: (func $selectify-fresh-lub (type $4) (param $x i32) (result anyref) + ;; CHECK: (func $selectify-fresh-lub (type $5) (param $x i32) (result anyref) ;; CHECK-NEXT: (select (result i31ref) ;; CHECK-NEXT: (ref.null none) ;; CHECK-NEXT: (ref.i31 @@ -340,34 +340,63 @@ ) ) - ;; CHECK: (func $restructure-br_if-constant-branch-1 (type $0) (param $x i32) (result i32) - ;; CHECK-NEXT: (block $x (result i32) - ;; CHECK-NEXT: (br_if $x - ;; CHECK-NEXT: (unreachable) - ;; CHECK-NEXT: (block (result i32) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (local.tee $x - ;; CHECK-NEXT: (i32.const 42) + ;; CHECK: (func $restructure-br_if-constant-branch-1 (type $3) (param $x i32) + ;; CHECK-NEXT: (local $1 i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block $x (result i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.tee $x + ;; CHECK-NEXT: (i32.const 42) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (br $x + ;; CHECK-NEXT: (i32.const 10) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (i32.const 42) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i32.const 20) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block $x0 (result i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block (result i32) + ;; CHECK-NEXT: (i32.const 10) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (i32.const 20) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - (func $restructure-br_if-constant-branch-1 (param $x i32) (result i32) - (block $x (result i32) - (br_if $x - (unreachable) - (local.tee $x - (i32.const 42) + (func $restructure-br_if-constant-branch-1 (param $x i32) + (drop + (block $x (result i32) + (drop + (br_if $x + (i32.const 10) + (local.tee $x + (i32.const 42) + ) + ) + ) + (i32.const 20) + ) + ) + (drop + (block $x (result i32) + (drop + (br_if $x + (i32.const 10) + (i32.const 0) + ) ) + (i32.const 20) ) - (unreachable) ) ) - ;; CHECK: (func $restructure-br_if-constant-branch-2 (type $5) (param $x i32) + ;; CHECK: (func $restructure-br_if-constant-branch-2 (type $3) (param $x i32) ;; CHECK-NEXT: (local $1 i32) ;; CHECK-NEXT: (local $2 i32) ;; CHECK-NEXT: (block $x @@ -539,17 +568,21 @@ ;; CHECK: (func $restructure-br_if-condition-invalidates-6 (type $2) (result i32) ;; CHECK-NEXT: (local $temp i32) + ;; CHECK-NEXT: (local $1 i32) + ;; CHECK-NEXT: (local $2 i32) ;; CHECK-NEXT: (block $block (result i32) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (br_if $block - ;; CHECK-NEXT: (local.get $temp) - ;; CHECK-NEXT: (block (result i32) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (local.tee $temp - ;; CHECK-NEXT: (i32.const 1) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (local.get $temp) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.set $2 + ;; CHECK-NEXT: (local.tee $temp + ;; CHECK-NEXT: (i32.const 1) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (br $block + ;; CHECK-NEXT: (local.get $1) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) @@ -574,17 +607,22 @@ ) ;; CHECK: (func $restructure-select-no-multivalue (type $1) + ;; CHECK-NEXT: (local $0 (tuple i32 i32)) ;; CHECK-NEXT: (tuple.drop 2 - ;; CHECK-NEXT: (block $block (type $3) (result i32 i32) - ;; CHECK-NEXT: (tuple.drop 2 - ;; CHECK-NEXT: (br_if $block - ;; CHECK-NEXT: (tuple.make 2 - ;; CHECK-NEXT: (i32.const 1) - ;; CHECK-NEXT: (call $restructure-br_if - ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: (block $block (type $4) (result i32 i32) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (local.set $0 + ;; CHECK-NEXT: (tuple.make 2 + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: (call $restructure-br_if + ;; CHECK-NEXT: (i32.const 2) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (i32.const 3) + ;; CHECK-NEXT: (br $block + ;; CHECK-NEXT: (local.get $0) + ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (tuple.make 2 diff --git a/test/lit/passes/remove-unused-brs_all-features.wast b/test/lit/passes/remove-unused-brs_all-features.wast index e2d89a5ead5..3f971df30e1 100644 --- a/test/lit/passes/remove-unused-brs_all-features.wast +++ b/test/lit/passes/remove-unused-brs_all-features.wast @@ -65,22 +65,20 @@ ;; CHECK: (func $test-prefinalize (type $4) (result f64) ;; CHECK-NEXT: (loop $loop (result f64) - ;; CHECK-NEXT: (if (result f64) - ;; CHECK-NEXT: (i32.const 1) - ;; CHECK-NEXT: (then - ;; CHECK-NEXT: (f64.const 0) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (else - ;; CHECK-NEXT: (block $block (result f64) - ;; CHECK-NEXT: (nop) - ;; CHECK-NEXT: (br_if $loop - ;; CHECK-NEXT: (i32.eqz - ;; CHECK-NEXT: (i32.const 0) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (block $block (result f64) + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (block + ;; CHECK-NEXT: (br $block + ;; CHECK-NEXT: (f64.const 0) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (br_if $loop + ;; CHECK-NEXT: (i32.eqz + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) From 904b4ef7c5f163f76890bc34b7c57db14a38973a Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Thu, 12 Jun 2025 11:07:54 +0800 Subject: [PATCH 15/27] Update also --- test/passes/remove-unused-brs_enable-multivalue.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/passes/remove-unused-brs_enable-multivalue.txt b/test/passes/remove-unused-brs_enable-multivalue.txt index 8e7881785c1..0f1fa887d66 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.txt +++ b/test/passes/remove-unused-brs_enable-multivalue.txt @@ -166,9 +166,10 @@ (i32.const 12) ) (drop - (br_if $topmost - (i32.const 1) - (i32.const 1) + (block + (br $topmost + (i32.const 1) + ) ) ) ) @@ -1088,6 +1089,7 @@ (drop (i32.const 1764950569) ) + (unreachable) ) (f32.const 1.1910939690100655e-32) (i32.const 1628057906) From 9eca5a3f701c626b5fb475baad9ab143042a12fb Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Fri, 13 Jun 2025 20:14:40 +0800 Subject: [PATCH 16/27] Remove unused include --- src/passes/RemoveUnusedBrs.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 54a151a557d..5e561bd1c57 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -27,7 +27,6 @@ #include "ir/localize.h" #include "ir/properties.h" #include "ir/utils.h" -#include "literal.h" #include "parsing.h" #include "pass.h" #include "support/small_set.h" From 025285111b6071e8a20019511026187134c90c52 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Fri, 13 Jun 2025 20:15:48 +0800 Subject: [PATCH 17/27] NFC: format the comments --- src/passes/RemoveUnusedBrs.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 5e561bd1c57..6bb49471918 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -1890,7 +1890,7 @@ struct RemoveUnusedBrs : public WalkerPass> { } auto* value = Properties::getFallthrough( curr->condition, passOptions, *getModule()); - // optimize if condition's fallthrough is a constant + // Optimize if condition's fallthrough is a constant. if (auto* c = value->dynCast()) { ChildLocalizer localizer( curr, getFunction(), *getModule(), passOptions); @@ -1901,7 +1901,7 @@ struct RemoveUnusedBrs : public WalkerPass> { curr->type = Type::unreachable; block->list.push_back(curr); block->finalize(); - // the type is changed , so refinalize + // The type changed, so refinalize. refinalize = true; replaceCurrent(block); } else { From 71c20822516c2147e15d5f31f4a88f770e8e8f81 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Fri, 13 Jun 2025 20:17:43 +0800 Subject: [PATCH 18/27] Only one replaceCurrent --- src/passes/RemoveUnusedBrs.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index 6bb49471918..64c68f2354e 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -1903,15 +1903,14 @@ struct RemoveUnusedBrs : public WalkerPass> { block->finalize(); // The type changed, so refinalize. refinalize = true; - replaceCurrent(block); } else { + // the branch is never taken, allow control flow to fall through if (curr->value) { block->list.push_back(curr->value); block->finalize(); } - // the branch is never taken, allow control flow to fall through - replaceCurrent(block); } + replaceCurrent(block); } } }; From e6116dc173ac8d0a5e0804b5573fd1f8c4cad12b Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Sun, 15 Jun 2025 09:44:31 +0800 Subject: [PATCH 19/27] Remove unused case --- test/lit/passes/O1.wast | 49 ----------------------------------------- 1 file changed, 49 deletions(-) diff --git a/test/lit/passes/O1.wast b/test/lit/passes/O1.wast index 099942b2cbf..49271699d73 100644 --- a/test/lit/passes/O1.wast +++ b/test/lit/passes/O1.wast @@ -4,19 +4,13 @@ ;; RUN: foreach %s %t wasm-opt -O1 -S -o - | filecheck %s (module - - (import "env" "fimport$0" (func $fimport$0 (param i32))) ;; CHECK: (type $0 (func (result i32))) - ;; CHECK: (type $1 (func)) - ;; CHECK: (memory $0 1 1) (memory $0 1 1) (global $global$0 (mut i32) (i32.const 10)) ;; CHECK: (export "foo" (func $foo)) - ;; CHECK: (export "two-branches-unreachable" (func $two-branches-unreachable)) - ;; CHECK: (func $foo (result i32) ;; CHECK-NEXT: (drop ;; CHECK-NEXT: (i32.const 0) @@ -44,48 +38,5 @@ (i32.const -2147483648) ) ) - ;; CHECK: (func $two-branches-unreachable - ;; CHECK-NEXT: (local $0 i32) - ;; CHECK-NEXT: (if - ;; CHECK-NEXT: (i32.eqz - ;; CHECK-NEXT: (local.get $0) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (then - ;; CHECK-NEXT: (unreachable) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) - (func $two-branches-unreachable (export "two-branches-unreachable") - ;; remove-unused-brs makes the break unconditional, - ;; thus in this case the two branches are unreachable, - ;; refinalization is required. - (local $0 i32) - (block $label - (block $block - (if - (local.get $0) - (then - (if - (local.tee $0 - (i32.const 1) - ) - (then - (br $label) - ) - (else - (br $block) - ) - ) - ) - (else - (unreachable) - ) - ) - ) - (call $fimport$0 - (local.get $0) - ) - ) - ) ) From 8e92604b6c78608cc06358ac7c0a77b2cfce1137 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Sun, 15 Jun 2025 10:13:46 +0800 Subject: [PATCH 20/27] fine tune to make the irrelevant test case change minimal --- test/lit/passes/remove-unused-brs.wast | 42 +++++++------------ .../remove-unused-brs_all-features.wast | 28 +++++++------ 2 files changed, 32 insertions(+), 38 deletions(-) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index e73b3bfae42..c8e7e91a3cb 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -371,6 +371,7 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) (func $restructure-br_if-constant-branch-1 (param $x i32) + ;; We can see a nonzero constant falls through the tee, so this br always happens. (drop (block $x (result i32) (drop @@ -384,6 +385,7 @@ (i32.const 20) ) ) + ;; We can see the zero condition, so just let the control flow fall through. (drop (block $x (result i32) (drop @@ -568,22 +570,13 @@ ;; CHECK: (func $restructure-br_if-condition-invalidates-6 (type $2) (result i32) ;; CHECK-NEXT: (local $temp i32) - ;; CHECK-NEXT: (local $1 i32) - ;; CHECK-NEXT: (local $2 i32) ;; CHECK-NEXT: (block $block (result i32) ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block - ;; CHECK-NEXT: (local.set $1 + ;; CHECK-NEXT: (br_if $block + ;; CHECK-NEXT: (local.get $temp) + ;; CHECK-NEXT: (local.tee $temp ;; CHECK-NEXT: (local.get $temp) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (local.set $2 - ;; CHECK-NEXT: (local.tee $temp - ;; CHECK-NEXT: (i32.const 1) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (br $block - ;; CHECK-NEXT: (local.get $1) - ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (local.get $temp) @@ -598,7 +591,7 @@ (br_if $block (local.get $temp) (local.tee $temp - (i32.const 1) + (local.get $temp) ) ) ) @@ -607,22 +600,18 @@ ) ;; CHECK: (func $restructure-select-no-multivalue (type $1) - ;; CHECK-NEXT: (local $0 (tuple i32 i32)) + ;; CHECK-NEXT: (local $x i32) ;; CHECK-NEXT: (tuple.drop 2 ;; CHECK-NEXT: (block $block (type $4) (result i32 i32) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block - ;; CHECK-NEXT: (local.set $0 - ;; CHECK-NEXT: (tuple.make 2 - ;; CHECK-NEXT: (i32.const 1) - ;; CHECK-NEXT: (call $restructure-br_if - ;; CHECK-NEXT: (i32.const 2) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (tuple.drop 2 + ;; CHECK-NEXT: (br_if $block + ;; CHECK-NEXT: (tuple.make 2 + ;; CHECK-NEXT: (i32.const 1) + ;; CHECK-NEXT: (call $restructure-br_if + ;; CHECK-NEXT: (i32.const 2) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (br $block - ;; CHECK-NEXT: (local.get $0) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (local.get $x) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: (tuple.make 2 @@ -633,6 +622,7 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) (func $restructure-select-no-multivalue + (local $x i32) (tuple.drop 2 (block $block (result i32 i32) (tuple.drop 2 @@ -647,7 +637,7 @@ (i32.const 2) ) ) - (i32.const 3) + (local.get $x) ) ) (tuple.make 2 diff --git a/test/lit/passes/remove-unused-brs_all-features.wast b/test/lit/passes/remove-unused-brs_all-features.wast index 3f971df30e1..87c1b2ae197 100644 --- a/test/lit/passes/remove-unused-brs_all-features.wast +++ b/test/lit/passes/remove-unused-brs_all-features.wast @@ -64,31 +64,35 @@ ) ;; CHECK: (func $test-prefinalize (type $4) (result f64) + ;; CHECK-NEXT: (local $x i32) ;; CHECK-NEXT: (loop $loop (result f64) - ;; CHECK-NEXT: (block $block (result f64) - ;; CHECK-NEXT: (drop - ;; CHECK-NEXT: (block - ;; CHECK-NEXT: (br $block - ;; CHECK-NEXT: (f64.const 0) - ;; CHECK-NEXT: ) - ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (if (result f64) + ;; CHECK-NEXT: (local.get $x) + ;; CHECK-NEXT: (then + ;; CHECK-NEXT: (f64.const 0) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (br_if $loop - ;; CHECK-NEXT: (i32.eqz - ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: (else + ;; CHECK-NEXT: (block $block (result f64) + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: (br_if $loop + ;; CHECK-NEXT: (i32.eqz + ;; CHECK-NEXT: (i32.const 0) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) - ;; CHECK-NEXT: (unreachable) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) (func $test-prefinalize (result f64) + (local $x i32) (loop $loop (result f64) (block $block (result f64) (drop (br_if $block (f64.const 0) - (i32.const 1) + (local.get $x) ) ) (if From 572e9eea5d08c5135e4c7de33c488f9ecf03d52b Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Tue, 17 Jun 2025 13:37:43 +0800 Subject: [PATCH 21/27] Update the comments --- test/lit/passes/remove-unused-brs.wast | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/lit/passes/remove-unused-brs.wast b/test/lit/passes/remove-unused-brs.wast index c8e7e91a3cb..da8c61cc941 100644 --- a/test/lit/passes/remove-unused-brs.wast +++ b/test/lit/passes/remove-unused-brs.wast @@ -422,10 +422,9 @@ ;; CHECK-NEXT: ) ;; CHECK-NEXT: ) (func $restructure-br_if-constant-branch-2 (param $x i32) - ;; there is no value, thus it can be simpler as below + ;; as before, but now there is no value. (block $x (br_if $x - ;; the branch is always taken, make it unconditional (local.tee $x (i32.const 1) ) @@ -433,7 +432,6 @@ ) (block $x (br_if $x - ;; the branch is never taken, allow control flow to fall through (local.tee $x (i32.const 0) ) From 832126d502864af8c719fc0dab44672cfb239d0d Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Tue, 17 Jun 2025 14:33:28 +0800 Subject: [PATCH 22/27] Revert remove-unused-brs_shrink-level=1.wast (and .txt), use params --- .../remove-unused-brs_shrink-level=1.txt | 70 ++++++++++--------- .../remove-unused-brs_shrink-level=1.wast | 30 ++++---- 2 files changed, 53 insertions(+), 47 deletions(-) diff --git a/test/passes/remove-unused-brs_shrink-level=1.txt b/test/passes/remove-unused-brs_shrink-level=1.txt index c3d7fc4e6c5..ca446f864bc 100644 --- a/test/passes/remove-unused-brs_shrink-level=1.txt +++ b/test/passes/remove-unused-brs_shrink-level=1.txt @@ -53,74 +53,80 @@ (i32.const 0) ) (func $join-br_ifs + (local $x i32) (block $out - (block - (br $out) - ) - (block - (br $out) + (br_if $out + (i32.or + (local.get $x) + (local.get $x) + ) ) - (block - (br $out) + (nop) + (br_if $out + (local.get $x) ) ) (block $out2 (block $out3 - (block - (br $out2) + (br_if $out2 + (local.get $x) ) - (block - (br $out3) + (br_if $out3 + (local.get $x) ) - (block - (br $out2) + (br_if $out2 + (local.get $x) ) ) (unreachable) ) (block $out4 (block $out5 - (block - (br $out4) - ) - (block - (br $out5) + (br_if $out4 + (local.get $x) ) - (block - (br $out5) + (br_if $out5 + (i32.or + (local.get $x) + (local.get $x) + ) ) + (nop) ) (unreachable) ) (block $out6 (block $out7 - (block - (br $out6) - ) - (block - (br $out6) + (br_if $out6 + (i32.or + (local.get $x) + (local.get $x) + ) ) - (block - (br $out7) + (nop) + (br_if $out7 + (local.get $x) ) ) (unreachable) ) (if (i32.eqz - (call $b14) + (i32.or + (call $b14) + (local.get $x) + ) ) (then (block $out8 (nop) - (block - ) + (nop) ) ) ) (block $out80 - (block - (br $out80) + (br_if $out80 + (local.get $x) ) (br_if $out80 (call $b14) diff --git a/test/passes/remove-unused-brs_shrink-level=1.wast b/test/passes/remove-unused-brs_shrink-level=1.wast index ce141c43003..4f56f0eed1f 100644 --- a/test/passes/remove-unused-brs_shrink-level=1.wast +++ b/test/passes/remove-unused-brs_shrink-level=1.wast @@ -55,41 +55,42 @@ (i32.const 0) ) (func $join-br_ifs + (local $x i32) (block $out - (br_if $out (i32.const 1)) - (br_if $out (i32.const 2)) - (br_if $out (i32.const 3)) + (br_if $out (local.get $x)) + (br_if $out (local.get $x)) + (br_if $out (local.get $x)) ) (block $out2 (block $out3 - (br_if $out2 (i32.const 1)) - (br_if $out3 (i32.const 2)) - (br_if $out2 (i32.const 3)) + (br_if $out2 (local.get $x)) + (br_if $out3 (local.get $x)) + (br_if $out2 (local.get $x)) ) (unreachable) ) (block $out4 (block $out5 - (br_if $out4 (i32.const 1)) - (br_if $out5 (i32.const 2)) - (br_if $out5 (i32.const 3)) + (br_if $out4 (local.get $x)) + (br_if $out5 (local.get $x)) + (br_if $out5 (local.get $x)) ) (unreachable) ) (block $out6 (block $out7 - (br_if $out6 (i32.const 1)) - (br_if $out6 (i32.const 2)) - (br_if $out7 (i32.const 3)) + (br_if $out6 (local.get $x)) + (br_if $out6 (local.get $x)) + (br_if $out7 (local.get $x)) ) (unreachable) ) (block $out8 (br_if $out8 (call $b14)) ;; side effect - (br_if $out8 (i32.const 0)) + (br_if $out8 (local.get $x)) ) (block $out8 - (br_if $out8 (i32.const 1)) + (br_if $out8 (local.get $x)) (br_if $out8 (call $b14)) ;; side effect ) ) @@ -190,4 +191,3 @@ (return (i32.const 3)) ) ) - From fccf9fbe2ce4115748c5b60d8f42e904c6984607 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Tue, 17 Jun 2025 14:38:56 +0800 Subject: [PATCH 23/27] Revert remove-unused-brs_shrink-level=1__ignore-implicit-traps.wast (and .txt), use params --- ...s_shrink-level=1_ignore-implicit-traps.txt | 70 ++++++++++--------- ..._shrink-level=1_ignore-implicit-traps.wast | 29 ++++---- 2 files changed, 53 insertions(+), 46 deletions(-) diff --git a/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt b/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt index 01c913eacab..461f5bc194b 100644 --- a/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt +++ b/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.txt @@ -45,74 +45,80 @@ (i32.const 0) ) (func $join-br_ifs + (local $x i32) (block $out - (block - (br $out) - ) - (block - (br $out) + (br_if $out + (i32.or + (local.get $x) + (local.get $x) + ) ) - (block - (br $out) + (nop) + (br_if $out + (local.get $x) ) ) (block $out2 (block $out3 - (block - (br $out2) + (br_if $out2 + (local.get $x) ) - (block - (br $out3) + (br_if $out3 + (local.get $x) ) - (block - (br $out2) + (br_if $out2 + (local.get $x) ) ) (unreachable) ) (block $out4 (block $out5 - (block - (br $out4) - ) - (block - (br $out5) + (br_if $out4 + (local.get $x) ) - (block - (br $out5) + (br_if $out5 + (i32.or + (local.get $x) + (local.get $x) + ) ) + (nop) ) (unreachable) ) (block $out6 (block $out7 - (block - (br $out6) - ) - (block - (br $out6) + (br_if $out6 + (i32.or + (local.get $x) + (local.get $x) + ) ) - (block - (br $out7) + (nop) + (br_if $out7 + (local.get $x) ) ) (unreachable) ) (if (i32.eqz - (call $b14) + (i32.or + (call $b14) + (local.get $x) + ) ) (then (block $out8 (nop) - (block - ) + (nop) ) ) ) (block $out80 - (block - (br $out80) + (br_if $out80 + (local.get $x) ) (br_if $out80 (call $b14) diff --git a/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.wast b/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.wast index 4f15dc9c020..b93cd89925e 100644 --- a/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.wast +++ b/test/passes/remove-unused-brs_shrink-level=1_ignore-implicit-traps.wast @@ -55,41 +55,42 @@ (i32.const 0) ) (func $join-br_ifs + (local $x i32) (block $out - (br_if $out (i32.const 1)) - (br_if $out (i32.const 2)) - (br_if $out (i32.const 3)) + (br_if $out (local.get $x)) + (br_if $out (local.get $x)) + (br_if $out (local.get $x)) ) (block $out2 (block $out3 - (br_if $out2 (i32.const 1)) - (br_if $out3 (i32.const 2)) - (br_if $out2 (i32.const 3)) + (br_if $out2 (local.get $x)) + (br_if $out3 (local.get $x)) + (br_if $out2 (local.get $x)) ) (unreachable) ) (block $out4 (block $out5 - (br_if $out4 (i32.const 1)) - (br_if $out5 (i32.const 2)) - (br_if $out5 (i32.const 3)) + (br_if $out4 (local.get $x)) + (br_if $out5 (local.get $x)) + (br_if $out5 (local.get $x)) ) (unreachable) ) (block $out6 (block $out7 - (br_if $out6 (i32.const 1)) - (br_if $out6 (i32.const 2)) - (br_if $out7 (i32.const 3)) + (br_if $out6 (local.get $x)) + (br_if $out6 (local.get $x)) + (br_if $out7 (local.get $x)) ) (unreachable) ) (block $out8 (br_if $out8 (call $b14)) ;; side effect - (br_if $out8 (i32.const 0)) + (br_if $out8 (local.get $x)) ) (block $out8 - (br_if $out8 (i32.const 1)) + (br_if $out8 (local.get $x)) (br_if $out8 (call $b14)) ;; side effect ) ) From 3d1f166a27eeb6c7ad19bc24b9b2e7e6b61655bc Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Tue, 17 Jun 2025 16:35:30 +0800 Subject: [PATCH 24/27] Update also --- .../remove-unused-brs_enable-multivalue.txt | 37 ++++++++++--------- .../remove-unused-brs_enable-multivalue.wast | 20 +++++----- 2 files changed, 30 insertions(+), 27 deletions(-) diff --git a/test/passes/remove-unused-brs_enable-multivalue.txt b/test/passes/remove-unused-brs_enable-multivalue.txt index 0f1fa887d66..1b5d9802ce6 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.txt +++ b/test/passes/remove-unused-brs_enable-multivalue.txt @@ -62,8 +62,8 @@ ) (func $b6 (param $i1 i32) (block $topmost - (block - (br $topmost) + (br_if $topmost + (local.get $i1) ) ) ) @@ -73,8 +73,8 @@ (drop (i32.const 0) ) - (block - (br $topmost) + (br_if $topmost + (local.get $i1) ) ) ) @@ -82,8 +82,8 @@ (func $b8 (param $i1 i32) (block $topmost (block $inner - (block - (br $topmost) + (br_if $topmost + (local.get $i1) ) ) ) @@ -91,8 +91,8 @@ (func $b9 (param $i1 i32) (block $topmost (block $inner - (block - (br $topmost) + (br_if $topmost + (local.get $i1) ) ) ) @@ -104,8 +104,8 @@ (drop (i32.const 0) ) - (block - (br $topmost) + (br_if $topmost + (local.get $i1) ) ) ) @@ -118,8 +118,8 @@ (drop (i32.const 0) ) - (block - (br $topmost) + (br_if $topmost + (local.get $i1) ) ) ) @@ -157,6 +157,7 @@ ) ) (func $b13 (result i32) + (local $x i32) (block $topmost (result i32) (if (i32.const 1) @@ -166,10 +167,9 @@ (i32.const 12) ) (drop - (block - (br $topmost - (i32.const 1) - ) + (br_if $topmost + (local.get $x) + (local.get $x) ) ) ) @@ -225,9 +225,10 @@ ) ) (func $b15 + (local $x i32) (block $topmost - (block - (br $topmost) + (br_if $topmost + (local.get $x) ) ) ) diff --git a/test/passes/remove-unused-brs_enable-multivalue.wast b/test/passes/remove-unused-brs_enable-multivalue.wast index 0deff0db881..eee8cd3c1ef 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.wast +++ b/test/passes/remove-unused-brs_enable-multivalue.wast @@ -59,7 +59,7 @@ (func $b6 (type $0) (param $i1 i32) (block $topmost (br_if $topmost - (i32.const 1) + (local.get $i1) ) ) ) @@ -70,7 +70,7 @@ (i32.const 0) ) (br_if $topmost - (i32.const 1) + (local.get $i1) ) ) ) @@ -79,7 +79,7 @@ (block $topmost (block $inner (br_if $topmost - (i32.const 1) + (local.get $i1) ) ) ) @@ -88,7 +88,7 @@ (block $topmost (block $inner (br_if $topmost - (i32.const 1) + (local.get $i1) ) ) ) @@ -101,7 +101,7 @@ (i32.const 0) ) (br_if $topmost - (i32.const 1) + (local.get $i1) ) ) ) @@ -115,7 +115,7 @@ (i32.const 0) ) (br_if $inner - (i32.const 1) + (local.get $i1) ) ) ) @@ -155,6 +155,7 @@ ) ) (func $b13 (type $2) (result i32) + (local $x i32) (block $topmost (result i32) (if (i32.const 1) @@ -165,8 +166,8 @@ ) (drop (br_if $topmost - (i32.const 1) - (i32.const 1) + (local.get $x) + (local.get $x) ) ) ) @@ -226,9 +227,10 @@ ) ) (func $b15 (type $1) + (local $x i32) (block $topmost (if - (i32.const 17) + (local.get $x) (then (br $topmost) ) From 1dcd61b13829d993989cc5eb3ce679e61ebb282e Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Wed, 18 Jun 2025 14:32:59 +0800 Subject: [PATCH 25/27] Update tests, keep minimal change --- .../remove-unused-brs_enable-multivalue.txt | 124 +++++++++++------- .../remove-unused-brs_enable-multivalue.wast | 54 ++++---- 2 files changed, 103 insertions(+), 75 deletions(-) diff --git a/test/passes/remove-unused-brs_enable-multivalue.txt b/test/passes/remove-unused-brs_enable-multivalue.txt index 1b5d9802ce6..789e582e6d3 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.txt +++ b/test/passes/remove-unused-brs_enable-multivalue.txt @@ -504,6 +504,7 @@ ) ) (func $loops + (local $x i32) (loop $in (block $out (br_if $in @@ -518,13 +519,15 @@ ) (loop (block $out0 - (block + (br_if $out0 + (local.get $x) ) ) ) (loop $in1 (block $out1 - (block + (br_if $out1 + (local.get $x) ) ) ) @@ -535,17 +538,24 @@ ) ) (loop $in4 - (block $out3 - (block + (if + (i32.eqz + (local.get $x) ) - (block - (br $in4) + (then + (block $out3 + (nop) + (br_if $in4 + (local.get $x) + ) + ) ) ) ) (loop $in5 (block $out4 - (block + (br_if $in5 + (local.get $x) ) ) ) @@ -756,17 +766,19 @@ ) (loop $in19 (block $out18 - (block + (drop + (local.get $x) ) (br $in19) ) ) (loop $in-not (block $out-not - (block - (br $out-not) + (br_if $out-not + (local.get $x) ) - (block + (br_if $out-not + (local.get $x) ) (call $loops) (br $in-not) @@ -824,16 +836,17 @@ ) ) (func $threading + (local $x i32) (drop (block $value-out (result i32) (block $value-in (result i32) (block $out (block $in - (block - (br $out) + (br_if $out + (local.get $x) ) - (block - (br $out) + (br_if $out + (local.get $x) ) (br $value-in (i32.const 3) @@ -848,8 +861,8 @@ (block $stack2 (block $stack3 (block $stack4 - (block - (br $stack1) + (br_if $stack1 + (local.get $x) ) (unreachable) ) @@ -874,7 +887,9 @@ ) ) (else - (br $leave) + (br_if $leave + (local.get $x) + ) ) ) (unreachable) @@ -893,7 +908,9 @@ (if (local.get $x) (then - (br $leave) + (br_if $leave + (local.get $x) + ) ) (else (br $out @@ -980,18 +997,26 @@ ) ) (func $iffify - (block $yes - (block - ) - (drop - (i32.const 1) + (local $x i32) + (if + (i32.eqz + (local.get $x) ) - (drop - (i32.const 2) + (then + (block $yes + (nop) + (drop + (i32.const 1) + ) + (drop + (i32.const 2) + ) + ) ) ) (block $no - (block + (br_if $no + (local.get $x) ) (drop (i32.const 1) @@ -1002,7 +1027,8 @@ ) ) (block $no2 - (block + (br_if $no2 + (local.get $x) ) ) (block $no3 @@ -1016,7 +1042,8 @@ ) (block $no5 (block $no4 - (block + (br_if $no5 + (local.get $x) ) (drop (i32.const 1) @@ -1072,25 +1099,22 @@ (func $untaken-brs-might-prevent-block-removal (param $0 f32) (param $1 i32) (param $2 f32) (param $3 i32) (param $4 i32) (param $5 f64) (param $6 f32) (result i32) (block $label$0 (result i32) (block $label$1 - (block - (br $label$1) + (br_if $label$1 + (local.get $1) ) (br_if $label$1 (i32.load offset=3 align=1 (select (call $untaken-brs-might-prevent-block-removal (f32.const 1.4904844647389837e-07) - (block + (br_if $label$0 (i32.store16 offset=4 align=1 (i32.const 1900641) (br $label$0 (i32.const 1628075109) ) ) - (drop - (i32.const 1764950569) - ) - (unreachable) + (local.get $1) ) (f32.const 1.1910939690100655e-32) (i32.const 1628057906) @@ -1905,8 +1929,10 @@ ) ) (func $same-target-br_if-and-br + (local $x i32) (block $x - (block + (drop + (local.get $x) ) (br $x) (unreachable) @@ -2292,9 +2318,10 @@ ) ) (func $if-block-br + (local $x i32) (block $label - (block - (br $label) + (br_if $label + (local.get $x) ) ) ) @@ -2511,6 +2538,7 @@ ) ) (func $refinalize-need-br-value (result i32) + (local $x i32) (loop $label$3 (result i32) (block $label$6 (result i32) (block $label$10 @@ -2518,7 +2546,8 @@ (block $label$503 (br_if $label$3 (block $label$530 (result i32) - (block + (br_if $label$503 + (local.get $x) ) (i32.const 0) ) @@ -2840,7 +2869,6 @@ ) (func $if_br_if (local $0 i32) - (local $1 i32) (block $label$1 (br_if $label$1 (select @@ -2873,15 +2901,11 @@ ) ) ) - (if - (i32.const 1026) - (then - (local.set $1 - (local.tee $0 - (i32.const -7) - ) - ) - (br $label$1) + (br_if $label$1 + (select + (i32.const 1026) + (i32.const 0) + (local.get $0) ) ) (i32.store diff --git a/test/passes/remove-unused-brs_enable-multivalue.wast b/test/passes/remove-unused-brs_enable-multivalue.wast index eee8cd3c1ef..55c276b8cb5 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.wast +++ b/test/passes/remove-unused-brs_enable-multivalue.wast @@ -540,6 +540,7 @@ ) ) (func $loops + (local $x i32) (loop $in (block $out (if (i32.const 0) (then (br $out))) @@ -551,13 +552,13 @@ ) (loop (block $out - (if (i32.const 0) (then (br $out))) + (if (local.get $x) (then (br $out))) (br $out) ) ) (loop $in (block $out - (if (i32.const 0) (then (br $out))) + (if (local.get $x) (then (br $out))) (br $out) ) ) @@ -567,13 +568,13 @@ ) (loop $in (block $out - (if (i32.const 0) (then (br $out))) - (br_if $in (i32.const 1)) + (if (local.get $x) (then (br $out))) + (br_if $in (local.get $x)) ) ) (loop $in (block $out - (if (i32.const 0) (then (br $in))) + (if (local.get $x) (then (br $in))) (br $out) ) ) @@ -757,14 +758,14 @@ ) (loop $in (block $out - (br_if $in (i32.const 0)) + (br_if $in (local.get $x)) (br $in) ) ) (loop $in-not ;; do NOT if-ify, the block can't be removed (block $out-not - (br_if $out-not (i32.const -1)) - (br_if $out-not (i32.const 0)) + (br_if $out-not (local.get $x)) + (br_if $out-not (local.get $x)) (call $loops) (br $in-not) ) @@ -792,17 +793,18 @@ ) ) (func $threading + (local $x i32) (drop (block $value-out (result i32) (block $value-in (result i32) (block $out (block $in - (if (i32.const 1) + (if (local.get $x) (then (br $in) ) ) - (br_if $in (i32.const 2)) + (br_if $in (local.get $x)) (br $value-in (i32.const 3)) ) (br $out) @@ -815,7 +817,7 @@ (block $stack2 (block $stack3 (block $stack4 - (if (i32.const 1) + (if (local.get $x) (then (br $stack4) ) @@ -844,7 +846,7 @@ ) ) (else - (br_if $leave (i32.const 1)) + (br_if $leave (local.get $x)) ) ) (unreachable) @@ -860,7 +862,7 @@ (if (local.get $x) (then - (br_if $leave (i32.const 1)) + (br_if $leave (local.get $x)) ) (else (br $out @@ -937,16 +939,17 @@ ) ) (func $iffify + (local $x i32) (block $yes (br_if $yes - (i32.const 0) + (local.get $x) ) (drop (i32.const 1)) (drop (i32.const 2)) ) (block $no (br_if $no - (i32.const 0) + (local.get $x) ) (drop (i32.const 1)) (br $no) @@ -954,7 +957,7 @@ ) (block $no2 (br_if $no2 - (i32.const 0) + (local.get $x) ) ) (block $no3 @@ -965,7 +968,7 @@ (block $no5 (block $no4 (br_if $no5 - (i32.const 0) + (local.get $x) ) (drop (i32.const 1)) (drop (i32.const 2)) @@ -1013,7 +1016,7 @@ (block $label$0 (result i32) (block $label$1 ;; this block has no taken brs, but we can't remove it without removing them first (br_if $label$1 - (i32.const 607395945) + (local.get $1) ) (br_if $label$1 (i32.load16_s offset=3 align=1 @@ -1027,7 +1030,7 @@ (i32.const 1628075109) ) ) - (i32.const 1764950569) + (local.get $1) ) (f32.const 1.1910939690100655e-32) (i32.const 1628057906) @@ -1617,9 +1620,10 @@ ) ) (func $same-target-br_if-and-br + (local $x i32) (block $x (br_if $x - (i32.const 0) + (local.get $x) ) (br $x) (unreachable) @@ -1945,9 +1949,10 @@ ) ) (func $if-block-br + (local $x i32) (block $label (if - (i32.const 1) + (local.get $x) (then (br $label) ) @@ -2162,6 +2167,7 @@ ) ) (func $refinalize-need-br-value (result i32) + (local $x i32) (loop $label$3 (result i32) (block $label$6 (result i32) (block $label$10 @@ -2170,7 +2176,7 @@ (br_if $label$3 (block $label$530 (result i32) (br_if $label$503 ;; while this br does not send a value - (i32.const 0) + (local.get $x) ) (i32.const 0) ) @@ -2570,9 +2576,7 @@ (i32.const 1026) (then (br_if $label$1 - (local.tee $0 ;; but here it is *not* ok - (i32.const -7) - ) + (local.get $0) ;; but here it is *not* ok ) ) ) From c036d3f955766dc138e1bf0273db3509a1e47e6d Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Wed, 18 Jun 2025 14:42:28 +0800 Subject: [PATCH 26/27] Amend remove-unused-brs_shrink-level=1.txt --- test/passes/remove-unused-brs_shrink-level=1.txt | 8 ++++++-- test/passes/remove-unused-brs_shrink-level=1.wast | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/test/passes/remove-unused-brs_shrink-level=1.txt b/test/passes/remove-unused-brs_shrink-level=1.txt index ca446f864bc..127f30508d7 100644 --- a/test/passes/remove-unused-brs_shrink-level=1.txt +++ b/test/passes/remove-unused-brs_shrink-level=1.txt @@ -150,17 +150,21 @@ ) ) (func $br-if-unreachable-pair + (local $x i32) (block $label$14 (br_if $label$14 (unreachable) ) - (block + (br_if $label$14 + (local.get $x) ) ) ) (func $br-if-unreachable-pair2 + (local $x i32) (block $label$14 - (block + (br_if $label$14 + (local.get $x) ) (br_if $label$14 (unreachable) diff --git a/test/passes/remove-unused-brs_shrink-level=1.wast b/test/passes/remove-unused-brs_shrink-level=1.wast index 4f56f0eed1f..00d29376b06 100644 --- a/test/passes/remove-unused-brs_shrink-level=1.wast +++ b/test/passes/remove-unused-brs_shrink-level=1.wast @@ -111,19 +111,21 @@ ) ) (func $br-if-unreachable-pair + (local $x i32) (block $label$14 (br_if $label$14 (unreachable) ) (br_if $label$14 - (i32.const 0) + (local.get $x) ) ) ) (func $br-if-unreachable-pair2 + (local $x i32) (block $label$14 (br_if $label$14 - (i32.const 0) + (local.get $x) ) (br_if $label$14 (unreachable) From 6cd99243a2b80cd89effa77c01e968c702930606 Mon Sep 17 00:00:00 2001 From: Ruiyang Xu Date: Sat, 21 Jun 2025 04:58:29 +0800 Subject: [PATCH 27/27] Update --- test/passes/remove-unused-brs_enable-multivalue.txt | 13 ++++++++----- .../passes/remove-unused-brs_enable-multivalue.wast | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/test/passes/remove-unused-brs_enable-multivalue.txt b/test/passes/remove-unused-brs_enable-multivalue.txt index 789e582e6d3..321b061e524 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.txt +++ b/test/passes/remove-unused-brs_enable-multivalue.txt @@ -2901,11 +2901,14 @@ ) ) ) - (br_if $label$1 - (select - (i32.const 1026) - (i32.const 0) - (local.get $0) + (if + (i32.const 1026) + (then + (br_if $label$1 + (local.tee $0 + (local.get $0) + ) + ) ) ) (i32.store diff --git a/test/passes/remove-unused-brs_enable-multivalue.wast b/test/passes/remove-unused-brs_enable-multivalue.wast index 55c276b8cb5..acbd3781ab9 100644 --- a/test/passes/remove-unused-brs_enable-multivalue.wast +++ b/test/passes/remove-unused-brs_enable-multivalue.wast @@ -2576,7 +2576,9 @@ (i32.const 1026) (then (br_if $label$1 - (local.get $0) ;; but here it is *not* ok + (local.tee $0 ;; but here it is *not* ok + (local.get $0) + ) ) ) )