dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.22k stars 4.72k forks source link

JIT: Bound check elimination regression #109365

Open hez2010 opened 3 hours ago

hez2010 commented 3 hours ago

Description

Repro:

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading;

class C
{
    private readonly static int[] _perm = [1,2,3,4];
    [MethodImpl(MethodImplOptions.NoInlining)]
    public int Hash(int value)
    {
        return _perm[value & (_perm.Length - 1)];
    }

    static void Main()
    {
        var c = new C();
        for (var i = 0; i < 1000; i++) // triggers tier-1
        {
            c.Hash(i);
            Thread.Sleep(1);
        }
    }
}

On .NET 9:

G_M000_IG01:                ;; offset=0x0000

G_M000_IG02:                ;; offset=0x0000
       mov      rax, 0x2170C000150
       mov      rax, gword ptr [rax]
       and      edx, 3
       mov      ecx, edx
       mov      eax, dword ptr [rax+4*rcx+0x10]

G_M000_IG03:                ;; offset=0x0016
       ret

On main:

G_M45462_IG01:  ;; offset=0x0000
       push     rax
                        ;; size=1 bbWeight=1 PerfScore 1.00
G_M45462_IG02:  ;; offset=0x0001
       mov      rax, 0x727ECC000180      ; const ptr
       mov      rax, gword ptr [rax]
       and      esi, 3
       cmp      esi, 4
       jae      SHORT G_M45462_IG04
       mov      ecx, esi
       mov      eax, dword ptr [rax+4*rcx+0x10]
                        ;; size=27 bbWeight=1 PerfScore 6.00
G_M45462_IG03:  ;; offset=0x001C
       add      rsp, 8
       ret      
                        ;; size=5 bbWeight=1 PerfScore 1.25
G_M45462_IG04:  ;; offset=0x0021
       call     CORINFO_HELP_RNGCHKFAIL
       int3     

Configuration

.NET 9, .NET 10 nightly

Regression?

Yes

dotnet-policy-service[bot] commented 3 hours ago

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