Skip to content

Commit

Permalink
JIT: Expand flow graph debug checking (#99238)
Browse files Browse the repository at this point in the history
Ensure that the derived flow graph annotations, like loops and the
dominator tree, do not lose their link with the current DFS tree.
  • Loading branch information
jakobbotsch authored Mar 6, 2024
1 parent bb919c2 commit add19f1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
18 changes: 14 additions & 4 deletions src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2323,6 +2323,11 @@ class FlowGraphDominatorTree

static BasicBlock* IntersectDom(BasicBlock* block1, BasicBlock* block2);
public:
const FlowGraphDfsTree* GetDfsTree()
{
return m_dfsTree;
}

BasicBlock* Intersect(BasicBlock* block, BasicBlock* block2);
bool Dominates(BasicBlock* dominator, BasicBlock* dominated);

Expand Down Expand Up @@ -2357,23 +2362,28 @@ class BlockToNaturalLoopMap
// exceptional flow, then CanReach returns false.
class BlockReachabilitySets
{
FlowGraphDfsTree* m_dfsTree;
const FlowGraphDfsTree* m_dfsTree;
BitVec* m_reachabilitySets;

BlockReachabilitySets(FlowGraphDfsTree* dfsTree, BitVec* reachabilitySets)
BlockReachabilitySets(const FlowGraphDfsTree* dfsTree, BitVec* reachabilitySets)
: m_dfsTree(dfsTree)
, m_reachabilitySets(reachabilitySets)
{
}

public:
const FlowGraphDfsTree* GetDfsTree()
{
return m_dfsTree;
}

bool CanReach(BasicBlock* from, BasicBlock* to);

#ifdef DEBUG
void Dump();
#endif

static BlockReachabilitySets* Build(FlowGraphDfsTree* dfsTree);
static BlockReachabilitySets* Build(const FlowGraphDfsTree* dfsTree);
};

enum class FieldKindForVN
Expand Down Expand Up @@ -6095,7 +6105,7 @@ class Compiler
bool fgDebugCheckIncomingProfileData(BasicBlock* block, ProfileChecks checks);
bool fgDebugCheckOutgoingProfileData(BasicBlock* block, ProfileChecks checks);

void fgDebugCheckDfsTree();
void fgDebugCheckFlowGraphAnnotations();

#endif // DEBUG

Expand Down
15 changes: 13 additions & 2 deletions src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4744,10 +4744,17 @@ void Compiler::fgDebugCheckLoops()
}

//------------------------------------------------------------------------------
// fgDebugCheckDfsTree: Checks that the DFS tree matches the current flow graph.
// fgDebugCheckFlowGraphAnnotations: Checks that all flow graph annotations
// that are currently non-null are valid.
//
void Compiler::fgDebugCheckDfsTree()
void Compiler::fgDebugCheckFlowGraphAnnotations()
{
if (m_dfsTree == nullptr)
{
assert((m_loops == nullptr) && (m_domTree == nullptr) && (m_reachabilitySets == nullptr));
return;
}

unsigned count =
fgRunDfs([](BasicBlock* block, unsigned preorderNum) { assert(block->bbPreorderNum == preorderNum); },
[=](BasicBlock* block, unsigned postorderNum) {
Expand All @@ -4757,6 +4764,10 @@ void Compiler::fgDebugCheckDfsTree()
[](BasicBlock* block, BasicBlock* succ) {});

assert(m_dfsTree->GetPostOrderCount() == count);

assert((m_loops == nullptr) || (m_loops->GetDfsTree() == m_dfsTree));
assert((m_domTree == nullptr) || (m_domTree->GetDfsTree() == m_dfsTree));
assert((m_reachabilitySets == nullptr) || (m_reachabilitySets->GetDfsTree() == m_dfsTree));
}

/*****************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/jit/flowgraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6144,7 +6144,7 @@ BlockToNaturalLoopMap* BlockToNaturalLoopMap::Build(FlowGraphNaturalLoops* loops
// This algorithm consumes O(n^2) memory because we're using dense
// bitsets to represent reachability.
//
BlockReachabilitySets* BlockReachabilitySets::Build(FlowGraphDfsTree* dfsTree)
BlockReachabilitySets* BlockReachabilitySets::Build(const FlowGraphDfsTree* dfsTree)
{
Compiler* comp = dfsTree->GetCompiler();
BitVecTraits postOrderTraits = dfsTree->PostOrderTraits();
Expand Down
5 changes: 1 addition & 4 deletions src/coreclr/jit/phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,7 @@ void Phase::PostPhase(PhaseStatus status)
comp->fgDebugCheckLinkedLocals();
}

if (comp->m_dfsTree != nullptr)
{
comp->fgDebugCheckDfsTree();
}
comp->fgDebugCheckFlowGraphAnnotations();
}
#endif // DEBUG
}

0 comments on commit add19f1

Please sign in to comment.