From 54c8e5ecf4f49aa4f14481d3320339fcab72c3f9 Mon Sep 17 00:00:00 2001 From: fye Date: Fri, 6 May 2022 09:05:22 -0700 Subject: [PATCH] PGO: split critical edges --- .../maple_ipa/src/ipa_phase_manager.cpp | 1 + .../maple_me/include/me_pgo_instrument.h | 14 ++++++++++++-- src/mapleall/maple_me/src/me_critical_edge.cpp | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/mapleall/maple_ipa/src/ipa_phase_manager.cpp b/src/mapleall/maple_ipa/src/ipa_phase_manager.cpp index 8cf4d85bcd..05bc4b00fa 100644 --- a/src/mapleall/maple_ipa/src/ipa_phase_manager.cpp +++ b/src/mapleall/maple_ipa/src/ipa_phase_manager.cpp @@ -194,6 +194,7 @@ bool SCCProfile::PhaseRun(SCCNode &scc) { SetQuiet(true); AddPhase("mecfgbuild", true); if (Options::profileGen) { + AddPhase("splitcriticaledge", true); AddPhase("profileGen", true); } else { AddPhase("profileUse", true); diff --git a/src/mapleall/maple_me/include/me_pgo_instrument.h b/src/mapleall/maple_me/include/me_pgo_instrument.h index 9cefde6cc9..ba6727676b 100644 --- a/src/mapleall/maple_me/include/me_pgo_instrument.h +++ b/src/mapleall/maple_me/include/me_pgo_instrument.h @@ -93,6 +93,8 @@ class PGOInstrument { void GetInstrumentBBs(std::vector &bbs) const { std::vector instrumentEdges; mst.GetInstrumentEdges(instrumentEdges); + std::unordered_set visitedBBs; + for (auto &edge : instrumentEdges) { BB *src = edge->GetSrcBB(); BB *dest = edge->GetDestBB(); @@ -107,8 +109,16 @@ class PGOInstrument { bbs.push_back(dest); } else { if (func->GetMIRModule().IsCModule()) { - // If it is a c module, do not split the edge until the next PGO phase - bbs.push_back(dest); + if (src->GetKind() == kBBIgoto) { + if (visitedBBs.find(dest) == visitedBBs.end()) { + // In this case, we have to instrument it anyway + bbs.push_back(dest); + visitedBBs.insert(dest); + } + } else { + func->GetCfg()->DumpToFile("profGenErrorForCModule", false); + CHECK_FATAL(false, "Unexpected case %d -> %d", src->UintID(), dest->UintID()); + } } else { func->GetCfg()->DumpToFile("profGenError", false); CHECK_FATAL(false, "impossible critial edge %d -> %d", src->UintID(), dest->UintID()); diff --git a/src/mapleall/maple_me/src/me_critical_edge.cpp b/src/mapleall/maple_me/src/me_critical_edge.cpp index c37fe842a8..b04a72496f 100644 --- a/src/mapleall/maple_me/src/me_critical_edge.cpp +++ b/src/mapleall/maple_me/src/me_critical_edge.cpp @@ -19,6 +19,7 @@ #include "me_dominance.h" #include "me_function.h" #include "me_loop_analysis.h" +#include "me_irmap.h" // This phase finds critical edges and split them into two, because their // presence would restrict the optimizations performed by SSAPRE-based phases. @@ -175,6 +176,23 @@ void MeSplitCEdge::BreakCriticalEdge(MeFunction &func, BB &pred, BB &succ) const UpdateCaseLabel(*newBB, func, pred, succ); } } + // profileGen invokes MESplitCEdge to remove critical edges without going through ME completely + // newBB needs to be materialized + if (Options::profileGen) { + LabelIdx succLabel = succ.GetBBLabel(); + ASSERT(succLabel != 0, "succ's label missed"); + if (func.GetIRMap() != nullptr) { + GotoNode stmt(OP_goto); + GotoMeStmt *gotoSucc = func.GetIRMap()->New(&stmt); + gotoSucc->SetOffset(succLabel); + newBB->AddMeStmtLast(gotoSucc); + } else { + GotoNode *gotoSucc = func.GetMirFunc()->GetCodeMempool()->New(OP_goto); + gotoSucc->SetOffset(succLabel); + newBB->AddStmtNode(gotoSucc); + } + newBB->SetKind(kBBGoto); + } } // Is a critical edge is cut by bb -- Gitee