Skip to content

Commit 9c456f6

Browse files
JIT: Compute fgCalledCount after synthesis (dotnet#112041)
Part of dotnet#107749. Follow-up to dotnet#111971 and dotnet#110693. For methods without profile data, ensure the default call count is available throughout compilation (this had no diffs for me locally). For methods with profile data, compute the call count after synthesis runs to ensure it is available early, and reasonably accurate. I'm only seeing diffs in OSR methods locally, due to the logic in `fgFixEntryFlowForOSR` (which runs right after profile incorporation) no longer affecting `fgCalledCount`. This method guesses that the loop iterates about 100x the method call count, and scales the method entry block's weight down accordingly. This gives the impression later on that `fgCalledCount` is much lower than what we calculated using `fgEntryBB`. The actual diffs seem to manifest largely in LSRA, which uses `fgCalledCount` to normalize block weights, though there are a few other phases that use `BasicBlock::getBBWeight` in lieu of the raw weight as well. I think we ought to consolidate our block weight strategy at some point, especially if we have newfound faith in `fgCalledCount`. For example, instead of this check in if conversion: ``` if (m_startBlock->getBBWeight(m_comp) > BB_UNITY_WEIGHT * 1.05) ``` Perhaps we could do: ``` if (m_startBlock->bbWeight > fgCalledCount * 1.05) ``` But that's for another PR.
1 parent c693bfa commit 9c456f6

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

src/coreclr/jit/compiler.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -5400,9 +5400,9 @@ class Compiler
54005400
// - Rationalization links all nodes into linear form which is kept until
54015401
// the end of compilation. The first and last nodes are stored in the block.
54025402
NodeThreading fgNodeThreading = NodeThreading::None;
5403-
weight_t fgCalledCount = BB_ZERO_WEIGHT; // count of the number of times this method was called
5404-
// This is derived from the profile data
5405-
// or is BB_UNITY_WEIGHT when we don't have profile data
5403+
weight_t fgCalledCount = BB_UNITY_WEIGHT; // count of the number of times this method was called
5404+
// This is derived from the profile data
5405+
// or is BB_UNITY_WEIGHT when we don't have profile data
54065406

54075407
bool fgFuncletsCreated = false; // true if the funclet creation phase has been run
54085408

src/coreclr/jit/fgprofile.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -4222,19 +4222,9 @@ PhaseStatus Compiler::fgComputeBlockWeights()
42224222

42234223
if (fgIsUsingProfileWeights())
42244224
{
4225-
// Compute fgCalledCount by subtracting any non-entry flow into fgFirstBB from its weight
4226-
fgCalledCount = fgFirstBB->bbWeight;
4227-
for (FlowEdge* const predEdge : fgFirstBB->PredEdges())
4228-
{
4229-
fgCalledCount = max(BB_ZERO_WEIGHT, fgCalledCount - predEdge->getLikelyWeight());
4230-
}
4231-
4232-
JITDUMP("We are using the profile weights and fgCalledCount is " FMT_WT "\n", fgCalledCount);
42334225
return PhaseStatus::MODIFIED_NOTHING;
42344226
}
42354227

4236-
JITDUMP(" -- no profile data, so using default called count\n");
4237-
fgCalledCount = BB_UNITY_WEIGHT;
42384228
return fgComputeMissingBlockWeights() ? PhaseStatus::MODIFIED_EVERYTHING : PhaseStatus::MODIFIED_NOTHING;
42394229
}
42404230

src/coreclr/jit/fgprofilesynthesis.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ void ProfileSynthesis::Run(ProfileSynthesisOption option)
161161
m_comp->Metrics.ProfileInconsistentInitially++;
162162
}
163163

164+
// Derive the method's call count from the entry block's weight
165+
//
166+
if (m_comp->fgIsUsingProfileWeights() && !m_comp->compIsForInlining())
167+
{
168+
weight_t entryWeight = m_entryBlock->bbWeight;
169+
for (FlowEdge* const predEdge : m_entryBlock->PredEdges())
170+
{
171+
entryWeight -= predEdge->getLikelyWeight();
172+
}
173+
174+
m_comp->fgCalledCount = max(BB_ZERO_WEIGHT, entryWeight);
175+
JITDUMP("fgCalledCount is " FMT_WT "\n", m_comp->fgCalledCount);
176+
}
177+
164178
#ifdef DEBUG
165179
// We want to assert that the profile is consistent.
166180
// However, we need to defer asserting since invalid IL can

0 commit comments

Comments
 (0)