Skip to content

Commit ccd8c3d

Browse files
JIT: Ensure BBF_PROF_WEIGHT flag is set when we have PGO data (#111780)
Part of #107749. Prerequisite to #111764 (comment). Add a post-phase check that ensures BBF_PROF_WEIGHT is set for every reachable block when we have profile data. This check revealed only one site -- when incorporating an inlinee's blocks -- where we couldn't rely on normal flow propagation to set the flag.
1 parent 03b2d3d commit ccd8c3d

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/coreclr/jit/compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4571,7 +4571,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
45714571
// Note: the importer is sensitive to block weights, so this has
45724572
// to happen before importation.
45734573
//
4574-
activePhaseChecks |= PhaseChecks::CHECK_PROFILE;
4574+
activePhaseChecks |= PhaseChecks::CHECK_PROFILE | PhaseChecks::CHECK_PROFILE_FLAGS;
45754575
DoPhase(this, PHASE_INCPROFILE, &Compiler::fgIncorporateProfileData);
45764576

45774577
activePhaseChecks |= PhaseChecks::CHECK_FG_INIT_BLOCK;

src/coreclr/jit/compiler.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,8 +1542,9 @@ enum class PhaseChecks : unsigned int
15421542
CHECK_LOOPS = 1 << 4, // loop integrity/canonicalization
15431543
CHECK_LIKELIHOODS = 1 << 5, // profile data likelihood integrity
15441544
CHECK_PROFILE = 1 << 6, // profile data full integrity
1545-
CHECK_LINKED_LOCALS = 1 << 7, // check linked list of locals
1546-
CHECK_FG_INIT_BLOCK = 1 << 8, // flow graph has an init block
1545+
CHECK_PROFILE_FLAGS = 1 << 7, // blocks with profile-derived weights have BBF_PROF_WEIGHT flag set
1546+
CHECK_LINKED_LOCALS = 1 << 8, // check linked list of locals
1547+
CHECK_FG_INIT_BLOCK = 1 << 9, // flow graph has an init block
15471548
};
15481549

15491550
inline constexpr PhaseChecks operator ~(PhaseChecks a)
@@ -1608,8 +1609,9 @@ enum class ProfileChecks : unsigned int
16081609
CHECK_HASLIKELIHOOD = 1 << 0, // check all FlowEdges for hasLikelihood
16091610
CHECK_LIKELIHOODSUM = 1 << 1, // check block succesor likelihoods sum to 1
16101611
CHECK_LIKELY = 1 << 2, // fully check likelihood based weights
1611-
RAISE_ASSERT = 1 << 3, // assert on check failure
1612-
CHECK_ALL_BLOCKS = 1 << 4, // check blocks even if bbHasProfileWeight is false
1612+
CHECK_FLAGS = 1 << 3, // check blocks with profile-derived weights have BBF_PROF_WEIGHT flag set
1613+
RAISE_ASSERT = 1 << 4, // assert on check failure
1614+
CHECK_ALL_BLOCKS = 1 << 5, // check blocks even if bbHasProfileWeight is false
16131615
};
16141616

16151617
inline constexpr ProfileChecks operator ~(ProfileChecks a)

src/coreclr/jit/fginline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1515,7 +1515,7 @@ void Compiler::fgInsertInlineeBlocks(InlineInfo* pInlineInfo)
15151515
noway_assert(!block->hasTryIndex());
15161516
noway_assert(!block->hasHndIndex());
15171517
block->copyEHRegion(iciBlock);
1518-
block->CopyFlags(iciBlock, BBF_BACKWARD_JUMP);
1518+
block->CopyFlags(iciBlock, BBF_BACKWARD_JUMP | BBF_PROF_WEIGHT);
15191519

15201520
// Update block nums appropriately
15211521
//

src/coreclr/jit/fgprofile.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4571,6 +4571,12 @@ void Compiler::fgDebugCheckProfile(PhaseChecks checks)
45714571
else if (hasFlag(checks, PhaseChecks::CHECK_PROFILE))
45724572
{
45734573
ProfileChecks profileChecks = ProfileChecks::CHECK_LIKELY | ProfileChecks::RAISE_ASSERT;
4574+
4575+
if (hasFlag(checks, PhaseChecks::CHECK_PROFILE_FLAGS))
4576+
{
4577+
profileChecks |= ProfileChecks::CHECK_FLAGS;
4578+
}
4579+
45744580
fgDebugCheckProfileWeights(profileChecks);
45754581
}
45764582
else if (hasFlag(checks, PhaseChecks::CHECK_LIKELIHOODS))
@@ -4611,6 +4617,7 @@ bool Compiler::fgDebugCheckProfileWeights(ProfileChecks checks)
46114617
const bool verifyLikelyWeights = hasFlag(checks, ProfileChecks::CHECK_LIKELY);
46124618
const bool verifyHasLikelihood = hasFlag(checks, ProfileChecks::CHECK_HASLIKELIHOOD);
46134619
const bool verifyLikelihoodSum = hasFlag(checks, ProfileChecks::CHECK_LIKELIHOODSUM);
4620+
const bool checkProfileFlag = hasFlag(checks, ProfileChecks::CHECK_FLAGS) && fgIsUsingProfileWeights();
46144621
const bool assertOnFailure = hasFlag(checks, ProfileChecks::RAISE_ASSERT) && fgPgoConsistent;
46154622
const bool checkAllBlocks = hasFlag(checks, ProfileChecks::CHECK_ALL_BLOCKS);
46164623

@@ -4633,6 +4640,7 @@ bool Compiler::fgDebugCheckProfileWeights(ProfileChecks checks)
46334640
unsigned problemBlocks = 0;
46344641
unsigned unprofiledBlocks = 0;
46354642
unsigned profiledBlocks = 0;
4643+
unsigned unflaggedBlocks = 0;
46364644
bool entryProfiled = false;
46374645
bool exitProfiled = false;
46384646
bool hasTry = false;
@@ -4643,10 +4651,20 @@ bool Compiler::fgDebugCheckProfileWeights(ProfileChecks checks)
46434651
//
46444652
for (BasicBlock* const block : Blocks())
46454653
{
4646-
if (!block->hasProfileWeight() && !checkAllBlocks)
4654+
if (!block->hasProfileWeight())
46474655
{
4648-
unprofiledBlocks++;
4649-
continue;
4656+
if (checkProfileFlag && (block->bbPreds != nullptr))
4657+
{
4658+
// Block has flow into it that isn't marked as profile-derived.
4659+
//
4660+
unflaggedBlocks++;
4661+
}
4662+
4663+
if (!checkAllBlocks)
4664+
{
4665+
unprofiledBlocks++;
4666+
continue;
4667+
}
46504668
}
46514669

46524670
// There is some profile data to check.
@@ -4831,6 +4849,12 @@ bool Compiler::fgDebugCheckProfileWeights(ProfileChecks checks)
48314849
}
48324850
}
48334851

4852+
if (unflaggedBlocks > 0)
4853+
{
4854+
JITDUMP("%d blocks are missing BBF_PROF_WEIGHT flag.\n", unflaggedBlocks);
4855+
assert(!"Missing BBF_PROF_WEIGHT flag");
4856+
}
4857+
48344858
return (problemBlocks == 0);
48354859
}
48364860

0 commit comments

Comments
 (0)