xref: /llvm-project/bolt/test/X86/bolt-address-translation.test (revision 11791ae7b0b05b8bd8d806331ff51da618912cf8)
1## Check a common case for BOLT address translation tables. These tables are used
2## to translate profile activity happening in a bolted binary back to the
3## original binary, so you can run BOLT again, with updated profile collected
4## in a production environment that only runs bolted binaries. As BOLT only
5## takes no-bolt binaries as inputs, this translation is necessary to cover
6## this scenario.
7#
8# RUN: yaml2obj %p/Inputs/blarge.yaml &> %t.exe
9# RUN: llvm-bolt %t.exe -o %t.out --data %p/Inputs/blarge.fdata \
10# RUN:   --reorder-blocks=normal --split-functions --enable-bat 2>&1 | FileCheck %s
11# RUN: llvm-bat-dump %t.out --dump-all \
12# RUN:   --translate=0x401180 | FileCheck %s --check-prefix=CHECK-BAT-DUMP
13#
14## In this test we focus on function usqrt at address 0x401170. This is a
15## non-reloc binary case, so we don't expect this address to change, that's
16## why we hardcode its address here. This address also comes hardcoded in the
17## blarge.yaml input file.
18##
19## This is the layout of the function before BOLT reorder blocks:
20##
21##    BB Layout   : .LBB02, .Ltmp39, .LFT1, .Ltmp38, .LFT2
22##
23## This is the layout of the function after BOLT reorder blocks:
24##
25##    BB Layout   : .LBB02, .Ltmp38, .Ltmp39, .LFT2, .LFT3
26##
27## .Ltmp38 is originally at offset 0x39 but gets moved to 0xc (see full dump
28## below).
29##
30## We check that BAT is able to translate references happening in .Ltmp38 to
31## its original offset.
32##
33
34## This binary has 3 functions with profile, all of them are split, so 6 maps.
35## BAT creates one map per function fragment.
36#
37# CHECK:      BOLT: 3 out of 7 functions were overwritten.
38# CHECK:      BOLT-INFO: Wrote 6 BAT maps
39# CHECK:      BOLT-INFO: Wrote 3 function and 58 basic block hashes
40# CHECK:      BOLT-INFO: BAT section size (bytes): 940
41#
42# usqrt mappings (hot part). We match against any key (left side containing
43# the bolted binary offsets) because BOLT may change where it puts instructions
44# depending on whether it is relaxing a branch or not. But the original input
45# binary offsets (right side) should be the same because these addresses are
46# hardcoded in the blarge.yaml file.
47#
48# CHECK-BAT-DUMP:      Function Address: 0x401170, hash: 0xace6cbc638b31983
49# CHECK-BAT-DUMP-NEXT: BB mappings:
50# CHECK-BAT-DUMP-NEXT: 0x0 -> 0x0 hash: 0x36007ba1d80c0000
51# CHECK-BAT-DUMP-NEXT: 0x8 -> 0x8 (branch)
52# CHECK-BAT-DUMP-NEXT: 0x{{.*}} -> 0x39 hash: 0x5c06705524800039
53# CHECK-BAT-DUMP-NEXT: 0x{{.*}} -> 0x3d (branch)
54# CHECK-BAT-DUMP-NEXT: 0x{{.*}} -> 0x10 hash: 0xd70d7a64320e0010
55# CHECK-BAT-DUMP-NEXT: 0x{{.*}} -> 0x30 (branch)
56#
57# CHECK-BAT-DUMP: 3 cold mappings
58#
59# Now check that the translation 0x401180 maps back to its correct
60# input offset (offset 3d in the usqrt input function).
61#
62# COM: CHECK-BAT-DUMP: Translating addresses according to parsed BAT tables:
63#      CHECK-BAT-DUMP: 0x401180 -> usqrt + 0x3d
64
65# -------------------------
66# Full dump for reference (this is not checked):
67# -------------------------
68
69Binary Function "usqrt" after finalize-functions
70  Number      : 7
71  State       : CFG finalized
72  Address     : 0x401170
73  Size        : 0x43
74  MaxSize     : 0x43
75  Offset      : 0xcb0
76  Section     : .text
77  Orc Section : .local.text.usqrt
78  LSDA        : 0x0
79  IsSimple    : 1
80  IsMultiEntry: 0
81  IsSplit     : 1
82  BB Count    : 5
83  Hash        : a6468f132ec176ca
84  BB Layout   : .LBB02, .Ltmp38, .Ltmp39, .LFT2, .LFT3
85  Exec Count  : 199
86  Profile Acc : 100.0%
87
88.LBB02 (4 instructions, align : 1)
89  Entry Point
90  Exec Count : 199
91  CFI State : 0
92  Input offset: 0
93    00000000:   movl    $0x20, %r8d
94    00000006:   xorl    %eax, %eax
95    00000008:   xorl    %edx, %edx # Offset: 8
96    0000000a:   jmp     .Ltmp39
97  Successors: .Ltmp39 (mispreds: 0, count: 0)
98
99.Ltmp38 (2 instructions, align : 1)
100  Exec Count : 4711
101  CFI State : 0
102  Input offset: 39
103  Predecessors: .Ltmp39, .LFT2
104    0000000c:   subl    $0x1, %r8d
105    00000010:   je      .LFT3 # Offset: 61
106  Successors: .LFT3 (mispreds: 0, count: 0), .Ltmp39 (mispreds: 33, count: 4711)
107
108.Ltmp39 (10 instructions, align : 1)
109  Exec Count : 4711
110  CFI State : 0
111  Input offset: 10
112  Predecessors: .Ltmp38, .LBB02
113    00000012:   movq    %rdi, %rcx
114    00000015:   addq    %rax, %rax
115    00000018:   shlq    $0x2, %rdi
116    0000001c:   andl    $0xc0000000, %ecx
117    00000022:   shrq    $0x1e, %rcx
118    00000026:   leaq    (%rcx,%rdx,4), %rdx
119    0000002a:   leaq    0x1(%rax,%rax), %rcx
120    0000002f:   cmpq    %rcx, %rdx
121    00000032:   jb      .Ltmp38 # Offset: 48
122    00000034:   jmp     .LFT2
123  Successors: .Ltmp38 (mispreds: 171, count: 2886), .LFT2 (mispreds: 0, count: 0)
124
125-------   HOT-COLD SPLIT POINT   -------
126
127.LFT2 (3 instructions, align : 1)
128  Exec Count : 0
129  CFI State : 0
130  Input offset: 32
131  Predecessors: .Ltmp39
132    00000036:   subq    %rcx, %rdx
133    00000039:   addq    $0x1, %rax # Offset: 53
134    0000003d:   jmp     .Ltmp38
135  Successors: .Ltmp38 (mispreds: 0, count: 0)
136
137.LFT3 (2 instructions, align : 1)
138  Exec Count : 0
139  CFI State : 0
140  Input offset: 3f
141  Predecessors: .Ltmp38
142    0000003f:   movq    %rax, (%rsi)
143    00000042:   retq # Offset: 66
144
145DWARF CFI Instructions:
146    <empty>
147End of Function "usqrt"
148