Skip to content

Commit fe00ab4

Browse files
authored
[NFC][MIR] Fix extra whitespace in MIR printing (#162928)
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.
1 parent 7e69051 commit fe00ab4

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

llvm/include/llvm/ADT/StringExtras.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,13 +529,15 @@ inline std::string join_items(Sep Separator, Args &&... Items) {
529529
class ListSeparator {
530530
bool First = true;
531531
StringRef Separator;
532+
StringRef Prefix;
532533

533534
public:
534-
ListSeparator(StringRef Separator = ", ") : Separator(Separator) {}
535+
ListSeparator(StringRef Separator = ", ", StringRef Prefix = "")
536+
: Separator(Separator), Prefix(Prefix) {}
535537
operator StringRef() {
536538
if (First) {
537539
First = false;
538-
return {};
540+
return Prefix;
539541
}
540542
return Separator;
541543
}

llvm/lib/CodeGen/MIRPrinter.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -862,48 +862,46 @@ static void printMI(raw_ostream &OS, MFPrintState &State,
862862

863863
OS << TII->getName(MI.getOpcode());
864864

865-
LS = ListSeparator();
865+
// Print a space after the opcode if any additional tokens are printed.
866+
LS = ListSeparator(", ", " ");
866867

867-
if (I < E) {
868-
OS << ' ';
869-
for (; I < E; ++I) {
870-
OS << LS;
871-
printMIOperand(OS, State, MI, I, TRI, TII, ShouldPrintRegisterTies,
872-
PrintedTypes, MRI, /*PrintDef=*/true);
873-
}
868+
for (; I < E; ++I) {
869+
OS << LS;
870+
printMIOperand(OS, State, MI, I, TRI, TII, ShouldPrintRegisterTies,
871+
PrintedTypes, MRI, /*PrintDef=*/true);
874872
}
875873

876874
// Print any optional symbols attached to this instruction as-if they were
877875
// operands.
878876
if (MCSymbol *PreInstrSymbol = MI.getPreInstrSymbol()) {
879-
OS << LS << " pre-instr-symbol ";
877+
OS << LS << "pre-instr-symbol ";
880878
MachineOperand::printSymbol(OS, *PreInstrSymbol);
881879
}
882880
if (MCSymbol *PostInstrSymbol = MI.getPostInstrSymbol()) {
883-
OS << LS << " post-instr-symbol ";
881+
OS << LS << "post-instr-symbol ";
884882
MachineOperand::printSymbol(OS, *PostInstrSymbol);
885883
}
886884
if (MDNode *HeapAllocMarker = MI.getHeapAllocMarker()) {
887-
OS << LS << " heap-alloc-marker ";
885+
OS << LS << "heap-alloc-marker ";
888886
HeapAllocMarker->printAsOperand(OS, State.MST);
889887
}
890888
if (MDNode *PCSections = MI.getPCSections()) {
891-
OS << LS << " pcsections ";
889+
OS << LS << "pcsections ";
892890
PCSections->printAsOperand(OS, State.MST);
893891
}
894892
if (MDNode *MMRA = MI.getMMRAMetadata()) {
895-
OS << LS << " mmra ";
893+
OS << LS << "mmra ";
896894
MMRA->printAsOperand(OS, State.MST);
897895
}
898896
if (uint32_t CFIType = MI.getCFIType())
899-
OS << LS << " cfi-type " << CFIType;
897+
OS << LS << "cfi-type " << CFIType;
900898

901899
if (auto Num = MI.peekDebugInstrNum())
902-
OS << LS << " debug-instr-number " << Num;
900+
OS << LS << "debug-instr-number " << Num;
903901

904902
if (PrintLocations) {
905903
if (const DebugLoc &DL = MI.getDebugLoc()) {
906-
OS << LS << " debug-location ";
904+
OS << LS << "debug-location ";
907905
DL->printAsOperand(OS, State.MST);
908906
}
909907
}

llvm/test/CodeGen/MIR/AArch64/return-address-signing.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# RUN: llc -mtriple=aarch64 -run-pass=prologepilog -run-pass=aarch64-ptrauth -o - %s 2>&1 | FileCheck %s
1+
# RUN: llc -mtriple=aarch64 -run-pass=prologepilog -run-pass=aarch64-ptrauth -o - %s 2>&1 | FileCheck --strict-whitespace %s
22
--- |
33
target datalayout = "e-m:e-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128"
44
target triple = "aarch64"

llvm/unittests/ADT/StringExtrasTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,12 @@ TEST(StringExtrasTest, ListSeparator) {
290290
EXPECT_EQ(S, "");
291291
S = LS2;
292292
EXPECT_EQ(S, " ");
293+
294+
ListSeparator LS3(",", "{");
295+
S = LS3;
296+
EXPECT_EQ(S, "{");
297+
S = LS3;
298+
EXPECT_EQ(S, ",");
293299
}
294300

295301
TEST(StringExtrasTest, toStringAPInt) {

0 commit comments

Comments
 (0)