Skip to content

Add FileCheck test for elided bounds checks#123585

Merged
EgorBo merged 4 commits intomainfrom
copilot/add-filecheck-test-bounds-checks
Jan 26, 2026
Merged

Add FileCheck test for elided bounds checks#123585
EgorBo merged 4 commits intomainfrom
copilot/add-filecheck-test-bounds-checks

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

Fixes #123583

Description

Adds a FileCheck test that verifies codegen doesn't contain bounds checks (CORINFO_HELP_RNGCHKFAIL) for patterns where the JIT should prove access is in-bounds.

Test Methods

  • ComplexBinaryOperators - Masking/shifting restricts index to 0-63 for 65-element span
  • LastCharCheck - String length comparison before indexed access
  • CountDigits - ulong.Log2() result (0-63) indexes 64-element span
  • AndByConst - Bitwise AND with constant
  • AndByLength - Bitwise AND with (span.Length - 1) pattern

Files Added

  • src/tests/JIT/opt/RangeChecks/ElidedBoundsChecks.cs - Test methods with X64-NOT and ARM64-NOT FileCheck annotations
  • src/tests/JIT/opt/RangeChecks/ElidedBoundsChecks.csproj - Project with HasDisasmCheck=true
[MethodImpl(MethodImplOptions.NoInlining)]
static byte AndByLength(int i)
{
    // X64-NOT: CORINFO_HELP_RNGCHKFAIL
    // ARM64-NOT: CORINFO_HELP_RNGCHKFAIL
    ReadOnlySpan<byte> span = new byte[] { 1, 2, 3, 4 };
    return span[i & (span.Length - 1)];
}
Original prompt

Add a test that uses FileCheck to varify that codegen doesn't contain bounds checks at all

the test should be located in src\tests\JIT\opt\RangeChecks folder

Check on x64 and arm64, here are the asm examples how bounds checks look like:

; x64:
call     CORINFO_HELP_RNGCHKFAIL
; arm64:
bl      CORINFO_HELP_RNGCHKFAIL

Use .cs and .csproj in C:\prj\runtime-main2\src\tests\JIT\opt\Subtract\ as a reference on how to create FileCheck tests (HasDisasmCheck)

Here are the functions I want to make sure they don't have bounds checks under optimizations:

public class ElidedBoundsChecks
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static int ComplexBinaryOperators(byte inData)
    {
        ReadOnlySpan<byte> base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="u8;
        return base64[((inData & 0x03) << 4) | ((inData & 0xf0) >> 4)];
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool LastCharCheck(string prefix, string path)
    {
        if (prefix.Length < path.Length)
            return (path[prefix.Length] == '/');
        return false;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static nint CountDigits(ulong value)
    {
        ReadOnlySpan<byte> log2ToPow10 =
        [
            1,  1,  1,  2,  2,  2,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,
            6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9,  10, 10, 10,
            10, 11, 11, 11, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 15, 15,
            15, 16, 16, 16, 16, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20
        ];
        return log2ToPow10[(int)ulong.Log2(value)];
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static byte AndByConst(int i)
    {
        ReadOnlySpan<byte> span = new byte[] { 1, 2, 3, 4 };
        return span[i & 2];
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static byte AndByLength(int i)
    {
        ReadOnlySpan<byte> span = new byte[] { 1, 2, 3, 4 };
        return span[i & (span.Length - 1)];
    }
}

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot AI changed the title [WIP] Add FileCheck test to verify absence of bounds checks Add FileCheck test for elided bounds checks Jan 24, 2026
Copilot AI requested a review from EgorBo January 24, 2026 23:35
@EgorBo EgorBo marked this pull request as ready for review January 25, 2026 11:18
Copilot AI review requested due to automatic review settings January 25, 2026 11:18
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

Adds a JIT FileCheck-based regression test to ensure bounds checks are fully elided (no CORINFO_HELP_RNGCHKFAIL) for several common “proven in-range” indexing patterns on x64 and arm64.

Changes:

  • Add new ElidedBoundsChecks test methods that exercise range-analysis patterns where the JIT should prove indices are in-bounds.
  • Add FileCheck X64-NOT / ARM64-NOT annotations to ensure no CORINFO_HELP_RNGCHKFAIL appears in generated code.
  • Add a dedicated test project with HasDisasmCheck=true and environment settings to force optimized JIT compilation.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/tests/JIT/opt/RangeChecks/ElidedBoundsChecks.cs Introduces FileCheck-annotated methods and an entrypoint that exercises them.
src/tests/JIT/opt/RangeChecks/ElidedBoundsChecks.csproj Enables disasm-based checking (HasDisasmCheck) and configures environment for stable optimized codegen.

@stephentoub stephentoub added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 26, 2026
@dotnet-policy-service
Copy link
Contributor

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

@EgorBo
Copy link
Member

EgorBo commented Jan 26, 2026

@dotnet/jit-contrib PTAL a FileCheck test. I just wanted to make sure none of my BCE related tests silently break these cases (will probably add various loop-related tests at some point)

@EgorBo EgorBo requested a review from a team January 26, 2026 13:04
@EgorBo EgorBo enabled auto-merge (squash) January 26, 2026 18:13
@EgorBo
Copy link
Member

EgorBo commented Jan 26, 2026

/ba-g deadletter

@EgorBo EgorBo merged commit 2353f6d into main Jan 26, 2026
75 of 89 checks passed
@EgorBo EgorBo deleted the copilot/add-filecheck-test-bounds-checks branch January 26, 2026 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

Fuzzlyn: incorrect RangeOps::And calculation

4 participants