Skip to content

Commit e80bc41

Browse files
authored
JIT: add concept of edge likelihood (#81738)
Now that FlowEdges are created early and persist, decorate them with likelihood information early, if we have edge-based PGO data. We don't use the likelihood for anything yet, but I need to get it in circulation so I can start working on refining both initial and subsequent consistency of the data. Also add a diagnostic checker for likelhood, and a way to enable it. All of this is off by default.
1 parent 765e3db commit e80bc41

File tree

7 files changed

+363
-112
lines changed

7 files changed

+363
-112
lines changed

src/coreclr/jit/block.h

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,12 +1800,25 @@ struct FlowEdge
18001800
weight_t m_edgeWeightMin;
18011801
weight_t m_edgeWeightMax;
18021802

1803+
// Likelihood that m_sourceBlock transfers control along this edge.
1804+
// Values in range [0..1]
1805+
weight_t m_likelihood;
1806+
18031807
// The count of duplicate "edges" (used for switch stmts or degenerate branches)
18041808
unsigned m_dupCount;
18051809

1810+
#ifdef DEBUG
1811+
bool m_likelihoodSet;
1812+
#endif
1813+
18061814
public:
18071815
FlowEdge(BasicBlock* block, FlowEdge* rest)
1808-
: m_nextPredEdge(rest), m_sourceBlock(block), m_edgeWeightMin(0), m_edgeWeightMax(0), m_dupCount(0)
1816+
: m_nextPredEdge(rest)
1817+
, m_sourceBlock(block)
1818+
, m_edgeWeightMin(0)
1819+
, m_edgeWeightMax(0)
1820+
, m_likelihood(0)
1821+
, m_dupCount(0) DEBUGARG(m_likelihoodSet(false))
18091822
{
18101823
}
18111824

@@ -1852,6 +1865,32 @@ struct FlowEdge
18521865
bool setEdgeWeightMaxChecked(weight_t newWeight, BasicBlock* bDst, weight_t slop, bool* wbUsedSlop);
18531866
void setEdgeWeights(weight_t newMinWeight, weight_t newMaxWeight, BasicBlock* bDst);
18541867

1868+
weight_t getLikelihood() const
1869+
{
1870+
return m_likelihood;
1871+
}
1872+
1873+
void setLikelihood(weight_t likelihood)
1874+
{
1875+
assert(likelihood >= 0.0);
1876+
assert(likelihood <= 1.0);
1877+
INDEBUG(m_likelihoodSet = true);
1878+
m_likelihood = likelihood;
1879+
}
1880+
1881+
#ifdef DEBUG
1882+
bool hasLikelihood() const
1883+
{
1884+
return m_likelihoodSet;
1885+
}
1886+
#endif
1887+
1888+
weight_t getLikelyWeight() const
1889+
{
1890+
assert(m_likelihoodSet);
1891+
return m_likelihood * m_sourceBlock->bbWeight;
1892+
}
1893+
18551894
unsigned getDupCount() const
18561895
{
18571896
return m_dupCount;

src/coreclr/jit/compiler.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4434,6 +4434,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
44344434
// Note: the importer is sensitive to block weights, so this has
44354435
// to happen before importation.
44364436
//
4437+
activePhaseChecks |= PhaseChecks::CHECK_PROFILE;
44374438
DoPhase(this, PHASE_INCPROFILE, &Compiler::fgIncorporateProfileData);
44384439

44394440
// If we're going to instrument code, we may need to prepare before
@@ -4453,8 +4454,8 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
44534454

44544455
// Enable the post-phase checks that use internal logic to decide when checking makes sense.
44554456
//
4456-
activePhaseChecks |= PhaseChecks::CHECK_EH | PhaseChecks::CHECK_LOOPS | PhaseChecks::CHECK_UNIQUE |
4457-
PhaseChecks::CHECK_PROFILE | PhaseChecks::CHECK_LINKED_LOCALS;
4457+
activePhaseChecks |=
4458+
PhaseChecks::CHECK_EH | PhaseChecks::CHECK_LOOPS | PhaseChecks::CHECK_UNIQUE | PhaseChecks::CHECK_LINKED_LOCALS;
44584459

44594460
// Import: convert the instrs in each basic block to a tree based intermediate representation
44604461
//

src/coreclr/jit/fgbasic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ void Compiler::fgReplaceSwitchJumpTarget(BasicBlock* blockSwitch, BasicBlock* ne
488488
//
489489
FlowEdge* const newEdge = fgAddRefPred(newTarget, blockSwitch);
490490

491-
// Now set the correct value of newEdge's lDupCount
491+
// Now set the correct value of newEdge's DupCount
492492
// and replace any other jumps in jumpTab[] that go to oldTarget.
493493
//
494494
i++;

src/coreclr/jit/fgdiagnostic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3635,7 +3635,7 @@ void Compiler::fgDebugCheckBlockLinks()
36353635
{
36363636
// Create a set with all the successors. Don't use BlockSet, so we don't need to worry
36373637
// about the BlockSet epoch.
3638-
BitVecTraits bitVecTraits(fgBBNumMax + 1, this);
3638+
BitVecTraits bitVecTraits(impInlineRoot()->fgBBNumMax + 1, this);
36393639
BitVec succBlocks(BitVecOps::MakeEmpty(&bitVecTraits));
36403640
for (BasicBlock* const bTarget : block->SwitchTargets())
36413641
{

0 commit comments

Comments
 (0)