Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Methods are not getting inlined #11087

Closed
bugproof opened this issue Sep 13, 2018 · 7 comments
Closed

Methods are not getting inlined #11087

bugproof opened this issue Sep 13, 2018 · 7 comments

Comments

@bugproof
Copy link

Why DecimalExtensions:Round is not getting inlined? I followed instructions here https://github.com/dotnet/coreclr/blob/master/Documentation/building/viewing-jit-dumps.md to get the jit dump. I published the program in release. As you can see it still calls DecimalExtensions:Round instead of calling Decimal:Round directly.

using System;
using System.Runtime.CompilerServices;

public static class DecimalExtensions
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)] // doesn't help
    public static decimal Round(this decimal @this)
    {
        return decimal.Round(@this);
    }
}

public static class Program
{
    public static void Main()
    {
        Console.WriteLine(1.2134m.Round());
        Console.WriteLine(decimal.Round(1.2134m));
    }
}

The dump:

G_M20519_IG01:        ; func=00, offs=000000H, size=0013H, gcrefRegs=00000000 {}, byrefRegs=00000000 {}, byref, nogc <-- Prolog IG

IN002e: 000000 push     rbp
IN002f: 000001 sub      rsp, 192
IN0030: 000008 vzeroupper
IN0031: 00000B lea      rbp, [rsp+C0H]

G_M20519_IG02:        ; offs=000013H, size=00D3H, gcrefRegs=00000000 {}, byrefRegs=00000000 {}, byref

IN0001: 000013 lea      rcx, bword ptr [V01 rbp-10H]
IN0002: 000017 vxorps   xmm0, xmm0
IN0003: 00001C vmovdqu  qword ptr [rcx], xmm0
IN0004: 000021 xor      ecx, ecx
IN0005: 000023 mov      dword ptr [V00+0x20 rsp+20H], ecx
IN0006: 000027 mov      dword ptr [V00+0x28 rsp+28H], 4
IN0007: 00002F lea      rcx, bword ptr [V01 rbp-10H]
IN0008: 000033 mov      edx, 0x2F66
IN0009: 000038 xor      r8d, r8d
IN000a: 00003B xor      r9d, r9d
IN000b: 00003E call     Decimal:.ctor(int,int,int,bool,ubyte):this
IN000c: 000043 lea      rcx, bword ptr [V02 rbp-20H]
IN000d: 000047 mov      qword ptr [V06 rbp-58H], rcx
IN000e: 00004B vmovdqu  xmm0, qword ptr [V01 rbp-10H]
IN000f: 000051 vmovdqu  qword ptr [V05 rbp-50H], xmm0
IN0010: 000057 mov      rcx, qword ptr [V06 rbp-58H]
IN0011: 00005B lea      rdx, bword ptr [V05 rbp-50H]
IN0012: 00005F call     DecimalExtensions:Round(struct):struct
IN0013: 000064 vmovdqu  xmm0, qword ptr [V02 rbp-20H]
IN0014: 00006A vmovdqu  qword ptr [V07 rbp-68H], xmm0
IN0015: 000070 lea      rcx, bword ptr [V07 rbp-68H]
IN0016: 000074 call     Console:WriteLine(struct)
IN0017: 000079 lea      rcx, bword ptr [V03 rbp-30H]
IN0018: 00007D vxorps   xmm0, xmm0
IN0019: 000082 vmovdqu  qword ptr [rcx], xmm0
IN001a: 000087 xor      ecx, ecx
IN001b: 000089 mov      dword ptr [V00+0x20 rsp+20H], ecx
IN001c: 00008D mov      dword ptr [V00+0x28 rsp+28H], 4
IN001d: 000095 lea      rcx, bword ptr [V03 rbp-30H]
IN001e: 000099 mov      edx, 0x2F66
IN001f: 00009E xor      r8d, r8d
IN0020: 0000A1 xor      r9d, r9d
IN0021: 0000A4 call     Decimal:.ctor(int,int,int,bool,ubyte):this
IN0022: 0000A9 lea      rcx, bword ptr [V04 rbp-40H]
IN0023: 0000AD mov      qword ptr [V09 rbp-80H], rcx
IN0024: 0000B1 vmovdqu  xmm0, qword ptr [V03 rbp-30H]
IN0025: 0000B7 vmovdqu  qword ptr [V08 rbp-78H], xmm0
IN0026: 0000BD mov      rcx, qword ptr [V09 rbp-80H]
IN0027: 0000C1 lea      rdx, bword ptr [V08 rbp-78H]
IN0028: 0000C5 call     Decimal:Round(struct):struct
IN0029: 0000CA vmovdqu  xmm0, qword ptr [V04 rbp-40H]
IN002a: 0000D0 vmovdqu  qword ptr [V10 rbp-90H], xmm0
IN002b: 0000D9 lea      rcx, bword ptr [V10 rbp-90H]
IN002c: 0000E0 call     Console:WriteLine(struct)
IN002d: 0000E5 nop

G_M20519_IG03:        ; offs=0000E6H, size=0006H, epilog, nogc, emitadd

IN0032: 0000E6 lea      rsp, [rbp]
IN0033: 0000EA pop      rbp
IN0034: 0000EB ret

@bugproof bugproof changed the title Static methods are not getting inlined Methods are not getting inlined Sep 13, 2018
@benaadams
Copy link
Member

Try setting the environment variable

set COMPlus_JitPrintInlinedMethods=1

Should give more info?

@bugproof
Copy link
Author

With COMPlus_JitPrintInlinedMethods=1:

****** DONE compiling Program:Main()
**************** Inline Tree
Inlines into 06000000 DomainBoundILStubClass:IL_STUB_PInvoke(int):long
  [0 IL=0010 TR=000011 060030F5] [FAILED: noinline per IL/cached result] StubHelpers:ClearLastError()
  [0 IL=0030 TR=000025 060030F6] [FAILED: noinline per IL/cached result] StubHelpers:SetLastError()
Budget: initialTime=192, finalTime=192, initialBudget=1920, currentBudget=1920
Budget: initialSize=1134, finalSize=1134
**************** Inline Tree
Inlines into 06000000 DomainBoundILStubClass:IL_STUB_PInvoke(long,long,int,byref,long):int
  [0 IL=0035 TR=000031 060030F5] [FAILED: noinline per IL/cached result] StubHelpers:ClearLastError()
  [0 IL=0055 TR=000049 060030F6] [FAILED: noinline per IL/cached result] StubHelpers:SetLastError()
Budget: initialTime=303, finalTime=303, initialBudget=3030, currentBudget=3030
Budget: initialSize=1978, finalSize=1978
**************** Inline Tree
Inlines into 06000000 DomainBoundILStubClass:IL_STUB_PInvoke():int
  [0 IL=0004 TR=000005 060030F5] [FAILED: noinline per IL/cached result] StubHelpers:ClearLastError()
  [0 IL=0024 TR=000018 060030F6] [FAILED: noinline per IL/cached result] StubHelpers:SetLastError()
Budget: initialTime=168, finalTime=168, initialBudget=1680, currentBudget=1680
Budget: initialSize=952, finalSize=952
**************** Inline Tree
Inlines into 06000000 DomainBoundILStubClass:IL_STUB_PInvoke(long):int
  [0 IL=0009 TR=000010 060030F5] [FAILED: noinline per IL/cached result] StubHelpers:ClearLastError()
  [0 IL=0029 TR=000024 060030F6] [FAILED: noinline per IL/cached result] StubHelpers:SetLastError()
Budget: initialTime=189, finalTime=189, initialBudget=1890, currentBudget=1890
Budget: initialSize=1111, finalSize=1111
**************** Inline Tree
Inlines into 06000000 DomainBoundILStubClass:IL_STUB_PInvoke(int,int,long,int,long,int,long,long):int
Budget: initialTime=372, finalTime=372, initialBudget=3720, currentBudget=3720
Budget: initialSize=2502, finalSize=2502

@benaadams
Copy link
Member

benaadams commented Sep 13, 2018

Guessing is with Core 2.2+; likely to be teired jitting? Try switching it off

COMPlus_TieredCompilation=0

Method needs to be called 10 times I think to recompile with inlining active with tiered Jit I believe

@bugproof
Copy link
Author

bugproof commented Sep 13, 2018

@benadams with tiered jitting switched off it seems to inline correctly, however, it's not listed when using COMPlus_JitPrintInlinedMethods=1 ( the output of inlined methods doesn't change)

@benaadams
Copy link
Member

benaadams commented Sep 13, 2018

With teiring enabled then it should inline on the 11th iteration of

public static void Run()
{
    Console.WriteLine(1.2134m.Round());
    Console.WriteLine(decimal.Round(1.2134m));
}

public static void Main()
{
    for (var i = 0; i < 20; i++)
    {
        Run();
    }
}

@AndyAyersMS
Copy link
Member

Also make sure you are copying a check or debug built JIT into a release build of the runtime/framework.

The debug build of the System.Private.CoreLib will have an attribute that inhibits all jit optimization. The checked build will still contain assertion checks.

@RussKeldorph
Copy link
Contributor

@Necronux Is this issue answered to your satisfaction? Can you close?

@msftgits msftgits transferred this issue from dotnet/coreclr Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants