@@ -41,6 +41,11 @@ void ProfileSynthesis::Run(ProfileSynthesisOption option)
4141 assert (m_loops != nullptr );
4242 }
4343
44+ if (m_loops->NumLoops () > 0 )
45+ {
46+ m_cyclicProbabilities = new (m_comp, CMK_Pgo) weight_t [m_loops->NumLoops ()];
47+ }
48+
4449 // Profile synthesis can be run before or after morph, so tolerate (non-)canonical method entries
4550 //
4651 m_entryBlock = (m_comp->opts .IsOSR () && (m_comp->fgEntryBB != nullptr )) ? m_comp->fgEntryBB : m_comp->fgFirstBB ;
@@ -168,7 +173,10 @@ void ProfileSynthesis::Run(ProfileSynthesisOption option)
168173 if (m_approximate)
169174 {
170175 JITDUMP (" Profile is inconsistent. Bypassing post-phase consistency checks.\n " );
171- m_comp->Metrics .ProfileInconsistentInitially ++;
176+ if (!m_comp->fgImportDone )
177+ {
178+ m_comp->Metrics .ProfileInconsistentInitially ++;
179+ }
172180 }
173181
174182 // Derive the method's call count from the entry block's weight
@@ -208,7 +216,7 @@ void ProfileSynthesis::Run(ProfileSynthesisOption option)
208216 // Leave a note so we can bypass the post-phase check, and
209217 // instead assert at the end of fgImport, if we make it that far.
210218 //
211- if (!isConsistent)
219+ if (!isConsistent && !m_comp-> fgImportDone )
212220 {
213221 m_comp->fgPgoDeferredInconsistency = true ;
214222 JITDUMP (" Will defer asserting until after importation\n " );
@@ -745,13 +753,6 @@ void ProfileSynthesis::RandomizeLikelihoods()
745753//
746754void ProfileSynthesis::ComputeCyclicProbabilities ()
747755{
748- m_cyclicProbabilities = nullptr ;
749- if (m_loops->NumLoops () == 0 )
750- {
751- return ;
752- }
753-
754- m_cyclicProbabilities = new (m_comp, CMK_Pgo) weight_t [m_loops->NumLoops ()];
755756 // Walk loops in post order to visit inner loops before outer loops.
756757 for (FlowGraphNaturalLoop* loop : m_loops->InPostOrder ())
757758 {
@@ -828,10 +829,7 @@ void ProfileSynthesis::ComputeCyclicProbabilities(FlowGraphNaturalLoop* loop)
828829
829830 for (FlowEdge* const edge : nestedLoop->EntryEdges ())
830831 {
831- if (BasicBlock::sameHndRegion (block, edge->getSourceBlock ()))
832- {
833- newWeight += edge->getLikelyWeight ();
834- }
832+ newWeight += edge->getLikelyWeight ();
835833 }
836834
837835 newWeight *= m_cyclicProbabilities[nestedLoop->GetIndex ()];
@@ -847,10 +845,10 @@ void ProfileSynthesis::ComputeCyclicProbabilities(FlowGraphNaturalLoop* loop)
847845 {
848846 BasicBlock* const sourceBlock = edge->getSourceBlock ();
849847
850- // Ignore flow across EH, or from preds not in the loop.
851- // Latter can happen if there are unreachable blocks that flow into the loop.
848+ // Ignore flow from preds not in the loop.
849+ // This can happen if there are unreachable blocks that flow into the loop.
852850 //
853- if (BasicBlock::sameHndRegion (block, sourceBlock) && loop->ContainsBlock (sourceBlock))
851+ if (loop->ContainsBlock (sourceBlock))
854852 {
855853 newWeight += edge->getLikelyWeight ();
856854 }
@@ -1214,7 +1212,8 @@ void ProfileSynthesis::GaussSeidelSolver()
12141212 const FlowGraphDfsTree* const dfs = m_loops->GetDfsTree ();
12151213 unsigned const blockCount = dfs->GetPostOrderCount ();
12161214 bool checkEntryExitWeight = true ;
1217- bool showDetails = false ;
1215+ bool const showDetails = false ;
1216+ bool const callFinalliesCreated = m_comp->fgImportDone ;
12181217
12191218 JITDUMP (" Synthesis solver: flow graph has %u improper loop headers\n " , m_improperLoopHeaders);
12201219
@@ -1290,9 +1289,10 @@ void ProfileSynthesis::GaussSeidelSolver()
12901289 {
12911290 newWeight = block->bbWeight ;
12921291
1293- // Finallies also add in the weight of their try.
1292+ // If we haven't added flow edges into/out of finallies yet,
1293+ // add in the weight of their corresponding try regions.
12941294 //
1295- if (ehDsc->HasFinallyHandler ())
1295+ if (!callFinalliesCreated && ehDsc->HasFinallyHandler ())
12961296 {
12971297 newWeight += countVector[ehDsc->ebdTryBeg ->bbNum ];
12981298 }
@@ -1318,11 +1318,7 @@ void ProfileSynthesis::GaussSeidelSolver()
13181318 for (FlowEdge* const edge : loop->EntryEdges ())
13191319 {
13201320 BasicBlock* const predBlock = edge->getSourceBlock ();
1321-
1322- if (BasicBlock::sameHndRegion (block, predBlock))
1323- {
1324- newWeight += edge->getLikelihood () * countVector[predBlock->bbNum ];
1325- }
1321+ newWeight += edge->getLikelihood () * countVector[predBlock->bbNum ];
13261322 }
13271323
13281324 // Scale by cyclic probability
@@ -1354,10 +1350,7 @@ void ProfileSynthesis::GaussSeidelSolver()
13541350 continue ;
13551351 }
13561352
1357- if (BasicBlock::sameHndRegion (block, predBlock))
1358- {
1359- newWeight += edge->getLikelihood () * countVector[predBlock->bbNum ];
1360- }
1353+ newWeight += edge->getLikelihood () * countVector[predBlock->bbNum ];
13611354 }
13621355
13631356 if (selfEdge != nullptr )
@@ -1444,10 +1437,12 @@ void ProfileSynthesis::GaussSeidelSolver()
14441437 }
14451438 }
14461439
1447- // If there were no improper headers, we will have converged in one pass.
1440+ // If there were no improper headers, we will have converged in one pass
14481441 // (profile may still be inconsistent, if there were capped cyclic probabilities).
1442+ // After the importer runs, we require that synthesis achieves profile consistency
1443+ // unless the resultant profile is approximate, so don't skip the below checks.
14491444 //
1450- if (m_improperLoopHeaders == 0 )
1445+ if (( m_improperLoopHeaders == 0 ) && !m_comp-> fgImportDone )
14511446 {
14521447 converged = true ;
14531448 break ;
0 commit comments