blob: 01e3ddd0639922a4c617978cab58d19e82331e9a [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
Xinliang David Li59411db2016-01-20 01:26:34 +000010#include "llvm/IR/Function.h"
11#include "llvm/IR/LLVMContext.h"
12#include "llvm/IR/Module.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000013#include "llvm/ProfileData/InstrProfReader.h"
14#include "llvm/ProfileData/InstrProfWriter.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000015#include "llvm/Support/Compression.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000016#include "gtest/gtest.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000017#include <cstdarg>
18
19using namespace llvm;
20
21static ::testing::AssertionResult NoError(std::error_code EC) {
22 if (!EC)
23 return ::testing::AssertionSuccess();
24 return ::testing::AssertionFailure() << "error " << EC.value()
25 << ": " << EC.message();
26}
27
28static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
29 std::error_code Found) {
30 if (Expected == Found)
31 return ::testing::AssertionSuccess();
32 return ::testing::AssertionFailure() << "error " << Found.value()
33 << ": " << Found.message();
34}
35
36namespace {
37
38struct InstrProfTest : ::testing::Test {
39 InstrProfWriter Writer;
40 std::unique_ptr<IndexedInstrProfReader> Reader;
41
Vedant Kumar00dab222016-01-29 22:54:45 +000042 void SetUp() { Writer.setOutputSparse(false); }
43
Justin Bogner2b6c5372015-02-18 01:58:17 +000044 void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
45 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
46 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
47 Reader = std::move(ReaderOrErr.get());
48 }
49};
50
Vedant Kumar00dab222016-01-29 22:54:45 +000051struct SparseInstrProfTest : public InstrProfTest {
52 void SetUp() { Writer.setOutputSparse(true); }
53};
54
55struct MaybeSparseInstrProfTest : public InstrProfTest,
56 public ::testing::WithParamInterface<bool> {
57 void SetUp() {
58 Writer.setOutputSparse(GetParam());
59 }
60};
61
62TEST_P(MaybeSparseInstrProfTest, write_and_read_empty_profile) {
Justin Bogner2b6c5372015-02-18 01:58:17 +000063 auto Profile = Writer.writeBuffer();
64 readProfile(std::move(Profile));
65 ASSERT_TRUE(Reader->begin() == Reader->end());
66}
67
Vedant Kumar00dab222016-01-29 22:54:45 +000068TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) {
Justin Bogner9e9a0572015-09-29 22:13:58 +000069 InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4});
70 Writer.addRecord(std::move(Record));
Justin Bogner2b6c5372015-02-18 01:58:17 +000071 auto Profile = Writer.writeBuffer();
72 readProfile(std::move(Profile));
73
74 auto I = Reader->begin(), E = Reader->end();
75 ASSERT_TRUE(I != E);
76 ASSERT_EQ(StringRef("foo"), I->Name);
77 ASSERT_EQ(0x1234U, I->Hash);
78 ASSERT_EQ(4U, I->Counts.size());
79 ASSERT_EQ(1U, I->Counts[0]);
80 ASSERT_EQ(2U, I->Counts[1]);
81 ASSERT_EQ(3U, I->Counts[2]);
82 ASSERT_EQ(4U, I->Counts[3]);
83 ASSERT_TRUE(++I == E);
84}
85
Vedant Kumar00dab222016-01-29 22:54:45 +000086TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) {
Xinliang David Li2004f002015-11-02 05:08:23 +000087 InstrProfRecord Record1("foo", 0x1234, {1, 2});
88 InstrProfRecord Record2("foo", 0x1235, {3, 4});
89 Writer.addRecord(std::move(Record1));
90 Writer.addRecord(std::move(Record2));
91 auto Profile = Writer.writeBuffer();
92 readProfile(std::move(Profile));
93
94 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234);
95 ASSERT_TRUE(NoError(R.getError()));
96 ASSERT_EQ(2U, R.get().Counts.size());
97 ASSERT_EQ(1U, R.get().Counts[0]);
98 ASSERT_EQ(2U, R.get().Counts[1]);
99
100 R = Reader->getInstrProfRecord("foo", 0x1235);
101 ASSERT_TRUE(NoError(R.getError()));
102 ASSERT_EQ(2U, R.get().Counts.size());
103 ASSERT_EQ(3U, R.get().Counts[0]);
104 ASSERT_EQ(4U, R.get().Counts[1]);
105
106 R = Reader->getInstrProfRecord("foo", 0x5678);
107 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError()));
108
109 R = Reader->getInstrProfRecord("bar", 0x1234);
110 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError()));
111}
112
Vedant Kumar00dab222016-01-29 22:54:45 +0000113TEST_P(MaybeSparseInstrProfTest, get_function_counts) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000114 InstrProfRecord Record1("foo", 0x1234, {1, 2});
115 InstrProfRecord Record2("foo", 0x1235, {3, 4});
116 Writer.addRecord(std::move(Record1));
117 Writer.addRecord(std::move(Record2));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000118 auto Profile = Writer.writeBuffer();
119 readProfile(std::move(Profile));
120
121 std::vector<uint64_t> Counts;
122 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
123 ASSERT_EQ(2U, Counts.size());
124 ASSERT_EQ(1U, Counts[0]);
125 ASSERT_EQ(2U, Counts[1]);
126
Justin Bogner09829f42015-06-22 23:56:53 +0000127 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
128 ASSERT_EQ(2U, Counts.size());
129 ASSERT_EQ(3U, Counts[0]);
130 ASSERT_EQ(4U, Counts[1]);
131
Justin Bogner2b6c5372015-02-18 01:58:17 +0000132 std::error_code EC;
133 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
134 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
135
136 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
137 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
138}
139
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000140// Profile data is copied from general.proftext
141TEST_F(InstrProfTest, get_profile_summary) {
142 InstrProfRecord Record1("func1", 0x1234, {97531});
143 InstrProfRecord Record2("func2", 0x1234, {0, 0});
144 InstrProfRecord Record3("func3", 0x1234,
145 {2305843009213693952, 1152921504606846976,
146 576460752303423488, 288230376151711744,
147 144115188075855872, 72057594037927936});
148 InstrProfRecord Record4("func4", 0x1234, {0});
149 Writer.addRecord(std::move(Record1));
150 Writer.addRecord(std::move(Record2));
151 Writer.addRecord(std::move(Record3));
152 Writer.addRecord(std::move(Record4));
153 auto Profile = Writer.writeBuffer();
154 readProfile(std::move(Profile));
155
156 ProfileSummary &PS = Reader->getSummary();
157 ASSERT_EQ(2305843009213693952U, PS.getMaxFunctionCount());
158 ASSERT_EQ(2305843009213693952U, PS.getMaxBlockCount());
159 ASSERT_EQ(10U, PS.getNumBlocks());
160 ASSERT_EQ(4539628424389557499U, PS.getTotalCount());
161 std::vector<ProfileSummaryEntry> &Details = PS.getDetailedSummary();
162 uint32_t Cutoff = 800000;
163 auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
164 return PE.Cutoff == Cutoff;
165 };
166 auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);
167 Cutoff = 900000;
168 auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);
169 Cutoff = 950000;
170 auto NinetyFivePerc = std::find_if(Details.begin(), Details.end(), Predicate);
171 Cutoff = 990000;
172 auto NinetyNinePerc = std::find_if(Details.begin(), Details.end(), Predicate);
173 ASSERT_EQ(576460752303423488U, EightyPerc->MinBlockCount);
174 ASSERT_EQ(288230376151711744U, NinetyPerc->MinBlockCount);
175 ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinBlockCount);
176 ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinBlockCount);
177}
178
Vedant Kumar00dab222016-01-29 22:54:45 +0000179TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000180 InstrProfRecord Record1("caller", 0x1234, {1, 2});
181 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
182 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
183 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
184
185 // 4 value sites.
186 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
187 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
188 {(uint64_t) "callee2", 2},
189 {(uint64_t) "callee3", 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000190 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000191 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000192 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000193 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
194 {(uint64_t) "callee2", 2}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000195 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000196 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000197 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000198
199 Writer.addRecord(std::move(Record1));
200 Writer.addRecord(std::move(Record2));
201 Writer.addRecord(std::move(Record3));
202 Writer.addRecord(std::move(Record4));
203 auto Profile = Writer.writeBuffer();
204 readProfile(std::move(Profile));
205
206 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
207 ASSERT_TRUE(NoError(R.getError()));
208 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
209 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
210 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
211 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
212 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
213
214 std::unique_ptr<InstrProfValueData[]> VD =
215 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000216
217 ASSERT_EQ(3U, VD[0].Count);
218 ASSERT_EQ(2U, VD[1].Count);
219 ASSERT_EQ(1U, VD[2].Count);
220
221 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
222 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
223 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
224}
225
Vedant Kumar00dab222016-01-29 22:54:45 +0000226TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000227 InstrProfRecord Record1("caller", 0x1234, {1, 2});
228 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
229 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
230 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
231
232 // 4 value sites.
233 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
234 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
235 {(uint64_t) "callee2", 2},
236 {(uint64_t) "callee3", 3}};
237 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
238 // No value profile data at the second site.
239 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
240 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
241 {(uint64_t) "callee2", 2}};
242 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
243 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
244 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
245
246 Writer.addRecord(std::move(Record1), 10);
247 Writer.addRecord(std::move(Record2));
248 Writer.addRecord(std::move(Record3));
249 Writer.addRecord(std::move(Record4));
250 auto Profile = Writer.writeBuffer();
251 readProfile(std::move(Profile));
252
253 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
254 ASSERT_TRUE(NoError(R.getError()));
255 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
256 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
257 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
258 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
259 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
260
261 std::unique_ptr<InstrProfValueData[]> VD =
262 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000263 ASSERT_EQ(30U, VD[0].Count);
264 ASSERT_EQ(20U, VD[1].Count);
265 ASSERT_EQ(10U, VD[2].Count);
266
Xinliang David Li2004f002015-11-02 05:08:23 +0000267 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
268 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
269 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
270}
271
Vedant Kumar00dab222016-01-29 22:54:45 +0000272TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) {
Xinliang David Li46ad3632016-01-22 19:53:31 +0000273 InstrProfRecord Record1("caller", 0x1234, {1, 2});
274 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
275 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
276 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
277
278 // 4 value sites.
279 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
280 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
281 {(uint64_t) "callee2", 2},
282 {(uint64_t) "callee3", 3}};
283 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
284 // No value profile data at the second site.
285 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
286 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
287 {(uint64_t) "callee2", 2}};
288 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
289 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
290 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
291
292 Writer.addRecord(std::move(Record1));
293 Writer.addRecord(std::move(Record2));
294 Writer.addRecord(std::move(Record3));
295 Writer.addRecord(std::move(Record4));
296
297 // Set big endian output.
298 Writer.setValueProfDataEndianness(support::big);
299
300 auto Profile = Writer.writeBuffer();
301 readProfile(std::move(Profile));
302
303 // Set big endian input.
304 Reader->setValueProfDataEndianness(support::big);
305
306 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
307 ASSERT_TRUE(NoError(R.getError()));
308 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
309 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
310 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
311 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
312 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
313
314 std::unique_ptr<InstrProfValueData[]> VD =
315 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
316 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
317 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
318 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
319
320 // Restore little endian default:
321 Writer.setValueProfDataEndianness(support::little);
322}
323
Vedant Kumar00dab222016-01-29 22:54:45 +0000324TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000325 static const char caller[] = "caller";
326 static const char callee1[] = "callee1";
327 static const char callee2[] = "callee2";
328 static const char callee3[] = "callee3";
329 static const char callee4[] = "callee4";
330
331 InstrProfRecord Record11(caller, 0x1234, {1, 2});
332 InstrProfRecord Record12(caller, 0x1234, {1, 2});
333 InstrProfRecord Record2(callee1, 0x1235, {3, 4});
334 InstrProfRecord Record3(callee2, 0x1235, {3, 4});
335 InstrProfRecord Record4(callee3, 0x1235, {3, 4});
336 InstrProfRecord Record5(callee3, 0x1235, {3, 4});
337 InstrProfRecord Record6(callee4, 0x1235, {3, 5});
Xinliang David Li2004f002015-11-02 05:08:23 +0000338
339 // 5 value sites.
340 Record11.reserveSites(IPVK_IndirectCallTarget, 5);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000341 InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
342 {uint64_t(callee2), 2},
343 {uint64_t(callee3), 3},
344 {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000345 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000346
Xinliang David Li872df222016-01-08 06:54:27 +0000347 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000348 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000349
Xinliang David Li872df222016-01-08 06:54:27 +0000350 InstrProfValueData VD2[] = {
351 {uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000352 Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000353
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000354 InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000355 Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000356
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000357 InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
358 {uint64_t(callee2), 2},
359 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000360 Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000361
362 // A differnt record for the same caller.
363 Record12.reserveSites(IPVK_IndirectCallTarget, 5);
Xinliang David Li872df222016-01-08 06:54:27 +0000364 InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000365 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000366
Xinliang David Li872df222016-01-08 06:54:27 +0000367 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000368 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000369
Xinliang David Li872df222016-01-08 06:54:27 +0000370 InstrProfValueData VD22[] = {
371 {uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000372 Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000373
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000374 Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000375
Xinliang David Li46ad3632016-01-22 19:53:31 +0000376 InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
377 {uint64_t(callee2), 2},
378 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000379 Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000380
381 Writer.addRecord(std::move(Record11));
382 // Merge profile data.
383 Writer.addRecord(std::move(Record12));
384
385 Writer.addRecord(std::move(Record2));
386 Writer.addRecord(std::move(Record3));
387 Writer.addRecord(std::move(Record4));
388 Writer.addRecord(std::move(Record5));
389 Writer.addRecord(std::move(Record6));
390 auto Profile = Writer.writeBuffer();
391 readProfile(std::move(Profile));
392
393 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
394 ASSERT_TRUE(NoError(R.getError()));
395 ASSERT_EQ(5U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
396 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
397 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
398 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
399 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
400 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
401
402 std::unique_ptr<InstrProfValueData[]> VD =
403 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li2004f002015-11-02 05:08:23 +0000404 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2"));
405 ASSERT_EQ(7U, VD[0].Count);
406 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3"));
407 ASSERT_EQ(6U, VD[1].Count);
408 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4"));
409 ASSERT_EQ(4U, VD[2].Count);
410 ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1"));
411 ASSERT_EQ(1U, VD[3].Count);
412
413 std::unique_ptr<InstrProfValueData[]> VD_2(
414 R.get().getValueForSite(IPVK_IndirectCallTarget, 2));
Xinliang David Li2004f002015-11-02 05:08:23 +0000415 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3"));
416 ASSERT_EQ(6U, VD_2[0].Count);
417 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4"));
418 ASSERT_EQ(4U, VD_2[1].Count);
419 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2"));
420 ASSERT_EQ(3U, VD_2[2].Count);
421 ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1"));
422 ASSERT_EQ(1U, VD_2[3].Count);
423
424 std::unique_ptr<InstrProfValueData[]> VD_3(
425 R.get().getValueForSite(IPVK_IndirectCallTarget, 3));
426 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1"));
427 ASSERT_EQ(1U, VD_3[0].Count);
428
429 std::unique_ptr<InstrProfValueData[]> VD_4(
430 R.get().getValueForSite(IPVK_IndirectCallTarget, 4));
Xinliang David Li2004f002015-11-02 05:08:23 +0000431 ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3"));
432 ASSERT_EQ(6U, VD_4[0].Count);
433 ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2"));
434 ASSERT_EQ(4U, VD_4[1].Count);
435 ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1"));
436 ASSERT_EQ(2U, VD_4[2].Count);
437}
438
Vedant Kumar00dab222016-01-29 22:54:45 +0000439TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000440 static const char bar[] = "bar";
441
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000442 const uint64_t Max = std::numeric_limits<uint64_t>::max();
443
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000444 InstrProfRecord Record1("foo", 0x1234, {1});
445 auto Result1 = Writer.addRecord(std::move(Record1));
446 ASSERT_EQ(Result1, instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000447
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000448 // Verify counter overflow.
449 InstrProfRecord Record2("foo", 0x1234, {Max});
450 auto Result2 = Writer.addRecord(std::move(Record2));
451 ASSERT_EQ(Result2, instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000452
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000453 InstrProfRecord Record3(bar, 0x9012, {8});
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000454 auto Result3 = Writer.addRecord(std::move(Record3));
455 ASSERT_EQ(Result3, instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000456
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000457 InstrProfRecord Record4("baz", 0x5678, {3, 4});
458 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000459 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000460 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
461 auto Result4 = Writer.addRecord(std::move(Record4));
462 ASSERT_EQ(Result4, instrprof_error::success);
463
464 // Verify value data counter overflow.
465 InstrProfRecord Record5("baz", 0x5678, {5, 6});
466 Record5.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000467 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000468 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
469 auto Result5 = Writer.addRecord(std::move(Record5));
470 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000471
472 auto Profile = Writer.writeBuffer();
473 readProfile(std::move(Profile));
474
475 // Verify saturation of counts.
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000476 ErrorOr<InstrProfRecord> ReadRecord1 =
477 Reader->getInstrProfRecord("foo", 0x1234);
478 ASSERT_TRUE(NoError(ReadRecord1.getError()));
479 ASSERT_EQ(Max, ReadRecord1.get().Counts[0]);
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000480
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000481 ErrorOr<InstrProfRecord> ReadRecord2 =
482 Reader->getInstrProfRecord("baz", 0x5678);
483 ASSERT_EQ(1U, ReadRecord2.get().getNumValueSites(IPVK_IndirectCallTarget));
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000484 std::unique_ptr<InstrProfValueData[]> VD =
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000485 ReadRecord2.get().getValueForSite(IPVK_IndirectCallTarget, 0);
486 ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
487 ASSERT_EQ(Max, VD[0].Count);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000488}
489
Xinliang David Li872df222016-01-08 06:54:27 +0000490// This test tests that when there are too many values
491// for a given site, the merged results are properly
492// truncated.
Vedant Kumar00dab222016-01-29 22:54:45 +0000493TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
Xinliang David Li872df222016-01-08 06:54:27 +0000494 static const char caller[] = "caller";
495
496 InstrProfRecord Record11(caller, 0x1234, {1, 2});
497 InstrProfRecord Record12(caller, 0x1234, {1, 2});
498
499 // 2 value sites.
500 Record11.reserveSites(IPVK_IndirectCallTarget, 2);
501 InstrProfValueData VD0[255];
502 for (int I = 0; I < 255; I++) {
503 VD0[I].Value = 2 * I;
504 VD0[I].Count = 2 * I + 1000;
505 }
506
507 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr);
508 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
509
510 Record12.reserveSites(IPVK_IndirectCallTarget, 2);
511 InstrProfValueData VD1[255];
512 for (int I = 0; I < 255; I++) {
513 VD1[I].Value = 2 * I + 1;
514 VD1[I].Count = 2 * I + 1001;
515 }
516
517 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr);
518 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
519
520 Writer.addRecord(std::move(Record11));
521 // Merge profile data.
522 Writer.addRecord(std::move(Record12));
523
524 auto Profile = Writer.writeBuffer();
525 readProfile(std::move(Profile));
526
527 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
528 ASSERT_TRUE(NoError(R.getError()));
529 std::unique_ptr<InstrProfValueData[]> VD(
530 R.get().getValueForSite(IPVK_IndirectCallTarget, 0));
531 ASSERT_EQ(2U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
532 ASSERT_EQ(255U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
NAKAMURA Takumi249edf32016-01-08 07:58:20 +0000533 for (unsigned I = 0; I < 255; I++) {
Xinliang David Li872df222016-01-08 06:54:27 +0000534 ASSERT_EQ(VD[I].Value, 509 - I);
535 ASSERT_EQ(VD[I].Count, 1509 - I);
536 }
537}
538
Xinliang David Lied966772015-11-25 23:31:18 +0000539// Synthesize runtime value profile data.
540ValueProfNode Site1Values[5] = {{{uint64_t("callee1"), 400}, &Site1Values[1]},
541 {{uint64_t("callee2"), 1000}, &Site1Values[2]},
542 {{uint64_t("callee3"), 500}, &Site1Values[3]},
543 {{uint64_t("callee4"), 300}, &Site1Values[4]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000544 {{uint64_t("callee5"), 100}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000545
546ValueProfNode Site2Values[4] = {{{uint64_t("callee5"), 800}, &Site2Values[1]},
547 {{uint64_t("callee3"), 1000}, &Site2Values[2]},
548 {{uint64_t("callee2"), 2500}, &Site2Values[3]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000549 {{uint64_t("callee1"), 1300}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000550
551ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]},
552 {{uint64_t("callee3"), 1000}, &Site3Values[2]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000553 {{uint64_t("callee4"), 5500}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000554
555ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000556 {{uint64_t("callee3"), 2000}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000557
558static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0],
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000559 &Site3Values[0], &Site4Values[0],
560 nullptr};
561
Xinliang David Lied966772015-11-25 23:31:18 +0000562static uint16_t NumValueSites[IPVK_Last + 1] = {5};
Vedant Kumar00dab222016-01-29 22:54:45 +0000563TEST_P(MaybeSparseInstrProfTest, runtime_value_prof_data_read_write) {
Xinliang David Lied966772015-11-25 23:31:18 +0000564 ValueProfRuntimeRecord RTRecord;
565 initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
566 &ValueProfNodes[0]);
567
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000568 ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr);
Xinliang David Lied966772015-11-25 23:31:18 +0000569
570 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
571
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000572 VPData->deserializeTo(Record, nullptr);
Xinliang David Lied966772015-11-25 23:31:18 +0000573
574 // Now read data from Record and sanity check the data
575 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
576 ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
577 ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
578 ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
579 ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
580 ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
581
582 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
583 return VD1.Count > VD2.Count;
584 };
585 std::unique_ptr<InstrProfValueData[]> VD_0(
586 Record.getValueForSite(IPVK_IndirectCallTarget, 0));
587 std::sort(&VD_0[0], &VD_0[5], Cmp);
588 ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2"));
589 ASSERT_EQ(1000U, VD_0[0].Count);
590 ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3"));
591 ASSERT_EQ(500U, VD_0[1].Count);
592 ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1"));
593 ASSERT_EQ(400U, VD_0[2].Count);
594 ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4"));
595 ASSERT_EQ(300U, VD_0[3].Count);
596 ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5"));
597 ASSERT_EQ(100U, VD_0[4].Count);
598
599 std::unique_ptr<InstrProfValueData[]> VD_1(
600 Record.getValueForSite(IPVK_IndirectCallTarget, 1));
601 std::sort(&VD_1[0], &VD_1[4], Cmp);
602 ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2"));
603 ASSERT_EQ(2500U, VD_1[0].Count);
604 ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1"));
605 ASSERT_EQ(1300U, VD_1[1].Count);
606 ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3"));
607 ASSERT_EQ(1000U, VD_1[2].Count);
608 ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5"));
609 ASSERT_EQ(800U, VD_1[3].Count);
610
611 std::unique_ptr<InstrProfValueData[]> VD_2(
612 Record.getValueForSite(IPVK_IndirectCallTarget, 2));
613 std::sort(&VD_2[0], &VD_2[3], Cmp);
614 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4"));
615 ASSERT_EQ(5500U, VD_2[0].Count);
616 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3"));
617 ASSERT_EQ(1000U, VD_2[1].Count);
618 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6"));
619 ASSERT_EQ(800U, VD_2[2].Count);
620
621 std::unique_ptr<InstrProfValueData[]> VD_3(
622 Record.getValueForSite(IPVK_IndirectCallTarget, 3));
623 std::sort(&VD_3[0], &VD_3[2], Cmp);
624 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3"));
625 ASSERT_EQ(2000U, VD_3[0].Count);
626 ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2"));
627 ASSERT_EQ(1800U, VD_3[1].Count);
628
629 finalizeValueProfRuntimeRecord(&RTRecord);
630 free(VPData);
631}
632
Vedant Kumar00dab222016-01-29 22:54:45 +0000633TEST_P(MaybeSparseInstrProfTest, get_max_function_count) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000634 InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2});
635 InstrProfRecord Record2("bar", 0, {1ULL << 63});
636 InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0});
637 Writer.addRecord(std::move(Record1));
638 Writer.addRecord(std::move(Record2));
639 Writer.addRecord(std::move(Record3));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000640 auto Profile = Writer.writeBuffer();
641 readProfile(std::move(Profile));
642
643 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
644}
645
Vedant Kumar00dab222016-01-29 22:54:45 +0000646TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) {
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000647 InstrProfRecord Record1("foo", 0x1234, {1, 2});
648 InstrProfRecord Record2("foo", 0x1235, {3, 4});
649 Writer.addRecord(std::move(Record1), 3);
650 Writer.addRecord(std::move(Record2), 5);
651 auto Profile = Writer.writeBuffer();
652 readProfile(std::move(Profile));
653
654 std::vector<uint64_t> Counts;
655 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
656 ASSERT_EQ(2U, Counts.size());
657 ASSERT_EQ(3U, Counts[0]);
658 ASSERT_EQ(6U, Counts[1]);
659
660 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
661 ASSERT_EQ(2U, Counts.size());
662 ASSERT_EQ(15U, Counts[0]);
663 ASSERT_EQ(20U, Counts[1]);
664}
665
Vedant Kumar00dab222016-01-29 22:54:45 +0000666TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) {
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000667 std::vector<StringRef> FuncNames;
668 FuncNames.push_back("func1");
669 FuncNames.push_back("func2");
670 FuncNames.push_back("func3");
671 FuncNames.push_back("bar1");
672 FuncNames.push_back("bar2");
673 FuncNames.push_back("bar3");
674 InstrProfSymtab Symtab;
675 Symtab.create(FuncNames);
676 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
677 ASSERT_EQ(StringRef("func1"), R);
678 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
679 ASSERT_EQ(StringRef("func2"), R);
680 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
681 ASSERT_EQ(StringRef("func3"), R);
682 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
683 ASSERT_EQ(StringRef("bar1"), R);
684 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
685 ASSERT_EQ(StringRef("bar2"), R);
686 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
687 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Lic96d3d12015-12-19 18:20:09 +0000688
689 // Now incrementally update the symtab
690 Symtab.addFuncName("blah_1");
691 Symtab.addFuncName("blah_2");
692 Symtab.addFuncName("blah_3");
693 // Finalize it
694 Symtab.finalizeSymtab();
695
696 // Check again
697 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));
698 ASSERT_EQ(StringRef("blah_1"), R);
699 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2"));
700 ASSERT_EQ(StringRef("blah_2"), R);
701 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3"));
702 ASSERT_EQ(StringRef("blah_3"), R);
703 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
704 ASSERT_EQ(StringRef("func1"), R);
705 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
706 ASSERT_EQ(StringRef("func2"), R);
707 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
708 ASSERT_EQ(StringRef("func3"), R);
709 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
710 ASSERT_EQ(StringRef("bar1"), R);
711 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
712 ASSERT_EQ(StringRef("bar2"), R);
713 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
714 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000715}
716
Vedant Kumar00dab222016-01-29 22:54:45 +0000717TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) {
Xinliang David Li59411db2016-01-20 01:26:34 +0000718 LLVMContext Ctx;
719 std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx);
720 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
721 /*isVarArg=*/false);
722 Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get());
723 Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get());
724 Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get());
725 Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get());
726 Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get());
727 Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get());
728 Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get());
729 Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get());
730 Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get());
731 Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get());
732 Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get());
733 Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get());
734
735 InstrProfSymtab ProfSymtab;
736 ProfSymtab.create(*(M.get()));
737
738 StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar",
739 "Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"};
740
741 for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) {
742 Function *F = M->getFunction(Funcs[I]);
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000743 ASSERT_TRUE(F != nullptr);
Xinliang David Lida656fe2016-01-20 02:49:53 +0000744 std::string PGOName = getPGOFuncName(*F);
Xinliang David Li3865fdc2016-01-22 18:13:34 +0000745 uint64_t Key = IndexedInstrProf::ComputeHash(PGOName);
Xinliang David Lida656fe2016-01-20 02:49:53 +0000746 ASSERT_EQ(StringRef(PGOName),
Xinliang David Li3865fdc2016-01-22 18:13:34 +0000747 ProfSymtab.getFuncName(Key));
748 ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key));
Xinliang David Li59411db2016-01-20 01:26:34 +0000749 }
750}
751
Vedant Kumar00dab222016-01-29 22:54:45 +0000752TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000753 std::vector<std::string> FuncNames1;
754 std::vector<std::string> FuncNames2;
Xinliang David Life28ccc2016-01-30 01:37:32 +0000755 for (int I = 0; I < 128; I++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000756 std::string str;
757 raw_string_ostream OS(str);
758 OS << "func_" << I;
759 FuncNames1.push_back(OS.str());
760 str.clear();
761 OS << "fooooooooooooooo_" << I;
762 FuncNames1.push_back(OS.str());
763 str.clear();
764 OS << "BAR_" << I;
765 FuncNames2.push_back(OS.str());
766 str.clear();
767 OS << "BlahblahBlahblahBar_" << I;
768 FuncNames2.push_back(OS.str());
769 }
770
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000771 for (bool DoCompression : {false, true}) {
772 // Compressing:
773 std::string FuncNameStrings1;
774 collectPGOFuncNameStrings(
775 FuncNames1, (DoCompression && zlib::isAvailable()), FuncNameStrings1);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000776
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000777 // Compressing:
778 std::string FuncNameStrings2;
779 collectPGOFuncNameStrings(
780 FuncNames2, (DoCompression && zlib::isAvailable()), FuncNameStrings2);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000781
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000782 for (int Padding = 0; Padding < 3; Padding++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000783 // Join with paddings:
784 std::string FuncNameStrings = FuncNameStrings1;
785 for (int P = 0; P < Padding; P++) {
786 FuncNameStrings.push_back('\0');
787 }
788 FuncNameStrings += FuncNameStrings2;
789
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000790 // Now decompress:
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000791 InstrProfSymtab Symtab;
792 Symtab.create(StringRef(FuncNameStrings));
793
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000794 // Now do the checks:
795 // First sampling some data points:
Xinliang David Lia8ba7af2016-01-29 21:26:31 +0000796 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000797 ASSERT_EQ(StringRef("func_0"), R);
798 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
799 ASSERT_EQ(StringRef("fooooooooooooooo_0"), R);
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000800 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[100]));
801 ASSERT_EQ(StringRef("BAR_50"), R);
802 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[101]));
803 ASSERT_EQ(StringRef("BlahblahBlahblahBar_50"), R);
Xinliang David Life28ccc2016-01-30 01:37:32 +0000804 for (int I = 0; I < 128; I++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000805 std::string N[4];
806 N[0] = FuncNames1[2 * I];
807 N[1] = FuncNames1[2 * I + 1];
808 N[2] = FuncNames2[2 * I];
809 N[3] = FuncNames2[2 * I + 1];
810 for (int J = 0; J < 4; J++) {
811 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J]));
812 ASSERT_EQ(StringRef(N[J]), R);
813 }
814 }
815 }
816 }
817}
818
Vedant Kumar00dab222016-01-29 22:54:45 +0000819TEST_F(SparseInstrProfTest, preserve_no_records) {
820 InstrProfRecord Record1("foo", 0x1234, {0});
821 InstrProfRecord Record2("bar", 0x4321, {0, 0});
822 InstrProfRecord Record3("bar", 0x4321, {0, 0, 0});
823
824 Writer.addRecord(std::move(Record1));
825 Writer.addRecord(std::move(Record2));
826 Writer.addRecord(std::move(Record3));
827 auto Profile = Writer.writeBuffer();
828 readProfile(std::move(Profile));
829
830 auto I = Reader->begin(), E = Reader->end();
831 ASSERT_TRUE(I == E);
832}
833
834INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest,
835 ::testing::Bool());
836
Justin Bogner2b6c5372015-02-18 01:58:17 +0000837} // end anonymous namespace