[llvm] AMDGPU: Resolve some next-use analysis #178873 post-merge feedback (PR #192960)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 12:47:39 PDT 2026


https://github.com/macurtis-amd updated https://github.com/llvm/llvm-project/pull/192960

>From 6fcd574be5cae873c3f3fee102958c1bfc6a7569 Mon Sep 17 00:00:00 2001
From: Matthew Curtis <macurtis at amd.com>
Date: Mon, 20 Apr 2026 06:46:21 -0500
Subject: [PATCH 1/2] AMDGPU: Resolve some next-use analysis #178873 post-merge
 feedback

https://github.com/llvm/llvm-project/pull/178873#discussion_r3095062168
> PassPass

https://github.com/llvm/llvm-project/pull/178873#discussion_r3095074124
> This probably should be isTransient

https://github.com/llvm/llvm-project/pull/178873#discussion_r3095074912
> No auto

https://github.com/llvm/llvm-project/pull/178873#discussion_r3095092269
> switch so new entries get a warning

https://github.com/llvm/llvm-project/pull/178873#discussion_r3095113032
> With operator< implemented std::min should work
---
 llvm/lib/Target/AMDGPU/AMDGPU.h               |  4 +--
 .../Target/AMDGPU/AMDGPUNextUseAnalysis.cpp   | 35 +++++++++++++------
 .../lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h |  8 -----
 .../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp |  4 +--
 4 files changed, 28 insertions(+), 23 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPU.h b/llvm/lib/Target/AMDGPU/AMDGPU.h
index 5c7259b7600a9..adea4d6fc69f2 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPU.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPU.h
@@ -195,10 +195,10 @@ extern char &SIFixSGPRCopiesLegacyID;
 void initializeSIFixVGPRCopiesLegacyPass(PassRegistry &);
 extern char &SIFixVGPRCopiesID;
 
-void initializeAMDGPUNextUseAnalysisLegacyPassPass(PassRegistry &);
+void initializeAMDGPUNextUseAnalysisLegacyPass(PassRegistry &);
 extern char &AMDGPUNextUseAnalysisLegacyID;
 
-void initializeAMDGPUNextUseAnalysisPrinterLegacyPassPass(PassRegistry &);
+void initializeAMDGPUNextUseAnalysisPrinterLegacyPass(PassRegistry &);
 extern char &AMDGPUNextUseAnalysisPrinterLegacyID;
 
 void initializeSILowerWWMCopiesLegacyPass(PassRegistry &);
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp
index f6fe48bf5bb49..4dda02c9bcbf9 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp
@@ -304,14 +304,17 @@ class llvm::AMDGPUNextUseAnalysisImpl {
   unsigned sizeOf(const MachineInstr &MI) const {
     // When !Cfg.CountPhis, PHIs do not contribute to distances/sizes since they
     // generally don't result in the generation of a machine instruction.
-    // FIXME: Consider using MI.isPseudo() or maybe MI.isMetaInstruction().
+
+    // FIXME: Consider using MI.isPseudo(), MI.isMetaInstruction(), or maybe
+    //        MI.isTransient(). Any such change will likely require fixing up
+    //        places in the code where we assume that only PHIs can have 0 size.
     return Cfg.CountPhis ? 1 : !MI.isPHI();
   }
 
   void calcInstrIds(const MachineBasicBlock *BB,
                     InstrToIdMap &MutableInstrToId) const {
     InstrIdTy Id = 0;
-    for (auto &MI : BB->instrs()) {
+    for (const MachineInstr &MI : BB->instrs()) {
       MutableInstrToId[&MI] = Id;
       Id += sizeOf(MI);
     }
@@ -402,11 +405,15 @@ class llvm::AMDGPUNextUseAnalysisImpl {
 
   enum class EdgeKind { Back = -1, None = 0, Forward = 1 };
   static constexpr StringRef toString(EdgeKind EK) {
-    if (EK == EdgeKind::Back)
+    switch (EK) {
+    case EdgeKind::Back:
       return "back";
-    if (EK == EdgeKind::Forward)
+    case EdgeKind::Forward:
       return "fwd";
-    return "none";
+    case EdgeKind::None:
+      return "none";
+    }
+    llvm_unreachable("Unexpected EdgeKind");
   }
 
   struct PathInfo {
@@ -878,8 +885,8 @@ class llvm::AMDGPUNextUseAnalysisImpl {
                 Curr.try_emplace(DestBlockNum, WeightedDist, UnweightedDist);
             if (!First) {
               InterBlockDistance &Slot = I->second;
-              Slot.Weighted = min(Slot.Weighted, WeightedDist);
-              Slot.Unweighted = min(Slot.Unweighted, UnweightedDist);
+              Slot.Weighted = std::min(Slot.Weighted, WeightedDist);
+              Slot.Unweighted = std::min(Slot.Unweighted, UnweightedDist);
             }
           }
         }
@@ -2119,6 +2126,9 @@ AMDGPUNextUseAnalysisImpl::AMDGPUNextUseAnalysisImpl(
   TRI = &TII->getRegisterInfo();
   MRI = &MF->getRegInfo();
 
+  assert(MRI->isSSA() && "amdgpu-next-use-analysis is only supported for "
+                         "machine functions in SSA form.");
+
   // FIXME: Hopefully we will soon converge on a single way of calculating
   // next-use distance and remove these presets.
   if (ConfigPresetOpt == "compute")
@@ -2296,10 +2306,11 @@ void AMDGPUNextUseAnalysisLegacyPass::getAnalysisUsage(
 char AMDGPUNextUseAnalysisLegacyPass::ID = 0;
 char &llvm::AMDGPUNextUseAnalysisLegacyID = AMDGPUNextUseAnalysisLegacyPass::ID;
 
-INITIALIZE_PASS_BEGIN(AMDGPUNextUseAnalysisLegacyPass, DEBUG_TYPE,
+using AMDGPUNextUseAnalysisLegacy = AMDGPUNextUseAnalysisLegacyPass;
+INITIALIZE_PASS_BEGIN(AMDGPUNextUseAnalysisLegacy, DEBUG_TYPE,
                       "Next Use Analysis", false, true)
 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
-INITIALIZE_PASS_END(AMDGPUNextUseAnalysisLegacyPass, DEBUG_TYPE,
+INITIALIZE_PASS_END(AMDGPUNextUseAnalysisLegacy, DEBUG_TYPE,
                     "Next Use Analysis", false, true)
 
 FunctionPass *llvm::createAMDGPUNextUseAnalysisLegacyPass() {
@@ -2577,14 +2588,16 @@ char AMDGPUNextUseAnalysisPrinterLegacyPass::ID = 0;
 char &AMDGPUNextUseAnalysisPrinterLegacyID =
     AMDGPUNextUseAnalysisPrinterLegacyPass::ID;
 
-INITIALIZE_PASS_BEGIN(AMDGPUNextUseAnalysisPrinterLegacyPass,
+using AMDGPUNextUseAnalysisPrinterLegacy =
+    AMDGPUNextUseAnalysisPrinterLegacyPass;
+INITIALIZE_PASS_BEGIN(AMDGPUNextUseAnalysisPrinterLegacy,
                       "amdgpu-next-use-printer",
                       "AMDGPU Next Use Analysis Printer", false, false)
 
 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
 
-INITIALIZE_PASS_END(AMDGPUNextUseAnalysisPrinterLegacyPass,
+INITIALIZE_PASS_END(AMDGPUNextUseAnalysisPrinterLegacy,
                     "amdgpu-next-use-printer",
                     "AMDGPU Next Use Analysis Printer", false, false)
 
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h
index 90cbb08c93f47..d993852c10e4f 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h
@@ -215,14 +215,6 @@ constexpr inline NextUseDistance operator-(NextUseDistance A,
   return A -= B;
 }
 
-constexpr inline NextUseDistance min(NextUseDistance A, NextUseDistance B) {
-  return A < B ? A : B;
-}
-
-constexpr inline NextUseDistance max(NextUseDistance A, NextUseDistance B) {
-  return A > B ? A : B;
-}
-
 //==============================================================================
 // AMDGPUNextUseAnalysis - Provides next-use distances for live registers or
 // sub-registers at a given MachineInstruction suitable for making spilling
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index d7e2264e51fd6..cb23a3090b03c 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -678,8 +678,8 @@ extern "C" LLVM_ABI LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTarget() {
   initializeSIShrinkInstructionsLegacyPass(*PR);
   initializeSIOptimizeExecMaskingPreRALegacyPass(*PR);
   initializeSIOptimizeVGPRLiveRangeLegacyPass(*PR);
-  initializeAMDGPUNextUseAnalysisLegacyPassPass(*PR);
-  initializeAMDGPUNextUseAnalysisPrinterLegacyPassPass(*PR);
+  initializeAMDGPUNextUseAnalysisLegacyPass(*PR);
+  initializeAMDGPUNextUseAnalysisPrinterLegacyPass(*PR);
   initializeSILoadStoreOptimizerLegacyPass(*PR);
   initializeAMDGPUCtorDtorLoweringLegacyPass(*PR);
   initializeAMDGPUAlwaysInlinePass(*PR);

>From 7946f42f872ea1a747fcc7bf92ded47077b99f15 Mon Sep 17 00:00:00 2001
From: Matthew Curtis <macurtis at amd.com>
Date: Tue, 28 Apr 2026 14:21:47 -0500
Subject: [PATCH 2/2] fixup! AMDGPU: Resolve some next-use analysis #178873
 post-merge feedback

---
 .../Target/AMDGPU/AMDGPUNextUseAnalysis.cpp   | 41 ++++++++++++++++---
 .../lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h |  9 ++++
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp
index 4dda02c9bcbf9..a76346ed53196 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.cpp
@@ -2085,6 +2085,10 @@ class llvm::AMDGPUNextUseAnalysisImpl {
   AMDGPUNextUseAnalysisImpl(const MachineFunction *, const MachineLoopInfo *);
   ~AMDGPUNextUseAnalysisImpl() { clearTables(); }
 
+  static MachineFunctionProperties getRequiredProperties() {
+    return MachineFunctionProperties().setIsSSA();
+  }
+
   AMDGPUNextUseAnalysis::Config getConfig() const { return Cfg; }
   void setConfig(AMDGPUNextUseAnalysis::Config NewCfg) {
     Cfg = NewCfg;
@@ -2126,9 +2130,6 @@ AMDGPUNextUseAnalysisImpl::AMDGPUNextUseAnalysisImpl(
   TRI = &TII->getRegisterInfo();
   MRI = &MF->getRegInfo();
 
-  assert(MRI->isSSA() && "amdgpu-next-use-analysis is only supported for "
-                         "machine functions in SSA form.");
-
   // FIXME: Hopefully we will soon converge on a single way of calculating
   // next-use distance and remove these presets.
   if (ConfigPresetOpt == "compute")
@@ -2284,12 +2285,22 @@ void AMDGPUNextUseAnalysis::getReachableUses(
 //------------------------------------------------------------------------------
 AMDGPUNextUseAnalysisLegacyPass::AMDGPUNextUseAnalysisLegacyPass()
     : MachineFunctionPass(ID) {}
+
+StringRef AMDGPUNextUseAnalysisLegacyPass::name() {
+  return "AMDGPU Next Use Analysis";
+}
+
 StringRef AMDGPUNextUseAnalysisLegacyPass::getPassName() const {
-  return "Next Use Analysis";
+  return name();
+}
+
+MachineFunctionProperties AMDGPUNextUseAnalysisLegacyPass::getRequiredProperties() const {
+  return AMDGPUNextUseAnalysisImpl::getRequiredProperties();
 }
 
 bool AMDGPUNextUseAnalysisLegacyPass::runOnMachineFunction(
     MachineFunction &MF) {
+  MFPropsModifier _(*this, MF);
   const MachineLoopInfo *MLI =
       &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
   NUA.reset(new AMDGPUNextUseAnalysis(&MF, MLI));
@@ -2322,9 +2333,14 @@ FunctionPass *llvm::createAMDGPUNextUseAnalysisLegacyPass() {
 //------------------------------------------------------------------------------
 AnalysisKey AMDGPUNextUseAnalysisPass::Key;
 
+MachineFunctionProperties AMDGPUNextUseAnalysisPass::getRequiredProperties() const {
+  return AMDGPUNextUseAnalysisImpl::getRequiredProperties();
+}
+
 AMDGPUNextUseAnalysisPass::Result
 AMDGPUNextUseAnalysisPass::run(MachineFunction &MF,
                                MachineFunctionAnalysisManager &MFAM) {
+  MFPropsModifier _(*this, MF);
   const MachineLoopInfo &MLI = MFAM.getResult<MachineLoopAnalysis>(MF);
   return AMDGPUNextUseAnalysis(&MF, &MLI);
 }
@@ -2555,12 +2571,21 @@ void printAsJson(raw_ostream &FallbackOS, TimerGroup &JsonTimerGroup,
 AMDGPUNextUseAnalysisPrinterLegacyPass::AMDGPUNextUseAnalysisPrinterLegacyPass()
     : MachineFunctionPass(ID) {}
 
-StringRef AMDGPUNextUseAnalysisPrinterLegacyPass::getPassName() const {
+StringRef AMDGPUNextUseAnalysisPrinterLegacyPass::name() {
   return "AMDGPU Next Use Analysis Printer";
 }
 
+StringRef AMDGPUNextUseAnalysisPrinterLegacyPass::getPassName() const {
+  return name();
+}
+
+MachineFunctionProperties AMDGPUNextUseAnalysisPrinterLegacyPass::getRequiredProperties() const {
+  return AMDGPUNextUseAnalysisImpl::getRequiredProperties();
+}
+
 bool AMDGPUNextUseAnalysisPrinterLegacyPass::runOnMachineFunction(
     MachineFunction &MF) {
+  MFPropsModifier _(*this, MF);
   TimerGroup JsonTimerGroup("amdgpu-next-use-analysis-json",
                             "AMDGPU Next Use Analysis JSON Printer", false);
   Timer JsonTimer("json", "Total time spent generating json", JsonTimerGroup);
@@ -2608,10 +2633,14 @@ FunctionPass *llvm::createAMDGPUNextUseAnalysisPrinterLegacyPass() {
 //------------------------------------------------------------------------------
 // New Pass Manager Printer Pass
 //------------------------------------------------------------------------------
+MachineFunctionProperties AMDGPUNextUseAnalysisPrinterPass::getRequiredProperties() const {
+  return AMDGPUNextUseAnalysisImpl::getRequiredProperties();
+}
+
 PreservedAnalyses
 AMDGPUNextUseAnalysisPrinterPass::run(MachineFunction &MF,
                                       MachineFunctionAnalysisManager &MFAM) {
-
+  MFPropsModifier _(*this, MF);
   TimerGroup JsonTimerGroup("amdgpu-next-use-analysis-json",
                             "AMDGPU Next Use Analysis JSON Printer", false);
   Timer JsonTimer("json", "Total time spent generating json", JsonTimerGroup);
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h
index d993852c10e4f..17315e7f738b6 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h
+++ b/llvm/lib/Target/AMDGPU/AMDGPUNextUseAnalysis.h
@@ -314,9 +314,12 @@ class AMDGPUNextUseAnalysisLegacyPass : public MachineFunctionPass {
   static char ID;
 
   AMDGPUNextUseAnalysisLegacyPass();
+  MachineFunctionProperties getRequiredProperties() const override;
 
   AMDGPUNextUseAnalysis &getNextUseAnalysis() { return *NUA; }
   const AMDGPUNextUseAnalysis &getNextUseAnalysis() const { return *NUA; }
+
+  static StringRef name();
   StringRef getPassName() const override;
 
 protected:
@@ -333,6 +336,8 @@ class AMDGPUNextUseAnalysisPass
   static AnalysisKey Key;
 
 public:
+  MachineFunctionProperties getRequiredProperties() const;
+
   using Result = AMDGPUNextUseAnalysis;
   Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM);
 };
@@ -347,7 +352,9 @@ class AMDGPUNextUseAnalysisPrinterLegacyPass : public MachineFunctionPass {
   static char ID;
 
   AMDGPUNextUseAnalysisPrinterLegacyPass();
+  MachineFunctionProperties getRequiredProperties() const override;
 
+  static StringRef name();
   StringRef getPassName() const override;
 
 protected:
@@ -360,6 +367,8 @@ class AMDGPUNextUseAnalysisPrinterPass
   raw_ostream &OS;
 
 public:
+  MachineFunctionProperties getRequiredProperties() const;
+
   explicit AMDGPUNextUseAnalysisPrinterPass(raw_ostream &OS) : OS(OS) {}
   PreservedAnalyses run(MachineFunction &MF,
                         MachineFunctionAnalysisManager &MFAM);



More information about the llvm-commits mailing list