llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[CodeGen][NPM] Add support for -print-regusage in New Pass Manager

Open TejaX-Alaghari opened this issue 1 month ago • 8 comments

Legacy pass manager uses doFinalization() method called at the end of module processing to print register usage information when -print-regusage flag is passed.

This patch attempts to add an alternative mechanism to support this functionality in the NPM.

TejaX-Alaghari avatar Nov 27 '25 05:11 TejaX-Alaghari

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

github-actions[bot] avatar Nov 27 '25 05:11 github-actions[bot]

@llvm/pr-subscribers-llvm-regalloc

Author: Teja Alaghari (TejaX-Alaghari)

Changes

Legacy pass manager uses doFinalization() method called at the end of module processing to print register usage information when -print-regusage flag is passed.

This patch attempts to add an alternative mechanism to support this functionality in the NPM.


Full diff: https://github.com/llvm/llvm-project/pull/169761.diff

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/RegisterUsageInfo.h (+1)
  • (modified) llvm/lib/CodeGen/RegisterUsageInfo.cpp (+9)
  • (modified) llvm/test/CodeGen/AMDGPU/ipra-regmask.ll (+1)
diff --git a/llvm/include/llvm/CodeGen/RegisterUsageInfo.h b/llvm/include/llvm/CodeGen/RegisterUsageInfo.h
index 9b0d30426f1d3..04acaf380405e 100644
--- a/llvm/include/llvm/CodeGen/RegisterUsageInfo.h
+++ b/llvm/include/llvm/CodeGen/RegisterUsageInfo.h
@@ -34,6 +34,7 @@ class TargetMachine;
 
 class PhysicalRegisterUsageInfo {
 public:
+  ~PhysicalRegisterUsageInfo();
   /// Set TargetMachine which is used to print analysis.
   void setTargetMachine(const TargetMachine &TM);
 
diff --git a/llvm/lib/CodeGen/RegisterUsageInfo.cpp b/llvm/lib/CodeGen/RegisterUsageInfo.cpp
index 2ef380fc7cad4..48466381f4d3e 100644
--- a/llvm/lib/CodeGen/RegisterUsageInfo.cpp
+++ b/llvm/lib/CodeGen/RegisterUsageInfo.cpp
@@ -56,6 +56,15 @@ bool PhysicalRegisterUsageInfo::doFinalization(Module &M) {
   return false;
 }
 
+PhysicalRegisterUsageInfo::~PhysicalRegisterUsageInfo() {
+  // As doFinalization() is not called for analysis results in the new PM,
+  // we print the register usage information here.
+  if (DumpRegUsage && TM)
+    print(errs());
+
+  RegMasks.shrink_and_clear();
+}
+
 void PhysicalRegisterUsageInfo::storeUpdateRegUsageInfo(
     const Function &FP, ArrayRef<uint32_t> RegMask) {
   RegMasks[&FP] = RegMask;
diff --git a/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll b/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll
index dc4bf21ab1269..052f1dd1e1c92 100644
--- a/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll
+++ b/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -enable-ipra -print-regusage -filetype=null 2>&1 < %s | FileCheck %s
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -enable-new-pm=1 -enable-ipra -print-regusage -filetype=null 2>&1 < %s | FileCheck %s
 
 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -stop-after=prologepilog -o - %s \
 ; RUN:   | llc -x=mir -mtriple=amdgcn-amd-amdhsa -passes="module(require<reg-usage>,function(machine-function(reg-usage-collector)),print<reg-usage>)" -filetype=null 2>&1 \

llvmbot avatar Dec 03 '25 06:12 llvmbot

@llvm/pr-subscribers-backend-amdgpu

Author: Teja Alaghari (TejaX-Alaghari)

Changes

Legacy pass manager uses doFinalization() method called at the end of module processing to print register usage information when -print-regusage flag is passed.

This patch attempts to add an alternative mechanism to support this functionality in the NPM.


Full diff: https://github.com/llvm/llvm-project/pull/169761.diff

3 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/RegisterUsageInfo.h (+1)
  • (modified) llvm/lib/CodeGen/RegisterUsageInfo.cpp (+9)
  • (modified) llvm/test/CodeGen/AMDGPU/ipra-regmask.ll (+1)
diff --git a/llvm/include/llvm/CodeGen/RegisterUsageInfo.h b/llvm/include/llvm/CodeGen/RegisterUsageInfo.h
index 9b0d30426f1d3..04acaf380405e 100644
--- a/llvm/include/llvm/CodeGen/RegisterUsageInfo.h
+++ b/llvm/include/llvm/CodeGen/RegisterUsageInfo.h
@@ -34,6 +34,7 @@ class TargetMachine;
 
 class PhysicalRegisterUsageInfo {
 public:
+  ~PhysicalRegisterUsageInfo();
   /// Set TargetMachine which is used to print analysis.
   void setTargetMachine(const TargetMachine &TM);
 
diff --git a/llvm/lib/CodeGen/RegisterUsageInfo.cpp b/llvm/lib/CodeGen/RegisterUsageInfo.cpp
index 2ef380fc7cad4..48466381f4d3e 100644
--- a/llvm/lib/CodeGen/RegisterUsageInfo.cpp
+++ b/llvm/lib/CodeGen/RegisterUsageInfo.cpp
@@ -56,6 +56,15 @@ bool PhysicalRegisterUsageInfo::doFinalization(Module &M) {
   return false;
 }
 
+PhysicalRegisterUsageInfo::~PhysicalRegisterUsageInfo() {
+  // As doFinalization() is not called for analysis results in the new PM,
+  // we print the register usage information here.
+  if (DumpRegUsage && TM)
+    print(errs());
+
+  RegMasks.shrink_and_clear();
+}
+
 void PhysicalRegisterUsageInfo::storeUpdateRegUsageInfo(
     const Function &FP, ArrayRef<uint32_t> RegMask) {
   RegMasks[&FP] = RegMask;
diff --git a/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll b/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll
index dc4bf21ab1269..052f1dd1e1c92 100644
--- a/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll
+++ b/llvm/test/CodeGen/AMDGPU/ipra-regmask.ll
@@ -1,5 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -enable-ipra -print-regusage -filetype=null 2>&1 < %s | FileCheck %s
+; RUN: llc -mtriple=amdgcn-amd-amdhsa -enable-new-pm=1 -enable-ipra -print-regusage -filetype=null 2>&1 < %s | FileCheck %s
 
 ; RUN: llc -mtriple=amdgcn-amd-amdhsa -stop-after=prologepilog -o - %s \
 ; RUN:   | llc -x=mir -mtriple=amdgcn-amd-amdhsa -passes="module(require<reg-usage>,function(machine-function(reg-usage-collector)),print<reg-usage>)" -filetype=null 2>&1 \

llvmbot avatar Dec 03 '25 06:12 llvmbot

Little concerned about how destructors are invoked (and possibly duplicate printing), would this be the right place to implement printing @aeubanks ?

vikramRH avatar Dec 03 '25 07:12 vikramRH

ping

TejaX-Alaghari avatar Dec 12 '25 03:12 TejaX-Alaghari

I thought the way to achieve this in new pass manager was to have an analysis printer pass

arsenm avatar Dec 12 '25 10:12 arsenm

I thought the way to achieve this in new pass manager was to have an analysis printer pass

As the module pass is destroyed only once, printing reg usage is implemented towards the end inside the destructor. And the lit tests also pass without any issues.

@arsenm and @aeubanks, please let us know if you see any critical problems with this approach, and we can implement the same as a separate pass.

TejaX-Alaghari avatar Dec 16 '25 04:12 TejaX-Alaghari

@arsenm and @aeubanks, please let us know if you see any critical problems with this approach, and we can implement the same as a separate pass.

I'd rather have the analysis printing pass like other analyses. Overall I want far fewer of these unstructured printing flags

arsenm avatar Dec 16 '25 12:12 arsenm