diff --git a/src/mapleall/maple_be/include/cg/cg.h b/src/mapleall/maple_be/include/cg/cg.h index 8cc3b4e74ad85172c008d7f10cf07ac534bcec7a..5c48b3cff7fee5082fa538b6f5acb08ef30a1987 100644 --- a/src/mapleall/maple_be/include/cg/cg.h +++ b/src/mapleall/maple_be/include/cg/cg.h @@ -201,7 +201,7 @@ class CG { } bool DoPatchLongBranch() const { - return cgOption.DoPatchLongBranch(); + return cgOption.DoPatchLongBranch() || (Globals::GetInstance()->GetOptimLevel() == CGOptions::kLevel0); } uint8 GetRematLevel() const { diff --git a/src/mapleall/maple_driver/include/driver_options.h b/src/mapleall/maple_driver/include/driver_options.h index 61a57df5d896e03f2ebbe9a2aff3ae44cd27237c..a7dbccfd73fd639cf8b0ea89196c44d77e10afa6 100644 --- a/src/mapleall/maple_driver/include/driver_options.h +++ b/src/mapleall/maple_driver/include/driver_options.h @@ -72,6 +72,7 @@ extern maplecl::Option genMapleBC; extern maplecl::Option genLMBC; extern maplecl::Option profileGen; extern maplecl::Option profileUse; +extern maplecl::Option missingProfDataIsError; extern maplecl::Option stackProtectorStrong; extern maplecl::Option stackProtectorAll; extern maplecl::Option inlineAsWeak; diff --git a/src/mapleall/maple_driver/src/driver_options.cpp b/src/mapleall/maple_driver/src/driver_options.cpp index 1f035bc54bad20b2658233ac3e97edb44f40af0b..33b4a04bb1bcd13a41d950681f5fcd68b8ad9aa4 100644 --- a/src/mapleall/maple_driver/src/driver_options.cpp +++ b/src/mapleall/maple_driver/src/driver_options.cpp @@ -171,6 +171,12 @@ maplecl::Option profileUse({"--profileUse"}, " --profileUse \tOptimize static languages with profile data\n", {driverCategory, mpl2mplCategory}); +maplecl::Option missingProfDataIsError({"--missing-profdata-is-error"}, + " --missing-profdata-is-error \tTreat missing profile data file as error\n" + " --no-missing-profdata-is-error \tOnly warn on missing profile data file\n", + {driverCategory}, + maplecl::DisableWith("--no-missing-profdata-is-error"), + maplecl::Init(true)); maplecl::Option stackProtectorStrong({"--stack-protector-strong"}, " --stack-protector-strong \tadd stack guard for some function\n", {driverCategory, meCategory, cgCategory}); diff --git a/src/mapleall/maple_ipa/src/ipa_clone.cpp b/src/mapleall/maple_ipa/src/ipa_clone.cpp index f164000454a4a42c418812cf1115a872f0dfed4e..fcbcf84c832052263b51a4cc6d1b02dceed3f898 100644 --- a/src/mapleall/maple_ipa/src/ipa_clone.cpp +++ b/src/mapleall/maple_ipa/src/ipa_clone.cpp @@ -395,6 +395,9 @@ void IpaClone::DecideCloneFunction(std::vector &result, uint32 paramInd if (oldCallNode == nullptr) { continue; } + if (callerFunc->GetFuncProfData() == nullptr) { + continue; + } uint64_t callsiteFreq = callerFunc->GetFuncProfData()->GetStmtFreq(stmtId); clonedSiteFreqs += callsiteFreq; } @@ -546,6 +549,9 @@ void IpaClone::CloneNoImportantExpressFunction(MIRFunction *func, uint32 paramIn if (oldCallNode == nullptr) { continue; } + if (callerFunc->GetFuncProfData() == nullptr) { + continue; + } uint64_t callsiteFreq = callerFunc->GetFuncProfData()->GetStmtFreq(stmtId); clonedSiteFreqs += callsiteFreq; } diff --git a/src/mapleall/maple_ir/include/mir_module.h b/src/mapleall/maple_ir/include/mir_module.h index 83eae82df72adaab66a74afa3ebb10212d8d045c..e09c84e554cbd798dd2f4725dcfd1c577516516d 100644 --- a/src/mapleall/maple_ir/include/mir_module.h +++ b/src/mapleall/maple_ir/include/mir_module.h @@ -344,21 +344,11 @@ class MIRModule { } std::string GetFileNameAsPostfix() const; + std::string GetFileNameWithPath() const; void SetFileName(const std::string &name) { fileName = name; } - std::string GetProfileDataFileName() const { - std::string profileDataFileName = GetFileName().substr(0, GetFileName().find_last_of(".")); - const char *gcovPath = std::getenv("GCOV_PREFIX"); - std::string gcovPrefix = gcovPath ? gcovPath : ""; - if (!gcovPrefix.empty() && (gcovPrefix.back() != '/')) { - gcovPrefix.append("/"); - } - profileDataFileName = gcovPrefix + profileDataFileName; - return profileDataFileName; - } - bool IsJavaModule() const { return srcLang == kSrcLangJava; } diff --git a/src/mapleall/maple_ir/src/mir_lower.cpp b/src/mapleall/maple_ir/src/mir_lower.cpp index 9c989137c0587d57e4a0dab579670bb9febecf66..38b6507b76e3d393df6dcac22786912ab11d2bd2 100644 --- a/src/mapleall/maple_ir/src/mir_lower.cpp +++ b/src/mapleall/maple_ir/src/mir_lower.cpp @@ -408,6 +408,9 @@ BlockNode *MIRLower::LowerDoloopStmt(DoloopNode &doloop) { if (GetFuncProfData()) { doloopnodeFreq = static_cast(GetFuncProfData()->GetStmtFreq(doloop.GetStmtID())); bodynodeFreq = static_cast(GetFuncProfData()->GetStmtFreq(doloop.GetDoBody()->GetStmtID())); + if (doloopnodeFreq < bodynodeFreq) { + doloopnodeFreq = bodynodeFreq; + } } auto *blk = mirModule.CurFuncCodeMemPool()->New(); if (doloop.IsPreg()) { diff --git a/src/mapleall/maple_ir/src/mir_module.cpp b/src/mapleall/maple_ir/src/mir_module.cpp index 334216779e569324ef8089266952982f60ae6564..8e53cfaf03d4266f53c126e38c9a8033a98e6377 100644 --- a/src/mapleall/maple_ir/src/mir_module.cpp +++ b/src/mapleall/maple_ir/src/mir_module.cpp @@ -747,6 +747,23 @@ std::string MIRModule::GetFileNameAsPostfix() const { return fileNameStr; } +std::string MIRModule::GetFileNameWithPath() const { + MIRInfoPair fileInfoElem = fileInfo.front(); + std::string fileNameWithoutPath = GlobalTables::GetStrTable().GetStringFromStrIdx(fileInfoElem.second); + // strip out the suffix + fileNameWithoutPath = fileNameWithoutPath.substr(0, fileNameWithoutPath.find_last_of(".")); + std::string fileNameWithPath; + // find the right entry because some entries are for included files + for (size_t i = 0; i < srcFileInfo.size(); ++i) { + MIRInfoPair infoElem = srcFileInfo[i]; + fileNameWithPath = GlobalTables::GetStrTable().GetStringFromStrIdx(infoElem.first); + if (fileNameWithPath.find(fileNameWithoutPath) != std::string::npos) { + break; + } + } + return fileNameWithPath; +} + void MIRModule::AddClass(TyIdx tyIdx) { (void)classList.insert(tyIdx); } diff --git a/src/mapleall/maple_me/src/lfo_loop_vec.cpp b/src/mapleall/maple_me/src/lfo_loop_vec.cpp index 189a49ec6e56fcb3456f5b6a1f4b3ffdb9cd8e6b..17e548f7e6207dd649bda72427da8c04ed71942c 100644 --- a/src/mapleall/maple_me/src/lfo_loop_vec.cpp +++ b/src/mapleall/maple_me/src/lfo_loop_vec.cpp @@ -1754,6 +1754,11 @@ DoloopNode *LoopVectorization::PrepareDoloop(DoloopNode *doloop, LoopTransPlan * ASSERT(parent && (parent->GetOpCode() == OP_block), "nullptr check"); BlockNode *pblock = static_cast(parent); pblock->InsertAfter(doloop, edoloop); + auto *profData = mirFunc->GetFuncProfData(); + if (profData) { + profData->SetStmtFreq(edoloop->GetStmtID(), 0); + profData->SetStmtFreq(edoloop->GetDoBody()->GetStmtID(), 0); + } } return doloop; } diff --git a/src/mapleall/maple_me/src/me_cfg.cpp b/src/mapleall/maple_me/src/me_cfg.cpp index 202d51f7fecf348b901679e9392a9c04fb7835c6..5eb25c28facdf4e516d3f41d65ad986d498f94e9 100644 --- a/src/mapleall/maple_me/src/me_cfg.cpp +++ b/src/mapleall/maple_me/src/me_cfg.cpp @@ -1553,9 +1553,9 @@ void MeCFG::CreateBasicBlocks() { curBB->SetBBLabel(labelIdx); // label node is not real node in bb, get frequency information to bb if (Options::profileUse && func.GetMirFunc()->GetFuncProfData()) { - auto freq = func.GetMirFunc()->GetFuncProfData()->GetStmtFreq(stmt->GetStmtID()); + int64 freq = func.GetMirFunc()->GetFuncProfData()->GetStmtFreq(stmt->GetStmtID()); if (freq >= 0) { - curBB->SetFrequency(freq); + curBB->SetFrequency(static_cast(freq)); } } break; @@ -1625,6 +1625,10 @@ void MeCFG::CreateBasicBlocks() { lastBB->SetFirst(func.GetMIRModule().GetMIRBuilder()->CreateStmtReturn(nullptr)); lastBB->SetLast(lastBB->GetStmtNodes().begin().d()); lastBB->SetKindReturn(); + if (updateFreq) { + FuncProfInfo *funcData = func.GetMirFunc()->GetFuncProfData(); + funcData->stmtFreqs[lastBB->GetLast().GetStmtID()] = 0; + } } else if (lastBB->GetKind() == kBBUnknown) { lastBB->SetKindReturn(); lastBB->SetAttributes(kBBAttrIsExit); @@ -1860,6 +1864,40 @@ void MeCFG::SwapBBId(BB &bb1, BB &bb2) { bb2.SetBBId(tmp); } +// bb must have 2 successors; construct the edge frequencies for the 2 edges +inline void ConstructEdgeFreqForBBWith2Succs(BB *bb) { + BB *fallthru = bb->GetSucc(0); + BB *targetBB = bb->GetSucc(1); + if (fallthru->GetPred().size() == 1) { + uint64 succ0Freq = fallthru->GetFrequency(); + bb->PushBackSuccFreq(succ0Freq); + if (bb->GetFrequency() > succ0Freq) { + bb->PushBackSuccFreq(bb->GetFrequency() - succ0Freq); + } else { + bb->PushBackSuccFreq(0); + } + } else if (targetBB->GetPred().size() == 1) { + uint64 succ1Freq = targetBB->GetFrequency(); + if (bb->GetAttributes(kBBAttrWontExit) && bb->GetSuccFreq().size() == 1) { + // special case: WontExitAnalysis() has pushed 0 to bb->succFreq + bb->GetSuccFreq()[0] = bb->GetFrequency(); + bb->PushBackSuccFreq(0); + } else if (bb->GetFrequency() >= succ1Freq) { // tolerate inaccuracy + bb->PushBackSuccFreq(0); + bb->PushBackSuccFreq(bb->GetFrequency()); + } else { + bb->PushBackSuccFreq(bb->GetFrequency() - succ1Freq); + bb->PushBackSuccFreq(succ1Freq); + } + } else if (fallthru->GetFrequency() > targetBB->GetFrequency()) { + bb->PushBackSuccFreq(bb->GetFrequency()); + bb->PushBackSuccFreq(0); + } else { + bb->PushBackSuccFreq(0); + bb->PushBackSuccFreq(bb->GetFrequency()); + } +} + // set bb succ frequency from bb freq // no critical edge is expected void MeCFG::ConstructEdgeFreqFromBBFreq() { @@ -1873,21 +1911,7 @@ void MeCFG::ConstructEdgeFreqFromBBFreq() { if (bb->GetSucc().size() == 1) { bb->PushBackSuccFreq(bb->GetFrequency()); } else if (bb->GetSucc().size() == 2) { - auto *fallthru = bb->GetSucc(0); - auto *targetBB = bb->GetSucc(1); - if (fallthru->GetPred().size() == 1) { - auto succ0Freq = fallthru->GetFrequency(); - bb->PushBackSuccFreq(succ0Freq); - ASSERT(bb->GetFrequency() >= succ0Freq, "sanity check"); - bb->PushBackSuccFreq(bb->GetFrequency() - succ0Freq); - } else if (targetBB->GetPred().size() == 1) { - auto succ1Freq = targetBB->GetFrequency(); - ASSERT(bb->GetFrequency() >= succ1Freq, "sanity check"); - bb->PushBackSuccFreq(bb->GetFrequency() - succ1Freq); - bb->PushBackSuccFreq(succ1Freq); - } else { - CHECK_FATAL(false, "ConstructEdgeFreqFromBBFreq::NYI critical edge"); - } + ConstructEdgeFreqForBBWith2Succs(bb); } else if (bb->GetSucc().size() > 2) { // switch case, no critical edge is supposted for (size_t i = 0; i < bb->GetSucc().size(); ++i) { @@ -1910,14 +1934,24 @@ void MeCFG::ConstructBBFreqFromStmtFreq() { for (auto bIt = valid_begin(); bIt != eIt; ++bIt) { if ((*bIt)->IsEmpty()) continue; StmtNode &first = (*bIt)->GetFirst(); + StmtNode &last = (*bIt)->GetLast(); if (funcData->stmtFreqs.count(first.GetStmtID()) > 0) { (*bIt)->SetFrequency(funcData->stmtFreqs[first.GetStmtID()]); - } else if (funcData->stmtFreqs.count((*bIt)->GetLast().GetStmtID()) > 0) { - (*bIt)->SetFrequency(funcData->stmtFreqs[(*bIt)->GetLast().GetStmtID()]); + } else if (funcData->stmtFreqs.count(last.GetStmtID()) > 0) { + (*bIt)->SetFrequency(funcData->stmtFreqs[last.GetStmtID()]); } else { - LogInfo::MapleLogger() << "ERROR:: bb " << (*bIt)->GetBBId() << "frequency is not set" - << "\n"; - ASSERT(0, "no freq set"); + bool foundFreq = false; + for (StmtNode &stmt : (*bIt)->GetStmtNodes()) { + if (funcData->stmtFreqs.count(stmt.GetStmtID()) > 0) { + (*bIt)->SetFrequency(funcData->stmtFreqs[stmt.GetStmtID()]); + foundFreq = true; + break; + } + } + if (not foundFreq) { + LogInfo::MapleLogger() << "ERROR:: bb " << (*bIt)->GetBBId() << " has not stmt with set freq" << "\n"; + CHECK_FATAL(0, "MeCFG::ConstructBBFreqFromStmtFreq: cannot set BB freq"); + } } } // add common entry and common exit @@ -1937,9 +1971,6 @@ void MeCFG::ConstructBBFreqFromStmtFreq() { ConstructEdgeFreqFromBBFreq(); // clear stmtFreqs since cfg frequency is create funcData->stmtFreqs.clear(); - - // set updateFrequency with true - updateFreq = true; } void MeCFG::ConstructStmtFreq() { @@ -2086,6 +2117,10 @@ bool MEMeCfg::PhaseRun(MeFunction &f) { MemPool *meCfgMp = GetPhaseMemPool(); theCFG = meCfgMp->New(meCfgMp, f); f.SetTheCfg(theCFG); + + if (Options::profileUse && f.GetMirFunc()->GetFuncProfData()) { + theCFG->SetUpdateCFGFreq(true); + } theCFG->CreateBasicBlocks(); if (theCFG->NumBBs() == 0) { /* there's no basicblock generated */ @@ -2104,7 +2139,7 @@ bool MEMeCfg::PhaseRun(MeFunction &f) { } theCFG->Verify(); // construct bb freq from stmt freq - if (Options::profileUse && f.GetMirFunc()->GetFuncProfData()) { + if (theCFG->UpdateCFGFreq()) { theCFG->ConstructBBFreqFromStmtFreq(); if (theCFG->DumpIRProfileFile()) { std::string fileName = "after-mecfgbuild"; diff --git a/src/mapleall/maple_me/src/me_loop_inversion.cpp b/src/mapleall/maple_me/src/me_loop_inversion.cpp index fddacb87d9cd8064afe753307f5780505d88d82a..7a615caf7753db991caeff7b479440625913604a 100644 --- a/src/mapleall/maple_me/src/me_loop_inversion.cpp +++ b/src/mapleall/maple_me/src/me_loop_inversion.cpp @@ -243,7 +243,6 @@ void MeLoopInversion::Convert(MeFunction &func, BB &bb, BB &pred, MapleMapGetFrequency() == 0, "sanity check"); latchBB->PushBackSuccFreq(0); latchBB->PushBackSuccFreq(0); } diff --git a/src/mapleall/maple_me/src/me_profile_use.cpp b/src/mapleall/maple_me/src/me_profile_use.cpp index d43fa03e7361af5cefbc653b38d1ea46a130af2c..d22d98e97a47a28e6ce2ba337cf26c4407cdc9ed 100644 --- a/src/mapleall/maple_me/src/me_profile_use.cpp +++ b/src/mapleall/maple_me/src/me_profile_use.cpp @@ -103,7 +103,7 @@ void MeProfUse::ComputeEdgeFreq() { while (change) { change = false; pass++; - CHECK_FATAL(pass != UINT8_MAX, "parse all edges fail"); + CHECK_FATAL(pass != UINT16_MAX, "too many passes in MeProfUse::ComputeEdgeFreq: %d", pass); /* * use the bb edge to infer the bb's count,when all bb's count is valid * then all edges count is valid diff --git a/src/mapleall/maple_me/src/me_value_range_prop.cpp b/src/mapleall/maple_me/src/me_value_range_prop.cpp index cc6b15f8dc9a2bf747568857ccf90c0979327197..96b8a8397b55367c11bb4b7240a5cc88a738f6a6 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3933,7 +3933,6 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( uint64 edgeFreq = 0; if (func.GetCfg()->UpdateCFGFreq()) { edgeFreq = pred.GetSuccFreq()[index]; - ASSERT(bb.GetFrequency() >= edgeFreq, "sanity check"); } pred.RemoveSucc(bb); DeleteThePhiNodeWhichOnlyHasOneOpnd(bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); diff --git a/src/mapleall/maple_me/src/optimizeCFG.cpp b/src/mapleall/maple_me/src/optimizeCFG.cpp index a6c6826b3179a0ea3d79cdf05656f67e78b0a1a2..ec8ccdcd9785e73aa3208ff187f29fd739509af2 100644 --- a/src/mapleall/maple_me/src/optimizeCFG.cpp +++ b/src/mapleall/maple_me/src/optimizeCFG.cpp @@ -1904,8 +1904,11 @@ bool OptimizeBB::SkipRedundantCond(BB &pred, BB &succ) { idx = succ.GetSuccIndex(*affectedBB); ASSERT(idx >= 0 && idx < succ.GetSucc().size(), "sanity check"); int64_t oldedgeFreq = static_cast(succ.GetSuccFreq()[static_cast(idx)]); - ASSERT(oldedgeFreq >= freq, "sanity check"); - succ.SetSuccFreq(idx, static_cast(oldedgeFreq) - freq); + if (oldedgeFreq >= freq) { + succ.SetSuccFreq(idx, static_cast(oldedgeFreq) - freq); + } else { + succ.SetSuccFreq(idx, 0); + } } return true; } diff --git a/src/mapleall/maple_me/src/pme_emit.cpp b/src/mapleall/maple_me/src/pme_emit.cpp index d5cf1e015ecdf1806c05ab326150012cb9b1e0a1..a567e52090ba422da0a62f3f90042aa0f76662cd 100644 --- a/src/mapleall/maple_me/src/pme_emit.cpp +++ b/src/mapleall/maple_me/src/pme_emit.cpp @@ -833,7 +833,7 @@ 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, add commentnode\n"; + 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 fdb427c437edac00a58ce717b1017bc4d883e334..c30a98121817e3df323d92c9dd4404b4b9e1cb30 100644 --- a/src/mapleall/maple_me/src/pme_mir_lower.cpp +++ b/src/mapleall/maple_me/src/pme_mir_lower.cpp @@ -73,8 +73,7 @@ BlockNode *PreMeMIRLower::LowerWhileStmt(WhileStmtNode &whileStmt) { if (GetFuncProfData()) { int64_t freq = GetFuncProfData()->GetStmtFreq(whileStmt.GetStmtID()) - GetFuncProfData()->GetStmtFreq(whilegotonode->GetStmtID()); - ASSERT(freq >= 0, "sanity check"); - GetFuncProfData()->SetStmtFreq(endlblstmt->GetStmtID(), freq); + GetFuncProfData()->SetStmtFreq(endlblstmt->GetStmtID(), freq > 0 ? freq : 0); } return blk; } diff --git a/src/mapleall/maple_util/include/mpl_profdata.h b/src/mapleall/maple_util/include/mpl_profdata.h index 23cf6f99b95eaef8c63518ea7c486478075acf9b..767bbf2ea4b3145b9b85803717acd5846e2a95af 100644 --- a/src/mapleall/maple_util/include/mpl_profdata.h +++ b/src/mapleall/maple_util/include/mpl_profdata.h @@ -130,6 +130,9 @@ class FuncProfInfo { return -1; // unstored } void SetStmtFreq(uint32_t stmtID, uint64_t freq) { + if (static_cast(freq) == -1) { + return; + } stmtFreqs[stmtID] = freq; } void EraseStmtFreq(uint32_t stmtID) { diff --git a/src/mapleall/mpl2mpl/src/gen_profile.cpp b/src/mapleall/mpl2mpl/src/gen_profile.cpp index a54af1b99d5379195bc3598a7f6605dd75186058..5741f7faa1fe7abd9c9e2680f0e085c827fbbba3 100644 --- a/src/mapleall/mpl2mpl/src/gen_profile.cpp +++ b/src/mapleall/mpl2mpl/src/gen_profile.cpp @@ -103,9 +103,10 @@ void ProfileGen::CreateModProfDesc() { MIRIntConst *checksumMirConst = GlobalTables::GetIntConstTable().GetOrCreateIntConst(cfgChkSum, *u32Ty); modProfDescSymMirConst->AddItem(checksumMirConst, 5); + std::string fileNameWithPath = mod.GetFileNameWithPath(); // Make the profile file name as fileName.gcda - std::string profFileName = mod.GetFileName().substr(0, mod.GetFileName().find_last_of(".")) + - namemangler::kProfFileNameExt; + std::string profFileName = fileNameWithPath.substr(0, fileNameWithPath.find_last_of(".")) + + namemangler::kProfFileNameExt; auto *profileFNMirConst = modMP->New(profFileName, *GlobalTables::GetTypeTable().GetTypeFromTyIdx(TyIdx(PTY_a64))); modProfDescSymMirConst->AddItem(profileFNMirConst, 6); diff --git a/src/mapleall/mpl2mpl/src/inline.cpp b/src/mapleall/mpl2mpl/src/inline.cpp index d6c5a33d6c7d325f2a22079737b784f4d939be9a..c3e558608215559ed73ba54ebe4a34f891e8778b 100644 --- a/src/mapleall/mpl2mpl/src/inline.cpp +++ b/src/mapleall/mpl2mpl/src/inline.cpp @@ -539,8 +539,13 @@ void MInline::InlineCallsBlockInternal(MIRFunction &func, BaseNode &baseNode, bo InlineResult result; result.reason = GetInlineFailedStr(failCode); if (canInline) { - result = AnalyzeCallee(func, *callee, callStmt); - canInline = result.canInline; + if (Options::profileUse && callee->GetFuncProfData() == nullptr) { + // callee is never executed according to profile data + canInline = false; + } else { + result = AnalyzeCallee(func, *callee, callStmt); + canInline = result.canInline; + } } if (canInline) { module.SetCurFunction(&func); diff --git a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp index 1d0ed18d7f155b55d00d872e38bcc6a51d95eb5d..a0702c4cf273cc100bc31c846e22afae0506ddfb 100644 --- a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp +++ b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp @@ -87,6 +87,7 @@ int MplProfDataParser::ReadMapleProfileData() { std::string mprofDataFile = Options::profile; if (mprofDataFile.empty()) { if (const char *envGcovprefix = std::getenv("GCOV_PREFIX")) { + std::string fileNameWithPath = m.GetFileNameWithPath(); static_cast(mprofDataFile.append(envGcovprefix)); if (mprofDataFile.back() != '/') { static_cast(mprofDataFile.append("/")); @@ -102,33 +103,30 @@ int MplProfDataParser::ReadMapleProfileData() { LogInfo::MapleLogger() << "set env GCOV_PREFIX_STRIP=" << strip << std::endl; } } - std::string profDataFileName = m.GetProfileDataFileName(); - if (dumpDetail) { - LogInfo::MapleLogger() << "profdata file stem before strip: " << profDataFileName << std::endl; - } - // reduce path in profDataFileName - while (stripnum > 0 && profDataFileName.size() > 1) { - size_t pos = profDataFileName.find_first_of("/", 1); + // strip path in fileNameWithPath according to stripnum + while (stripnum > 0 && fileNameWithPath.size() > 1) { + size_t pos = fileNameWithPath.find_first_of("/", 1); if (pos == std::string::npos) { break; } - profDataFileName = profDataFileName.substr(pos); + fileNameWithPath= fileNameWithPath.substr(pos); stripnum--; } if (dumpDetail) { - LogInfo::MapleLogger() << "profdata file stem after strip: " << profDataFileName << std::endl; + LogInfo::MapleLogger() << "profdata file stem after strip: " << fileNameWithPath<< std::endl; } - CHECK_FATAL(profDataFileName.size() > 0, "sanity check"); - static_cast(mprofDataFile.append(profDataFileName)); + CHECK_FATAL(fileNameWithPath.size() > 0, "sanity check"); + static_cast(mprofDataFile.append(fileNameWithPath)); } else { - // if gcov_prefix is not set, find .mprofdata according to m.profiledata - mprofDataFile = m.GetProfileDataFileName(); - if (dumpDetail) { - LogInfo::MapleLogger() << "NO ENV, profdata file stem: " << mprofDataFile << std::endl; + mprofDataFile = m.GetFileName(); + // strip path in mprofDataFile + size_t pos = mprofDataFile.find_last_of("/"); + if (pos != std::string::npos) { + mprofDataFile = mprofDataFile.substr(pos + 1); } } - // add .mprofdata - static_cast(mprofDataFile.append(namemangler::kMplProfFileNameExt)); + // change the suffix to .mprofdata + mprofDataFile = mprofDataFile.substr(0, mprofDataFile.find_last_of(".")) + namemangler::kMplProfFileNameExt; } ASSERT(!mprofDataFile.empty(), "null check"); LogInfo::MapleLogger() << "profileUse will open " << mprofDataFile << std::endl; @@ -136,13 +134,18 @@ int MplProfDataParser::ReadMapleProfileData() { profData = mempool->New(mempool, &alloc); // read .mprofdata std::ifstream inputStream(mprofDataFile, (std::ios::in | std::ios::binary)); - CHECK_FATAL(inputStream, "Could not open the file %s, quit\n", mprofDataFile.c_str()); + if (!inputStream) { + if (opts::missingProfDataIsError) { + CHECK_FATAL(inputStream, "Could not open profile data file %s, quit\n", mprofDataFile.c_str()); + } else { + WARN(kLncWarn, "Could not open profile data file %s\n", mprofDataFile.c_str()); + } + return 1; + } // get length of file static_cast(inputStream.seekg(0, std::ios::end)); uint32_t length = static_cast(inputStream.tellg()); static_cast(inputStream.seekg(0, std::ios::beg)); - const uint32_t sizeThreshold = 1024 * 10; - CHECK_FATAL(length <= sizeThreshold, "NYI::large .mprofdata file size is larger than threashold, do chunk memory\n"); std::unique_ptr buffer = std::make_unique(length); static_cast(inputStream.read(buffer.get(), length)); @@ -175,6 +178,9 @@ void MMplProfDataParser::GetAnalysisDependence(AnalysisDep &aDep) const { bool MMplProfDataParser::PhaseRun(maple::MIRModule &m) { MemPool *memPool = m.GetMemPool(); // use global pool to store profile data bool enableDebug = false; // true to dump trace + if (m.GetFunctionList().empty()) { + return false; // there is no executable code + } MplProfDataParser parser(m, memPool, enableDebug); int res = parser.ReadMapleProfileData(); if (res) {