[llvm] [BOLT] fix print-mem-data not working (PR #156332)
Haibo Jiang via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 9 05:15:11 PDT 2025
https://github.com/Jianghibo updated https://github.com/llvm/llvm-project/pull/156332
>From 5b5b14f903fe59075a3b9f3e00b83e14b2a88f09 Mon Sep 17 00:00:00 2001
From: jianghaibo <jianghaibo9 at huawei.com>
Date: Mon, 1 Sep 2025 22:13:52 +0800
Subject: [PATCH] [BOLT] fix print-mem-data not working
---
bolt/include/bolt/Core/MCPlusBuilder.h | 3 +-
bolt/lib/Core/BinaryContext.cpp | 2 +-
bolt/lib/Core/MCPlusBuilder.cpp | 10 ++++--
bolt/test/AArch64/Inputs/print-mem-data.fdata | 3 ++
bolt/test/AArch64/print-mem-data.s | 33 +++++++++++++++++++
5 files changed, 46 insertions(+), 5 deletions(-)
create mode 100644 bolt/test/AArch64/Inputs/print-mem-data.fdata
create mode 100644 bolt/test/AArch64/print-mem-data.s
diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index ae04891e791f9..17b7743e31c00 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -2190,7 +2190,8 @@ class MCPlusBuilder {
}
/// Print each annotation attached to \p Inst.
- void printAnnotations(const MCInst &Inst, raw_ostream &OS) const;
+ void printAnnotations(const MCInst &Inst, raw_ostream &OS,
+ bool PrintMemData = false) const;
/// Remove annotation with a given \p Index.
///
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index dd0d041692484..e937751450ea8 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -2027,7 +2027,7 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
if (MCSymbol *Label = MIB->getInstLabel(Instruction))
OS << " # Label: " << *Label;
- MIB->printAnnotations(Instruction, OS);
+ MIB->printAnnotations(Instruction, OS, PrintMemData || opts::PrintMemData);
if (opts::PrintDebugInfo)
printDebugInfo(OS, Instruction, Function, DwCtx.get());
diff --git a/bolt/lib/Core/MCPlusBuilder.cpp b/bolt/lib/Core/MCPlusBuilder.cpp
index 7f962e14ea115..52475227eb32f 100644
--- a/bolt/lib/Core/MCPlusBuilder.cpp
+++ b/bolt/lib/Core/MCPlusBuilder.cpp
@@ -378,8 +378,8 @@ void MCPlusBuilder::stripAnnotations(MCInst &Inst, bool KeepTC) const {
setTailCall(Inst);
}
-void MCPlusBuilder::printAnnotations(const MCInst &Inst,
- raw_ostream &OS) const {
+void MCPlusBuilder::printAnnotations(const MCInst &Inst, raw_ostream &OS,
+ bool PrintMemData) const {
std::optional<unsigned> FirstAnnotationOp = getFirstAnnotationOpIndex(Inst);
if (!FirstAnnotationOp)
return;
@@ -390,7 +390,11 @@ void MCPlusBuilder::printAnnotations(const MCInst &Inst,
const int64_t Value = extractAnnotationValue(Imm);
const auto *Annotation = reinterpret_cast<const MCAnnotation *>(Value);
if (Index >= MCAnnotation::kGeneric) {
- OS << " # " << AnnotationNames[Index - MCAnnotation::kGeneric] << ": ";
+ std::string AnnotationName =
+ AnnotationNames[Index - MCAnnotation::kGeneric];
+ if (!PrintMemData && AnnotationName == "MemoryAccessProfile")
+ continue;
+ OS << " # " << AnnotationName << ": ";
Annotation->print(OS);
}
}
diff --git a/bolt/test/AArch64/Inputs/print-mem-data.fdata b/bolt/test/AArch64/Inputs/print-mem-data.fdata
new file mode 100644
index 0000000000000..77dd80aca142a
--- /dev/null
+++ b/bolt/test/AArch64/Inputs/print-mem-data.fdata
@@ -0,0 +1,3 @@
+4 main 10 4 a/1 137c810 2
+4 main 10 4 a/1 1381870 1
+4 main 10 4 a/1 139c790 1
diff --git a/bolt/test/AArch64/print-mem-data.s b/bolt/test/AArch64/print-mem-data.s
new file mode 100644
index 0000000000000..510f2b8ff8408
--- /dev/null
+++ b/bolt/test/AArch64/print-mem-data.s
@@ -0,0 +1,33 @@
+# FIXME: Inputs/print-mem-data.fdata will be deleted after link_fdata supports parsing
+# memory access profile.
+# Check that --print-mem-data option works properly in llvm-bolt
+#
+# RUN: %clang %cflags -fPIC -pie %s -o %t.exe -nostdlib -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt --print-mem-data=true --print-cfg \
+# RUN: --data %S/Inputs/print-mem-data.fdata | FileCheck %s -check-prefix=CHECK-PRINT
+# RUN: llvm-bolt %t.exe -o %t.bolt --print-cfg \
+# RUN: --data %S/Inputs/print-mem-data.fdata | FileCheck %s -check-prefix=CHECK-DEFAULT
+
+# CHECK-PRINT: ldr w2, [x1], #0x4 # MemoryAccessProfile: 4 total counts :
+# CHECK-PRINT-NEXT: { 0x137c810: 2 },
+# CHECK-PRINT-NEXT: { 0x1381870: 1 },
+# CHECK-PRINT-NEXT: { 0x139c790: 1 }
+# CHECK-DEFAULT-NOT: MemoryAccessProfile
+
+ .text
+ .align 4
+ .global main
+ .type main, %function
+main:
+ sub sp, sp, #48
+ add x1, sp, 8
+ add x3, sp, 48
+ mov w0, 0
+.L2:
+ ldr w2, [x1], 4
+ add w0, w0, w2
+ cmp x1, x3
+ bne .L2
+ add sp, sp, 48
+ ret
+ .size main, .-main
More information about the llvm-commits
mailing list