blob: b71712fba7d974f4a29929c168e9ae85ea555e21 [file] [log] [blame]
Eugene Zelenkoffec81c2015-11-04 22:32:32 +00001//===- unittest/ProfileData/InstrProfTest.cpp -------------------*- C++ -*-===//
Justin Bogner2b6c5372015-02-18 01:58:17 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ProfileData/InstrProfReader.h"
11#include "llvm/ProfileData/InstrProfWriter.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000012#include "llvm/Support/Compression.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000013#include "gtest/gtest.h"
14
15#include <cstdarg>
16
17using namespace llvm;
18
19static ::testing::AssertionResult NoError(std::error_code EC) {
20 if (!EC)
21 return ::testing::AssertionSuccess();
22 return ::testing::AssertionFailure() << "error " << EC.value()
23 << ": " << EC.message();
24}
25
26static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
27 std::error_code Found) {
28 if (Expected == Found)
29 return ::testing::AssertionSuccess();
30 return ::testing::AssertionFailure() << "error " << Found.value()
31 << ": " << Found.message();
32}
33
34namespace {
35
36struct InstrProfTest : ::testing::Test {
37 InstrProfWriter Writer;
38 std::unique_ptr<IndexedInstrProfReader> Reader;
39
40 void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
41 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
42 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
43 Reader = std::move(ReaderOrErr.get());
44 }
45};
46
47TEST_F(InstrProfTest, write_and_read_empty_profile) {
48 auto Profile = Writer.writeBuffer();
49 readProfile(std::move(Profile));
50 ASSERT_TRUE(Reader->begin() == Reader->end());
51}
52
53TEST_F(InstrProfTest, write_and_read_one_function) {
Justin Bogner9e9a0572015-09-29 22:13:58 +000054 InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4});
55 Writer.addRecord(std::move(Record));
Justin Bogner2b6c5372015-02-18 01:58:17 +000056 auto Profile = Writer.writeBuffer();
57 readProfile(std::move(Profile));
58
59 auto I = Reader->begin(), E = Reader->end();
60 ASSERT_TRUE(I != E);
61 ASSERT_EQ(StringRef("foo"), I->Name);
62 ASSERT_EQ(0x1234U, I->Hash);
63 ASSERT_EQ(4U, I->Counts.size());
64 ASSERT_EQ(1U, I->Counts[0]);
65 ASSERT_EQ(2U, I->Counts[1]);
66 ASSERT_EQ(3U, I->Counts[2]);
67 ASSERT_EQ(4U, I->Counts[3]);
68 ASSERT_TRUE(++I == E);
69}
70
Xinliang David Li2004f002015-11-02 05:08:23 +000071TEST_F(InstrProfTest, get_instr_prof_record) {
72 InstrProfRecord Record1("foo", 0x1234, {1, 2});
73 InstrProfRecord Record2("foo", 0x1235, {3, 4});
74 Writer.addRecord(std::move(Record1));
75 Writer.addRecord(std::move(Record2));
76 auto Profile = Writer.writeBuffer();
77 readProfile(std::move(Profile));
78
79 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234);
80 ASSERT_TRUE(NoError(R.getError()));
81 ASSERT_EQ(2U, R.get().Counts.size());
82 ASSERT_EQ(1U, R.get().Counts[0]);
83 ASSERT_EQ(2U, R.get().Counts[1]);
84
85 R = Reader->getInstrProfRecord("foo", 0x1235);
86 ASSERT_TRUE(NoError(R.getError()));
87 ASSERT_EQ(2U, R.get().Counts.size());
88 ASSERT_EQ(3U, R.get().Counts[0]);
89 ASSERT_EQ(4U, R.get().Counts[1]);
90
91 R = Reader->getInstrProfRecord("foo", 0x5678);
92 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError()));
93
94 R = Reader->getInstrProfRecord("bar", 0x1234);
95 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError()));
96}
97
Justin Bogner2b6c5372015-02-18 01:58:17 +000098TEST_F(InstrProfTest, get_function_counts) {
Justin Bogner9e9a0572015-09-29 22:13:58 +000099 InstrProfRecord Record1("foo", 0x1234, {1, 2});
100 InstrProfRecord Record2("foo", 0x1235, {3, 4});
101 Writer.addRecord(std::move(Record1));
102 Writer.addRecord(std::move(Record2));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000103 auto Profile = Writer.writeBuffer();
104 readProfile(std::move(Profile));
105
106 std::vector<uint64_t> Counts;
107 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
108 ASSERT_EQ(2U, Counts.size());
109 ASSERT_EQ(1U, Counts[0]);
110 ASSERT_EQ(2U, Counts[1]);
111
Justin Bogner09829f42015-06-22 23:56:53 +0000112 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
113 ASSERT_EQ(2U, Counts.size());
114 ASSERT_EQ(3U, Counts[0]);
115 ASSERT_EQ(4U, Counts[1]);
116
Justin Bogner2b6c5372015-02-18 01:58:17 +0000117 std::error_code EC;
118 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
119 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
120
121 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
122 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
123}
124
Xinliang David Li2004f002015-11-02 05:08:23 +0000125TEST_F(InstrProfTest, get_icall_data_read_write) {
126 InstrProfRecord Record1("caller", 0x1234, {1, 2});
127 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
128 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
129 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
130
131 // 4 value sites.
132 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
133 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
134 {(uint64_t) "callee2", 2},
135 {(uint64_t) "callee3", 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000136 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000137 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000138 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000139 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
140 {(uint64_t) "callee2", 2}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000141 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000142 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000143 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000144
145 Writer.addRecord(std::move(Record1));
146 Writer.addRecord(std::move(Record2));
147 Writer.addRecord(std::move(Record3));
148 Writer.addRecord(std::move(Record4));
149 auto Profile = Writer.writeBuffer();
150 readProfile(std::move(Profile));
151
152 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
153 ASSERT_TRUE(NoError(R.getError()));
154 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
155 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
156 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
157 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
158 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
159
160 std::unique_ptr<InstrProfValueData[]> VD =
161 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000162
163 ASSERT_EQ(3U, VD[0].Count);
164 ASSERT_EQ(2U, VD[1].Count);
165 ASSERT_EQ(1U, VD[2].Count);
166
167 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
168 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
169 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
170}
171
172TEST_F(InstrProfTest, get_icall_data_read_write_with_weight) {
173 InstrProfRecord Record1("caller", 0x1234, {1, 2});
174 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
175 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
176 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
177
178 // 4 value sites.
179 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
180 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
181 {(uint64_t) "callee2", 2},
182 {(uint64_t) "callee3", 3}};
183 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
184 // No value profile data at the second site.
185 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
186 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
187 {(uint64_t) "callee2", 2}};
188 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
189 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
190 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
191
192 Writer.addRecord(std::move(Record1), 10);
193 Writer.addRecord(std::move(Record2));
194 Writer.addRecord(std::move(Record3));
195 Writer.addRecord(std::move(Record4));
196 auto Profile = Writer.writeBuffer();
197 readProfile(std::move(Profile));
198
199 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
200 ASSERT_TRUE(NoError(R.getError()));
201 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
202 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
203 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
204 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
205 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
206
207 std::unique_ptr<InstrProfValueData[]> VD =
208 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000209 ASSERT_EQ(30U, VD[0].Count);
210 ASSERT_EQ(20U, VD[1].Count);
211 ASSERT_EQ(10U, VD[2].Count);
212
Xinliang David Li2004f002015-11-02 05:08:23 +0000213 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
214 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
215 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
216}
217
Xinliang David Liee415892015-11-10 00:24:45 +0000218TEST_F(InstrProfTest, get_icall_data_read_write_big_endian) {
219 InstrProfRecord Record1("caller", 0x1234, {1, 2});
220 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
221 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
222 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
223
224 // 4 value sites.
225 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
226 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
227 {(uint64_t) "callee2", 2},
228 {(uint64_t) "callee3", 3}};
229 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
230 // No value profile data at the second site.
231 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
232 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
233 {(uint64_t) "callee2", 2}};
234 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
235 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
236 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
237
238 Writer.addRecord(std::move(Record1));
239 Writer.addRecord(std::move(Record2));
240 Writer.addRecord(std::move(Record3));
241 Writer.addRecord(std::move(Record4));
242
243 // Set big endian output.
244 Writer.setValueProfDataEndianness(support::big);
245
246 auto Profile = Writer.writeBuffer();
247 readProfile(std::move(Profile));
248
249 // Set big endian input.
250 Reader->setValueProfDataEndianness(support::big);
251
252 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
253 ASSERT_TRUE(NoError(R.getError()));
254 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
255 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
256 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
257 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
258 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
259
260 std::unique_ptr<InstrProfValueData[]> VD =
261 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Liee415892015-11-10 00:24:45 +0000262 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
263 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
264 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
265
266 // Restore little endian default:
267 Writer.setValueProfDataEndianness(support::little);
268}
269
Xinliang David Li2004f002015-11-02 05:08:23 +0000270TEST_F(InstrProfTest, get_icall_data_merge1) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000271 static const char caller[] = "caller";
272 static const char callee1[] = "callee1";
273 static const char callee2[] = "callee2";
274 static const char callee3[] = "callee3";
275 static const char callee4[] = "callee4";
276
277 InstrProfRecord Record11(caller, 0x1234, {1, 2});
278 InstrProfRecord Record12(caller, 0x1234, {1, 2});
279 InstrProfRecord Record2(callee1, 0x1235, {3, 4});
280 InstrProfRecord Record3(callee2, 0x1235, {3, 4});
281 InstrProfRecord Record4(callee3, 0x1235, {3, 4});
282 InstrProfRecord Record5(callee3, 0x1235, {3, 4});
283 InstrProfRecord Record6(callee4, 0x1235, {3, 5});
Xinliang David Li2004f002015-11-02 05:08:23 +0000284
285 // 5 value sites.
286 Record11.reserveSites(IPVK_IndirectCallTarget, 5);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000287 InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
288 {uint64_t(callee2), 2},
289 {uint64_t(callee3), 3},
290 {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000291 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000292
293 // No valeu profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000294 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000295
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000296 InstrProfValueData VD2[] = {{uint64_t(callee1), 1},
297 {uint64_t(callee2), 2},
298 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000299 Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000300
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000301 InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000302 Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000303
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000304 InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
305 {uint64_t(callee2), 2},
306 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000307 Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000308
309 // A differnt record for the same caller.
310 Record12.reserveSites(IPVK_IndirectCallTarget, 5);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000311 InstrProfValueData VD02[] = {{uint64_t(callee2), 5},
312 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000313 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000314
315 // No valeu profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000316 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000317
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000318 InstrProfValueData VD22[] = {{uint64_t(callee2), 1},
319 {uint64_t(callee3), 3},
320 {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000321 Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000322
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000323 Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000324
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000325 InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
326 {uint64_t(callee2), 2},
327 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000328 Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000329
330 Writer.addRecord(std::move(Record11));
331 // Merge profile data.
332 Writer.addRecord(std::move(Record12));
333
334 Writer.addRecord(std::move(Record2));
335 Writer.addRecord(std::move(Record3));
336 Writer.addRecord(std::move(Record4));
337 Writer.addRecord(std::move(Record5));
338 Writer.addRecord(std::move(Record6));
339 auto Profile = Writer.writeBuffer();
340 readProfile(std::move(Profile));
341
342 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
343 ASSERT_TRUE(NoError(R.getError()));
344 ASSERT_EQ(5U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
345 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
346 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
347 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
348 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
349 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
350
351 std::unique_ptr<InstrProfValueData[]> VD =
352 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li2004f002015-11-02 05:08:23 +0000353 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2"));
354 ASSERT_EQ(7U, VD[0].Count);
355 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3"));
356 ASSERT_EQ(6U, VD[1].Count);
357 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4"));
358 ASSERT_EQ(4U, VD[2].Count);
359 ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1"));
360 ASSERT_EQ(1U, VD[3].Count);
361
362 std::unique_ptr<InstrProfValueData[]> VD_2(
363 R.get().getValueForSite(IPVK_IndirectCallTarget, 2));
Xinliang David Li2004f002015-11-02 05:08:23 +0000364 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3"));
365 ASSERT_EQ(6U, VD_2[0].Count);
366 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4"));
367 ASSERT_EQ(4U, VD_2[1].Count);
368 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2"));
369 ASSERT_EQ(3U, VD_2[2].Count);
370 ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1"));
371 ASSERT_EQ(1U, VD_2[3].Count);
372
373 std::unique_ptr<InstrProfValueData[]> VD_3(
374 R.get().getValueForSite(IPVK_IndirectCallTarget, 3));
375 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1"));
376 ASSERT_EQ(1U, VD_3[0].Count);
377
378 std::unique_ptr<InstrProfValueData[]> VD_4(
379 R.get().getValueForSite(IPVK_IndirectCallTarget, 4));
Xinliang David Li2004f002015-11-02 05:08:23 +0000380 ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3"));
381 ASSERT_EQ(6U, VD_4[0].Count);
382 ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2"));
383 ASSERT_EQ(4U, VD_4[1].Count);
384 ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1"));
385 ASSERT_EQ(2U, VD_4[2].Count);
386}
387
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000388TEST_F(InstrProfTest, get_icall_data_merge1_saturation) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000389 static const char bar[] = "bar";
390
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000391 const uint64_t Max = std::numeric_limits<uint64_t>::max();
392
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000393 InstrProfRecord Record1("foo", 0x1234, {1});
394 auto Result1 = Writer.addRecord(std::move(Record1));
395 ASSERT_EQ(Result1, instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000396
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000397 // Verify counter overflow.
398 InstrProfRecord Record2("foo", 0x1234, {Max});
399 auto Result2 = Writer.addRecord(std::move(Record2));
400 ASSERT_EQ(Result2, instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000401
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000402 InstrProfRecord Record3(bar, 0x9012, {8});
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000403 auto Result3 = Writer.addRecord(std::move(Record3));
404 ASSERT_EQ(Result3, instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000405
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000406 InstrProfRecord Record4("baz", 0x5678, {3, 4});
407 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000408 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000409 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
410 auto Result4 = Writer.addRecord(std::move(Record4));
411 ASSERT_EQ(Result4, instrprof_error::success);
412
413 // Verify value data counter overflow.
414 InstrProfRecord Record5("baz", 0x5678, {5, 6});
415 Record5.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000416 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000417 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
418 auto Result5 = Writer.addRecord(std::move(Record5));
419 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000420
421 auto Profile = Writer.writeBuffer();
422 readProfile(std::move(Profile));
423
424 // Verify saturation of counts.
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000425 ErrorOr<InstrProfRecord> ReadRecord1 =
426 Reader->getInstrProfRecord("foo", 0x1234);
427 ASSERT_TRUE(NoError(ReadRecord1.getError()));
428 ASSERT_EQ(Max, ReadRecord1.get().Counts[0]);
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000429
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000430 ErrorOr<InstrProfRecord> ReadRecord2 =
431 Reader->getInstrProfRecord("baz", 0x5678);
432 ASSERT_EQ(1U, ReadRecord2.get().getNumValueSites(IPVK_IndirectCallTarget));
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000433 std::unique_ptr<InstrProfValueData[]> VD =
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000434 ReadRecord2.get().getValueForSite(IPVK_IndirectCallTarget, 0);
435 ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
436 ASSERT_EQ(Max, VD[0].Count);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000437}
438
Xinliang David Lied966772015-11-25 23:31:18 +0000439// Synthesize runtime value profile data.
440ValueProfNode Site1Values[5] = {{{uint64_t("callee1"), 400}, &Site1Values[1]},
441 {{uint64_t("callee2"), 1000}, &Site1Values[2]},
442 {{uint64_t("callee3"), 500}, &Site1Values[3]},
443 {{uint64_t("callee4"), 300}, &Site1Values[4]},
444 {{uint64_t("callee5"), 100}, 0}};
445
446ValueProfNode Site2Values[4] = {{{uint64_t("callee5"), 800}, &Site2Values[1]},
447 {{uint64_t("callee3"), 1000}, &Site2Values[2]},
448 {{uint64_t("callee2"), 2500}, &Site2Values[3]},
449 {{uint64_t("callee1"), 1300}, 0}};
450
451ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]},
452 {{uint64_t("callee3"), 1000}, &Site3Values[2]},
453 {{uint64_t("callee4"), 5500}, 0}};
454
455ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]},
456 {{uint64_t("callee3"), 2000}, 0}};
457
458static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0],
459 &Site3Values[0], &Site4Values[0], 0};
460static uint16_t NumValueSites[IPVK_Last + 1] = {5};
461TEST_F(InstrProfTest, runtime_value_prof_data_read_write) {
462 ValueProfRuntimeRecord RTRecord;
463 initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
464 &ValueProfNodes[0]);
465
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000466 ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr);
Xinliang David Lied966772015-11-25 23:31:18 +0000467
468 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
469
470 VPData->deserializeTo(Record, 0);
471
472 // Now read data from Record and sanity check the data
473 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
474 ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
475 ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
476 ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
477 ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
478 ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
479
480 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
481 return VD1.Count > VD2.Count;
482 };
483 std::unique_ptr<InstrProfValueData[]> VD_0(
484 Record.getValueForSite(IPVK_IndirectCallTarget, 0));
485 std::sort(&VD_0[0], &VD_0[5], Cmp);
486 ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2"));
487 ASSERT_EQ(1000U, VD_0[0].Count);
488 ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3"));
489 ASSERT_EQ(500U, VD_0[1].Count);
490 ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1"));
491 ASSERT_EQ(400U, VD_0[2].Count);
492 ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4"));
493 ASSERT_EQ(300U, VD_0[3].Count);
494 ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5"));
495 ASSERT_EQ(100U, VD_0[4].Count);
496
497 std::unique_ptr<InstrProfValueData[]> VD_1(
498 Record.getValueForSite(IPVK_IndirectCallTarget, 1));
499 std::sort(&VD_1[0], &VD_1[4], Cmp);
500 ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2"));
501 ASSERT_EQ(2500U, VD_1[0].Count);
502 ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1"));
503 ASSERT_EQ(1300U, VD_1[1].Count);
504 ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3"));
505 ASSERT_EQ(1000U, VD_1[2].Count);
506 ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5"));
507 ASSERT_EQ(800U, VD_1[3].Count);
508
509 std::unique_ptr<InstrProfValueData[]> VD_2(
510 Record.getValueForSite(IPVK_IndirectCallTarget, 2));
511 std::sort(&VD_2[0], &VD_2[3], Cmp);
512 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4"));
513 ASSERT_EQ(5500U, VD_2[0].Count);
514 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3"));
515 ASSERT_EQ(1000U, VD_2[1].Count);
516 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6"));
517 ASSERT_EQ(800U, VD_2[2].Count);
518
519 std::unique_ptr<InstrProfValueData[]> VD_3(
520 Record.getValueForSite(IPVK_IndirectCallTarget, 3));
521 std::sort(&VD_3[0], &VD_3[2], Cmp);
522 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3"));
523 ASSERT_EQ(2000U, VD_3[0].Count);
524 ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2"));
525 ASSERT_EQ(1800U, VD_3[1].Count);
526
527 finalizeValueProfRuntimeRecord(&RTRecord);
528 free(VPData);
529}
530
Justin Bogner2b6c5372015-02-18 01:58:17 +0000531TEST_F(InstrProfTest, get_max_function_count) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000532 InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2});
533 InstrProfRecord Record2("bar", 0, {1ULL << 63});
534 InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0});
535 Writer.addRecord(std::move(Record1));
536 Writer.addRecord(std::move(Record2));
537 Writer.addRecord(std::move(Record3));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000538 auto Profile = Writer.writeBuffer();
539 readProfile(std::move(Profile));
540
541 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
542}
543
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000544TEST_F(InstrProfTest, get_weighted_function_counts) {
545 InstrProfRecord Record1("foo", 0x1234, {1, 2});
546 InstrProfRecord Record2("foo", 0x1235, {3, 4});
547 Writer.addRecord(std::move(Record1), 3);
548 Writer.addRecord(std::move(Record2), 5);
549 auto Profile = Writer.writeBuffer();
550 readProfile(std::move(Profile));
551
552 std::vector<uint64_t> Counts;
553 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
554 ASSERT_EQ(2U, Counts.size());
555 ASSERT_EQ(3U, Counts[0]);
556 ASSERT_EQ(6U, Counts[1]);
557
558 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
559 ASSERT_EQ(2U, Counts.size());
560 ASSERT_EQ(15U, Counts[0]);
561 ASSERT_EQ(20U, Counts[1]);
562}
563
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000564TEST_F(InstrProfTest, instr_prof_symtab_test) {
565 std::vector<StringRef> FuncNames;
566 FuncNames.push_back("func1");
567 FuncNames.push_back("func2");
568 FuncNames.push_back("func3");
569 FuncNames.push_back("bar1");
570 FuncNames.push_back("bar2");
571 FuncNames.push_back("bar3");
572 InstrProfSymtab Symtab;
573 Symtab.create(FuncNames);
574 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
575 ASSERT_EQ(StringRef("func1"), R);
576 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
577 ASSERT_EQ(StringRef("func2"), R);
578 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
579 ASSERT_EQ(StringRef("func3"), R);
580 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
581 ASSERT_EQ(StringRef("bar1"), R);
582 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
583 ASSERT_EQ(StringRef("bar2"), R);
584 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
585 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Lic96d3d12015-12-19 18:20:09 +0000586
587 // Now incrementally update the symtab
588 Symtab.addFuncName("blah_1");
589 Symtab.addFuncName("blah_2");
590 Symtab.addFuncName("blah_3");
591 // Finalize it
592 Symtab.finalizeSymtab();
593
594 // Check again
595 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));
596 ASSERT_EQ(StringRef("blah_1"), R);
597 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2"));
598 ASSERT_EQ(StringRef("blah_2"), R);
599 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3"));
600 ASSERT_EQ(StringRef("blah_3"), R);
601 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
602 ASSERT_EQ(StringRef("func1"), R);
603 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
604 ASSERT_EQ(StringRef("func2"), R);
605 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
606 ASSERT_EQ(StringRef("func3"), R);
607 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
608 ASSERT_EQ(StringRef("bar1"), R);
609 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
610 ASSERT_EQ(StringRef("bar2"), R);
611 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
612 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000613}
614
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000615TEST_F(InstrProfTest, instr_prof_symtab_compression_test) {
616 std::vector<std::string> FuncNames1;
617 std::vector<std::string> FuncNames2;
618 for (int I = 0; I < 10 * 1024; I++) {
619 std::string str;
620 raw_string_ostream OS(str);
621 OS << "func_" << I;
622 FuncNames1.push_back(OS.str());
623 str.clear();
624 OS << "fooooooooooooooo_" << I;
625 FuncNames1.push_back(OS.str());
626 str.clear();
627 OS << "BAR_" << I;
628 FuncNames2.push_back(OS.str());
629 str.clear();
630 OS << "BlahblahBlahblahBar_" << I;
631 FuncNames2.push_back(OS.str());
632 }
633
634 for (int Padding = 0; Padding < 10; Padding++) {
635 for (int DoCompression = 0; DoCompression < 2; DoCompression++) {
636 // Compressing:
637 std::string FuncNameStrings1;
638 collectPGOFuncNameStrings(FuncNames1,
639 (DoCompression != 0 && zlib::isAvailable()),
640 FuncNameStrings1);
641
642 // Compressing:
643 std::string FuncNameStrings2;
644 collectPGOFuncNameStrings(FuncNames2,
645 (DoCompression != 0 && zlib::isAvailable()),
646 FuncNameStrings2);
647
648 // Join with paddings:
649 std::string FuncNameStrings = FuncNameStrings1;
650 for (int P = 0; P < Padding; P++) {
651 FuncNameStrings.push_back('\0');
652 }
653 FuncNameStrings += FuncNameStrings2;
654
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000655 // Now decompress:
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000656 InstrProfSymtab Symtab;
657 Symtab.create(StringRef(FuncNameStrings));
658
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000659 // Now do the checks:
660 // First sampling some data points:
661 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
662 ASSERT_EQ(StringRef("func_0"), R);
663 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
664 ASSERT_EQ(StringRef("fooooooooooooooo_0"), R);
665 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[998]));
666 ASSERT_EQ(StringRef("func_499"), R);
667 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[999]));
668 ASSERT_EQ(StringRef("fooooooooooooooo_499"), R);
669 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[100]));
670 ASSERT_EQ(StringRef("BAR_50"), R);
671 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[101]));
672 ASSERT_EQ(StringRef("BlahblahBlahblahBar_50"), R);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000673 for (int I = 0; I < 10 * 1024; I++) {
674 std::string N[4];
675 N[0] = FuncNames1[2 * I];
676 N[1] = FuncNames1[2 * I + 1];
677 N[2] = FuncNames2[2 * I];
678 N[3] = FuncNames2[2 * I + 1];
679 for (int J = 0; J < 4; J++) {
680 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J]));
681 ASSERT_EQ(StringRef(N[J]), R);
682 }
683 }
684 }
685 }
686}
687
Justin Bogner2b6c5372015-02-18 01:58:17 +0000688} // end anonymous namespace