Skip to content

JIT: Compute fgCalledCount earlier #110693

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

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 2 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -6233,7 +6233,7 @@ class Compiler
#endif
PhaseStatus fgComputeBlockWeights();
bool fgComputeMissingBlockWeights(weight_t* returnWeight);
bool fgComputeCalledCount(weight_t returnWeight);
void fgComputeCalledCount();

bool fgReorderBlocks(bool useProfile);
void fgDoReversePostOrderLayout();
Expand Down Expand Up @@ -6491,6 +6491,7 @@ class Compiler
PhaseStatus fgPrepareToInstrumentMethod();
PhaseStatus fgInstrumentMethod();
PhaseStatus fgIncorporateProfileData();
bool fgIncorporateProfileCounts();
bool fgIncorporateBlockCounts();
bool fgIncorporateEdgeCounts();

Expand Down
101 changes: 35 additions & 66 deletions src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2772,6 +2772,30 @@ PhaseStatus Compiler::fgInstrumentMethod()
// appropriate phase status
//
PhaseStatus Compiler::fgIncorporateProfileData()
{
bool incorporatedAny = fgIncorporateProfileCounts();

if (!incorporatedAny)
{
return PhaseStatus::MODIFIED_NOTHING;
}

if (!compIsForInlining() && fgIsUsingProfileWeights())
{
fgComputeCalledCount();
}

return PhaseStatus::MODIFIED_EVERYTHING;
}

//------------------------------------------------------------------------
// fgIncorporateProfileCounts: add block/edge profile data to the flowgraph
// and compute profile scale for inlinees
//
// Returns:
// True if any counts were incorporated, otherwise false.
//
bool Compiler::fgIncorporateProfileCounts()
{
// Are we doing profile stress?
//
Expand All @@ -2781,15 +2805,15 @@ PhaseStatus Compiler::fgIncorporateProfileData()
fgIncorporateBlockCounts();
ProfileSynthesis::Run(this, ProfileSynthesisOption::RepairLikelihoods);
fgApplyProfileScale();
return PhaseStatus::MODIFIED_EVERYTHING;
return true;
}

// For now we only rely on profile data when optimizing.
//
if (!opts.OptimizationEnabled())
{
JITDUMP("not optimizing, so not incorporating any profile data\n");
return PhaseStatus::MODIFIED_NOTHING;
return false;
}

#ifdef DEBUG
Expand All @@ -2802,7 +2826,7 @@ PhaseStatus Compiler::fgIncorporateProfileData()
JITDUMP("Synthesizing profile data\n");
ProfileSynthesis::Run(this, ProfileSynthesisOption::AssignLikelihoods);
fgApplyProfileScale();
return PhaseStatus::MODIFIED_EVERYTHING;
return true;
}
}

Expand All @@ -2814,7 +2838,7 @@ PhaseStatus Compiler::fgIncorporateProfileData()
JITDUMP("Synthesizing profile data and writing it out as the actual profile data\n");
ProfileSynthesis::Run(this, ProfileSynthesisOption::AssignLikelihoods);
fgApplyProfileScale();
return PhaseStatus::MODIFIED_EVERYTHING;
return true;
}
#endif

Expand Down Expand Up @@ -2845,7 +2869,7 @@ PhaseStatus Compiler::fgIncorporateProfileData()
//
fgApplyProfileScale();

return compIsForInlining() ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
return compIsForInlining();
}

// Summarize profile data
Expand Down Expand Up @@ -2960,7 +2984,7 @@ PhaseStatus Compiler::fgIncorporateProfileData()
//
fgApplyProfileScale();

return PhaseStatus::MODIFIED_EVERYTHING;
return true;
}

//------------------------------------------------------------------------
Expand Down Expand Up @@ -4242,15 +4266,6 @@ PhaseStatus Compiler::fgComputeBlockWeights()

madeChanges |= fgComputeMissingBlockWeights(&returnWeight);

if (usingProfileWeights)
{
madeChanges |= fgComputeCalledCount(returnWeight);
}
else
{
JITDUMP(" -- no profile data, so using default called count\n");
}

return madeChanges ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
}

Expand Down Expand Up @@ -4424,70 +4439,24 @@ bool Compiler::fgComputeMissingBlockWeights(weight_t* returnWeight)
// fgComputeCalledCount: when profile information is in use,
// compute fgCalledCount
//
// Argument:
// returnWeight - sum of weights for all return and throw blocks
//
// Returns:
// true if any changes were made
//
bool Compiler::fgComputeCalledCount(weight_t returnWeight)
void Compiler::fgComputeCalledCount()
{
// When we are not using profile data we have already setup fgCalledCount
// only set it here if we are using profile data
assert(fgIsUsingProfileWeights());
bool madeChanges = false;

BasicBlock* firstILBlock = fgFirstBB; // The first block for IL code (i.e. for the IL code at offset 0)

// OSR methods can have complex entry flow, and so
// for OSR we ensure fgFirstBB has plausible profile data.
//
if (!opts.IsOSR())
fgCalledCount = fgFirstBB->bbWeight;
for (FlowEdge* predEdge : fgFirstBB->PredEdges())
{
// Skip past any/all BBF_INTERNAL blocks that may have been added before the first real IL block.
//
while (firstILBlock->HasFlag(BBF_INTERNAL))
{
firstILBlock = firstILBlock->Next();
}
}

// The 'firstILBlock' is now expected to have a profile-derived weight
assert(firstILBlock->hasProfileWeight());

// If the first block only has one ref then we use its weight for fgCalledCount.
// Otherwise we have backedges into the first block, so instead we use the sum
// of the return block weights for fgCalledCount.
//
// If the profile data has a 0 for the returnWeight
// (i.e. the function never returns because it always throws)
// then just use the first block weight rather than 0.
//
if ((firstILBlock->countOfInEdges() == 1) || (returnWeight == BB_ZERO_WEIGHT))
{
fgCalledCount = firstILBlock->bbWeight;
}
else
{
fgCalledCount = returnWeight;
}

// If we allocated a scratch block as the first BB then we need
// to set its profile-derived weight to be fgCalledCount
if (fgFirstBB->HasFlag(BBF_INTERNAL))
{
fgFirstBB->setBBProfileWeight(fgCalledCount);
madeChanges = true;
fgCalledCount -= predEdge->getSourceBlock()->bbWeight * predEdge->getLikelihood();
}

#if DEBUG
if (verbose)
{
printf("We are using the Profile Weights and fgCalledCount is " FMT_WT "\n", fgCalledCount);
printf("fgCalledCount is " FMT_WT "\n", fgCalledCount);
}
#endif

return madeChanges;
}

//------------------------------------------------------------------------
Expand Down
Loading