Skip to content

Commit

Permalink
[release/8.0] Check if loop body occured before loopTop and if so unm…
Browse files Browse the repository at this point in the history
…ark alignment (dotnet#91918)

* Check if loop body occured before loopTop"

* Check if bbNatLoopNum is not NOT_IN_LOOP

* review feedback

---------

Co-authored-by: Kunal Pathak <[email protected]>
  • Loading branch information
github-actions[bot] and kunalspathak authored Sep 18, 2023
1 parent 7226388 commit bf1e333
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5280,6 +5280,13 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
weight_t minBlockSoFar = BB_MAX_WEIGHT;
BasicBlock* bbHavingAlign = nullptr;
BasicBlock::loopNumber currentAlignedLoopNum = BasicBlock::NOT_IN_LOOP;
bool visitedLoopNum[BasicBlock::MAX_LOOP_NUM];
memset(visitedLoopNum, false, sizeof(visitedLoopNum));

#ifdef DEBUG
unsigned visitedBlockForLoopNum[BasicBlock::MAX_LOOP_NUM];
memset(visitedBlockForLoopNum, 0, sizeof(visitedBlockForLoopNum));
#endif

if ((fgFirstBB != nullptr) && fgFirstBB->isLoopAlign())
{
Expand All @@ -5302,7 +5309,7 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
}
}

// If there is a unconditional jump (which is not part of callf/always pair)
// If there is an unconditional jump (which is not part of callf/always pair)
if (opts.compJitHideAlignBehindJmp && (block->bbJumpKind == BBJ_ALWAYS) && !block->isBBCallAlwaysPairTail())
{
// Track the lower weight blocks
Expand Down Expand Up @@ -5356,12 +5363,19 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
madeChanges = true;
unmarkedLoopAlign = true;
}
else if ((block->bbNatLoopNum != BasicBlock::NOT_IN_LOOP) && (block->bbNatLoopNum == loopTop->bbNatLoopNum))
else if ((loopTop->bbNatLoopNum != BasicBlock::NOT_IN_LOOP) && visitedLoopNum[loopTop->bbNatLoopNum])
{
#ifdef DEBUG
char buffer[100];
sprintf_s(buffer, 100, "loop block " FMT_BB " appears before top of loop",
visitedBlockForLoopNum[loopTop->bbNatLoopNum]);
#endif

// In some odd cases we may see blocks within the loop before we see the
// top block of the loop. Just bail on aligning such loops.
//
loopTop->unmarkLoopAlign(this DEBUG_ARG("loop block appears before top of loop"));

loopTop->unmarkLoopAlign(this DEBUG_ARG(buffer));
madeChanges = true;
unmarkedLoopAlign = true;
}
Expand Down Expand Up @@ -5396,6 +5410,20 @@ PhaseStatus Compiler::placeLoopAlignInstructions()
break;
}
}

if (block->bbNatLoopNum != BasicBlock::NOT_IN_LOOP)
{
#ifdef DEBUG
if (!visitedLoopNum[block->bbNatLoopNum])
{
// Record the first block for which bbNatLoopNum was seen for
// debugging purpose.
visitedBlockForLoopNum[block->bbNatLoopNum] = block->bbNum;
}
#endif
// If this block is part of loop, mark the loopNum as visited.
visitedLoopNum[block->bbNatLoopNum] = true;
}
}

assert(loopsToProcess == 0);
Expand Down

0 comments on commit bf1e333

Please sign in to comment.