JIT bit test is not recognized for pattern matching syntax

Open
#107,801 2 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
compilers

Research direction

Start with the two C# functions in the issue and reproduce their generated assembly, comparing the pattern-matching form with the explicit comparisons. Trace how the JIT handles the pattern and bit-test opportunity; done means the pattern-matching form can produce equivalent bit-test code rather than the larger jump table.

Written by the indexing model from the issue text.

Description

area-CodeGen-coreclr help wanted in-pr

Reported in https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-9/?commentid=21177#comment-21177

The following two functions emit different codegen (suboptimal in case of Test1):

void Test1(char c)
{
    if (c is ' ' or '\t' or '\r' or '\n')
        Console.WriteLine("Hello, World!");
}


void Test2(char c)
{
    if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
        Console.WriteLine("Hello, World!");
}

Codegen:

; Assembly listing for method Program:Test1(ushort):this (FullOpts)
       movzx    rcx, dx
       lea      eax, [rcx-0x09]
       cmp      eax, 4
       ja       SHORT G_M39625_IG04
       mov      ecx, eax
       lea      rax, [reloc @RWD00]
       mov      eax, dword ptr [rax+4*rcx]
       lea      rdx, G_M39625_IG02
       add      rax, rdx
       jmp      rax
G_M39625_IG04:
       cmp      ecx, 32
       je       SHORT G_M39625_IG06
G_M39625_IG05:
       ret      
G_M39625_IG06:
       mov      rcx, 0x21100204D88      ; 'Hello, World!'
       tail.jmp [System.Console:WriteLine(System.String)]
RWD00  	dd	00000029h ; case G_M39625_IG06
       	dd	00000029h ; case G_M39625_IG06
       	dd	00000028h ; case G_M39625_IG05
       	dd	00000028h ; case G_M39625_IG05
       	dd	00000029h ; case G_M39625_IG06
; Total bytes of code 57


; Assembly listing for method Program:Test2(ushort):this (FullOpts)
       movzx    rcx, dx
       cmp      ecx, 32
       ja       SHORT G_M37482_IG04
       mov      eax, 0xFFFFD9FF
       bt       rax, rcx
       jae      SHORT G_M37482_IG05
G_M37482_IG04:
       ret      
G_M37482_IG05:
       mov      rcx, 0x21100204D88      ; 'Hello, World!'
       tail.jmp [System.Console:WriteLine(System.String)]
; Total bytes of code 36
Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.