Skip to content

Tier0 castclass pattern allocates #90965

@EgorBo

Description

@EgorBo

The following minimal repro is the reason why we regressed TE benchmarks in #90899

using System.Runtime.CompilerServices;

Test<MyStruct>(default);

[MethodImpl(MethodImplOptions.NoInlining)] 
static void Test<T>(T t) => ((IMyInterface)t).DoWork();

public interface IMyInterface
{
    void DoWork();
}

public struct MyStruct : IMyInterface
{
    int a;

    [MethodImpl(MethodImplOptions.NoInlining)]
    public void DoWork() {}
}

Tier0 codegen for Test:

; Assembly listing for method Program:<<Main>$>g__Test|0_0[MyStruct](MyStruct) (Tier0)
       push     rbp
       sub      rsp, 48
       lea      rbp, [rsp+0x30]
       xor      eax, eax
       mov      qword ptr [rbp-0x08], rax
       mov      qword ptr [rbp-0x10], rax
       mov      qword ptr [rbp+0x10], rcx
       lea      rdx, [rbp+0x10]
       mov      rcx, 0x7FFAFBFEE288      ; MyStruct
       call     CORINFO_HELP_BOX  ;;;; <------------------------- allocation!
       mov      gword ptr [rbp-0x08], rax
       mov      rdx, gword ptr [rbp-0x08]
       mov      rcx, 0x7FFAFBFEE0B8      ; IMyInterface
       call     [CORINFO_HELP_CHKCASTINTERFACE]
       mov      gword ptr [rbp-0x10], rax
       mov      rcx, gword ptr [rbp-0x10]
       mov      r11, 0x7FFAFB4A0248
       call     [r11]IMyInterface:DoWork():this
       nop      
       add      rsp, 48
       pop      rbp
       ret

Tier1 codegen for it:

; Assembly listing for method Program:<<Main>$>g__Test|0_0[MyStruct](MyStruct) (FullOpts)
       sub      rsp, 40
       mov      dword ptr [rsp+0x20], ecx
       lea      rcx, [rsp+0x20]
       call     [MyStruct:DoWork():this]
       nop      
       add      rsp, 40
       ret

Presumably, it needs:

  1. Don't share temps for boxed values in impImportAndPushBox (so locals can have exact class info)
  2. impOptimizeCastClassOrIsInst should not quit if opts are disabled
  3. impDevirtualizeCall should not quit if opts are disabled - and still should be able to optimize calls on unboxed entries

Metadata

Metadata

Assignees

Labels

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

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions