Skip to content

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

@MihaZupan

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);

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions