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);
163 ASSERT_EQ(3U, VD[0].Count);
164 ASSERT_EQ(2U, VD[1].Count);
165 ASSERT_EQ(1U, VD[2].Count);
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"));
172 TEST_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});
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);
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));
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));
207 std::unique_ptr<InstrProfValueData[]> VD =
208 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
209 ASSERT_EQ(30U, VD[0].Count);
210 ASSERT_EQ(20U, VD[1].Count);
211 ASSERT_EQ(10U, VD[2].Count);
213 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"));
218 TEST_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});
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);
238 Writer.addRecord(std::move(Record1));
239 Writer.addRecord(std::move(Record2));
240 Writer.addRecord(std::move(Record3));
241 Writer.addRecord(std::move(Record4));
243 // Set big endian output.
244 Writer.setValueProfDataEndianness(support::big);
246 auto Profile = Writer.writeBuffer();
247 readProfile(std::move(Profile));
249 // Set big endian input.
250 Reader->setValueProfDataEndianness(support::big);
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));
260 std::unique_ptr<InstrProfValueData[]> VD =
261 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
262 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"));
266 // Restore little endian default:
267 Writer.setValueProfDataEndianness(support::little);
270 TEST_F(InstrProfTest, get_icall_data_merge1) {
271 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";
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});
286 Record11.reserveSites(IPVK_IndirectCallTarget, 5);
287 InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
288 {uint64_t(callee2), 2},
289 {uint64_t(callee3), 3},
290 {uint64_t(callee4), 4}};
291 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
293 // No valeu profile data at the second site.
294 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
296 InstrProfValueData VD2[] = {{uint64_t(callee1), 1},
297 {uint64_t(callee2), 2},
298 {uint64_t(callee3), 3}};
299 Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
301 InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
302 Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
304 InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
305 {uint64_t(callee2), 2},
306 {uint64_t(callee3), 3}};
307 Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
309 // A differnt record for the same caller.
310 Record12.reserveSites(IPVK_IndirectCallTarget, 5);
311 InstrProfValueData VD02[] = {{uint64_t(callee2), 5},
312 {uint64_t(callee3), 3}};
313 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
315 // No valeu profile data at the second site.
316 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
318 InstrProfValueData VD22[] = {{uint64_t(callee2), 1},
319 {uint64_t(callee3), 3},
320 {uint64_t(callee4), 4}};
321 Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
323 Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
325 InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
326 {uint64_t(callee2), 2},
327 {uint64_t(callee3), 3}};
328 Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
330 Writer.addRecord(std::move(Record11));
331 // Merge profile data.
332 Writer.addRecord(std::move(Record12));
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));
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));
351 std::unique_ptr<InstrProfValueData[]> VD =
352 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
353 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);
362 std::unique_ptr<InstrProfValueData[]> VD_2(
363 R.get().getValueForSite(IPVK_IndirectCallTarget, 2));
364 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);
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);
378 std::unique_ptr<InstrProfValueData[]> VD_4(
379 R.get().getValueForSite(IPVK_IndirectCallTarget, 4));
380 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);
388 TEST_F(InstrProfTest, get_icall_data_merge1_saturation) {
389 static const char bar[] = "bar";
391 const uint64_t Max = std::numeric_limits<uint64_t>::max();
393 InstrProfRecord Record1("foo", 0x1234, {1});
394 auto Result1 = Writer.addRecord(std::move(Record1));
395 ASSERT_EQ(Result1, instrprof_error::success);
397 // 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);
402 InstrProfRecord Record3(bar, 0x9012, {8});
403 auto Result3 = Writer.addRecord(std::move(Record3));
404 ASSERT_EQ(Result3, instrprof_error::success);
406 InstrProfRecord Record4("baz", 0x5678, {3, 4});
407 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
408 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
409 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
410 auto Result4 = Writer.addRecord(std::move(Record4));
411 ASSERT_EQ(Result4, instrprof_error::success);
413 // Verify value data counter overflow.
414 InstrProfRecord Record5("baz", 0x5678, {5, 6});
415 Record5.reserveSites(IPVK_IndirectCallTarget, 1);
416 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
417 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
418 auto Result5 = Writer.addRecord(std::move(Record5));
419 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
421 auto Profile = Writer.writeBuffer();
422 readProfile(std::move(Profile));
424 // Verify saturation of counts.
425 ErrorOr<InstrProfRecord> ReadRecord1 =
426 Reader->getInstrProfRecord("foo", 0x1234);
427 ASSERT_TRUE(NoError(ReadRecord1.getError()));
428 ASSERT_EQ(Max, ReadRecord1.get().Counts[0]);
430 ErrorOr<InstrProfRecord> ReadRecord2 =
431 Reader->getInstrProfRecord("baz", 0x5678);
432 ASSERT_EQ(1U, ReadRecord2.get().getNumValueSites(IPVK_IndirectCallTarget));
433 std::unique_ptr<InstrProfValueData[]> VD =
434 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);
439 // Synthesize runtime value profile data.
440 ValueProfNode 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}};
446 ValueProfNode 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}};
451 ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]},
452 {{uint64_t("callee3"), 1000}, &Site3Values[2]},
453 {{uint64_t("callee4"), 5500}, 0}};
455 ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]},
456 {{uint64_t("callee3"), 2000}, 0}};
458 static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0],
459 &Site3Values[0], &Site4Values[0], 0};
460 static uint16_t NumValueSites[IPVK_Last + 1] = {5};
461 TEST_F(InstrProfTest, runtime_value_prof_data_read_write) {
462 ValueProfRuntimeRecord RTRecord;
463 initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
466 ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr);
468 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
470 VPData->deserializeTo(Record, 0);
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));
480 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
481 return VD1.Count > VD2.Count;
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);
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);
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);
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);
527 finalizeValueProfRuntimeRecord(&RTRecord);
531 TEST_F(InstrProfTest, get_max_function_count) {
532 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));
538 auto Profile = Writer.writeBuffer();
539 readProfile(std::move(Profile));
541 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
544 TEST_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));
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]);
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]);
564 TEST_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);
587 // Now incrementally update the symtab
588 Symtab.addFuncName("blah_1");
589 Symtab.addFuncName("blah_2");
590 Symtab.addFuncName("blah_3");
592 Symtab.finalizeSymtab();
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);
615 TEST_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++) {
620 raw_string_ostream OS(str);
622 FuncNames1.push_back(OS.str());
624 OS << "fooooooooooooooo_" << I;
625 FuncNames1.push_back(OS.str());
628 FuncNames2.push_back(OS.str());
630 OS << "BlahblahBlahblahBar_" << I;
631 FuncNames2.push_back(OS.str());
634 for (int Padding = 0; Padding < 10; Padding++) {
635 for (int DoCompression = 0; DoCompression < 2; DoCompression++) {
637 std::string FuncNameStrings1;
638 collectPGOFuncNameStrings(FuncNames1,
639 (DoCompression != 0 && zlib::isAvailable()),
643 std::string FuncNameStrings2;
644 collectPGOFuncNameStrings(FuncNames2,
645 (DoCompression != 0 && zlib::isAvailable()),
648 // Join with paddings:
649 std::string FuncNameStrings = FuncNameStrings1;
650 for (int P = 0; P < Padding; P++) {
651 FuncNameStrings.push_back('\0');
653 FuncNameStrings += FuncNameStrings2;
656 InstrProfSymtab Symtab;
657 Symtab.create(StringRef(FuncNameStrings));
659 // 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);
673 for (int I = 0; I < 10 * 1024; I++) {
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);
688 } // end anonymous namespace