From 9c64f4cb604fb49c26cff8b4e32eca626577dd98 Mon Sep 17 00:00:00 2001 From: William Chen Date: Tue, 31 Aug 2021 02:37:56 -0700 Subject: [PATCH 1/4] BB frequency based coloring priority RA --- .../include/cg/aarch64/aarch64_color_ra.h | 2 +- src/mapleall/maple_be/include/cg/cgfunc.h | 1 + .../src/cg/aarch64/aarch64_color_ra.cpp | 10 ++++ src/mapleall/maple_be/src/cg/cgfunc.cpp | 50 +++++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h b/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h index 3ce9ce49dd..ecc73275d1 100644 --- a/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h +++ b/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h @@ -27,7 +27,7 @@ namespace maplebe { #define USE_LRA #define USE_SPLIT -#undef USE_BB_FREQUENCY +#define USE_BB_FREQUENCY #define OPTIMIZE_FOR_PROLOG #undef REUSE_SPILLMEM #undef COLOR_SPLIT diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index a4d023136a..5d018c5110 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -138,6 +138,7 @@ class CGFunc { void GenerateInstruction(); bool MemBarOpt(StmtNode &membar); void UpdateCallBBFrequency(); + void BuildStaticBBFrequency(); void HandleFunction(); void ProcessExitBBVec(); virtual void MergeReturn() = 0; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp index 45fbba1703..313f8fb570 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp @@ -51,8 +51,10 @@ namespace maplebe { * R0->GetRegisterNumber() == 1 * V0->GetRegisterNumber() == 33 */ +#ifndef USE_BB_FREQUENCY constexpr uint32 kLoopWeight = 20; constexpr uint32 kAdjustWeight = 2; +#endif constexpr uint32 kInsnStep = 2; #define GCRA_DUMP CG_DEBUG_FUNC(*cgFunc) @@ -254,6 +256,9 @@ void GraphColorRegAllocator::CalculatePriority(LiveRange &lr) const { } return; #endif /* RANDOM_PRIORITY */ +#ifdef USE_BB_FREQUENCY + cgFunc->BuildStaticBBFrequency(); +#endif float pri = 0.0; uint32 bbNum = 0; auto calculatePriorityFunc = [&lr, &bbNum, &pri, this] (uint32 bbID) { @@ -266,6 +271,11 @@ void GraphColorRegAllocator::CalculatePriority(LiveRange &lr) const { uint32 mult; #ifdef USE_BB_FREQUENCY mult = bb->GetFrequency(); + if (lr.GetNumCall() > 0) { + mult *= 2; + } else { + mult /= 2; + } #else /* USE_BB_FREQUENCY */ if (bb->GetLoop() != nullptr) { uint32 loopFactor; diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index 7e51397b40..ed5bd7acb5 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -1592,6 +1592,56 @@ void CGFunc::UpdateCallBBFrequency() { } } +void BBFrequencySet(BB *bb) { + uint32 freq = bb->GetFrequency(); + for (auto pred : bb->GetPreds()) { + uint32 predFreq = pred->GetFrequency(); + if (pred->GetLoop()) { + predFreq /= (pred->GetLoop()->GetLoopLevel() * 10); + } + freq += (predFreq / pred->GetSuccs().size()); + } + if (bb->GetLoop()) { + freq *= (bb->GetLoop()->GetLoopLevel() * 10); + } + bb->SetFrequency(freq); +} + +void BBFrequencyVisitSucc(std::queue &BBQueue) { + while (BBQueue.size()) { + BB *bb = BBQueue.front(); + BBQueue.pop(); + BBFrequencySet(bb); + for (auto succ : bb->GetSuccs()) { + if (succ->GetFrequency()) { + continue; + } + bool allPredVisited = true; + for (auto pred : succ->GetPreds()) { + auto &loopPred = succ->GetLoopPreds(); + if (std::find(loopPred.begin(), loopPred.end(), pred) != loopPred.end()) { + continue; + } + if (pred->GetFrequency() == 0) { + allPredVisited = false; + break; + } + } + if (allPredVisited) { + BBQueue.push(succ); + } + } + } +} + +/* build static bb frequency assuming loop detection already done */ +void CGFunc::BuildStaticBBFrequency() { + BB *bb = FIRST_BB_OF_FUNC(this); + std::queue BBQueue; + BBQueue.push(bb); + BBFrequencyVisitSucc(BBQueue); +} + void CGFunc::HandleFunction() { /* select instruction */ GenerateInstruction(); -- Gitee From 6515c5e055ae5a5784fcd4fe80ec90931fcade57 Mon Sep 17 00:00:00 2001 From: William Chen Date: Thu, 16 Sep 2021 00:09:40 -0700 Subject: [PATCH 2/4] Color RA change to priority function. Replace checking of calls for entire LR to each individual bb. Lower priority of long LR with few def and uses in large function. --- .../include/cg/aarch64/aarch64_color_ra.h | 1 - src/mapleall/maple_be/include/cg/cg_option.h | 14 +++++ .../src/cg/aarch64/aarch64_color_ra.cpp | 58 ++++++++++++------- src/mapleall/maple_be/src/cg/cg_option.cpp | 15 +++++ 4 files changed, 65 insertions(+), 23 deletions(-) diff --git a/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h b/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h index 7c4678f155..afd4d5b046 100644 --- a/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h +++ b/src/mapleall/maple_be/include/cg/aarch64/aarch64_color_ra.h @@ -27,7 +27,6 @@ namespace maplebe { #define USE_LRA #define USE_SPLIT -#define USE_BB_FREQUENCY #define OPTIMIZE_FOR_PROLOG #undef REUSE_SPILLMEM #undef COLOR_SPLIT diff --git a/src/mapleall/maple_be/include/cg/cg_option.h b/src/mapleall/maple_be/include/cg/cg_option.h index da144fda93..cdba19112a 100644 --- a/src/mapleall/maple_be/include/cg/cg_option.h +++ b/src/mapleall/maple_be/include/cg/cg_option.h @@ -663,6 +663,7 @@ class CGOptions : public MapleDriverOptionBase { static bool DoGlobalOpt() { return doGlobalOpt; } + static void EnableVregRename() { doVregRename = true; } @@ -675,6 +676,18 @@ class CGOptions : public MapleDriverOptionBase { return doVregRename; } + static void EnableUseRaBBFreq() { + useRaBBFreq = true; + } + + static void DisableUseRaBBFreq() { + useRaBBFreq = false; + } + + static bool UseRaBBFreq() { + return useRaBBFreq; + } + static void EnableMultiPassColorRA() { doMultiPassColorRA = true; } @@ -1126,6 +1139,7 @@ class CGOptions : public MapleDriverOptionBase { static bool doStoreLoadOpt; static bool doGlobalOpt; static bool doVregRename; + static bool useRaBBFreq; static bool doMultiPassColorRA; static bool doPrePeephole; static bool doPeephole; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp index baabe6a541..87194c5146 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_color_ra.cpp @@ -51,11 +51,15 @@ namespace maplebe { * R0->GetRegisterNumber() == 1 * V0->GetRegisterNumber() == 33 */ -#ifndef USE_BB_FREQUENCY constexpr uint32 kLoopWeight = 20; constexpr uint32 kAdjustWeight = 2; -#endif constexpr uint32 kInsnStep = 2; +constexpr uint32 kPriorityDefThreashold = 1; +constexpr uint32 kPriorityUseThreashold = 5; +constexpr uint32 kPriorityBBThreashold = 1000; +constexpr float kPriorityRatioThreashold = 0.9; +constexpr float kPriorityCallMult = 1.1; +constexpr float kPriorityNoCallMult = 0.9; #define GCRA_DUMP CG_DEBUG_FUNC(*cgFunc) @@ -256,39 +260,43 @@ void GraphColorRegAllocator::CalculatePriority(LiveRange &lr) const { } return; #endif /* RANDOM_PRIORITY */ -#ifdef USE_BB_FREQUENCY - cgFunc->BuildStaticBBFrequency(); -#endif + if (CGOptions::UseRaBBFreq()) { + cgFunc->BuildStaticBBFrequency(); + } float pri = 0.0; uint32 bbNum = 0; - auto calculatePriorityFunc = [&lr, &bbNum, &pri, this] (uint32 bbID) { + uint32 numDefs = 0; + uint32 numUses = 0; + auto calculatePriorityFunc = [&lr, &bbNum, &numDefs, &numUses, &pri, this] (uint32 bbID) { auto lu = lr.FindInLuMap(bbID); ASSERT(lu != lr.EndOfLuMap(), "can not find live unit"); BB *bb = bbVec[bbID]; if (bb->GetFirstInsn() != nullptr && !bb->IsSoloGoto()) { ++bbNum; + numDefs += lu->second->GetDefNum(); + numUses += lu->second->GetUseNum(); uint32 useCnt = lu->second->GetDefNum() + lu->second->GetUseNum(); uint32 mult; -#ifdef USE_BB_FREQUENCY - mult = bb->GetFrequency(); - if (lr.GetNumCall() > 0) { - mult *= 2; - } else { - mult /= 2; - } -#else /* USE_BB_FREQUENCY */ - if (bb->GetLoop() != nullptr) { - uint32 loopFactor; - if (lr.GetNumCall() > 0) { - loopFactor = bb->GetLoop()->GetLoopLevel() * kAdjustWeight; + if (CGOptions::UseRaBBFreq()) { + mult = bb->GetFrequency(); + if (bb->HasCall()) { + mult *= kPriorityCallMult; } else { - loopFactor = bb->GetLoop()->GetLoopLevel() / kAdjustWeight; + mult *= kPriorityNoCallMult; } - mult = static_cast(pow(kLoopWeight, loopFactor)); } else { - mult = 1; + if (bb->GetLoop() != nullptr) { + uint32 loopFactor; + if (bb->HasCall()) { + loopFactor = bb->GetLoop()->GetLoopLevel() * kAdjustWeight; + } else { + loopFactor = bb->GetLoop()->GetLoopLevel() / kAdjustWeight; + } + mult = static_cast(pow(kLoopWeight, loopFactor)); + } else { + mult = 1; + } } -#endif /* USE_BB_FREQUENCY */ pri += useCnt * mult; } }; @@ -299,6 +307,12 @@ void GraphColorRegAllocator::CalculatePriority(LiveRange &lr) const { } else { lr.SetPriority(0.0); } + if (lr.GetPriority() > 0 && numDefs <= kPriorityDefThreashold && numUses <= kPriorityUseThreashold && + cgFunc->NumBBs() > kPriorityBBThreashold && + (float(lr.GetNumBBMembers()) / cgFunc->NumBBs()) > kPriorityRatioThreashold) { + /* for large functions, delay allocating long LR with few defs and uses */ + lr.SetPriority(0.0); + } } void GraphColorRegAllocator::PrintBBs() const { diff --git a/src/mapleall/maple_be/src/cg/cg_option.cpp b/src/mapleall/maple_be/src/cg/cg_option.cpp index 3acb12f11b..2c9d060fc8 100644 --- a/src/mapleall/maple_be/src/cg/cg_option.cpp +++ b/src/mapleall/maple_be/src/cg/cg_option.cpp @@ -66,6 +66,7 @@ bool CGOptions::doICO = false; bool CGOptions::doStoreLoadOpt = false; bool CGOptions::doGlobalOpt = false; bool CGOptions::doVregRename = false; +bool CGOptions::useRaBBFreq = true; bool CGOptions::doMultiPassColorRA = true; bool CGOptions::doPrePeephole = false; bool CGOptions::doPeephole = false; @@ -125,6 +126,7 @@ enum OptionIndex : uint64 { kPreSchedule, kSchedule, kVregRename, + kRaBBFreq, kMultiPassRA, kWriteRefFieldOpt, kDumpOlog, @@ -437,6 +439,16 @@ const Descriptor kUsage[] = { " --no-vreg-rename\n", "mplcg", {} }, + { kRaBBFreq, + kEnable, + "", + "ra-freq", + kBuildTypeExperimental, + kArgCheckPolicyBool, + " --ra-freq \tUse BB frequency for coloring RA\n" + " --no-ra-freq\n", + "mplcg", + {} }, { kMultiPassRA, kEnable, "", @@ -1355,6 +1367,9 @@ bool CGOptions::SolveOptions(const std::vector