Skip to content

Commit 5b96528

Browse files
JIT: Consider block weights instead of BBF_RUN_RARELY flag in fgMoveColdBlocks (#103492)
Based on feedback in #102763 (comment), define "cold" blocks based on whether their weights are below a certain threshold, rather than only considering blocks marked with BBF_RUN_RARELY, in fgMoveColdBlocks. I added a BasicBlock method for doing this weight check rather than localizing it to fgMoveColdBlocks, as I plan to use it elsewhere in the layout phase.
1 parent eb6f1ae commit 5b96528

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/coreclr/jit/block.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,7 @@ struct BasicBlock : private LIR::Range
11631163
#define BB_UNITY_WEIGHT_UNSIGNED 100 // how much a normal execute once block weighs
11641164
#define BB_LOOP_WEIGHT_SCALE 8.0 // synthetic profile scale factor for loops
11651165
#define BB_ZERO_WEIGHT 0.0
1166+
#define BB_COLD_WEIGHT 0.01 // Upper bound for cold weights; used during block layout
11661167
#define BB_MAX_WEIGHT FLT_MAX // maximum finite weight -- needs rethinking.
11671168

11681169
weight_t bbWeight; // The dynamic execution weight of this block
@@ -1261,6 +1262,11 @@ struct BasicBlock : private LIR::Range
12611262
return (bbWeight >= BB_MAX_WEIGHT);
12621263
}
12631264

1265+
bool isBBWeightCold(Compiler* comp) const
1266+
{
1267+
return getBBWeight(comp) < BB_COLD_WEIGHT;
1268+
}
1269+
12641270
// Returns "true" if the block is empty. Empty here means there are no statement
12651271
// trees *except* PHI definitions.
12661272
bool isEmpty() const;

src/coreclr/jit/fgopt.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4912,7 +4912,8 @@ void Compiler::fgMoveColdBlocks()
49124912
// as we want to keep these pairs contiguous
49134913
// (if we encounter the end of a pair below, we'll move the whole pair).
49144914
//
4915-
if (!block->isRunRarely() || block->hasTryIndex() || block->hasHndIndex() || block->isBBCallFinallyPair())
4915+
if (!block->isBBWeightCold(this) || block->hasTryIndex() || block->hasHndIndex() ||
4916+
block->isBBCallFinallyPair())
49164917
{
49174918
continue;
49184919
}
@@ -4937,7 +4938,7 @@ void Compiler::fgMoveColdBlocks()
49374938
// We have moved all cold main blocks before lastMainBB to after lastMainBB.
49384939
// If lastMainBB itself is cold, move it to the end of the method to restore its relative ordering.
49394940
//
4940-
if (lastMainBB->isRunRarely())
4941+
if (lastMainBB->isBBWeightCold(this))
49414942
{
49424943
BasicBlock* const newLastMainBB = this->fgLastBBInMainFunction();
49434944
if (lastMainBB != newLastMainBB)
@@ -4991,13 +4992,14 @@ void Compiler::fgMoveColdBlocks()
49914992
{
49924993
prev = block->Prev();
49934994

4994-
// Only consider rarely-run blocks in try regions.
4995+
// Only consider cold blocks in try regions.
49954996
// If we have such a block that is also part of an exception handler, don't bother moving it.
49964997
// Finally, don't move block if it is the beginning of a call-finally pair,
49974998
// as we want to keep these pairs contiguous
49984999
// (if we encounter the end of a pair below, we'll move the whole pair).
49995000
//
5000-
if (!block->hasTryIndex() || !block->isRunRarely() || block->hasHndIndex() || block->isBBCallFinallyPair())
5001+
if (!block->isBBWeightCold(this) || !block->hasTryIndex() || block->hasHndIndex() ||
5002+
block->isBBCallFinallyPair())
50015003
{
50025004
continue;
50035005
}
@@ -5008,7 +5010,7 @@ void Compiler::fgMoveColdBlocks()
50085010
// Don't move the beginning of a try region.
50095011
// Also, if this try region's entry is cold, don't bother moving its blocks.
50105012
//
5011-
if ((HBtab->ebdTryBeg == block) || (HBtab->ebdTryBeg->isRunRarely()))
5013+
if ((HBtab->ebdTryBeg == block) || HBtab->ebdTryBeg->isBBWeightCold(this))
50125014
{
50135015
continue;
50145016
}
@@ -5069,7 +5071,7 @@ void Compiler::fgMoveColdBlocks()
50695071
// We moved cold blocks to the end of this try region, but the old end block is cold, too.
50705072
// Move the old end block to the end of the region to preserve its relative ordering.
50715073
//
5072-
if ((tryEnd != newTryEnd) && tryEnd->isRunRarely() && !tryEnd->hasHndIndex())
5074+
if ((tryEnd != newTryEnd) && !tryEnd->hasHndIndex() && tryEnd->isBBWeightCold(this))
50735075
{
50745076
BasicBlock* const prev = tryEnd->Prev();
50755077
fgUnlinkBlock(tryEnd);

0 commit comments

Comments
 (0)