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

Fix compare immediate and branch helper for 2^n #113523

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

snickolls-arm
Copy link
Contributor

The helper function should only generate tbz, when the immediate being compared is a power of 2 and the test is for inequality.

Also adds documentation for the helper function.

This function has only been used so far for internal checks generated in the emitter, so there are no diffs. I found this issue by using the function in some other work.

The helper function should only generate `tbz`, when the immediate being
compared is a power of 2 and the test is for inequality.

Also adds documentation for the helper function.
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Mar 14, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Mar 14, 2025
@snickolls-arm
Copy link
Contributor Author

@a74nh @kunalspathak

@@ -5159,12 +5176,11 @@ void CodeGen::genCompareImmAndJump(
instruction ins = (cond == GenCondition::EQ) ? INS_cbz : INS_cbnz;
GetEmitter()->emitIns_J_R(ins, size, target, reg);
}
else if (isPow2(compareImm))
else if (isPow2((size_t)compareImm) && (cond == GenCondition::NE))
Copy link
Member

Choose a reason for hiding this comment

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

What happens when isPow2(compareImm) && (cond == GenCondition::EQ)? In that case, we should be using tbnz, right?

I think the previous code was fine, except the conditions were reversed...If isPow2(), we should have:

instruction ins = (cond == GenCondition::EQ) ? INS_tbnz : INS_tbz;

Copy link
Member

Choose a reason for hiding this comment

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

I think this transformation is incorrect, it's never correct to change x != 2^i into (x & 2^i) == 0, or vice versa. I don't think we can use tbz or tbnz at all here. For example consider i = 0, x = 3. There we have x != 2^i but we do not have (x & 2^i) == 0.

Copy link
Member

Choose a reason for hiding this comment

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

We do have a check of compareImm == 0 before we check for isPow2(compareImm), if thats what you meant.

Copy link
Member

Choose a reason for hiding this comment

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

That transformation looks ok to me. This one doesn't.

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

This function has only been used so far for internal checks generated in the emitter, so there are no diffs

I did not understand this. We are using genCompareImmAndJump() inside genJumpToThrowHlpBlk(), no?

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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants