blob: 9ef3487ba18d0130cb2d851c094a9ba2106850d6 [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"
Xinliang David Li402477d2016-02-04 19:11:43 +000011#include "llvm/IR/IRBuilder.h"
Xinliang David Li59411db2016-01-20 01:26:34 +000012#include "llvm/IR/LLVMContext.h"
13#include "llvm/IR/Module.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000014#include "llvm/ProfileData/InstrProfReader.h"
15#include "llvm/ProfileData/InstrProfWriter.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000016#include "llvm/Support/Compression.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000017#include "gtest/gtest.h"
Justin Bogner2b6c5372015-02-18 01:58:17 +000018#include <cstdarg>
19
20using namespace llvm;
21
Vedant Kumar9152fd12016-05-19 03:54:45 +000022static ::testing::AssertionResult NoError(Error E) {
23 if (!E)
Justin Bogner2b6c5372015-02-18 01:58:17 +000024 return ::testing::AssertionSuccess();
Vedant Kumar9152fd12016-05-19 03:54:45 +000025 return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
26 << "\n";
Justin Bogner2b6c5372015-02-18 01:58:17 +000027}
28
Vedant Kumar9152fd12016-05-19 03:54:45 +000029static ::testing::AssertionResult ErrorEquals(instrprof_error Expected,
30 Error E) {
31 instrprof_error Found;
32 std::string FoundMsg;
33 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
34 Found = IPE.get();
35 FoundMsg = IPE.message();
36 });
Justin Bogner2b6c5372015-02-18 01:58:17 +000037 if (Expected == Found)
38 return ::testing::AssertionSuccess();
Vedant Kumar9152fd12016-05-19 03:54:45 +000039 return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
Justin Bogner2b6c5372015-02-18 01:58:17 +000040}
41
42namespace {
43
44struct InstrProfTest : ::testing::Test {
45 InstrProfWriter Writer;
46 std::unique_ptr<IndexedInstrProfReader> Reader;
47
Vedant Kumar00dab222016-01-29 22:54:45 +000048 void SetUp() { Writer.setOutputSparse(false); }
49
Justin Bogner2b6c5372015-02-18 01:58:17 +000050 void readProfile(std::unique_ptr<MemoryBuffer> Profile) {
51 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
Vedant Kumar9152fd12016-05-19 03:54:45 +000052 ASSERT_TRUE(NoError(ReaderOrErr.takeError()));
Justin Bogner2b6c5372015-02-18 01:58:17 +000053 Reader = std::move(ReaderOrErr.get());
54 }
55};
56
Vedant Kumar00dab222016-01-29 22:54:45 +000057struct SparseInstrProfTest : public InstrProfTest {
58 void SetUp() { Writer.setOutputSparse(true); }
59};
60
61struct MaybeSparseInstrProfTest : public InstrProfTest,
62 public ::testing::WithParamInterface<bool> {
Vedant Kumar0259b7b2016-03-22 15:14:18 +000063 void SetUp() { Writer.setOutputSparse(GetParam()); }
Vedant Kumar00dab222016-01-29 22:54:45 +000064};
65
66TEST_P(MaybeSparseInstrProfTest, write_and_read_empty_profile) {
Justin Bogner2b6c5372015-02-18 01:58:17 +000067 auto Profile = Writer.writeBuffer();
68 readProfile(std::move(Profile));
69 ASSERT_TRUE(Reader->begin() == Reader->end());
70}
71
Vedant Kumar00dab222016-01-29 22:54:45 +000072TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) {
Justin Bogner9e9a0572015-09-29 22:13:58 +000073 InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4});
Vedant Kumarec210812016-05-03 17:07:06 +000074 NoError(Writer.addRecord(std::move(Record)));
Justin Bogner2b6c5372015-02-18 01:58:17 +000075 auto Profile = Writer.writeBuffer();
76 readProfile(std::move(Profile));
77
78 auto I = Reader->begin(), E = Reader->end();
79 ASSERT_TRUE(I != E);
80 ASSERT_EQ(StringRef("foo"), I->Name);
81 ASSERT_EQ(0x1234U, I->Hash);
82 ASSERT_EQ(4U, I->Counts.size());
83 ASSERT_EQ(1U, I->Counts[0]);
84 ASSERT_EQ(2U, I->Counts[1]);
85 ASSERT_EQ(3U, I->Counts[2]);
86 ASSERT_EQ(4U, I->Counts[3]);
87 ASSERT_TRUE(++I == E);
88}
89
Vedant Kumar00dab222016-01-29 22:54:45 +000090TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) {
Xinliang David Li2004f002015-11-02 05:08:23 +000091 InstrProfRecord Record1("foo", 0x1234, {1, 2});
92 InstrProfRecord Record2("foo", 0x1235, {3, 4});
Vedant Kumarec210812016-05-03 17:07:06 +000093 NoError(Writer.addRecord(std::move(Record1)));
94 NoError(Writer.addRecord(std::move(Record2)));
Xinliang David Li2004f002015-11-02 05:08:23 +000095 auto Profile = Writer.writeBuffer();
96 readProfile(std::move(Profile));
97
Vedant Kumar9152fd12016-05-19 03:54:45 +000098 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234);
99 ASSERT_TRUE(NoError(R.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000100 ASSERT_EQ(2U, R->Counts.size());
101 ASSERT_EQ(1U, R->Counts[0]);
102 ASSERT_EQ(2U, R->Counts[1]);
Xinliang David Li2004f002015-11-02 05:08:23 +0000103
104 R = Reader->getInstrProfRecord("foo", 0x1235);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000105 ASSERT_TRUE(NoError(R.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000106 ASSERT_EQ(2U, R->Counts.size());
107 ASSERT_EQ(3U, R->Counts[0]);
108 ASSERT_EQ(4U, R->Counts[1]);
Xinliang David Li2004f002015-11-02 05:08:23 +0000109
110 R = Reader->getInstrProfRecord("foo", 0x5678);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000111 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.takeError()));
Xinliang David Li2004f002015-11-02 05:08:23 +0000112
113 R = Reader->getInstrProfRecord("bar", 0x1234);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000114 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError()));
Xinliang David Li2004f002015-11-02 05:08:23 +0000115}
116
Vedant Kumar00dab222016-01-29 22:54:45 +0000117TEST_P(MaybeSparseInstrProfTest, get_function_counts) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000118 InstrProfRecord Record1("foo", 0x1234, {1, 2});
119 InstrProfRecord Record2("foo", 0x1235, {3, 4});
Vedant Kumarec210812016-05-03 17:07:06 +0000120 NoError(Writer.addRecord(std::move(Record1)));
121 NoError(Writer.addRecord(std::move(Record2)));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000122 auto Profile = Writer.writeBuffer();
123 readProfile(std::move(Profile));
124
125 std::vector<uint64_t> Counts;
126 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
127 ASSERT_EQ(2U, Counts.size());
128 ASSERT_EQ(1U, Counts[0]);
129 ASSERT_EQ(2U, Counts[1]);
130
Justin Bogner09829f42015-06-22 23:56:53 +0000131 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
132 ASSERT_EQ(2U, Counts.size());
133 ASSERT_EQ(3U, Counts[0]);
134 ASSERT_EQ(4U, Counts[1]);
135
Vedant Kumar9152fd12016-05-19 03:54:45 +0000136 Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts);
137 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, std::move(E1)));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000138
Vedant Kumar9152fd12016-05-19 03:54:45 +0000139 Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts);
140 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, std::move(E2)));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000141}
142
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000143// Profile data is copied from general.proftext
144TEST_F(InstrProfTest, get_profile_summary) {
145 InstrProfRecord Record1("func1", 0x1234, {97531});
146 InstrProfRecord Record2("func2", 0x1234, {0, 0});
147 InstrProfRecord Record3("func3", 0x1234,
148 {2305843009213693952, 1152921504606846976,
149 576460752303423488, 288230376151711744,
150 144115188075855872, 72057594037927936});
151 InstrProfRecord Record4("func4", 0x1234, {0});
Vedant Kumarec210812016-05-03 17:07:06 +0000152 NoError(Writer.addRecord(std::move(Record1)));
153 NoError(Writer.addRecord(std::move(Record2)));
154 NoError(Writer.addRecord(std::move(Record3)));
155 NoError(Writer.addRecord(std::move(Record4)));
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000156 auto Profile = Writer.writeBuffer();
157 readProfile(std::move(Profile));
158
Easwaran Ramanf9709ee2016-03-14 22:23:28 +0000159 auto VerifySummary = [](InstrProfSummary &IPS) mutable {
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000160 ASSERT_EQ(2305843009213693952U, IPS.getMaxFunctionCount());
161 ASSERT_EQ(2305843009213693952U, IPS.getMaxBlockCount());
162 ASSERT_EQ(10U, IPS.getNumBlocks());
163 ASSERT_EQ(4539628424389557499U, IPS.getTotalCount());
164 std::vector<ProfileSummaryEntry> &Details = IPS.getDetailedSummary();
165 uint32_t Cutoff = 800000;
166 auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
167 return PE.Cutoff == Cutoff;
168 };
169 auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate);
170 Cutoff = 900000;
171 auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate);
172 Cutoff = 950000;
173 auto NinetyFivePerc =
174 std::find_if(Details.begin(), Details.end(), Predicate);
175 Cutoff = 990000;
176 auto NinetyNinePerc =
177 std::find_if(Details.begin(), Details.end(), Predicate);
178 ASSERT_EQ(576460752303423488U, EightyPerc->MinCount);
179 ASSERT_EQ(288230376151711744U, NinetyPerc->MinCount);
180 ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinCount);
181 ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinCount);
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000182 };
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000183 InstrProfSummary &PS = Reader->getSummary();
Easwaran Ramanf9709ee2016-03-14 22:23:28 +0000184 VerifySummary(PS);
Easwaran Raman26628d32016-03-18 21:29:30 +0000185
186 // Test that conversion of summary to and from Metadata works.
Mehdi Amini03b42e42016-04-14 21:59:01 +0000187 LLVMContext Context;
188 Metadata *MD = PS.getMD(Context);
Easwaran Raman7c4f25d2016-03-01 18:30:58 +0000189 ASSERT_TRUE(MD);
190 ProfileSummary *PSFromMD = ProfileSummary::getFromMD(MD);
191 ASSERT_TRUE(PSFromMD);
192 ASSERT_TRUE(isa<InstrProfSummary>(PSFromMD));
193 InstrProfSummary *IPS = cast<InstrProfSummary>(PSFromMD);
Easwaran Ramanf9709ee2016-03-14 22:23:28 +0000194 VerifySummary(*IPS);
Easwaran Raman75c21a92016-03-03 23:55:41 +0000195 delete IPS;
Easwaran Raman26628d32016-03-18 21:29:30 +0000196
197 // Test that summary can be attached to and read back from module.
Mehdi Amini03b42e42016-04-14 21:59:01 +0000198 Module M("my_module", Context);
Easwaran Raman26628d32016-03-18 21:29:30 +0000199 M.setProfileSummary(MD);
200 MD = M.getProfileSummary();
201 ASSERT_TRUE(MD);
202 PSFromMD = ProfileSummary::getFromMD(MD);
203 ASSERT_TRUE(PSFromMD);
204 ASSERT_TRUE(isa<InstrProfSummary>(PSFromMD));
205 IPS = cast<InstrProfSummary>(PSFromMD);
206 VerifySummary(*IPS);
207 delete IPS;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000208}
209
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000210static const char callee1[] = "callee1";
211static const char callee2[] = "callee2";
212static const char callee3[] = "callee3";
213static const char callee4[] = "callee4";
214static const char callee5[] = "callee5";
215static const char callee6[] = "callee6";
216
Vedant Kumar00dab222016-01-29 22:54:45 +0000217TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000218 InstrProfRecord Record1("caller", 0x1234, {1, 2});
219 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
220 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
221 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
222
223 // 4 value sites.
224 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000225 InstrProfValueData VD0[] = {
226 {(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000227 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
Xinliang David Liee415892015-11-10 00:24:45 +0000228 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000229 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000230 InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000231 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000232 InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000233 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000234
Vedant Kumarec210812016-05-03 17:07:06 +0000235 NoError(Writer.addRecord(std::move(Record1)));
236 NoError(Writer.addRecord(std::move(Record2)));
237 NoError(Writer.addRecord(std::move(Record3)));
238 NoError(Writer.addRecord(std::move(Record4)));
Xinliang David Li2004f002015-11-02 05:08:23 +0000239 auto Profile = Writer.writeBuffer();
240 readProfile(std::move(Profile));
241
Vedant Kumar9152fd12016-05-19 03:54:45 +0000242 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
243 ASSERT_TRUE(NoError(R.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000244 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
245 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
246 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
247 ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
248 ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
Xinliang David Li2004f002015-11-02 05:08:23 +0000249
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000250 uint64_t TotalC;
Xinliang David Li2004f002015-11-02 05:08:23 +0000251 std::unique_ptr<InstrProfValueData[]> VD =
David Blaikiefed557e2016-02-09 01:02:24 +0000252 R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000253
254 ASSERT_EQ(3U, VD[0].Count);
255 ASSERT_EQ(2U, VD[1].Count);
256 ASSERT_EQ(1U, VD[2].Count);
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000257 ASSERT_EQ(6U, TotalC);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000258
259 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
260 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
261 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
262}
263
Xinliang David Li402477d2016-02-04 19:11:43 +0000264TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) {
265 InstrProfRecord Record("caller", 0x1234, {1, 2});
266 Record.reserveSites(IPVK_IndirectCallTarget, 1);
Rong Xu69683f12016-02-10 22:19:43 +0000267 InstrProfValueData VD0[] = {{1000, 1}, {2000, 2}, {3000, 3}, {5000, 5},
268 {4000, 4}, {6000, 6}};
269 Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 6, nullptr);
Vedant Kumarec210812016-05-03 17:07:06 +0000270 NoError(Writer.addRecord(std::move(Record)));
Xinliang David Li402477d2016-02-04 19:11:43 +0000271 auto Profile = Writer.writeBuffer();
272 readProfile(std::move(Profile));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000273 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
274 ASSERT_TRUE(NoError(R.takeError()));
Xinliang David Li402477d2016-02-04 19:11:43 +0000275
276 LLVMContext Ctx;
277 std::unique_ptr<Module> M(new Module("MyModule", Ctx));
278 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
279 /*isVarArg=*/false);
280 Function *F =
281 Function::Create(FTy, Function::ExternalLinkage, "caller", M.get());
282 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
283
284 IRBuilder<> Builder(BB);
285 BasicBlock *TBB = BasicBlock::Create(Ctx, "", F);
286 BasicBlock *FBB = BasicBlock::Create(Ctx, "", F);
287
288 // Use branch instruction to annotate with value profile data for simplicity
289 Instruction *Inst = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
290 Instruction *Inst2 = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB);
David Blaikiefed557e2016-02-09 01:02:24 +0000291 annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0);
Xinliang David Li402477d2016-02-04 19:11:43 +0000292
293 InstrProfValueData ValueData[5];
294 uint32_t N;
295 uint64_t T;
296 bool Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5,
297 ValueData, N, T);
298 ASSERT_TRUE(Res);
299 ASSERT_EQ(3U, N);
Rong Xu69683f12016-02-10 22:19:43 +0000300 ASSERT_EQ(21U, T);
Xinliang David Li402477d2016-02-04 19:11:43 +0000301 // The result should be sorted already:
Rong Xu69683f12016-02-10 22:19:43 +0000302 ASSERT_EQ(6000U, ValueData[0].Value);
303 ASSERT_EQ(6U, ValueData[0].Count);
304 ASSERT_EQ(5000U, ValueData[1].Value);
305 ASSERT_EQ(5U, ValueData[1].Count);
306 ASSERT_EQ(4000U, ValueData[2].Value);
307 ASSERT_EQ(4U, ValueData[2].Count);
Xinliang David Li402477d2016-02-04 19:11:43 +0000308 Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 1, ValueData,
309 N, T);
310 ASSERT_TRUE(Res);
311 ASSERT_EQ(1U, N);
Rong Xu69683f12016-02-10 22:19:43 +0000312 ASSERT_EQ(21U, T);
Xinliang David Li402477d2016-02-04 19:11:43 +0000313
314 Res = getValueProfDataFromInst(*Inst2, IPVK_IndirectCallTarget, 5, ValueData,
315 N, T);
316 ASSERT_FALSE(Res);
Rong Xu69683f12016-02-10 22:19:43 +0000317
318 // Remove the MD_prof metadata
319 Inst->setMetadata(LLVMContext::MD_prof, 0);
320 // Annotate 5 records this time.
321 annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0, 5);
322 Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5,
323 ValueData, N, T);
324 ASSERT_TRUE(Res);
325 ASSERT_EQ(5U, N);
326 ASSERT_EQ(21U, T);
327 ASSERT_EQ(6000U, ValueData[0].Value);
328 ASSERT_EQ(6U, ValueData[0].Count);
329 ASSERT_EQ(5000U, ValueData[1].Value);
330 ASSERT_EQ(5U, ValueData[1].Count);
331 ASSERT_EQ(4000U, ValueData[2].Value);
332 ASSERT_EQ(4U, ValueData[2].Count);
333 ASSERT_EQ(3000U, ValueData[3].Value);
334 ASSERT_EQ(3U, ValueData[3].Count);
335 ASSERT_EQ(2000U, ValueData[4].Value);
336 ASSERT_EQ(2U, ValueData[4].Count);
Rong Xubb494902016-02-12 21:36:17 +0000337
338 // Remove the MD_prof metadata
339 Inst->setMetadata(LLVMContext::MD_prof, 0);
340 // Annotate with 4 records.
341 InstrProfValueData VD0Sorted[] = {{1000, 6}, {2000, 5}, {3000, 4}, {4000, 3},
342 {5000, 2}, {6000, 1}};
Rong Xu311ada12016-03-30 16:56:31 +0000343 annotateValueSite(*M, *Inst, makeArrayRef(VD0Sorted).slice(2), 10,
344 IPVK_IndirectCallTarget, 5);
Rong Xubb494902016-02-12 21:36:17 +0000345 Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5,
346 ValueData, N, T);
347 ASSERT_TRUE(Res);
348 ASSERT_EQ(4U, N);
349 ASSERT_EQ(10U, T);
350 ASSERT_EQ(3000U, ValueData[0].Value);
351 ASSERT_EQ(4U, ValueData[0].Count);
352 ASSERT_EQ(4000U, ValueData[1].Value);
353 ASSERT_EQ(3U, ValueData[1].Count);
354 ASSERT_EQ(5000U, ValueData[2].Value);
355 ASSERT_EQ(2U, ValueData[2].Count);
356 ASSERT_EQ(6000U, ValueData[3].Value);
357 ASSERT_EQ(1U, ValueData[3].Count);
Xinliang David Li402477d2016-02-04 19:11:43 +0000358}
359
Vedant Kumar00dab222016-01-29 22:54:45 +0000360TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000361 InstrProfRecord Record1("caller", 0x1234, {1, 2});
362 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
363 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
364 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
365
366 // 4 value sites.
367 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000368 InstrProfValueData VD0[] = {
369 {(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}};
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000370 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
371 // No value profile data at the second site.
372 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000373 InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}};
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000374 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000375 InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000376 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
377
Vedant Kumarec210812016-05-03 17:07:06 +0000378 NoError(Writer.addRecord(std::move(Record1), 10));
379 NoError(Writer.addRecord(std::move(Record2)));
380 NoError(Writer.addRecord(std::move(Record3)));
381 NoError(Writer.addRecord(std::move(Record4)));
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000382 auto Profile = Writer.writeBuffer();
383 readProfile(std::move(Profile));
384
Vedant Kumar9152fd12016-05-19 03:54:45 +0000385 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
386 ASSERT_TRUE(NoError(R.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000387 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
388 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
389 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
390 ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
391 ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000392
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000393 uint64_t TotalC;
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000394 std::unique_ptr<InstrProfValueData[]> VD =
David Blaikiefed557e2016-02-09 01:02:24 +0000395 R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000396 ASSERT_EQ(30U, VD[0].Count);
397 ASSERT_EQ(20U, VD[1].Count);
398 ASSERT_EQ(10U, VD[2].Count);
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000399 ASSERT_EQ(60U, TotalC);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000400
Xinliang David Li2004f002015-11-02 05:08:23 +0000401 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
402 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
403 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
404}
405
Vedant Kumar00dab222016-01-29 22:54:45 +0000406TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) {
Xinliang David Li46ad3632016-01-22 19:53:31 +0000407 InstrProfRecord Record1("caller", 0x1234, {1, 2});
408 InstrProfRecord Record2("callee1", 0x1235, {3, 4});
409 InstrProfRecord Record3("callee2", 0x1235, {3, 4});
410 InstrProfRecord Record4("callee3", 0x1235, {3, 4});
411
412 // 4 value sites.
413 Record1.reserveSites(IPVK_IndirectCallTarget, 4);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000414 InstrProfValueData VD0[] = {
415 {(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}};
Xinliang David Li46ad3632016-01-22 19:53:31 +0000416 Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr);
417 // No value profile data at the second site.
418 Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000419 InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}};
Xinliang David Li46ad3632016-01-22 19:53:31 +0000420 Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr);
Xinliang David Lia3e0d452016-04-10 02:35:53 +0000421 InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}};
Xinliang David Li46ad3632016-01-22 19:53:31 +0000422 Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
423
Vedant Kumarec210812016-05-03 17:07:06 +0000424 NoError(Writer.addRecord(std::move(Record1)));
425 NoError(Writer.addRecord(std::move(Record2)));
426 NoError(Writer.addRecord(std::move(Record3)));
427 NoError(Writer.addRecord(std::move(Record4)));
Xinliang David Li46ad3632016-01-22 19:53:31 +0000428
429 // Set big endian output.
430 Writer.setValueProfDataEndianness(support::big);
431
432 auto Profile = Writer.writeBuffer();
433 readProfile(std::move(Profile));
434
435 // Set big endian input.
436 Reader->setValueProfDataEndianness(support::big);
437
Vedant Kumar9152fd12016-05-19 03:54:45 +0000438 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
439 ASSERT_TRUE(NoError(R.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000440 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
441 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
442 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
443 ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
444 ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
Xinliang David Li46ad3632016-01-22 19:53:31 +0000445
446 std::unique_ptr<InstrProfValueData[]> VD =
David Blaikiefed557e2016-02-09 01:02:24 +0000447 R->getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li46ad3632016-01-22 19:53:31 +0000448 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3"));
449 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2"));
450 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1"));
451
452 // Restore little endian default:
453 Writer.setValueProfDataEndianness(support::little);
454}
455
Vedant Kumar00dab222016-01-29 22:54:45 +0000456TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000457 static const char caller[] = "caller";
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000458 InstrProfRecord Record11(caller, 0x1234, {1, 2});
459 InstrProfRecord Record12(caller, 0x1234, {1, 2});
460 InstrProfRecord Record2(callee1, 0x1235, {3, 4});
461 InstrProfRecord Record3(callee2, 0x1235, {3, 4});
462 InstrProfRecord Record4(callee3, 0x1235, {3, 4});
463 InstrProfRecord Record5(callee3, 0x1235, {3, 4});
464 InstrProfRecord Record6(callee4, 0x1235, {3, 5});
Xinliang David Li2004f002015-11-02 05:08:23 +0000465
466 // 5 value sites.
467 Record11.reserveSites(IPVK_IndirectCallTarget, 5);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000468 InstrProfValueData VD0[] = {{uint64_t(callee1), 1},
469 {uint64_t(callee2), 2},
470 {uint64_t(callee3), 3},
471 {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000472 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000473
Xinliang David Li872df222016-01-08 06:54:27 +0000474 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000475 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000476
Xinliang David Li872df222016-01-08 06:54:27 +0000477 InstrProfValueData VD2[] = {
478 {uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000479 Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000480
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000481 InstrProfValueData VD3[] = {{uint64_t(callee1), 1}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000482 Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000483
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000484 InstrProfValueData VD4[] = {{uint64_t(callee1), 1},
485 {uint64_t(callee2), 2},
486 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000487 Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000488
David Majnemer68318e02016-04-22 06:37:48 +0000489 // A different record for the same caller.
Xinliang David Li2004f002015-11-02 05:08:23 +0000490 Record12.reserveSites(IPVK_IndirectCallTarget, 5);
Xinliang David Li872df222016-01-08 06:54:27 +0000491 InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000492 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000493
Xinliang David Li872df222016-01-08 06:54:27 +0000494 // No value profile data at the second site.
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000495 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000496
Xinliang David Li872df222016-01-08 06:54:27 +0000497 InstrProfValueData VD22[] = {
498 {uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000499 Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000500
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000501 Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000502
Xinliang David Li46ad3632016-01-22 19:53:31 +0000503 InstrProfValueData VD42[] = {{uint64_t(callee1), 1},
504 {uint64_t(callee2), 2},
505 {uint64_t(callee3), 3}};
Eugene Zelenkoffec81c2015-11-04 22:32:32 +0000506 Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr);
Xinliang David Li2004f002015-11-02 05:08:23 +0000507
Vedant Kumarec210812016-05-03 17:07:06 +0000508 NoError(Writer.addRecord(std::move(Record11)));
Xinliang David Li2004f002015-11-02 05:08:23 +0000509 // Merge profile data.
Vedant Kumarec210812016-05-03 17:07:06 +0000510 NoError(Writer.addRecord(std::move(Record12)));
Xinliang David Li2004f002015-11-02 05:08:23 +0000511
Vedant Kumarec210812016-05-03 17:07:06 +0000512 NoError(Writer.addRecord(std::move(Record2)));
513 NoError(Writer.addRecord(std::move(Record3)));
514 NoError(Writer.addRecord(std::move(Record4)));
515 NoError(Writer.addRecord(std::move(Record5)));
516 NoError(Writer.addRecord(std::move(Record6)));
Xinliang David Li2004f002015-11-02 05:08:23 +0000517 auto Profile = Writer.writeBuffer();
518 readProfile(std::move(Profile));
519
Vedant Kumar9152fd12016-05-19 03:54:45 +0000520 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
521 ASSERT_TRUE(NoError(R.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000522 ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget));
523 ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
524 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
525 ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
526 ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
527 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
Xinliang David Li2004f002015-11-02 05:08:23 +0000528
529 std::unique_ptr<InstrProfValueData[]> VD =
David Blaikiefed557e2016-02-09 01:02:24 +0000530 R->getValueForSite(IPVK_IndirectCallTarget, 0);
Xinliang David Li2004f002015-11-02 05:08:23 +0000531 ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2"));
532 ASSERT_EQ(7U, VD[0].Count);
533 ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3"));
534 ASSERT_EQ(6U, VD[1].Count);
535 ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4"));
536 ASSERT_EQ(4U, VD[2].Count);
537 ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1"));
538 ASSERT_EQ(1U, VD[3].Count);
539
540 std::unique_ptr<InstrProfValueData[]> VD_2(
David Blaikiefed557e2016-02-09 01:02:24 +0000541 R->getValueForSite(IPVK_IndirectCallTarget, 2));
Xinliang David Li2004f002015-11-02 05:08:23 +0000542 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3"));
543 ASSERT_EQ(6U, VD_2[0].Count);
544 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4"));
545 ASSERT_EQ(4U, VD_2[1].Count);
546 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2"));
547 ASSERT_EQ(3U, VD_2[2].Count);
548 ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1"));
549 ASSERT_EQ(1U, VD_2[3].Count);
550
551 std::unique_ptr<InstrProfValueData[]> VD_3(
David Blaikiefed557e2016-02-09 01:02:24 +0000552 R->getValueForSite(IPVK_IndirectCallTarget, 3));
Xinliang David Li2004f002015-11-02 05:08:23 +0000553 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1"));
554 ASSERT_EQ(1U, VD_3[0].Count);
555
556 std::unique_ptr<InstrProfValueData[]> VD_4(
David Blaikiefed557e2016-02-09 01:02:24 +0000557 R->getValueForSite(IPVK_IndirectCallTarget, 4));
Xinliang David Li2004f002015-11-02 05:08:23 +0000558 ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3"));
559 ASSERT_EQ(6U, VD_4[0].Count);
560 ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2"));
561 ASSERT_EQ(4U, VD_4[1].Count);
562 ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1"));
563 ASSERT_EQ(2U, VD_4[2].Count);
564}
565
Vedant Kumar00dab222016-01-29 22:54:45 +0000566TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) {
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000567 static const char bar[] = "bar";
568
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000569 const uint64_t Max = std::numeric_limits<uint64_t>::max();
570
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000571 InstrProfRecord Record1("foo", 0x1234, {1});
572 auto Result1 = Writer.addRecord(std::move(Record1));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000573 ASSERT_EQ(InstrProfError::take(std::move(Result1)),
574 instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000575
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000576 // Verify counter overflow.
577 InstrProfRecord Record2("foo", 0x1234, {Max});
578 auto Result2 = Writer.addRecord(std::move(Record2));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000579 ASSERT_EQ(InstrProfError::take(std::move(Result2)),
580 instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000581
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000582 InstrProfRecord Record3(bar, 0x9012, {8});
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000583 auto Result3 = Writer.addRecord(std::move(Record3));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000584 ASSERT_EQ(InstrProfError::take(std::move(Result3)),
585 instrprof_error::success);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000586
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000587 InstrProfRecord Record4("baz", 0x5678, {3, 4});
588 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000589 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000590 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
591 auto Result4 = Writer.addRecord(std::move(Record4));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000592 ASSERT_EQ(InstrProfError::take(std::move(Result4)),
593 instrprof_error::success);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000594
595 // Verify value data counter overflow.
596 InstrProfRecord Record5("baz", 0x5678, {5, 6});
597 Record5.reserveSites(IPVK_IndirectCallTarget, 1);
NAKAMURA Takumi7345ac02015-12-27 06:18:57 +0000598 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000599 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
600 auto Result5 = Writer.addRecord(std::move(Record5));
Vedant Kumar9152fd12016-05-19 03:54:45 +0000601 ASSERT_EQ(InstrProfError::take(std::move(Result5)),
602 instrprof_error::counter_overflow);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000603
604 auto Profile = Writer.writeBuffer();
605 readProfile(std::move(Profile));
606
607 // Verify saturation of counts.
Vedant Kumar9152fd12016-05-19 03:54:45 +0000608 Expected<InstrProfRecord> ReadRecord1 =
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000609 Reader->getInstrProfRecord("foo", 0x1234);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000610 ASSERT_TRUE(NoError(ReadRecord1.takeError()));
David Blaikiefed557e2016-02-09 01:02:24 +0000611 ASSERT_EQ(Max, ReadRecord1->Counts[0]);
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000612
Vedant Kumar9152fd12016-05-19 03:54:45 +0000613 Expected<InstrProfRecord> ReadRecord2 =
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000614 Reader->getInstrProfRecord("baz", 0x5678);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000615 ASSERT_TRUE(bool(ReadRecord2));
David Blaikiefed557e2016-02-09 01:02:24 +0000616 ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget));
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000617 std::unique_ptr<InstrProfValueData[]> VD =
David Blaikiefed557e2016-02-09 01:02:24 +0000618 ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0);
Nathan Slingerland48dd0802015-12-16 21:45:43 +0000619 ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3));
620 ASSERT_EQ(Max, VD[0].Count);
Daniel Sandersbe9db3c2015-11-20 13:13:53 +0000621}
622
Xinliang David Li872df222016-01-08 06:54:27 +0000623// This test tests that when there are too many values
624// for a given site, the merged results are properly
625// truncated.
Vedant Kumar00dab222016-01-29 22:54:45 +0000626TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) {
Xinliang David Li872df222016-01-08 06:54:27 +0000627 static const char caller[] = "caller";
628
629 InstrProfRecord Record11(caller, 0x1234, {1, 2});
630 InstrProfRecord Record12(caller, 0x1234, {1, 2});
631
632 // 2 value sites.
633 Record11.reserveSites(IPVK_IndirectCallTarget, 2);
634 InstrProfValueData VD0[255];
635 for (int I = 0; I < 255; I++) {
636 VD0[I].Value = 2 * I;
637 VD0[I].Count = 2 * I + 1000;
638 }
639
640 Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr);
641 Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
642
643 Record12.reserveSites(IPVK_IndirectCallTarget, 2);
644 InstrProfValueData VD1[255];
645 for (int I = 0; I < 255; I++) {
646 VD1[I].Value = 2 * I + 1;
647 VD1[I].Count = 2 * I + 1001;
648 }
649
650 Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr);
651 Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr);
652
Vedant Kumarec210812016-05-03 17:07:06 +0000653 NoError(Writer.addRecord(std::move(Record11)));
Xinliang David Li872df222016-01-08 06:54:27 +0000654 // Merge profile data.
Vedant Kumarec210812016-05-03 17:07:06 +0000655 NoError(Writer.addRecord(std::move(Record12)));
Xinliang David Li872df222016-01-08 06:54:27 +0000656
657 auto Profile = Writer.writeBuffer();
658 readProfile(std::move(Profile));
659
Vedant Kumar9152fd12016-05-19 03:54:45 +0000660 Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234);
661 ASSERT_TRUE(NoError(R.takeError()));
Xinliang David Li872df222016-01-08 06:54:27 +0000662 std::unique_ptr<InstrProfValueData[]> VD(
David Blaikiefed557e2016-02-09 01:02:24 +0000663 R->getValueForSite(IPVK_IndirectCallTarget, 0));
664 ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget));
665 ASSERT_EQ(255U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
NAKAMURA Takumi249edf32016-01-08 07:58:20 +0000666 for (unsigned I = 0; I < 255; I++) {
Xinliang David Li872df222016-01-08 06:54:27 +0000667 ASSERT_EQ(VD[I].Value, 509 - I);
668 ASSERT_EQ(VD[I].Count, 1509 - I);
669 }
670}
671
Xinliang David Li8e6b9172016-05-13 00:23:49 +0000672static void addValueProfData(InstrProfRecord &Record) {
673 Record.reserveSites(IPVK_IndirectCallTarget, 5);
674 InstrProfValueData VD0[] = {{uint64_t(callee1), 400},
675 {uint64_t(callee2), 1000},
676 {uint64_t(callee3), 500},
677 {uint64_t(callee4), 300},
678 {uint64_t(callee5), 100}};
679 Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 5, nullptr);
680 InstrProfValueData VD1[] = {{uint64_t(callee5), 800},
681 {uint64_t(callee3), 1000},
682 {uint64_t(callee2), 2500},
683 {uint64_t(callee1), 1300}};
684 Record.addValueData(IPVK_IndirectCallTarget, 1, VD1, 4, nullptr);
685 InstrProfValueData VD2[] = {{uint64_t(callee6), 800},
686 {uint64_t(callee3), 1000},
687 {uint64_t(callee4), 5500}};
688 Record.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr);
689 InstrProfValueData VD3[] = {{uint64_t(callee2), 1800},
690 {uint64_t(callee3), 2000}};
691 Record.addValueData(IPVK_IndirectCallTarget, 3, VD3, 2, nullptr);
692 Record.addValueData(IPVK_IndirectCallTarget, 4, nullptr, 0, nullptr);
693}
Xinliang David Lied966772015-11-25 23:31:18 +0000694
Xinliang David Li8e6b9172016-05-13 00:23:49 +0000695TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write) {
696 InstrProfRecord SrcRecord("caller", 0x1234, {1ULL << 31, 2});
697 addValueProfData(SrcRecord);
698 std::unique_ptr<ValueProfData> VPData =
699 ValueProfData::serializeFrom(SrcRecord);
Xinliang David Lied966772015-11-25 23:31:18 +0000700
701 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000702 VPData->deserializeTo(Record, nullptr);
Xinliang David Lied966772015-11-25 23:31:18 +0000703
704 // Now read data from Record and sanity check the data
705 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
706 ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
707 ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
708 ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2));
709 ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3));
710 ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4));
711
712 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
713 return VD1.Count > VD2.Count;
714 };
715 std::unique_ptr<InstrProfValueData[]> VD_0(
716 Record.getValueForSite(IPVK_IndirectCallTarget, 0));
717 std::sort(&VD_0[0], &VD_0[5], Cmp);
718 ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2"));
719 ASSERT_EQ(1000U, VD_0[0].Count);
720 ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3"));
721 ASSERT_EQ(500U, VD_0[1].Count);
722 ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1"));
723 ASSERT_EQ(400U, VD_0[2].Count);
724 ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4"));
725 ASSERT_EQ(300U, VD_0[3].Count);
726 ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5"));
727 ASSERT_EQ(100U, VD_0[4].Count);
728
729 std::unique_ptr<InstrProfValueData[]> VD_1(
730 Record.getValueForSite(IPVK_IndirectCallTarget, 1));
731 std::sort(&VD_1[0], &VD_1[4], Cmp);
732 ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2"));
733 ASSERT_EQ(2500U, VD_1[0].Count);
734 ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1"));
735 ASSERT_EQ(1300U, VD_1[1].Count);
736 ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3"));
737 ASSERT_EQ(1000U, VD_1[2].Count);
738 ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5"));
739 ASSERT_EQ(800U, VD_1[3].Count);
740
741 std::unique_ptr<InstrProfValueData[]> VD_2(
742 Record.getValueForSite(IPVK_IndirectCallTarget, 2));
743 std::sort(&VD_2[0], &VD_2[3], Cmp);
744 ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4"));
745 ASSERT_EQ(5500U, VD_2[0].Count);
746 ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3"));
747 ASSERT_EQ(1000U, VD_2[1].Count);
748 ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6"));
749 ASSERT_EQ(800U, VD_2[2].Count);
750
751 std::unique_ptr<InstrProfValueData[]> VD_3(
752 Record.getValueForSite(IPVK_IndirectCallTarget, 3));
753 std::sort(&VD_3[0], &VD_3[2], Cmp);
754 ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3"));
755 ASSERT_EQ(2000U, VD_3[0].Count);
756 ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2"));
757 ASSERT_EQ(1800U, VD_3[1].Count);
Xinliang David Lied966772015-11-25 23:31:18 +0000758}
759
Xinliang David Li8e6b9172016-05-13 00:23:49 +0000760TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write_mapping) {
Xinliang David Li28464482016-04-10 03:32:02 +0000761
Xinliang David Li8e6b9172016-05-13 00:23:49 +0000762 InstrProfRecord SrcRecord("caller", 0x1234, {1ULL << 31, 2});
763 addValueProfData(SrcRecord);
764 std::unique_ptr<ValueProfData> VPData =
765 ValueProfData::serializeFrom(SrcRecord);
Xinliang David Li28464482016-04-10 03:32:02 +0000766
767 InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2});
768 InstrProfSymtab Symtab;
769 Symtab.mapAddress(uint64_t(callee1), 0x1000ULL);
770 Symtab.mapAddress(uint64_t(callee2), 0x2000ULL);
771 Symtab.mapAddress(uint64_t(callee3), 0x3000ULL);
772 Symtab.mapAddress(uint64_t(callee4), 0x4000ULL);
773 // Missing mapping for callee5
774 Symtab.finalizeSymtab();
775
776 VPData->deserializeTo(Record, &Symtab.getAddrHashMap());
777
778 // Now read data from Record and sanity check the data
Xinliang David Li8e6b9172016-05-13 00:23:49 +0000779 ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget));
Xinliang David Li28464482016-04-10 03:32:02 +0000780 ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
781
782 auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) {
783 return VD1.Count > VD2.Count;
784 };
785 std::unique_ptr<InstrProfValueData[]> VD_0(
786 Record.getValueForSite(IPVK_IndirectCallTarget, 0));
787 std::sort(&VD_0[0], &VD_0[5], Cmp);
788 ASSERT_EQ(VD_0[0].Value, 0x2000ULL);
789 ASSERT_EQ(1000U, VD_0[0].Count);
790 ASSERT_EQ(VD_0[1].Value, 0x3000ULL);
791 ASSERT_EQ(500U, VD_0[1].Count);
792 ASSERT_EQ(VD_0[2].Value, 0x1000ULL);
793 ASSERT_EQ(400U, VD_0[2].Count);
794
795 // callee5 does not have a mapped value -- default to 0.
796 ASSERT_EQ(VD_0[4].Value, 0ULL);
797}
798
Vedant Kumar00dab222016-01-29 22:54:45 +0000799TEST_P(MaybeSparseInstrProfTest, get_max_function_count) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000800 InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2});
801 InstrProfRecord Record2("bar", 0, {1ULL << 63});
802 InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0});
Vedant Kumarec210812016-05-03 17:07:06 +0000803 NoError(Writer.addRecord(std::move(Record1)));
804 NoError(Writer.addRecord(std::move(Record2)));
805 NoError(Writer.addRecord(std::move(Record3)));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000806 auto Profile = Writer.writeBuffer();
807 readProfile(std::move(Profile));
808
809 ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount());
810}
811
Vedant Kumar00dab222016-01-29 22:54:45 +0000812TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) {
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000813 InstrProfRecord Record1("foo", 0x1234, {1, 2});
814 InstrProfRecord Record2("foo", 0x1235, {3, 4});
Vedant Kumarec210812016-05-03 17:07:06 +0000815 NoError(Writer.addRecord(std::move(Record1), 3));
816 NoError(Writer.addRecord(std::move(Record2), 5));
Nathan Slingerland7f5b47d2015-12-15 17:37:09 +0000817 auto Profile = Writer.writeBuffer();
818 readProfile(std::move(Profile));
819
820 std::vector<uint64_t> Counts;
821 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts)));
822 ASSERT_EQ(2U, Counts.size());
823 ASSERT_EQ(3U, Counts[0]);
824 ASSERT_EQ(6U, Counts[1]);
825
826 ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts)));
827 ASSERT_EQ(2U, Counts.size());
828 ASSERT_EQ(15U, Counts[0]);
829 ASSERT_EQ(20U, Counts[1]);
830}
831
Xinliang David Lia0601e72016-02-09 05:47:08 +0000832// Testing symtab creator interface used by indexed profile reader.
Vedant Kumar00dab222016-01-29 22:54:45 +0000833TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) {
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000834 std::vector<StringRef> FuncNames;
835 FuncNames.push_back("func1");
836 FuncNames.push_back("func2");
837 FuncNames.push_back("func3");
838 FuncNames.push_back("bar1");
839 FuncNames.push_back("bar2");
840 FuncNames.push_back("bar3");
841 InstrProfSymtab Symtab;
842 Symtab.create(FuncNames);
843 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
844 ASSERT_EQ(StringRef("func1"), R);
845 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
846 ASSERT_EQ(StringRef("func2"), R);
847 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
848 ASSERT_EQ(StringRef("func3"), R);
849 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
850 ASSERT_EQ(StringRef("bar1"), R);
851 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
852 ASSERT_EQ(StringRef("bar2"), R);
853 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
854 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Lic96d3d12015-12-19 18:20:09 +0000855
Xinliang David Li86b4b912016-02-10 06:36:55 +0000856 // negative tests
857 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar4"));
858 ASSERT_EQ(StringRef(), R);
859 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("foo4"));
860 ASSERT_EQ(StringRef(), R);
861
Xinliang David Lic96d3d12015-12-19 18:20:09 +0000862 // Now incrementally update the symtab
863 Symtab.addFuncName("blah_1");
864 Symtab.addFuncName("blah_2");
865 Symtab.addFuncName("blah_3");
866 // Finalize it
867 Symtab.finalizeSymtab();
868
869 // Check again
870 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1"));
871 ASSERT_EQ(StringRef("blah_1"), R);
872 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2"));
873 ASSERT_EQ(StringRef("blah_2"), R);
874 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3"));
875 ASSERT_EQ(StringRef("blah_3"), R);
876 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1"));
877 ASSERT_EQ(StringRef("func1"), R);
878 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2"));
879 ASSERT_EQ(StringRef("func2"), R);
880 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3"));
881 ASSERT_EQ(StringRef("func3"), R);
882 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1"));
883 ASSERT_EQ(StringRef("bar1"), R);
884 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2"));
885 ASSERT_EQ(StringRef("bar2"), R);
886 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3"));
887 ASSERT_EQ(StringRef("bar3"), R);
Xinliang David Li2ee5c4d2015-12-19 07:44:57 +0000888}
889
Xinliang David Lia0601e72016-02-09 05:47:08 +0000890// Testing symtab creator interface used by value profile transformer.
Vedant Kumar00dab222016-01-29 22:54:45 +0000891TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) {
Xinliang David Li59411db2016-01-20 01:26:34 +0000892 LLVMContext Ctx;
893 std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx);
894 FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx),
895 /*isVarArg=*/false);
896 Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get());
897 Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get());
898 Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get());
899 Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get());
900 Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get());
901 Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get());
902 Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get());
903 Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get());
904 Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get());
905 Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get());
906 Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get());
907 Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get());
908
909 InstrProfSymtab ProfSymtab;
David Blaikiefed557e2016-02-09 01:02:24 +0000910 ProfSymtab.create(*M);
Xinliang David Li59411db2016-01-20 01:26:34 +0000911
912 StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar",
913 "Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"};
914
915 for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) {
916 Function *F = M->getFunction(Funcs[I]);
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000917 ASSERT_TRUE(F != nullptr);
Xinliang David Lida656fe2016-01-20 02:49:53 +0000918 std::string PGOName = getPGOFuncName(*F);
Xinliang David Li3865fdc2016-01-22 18:13:34 +0000919 uint64_t Key = IndexedInstrProf::ComputeHash(PGOName);
Xinliang David Lida656fe2016-01-20 02:49:53 +0000920 ASSERT_EQ(StringRef(PGOName),
Xinliang David Li3865fdc2016-01-22 18:13:34 +0000921 ProfSymtab.getFuncName(Key));
922 ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key));
Xinliang David Li59411db2016-01-20 01:26:34 +0000923 }
924}
925
Xinliang David Lia0601e72016-02-09 05:47:08 +0000926// Testing symtab serialization and creator/deserialization interface
927// used by coverage map reader, and raw profile reader.
Vedant Kumar00dab222016-01-29 22:54:45 +0000928TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000929 std::vector<std::string> FuncNames1;
930 std::vector<std::string> FuncNames2;
Xinliang David Lic2f25cc42016-02-09 05:36:57 +0000931 for (int I = 0; I < 3; I++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000932 std::string str;
933 raw_string_ostream OS(str);
934 OS << "func_" << I;
935 FuncNames1.push_back(OS.str());
936 str.clear();
Vedant Kumar86705ba2016-03-28 21:06:42 +0000937 OS << "f oooooooooooooo_" << I;
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000938 FuncNames1.push_back(OS.str());
939 str.clear();
940 OS << "BAR_" << I;
941 FuncNames2.push_back(OS.str());
942 str.clear();
943 OS << "BlahblahBlahblahBar_" << I;
944 FuncNames2.push_back(OS.str());
945 }
946
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000947 for (bool DoCompression : {false, true}) {
948 // Compressing:
949 std::string FuncNameStrings1;
Vedant Kumarec210812016-05-03 17:07:06 +0000950 NoError(collectPGOFuncNameStrings(
951 FuncNames1, (DoCompression && zlib::isAvailable()), FuncNameStrings1));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000952
Xinliang David Lidd4ae7b2016-01-29 22:29:15 +0000953 // Compressing:
954 std::string FuncNameStrings2;
Vedant Kumarec210812016-05-03 17:07:06 +0000955 NoError(collectPGOFuncNameStrings(
956 FuncNames2, (DoCompression && zlib::isAvailable()), FuncNameStrings2));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000957
Xinliang David Lic2f25cc42016-02-09 05:36:57 +0000958 for (int Padding = 0; Padding < 2; Padding++) {
959 // Join with paddings :
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000960 std::string FuncNameStrings = FuncNameStrings1;
961 for (int P = 0; P < Padding; P++) {
962 FuncNameStrings.push_back('\0');
963 }
964 FuncNameStrings += FuncNameStrings2;
965
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000966 // Now decompress:
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000967 InstrProfSymtab Symtab;
Vedant Kumarec210812016-05-03 17:07:06 +0000968 NoError(Symtab.create(StringRef(FuncNameStrings)));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000969
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000970 // Now do the checks:
971 // First sampling some data points:
Xinliang David Lia8ba7af2016-01-29 21:26:31 +0000972 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0]));
Xinliang David Li8dec8b12016-01-04 23:59:14 +0000973 ASSERT_EQ(StringRef("func_0"), R);
974 R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1]));
Vedant Kumar86705ba2016-03-28 21:06:42 +0000975 ASSERT_EQ(StringRef("f oooooooooooooo_0"), R);
Xinliang David Lic2f25cc42016-02-09 05:36:57 +0000976 for (int I = 0; I < 3; I++) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000977 std::string N[4];
978 N[0] = FuncNames1[2 * I];
979 N[1] = FuncNames1[2 * I + 1];
980 N[2] = FuncNames2[2 * I];
981 N[3] = FuncNames2[2 * I + 1];
982 for (int J = 0; J < 4; J++) {
983 StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J]));
984 ASSERT_EQ(StringRef(N[J]), R);
985 }
986 }
987 }
988 }
989}
990
Vedant Kumar00dab222016-01-29 22:54:45 +0000991TEST_F(SparseInstrProfTest, preserve_no_records) {
992 InstrProfRecord Record1("foo", 0x1234, {0});
993 InstrProfRecord Record2("bar", 0x4321, {0, 0});
994 InstrProfRecord Record3("bar", 0x4321, {0, 0, 0});
995
Vedant Kumarec210812016-05-03 17:07:06 +0000996 NoError(Writer.addRecord(std::move(Record1)));
997 NoError(Writer.addRecord(std::move(Record2)));
998 NoError(Writer.addRecord(std::move(Record3)));
Vedant Kumar00dab222016-01-29 22:54:45 +0000999 auto Profile = Writer.writeBuffer();
1000 readProfile(std::move(Profile));
1001
1002 auto I = Reader->begin(), E = Reader->end();
1003 ASSERT_TRUE(I == E);
1004}
1005
1006INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest,
1007 ::testing::Bool());
1008
Justin Bogner2b6c5372015-02-18 01:58:17 +00001009} // end anonymous namespace