Skip to content

JIT: Switch should create assertions #113992

Description

@EgorBo
static void Test(int x)
{
    switch (x)
    {
        case 1:
            Console.WriteLine(x == 1);
            break;
        case 2:
            Console.WriteLine(x == 2);
            break;
        case 3:
            Console.WriteLine(x == 3);
            break;
    }
}

Currently, JIT can't fold these x == y checks, but should be able to. This is a minimal repro for a popular case when Span/Array Length is used for switch and then all legs use that span.

For example, any attempt to remove unsafe code from the following function will hit regressions because of it:

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong GetKey(ReadOnlySpan<byte> name)
{
ref byte reference = ref MemoryMarshal.GetReference(name);
int length = name.Length;
ulong key = (ulong)(byte)length << 56;
switch (length)
{
case 0: goto ComputedKey;
case 1: goto OddLength;
case 2: key |= Unsafe.ReadUnaligned<ushort>(ref reference); goto ComputedKey;
case 3: key |= Unsafe.ReadUnaligned<ushort>(ref reference); goto OddLength;
case 4: key |= Unsafe.ReadUnaligned<uint>(ref reference); goto ComputedKey;
case 5: key |= Unsafe.ReadUnaligned<uint>(ref reference); goto OddLength;
case 6: key |= Unsafe.ReadUnaligned<uint>(ref reference) | (ulong)Unsafe.ReadUnaligned<ushort>(ref Unsafe.Add(ref reference, 4)) << 32; goto ComputedKey;
case 7: key |= Unsafe.ReadUnaligned<uint>(ref reference) | (ulong)Unsafe.ReadUnaligned<ushort>(ref Unsafe.Add(ref reference, 4)) << 32; goto OddLength;
default: key |= Unsafe.ReadUnaligned<ulong>(ref reference) & 0x00ffffffffffffffL; goto ComputedKey;

Activity

  1. ghost added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Mar 28, 2025
  2. dotnet-policy-service commented on Mar 28, 2025

    @dotnet-policy-service
    Contributor

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

  3. self-assigned this
    on Mar 28, 2025
  4. removed
    untriagedNew issue has not been triaged by the area owner
    on Mar 28, 2025
  5. added this to the Future milestone on Mar 28, 2025
  6. added
    in-prThere is an active PR which will close this issue when it is merged
    on Mar 28, 2025
  7. modified the milestones: Future, 10.0.0 on Mar 31, 2025
  8. locked and limited conversation to collaborators on May 1, 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-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 mergedreduce-unsafe

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions