instrprof-value-prof.c
9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// RUN: %clang_profgen -mllvm -vp-static-alloc=false -O2 -o %t %s
// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t
// RUN: env LLVM_PROFILE_FILE=%t-2.profraw %run %t DO_NOT_INSTRUMENT
// RUN: llvm-profdata merge -o %t.profdata %t.profraw
// RUN: llvm-profdata merge -o %t-2.profdata %t-2.profraw
// RUN: llvm-profdata merge -o %t-merged.profdata %t.profraw %t-2.profdata
// RUN: llvm-profdata show --all-functions -ic-targets %t-2.profdata | FileCheck %s -check-prefix=NO-VALUE
// RUN: llvm-profdata show --all-functions -ic-targets %t.profdata | FileCheck %s
// RUN: llvm-profdata show --all-functions -ic-targets %t-merged.profdata | FileCheck %s
//
// RUN: env LLVM_PROFILE_FILE=%t-3.profraw LLVM_VP_BUFFER_SIZE=1 %run %t
// RUN: env LLVM_PROFILE_FILE=%t-4.profraw LLVM_VP_BUFFER_SIZE=8 %run %t
// RUN: env LLVM_PROFILE_FILE=%t-5.profraw LLVM_VP_BUFFER_SIZE=128 %run %t
// RUN: env LLVM_PROFILE_FILE=%t-6.profraw LLVM_VP_BUFFER_SIZE=1024 %run %t
// RUN: env LLVM_PROFILE_FILE=%t-7.profraw LLVM_VP_BUFFER_SIZE=102400 %run %t
// RUN: llvm-profdata merge -o %t-3.profdata %t-3.profraw
// RUN: llvm-profdata merge -o %t-4.profdata %t-4.profraw
// RUN: llvm-profdata merge -o %t-5.profdata %t-5.profraw
// RUN: llvm-profdata merge -o %t-6.profdata %t-6.profraw
// RUN: llvm-profdata merge -o %t-7.profdata %t-7.profraw
// RUN: llvm-profdata show --all-functions -ic-targets %t-3.profdata | FileCheck %s
// RUN: llvm-profdata show --all-functions -ic-targets %t-4.profdata | FileCheck %s
// RUN: llvm-profdata show --all-functions -ic-targets %t-5.profdata | FileCheck %s
// RUN: llvm-profdata show --all-functions -ic-targets %t-6.profdata | FileCheck %s
// RUN: llvm-profdata show --all-functions -ic-targets %t-7.profdata | FileCheck %s
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __llvm_profile_data __llvm_profile_data;
const __llvm_profile_data *__llvm_profile_begin_data(void);
const __llvm_profile_data *__llvm_profile_end_data(void);
void __llvm_profile_set_num_value_sites(__llvm_profile_data *Data,
uint32_t ValueKind,
uint16_t NumValueSites);
__llvm_profile_data *
__llvm_profile_iterate_data(const __llvm_profile_data *Data);
void *__llvm_get_function_addr(const __llvm_profile_data *Data);
void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data,
uint32_t CounterIndex);
#define DEF_FUNC(x) \
void x() {}
#define DEF_2_FUNCS(x) DEF_FUNC(x##_1) DEF_FUNC(x##_2)
#define DEF_4_FUNCS(x) DEF_2_FUNCS(x##_1) DEF_2_FUNCS(x##_2)
#define DEF_8_FUNCS(x) DEF_4_FUNCS(x##_1) DEF_4_FUNCS(x##_2)
#define DEF_16_FUNCS(x) DEF_8_FUNCS(x##_1) DEF_8_FUNCS(x##_2)
#define DEF_32_FUNCS(x) DEF_16_FUNCS(x##_1) DEF_16_FUNCS(x##_2)
#define DEF_64_FUNCS(x) DEF_32_FUNCS(x##_1) DEF_32_FUNCS(x##_2)
#define DEF_128_FUNCS(x) DEF_64_FUNCS(x##_1) DEF_64_FUNCS(x##_2)
#define FUNC_ADDR(x) &x,
#define FUNC_2_ADDRS(x) FUNC_ADDR(x##_1) FUNC_ADDR(x##_2)
#define FUNC_4_ADDRS(x) FUNC_2_ADDRS(x##_1) FUNC_2_ADDRS(x##_2)
#define FUNC_8_ADDRS(x) FUNC_4_ADDRS(x##_1) FUNC_4_ADDRS(x##_2)
#define FUNC_16_ADDRS(x) FUNC_8_ADDRS(x##_1) FUNC_8_ADDRS(x##_2)
#define FUNC_32_ADDRS(x) FUNC_16_ADDRS(x##_1) FUNC_16_ADDRS(x##_2)
#define FUNC_64_ADDRS(x) FUNC_32_ADDRS(x##_1) FUNC_32_ADDRS(x##_2)
#define FUNC_128_ADDRS(x) FUNC_64_ADDRS(x##_1) FUNC_64_ADDRS(x##_2)
DEF_8_FUNCS(callee)
DEF_128_FUNCS(caller)
void *CallerAddrs[] = {FUNC_128_ADDRS(caller)};
void *CalleeAddrs[] = {FUNC_8_ADDRS(callee)};
typedef struct CallerInfo {
void *CallerAddr;
uint32_t NS; /* Number value sites. */
} CallerInfo;
CallerInfo CallerInfos[128];
int cmpaddr(const void *p1, const void *p2) {
CallerInfo *addr1 = (CallerInfo *)p1;
CallerInfo *addr2 = (CallerInfo *)p2;
return (intptr_t)addr2->CallerAddr - (intptr_t)addr1->CallerAddr;
}
int main(int argc, const char *argv[]) {
unsigned S, NS = 0, I, V, doInstrument = 1;
const __llvm_profile_data *Data, *DataEnd;
if (argc >= 2 && !strcmp(argv[1], "DO_NOT_INSTRUMENT"))
doInstrument = 0;
for (I = 0; I < 128; I++) {
CallerInfos[I].CallerAddr = CallerAddrs[I];
CallerInfos[I].NS = I;
}
qsort(CallerInfos, sizeof(CallerInfos) / sizeof(CallerInfo), sizeof(CallerInfo),
cmpaddr);
/* We will synthesis value profile data for 128 callers functions declared.
* The number of value sites for each caller function is recorded in
* the NS field of the CallerInfo object. For each value site, the number of
* callee values is determined by the site index (modulo 8). The frequency
* of each callee target synthesized is equal to V + 1, in which V is the
* index of the target value for the callsite. */
Data = __llvm_profile_begin_data();
DataEnd = __llvm_profile_end_data();
for (; Data < DataEnd; Data = __llvm_profile_iterate_data(Data)) {
void *func = __llvm_get_function_addr(Data);
CallerInfo Key, *Res;
Key.CallerAddr = func;
Res = (CallerInfo *) bsearch(&Key, CallerInfos, sizeof(CallerInfos) / sizeof(CallerInfo),
sizeof(CallerInfo), cmpaddr);
if (Res) {
NS = Res->NS;
__llvm_profile_set_num_value_sites((__llvm_profile_data *)Data,
0 /*IPVK_IndirectCallTarget */, NS);
if (!doInstrument) {
continue;
}
for (S = 0; S < NS; S++) {
for (V = 0; V < S % 8; V++) {
unsigned C;
for (C = 0; C < V + 1; C++)
__llvm_profile_instrument_target((uint64_t)CalleeAddrs[V],
(void *)Data, S);
}
}
}
}
}
// NO-VALUE: Indirect Call Site Count: 127
// NO-VALUE-NEXT: Indirect Target Results:
// CHECK-LABEL: caller_1_1_1_1_2_2_1:
// CHECK: Indirect Call Site Count: 6
// CHECK-NEXT: Indirect Target Results:
// CHECK-NEXT: [ 1, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 2, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 2, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 3, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 3, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 3, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 4, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 4, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 4, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 4, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 5, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 5, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 5, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 5, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 5, callee_1_1_1, 1 ]
// CHECK-LABEL: caller_2_2_2_2_2_2_2:
// CHECK: Indirect Call Site Count: 127
// CHECK-NEXT: Indirect Target Results:
// CHECK-NEXT: [ 1, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 2, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 2, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 3, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 3, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 3, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 4, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 4, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 4, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 4, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 5, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 5, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 5, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 5, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 5, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 6, callee_2_1_2, 6 ]
// CHECK-NEXT: [ 6, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 6, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 6, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 6, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 6, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 7, callee_2_2_1, 7 ]
// CHECK-NEXT: [ 7, callee_2_1_2, 6 ]
// CHECK-NEXT: [ 7, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 7, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 7, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 7, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 7, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 9, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 10, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 10, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 11, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 11, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 11, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 12, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 12, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 12, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 12, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 13, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 13, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 13, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 13, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 13, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 14, callee_2_1_2, 6 ]
// CHECK-NEXT: [ 14, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 14, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 14, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 14, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 14, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 15, callee_2_2_1, 7 ]
// CHECK-NEXT: [ 15, callee_2_1_2, 6 ]
// CHECK-NEXT: [ 15, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 15, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 15, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 15, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 15, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 17, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 18, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 18, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 19, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 19, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 19, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 20, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 20, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 20, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 20, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 21, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 21, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 21, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 21, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 21, callee_1_1_1, 1 ]
// CHECK-NEXT: [ 22, callee_2_1_2, 6 ]
// CHECK-NEXT: [ 22, callee_2_1_1, 5 ]
// CHECK-NEXT: [ 22, callee_1_2_2, 4 ]
// CHECK-NEXT: [ 22, callee_1_2_1, 3 ]
// CHECK-NEXT: [ 22, callee_1_1_2, 2 ]
// CHECK-NEXT: [ 22, callee_1_1_1, 1 ]