llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-backend-hexagon Author: Ikhlas Ajbar (iajbar) <details> <summary>Changes</summary> The WindowScheduler clones the loop body into TripleMBB and advances the base registers of memory accesses to later iterations, but the clones keep the original iteration's memory operands. The per-window scheduler could therefore treat a store and a load from different copies as non-aliasing and reorder them, even though they touch the same address in consecutive iterations, producing wrong code. Detect loop-carried memory dependences on the original loop and enforce them with a DAG mutation while list scheduling each window. Backport of llvm/llvm-project@<!-- -->b1db34427810b2d14af8293a8c5493119c5ccb45. Fixes #<!-- -->209946 --- Patch is 40.62 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/243336.diff 5 Files Affected: - (modified) llvm/include/llvm/CodeGen/MachinePipeliner.h (+17-4) - (modified) llvm/include/llvm/CodeGen/WindowScheduler.h (+25-3) - (modified) llvm/lib/CodeGen/MachinePipeliner.cpp (+38-36) - (modified) llvm/lib/CodeGen/WindowScheduler.cpp (+194-4) - (added) llvm/test/CodeGen/Hexagon/swp-ws-loop-carried-dep.mir (+325) ``````````diff diff --git a/llvm/include/llvm/CodeGen/MachinePipeliner.h b/llvm/include/llvm/CodeGen/MachinePipeliner.h index 6c06ea4b2927ea..70364248f4ea07 100644 --- a/llvm/include/llvm/CodeGen/MachinePipeliner.h +++ b/llvm/include/llvm/CodeGen/MachinePipeliner.h @@ -63,6 +63,23 @@ class SMSchedule; extern LLVM_ABI cl::opt<bool> SwpEnableCopyToPhi; extern LLVM_ABI cl::opt<int> SwpForceIssueWidth; +/// Return true if BaseMI and OtherMI may access overlapping memory in a later +/// iteration of the loop LoopBB. Returns true conservatively whenever overlap +/// cannot be ruled out. +LLVM_ABI bool mayOverlapInLaterIter(const MachineInstr *BaseMI, + const MachineInstr *OtherMI, + const MachineBasicBlock *LoopBB, + const MachineRegisterInfo &MRI, + const TargetInstrInfo &TII); + +/// Software pipelining policy for a loop, which a target can customize by +/// implementing TargetSubtargetInfo::overridePipelinerPolicy. +struct MachinePipelinerPolicy { + /// Limit the register pressure of the scheduled loop, retrying at a higher + /// II when a schedule needs too many registers. + bool ShouldLimitRegPressure = false; +}; + /// The main class in the implementation of the target independent /// software pipeliner pass. class LLVM_ABI MachinePipeliner : public MachineFunctionPass { @@ -449,9 +466,6 @@ class LLVM_ABI SwingSchedulerDAG : public ScheduleDAGInstrs { const SwingSchedulerDDG *getDDG() const { return DDG.get(); } - bool mayOverlapInLaterIter(const MachineInstr *BaseMI, - const MachineInstr *OtherMI) const; - private: LoopCarriedEdges addLoopCarriedDependences(); void updatePhiDependences(); @@ -471,7 +485,6 @@ class LLVM_ABI SwingSchedulerDAG : public ScheduleDAGInstrs { void computeNodeOrder(NodeSetType &NodeSets); void checkValidNodeOrder(const NodeSetType &Circuits) const; bool schedulePipeline(SMSchedule &Schedule); - bool computeDelta(const MachineInstr &MI, int &Delta) const; MachineInstr *findDefInLoop(Register Reg); bool canUseLastOffsetValue(MachineInstr *MI, unsigned &BasePos, unsigned &OffsetPos, Register &NewBase, diff --git a/llvm/include/llvm/CodeGen/WindowScheduler.h b/llvm/include/llvm/CodeGen/WindowScheduler.h index 12b3474dfa784a..2a89b8aa0ccb8a 100644 --- a/llvm/include/llvm/CodeGen/WindowScheduler.h +++ b/llvm/include/llvm/CodeGen/WindowScheduler.h @@ -42,6 +42,7 @@ #ifndef LLVM_CODEGEN_WINDOWSCHEDULER_H #define LLVM_CODEGEN_WINDOWSCHEDULER_H +#include "llvm/ADT/DenseSet.h" #include "llvm/CodeGen/MachineLoopInfo.h" #include "llvm/CodeGen/MachineRegisterInfo.h" #include "llvm/CodeGen/MachineScheduler.h" @@ -56,6 +57,13 @@ enum WindowSchedulingFlag { WS_Force /// Use window algorithm instead of SMS algorithm. }; +/// TriCloneInfo describes an MI clone in TripleMBB: the original MI it was +/// cloned from and the index of the copy (0, 1, or 2) it belongs to. +struct TriCloneInfo { + MachineInstr *Ori = nullptr; + unsigned Copy = 0; +}; + /// The main class in the implementation of the target independent window /// scheduler. class LLVM_ABI WindowScheduler { @@ -80,11 +88,20 @@ class LLVM_ABI WindowScheduler { SmallVector<MachineInstr *> OriMIs; /// TriMIs keeps the MIs of TripleMBB, which is used to restore TripleMBB. SmallVector<MachineInstr *> TriMIs; - /// TriToOri keeps the mappings between the MI clones in TripleMBB and their - /// original MI. - DenseMap<MachineInstr *, MachineInstr *> TriToOri; + /// TriToOri keeps the mappings between the MI clones in TripleMBB and the + /// information about their original MI and copy index. + DenseMap<MachineInstr *, TriCloneInfo> TriToOri; /// OriToCycle keeps the mappings between the original MI and its issue cycle. DenseMap<MachineInstr *, int> OriToCycle; + /// LoopCarriedMemDeps keeps ordered pairs (Pred, Succ) of original memory MIs + /// such that the iteration-N access Pred must be ordered before the + /// iteration-(N+1) access Succ. + DenseSet<std::pair<MachineInstr *, MachineInstr *>> LoopCarriedMemDeps; + /// MemDepEdgeFailed is set when enforcing a loop-carried memory dependence as + /// an artificial DAG edge would have created a cycle, so the edge could not + /// be added. In that case the current window must be abandoned, as its + /// schedule would not honor the dependence. + bool MemDepEdgeFailed = false; /// SchedResult keeps the result of each list scheduling, and the format of /// the tuple is <MI pointer, Cycle, Stage, Order ID>. SmallVector<std::tuple<MachineInstr *, int, int, int>, 256> SchedResult; @@ -118,6 +135,11 @@ class LLVM_ABI WindowScheduler { virtual bool initialize(); /// Add some related processing before running window scheduling. virtual void preProcess(); + /// Detect loop-carried memory dependences in the original MBB that must be + /// preserved across loop iterations. This must run on the live MBB before + /// backupMBB(), as it analyzes the induction recurrence of memory accesses. + /// The hazardous pairs of original MIs are recorded in LoopCarriedMemDeps. + void collectLoopCarriedMemDeps(); /// Add some related processing after running window scheduling. virtual void postProcess(); /// Back up the MIs in the original MBB and remove them from MBB. diff --git a/llvm/lib/CodeGen/MachinePipeliner.cpp b/llvm/lib/CodeGen/MachinePipeliner.cpp index bfd4fa0c589afd..43348c944aa806 100644 --- a/llvm/lib/CodeGen/MachinePipeliner.cpp +++ b/llvm/lib/CodeGen/MachinePipeliner.cpp @@ -325,12 +325,10 @@ class LoopCarriedOrderDepsTracker { std::vector<TaggedSUnit> TaggedSUnits; const TargetInstrInfo *TII = nullptr; - const TargetRegisterInfo *TRI = nullptr; public: LoopCarriedOrderDepsTracker(SwingSchedulerDAG *SSD, BatchAAResults *BAA, - const TargetInstrInfo *TII, - const TargetRegisterInfo *TRI); + const TargetInstrInfo *TII); /// The main function to compute loop-carried order-dependencies. void computeDependencies(); @@ -961,7 +959,7 @@ void SwingSchedulerDAG::finishBlock() { /// Return the register values for the operands of a Phi instruction. /// This function assume the instruction is a Phi. -static void getPhiRegs(MachineInstr &Phi, MachineBasicBlock *Loop, +static void getPhiRegs(const MachineInstr &Phi, const MachineBasicBlock *Loop, Register &InitVal, Register &LoopVal) { assert(Phi.isPHI() && "Expecting a Phi."); @@ -1055,9 +1053,7 @@ bool SUnitWithMemInfo::getUnderlyingObjects() { static bool hasLoopCarriedMemDep(const SUnitWithMemInfo &Src, const SUnitWithMemInfo &Dst, BatchAAResults &BAA, - const TargetInstrInfo *TII, - const TargetRegisterInfo *TRI, - const SwingSchedulerDAG *SSD) { + const TargetInstrInfo *TII) { if (Src.isTriviallyDisjoint(Dst)) return false; if (isSuccOrder(Src.SU, Dst.SU)) @@ -1066,7 +1062,9 @@ static bool hasLoopCarriedMemDep(const SUnitWithMemInfo &Src, MachineInstr &SrcMI = *Src.SU->getInstr(); MachineInstr &DstMI = *Dst.SU->getInstr(); - if (!SSD->mayOverlapInLaterIter(&SrcMI, &DstMI)) + const MachineBasicBlock *LoopBB = SrcMI.getParent(); + const MachineRegisterInfo &MRI = LoopBB->getParent()->getRegInfo(); + if (!mayOverlapInLaterIter(&SrcMI, &DstMI, LoopBB, MRI, *TII)) return false; // Second, the more expensive check that uses alias analysis on the @@ -1107,10 +1105,9 @@ void LoopCarriedOrderDepsTracker::NoBarrierInstsChunk::append(SUnit *SU) { } LoopCarriedOrderDepsTracker::LoopCarriedOrderDepsTracker( - SwingSchedulerDAG *SSD, BatchAAResults *BAA, const TargetInstrInfo *TII, - const TargetRegisterInfo *TRI) + SwingSchedulerDAG *SSD, BatchAAResults *BAA, const TargetInstrInfo *TII) : DAG(SSD), BAA(BAA), SUnits(DAG->SUnits), N(SUnits.size()), - LoopCarried(N, BitVector(N)), TII(TII), TRI(TRI) {} + LoopCarried(N, BitVector(N)), TII(TII) {} void LoopCarriedOrderDepsTracker::computeDependencies() { // Traverse all instructions and extract only what we are targetting. @@ -1148,7 +1145,7 @@ void LoopCarriedOrderDepsTracker::addDependenciesBetweenSUs( if (Src.SU == Dst.SU) return; - if (hasLoopCarriedMemDep(Src, Dst, *BAA, TII, TRI, DAG)) + if (hasLoopCarriedMemDep(Src, Dst, *BAA, TII)) setLoopCarriedDep(Src.SU, Dst.SU); } @@ -1257,7 +1254,7 @@ LoopCarriedEdges SwingSchedulerDAG::addLoopCarriedDependences() { LoopCarriedEdges LCE; // Add loop-carried order-dependencies - LoopCarriedOrderDepsTracker LCODTracker(this, &BAA, TII, TRI); + LoopCarriedOrderDepsTracker LCODTracker(this, &BAA, TII); LCODTracker.computeDependencies(); for (unsigned I = 0; I != SUnits.size(); I++) for (const int Succ : LCODTracker.getLoopCarried(I).set_bits()) @@ -2953,7 +2950,7 @@ static bool findLoopIncrementValue(const MachineOperand &Op, int &Value) { if (!CurReg.isValid() || !CurReg.isVirtual()) return false; MachineInstr *Def = MRI.getVRegDef(CurReg); - if (Def->getParent() != LoopBB) + if (!Def || Def->getParent() != LoopBB) return false; if (Def->isCopy()) { @@ -3003,12 +3000,13 @@ static bool findLoopIncrementValue(const MachineOperand &Op, int &Value) { /// Return true if we can compute the amount the instruction changes /// during each iteration. Set Delta to the amount of the change. -bool SwingSchedulerDAG::computeDelta(const MachineInstr &MI, int &Delta) const { - const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); +static bool computeDelta(const MachineInstr &MI, const TargetInstrInfo &TII, + int &Delta) { + const TargetRegisterInfo *TRI = MI.getMF()->getSubtarget().getRegisterInfo(); const MachineOperand *BaseOp; int64_t Offset; bool OffsetIsScalable; - if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI)) + if (!TII.getMemOperandWithOffset(MI, BaseOp, Offset, OffsetIsScalable, TRI)) return false; // FIXME: This algorithm assumes instructions have fixed-size offsets. @@ -3138,22 +3136,26 @@ MachineInstr *SwingSchedulerDAG::findDefInLoop(Register Reg) { /// Return false if there is no overlap between the region accessed by BaseMI in /// an iteration and the region accessed by OtherMI in subsequent iterations. -bool SwingSchedulerDAG::mayOverlapInLaterIter( - const MachineInstr *BaseMI, const MachineInstr *OtherMI) const { +bool llvm::mayOverlapInLaterIter(const MachineInstr *BaseMI, + const MachineInstr *OtherMI, + const MachineBasicBlock *LoopBB, + const MachineRegisterInfo &MRI, + const TargetInstrInfo &TII) { int DeltaB, DeltaO, Delta; - if (!computeDelta(*BaseMI, DeltaB) || !computeDelta(*OtherMI, DeltaO) || - DeltaB != DeltaO) + if (!computeDelta(*BaseMI, TII, DeltaB) || + !computeDelta(*OtherMI, TII, DeltaO) || DeltaB != DeltaO) return true; Delta = DeltaB; const MachineOperand *BaseOpB, *BaseOpO; int64_t OffsetB, OffsetO; bool OffsetBIsScalable, OffsetOIsScalable; - const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo(); - if (!TII->getMemOperandWithOffset(*BaseMI, BaseOpB, OffsetB, - OffsetBIsScalable, TRI) || - !TII->getMemOperandWithOffset(*OtherMI, BaseOpO, OffsetO, - OffsetOIsScalable, TRI)) + const TargetRegisterInfo *TRI = + BaseMI->getMF()->getSubtarget().getRegisterInfo(); + if (!TII.getMemOperandWithOffset(*BaseMI, BaseOpB, OffsetB, OffsetBIsScalable, + TRI) || + !TII.getMemOperandWithOffset(*OtherMI, BaseOpO, OffsetO, + OffsetOIsScalable, TRI)) return true; if (OffsetBIsScalable || OffsetOIsScalable) @@ -3169,24 +3171,24 @@ bool SwingSchedulerDAG::mayOverlapInLaterIter( if (!RegB.isVirtual() || !RegO.isVirtual()) return true; - MachineInstr *DefB = MRI.getVRegDef(BaseOpB->getReg()); - MachineInstr *DefO = MRI.getVRegDef(BaseOpO->getReg()); + MachineInstr *DefB = MRI.getVRegDef(RegB); + MachineInstr *DefO = MRI.getVRegDef(RegO); if (!DefB || !DefO || !DefB->isPHI() || !DefO->isPHI()) return true; - Register InitValB; - Register LoopValB; - Register InitValO; - Register LoopValO; - getPhiRegs(*DefB, BB, InitValB, LoopValB); - getPhiRegs(*DefO, BB, InitValO, LoopValO); + Register InitValB, LoopValB, InitValO, LoopValO; + getPhiRegs(*DefB, LoopBB, InitValB, LoopValB); + getPhiRegs(*DefO, LoopBB, InitValO, LoopValO); + if (!InitValB.isValid() || !InitValO.isValid()) + return true; MachineInstr *InitDefB = MRI.getVRegDef(InitValB); MachineInstr *InitDefO = MRI.getVRegDef(InitValO); - - if (!InitDefB->isIdenticalTo(*InitDefO)) + if (!InitDefB || !InitDefO || !InitDefB->isIdenticalTo(*InitDefO)) return true; } + if (BaseMI->memoperands_empty() || OtherMI->memoperands_empty()) + return true; LocationSize AccessSizeB = (*BaseMI->memoperands_begin())->getSize(); LocationSize AccessSizeO = (*OtherMI->memoperands_begin())->getSize(); diff --git a/llvm/lib/CodeGen/WindowScheduler.cpp b/llvm/lib/CodeGen/WindowScheduler.cpp index 3849a2c2caddf1..cb4f4dfa9295b4 100644 --- a/llvm/lib/CodeGen/WindowScheduler.cpp +++ b/llvm/lib/CodeGen/WindowScheduler.cpp @@ -37,10 +37,14 @@ //===----------------------------------------------------------------------===// #include "llvm/CodeGen/WindowScheduler.h" #include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Analysis/MemoryLocation.h" +#include "llvm/Analysis/ValueTracking.h" #include "llvm/CodeGen/LiveIntervals.h" #include "llvm/CodeGen/MachineLoopInfo.h" #include "llvm/CodeGen/MachinePipeliner.h" #include "llvm/CodeGen/ModuloSchedule.h" +#include "llvm/CodeGen/ScheduleDAGMutation.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Debug.h" @@ -101,6 +105,136 @@ static cl::opt<unsigned> cl::desc("The upper limit of II in the window algorithm."), cl::Hidden, cl::init(1000)); +/// Collect the underlying objects and alias metadata for the single memory +/// operand of MI. Returns false when MI does not have exactly one memory +/// operand with a known IR value. +static bool getMemInfo(const MachineInstr *MI, const Value *&MemOpValue, + int64_t &MemOpOffset, + SmallVectorImpl<const Value *> &UnderlyingObjs, + AAMDNodes &AATags, bool &IsAllIdentified) { + if (!MI->hasOneMemOperand()) + return false; + MachineMemOperand *MM = *MI->memoperands_begin(); + if (!MM->getValue()) + return false; + MemOpValue = MM->getValue(); + MemOpOffset = MM->getOffset(); + getUnderlyingObjects(MemOpValue, UnderlyingObjs); + AATags = MM->getAAInfo(); + IsAllIdentified = llvm::all_of(UnderlyingObjs, isIdentifiedObject); + return true; +} + +/// Return true if there is a loop-carried order dependency from memory access +/// SrcMI to DstMI in loop LoopBB. +static bool hasLoopCarriedMemDep(const MachineInstr *SrcMI, + const MachineInstr *DstMI, BatchAAResults &BAA, + const MachineBasicBlock *LoopBB, + const MachineRegisterInfo &MRI, + const TargetInstrInfo *TII) { + const Value *SrcVal = nullptr, *DstVal = nullptr; + int64_t SrcOffset = 0, DstOffset = 0; + SmallVector<const Value *, 4> SrcObjs, DstObjs; + AAMDNodes SrcAATags, DstAATags; + bool SrcAllIdentified = false, DstAllIdentified = false; + bool SrcKnown = getMemInfo(SrcMI, SrcVal, SrcOffset, SrcObjs, SrcAATags, + SrcAllIdentified); + bool DstKnown = getMemInfo(DstMI, DstVal, DstOffset, DstObjs, DstAATags, + DstAllIdentified); + + // If all underlying objects are identified and disjoint, the accesses cannot + // alias. + if (SrcKnown && DstKnown && SrcAllIdentified && DstAllIdentified && + llvm::none_of(SrcObjs, [&](const Value *Obj) { + return llvm::is_contained(DstObjs, Obj); + })) + return false; + + // The overlap helper inspects only the first memory operand. Multi-MMO + // instructions could carry an overlap through a later operand, so be + // conservative and keep the dependency for them. + if (!SrcMI->hasOneMemOperand() || !DstMI->hasOneMemOperand()) + return true; + + if (!mayOverlapInLaterIter(SrcMI, DstMI, LoopBB, MRI, *TII)) + return false; + + // The more expensive check that uses alias analysis on the base values. + if (!SrcKnown || !DstKnown) + return true; + if (SrcVal == DstVal && SrcOffset <= DstOffset) + return true; + + if (BAA.isNoAlias(MemoryLocation::getBeforeOrAfter(SrcVal, SrcAATags), + MemoryLocation::getBeforeOrAfter(DstVal, DstAATags))) + return false; + + // AliasAnalysis sometimes gives up on following the underlying object. In + // such a case, separate checks for underlying objects may prove that there + // are no aliases between two accesses. + for (const Value *SrcObj : SrcObjs) + for (const Value *DstObj : DstObjs) + if (!BAA.isNoAlias(MemoryLocation::getBeforeOrAfter(SrcObj, SrcAATags), + MemoryLocation::getBeforeOrAfter(DstObj, DstAATags))) + return true; + + return false; +} + +namespace { +/// A DAG mutation that enforces the loop-carried memory dependences discovered +/// on the original loop while list scheduling each window of TripleMBB. +/// +/// For each recorded hazard pair (Pred, Succ) we add an artificial order edge +/// between the clone of Pred and the clone of Succ belonging to a later copy, +/// so that the iteration-N Pred is kept before the iteration-(N+1) Succ. +class WindowMemDepMutation : public ScheduleDAGMutation { + const DenseSet<std::pair<MachineInstr *, MachineInstr *>> &LoopCarriedMemDeps; + const DenseMap<MachineInstr *, TriCloneInfo> &TriToOri; + bool &EdgeFailed; + +public: + WindowMemDepMutation( + const DenseSet<std::pair<MachineInstr *, MachineInstr *>> &Deps, + const DenseMap<MachineInstr *, TriCloneInfo> &TriToOri, bool &EdgeFailed) + : LoopCarriedMemDeps(Deps), TriToOri(TriToOri), EdgeFailed(EdgeFailed) {} + + void apply(ScheduleDAGInstrs *DAG) override { + if (LoopCarriedMemDeps.empty()) + return; + for (SUnit &PredSU : DAG->SUnits) { + MachineInstr *PredMI = PredSU.getInstr(); + if (!PredMI || !PredMI->mayLoadOrStore()) + continue; + auto PredIt = TriToOri.find(PredMI); + if (PredIt == TriToOri.end()) + continue; + for (SUnit &SuccSU : DAG->SUnits) { + MachineInstr *SuccMI = SuccSU.getInstr(); + if (&PredSU == &SuccSU || !SuccMI || !SuccMI->mayLoadOrStore()) + continue; + auto SuccIt = TriToOri.find(SuccMI); + if (SuccIt == TriToOri.end()) + continue; + // Only order a Pred from an earlier copy before a Succ from a later + // copy, which models the iteration-N to iteration-(N+1) dependence. + if (PredIt->second.Copy >= SuccIt->second.Copy) + continue; + if (!LoopCarriedMemDeps.contains( + {PredIt->second.Ori, SuccIt->second.Ori})) + continue; + // If the edge would create a cycle, addEdge() returns false and does + // not insert it. The required dependence then cannot be enforced in + // this window, so mark it to make the caller abandon this window's + // schedule. + if (!DAG->addEdge(&SuccSU, SDep(&PredSU, SDep::Barrier))) + EdgeFailed = true; + } + } + } +}; +} // end anonymous namespace + WindowScheduler::WindowScheduler(MachineSchedContext *C, MachineLoop &ML) : Context(C), MF(C->MF), MBB(ML.getHeader()), Loop(ML), Subtarget(&MF->getSubtarget()), TII(Subtar... [truncated] `````````` </details> https://github.com/llvm/llvm-project/pull/243336 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
