Skip to content

[Bounds checks] Conditional Slice pattern #121682

Closed

Description

@EgorBo
void Test(Span<char> span, bool cond)
{
    if (span.Length >= 32)
    {
        if (cond)
        {
            span[0] = '-';
            span = span.Slice(1);
        }
        span[0] = 'x';
    }
}

Current codegen:

; Method Program:Test(System.Span`1[char],bool):this (FullOpts)
G_M52757_IG01:
       push     rbp
       mov      rbp, rsp
G_M52757_IG02:
       cmp      edx, 32
       jl       SHORT G_M52757_IG05
G_M52757_IG03:
       test     cl, cl
       je       SHORT G_M52757_IG04
       mov      word  ptr [rsi], 45
       add      rsi, 2
       dec      edx
G_M52757_IG04:
       test     edx, edx
       je       SHORT G_M52757_IG06
       mov      word  ptr [rsi], 120
G_M52757_IG05:
       pop      rbp
       ret      
G_M52757_IG06:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 41

Expected codegen:

No bounds checks.

Activity

  1. added
    untriaged
    on Nov 16, 2025
  2. self-assigned this
    on Nov 16, 2025
  3. added this to the 11.0.0 milestone on Nov 16, 2025
  4. added
    area-CodeGen-coreclr
    on Nov 16, 2025
  5. removed
    untriaged
    on Nov 16, 2025
  6. dotnet-policy-service commented on Nov 16, 2025

    @dotnet-policy-service
    Contributor

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

  7. locked and limited conversation to collaborators on Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrreduce-unsafe

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions