Skip to content

Commit 1236138

Browse files
AndyAyersMSpull[bot]
authored andcommitted
JIT: update block weight for uncond to cond flow opt (#98324)
This optimization duplicates code and flow in a BBJ_COND successor into one of its preds; as a result the weight of the successor should decrease. Fixes some issues seen with odd perf scores in the ML/CSE experiment. Contributes to #93020
1 parent 02956ef commit 1236138

1 file changed

Lines changed: 25 additions & 22 deletions

File tree

src/coreclr/jit/fgopt.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,27 +2470,32 @@ bool Compiler::fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock*
24702470
fgInsertStmtAtEnd(block, cloneStmt);
24712471
}
24722472

2473-
// add an unconditional block after this block to jump to the target block's fallthrough block
2473+
// Fix up block's flow.
2474+
// Assume edge likelihoods transfer over.
24742475
//
2475-
assert(!target->IsLast());
2476-
BasicBlock* next = fgNewBBafter(BBJ_ALWAYS, block, true, target->GetFalseTarget());
2477-
2478-
// Fix up block's flow
2479-
//
2480-
block->SetCond(target->GetTrueTarget(), next);
2481-
fgAddRefPred(block->GetTrueTarget(), block);
24822476
fgRemoveRefPred(target, block);
24832477

2484-
// The new block 'next' will inherit its weight from 'block'
2485-
//
2486-
next->inheritWeight(block);
2487-
fgAddRefPred(next, block);
2488-
fgAddRefPred(next->GetTarget(), next);
2478+
FlowEdge* const targetTrueEdge = fgGetPredForBlock(target->GetTrueTarget(), target);
2479+
FlowEdge* const targetFalseEdge = fgGetPredForBlock(target->GetFalseTarget(), target);
2480+
block->SetCond(target->GetTrueTarget(), target->GetFalseTarget());
2481+
fgAddRefPred(block->GetTrueTarget(), block, targetTrueEdge);
2482+
fgAddRefPred(block->GetFalseTarget(), block, targetFalseEdge);
24892483

2490-
JITDUMP("fgOptimizeUncondBranchToSimpleCond(from " FMT_BB " to cond " FMT_BB "), created new uncond " FMT_BB "\n",
2491-
block->bbNum, target->bbNum, next->bbNum);
2484+
JITDUMP("fgOptimizeUncondBranchToSimpleCond(from " FMT_BB " to cond " FMT_BB "), modified " FMT_BB "\n",
2485+
block->bbNum, target->bbNum, block->bbNum);
24922486
JITDUMP(" expecting opts to key off V%02u in " FMT_BB "\n", lclNum, block->bbNum);
24932487

2488+
if (target->hasProfileWeight() && block->hasProfileWeight())
2489+
{
2490+
// Remove weight from target since block now bypasses it...
2491+
//
2492+
weight_t targetWeight = target->bbWeight;
2493+
weight_t blockWeight = block->bbWeight;
2494+
target->setBBProfileWeight(max(0, targetWeight - blockWeight));
2495+
JITDUMP("Decreased " FMT_BB " profile weight from " FMT_WT " to " FMT_WT "\n", target->bbNum, targetWeight,
2496+
target->bbWeight);
2497+
}
2498+
24942499
return true;
24952500
}
24962501

@@ -4817,13 +4822,11 @@ bool Compiler::fgUpdateFlowGraph(bool doTailDuplication /* = false */, bool isPh
48174822
if (doTailDuplication && fgOptimizeUncondBranchToSimpleCond(block, bDest))
48184823
{
48194824
assert(block->KindIs(BBJ_COND));
4820-
change = true;
4821-
modified = true;
4822-
bDest = block->GetTrueTarget();
4823-
bNext = block->GetFalseTarget();
4824-
4825-
// TODO-NoFallThrough: Adjust the above logic once bbFalseTarget can diverge from bbNext
4826-
assert(block->NextIs(bNext));
4825+
assert(bNext == block->Next());
4826+
change = true;
4827+
modified = true;
4828+
bDest = block->GetTrueTarget();
4829+
bFalseDest = block->GetFalseTarget();
48274830
}
48284831
}
48294832

0 commit comments

Comments
 (0)