Skip to content

Fold Span.Slice(X) like bound checks#122040

Merged
EgorBo merged 4 commits intodotnet:mainfrom
EgorBo:fold-x-add-cns
Nov 29, 2025
Merged

Fold Span.Slice(X) like bound checks#122040
EgorBo merged 4 commits intodotnet:mainfrom
EgorBo:fold-x-add-cns

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Nov 28, 2025

Diffs.

    static Vector256<int> Test(Span<int> values)
    {
        if (values.Length < 16)
            return default;
        return Vector256.Create(values.Slice(8));
    }

Was:

; Method Program:Test(System.Span`1[int]):System.Runtime.Intrinsics.Vector256`1[int] (FullOpts)
       sub      rsp, 40
       mov      rax, bword ptr [rdx]
       mov      edx, dword ptr [rdx+0x08]
       cmp      edx, 16
       jl       SHORT G_M55981_IG04
       add      rax, 32
       add      edx, -8
       cmp      edx, 8
       jl       SHORT G_M55981_IG07
       vmovups  ymm0, ymmword ptr [rax]
       vmovups  ymmword ptr [rcx], ymm0
       jmp      SHORT G_M55981_IG05
G_M55981_IG04:
       vxorps   ymm0, ymm0, ymm0
       vmovups  ymmword ptr [rcx], ymm0
G_M55981_IG05:
       mov      rax, rcx
       vzeroupper 
       add      rsp, 40
       ret      
G_M55981_IG07:
       mov      ecx, 6
       call     [System.ThrowHelper:ThrowArgumentOutOfRangeException(int)]
       int3     
; Total bytes of code: 68

Now:

; Method Program:Test(System.Span`1[int]):System.Runtime.Intrinsics.Vector256`1[int] (FullOpts)
       mov      rax, bword ptr [rdx]
       mov      edx, dword ptr [rdx+0x08]
       cmp      edx, 16
       jl       SHORT G_M55981_IG04
       add      rax, 32
       vmovups  ymm0, ymmword ptr [rax]
       vmovups  ymmword ptr [rcx], ymm0
       jmp      SHORT G_M55981_IG05
G_M55981_IG04:
       vxorps   ymm0, ymm0, ymm0
       vmovups  ymmword ptr [rcx], ymm0
G_M55981_IG05:
       mov      rax, rcx
       vzeroupper 
       ret      
; Total bytes of code: 40

@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 28, 2025
@dotnet-policy-service
Copy link
Contributor

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

@EgorBo EgorBo marked this pull request as ready for review November 28, 2025 18:25
Copilot AI review requested due to automatic review settings November 28, 2025 18:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes bound checks for Span.Slice(X) operations by eliminating redundant checks when sufficient range information is available from assertions. The optimization reduces code size by approximately 41% in the example case (from 68 to 40 bytes).

  • Adds support for subtracting constant limits in range operations
  • Refactors range operations to use a generic template-based approach
  • Implements peeling of constant offsets from ADD operations in relational comparisons

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/coreclr/jit/rangecheck.h Introduces SubtractConstantLimit function and refactors Add, Subtract, and Multiply operations to use a generic ApplyRangeOp template, eliminating code duplication
src/coreclr/jit/assertionprop.cpp Adds logic to peel constant offsets from ADD operations in relational comparisons, enabling range-based optimization for expressions like X + CNS relop Y

@EgorBo
Copy link
Member Author

EgorBo commented Nov 28, 2025

PTAL @dotnet/jit-contrib some nice diffs.

Another way to handle x + CNS1 relop CNS2 is to fold it on the VN level to x + CNS1 - CNS2 relop 0, but then it might confuse assertions created for x + CNS1 VN (I'll experiment with such foldings separately).

@EgorBo EgorBo requested a review from a team November 28, 2025 18:40
@EgorBo EgorBo merged commit bae19aa into dotnet:main Nov 29, 2025
119 checks passed
@EgorBo EgorBo deleted the fold-x-add-cns branch November 29, 2025 17:02
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI reduce-unsafe

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

Comments