Skip to content

Profiled casts: isinst is no longer hoisted #96837

@EgorBo

Description

@EgorBo

Since we expand a lot more casts now, we hit more cases where expanded (early) cast becomes non-hoistable, example:

[MethodImpl(MethodImplOptions.NoInlining)]
bool foo(object o)
{
    bool result = false;
    for (int i = 0; i < 100000; i++)
        result = o is IDisposable;  // o is "mostly" Program instance in my benchmark
    return result;
}

Codegen with DOTNET_JitProfileCasts=0:

G_M31828_IG01:
       push     rbx
       sub      rsp, 32
G_M31828_IG02:
       xor      ebx, ebx
       mov      rcx, 0x7FF80DB09C28      ; System.IDisposable
       call     CORINFO_HELP_ISINSTANCEOFINTERFACE
       test     rax, rax
       setne    al
       movzx    rax, al
       jmp      SHORT G_M31828_IG03
       align    [0 bytes for IG03]

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <loop_start>
G_M31828_IG03:
       mov      ecx, eax
       inc      ebx
       cmp      ebx, 0x186A0
       jl       SHORT G_M31828_IG03
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <loop_end>

G_M31828_IG04:
       mov      eax, ecx
G_M31828_IG05:
       add      rsp, 32
       pop      rbx
       ret

Codegen with DOTNET_JitProfileCasts=1:

G_M31828_IG01:
       push     rsi
       push     rbx
       sub      rsp, 40
       mov      rbx, rdx
G_M31828_IG02:
       xor      esi, esi

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <loop_start>
G_M31828_IG03:
       mov      rax, rbx
       test     rax, rax
       je       SHORT G_M31828_IG06
G_M31828_IG04:
       mov      rdx, 0x7FF80DDE0988      ; Program
       cmp      qword ptr [rax], rdx
       je       SHORT G_M31828_IG06
G_M31828_IG05:
       mov      rdx, rbx
       mov      rcx, 0x7FF80DAF9C28      ; System.IDisposable
       call     CORINFO_HELP_ISINSTANCEOFINTERFACE
G_M31828_IG06:
       test     rax, rax
       setne    al
       movzx    rax, al
       inc      esi
       cmp      esi, 0x186A0
       jl       SHORT G_M31828_IG03
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; <loop_end>

G_M31828_IG07:
       add      rsp, 40
       pop      rbx
       pop      rsi
       ret

Options:

  1. Don't instrument isinst if it's inisde a loop (backward branch) - the easiest and the most conservative option.
  2. Expand all casts in a late phase - we might miss some branch opts
  3. Extend loop clonning

castclass is fine, we don't hoist it due to side-effects anyway.

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

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions