[llvm] [AMDGPU] Break WAR Hazards Using Scavenged Registers to Assist Load Clustering (PR #216343)
Patrick Simmons via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 13:49:45 PDT 2026
================
@@ -0,0 +1,566 @@
+//===- GCNBreakLoadClusterDeps.cpp ----------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+/// \file
+/// Post-RA pass that breaks false (WAR/WAW) anti-dependencies on the address
+/// computation feeding clusterable memory loads, so that the downstream post-RA
+/// load-clustering scheduler can issue the loads as a burst and expose more
+/// memory-level parallelism.
+///
+/// Register allocation may pack the per-lane index/address computation of
+/// several adjacent loads into a small set of registers (e.g. funneling every
+/// extracted index through a single scratch VGPR, or reusing one address
+/// register pair across two loads). Those reuses are anti-dependencies: they
+/// serialize the address chains and pin the loads apart even though the loads
+/// are semantically independent. The post-RA MachineScheduler's load-cluster
+/// mutation cannot rename registers, so it cannot undo them.
+///
+/// This pass renames the reused registers on those address chains to free
+/// registers scavenged from the function, bounded by the VGPR budget of the
+/// current occupancy so it never spends registers that would drop the number of
+/// concurrent waves. It performs no rescheduling itself: once the false
+/// dependencies are gone the existing load-cluster scheduler does the reorder.
+//===----------------------------------------------------------------------===//
+
+#include "GCNBreakLoadClusterDeps.h"
+#include "AMDGPU.h"
+#include "GCNSubtarget.h"
+#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
+#include "SIMachineFunctionInfo.h"
+#include "SIRegisterInfo.h"
+#include "llvm/ADT/BitVector.h"
+#include "llvm/CodeGen/MachineBasicBlock.h"
+#include "llvm/CodeGen/MachineFunctionPass.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Pass.h"
+
+#include <algorithm>
+#include <tuple>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+using namespace llvm;
+
+#define DEBUG_TYPE "amdgpu-break-load-cluster-deps"
+
+// The lane sets only ever hold 32-bit VGPRs, so they are indexed by
+// (Reg - AMDGPU::VGPR0) into a BitVector sized to the number of 32-bit VGPRs,
+// rather than one spanning every target register (which would be tens of
+// thousands of bits).
+
+namespace {
+
+/// Target-independent-of-pass-manager implementation.
+class GCNBreakLoadClusterDepsImpl {
+ const GCNSubtarget *ST = nullptr;
+ const SIRegisterInfo *TRI = nullptr;
+ const SIInstrInfo *TII = nullptr;
+ MachineRegisterInfo *MRI = nullptr;
+ unsigned OccupancyBudget;
+ // Number of 32-bit VGPRs; the size of every lane-set BitVector below.
+ unsigned NumVGPR32 = 0;
+
+ // True if any lane set in \p A is not set in \p B (i.e. A \ B is non-empty).
+ static bool anyLanesOutside(const BitVector &A, const BitVector &B) {
+ BitVector Tmp = A;
+ Tmp.reset(B);
+ return Tmp.any();
+ }
+
+ BitVector getVGPR32Components(Register Reg) const;
+ std::pair<BitVector, BitVector> getUsesAndDefsFor(MachineInstr &MI) const;
+ Register promoteToSuperRegister(MachineInstr &MI, Register SubReg, bool Defs,
+ bool Uses);
+ Register renameRegister(Register FromReg, Register ToReg, Register RenameReg);
+ bool findReplaceRegisterOperand(MachineInstr &MI, unsigned OpNum,
+ const BitVector &BannedRegs,
+ bool MIMustBeKiller = false);
+ bool isVGPRLoad(MachineInstr &MI) const {
+ // Exclude image (MIMG/VIMAGE/VSAMPLE) loads. Their address operands are
+ // per-coordinate VGPRs, not the reused address chain this pass targets, and
+ // renaming them fights GCNNSAReassign, which deliberately picks the NSA
+ // address-register layout (renaming forces the larger NSA encoding).
+ return SIInstrInfo::isVMEM(MI) && !SIInstrInfo::isImage(MI) &&
+ MI.mayLoad() && MI.getOperand(0).isReg() &&
+ TRI->isVGPR(*MRI, MI.getOperand(0).getReg());
+ }
+
+public:
+ bool run(MachineFunction &MF);
+ bool runOnMachineBasicBlock(MachineBasicBlock &MBB);
+};
+
+/// Legacy-PM wrapper.
+class GCNBreakLoadClusterDepsLegacy : public MachineFunctionPass {
+public:
+ static char ID;
+
+ GCNBreakLoadClusterDepsLegacy() : MachineFunctionPass(ID) {}
+
+ bool runOnMachineFunction(MachineFunction &MF) override;
+
+ StringRef getPassName() const override {
+ return "AMDGPU Break Load Cluster Dependencies";
+ }
+
+ void getAnalysisUsage(AnalysisUsage &AU) const override {
+ AU.setPreservesCFG();
+ MachineFunctionPass::getAnalysisUsage(AU);
+ }
+};
+
+} // end anonymous namespace
+
+// Return the set of 32-bit VGPR lanes covered by physical VGPR `Reg` (any
+// width), indexed by (lane - AMDGPU::VGPR0).
+BitVector GCNBreakLoadClusterDepsImpl::getVGPR32Components(Register Reg) const {
+ BitVector ToReturn(NumVGPR32);
----------------
linuxrocks123 wrote:
I rewrote this function to be more concise. LiveRegUnits uses BitVectors internally, and those BitVectors would be much larger than mine, so I've left everything else as is.
https://github.com/llvm/llvm-project/pull/216343
More information about the llvm-commits
mailing list