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: Add loop-aware RPO, and use as LSRA's block sequence #108086

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add loop-aware RPO and use in LSRA
  • Loading branch information
amanasifkhalid committed Sep 20, 2024
commit 2d21d9ed20aca1a6b7d5a39358fa04ccbec78b1f
6 changes: 5 additions & 1 deletion src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4524,7 +4524,7 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
compFunctionTraceStart();

// Enable flow graph checks
activePhaseChecks |= PhaseChecks::CHECK_FG;
activePhaseChecks |= PhaseChecks::CHECK_FG | PhaseChecks::CHECK_FG_ANNOTATIONS;

// Prepare for importation
//
Expand Down Expand Up @@ -5239,6 +5239,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
lvaTrackedFixed = true;
const unsigned numBlocksBeforeLSRA = fgBBcount;

// Backend phases will use a loop-aware RPO traversal of the flowgraph,
// so skip checking pre/postorder numbers for correctness.
activePhaseChecks &= ~PhaseChecks::CHECK_FG_ANNOTATIONS;

// Now that lowering is completed we can proceed to perform register allocation
//
auto linearScanPhase = [this] {
Expand Down
20 changes: 11 additions & 9 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1527,15 +1527,16 @@ extern const char* PhaseEnums[];
// clang-format off
enum class PhaseChecks : unsigned int
{
CHECK_NONE = 0,
CHECK_IR = 1 << 0, // ir flags, etc
CHECK_UNIQUE = 1 << 1, // tree node uniqueness
CHECK_FG = 1 << 2, // flow graph integrity
CHECK_EH = 1 << 3, // eh table integrity
CHECK_LOOPS = 1 << 4, // loop integrity/canonicalization
CHECK_LIKELIHOODS = 1 << 5, // profile data likelihood integrity
CHECK_PROFILE = 1 << 6, // profile data full integrity
CHECK_LINKED_LOCALS = 1 << 7, // check linked list of locals
CHECK_NONE = 0,
CHECK_IR = 1 << 0, // ir flags, etc
CHECK_UNIQUE = 1 << 1, // tree node uniqueness
CHECK_FG = 1 << 2, // flow graph integrity
CHECK_EH = 1 << 3, // eh table integrity
CHECK_LOOPS = 1 << 4, // loop integrity/canonicalization
CHECK_LIKELIHOODS = 1 << 5, // profile data likelihood integrity
CHECK_PROFILE = 1 << 6, // profile data full integrity
CHECK_LINKED_LOCALS = 1 << 7, // check linked list of locals
CHECK_FG_ANNOTATIONS = 1 << 8, // check flowgraph annotation data structures
};

inline constexpr PhaseChecks operator ~(PhaseChecks a)
Expand Down Expand Up @@ -6286,6 +6287,7 @@ class Compiler

template <const bool useProfile = false>
FlowGraphDfsTree* fgComputeDfs();
FlowGraphDfsTree* fgComputeLoopAwareDfs();
void fgInvalidateDfsTree();

void fgRemoveReturnBlock(BasicBlock* block);
Expand Down
56 changes: 56 additions & 0 deletions src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4120,6 +4120,62 @@ FlowGraphDfsTree* Compiler::fgComputeDfs()
template FlowGraphDfsTree* Compiler::fgComputeDfs<false>();
template FlowGraphDfsTree* Compiler::fgComputeDfs<true>();

FlowGraphDfsTree* Compiler::fgComputeLoopAwareDfs()
{
if (m_dfsTree == nullptr)
{
m_dfsTree = fgComputeDfs</* useProfile */ true>();
}

if (!m_dfsTree->HasCycle())
{
return m_dfsTree;
}

if (m_loops == nullptr)
{
m_loops = FlowGraphNaturalLoops::Find(m_dfsTree);
}

m_blockToLoop = BlockToNaturalLoopMap::Build(m_loops);

EnsureBasicBlockEpoch();
BlockSet visitedBlocks(BlockSetOps::MakeEmpty(this));

BasicBlock** loopAwarePostOrder = new (this, CMK_DepthFirstSearch) BasicBlock*[fgBBcount];
const unsigned numBlocks = m_dfsTree->GetPostOrderCount();
unsigned newIndex = numBlocks - 1;

auto visitBlock = [this, loopAwarePostOrder, &visitedBlocks, &newIndex,
numBlocks](BasicBlock* block) -> BasicBlockVisit {
if (!BlockSetOps::IsMember(this, visitedBlocks, block->bbNum))
{
assert(newIndex < numBlocks);
loopAwarePostOrder[newIndex--] = block;
BlockSetOps::AddElemD(this, visitedBlocks, block->bbNum);
}

return BasicBlockVisit::Continue;
};

for (unsigned i = numBlocks; i != 0; i--)
{
BasicBlock* const block = m_dfsTree->GetPostOrder(i - 1);
FlowGraphNaturalLoop* const loop = m_blockToLoop->GetLoop(block);

if ((loop != nullptr) && (block == loop->GetHeader()))
{
loop->VisitLoopBlocksReversePostOrder(visitBlock);
}
else
{
visitBlock(block);
}
}

return new (this, CMK_DepthFirstSearch) FlowGraphDfsTree(this, loopAwarePostOrder, numBlocks, /* hasCycle */ true);
}

//------------------------------------------------------------------------
// fgInvalidateDfsTree: Invalidate computed DFS tree and dependent annotations
// (like loops, dominators and SSA).
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/jit/lsra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -949,10 +949,11 @@ void LinearScan::setBlockSequence()
#endif // DEBUG

// Initialize the "visited" blocks set.
bbVisitedSet = BlockSetOps::MakeEmpty(compiler);
bbVisitedSet = BlockSetOps::MakeEmpty(compiler);
compiler->m_dfsTree = compiler->fgComputeLoopAwareDfs();

assert((blockSequence == nullptr) && (bbSeqCount == 0));
FlowGraphDfsTree* const dfsTree = compiler->fgComputeDfs</* useProfile */ true>();
FlowGraphDfsTree* const dfsTree = compiler->m_dfsTree;
blockSequence = dfsTree->GetPostOrder();
bbNumMaxBeforeResolution = compiler->fgBBNumMax;
blockInfo = new (compiler, CMK_LSRA) LsraBlockInfo[bbNumMaxBeforeResolution + 1];
Expand Down
5 changes: 4 additions & 1 deletion src/coreclr/jit/phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ void Phase::PostPhase(PhaseStatus status)
comp->fgDebugCheckLinkedLocals();
}

comp->fgDebugCheckFlowGraphAnnotations();
if (hasFlag(checks, PhaseChecks::CHECK_FG_ANNOTATIONS))
{
comp->fgDebugCheckFlowGraphAnnotations();
}
}
#endif // DEBUG
}
Loading