================ @@ -0,0 +1,170 @@ +//===- SIWaitcntPadding.cpp - Counter padding hints for waitcnts ----------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Lower waitcnt padding hints to actual padding before waitcnt insertion +// analysis. +// +//===----------------------------------------------------------------------===// + +#include "SIWaitcntPadding.h" +#include "AMDGPUWaitcntUtils.h" +#include "GCNSubtarget.h" +#include "llvm/ADT/DenseSet.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/CodeGen/MachineDomTreeUpdater.h" +#include "llvm/CodeGen/MachineInstrBuilder.h" +#include "llvm/CodeGen/MachineLoopInfo.h" +#include "llvm/CodeGen/MachinePostDominators.h" +#include "llvm/Support/Debug.h" +#include <optional> + +using namespace llvm; + +#define DEBUG_TYPE "si-insert-waitcnts" + +namespace { + +struct PaddingEdge { + MachineBasicBlock *Branch; + MachineBasicBlock *Pred; + MachineBasicBlock *Join; +}; + +[[maybe_unused]] void printIgnored(const MachineBasicBlock &Arm, + StringRef Reason) { + dbgs() << "Ignoring PAD_WAITCNT in " << printMBBReference(Arm) << " of " + << Arm.getParent()->getName() << ": " << Reason << '\n'; +} + +std::optional<PaddingEdge> getCounterPaddingEdge(MachineBasicBlock &Arm, + MachineLoopInfo &MLI, + const SIInstrInfo &TII) { + auto Reject = [&Arm](StringRef Reason) -> std::optional<PaddingEdge> { + LLVM_DEBUG(printIgnored(Arm, Reason)); + return std::nullopt; + }; + + MachineBasicBlock *Branch = Arm.getSinglePredecessor(); + MachineBasicBlock *Join = Arm.getSingleSuccessor(); + if (!Branch || !Join) + return Reject("arm lacks a single predecessor and successor"); + if (Branch->succ_size() != 2 || Join->pred_size() != 2) + return Reject("branch or join does not have exactly two edges"); + if (Arm.isEHPad() || Join->isEHPad()) + return Reject("arm or join is an EH pad"); + + MachineBasicBlock *Other = nullptr; + for (MachineBasicBlock *Pred : Join->predecessors()) + if (Pred != &Arm) + Other = Pred; + if (!Other || (Other != Branch && (Other->getSinglePredecessor() != Branch || + Other->getSingleSuccessor() != Join))) + return Reject("not a triangle or diamond"); + + MachineLoop *Loop = MLI.getLoopFor(Join); + if (MLI.getLoopFor(Branch) != Loop || MLI.getLoopFor(&Arm) != Loop || + MLI.getLoopFor(Other) != Loop) + return Reject("branch, arms and join are not in the same loop"); + if (Loop && (Loop->getHeader() == Join || Loop->isLoopLatch(Join))) + return Reject("join is a loop header or latch"); + + MachineBasicBlock *TBB = nullptr, *FBB = nullptr; + SmallVector<MachineOperand, 4> Cond; + if (TII.analyzeBranch(*Branch, TBB, FBB, Cond) || Cond.empty()) + return Reject("branch is not an analyzable conditional branch"); + for (MachineBasicBlock *MBB : {&Arm, Other}) { + if (MBB == Branch) + continue; + for (const MachineInstr &MI : MBB->terminators()) + if (MI.getOpcode() != AMDGPU::S_BRANCH) + return Reject("arm has a terminator other than S_BRANCH"); + } + if (Other == Branch && !Branch->canSplitCriticalEdge(Join, &MLI)) + return Reject("branch-to-join edge cannot be split"); + return PaddingEdge{Branch, Other, Join}; +} + +} // end anonymous namespace + +AMDGPU::CounterPaddingResult AMDGPU::expandCounterPaddingHints( + MachineFunction &MF, MachineLoopInfo &MLI, MachineDominatorTree *MDT, + MachinePostDominatorTree &PDT, const HardwareLimits &Limits) { + const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); + const SIInstrInfo &TII = *ST.getInstrInfo(); + const SIRegisterInfo &TRI = TII.getRegisterInfo(); + + struct Request { + MachineBasicBlock *Arm; + PaddingEdge Edge; + unsigned Count; + DebugLoc DL; + }; + SmallVector<Request, 8> Requests; + DenseSet<MachineBasicBlock *> SeenBranches, RepeatedBranches; ---------------- danzimm wrote:
Disallowing repeated pad instructions is artificial. After going through test cases again I think we should allow repeated pad hints, adding them together (working on that today) https://github.com/llvm/llvm-project/pull/243204 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
