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

Improve compare-and-branch sequences produced by Emitter #111797

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

snickolls-arm
Copy link
Contributor

Introduces an overload of genJumpToThrowHlpBlk that allows you to pass a function that generates the branch code, before it creates an inline throw block. This allows us to choose compare-and-branch sequences such as cbz on ARM64 for checks added in the emitter, such as divide-by-zero.

Working towards #68028, I'll be looking into something similar for #42514 next using this helper I've added.

<==
cmp     w1, #0
beq     G_M17037_IG05
==>
cbz     w1, G_M17037_IG05

Introduces an overload of `genJumpToThrowHlpBlk` that allows you to pass
a function that generates the branch code, before it creates an inline
throw block. This allows us to choose compare-and-branch sequences such
as `cbz` on ARM64 for checks added in the emitter, such as divide-by-zero.
@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 Jan 24, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 24, 2025
@snickolls-arm
Copy link
Contributor Author

@a74nh @kunalspathak

Copy link
Contributor

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

Emit `cbz` instead of `cmp+b.ls` when checking bounds for an access to the
0 index of an array. It can only throw when `arrayLength == 0`.

Fixes dotnet#42514
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.

Added few comments.

BasicBlock* CodeGen::genGetThrowHelper(SpecialCodeKind codeKind)
{
bool useThrowHlpBlk = compiler->fgUseThrowHelperBlocks();
#if defined(UNIX_X86_ABI)
Copy link
Member

Choose a reason for hiding this comment

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

can we remove this s because it is mainly used for arm64. Perhaps move this code from codegencommon.cpp to codegenarm64.cpp?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'll do that.

// For ARM64 we only expect equality comparisons.
assert((cond == GenCondition::EQ) || (cond == GenCondition::NE));

if (compareImm == 0)
Copy link
Member

Choose a reason for hiding this comment

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

this method is just called with compareImm == 0. Are you planning to use it for other scenarios as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I would try and use this helper anywhere I have an immediate and want to compare-and-branch as it would handle the instruction selection generally.

Maybe if we used it here it might emit tbnz sometimes for 2^n? Otherwise it just simplifies that block.

if (comparand->IsIntegralConst(0))

Possibly another cbz case:
GetEmitter()->emitIns_R_I(INS_cmp, EA_4BYTE, data->GetRegNum(), 0);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-arm64 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