Skip to content

Compute precise generic context after de-virtualization #38477

Closed
@jkotas

Description

@jkotas

Compile with optimizations on:

using System;
using System.Runtime.CompilerServices;

class G<T>
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public virtual bool M() => typeof(T) == typeof(string);
}

class Program
{
    static int Main() => new G<string>().M() ? 1 : -1;
}

Expected result (disassembly of Main method):

     mov      eax, 1
     ret

Actual result:

G_M24375_IG01:
       4883EC28             sub      rsp, 40
                                                ;; bbWeight=1    PerfScore 0.25
G_M24375_IG02:
       48B9F0A37960FD7F0000 mov      rcx, 0x7FFD6079A3F0
       E8CDF1905F           call     CORINFO_HELP_NEWSFAST
       488BC8               mov      rcx, rax
        // The call is devirtualized properly, but inlining fails due to imprecise generic context
        // Note that the inlining succeeds when M is changed to be non-virtual
       FF15F4672700         call     [G`1[__Canon][System.__Canon]:M():bool:this]
       85C0                 test     eax, eax
       750A                 jne      SHORT G_M24375_IG05
                                                ;; bbWeight=1    PerfScore 5.75
G_M24375_IG03:
       B8FFFFFFFF           mov      eax, -1
                                                ;; bbWeight=0.50 PerfScore 0.13
G_M24375_IG04:
       4883C428             add      rsp, 40
       C3                   ret
                                                ;; bbWeight=0.50 PerfScore 0.63
G_M24375_IG05:
       B801000000           mov      eax, 1
                                                ;; bbWeight=0.50 PerfScore 0.13
G_M24375_IG06:
       4883C428             add      rsp, 40
       C3                   ret
                                                ;; bbWeight=0.50 PerfScore 0.63

category:cq
theme:devirtualization
skill-level:expert
cost:medium

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