Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/coreclr/jit/assertionprop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3166,10 +3166,10 @@ bool Compiler::optIsProfitableToSubstitute(GenTree* dest, BasicBlock* destBlock,
// Avoid propagating if the weighted use cost is significantly greater than the def cost.
// NOTE: this currently does not take "a float living across a call" case into account
// where we might end up with spill/restore on ABIs without callee-saved registers
const weight_t defBlockWeight = defBlock->getBBWeight(this);
const weight_t lclblockWeight = destBlock->getBBWeight(this);
const weight_t defBlockWeight = defBlock->bbWeight;
const weight_t lclblockWeight = destBlock->bbWeight;

if ((defBlockWeight > 0) && ((lclblockWeight / defBlockWeight) >= BB_LOOP_WEIGHT_SCALE))
if ((defBlockWeight > BB_ZERO_WEIGHT) && ((lclblockWeight / defBlockWeight) >= BB_LOOP_WEIGHT_SCALE))
{
JITDUMP("Constant propagation inside loop " FMT_BB " is not profitable\n", destBlock->bbNum);
return false;
Expand Down
47 changes: 10 additions & 37 deletions src/coreclr/jit/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,36 +1589,16 @@ BBJumpTable::BBJumpTable(Compiler* comp, const BBJumpTable* other)
// compiler - Compiler instance
//
// Notes:
// If we don't have profile data then getCalledCount will return BB_UNITY_WEIGHT (100)
// If we don't have profile data then getCalledCount will return BB_UNITY_WEIGHT (1.0)
// otherwise it returns the number of times that profile data says the method was called.

// static
weight_t BasicBlock::getCalledCount(Compiler* comp)
{
// when we don't have profile data then fgCalledCount will be BB_UNITY_WEIGHT (100)
weight_t calledCount = comp->fgCalledCount;

// If we haven't yet reach the place where we setup fgCalledCount it could still be zero
// so return a reasonable value to use until we set it.
//
if (calledCount == 0)
{
if (comp->fgIsUsingProfileWeights())
{
// When we use profile data block counts we have exact counts,
// not multiples of BB_UNITY_WEIGHT (100)
calledCount = 1;
}
else
{
calledCount = comp->fgFirstBB->bbWeight;

if (calledCount == 0)
{
calledCount = BB_UNITY_WEIGHT;
}
}
}
// When we don't have profile data then fgCalledCount will be BB_UNITY_WEIGHT
const weight_t calledCount = comp->fgCalledCount;
assert(calledCount != BB_ZERO_WEIGHT);
assert((calledCount == BB_UNITY_WEIGHT) || comp->fgIsUsingProfileWeights());
return calledCount;
}

Expand All @@ -1634,20 +1614,13 @@ weight_t BasicBlock::getCalledCount(Compiler* comp)
//
weight_t BasicBlock::getBBWeight(Compiler* comp) const
{
if (this->bbWeight == BB_ZERO_WEIGHT)
{
return BB_ZERO_WEIGHT;
}
else
{
weight_t calledCount = getCalledCount(comp);
weight_t calledCount = getCalledCount(comp);

// Normalize the bbWeight.
//
weight_t fullResult = (this->bbWeight / calledCount) * BB_UNITY_WEIGHT;
// Normalize the bbWeight.
//
weight_t fullResult = this->bbWeight / calledCount;

return fullResult;
}
return fullResult;
}

//------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5298,7 +5298,7 @@ bool Compiler::shouldAlignLoop(FlowGraphNaturalLoop* loop, BasicBlock* top)
// Now we have an innerloop candidate that might need alignment

weight_t topWeight = top->getBBWeight(this);
weight_t compareWeight = opts.compJitAlignLoopMinBlockWeight * BB_UNITY_WEIGHT;
weight_t compareWeight = opts.compJitAlignLoopMinBlockWeight;
if (topWeight < compareWeight)
{
JITDUMP("Skipping alignment for " FMT_LP " that starts at " FMT_BB ", weight=" FMT_WT " < " FMT_WT ".\n",
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/jit/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1489,8 +1489,8 @@ weight_t emitter::getCurrentBlockWeight()
}
else // we have a null compCurBB
{
// prolog or epilog case, so just use the standard weight
return BB_UNITY_WEIGHT;
// prolog or epilog case, so just use the method's call count
return BasicBlock::getCalledCount(emitComp);
}
}

Expand Down Expand Up @@ -4408,7 +4408,7 @@ size_t emitter::emitIssue1Instr(insGroup* ig, instrDesc* id, BYTE** dp)
#if defined(DEBUG) || defined(LATE_DISASM)
float insExeCost = insEvaluateExecutionCost(id);
// All compPerfScore calculations must be performed using doubles
double insPerfScore = (double)(ig->igWeight / (double)BB_UNITY_WEIGHT) * insExeCost;
double insPerfScore = ((double)ig->igWeight) * insExeCost;
emitComp->Metrics.PerfScore += insPerfScore;
ig->igPerfScore += insPerfScore;
#endif // defined(DEBUG) || defined(LATE_DISASM)
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void Compiler::fgCreateNewInitBB()
{
// If the result is clearly nonsensical, just inherit
//
JITDUMP("\fgCanonicalizeFirstBB: Profile data could not be locally repaired. Data %s inconsistent.\n",
JITDUMP("\nfgCanonicalizeFirstBB: Profile data could not be locally repaired. Data %s inconsistent.\n",
fgPgoConsistent ? "is now" : "was already");

if (fgPgoConsistent)
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ bool Compiler::fgDumpFlowGraph(Phases phase, PhasePosition pos)
// "Raw" Profile weight
if (block->hasProfileWeight() || (JitConfig.JitSynthesizeCounts() > 0))
{
fprintf(fgxFile, "\\n\\n%7.2f", ((double)block->getBBWeight(this)) / BB_UNITY_WEIGHT);
fprintf(fgxFile, "\\n\\n%7.2f", (double)block->getBBWeight(this));
}

// end of block label
Expand Down Expand Up @@ -1833,18 +1833,18 @@ void Compiler::fgTableDispBasicBlock(const BasicBlock* block,
{
weight_t weight = block->getBBWeight(this);

if (weight > 99999) // Is it going to be more than 6 characters?
if (weight > 999.99) // Is it going to be more than 6 characters?
Copy link
Preview

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

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

The threshold has been changed from 99999 to 999.99, but the comment still refers to '6 characters'. With the new threshold, weights like 1000.00 would be 7 characters, making the comment inaccurate. Update the comment to reflect the actual character count logic.

Suggested change
if (weight > 999.99) // Is it going to be more than 6 characters?
if (weight > 999.99) // If the weight does not fit in the standard "ddd.dd" format (i.e., is 1000.00 or greater)

Copilot uses AI. Check for mistakes.

{
if (weight <= 99999 * BB_UNITY_WEIGHT)
if (weight <= 99999)
{
// print weight in this format ddddd.
printf("%5u.", (unsigned)FloatingPointUtils::round(weight / BB_UNITY_WEIGHT));
printf("%5u.", (unsigned)FloatingPointUtils::round(weight));
}
else // print weight in terms of k (i.e. 156k )
{
// print weight in this format dddddk
weight_t weightK = weight / 1000;
printf("%5uk", (unsigned)FloatingPointUtils::round(weightK / BB_UNITY_WEIGHT));
printf("%5uk", (unsigned)FloatingPointUtils::round(weightK));
}
}
else // print weight in this format ddd.dd
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ void Compiler::fgApplyProfileScale()
//
if (calleeWeight <= firstBlockPredWeight)
{
calleeWeight = fgHaveProfileWeights() ? 1.0 : BB_UNITY_WEIGHT;
calleeWeight = BB_UNITY_WEIGHT;
JITDUMP(" ... callee entry has zero or negative weight, will use weight of " FMT_WT " to scale\n",
calleeWeight);
JITDUMP("Profile data could not be scaled consistently. Data %s inconsistent.\n",
Expand Down
13 changes: 11 additions & 2 deletions src/coreclr/jit/fgprofilesynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,17 @@ void ProfileSynthesis::Run(ProfileSynthesisOption option)
entryWeight -= predEdge->getLikelyWeight();
}

m_comp->fgCalledCount = max(BB_ZERO_WEIGHT, entryWeight);
JITDUMP("fgCalledCount is " FMT_WT "\n", m_comp->fgCalledCount);
entryWeight = max(BB_ZERO_WEIGHT, entryWeight);

if (entryWeight == BB_ZERO_WEIGHT)
{
assert(m_hasInfiniteLoop);
JITDUMP("Entry block is the head of an infinite loop.\n");
entryWeight = 1.0;
}

JITDUMP("fgCalledCount is " FMT_WT "\n", entryWeight);
m_comp->fgCalledCount = entryWeight;
}

#ifdef DEBUG
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/ifconversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ bool OptIfConversionDsc::optIfConvert()
// dependencies can cause significant stalls if if-converted.
// Detect via the block weight as that will be high when inside a loop.

if (m_startBlock->getBBWeight(m_comp) > BB_UNITY_WEIGHT * 1.05)
if (m_startBlock->getBBWeight(m_comp) > 1.05)
{
JITDUMP("Skipping if-conversion inside loop (via weight)\n");
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/jit/inductionvariableopts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,8 @@ bool Compiler::optIsIVWideningProfitable(
return BasicBlockVisit::Continue;
});

// TODO: 'savedCost' is already computed using normalized weights. Don't divide by the method entry block's weight,
// in case there is flow into it?
const weight_t ALLOWED_SIZE_REGRESSION_PER_CYCLE_IMPROVEMENT = 2;
weight_t cycleImprovementPerInvoc = savedCost / fgFirstBB->getBBWeight(this);

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/loopcloning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2860,7 +2860,7 @@ bool Compiler::optCheckLoopCloningGDVTestProfitable(GenTreeOp* guard, LoopCloneV

// Check for (2)
//
if (loop->GetHeader()->getBBWeight(this) < (loopFrequency * BB_UNITY_WEIGHT))
if (loop->GetHeader()->getBBWeight(this) < loopFrequency)
{
JITDUMP(" No; loop does not iterate often enough.\n");
return WALK_CONTINUE;
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/jit/lower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7788,8 +7788,7 @@ bool Lowering::LowerUnsignedDivOrMod(GenTreeOp* divMod)
#endif
}

const bool requiresDividendMultiuse = !isDiv;
const weight_t curBBWeight = m_block->getBBWeight(comp);
const bool requiresDividendMultiuse = !isDiv;

if (requiresDividendMultiuse)
{
Expand Down
8 changes: 5 additions & 3 deletions src/coreclr/jit/optcse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2406,7 +2406,7 @@ void CSE_HeuristicParameterized::CaptureLocalWeights()
}

JITDUMP("V%02u," FMT_WT "\n", m_pCompiler->lvaGetLclNum(varDsc), varDsc->lvRefCntWtd());
m_localWeights->push_back(varDsc->lvRefCntWtd() / BB_UNITY_WEIGHT);
m_localWeights->push_back(varDsc->lvRefCntWtd());
}
}

Expand Down Expand Up @@ -4400,7 +4400,7 @@ bool CSE_Heuristic::PromotionCheck(CSE_Candidate* candidate)
weight_t no_cse_cost = 0;
weight_t yes_cse_cost = 0;
unsigned extra_yes_cost = 0;
unsigned extra_no_cost = 0;
weight_t extra_no_cost = 0;
Copy link
Preview

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

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

The variable type has been changed from unsigned to weight_t but the corresponding printf format specifier on line 4746 has been updated to %f. Ensure this type change is intentional and consistent with how extra_no_cost is used throughout the function.

Suggested change
weight_t extra_no_cost = 0;
weight_t extra_no_cost = 0.0;

Copilot uses AI. Check for mistakes.


// The 'cseRefCnt' is the RefCnt that we will have if we promote this CSE into a new LclVar
// Each CSE Def will contain two Refs and each CSE Use will have one Ref of this new LclVar
Expand Down Expand Up @@ -4724,6 +4724,8 @@ bool CSE_Heuristic::PromotionCheck(CSE_Candidate* candidate)
// weighted count
extra_no_cost = candidate->Size() - cse_use_cost;
extra_no_cost = extra_no_cost * dsc->csdUseCount * 2;
// TODO-BB-UNITY-WEIGHT: Remove?
extra_no_cost /= 100;
Copy link
Preview

Copilot AI Aug 18, 2025

Choose a reason for hiding this comment

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

This hardcoded division by 100 appears to be a temporary workaround based on the TODO comment. Consider replacing the magic number 100 with a named constant or removing this scaling entirely if it's no longer needed after the BB_UNITY_WEIGHT changes.

Suggested change
extra_no_cost /= 100;

Copilot uses AI. Check for mistakes.

}

/* no_cse_cost is the cost estimate when we decide not to make a CSE */
Expand All @@ -4741,7 +4743,7 @@ bool CSE_Heuristic::PromotionCheck(CSE_Candidate* candidate)
printf("cseRefCnt=%f, aggressiveRefCnt=%f, moderateRefCnt=%f\n", cseRefCnt, aggressiveRefCnt, moderateRefCnt);
printf("defCnt=%f, useCnt=%f, cost=%d, size=%d%s\n", candidate->DefCount(), candidate->UseCount(),
candidate->Cost(), candidate->Size(), candidate->LiveAcrossCall() ? ", LiveAcrossCall" : "");
printf("def_cost=%d, use_cost=%d, extra_no_cost=%d, extra_yes_cost=%d\n", cse_def_cost, cse_use_cost,
printf("def_cost=%d, use_cost=%d, extra_no_cost=%f, extra_yes_cost=%d\n", cse_def_cost, cse_use_cost,
extra_no_cost, extra_yes_cost);

printf("CSE cost savings check (%f >= %f) %s\n", no_cse_cost, yes_cse_cost,
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/promotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,6 +890,7 @@ class LocalUses
// give a bonus to promoting remainders that may not have scalar uses
// but will allow fully decomposing stores away.

// TODO: Don't divide by entry block's normalized weight, since we're already summing normalized weights
weight_t cycleImprovementPerInvoc = (costWithout - costWith) / comp->fgFirstBB->getBBWeight(comp);
weight_t sizeImprovement = sizeWithout - sizeWith;

Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/regalloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool Compiler::shouldDoubleAlign(
JITDUMP(" Sum of weighted ref counts for EBP enregistered variables: %f\n", refCntWtdEBP);
JITDUMP(" Sum of weighted ref counts for weighted stack based doubles: %f\n", refCntWtdStkDbl);

if (((weight_t)bytesUsed) > ((refCntWtdStkDbl * misaligned_weight) / BB_UNITY_WEIGHT))
if (((weight_t)bytesUsed) > (refCntWtdStkDbl * misaligned_weight))
{
JITDUMP(" Predicting not to double-align ESP to save %d bytes of code.\n", bytesUsed);
}
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ const char* refCntWtd2str(weight_t refCntWtd, bool padForDecimalPlaces)
}
else
{
weight_t scaledWeight = refCntWtd / BB_UNITY_WEIGHT;
weight_t scaledWeight = refCntWtd;
weight_t intPart = (weight_t)floor(scaledWeight);
bool isLarge = intPart > 1e9;
bool isSmall = (intPart < 1e-2) && (intPart != 0);
Expand Down
Loading