Skip to content

feat(x64): fuse *_overflow + brif to jo/jb#13919

Open
playX18 wants to merge 2 commits into
bytecodealliance:mainfrom
playX18:ap/fuse-overflow
Open

feat(x64): fuse *_overflow + brif to jo/jb#13919
playX18 wants to merge 2 commits into
bytecodealliance:mainfrom
playX18:ap/fuse-overflow

Conversation

@playX18

@playX18 playX18 commented Jul 21, 2026

Copy link
Copy Markdown

Was discussed shortly (#cranelift > Overflow flag is not used for branches?)

Why?

At the moment Cranelift to check for overflow generates something like add + seto + test and then jumps on the result of test. While this works, it is not optimal lowering. Better lowering is add + jo which removes ~2uops and reduces register pressure as we do not need to store overflow value if it can be fused.

@playX18
playX18 requested a review from a team as a code owner July 21, 2026 12:22
@playX18
playX18 requested review from cfallin and removed request for a team July 21, 2026 12:22
@github-actions github-actions Bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen isle Related to the ISLE domain-specific language labels Jul 21, 2026
@github-actions

Copy link
Copy Markdown

Subscribe to Label Action

cc @cfallin, @fitzgen

Details This issue or pull request has been labeled: "cranelift", "cranelift:area:machinst", "cranelift:area:x64", "isle"

Thus the following users have been cc'd because of the following labels:

  • cfallin: isle
  • fitzgen: isle

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

}
}

/// Shared fuse-safety checks for matching an overflow-flag value.

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.

Unfortunately I don't think we'll want to take this as-is: it's too complex, in an area where we've already had some really subtle and difficult-to-reason-about bugs.

This set of conditions crosses several abstraction levels: it reasons about flag materialization, and especially about "another use of the same value", and then does code-motion if not. That kind of just-so optimization is both precarious (it's distributed invariant maintenance: the main lowering algorithm is responsible for putting defs before uses, but we're overriding that logic on the result of the overflow-checking op because we think it shouldn't matter to do it late in some blocks) and brittle (instruction order can suddenly influence things in unexpected and spooky-action-at-a-distance ways).

For that reason I think this "absorption" is an antipattern, a patch on top of the design that does not fit well with it, and we should instead try to use mechanisms we already have and/or come up with a more general framework that is less brittle and can be more fully integrated into the lowering algorithm.

Would you be willing to attend the next Cranelift meeting? I'd be happy to discuss further and we can try to come up with better approaches here...

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yes, I would like to join the next Cranelift meeting. How do I get an invite? :)

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.

cc @fitzgen for a calendar invite

@fitzgen

fitzgen commented Jul 22, 2026

Copy link
Copy Markdown
Member

I think the correct way to do this is to formalize a classic, sliding-window-style peephole pass for vcode with a new set of ISLE rules, rather than attempting to, but only half-succeeding to, force it into our existing lowering and rules. This new peephole pass could reuse our existing value use counts from lowering to determine whether the condition needs to be materialized because it is used in other places as well or not. And, fwiw, that issue talks a lot about automata and such, but we could certainly start with a very naive matching strategy and then improve it in tree.

Also cc #4124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift:area:x64 Issues related to x64 codegen cranelift Issues related to the Cranelift code generator isle Related to the ISLE domain-specific language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants