Skip to content

Conversation

@ashay
Copy link

@ashay ashay commented Aug 16, 2025

Prior to this patch, when emitting code for x64, if an instruction
updated the flags register and if the following instruction was an
equals or not-equals comparison with zero, the runtime elided the test
instruction for materializing the comparison.

Although correct, this is limiting, since it excludes the predicates
SGT, SGE, SLT, and SLE. This patch extends the optimization to allow the
omission of the test instruction for all signed integer comparisons.
We skip unsigned integer comparisons since unsigned comparisons with
zero can be evaluated earlier in the compilation process.

Prior to this patch, when emitting code for x64, if an instruction
updated the flags register and if the following instruction was an
equals or not-equals comparison with zero, the runtime elided the `test`
instruction for materializing the comparison.

Although correct, this is limiting, since it excludes the predicates
SGT, SGE, SLT, and SLE. This patch extends the optimization to allow the
omission of the `test` instruction for all signed integer comparisons.
We skip unsigned integer comparisons since unsigned comparisons with
zero can be evaluated earlier in the compilation process.
@github-actions github-actions bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Aug 16, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 16, 2025
@ashay
Copy link
Author

ashay commented Aug 16, 2025

@jakobbotsch @xtqqczze Sorry about the noise, but here's the updated PR after incorporating xtqqczze's changes.

@jkotas jkotas added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Aug 16, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@MichalPetryka
Copy link
Contributor

No diffs?

@ashay
Copy link
Author

ashay commented Aug 16, 2025

No diffs?

Huh.. I guess we never hit the case in any of the tests. I'm not sure what the project policy is, but if this patch isn't useful, I'm happy to close the PR.

@xtqqczze
Copy link
Contributor

No diffs?

No diffs appear because the current change has a redundant check for whether the carry flag is reset. This does not address #117866, since instructions such as add write the carry flag.

Reference: instrsxarch.h#L76

@ashay
Copy link
Author

ashay commented Aug 16, 2025

No diffs appear because the current change has a redundant check for whether the carry flag is reset.

But add doesn't reset OF either (it writes to it). So if the check were to be reduced to DoesResetOverflowFlag(), it still wouldn't fire for the add instruction, would it?

@xtqqczze
Copy link
Contributor

No diffs appear because the current change has a redundant check for whether the carry flag is reset.

But add doesn't reset OF either (it writes to it). So if the check were to be reduced to DoesResetOverflowFlag(), it still wouldn't fire for the add instruction, would it?

Ah yes, this would only match:

  • blsi | Resets_OF | Writes_SF | Writes_ZF | Undefined_AF | Undefined_PF | Writes_CF
  • blsmsk | Resets_OF | Writes_SF | Resets_ZF | Undefined_AF | Undefined_PF | Writes_CF
  • blsr | Resets_OF | Writes_SF | Writes_ZF | Undefined_AF | Undefined_PF | Writes_CF
  • bzhi | Resets_OF | Writes_SF | Writes_ZF | Undefined_AF | Undefined_PF | Writes_CF

@xtqqczze
Copy link
Contributor

We can still optimise some cases, like the following, at least:

void M(int x)
{
    if ((x & (x - 1)) <= 0) // blsi
        throw null!;
}

sharplab

@JulieLeeMSFT
Copy link
Member

@tannergooding, please review this PR.

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 8, 2025

@ashay Pipeline build has been deleted, please update with merge commit to restart CI.

Copilot AI review requested due to automatic review settings September 8, 2025 17:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR extends an x64 code generation optimization to eliminate redundant test instructions for signed comparison predicates. Previously, the optimization only applied to equals/not-equals comparisons with zero, but now includes all signed integer comparisons (SGT, SGE, SLT, SLE).

  • Adds support for omitting test instructions when using signed comparison predicates
  • Introduces a new helper function to check if instructions reset the overflow flag
  • Extends the existing AreFlagsSetToZeroCmp function with additional condition checks

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/coreclr/jit/emitxarch.h Adds declaration for new DoesResetOverflowFlag helper function
src/coreclr/jit/emitxarch.cpp Implements DoesResetOverflowFlag and extends AreFlagsSetToZeroCmp with signed comparison optimizations

@ashay
Copy link
Author

ashay commented Sep 8, 2025

Thanks @xtqqczze, I merged the main branch just now.

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 9, 2025

Diffs are not an improvement.

@xtqqczze
Copy link
Contributor

xtqqczze commented Sep 9, 2025

This PR does not seem fruitful; the issue (#117866) it was intended to address was closed as invalid.

@JulieLeeMSFT
Copy link
Member

@ashay, please address the feedback.

This PR does not seem fruitful; the issue (#117866) it was intended to address was closed as invalid.

@JulieLeeMSFT JulieLeeMSFT added the needs-author-action An issue or pull request that requires more info or actions from the author. label Oct 6, 2025
@ashay
Copy link
Author

ashay commented Oct 7, 2025

Closing this PR since the issue associated with this change was marked as invalid.

@ashay ashay closed this Oct 7, 2025
@ashay ashay deleted the issue-117866 branch October 7, 2025 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member needs-author-action An issue or pull request that requires more info or actions from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants