Skip to content

Cranelift: Fold branch-to-trap optimization into the egraphs pass#13688

Merged
cfallin merged 2 commits into
bytecodealliance:mainfrom
fitzgen:branch-to-trap-in-egraph-pass
Jun 18, 2026
Merged

Cranelift: Fold branch-to-trap optimization into the egraphs pass#13688
cfallin merged 2 commits into
bytecodealliance:mainfrom
fitzgen:branch-to-trap-in-egraph-pass

Conversation

@fitzgen

@fitzgen fitzgen commented Jun 18, 2026

Copy link
Copy Markdown
Member

The last thing that the legalization pass did was branch-to-trap optimization, where a conditional branch to a block that just unconditionally traps was replaced with a trapz or trapnz. This commit moves this optimization into the egraph pass via:

  • Making the branch-to-trap analysis compute its results for a given block on-demand (and memoize its results) so that traversal order doesn't matter.

  • Adding a SimplifySkeletonInst::ReplaceWithTwo variant to replace a skeleton instruction with two other instructions.

  • Adding two new simplify_skeleton ISLE rules that use results from the branch-to-trap analysis and the ReplaceWithTwo variant to perform the rewrite.

The legalizer and its filetest infrastructure is completely removed.

The last thing that the legalization pass did was branch-to-trap optimization,
where a conditional branch to a block that just unconditionally traps was
replaced with a `trapz` or `trapnz`. This commit moves this optimization into
the egraph pass via:

* Making the branch-to-trap analysis compute its results for a given block
  on-demand (and  memoize its results) so that traversal order doesn't matter.

* Adding a `SimplifySkeletonInst::ReplaceWithTwo` variant to replace a skeleton
  instruction with two other instructions.

* Adding two new `simplify_skeleton` ISLE rules that use results from the
  branch-to-trap analysis and the `ReplaceWithTwo` variant to perform the
  rewrite.

The legalizer and its filetest infrastructure is completely removed.
@fitzgen fitzgen requested review from a team as code owners June 18, 2026 15:25
@fitzgen fitzgen requested review from cfallin and removed request for a team June 18, 2026 15:25

@cfallin cfallin left a comment

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.

Looks great -- thanks!

@cfallin cfallin added this pull request to the merge queue Jun 18, 2026
Merged via the queue into bytecodealliance:main with commit e04ccb5 Jun 18, 2026
52 checks passed
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