From 13e6b41d68a706f35c82ad1fccee89494a2826be Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Fri, 19 Aug 2022 00:13:24 -0700 Subject: [PATCH 1/3] profdata parser should abort when it cannot find the profdata file Added me option --dumpcfgofphases to help triaging frequency update issues. In valueRangePropagation, do not delete edges or BBs that have non-zero freq. At end of optimizeCFG phase, call UpdateEdgeFreqWithBBFreq(). Use i32 instead of u1 for comparison notes created in irmap. --- src/mapleall/maple_me/include/me_cfg.h | 6 +++- src/mapleall/maple_me/include/me_option.h | 1 + src/mapleall/maple_me/include/me_options.h | 1 + .../maple_me/include/me_value_range_prop.h | 2 +- src/mapleall/maple_me/src/bb.cpp | 6 +++- src/mapleall/maple_me/src/irmap.cpp | 16 ++++----- src/mapleall/maple_me/src/me_bb_layout.cpp | 13 +++---- .../maple_me/src/me_critical_edge.cpp | 3 ++ src/mapleall/maple_me/src/me_irmap_build.cpp | 3 ++ src/mapleall/maple_me/src/me_option.cpp | 2 ++ src/mapleall/maple_me/src/me_options.cpp | 4 +++ .../maple_me/src/me_value_range_prop.cpp | 35 ++++++++++++------- src/mapleall/maple_me/src/optimizeCFG.cpp | 7 ++-- .../mpl2mpl/src/mpl_profdata_parser.cpp | 20 +++++------ 14 files changed, 76 insertions(+), 43 deletions(-) diff --git a/src/mapleall/maple_me/include/me_cfg.h b/src/mapleall/maple_me/include/me_cfg.h index 8465e37e75..eee61dc7eb 100644 --- a/src/mapleall/maple_me/include/me_cfg.h +++ b/src/mapleall/maple_me/include/me_cfg.h @@ -45,7 +45,11 @@ class MeCFG : public AnalysisResult { endTryBB2TryBB(mecfgAlloc.Adapter()), sccTopologicalVec(mecfgAlloc.Adapter()), sccOfBB(mecfgAlloc.Adapter()), - backEdges(mecfgAlloc.Adapter()) {} + backEdges(mecfgAlloc.Adapter()) { + if (MeOption::dumpCfgOfPhases) { + dumpIRProfileFile = true; + } + } ~MeCFG() = default; diff --git a/src/mapleall/maple_me/include/me_option.h b/src/mapleall/maple_me/include/me_option.h index ae78247e38..4e43e1e05e 100644 --- a/src/mapleall/maple_me/include/me_option.h +++ b/src/mapleall/maple_me/include/me_option.h @@ -184,6 +184,7 @@ class MeOption { static uint8 rematLevel; static bool layoutWithPredict; static bool unifyRets; + static bool dumpCfgOfPhases; // safety check option begin static SafetyCheckMode npeCheckMode; static bool isNpeCheckAll; diff --git a/src/mapleall/maple_me/include/me_options.h b/src/mapleall/maple_me/include/me_options.h index 4b009116a7..da127b6d3d 100644 --- a/src/mapleall/maple_me/include/me_options.h +++ b/src/mapleall/maple_me/include/me_options.h @@ -134,6 +134,7 @@ extern maplecl::Option warning; extern maplecl::Option remat; extern maplecl::Option unifyrets; extern maplecl::Option lfo; +extern maplecl::Option dumpCfgOfPhases; } diff --git a/src/mapleall/maple_me/include/me_value_range_prop.h b/src/mapleall/maple_me/include/me_value_range_prop.h index 719e55c0fb..4e9d8ce6b5 100644 --- a/src/mapleall/maple_me/include/me_value_range_prop.h +++ b/src/mapleall/maple_me/include/me_value_range_prop.h @@ -751,7 +751,7 @@ class ValueRangePropagation { bool OnlyHaveCondGotoStmt(BB &bb) const; bool RemoveUnreachableEdge(BB &pred, BB &bb, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, std::map> &ssaupdateCandsForCondExpr); - void RemoveUnreachableBB(BB &condGotoBB, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, + bool RemoveUnreachableBB(BB &condGotoBB, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, std::map> &ssaupdateCandsForCondExpr); BB *CreateNewGotoBBWithoutCondGotoStmt(BB &bb); void CopyMeStmts(BB &fromBB, BB &toBB); diff --git a/src/mapleall/maple_me/src/bb.cpp b/src/mapleall/maple_me/src/bb.cpp index 181e6ed831..f7ed0e990f 100644 --- a/src/mapleall/maple_me/src/bb.cpp +++ b/src/mapleall/maple_me/src/bb.cpp @@ -485,7 +485,11 @@ void BB::UpdateEdgeFreqs(bool updateBBFreqOfSucc) { succFreqs += GetSuccFreq()[i]; } int diff = static_cast(abs(succFreqs - GetFrequency())); - if (len == 0 || diff <= 1) {return;} + if (len == 0 || + (len == 1 && diff <= 0) || + (len > 1 && diff <= 1)) { + return; + } for (uint32 i = 0; i < len; ++i) { int64_t sfreq = GetSuccFreq()[static_cast(i)]; int64_t scalefreq = (succFreqs == 0 ? (frequency / len) : (sfreq * frequency / succFreqs)); diff --git a/src/mapleall/maple_me/src/irmap.cpp b/src/mapleall/maple_me/src/irmap.cpp index 91b6da3762..0c40574e10 100644 --- a/src/mapleall/maple_me/src/irmap.cpp +++ b/src/mapleall/maple_me/src/irmap.cpp @@ -1938,7 +1938,7 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { return opnd0; } else if ((cmpop == OP_ne && opnd1const->IsOne()) || (cmpop == OP_eq && opnd1const->IsZero())) { if (IsCompareHasReverseOp(opnd0->GetOp())) { - OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_u1, opnd0->GetNumOpnds()); + OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_i32, opnd0->GetNumOpnds()); reverseMeExpr.SetOpnd(0, opnd0->GetOpnd(0)); reverseMeExpr.SetOpnd(1, opnd0->GetOpnd(1)); reverseMeExpr.SetOpndType(static_cast(opnd0)->GetOpndType()); @@ -1955,7 +1955,7 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { if (constVal->GetKind() == kConstInt && constVal->IsZero()) { auto *subOpnd0 = opnd0->GetOpnd(0); auto *subOpnd1 = opnd0->GetOpnd(1); - return CreateMeExprCompare(cmpop, PTY_u1, subOpnd0->GetPrimType(), *subOpnd0, *subOpnd1); + return CreateMeExprCompare(cmpop, PTY_i32, subOpnd0->GetPrimType(), *subOpnd0, *subOpnd1); } } @@ -1974,7 +1974,7 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { return nullptr; } auto *newcmp = - CreateMeExprCompare(OP_ne, PTY_u1, opnd0->GetPrimType(), *opnd0->GetOpnd(0), *opnd0->GetOpnd(1)); + CreateMeExprCompare(OP_ne, PTY_i32, opnd0->GetPrimType(), *opnd0->GetOpnd(0), *opnd0->GetOpnd(1)); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; } @@ -1992,13 +1992,13 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { } // we prefer ne/eq in following optimizations if (opnd0->GetMeOp() == kMeOpConst && static_cast(opnd0)->GetConstVal()->IsZero()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_eq : OP_ne, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_eq : OP_ne, PTY_i32, cmpExpr->GetOpndType(), *opnd1, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; } if (opnd1->GetMeOp() == kMeOpConst && static_cast(opnd1)->GetConstVal()->IsOne()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_ne : OP_eq, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_ge ? OP_ne : OP_eq, PTY_i32, cmpExpr->GetOpndType(), *opnd0, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; @@ -2018,13 +2018,13 @@ MeExpr *IRMap::SimplifyCmpExpr(OpMeExpr *cmpExpr) { } // we prefer ne/eq in following optimizations if (opnd1->GetMeOp() == kMeOpConst && static_cast(opnd1)->GetConstVal()->IsZero()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_ne : OP_eq, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_ne : OP_eq, PTY_i32, cmpExpr->GetOpndType(), *opnd0, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; } if (opnd0->GetMeOp() == kMeOpConst && static_cast(opnd0)->GetConstVal()->IsOne()) { - auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_eq : OP_ne, PTY_u1, cmpExpr->GetOpndType(), + auto *newcmp = CreateMeExprCompare(cmpop == OP_gt ? OP_eq : OP_ne, PTY_i32, cmpExpr->GetOpndType(), *opnd1, *CreateIntConstMeExpr(0, cmpExpr->GetOpndType())); auto *simplified = SimplifyCmpExpr(static_cast(newcmp)); return simplified == nullptr ? newcmp : simplified; @@ -2607,7 +2607,7 @@ MeExpr *IRMap::SimplifyOpMeExpr(OpMeExpr *opmeexpr) { case OP_lnot: { MeExpr *opnd0 = opmeexpr->GetOpnd(0); if (IsCompareHasReverseOp(opnd0->GetOp())) { - OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_u1, opnd0->GetNumOpnds()); + OpMeExpr reverseMeExpr(kInvalidExprID, GetReverseCmpOp(opnd0->GetOp()), PTY_i32, opnd0->GetNumOpnds()); reverseMeExpr.SetOpnd(0, opnd0->GetOpnd(0)); reverseMeExpr.SetOpnd(1, opnd0->GetOpnd(1)); reverseMeExpr.SetOpndType(opnd0->GetOpnd(0)->GetPrimType()); diff --git a/src/mapleall/maple_me/src/me_bb_layout.cpp b/src/mapleall/maple_me/src/me_bb_layout.cpp index 8c5612db86..ceafc3e6b0 100644 --- a/src/mapleall/maple_me/src/me_bb_layout.cpp +++ b/src/mapleall/maple_me/src/me_bb_layout.cpp @@ -1377,16 +1377,17 @@ bool MEBBLayout::PhaseRun(maple::MeFunction &f) { bbLayout->RunLayout(); f.SetLaidOutBBs(bbLayout->GetBBs()); - if (DEBUGFUNC_NEWPM(f) || Options::profileUse) { - // verify CFG : check condBB's succs should be different - bbLayout->VerifyBB(); - bbLayout->DumpBBPhyOrder(); + if (Options::profileUse) { if (cfg->UpdateCFGFreq() && cfg->DumpIRProfileFile()) { cfg->DumpToFile("after-bblayout", false, true); - } else { - cfg->DumpToFile("afterBBLayout", false); } } + if (DEBUGFUNC_NEWPM(f)) { + // verify CFG : check condBB's succs should be different + bbLayout->VerifyBB(); + bbLayout->DumpBBPhyOrder(); + cfg->DumpToFile("afterBBLayout", false); + } return true; } } // namespace maple diff --git a/src/mapleall/maple_me/src/me_critical_edge.cpp b/src/mapleall/maple_me/src/me_critical_edge.cpp index 4dca8f82a2..fd97bb2452 100644 --- a/src/mapleall/maple_me/src/me_critical_edge.cpp +++ b/src/mapleall/maple_me/src/me_critical_edge.cpp @@ -313,6 +313,9 @@ bool MESplitCEdge::PhaseRun(maple::MeFunction &f) { f.GetPreMeFunc()->pmeCreatedWhileLabelSet.clear(); } } + if (f.GetCfg()->UpdateCFGFreq() && (f.GetCfg()->DumpIRProfileFile())) { + f.GetCfg()->DumpToFile("after-splitcriticaledge", false, true); + } return false; } } // namespace maple diff --git a/src/mapleall/maple_me/src/me_irmap_build.cpp b/src/mapleall/maple_me/src/me_irmap_build.cpp index f34555f5c6..76449544f7 100644 --- a/src/mapleall/maple_me/src/me_irmap_build.cpp +++ b/src/mapleall/maple_me/src/me_irmap_build.cpp @@ -58,6 +58,9 @@ bool MEIRMapBuild::PhaseRun(maple::MeFunction &f) { } if (DEBUGFUNC_NEWPM(f)) { irMap->Dump(); + if (f.GetCfg()->UpdateCFGFreq()) { + f.GetCfg()->DumpToFile("irmapbuild", true, true); + } } // delete mempool for meirmap temporaries diff --git a/src/mapleall/maple_me/src/me_option.cpp b/src/mapleall/maple_me/src/me_option.cpp index d01372ee56..9479bd1442 100644 --- a/src/mapleall/maple_me/src/me_option.cpp +++ b/src/mapleall/maple_me/src/me_option.cpp @@ -130,6 +130,7 @@ bool MeOption::isNpeCheckAll = false; SafetyCheckMode MeOption::boundaryCheckMode = SafetyCheckMode::kNoCheck; bool MeOption::safeRegionMode = false; bool MeOption::unifyRets = false; +bool MeOption::dumpCfgOfPhases = false; #if MIR_JAVA std::string MeOption::acquireFuncName = "Landroid/location/LocationManager;|requestLocationUpdates|"; std::string MeOption::releaseFuncName = "Landroid/location/LocationManager;|removeUpdates|"; @@ -306,6 +307,7 @@ bool MeOption::SolveOptions(bool isDebug) { maplecl::CopyIfEnabled(dseKeepRef, opts::me::dsekeepref); maplecl::CopyIfEnabled(lessThrowAlias, opts::me::lessthrowalias); maplecl::CopyIfEnabled(propBase, opts::me::propbase); + maplecl::CopyIfEnabled(dumpCfgOfPhases, opts::me::dumpCfgOfPhases); if (opts::me::propiloadref.IsEnabledByUser()) { propIloadRef = opts::me::propiloadref; diff --git a/src/mapleall/maple_me/src/me_options.cpp b/src/mapleall/maple_me/src/me_options.cpp index 66103cf29f..79adab5f52 100644 --- a/src/mapleall/maple_me/src/me_options.cpp +++ b/src/mapleall/maple_me/src/me_options.cpp @@ -695,4 +695,8 @@ maplecl::Option lfo({"--lfo"}, {meCategory}, maplecl::DisableWith("--no-lfo")); +maplecl::Option dumpCfgOfPhases({"--dumpcfgofphases"}, + " --dumpcfgofphases \tDump CFG from various phases to .dot files\n", + {meCategory}); + } 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 189ecab9ae..c61b5d03c8 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3134,6 +3134,9 @@ void ValueRangePropagation::AnalysisUnreachableBBOrEdge(BB &bb, BB &unreachableB if (onlyPropVR) { return; } + if (func.GetCfg()->UpdateCFGFreq() && unreachableBB.GetFrequency() > 0) { + return; + } for (auto &pred : bb.GetPred()) { UpdateProfile(*pred, bb, unreachableBB); } @@ -3508,7 +3511,7 @@ void ValueRangePropagation::UpdateProfile(BB &pred, BB &bb, const BB &targetBB) // \ / \ ----> | | // \ / \ | | // false true false true -void ValueRangePropagation::RemoveUnreachableBB( +bool ValueRangePropagation::RemoveUnreachableBB( BB &condGotoBB, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, std::map> &ssaupdateCandsForCondExpr) { CHECK_FATAL(condGotoBB.GetSucc().size() == kNumOperands, "must have 2 succ"); @@ -3518,17 +3521,22 @@ void ValueRangePropagation::RemoveUnreachableBB( if (OnlyHaveOneCondGotoPredBB(*succ1, condGotoBB)) { AnalysisUnreachableBBOrEdge(condGotoBB, *succ1, trueBranch); } else { + int64_t removedFreq = 0; + if (func.GetCfg()->UpdateCFGFreq()) { + removedFreq = condGotoBB.GetSuccFreq()[1]; + if (removedFreq > 0) { + return false; // cannot delete an edge that is actually executed + } + } if (condGotoBB.GetPred().size() == 1) { UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - // update frequency before cfg changed + condGotoBB.RemoveSucc(*succ1); if (func.GetCfg()->UpdateCFGFreq()) { - int64_t removedFreq = static_cast(condGotoBB.GetSuccFreq()[1]); condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); succ0->SetFrequency(static_cast(succ0->GetFrequency() + removedFreq)); } - condGotoBB.RemoveSucc(*succ1); DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ1, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } @@ -3536,15 +3544,17 @@ void ValueRangePropagation::RemoveUnreachableBB( if (OnlyHaveOneCondGotoPredBB(*succ0, condGotoBB)) { AnalysisUnreachableBBOrEdge(condGotoBB, *succ0, trueBranch); } else { + int64_t removedFreq = 0; + if (func.GetCfg()->UpdateCFGFreq()) { + removedFreq = condGotoBB.GetSuccFreq()[0]; + if (removedFreq > 0) { + return false; // cannot delete an edge that is actually executed + } + } if (condGotoBB.GetPred().size() == 1) { UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - int64_t removedFreq = 0; - // update frequency before cfg changed - if (func.GetCfg()->UpdateCFGFreq()) { - removedFreq = static_cast(condGotoBB.GetSuccFreq()[0]); - } condGotoBB.RemoveSucc(*succ0); if (func.GetCfg()->UpdateCFGFreq()) { condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); @@ -3554,6 +3564,7 @@ void ValueRangePropagation::RemoveUnreachableBB( condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } } + return true; } BB *ValueRangePropagation::CreateNewGotoBBWithoutCondGotoStmt(BB &bb) { @@ -3813,8 +3824,7 @@ bool ValueRangePropagation::CopyFallthruBBAndRemoveUnreachableEdge( currBB = currBB->GetSucc(0); } /* case2: the number of branches reaching to condBB == 1 */ - RemoveUnreachableBB(*currBB, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); - return true; + return RemoveUnreachableBB(*currBB, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); } // If the valuerange of opnd in pred0 and pred1 is equal the valuerange in true branch: @@ -3833,8 +3843,7 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( CHECK_FATAL(bb.GetKind() == kBBCondGoto, "must be condgoto bb"); if (GetRealPredSize(bb) < kNumOperands) { CHECK_FATAL(GetRealPredSize(bb) == 1, "must have one pred"); - RemoveUnreachableBB(bb, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); - return true; + return RemoveUnreachableBB(bb, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); } if (OnlyHaveCondGotoStmt(bb)) { PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); diff --git a/src/mapleall/maple_me/src/optimizeCFG.cpp b/src/mapleall/maple_me/src/optimizeCFG.cpp index f8457c36f2..6d3a886b93 100644 --- a/src/mapleall/maple_me/src/optimizeCFG.cpp +++ b/src/mapleall/maple_me/src/optimizeCFG.cpp @@ -2594,8 +2594,11 @@ bool MEOptimizeCFG::PhaseRun(maple::MeFunction &f) { MeSSAUpdate ssaUpdate(f, *f.GetMeSSATab(), *dom, cands); ssaUpdate.Run(); } - if (f.GetCfg()->DumpIRProfileFile()) { - f.GetCfg()->DumpToFile("after-OptimizeCFG", false, f.GetCfg()->UpdateCFGFreq()); + if (f.GetCfg()->UpdateCFGFreq()) { + f.GetCfg()->UpdateEdgeFreqWithBBFreq(); + if (f.GetCfg()->DumpIRProfileFile()) { + f.GetCfg()->DumpToFile("after-OptimizeCFG", false, f.GetCfg()->UpdateCFGFreq()); + } } } return change; diff --git a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp index eeaa70cd47..475bff7260 100644 --- a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp +++ b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp @@ -45,7 +45,7 @@ int ProfileSummaryImport::ReadSummary(MplProfileData *profData) { uint64_t magicNum = ReadNum(); if (magicNum != kMapleProfDataMagicNumber) { LogInfo::MapleLogger() << "magic number error, quit\n"; - return 1; + abort(); } uint64_t checksum = ReadNum(); uint32_t runtimes = ReadNum(); @@ -97,19 +97,19 @@ int MplProfDataParser::ReadMapleProfileData() { static_cast(mprofDataFile.append("/")); } if (dumpDetail) { - LogInfo::MapleLogger() << "set env gcov_prefix= " << mprofDataFile << std::endl; + LogInfo::MapleLogger() << "set env GCOV_PREFIX= " << mprofDataFile << std::endl; } uint32_t stripnum = 0; if (const char *envGcovprefixstrip = std::getenv("GCOV_PREFIX_STRIP")) { std::string strip(envGcovprefixstrip); stripnum = static_cast(std::stoi(strip)); if (dumpDetail) { - LogInfo::MapleLogger() << "set env gcov_prefix_strip=" << strip << std::endl; + LogInfo::MapleLogger() << "set env GCOV_PREFIX_STRIP=" << strip << std::endl; } } std::string profDataFileName = m.GetProfileDataFileName(); if (dumpDetail) { - LogInfo::MapleLogger() << "module profdata Name: " << profDataFileName << std::endl; + LogInfo::MapleLogger() << "profdata file stem before strip: " << profDataFileName << std::endl; } // reduce path in profDataFileName while (stripnum > 0 && profDataFileName.size() > 1) { @@ -121,7 +121,7 @@ int MplProfDataParser::ReadMapleProfileData() { stripnum--; } if (dumpDetail) { - LogInfo::MapleLogger() << "after strip, module profdata Name: " << profDataFileName << std::endl; + LogInfo::MapleLogger() << "profdata file stem after strip: " << profDataFileName << std::endl; } CHECK_FATAL(profDataFileName.size() > 0, "sanity check"); static_cast(mprofDataFile.append(profDataFileName)); @@ -129,23 +129,21 @@ int MplProfDataParser::ReadMapleProfileData() { // if gcov_prefix is not set, find .mprofdata according to m.profiledata mprofDataFile = m.GetProfileDataFileName(); if (dumpDetail) { - LogInfo::MapleLogger() << "NO ENV, module profdata Name: " << mprofDataFile << std::endl; + LogInfo::MapleLogger() << "NO ENV, profdata file stem: " << mprofDataFile << std::endl; } } // add .mprofdata static_cast(mprofDataFile.append(namemangler::kMplProfFileNameExt)); } ASSERT(!mprofDataFile.empty(), "null check"); - if (dumpDetail) { - LogInfo::MapleLogger() << "will open mprofileData " << mprofDataFile << std::endl; - } + LogInfo::MapleLogger() << "profileUse will open " << mprofDataFile << std::endl; // create mpl profdata profData = mempool->New(mempool, &alloc); // read .mprofdata std::ifstream inputStream(mprofDataFile, (std::ios::in | std::ios::binary)); if (!inputStream) { LogInfo::MapleLogger() << "Could not open the file " << mprofDataFile << "\n"; - return 1; + abort(); } // get length of file static_cast(inputStream.seekg(0, std::ios::end)); @@ -188,7 +186,7 @@ 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 = true; // true to dump trace + bool enableDebug = false; // true to dump trace MplProfDataParser parser(m, memPool, enableDebug); int res = parser.ReadMapleProfileData(); if (res) { -- Gitee From 950b2ea4bba7c6a93a87681f927df0e8259824a2 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Mon, 22 Aug 2022 19:27:49 -0700 Subject: [PATCH 2/3] Undo modification to me_value_range_prop.cpp because the changes have issues --- .../maple_me/include/me_value_range_prop.h | 2 +- src/mapleall/maple_me/src/bb.cpp | 2 +- .../maple_me/src/me_value_range_prop.cpp | 35 +++++++------------ .../mpl2mpl/include/mpl_profdata_parser.h | 2 +- .../mpl2mpl/src/mpl_profdata_parser.cpp | 12 ++----- 5 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/mapleall/maple_me/include/me_value_range_prop.h b/src/mapleall/maple_me/include/me_value_range_prop.h index 4e9d8ce6b5..719e55c0fb 100644 --- a/src/mapleall/maple_me/include/me_value_range_prop.h +++ b/src/mapleall/maple_me/include/me_value_range_prop.h @@ -751,7 +751,7 @@ class ValueRangePropagation { bool OnlyHaveCondGotoStmt(BB &bb) const; bool RemoveUnreachableEdge(BB &pred, BB &bb, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, std::map> &ssaupdateCandsForCondExpr); - bool RemoveUnreachableBB(BB &condGotoBB, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, + void RemoveUnreachableBB(BB &condGotoBB, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, std::map> &ssaupdateCandsForCondExpr); BB *CreateNewGotoBBWithoutCondGotoStmt(BB &bb); void CopyMeStmts(BB &fromBB, BB &toBB); diff --git a/src/mapleall/maple_me/src/bb.cpp b/src/mapleall/maple_me/src/bb.cpp index f7ed0e990f..25b2aaa8dc 100644 --- a/src/mapleall/maple_me/src/bb.cpp +++ b/src/mapleall/maple_me/src/bb.cpp @@ -485,7 +485,7 @@ void BB::UpdateEdgeFreqs(bool updateBBFreqOfSucc) { succFreqs += GetSuccFreq()[i]; } int diff = static_cast(abs(succFreqs - GetFrequency())); - if (len == 0 || + if (len == 0 || (len == 1 && diff <= 0) || (len > 1 && diff <= 1)) { return; 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 c61b5d03c8..189ecab9ae 100644 --- a/src/mapleall/maple_me/src/me_value_range_prop.cpp +++ b/src/mapleall/maple_me/src/me_value_range_prop.cpp @@ -3134,9 +3134,6 @@ void ValueRangePropagation::AnalysisUnreachableBBOrEdge(BB &bb, BB &unreachableB if (onlyPropVR) { return; } - if (func.GetCfg()->UpdateCFGFreq() && unreachableBB.GetFrequency() > 0) { - return; - } for (auto &pred : bb.GetPred()) { UpdateProfile(*pred, bb, unreachableBB); } @@ -3511,7 +3508,7 @@ void ValueRangePropagation::UpdateProfile(BB &pred, BB &bb, const BB &targetBB) // \ / \ ----> | | // \ / \ | | // false true false true -bool ValueRangePropagation::RemoveUnreachableBB( +void ValueRangePropagation::RemoveUnreachableBB( BB &condGotoBB, BB &trueBranch, ScalarMeExpr *updateSSAExceptTheScalarExpr, std::map> &ssaupdateCandsForCondExpr) { CHECK_FATAL(condGotoBB.GetSucc().size() == kNumOperands, "must have 2 succ"); @@ -3521,22 +3518,17 @@ bool ValueRangePropagation::RemoveUnreachableBB( if (OnlyHaveOneCondGotoPredBB(*succ1, condGotoBB)) { AnalysisUnreachableBBOrEdge(condGotoBB, *succ1, trueBranch); } else { - int64_t removedFreq = 0; - if (func.GetCfg()->UpdateCFGFreq()) { - removedFreq = condGotoBB.GetSuccFreq()[1]; - if (removedFreq > 0) { - return false; // cannot delete an edge that is actually executed - } - } if (condGotoBB.GetPred().size() == 1) { UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); - condGotoBB.RemoveSucc(*succ1); + // update frequency before cfg changed if (func.GetCfg()->UpdateCFGFreq()) { + int64_t removedFreq = static_cast(condGotoBB.GetSuccFreq()[1]); condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); succ0->SetFrequency(static_cast(succ0->GetFrequency() + removedFreq)); } + condGotoBB.RemoveSucc(*succ1); DeleteThePhiNodeWhichOnlyHasOneOpnd(*succ1, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } @@ -3544,17 +3536,15 @@ bool ValueRangePropagation::RemoveUnreachableBB( if (OnlyHaveOneCondGotoPredBB(*succ0, condGotoBB)) { AnalysisUnreachableBBOrEdge(condGotoBB, *succ0, trueBranch); } else { - int64_t removedFreq = 0; - if (func.GetCfg()->UpdateCFGFreq()) { - removedFreq = condGotoBB.GetSuccFreq()[0]; - if (removedFreq > 0) { - return false; // cannot delete an edge that is actually executed - } - } if (condGotoBB.GetPred().size() == 1) { UpdateProfile(*condGotoBB.GetPred(0), condGotoBB, trueBranch); } condGotoBB.SetKind(kBBFallthru); + int64_t removedFreq = 0; + // update frequency before cfg changed + if (func.GetCfg()->UpdateCFGFreq()) { + removedFreq = static_cast(condGotoBB.GetSuccFreq()[0]); + } condGotoBB.RemoveSucc(*succ0); if (func.GetCfg()->UpdateCFGFreq()) { condGotoBB.SetSuccFreq(0, condGotoBB.GetFrequency()); @@ -3564,7 +3554,6 @@ bool ValueRangePropagation::RemoveUnreachableBB( condGotoBB.RemoveMeStmt(condGotoBB.GetLastMe()); } } - return true; } BB *ValueRangePropagation::CreateNewGotoBBWithoutCondGotoStmt(BB &bb) { @@ -3824,7 +3813,8 @@ bool ValueRangePropagation::CopyFallthruBBAndRemoveUnreachableEdge( currBB = currBB->GetSucc(0); } /* case2: the number of branches reaching to condBB == 1 */ - return RemoveUnreachableBB(*currBB, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); + RemoveUnreachableBB(*currBB, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); + return true; } // If the valuerange of opnd in pred0 and pred1 is equal the valuerange in true branch: @@ -3843,7 +3833,8 @@ bool ValueRangePropagation::RemoveTheEdgeOfPredBB( CHECK_FATAL(bb.GetKind() == kBBCondGoto, "must be condgoto bb"); if (GetRealPredSize(bb) < kNumOperands) { CHECK_FATAL(GetRealPredSize(bb) == 1, "must have one pred"); - return RemoveUnreachableBB(bb, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); + RemoveUnreachableBB(bb, trueBranch, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); + return true; } if (OnlyHaveCondGotoStmt(bb)) { PrepareForSSAUpdateWhenPredBBIsRemoved(pred, bb, updateSSAExceptTheScalarExpr, ssaupdateCandsForCondExpr); diff --git a/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h b/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h index 1a83fa5c83..386895da8a 100644 --- a/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h +++ b/src/mapleall/mpl2mpl/include/mpl_profdata_parser.h @@ -81,7 +81,7 @@ class ProfDataBinaryImportBase { class ProfileSummaryImport : public ProfDataBinaryImportBase { public: ProfileSummaryImport(std::string &outputFile, std::ifstream &input) : ProfDataBinaryImportBase(outputFile, input) {} - int ReadSummary(MplProfileData*); + void ReadSummary(MplProfileData*); private: void ReadMProfMagic(); diff --git a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp index 475bff7260..d8b5deefa1 100644 --- a/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp +++ b/src/mapleall/mpl2mpl/src/mpl_profdata_parser.cpp @@ -40,7 +40,7 @@ T ProfDataBinaryImportBase::ReadNum() { return static_cast(val); } -int ProfileSummaryImport::ReadSummary(MplProfileData *profData) { +void ProfileSummaryImport::ReadSummary(MplProfileData *profData) { CHECK_FATAL(profData != nullptr, "sanity check"); uint64_t magicNum = ReadNum(); if (magicNum != kMapleProfDataMagicNumber) { @@ -62,8 +62,6 @@ int ProfileSummaryImport::ReadSummary(MplProfileData *profData) { uint64_t r4 = ReadNum(); profData->summary.AddHistogramRecord(r1, r2, r3, r4); } - - return 0; } int FunctionProfileImport::ReadFuncProfile(MplProfileData *profData) { @@ -158,18 +156,14 @@ int MplProfDataParser::ReadMapleProfileData() { // read 1st part summary ProfileSummaryImport summaryImport(mprofDataFile, inputStream); summaryImport.SetPosition(static_cast(static_cast(buffer.get()))); - int res = summaryImport.ReadSummary(profData); - if (res) { - LogInfo::MapleLogger() << "no summary part\n"; - return 1; - } + summaryImport.ReadSummary(profData); if (dumpDetail) { profData->summary.DumpSummary(); } // read 2nd part function profile data FunctionProfileImport funcImport(mprofDataFile, inputStream); funcImport.SetPosition(summaryImport.GetPosition()); - res = funcImport.ReadFuncProfile(profData); + int res = funcImport.ReadFuncProfile(profData); if (res) { LogInfo::MapleLogger() << "no function profile part\n"; return 1; -- Gitee From d55c202f6342c5b8333e8720e54c798270af9481 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Mon, 22 Aug 2022 20:00:32 -0700 Subject: [PATCH 3/3] Update frequences in jumpThreading phase --- .../maple_me/src/me_jump_threading.cpp | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/mapleall/maple_me/src/me_jump_threading.cpp b/src/mapleall/maple_me/src/me_jump_threading.cpp index d699ae6cb3..a5679db5bf 100644 --- a/src/mapleall/maple_me/src/me_jump_threading.cpp +++ b/src/mapleall/maple_me/src/me_jump_threading.cpp @@ -159,6 +159,11 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorAddSucc(*newSuccBB); + if (func.GetCfg()->UpdateCFGFreq()) { + uint64 freqUsed = old2NewBB[idxOfCurrBB].second->GetFrequency(); + old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(freqUsed); + newSuccBB->SetFrequency(freqUsed); + } break; } for (auto *temp : currentBB->GetSucc()) { @@ -178,8 +183,16 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorReplaceSucc(temp, newTemp, true); + if (func.GetCfg()->UpdateCFGFreq()) { + int idxInSucc = currentBB->GetSuccIndex(*newTemp); + newTemp->SetFrequency(currentBB->GetSuccFreq()[idxInSucc]); + } } else { old2NewBB[idxOfCurrBB].second->AddSucc(*newTemp); + if (func.GetCfg()->UpdateCFGFreq()) { + old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(0); + newTemp->SetFrequency(0); + } } } else if (currentBB != old2NewBB[idxOfCurrBB].second) { // Deal with the case like the succ of currentBB is not in path. @@ -188,6 +201,9 @@ void JumpThreading::ConnectNewPath(std::vector &currPath, std::vectorAddSucc(*newTemp); + if (func.GetCfg()->UpdateCFGFreq()) { + old2NewBB[idxOfCurrBB].second->PushBackSuccFreq(newTemp->GetFrequency()); + } } // Set label for new bb. SetNewOffsetOfLastMeStmtForNewBB(*currentBB, *old2NewBB[idxOfCurrBB].second, *currentSucc, *newTemp); @@ -326,7 +342,7 @@ void JumpThreading::ExecuteJumpThreading() { CopyAndConnectPath(*currPath); if (JumpThreading::isDebug) { func.GetCfg()->DumpToFile( - "jump-threading-after" + std::to_string(func.jumpThreadingRuns) + "_" + std::to_string(i)); + "jump-threading-after" + std::to_string(func.jumpThreadingRuns) + "_" + std::to_string(i), false, func.GetCfg()->UpdateCFGFreq()); } startBBs[currPath->at(0)->GetBBId()] = true; DEBUG_LOG() << "============path " << i << "end\n"; @@ -709,7 +725,7 @@ bool MEJumpThreading::PhaseRun(maple::MeFunction &f) { if (JumpThreading::isDebug) { LogInfo::MapleLogger() << f.GetName() << "\n"; f.Dump(false); - f.GetCfg()->DumpToFile("jump-threading-before" + std::to_string(f.jumpThreadingRuns)); + f.GetCfg()->DumpToFile("jump-threading-before" + std::to_string(f.jumpThreadingRuns), false, f.GetCfg()->UpdateCFGFreq()); } std::map>> cands((std::less())); LoopScalarAnalysisResult sa(*irMap, nullptr); @@ -734,8 +750,8 @@ bool MEJumpThreading::PhaseRun(maple::MeFunction &f) { if (JumpThreading::isDebug) { LogInfo::MapleLogger() << f.GetName() << "\n"; f.Dump(false); - f.GetCfg()->DumpToFile("jump-threading-after" + std::to_string(f.jumpThreadingRuns)); + f.GetCfg()->DumpToFile("jump-threading-after" + std::to_string(f.jumpThreadingRuns), false, f.GetCfg()->UpdateCFGFreq()); } return false; } -} // namespace maple \ No newline at end of file +} // namespace maple -- Gitee