Skip to content

Commit

Permalink
Enable the latest managed pgo data (#49793)
Browse files Browse the repository at this point in the history
- Implementation is parallel to existing ibc handling, so that it can be toggled on/off by adjusting the `UsingToolIbcOptimization` property
- Use the same data for all assemblies produced in current build
- Apply data to release builds only
- Disable mismatch assertions in jit for current state where il mismatches are common
  • Loading branch information
davidwrighton authored Mar 19, 2021
1 parent 3df7a6e commit 5d4a851
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 4 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<RepoToolsLocalDir>$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', 'tools-local'))</RepoToolsLocalDir>
<RepoTasksDir>$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', 'src', 'tasks'))</RepoTasksDir>
<IbcOptimizationDataDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsDir)', 'ibc'))</IbcOptimizationDataDir>
<MibcOptimizationDataDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsDir)', 'mibc'))</MibcOptimizationDataDir>
<XmlDocDir>$([MSBuild]::NormalizeDirectory('$(ArtifactsBinDir)', 'docs'))</XmlDocDir>
<XmlDocFileRoot>$([MSBuild]::NormalizeDirectory('$(NuGetPackageRoot)', 'microsoft.private.intellisense', '$(MicrosoftPrivateIntellisenseVersion)', 'IntellisenseFiles', 'net'))</XmlDocFileRoot>
<DocsDir>$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', 'docs'))</DocsDir>
Expand Down
5 changes: 4 additions & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<!-- Opt-in/out repo features -->
<UsingToolMicrosoftNetCompilers>true</UsingToolMicrosoftNetCompilers>
<UsingToolMicrosoftNetILLinkTasks>true</UsingToolMicrosoftNetILLinkTasks>
<UsingToolIbcOptimization>true</UsingToolIbcOptimization>
<UsingToolIbcOptimization>false</UsingToolIbcOptimization>
<UsingToolXliff>false</UsingToolXliff>
<!--
The NETStandard.Library targeting pack uses this patch version, which does not match the
Expand Down Expand Up @@ -116,6 +116,9 @@
<SystemSecurityCryptographyX509CertificatesTestDataVersion>6.0.0-beta.21162.1</SystemSecurityCryptographyX509CertificatesTestDataVersion>
<SystemWindowsExtensionsTestDataVersion>6.0.0-beta.21162.1</SystemWindowsExtensionsTestDataVersion>
<!-- dotnet-optimization dependencies -->
<optimizationwindows_ntx64MIBCRuntimeVersion>99.99.99-master-20210317.2</optimizationwindows_ntx64MIBCRuntimeVersion>
<optimizationwindows_ntx86MIBCRuntimeVersion>99.99.99-master-20210317.2</optimizationwindows_ntx86MIBCRuntimeVersion>
<optimizationlinuxx64MIBCRuntimeVersion>99.99.99-master-20210317.2</optimizationlinuxx64MIBCRuntimeVersion>
<optimizationwindows_ntx64IBCCoreFxVersion>99.99.99-master-20200806.6</optimizationwindows_ntx64IBCCoreFxVersion>
<optimizationlinuxx64IBCCoreFxVersion>99.99.99-master-20200806.6</optimizationlinuxx64IBCCoreFxVersion>
<optimizationwindows_ntx64IBCCoreCLRVersion>99.99.99-master-20200806.6</optimizationwindows_ntx64IBCCoreCLRVersion>
Expand Down
46 changes: 46 additions & 0 deletions eng/restore/optimizationData.targets
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,50 @@

</Target>


<ItemGroup>
<!-- Mibc data to use when exact architecture match is available -->
<MIBCPackageDef Include="optimization.windows_nt-x86.mibc.runtime" Version="$(optimizationwindows_ntx86MIBCRuntimeVersion)" MibcArchitecture="Windows/x86"/>
<MIBCPackageDef Include="optimization.windows_nt-x64.mibc.runtime" Version="$(optimizationwindows_ntx64MIBCRuntimeVersion)" MibcArchitecture="Windows/x64"/>
<MIBCPackageDef Include="optimization.linux-x64.mibc.runtime" Version="$(optimizationlinuxx64MIBCRuntimeVersion)" MibcArchitecture="Linux/x64"/>

<!-- Mibc data to use when exact architecture match not available -->
<MIBCPackageDef Include="optimization.windows_nt-x64.mibc.runtime" Version="$(optimizationwindows_ntx64MIBCRuntimeVersion)" MibcArchitecture="Windows"/>
<MIBCPackageDef Include="optimization.linux-x64.mibc.runtime" Version="$(optimizationlinuxx64MIBCRuntimeVersion)" MibcArchitecture="Linux"/>
<MIBCPackageDef Include="optimization.linux-x64.mibc.runtime" Version="$(optimizationlinuxx64MIBCRuntimeVersion)" MibcArchitecture="OSX"/>

<MIBCPackage Include="@(MIBCPackageDef->HasMetadata('MibcArchitecture')->WithMetadataValue('MibcArchitecture','$(TargetOS)/$(TargetArchitecture)'))" />
<MIBCPackage Include="@(MIBCPackageDef->HasMetadata('MibcArchitecture')->WithMetadataValue('MibcArchitecture','$(TargetOS)'))" Condition="'@(MIBCPackage)' == ''" />

<!-- Fallback in case no os specific data is available-->
<MIBCPackage Include="optimization.linux-x64.mibc.runtime" Version="$(optimizationlinuxx64MIBCRuntimeVersion)" Condition="'@(MIBCPackage)' == ''" />

<PackageDownload Include="@(MIBCPackage)" Version="[%(Version)]" />
</ItemGroup>

<!-- IBC data packages don't follow NuGet conventions. -->
<Target Name="GetMIBCData"
AfterTargets="Restore">

<ItemGroup>
<MIBCPackage>
<PackagePath>$(NuGetPackageRoot)%(MIBCPackage.Identity)/%(MIBCPackage.Version)</PackagePath>
</MIBCPackage>
<_optimizationMibcFile Include="%(MIBCPackage.PackagePath)/**/*.mibc" SubdirectoryName="$(TargetOS)/$(TargetArchitecture)" />
<ExcessFilesCurrentlyPresent Include="$(MibcOptimizationDataDir)/**"
Exclude="@(_optimizationMibcFile->'$(MibcOptimizationDataDir)%(SubdirectoryName)/%(RecursiveDir)%(Filename)%(Extension)')"/>
</ItemGroup>

<Error Condition="'@(_optimizationMibcFile)' == ''" Text="Failed to restore Mibc optimization data" />

<!-- Clean mibc restore directory so that no extra mibc files are present. Improves incremental build resiliency -->
<Delete Files="@(ExcessFilesCurrentlyPresent)" />

<!-- Copy the correct mibc files into place -->
<Copy SourceFiles="@(_optimizationMibcFile)"
DestinationFiles="@(_optimizationMibcFile->'$(MibcOptimizationDataDir)%(SubdirectoryName)/%(RecursiveDir)%(Filename)%(Extension)')"
SkipUnchangedFiles="true"
UseHardlinksIfPossible="true" />

</Target>
</Project>
6 changes: 6 additions & 0 deletions src/coreclr/crossgen-corelib.proj
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,17 @@
<Message Importance="High"
Text="Generating native image of System.Private.CoreLib for $(OSPlatformConfig). Logging to $(CrossGenCoreLibLog)" />

<ItemGroup>
<OptimizationMibcFiles Include="$(MibcOptimizationDataDir)/$(TargetOS)/$(TargetArchitecture)/**/*.mibc" />
</ItemGroup>

<PropertyGroup>
<CrossGenDllCmd>$(DotNetCli) $([MSBuild]::NormalizePath('$(BinDir)', '$(CrossDir)', 'crossgen2', 'crossgen2.dll'))</CrossGenDllCmd>
<CrossGenDllCmd>$(CrossGenDllCmd) -o:$(CoreLibOutputPath)</CrossGenDllCmd>
<CrossGenDllCmd>$(CrossGenDllCmd) -r:$([MSBuild]::NormalizePath('$(BinDir)', 'IL', '*.dll'))</CrossGenDllCmd>
<CrossGenDllCmd>$(CrossGenDllCmd) --targetarch:$(TargetArchitecture)</CrossGenDllCmd>
<MibcArgs>@(OptimizationMibcFiles->'-m:%(Identity)', ' ')</MibcArgs>
<CrossGenDllCmd Condition="'$(UsingToolIbcOptimization)' != 'true' and '$(Configuration)' == 'Release'">$(CrossGenDllCmd) $(MibcArgs) --embed-pgo-data</CrossGenDllCmd>
<CrossGenDllCmd>$(CrossGenDllCmd) -O</CrossGenDllCmd>
<CrossGenDllCmd>$(CrossGenDllCmd) $(CoreLibInputPath)</CrossGenDllCmd>
</PropertyGroup>
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2445,7 +2445,10 @@ void EfficientEdgeCountReconstructor::Propagate()
{
// We don't expect mismatches or convergence failures.
//
assert(!m_mismatch);

// Mismatches are currently expected as the flow for static pgo doesn't prevent them now.
// assert(!m_mismatch);

assert(!m_failedToConverge);

// If any issues arose during reconstruction, don't set weights.
Expand Down
24 changes: 23 additions & 1 deletion src/coreclr/tools/dotnet-pgo/CommandLineOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ internal class CommandLineOptions
public bool DetailedProgressMessages;
public List<FileInfo> InputFilesToMerge;
public List<AssemblyName> IncludedAssemblies = new List<AssemblyName>();
public bool DumpMibc = false;
public FileInfo InputFileToDump;

public string[] HelpArgs = Array.Empty<string>();

Expand Down Expand Up @@ -259,6 +261,26 @@ void HelpOption()
#endif
}

var dumpCommand = syntax.DefineCommand(name: "dump", value: ref command, help: "Dump the contents of a Mibc file.");
if (dumpCommand.IsActive)
{
DumpMibc = true;
HelpArgs = new string[] { "dump", "--help", "input", "output" };

VerbosityOption();
HelpOption();

string inputFileToDump = null;
syntax.DefineParameter(name: "input", ref inputFileToDump, "Name of the input mibc file to dump.");
if (inputFileToDump != null)
InputFileToDump = new FileInfo(inputFileToDump);

string outputFile = null;
syntax.DefineParameter(name: "output", ref outputFile, "Name of the output dump file.");
if (outputFile != null)
OutputFileName = new FileInfo(outputFile);
}


if (syntax.ActiveCommand == null)
{
Expand Down Expand Up @@ -341,7 +363,7 @@ private CommandLineOptions()
private void ParseCommmandLineHelper(string[] args)
{
ArgumentSyntax argSyntax = ArgumentSyntax.Parse(args, DefineArgumentSyntax);
if (Help || (!FileType.HasValue && (InputFilesToMerge == null)))
if (Help || (!FileType.HasValue && (InputFilesToMerge == null) && !DumpMibc))
{
Help = true;
}
Expand Down
104 changes: 104 additions & 0 deletions src/coreclr/tools/dotnet-pgo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
using System.Reflection.PortableExecutable;
using ILCompiler.IBC;
using ILCompiler;
using System.Runtime.Serialization.Json;
using System.Text.Json;
using System.Text.Encodings.Web;

namespace Microsoft.Diagnostics.Tools.Pgo
{
Expand Down Expand Up @@ -241,6 +244,10 @@ static int InnerMain(CommandLineOptions commandLineOptions)
if (!commandLineOptions.DetailedProgressMessages)
s_logger.HideDetailedMessages();

if (commandLineOptions.DumpMibc)
{
return InnerDumpMain(commandLineOptions);
}
if (commandLineOptions.InputFilesToMerge != null)
{
return InnerMergeMain(commandLineOptions);
Expand All @@ -251,6 +258,103 @@ static int InnerMain(CommandLineOptions commandLineOptions)
}
}

static int InnerDumpMain(CommandLineOptions commandLineOptions)
{
if ((commandLineOptions.InputFileToDump == null) || (!commandLineOptions.InputFileToDump.Exists))
{
PrintUsage(commandLineOptions, "Valid input file must be specified");
return -8;
}

if (commandLineOptions.OutputFileName == null)
{
PrintUsage(commandLineOptions, "Output filename must be specified");
return -8;
}

PrintDetailedMessage($"Opening {commandLineOptions.InputFileToDump}");
var mibcPeReader = MIbcProfileParser.OpenMibcAsPEReader(commandLineOptions.InputFileToDump.FullName);
var tsc = new TypeRefTypeSystem.TypeRefTypeSystemContext(new PEReader[] { mibcPeReader });

PrintDetailedMessage($"Parsing {commandLineOptions.InputFileToDump}");
var profileData = MIbcProfileParser.ParseMIbcFile(tsc, mibcPeReader, null, onlyDefinedInAssembly: null);

using (FileStream outputFile = new FileStream(commandLineOptions.OutputFileName.FullName, FileMode.Create, FileAccess.Write))
{
JsonWriterOptions options = new JsonWriterOptions();
options.Indented = true;
options.SkipValidation = false;
options.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;

using Utf8JsonWriter jsonWriter = new Utf8JsonWriter(outputFile, options);
jsonWriter.WriteStartObject();
jsonWriter.WriteStartArray("Methods");
foreach (MethodProfileData data in profileData.GetAllMethodProfileData())
{
jsonWriter.WriteStartObject();
jsonWriter.WriteString("Method", data.Method.ToString());
if (data.CallWeights != null)
{
jsonWriter.WriteStartArray("CallWeights");
foreach (var callWeight in data.CallWeights)
{
jsonWriter.WriteString("Method", callWeight.Key.ToString());
jsonWriter.WriteNumber("Weight", callWeight.Value);
}
jsonWriter.WriteEndArray();
}
if (data.ExclusiveWeight != 0)
{
jsonWriter.WriteNumber("ExclusiveWeight", data.ExclusiveWeight);
}
if (data.SchemaData != null)
{
jsonWriter.WriteStartArray("InstrumentationData");
foreach (var schemaElem in data.SchemaData)
{
jsonWriter.WriteStartObject();
jsonWriter.WriteNumber("ILOffset", schemaElem.ILOffset);
jsonWriter.WriteString("InstrumentationKind", schemaElem.InstrumentationKind.ToString());
jsonWriter.WriteNumber("Other", schemaElem.Other);
if (schemaElem.DataHeldInDataLong)
{
jsonWriter.WriteNumber("Data", schemaElem.DataLong);
}
else
{
if (schemaElem.DataObject == null)
{
// No data associated with this item
}
else if (schemaElem.DataObject.Length == 1)
{
jsonWriter.WriteString("Data", schemaElem.DataObject.GetValue(0).ToString());
}
else
{
jsonWriter.WriteStartArray("Data");
foreach (var dataElem in schemaElem.DataObject)
{
jsonWriter.WriteStringValue(dataElem.ToString());
}
jsonWriter.WriteEndArray();
}
}
jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
}

jsonWriter.WriteEndObject();
}
jsonWriter.WriteEndArray();
jsonWriter.WriteEndObject();
}
PrintMessage($"Generated {commandLineOptions.OutputFileName}");

return 0;
}


static int InnerMergeMain(CommandLineOptions commandLineOptions)
{
Expand Down
14 changes: 13 additions & 1 deletion src/installer/pkg/sfx/Microsoft.NETCore.App/ReadyToRun.targets
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
<Project>
<Target Name="ResolveReadyToRunCompilers" DependsOnTargets="ResolveRuntimeFilesFromLocalBuild">
<ItemGroup>
<OptimizationMibcFiles Include="$(MibcOptimizationDataDir)/$(TargetOS)/$(TargetArchitecture)/**/*.mibc" />
</ItemGroup>

<ItemGroup>
<PublishReadyToRunCrossgen2ExtraArgsList Include="--targetarch:$(TargetArchitecture)"/>

<!-- Only use mibc files if UsingToolIbcOptimization is false. Allows enabling/disabling using ibc instead of mibc data -->
<PublishReadyToRunCrossgen2ExtraArgsList Condition="'$(UsingToolIbcOptimization)' != 'true' and '$(Configuration)' == 'Release'" Include="@(OptimizationMibcFiles->'-m:%(Identity)')"/>
<PublishReadyToRunCrossgen2ExtraArgsList Condition="'$(UsingToolIbcOptimization)' != 'true' and '$(Configuration)' == 'Release'" Include="--embed-pgo-data"/>
</ItemGroup>

<!-- The following property group can be simplified once runtime repo switches over to SDK 6.0 drop -->
<PropertyGroup>
<CrossDir />
<CrossDir Condition="'$(BuildArchitecture)' != '$(TargetArchitecture)'">x64</CrossDir>
<Crossgen2Dir>$(CoreCLRArtifactsPath)\$(CrossDir)\crossgen2</Crossgen2Dir>
<Crossgen2Exe>$(Crossgen2Dir)\crossgen2$(ExeSuffix)</Crossgen2Exe>
<PublishReadyToRunUseCrossgen2>true</PublishReadyToRunUseCrossgen2>
<PublishReadyToRunCrossgen2ExtraArgs>--targetarch:$(TargetArchitecture)</PublishReadyToRunCrossgen2ExtraArgs>
<PublishReadyToRunCrossgen2ExtraArgs>@(PublishReadyToRunCrossgen2ExtraArgsList)</PublishReadyToRunCrossgen2ExtraArgs>

<JitTargetOSComponent>unix</JitTargetOSComponent>
<JitTargetOSComponent Condition="'$(TargetOS)' == 'windows'">win</JitTargetOSComponent>
Expand Down

0 comments on commit 5d4a851

Please sign in to comment.