Skip to content

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 13, 2025

Not entirely a proper fix for #112524, but still helps. Basically, if we have an assertion that "(i + CNS1)" is within bounds, then any "(i + CNS2)" is within the same bounds if CNS2 is in [0..CNS1] range.

static void Test(int[] arr, int i)
{
    arr[i + 3] = 0;
    arr[i + 2] = 0;
    arr[i + 1] = 0;
}

Was:

; Method Proga:Test(int[],int) (FullOpts)
       sub      rsp, 40
       lea      eax, [rdx+0x03]
       mov      r8d, dword ptr [rcx+0x08]
       cmp      eax, r8d
       jae      SHORT G_M8086_IG06
       xor      r10d, r10d
       mov      dword ptr [rcx+4*rax+0x10], r10d
       lea      eax, [rdx+0x02]
       cmp      eax, r8d
       jae      SHORT G_M8086_IG06
       mov      dword ptr [rcx+4*rax+0x10], r10d
       inc      edx
       cmp      edx, r8d
       jae      SHORT G_M8086_IG06
       mov      eax, edx
       mov      dword ptr [rcx+4*rax+0x10], r10d
       add      rsp, 40
       ret      
G_M8086_IG06:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 62

Now:

; Method Proga:Test(int[],int) (FullOpts)
       sub      rsp, 40
       lea      eax, [rdx+0x03]
       mov      r8d, dword ptr [rcx+0x08]
       cmp      eax, r8d
       jae      SHORT G_M8086_IG06
       xor      r8d, r8d
       mov      dword ptr [rcx+4*rax+0x10], r8d
       lea      eax, [rdx+0x02]
       mov      dword ptr [rcx+4*rax+0x10], r8d
       inc      edx
       mov      eax, edx
       mov      dword ptr [rcx+4*rax+0x10], r8d
       add      rsp, 40
       ret      
G_M8086_IG06:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 52

@ghost ghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 13, 2025
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 February 14, 2025 07:15
@Copilot Copilot AI review requested due to automatic review settings February 14, 2025 07:15
Copy link
Contributor

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

Copilot wasn't able to review any files in this pull request.

Files not reviewed (2)
  • src/coreclr/jit/assertionprop.cpp: Language not supported
  • src/coreclr/jit/valuenum.cpp: Language not supported

@EgorBo
Copy link
Member Author

EgorBo commented Feb 14, 2025

PTAL @jakobbotsch @AndyAyersMS @dotnet/jit-contrib Diffs - mostly just removed bounds checks.

// Order arg0 arg1 by numerical VN value.
if (arg0VN > arg1VN)
// Order arg0 arg1 by numerical VN value, but keep constant non-handle VNs on the right.
if ((arg0VN > arg1VN) || IsVNConstantNonHandle(arg0VN))
Copy link
Member Author

@EgorBo EgorBo Feb 14, 2025

Choose a reason for hiding this comment

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

Previous code that was swapping VN operands in a commutative binary func could move constants to the left side which is not desired. Not a correctness issue, but a bit annoying for downstream phases

Comment on lines +5472 to +5476
if ((currentIdx == assertedFuncApp.m_args[0]) && (currentOffset >= 0) &&
(assertedOffset >= currentOffset))
{
isRedundant = true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Is there anything to worry about with regards to overflow here? E.g. if i + cns1 does not overflow yet i + cns2 does

Copy link
Member

Choose a reason for hiding this comment

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

Or, perhaps something like i = -5, cns1 = 5, cns2 = 4

Copy link
Member Author

Choose a reason for hiding this comment

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

@jakobbotsch ah, we need to also check the index for being non-negative (via assertions), the diffs are a lot smaller if I check that. I guess #112595 will handle this

@EgorBo EgorBo closed this Feb 17, 2025
@github-actions github-actions bot locked and limited conversation to collaborators Mar 20, 2025
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.

2 participants