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

PGO: Profile isinst/castclass #65460

Merged
merged 11 commits into from
Feb 17, 2022
Merged

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Feb 16, 2022

This PR adds class probes for some castclass/isinst operations in order to then optimize them in Tier1.
These probes are only supposed to be run for static PGO since they add some overhead for the dynamic one and we want to avoid it, while for the static one we can afford some overhead for a training session.

I didn't push the change to utilize the PGO for cast/isinst because I want this to be merged first, then flow into our static pgo infrastructure (where I'll enable this flag) and collect isinst/castclass for StandardOptimizationData.mibc. Then, I'll merge the second change and it will allow me to catch improvements properly for dotnet/performance.

Simple benchmark with the 2nd change (PGO is enabled):

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

public class Program2 : Program {}

public class Program
{
    public static void Main(string[] args)
    {
        BenchmarkSwitcher
            .FromAssembly(typeof(Program).Assembly)
            .Run(args);
    }

    public object[] testData;

    [GlobalSetup]
    public void Setup()
    {
        testData = new object[1024];
        for (int i = 0; i < testData.Length; i++)
            testData[i] = new Program2();
    }

    [Benchmark]
    public int CountPrograms()
    {
        var array = testData;
        int count = 0;
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] is Program)
                count++;
        }
        return count;
    }

    [Benchmark]
    public void CastPrograms()
    {
        var array = testData;
        for (int i = 0; i < array.Length; i++)
            Consume((Program)array[i]);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Program p) {}
}
Method Job Toolchain Mean Error StdDev Ratio
CountPrograms Job-AGRJVL \Core_Root\corerun.exe 895.6 ns 2.03 ns 1.69 ns 1.00
CountPrograms Job-UTLWER \Core_Root_base\corerun.exe 2,014.3 ns 5.81 ns 4.53 ns 2.25
CastPrograms Job-AGRJVL \Core_Root\corerun.exe 1,577.2 ns 6.27 ns 5.24 ns 1.00
CastPrograms Job-UTLWER \Core_Root_base\corerun.exe 2,922.8 ns 18.81 ns 16.67 ns 1.85

@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 16, 2022
@ghost ghost assigned EgorBo Feb 16, 2022
@ghost
Copy link

ghost commented Feb 16, 2022

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

Issue Details

This PR adds class probes for some castclass/isinst operations in order to then optimize them in Tier1.
These probes are only supposed to be run for static PGO since they add some overhead for the dynamic one and we want to avoid it, while for the static one we can afford some overhead for a training session.

I didn't push the change to utilize the PGO for cast/isinst because I want this to be merged first, then flow into our static pgo infrastructure (where I'll enable this flag) and collect isinst/castclass for StandardOptimizationData.mibc. Then, I'll merge the second change and it will allow me to catch improvements properly for dotnet/performance.

Simple benchmark with the 2nd change (PGO is enabled):

using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

public class Program2 : Program {}

public class Program
{
    public static void Main(string[] args)
    {
        BenchmarkSwitcher
            .FromAssembly(typeof(Program).Assembly)
            .Run(args);
    }

    public object[] testData;

    [GlobalSetup]
    public void Setup()
    {
        testData = new object[1024];
        for (int i = 0; i < testData.Length; i++)
            testData[i] = new Program2();
    }

    [Benchmark]
    public int CountPrograms()
    {
        var array = testData;
        int count = 0;
        for (int i = 0; i < array.Length; i++)
        {
            if (array[i] is Program)
                count++;
        }
        return count;
    }

    [Benchmark]
    public void CastPrograms()
    {
        var array = testData;
        for (int i = 0; i < array.Length; i++)
            Consume((Program)array[i]);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Consume(Program p) {}
}
Method Job Toolchain Mean Error StdDev Ratio
CountPrograms Job-AGRJVL \Core_Root\corerun.exe 895.6 ns 2.03 ns 1.69 ns 1.00
CountPrograms Job-UTLWER \Core_Root_base\corerun.exe 2,014.3 ns 5.81 ns 4.53 ns 2.25
CastPrograms Job-AGRJVL \Core_Root\corerun.exe 1,577.2 ns 6.27 ns 5.24 ns 1.00
CastPrograms Job-UTLWER \Core_Root_base\corerun.exe 2,922.8 ns 18.81 ns 16.67 ns 1.85
Author: EgorBo
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@EgorBo
Copy link
Member Author

EgorBo commented Feb 16, 2022

/azp list

@azure-pipelines

This comment was marked as resolved.

@EgorBo
Copy link
Member Author

EgorBo commented Feb 16, 2022

/azp run runtime-coreclr pgo, runtime-coreclr libraries-pgo

@azure-pipelines
Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@EgorBo
Copy link
Member Author

EgorBo commented Feb 16, 2022

@AndyAyersMS @jakobbotsch PTAL

src/coreclr/jit/fgprofile.cpp Outdated Show resolved Hide resolved
src/coreclr/jit/importer.cpp Outdated Show resolved Hide resolved
Copy link
Member

@AndyAyersMS AndyAyersMS left a comment

Choose a reason for hiding this comment

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

LGTM.

One minor suggestion.

@EgorBo EgorBo merged commit 09b6149 into dotnet:main Feb 17, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 20, 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.

2 participants