Skip to content

Commit 4619c6c

Browse files
JIT: Set edge likelihood in SetTargetEdge, SetKindAndTargetEdge (#99269)
Part of #93020. When setting the successor edge of a block with one successor, the edge likelihood should always be 1.0. We can enforce this in BasicBlock methods that set bbTargetEdge, and simplify their call sites.
1 parent 7c0ed47 commit 4619c6c

File tree

12 files changed

+37
-68
lines changed

12 files changed

+37
-68
lines changed

src/coreclr/jit/block.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,9 @@ struct BasicBlock : private LIR::Range
847847
assert(HasInitializedTarget());
848848
assert(bbTargetEdge->getSourceBlock() == this);
849849
assert(bbTargetEdge->getDestinationBlock() != nullptr);
850+
851+
// This is the only successor edge for this block, so likelihood should be 1.0
852+
bbTargetEdge->setLikelihood(1.0);
850853
}
851854

852855
BasicBlock* GetTrueTarget() const
@@ -932,16 +935,23 @@ struct BasicBlock : private LIR::Range
932935
SetTrueEdge(trueEdge);
933936
}
934937

935-
// Set both the block kind and target edge. This can clear `bbTargetEdge` when setting
936-
// block kinds that don't use `bbTargetEdge`.
937-
void SetKindAndTargetEdge(BBKinds kind, FlowEdge* targetEdge = nullptr)
938+
// Set both the block kind and target edge.
939+
void SetKindAndTargetEdge(BBKinds kind, FlowEdge* targetEdge)
938940
{
939941
bbKind = kind;
940942
bbTargetEdge = targetEdge;
943+
assert(HasInitializedTarget());
944+
945+
// This is the only successor edge for this block, so likelihood should be 1.0
946+
bbTargetEdge->setLikelihood(1.0);
947+
}
941948

942-
// If bbKind indicates this block has a jump, bbTargetEdge cannot be null.
943-
// You shouldn't use this to set a BBJ_COND, BBJ_SWITCH, or BBJ_EHFINALLYRET.
944-
assert(HasTarget() ? HasInitializedTarget() : (bbTargetEdge == nullptr));
949+
// Set the block kind, and clear bbTargetEdge.
950+
void SetKindAndTargetEdge(BBKinds kind)
951+
{
952+
bbKind = kind;
953+
bbTargetEdge = nullptr;
954+
assert(!HasTarget());
945955
}
946956

947957
bool HasInitializedTarget() const

src/coreclr/jit/fgbasic.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ bool Compiler::fgEnsureFirstBBisScratch()
227227

228228
// The new scratch bb will fall through to the old first bb
229229
FlowEdge* const edge = fgAddRefPred(fgFirstBB, block);
230-
edge->setLikelihood(1.0);
231230
block->SetKindAndTargetEdge(BBJ_ALWAYS, edge);
232231
fgInsertBBbefore(fgFirstBB, block);
233232
}
@@ -678,7 +677,6 @@ void Compiler::fgReplaceJumpTarget(BasicBlock* block, BasicBlock* oldTarget, Bas
678677

679678
if (block->KindIs(BBJ_ALWAYS))
680679
{
681-
newEdge->setLikelihood(1.0);
682680
block->SetTargetEdge(newEdge);
683681
}
684682
else
@@ -2964,7 +2962,6 @@ void Compiler::fgLinkBasicBlocks()
29642962
// Redundantly use SetKindAndTargetEdge() instead of SetTargetEdge() just this once,
29652963
// so we don't break the HasInitializedTarget() invariant of SetTargetEdge().
29662964
FlowEdge* const newEdge = fgAddRefPred<initializingPreds>(jumpDest, curBBdesc);
2967-
newEdge->setLikelihood(1.0);
29682965
curBBdesc->SetKindAndTargetEdge(curBBdesc->GetKind(), newEdge);
29692966

29702967
if (curBBdesc->GetTarget()->bbNum <= curBBdesc->bbNum)
@@ -3862,7 +3859,6 @@ void Compiler::fgFindBasicBlocks()
38623859
{
38633860
// Mark catch handler as successor.
38643861
FlowEdge* const newEdge = fgAddRefPred(hndBegBB, block);
3865-
newEdge->setLikelihood(1.0);
38663862
block->SetTargetEdge(newEdge);
38673863
assert(hndBegBB->bbCatchTyp == BBCT_FILTER_HANDLER);
38683864
break;
@@ -4198,7 +4194,6 @@ void Compiler::fgFixEntryFlowForOSR()
41984194
assert(fgFirstBB->KindIs(BBJ_ALWAYS) && fgFirstBB->JumpsToNext());
41994195
fgRemoveRefPred(fgFirstBB->GetTargetEdge());
42004196
FlowEdge* const newEdge = fgAddRefPred(fgOSREntryBB, fgFirstBB);
4201-
newEdge->setLikelihood(1.0);
42024197
fgFirstBB->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
42034198

42044199
// We don't know the right weight for this block, since
@@ -4790,7 +4785,6 @@ BasicBlock* Compiler::fgSplitBlockAtEnd(BasicBlock* curr)
47904785

47914786
// Default to fallthrough, and add the arc for that.
47924787
FlowEdge* const newEdge = fgAddRefPred(newBlock, curr);
4793-
newEdge->setLikelihood(1.0);
47944788

47954789
// Transfer the kind and target. Do this after the code above, to avoid null-ing out the old targets used by the
47964790
// above code.
@@ -5041,7 +5035,6 @@ BasicBlock* Compiler::fgSplitEdge(BasicBlock* curr, BasicBlock* succ)
50415035

50425036
// And 'succ' has 'newBlock' as a new predecessor.
50435037
FlowEdge* const newEdge = fgAddRefPred(succ, newBlock);
5044-
newEdge->setLikelihood(1.0);
50455038
newBlock->SetTargetEdge(newEdge);
50465039

50475040
// This isn't accurate, but it is complex to compute a reasonable number so just assume that we take the

src/coreclr/jit/fgdiagnostic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2786,6 +2786,7 @@ bool BBPredsChecker::CheckJump(BasicBlock* blockPred, BasicBlock* block)
27862786
case BBJ_EHCATCHRET:
27872787
case BBJ_EHFILTERRET:
27882788
assert(blockPred->TargetIs(block));
2789+
assert(blockPred->GetTargetEdge()->getLikelihood() == 1.0);
27892790
return true;
27902791

27912792
case BBJ_EHFINALLYRET:

src/coreclr/jit/fginline.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -682,11 +682,8 @@ class SubstitutePlaceholdersAndDevirtualizeWalker : public GenTreeVisitor<Substi
682682
else
683683
{
684684
m_compiler->fgRemoveRefPred(block->GetFalseEdge());
685-
block->SetKind(BBJ_ALWAYS);
685+
block->SetKindAndTargetEdge(BBJ_ALWAYS, block->GetTrueEdge());
686686
}
687-
688-
FlowEdge* const edge = m_compiler->fgGetPredForBlock(block->GetTarget(), block);
689-
edge->setLikelihood(1.0);
690687
}
691688
}
692689
else
@@ -1537,7 +1534,6 @@ void Compiler::fgInsertInlineeBlocks(InlineInfo* pInlineInfo)
15371534
bottomBlock->bbNum);
15381535

15391536
FlowEdge* const newEdge = fgAddRefPred(bottomBlock, block);
1540-
newEdge->setLikelihood(1.0);
15411537
block->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
15421538

15431539
if (block == InlineeCompiler->fgLastBB)

src/coreclr/jit/fgopt.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,6 @@ PhaseStatus Compiler::fgPostImportationCleanup()
648648
else
649649
{
650650
FlowEdge* const newEdge = fgAddRefPred(newTryEntry->Next(), newTryEntry);
651-
newEdge->setLikelihood(1.0);
652651
newTryEntry->SetFlags(BBF_NONE_QUIRK);
653652
newTryEntry->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
654653
}
@@ -2634,7 +2633,7 @@ void Compiler::fgRemoveConditionalJump(BasicBlock* block)
26342633

26352634
/* Conditional is gone - always jump to target */
26362635

2637-
block->SetKind(BBJ_ALWAYS);
2636+
block->SetKindAndTargetEdge(BBJ_ALWAYS, block->GetTrueEdge());
26382637
assert(block->TargetIs(target));
26392638

26402639
// TODO-NoFallThrough: Set BBF_NONE_QUIRK only when false target is the next block

src/coreclr/jit/fgprofile.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ void BlockCountInstrumentor::RelocateProbes()
511511
intermediary->SetFlags(BBF_IMPORTED | BBF_MARKED | BBF_NONE_QUIRK);
512512
intermediary->inheritWeight(block);
513513
FlowEdge* const newEdge = m_comp->fgAddRefPred(block, intermediary);
514-
newEdge->setLikelihood(1.0);
515514
intermediary->SetTargetEdge(newEdge);
516515
SetModifiedFlow();
517516

@@ -1683,7 +1682,6 @@ void EfficientEdgeCountInstrumentor::RelocateProbes()
16831682
intermediary->SetFlags(BBF_IMPORTED | BBF_NONE_QUIRK);
16841683
intermediary->inheritWeight(block);
16851684
FlowEdge* const newEdge = m_comp->fgAddRefPred(block, intermediary);
1686-
newEdge->setLikelihood(1.0);
16871685
intermediary->SetTargetEdge(newEdge);
16881686
NewRelocatedProbe(intermediary, probe->source, probe->target, &leader);
16891687
SetModifiedFlow();
@@ -3921,7 +3919,7 @@ void EfficientEdgeCountReconstructor::PropagateOSREntryEdges(BasicBlock* block,
39213919

39223920
assert(flowEdge != nullptr);
39233921

3924-
// Naive likelihood should have been set during pred initialization in fgAddRefPred
3922+
// Naive likelihood should have been set during pred initialization in fgLinkBasicBlocks
39253923
//
39263924
assert(flowEdge->hasLikelihood());
39273925
weight_t likelihood = 0;
@@ -3980,8 +3978,8 @@ void EfficientEdgeCountReconstructor::PropagateEdges(BasicBlock* block, BlockInf
39803978
// If there is a pseudo edge,
39813979
// There should be only one successor for block. The flow
39823980
// from block to successor will not represent real flow.
3983-
// We set likelihood anyways so we can assert later
3984-
// that all flow edges have known likelihood.
3981+
// Likelihood should be set to 1.0 already, as we already know
3982+
// this block has only one successor.
39853983
//
39863984
// Note the flowEdge target may not be the same as the pseudo edge target.
39873985
//
@@ -3992,7 +3990,7 @@ void EfficientEdgeCountReconstructor::PropagateEdges(BasicBlock* block, BlockInf
39923990
assert(block->HasInitializedTarget());
39933991
FlowEdge* const flowEdge = block->GetTargetEdge();
39943992
assert(flowEdge != nullptr);
3995-
flowEdge->setLikelihood(1.0);
3993+
assert(flowEdge->getLikelihood() == 1.0);
39963994
return;
39973995
}
39983996

src/coreclr/jit/flowgraph.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,6 @@ void Compiler::fgConvertSyncReturnToLeave(BasicBlock* block)
16291629

16301630
// Convert the BBJ_RETURN to BBJ_ALWAYS, jumping to genReturnBB.
16311631
FlowEdge* const newEdge = fgAddRefPred(genReturnBB, block);
1632-
newEdge->setLikelihood(1.0);
16331632
block->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
16341633

16351634
#ifdef DEBUG
@@ -2101,7 +2100,6 @@ class MergedReturns
21012100
// Change BBJ_RETURN to BBJ_ALWAYS targeting const return block.
21022101
assert((comp->info.compFlags & CORINFO_FLG_SYNCH) == 0);
21032102
FlowEdge* const newEdge = comp->fgAddRefPred(constReturnBlock, returnBlock);
2104-
newEdge->setLikelihood(1.0);
21052103
returnBlock->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
21062104

21072105
// Remove GT_RETURN since constReturnBlock returns the constant.

src/coreclr/jit/importer.cpp

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,6 @@ BasicBlock* Compiler::impPushCatchArgOnStack(BasicBlock* hndBlk, CORINFO_CLASS_H
20262026
newBlk->bbCodeOffs = hndBlk->bbCodeOffs;
20272027

20282028
FlowEdge* const newEdge = fgAddRefPred(hndBlk, newBlk);
2029-
newEdge->setLikelihood(1.0);
20302029
newBlk->SetTargetEdge(newEdge);
20312030

20322031
// Spill into a temp.
@@ -4437,7 +4436,6 @@ void Compiler::impImportLeave(BasicBlock* block)
44374436

44384437
// the previous call to a finally returns to this call (to the next finally in the chain)
44394438
FlowEdge* const newEdge = fgAddRefPred(callBlock, step);
4440-
newEdge->setLikelihood(1.0);
44414439
step->SetTargetEdge(newEdge);
44424440

44434441
// The new block will inherit this block's weight.
@@ -4487,7 +4485,6 @@ void Compiler::impImportLeave(BasicBlock* block)
44874485
assert(finallyNesting <= compHndBBtabCount);
44884486

44894487
FlowEdge* const newEdge = fgAddRefPred(HBtab->ebdHndBeg, callBlock);
4490-
newEdge->setLikelihood(1.0);
44914488
callBlock->SetKindAndTargetEdge(BBJ_CALLFINALLY, newEdge);
44924489

44934490
GenTree* endLFin = new (this, GT_END_LFIN) GenTreeVal(GT_END_LFIN, TYP_VOID, finallyNesting);
@@ -4539,7 +4536,6 @@ void Compiler::impImportLeave(BasicBlock* block)
45394536

45404537
{
45414538
FlowEdge* const newEdge = fgAddRefPred(finalStep, step);
4542-
newEdge->setLikelihood(1.0);
45434539
step->SetTargetEdge(newEdge);
45444540
}
45454541

@@ -4572,7 +4568,6 @@ void Compiler::impImportLeave(BasicBlock* block)
45724568
// this is the ultimate destination of the LEAVE
45734569
{
45744570
FlowEdge* const newEdge = fgAddRefPred(leaveTarget, finalStep);
4575-
newEdge->setLikelihood(1.0);
45764571
finalStep->SetTargetEdge(newEdge);
45774572
}
45784573

@@ -4692,7 +4687,6 @@ void Compiler::impImportLeave(BasicBlock* block)
46924687
}
46934688

46944689
FlowEdge* const newEdge = fgAddRefPred(exitBlock, step);
4695-
newEdge->setLikelihood(1.0);
46964690
step->SetTargetEdge(newEdge); // the previous step (maybe a call to a nested finally, or a nested catch
46974691
// exit) returns to this block
46984692

@@ -4737,7 +4731,6 @@ void Compiler::impImportLeave(BasicBlock* block)
47374731
// next block, and flow optimizations will remove it.
47384732
fgRemoveRefPred(block->GetTargetEdge());
47394733
FlowEdge* const newEdge = fgAddRefPred(callBlock, block);
4740-
newEdge->setLikelihood(1.0);
47414734
block->SetKindAndTargetEdge(BBJ_ALWAYS, newEdge);
47424735

47434736
// The new block will inherit this block's weight.
@@ -4807,7 +4800,6 @@ void Compiler::impImportLeave(BasicBlock* block)
48074800
}
48084801

48094802
FlowEdge* const newEdge = fgAddRefPred(step2, step);
4810-
newEdge->setLikelihood(1.0);
48114803
step->SetTargetEdge(newEdge);
48124804
step2->inheritWeight(block);
48134805
step2->CopyFlags(block, BBF_RUN_RARELY);
@@ -4848,7 +4840,6 @@ void Compiler::impImportLeave(BasicBlock* block)
48484840
}
48494841

48504842
FlowEdge* const newEdge = fgAddRefPred(callBlock, step);
4851-
newEdge->setLikelihood(1.0);
48524843
step->SetTargetEdge(newEdge); // the previous call to a finally returns to this call (to the next
48534844
// finally in the chain)
48544845

@@ -4885,7 +4876,6 @@ void Compiler::impImportLeave(BasicBlock* block)
48854876
#endif
48864877

48874878
FlowEdge* const newEdge = fgAddRefPred(HBtab->ebdHndBeg, callBlock);
4888-
newEdge->setLikelihood(1.0);
48894879
callBlock->SetKindAndTargetEdge(BBJ_CALLFINALLY, newEdge);
48904880
}
48914881
else if (HBtab->HasCatchHandler() && jitIsBetween(blkAddr, tryBeg, tryEnd) &&
@@ -4954,7 +4944,6 @@ void Compiler::impImportLeave(BasicBlock* block)
49544944
}
49554945

49564946
FlowEdge* const newEdge = fgAddRefPred(catchStep, step);
4957-
newEdge->setLikelihood(1.0);
49584947
step->SetTargetEdge(newEdge);
49594948

49604949
// The new block will inherit this block's weight.
@@ -5009,7 +4998,6 @@ void Compiler::impImportLeave(BasicBlock* block)
50094998
fgRemoveRefPred(step->GetTargetEdge());
50104999
}
50115000
FlowEdge* const newEdge = fgAddRefPred(leaveTarget, step);
5012-
newEdge->setLikelihood(1.0);
50135001
step->SetTargetEdge(newEdge); // this is the ultimate destination of the LEAVE
50145002

50155003
#ifdef DEBUG
@@ -5072,7 +5060,6 @@ void Compiler::impResetLeaveBlock(BasicBlock* block, unsigned jmpAddr)
50725060
BasicBlock* dupBlock = BasicBlock::New(this);
50735061
dupBlock->CopyFlags(block);
50745062
FlowEdge* const newEdge = fgAddRefPred(block->GetTarget(), dupBlock);
5075-
newEdge->setLikelihood(1.0);
50765063
dupBlock->SetKindAndTargetEdge(BBJ_CALLFINALLY, newEdge);
50775064
dupBlock->copyEHRegion(block);
50785065
dupBlock->bbCatchTyp = block->bbCatchTyp;
@@ -5104,7 +5091,6 @@ void Compiler::impResetLeaveBlock(BasicBlock* block, unsigned jmpAddr)
51045091

51055092
fgRemoveRefPred(block->GetTargetEdge());
51065093
FlowEdge* const newEdge = fgAddRefPred(fgLookupBB(jmpAddr), block);
5107-
newEdge->setLikelihood(1.0);
51085094
block->SetKindAndTargetEdge(BBJ_LEAVE, newEdge);
51095095

51105096
// We will leave the BBJ_ALWAYS block we introduced. When it's reimported
@@ -7187,7 +7173,7 @@ void Compiler::impImportBlockCode(BasicBlock* block)
71877173
JITDUMP(FMT_BB " always branches to " FMT_BB ", changing to BBJ_ALWAYS\n", block->bbNum,
71887174
block->GetFalseTarget()->bbNum);
71897175
fgRemoveRefPred(block->GetFalseEdge());
7190-
block->SetKind(BBJ_ALWAYS);
7176+
block->SetKindAndTargetEdge(BBJ_ALWAYS, block->GetTrueEdge());
71917177

71927178
// TODO-NoFallThrough: Once false target can diverge from bbNext, it may not make sense to
71937179
// set BBF_NONE_QUIRK
@@ -7262,7 +7248,7 @@ void Compiler::impImportBlockCode(BasicBlock* block)
72627248
JITDUMP("\nThe conditional jump becomes an unconditional jump to " FMT_BB "\n",
72637249
block->GetTrueTarget()->bbNum);
72647250
fgRemoveRefPred(block->GetFalseEdge());
7265-
block->SetKind(BBJ_ALWAYS);
7251+
block->SetKindAndTargetEdge(BBJ_ALWAYS, block->GetTrueEdge());
72667252
}
72677253
else
72687254
{
@@ -7276,9 +7262,6 @@ void Compiler::impImportBlockCode(BasicBlock* block)
72767262
// to set BBF_NONE_QUIRK
72777263
block->SetFlags(BBF_NONE_QUIRK);
72787264
}
7279-
7280-
FlowEdge* const edge = fgGetPredForBlock(block->GetTarget(), block);
7281-
edge->setLikelihood(1.0);
72827265
}
72837266

72847267
break;
@@ -7452,7 +7435,7 @@ void Compiler::impImportBlockCode(BasicBlock* block)
74527435
JITDUMP(FMT_BB " always branches to " FMT_BB ", changing to BBJ_ALWAYS\n", block->bbNum,
74537436
block->GetFalseTarget()->bbNum);
74547437
fgRemoveRefPred(block->GetFalseEdge());
7455-
block->SetKind(BBJ_ALWAYS);
7438+
block->SetKindAndTargetEdge(BBJ_ALWAYS, block->GetTrueEdge());
74567439

74577440
// TODO-NoFallThrough: Once false target can diverge from bbNext, it may not make sense to
74587441
// set BBF_NONE_QUIRK
@@ -7540,7 +7523,6 @@ void Compiler::impImportBlockCode(BasicBlock* block)
75407523
{
75417524
// transform the basic block into a BBJ_ALWAYS
75427525
block->SetKindAndTargetEdge(BBJ_ALWAYS, curEdge);
7543-
curEdge->setLikelihood(1.0);
75447526
foundVal = true;
75457527
}
75467528
else

0 commit comments

Comments
 (0)