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

Fix unnecessary bounds check with ulong index #101352

Merged
merged 6 commits into from
May 1, 2024

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Apr 21, 2024

Closes #76198

string Test(ulong ulValue, string[] names)
{
    if (ulValue < (ulong)names.Length)
        return names[(uint)ulValue];
    return null;
}

Codegen diff:

; Method Prog:Test(ulong,System.String[]):System.String:this (FullOpts)
       sub      rsp, 40
       mov      eax, dword ptr [r8+0x08]
-      mov      ecx, eax
-      cmp      rcx, rdx
+      cmp      rax, rdx
       jbe      SHORT G_M5198_IG05
-      cmp      edx, eax
-      jae      SHORT G_M5198_IG07
       mov      eax, edx
       mov      rax, gword ptr [r8+8*rax+0x10]
       add      rsp, 40
       ret      
G_M5198_IG05:
       xor      rax, rax
       add      rsp, 40
       ret      
-G_M5198_IG07:
-      call     CORINFO_HELP_RNGCHKFAIL
-      int3     
-; Total bytes of code: 44
+; Total bytes of code: 32

@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 Apr 21, 2024
@EgorBo EgorBo marked this pull request as ready for review April 21, 2024 20:32
@EgorBo
Copy link
Member Author

EgorBo commented Apr 30, 2024

PTAL @AndyAyersMS @dotnet/jit-contrib

Very few diffs, but afair, @stephentoub had to use unsafe to workaround it.

the idea that (ulong)i < (ulong)arr.Length creates an assertion that i is less than arr.Length (unsigned) - basically, the same assertion that we generate for (uint)i < (uint)arr.Length today, I even re-used the same path

@EgorBo EgorBo requested a review from AndyAyersMS April 30, 2024 16:06
Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

Can you update the function header comment too?

@EgorBo
Copy link
Member Author

EgorBo commented May 1, 2024

Can you update the function header comment too?

Done

Failure is #101721

@EgorBo EgorBo merged commit 4326eb7 into dotnet:main May 1, 2024
105 of 107 checks passed
@EgorBo EgorBo deleted the fix-bound-checks-ulong-idx branch May 1, 2024 12:50
michaelgsharp pushed a commit to michaelgsharp/runtime that referenced this pull request May 9, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 1, 2024
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unnecessary bounds check with ulong index
2 participants