Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JIT: Fix weights in fgOptimizeUncondBranchToSimpleCond #50490

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,8 @@ struct flowList
bool* wbUsedSlop);
void setEdgeWeights(BasicBlock::weight_t newMinWeight, BasicBlock::weight_t newMaxWeight, BasicBlock* bDst);

void scaleEdgeWeights(BasicBlock::weight_t scale, BasicBlock* bDst);

flowList(BasicBlock* block, flowList* rest)
: flNext(rest), m_block(block), flEdgeWeightMin(0), flEdgeWeightMax(0), flDupCount(0)
{
Expand Down
32 changes: 32 additions & 0 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3208,6 +3208,16 @@ bool Compiler::fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock*
// backend always calls `fgUpdateFlowGraph` with `doTailDuplication` set to false.
assert(!block->IsLIR());

// See https://github.com/dotnet/runtime/pull/50490#pullrequestreview-625700642
//
// A - block
// X - target
// C - target->bbJumpDest
// D - target->bbNext

const BasicBlock::weight_t scaleA = block->bbWeight / target->bbWeight;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably should watch for division by zero here.

const BasicBlock::weight_t scaleNotA = 1 - scaleA;

Statement* stmt = target->FirstNonPhiDef();
assert(stmt == target->lastStmt());

Expand All @@ -3230,6 +3240,28 @@ bool Compiler::fgOptimizeUncondBranchToSimpleCond(BasicBlock* block, BasicBlock*
fgAddRefPred(next, block);
fgAddRefPred(next->bbJumpDest, next);

// weight(X) = weight(X) * ScaleNotA
target->scaleBBWeight(scaleNotA);

// weight(X->C) = weight(X->C) * ScaleNotA
flowList* xcEdge = fgGetPredForBlock(target->bbJumpDest, target);
xcEdge->scaleEdgeWeights(scaleNotA, target->bbJumpDest);

// weight(X->D) = weight(X->D) * ScaleNotA
flowList* xdEdge = fgGetPredForBlock(target->bbNext, target);
xdEdge->scaleEdgeWeights(scaleNotA, target->bbNext);

// weight(A->C) = weight(X->C) * ScaleA
flowList* acEdge = fgGetPredForBlock(target->bbJumpDest, block);
acEdge->setEdgeWeights(xcEdge->edgeWeightMin() * scaleA, xcEdge->edgeWeightMin() * scaleA, target->bbJumpDest);

// weight(A->T) = weight(X->D) * ScaleA
flowList* atEdge = fgGetPredForBlock(next, block);
atEdge->setEdgeWeights(xdEdge->edgeWeightMin() * scaleA, xdEdge->edgeWeightMin() * scaleA, target->bbNext);

// weight(T) = weight(X->D) * ScaleA
next->setBBProfileWeight(xdEdge->edgeWeightMax() * scaleA);

JITDUMP("fgOptimizeUncondBranchToSimpleCond(from " FMT_BB " to cond " FMT_BB "), created new uncond " FMT_BB "\n",
block->bbNum, target->bbNum, next->bbNum);
JITDUMP(" expecting opts to key off V%02u, added cloned compare [%06u] to " FMT_BB "\n", lclNum,
Expand Down
16 changes: 16 additions & 0 deletions src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2750,6 +2750,22 @@ void flowList::setEdgeWeights(BasicBlock::weight_t theMinWeight, BasicBlock::wei
flEdgeWeightMax = theMaxWeight;
}

//------------------------------------------------------------------------
// scaleEdgeWeights: Scale both min and max weights by some factor
//
// Arguments:
// scale - scale to apply
// bDst - the destination block for the edge
//
void flowList::scaleEdgeWeights(BasicBlock::weight_t scale, BasicBlock* bDst)
{
JITDUMP("Scalling edge weights for " FMT_BB " -> " FMT_BB " by " FMT_WT "\n", getBlock()->bbNum, bDst->bbNum,
scale);

flEdgeWeightMin *= scale;
flEdgeWeightMax *= scale;
}

//-------------------------------------------------------------
// fgComputeBlockAndEdgeWeights: determine weights for blocks
// and optionally for edges
Expand Down