From 38aa46dd1f5b4ed3935bf9d8e18159f46fef5585 Mon Sep 17 00:00:00 2001 From: Haibo Jiang Date: Thu, 11 Sep 2025 20:29:05 +0800 Subject: [PATCH 1/2] [BOLT] Fix unrecognized option values for print-sorted-by-order (#155613) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently llvm-bolt does not recognize the input value for ’-print-sorted-by-order‘. This patch adds support for ascending and descending values for the flag. --- bolt/lib/Passes/BinaryPasses.cpp | 7 ++++- bolt/test/AArch64/print-sorted-by-order.s | 31 +++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 bolt/test/AArch64/print-sorted-by-order.s diff --git a/bolt/lib/Passes/BinaryPasses.cpp b/bolt/lib/Passes/BinaryPasses.cpp index fa95ad7324ac..5b7f7d40244c 100644 --- a/bolt/lib/Passes/BinaryPasses.cpp +++ b/bolt/lib/Passes/BinaryPasses.cpp @@ -59,7 +59,12 @@ static cl::opt DynoStatsSortOrderOpt( "print-sorted-by-order", cl::desc("use ascending or descending order when printing functions " "ordered by dyno stats"), - cl::init(DynoStatsSortOrder::Descending), cl::cat(BoltOptCategory)); + cl::init(DynoStatsSortOrder::Descending), + cl::values(clEnumValN(DynoStatsSortOrder::Ascending, "ascending", + "Ascending order"), + clEnumValN(DynoStatsSortOrder::Descending, "descending", + "Descending order")), + cl::cat(BoltOptCategory)); cl::list HotTextMoveSections("hot-text-move-sections", diff --git a/bolt/test/AArch64/print-sorted-by-order.s b/bolt/test/AArch64/print-sorted-by-order.s new file mode 100644 index 000000000000..30b5297ec5cd --- /dev/null +++ b/bolt/test/AArch64/print-sorted-by-order.s @@ -0,0 +1,31 @@ +# Check that --print-sorted-by-order= option works properly in llvm-bolt +# +# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o +# RUN: %clang %cflags -fPIC -pie %t.o -o %t.exe -nostdlib -Wl,-q +# RUN: link_fdata %s %t.o %t.fdata +# RUN: llvm-bolt %t.exe -o %t.bolt --print-sorted-by=all --print-sorted-by-order=ascending \ +# RUN: --data %t.fdata | FileCheck %s -check-prefix=CHECK-ASCEND +# RUN: llvm-bolt %t.exe -o %t.bolt --print-sorted-by=all --print-sorted-by-order=descending \ +# RUN: --data %t.fdata | FileCheck %s -check-prefix=CHECK-DESCEND + +# CHECK-ASCEND: BOLT-INFO: top functions sorted by dyno stats are: +# CHECK-ASCEND-NEXT: bar +# CHECK-ASCEND-NEXT: foo +# CHECK-DESCEND: BOLT-INFO: top functions sorted by dyno stats are: +# CHECK-DESCEND-NEXT: foo +# CHECK-DESCEND-NEXT: bar + + .text + .align 4 + .global bar + .type bar, %function +bar: + mov w0, wzr + ret + + .global foo + .type foo, %function +foo: +# FDATA: 1 foo 0 1 bar 0 0 1 + bl bar + ret -- Gitee From 65b08868535369bfca76492730b9301afc9d767f Mon Sep 17 00:00:00 2001 From: Haibo Jiang Date: Fri, 12 Sep 2025 17:10:28 +0800 Subject: [PATCH 2/2] [BOLT] fix print-mem-data not working (#156332) This option `print-mem-data` is currently not working, use this fix to restore its functionality. --- bolt/include/bolt/Core/MCPlusBuilder.h | 3 +- bolt/lib/Core/BinaryContext.cpp | 2 +- bolt/lib/Core/MCPlusBuilder.cpp | 10 +++++-- bolt/test/AArch64/print-mem-data.test | 40 ++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 bolt/test/AArch64/print-mem-data.test diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h index 2e167ffb2909..7f4f8fcc090d 100644 --- a/bolt/include/bolt/Core/MCPlusBuilder.h +++ b/bolt/include/bolt/Core/MCPlusBuilder.h @@ -1978,7 +1978,8 @@ public: } /// 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 7c3b4a87cfac..446d12b8312b 100644 --- a/bolt/lib/Core/BinaryContext.cpp +++ b/bolt/lib/Core/BinaryContext.cpp @@ -1996,7 +1996,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 778528d251b6..7eb6c5ff390b 100644 --- a/bolt/lib/Core/MCPlusBuilder.cpp +++ b/bolt/lib/Core/MCPlusBuilder.cpp @@ -379,8 +379,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 FirstAnnotationOp = getFirstAnnotationOpIndex(Inst); if (!FirstAnnotationOp) return; @@ -391,7 +391,11 @@ void MCPlusBuilder::printAnnotations(const MCInst &Inst, const int64_t Value = extractAnnotationValue(Imm); const auto *Annotation = reinterpret_cast(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/print-mem-data.test b/bolt/test/AArch64/print-mem-data.test new file mode 100644 index 000000000000..09d4f4640a45 --- /dev/null +++ b/bolt/test/AArch64/print-mem-data.test @@ -0,0 +1,40 @@ +# Check that --print-mem-data option works properly in llvm-bolt + +# RUN: split-file %s %t +# RUN: %clang %cflags -fPIC -pie %t/main.s -o %t.exe -nostdlib -Wl,-q +# RUN: llvm-bolt %t.exe -o %t.bolt --print-mem-data=true --print-cfg \ +# RUN: --data %t/fdata | FileCheck %s -check-prefix=CHECK-PRINT +# RUN: llvm-bolt %t.exe -o %t.bolt --print-cfg \ +# RUN: --data %t/fdata | FileCheck %s -check-prefix=CHECK-DEFAULT + +# CHECK-PRINT: ldr w2, [x1], #0x4 # MemoryAccessProfile: 7 total counts : +# CHECK-PRINT-NEXT: { 0x123: 1 }, +# CHECK-PRINT-NEXT: { 0x456: 2 }, +# CHECK-PRINT-NEXT: { 0xabc: 4 } +# CHECK-DEFAULT-NOT: MemoryAccessProfile + +#--- main.s + .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 + +# The three memory access data generated by the load at +# offset 0x10 in the main. +#--- fdata +4 main 10 4 otherSym 123 1 +4 main 10 4 otherSym 456 2 +4 main 10 4 otherSym abc 4 -- Gitee