Skip to content

JIT fails to treat expression as non-negative after range check #112765

Open

Description

@MihaZupan

https://godbolt.org/z/Wv5bv99bb

public static short Test1(ref short searchSpace, int searchSpaceLength)
{
    if (searchSpaceLength < Vector128<short>.Count)
        return 42;
    
    return Unsafe.Add(ref searchSpace, searchSpaceLength - Vector128<short>.Count);
}

public static short Test2(ref short searchSpace, int searchSpaceLength)
{
    if (searchSpaceLength < Vector128<short>.Count)
        return 42;
    
    return Unsafe.Add(ref searchSpace, (uint)searchSpaceLength - (nuint)Vector128<short>.Count);
}
-add      esi, -8
-movsxd   rax, esi
-movsx    rax, word  ptr [rdi+2*rax]
+mov      eax, esi
+movsx    rax, word  ptr [rdi+2*rax-0x10]

This occurs in practice in a bunch of places throughput our IndexOf-like logic, e.g.

ref short oneVectorAwayFromEnd = ref Unsafe.Add(ref searchSpace, length - Vector256<short>.Count);

ref short oneVectorAwayFromEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength - Vector256<short>.Count);

Activity

  1. added
    area-CodeGen-coreclr
    on Feb 20, 2025
  2. added
    untriaged
    on Feb 20, 2025
  3. dotnet-policy-service commented on Feb 20, 2025

    @dotnet-policy-service
    Contributor

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

  4. self-assigned this
    on Feb 20, 2025
  5. removed
    untriaged
    on Feb 20, 2025
  6. added this to the 10.0.0 milestone on Feb 20, 2025
  7. added
    in-pr
    on Feb 20, 2025
  8. modified the milestones: 10.0.0, 11.0.0 on Jul 18, 2025
  9. modified the milestones: 11.0.0, 12.0.0 on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrin-pr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions