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

JIT: profile synthesis consistency checking and more #83068

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions eng/pipelines/common/templates/runtimes/run-test-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ jobs:
- fullpgo_random_gdv_methodprofiling_only
- fullpgo_random_gdv_edge
- fullpgo_methodprofiling_always_optimized
- syntheticpgo
${{ if in(parameters.testGroup, 'gc-longrunning') }}:
longRunningGcTests: true
scenarios:
Expand Down
1 change: 1 addition & 0 deletions eng/pipelines/libraries/run-test-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,4 @@ jobs:
- fullpgo_random_gdv_edge
- jitosr_stress
- jitosr_stress_random
- syntheticpgo
52 changes: 50 additions & 2 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,53 @@ inline constexpr FlowGraphUpdates operator&(FlowGraphUpdates a, FlowGraphUpdates
return (FlowGraphUpdates)((unsigned int)a & (unsigned int)b);
}

// Profile checking options
//
// clang-format off
enum class ProfileChecks : unsigned int
{
CHECK_NONE = 0,
CHECK_CLASSIC = 1 << 0, // check "classic" jit weights
CHECK_LIKELY = 1 << 1, // check likelihood based weights
RAISE_ASSERT = 1 << 2, // assert on check failure
CHECK_ALL_BLOCKS = 1 << 3, // check blocks even if bbHasProfileWeight is false
};

inline constexpr ProfileChecks operator ~(ProfileChecks a)
{
return (ProfileChecks)(~(unsigned int)a);
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should have a CHECK_ALL that contains all the legal bits (currently 0xF) and have this be:

Suggested change
return (ProfileChecks)(~(unsigned int)a);
return (ProfileChecks)(CHECK_ALL & ~(unsigned int)a);

to prevent returning bits that are not "legal" ProfileChecks bits.

Could validate/cap the value of JitConfig.JitProfileChecks() this way, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is all boilerplate for our common enum class approach, so we'd probably want to try and handle them all this way.

Since enum class is (somewhat) type safe seems like we could just mask when converting from int or other similar types?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, I hadn't noticed that other similar cases had also defined operator~() without this. A minor thing, to be sure.

}

inline constexpr ProfileChecks operator |(ProfileChecks a, ProfileChecks b)
{
return (ProfileChecks)((unsigned int)a | (unsigned int)b);
}

inline constexpr ProfileChecks operator &(ProfileChecks a, ProfileChecks b)
{
return (ProfileChecks)((unsigned int)a & (unsigned int)b);
}

inline ProfileChecks& operator |=(ProfileChecks& a, ProfileChecks b)
{
return a = (ProfileChecks)((unsigned int)a | (unsigned int)b);
}

inline ProfileChecks& operator &=(ProfileChecks& a, ProfileChecks b)
{
return a = (ProfileChecks)((unsigned int)a & (unsigned int)b);
}

inline ProfileChecks& operator ^=(ProfileChecks& a, ProfileChecks b)
{
return a = (ProfileChecks)((unsigned int)a ^ (unsigned int)b);
}

inline bool hasFlag(const ProfileChecks& flagSet, const ProfileChecks& flag)
{
return ((flagSet & flag) == flag);
}

//---------------------------------------------------------------
// Compilation time.
//
Expand Down Expand Up @@ -5516,8 +5563,9 @@ class Compiler
void fgDebugCheckFlagsHelper(GenTree* tree, GenTreeFlags actualFlags, GenTreeFlags expectedFlags);
void fgDebugCheckTryFinallyExits();
void fgDebugCheckProfileWeights();
bool fgDebugCheckIncomingProfileData(BasicBlock* block);
bool fgDebugCheckOutgoingProfileData(BasicBlock* block);
void fgDebugCheckProfileWeights(ProfileChecks checks);
bool fgDebugCheckIncomingProfileData(BasicBlock* block, ProfileChecks checks);
bool fgDebugCheckOutgoingProfileData(BasicBlock* block, ProfileChecks checks);

#endif // DEBUG

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ bool Compiler::fgDumpFlowGraph(Phases phase, PhasePosition pos)
}

// "Raw" Profile weight
if (block->hasProfileWeight())
if (block->hasProfileWeight() || (JitConfig.JitSynthesizeCounts() > 0))
{
fprintf(fgxFile, "\\n\\n%7.2f", ((double)block->getBBWeight(this)) / BB_UNITY_WEIGHT);
}
Expand Down
142 changes: 115 additions & 27 deletions src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,26 @@ void BlockCountInstrumentor::Instrument(BasicBlock* block, Schema& schema, uint8
(entry.InstrumentationKind == ICorJitInfo::PgoInstrumentationKind::BasicBlockLongCount));
uint8_t* addrOfCurrentExecutionCount = entry.Offset + profileMemory;

#ifdef DEBUG
if (JitConfig.JitPropagateSynthesizedCountsToProfileData() > 0)
{
// Write the current synthesized count as the profile data
//
weight_t blockWeight = block->bbWeight;

if (entry.InstrumentationKind == ICorJitInfo::PgoInstrumentationKind::EdgeIntCount)
{
*((uint32_t*)addrOfCurrentExecutionCount) = (uint32_t)blockWeight;
}
else
{
*((uint64_t*)addrOfCurrentExecutionCount) = (uint64_t)blockWeight;
}

return;
}
#endif

var_types typ =
entry.InstrumentationKind == ICorJitInfo::PgoInstrumentationKind::BasicBlockIntCount ? TYP_INT : TYP_LONG;

Expand Down Expand Up @@ -1742,6 +1762,32 @@ void EfficientEdgeCountInstrumentor::Instrument(BasicBlock* block, Schema& schem

uint8_t* addrOfCurrentExecutionCount = profileMemory + entry.Offset;

#ifdef DEBUG
if (JitConfig.JitPropagateSynthesizedCountsToProfileData() > 0)
{
// Write the current synthesized count as the profile data
//
// Todo: handle pseudo edges!
FlowEdge* const edge = m_comp->fgGetPredForBlock(source, target);

if (edge != nullptr)
{
weight_t edgeWeight = edge->getLikelyWeight();

if (entry.InstrumentationKind == ICorJitInfo::PgoInstrumentationKind::EdgeIntCount)
{
*((uint32_t*)addrOfCurrentExecutionCount) = (uint32_t)edgeWeight;
}
else
{
*((uint64_t*)addrOfCurrentExecutionCount) = (uint64_t)edgeWeight;
}
}

return;
}
#endif

// Determine where to place the probe.
//
BasicBlock* instrumentedBlock = nullptr;
Expand Down Expand Up @@ -2428,7 +2474,7 @@ PhaseStatus Compiler::fgIncorporateProfileData()
}

#ifdef DEBUG
// Optionally just run synthesis
// Optionally run synthesis
//
if ((JitConfig.JitSynthesizeCounts() > 0) && !compIsForInlining())
{
Expand All @@ -2439,6 +2485,17 @@ PhaseStatus Compiler::fgIncorporateProfileData()
return PhaseStatus::MODIFIED_EVERYTHING;
}
}

// Or run synthesis and save the data out as the actual profile data
//
if (opts.jitFlags->IsSet(JitFlags::JIT_FLAG_BBINSTR) &&
(JitConfig.JitPropagateSynthesizedCountsToProfileData() > 0) && !compIsForInlining())
{
JITDUMP("Synthesizing profile data and writing it out as the actual profile data\n");
ProfileSynthesis::Run(this, ProfileSynthesisOption::AssignLikelihoods);
fgPgoHaveWeights = false;
return PhaseStatus::MODIFIED_EVERYTHING;
}
#endif

// Do we have profile data?
Expand Down Expand Up @@ -4845,15 +4902,8 @@ bool Compiler::fgProfileWeightsConsistent(weight_t weight1, weight_t weight2)
// (or nearly so)
//
// Notes:
// For each profiled block, check that the flow of counts into
// the block matches the flow of counts out of the block.
//
// We ignore EH flow as we don't have explicit edges and generally
// we expect EH edge counts to be small, so errors from ignoring
// them should be rare.
//
// There's no point checking until we've built pred lists, as
// we can't easily reason about consistency without them.
// Does nothing, if profile checks are disabled, or there are
// no profile weights or pred lists.
//
void Compiler::fgDebugCheckProfileWeights()
{
Expand All @@ -4865,13 +4915,37 @@ void Compiler::fgDebugCheckProfileWeights()
return;
}

fgDebugCheckProfileWeights((ProfileChecks)JitConfig.JitProfileChecks());
}

//------------------------------------------------------------------------
// fgDebugCheckProfileWeights: verify profile weights are self-consistent
// (or nearly so)
//
// Arguments:
// checks - checker options
//
// Notes:
// For each profiled block, check that the flow of counts into
// the block matches the flow of counts out of the block.
//
// We ignore EH flow as we don't have explicit edges and generally
// we expect EH edge counts to be small, so errors from ignoring
// them should be rare.
//
// There's no point checking until we've built pred lists, as
// we can't easily reason about consistency without them.
//
void Compiler::fgDebugCheckProfileWeights(ProfileChecks checks)
{
// We can check classic (min/max, late computed) weights
// and/or
// new likelyhood based weights.
//
const bool verifyClassicWeights = fgEdgeWeightsComputed && (JitConfig.JitProfileChecks() & 0x1) == 0x1;
const bool verifyLikelyWeights = (JitConfig.JitProfileChecks() & 0x2) == 0x2;
const bool assertOnFailure = (JitConfig.JitProfileChecks() & 0x4) == 0x4;
const bool verifyClassicWeights = fgEdgeWeightsComputed && hasFlag(checks, ProfileChecks::CHECK_CLASSIC);
const bool verifyLikelyWeights = hasFlag(checks, ProfileChecks::CHECK_LIKELY);
const bool assertOnFailure = hasFlag(checks, ProfileChecks::RAISE_ASSERT);
const bool checkAllBlocks = hasFlag(checks, ProfileChecks::CHECK_ALL_BLOCKS);

if (!(verifyClassicWeights || verifyLikelyWeights))
{
Expand All @@ -4891,7 +4965,7 @@ void Compiler::fgDebugCheckProfileWeights()
//
for (BasicBlock* const block : Blocks())
{
if (!block->hasProfileWeight())
if (!block->hasProfileWeight() && !checkAllBlocks)
{
unprofiledBlocks++;
continue;
Expand Down Expand Up @@ -4929,8 +5003,11 @@ void Compiler::fgDebugCheckProfileWeights()
//
if (block->KindIs(BBJ_RETURN, BBJ_THROW))
{
exitWeight += blockWeight;
exitProfiled = !opts.IsOSR();
if (BasicBlock::sameHndRegion(block, fgFirstBB))
{
exitWeight += blockWeight;
exitProfiled = !opts.IsOSR();
}
verifyOutgoing = false;
}

Expand Down Expand Up @@ -4969,12 +5046,12 @@ void Compiler::fgDebugCheckProfileWeights()

if (verifyIncoming)
{
incomingConsistent = fgDebugCheckIncomingProfileData(block);
incomingConsistent = fgDebugCheckIncomingProfileData(block, checks);
}

if (verifyOutgoing)
{
outgoingConsistent = fgDebugCheckOutgoingProfileData(block);
outgoingConsistent = fgDebugCheckOutgoingProfileData(block, checks);
}

if (!incomingConsistent || !outgoingConsistent)
Expand All @@ -4987,7 +5064,13 @@ void Compiler::fgDebugCheckProfileWeights()
//
if (entryProfiled && exitProfiled)
{
if (!fgProfileWeightsConsistent(entryWeight, exitWeight))
// Note these may not agree, if fgEntryBB is a loop header.
//
if (fgFirstBB->bbRefs > 1)
{
JITDUMP(" Method entry " FMT_BB " is loop head, can't check entry/exit balance\n");
}
else if (!fgProfileWeightsConsistent(entryWeight, exitWeight))
{
problemBlocks++;
JITDUMP(" Method entry " FMT_WT " method exit " FMT_WT " weight mismatch\n", entryWeight, exitWeight);
Expand Down Expand Up @@ -5025,18 +5108,19 @@ void Compiler::fgDebugCheckProfileWeights()
// block matches the profile weight of the block.
//
// Arguments:
// block - block to check
// block - block to check
// checks - checker options
//
// Returns:
// true if counts consistent or checking disabled, false otherwise.
//
// Notes:
// Only useful to call on blocks with predecessors.
//
bool Compiler::fgDebugCheckIncomingProfileData(BasicBlock* block)
bool Compiler::fgDebugCheckIncomingProfileData(BasicBlock* block, ProfileChecks checks)
{
const bool verifyClassicWeights = fgEdgeWeightsComputed && (JitConfig.JitProfileChecks() & 0x1) == 0x1;
const bool verifyLikelyWeights = (JitConfig.JitProfileChecks() & 0x2) == 0x2;
const bool verifyClassicWeights = fgEdgeWeightsComputed && hasFlag(checks, ProfileChecks::CHECK_CLASSIC);
const bool verifyLikelyWeights = hasFlag(checks, ProfileChecks::CHECK_LIKELY);

if (!(verifyClassicWeights || verifyLikelyWeights))
{
Expand All @@ -5056,7 +5140,10 @@ bool Compiler::fgDebugCheckIncomingProfileData(BasicBlock* block)
incomingWeightMax += predEdge->edgeWeightMax();
if (predEdge->hasLikelihood())
{
incomingLikelyWeight += predEdge->getLikelyWeight();
if (BasicBlock::sameHndRegion(block, predEdge->getSourceBlock()))
{
incomingLikelyWeight += predEdge->getLikelyWeight();
}
}
else
{
Expand Down Expand Up @@ -5122,17 +5209,18 @@ bool Compiler::fgDebugCheckIncomingProfileData(BasicBlock* block)
//
// Arguments:
// block - block to check
// checks - checker options
//
// Returns:
// true if counts consistent or checking disabled, false otherwise.
//
// Notes:
// Only useful to call on blocks with successors.
//
bool Compiler::fgDebugCheckOutgoingProfileData(BasicBlock* block)
bool Compiler::fgDebugCheckOutgoingProfileData(BasicBlock* block, ProfileChecks checks)
{
const bool verifyClassicWeights = fgEdgeWeightsComputed && (JitConfig.JitProfileChecks() & 0x1) == 0x1;
const bool verifyLikelyWeights = (JitConfig.JitProfileChecks() & 0x2) == 0x2;
const bool verifyClassicWeights = fgEdgeWeightsComputed && hasFlag(checks, ProfileChecks::CHECK_CLASSIC);
const bool verifyLikelyWeights = hasFlag(checks, ProfileChecks::CHECK_LIKELY);

if (!(verifyClassicWeights || verifyLikelyWeights))
{
Expand Down
Loading