-
Notifications
You must be signed in to change notification settings - Fork 786
RemoveUnusedBrs: optimize unreachable control flow mixed with side-effecting branches #7639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8b5a617
b9db9cf
223bfc0
1f44ad6
1feb9ca
df10dce
bae9749
6eafa8c
3dacea0
e41f344
abbe4ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -24,6 +24,8 @@ | |||||||||||||||||
#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" | ||||||||||||||||||
#include "pass.h" | ||||||||||||||||||
|
@@ -1881,6 +1883,42 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { | |||||||||||||||||
start = end; | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
void visitBreak(Break* curr) { | ||||||||||||||||||
if (!curr->condition) { | ||||||||||||||||||
return; | ||||||||||||||||||
} | ||||||||||||||||||
auto* value = Properties::getFallthrough( | ||||||||||||||||||
curr->condition, passOptions, *getModule()); | ||||||||||||||||||
// optimize if condition's fallthrough is a constant | ||||||||||||||||||
if (auto* c = value->dynCast<Const>()) { | ||||||||||||||||||
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; | ||||||||||||||||||
curr->type = Type::unreachable; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because of this line As for the case found by the fuzzer, it transforms code from block ;; label = @1
global.get 0
if ;; label = @2
i32.const 1
br_if 1 (;@1;)
else
unreachable
end
local.get 0
call 0
end to block ;; label = @1
global.get 0
if ;; label = @2
br 1 (;@1;)
else
unreachable
end
local.get 0
call 0
end which in binaryen/src/passes/SimplifyLocals.cpp Lines 659 to 666 in 700fa15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this assertion always hold true? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. If an So we need to figure out how things got to that situation, and prevent it. Likely a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably the issue is that turning a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, and I've updated. And I've also fuzzing for about one hour; all is well. |
||||||||||||||||||
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 | ||||||||||||||||||
replaceCurrent(block); | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
} | ||||||||||||||||||
xuruiyang2002 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
}; | ||||||||||||||||||
FinalOptimizer finalOptimizer(getPassOptions()); | ||||||||||||||||||
finalOptimizer.setModule(getModule()); | ||||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.