-
Couldn't load subscription status.
- Fork 5.2k
Elide redundant test instruction for signed comparison predicates #118806
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
Conversation
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.
|
@jakobbotsch @xtqqczze Sorry about the noise, but here's the updated PR after incorporating xtqqczze's changes. |
|
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch |
|
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. |
|
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 Reference: instrsxarch.h#L76 |
But |
Ah yes, this would only match:
|
|
We can still optimise some cases, like the following, at least: void M(int x)
{
if ((x & (x - 1)) <= 0) // blsi
throw null!;
} |
|
@tannergooding, please review this PR. |
|
@ashay Pipeline build has been deleted, please update with merge commit to restart CI. |
There was a problem hiding this 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
AreFlagsSetToZeroCmpfunction 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 |
|
Thanks @xtqqczze, I merged the main branch just now. |
|
Diffs are not an improvement. |
|
This PR does not seem fruitful; the issue (#117866) it was intended to address was closed as invalid. |
|
Closing this PR since the issue associated with this change was marked as invalid. |
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.