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: Use post order computed by SSA in VN #94623

Merged
merged 1 commit into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4493,6 +4493,8 @@ class Compiler
unsigned fgBBNumMax; // The max bbNum that has been assigned to basic blocks
unsigned fgDomBBcount; // # of BBs for which we have dominator and reachability information
BasicBlock** fgBBReversePostorder; // Blocks in reverse postorder
BasicBlock** fgSSAPostOrder; // Blocks in postorder, computed during SSA
unsigned fgSSAPostOrderCount; // Number of blocks in fgSSAPostOrder

// After the dominance tree is computed, we cache a DFS preorder number and DFS postorder number to compute
// dominance queries in O(1). fgDomTreePreOrder and fgDomTreePostOrder are arrays giving the block's preorder and
Expand Down Expand Up @@ -5057,7 +5059,7 @@ class Compiler

// The value numbers for this compilation.
ValueNumStore* vnStore;
struct ValueNumberState* vnVisitState;
class ValueNumberState* vnState;

public:
ValueNumStore* GetValueNumStore()
Expand Down Expand Up @@ -5723,7 +5725,7 @@ class Compiler

protected:
friend class SsaBuilder;
friend struct ValueNumberState;
friend class ValueNumberState;

//--------------------- Detect the basic blocks ---------------------------

Expand Down
1 change: 0 additions & 1 deletion src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,6 @@ bool Compiler::fgRemoveDeadBlocks()
// Notes:
// Each block's `bbPreorderNum` and `bbPostorderNum` is set.
// The `fgBBReversePostorder` array is filled in with the `BasicBlock*` in reverse post-order.
// This algorithm only pays attention to the actual blocks. It ignores any imaginary entry block.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't totally sure what this was referring to... but today we do ensure all blocks are visited below, so it seemed odd.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SSA creates a stack-allocated BB0 when it computes dominators, I suspect this is the "imaginary entry block."

Copy link
Member Author

@jakobbotsch jakobbotsch Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, might be. Still, not sure it is too relevant for this computation.

//
// Unreachable blocks will have higher pre and post order numbers than reachable blocks.
// Hence they will appear at lower indices in the fgBBReversePostorder array.
Expand Down
28 changes: 9 additions & 19 deletions src/coreclr/jit/ssabuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ PhaseStatus Compiler::fgSsaBuild()
JitTestCheckSSA();
#endif // DEBUG

fgSSAPostOrder = builder.GetPostOrder(&fgSSAPostOrderCount);

return PhaseStatus::MODIFIED_EVERYTHING;
}

Expand Down Expand Up @@ -144,7 +146,7 @@ SsaBuilder::SsaBuilder(Compiler* pCompiler)
// Return Value:
// The number of nodes visited while performing DFS on the graph.
//
int SsaBuilder::TopologicalSort(BasicBlock** postOrder, int count)
unsigned SsaBuilder::TopologicalSort(BasicBlock** postOrder, int count)
{
Compiler* comp = m_pCompiler;

Expand Down Expand Up @@ -179,7 +181,7 @@ int SsaBuilder::TopologicalSort(BasicBlock** postOrder, int count)
};

// Compute order.
int postIndex = 0;
unsigned postIndex = 0;
BasicBlock* block = comp->fgFirstBB;
BitVecOps::AddElemD(&m_visitedTraits, m_visited, block->bbNum);

Expand All @@ -206,16 +208,13 @@ int SsaBuilder::TopologicalSort(BasicBlock** postOrder, int count)
// all successors have been visited
blocks.Pop();

DBG_SSA_JITDUMP("[SsaBuilder::TopologicalSort] postOrder[%d] = " FMT_BB "\n", postIndex, block->bbNum);
DBG_SSA_JITDUMP("[SsaBuilder::TopologicalSort] postOrder[%u] = " FMT_BB "\n", postIndex, block->bbNum);
postOrder[postIndex] = block;
block->bbPostorderNum = postIndex;
postIndex++;
}
}

// In the absence of EH (because catch/finally have no preds), this should be valid.
// assert(postIndex == (count - 1));

return postIndex;
}

Expand Down Expand Up @@ -1500,16 +1499,7 @@ void SsaBuilder::Build()

// Allocate the postOrder array for the graph.

BasicBlock** postOrder;

if (blockCount > DEFAULT_MIN_OPTS_BB_COUNT)
{
postOrder = new (m_allocator) BasicBlock*[blockCount];
}
else
{
postOrder = (BasicBlock**)_alloca(blockCount * sizeof(BasicBlock*));
}
m_postOrder = new (m_allocator) BasicBlock*[blockCount];

m_visitedTraits = BitVecTraits(blockCount, m_pCompiler);
m_visited = BitVecOps::MakeEmpty(&m_visitedTraits);
Expand All @@ -1527,12 +1517,12 @@ void SsaBuilder::Build()
}

// Topologically sort the graph.
int count = TopologicalSort(postOrder, blockCount);
m_postOrderCount = TopologicalSort(m_postOrder, blockCount);
JITDUMP("[SsaBuilder] Topologically sorted the graph.\n");
EndPhase(PHASE_BUILD_SSA_TOPOSORT);

// Compute IDom(b).
ComputeImmediateDom(postOrder, count);
ComputeImmediateDom(m_postOrder, m_postOrderCount);

m_pCompiler->fgSsaDomTree = m_pCompiler->fgBuildDomTree();
EndPhase(PHASE_BUILD_SSA_DOMS);
Expand All @@ -1551,7 +1541,7 @@ void SsaBuilder::Build()
}

// Insert phi functions.
InsertPhiFunctions(postOrder, count);
InsertPhiFunctions(m_postOrder, m_postOrderCount);

// Rename local variables and collect UD information for each ssa var.
RenameVariables();
Expand Down
10 changes: 9 additions & 1 deletion src/coreclr/jit/ssabuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ class SsaBuilder
// variable are stored in the "per SSA data" on the local descriptor.
void Build();

BasicBlock** GetPostOrder(unsigned* count)
{
*count = m_postOrderCount;
return m_postOrder;
}

private:
// Ensures that the basic block graph has a root for the dominator graph, by ensuring
// that there is a first block that is not in a try region (adding an empty block for that purpose
Expand All @@ -44,7 +50,7 @@ class SsaBuilder
// the blocks in post order (i.e., a node's children first) in the array. Returns the
// number of nodes visited while sorting the graph. In other words, valid entries in
// the output array.
int TopologicalSort(BasicBlock** postOrder, int count);
unsigned TopologicalSort(BasicBlock** postOrder, int count);

// Requires "postOrder" to hold the blocks of the flowgraph in topologically sorted
// order. Requires count to be the valid entries in the "postOrder" array. Computes
Expand Down Expand Up @@ -101,4 +107,6 @@ class SsaBuilder
BitVec m_visited;

SsaRenameState m_renameStack;
BasicBlock** m_postOrder = nullptr;
unsigned m_postOrderCount = 0;
};
Loading