Skip to content
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

MergeBlocks: Optimize all dropped blocks #6984

Merged
merged 12 commits into from
Oct 4, 2024
Merged

Conversation

kripken
Copy link
Member

@kripken kripken commented Oct 3, 2024

Just call optimizeDroppedBlock from visitDrop to handle that.

Followup to #6982. This optimizes the new testcase added there. Some older
tests also improve, view without whitespace for a much smaller diff, as
the changes are just to remove some unneeded blocks.

@kripken kripken requested a review from aheejin October 3, 2024 16:11
@@ -434,6 +434,15 @@ struct MergeBlocks
optimizeBlock(curr, getModule(), getPassOptions(), branchInfo);
}

void visitDrop(Drop* curr) {
Copy link
Member

@aheejin aheejin Oct 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between the cases handled by this and this code in optimizeBlock?

if (auto* drop = list[i]->dynCast<Drop>()) {

Can't all cases be handled by this new visitDrop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code in optimizeBlock runs on the children of a block, and we do need to run that more than once, unlike visitDrop: Each time we make a signficant change to a block, it's possible that merging with children of the block is possible, so we call optimizeBlock on such occasions.

On the other hand, for things whose parent isn't a block, the new visitDrop is enough, since the parent can't be merged with it, so we can just do that once.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I was completely wrong here! You were right, we can remove this code. It looks like changes in the parent never lead to new improvements for the child, so we can just do the visitDrop part once.

I'll do some fuzzing to verify this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, actually that was wrong, it turns out. There is at least one case where we do end up improving a child based on the parents, which I added a testcase for now (after reverting the last commit). What seems to happen is that after we optimize the parent there, we then look at the child and see that it can be removed with its name. (This was not noticed in the test suite since we run --remove-unused-names.)

Logically I still think it makes sense that we only need the call from visitDrop, but that would require changes to the pass, and I'm not sure it's worth a large refactoring.

Copy link
Member

@aheejin aheejin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking!

@kripken kripken merged commit 0be8d5e into WebAssembly:main Oct 4, 2024
13 checks passed
@kripken kripken deleted the mb.gen.2 branch October 4, 2024 18:28
kripken added a commit that referenced this pull request Oct 7, 2024
When I refactored the optimizeDroppedBlock logic in #6982, I didn't move the
unreachability check with that code, which was wrong. When that function
was called from another place in #6984, the fuzzer found an issue.

Diff without whitespace is smaller. This reverts almost all the test updates
from #6984 - those changes were on blocks with unreachable children.
The change was safe on them, but in general removing a block value in the
presence of unreachable code is tricky, so it's best to avoid it.

The testcase is a little bizarre, but it's the one the fuzzer found and I can't
find a way to generate a better one (other than to reduce it, which I did).
kripken added a commit that referenced this pull request Oct 10, 2024
… too (#6994)

In #6984 we optimized dropped blocks even if they had unreachable code. In #6988
that part was reverted, and blocks with unreachable code were ignored once more.
However, I realized that the check was not actually for unreachable code, but for
having an unreachable child, so it would miss things like this:

(block
  (block
    ..
    (br $somewhere) ;; unreachable type, but no unreachable code
  )
)

But it is useful to merge such blocks: we don't need the inner block here.

To fix this, just run ReFinalize if we change anything, which will propagate
unreachability as needed. I think MergeBlocks was written before we had
that utility, so it didn't use it...

This is not only useful for itself but will unblock an EH optimization in a
later PR, that has code in this form. It also simplifies the code by removing
the hasUnreachableChild checks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants