Skip to content

JIT: Use integral ranges to narrow comparisons with memory operands - #134075

Merged
jakobbotsch merged 2 commits into
dotnet:mainfrom
jakobbotsch:integral-range-compare-narrowing-standalone
Sep 24, 2026
Merged

jakobbotsch merged 2 commits into
dotnet:mainfrom
jakobbotsch:integral-range-compare-narrowing-standalone

Conversation

@jakobbotsch

Copy link
Copy Markdown
Member

Generalize comparison containment to constants and operands whose IntegralRange fits the small memory operand, without changing operand types or removing casts.

Share GetCompareSize between lowering, codegen, and x86 register allocation. Adjust unsigned conditions after containment using established operand ranges, and pass comparison sizes directly to flag-reuse checks.

Generalize comparison containment to constants and operands whose IntegralRange fits the small memory operand, without changing operand types or removing casts.

Share GetCompareSize between lowering, codegen, and x86 register allocation. Adjust unsigned conditions after containment using established operand ranges, and pass comparison sizes directly to flag-reuse checks.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c28b3c3-cf6c-4149-8eb1-964831492011
@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Sep 16, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@jakobbotsch
jakobbotsch marked this pull request as ready for review September 22, 2026 05:56
Copilot AI lite review requested due to automatic review settings September 22, 2026 05:56
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).
10 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread src/coreclr/jit/lowerxarch.cpp
@jakobbotsch

Copy link
Copy Markdown
Member Author

cc @dotnet/jit-contrib PTAL @dhartglassMSFT @EgorBo

Diffs. Mainly fixing a source of regressions I noticed in #134048.

The idea is that when we have a comparison done with a 1 or 2 byte load, then that load can be contained if we can do the comparison as a 1 or 2 byte comparison. We did that already when the other operand was small typed -- this PR expands it to allow the optimization in more cases via IntegralRange.

Some of the diffs have an unnecessary widening done before them. E.g.
image

It isn't completely trivial because casts may have contained operands so deleting them is not completely straightforward. I will leave it for a potential follow-up.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The unsigned rewrite can trigger a DEBUG assertion, and a focused regression test is still needed.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 High severity · 1 Medium severity

Open (2)
What changed in this PR

This pull request broadens x86/x64 JIT comparison containment using integral ranges and centralizes comparison-width calculation.

Changes:

  • Enables narrower memory comparisons when operand ranges permit.
  • Shares GetCompareSize() across lowering, register allocation, and code generation.
  • Updates unsigned-condition handling and x86 byte-register constraints.
File Summary
src/​coreclr/​jit/​lsrabuild.cpp Applies comparison width to x86 register constraints.
src/​coreclr/​jit/​lowerxarch.cpp Adds range-aware containment and condition handling.
src/​coreclr/​jit/​lower.cpp Removes prior comparison narrowing logic.
src/​coreclr/​jit/​gentree.h Declares the comparison-size helper.
src/​coreclr/​jit/​gentree.cpp Implements comparison-size calculation.
src/​coreclr/​jit/​codegenxarch.cpp Uses shared width for emission and flag reuse.
src/​coreclr/​jit/​codegen.h Updates the flag-reuse helper signature.

Comment thread src/coreclr/jit/lowerxarch.cpp Outdated
Comment thread src/coreclr/jit/lowerxarch.cpp Outdated
Avoid newly narrowing backend flag producers after their condition has moved to a consumer. Only set unsigned semantics on operators supported by SetUnsigned.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 4c28b3c3-cf6c-4149-8eb1-964831492011
Copilot AI review requested due to automatic review settings September 22, 2026 06:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Requested x86/x64 coverage and codegen, benchmark, and compile-time results remain outstanding.

Get a fresh assessment by requesting another Copilot review.

Review effort: Lite
Findings: 1 Medium severity · 1 Low severity

Open (2)
Resolved since last review (2)

Comment thread src/coreclr/jit/lowerxarch.cpp
Comment thread src/coreclr/jit/lowerxarch.cpp
@jakobbotsch

Copy link
Copy Markdown
Member Author

/azp run runtime, runtime-coreclr superpmi-diffs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).

@jakobbotsch
jakobbotsch merged commit 132c3cf into dotnet:main Sep 24, 2026
138 of 142 checks passed
@jakobbotsch
jakobbotsch deleted the integral-range-compare-narrowing-standalone branch September 24, 2026 09:32
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Sep 25, 2026
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants