Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Feb 2, 2026

Description

The CountDigits method uses ulong.Log2() which requires hardware intrinsics (Lzcnt on x64, ArmBase on ARM64) for the JIT to elide bounds checks on the array access. When run with jitstress_isas_nohwintrinsic, the bounds check remains, causing the test's X64-NOT: CORINFO_HELP_RNGCHKFAIL assertion to fail.

Changes

  • Added runtime guards around CountDigits test invocations to only execute when Lzcnt.X64.IsSupported || ArmBase.Arm64.IsSupported
  • Added required namespace imports for hardware intrinsic checks
// Requires HWIntrinsics to expand Log2.
if (Lzcnt.X64.IsSupported || ArmBase.Arm64.IsSupported)
{
    if (CountDigits(1) != 1)
        return 0;

    if (CountDigits(10000000000000000000UL) != 20)
        return 0;
}

Test now skips CountDigits validation when hardware intrinsics are unavailable, while still validating other bounds check elision scenarios.

Original prompt

This section details on the original issue you should resolve

<issue_title>Test Failure: JIT/opt/RangeChecks/ElidedBoundsChecks/ElidedBoundsChecks.cmd</issue_title>
<issue_description>Failed in: runtime-coreclr jitstress-isas-x86 20260131.2

Failed tests:

coreclr osx x64 Checked jitstress_isas_nohwintrinsic @ OSX.15.Amd64.Open
- JIT/opt/RangeChecks/ElidedBoundsChecks/ElidedBoundsChecks.cmd

Error message:

__tmp2_ElidedBoundsChecks.cs:32:14: error: X64-NOT: excluded string found in input
 // X64-NOT: CORINFO_HELP_RNGCHKFAIL
             ^
__jit_disasm.out:152:7: note: found here
 call CORINFO_HELP_RNGCHKFAIL
      ^~~~~~~~~~~~~~~~~~~~~~~

Input file: __jit_disasm.out
Check file: __tmp2_ElidedBoundsChecks.cs

-dump-input=help explains the following input dump.

Input was:
<<<<<<
        .
        .
        .
      112: ; 
      113: ; Lcl frame size = 0 
      114: ; BEGIN METHOD ElidedBoundsChecks:CountDigits(ulong):nint 
      115:  
      116: G_M15887_IG01: 
      117:  push rbp 
      118:  mov rbp, rsp 
      119:  ;; size=4 bbWeight=1 PerfScore 1.25 
      120: G_M15887_IG02: 
      121:  or rdi, 1 
      122:  mov rax, rdi 
      123:  shr rax, 32 
      124:  test eax, eax 
      125:  je SHORT G_M15887_IG04 
      126:  ;; size=15 bbWeight=1 PerfScore 2.25 
      127: G_M15887_IG03: 
      128:  mov edi, eax 
      129:  or edi, 1 
      130:  mov rax, 0xD1FFAB1E ; code for System.Numerics.BitOperations:Log2SoftwareFallback(uint):int 
      131:  call [rax]System.Numerics.BitOperations:Log2SoftwareFallback(uint):int 
      132:  add eax, 32 
      133:  jmp SHORT G_M15887_IG05 
      134:  ;; size=22 bbWeight=0.50 PerfScore 3.00 
      135: G_M15887_IG04: 
      136:  or edi, 1 
      137:  mov rax, 0xD1FFAB1E ; code for System.Numerics.BitOperations:Log2SoftwareFallback(uint):int 
      138:  call [rax]System.Numerics.BitOperations:Log2SoftwareFallback(uint):int 
      139:  ;; size=15 bbWeight=0.50 PerfScore 1.75 
      140: G_M15887_IG05: 
      141:  cmp eax, 64 
      142:  jae SHORT G_M15887_IG07 
      143:  mov eax, eax 
      144:  mov rcx, 0xD1FFAB1E ; static handle 
      145:  movzx rax, byte ptr [rcx+rax] 
      146:  ;; size=21 bbWeight=1 PerfScore 3.75 
      147: G_M15887_IG06: 
      148:  pop rbp 
      149:  ret  
      150:  ;; size=2 bbWeight=1 PerfScore 1.50 
      151: G_M15887_IG07: 
      152:  call CORINFO_HELP_RNGCHKFAIL 
not:32           !~~~~~~~~~~~~~~~~~~~~~~  error: no match expected
      153:  int3  
      154:  ;; size=6 bbWeight=0 PerfScore 0.00 
      155: ; END METHOD ElidedBoundsChecks:CountDigits(ulong):nint 
      156:  
      157: ; Total bytes of code 85, prolog size 4, PerfScore 13.50, instruction count 25, allocated bytes for code 85 (MethodHash=0a4dc1f0) for method ElidedBoundsChecks:CountDigits(ulong):nint (FullOpts) 
      158: ; ============================================================ 
      159:  
      160: ; Assembly listing for method ElidedBoundsChecks:AndByConst(int):byte (FullOpts) 
      161: ; Emitting BLENDED_CODE for generic X64 on Apple 
      162: ; FullOpts code 
      163: ; optimized code 
      164: ; rsp based frame 
      165: ; partially interruptible 
      166: ; No PGO data 
      167: ; 0 inlinees with PGO data; 0 single block inlinees; 1 inlinees without PGO data 
      168: ; Final local variable assignments 
      169: ; 
      170: ; V00 arg0 [V00,T00] ( 3, 3 ) int -> rdi single-def 
      171: ;* V01 loc0 [V01 ] ( 0, 0 ) struct (16) zero-ref ld-addr-op <System.ReadOnlySpan`1[byte]> 
      172: ;# V02 OutArgs [V02 ] ( 1, 1 ) struct ( 0) [rsp+0x00] do-not-enreg[XS] addr-exposed "OutgoingArgSpace" <Empty> 
      173: ;* V03 tmp1 [V03 ] ( 0, 0 ) struct (16) zero-ref ld-addr-op "NewObj constructor temp" <System.ReadOnlySpan`1[byte]> 
      174: ; V04 tmp2 [V04,T01] ( 2, 4 ) int -> rdi "Span.get_Item index" 
      175: ;* V05 tmp3 [V05 ] ( 0, 0 ) byref -> zero-ref single-def "field V01._reference (fldOffset=0x0)" P-INDEP 
      176: ;* V06 tmp4 [V06 ] ( 0, 0 ) int -> zero-ref single-def "field V01._length (fldOffset=0x8)" P-INDEP 
      177: ;* V07 tmp5 [V07 ] ( 0, 0 ) byref -> zero-ref single-def "field V03._reference (fldOffset=0x0)" P-INDEP 
      178: ;* V08 tmp6 [V08 ] ( 0, 0 ) int -> zero-ref single-def "field V03._length (fldOffset=0x8)" P-INDEP 
      179: ; 
      180: ; Lcl frame size = 0 
      181: ; BEGIN METHOD ElidedBoundsChecks:An

Stack trace:

   at Xunit.Assert.True(Nullable`1 condition, String userMessage) in /_/src/arcade/src/Microsoft.DotNet.XUnitAssert/src/BooleanAsserts.cs:line 141
   at Program.<<Main>$>g__TestExecutor373|0_374(StreamWriter tempLogSw, StreamWriter statsCsvSw, <>c__DisplayClass0_0&)
```</issue_description>

<agent_instructions>Fix the issue. ElidedBoundsChecks.CountDigits from src\tests\JIT\opt\RangeChecks\ElidedBoundsChecks.cs reli...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes dotnet/runtime#123868

<!-- START COPILOT CODING AGENT TIPS -->
---

✨ Let Copilot coding agent [set things up for you](https://github.com/dotnet/runtime/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.

…its tests

The CountDigits method relies on hardware intrinsics (specifically Lzcnt) to efficiently optimize bounds checks. When running with jitstress_isas_nohwintrinsic, these intrinsics are disabled, causing bounds checks to remain in the generated code and the test to fail.

This fix wraps the CountDigits test calls in a conditional check that only runs them when Lzcnt.X64.IsSupported or ArmBase.Arm64.IsSupported is true.

Co-authored-by: EgorBo <[email protected]>
Copilot AI changed the title [WIP] Fix test failure for ElidedBoundsChecks in JIT Fix ElidedBoundsChecks test failure under jitstress_isas_nohwintrinsic Feb 2, 2026
Copilot AI requested a review from EgorBo February 2, 2026 11:55
@EgorBo
Copy link
Member

EgorBo commented Feb 2, 2026

/azp list

@azure-pipelines

This comment was marked as resolved.

@EgorBo
Copy link
Member

EgorBo commented Feb 2, 2026

/azp run runtime-coreclr jitstress-isas-x86

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo EgorBo marked this pull request as ready for review February 2, 2026 12:01
Copilot AI review requested due to automatic review settings February 2, 2026 12:01
Copy link
Contributor

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.

Pull request overview

This PR fixes a test failure in ElidedBoundsChecks that occurs when running with jitstress_isas_nohwintrinsic. The CountDigits method relies on ulong.Log2() which requires Lzcnt (x64) or ArmBase (ARM64) hardware intrinsics for the JIT to elide bounds checks. Without these intrinsics, the implementation falls back to software methods that prevent the optimization.

Changes:

  • Added runtime guards to skip CountDigits test invocations when hardware intrinsics (Lzcnt.X64 or ArmBase.Arm64) are unavailable
  • Added required namespace imports (System.Runtime.Intrinsics.Arm and System.Runtime.Intrinsics.X86) to support the intrinsic checks

@EgorBo
Copy link
Member

EgorBo commented Feb 2, 2026

/azp run runtime-coreclr jitstress-isas-x86

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@EgorBo
Copy link
Member

EgorBo commented Feb 2, 2026

PTAL @dotnet/jit-contrib simple fix - outerloop pipeline ran this FILECHECK test with HWIntrinsics=0 - it led to int.Log2 to be expanded into a software fallback (not inlined) and JIT had no chance to perform a range analysis on that.

@EgorBo EgorBo requested a review from a team February 2, 2026 13:25
@EgorBo EgorBo merged commit 4f3118f into main Feb 2, 2026
78 checks passed
@jkotas jkotas deleted the copilot/fix-elided-bounds-checks-failure branch February 2, 2026 23:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants