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

Rely on PGO for isinst/castclass #65922

Merged
merged 9 commits into from
Apr 1, 2022
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 26, 2022

Follow up to #65460 (and d-o)

Example (with DOTNET_JitCastProfiling=1):

using System.Runtime.CompilerServices;
using System.Threading;

public interface IClass {}
public class ClassA : IClass {}
public class ClassB : ClassA {}


public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static ClassA CastToClassA(object o) => (ClassA)o; // currently we always emit a fast path for ClassA here
                                                       // but in my case o is always ClassB (PGO helps)

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsClassA(object o) => o is ClassA;     // we don't expand it without PGO

    [MethodImpl(MethodImplOptions.NoInlining)]
    static IClass CastToIClass(object o) => (IClass)o; // we don't expand it without PGO

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsIClass(object o) => o is IClass;     // we don't expand it without PGO


    public static void Main()
    {
        // promote methods to tier1
        var b = new ClassB();
        for (int i = 0; i < 100; i++)
        {
            CastToClassA(b);
            IsClassA(b);
            CastToIClass(b);
            IsIClass(b);
            Thread.Sleep(16);
        }
    }
}

Codegen diff: https://www.diffchecker.com/RFblv9RB

@ghost ghost assigned EgorBo Feb 26, 2022
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Feb 26, 2022
@ghost
Copy link

ghost commented Feb 26, 2022

Tagging subscribers to this area: @JulieLeeMSFT
See info in area-owners.md if you want to be subscribed.

Issue Details

Follow up to #65460

Example (with DOTNET_JitCastProfiling=1):

using System.Runtime.CompilerServices;
using System.Threading;

public interface IClass {}
public class ClassA : IClass {}
public class ClassB : ClassA {}


public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static ClassA CastToClassA(object o) => (ClassA)o;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsClassA(object o) => o is ClassA;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static IClass CastToIClass(object o) => (IClass)o;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool IsIClass(object o) => o is IClass;


    public static void Main()
    {
        // promote methods to tier1
        var b = new ClassB();
        for (int i = 0; i < 100; i++)
        {
            CastToClassA(b);
            IsClassA(b);
            CastToIClass(b);
            IsIClass(b);
            Thread.Sleep(16);
        }
    }
}

Codegen diff: https://www.diffchecker.com/RFblv9RB

Author: EgorBo
Assignees: EgorBo
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo EgorBo marked this pull request as ready for review March 21, 2022 08:28
@EgorBo
Copy link
Member Author

EgorBo commented Mar 21, 2022

@AndyAyersMS @jakobbotsch PTAL

This PR consumes profile data from mibc for casts/isinst. I guarded it with DOTNET_JitConsumeProfileForCasts just to wait till dotnet-optimization properly propagates mibc with casts/isinst.

Also, I added "random class" stress mode support for it

@EgorBo
Copy link
Member Author

EgorBo commented Mar 28, 2022

@AndyAyersMS @jakobbotsch PTAL

CORINFO_CLASS_HANDLE likelyCls = likelyClass.clsHandle;

if ((likelyCls != NO_CLASS_HANDLE) &&
(likelyClass.likelihood > (UINT32)JitConfig.JitGuardedDevirtualizationChainLikelihood()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this rather get its own config variable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just didn't want to produce even more variables 😄 and the default value for this one was OK to me.
in my other PR to add "multiple guesses" I slightly changed the whole logic so I'll leave it as is for now

@EgorBo EgorBo merged commit f249a3d into dotnet:main Apr 1, 2022
@ghost ghost locked as resolved and limited conversation to collaborators May 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants