Skip to content

Commit 381e1bb

Browse files
authored
[BOLT] fix print-mem-data not working (#156332)
This option `print-mem-data` is currently not working, use this fix to restore its functionality.
1 parent 1bafd02 commit 381e1bb

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

bolt/include/bolt/Core/MCPlusBuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2216,7 +2216,8 @@ class MCPlusBuilder {
22162216
}
22172217

22182218
/// Print each annotation attached to \p Inst.
2219-
void printAnnotations(const MCInst &Inst, raw_ostream &OS) const;
2219+
void printAnnotations(const MCInst &Inst, raw_ostream &OS,
2220+
bool PrintMemData = false) const;
22202221

22212222
/// Remove annotation with a given \p Index.
22222223
///

bolt/lib/Core/BinaryContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2044,7 +2044,7 @@ void BinaryContext::printInstruction(raw_ostream &OS, const MCInst &Instruction,
20442044
if (MCSymbol *Label = MIB->getInstLabel(Instruction))
20452045
OS << " # Label: " << *Label;
20462046

2047-
MIB->printAnnotations(Instruction, OS);
2047+
MIB->printAnnotations(Instruction, OS, PrintMemData || opts::PrintMemData);
20482048

20492049
if (opts::PrintDebugInfo)
20502050
printDebugInfo(OS, Instruction, Function, DwCtx.get());

bolt/lib/Core/MCPlusBuilder.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,8 @@ void MCPlusBuilder::stripAnnotations(MCInst &Inst, bool KeepTC) const {
378378
setTailCall(Inst);
379379
}
380380

381-
void MCPlusBuilder::printAnnotations(const MCInst &Inst,
382-
raw_ostream &OS) const {
381+
void MCPlusBuilder::printAnnotations(const MCInst &Inst, raw_ostream &OS,
382+
bool PrintMemData) const {
383383
std::optional<unsigned> FirstAnnotationOp = getFirstAnnotationOpIndex(Inst);
384384
if (!FirstAnnotationOp)
385385
return;
@@ -390,7 +390,11 @@ void MCPlusBuilder::printAnnotations(const MCInst &Inst,
390390
const int64_t Value = extractAnnotationValue(Imm);
391391
const auto *Annotation = reinterpret_cast<const MCAnnotation *>(Value);
392392
if (Index >= MCAnnotation::kGeneric) {
393-
OS << " # " << AnnotationNames[Index - MCAnnotation::kGeneric] << ": ";
393+
std::string AnnotationName =
394+
AnnotationNames[Index - MCAnnotation::kGeneric];
395+
if (!PrintMemData && AnnotationName == "MemoryAccessProfile")
396+
continue;
397+
OS << " # " << AnnotationName << ": ";
394398
Annotation->print(OS);
395399
}
396400
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Check that --print-mem-data option works properly in llvm-bolt
2+
3+
# RUN: split-file %s %t
4+
# RUN: %clang %cflags -fPIC -pie %t/main.s -o %t.exe -nostdlib -Wl,-q
5+
# RUN: llvm-bolt %t.exe -o %t.bolt --print-mem-data=true --print-cfg \
6+
# RUN: --data %t/fdata | FileCheck %s -check-prefix=CHECK-PRINT
7+
# RUN: llvm-bolt %t.exe -o %t.bolt --print-cfg \
8+
# RUN: --data %t/fdata | FileCheck %s -check-prefix=CHECK-DEFAULT
9+
10+
# CHECK-PRINT: ldr w2, [x1], #0x4 # MemoryAccessProfile: 7 total counts :
11+
# CHECK-PRINT-NEXT: { 0x123: 1 },
12+
# CHECK-PRINT-NEXT: { 0x456: 2 },
13+
# CHECK-PRINT-NEXT: { 0xabc: 4 }
14+
# CHECK-DEFAULT-NOT: MemoryAccessProfile
15+
16+
#--- main.s
17+
.text
18+
.align 4
19+
.global main
20+
.type main, %function
21+
main:
22+
sub sp, sp, #48
23+
add x1, sp, 8
24+
add x3, sp, 48
25+
mov w0, 0
26+
.L2:
27+
ldr w2, [x1], 4
28+
add w0, w0, w2
29+
cmp x1, x3
30+
bne .L2
31+
add sp, sp, 48
32+
ret
33+
.size main, .-main
34+
35+
# The three memory access data generated by the load at
36+
# offset 0x10 in the main.
37+
#--- fdata
38+
4 main 10 4 otherSym 123 1
39+
4 main 10 4 otherSym 456 2
40+
4 main 10 4 otherSym abc 4

0 commit comments

Comments
 (0)