Skip to content

Commit

Permalink
JIT: ensure we keep throw blocks cold when combining blocks (dotnet#8…
Browse files Browse the repository at this point in the history
…3842)

Noticed in a bunch of diffs that there were throw blocks intermixed
with regular blocks. Turns out when compacting a block followed by
a throw, we'd lose track of the fact that the combination should be cold.
  • Loading branch information
AndyAyersMS authored Mar 23, 2023
1 parent 2419c20 commit 8999f4b
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2224,35 +2224,43 @@ void Compiler::fgCompactBlocks(BasicBlock* block, BasicBlock* bNext)
}
}

// If either block or bNext has a profile weight
// If bNext is BBJ_THROW, block will become run rarely.
//
// Otherwise, if either block or bNext has a profile weight
// or if both block and bNext have non-zero weights
// then we will use the max weight for the block.
//
const bool hasProfileWeight = block->hasProfileWeight() || bNext->hasProfileWeight();
const bool hasNonZeroWeight = (block->bbWeight > BB_ZERO_WEIGHT) || (bNext->bbWeight > BB_ZERO_WEIGHT);

if (hasProfileWeight || hasNonZeroWeight)
if (bNext->bbJumpKind == BBJ_THROW)
{
block->bbSetRunRarely();
}
else
{
weight_t const newWeight = max(block->bbWeight, bNext->bbWeight);
const bool hasProfileWeight = block->hasProfileWeight() || bNext->hasProfileWeight();
const bool hasNonZeroWeight = (block->bbWeight > BB_ZERO_WEIGHT) || (bNext->bbWeight > BB_ZERO_WEIGHT);

if (hasProfileWeight)
if (hasProfileWeight || hasNonZeroWeight)
{
block->setBBProfileWeight(newWeight);
weight_t const newWeight = max(block->bbWeight, bNext->bbWeight);

if (hasProfileWeight)
{
block->setBBProfileWeight(newWeight);
}
else
{
assert(newWeight != BB_ZERO_WEIGHT);
block->bbWeight = newWeight;
block->bbFlags &= ~BBF_RUN_RARELY;
}
}
// otherwise if either block has a zero weight we select the zero weight
else
{
assert(newWeight != BB_ZERO_WEIGHT);
block->bbWeight = newWeight;
block->bbFlags &= ~BBF_RUN_RARELY;
noway_assert((block->bbWeight == BB_ZERO_WEIGHT) || (bNext->bbWeight == BB_ZERO_WEIGHT));
block->bbSetRunRarely();
}
}
// otherwise if either block has a zero weight we select the zero weight
else
{
noway_assert((block->bbWeight == BB_ZERO_WEIGHT) || (bNext->bbWeight == BB_ZERO_WEIGHT));
block->bbWeight = BB_ZERO_WEIGHT;
block->bbFlags |= BBF_RUN_RARELY; // Set the RarelyRun flag
}

/* set the right links */

Expand Down

0 comments on commit 8999f4b

Please sign in to comment.