From 380ee2c0cf6639daa011e63654e819a23201c05d Mon Sep 17 00:00:00 2001 From: fye Date: Mon, 24 Oct 2022 10:39:33 -0700 Subject: [PATCH 1/2] PGO: profileGen me emit and others --- src/mapleall/maple_ir/src/mir_lower.cpp | 4 ++ src/mapleall/maple_me/src/me_emit.cpp | 61 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/src/mapleall/maple_ir/src/mir_lower.cpp b/src/mapleall/maple_ir/src/mir_lower.cpp index 389dc6fc87..9c989137c0 100644 --- a/src/mapleall/maple_ir/src/mir_lower.cpp +++ b/src/mapleall/maple_ir/src/mir_lower.cpp @@ -307,6 +307,10 @@ BlockNode *MIRLower::LowerSwitchStmt(SwitchNode *switchNode) { blk->AddStatement(condGoto); GotoNode *gotoStmt = builder->CreateStmtGoto(OP_goto, defaultLabel); blk->AddStatement(gotoStmt); + if (Options::profileUse && mirModule.CurFunction()->GetFuncProfData()) { + mirModule.CurFunction()->GetFuncProfData()->SetStmtFreq(gotoStmt->GetStmtID(), + mirModule.CurFunction()->GetFuncProfData()->GetStmtFreq(switchNode->GetStmtID())); + } } else { // brtrue (x < minCaseVal) @default_label // brtrue (x > maxCaseVal) @default_label diff --git a/src/mapleall/maple_me/src/me_emit.cpp b/src/mapleall/maple_me/src/me_emit.cpp index eaea1c8ede..886640aed2 100644 --- a/src/mapleall/maple_me/src/me_emit.cpp +++ b/src/mapleall/maple_me/src/me_emit.cpp @@ -170,12 +170,73 @@ bool ProfileGenEmit::PhaseRun(maple::MeFunction &f) { sym->SetIsDeleted(); } } +#ifdef yefeng + if (Options::profileGen ) { + std::unordered_map visitedBBs; + std::stack listBBs; + std::vector predDep(f.GetCfg()->GetAllBBs().size(), nullptr); + std::vector succDep(f.GetCfg()->GetAllBBs().size(), nullptr); + // Set up pred-succ single fallthrough chain dependency + for (BB *bb : f.GetCfg()->GetAllBBs()) { + if (bb != nullptr && bb->GetSucc().size() != 0 && + ((bb->GetKind() == kBBCondGoto) || (bb->GetKind() == kBBFallthru) || bb->GetKind() == kBBUnknown)) { + predDep[bb->GetSucc()[0]->GetBBId()] = bb; + succDep[bb->GetBBId()] = bb->GetSucc()[0]; + } + } + // Emit all dependency chains of BBs firstd + for (BB *succ : succDep) { + if (!succ) { + continue; + } + BB *pred = predDep[succ->GetBBId()]; + if (pred && !visitedBBs[pred]) { + pred->EmitBB(*mirFunction->GetBody(), false); + visitedBBs[pred] = true; + while (succ && !visitedBBs[succ]) { + succ->EmitBB(*mirFunction->GetBody(), false); + visitedBBs[succ] = true; + succ = succDep[succ->GetBBId()]; + } + } + } + // Emit remaining BBs + for (BB *bb : f.GetCfg()->GetAllBBs()) { + if (bb == nullptr || visitedBBs[bb]) { + continue; + } else { + listBBs.push(bb); + while (!listBBs.empty()) { + bb = listBBs.top(); + listBBs.pop(); + bb->EmitBB(*mirFunction->GetBody(), false); + visitedBBs[bb] = true; + // Make sure default target BB to emit first + for (int32 idx = bb->GetSucc().size() - 1; idx >= 0; idx--) { + if (!visitedBBs[bb->GetSucc()[idx]]) { + listBBs.push(bb->GetSucc()[idx]); + } + } + } + } + } + } else { + for (BB *bb : f.GetCfg()->GetAllBBs()) { + if (bb == nullptr) { + continue; + } + bb->EmitBB(*mirFunction->GetBody(), false); + } + } + +#else for (BB *bb : f.GetCfg()->GetAllBBs()) { if (bb == nullptr) { continue; } bb->EmitBB(*mirFunction->GetBody(), false); } +#endif ResetDependentedSymbolLive(mirFunction); } return false; -- Gitee From cf3b7cb5b4447a8ad2ab63d993a127210c4ca995 Mon Sep 17 00:00:00 2001 From: fye Date: Fri, 11 Nov 2022 08:43:34 -0800 Subject: [PATCH 2/2] PGO: profileGen me emit and others --- src/mapleall/maple_me/src/me_emit.cpp | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/mapleall/maple_me/src/me_emit.cpp b/src/mapleall/maple_me/src/me_emit.cpp index 886640aed2..67afe269e1 100644 --- a/src/mapleall/maple_me/src/me_emit.cpp +++ b/src/mapleall/maple_me/src/me_emit.cpp @@ -170,7 +170,6 @@ bool ProfileGenEmit::PhaseRun(maple::MeFunction &f) { sym->SetIsDeleted(); } } -#ifdef yefeng if (Options::profileGen ) { std::unordered_map visitedBBs; std::stack listBBs; @@ -228,15 +227,6 @@ bool ProfileGenEmit::PhaseRun(maple::MeFunction &f) { bb->EmitBB(*mirFunction->GetBody(), false); } } - -#else - for (BB *bb : f.GetCfg()->GetAllBBs()) { - if (bb == nullptr) { - continue; - } - bb->EmitBB(*mirFunction->GetBody(), false); - } -#endif ResetDependentedSymbolLive(mirFunction); } return false; -- Gitee