Skip to content

Regression: Unnecessary bounds check generated for Span/ReadOnlySpan #116073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
rameel opened this issue May 28, 2025 · 5 comments
Open

Regression: Unnecessary bounds check generated for Span/ReadOnlySpan #116073

rameel opened this issue May 28, 2025 · 5 comments
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone

Comments

@rameel
Copy link
Contributor

rameel commented May 28, 2025

The same unnecessary bounds check previously reported for string (issue #115793, fixed in PR #115980) is also present for Span and ReadOnlySpan

Repro: Godbolt

static bool Is(ReadOnlySpan<char> prefix, ReadOnlySpan<char> path)
{
    if ((uint)prefix.Length < (uint)path.Length)
        return (path[prefix.Length] == '/');

    return false;
}

trunk-20250528+ab3d27fd24:

       push     rbp
       mov      rbp, rsp
       cmp      esi, ecx
       jl       SHORT G_M18054_IG05
       xor      eax, eax
       pop      rbp
       ret      
G_M18054_IG05:  ;; offset=0x000C
       cmp      esi, ecx
       jae      SHORT G_M18054_IG07
       mov      eax, esi
       cmp      word  ptr [rdx+2*rax], 47
       sete     al
       movzx    rax, al
       pop      rbp
       ret      
G_M18054_IG07:  ;; offset=0x001F
       call     CORINFO_HELP_RNGCHKFAIL
       int3

coreclr 9.0.2+80aa709f5d:

       cmp      esi, ecx
       jb       SHORT G_M18054_IG05
       xor      eax, eax
       ret      
G_M18054_IG05:  ;; offset=0x0007
       mov      eax, esi
       cmp      word  ptr [rdx+2*rax], 47
       sete     al
       movzx    rax, al
       ret      
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 28, 2025
@github-actions github-actions bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label May 28, 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 self-assigned this May 28, 2025
@EgorBo EgorBo removed the untriaged New issue has not been triaged by the area owner label May 28, 2025
@EgorBo EgorBo added this to the 10.0.0 milestone May 28, 2025
@EgorBo
Copy link
Member

EgorBo commented May 28, 2025

Thanks for filing issues like this! Let me check, Spans are a bit special in JIT

@EgorBo
Copy link
Member

EgorBo commented May 28, 2025

Looks like it was regressed by this transformation:

// Normalize unsigned comparisons to signed if both operands a known to be never negative.
if (tree->IsUnsigned() && varTypeIsIntegral(op1) && op1->IsNeverNegative(this) &&
op2->IsNeverNegative(this))
{
tree->ClearUnsigned();
}

Unfortunately, there is no simple fix for now and if I remove this transformation it will be a quite noticeable size regressions in other places.

@EgorBo EgorBo modified the milestones: 10.0.0, Future May 28, 2025
@rameel
Copy link
Contributor Author

rameel commented May 30, 2025

Unfortunate. Is this the same reason in #115090 as well?

@EgorBo
Copy link
Member

EgorBo commented May 30, 2025

Unfortunate. Is this the same reason in #115090 as well?

That one is different, but also not trivial (we have a similar issue I believe, for span = span.Slice kinds of loops)

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

No branches or pull requests

2 participants