1 //===- unittest/ProfileData/InstrProfTest.cpp -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/ProfileData/InstrProfReader.h"
11 #include "llvm/ProfileData/InstrProfWriter.h"
12 #include "llvm/Support/Compression.h"
13 #include "gtest/gtest.h"
19 static ::testing::AssertionResult NoError(std::error_code EC) {
21 return ::testing::AssertionSuccess();
22 return ::testing::AssertionFailure() << "error " << EC.value()
23 << ": " << EC.message();
26 static ::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();
36 struct InstrProfTest : ::testing::Test {
37 InstrProfWriter Writer;
38 std::unique_ptr<IndexedInstrProfReader> Reader;
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());
47 TEST_F(InstrProfTest, write_and_read_empty_profile) {
48 auto Profile = Writer.writeBuffer();
49 readProfile(std::move(Profile));
50 ASSERT_TRUE(Reader->begin() == Reader->end());
53 TEST_F(InstrProfTest, write_and_read_one_function) {
54 InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4});
55 Writer.addRecord(std::move(Record));
56 auto Profile = Writer.writeBuffer();
57 readProfile(std::move(Profile));
59 auto I = Reader->begin(), E = Reader->end();
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);
71 TEST_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));
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]);
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]);
91 R = Reader->getInstrProfRecord("foo", 0x5678);
92 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError()));
94 R = Reader->getInstrProfRecord("bar", 0x1234);
95 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError()));
98 TEST_F(InstrProfTest, get_function_counts) {
99 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));
103 auto Profile = Writer.writeBuffer();
104 readProfile(std::move(Profile));
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]);
112 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]);
118 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
119 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
121 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
122 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
125 TEST_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});
132 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
133 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
134 {(uint64_t) "callee2", 2},
135 {(uint64_t) "callee3", 3}};
136 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
137 // No value profile data at the second site.
138 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
139 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
140 {(uint64_t) "callee2", 2}};
141 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
142 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
143 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
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));
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));
160 std::unique_ptr<InstrProfValueData[]> VD =
161 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
162 // Now sort the target acording to frequency.
163 std::sort(&VD[0], &VD[3],
164 [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
165 return VD1.Count > VD2.Count;
168 ASSERT_EQ(3U, VD[0].Count);
169 ASSERT_EQ(2U, VD[1].Count);
170 ASSERT_EQ(1U, VD[2].Count);
172 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
173 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
174 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
177 TEST_F(InstrProfTest, get_icall_data_read_write_with_weight) {
178 InstrProfRecord Record1("caller", 0x1234, {1, 2});
179 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
180 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
181 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
184 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
185 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
186 {(uint64_t) "callee2", 2},
187 {(uint64_t) "callee3", 3}};
188 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
189 // No value profile data at the second site.
190 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
191 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
192 {(uint64_t) "callee2", 2}};
193 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
194 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
195 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
197 Writer.addRecord(std::move(Record1), 10);
198 Writer.addRecord(std::move(Record2));
199 Writer.addRecord(std::move(Record3));
200 Writer.addRecord(std::move(Record4));
201 auto Profile = Writer.writeBuffer();
202 readProfile(std::move(Profile));
204 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
205 ASSERT_TRUE(NoError(R.getError()));
206 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
207 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
208 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
209 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
210 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
212 std::unique_ptr<InstrProfValueData[]> VD =
213 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
214 // Now sort the target acording to frequency.
215 std::sort(&VD[0], &VD[3],
216 [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
217 return VD1.Count > VD2.Count;
219 ASSERT_EQ(30U, VD[0].Count);
220 ASSERT_EQ(20U, VD[1].Count);
221 ASSERT_EQ(10U, VD[2].Count);
223 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
224 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
225 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
228 TEST_F(InstrProfTest, get_icall_data_read_write_big_endian) {
229 InstrProfRecord Record1("caller", 0x1234, {1, 2});
230 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
231 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
232 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
235 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
236 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
237 {(uint64_t) "callee2", 2},
238 {(uint64_t) "callee3", 3}};
239 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
240 // No value profile data at the second site.
241 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
242 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
243 {(uint64_t) "callee2", 2}};
244 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
245 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
246 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
248 Writer.addRecord(std::move(Record1));
249 Writer.addRecord(std::move(Record2));
250 Writer.addRecord(std::move(Record3));
251 Writer.addRecord(std::move(Record4));
253 // Set big endian output.
254 Writer.setValueProfDataEndianness(support::big);
256 auto Profile = Writer.writeBuffer();
257 readProfile(std::move(Profile));
259 // Set big endian input.
260 Reader->setValueProfDataEndianness(support::big);
262 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
263 ASSERT_TRUE(NoError(R.getError()));
264 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
265 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
266 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
267 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
268 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
270 std::unique_ptr<InstrProfValueData[]> VD =
271 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
272 // Now sort the target acording to frequency.
273 std::sort(&VD[0], &VD[3],
274 [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
275 return VD1.Count > VD2.Count;
277 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
278 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
279 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
281 // Restore little endian default:
282 Writer.setValueProfDataEndianness(support::little);
285 TEST_F(InstrProfTest, get_icall_data_merge1) {
286 static const char caller[] = "caller";
287 static const char callee1[] = "callee1";
288 static const char callee2[] = "callee2";
289 static const char callee3[] = "callee3";
290 static const char callee4[] = "callee4";
292 InstrProfRecord Record11(caller, 0x1234, {1, 2});
293 InstrProfRecord Record12(caller, 0x1234, {1, 2});
294 InstrProfRecord Record2(callee1, 0x1235, {3, 4});
295 InstrProfRecord Record3(callee2, 0x1235, {3, 4});
296 InstrProfRecord Record4(callee3, 0x1235, {3, 4});
297 InstrProfRecord Record5(callee3, 0x1235, {3, 4});
298 InstrProfRecord Record6(callee4, 0x1235, {3, 5});
301 Record11.reserveSites(IPVK_IndirectCallTarget, 5);
302 InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
303 {uint64_t(callee2), 2},
304 {uint64_t(callee3), 3},
305 {uint64_t(callee4), 4}};
306 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
308 // No valeu profile data at the second site.
309 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
311 InstrProfValueData VD2[] = {{uint64_t(callee1), 1},
312 {uint64_t(callee2), 2},
313 {uint64_t(callee3), 3}};
314 Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
316 InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
317 Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
319 InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
320 {uint64_t(callee2), 2},
321 {uint64_t(callee3), 3}};
322 Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
324 // A differnt record for the same caller.
325 Record12.reserveSites(IPVK_IndirectCallTarget, 5);
326 InstrProfValueData VD02[] = {{uint64_t(callee2), 5},
327 {uint64_t(callee3), 3}};
328 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
330 // No valeu profile data at the second site.
331 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
333 InstrProfValueData VD22[] = {{uint64_t(callee2), 1},
334 {uint64_t(callee3), 3},
335 {uint64_t(callee4), 4}};
336 Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
338 Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
340 InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
341 {uint64_t(callee2), 2},
342 {uint64_t(callee3), 3}};
343 Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
345 Writer.addRecord(std::move(Record11));
346 // Merge profile data.
347 Writer.addRecord(std::move(Record12));
349 Writer.addRecord(std::move(Record2));
350 Writer.addRecord(std::move(Record3));
351 Writer.addRecord(std::move(Record4));
352 Writer.addRecord(std::move(Record5));
353 Writer.addRecord(std::move(Record6));
354 auto Profile = Writer.writeBuffer();
355 readProfile(std::move(Profile));
357 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
358 ASSERT_TRUE(NoError(R.getError()));
359 ASSERT_EQ(5U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
360 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
361 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
362 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
363 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
364 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
366 std::unique_ptr<InstrProfValueData[]> VD =
367 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
368 // Now sort the target acording to frequency.
369 std::sort(&VD[0], &VD[4],
370 [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
371 return VD1.Count > VD2.Count;
373 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2"));
374 ASSERT_EQ(7U, VD[0].Count);
375 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3"));
376 ASSERT_EQ(6U, VD[1].Count);
377 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4"));
378 ASSERT_EQ(4U, VD[2].Count);
379 ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1"));
380 ASSERT_EQ(1U, VD[3].Count);
382 std::unique_ptr<InstrProfValueData[]> VD_2(
383 R.get().getValueForSite(IPVK_IndirectCallTarget, 2));
384 std::sort(&VD_2[0], &VD_2[4],
385 [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
386 return VD1.Count > VD2.Count;
388 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3"));
389 ASSERT_EQ(6U, VD_2[0].Count);
390 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4"));
391 ASSERT_EQ(4U, VD_2[1].Count);
392 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2"));
393 ASSERT_EQ(3U, VD_2[2].Count);
394 ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1"));
395 ASSERT_EQ(1U, VD_2[3].Count);
397 std::unique_ptr<InstrProfValueData[]> VD_3(
398 R.get().getValueForSite(IPVK_IndirectCallTarget, 3));
399 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1"));
400 ASSERT_EQ(1U, VD_3[0].Count);
402 std::unique_ptr<InstrProfValueData[]> VD_4(
403 R.get().getValueForSite(IPVK_IndirectCallTarget, 4));
404 std::sort(&VD_4[0], &VD_4[3],
405 [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
406 return VD1.Count > VD2.Count;
408 ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3"));
409 ASSERT_EQ(6U, VD_4[0].Count);
410 ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2"));
411 ASSERT_EQ(4U, VD_4[1].Count);
412 ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1"));
413 ASSERT_EQ(2U, VD_4[2].Count);
416 TEST_F(InstrProfTest, get_icall_data_merge1_saturation) {
417 static const char bar[] = "bar";
419 const uint64_t Max = std::numeric_limits<uint64_t>::max();
421 InstrProfRecord Record1("foo", 0x1234, {1});
422 auto Result1 = Writer.addRecord(std::move(Record1));
423 ASSERT_EQ(Result1, instrprof_error::success);
425 // Verify counter overflow.
426 InstrProfRecord Record2("foo", 0x1234, {Max});
427 auto Result2 = Writer.addRecord(std::move(Record2));
428 ASSERT_EQ(Result2, instrprof_error::counter_overflow);
430 InstrProfRecord Record3(bar, 0x9012, {8});
431 auto Result3 = Writer.addRecord(std::move(Record3));
432 ASSERT_EQ(Result3, instrprof_error::success);
434 InstrProfRecord Record4("baz", 0x5678, {3, 4});
435 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
436 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
437 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
438 auto Result4 = Writer.addRecord(std::move(Record4));
439 ASSERT_EQ(Result4, instrprof_error::success);
441 // Verify value data counter overflow.
442 InstrProfRecord Record5("baz", 0x5678, {5, 6});
443 Record5.reserveSites(IPVK_IndirectCallTarget, 1);
444 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
445 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
446 auto Result5 = Writer.addRecord(std::move(Record5));
447 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
449 auto Profile = Writer.writeBuffer();
450 readProfile(std::move(Profile));
452 // Verify saturation of counts.
453 ErrorOr<InstrProfRecord> ReadRecord1 =
454 Reader->getInstrProfRecord("foo", 0x1234);
455 ASSERT_TRUE(NoError(ReadRecord1.getError()));
456 ASSERT_EQ(Max, ReadRecord1.get().Counts[0]);
458 ErrorOr<InstrProfRecord> ReadRecord2 =
459 Reader->getInstrProfRecord("baz", 0x5678);
460 ASSERT_EQ(1U, ReadRecord2.get().getNumValueSites(IPVK_IndirectCallTarget));
461 std::unique_ptr<InstrProfValueData[]> VD =
462 ReadRecord2.get().getValueForSite(IPVK_IndirectCallTarget, 0);
463 ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
464 ASSERT_EQ(Max, VD[0].Count);
467 // Synthesize runtime value profile data.
468 ValueProfNode Site1Values[5] = {{{uint64_t("callee1"), 400}, &Site1Values[1]},
469 {{uint64_t("callee2"), 1000}, &Site1Values[2]},
470 {{uint64_t("callee3"), 500}, &Site1Values[3]},
471 {{uint64_t("callee4"), 300}, &Site1Values[4]},
472 {{uint64_t("callee5"), 100}, 0}};
474 ValueProfNode Site2Values[4] = {{{uint64_t("callee5"), 800}, &Site2Values[1]},
475 {{uint64_t("callee3"), 1000}, &Site2Values[2]},
476 {{uint64_t("callee2"), 2500}, &Site2Values[3]},
477 {{uint64_t("callee1"), 1300}, 0}};
479 ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]},
480 {{uint64_t("callee3"), 1000}, &Site3Values[2]},
481 {{uint64_t("callee4"), 5500}, 0}};
483 ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]},
484 {{uint64_t("callee3"), 2000}, 0}};
486 static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0],
487 &Site3Values[0], &Site4Values[0], 0};
488 static uint16_t NumValueSites[IPVK_Last + 1] = {5};
489 TEST_F(InstrProfTest, runtime_value_prof_data_read_write) {
490 ValueProfRuntimeRecord RTRecord;
491 initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
494 ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr);
496 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
498 VPData->deserializeTo(Record, 0);
500 // Now read data from Record and sanity check the data
501 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
502 ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
503 ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
504 ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
505 ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
506 ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
508 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
509 return VD1.Count > VD2.Count;
511 std::unique_ptr<InstrProfValueData[]> VD_0(
512 Record.getValueForSite(IPVK_IndirectCallTarget, 0));
513 std::sort(&VD_0[0], &VD_0[5], Cmp);
514 ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2"));
515 ASSERT_EQ(1000U, VD_0[0].Count);
516 ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3"));
517 ASSERT_EQ(500U, VD_0[1].Count);
518 ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1"));
519 ASSERT_EQ(400U, VD_0[2].Count);
520 ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4"));
521 ASSERT_EQ(300U, VD_0[3].Count);
522 ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5"));
523 ASSERT_EQ(100U, VD_0[4].Count);
525 std::unique_ptr<InstrProfValueData[]> VD_1(
526 Record.getValueForSite(IPVK_IndirectCallTarget, 1));
527 std::sort(&VD_1[0], &VD_1[4], Cmp);
528 ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2"));
529 ASSERT_EQ(2500U, VD_1[0].Count);
530 ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1"));
531 ASSERT_EQ(1300U, VD_1[1].Count);
532 ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3"));
533 ASSERT_EQ(1000U, VD_1[2].Count);
534 ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5"));
535 ASSERT_EQ(800U, VD_1[3].Count);
537 std::unique_ptr<InstrProfValueData[]> VD_2(
538 Record.getValueForSite(IPVK_IndirectCallTarget, 2));
539 std::sort(&VD_2[0], &VD_2[3], Cmp);
540 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4"));
541 ASSERT_EQ(5500U, VD_2[0].Count);
542 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3"));
543 ASSERT_EQ(1000U, VD_2[1].Count);
544 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6"));
545 ASSERT_EQ(800U, VD_2[2].Count);
547 std::unique_ptr<InstrProfValueData[]> VD_3(
548 Record.getValueForSite(IPVK_IndirectCallTarget, 3));
549 std::sort(&VD_3[0], &VD_3[2], Cmp);
550 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3"));
551 ASSERT_EQ(2000U, VD_3[0].Count);
552 ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2"));
553 ASSERT_EQ(1800U, VD_3[1].Count);
555 finalizeValueProfRuntimeRecord(&RTRecord);
559 TEST_F(InstrProfTest, get_max_function_count) {
560 InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2});
561 InstrProfRecord Record2("bar", 0, {1ULL << 63});
562 InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0});
563 Writer.addRecord(std::move(Record1));
564 Writer.addRecord(std::move(Record2));
565 Writer.addRecord(std::move(Record3));
566 auto Profile = Writer.writeBuffer();
567 readProfile(std::move(Profile));
569 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
572 TEST_F(InstrProfTest, get_weighted_function_counts) {
573 InstrProfRecord Record1("foo", 0x1234, {1, 2});
574 InstrProfRecord Record2("foo", 0x1235, {3, 4});
575 Writer.addRecord(std::move(Record1), 3);
576 Writer.addRecord(std::move(Record2), 5);
577 auto Profile = Writer.writeBuffer();
578 readProfile(std::move(Profile));
580 std::vector<uint64_t> Counts;
581 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
582 ASSERT_EQ(2U, Counts.size());
583 ASSERT_EQ(3U, Counts[0]);
584 ASSERT_EQ(6U, Counts[1]);
586 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
587 ASSERT_EQ(2U, Counts.size());
588 ASSERT_EQ(15U, Counts[0]);
589 ASSERT_EQ(20U, Counts[1]);
592 TEST_F(InstrProfTest, instr_prof_symtab_test) {
593 std::vector<StringRef> FuncNames;
594 FuncNames.push_back("func1");
595 FuncNames.push_back("func2");
596 FuncNames.push_back("func3");
597 FuncNames.push_back("bar1");
598 FuncNames.push_back("bar2");
599 FuncNames.push_back("bar3");
600 InstrProfSymtab Symtab;
601 Symtab.create(FuncNames);
602 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
603 ASSERT_EQ(StringRef("func1"), R);
604 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
605 ASSERT_EQ(StringRef("func2"), R);
606 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
607 ASSERT_EQ(StringRef("func3"), R);
608 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
609 ASSERT_EQ(StringRef("bar1"), R);
610 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
611 ASSERT_EQ(StringRef("bar2"), R);
612 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
613 ASSERT_EQ(StringRef("bar3"), R);
615 // Now incrementally update the symtab
616 Symtab.addFuncName("blah_1");
617 Symtab.addFuncName("blah_2");
618 Symtab.addFuncName("blah_3");
620 Symtab.finalizeSymtab();
623 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));
624 ASSERT_EQ(StringRef("blah_1"), R);
625 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2"));
626 ASSERT_EQ(StringRef("blah_2"), R);
627 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3"));
628 ASSERT_EQ(StringRef("blah_3"), R);
629 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
630 ASSERT_EQ(StringRef("func1"), R);
631 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
632 ASSERT_EQ(StringRef("func2"), R);
633 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
634 ASSERT_EQ(StringRef("func3"), R);
635 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
636 ASSERT_EQ(StringRef("bar1"), R);
637 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
638 ASSERT_EQ(StringRef("bar2"), R);
639 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
640 ASSERT_EQ(StringRef("bar3"), R);
643 TEST_F(InstrProfTest, instr_prof_symtab_compression_test) {
644 std::vector<std::string> FuncNames1;
645 std::vector<std::string> FuncNames2;
646 for (int I = 0; I < 10 * 1024; I++) {
648 raw_string_ostream OS(str);
650 FuncNames1.push_back(OS.str());
652 OS << "fooooooooooooooo_" << I;
653 FuncNames1.push_back(OS.str());
656 FuncNames2.push_back(OS.str());
658 OS << "BlahblahBlahblahBar_" << I;
659 FuncNames2.push_back(OS.str());
662 for (int Padding = 0; Padding < 10; Padding++) {
663 for (int DoCompression = 0; DoCompression < 2; DoCompression++) {
665 std::string FuncNameStrings1;
666 collectPGOFuncNameStrings(FuncNames1,
667 (DoCompression != 0 && zlib::isAvailable()),
671 std::string FuncNameStrings2;
672 collectPGOFuncNameStrings(FuncNames2,
673 (DoCompression != 0 && zlib::isAvailable()),
676 // Join with paddings:
677 std::string FuncNameStrings = FuncNameStrings1;
678 for (int P = 0; P < Padding; P++) {
679 FuncNameStrings.push_back('\0');
681 FuncNameStrings += FuncNameStrings2;
684 InstrProfSymtab Symtab;
685 Symtab.create(StringRef(FuncNameStrings));
687 // Now do the checks:
688 // First sampling some data points:
689 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
690 ASSERT_EQ(StringRef("func_0"), R);
691 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
692 ASSERT_EQ(StringRef("fooooooooooooooo_0"), R);
693 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[998]));
694 ASSERT_EQ(StringRef("func_499"), R);
695 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[999]));
696 ASSERT_EQ(StringRef("fooooooooooooooo_499"), R);
697 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[100]));
698 ASSERT_EQ(StringRef("BAR_50"), R);
699 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[101]));
700 ASSERT_EQ(StringRef("BlahblahBlahblahBar_50"), R);
701 for (int I = 0; I < 10 * 1024; I++) {
703 N[0] = FuncNames1[2 * I];
704 N[1] = FuncNames1[2 * I + 1];
705 N[2] = FuncNames2[2 * I];
706 N[3] = FuncNames2[2 * I + 1];
707 for (int J = 0; J < 4; J++) {
708 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J]));
709 ASSERT_EQ(StringRef(N[J]), R);
716 } // end anonymous namespace