Skip to content

JIT: Fold signed and LT/GE compares against checked bounds in AssertionProp - #138248

Draft
EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:ap-fold-bound-relop
Draft

EgorBo wants to merge 2 commits into
dotnet:mainfrom
EgorBo:ap-fold-bound-relop

Conversation

@EgorBo

@EgorBo EgorBo commented Nov 25, 2026

Copy link
Copy Markdown
Member

Relax the symbolic-bound GetRange check in Compiler::optAssertionPropGlobal_RelOp to cover all ordered signed/unsigned compares (GT_LE, GT_LT, GT_GE, GT_GT) against a never-negative checked bound, instead of restricting it to unsigned GT_GT/GT_LE.

For reverse loops over spans/arrays, loop cloning can't statically evaluate the non-constant initial value span.Length - 1 and emits a i >= span.Length guard between the fast and slow clones. Previously, AssertionProp didn't fold this signed GE guard, while RangeCheck later removed the bounds check from the slow clone too, leaving a live branch dispatching between two identical loops.

public static void IncrementReverse(Span<int> span) {
    for (int i = span.Length - 1; i >= 0; i--) {
        span[i]++;
    }
}
 ; Program:IncrementReverse(System.Span`1[int]) (FullOpts)
-; Total bytes of code 44, instruction count 20
+; Total bytes of code 21, instruction count 10
 G_M8695_IG02:
        mov      rax, bword ptr [rcx]
        mov      ecx, dword ptr [rcx+0x08]
-       lea      edx, [rcx-0x01]
-       test     edx, edx
-       jl       SHORT G_M8695_IG05
-G_M8695_IG03:
-       cmp      edx, ecx
-       jge      SHORT G_M8695_IG06
-       align    [0 bytes for IG04]
-G_M8695_IG04:
-       mov      ecx, edx
-       lea      rcx, bword ptr [rax+4*rcx]
-       inc      dword ptr [rcx]
-       dec      edx
-       jns      SHORT G_M8695_IG04
-G_M8695_IG05:
-       ret
-G_M8695_IG06:
-       mov      ecx, edx
-       lea      rcx, bword ptr [rax+4*rcx]
-       inc      dword ptr [rcx]
-       dec      edx
-       jns      SHORT G_M8695_IG06
-       jmp      SHORT G_M8695_IG05
+       dec      ecx
+       js       SHORT G_M8695_IG04
+       align    [0 bytes for IG03]
+G_M8695_IG03:
+       lea      rdx, bword ptr [rax+4*rcx]
+       inc      dword ptr [rdx]
+       dec      ecx
+       jns      SHORT G_M8695_IG03
+G_M8695_IG04:
+       ret

…unds in AssertionProp

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 0011b98e-8e2b-49f8-8261-844764239c0c
@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 Nov 25, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 5 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.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: e65c3f35-27f7-4371-a6ef-9672007337c5

This branch has not been deployed

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

1 participant