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: Replace BlockSet with BitVec, and remove BB epoch #110034

Merged
merged 11 commits into from
Nov 21, 2024
4 changes: 2 additions & 2 deletions src/coreclr/jit/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,10 +1036,10 @@ unsigned JitPtrKeyFuncs<BasicBlock>::GetHashCode(const BasicBlock* ptr)
unsigned hash = SsaStressHashHelper();
if (hash != 0)
{
return (hash ^ (ptr->bbNum << 16) ^ ptr->bbNum);
return (hash ^ (ptr->bbID << 16) ^ ptr->bbID);
}
#endif
return ptr->bbNum;
return ptr->bbID;
}

//------------------------------------------------------------------------
Expand Down
38 changes: 1 addition & 37 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -5247,42 +5247,6 @@ class Compiler
// bits. This is precomputed to avoid doing math every time BasicBlockBitSetTraits::GetArrSize() is called.
unsigned fgBBSetCountInSizeTUnits = 0;

void NewBasicBlockEpoch()
{
INDEBUG(unsigned oldEpochArrSize = fgBBSetCountInSizeTUnits);

// We have a new epoch. Compute and cache the size needed for new BlockSets.
fgCurBBEpoch++;
fgCurBBEpochSize = fgBBNumMax + 1;
fgBBSetCountInSizeTUnits =
roundUp(fgCurBBEpochSize, (unsigned)(sizeof(size_t) * 8)) / unsigned(sizeof(size_t) * 8);

#ifdef DEBUG
if (verbose)
{
unsigned epochArrSize = BasicBlockBitSetTraits::GetArrSize(this);
printf("\nNew BlockSet epoch %d, # of blocks (including unused BB00): %u, bitset array size: %u (%s)",
fgCurBBEpoch, fgCurBBEpochSize, epochArrSize, (epochArrSize <= 1) ? "short" : "long");
if ((fgCurBBEpoch != 1) && ((oldEpochArrSize <= 1) != (epochArrSize <= 1)))
{
// If we're not just establishing the first epoch, and the epoch array size has changed such that we're
// going to change our bitset representation from short (just a size_t bitset) to long (a pointer to an
// array of size_t bitsets), then print that out.
printf("; NOTE: BlockSet size was previously %s!", (oldEpochArrSize <= 1) ? "short" : "long");
}
printf("\n");
}
#endif // DEBUG
}

void EnsureBasicBlockEpoch()
{
if (fgCurBBEpochSize != fgBBNumMax + 1)
{
NewBasicBlockEpoch();
}
}

bool fgEnsureFirstBBisScratch();
bool fgFirstBBisScratch();
bool fgBBisScratch(BasicBlock* block);
Expand Down Expand Up @@ -6300,7 +6264,7 @@ class Compiler
};

template <bool hasEH>
void fgMoveHotJumps();
void fgMoveHotJumps(FlowGraphDfsTree* dfsTree);

bool fgFuncletsAreCold();

Expand Down
24 changes: 0 additions & 24 deletions src/coreclr/jit/fgbasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5400,30 +5400,6 @@ bool Compiler::fgRenumberBlocks()
JITDUMP("=============== No blocks renumbered!\n");
}

// Now update the BlockSet epoch, which depends on the block numbers.
// If any blocks have been renumbered then create a new BlockSet epoch.
// Even if we have not renumbered any blocks, we might still need to force
// a new BlockSet epoch, for one of several reasons. If there are any new
// blocks with higher numbers than the former maximum numbered block, then we
// need a new epoch with a new size matching the new largest numbered block.
// Also, if the number of blocks is different from the last time we set the
// BlockSet epoch, then we need a new epoch. This wouldn't happen if we
// renumbered blocks after every block addition/deletion, but it might be
// the case that we can change the number of blocks, then set the BlockSet
// epoch without renumbering, then change the number of blocks again, then
// renumber.
if (renumbered || newMaxBBNum)
{
NewBasicBlockEpoch();

// The key in the unique switch successor map is dependent on the block number, so invalidate that cache.
InvalidateUniqueSwitchSuccMap();
}
else
{
EnsureBasicBlockEpoch();
}

// Tell our caller if any blocks actually were renumbered.
return renumbered || newMaxBBNum;
}
Expand Down
28 changes: 19 additions & 9 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4463,8 +4463,11 @@ bool Compiler::fgReorderBlocks(bool useProfile)
// Template parameters:
// hasEH - If true, method has EH regions, so check that we don't try to move blocks in different regions
//
// Parameters:
// dfsTree - The depth-first traversal of the flowgraph
//
template <bool hasEH>
void Compiler::fgMoveHotJumps()
void Compiler::fgMoveHotJumps(FlowGraphDfsTree* dfsTree)
{
#ifdef DEBUG
if (verbose)
Expand All @@ -4477,17 +4480,22 @@ void Compiler::fgMoveHotJumps()
}
#endif // DEBUG

EnsureBasicBlockEpoch();
BlockSet visitedBlocks(BlockSetOps::MakeEmpty(this));
BlockSetOps::AddElemD(this, visitedBlocks, fgFirstBB->bbNum);
assert(dfsTree != nullptr);
BitVecTraits traits(dfsTree->PostOrderTraits());
BitVec visitedBlocks = BitVecOps::MakeEmpty(&traits);

// If we have a funclet region, don't bother reordering anything in it.
//
BasicBlock* next;
for (BasicBlock* block = fgFirstBB; block != fgFirstFuncletBB; block = next)
{
next = block->Next();
BlockSetOps::AddElemD(this, visitedBlocks, block->bbNum);
if (!dfsTree->Contains(block))
{
continue;
}

BitVecOps::AddElemD(&traits, visitedBlocks, block->bbPostorderNum);

// Don't bother trying to move cold blocks
//
Expand Down Expand Up @@ -4534,7 +4542,8 @@ void Compiler::fgMoveHotJumps()
}

BasicBlock* target = targetEdge->getDestinationBlock();
bool isBackwardJump = BlockSetOps::IsMember(this, visitedBlocks, target->bbNum);
bool isBackwardJump = BitVecOps::IsMember(&traits, visitedBlocks, target->bbPostorderNum);
assert(dfsTree->Contains(target));

if (isBackwardJump)
{
Expand All @@ -4553,7 +4562,8 @@ void Compiler::fgMoveHotJumps()
//
targetEdge = unlikelyEdge;
target = targetEdge->getDestinationBlock();
isBackwardJump = BlockSetOps::IsMember(this, visitedBlocks, target->bbNum);
isBackwardJump = BitVecOps::IsMember(&traits, visitedBlocks, target->bbPostorderNum);
assert(dfsTree->Contains(target));

if (isBackwardJump)
{
Expand Down Expand Up @@ -4696,7 +4706,7 @@ void Compiler::fgDoReversePostOrderLayout()
}
}

fgMoveHotJumps</* hasEH */ false>();
fgMoveHotJumps</* hasEH */ false>(dfsTree);

return;
}
Expand Down Expand Up @@ -4769,7 +4779,7 @@ void Compiler::fgDoReversePostOrderLayout()
fgInsertBBafter(pair.callFinally, pair.callFinallyRet);
}

fgMoveHotJumps</* hasEH */ true>();
fgMoveHotJumps</* hasEH */ true>(dfsTree);
}

//-----------------------------------------------------------------------------
Expand Down
Loading
Loading