Skip to content

Bound checks for static readonly arrays #76578

@EgorBo

Description

@EgorBo

Noticed in @stephentoub's PR #76519 that for this case:

static readonly string[] s_singleDigitStringCache = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

static string UInt32ToDecStr(uint value)
{
    if (value < 10)
        return s_singleDigitStringCache[value];

    return null;
}

we don't elide bound checks for statically inited field:

; Assembly listing for method Program:UInt32ToDecStr(uint):System.String
; Tier-1 compilation
G_M44444_IG01:              
       4883EC28             sub      rsp, 40
G_M44444_IG02:              
       83F90A               cmp      ecx, 10
       731E                 jae      SHORT G_M44444_IG05
G_M44444_IG03:              
       48B8A01E80C89F010000 mov      rax, 0x19FC8801EA0
       488B00               mov      rax, gword ptr [rax]
       3B4808               cmp      ecx, dword ptr [rax+08H]
       7313                 jae      SHORT G_M44444_IG07
       8BD1                 mov      edx, ecx
       488B44D010           mov      rax, gword ptr [rax+8*rdx+10H]
G_M44444_IG04:              
       4883C428             add      rsp, 40
       C3                   ret      
G_M44444_IG05:              
       33C0                 xor      rax, rax
G_M44444_IG06:              
       4883C428             add      rsp, 40
       C3                   ret      
G_M44444_IG07:              
       E8CD4B0D5F           call     CORINFO_HELP_RNGCHKFAIL
       CC                   int3     
; Total bytes of code 52

I made a quick prototype EgorBo@70f911d - it works but it's blocked on API that is added in #76112 that is blocked on other things so I'm filing an issue to get back to it.

So, basically, we want to be able to fold str/array.Length to a constant for any static readonly field of Array or String type (doesn't matter if it's frozen or not)

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions