Skip to content

Commit

Permalink
JIT: profile updates for return merges and tail calls (#48773)
Browse files Browse the repository at this point in the history
Stop trying to update the common return block profile data during return
merging, as it is not yet known which return blocks will become tail calls.
Start updating constant return block profile data during return merging
as this is when we transform the flow.

Update the common return block profile data during return merging in
morph (adding more counts) and when creating tail calls (removing counts).

Update profile consistency checker to handle switches properly and to use
tolerant compares.

Add extra dumping when solving for edge weights or adjusting flow edge
weights to help track down where errors are coming from.

Add new FMT_WT formatting string for profile weights, and start using it
in fgprofile. Use %g so we don't see huge digit strings.

Handle constant return merges too.

Also fix dump output from `setEdgeWeights` and pass in the destination
of the edge.

Refactor `setBBProfileWeight` to also handle setting/clearing rarely run.
  • Loading branch information
AndyAyersMS committed Mar 2, 2021
1 parent c759d0e commit cb606ad
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 145 deletions.
25 changes: 22 additions & 3 deletions src/coreclr/jit/block.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ typedef BitVec_ValRet_T ASSERT_VALRET_TP;
// This define is used with string concatenation to put this in printf format strings (Note that %u means unsigned int)
#define FMT_BB "BB%02u"

// And this format for profile weights
#define FMT_WT "%.7g"

/*****************************************************************************
*
* Each basic block ends with a jump which is described as a value
Expand Down Expand Up @@ -554,10 +557,20 @@ struct BasicBlock : private LIR::Range
}

// setBBProfileWeight -- Set the profile-derived weight for a basic block
// and update the run rarely flag as appropriate.
void setBBProfileWeight(weight_t weight)
{
this->bbFlags |= BBF_PROF_WEIGHT;
this->bbWeight = weight;

if (weight == BB_ZERO_WEIGHT)
{
this->bbFlags |= BBF_RUN_RARELY;
}
else
{
this->bbFlags &= ~BBF_RUN_RARELY;
}
}

// modifyBBWeight -- same as setBBWeight, but also make sure that if the block is rarely run, it stays that
Expand Down Expand Up @@ -1291,9 +1304,15 @@ struct flowList
// They return false if the newWeight is not between the current [min..max]
// when slop is non-zero we allow for the case where our weights might be off by 'slop'
//
bool setEdgeWeightMinChecked(BasicBlock::weight_t newWeight, BasicBlock::weight_t slop, bool* wbUsedSlop);
bool setEdgeWeightMaxChecked(BasicBlock::weight_t newWeight, BasicBlock::weight_t slop, bool* wbUsedSlop);
void setEdgeWeights(BasicBlock::weight_t newMinWeight, BasicBlock::weight_t newMaxWeight);
bool setEdgeWeightMinChecked(BasicBlock::weight_t newWeight,
BasicBlock* bDst,
BasicBlock::weight_t slop,
bool* wbUsedSlop);
bool setEdgeWeightMaxChecked(BasicBlock::weight_t newWeight,
BasicBlock* bDst,
BasicBlock::weight_t slop,
bool* wbUsedSlop);
void setEdgeWeights(BasicBlock::weight_t newMinWeight, BasicBlock::weight_t newMaxWeight, BasicBlock* bDst);

flowList(BasicBlock* block, flowList* rest)
: flNext(rest), m_block(block), flEdgeWeightMin(0), flEdgeWeightMax(0), flDupCount(0)
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/jit/fgflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ flowList* Compiler::fgAddRefPred(BasicBlock* block,
// If our caller has given us the old edge weights
// then we will use them.
//
flow->setEdgeWeights(oldEdge->edgeWeightMin(), oldEdge->edgeWeightMax());
flow->setEdgeWeights(oldEdge->edgeWeightMin(), oldEdge->edgeWeightMax(), block);
}
else
{
Expand All @@ -284,17 +284,17 @@ flowList* Compiler::fgAddRefPred(BasicBlock* block,
// otherwise it is the same as the edge's max weight.
if (blockPred->NumSucc() > 1)
{
flow->setEdgeWeights(BB_ZERO_WEIGHT, newWeightMax);
flow->setEdgeWeights(BB_ZERO_WEIGHT, newWeightMax, block);
}
else
{
flow->setEdgeWeights(flow->edgeWeightMax(), newWeightMax);
flow->setEdgeWeights(flow->edgeWeightMax(), newWeightMax, block);
}
}
}
else
{
flow->setEdgeWeights(BB_ZERO_WEIGHT, BB_MAX_WEIGHT);
flow->setEdgeWeights(BB_ZERO_WEIGHT, BB_MAX_WEIGHT, block);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2323,7 +2323,7 @@ bool Compiler::fgOptimizeBranchToEmptyUnconditional(BasicBlock* block, BasicBloc
{
newEdge2Max = BB_ZERO_WEIGHT;
}
edge2->setEdgeWeights(newEdge2Min, newEdge2Max);
edge2->setEdgeWeights(newEdge2Min, newEdge2Max, bDest);
}
}

Expand Down
Loading

0 comments on commit cb606ad

Please sign in to comment.