Skip to content

Commit

Permalink
Add ability for crossgen2 to synthesize PGO histograms
Browse files Browse the repository at this point in the history
Add an option --synthesize-random-mibc. When passed, crossgen2 will use
metadata from the compilation group to synthesize PGO histograms for
methods that did not have any input profile data.

Mainly for testing purposes. Currently only works in R2R mode, but
hopefully will also work to embed random static PGO data in the future.
  • Loading branch information
jakobbotsch committed Oct 31, 2022
1 parent 30d8593 commit 255a128
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 73 deletions.
11 changes: 5 additions & 6 deletions src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3919,11 +3919,6 @@ private uint getJitFlags(ref CORJIT_FLAGS flags, uint sizeInBytes)
return (uint)sizeof(CORJIT_FLAGS);
}

private PgoSchemaElem[] getPgoInstrumentationResults(MethodDesc method)
{
return _compilation.ProfileData[method]?.SchemaData;
}

private MemoryStream _cachedMemoryStream = new MemoryStream();

public static void ComputeJitPgoInstrumentationSchema(Func<object, IntPtr> objectToHandle, PgoSchemaElem[] pgoResultsSchemas, out PgoInstrumentationSchema[] nativeSchemas, MemoryStream instrumentationData, Func<TypeDesc, bool> typeFilter = null)
Expand Down Expand Up @@ -4001,7 +3996,11 @@ private HRESULT getPgoInstrumentationResults(CORINFO_METHOD_STRUCT_* ftnHnd, ref

if (!_pgoResults.TryGetValue(methodDesc, out PgoInstrumentationResults pgoResults))
{
var pgoResultsSchemas = getPgoInstrumentationResults(methodDesc);
#if READYTORUN
PgoSchemaElem[] pgoResultsSchemas = _compilation.ProfileData.GetAllowSynthesis(_compilation, methodDesc)?.SchemaData;
#else
PgoSchemaElem[] pgoResultsSchemas = _compilation.ProfileData[methodDesc]?.SchemaData;
#endif
if (pgoResultsSchemas == null)
{
pgoResults.hr = HRESULT.E_NOTIMPL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ public abstract class ProfileData
public abstract IEnumerable<MethodProfileData> GetAllMethodProfileData();
public abstract byte[] GetMethodBlockCount(MethodDesc m);

public static void MergeProfileData(ref bool partialNgen, Dictionary<MethodDesc, MethodProfileData> mergedProfileData, ProfileData profileData)
public static void MergeProfileData(Dictionary<MethodDesc, MethodProfileData> mergedProfileData, ProfileData profileData)
{
if (profileData.PartialNGen)
partialNgen = true;

PgoSchemaElem[][] schemaElemMergerArray = new PgoSchemaElem[2][];

foreach (MethodProfileData data in profileData.GetAllMethodProfileData())
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,7 @@ private bool CrossModuleInlineableInternal(MethodDesc method)
return _crossModuleInlineableCache.GetOrAdd(method, _crossModuleInlineableCacheUncached);
}

private bool CrossModuleInlineableUncached(MethodDesc method)
private bool CrossModuleInlineableUncached(MethodDesc method)
{
// Defined in corelib
MetadataType owningMetadataType = method.OwningType.GetTypeDefinition() as MetadataType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed override bool ContainsMethodBody(MethodDesc method, bool unboxingS

if (_profileGuidedCompileRestriction != null)
{
if (!_profileGuidedCompileRestriction.IsMethodInProfileData(method))
if (!_profileGuidedCompileRestriction.IsMethodInInputProfileData(method))
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ public ProfileDataManager(IEnumerable<string> mibcFiles,
}
}

bool dummy = false;

// Merge all data together
foreach (ProfileData profileData in _inputData)
{
ProfileData.MergeProfileData(ref dummy, _mergedProfileData, profileData);
ProfileData.MergeProfileData(_mergedProfileData, profileData);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/tools/aot/crossgen2/Crossgen2RootCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ internal class Crossgen2RootCommand : RootCommand
new(new[] { "--make-repro-path" }, "Path where to place a repro package");
public Option<bool> HotColdSplitting { get; } =
new(new[] { "--hot-cold-splitting" }, SR.HotColdSplittingOption);
public Option<bool> SynthesizeRandomMibc { get; } =
new(new[] { "--synthesize-random-mibc" });

public bool CompositeOrInputBubble { get; private set; }
public OptimizationMode OptimizationMode { get; private set; }
Expand Down Expand Up @@ -243,6 +245,7 @@ public Crossgen2RootCommand(string[] args) : base(SR.Crossgen2BannerText)
AddOption(CallChainProfileFile);
AddOption(MakeReproPath);
AddOption(HotColdSplitting);
AddOption(SynthesizeRandomMibc);

this.SetHandler(context =>
{
Expand Down
3 changes: 2 additions & 1 deletion src/coreclr/tools/aot/crossgen2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ private void RunSingleCompilation(Dictionary<string, string> inFilePaths, Instru
compilationGroup,
Get(_command.EmbedPgoData),
Get(_command.SupportIbc),
crossModuleInlineableCode.Count == 0 ? compilationGroup.VersionsWithMethodBody : compilationGroup.CrossModuleInlineable);
crossModuleInlineableCode.Count == 0 ? compilationGroup.VersionsWithMethodBody : compilationGroup.CrossModuleInlineable,
Get(_command.SynthesizeRandomMibc));

bool partial = Get(_command.Partial);
compilationGroup.ApplyProfileGuidedOptimizationData(profileDataManager, partial);
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/tools/dotnet-pgo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,12 @@ private int InnerMergeMain()
{
var tsc = new TypeRefTypeSystem.TypeRefTypeSystemContext(mibcReaders);

bool partialNgen = false;
Dictionary<MethodDesc, MethodProfileData> mergedProfileData = new Dictionary<MethodDesc, MethodProfileData>();
for (int i = 0; i < mibcReaders.Length; i++)
{
var peReader = mibcReaders[i];
PrintDetailedMessage($"Merging {paths[i]}");
ProfileData.MergeProfileData(ref partialNgen, mergedProfileData, MIbcProfileParser.ParseMIbcFile(tsc, peReader, assemblyNamesInBubble, onlyDefinedInAssembly: null));
ProfileData.MergeProfileData(mergedProfileData, MIbcProfileParser.ParseMIbcFile(tsc, peReader, assemblyNamesInBubble, onlyDefinedInAssembly: null));
}

MibcConfig mergedConfig = ParseMibcConfigsAndMerge(tsc, mibcReaders);
Expand Down

0 comments on commit 255a128

Please sign in to comment.