From 5243906b2b41261b1395e617f6535e9603105e7a Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Fri, 18 Nov 2022 09:09:27 -0800 Subject: [PATCH 1/2] Fixed 3 more issues under PGO --- src/mapleall/maple_be/src/be/lower.cpp | 2 +- src/mapleall/maple_me/src/pme_mir_lower.cpp | 12 +++++++----- src/mapleall/mpl2mpl/src/inline.cpp | 2 +- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 83850176ef..2b7abc903f 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -1876,7 +1876,7 @@ BlockNode *CGLowerer::LowerBlock(BlockNode &block) { newLabelIdx = GetLabelIdx(*mirModule.CurFunction()); defaultLabel = mirBuilder->CreateStmtLabel(newLabelIdx); } - SwitchLowerer switchLowerer(mirModule, static_cast(*stmt), switchAllocator); + SwitchLowerer switchLowerer(mirModule, static_cast(*stmt), this, switchAllocator); BlockNode *blk = switchLowerer.LowerSwitch(newLabelIdx); if (blk->GetFirst() != nullptr && defaultLabel != nullptr && IsSwitchToRangeGoto(*blk)) { blk->AddStatement(defaultLabel); diff --git a/src/mapleall/maple_me/src/pme_mir_lower.cpp b/src/mapleall/maple_me/src/pme_mir_lower.cpp index c30a981218..9587b2832a 100644 --- a/src/mapleall/maple_me/src/pme_mir_lower.cpp +++ b/src/mapleall/maple_me/src/pme_mir_lower.cpp @@ -120,7 +120,7 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { if (GetFuncProfData()) { GetFuncProfData()->CopyStmtFreq(evalstmt->GetStmtID(), ifstmt.GetStmtID()); } - } else if (elseempty && !GetFuncProfData()) { + } else if (elseempty && !Options::profileUse && !Options::profileGen) { // brfalse // // label @@ -157,7 +157,7 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { GetFuncProfData()->GetStmtFreq(ifstmt.GetThenPart()->GetStmtID()); GetFuncProfData()->SetStmtFreq(labstmt->GetStmtID(), freq); } - } else if (thenempty && !GetFuncProfData()) { + } else if (thenempty && !Options::profileUse && !Options::profileGen) { // brtrue // // label @@ -286,14 +286,16 @@ BlockNode *PreMeMIRLower::LowerIfStmt(IfStmtNode &ifstmt, bool recursive) { } ifInfo->endLabel = endlabelidx; } - if (GetFuncProfData()) { + if (Options::profileUse || Options::profileGen) { // generate extra label to avoid critical edge LabelIdx extraLabelIdx = mirFunc->GetLabelTab()->CreateLabelWithPrefix('x'); preMeFunc->SetIfLabelCreatedByPreMe(extraLabelIdx); LabelNode *extraLabelNode = mirbuilder->CreateStmtLabel(extraLabelIdx); blk->AddStatement(extraLabelNode); - // set stmtfreqs - GetFuncProfData()->CopyStmtFreq(extraLabelNode->GetStmtID(), ifstmt.GetStmtID()); + if (GetFuncProfData()) { + // set stmtfreqs + GetFuncProfData()->CopyStmtFreq(extraLabelNode->GetStmtID(), ifstmt.GetStmtID()); + } } return blk; } diff --git a/src/mapleall/mpl2mpl/src/inline.cpp b/src/mapleall/mpl2mpl/src/inline.cpp index c3e5586082..66cd933afd 100644 --- a/src/mapleall/mpl2mpl/src/inline.cpp +++ b/src/mapleall/mpl2mpl/src/inline.cpp @@ -531,7 +531,7 @@ void MInline::InlineCallsBlockInternal(MIRFunction &func, BaseNode &baseNode, bo CallInfo *callInfo = cgNode->GetCallInfo(callStmt); std::pair canInlineRet = InlineAnalyzer::CanInlineImpl({&func, callee}, callStmt, *cg, currInlineDepth, true); // earlyInline: true - bool canInline = canInlineRet.first; + bool canInline = canInlineRet.first && callStmt.GetEnclosingBlock(); InlineFailedCode failCode = canInlineRet.second; if (callInfo != nullptr) { // cache result to avoid recompute callInfo->SetInlineFailedCode(failCode); -- Gitee From 2ee74586de661f75207675cacdc64ee3f215f874 Mon Sep 17 00:00:00 2001 From: fye Date: Wed, 23 Nov 2022 18:55:33 -0800 Subject: [PATCH 2/2] PGO: bug fixes on SwitchLowerer and MeCFG --- src/mapleall/maple_be/src/be/switch_lowerer.cpp | 2 +- src/mapleall/maple_me/src/me_cfg.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mapleall/maple_be/src/be/switch_lowerer.cpp b/src/mapleall/maple_be/src/be/switch_lowerer.cpp index d361660d44..b9f12d3a40 100644 --- a/src/mapleall/maple_be/src/be/switch_lowerer.cpp +++ b/src/mapleall/maple_be/src/be/switch_lowerer.cpp @@ -286,7 +286,7 @@ BlockNode *SwitchLowerer::BuildCodeForSwitchItems(int32 start, int32 end, bool l if (Options::profileUse && freqPriority) { for (std::pair f2c : freq2case) { uint32 idx = static_cast(f2c.second); - cGoto = BuildCondGotoNode(switchItems[idx].first, OP_brtrue, *BuildCmpNode(OP_eq, switchItems[idx].first)); + cGoto = BuildCondGotoNode(idx, OP_brtrue, *BuildCmpNode(OP_eq, idx)); if (cGoto != nullptr) { localBlk->AddStatement(cGoto); } diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index b2e0986c39..9d02e76718 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1935,14 +1935,14 @@ void MeCFG::ConstructBBFreqFromStmtFreq() { if ((*bIt)->IsEmpty()) continue; StmtNode &first = (*bIt)->GetFirst(); StmtNode &last = (*bIt)->GetLast(); - if (funcData->stmtFreqs.count(first.GetStmtID()) > 0) { + if (funcData->stmtFreqs.count(first.GetStmtID()) >= 0) { (*bIt)->SetFrequency(funcData->stmtFreqs[first.GetStmtID()]); - } else if (funcData->stmtFreqs.count(last.GetStmtID()) > 0) { + } else if (funcData->stmtFreqs.count(last.GetStmtID()) >= 0) { (*bIt)->SetFrequency(funcData->stmtFreqs[last.GetStmtID()]); } else { bool foundFreq = false; for (StmtNode &stmt : (*bIt)->GetStmtNodes()) { - if (funcData->stmtFreqs.count(stmt.GetStmtID()) > 0) { + if (funcData->stmtFreqs.count(stmt.GetStmtID()) >= 0) { (*bIt)->SetFrequency(funcData->stmtFreqs[stmt.GetStmtID()]); foundFreq = true; break; -- Gitee