Skip to content

Register assembly printer passes #138348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2025
Merged

Conversation

MatzeB
Copy link
Contributor

@MatzeB MatzeB commented May 2, 2025

Register assembly printer passes in the pass registry.

This makes it possible to use llc -start-before=<target>-asm-printer ... in tests.

Adds a char &ID parameter to the AssemblyPrinter constructor to allow targets to use the INITIALIZE_PASS macros and register the pass in the pass registry. This currently has a default parameter so it won't break any targets that have not been updated.

Copy link

github-actions bot commented May 2, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@MatzeB
Copy link
Contributor Author

MatzeB commented May 2, 2025

This currently changes AArch64 and X86 to register their AsmPrinter passes and adjusts some tests that seem like they just wanted to run assembly printing.

Putting this up for comments/feedback before I do all the typing for the other 23 targets... Or would be acceptable to land this with just some targets adapted?

@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-backend-spir-v
@llvm/pr-subscribers-backend-msp430
@llvm/pr-subscribers-backend-m68k
@llvm/pr-subscribers-backend-risc-v
@llvm/pr-subscribers-backend-webassembly
@llvm/pr-subscribers-backend-aarch64
@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-backend-x86

Author: Matthias Braun (MatzeB)

Changes

Register assembly printer passes in the pass registry.

This makes it possible to use llc -start-before=&lt;target&gt;-asm-printer ... in tests.

Adds a char &amp;ID parameter to the AssemblyPrinter constructor to allow targets to use the INITIALIZE_PASS macros and register the pass in the pass registry. This currently has a default parameter so it won't break any targets that have not been updated.


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

14 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/AsmPrinter.h (+2-1)
  • (modified) llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp (+2-1)
  • (modified) llvm/lib/Target/AArch64/AArch64.h (+1)
  • (modified) llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp (+9-2)
  • (modified) llvm/lib/Target/AArch64/AArch64TargetMachine.cpp (+1)
  • (modified) llvm/lib/Target/X86/X86.h (+1)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.cpp (+7-1)
  • (modified) llvm/lib/Target/X86/X86AsmPrinter.h (+4)
  • (modified) llvm/lib/Target/X86/X86TargetMachine.cpp (+1)
  • (modified) llvm/test/CodeGen/X86/align-basic-block-sections.mir (+1-1)
  • (modified) llvm/test/CodeGen/X86/basic-block-address-map-mir-parse.mir (+1-1)
  • (modified) llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir (+1-1)
  • (modified) llvm/test/DebugInfo/MIR/AArch64/clobber-sp.mir (+1-1)
  • (modified) llvm/test/DebugInfo/X86/single-location.mir (+1-1)
diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h
index 79d23986f3b43..9132a0a6ea5a3 100644
--- a/llvm/include/llvm/CodeGen/AsmPrinter.h
+++ b/llvm/include/llvm/CodeGen/AsmPrinter.h
@@ -240,7 +240,8 @@ class AsmPrinter : public MachineFunctionPass {
   bool DbgInfoAvailable = false;
 
 protected:
-  explicit AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer);
+  AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer,
+             char &ID = AsmPrinter::ID);
 
 public:
   ~AsmPrinter() override;
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index bdcd54a135da9..d7710212e8cc3 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -382,7 +382,8 @@ Align AsmPrinter::getGVAlignment(const GlobalObject *GV, const DataLayout &DL,
   return Alignment;
 }
 
-AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer)
+AsmPrinter::AsmPrinter(TargetMachine &tm, std::unique_ptr<MCStreamer> Streamer,
+                       char &ID)
     : MachineFunctionPass(ID), TM(tm), MAI(tm.getMCAsmInfo()),
       OutContext(Streamer->getContext()), OutStreamer(std::move(Streamer)),
       SM(*this) {
diff --git a/llvm/lib/Target/AArch64/AArch64.h b/llvm/lib/Target/AArch64/AArch64.h
index ffa578d412b3c..a5bb7520aec0d 100644
--- a/llvm/lib/Target/AArch64/AArch64.h
+++ b/llvm/lib/Target/AArch64/AArch64.h
@@ -77,6 +77,7 @@ ModulePass *createAArch64Arm64ECCallLoweringPass();
 void initializeAArch64A53Fix835769Pass(PassRegistry&);
 void initializeAArch64A57FPLoadBalancingPass(PassRegistry&);
 void initializeAArch64AdvSIMDScalarPass(PassRegistry&);
+void initializeAArch64AsmPrinterPass(PassRegistry&);
 void initializeAArch64PointerAuthPass(PassRegistry&);
 void initializeAArch64BranchTargetsPass(PassRegistry&);
 void initializeAArch64CFIFixupPass(PassRegistry&);
diff --git a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
index 870df4c387ca4..38be677ec805b 100644
--- a/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
+++ b/llvm/lib/Target/AArch64/AArch64AsmPrinter.cpp
@@ -96,9 +96,11 @@ class AArch64AsmPrinter : public AsmPrinter {
       SectionToImportedFunctionCalls;
 
 public:
+  static char ID;
+
   AArch64AsmPrinter(TargetMachine &TM, std::unique_ptr<MCStreamer> Streamer)
-      : AsmPrinter(TM, std::move(Streamer)), MCInstLowering(OutContext, *this),
-        FM(*this) {}
+      : AsmPrinter(TM, std::move(Streamer), ID),
+        MCInstLowering(OutContext, *this), FM(*this) {}
 
   StringRef getPassName() const override { return "AArch64 Assembly Printer"; }
 
@@ -3523,6 +3525,11 @@ const MCExpr *AArch64AsmPrinter::lowerConstant(const Constant *CV,
   return AsmPrinter::lowerConstant(CV, BaseCV, Offset);
 }
 
+char AArch64AsmPrinter::ID = 0;
+
+INITIALIZE_PASS(AArch64AsmPrinter, "aarch64-asm-printer",
+                "AArch64 Assmebly Printer", false, false)
+
 // Force static initialization.
 extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64AsmPrinter() {
   RegisterAsmPrinter<AArch64AsmPrinter> X(getTheAArch64leTarget());
diff --git a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
index 431076f188d98..5693a551e679f 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetMachine.cpp
@@ -235,6 +235,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAArch64Target() {
   initializeAArch64A53Fix835769Pass(PR);
   initializeAArch64A57FPLoadBalancingPass(PR);
   initializeAArch64AdvSIMDScalarPass(PR);
+  initializeAArch64AsmPrinterPass(PR);
   initializeAArch64BranchTargetsPass(PR);
   initializeAArch64CollectLOHPass(PR);
   initializeAArch64CompressJumpTablesPass(PR);
diff --git a/llvm/lib/Target/X86/X86.h b/llvm/lib/Target/X86/X86.h
index e6c0612101bc7..ba53ffd857fb3 100644
--- a/llvm/lib/Target/X86/X86.h
+++ b/llvm/lib/Target/X86/X86.h
@@ -176,6 +176,7 @@ void initializeFPSPass(PassRegistry &);
 void initializeFixupBWInstPassPass(PassRegistry &);
 void initializeFixupLEAPassPass(PassRegistry &);
 void initializeX86ArgumentStackSlotPassPass(PassRegistry &);
+void initializeX86AsmPrinterPass(PassRegistry &);
 void initializeX86FixupInstTuningPassPass(PassRegistry &);
 void initializeX86FixupVectorConstantsPassPass(PassRegistry &);
 void initializeWinEHStatePassPass(PassRegistry &);
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.cpp b/llvm/lib/Target/X86/X86AsmPrinter.cpp
index 29ec14e8cf46d..5f5bfc70e8a1a 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.cpp
+++ b/llvm/lib/Target/X86/X86AsmPrinter.cpp
@@ -17,6 +17,7 @@
 #include "MCTargetDesc/X86MCTargetDesc.h"
 #include "MCTargetDesc/X86TargetStreamer.h"
 #include "TargetInfo/X86TargetInfo.h"
+#include "X86.h"
 #include "X86InstrInfo.h"
 #include "X86MachineFunctionInfo.h"
 #include "X86Subtarget.h"
@@ -53,7 +54,7 @@ using namespace llvm;
 
 X86AsmPrinter::X86AsmPrinter(TargetMachine &TM,
                              std::unique_ptr<MCStreamer> Streamer)
-    : AsmPrinter(TM, std::move(Streamer)), FM(*this) {}
+    : AsmPrinter(TM, std::move(Streamer), ID), FM(*this) {}
 
 //===----------------------------------------------------------------------===//
 // Primitive Helper Functions.
@@ -1086,6 +1087,11 @@ void X86AsmPrinter::emitEndOfAsmFile(Module &M) {
   }
 }
 
+char X86AsmPrinter::ID = 0;
+
+INITIALIZE_PASS(X86AsmPrinter, "x86-asm-printer", "X86 Assembly Printer", false,
+                false)
+
 //===----------------------------------------------------------------------===//
 // Target Registry Stuff
 //===----------------------------------------------------------------------===//
diff --git a/llvm/lib/Target/X86/X86AsmPrinter.h b/llvm/lib/Target/X86/X86AsmPrinter.h
index 8dd7fa4431680..61d8f45501ab1 100644
--- a/llvm/lib/Target/X86/X86AsmPrinter.h
+++ b/llvm/lib/Target/X86/X86AsmPrinter.h
@@ -25,6 +25,10 @@ class X86Subtarget;
 class TargetMachine;
 
 class LLVM_LIBRARY_VISIBILITY X86AsmPrinter : public AsmPrinter {
+public:
+  static char ID;
+
+private:
   const X86Subtarget *Subtarget = nullptr;
   FaultMaps FM;
   std::unique_ptr<MCCodeEmitter> CodeEmitter;
diff --git a/llvm/lib/Target/X86/X86TargetMachine.cpp b/llvm/lib/Target/X86/X86TargetMachine.cpp
index 975b94c18fb7f..5fff9c30205dd 100644
--- a/llvm/lib/Target/X86/X86TargetMachine.cpp
+++ b/llvm/lib/Target/X86/X86TargetMachine.cpp
@@ -102,6 +102,7 @@ extern "C" LLVM_C_ABI void LLVMInitializeX86Target() {
   initializeX86ReturnThunksPass(PR);
   initializeX86DAGToDAGISelLegacyPass(PR);
   initializeX86ArgumentStackSlotPassPass(PR);
+  initializeX86AsmPrinterPass(PR);
   initializeX86FixupInstTuningPassPass(PR);
   initializeX86FixupVectorConstantsPassPass(PR);
   initializeX86DynAllocaExpanderPass(PR);
diff --git a/llvm/test/CodeGen/X86/align-basic-block-sections.mir b/llvm/test/CodeGen/X86/align-basic-block-sections.mir
index 17a675f5c5b72..02ccbcf0c0897 100644
--- a/llvm/test/CodeGen/X86/align-basic-block-sections.mir
+++ b/llvm/test/CodeGen/X86/align-basic-block-sections.mir
@@ -1,5 +1,5 @@
 # Check if the alignment directive is put on the correct place when the basic block section option is used.
-# RUN: llc -mtriple x86_64-unknown-linux-gnu -start-after=bbsections-prepare  %s -o - | FileCheck %s -check-prefix=CHECK
+# RUN: llc -mtriple x86_64-unknown-linux-gnu -start-before=x86-asm-printer %s -o - | FileCheck %s -check-prefix=CHECK
 
 # How to generate the input:
 # foo.c
diff --git a/llvm/test/CodeGen/X86/basic-block-address-map-mir-parse.mir b/llvm/test/CodeGen/X86/basic-block-address-map-mir-parse.mir
index 8ac93c79fa5a2..a49a4e2726021 100644
--- a/llvm/test/CodeGen/X86/basic-block-address-map-mir-parse.mir
+++ b/llvm/test/CodeGen/X86/basic-block-address-map-mir-parse.mir
@@ -1,5 +1,5 @@
 # Start after bbsections0-prepare and check that the BB address map is generated.
-# RUN: llc -mtriple x86_64-unknown-linux-gnu -start-after=bbsections-prepare -basic-block-address-map %s -o - | FileCheck %s -check-prefix=CHECK
+# RUN: llc -mtriple x86_64-unknown-linux-gnu -start-before=x86-asm-printer -basic-block-address-map %s -o - | FileCheck %s -check-prefix=CHECK
 
 # How to generate the input:
 # foo.cc
diff --git a/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir b/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
index 967622a11cd2b..e49ff140935a3 100644
--- a/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
+++ b/llvm/test/CodeGen/X86/basic-block-sections-mir-parse.mir
@@ -1,5 +1,5 @@
 # Start after bbsections0-prepare and check if the right code is generated.
-# RUN: llc -mtriple x86_64-unknown-linux-gnu -start-after=bbsections-prepare  %s -o - | FileCheck %s -check-prefix=CHECK
+# RUN: llc -mtriple x86_64-unknown-linux-gnu -start-before=x86-asm-printer  %s -o - | FileCheck %s -check-prefix=CHECK
 
 
 # How to generate the input:
diff --git a/llvm/test/DebugInfo/MIR/AArch64/clobber-sp.mir b/llvm/test/DebugInfo/MIR/AArch64/clobber-sp.mir
index c245684f1fca8..b248ee76a4141 100644
--- a/llvm/test/DebugInfo/MIR/AArch64/clobber-sp.mir
+++ b/llvm/test/DebugInfo/MIR/AArch64/clobber-sp.mir
@@ -1,4 +1,4 @@
-# RUN: llc -start-after=livedebugvalues -filetype=obj -o - %s \
+# RUN: llc -start-before=aarch64-asm-printer -filetype=obj -o - %s \
 # RUN:   | llvm-dwarfdump - | FileCheck %s
 # CHECK: .debug_info contents:
 # CHECK: DW_TAG_formal_parameter
diff --git a/llvm/test/DebugInfo/X86/single-location.mir b/llvm/test/DebugInfo/X86/single-location.mir
index 79049e31f3710..a1a167eda628b 100644
--- a/llvm/test/DebugInfo/X86/single-location.mir
+++ b/llvm/test/DebugInfo/X86/single-location.mir
@@ -1,4 +1,4 @@
-# RUN: llc -start-after=livedebugvalues --filetype=obj %s -o - \
+# RUN: llc -start-before=x86-asm-printer --filetype=obj %s -o - \
 # RUN:     | llvm-dwarfdump -v - | FileCheck %s
 #
 # Generated at -O2, stopped after livedebugvalues, with some metadata removed

@MatzeB MatzeB force-pushed the register-asm-printer branch from a86d5b9 to aa52b73 Compare May 2, 2025 22:11
@lenary
Copy link
Member

lenary commented May 3, 2025

Or would be acceptable to land this with just some targets adapted?

I think it would be acceptable. RISC-V lgtm.

@MatzeB
Copy link
Contributor Author

MatzeB commented May 7, 2025

I believe SPIRV failures are pre-existing, and SPIRV is an experimental target.

@MatzeB MatzeB merged commit 675cb70 into llvm:main May 7, 2025
8 of 11 checks passed
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Register assembly printer passes in the pass registry.

This makes it possible to use `llc -start-before=<target>-asm-printer ...` in tests.

Adds a `char &ID` parameter to the AssemblyPrinter constructor to allow
targets to use the `INITIALIZE_PASS` macros and register the pass in the
pass registry. This currently has a default parameter so it won't break
any targets that have not been updated.
@@ -1742,3 +1742,8 @@ void AMDGPUAsmPrinter::emitResourceUsageRemarks(
EmitResourceUsageRemark("BytesLDS", "LDS Size [bytes/block]",
CurrentProgramInfo.LDSSize);
}

char AMDGPUAsmPrinter::ID = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the corresponding argument in the constructor

@@ -1,4 +1,4 @@
# RUN: llc -mtriple=riscv32 -verify-machineinstrs -start-before=riscv-expand-pseudo -simplify-mir -o /dev/null -pass-remarks-analysis=asm-printer %s 2>&1 | FileCheck %s
# RUN: llc -mtriple=riscv32 -verify-machineinstrs -start-before=riscv-asm-printer -simplify-mir -o /dev/null -pass-remarks-analysis=asm-printer %s 2>&1 | FileCheck %s
Copy link
Contributor

@optimisan optimisan May 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this was passing I didn't review too closely, but all of these tests are passing for the wrong reasons:

TargetPassConfig does not handle the AsmPrinter; it is directly added to the pipeline outside of TargetPassConfig. Rather, what it does handle is determining if the pass pipeline runs till the AsmPrinter (which is incorrectly named willCompleteCodeGenPipeline). Based on this, either AsmPrinter or MIRPrinter pass is added at the end.

Problem is TargetPassConfig does not check if the provided passes in these pipeline-slicing options actually are in the pipeline -- if they match we slice, else we do nothing.

So here we can substitute riscv-asm-printer with any pass that is not in the pipeline (try something like irtranslator or x86-asm-printer)

Further, this test fails on qualifying this (without changing the pipeline) with -stop-after=riscv-asm-printer (why? Because willCompleteCodegenPipeline does not properly check if indeed said pass was in the pipeline)

All in all -stop-before=xx-asm-printer and -start-before=xx-asm-printer work as intended, but -stop-after and -start-after do not (they do the same thing as their "before" counterparts)

I'm not sure if we should try to fix this; this issue is already fixed in the NPM path and no tests reference asm-printer like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants