-
Notifications
You must be signed in to change notification settings - Fork 892
Sink drop into arms of concrete if expressions in Vacuum pass #9155
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
Open
gkdn
wants to merge
1
commit into
WebAssembly:main
Choose a base branch
from
gkdn:vacuum-sink-drop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -331,17 +331,6 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> { | |
| curr->condition = | ||
| Builder(*getModule()).makeUnary(EqZInt32, curr->condition); | ||
| BranchHints::flip(curr, getFunction()); | ||
| } else if (curr->ifTrue->is<Drop>() && curr->ifFalse->is<Drop>()) { | ||
| // instead of dropping both sides, drop the if, if they are the same | ||
| // type | ||
| auto* left = curr->ifTrue->cast<Drop>()->value; | ||
| auto* right = curr->ifFalse->cast<Drop>()->value; | ||
| if (left->type == right->type) { | ||
| curr->ifTrue = left; | ||
| curr->ifFalse = right; | ||
| curr->finalize(); | ||
| replaceCurrent(Builder(*getModule()).makeDrop(curr)); | ||
| } | ||
| } | ||
| } else { | ||
| // This is an if without an else. If the body is empty, we do not need it. | ||
|
|
@@ -427,9 +416,11 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> { | |
| } | ||
| } | ||
| } | ||
| // sink a drop into an arm of an if-else if the other arm ends in an | ||
| // unreachable, as it if is a branch, this can make that branch optimizable | ||
| // and more vacuuming possible | ||
| // Sink a drop into an arm of an if-else if the other arm ends in an | ||
| // unreachable, or sink drops into both arms if both arms are concrete. | ||
| // This allows the if expression to become void, eliminates block type | ||
| // overhead, and enables further vacuuming/dead-code elimination in each | ||
| // arm. | ||
| auto* iff = curr->value->dynCast<If>(); | ||
| if (iff && iff->ifFalse && iff->type.isConcrete()) { | ||
| // reuse the drop in both cases | ||
|
|
@@ -445,6 +436,13 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum>> { | |
| iff->ifTrue = curr; | ||
| iff->type = Type::none; | ||
| replaceCurrent(iff); | ||
| } else if (iff->ifTrue->type.isConcrete() && | ||
| iff->ifFalse->type.isConcrete()) { | ||
| Builder builder(*getModule()); | ||
| iff->ifTrue = builder.dropIfConcretelyTyped(iff->ifTrue); | ||
| iff->ifFalse = builder.dropIfConcretelyTyped(iff->ifFalse); | ||
|
Member
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. These can just be |
||
| iff->type = Type::none; | ||
| replaceCurrent(iff); | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For MVP types this still saves one byte, so I'm not sure we want to remove this completely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per your other comment, I asked agent to prototype special casing MVP (re-hoist all isBasic()), it didn't make a meaningful difference.
Agent made following argument while prototyping but I didn't dig to verify claims:
Why MVP expressions survive Vacuum::optimizeDrop—and why hoisting hurts downstream passes
Because all MVP basic types are defaultable (Type::isDefaultable()), Vacuum::optimize and Vacuum::optimizeDrop already eliminate (drop X) for every pure expression, unary/binary arithmetic or comparison wrapper, non-trapping load, local.tee, and poppable block. Consequently, an MVP-typed arm can remain a Drop after Vacuum only when the arm is:
Call / CallRef / CallIndirect (11 instances in calcworker_wasm):
DeadArgumentElimination::visitDrop only records info->droppedCalls[call] when Drop directly wraps Call.
Re-hoisting drop out of (if cond (drop (call $f)) (drop (call $g))) into (drop (if (result i32) cond (call $f) (call $g))) couples $f's return type to $g's return type and hides (drop (call $f)) from dae-optimizing.
In calcworker_wasm, re-hoisting MVP Drops (Variant B1) prevented DAE from stripping the unused i32 return value of $725 (because $725 was paired in an if-else in $2119 with $841, whose i32 return value is used elsewhere), which also prevented eliminating a caller thunk ($2772).
Break (br_if carrying a value) or Block with value-carrying branches (5 instances in calcworker_wasm):
ProblemFinder::visitExpression and MergeBlocks::visitDrop require br_if and block to be directly wrapped in Drop to strip unused branch values from enclosing blocks.