Skip to content

Commit 4056443

Browse files
authored
Remove 'none' type as a branch target in ReFinalize (#2492)
That was needed for super-old wasm type system, where we allowed (block $x (br_if $x (unreachable) (nop) ) ) That is, we differentiated "taken" branches from "named" ones (just referred to by name, but not actually taken as it's in unreachable code). We don't need to differentiate those any more. Remove the ReFinalize code that considered it, and also remove the named/taken distinction in other places.
1 parent a2f1a63 commit 4056443

13 files changed

Lines changed: 21 additions & 100 deletions

src/cfg/Relooper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ struct Optimizer : public RelooperRecursor {
804804
Outer = Builder.makeBlock(Curr);
805805
} else if (Outer->name.is()) {
806806
// Perhaps the name can be removed.
807-
if (!wasm::BranchUtils::BranchSeeker::hasNamed(Outer, Outer->name)) {
807+
if (!wasm::BranchUtils::BranchSeeker::has(Outer, Outer->name)) {
808808
Outer->name = wasm::Name();
809809
} else {
810810
Outer = Builder.makeBlock(Curr);

src/ir/ReFinalize.cpp

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,41 +44,12 @@ void ReFinalize::visitBlock(Block* curr) {
4444
curr->type = none;
4545
return;
4646
}
47-
auto old = curr->type;
4847
// do this quickly, without any validation
4948
// last element determines type
5049
curr->type = curr->list.back()->type;
5150
// if concrete, it doesn't matter if we have an unreachable child, and we
5251
// don't need to look at breaks
5352
if (curr->type.isConcrete()) {
54-
// make sure our branches make sense for us - we may have just made
55-
// ourselves concrete for a value flowing out, while branches did not send a
56-
// value. such branches could not have been actually taken before, that is,
57-
// there were in unreachable code, but we do still need to fix them up here.
58-
if (!old.isConcrete()) {
59-
auto iter = breakValues.find(curr->name);
60-
if (iter != breakValues.end()) {
61-
// there is a break to here
62-
auto type = iter->second;
63-
if (type == none) {
64-
// we need to fix this up. set the values to unreachables
65-
// note that we don't need to handle br_on_exn here, because its value
66-
// type is never none
67-
for (auto* br : FindAll<Break>(curr).list) {
68-
handleBranchForVisitBlock(br, curr->name, getModule());
69-
}
70-
for (auto* sw : FindAll<Switch>(curr).list) {
71-
handleBranchForVisitBlock(sw, curr->name, getModule());
72-
}
73-
// and we need to propagate that type out, re-walk
74-
ReFinalize fixer;
75-
fixer.setModule(getModule());
76-
Expression* temp = curr;
77-
fixer.walk(temp);
78-
assert(temp == curr);
79-
}
80-
}
81-
}
8253
return;
8354
}
8455
// otherwise, we have no final fallthrough element to determine the type,

src/ir/block-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ inline Expression*
3434
simplifyToContents(Block* block, T* parent, bool allowTypeChange = false) {
3535
auto& list = block->list;
3636
if (list.size() == 1 &&
37-
!BranchUtils::BranchSeeker::hasNamed(list[0], block->name)) {
37+
!BranchUtils::BranchSeeker::has(list[0], block->name)) {
3838
// just one element. try to replace the block
3939
auto* singleton = list[0];
4040
auto sideEffects =

src/ir/branch-utils.h

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,8 @@ inline std::set<Name> getBranchTargets(Expression* ast) {
151151
// Finds if there are branches targeting a name. Note that since names are
152152
// unique in our IR, we just need to look for the name, and do not need
153153
// to analyze scoping.
154-
// By default we consider all branches, so any place there is a branch that
155-
// names the target. You can unset 'named' to only note branches that appear
156-
// reachable (i.e., are not obviously unreachable).
157154
struct BranchSeeker : public PostWalker<BranchSeeker> {
158155
Name target;
159-
bool named = true;
160156

161157
Index found = 0;
162158
Type valueType;
@@ -176,31 +172,13 @@ struct BranchSeeker : public PostWalker<BranchSeeker> {
176172
}
177173

178174
void visitBreak(Break* curr) {
179-
if (!named) {
180-
// ignore an unreachable break
181-
if (curr->condition && curr->condition->type == unreachable) {
182-
return;
183-
}
184-
if (curr->value && curr->value->type == unreachable) {
185-
return;
186-
}
187-
}
188175
// check the break
189176
if (curr->name == target) {
190177
noteFound(curr->value);
191178
}
192179
}
193180

194181
void visitSwitch(Switch* curr) {
195-
if (!named) {
196-
// ignore an unreachable switch
197-
if (curr->condition->type == unreachable) {
198-
return;
199-
}
200-
if (curr->value && curr->value->type == unreachable) {
201-
return;
202-
}
203-
}
204182
// check the switch
205183
for (auto name : curr->targets) {
206184
if (name == target) {
@@ -213,39 +191,13 @@ struct BranchSeeker : public PostWalker<BranchSeeker> {
213191
}
214192

215193
void visitBrOnExn(BrOnExn* curr) {
216-
if (!named) {
217-
// ignore an unreachable br_on_exn
218-
if (curr->exnref->type == unreachable) {
219-
return;
220-
}
221-
}
222194
// check the br_on_exn
223195
if (curr->name == target) {
224196
noteFound(curr->sent);
225197
}
226198
}
227199

228-
static bool hasReachable(Expression* tree, Name target) {
229-
if (!target.is()) {
230-
return false;
231-
}
232-
BranchSeeker seeker(target);
233-
seeker.named = false;
234-
seeker.walk(tree);
235-
return seeker.found > 0;
236-
}
237-
238-
static Index countReachable(Expression* tree, Name target) {
239-
if (!target.is()) {
240-
return 0;
241-
}
242-
BranchSeeker seeker(target);
243-
seeker.named = false;
244-
seeker.walk(tree);
245-
return seeker.found;
246-
}
247-
248-
static bool hasNamed(Expression* tree, Name target) {
200+
static bool has(Expression* tree, Name target) {
249201
if (!target.is()) {
250202
return false;
251203
}
@@ -254,7 +206,7 @@ struct BranchSeeker : public PostWalker<BranchSeeker> {
254206
return seeker.found > 0;
255207
}
256208

257-
static Index countNamed(Expression* tree, Name target) {
209+
static Index count(Expression* tree, Name target) {
258210
if (!target.is()) {
259211
return 0;
260212
}

src/passes/DeadCodeElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ struct DeadCodeElimination
195195
reachableBreaks.erase(curr->name);
196196
}
197197
if (isUnreachable(curr->body) &&
198-
!BranchUtils::BranchSeeker::hasNamed(curr->body, curr->name)) {
198+
!BranchUtils::BranchSeeker::has(curr->body, curr->name)) {
199199
replaceCurrent(curr->body);
200200
return;
201201
}

src/passes/MergeBlocks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ optimizeBlock(Block* curr, Module* module, PassOptions& passOptions) {
287287
auto childName = childBlock->name;
288288
for (size_t j = 0; j < childSize; j++) {
289289
auto* item = childList[j];
290-
if (BranchUtils::BranchSeeker::hasNamed(item, childName)) {
290+
if (BranchUtils::BranchSeeker::has(item, childName)) {
291291
// We can't remove this from the child.
292292
keepStart = j;
293293
keepEnd = childSize;
@@ -300,7 +300,7 @@ optimizeBlock(Block* curr, Module* module, PassOptions& passOptions) {
300300
auto childName = loop->name;
301301
for (auto j = int(childSize - 1); j >= 0; j--) {
302302
auto* item = childList[j];
303-
if (BranchUtils::BranchSeeker::hasNamed(item, childName)) {
303+
if (BranchUtils::BranchSeeker::has(item, childName)) {
304304
// We can't remove this from the child.
305305
keepStart = 0;
306306
keepEnd = std::max(Index(j + 1), keepEnd);

src/passes/RemoveUnusedBrs.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
504504
// (b) this br_if is the only branch to that block (so the block
505505
// will vanish)
506506
if (brIf->name == block->name &&
507-
BranchUtils::BranchSeeker::countNamed(block, block->name) ==
508-
1) {
507+
BranchUtils::BranchSeeker::count(block, block->name) == 1) {
509508
// note that we could drop the last element here, it is a br we
510509
// know for sure is removable, but telling stealSlice to steal all
511510
// to the end is more efficient, it can just truncate.
@@ -566,16 +565,16 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
566565
worked = true;
567566
} else if (auto* iff = curr->list[0]->dynCast<If>()) {
568567
// The label can't be used in the condition.
569-
if (BranchUtils::BranchSeeker::countNamed(iff->condition,
570-
curr->name) == 0) {
568+
if (BranchUtils::BranchSeeker::count(iff->condition, curr->name) ==
569+
0) {
571570
// We can move the block into either arm, if there are no uses in
572571
// the other.
573572
Expression** target = nullptr;
574-
if (!iff->ifFalse || BranchUtils::BranchSeeker::countNamed(
573+
if (!iff->ifFalse || BranchUtils::BranchSeeker::count(
575574
iff->ifFalse, curr->name) == 0) {
576575
target = &iff->ifTrue;
577-
} else if (BranchUtils::BranchSeeker::countNamed(
578-
iff->ifTrue, curr->name) == 0) {
576+
} else if (BranchUtils::BranchSeeker::count(iff->ifTrue,
577+
curr->name) == 0) {
579578
target = &iff->ifFalse;
580579
}
581580
if (target) {
@@ -874,7 +873,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> {
874873
// can ignore.
875874
if (br && br->condition && br->name == curr->name &&
876875
br->type != unreachable) {
877-
if (BranchUtils::BranchSeeker::countNamed(curr, curr->name) == 1) {
876+
if (BranchUtils::BranchSeeker::count(curr, curr->name) == 1) {
878877
// no other breaks to that name, so we can do this
879878
if (!drop) {
880879
assert(!br->value);

src/passes/StackIR.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ class StackIROptimizer {
238238
continue;
239239
}
240240
if (auto* block = inst->origin->dynCast<Block>()) {
241-
if (!BranchUtils::BranchSeeker::hasNamed(block, block->name)) {
241+
if (!BranchUtils::BranchSeeker::has(block, block->name)) {
242242
// TODO optimize, maybe run remove-unused-names
243243
inst = nullptr;
244244
}

src/passes/Vacuum.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,6 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> {
364364
bool canPop = true;
365365
if (block->name.is()) {
366366
BranchUtils::BranchSeeker seeker(block->name);
367-
seeker.named = true;
368367
Expression* temp = block;
369368
seeker.walk(temp);
370369
if (seeker.found && seeker.valueType != none) {

src/tools/wasm-reduce.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ struct Reducer
517517
// replace a singleton
518518
auto& list = block->list;
519519
if (list.size() == 1 &&
520-
!BranchUtils::BranchSeeker::hasNamed(block, block->name)) {
520+
!BranchUtils::BranchSeeker::has(block, block->name)) {
521521
if (tryToReplaceCurrent(block->list[0])) {
522522
return;
523523
}
@@ -549,7 +549,7 @@ struct Reducer
549549
return; // nothing more to do
550550
} else if (auto* loop = curr->dynCast<Loop>()) {
551551
if (shouldTryToReduce() &&
552-
!BranchUtils::BranchSeeker::hasNamed(loop, loop->name)) {
552+
!BranchUtils::BranchSeeker::has(loop, loop->name)) {
553553
tryToReplaceCurrent(loop->body);
554554
}
555555
return; // nothing more to do

0 commit comments

Comments
 (0)