From d11a72f583fb9d7092e2cfa2c22086ccf89fb6f2 Mon Sep 17 00:00:00 2001 From: fye Date: Thu, 1 Dec 2022 14:42:34 -0800 Subject: [PATCH] PGO: frequency maiintenance on lowering while and if stmt --- src/mapleall/maple_me/src/me_cfg.cpp | 2 +- src/mapleall/maple_me/src/pme_emit.cpp | 4 +++- src/mapleall/maple_me/src/pme_mir_lower.cpp | 18 +++++++++++++++--- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index b2e0986c39..87320d4744 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1882,7 +1882,7 @@ inline void ConstructEdgeFreqForBBWith2Succs(BB &bb) { // special case: WontExitAnalysis() has pushed 0 to bb->succFreq bb.GetSuccFreq()[0] = bb.GetFrequency(); bb.PushBackSuccFreq(0); - } else if (bb.GetFrequency() >= succ1Freq) { // tolerate inaccuracy + } else if (bb.GetFrequency() <= succ1Freq) { // tolerate inaccuracy bb.PushBackSuccFreq(0); bb.PushBackSuccFreq(bb.GetFrequency()); } else { diff --git a/src/mapleall/maple_me/src/pme_emit.cpp b/src/mapleall/maple_me/src/pme_emit.cpp index a567e52090..cab8aab318 100644 --- a/src/mapleall/maple_me/src/pme_emit.cpp +++ b/src/mapleall/maple_me/src/pme_emit.cpp @@ -833,7 +833,9 @@ void PreMeEmitter::EmitBB(BB *bb, BlockNode *curBlk) { if (setLastFreq) { GetFuncProfData()->SetStmtFreq(curBlk->GetLast()->GetStmtID(), bb->GetFrequency()); } else if (bbIsEmpty) { - LogInfo::MapleLogger() << " bb " << bb->GetBBId() << ": no stmt used to add frequency; added comment node\n"; + if (!MeOption::quiet) { + LogInfo::MapleLogger() << " bb " << bb->GetBBId() << ": no stmt used to add frequency; added comment node\n"; + } CommentNode *commentNode = codeMP->New(*(mirFunc->GetModule())); commentNode->SetComment("freqStmt"+std::to_string(commentNode->GetStmtID())); GetFuncProfData()->SetStmtFreq(commentNode->GetStmtID(), bb->GetFrequency()); diff --git a/src/mapleall/maple_me/src/pme_mir_lower.cpp b/src/mapleall/maple_me/src/pme_mir_lower.cpp index c30a981218..a7f05476b6 100644 --- a/src/mapleall/maple_me/src/pme_mir_lower.cpp +++ b/src/mapleall/maple_me/src/pme_mir_lower.cpp @@ -71,9 +71,11 @@ BlockNode *PreMeMIRLower::LowerWhileStmt(WhileStmtNode &whileStmt) { endlblstmt->SetSrcPos(pos); blk->AddStatement(endlblstmt); if (GetFuncProfData()) { + CHECK_FATAL(GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) >= + GetFuncProfData()->GetStmtFreq(whilegotonode->GetStmtID()), "inconsistent profiling on while stmt"); int64_t freq = GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) - GetFuncProfData()->GetStmtFreq(whilegotonode->GetStmtID()); - GetFuncProfData()->SetStmtFreq(endlblstmt->GetStmtID(), freq > 0 ? freq : 0); + GetFuncProfData()->SetStmtFreq(endlblstmt->GetStmtID(), freq); } return blk; } @@ -240,7 +242,15 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { // set stmtfreqs if (GetFuncProfData()) { if (!thenempty) { - GetFuncProfData()->CopyStmtFreq(gotostmt->GetStmtID(), ifstmt.GetThenPart()->GetStmtID()); + if (ifstmt.GetThenPart()->GetLast()->IsCondBr()) { + // Estimate a freq within [0, ifstmt-freq] without going after further + uint64 ifFreq = GetFuncProfData()->GetStmtFreq(ifstmt.GetStmtID()); + uint64 ifElseFreq = GetFuncProfData()->GetStmtFreq(ifstmt.GetElsePart()->GetStmtID()); + uint64 freqDiff = (ifFreq >= ifElseFreq) ? (ifFreq - ifElseFreq) : 0; + GetFuncProfData()->SetStmtFreq(gotostmt->GetStmtID(), freqDiff); + } else { + GetFuncProfData()->CopyStmtFreq(gotostmt->GetStmtID(), ifstmt.GetThenPart()->GetStmtID()); + } } else { GetFuncProfData()->SetStmtFreq(gotostmt->GetStmtID(), 0); } @@ -252,7 +262,9 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { SrcPosition pos = func->GetMirFunc()->GetScope()->GetScopeEndPos(ifstmt.GetThenPart()->GetSrcPos()); labstmt->SetSrcPos(pos); blk->AddStatement(labstmt); - + if (GetFuncProfData()) { + GetFuncProfData()->CopyStmtFreq(labstmt->GetStmtID(), ifstmt.GetElsePart()->GetStmtID()); + } if (!elseempty) { blk->AppendStatementsFromBlock(*ifstmt.GetElsePart()); } -- Gitee