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

JIT: Bounds check not elided for inlined function with safe arguments on Windows #112019

Open
BoyBaykiller opened this issue Jan 30, 2025 · 4 comments
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI tenet-performance Performance related issue
Milestone

Comments

@BoyBaykiller
Copy link

JIT currently elides bounds check for for (int i = 0; i < arr.Length; i++) loops.
When ClearBase is inlined with the given function arguments it is equivalent to exactly that, however the bounds check is not removed when targeting windows. See: https://godbolt.org/z/PMx8oPrM9. With int[] the redundant check is gone.

static void Clear(Span<int> arr)
{
    ClearBase(arr, arr.Length);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
static void ClearBase(Span<int> arr, int len)
{
    for (int i = 0; i < len; i++)
    {
        arr[i] = 0;
    }
}
Program:Clear(System.Span`1[int]) (FullOpts):
       sub      rsp, 40
       mov      rax, bword ptr [rcx]
       mov      ecx, dword ptr [rcx+0x08]
       mov      edx, ecx
       xor      r8d, r8d
       test     ecx, ecx
       jle      SHORT G_M46351_IG04
       align    [13 bytes for IG03]
G_M46351_IG03:  ;; offset=0x0020
       cmp      r8d, edx
       jae      SHORT G_M46351_IG05
       xor      r10d, r10d
       mov      dword ptr [rax+4*r8], r10d
       inc      r8d
       cmp      r8d, ecx
       jl       SHORT G_M46351_IG03
G_M46351_IG04:  ;; offset=0x0034
       add      rsp, 40
       ret      
G_M46351_IG05:  ;; offset=0x0039
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
@BoyBaykiller BoyBaykiller added the tenet-performance Performance related issue label Jan 30, 2025
@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 30, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 30, 2025
Copy link
Contributor

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

@EgorBo
Copy link
Member

EgorBo commented Jan 30, 2025

Dup of #82946

@EgorBo
Copy link
Member

EgorBo commented Jan 31, 2025

Although, presumably, it should remove the bound checks after inlining even without Loop Clonning cc @dotnet/jit-contrib

@BoyBaykiller
Copy link
Author

Yes. And I want to reiterate the bounds check is already removed when --targetos linux but not --targetos windows

@JulieLeeMSFT JulieLeeMSFT removed the untriaged New issue has not been triaged by the area owner label Feb 3, 2025
@JulieLeeMSFT JulieLeeMSFT added this to the Future milestone Feb 3, 2025
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 tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests

4 participants