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 8b23669479e03358151b662049c438b9b218dfd0..11e43f48bec76edd7b81ed0be2e214a39f4e1833 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 -#undef USE_BB_FREQUENCY #define OPTIMIZE_FOR_PROLOG #undef REUSE_SPILLMEM #undef COLOR_SPLIT @@ -524,11 +523,11 @@ class LiveRange { ++numUses; } - uint32 GetFrequency() const { + float GetFrequency() const { return frequency; } - void SetFrequency(uint32 frequency) { + void SetFrequency(float frequency) { this->frequency = frequency; } #endif /* OPTIMIZE_FOR_PROLOG */ @@ -621,7 +620,7 @@ class LiveRange { #ifdef OPTIMIZE_FOR_PROLOG uint32 numDefs = 0; uint32 numUses = 0; - uint32 frequency = 0; + float frequency = 0; #endif /* OPTIMIZE_FOR_PROLOG */ MemOperand *spillMem = nullptr; /* memory operand used for spill, if any */ regno_t spillReg = 0; /* register operand for spill at current point */ diff --git a/src/mapleall/maple_be/include/cg/cg_option.h b/src/mapleall/maple_be/include/cg/cg_option.h index da144fda935bb6cbc0a9c034114b0fbb4f58ebb6..cdba19112a7e1f9ea09d20136f3f466da02f74f2 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/include/cg/cgbb.h b/src/mapleall/maple_be/include/cg/cgbb.h index a08a0d748ae0b476847d91720e9679e7687941ee..8da16f8d187ae04f0fdbe74f56f0d91b92bbac27 100644 --- a/src/mapleall/maple_be/include/cg/cgbb.h +++ b/src/mapleall/maple_be/include/cg/cgbb.h @@ -263,10 +263,10 @@ class BB { void SetLevel(uint32 arg) { level = arg; } - uint32 GetFrequency() const { + float GetFrequency() const { return frequency; } - void SetFrequency(uint32 arg) { + void SetFrequency(float arg) { frequency = arg; } BB *GetNext() { @@ -689,7 +689,7 @@ class BB { static const std::string bbNames[kBBLast]; uint32 id; uint32 level = 0; - uint32 frequency = 0; + float frequency = 0; BB *prev = nullptr; /* Doubly linked list of BBs; */ BB *next = nullptr; /* They represent the order in which blocks are to be emitted. */ diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index 2a6b15c9a69ca6a05fc4ce79c6a0df03229afb23..7a098d0d1d766ec6eefbadbce3f7a08a5a4a981a 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -35,7 +35,7 @@ namespace maplebe { constexpr int32 kBBLimit = 10000; -constexpr int32 kFreqBase = 10000; +constexpr int32 kFreqBase = 500000; struct MemOpndCmp { bool operator()(const MemOperand *lhs, const MemOperand *rhs) const { CHECK_FATAL(lhs != nullptr, "null ptr check"); @@ -142,6 +142,7 @@ class CGFunc { void GenerateInstruction(); bool MemBarOpt(StmtNode &membar); void UpdateCallBBFrequency(); + void BuildStaticBBFrequency(); void HandleFunction(); void ProcessExitBBVec(); virtual void MergeReturn() = 0; @@ -853,7 +854,7 @@ class CGFunc { return bb; } - BB *CreateNewBB(bool unreachable, BB::BBKind kind, uint32 frequency) { + BB *CreateNewBB(bool unreachable, BB::BBKind kind, float frequency) { BB *newBB = CreateNewBB(); newBB->SetKind(kind); newBB->SetUnreachable(unreachable); @@ -861,7 +862,7 @@ class CGFunc { return newBB; } - BB *CreateNewBB(LabelIdx label, bool unreachable, BB::BBKind kind, uint32 frequency) { + BB *CreateNewBB(LabelIdx label, bool unreachable, BB::BBKind kind, float frequency) { BB *newBB = CreateNewBB(unreachable, kind, frequency); newBB->AddLabel(label); SetLab2BBMap(label, *newBB); @@ -1000,7 +1001,7 @@ class CGFunc { bool isAfterRegAlloc = false; bool isAggParamInReg = false; bool hasTakenLabel = false; - uint32 frequency = 0; + float frequency = 0; DebugInfo *debugInfo = nullptr; /* debugging info */ MapleVector dbgCallFrameLocations; RegOperand *aggParamReg = nullptr; diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp index d4c75998242c93ee06626f8b98fe9a68971ec3c3..4015e0fdf43f7870bad7d422b6422f9ffb06b9a3 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_cgfunc.cpp @@ -5672,7 +5672,7 @@ void AArch64CGFunc::MergeReturn() { LabelIdx labidx = CreateLabel(); LabelOperand &targetOpnd = GetOrCreateLabelOperand(labidx); - uint32 freq = 0; + float freq = 0; for (auto *tmpBB : GetExitBBsVec()) { ASSERT(tmpBB->GetKind() == BB::kBBReturn, "Error: suppose to merge multi return bb"); tmpBB->SetKind(BB::kBBGoto); @@ -6078,7 +6078,7 @@ void AArch64CGFunc::HandleRCCall(bool begin, const MIRSymbol *retRef) { AppendCall(*callSym, *srcOpnds); GetCurBB()->SetHasCall(); - if (frequency != 0) { + if (frequency != 0.0) { GetCurBB()->SetFrequency(frequency); } return; @@ -6111,7 +6111,7 @@ void AArch64CGFunc::HandleRCCall(bool begin, const MIRSymbol *retRef) { Insn &callInsn = AppendCall(*callSym, *srcOpnds, true); static_cast(callInsn).SetRefSkipIndex(stOffset); GetCurBB()->SetHasCall(); - if (frequency != 0) { + if (frequency != 0.0) { GetCurBB()->SetFrequency(frequency); } return; @@ -6193,7 +6193,7 @@ void AArch64CGFunc::HandleRCCall(bool begin, const MIRSymbol *retRef) { Insn &callInsn = AppendCall(*sym, *srcOpnds, true); static_cast(callInsn).SetRefSkipIndex(refSkipIndex); - if (frequency != 0) { + if (frequency != 0.0) { GetCurBB()->SetFrequency(frequency); } GetCurBB()->SetHasCall(); 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 df49ab6e13c1746f2cc7bb00efc1a4e192a891c2..d046aae9af4845bfb97455ba3b2d40e503b5e846 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 @@ -55,6 +55,12 @@ constexpr uint32 kLoopWeight = 20; constexpr uint32 kAdjustWeight = 2; constexpr uint32 kInsnStep = 2; constexpr uint32 kMaxSplitCount = 3; +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) @@ -257,29 +263,38 @@ void GraphColorRegAllocator::CalculatePriority(LiveRange &lr) const { #endif /* RANDOM_PRIORITY */ 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(); -#else /* USE_BB_FREQUENCY */ - if (bb->GetLoop() != nullptr) { - uint32 loopFactor; - if (lr.GetNumCall() > 0) { - loopFactor = bb->GetLoop()->GetLoopLevel() * kAdjustWeight; + float mult; + 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 (lr.GetNumCall()) { + 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; } }; @@ -290,6 +305,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 { @@ -1213,7 +1234,7 @@ void GraphColorRegAllocator::Separate() { #endif /* USE_LRA */ #ifdef OPTIMIZE_FOR_PROLOG if (doOptProlog && ((lr->GetNumDefs() <= 1) && (lr->GetNumUses() <= 1) && (lr->GetNumCall() > 0)) && - (lr->GetFrequency() <= (cgFunc->GetFirstBB()->GetFrequency() << 1))) { + (lr->GetFrequency() <= (cgFunc->GetFirstBB()->GetFrequency() * 2))) { if (lr->GetRegType() == kRegTyInt) { intDelayed.emplace_back(lr); } else { diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_reg_alloc.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_reg_alloc.cpp index 465f27c657235ff4bd3b07d18065af2fd63c5425..4dc26bb15c139854a1df0eed6de1dd34fc1b2ba7 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_reg_alloc.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_reg_alloc.cpp @@ -566,6 +566,9 @@ bool CgRegAlloc::PhaseRun(maplebe::CGFunc &f) { &CgDomAnalysis::id, f); dom = static_cast(it)->GetResult(); CHECK_FATAL(dom != nullptr, "null ptr check"); + if (CGOptions::UseRaBBFreq()) { + f.BuildStaticBBFrequency(); + } } while (success == false) { MemPool *phaseMp = GetPhaseMemPool(); diff --git a/src/mapleall/maple_be/src/cg/cg_option.cpp b/src/mapleall/maple_be/src/cg/cg_option.cpp index 3acb12f11b44637fe0d67334286b71ee6d29f07b..2c9d060fc855189e8c1cfb95f40913500cda7d6e 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