Skip to content

Commit 18ae5dd

Browse files
JIT: Remove BBF_RUN_RARELY (#118147)
1 parent 2bbb5f1 commit 18ae5dd

File tree

9 files changed

+59
-192
lines changed

9 files changed

+59
-192
lines changed

src/coreclr/jit/async.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,10 +1730,8 @@ BasicBlock* AsyncTransformation::CreateResumption(BasicBlock* bloc
17301730
BasicBlock* resumeBB = m_comp->fgNewBBafter(BBJ_ALWAYS, m_lastResumptionBB, true);
17311731
FlowEdge* remainderEdge = m_comp->fgAddRefPred(remainder, resumeBB);
17321732

1733-
// It does not really make sense to inherit from the target, but given this
1734-
// is always 0% this just propagates the profile weight flag + sets
1735-
// BBF_RUN_RARELY.
1736-
resumeBB->inheritWeightPercentage(remainder, 0);
1733+
resumeBB->bbSetRunRarely();
1734+
resumeBB->CopyFlags(remainder, BBF_PROF_WEIGHT);
17371735
resumeBB->SetTargetEdge(remainderEdge);
17381736
resumeBB->clearTryIndex();
17391737
resumeBB->clearHndIndex();

src/coreclr/jit/block.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ void BasicBlock::dspFlags() const
491491
{BBF_IMPORTED, "i"},
492492
{BBF_IS_LIR, "LIR"},
493493
{BBF_PROF_WEIGHT, "IBC"},
494-
{BBF_RUN_RARELY, "rare"},
495494
{BBF_MARKED, "m"},
496495
{BBF_REMOVED, "del"},
497496
{BBF_DONT_REMOVE, "keep"},

src/coreclr/jit/block.h

Lines changed: 41 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -415,37 +415,36 @@ enum BasicBlockFlags : uint64_t
415415
BBF_CLONED_FINALLY_BEGIN = MAKE_BBFLAG( 7), // First block of a cloned finally region
416416
BBF_CLONED_FINALLY_END = MAKE_BBFLAG( 8), // Last block of a cloned finally region
417417
BBF_HAS_SUPPRESSGC_CALL = MAKE_BBFLAG( 9), // BB contains a call to a method with SuppressGCTransitionAttribute
418-
BBF_RUN_RARELY = MAKE_BBFLAG(10), // BB is rarely run (catch clauses, blocks with throws etc)
419-
BBF_HAS_LABEL = MAKE_BBFLAG(11), // BB needs a label
420-
BBF_LOOP_ALIGN = MAKE_BBFLAG(12), // Block is lexically the first block in a loop we intend to align.
421-
BBF_HAS_ALIGN = MAKE_BBFLAG(13), // BB ends with 'align' instruction
422-
BBF_HAS_JMP = MAKE_BBFLAG(14), // BB executes a JMP instruction (instead of return)
423-
BBF_GC_SAFE_POINT = MAKE_BBFLAG(15), // BB has a GC safe point (e.g. a call)
424-
BBF_HAS_MDARRAYREF = MAKE_BBFLAG(16), // Block has a multi-dimensional array reference
425-
BBF_HAS_NEWOBJ = MAKE_BBFLAG(17), // BB contains 'new' of an object type.
426-
427-
BBF_RETLESS_CALL = MAKE_BBFLAG(18), // BBJ_CALLFINALLY that will never return (and therefore, won't need a paired
418+
BBF_HAS_LABEL = MAKE_BBFLAG(10), // BB needs a label
419+
BBF_LOOP_ALIGN = MAKE_BBFLAG(11), // Block is lexically the first block in a loop we intend to align.
420+
BBF_HAS_ALIGN = MAKE_BBFLAG(12), // BB ends with 'align' instruction
421+
BBF_HAS_JMP = MAKE_BBFLAG(13), // BB executes a JMP instruction (instead of return)
422+
BBF_GC_SAFE_POINT = MAKE_BBFLAG(14), // BB has a GC safe point (e.g. a call)
423+
BBF_HAS_MDARRAYREF = MAKE_BBFLAG(15), // Block has a multi-dimensional array reference
424+
BBF_HAS_NEWOBJ = MAKE_BBFLAG(16), // BB contains 'new' of an object type.
425+
426+
BBF_RETLESS_CALL = MAKE_BBFLAG(17), // BBJ_CALLFINALLY that will never return (and therefore, won't need a paired
428427
// BBJ_CALLFINALLYRET); see isBBCallFinallyPair().
429-
BBF_COLD = MAKE_BBFLAG(19), // BB is cold
430-
BBF_PROF_WEIGHT = MAKE_BBFLAG(20), // BB weight is computed from profile data
431-
BBF_KEEP_BBJ_ALWAYS = MAKE_BBFLAG(21), // A special BBJ_ALWAYS block, used by EH code generation. Keep the jump kind
428+
BBF_COLD = MAKE_BBFLAG(18), // BB is cold
429+
BBF_PROF_WEIGHT = MAKE_BBFLAG(19), // BB weight is computed from profile data
430+
BBF_KEEP_BBJ_ALWAYS = MAKE_BBFLAG(20), // A special BBJ_ALWAYS block, used by EH code generation. Keep the jump kind
432431
// as BBJ_ALWAYS. Used on x86 for the final step block out of a finally.
433-
BBF_HAS_CALL = MAKE_BBFLAG(22), // BB contains a call
434-
BBF_DOMINATED_BY_EXCEPTIONAL_ENTRY = MAKE_BBFLAG(23), // Block is dominated by exceptional entry.
435-
BBF_BACKWARD_JUMP = MAKE_BBFLAG(24), // BB is surrounded by a backward jump/switch arc
436-
BBF_BACKWARD_JUMP_SOURCE = MAKE_BBFLAG(25), // Block is a source of a backward jump
437-
BBF_BACKWARD_JUMP_TARGET = MAKE_BBFLAG(26), // Block is a target of a backward jump
438-
BBF_PATCHPOINT = MAKE_BBFLAG(27), // Block is a patchpoint
439-
BBF_PARTIAL_COMPILATION_PATCHPOINT = MAKE_BBFLAG(28), // Block is a partial compilation patchpoint
440-
BBF_HAS_HISTOGRAM_PROFILE = MAKE_BBFLAG(29), // BB contains a call needing a histogram profile
441-
BBF_TAILCALL_SUCCESSOR = MAKE_BBFLAG(30), // BB has pred that has potential tail call
442-
BBF_RECURSIVE_TAILCALL = MAKE_BBFLAG(31), // Block has recursive tailcall that may turn into a loop
443-
BBF_NO_CSE_IN = MAKE_BBFLAG(32), // Block should kill off any incoming CSE
444-
BBF_CAN_ADD_PRED = MAKE_BBFLAG(33), // Ok to add pred edge to this block, even when "safe" edge creation disabled
445-
BBF_HAS_VALUE_PROFILE = MAKE_BBFLAG(34), // Block has a node that needs a value probing
446-
BBF_HAS_NEWARR = MAKE_BBFLAG(35), // BB contains 'new' of an array type.
447-
BBF_MAY_HAVE_BOUNDS_CHECKS = MAKE_BBFLAG(36), // BB *likely* has a bounds check (after rangecheck phase).
448-
BBF_ASYNC_RESUMPTION = MAKE_BBFLAG(37), // Block is a resumption block in an async method
432+
BBF_HAS_CALL = MAKE_BBFLAG(21), // BB contains a call
433+
BBF_DOMINATED_BY_EXCEPTIONAL_ENTRY = MAKE_BBFLAG(22), // Block is dominated by exceptional entry.
434+
BBF_BACKWARD_JUMP = MAKE_BBFLAG(23), // BB is surrounded by a backward jump/switch arc
435+
BBF_BACKWARD_JUMP_SOURCE = MAKE_BBFLAG(24), // Block is a source of a backward jump
436+
BBF_BACKWARD_JUMP_TARGET = MAKE_BBFLAG(25), // Block is a target of a backward jump
437+
BBF_PATCHPOINT = MAKE_BBFLAG(26), // Block is a patchpoint
438+
BBF_PARTIAL_COMPILATION_PATCHPOINT = MAKE_BBFLAG(27), // Block is a partial compilation patchpoint
439+
BBF_HAS_HISTOGRAM_PROFILE = MAKE_BBFLAG(28), // BB contains a call needing a histogram profile
440+
BBF_TAILCALL_SUCCESSOR = MAKE_BBFLAG(29), // BB has pred that has potential tail call
441+
BBF_RECURSIVE_TAILCALL = MAKE_BBFLAG(30), // Block has recursive tailcall that may turn into a loop
442+
BBF_NO_CSE_IN = MAKE_BBFLAG(31), // Block should kill off any incoming CSE
443+
BBF_CAN_ADD_PRED = MAKE_BBFLAG(32), // Ok to add pred edge to this block, even when "safe" edge creation disabled
444+
BBF_HAS_VALUE_PROFILE = MAKE_BBFLAG(33), // Block has a node that needs a value probing
445+
BBF_HAS_NEWARR = MAKE_BBFLAG(34), // BB contains 'new' of an array type.
446+
BBF_MAY_HAVE_BOUNDS_CHECKS = MAKE_BBFLAG(35), // BB *likely* has a bounds check (after rangecheck phase).
447+
BBF_ASYNC_RESUMPTION = MAKE_BBFLAG(36), // Block is a resumption block in an async method
449448

450449
// The following are sets of flags.
451450

@@ -468,7 +467,6 @@ enum BasicBlockFlags : uint64_t
468467
// Flags gained by the bottom block when a block is split.
469468
// Note, this is a conservative guess.
470469
// For example, the bottom block might or might not have BBF_HAS_NEWARR, but we assume it has BBF_HAS_NEWARR.
471-
// TODO: Should BBF_RUN_RARELY be added to BBF_SPLIT_GAINED ?
472470

473471
BBF_SPLIT_GAINED = BBF_DONT_REMOVE | BBF_HAS_JMP | BBF_BACKWARD_JUMP | BBF_PROF_WEIGHT | BBF_HAS_NEWARR | \
474472
BBF_HAS_NEWOBJ | BBF_KEEP_BBJ_ALWAYS | BBF_CLONED_FINALLY_END | BBF_HAS_HISTOGRAM_PROFILE | BBF_HAS_VALUE_PROFILE | BBF_HAS_MDARRAYREF | BBF_NEEDS_GCPOLL | BBF_MAY_HAVE_BOUNDS_CHECKS | BBF_ASYNC_RESUMPTION,
@@ -1169,9 +1167,16 @@ struct BasicBlock : private LIR::Range
11691167
unsigned bbRefs; // number of blocks that can reach here, either by fall-through or a branch. If this falls to zero,
11701168
// the block is unreachable.
11711169

1170+
#define BB_UNITY_WEIGHT 100.0 // how much a normal execute once block weighs
1171+
#define BB_UNITY_WEIGHT_UNSIGNED 100 // how much a normal execute once block weighs
1172+
#define BB_LOOP_WEIGHT_SCALE 8.0 // synthetic profile scale factor for loops
1173+
#define BB_ZERO_WEIGHT 0.0
1174+
#define BB_COLD_WEIGHT 0.01 // Upper bound for cold weights; used during block layout
1175+
#define BB_MAX_WEIGHT FLT_MAX // maximum finite weight -- needs rethinking.
1176+
11721177
bool isRunRarely() const
11731178
{
1174-
return HasFlag(BBF_RUN_RARELY);
1179+
return (bbWeight == BB_ZERO_WEIGHT);
11751180
}
11761181

11771182
bool isLoopAlign() const
@@ -1196,13 +1201,6 @@ struct BasicBlock : private LIR::Range
11961201
const char* dspToString(int blockNumPadding = 0) const;
11971202
#endif // DEBUG
11981203

1199-
#define BB_UNITY_WEIGHT 100.0 // how much a normal execute once block weighs
1200-
#define BB_UNITY_WEIGHT_UNSIGNED 100 // how much a normal execute once block weighs
1201-
#define BB_LOOP_WEIGHT_SCALE 8.0 // synthetic profile scale factor for loops
1202-
#define BB_ZERO_WEIGHT 0.0
1203-
#define BB_COLD_WEIGHT 0.01 // Upper bound for cold weights; used during block layout
1204-
#define BB_MAX_WEIGHT FLT_MAX // maximum finite weight -- needs rethinking.
1205-
12061204
weight_t bbWeight; // The dynamic execution weight of this block
12071205

12081206
// getCalledCount -- get the value used to normalize weights for this method
@@ -1235,15 +1233,6 @@ struct BasicBlock : private LIR::Range
12351233
{
12361234
this->SetFlags(BBF_PROF_WEIGHT);
12371235
this->bbWeight = weight;
1238-
1239-
if (weight == BB_ZERO_WEIGHT)
1240-
{
1241-
this->SetFlags(BBF_RUN_RARELY);
1242-
}
1243-
else
1244-
{
1245-
this->RemoveFlags(BBF_RUN_RARELY);
1246-
}
12471236
}
12481237

12491238
// increaseBBProfileWeight -- Increase the profile-derived weight for a basic block
@@ -1278,44 +1267,20 @@ struct BasicBlock : private LIR::Range
12781267
{
12791268
assert(0 <= percentage && percentage <= 100);
12801269

1281-
this->bbWeight = (bSrc->bbWeight * percentage) / 100;
1282-
1283-
if (bSrc->hasProfileWeight())
1284-
{
1285-
this->SetFlags(BBF_PROF_WEIGHT);
1286-
}
1287-
else
1288-
{
1289-
this->RemoveFlags(BBF_PROF_WEIGHT);
1290-
}
1291-
1292-
if (this->bbWeight == BB_ZERO_WEIGHT)
1293-
{
1294-
this->SetFlags(BBF_RUN_RARELY);
1295-
}
1296-
else
1297-
{
1298-
this->RemoveFlags(BBF_RUN_RARELY);
1299-
}
1270+
this->bbWeight = (bSrc->bbWeight * percentage) / 100;
1271+
const BasicBlockFlags hasProfileWeight = bSrc->GetFlagsRaw() & BBF_PROF_WEIGHT;
1272+
this->RemoveFlags(BBF_PROF_WEIGHT);
1273+
this->SetFlags(hasProfileWeight);
13001274
}
13011275

13021276
// Scale a blocks' weight by some factor.
13031277
//
13041278
void scaleBBWeight(weight_t scale)
13051279
{
13061280
this->bbWeight = this->bbWeight * scale;
1307-
1308-
if (this->bbWeight == BB_ZERO_WEIGHT)
1309-
{
1310-
this->SetFlags(BBF_RUN_RARELY);
1311-
}
1312-
else
1313-
{
1314-
this->RemoveFlags(BBF_RUN_RARELY);
1315-
}
13161281
}
13171282

1318-
// Set block weight to zero, and set run rarely flag.
1283+
// Set block weight to zero.
13191284
//
13201285
void bbSetRunRarely()
13211286
{
@@ -1808,71 +1773,6 @@ struct BasicBlock : private LIR::Range
18081773
{
18091774
}
18101775

1811-
// Iteratable collection of successors of a block.
1812-
template <typename TPosition>
1813-
class Successors
1814-
{
1815-
Compiler* m_comp;
1816-
BasicBlock* m_block;
1817-
1818-
public:
1819-
Successors(Compiler* comp, BasicBlock* block)
1820-
: m_comp(comp)
1821-
, m_block(block)
1822-
{
1823-
}
1824-
1825-
class iterator
1826-
{
1827-
Compiler* m_comp;
1828-
BasicBlock* m_block;
1829-
TPosition m_pos;
1830-
1831-
public:
1832-
iterator(Compiler* comp, BasicBlock* block)
1833-
: m_comp(comp)
1834-
, m_block(block)
1835-
, m_pos(comp, block)
1836-
{
1837-
}
1838-
1839-
iterator()
1840-
: m_pos()
1841-
{
1842-
}
1843-
1844-
void operator++(void)
1845-
{
1846-
m_pos.Advance(m_comp, m_block);
1847-
}
1848-
1849-
BasicBlock* operator*()
1850-
{
1851-
return m_pos.Current(m_comp, m_block);
1852-
}
1853-
1854-
bool operator==(const iterator& other)
1855-
{
1856-
return m_pos == other.m_pos;
1857-
}
1858-
1859-
bool operator!=(const iterator& other)
1860-
{
1861-
return m_pos != other.m_pos;
1862-
}
1863-
};
1864-
1865-
iterator begin()
1866-
{
1867-
return iterator(m_comp, m_block);
1868-
}
1869-
1870-
iterator end()
1871-
{
1872-
return iterator();
1873-
}
1874-
};
1875-
18761776
template <typename TFunc>
18771777
BasicBlockVisit VisitEHEnclosedHandlerSecondPassSuccs(Compiler* comp, TFunc func);
18781778

src/coreclr/jit/fgdiagnostic.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -828,7 +828,7 @@ bool Compiler::fgDumpFlowGraph(Phases phase, PhasePosition pos)
828828
const bool isTryEntryBlock = bbIsTryBeg(block);
829829
const bool isFuncletEntryBlock = fgFuncletsCreated && bbIsFuncletBeg(block);
830830

831-
if (isTryEntryBlock || isFuncletEntryBlock || block->HasAnyFlag(BBF_RUN_RARELY | BBF_LOOP_ALIGN))
831+
if (isTryEntryBlock || isFuncletEntryBlock || block->HasFlag(BBF_LOOP_ALIGN))
832832
{
833833
// Display a very few, useful, block flags
834834
fprintf(fgxFile, " [");
@@ -840,10 +840,6 @@ bool Compiler::fgDumpFlowGraph(Phases phase, PhasePosition pos)
840840
{
841841
fprintf(fgxFile, "F");
842842
}
843-
if (block->HasFlag(BBF_RUN_RARELY))
844-
{
845-
fprintf(fgxFile, "R");
846-
}
847843
if (block->HasFlag(BBF_LOOP_ALIGN))
848844
{
849845
fprintf(fgxFile, "A");
@@ -3133,16 +3129,6 @@ void Compiler::fgDebugCheckBBlist(bool checkBBNum /* = false */, bool checkBBRef
31333129
}
31343130
}
31353131
}
3136-
3137-
/* Check if BBF_RUN_RARELY is set that we have bbWeight of zero */
3138-
if (block->isRunRarely())
3139-
{
3140-
assert(block->bbWeight == BB_ZERO_WEIGHT);
3141-
}
3142-
else
3143-
{
3144-
assert(block->bbWeight > BB_ZERO_WEIGHT);
3145-
}
31463132
}
31473133

31483134
assert(fgBBcount == numBlocks);
@@ -3942,6 +3928,12 @@ void Compiler::fgDebugCheckBlockLinks()
39423928
// If this is a switch, check that the tables are consistent.
39433929
if (block->KindIs(BBJ_SWITCH))
39443930
{
3931+
// Switch blocks with dominant cases must have profile-derived weights.
3932+
if (block->GetSwitchTargets()->HasDominantCase())
3933+
{
3934+
assert(block->hasProfileWeight());
3935+
}
3936+
39453937
// Create a set with all the successors.
39463938
BitVecTraits bitVecTraits(fgBBNumMax + 1, this);
39473939
BitVec succBlocks(BitVecOps::MakeEmpty(&bitVecTraits));

src/coreclr/jit/fginline.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,8 +1591,8 @@ void Compiler::fgInsertInlineeBlocks(InlineInfo* pInlineInfo)
15911591
noway_assert((inlineeBlockFlags & BBF_HAS_JMP) == 0);
15921592
noway_assert((inlineeBlockFlags & BBF_KEEP_BBJ_ALWAYS) == 0);
15931593

1594-
// Todo: we may want to exclude other flags here.
1595-
iciBlock->SetFlags(inlineeBlockFlags & ~BBF_RUN_RARELY);
1594+
// Todo: we may want to exclude some flags here.
1595+
iciBlock->SetFlags(inlineeBlockFlags);
15961596

15971597
#ifdef DEBUG
15981598
if (verbose)

src/coreclr/jit/fgopt.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,11 +2538,11 @@ bool Compiler::fgOptimizeBranch(BasicBlock* bJump)
25382538
if (fgIsUsingProfileWeights())
25392539
{
25402540
// Only rely upon the profile weight when all three of these blocks
2541-
// have either good profile weights or are rarelyRun
2541+
// have either good profile weights or are rarely run
25422542
//
2543-
if (bJump->HasAnyFlag(BBF_PROF_WEIGHT | BBF_RUN_RARELY) &&
2544-
bDest->HasAnyFlag(BBF_PROF_WEIGHT | BBF_RUN_RARELY) &&
2545-
trueTarget->HasAnyFlag(BBF_PROF_WEIGHT | BBF_RUN_RARELY))
2543+
if ((bJump->hasProfileWeight() || bJump->isRunRarely()) &&
2544+
(bDest->hasProfileWeight() || bDest->isRunRarely()) &&
2545+
(trueTarget->hasProfileWeight() || trueTarget->isRunRarely()))
25462546
{
25472547
haveProfileWeights = true;
25482548

@@ -3132,7 +3132,6 @@ bool Compiler::fgExpandRarelyRunBlocks()
31323132
// Set the BBJ_CALLFINALLY block to the same weight as the BBJ_CALLFINALLYRET block and
31333133
// mark it rarely run.
31343134
bPrev->bbWeight = block->bbWeight;
3135-
bPrev->SetFlags(BBF_RUN_RARELY);
31363135
#ifdef DEBUG
31373136
if (verbose)
31383137
{
@@ -3147,7 +3146,6 @@ bool Compiler::fgExpandRarelyRunBlocks()
31473146
// Set the BBJ_CALLFINALLYRET block to the same weight as the BBJ_CALLFINALLY block and
31483147
// mark it rarely run.
31493148
block->bbWeight = bPrev->bbWeight;
3150-
block->SetFlags(BBF_RUN_RARELY);
31513149
#ifdef DEBUG
31523150
if (verbose)
31533151
{

src/coreclr/jit/fgprofile.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,14 +4345,6 @@ bool Compiler::fgComputeMissingBlockWeights()
43454345
changed = true;
43464346
modified = true;
43474347
bDst->bbWeight = newWeight;
4348-
if (newWeight == BB_ZERO_WEIGHT)
4349-
{
4350-
bDst->SetFlags(BBF_RUN_RARELY);
4351-
}
4352-
else
4353-
{
4354-
bDst->RemoveFlags(BBF_RUN_RARELY);
4355-
}
43564348
}
43574349
}
43584350
else if (!bDst->hasProfileWeight() && bbIsHandlerBeg(bDst) && !bDst->isRunRarely())

src/coreclr/jit/importer.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5078,7 +5078,6 @@ void Compiler::impImportLeave(BasicBlock* block)
50785078
}
50795079

50805080
step2->inheritWeight(block);
5081-
step2->CopyFlags(block, BBF_RUN_RARELY);
50825081
step2->SetFlags(BBF_IMPORTED);
50835082

50845083
#ifdef DEBUG
@@ -5361,10 +5360,9 @@ void Compiler::impResetLeaveBlock(BasicBlock* block, unsigned jmpAddr)
53615360
// b) weight zero
53625361
// c) prevent from being imported
53635362
// d) as internal
5364-
// e) as rarely run
5365-
dupBlock->bbRefs = 0;
5366-
dupBlock->bbWeight = BB_ZERO_WEIGHT;
5367-
dupBlock->SetFlags(BBF_IMPORTED | BBF_INTERNAL | BBF_RUN_RARELY);
5363+
dupBlock->bbRefs = 0;
5364+
dupBlock->bbSetRunRarely();
5365+
dupBlock->SetFlags(BBF_IMPORTED | BBF_INTERNAL);
53685366

53695367
// Insert the block right after the block which is getting reset so that BBJ_CALLFINALLY and BBJ_ALWAYS
53705368
// will be next to each other.

0 commit comments

Comments
 (0)