Skip to content

JIT: Fold unsigned constant range tests - #135421

Merged
EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:jit-fold-unsigned-range-tests
Oct 9, 2026
Merged

EgorBo merged 1 commit into
dotnet:mainfrom
EgorBo:jit-fold-unsigned-range-tests

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Closes #135418

Enable unsigned constant range folding in optOptimizeBools. For uint n:

bool Pattern() => n is >= '0' and <= '9';
-       mov      ecx, dword ptr [rcx+0x08]
-       cmp      ecx, 48
-       jae      SHORT G_M53568_IG05
-       xor      eax, eax
-       ret
-G_M53568_IG05:
-       cmp      ecx, 57
+       mov      eax, dword ptr [rcx+0x08]
+       sub      eax, 48
+       cmp      eax, 9
        setbe    al
        movzx    rax, al
        ret
-; Total bytes of code: 21
+; Total bytes of code: 16

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0a3719cb-02eb-4e5d-bce9-bffc4f4ee19c
@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 Oct 8, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 pipeline(s).
11 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.

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.

🟡 Changes recommended

The newly enabled unsigned transformation needs targeted regression coverage; builds and runtime tests were not run during this review.

1 open finding
What changed in this PR

Extends the JIT’s existing range folding to unsigned comparisons, addressing the digit-range optimization in #135418.

Changes:

  • Allows unsigned constant range tests.
  • Requires matching signedness for folding and preserves the signed-only nonnegative fallback.
File Description
src/​coreclr/​jit/​optimizebools.cpp Enables unsigned range folding with signedness guards.

🧠 Review effort: Balanced


Give feedback about Copilot approvals in this survey to enter a drawing for a $150 gift card.

Comment thread src/coreclr/jit/optimizebools.cpp
@EgorBo

EgorBo commented Oct 9, 2026 •

Copy link
Copy Markdown
Member Author

PTAL @adamperlin @dotnet/jit-contrib The diffs are quite small, but there is no reason to give up on Unsigned in that transformation (I remember I just conservatively gave up on them there). Fixes user's repro: #135418

@EgorBo
EgorBo requested a review from adamperlin October 9, 2026 00:36
@Corniel

Corniel commented Oct 9, 2026 •

Copy link
Copy Markdown

@EgorBo Note that in my example, I used an uint but also char. In both cases it was not applied. If understand correctly, the char case should already have worked?!

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

This LGTM. Do existing tests for range folding cover the unsigned case?

@adamperlin

Copy link
Copy Markdown
Contributor

@EgorBo Note that in my example, I used an uint but also char. In both cases it was not applied. If understand correctly, the char case should already have worked?!

The JIT actually represents char as ushort, so I believe it's consistent that the optimization wasn't being applied to the char case before!

@Corniel

Corniel commented Oct 9, 2026 •

Copy link
Copy Markdown

The JIT actually represents char as ushort, so I believe it's consistent that the optimization wasn't being applied to the char case before!

It makes a lot of sense, and explains why it did not work either way.

@EgorBo
EgorBo merged commit d1ce1ed into dotnet:main Oct 9, 2026
143 checks passed
@EgorBo
EgorBo deleted the jit-fold-unsigned-range-tests branch October 9, 2026 18:03
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 13, 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.

JIT: Why are ranche checks not optimized

4 participants