Skip to content

Commit

Permalink
Filter blocks to be used for hiding alignment instruction (#62262)
Browse files Browse the repository at this point in the history
* fix for #61899

* proper fix

* Fix for #62238

* misc change

* Revert "fix for #61899"

This reverts commit 1fc26a5.

* fix formatting

* fix formatting once again

* add validJumpKind check

* review comments
  • Loading branch information
kunalspathak authored Dec 6, 2021
1 parent f8871cb commit a9947a1
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5245,26 +5245,42 @@ void Compiler::placeLoopAlignInstructions()
JITDUMP("Inside placeLoopAlignInstructions for %d loops.\n", loopAlignCandidates);

// Add align only if there were any loops that needed alignment
weight_t minBlockSoFar = BB_MAX_WEIGHT;
BasicBlock* bbHavingAlign = nullptr;
weight_t minBlockSoFar = BB_MAX_WEIGHT;
BasicBlock* bbHavingAlign = nullptr;
BasicBlock::loopNumber currentAlignedLoopNum = BasicBlock::NOT_IN_LOOP;

if ((fgFirstBB != nullptr) && fgFirstBB->isLoopAlign())
{
// Adding align instruction in prolog is not supported
// hence just remove that loop from our list.
loopsToProcess--;
}

for (BasicBlock* const block : Blocks())
{
if ((block == fgFirstBB) && block->isLoopAlign())
if (currentAlignedLoopNum != BasicBlock::NOT_IN_LOOP)
{
// Adding align instruction in prolog is not supported
// hence skip the align block if it is the first block.
loopsToProcess--;
continue;
// We've been processing blocks within an aligned loop. Are we out of that loop now?
if (currentAlignedLoopNum != block->bbNatLoopNum)
{
currentAlignedLoopNum = BasicBlock::NOT_IN_LOOP;
}
}

// If there is a unconditional jump
if (opts.compJitHideAlignBehindJmp && (block->bbJumpKind == BBJ_ALWAYS))
// If there is a unconditional jump (which is not part of callf/always pair)
if (opts.compJitHideAlignBehindJmp && (block->bbJumpKind == BBJ_ALWAYS) && !block->isBBCallAlwaysPairTail())
{
// Track the lower weight blocks
if (block->bbWeight < minBlockSoFar)
{
minBlockSoFar = block->bbWeight;
bbHavingAlign = block;
JITDUMP(FMT_BB ", bbWeight=" FMT_WT " ends with unconditional 'jmp' \n", block->bbNum, block->bbWeight);
if (currentAlignedLoopNum == BasicBlock::NOT_IN_LOOP)
{
// Ok to insert align instruction in this block because it is not part of any aligned loop.
minBlockSoFar = block->bbWeight;
bbHavingAlign = block;
JITDUMP(FMT_BB ", bbWeight=" FMT_WT " ends with unconditional 'jmp' \n", block->bbNum,
block->bbWeight);
}
}
}

Expand All @@ -5285,8 +5301,9 @@ void Compiler::placeLoopAlignInstructions()
}

bbHavingAlign->bbFlags |= BBF_HAS_ALIGN;
minBlockSoFar = BB_MAX_WEIGHT;
bbHavingAlign = nullptr;
minBlockSoFar = BB_MAX_WEIGHT;
bbHavingAlign = nullptr;
currentAlignedLoopNum = block->bbNext->bbNatLoopNum;

if (--loopsToProcess == 0)
{
Expand Down

0 comments on commit a9947a1

Please sign in to comment.