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

Merged

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

@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.

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);

Copy link
Member

Choose a reason for hiding this comment

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

Do not want to rerun CI for this, but in your next patch, can you please add method summary docs for this method?

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.
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.

Very nice impressive diffs. Thanks for your contribution.

image

// 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.

Do not want to rerun CI for this, but in your next patch, can you please add method summary docs for this method?

@kunalspathak kunalspathak merged commit 1b23d6f into dotnet:main Feb 4, 2025
114 of 116 checks passed
@snickolls-arm snickolls-arm deleted the improve-emitter-compare-branch branch February 5, 2025 09:34
grendello added a commit to grendello/runtime that referenced this pull request Feb 5, 2025
* main:
  JIT: Set PGO data inconsistent when flow disappears in cast expansion (dotnet#112147)
  [H/3] Fix handling H3_NO_ERROR (dotnet#112125)
  Change some workflows using `pull_request` to use `pull_request_target` instead (dotnet#112161)
  Annotate ConfiguredCancelableAsyncEnumerable T with allows ref struct and update extensions (dotnet#111953)
  Delete copy of performance pipelines in previous location (dotnet#112113)
  Optimize BigInteger.Divide (dotnet#96895)
  Use current STJ in HostModel and remove unnecessary audit suppressions (dotnet#109852)
  JIT: Unify handling of InstParam argument during inlining (dotnet#112119)
  Remove unneeded DiagnosticSource content (dotnet#112116)
  Improve compare-and-branch sequences produced by Emitter (dotnet#111797)
  Jit: Conditional Escape Analysis and Cloning (dotnet#111473)
  Re-enable HKDF-SHA3 on Azure Linux
  Remove fstream usage from corehost (dotnet#111859)
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