Skip to content
Closed
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
4 changes: 4 additions & 0 deletions src/coreclr/jit/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5150,6 +5150,10 @@ void Compiler::compCompile(void** methodCodePtr, uint32_t* methodCodeSize, JitFl
// Conditional to Switch conversion
//
DoPhase(this, PHASE_SWITCH_RECOGNITION, &Compiler::optSwitchRecognition);

// Run profile repair
//
DoPhase(this, PHASE_REPAIR_PROFILE, &Compiler::fgRepairProfile);
}

#ifdef DEBUG
Expand Down
12 changes: 11 additions & 1 deletion src/coreclr/jit/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -1935,12 +1935,16 @@ class FlowGraphDfsTree
// Whether the DFS that produced the tree found any backedges.
bool m_hasCycle;

// Whether the DFS that produced the tree used edge likelihoods to influence successor visitation order.
bool m_profileAware;

public:
FlowGraphDfsTree(Compiler* comp, BasicBlock** postOrder, unsigned postOrderCount, bool hasCycle)
FlowGraphDfsTree(Compiler* comp, BasicBlock** postOrder, unsigned postOrderCount, bool hasCycle, bool profileAware)
: m_comp(comp)
, m_postOrder(postOrder)
, m_postOrderCount(postOrderCount)
, m_hasCycle(hasCycle)
, m_profileAware(profileAware)
{
}

Expand Down Expand Up @@ -1975,6 +1979,11 @@ class FlowGraphDfsTree
return m_hasCycle;
}

bool IsProfileAware() const
{
return m_profileAware;
}

#ifdef DEBUG
void Dump() const;
#endif // DEBUG
Expand Down Expand Up @@ -6617,6 +6626,7 @@ class Compiler
}

void fgRemoveProfileData(const char* reason);
PhaseStatus fgRepairProfile();
void fgRepairProfileCondToUncond(BasicBlock* block, FlowEdge* retainedEdge, FlowEdge* removedEdge, int* metric = nullptr);

//-------- Insert a statement at the start or end of a basic block --------
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/jit/compphases.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ CompPhaseNameMacro(PHASE_INSERT_GC_POLLS, "Insert GC Polls",
CompPhaseNameMacro(PHASE_CREATE_THROW_HELPERS, "Create throw helper blocks", false, -1, true)
CompPhaseNameMacro(PHASE_DETERMINE_FIRST_COLD_BLOCK, "Determine first cold block", false, -1, true)
CompPhaseNameMacro(PHASE_RATIONALIZE, "Rationalize IR", false, -1, false)
CompPhaseNameMacro(PHASE_REPAIR_PROFILE, "Repair profile", false, -1, false)

CompPhaseNameMacro(PHASE_LCLVARLIVENESS, "Local var liveness", true, -1, false)
CompPhaseNameMacro(PHASE_LCLVARLIVENESS_INIT, "Local var liveness init", false, PHASE_LCLVARLIVENESS, false)
Expand Down
27 changes: 21 additions & 6 deletions src/coreclr/jit/fgdiagnostic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4735,15 +4735,30 @@ void Compiler::fgDebugCheckFlowGraphAnnotations()
return;
}

unsigned count = fgRunDfs(
[](BasicBlock* block, unsigned preorderNum) {
auto visitPreorder = [](BasicBlock* block, unsigned preorderNum) {
assert(block->bbPreorderNum == preorderNum);
},
[=](BasicBlock* block, unsigned postorderNum) {
};

auto visitPostorder = [=](BasicBlock* block, unsigned postorderNum) {
assert(block->bbPostorderNum == postorderNum);
assert(m_dfsTree->GetPostOrder(postorderNum) == block);
},
[](BasicBlock* block, BasicBlock* succ) {});
};

auto visitEdge = [](BasicBlock* block, BasicBlock* succ) {};

unsigned count;
if (m_dfsTree->IsProfileAware())
{
count = fgRunDfs<decltype(visitPreorder), decltype(visitPostorder), decltype(visitEdge), true>(visitPreorder,
visitPostorder,
visitEdge);
}
else
{
count = fgRunDfs<decltype(visitPreorder), decltype(visitPostorder), decltype(visitEdge), false>(visitPreorder,
visitPostorder,
visitEdge);
}

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

Expand Down
16 changes: 9 additions & 7 deletions src/coreclr/jit/fgopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4663,10 +4663,6 @@ void Compiler::fgDoReversePostOrderLayout()
}
#endif // DEBUG

// Compute DFS of all blocks in the method, using profile data to determine the order successors are visited in.
//
m_dfsTree = fgComputeDfs</* useProfile */ true>();

// If LSRA didn't create any new blocks, we can reuse its loop-aware RPO traversal,
// which is cached in Compiler::fgBBs.
// If the cache isn't available, we need to recompute the loop-aware RPO.
Expand All @@ -4675,15 +4671,21 @@ void Compiler::fgDoReversePostOrderLayout()

if (rpoSequence == nullptr)
{
rpoSequence = new (this, CMK_BasicBlock) BasicBlock*[m_dfsTree->GetPostOrderCount()];
assert(m_dfsTree == nullptr);
m_dfsTree = fgComputeDfs</* useProfile */ true>();
FlowGraphNaturalLoops* const loops = FlowGraphNaturalLoops::Find(m_dfsTree);
unsigned index = 0;
auto addToSequence = [rpoSequence, &index](BasicBlock* block) {
rpoSequence = new (this, CMK_BasicBlock) BasicBlock*[m_dfsTree->GetPostOrderCount()];
unsigned index = 0;
auto addToSequence = [rpoSequence, &index](BasicBlock* block) {
rpoSequence[index++] = block;
};

fgVisitBlocksInLoopAwareRPO(m_dfsTree, loops, addToSequence);
}
else
{
assert(m_dfsTree != nullptr);
}

// Fast path: We don't have any EH regions, so just reorder the blocks
//
Expand Down
36 changes: 35 additions & 1 deletion src/coreclr/jit/fgprofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4977,9 +4977,43 @@ bool Compiler::fgDebugCheckOutgoingProfileData(BasicBlock* block, ProfileChecks

#endif // DEBUG

//------------------------------------------------------------------------
// fgRepairProfile: If we have PGO data and the profile is inconsistent,
// run synthesis to re-establish consistency.
//
// Returns:
// PhaseStatus indicating if profile synthesis ran or not.
//
PhaseStatus Compiler::fgRepairProfile()
{
if (fgIsUsingProfileWeights())
{
if (fgPgoConsistent)
{
JITDUMP("Profile is already consistent.\n");
}
else
{
ProfileSynthesis::Run(this, ProfileSynthesisOption::RetainLikelihoods);

// Profile synthesis computes a profile-aware DFS tree.
// Note that we don't invalidate the tree here, as we opted to retain likelihoods,
// so the traversal order shouldn't be different after repairing the profile.
// Thus, future phases can reuse this tree.
return PhaseStatus::MODIFIED_EVERYTHING;
}
}
else
{
JITDUMP("No PGO data. Skipping profile repair.\n");
}

return PhaseStatus::MODIFIED_NOTHING;
}

//------------------------------------------------------------------------
// fgRepairProfileCondToUncond: attempt to repair profile after modifying
// a conditinal branch to an unconditional branch.
// a conditional branch to an unconditional branch.
//
// Arguments:
// block - block that was just altered
Expand Down
18 changes: 14 additions & 4 deletions src/coreclr/jit/fgprofilesynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@
//
void ProfileSynthesis::Run(ProfileSynthesisOption option)
{
m_dfsTree = m_comp->fgComputeDfs();
m_loops = FlowGraphNaturalLoops::Find(m_dfsTree);
m_improperLoopHeaders = m_loops->ImproperLoopHeaders();
m_entryBlock = m_comp->opts.IsOSR() ? m_comp->fgEntryBB : m_comp->fgFirstBB;
if (m_dfsTree == nullptr)
{
m_dfsTree = m_comp->fgComputeDfs</* useProfile */ true>();
m_loops = FlowGraphNaturalLoops::Find(m_dfsTree);
m_improperLoopHeaders = m_loops->ImproperLoopHeaders();
}
else
{
assert(m_loops != nullptr);
}

// Profile synthesis can be run before or after morph, so tolerate (non-)canonical method entries
//
m_entryBlock = (m_comp->opts.IsOSR() && (m_comp->fgEntryBB != nullptr)) ? m_comp->fgEntryBB : m_comp->fgFirstBB;

// Retain or compute edge likelihood information
//
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 @@ -4353,7 +4353,7 @@ FlowGraphDfsTree* Compiler::fgComputeDfs()
fgRunDfs<decltype(visitPreorder), decltype(visitPostorder), decltype(visitEdge), useProfile>(visitPreorder,
visitPostorder,
visitEdge);
return new (this, CMK_DepthFirstSearch) FlowGraphDfsTree(this, postOrder, numBlocks, hasCycle);
return new (this, CMK_DepthFirstSearch) FlowGraphDfsTree(this, postOrder, numBlocks, hasCycle, useProfile);
}

// Add explicit instantiations.
Expand Down
20 changes: 17 additions & 3 deletions src/coreclr/jit/lsra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,13 +947,26 @@ void LinearScan::setBlockSequence()
bbVisitedSet = BitVecOps::MakeEmpty(traits);

assert((blockSequence == nullptr) && (bbSeqCount == 0));
FlowGraphDfsTree* const dfsTree = compiler->fgComputeDfs</* useProfile */ true>();

if (compiler->m_dfsTree == nullptr)
{
assert(compiler->m_loops == nullptr);
compiler->m_dfsTree = compiler->fgComputeDfs</* useProfile */ true>();
}

FlowGraphDfsTree* const dfsTree = compiler->m_dfsTree;
blockSequence = new (compiler, CMK_LSRA) BasicBlock*[compiler->fgBBcount];

if (compiler->opts.OptimizationEnabled() && dfsTree->HasCycle())
{
// Ensure loop bodies are compact in the visitation order
FlowGraphNaturalLoops* const loops = FlowGraphNaturalLoops::Find(dfsTree);
// Ensure loop bodies are compact in the visitation order.
// We might need to recompute loops if they weren't retained from previous phases.
if (compiler->m_loops == nullptr)
{
compiler->m_loops = FlowGraphNaturalLoops::Find(dfsTree);
}

FlowGraphNaturalLoops* const loops = compiler->m_loops;
unsigned index = 0;

auto addToSequence = [this, &index](BasicBlock* block) {
Expand Down Expand Up @@ -1319,6 +1332,7 @@ PhaseStatus LinearScan::doLinearScan()
{
assert(compiler->fgBBcount > bbSeqCount);
compiler->fgBBs = nullptr;
compiler->fgInvalidateDfsTree();
}

return PhaseStatus::MODIFIED_EVERYTHING;
Expand Down
Loading