Skip to content

PGO: Re-order blocks without side-effects #75733

@EgorBo

Description

@EgorBo

Consider the following example:

static void Main(string[] args)
{
    Console.WriteLine((int)'3');
    for (int i = 0; i < 100; i++)
    {
        foo('3');
        Thread.Sleep(16);
    }
}

[MethodImpl(MethodImplOptions.NoInlining)]
static ulong foo(int x)
{
    if (x == '1')
        return 1;
    if (x == '2')
        return 2;
    if (x == '3')
        return 3;
    return 0;
}

and run it with Dynamic PGO, codegen for foo:

; Assembly listing for method foo(int):ulong
; Tier-1 compilation
; optimized using profile data
; with Dynamic PGO: edge weights are valid, and fgCalledCount is 4

G_M55197_IG01:              ;; offset=0000H
						;; size=0 bbWeight=1    PerfScore 0.00
G_M55197_IG02:              ;; offset=0000H
       83F931               cmp      ecx, 49 ; '1'
       7413                 je       SHORT G_M55197_IG06
       83F932               cmp      ecx, 50 ; '2'
       7414                 je       SHORT G_M55197_IG08
       83F933               cmp      ecx, 51 ; '3'
       7506                 jne      SHORT G_M55197_IG04
       B803000000           mov      eax, 3
						;; size=20 bbWeight=1    PerfScore 4.00
G_M55197_IG03:              ;; offset=0014H
       C3                   ret      
						;; size=1 bbWeight=1    PerfScore 1.00
G_M55197_IG04:              ;; offset=0015H
       33C0                 xor      eax, eax
						;; size=2 bbWeight=0    PerfScore 0.00
G_M55197_IG05:              ;; offset=0017H
       C3                   ret      
						;; size=1 bbWeight=0    PerfScore 0.00
G_M55197_IG06:              ;; offset=0018H
       B801000000           mov      eax, 1
						;; size=5 bbWeight=0    PerfScore 0.00
G_M55197_IG07:              ;; offset=001DH
       C3                   ret      
						;; size=1 bbWeight=0    PerfScore 0.00
G_M55197_IG08:              ;; offset=001EH
       B802000000           mov      eax, 2
						;; size=5 bbWeight=0    PerfScore 0.00
G_M55197_IG09:              ;; offset=0023H
       C3                   ret      
						;; size=1 bbWeight=0    PerfScore 0.00

I'd expected it to re-order checks and make if (x == '3') first one

cc @AndyAyersMS @jakobbotsch

category:cq
theme:profile-feedback

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions