Skip to content

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Oct 10, 2025

Fix a whitespace regression in MIR printing that was introduced in #137361.

The default value for ListSeparator is ", ", so we don't need to print an additional space in front of tokens for optional symbols and other things printed after operands.

Note, the modified LIT test will fail at trunk without the fix, demonstrating that the extra space before , pre-instr-symbol <mcsymbol > on Line 63 exists currently and is fixed with this change.

@jurahul jurahul force-pushed the nfc_remove_space_in_mir_print branch from e6988e1 to 05b3306 Compare October 11, 2025 01:24
@jurahul jurahul marked this pull request as ready for review October 12, 2025 17:40
@jurahul jurahul requested review from MatzeB and topperc October 12, 2025 17:40
@llvmbot
Copy link
Member

llvmbot commented Oct 12, 2025

@llvm/pr-subscribers-llvm-adt

Author: Rahul Joshi (jurahul)

Changes

Fix a whitespace regression in MIR printing that was introduced in #137361.

The default value for ListSeparator is ", ", so we don't need to print an additional space in front of tokens for optional symbols and other things printed after operands.

Note, the modified LIT test will fail at trunk without the fix, demonstrating that the extra space before , pre-instr-symbol &lt;mcsymbol &gt; on Line 63 exists currently and is fixed with this change.


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

3 Files Affected:

  • (modified) llvm/include/llvm/ADT/StringExtras.h (+4-2)
  • (modified) llvm/lib/CodeGen/MIRPrinter.cpp (+14-16)
  • (modified) llvm/test/CodeGen/MIR/AArch64/return-address-signing.mir (+1-1)
diff --git a/llvm/include/llvm/ADT/StringExtras.h b/llvm/include/llvm/ADT/StringExtras.h
index 7d81c63485be2..2440e7678a831 100644
--- a/llvm/include/llvm/ADT/StringExtras.h
+++ b/llvm/include/llvm/ADT/StringExtras.h
@@ -529,13 +529,15 @@ inline std::string join_items(Sep Separator, Args &&... Items) {
 class ListSeparator {
   bool First = true;
   StringRef Separator;
+  StringRef Prefix;
 
 public:
-  ListSeparator(StringRef Separator = ", ") : Separator(Separator) {}
+  ListSeparator(StringRef Separator = ", ", StringRef Prefix = "")
+      : Separator(Separator), Prefix(Prefix) {}
   operator StringRef() {
     if (First) {
       First = false;
-      return {};
+      return Prefix;
     }
     return Separator;
   }
diff --git a/llvm/lib/CodeGen/MIRPrinter.cpp b/llvm/lib/CodeGen/MIRPrinter.cpp
index bf8a6cdf097a9..368aab73498ec 100644
--- a/llvm/lib/CodeGen/MIRPrinter.cpp
+++ b/llvm/lib/CodeGen/MIRPrinter.cpp
@@ -866,48 +866,46 @@ static void printMI(raw_ostream &OS, MFPrintState &State,
 
   OS << TII->getName(MI.getOpcode());
 
-  LS = ListSeparator();
+  // Print a space after the opcode if any additional tokens are printed.
+  LS = ListSeparator(", ", " ");
 
-  if (I < E) {
-    OS << ' ';
-    for (; I < E; ++I) {
-      OS << LS;
-      printMIOperand(OS, State, MI, I, TRI, TII, ShouldPrintRegisterTies,
-                     PrintedTypes, MRI, /*PrintDef=*/true);
-    }
+  for (; I < E; ++I) {
+    OS << LS;
+    printMIOperand(OS, State, MI, I, TRI, TII, ShouldPrintRegisterTies,
+                   PrintedTypes, MRI, /*PrintDef=*/true);
   }
 
   // Print any optional symbols attached to this instruction as-if they were
   // operands.
   if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol()) {
-    OS << LS << " pre-instr-symbol ";
+    OS << LS << "pre-instr-symbol ";
     MachineOperand::printSymbol(OS, *PreInstrSymbol);
   }
   if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol()) {
-    OS << LS << " post-instr-symbol ";
+    OS << LS << "post-instr-symbol ";
     MachineOperand::printSymbol(OS, *PostInstrSymbol);
   }
   if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker()) {
-    OS << LS << " heap-alloc-marker ";
+    OS << LS << "heap-alloc-marker ";
     HeapAllocMarker->printAsOperand(OS, State.MST);
   }
   if (MDNode *PCSections = MI.getPCSections()) {
-    OS << LS << " pcsections ";
+    OS << LS << "pcsections ";
     PCSections->printAsOperand(OS, State.MST);
   }
   if (MDNode *MMRA = MI.getMMRAMetadata()) {
-    OS << LS << " mmra ";
+    OS << LS << "mmra ";
     MMRA->printAsOperand(OS, State.MST);
   }
   if (uint32_t CFIType = MI.getCFIType())
-    OS << LS << " cfi-type " << CFIType;
+    OS << LS << "cfi-type " << CFIType;
 
   if (auto Num = MI.peekDebugInstrNum())
-    OS << LS << " debug-instr-number " << Num;
+    OS << LS << "debug-instr-number " << Num;
 
   if (PrintLocations) {
     if (const DebugLoc &DL = MI.getDebugLoc()) {
-      OS << LS << " debug-location ";
+      OS << LS << "debug-location ";
       DL->printAsOperand(OS, State.MST);
     }
   }
diff --git a/llvm/test/CodeGen/MIR/AArch64/return-address-signing.mir b/llvm/test/CodeGen/MIR/AArch64/return-address-signing.mir
index 1030917c87419..302f70fc15192 100644
--- a/llvm/test/CodeGen/MIR/AArch64/return-address-signing.mir
+++ b/llvm/test/CodeGen/MIR/AArch64/return-address-signing.mir
@@ -1,4 +1,4 @@
-# RUN: llc -mtriple=aarch64 -run-pass=prologepilog -run-pass=aarch64-ptrauth -o - %s 2>&1 | FileCheck %s
+# RUN: llc -mtriple=aarch64 -run-pass=prologepilog -run-pass=aarch64-ptrauth -o - %s 2>&1 | FileCheck --strict-whitespace %s
 --- |
   target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
   target triple = "aarch64"

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

Can you add unit tests for ListSeparator?

@jurahul
Copy link
Contributor Author

jurahul commented Oct 13, 2025

Added unit test for ListSeparator changes.

@jurahul jurahul requested a review from kuhar October 13, 2025 13:25
@jurahul jurahul merged commit fe00ab4 into llvm:main Oct 13, 2025
10 checks passed
@jurahul jurahul deleted the nfc_remove_space_in_mir_print branch October 13, 2025 20:05
akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
Fix a whitespace regression in MIR printing that was introduced in
llvm#137361.

The default value for `ListSeparator` is `", "`, so we don't need to
print an additional space in front of tokens for optional symbols and
other things printed after operands.

Note, the modified LIT test will fail at trunk without the fix,
demonstrating that the extra space before `, pre-instr-symbol <mcsymbol
>` on Line 63 exists currently and is fixed with this change.
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.

3 participants