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

Cranelift/s390x: do not use one-way conditional branches. #10087

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

cfallin
Copy link
Member

@cfallin cfallin commented Jan 23, 2025

This is a followup to #10086, this time removing the one-armed branch variant for s390x. This branch was only used as the default-target branch in the br_table lowering. This PR incorporates the branch into the JTSequence pseudo-instruction. Some care is needed to keep the ProducesBool abstraction; it is unwrapped into its ProducesFlags and the JTSequence becomes a ConsumesFlags, so the compare for the jump-table bound (for default target) is not part of the pseudoinst. (This is OK because regalloc-inserted moves never alter flags, by explicit contract; the same reason allows cmp/branch terminators.)

Along the way I noticed that the comments on JTSequence claimed that targets included the default, but this is (no longer?) the case, as the targets are unwrapped by jump_table_targets which peels off the first (default) separately. Aside from comments, this only affected pretty-printing; codegen was correct.

With this, we have no more one-armed branches; hence, this fixes #9980.

@cfallin cfallin requested a review from a team as a code owner January 23, 2025 03:28
@cfallin cfallin requested review from abrown and removed request for a team January 23, 2025 03:28
This is a followup to bytecodealliance#10086, this time removing the one-armed branch
variant for s390x. This branch was only used as the default-target
branch in the `br_table` lowering. This PR incorporates the branch into
the `JTSequence` pseudo-instruction. Some care is needed to keep the
`ProducesBool` abstraction; it is unwrapped into its `ProducesFlags` and
the `JTSequence` becomes a `ConsumesFlags`, so the compare for the
jump-table bound (for default target) is not part of the pseudoinst.
(This is OK because regalloc-inserted moves never alter flags, by
explicit contract; the same reason allows cmp/branch terminators.)

Along the way I noticed that the comments on `JTSequence` claimed that
`targets` included the default, but this is (no longer?) the case, as
the targets are unwrapped by `jump_table_targets` which peels off the
first (default) separately. Aside from comments, this only affected
pretty-printing; codegen was correct.

With this, we have no more one-armed branches; hence, this fixes bytecodealliance#9980.
@cfallin cfallin force-pushed the s390x-one-armed-branch branch from 6225ef6 to c214eea Compare January 23, 2025 03:41
@github-actions github-actions bot added the cranelift Issues related to the Cranelift code generator label Jan 23, 2025
@cfallin
Copy link
Member Author

cfallin commented Jan 23, 2025

cc @uweigand if you are available to review? Otherwise we're probably OK with the usual reviewer rotation...

Copy link
Member

@uweigand uweigand left a comment

Choose a reason for hiding this comment

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

Looks generally good to me, but see inline for a couple of comments.

As a longer-term improvement, I'm wondering if the whole default case handling couldn't just be done by common code using regular basic blocks and branch instructions, enabling all the normal optimization machinery? The back-end would then only have to do the actual tablejump (simply assuming that no overflow can happen).

cranelift/codegen/src/isa/s390x/inst.isle Show resolved Hide resolved
; clgfi %r3, 3
; jghe label4
; sllg %r3, %r3, 2
Copy link
Member

Choose a reason for hiding this comment

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

The generated code is slightly worse now as the shifting is done before the branch. This probably doesn't have a very significant impact on the pipeline, but it does increase register pressure. One alternative would be to compare the shifted value instead (assuming there are no overflow considerations).

But I guess this isn't serious enough that it should prevent merging 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.

Hmm, that's true -- I did the code motion without looking at the instruction semantics in detail to optimize. We can optimize in a followup if needed I suppose.

@cfallin cfallin force-pushed the s390x-one-armed-branch branch from 9a932fe to bf8a9b6 Compare January 23, 2025 17:01
@cfallin
Copy link
Member Author

cfallin commented Jan 23, 2025

Could I bother someone for a rubber-stamp here (@uweigand's not an official approver) -- @alexcrichton or @fitzgen maybe? (Or @abrown as original reviewer?) Thanks!

@cfallin cfallin enabled auto-merge January 23, 2025 17:19
@cfallin cfallin added this pull request to the merge queue Jan 23, 2025
Merged via the queue into bytecodealliance:main with commit e4699f7 Jan 23, 2025
39 checks passed
@cfallin cfallin deleted the s390x-one-armed-branch branch January 23, 2025 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift Issues related to the Cranelift code generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Divergent behavior between regalloc_algorithm={backtracking,single_pass}
3 participants