Skip to content

OptimizeInstructions: Do not remove an inner cast when there are descriptor effects in the way - #9067

Merged
kripken merged 4 commits into
WebAssembly:mainfrom
kripken:oi.desc
Sep 2, 2026
Merged

OptimizeInstructions: Do not remove an inner cast when there are descriptor effects in the way#9067
kripken merged 4 commits into
WebAssembly:mainfrom
kripken:oi.desc

Conversation

@kripken

@kripken kripken commented Sep 1, 2026

Copy link
Copy Markdown
Member

No description provided.

@kripken
kripken requested a review from tlively September 1, 2026 21:33
@kripken
kripken requested a review from a team as a code owner September 1, 2026 21:33
Comment thread src/passes/OptimizeInstructions.cpp Outdated
Comment on lines +2561 to +2564
ShallowEffectAnalyzer movingEffects(options, *getModule(), curr->ref);
if (movingEffects.orderedBefore(descEffects)) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't understand why this is correct. We're replacing the current ref argument (which is evaluated before the desc) with the child's ref argument (which is also evaluated before the desc). So what's being reordered?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The trap due to the inner cast. See the test, which is this:

(outer.cast
  (inner.cast (inner.ref))
  (descriptor with effects)
)

=>

(outer.cast
  (inner.ref)
  (descriptor with effects)
)

Now inner.cast is gone. Before, if it trapped, it trapped before the descriptor's effects. Now, the outer cast still traps - we didn't lose the trap - but it is after the descriptor.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see, it's not the expressions that are being reordered; it's the trap effect. And trapping is the only effect that matters here because we already know that curr->ref is another cast. It would be helpful to call that out slightly more explicitly in the comment.

Also, we should combine this new logic into the calculation of safe above. This is more than a nice refactoring because returning early here prevents later patterns from being evaluated.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, comment expanded and logic reordered.

@kripken
kripken merged commit 0f19a05 into WebAssembly:main Sep 2, 2026
16 checks passed
@kripken
kripken deleted the oi.desc branch September 2, 2026 15:29
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