Skip to content

Commit a9947a1

Browse files
authored
Filter blocks to be used for hiding alignment instruction (#62262)
* 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
1 parent f8871cb commit a9947a1

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/coreclr/jit/compiler.cpp

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5245,26 +5245,42 @@ void Compiler::placeLoopAlignInstructions()
52455245
JITDUMP("Inside placeLoopAlignInstructions for %d loops.\n", loopAlignCandidates);
52465246

52475247
// Add align only if there were any loops that needed alignment
5248-
weight_t minBlockSoFar = BB_MAX_WEIGHT;
5249-
BasicBlock* bbHavingAlign = nullptr;
5248+
weight_t minBlockSoFar = BB_MAX_WEIGHT;
5249+
BasicBlock* bbHavingAlign = nullptr;
5250+
BasicBlock::loopNumber currentAlignedLoopNum = BasicBlock::NOT_IN_LOOP;
5251+
5252+
if ((fgFirstBB != nullptr) && fgFirstBB->isLoopAlign())
5253+
{
5254+
// Adding align instruction in prolog is not supported
5255+
// hence just remove that loop from our list.
5256+
loopsToProcess--;
5257+
}
5258+
52505259
for (BasicBlock* const block : Blocks())
52515260
{
5252-
if ((block == fgFirstBB) && block->isLoopAlign())
5261+
if (currentAlignedLoopNum != BasicBlock::NOT_IN_LOOP)
52535262
{
5254-
// Adding align instruction in prolog is not supported
5255-
// hence skip the align block if it is the first block.
5256-
loopsToProcess--;
5257-
continue;
5263+
// We've been processing blocks within an aligned loop. Are we out of that loop now?
5264+
if (currentAlignedLoopNum != block->bbNatLoopNum)
5265+
{
5266+
currentAlignedLoopNum = BasicBlock::NOT_IN_LOOP;
5267+
}
52585268
}
52595269

5260-
// If there is a unconditional jump
5261-
if (opts.compJitHideAlignBehindJmp && (block->bbJumpKind == BBJ_ALWAYS))
5270+
// If there is a unconditional jump (which is not part of callf/always pair)
5271+
if (opts.compJitHideAlignBehindJmp && (block->bbJumpKind == BBJ_ALWAYS) && !block->isBBCallAlwaysPairTail())
52625272
{
5273+
// Track the lower weight blocks
52635274
if (block->bbWeight < minBlockSoFar)
52645275
{
5265-
minBlockSoFar = block->bbWeight;
5266-
bbHavingAlign = block;
5267-
JITDUMP(FMT_BB ", bbWeight=" FMT_WT " ends with unconditional 'jmp' \n", block->bbNum, block->bbWeight);
5276+
if (currentAlignedLoopNum == BasicBlock::NOT_IN_LOOP)
5277+
{
5278+
// Ok to insert align instruction in this block because it is not part of any aligned loop.
5279+
minBlockSoFar = block->bbWeight;
5280+
bbHavingAlign = block;
5281+
JITDUMP(FMT_BB ", bbWeight=" FMT_WT " ends with unconditional 'jmp' \n", block->bbNum,
5282+
block->bbWeight);
5283+
}
52685284
}
52695285
}
52705286

@@ -5285,8 +5301,9 @@ void Compiler::placeLoopAlignInstructions()
52855301
}
52865302

52875303
bbHavingAlign->bbFlags |= BBF_HAS_ALIGN;
5288-
minBlockSoFar = BB_MAX_WEIGHT;
5289-
bbHavingAlign = nullptr;
5304+
minBlockSoFar = BB_MAX_WEIGHT;
5305+
bbHavingAlign = nullptr;
5306+
currentAlignedLoopNum = block->bbNext->bbNatLoopNum;
52905307

52915308
if (--loopsToProcess == 0)
52925309
{

0 commit comments

Comments
 (0)