blob: 3a881a5c9062fee99b22e89f90f2519491ade9a4 [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
Vedant Kumar00dab222016-01-29 22:54:45 +0000140TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000141 InstrProfRecord Record1("caller", 0x1234, {1, 2});
142 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
143 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
144 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
145
146 // 4 value sites.
147 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
148 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
149 {(uint64_t) "callee2", 2},
150 {(uint64_t) "callee3", 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000151 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000152 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000153 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000154 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
155 {(uint64_t) "callee2", 2}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000156 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000157 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000158 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000159
160 Writer.addRecord(std::move(Record1));
161 Writer.addRecord(std::move(Record2));
162 Writer.addRecord(std::move(Record3));
163 Writer.addRecord(std::move(Record4));
164 auto Profile = Writer.writeBuffer();
165 readProfile(std::move(Profile));
166
167 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
168 ASSERT_TRUE(NoError(R.getError()));
169 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
170 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
171 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
172 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
173 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
174
175 std::unique_ptr<InstrProfValueData[]> VD =
176 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000177
178 ASSERT_EQ(3U, VD[0].Count);
179 ASSERT_EQ(2U, VD[1].Count);
180 ASSERT_EQ(1U, VD[2].Count);
181
182 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
183 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
184 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
185}
186
Vedant Kumar00dab222016-01-29 22:54:45 +0000187TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000188 InstrProfRecord Record1("caller", 0x1234, {1, 2});
189 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
190 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
191 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
192
193 // 4 value sites.
194 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
195 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
196 {(uint64_t) "callee2", 2},
197 {(uint64_t) "callee3", 3}};
198 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
199 // No value profile data at the second site.
200 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
201 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
202 {(uint64_t) "callee2", 2}};
203 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
204 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
205 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
206
207 Writer.addRecord(std::move(Record1), 10);
208 Writer.addRecord(std::move(Record2));
209 Writer.addRecord(std::move(Record3));
210 Writer.addRecord(std::move(Record4));
211 auto Profile = Writer.writeBuffer();
212 readProfile(std::move(Profile));
213
214 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
215 ASSERT_TRUE(NoError(R.getError()));
216 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
217 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
218 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
219 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
220 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
221
222 std::unique_ptr<InstrProfValueData[]> VD =
223 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000224 ASSERT_EQ(30U, VD[0].Count);
225 ASSERT_EQ(20U, VD[1].Count);
226 ASSERT_EQ(10U, VD[2].Count);
227
Xinliang David Li2004f002015-11-02 05:08:23 +0000228 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
229 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
230 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
231}
232
Vedant Kumar00dab222016-01-29 22:54:45 +0000233TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) {
Xinliang David Li46ad3632016-01-22 19:53:31 +0000234 InstrProfRecord Record1("caller", 0x1234, {1, 2});
235 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
236 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
237 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
238
239 // 4 value sites.
240 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
241 InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1},
242 {(uint64_t) "callee2", 2},
243 {(uint64_t) "callee3", 3}};
244 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
245 // No value profile data at the second site.
246 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
247 InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1},
248 {(uint64_t) "callee2", 2}};
249 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
250 InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}};
251 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
252
253 Writer.addRecord(std::move(Record1));
254 Writer.addRecord(std::move(Record2));
255 Writer.addRecord(std::move(Record3));
256 Writer.addRecord(std::move(Record4));
257
258 // Set big endian output.
259 Writer.setValueProfDataEndianness(support::big);
260
261 auto Profile = Writer.writeBuffer();
262 readProfile(std::move(Profile));
263
264 // Set big endian input.
265 Reader->setValueProfDataEndianness(support::big);
266
267 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
268 ASSERT_TRUE(NoError(R.getError()));
269 ASSERT_EQ(4U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
270 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
271 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
272 ASSERT_EQ(2U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
273 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
274
275 std::unique_ptr<InstrProfValueData[]> VD =
276 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
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"));
280
281 // Restore little endian default:
282 Writer.setValueProfDataEndianness(support::little);
283}
284
Vedant Kumar00dab222016-01-29 22:54:45 +0000285TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000286 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";
291
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});
Xinliang David Li2004f002015-11-02 05:08:23 +0000299
300 // 5 value sites.
301 Record11.reserveSites(IPVK_IndirectCallTarget, 5);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000302 InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
303 {uint64_t(callee2), 2},
304 {uint64_t(callee3), 3},
305 {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000306 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000307
Xinliang David Li872df222016-01-08 06:54:27 +0000308 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000309 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000310
Xinliang David Li872df222016-01-08 06:54:27 +0000311 InstrProfValueData VD2[] = {
312 {uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000313 Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000314
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000315 InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000316 Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000317
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000318 InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
319 {uint64_t(callee2), 2},
320 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000321 Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000322
323 // A differnt record for the same caller.
324 Record12.reserveSites(IPVK_IndirectCallTarget, 5);
Xinliang David Li872df222016-01-08 06:54:27 +0000325 InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000326 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000327
Xinliang David Li872df222016-01-08 06:54:27 +0000328 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000329 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000330
Xinliang David Li872df222016-01-08 06:54:27 +0000331 InstrProfValueData VD22[] = {
332 {uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000333 Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000334
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000335 Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000336
Xinliang David Li46ad3632016-01-22 19:53:31 +0000337 InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
338 {uint64_t(callee2), 2},
339 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000340 Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000341
342 Writer.addRecord(std::move(Record11));
343 // Merge profile data.
344 Writer.addRecord(std::move(Record12));
345
346 Writer.addRecord(std::move(Record2));
347 Writer.addRecord(std::move(Record3));
348 Writer.addRecord(std::move(Record4));
349 Writer.addRecord(std::move(Record5));
350 Writer.addRecord(std::move(Record6));
351 auto Profile = Writer.writeBuffer();
352 readProfile(std::move(Profile));
353
354 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
355 ASSERT_TRUE(NoError(R.getError()));
356 ASSERT_EQ(5U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
357 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
358 ASSERT_EQ(0U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
359 ASSERT_EQ(4U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
360 ASSERT_EQ(1U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
361 ASSERT_EQ(3U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
362
363 std::unique_ptr<InstrProfValueData[]> VD =
364 R.get().getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li2004f002015-11-02 05:08:23 +0000365 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2"));
366 ASSERT_EQ(7U, VD[0].Count);
367 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3"));
368 ASSERT_EQ(6U, VD[1].Count);
369 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4"));
370 ASSERT_EQ(4U, VD[2].Count);
371 ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1"));
372 ASSERT_EQ(1U, VD[3].Count);
373
374 std::unique_ptr<InstrProfValueData[]> VD_2(
375 R.get().getValueForSite(IPVK_IndirectCallTarget, 2));
Xinliang David Li2004f002015-11-02 05:08:23 +0000376 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3"));
377 ASSERT_EQ(6U, VD_2[0].Count);
378 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4"));
379 ASSERT_EQ(4U, VD_2[1].Count);
380 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2"));
381 ASSERT_EQ(3U, VD_2[2].Count);
382 ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1"));
383 ASSERT_EQ(1U, VD_2[3].Count);
384
385 std::unique_ptr<InstrProfValueData[]> VD_3(
386 R.get().getValueForSite(IPVK_IndirectCallTarget, 3));
387 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1"));
388 ASSERT_EQ(1U, VD_3[0].Count);
389
390 std::unique_ptr<InstrProfValueData[]> VD_4(
391 R.get().getValueForSite(IPVK_IndirectCallTarget, 4));
Xinliang David Li2004f002015-11-02 05:08:23 +0000392 ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3"));
393 ASSERT_EQ(6U, VD_4[0].Count);
394 ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2"));
395 ASSERT_EQ(4U, VD_4[1].Count);
396 ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1"));
397 ASSERT_EQ(2U, VD_4[2].Count);
398}
399
Vedant Kumar00dab222016-01-29 22:54:45 +0000400TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000401 static const char bar[] = "bar";
402
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000403 const uint64_t Max = std::numeric_limits<uint64_t>::max();
404
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000405 InstrProfRecord Record1("foo", 0x1234, {1});
406 auto Result1 = Writer.addRecord(std::move(Record1));
407 ASSERT_EQ(Result1, instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000408
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000409 // Verify counter overflow.
410 InstrProfRecord Record2("foo", 0x1234, {Max});
411 auto Result2 = Writer.addRecord(std::move(Record2));
412 ASSERT_EQ(Result2, instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000413
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000414 InstrProfRecord Record3(bar, 0x9012, {8});
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000415 auto Result3 = Writer.addRecord(std::move(Record3));
416 ASSERT_EQ(Result3, instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000417
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000418 InstrProfRecord Record4("baz", 0x5678, {3, 4});
419 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000420 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000421 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
422 auto Result4 = Writer.addRecord(std::move(Record4));
423 ASSERT_EQ(Result4, instrprof_error::success);
424
425 // Verify value data counter overflow.
426 InstrProfRecord Record5("baz", 0x5678, {5, 6});
427 Record5.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000428 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000429 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
430 auto Result5 = Writer.addRecord(std::move(Record5));
431 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000432
433 auto Profile = Writer.writeBuffer();
434 readProfile(std::move(Profile));
435
436 // Verify saturation of counts.
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000437 ErrorOr<InstrProfRecord> ReadRecord1 =
438 Reader->getInstrProfRecord("foo", 0x1234);
439 ASSERT_TRUE(NoError(ReadRecord1.getError()));
440 ASSERT_EQ(Max, ReadRecord1.get().Counts[0]);
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000441
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000442 ErrorOr<InstrProfRecord> ReadRecord2 =
443 Reader->getInstrProfRecord("baz", 0x5678);
444 ASSERT_EQ(1U, ReadRecord2.get().getNumValueSites(IPVK_IndirectCallTarget));
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000445 std::unique_ptr<InstrProfValueData[]> VD =
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000446 ReadRecord2.get().getValueForSite(IPVK_IndirectCallTarget, 0);
447 ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
448 ASSERT_EQ(Max, VD[0].Count);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000449}
450
Xinliang David Li872df222016-01-08 06:54:27 +0000451// This test tests that when there are too many values
452// for a given site, the merged results are properly
453// truncated.
Vedant Kumar00dab222016-01-29 22:54:45 +0000454TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
Xinliang David Li872df222016-01-08 06:54:27 +0000455 static const char caller[] = "caller";
456
457 InstrProfRecord Record11(caller, 0x1234, {1, 2});
458 InstrProfRecord Record12(caller, 0x1234, {1, 2});
459
460 // 2 value sites.
461 Record11.reserveSites(IPVK_IndirectCallTarget, 2);
462 InstrProfValueData VD0[255];
463 for (int I = 0; I < 255; I++) {
464 VD0[I].Value = 2 * I;
465 VD0[I].Count = 2 * I + 1000;
466 }
467
468 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr);
469 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
470
471 Record12.reserveSites(IPVK_IndirectCallTarget, 2);
472 InstrProfValueData VD1[255];
473 for (int I = 0; I < 255; I++) {
474 VD1[I].Value = 2 * I + 1;
475 VD1[I].Count = 2 * I + 1001;
476 }
477
478 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr);
479 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
480
481 Writer.addRecord(std::move(Record11));
482 // Merge profile data.
483 Writer.addRecord(std::move(Record12));
484
485 auto Profile = Writer.writeBuffer();
486 readProfile(std::move(Profile));
487
488 ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
489 ASSERT_TRUE(NoError(R.getError()));
490 std::unique_ptr<InstrProfValueData[]> VD(
491 R.get().getValueForSite(IPVK_IndirectCallTarget, 0));
492 ASSERT_EQ(2U, R.get().getNumValueSites(IPVK_IndirectCallTarget));
493 ASSERT_EQ(255U, R.get().getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
NAKAMURA Takumi249edf32016-01-08 07:58:20 +0000494 for (unsigned I = 0; I < 255; I++) {
Xinliang David Li872df222016-01-08 06:54:27 +0000495 ASSERT_EQ(VD[I].Value, 509 - I);
496 ASSERT_EQ(VD[I].Count, 1509 - I);
497 }
498}
499
Xinliang David Lied966772015-11-25 23:31:18 +0000500// Synthesize runtime value profile data.
501ValueProfNode Site1Values[5] = {{{uint64_t("callee1"), 400}, &Site1Values[1]},
502 {{uint64_t("callee2"), 1000}, &Site1Values[2]},
503 {{uint64_t("callee3"), 500}, &Site1Values[3]},
504 {{uint64_t("callee4"), 300}, &Site1Values[4]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000505 {{uint64_t("callee5"), 100}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000506
507ValueProfNode Site2Values[4] = {{{uint64_t("callee5"), 800}, &Site2Values[1]},
508 {{uint64_t("callee3"), 1000}, &Site2Values[2]},
509 {{uint64_t("callee2"), 2500}, &Site2Values[3]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000510 {{uint64_t("callee1"), 1300}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000511
512ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]},
513 {{uint64_t("callee3"), 1000}, &Site3Values[2]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000514 {{uint64_t("callee4"), 5500}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000515
516ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]},
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000517 {{uint64_t("callee3"), 2000}, nullptr}};
Xinliang David Lied966772015-11-25 23:31:18 +0000518
519static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0],
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000520 &Site3Values[0], &Site4Values[0],
521 nullptr};
522
Xinliang David Lied966772015-11-25 23:31:18 +0000523static uint16_t NumValueSites[IPVK_Last + 1] = {5};
Vedant Kumar00dab222016-01-29 22:54:45 +0000524TEST_P(MaybeSparseInstrProfTest, runtime_value_prof_data_read_write) {
Xinliang David Lied966772015-11-25 23:31:18 +0000525 ValueProfRuntimeRecord RTRecord;
526 initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0],
527 &ValueProfNodes[0]);
528
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000529 ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr);
Xinliang David Lied966772015-11-25 23:31:18 +0000530
531 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
532
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000533 VPData->deserializeTo(Record, nullptr);
Xinliang David Lied966772015-11-25 23:31:18 +0000534
535 // Now read data from Record and sanity check the data
536 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
537 ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
538 ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
539 ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
540 ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
541 ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
542
543 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
544 return VD1.Count > VD2.Count;
545 };
546 std::unique_ptr<InstrProfValueData[]> VD_0(
547 Record.getValueForSite(IPVK_IndirectCallTarget, 0));
548 std::sort(&VD_0[0], &VD_0[5], Cmp);
549 ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2"));
550 ASSERT_EQ(1000U, VD_0[0].Count);
551 ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3"));
552 ASSERT_EQ(500U, VD_0[1].Count);
553 ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1"));
554 ASSERT_EQ(400U, VD_0[2].Count);
555 ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4"));
556 ASSERT_EQ(300U, VD_0[3].Count);
557 ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5"));
558 ASSERT_EQ(100U, VD_0[4].Count);
559
560 std::unique_ptr<InstrProfValueData[]> VD_1(
561 Record.getValueForSite(IPVK_IndirectCallTarget, 1));
562 std::sort(&VD_1[0], &VD_1[4], Cmp);
563 ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2"));
564 ASSERT_EQ(2500U, VD_1[0].Count);
565 ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1"));
566 ASSERT_EQ(1300U, VD_1[1].Count);
567 ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3"));
568 ASSERT_EQ(1000U, VD_1[2].Count);
569 ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5"));
570 ASSERT_EQ(800U, VD_1[3].Count);
571
572 std::unique_ptr<InstrProfValueData[]> VD_2(
573 Record.getValueForSite(IPVK_IndirectCallTarget, 2));
574 std::sort(&VD_2[0], &VD_2[3], Cmp);
575 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4"));
576 ASSERT_EQ(5500U, VD_2[0].Count);
577 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3"));
578 ASSERT_EQ(1000U, VD_2[1].Count);
579 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6"));
580 ASSERT_EQ(800U, VD_2[2].Count);
581
582 std::unique_ptr<InstrProfValueData[]> VD_3(
583 Record.getValueForSite(IPVK_IndirectCallTarget, 3));
584 std::sort(&VD_3[0], &VD_3[2], Cmp);
585 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3"));
586 ASSERT_EQ(2000U, VD_3[0].Count);
587 ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2"));
588 ASSERT_EQ(1800U, VD_3[1].Count);
589
590 finalizeValueProfRuntimeRecord(&RTRecord);
591 free(VPData);
592}
593
Vedant Kumar00dab222016-01-29 22:54:45 +0000594TEST_P(MaybeSparseInstrProfTest, get_max_function_count) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000595 InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2});
596 InstrProfRecord Record2("bar", 0, {1ULL << 63});
597 InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0});
598 Writer.addRecord(std::move(Record1));
599 Writer.addRecord(std::move(Record2));
600 Writer.addRecord(std::move(Record3));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000601 auto Profile = Writer.writeBuffer();
602 readProfile(std::move(Profile));
603
604 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
605}
606
Vedant Kumar00dab222016-01-29 22:54:45 +0000607TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) {
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000608 InstrProfRecord Record1("foo", 0x1234, {1, 2});
609 InstrProfRecord Record2("foo", 0x1235, {3, 4});
610 Writer.addRecord(std::move(Record1), 3);
611 Writer.addRecord(std::move(Record2), 5);
612 auto Profile = Writer.writeBuffer();
613 readProfile(std::move(Profile));
614
615 std::vector<uint64_t> Counts;
616 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
617 ASSERT_EQ(2U, Counts.size());
618 ASSERT_EQ(3U, Counts[0]);
619 ASSERT_EQ(6U, Counts[1]);
620
621 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
622 ASSERT_EQ(2U, Counts.size());
623 ASSERT_EQ(15U, Counts[0]);
624 ASSERT_EQ(20U, Counts[1]);
625}
626
Vedant Kumar00dab222016-01-29 22:54:45 +0000627TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) {
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000628 std::vector<StringRef> FuncNames;
629 FuncNames.push_back("func1");
630 FuncNames.push_back("func2");
631 FuncNames.push_back("func3");
632 FuncNames.push_back("bar1");
633 FuncNames.push_back("bar2");
634 FuncNames.push_back("bar3");
635 InstrProfSymtab Symtab;
636 Symtab.create(FuncNames);
637 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
638 ASSERT_EQ(StringRef("func1"), R);
639 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
640 ASSERT_EQ(StringRef("func2"), R);
641 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
642 ASSERT_EQ(StringRef("func3"), R);
643 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
644 ASSERT_EQ(StringRef("bar1"), R);
645 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
646 ASSERT_EQ(StringRef("bar2"), R);
647 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
648 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Lic96d3d12015-12-19 18:20:09 +0000649
650 // Now incrementally update the symtab
651 Symtab.addFuncName("blah_1");
652 Symtab.addFuncName("blah_2");
653 Symtab.addFuncName("blah_3");
654 // Finalize it
655 Symtab.finalizeSymtab();
656
657 // Check again
658 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));
659 ASSERT_EQ(StringRef("blah_1"), R);
660 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2"));
661 ASSERT_EQ(StringRef("blah_2"), R);
662 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3"));
663 ASSERT_EQ(StringRef("blah_3"), R);
664 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
665 ASSERT_EQ(StringRef("func1"), R);
666 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
667 ASSERT_EQ(StringRef("func2"), R);
668 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
669 ASSERT_EQ(StringRef("func3"), R);
670 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
671 ASSERT_EQ(StringRef("bar1"), R);
672 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
673 ASSERT_EQ(StringRef("bar2"), R);
674 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
675 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000676}
677
Vedant Kumar00dab222016-01-29 22:54:45 +0000678TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) {
Xinliang David Li59411db2016-01-20 01:26:34 +0000679 LLVMContext Ctx;
680 std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx);
681 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
682 /*isVarArg=*/false);
683 Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get());
684 Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get());
685 Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get());
686 Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get());
687 Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get());
688 Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get());
689 Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get());
690 Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get());
691 Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get());
692 Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get());
693 Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get());
694 Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get());
695
696 InstrProfSymtab ProfSymtab;
697 ProfSymtab.create(*(M.get()));
698
699 StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar",
700 "Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"};
701
702 for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) {
703 Function *F = M->getFunction(Funcs[I]);
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000704 ASSERT_TRUE(F != nullptr);
Xinliang David Lida656fe2016-01-20 02:49:53 +0000705 std::string PGOName = getPGOFuncName(*F);
Xinliang David Li3865fdc2016-01-22 18:13:34 +0000706 uint64_t Key = IndexedInstrProf::ComputeHash(PGOName);
Xinliang David Lida656fe2016-01-20 02:49:53 +0000707 ASSERT_EQ(StringRef(PGOName),
Xinliang David Li3865fdc2016-01-22 18:13:34 +0000708 ProfSymtab.getFuncName(Key));
709 ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key));
Xinliang David Li59411db2016-01-20 01:26:34 +0000710 }
711}
712
Vedant Kumar00dab222016-01-29 22:54:45 +0000713TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000714 std::vector<std::string> FuncNames1;
715 std::vector<std::string> FuncNames2;
Xinliang David Life28ccc2016-01-30 01:37:32 +0000716 for (int I = 0; I < 128; I++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000717 std::string str;
718 raw_string_ostream OS(str);
719 OS << "func_" << I;
720 FuncNames1.push_back(OS.str());
721 str.clear();
722 OS << "fooooooooooooooo_" << I;
723 FuncNames1.push_back(OS.str());
724 str.clear();
725 OS << "BAR_" << I;
726 FuncNames2.push_back(OS.str());
727 str.clear();
728 OS << "BlahblahBlahblahBar_" << I;
729 FuncNames2.push_back(OS.str());
730 }
731
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000732 for (bool DoCompression : {false, true}) {
733 // Compressing:
734 std::string FuncNameStrings1;
735 collectPGOFuncNameStrings(
736 FuncNames1, (DoCompression && zlib::isAvailable()), FuncNameStrings1);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000737
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000738 // Compressing:
739 std::string FuncNameStrings2;
740 collectPGOFuncNameStrings(
741 FuncNames2, (DoCompression && zlib::isAvailable()), FuncNameStrings2);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000742
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000743 for (int Padding = 0; Padding < 3; Padding++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000744 // Join with paddings:
745 std::string FuncNameStrings = FuncNameStrings1;
746 for (int P = 0; P < Padding; P++) {
747 FuncNameStrings.push_back('\0');
748 }
749 FuncNameStrings += FuncNameStrings2;
750
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000751 // Now decompress:
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000752 InstrProfSymtab Symtab;
753 Symtab.create(StringRef(FuncNameStrings));
754
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000755 // Now do the checks:
756 // First sampling some data points:
Xinliang David Lia8ba7af2016-01-29 21:26:31 +0000757 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000758 ASSERT_EQ(StringRef("func_0"), R);
759 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
760 ASSERT_EQ(StringRef("fooooooooooooooo_0"), R);
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000761 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[100]));
762 ASSERT_EQ(StringRef("BAR_50"), R);
763 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[101]));
764 ASSERT_EQ(StringRef("BlahblahBlahblahBar_50"), R);
Xinliang David Life28ccc2016-01-30 01:37:32 +0000765 for (int I = 0; I < 128; I++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000766 std::string N[4];
767 N[0] = FuncNames1[2 * I];
768 N[1] = FuncNames1[2 * I + 1];
769 N[2] = FuncNames2[2 * I];
770 N[3] = FuncNames2[2 * I + 1];
771 for (int J = 0; J < 4; J++) {
772 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J]));
773 ASSERT_EQ(StringRef(N[J]), R);
774 }
775 }
776 }
777 }
778}
779
Vedant Kumar00dab222016-01-29 22:54:45 +0000780TEST_F(SparseInstrProfTest, preserve_no_records) {
781 InstrProfRecord Record1("foo", 0x1234, {0});
782 InstrProfRecord Record2("bar", 0x4321, {0, 0});
783 InstrProfRecord Record3("bar", 0x4321, {0, 0, 0});
784
785 Writer.addRecord(std::move(Record1));
786 Writer.addRecord(std::move(Record2));
787 Writer.addRecord(std::move(Record3));
788 auto Profile = Writer.writeBuffer();
789 readProfile(std::move(Profile));
790
791 auto I = Reader->begin(), E = Reader->end();
792 ASSERT_TRUE(I == E);
793}
794
795INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest,
796 ::testing::Bool());
797
Justin Bogner2b6c5372015-02-18 01:58:17 +0000798} // end anonymous namespace