Skip to content

JIT: Fold type checks with GDV #52370

@EgorBo

Description

@EgorBo
static A GetAObj() => new C();

[MethodImpl(MethodImplOptions.NoInlining)]
static int Test() => GetAObj().GetValue();


public class A
{
    public virtual int GetValue() => 1;
}

public sealed class C : A
{
    public override int GetValue() => 3;
}

Current codegen for Test (PGO, GDV):

G_M58954_IG01:    
       4883EC28             sub      rsp, 40
G_M58954_IG02:            
       48B9C084615BFE7F0000 mov      rcx, 0x7FFE5B6184C0
       E85DCB545F           call     CORINFO_HELP_NEWSFAST
       488BC8               mov      rcx, rax
       48B8C084615BFE7F0000 mov      rax, 0x7FFE5B6184C0
       483901               cmp      qword ptr [rcx], rax ;; this check should be folded and fallback is removed
       750A                 jne      SHORT G_M58954_IG04
       B803000000           mov      eax, 3
G_M58954_IG03:      
       4883C428             add      rsp, 40
       C3                   ret      
G_M58954_IG04:         
       FF157B301700         call     [C:GetValue():int:this]
       EBF3                 jmp      SHORT G_M58954_IG03

After inlining we know the exact type of the GetAObj, so we should fold this:

***** BB01
STMT00009 (IL 0x000...  ???)
N005 ( 16, 16) [000029] -AC-----R---              *  ASG       ref   
N004 (  1,  1) [000028] D------N----              +--*  LCL_VAR   ref    V04 tmp4         d:1
N003 ( 16, 16) [000027] --C---------              \--*  CALL help ref    HELPER.CORINFO_HELP_NEWSFAST
N002 (  2, 10) [000026] H----------- arg0 in rcx     \--*  CNS_INT(h) long   0x7ffe5b628668 method

***** BB01
STMT00001 (IL   ???...  ???)
N003 (  1,  3) [000004] -A------R---              *  ASG       ref   
N002 (  1,  1) [000003] D------N----              +--*  LCL_VAR   ref    V01 tmp1         d:1
N001 (  1,  1) [000032] ------------              \--*  LCL_VAR   ref    V04 tmp4         u:1

***** BB01
STMT00004 (IL   ???...  ???)
N005 (  8, 15) [000013] ---X--------              *  JTRUE     void  
N004 (  6, 13) [000012] J--X---N----              \--*  NE        int   
N002 (  3,  2) [000010] #--X--------                 +--*  IND       long  
N001 (  1,  1) [000009] ------------                 |  \--*  LCL_VAR   ref    V04 tmp4         u:1 (last use)
N003 (  2, 10) [000011] H-----------                 \--*  CNS_INT(h) long   0x7ffe5b628668 class

We assign the exact class (B) to tmp4 which is then copied to tmp1 and checked is B (which is always true).

/cc @AndyAyersMS

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions