From a962430ff390bad29f190672dbc825c0481faf38 Mon Sep 17 00:00:00 2001 From: Fred Chow Date: Fri, 11 Feb 2022 22:30:56 -0800 Subject: [PATCH] made phi's inside loops speculative downsafe so saves are never inserted inside loops Disabled inserting saves as early as possible as this causes incorrectness (don't know why). --- src/mapleall/maple_be/include/cg/cg_ssa_pre.h | 5 +- src/mapleall/maple_be/include/cg/cg_ssu_pre.h | 6 +- src/mapleall/maple_be/src/cg/cg_ssa_pre.cpp | 80 +++++++++++++++++-- src/mapleall/maple_be/src/cg/cg_ssu_pre.cpp | 4 +- 4 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/mapleall/maple_be/include/cg/cg_ssa_pre.h b/src/mapleall/maple_be/include/cg/cg_ssa_pre.h index 847310596c..6c88c2bef2 100644 --- a/src/mapleall/maple_be/include/cg/cg_ssa_pre.h +++ b/src/mapleall/maple_be/include/cg/cg_ssa_pre.h @@ -123,6 +123,7 @@ class PhiOcc : public Occ { bool isDownsafe = true; + bool speculativeDownsafe = false; // true if set to downsafe via speculation bool isCanBeAvail = true; bool isLater = true; MapleVector phiOpnds; @@ -140,7 +141,7 @@ class ExitOcc : public Occ { class SSAPre { public: - SSAPre(CGFunc *cgfunc, DomAnalysis *dm, MemPool *memPool, SsaPreWorkCand *wkcand, bool enDebug) + SSAPre(CGFunc *cgfunc, DomAnalysis *dm, MemPool *memPool, SsaPreWorkCand *wkcand, bool aeap, bool enDebug) : cgFunc(cgfunc), dom(dm), preMp(memPool), @@ -153,6 +154,7 @@ class SSAPre { allOccs(preAllocator.Adapter()), phiOccs(preAllocator.Adapter()), exitOccs(preAllocator.Adapter()), + asEarlyAsPossible(aeap), enabledDebug(enDebug) {} ~SSAPre() = default; @@ -203,6 +205,7 @@ class SSAPre { MapleVector allOccs; MapleVector phiOccs; MapleVector exitOccs; + bool asEarlyAsPossible; bool enabledDebug; }; diff --git a/src/mapleall/maple_be/include/cg/cg_ssu_pre.h b/src/mapleall/maple_be/include/cg/cg_ssu_pre.h index b59725b170..e9a802b4b7 100644 --- a/src/mapleall/maple_be/include/cg/cg_ssu_pre.h +++ b/src/mapleall/maple_be/include/cg/cg_ssu_pre.h @@ -155,7 +155,7 @@ class SKillOcc : public SOcc { class SSUPre { public: - SSUPre(CGFunc *cgfunc, PostDomAnalysis *pd, MemPool *memPool, SPreWorkCand *wkcand, bool redSaves, bool enDebug) + SSUPre(CGFunc *cgfunc, PostDomAnalysis *pd, MemPool *memPool, SPreWorkCand *wkcand, bool alap, bool enDebug) : cgFunc(cgfunc), pdom(pd), spreMp(memPool), @@ -168,7 +168,7 @@ class SSUPre { allOccs(spreAllocator.Adapter()), lambdaOccs(spreAllocator.Adapter()), entryOccs(spreAllocator.Adapter()), - redundanciesAmongSaves(redSaves), + asLateAsPossible(alap), enabledDebug(enDebug) { CreateEntryOcc(cgfunc->GetFirstBB()); } @@ -225,7 +225,7 @@ class SSUPre { MapleVector allOccs; MapleVector lambdaOccs; MapleVector entryOccs; - bool redundanciesAmongSaves; // if there is redundancy among the input saves + bool asLateAsPossible; bool enabledDebug; }; diff --git a/src/mapleall/maple_be/src/cg/cg_ssa_pre.cpp b/src/mapleall/maple_be/src/cg/cg_ssa_pre.cpp index 1fcef68181..70db43dc23 100644 --- a/src/mapleall/maple_be/src/cg/cg_ssa_pre.cpp +++ b/src/mapleall/maple_be/src/cg/cg_ssa_pre.cpp @@ -13,6 +13,7 @@ * See the Mulan PSL v2 for more details. */ #include "cgfunc.h" +#include "loop.h" #include "cg_ssa_pre.h" namespace maplebe { @@ -26,7 +27,8 @@ void SSAPre::CodeMotion() { } PhiOpndOcc *phiOpndOcc = static_cast(occ); if (phiOpndOcc->insertHere) { - ASSERT(false, "should not do save of callee-save register at BB exit"); + ASSERT(phiOpndOcc->cgbb->GetLoop() == nullptr, "cg_ssapre: save inserted inside loop"); + workCand->saveAtEntryBBs.insert(phiOpndOcc->cgbb->GetId()); } } // pass 2 only doing deletion @@ -36,6 +38,7 @@ void SSAPre::CodeMotion() { } RealOcc *realOcc = static_cast(occ); if (!realOcc->redundant) { + ASSERT(realOcc->cgbb->GetLoop() == nullptr, "cg_ssapre: save in place inside loop"); workCand->saveAtEntryBBs.insert(realOcc->cgbb->GetId()); } } @@ -187,7 +190,7 @@ void SSAPre::ComputeLater() const { break; } } - if (existNonNullUse) { + if (existNonNullUse || phiOcc->speculativeDownsafe) { ResetLater(phiOcc); } } @@ -220,6 +223,9 @@ void SSAPre::ResetDownsafe(const PhiOpndOcc *phiOpnd) const { return; } PhiOcc *defPhiOcc = static_cast(defOcc); + if (defPhiOcc->speculativeDownsafe) { + return; + } if (!defPhiOcc->isDownsafe) { return; } @@ -242,6 +248,9 @@ void SSAPre::ComputeDownsafe() const { LogInfo::MapleLogger() << " _______ after downsafe computation _______" << '\n'; for (PhiOcc *phiOcc : phiOccs) { phiOcc->Dump(); + if (phiOcc->speculativeDownsafe) { + LogInfo::MapleLogger() << " spec_downsafe /"; + } if (phiOcc->isDownsafe) { LogInfo::MapleLogger() << " downsafe"; } @@ -251,6 +260,22 @@ void SSAPre::ComputeDownsafe() const { } // ================ Step 2: rename ================ +static void PropagateSpeculativeDownsafe(PhiOcc *phiOcc) { + if (phiOcc->speculativeDownsafe) { + return; + } + phiOcc->isDownsafe = true; + phiOcc->speculativeDownsafe = true; + for (PhiOpndOcc *phiOpndOcc : phiOcc->phiOpnds) { + if (phiOpndOcc->def != nullptr && phiOpndOcc->def->occTy == kAOccPhi) { + PhiOcc *nextPhiOcc = static_cast(phiOpndOcc->def); + if (nextPhiOcc->cgbb->GetLoop() != nullptr) { + PropagateSpeculativeDownsafe(nextPhiOcc); + } + } + } +} + void SSAPre::Rename() { std::stack occStack; classCount = 0; @@ -265,7 +290,10 @@ void SSAPre::Rename() { if (!occStack.empty()) { Occ *topOcc = occStack.top(); if (topOcc->occTy == kAOccPhi) { - static_cast(topOcc)->isDownsafe = false; + PhiOcc *phiTopOcc = static_cast(topOcc); + if (!phiTopOcc->speculativeDownsafe) { + phiTopOcc->isDownsafe = false; + } } } break; @@ -285,6 +313,10 @@ void SSAPre::Rename() { occ->classId = topOcc->classId; if (topOcc->occTy == kAOccPhi) { occStack.push(occ); + if (occ->cgbb->GetLoop() != nullptr) { + static_cast(topOcc)->isDownsafe = true; + static_cast(topOcc)->speculativeDownsafe = true; + } } break; } @@ -306,10 +338,29 @@ void SSAPre::Rename() { break; } } + // loop thru phiOccs to propagate speculativeDownsafe + for (PhiOcc *phiOcc : phiOccs) { + if (phiOcc->speculativeDownsafe) { + for (PhiOpndOcc *phiOpndOcc : phiOcc->phiOpnds) { + if (phiOpndOcc->def != nullptr && phiOpndOcc->def->occTy == kAOccPhi) { + PhiOcc *nextPhiOcc = static_cast(phiOpndOcc->def); + if (nextPhiOcc->cgbb->GetLoop() != nullptr) { + PropagateSpeculativeDownsafe(nextPhiOcc); + } + } + } + } + } if (enabledDebug) { LogInfo::MapleLogger() << " _______ after rename _______" << '\n'; for (Occ *occ : allOccs) { occ->Dump(); + if (occ->occTy == kAOccPhi) { + PhiOcc *phiOcc = static_cast(occ); + if (phiOcc->speculativeDownsafe) { + LogInfo::MapleLogger() << " spec_downsafe /"; + } + } LogInfo::MapleLogger() << '\n'; } } @@ -478,14 +529,27 @@ void SSAPre::PropagateNotAnt(BB *bb, std::set *visitedBBs) { void SSAPre::FormRealsNExits() { std::set visitedBBs; - PropagateNotAnt(cgFunc->GetCommonExitBB(), &visitedBBs); + if (asEarlyAsPossible) { + for (BB *cgbb : cgFunc->GetExitBBsVec()) { + if (!cgbb->IsUnreachable()) { + PropagateNotAnt(cgbb, &visitedBBs); + } + } + } for (uint32 i = 0; i < dom->GetDtPreOrderSize(); i++) { BBId bbid = dom->GetDtPreOrderItem(i); BB *cgbb = cgFunc->GetAllBBs()[bbid]; - if (fullyAntBBs[cgbb->GetId()]) { - RealOcc *realOcc = preMp->New(cgbb); - realOccs.push_back(realOcc); + if (asEarlyAsPossible) { + if (fullyAntBBs[cgbb->GetId()]) { + RealOcc *realOcc = preMp->New(cgbb); + realOccs.push_back(realOcc); + } + } else { + if (workCand->occBBs.count(cgbb->GetId()) != 0) { + RealOcc *realOcc = preMp->New(cgbb); + realOccs.push_back(realOcc); + } } if (!cgbb->IsUnreachable() && (cgbb->NumSuccs() == 0 || cgbb->GetKind() == BB::kBBReturn)) { ExitOcc *exitOcc = preMp->New(cgbb); @@ -528,7 +592,7 @@ void SSAPre::ApplySSAPre() { void DoSavePlacementOpt(CGFunc *f, DomAnalysis *dom, SsaPreWorkCand *workCand) { MemPool *tempMP = memPoolCtrler.NewMemPool("cg_ssa_pre", true); - SSAPre cgssapre(f, dom, tempMP, workCand, false/*enabledDebug*/); + SSAPre cgssapre(f, dom, tempMP, workCand, false/*asEarlyAsPossible*/, false/*enabledDebug*/); cgssapre.ApplySSAPre(); diff --git a/src/mapleall/maple_be/src/cg/cg_ssu_pre.cpp b/src/mapleall/maple_be/src/cg/cg_ssu_pre.cpp index d7fcff18af..c6d42f688b 100644 --- a/src/mapleall/maple_be/src/cg/cg_ssu_pre.cpp +++ b/src/mapleall/maple_be/src/cg/cg_ssu_pre.cpp @@ -521,7 +521,7 @@ void SSUPre::PropagateNotAvail(BB *bb, std::set *visitedBBs) { } void SSUPre::FormReals() { - if (redundanciesAmongSaves) { + if (!asLateAsPossible) { for (uint32 i = 0; i < pdom->GetPdtPreOrderSize(); i++) { BBId bbid = pdom->GetPdtPreOrderItem(i); BB *cgbb = cgFunc->GetAllBBs()[bbid]; @@ -593,7 +593,7 @@ void SSUPre::ApplySSUPre() { void DoRestorePlacementOpt(CGFunc *f, PostDomAnalysis *pdom, SPreWorkCand *workCand) { MemPool *tempMP = memPoolCtrler.NewMemPool("cg_ssu_pre", true); - SSUPre cgssupre(f, pdom, tempMP, workCand, false/*redundanciesAmongSaves*/, false/*enabledDebug*/); + SSUPre cgssupre(f, pdom, tempMP, workCand, true/*asLateAsPossible*/, false/*enabledDebug*/); cgssupre.ApplySSUPre(); -- Gitee