Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 1 | //===- unittest/ProfileData/InstrProfTest.cpp -------------------*- C++ -*-===// |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 2 | // |
| 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 Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Function.h" |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 11 | #include "llvm/IR/IRBuilder.h" |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 12 | #include "llvm/IR/LLVMContext.h" |
| 13 | #include "llvm/IR/Module.h" |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 14 | #include "llvm/ProfileData/InstrProfReader.h" |
| 15 | #include "llvm/ProfileData/InstrProfWriter.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compression.h" |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 17 | #include "gtest/gtest.h" |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 18 | #include <cstdarg> |
| 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | static ::testing::AssertionResult NoError(std::error_code EC) { |
| 23 | if (!EC) |
| 24 | return ::testing::AssertionSuccess(); |
| 25 | return ::testing::AssertionFailure() << "error " << EC.value() |
| 26 | << ": " << EC.message(); |
| 27 | } |
| 28 | |
| 29 | static ::testing::AssertionResult ErrorEquals(std::error_code Expected, |
| 30 | std::error_code Found) { |
| 31 | if (Expected == Found) |
| 32 | return ::testing::AssertionSuccess(); |
| 33 | return ::testing::AssertionFailure() << "error " << Found.value() |
| 34 | << ": " << Found.message(); |
| 35 | } |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | struct InstrProfTest : ::testing::Test { |
| 40 | InstrProfWriter Writer; |
| 41 | std::unique_ptr<IndexedInstrProfReader> Reader; |
| 42 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 43 | void SetUp() { Writer.setOutputSparse(false); } |
| 44 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 45 | void readProfile(std::unique_ptr<MemoryBuffer> Profile) { |
| 46 | auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); |
| 47 | ASSERT_TRUE(NoError(ReaderOrErr.getError())); |
| 48 | Reader = std::move(ReaderOrErr.get()); |
| 49 | } |
| 50 | }; |
| 51 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 52 | struct SparseInstrProfTest : public InstrProfTest { |
| 53 | void SetUp() { Writer.setOutputSparse(true); } |
| 54 | }; |
| 55 | |
| 56 | struct MaybeSparseInstrProfTest : public InstrProfTest, |
| 57 | public ::testing::WithParamInterface<bool> { |
| 58 | void SetUp() { |
| 59 | Writer.setOutputSparse(GetParam()); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | TEST_P(MaybeSparseInstrProfTest, write_and_read_empty_profile) { |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 64 | auto Profile = Writer.writeBuffer(); |
| 65 | readProfile(std::move(Profile)); |
| 66 | ASSERT_TRUE(Reader->begin() == Reader->end()); |
| 67 | } |
| 68 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 69 | TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 70 | InstrProfRecord Record("foo", 0x1234, {1, 2, 3, 4}); |
| 71 | Writer.addRecord(std::move(Record)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 72 | auto Profile = Writer.writeBuffer(); |
| 73 | readProfile(std::move(Profile)); |
| 74 | |
| 75 | auto I = Reader->begin(), E = Reader->end(); |
| 76 | ASSERT_TRUE(I != E); |
| 77 | ASSERT_EQ(StringRef("foo"), I->Name); |
| 78 | ASSERT_EQ(0x1234U, I->Hash); |
| 79 | ASSERT_EQ(4U, I->Counts.size()); |
| 80 | ASSERT_EQ(1U, I->Counts[0]); |
| 81 | ASSERT_EQ(2U, I->Counts[1]); |
| 82 | ASSERT_EQ(3U, I->Counts[2]); |
| 83 | ASSERT_EQ(4U, I->Counts[3]); |
| 84 | ASSERT_TRUE(++I == E); |
| 85 | } |
| 86 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 87 | TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 88 | InstrProfRecord Record1("foo", 0x1234, {1, 2}); |
| 89 | InstrProfRecord Record2("foo", 0x1235, {3, 4}); |
| 90 | Writer.addRecord(std::move(Record1)); |
| 91 | Writer.addRecord(std::move(Record2)); |
| 92 | auto Profile = Writer.writeBuffer(); |
| 93 | readProfile(std::move(Profile)); |
| 94 | |
| 95 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234); |
| 96 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 97 | ASSERT_EQ(2U, R->Counts.size()); |
| 98 | ASSERT_EQ(1U, R->Counts[0]); |
| 99 | ASSERT_EQ(2U, R->Counts[1]); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 100 | |
| 101 | R = Reader->getInstrProfRecord("foo", 0x1235); |
| 102 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 103 | ASSERT_EQ(2U, R->Counts.size()); |
| 104 | ASSERT_EQ(3U, R->Counts[0]); |
| 105 | ASSERT_EQ(4U, R->Counts[1]); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 106 | |
| 107 | R = Reader->getInstrProfRecord("foo", 0x5678); |
| 108 | ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError())); |
| 109 | |
| 110 | R = Reader->getInstrProfRecord("bar", 0x1234); |
| 111 | ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError())); |
| 112 | } |
| 113 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 114 | TEST_P(MaybeSparseInstrProfTest, get_function_counts) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 115 | InstrProfRecord Record1("foo", 0x1234, {1, 2}); |
| 116 | InstrProfRecord Record2("foo", 0x1235, {3, 4}); |
| 117 | Writer.addRecord(std::move(Record1)); |
| 118 | Writer.addRecord(std::move(Record2)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 119 | auto Profile = Writer.writeBuffer(); |
| 120 | readProfile(std::move(Profile)); |
| 121 | |
| 122 | std::vector<uint64_t> Counts; |
| 123 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts))); |
| 124 | ASSERT_EQ(2U, Counts.size()); |
| 125 | ASSERT_EQ(1U, Counts[0]); |
| 126 | ASSERT_EQ(2U, Counts[1]); |
| 127 | |
Justin Bogner | 09829f4 | 2015-06-22 23:56:53 +0000 | [diff] [blame] | 128 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts))); |
| 129 | ASSERT_EQ(2U, Counts.size()); |
| 130 | ASSERT_EQ(3U, Counts[0]); |
| 131 | ASSERT_EQ(4U, Counts[1]); |
| 132 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 133 | std::error_code EC; |
| 134 | EC = Reader->getFunctionCounts("foo", 0x5678, Counts); |
| 135 | ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); |
| 136 | |
| 137 | EC = Reader->getFunctionCounts("bar", 0x1234, Counts); |
| 138 | ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); |
| 139 | } |
| 140 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 141 | // Profile data is copied from general.proftext |
| 142 | TEST_F(InstrProfTest, get_profile_summary) { |
| 143 | InstrProfRecord Record1("func1", 0x1234, {97531}); |
| 144 | InstrProfRecord Record2("func2", 0x1234, {0, 0}); |
| 145 | InstrProfRecord Record3("func3", 0x1234, |
| 146 | {2305843009213693952, 1152921504606846976, |
| 147 | 576460752303423488, 288230376151711744, |
| 148 | 144115188075855872, 72057594037927936}); |
| 149 | InstrProfRecord Record4("func4", 0x1234, {0}); |
| 150 | Writer.addRecord(std::move(Record1)); |
| 151 | Writer.addRecord(std::move(Record2)); |
| 152 | Writer.addRecord(std::move(Record3)); |
| 153 | Writer.addRecord(std::move(Record4)); |
| 154 | auto Profile = Writer.writeBuffer(); |
| 155 | readProfile(std::move(Profile)); |
| 156 | |
| 157 | ProfileSummary &PS = Reader->getSummary(); |
| 158 | ASSERT_EQ(2305843009213693952U, PS.getMaxFunctionCount()); |
| 159 | ASSERT_EQ(2305843009213693952U, PS.getMaxBlockCount()); |
| 160 | ASSERT_EQ(10U, PS.getNumBlocks()); |
| 161 | ASSERT_EQ(4539628424389557499U, PS.getTotalCount()); |
| 162 | std::vector<ProfileSummaryEntry> &Details = PS.getDetailedSummary(); |
| 163 | uint32_t Cutoff = 800000; |
| 164 | auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) { |
| 165 | return PE.Cutoff == Cutoff; |
| 166 | }; |
| 167 | auto EightyPerc = std::find_if(Details.begin(), Details.end(), Predicate); |
| 168 | Cutoff = 900000; |
| 169 | auto NinetyPerc = std::find_if(Details.begin(), Details.end(), Predicate); |
| 170 | Cutoff = 950000; |
| 171 | auto NinetyFivePerc = std::find_if(Details.begin(), Details.end(), Predicate); |
| 172 | Cutoff = 990000; |
| 173 | auto NinetyNinePerc = std::find_if(Details.begin(), Details.end(), Predicate); |
| 174 | ASSERT_EQ(576460752303423488U, EightyPerc->MinBlockCount); |
| 175 | ASSERT_EQ(288230376151711744U, NinetyPerc->MinBlockCount); |
| 176 | ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinBlockCount); |
| 177 | ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinBlockCount); |
| 178 | } |
| 179 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 180 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 181 | InstrProfRecord Record1("caller", 0x1234, {1, 2}); |
| 182 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 183 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 184 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 185 | |
| 186 | // 4 value sites. |
| 187 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
| 188 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 189 | {(uint64_t) "callee2", 2}, |
| 190 | {(uint64_t) "callee3", 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 191 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 192 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 193 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 194 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 195 | {(uint64_t) "callee2", 2}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 196 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 197 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 198 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 199 | |
| 200 | Writer.addRecord(std::move(Record1)); |
| 201 | Writer.addRecord(std::move(Record2)); |
| 202 | Writer.addRecord(std::move(Record3)); |
| 203 | Writer.addRecord(std::move(Record4)); |
| 204 | auto Profile = Writer.writeBuffer(); |
| 205 | readProfile(std::move(Profile)); |
| 206 | |
| 207 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 208 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 209 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 210 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 211 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 212 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 213 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 214 | |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 215 | uint64_t TotalC; |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 216 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 217 | R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 218 | |
| 219 | ASSERT_EQ(3U, VD[0].Count); |
| 220 | ASSERT_EQ(2U, VD[1].Count); |
| 221 | ASSERT_EQ(1U, VD[2].Count); |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 222 | ASSERT_EQ(6U, TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 223 | |
| 224 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 225 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 226 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 227 | } |
| 228 | |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 229 | TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) { |
| 230 | InstrProfRecord Record("caller", 0x1234, {1, 2}); |
| 231 | Record.reserveSites(IPVK_IndirectCallTarget, 1); |
| 232 | InstrProfValueData VD0[] = {{1000, 1}, {2000, 2}, {3000, 3}}; |
| 233 | Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 234 | Writer.addRecord(std::move(Record)); |
| 235 | auto Profile = Writer.writeBuffer(); |
| 236 | readProfile(std::move(Profile)); |
| 237 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 238 | ASSERT_TRUE(NoError(R.getError())); |
| 239 | |
| 240 | LLVMContext Ctx; |
| 241 | std::unique_ptr<Module> M(new Module("MyModule", Ctx)); |
| 242 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 243 | /*isVarArg=*/false); |
| 244 | Function *F = |
| 245 | Function::Create(FTy, Function::ExternalLinkage, "caller", M.get()); |
| 246 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 247 | |
| 248 | IRBuilder<> Builder(BB); |
| 249 | BasicBlock *TBB = BasicBlock::Create(Ctx, "", F); |
| 250 | BasicBlock *FBB = BasicBlock::Create(Ctx, "", F); |
| 251 | |
| 252 | // Use branch instruction to annotate with value profile data for simplicity |
| 253 | Instruction *Inst = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
| 254 | Instruction *Inst2 = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 255 | annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 256 | |
| 257 | InstrProfValueData ValueData[5]; |
| 258 | uint32_t N; |
| 259 | uint64_t T; |
| 260 | bool Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5, |
| 261 | ValueData, N, T); |
| 262 | ASSERT_TRUE(Res); |
| 263 | ASSERT_EQ(3U, N); |
| 264 | ASSERT_EQ(6U, T); |
| 265 | // The result should be sorted already: |
| 266 | ASSERT_EQ(3000U, ValueData[0].Value); |
| 267 | ASSERT_EQ(3U, ValueData[0].Count); |
| 268 | ASSERT_EQ(2000U, ValueData[1].Value); |
| 269 | ASSERT_EQ(2U, ValueData[1].Count); |
| 270 | ASSERT_EQ(1000U, ValueData[2].Value); |
| 271 | ASSERT_EQ(1U, ValueData[2].Count); |
| 272 | Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 1, ValueData, |
| 273 | N, T); |
| 274 | ASSERT_TRUE(Res); |
| 275 | ASSERT_EQ(1U, N); |
| 276 | ASSERT_EQ(6U, T); |
| 277 | |
| 278 | Res = getValueProfDataFromInst(*Inst2, IPVK_IndirectCallTarget, 5, ValueData, |
| 279 | N, T); |
| 280 | ASSERT_FALSE(Res); |
| 281 | } |
| 282 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 283 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) { |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 284 | InstrProfRecord Record1("caller", 0x1234, {1, 2}); |
| 285 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 286 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 287 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 288 | |
| 289 | // 4 value sites. |
| 290 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
| 291 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 292 | {(uint64_t) "callee2", 2}, |
| 293 | {(uint64_t) "callee3", 3}}; |
| 294 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 295 | // No value profile data at the second site. |
| 296 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 297 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 298 | {(uint64_t) "callee2", 2}}; |
| 299 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
| 300 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
| 301 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
| 302 | |
| 303 | Writer.addRecord(std::move(Record1), 10); |
| 304 | Writer.addRecord(std::move(Record2)); |
| 305 | Writer.addRecord(std::move(Record3)); |
| 306 | Writer.addRecord(std::move(Record4)); |
| 307 | auto Profile = Writer.writeBuffer(); |
| 308 | readProfile(std::move(Profile)); |
| 309 | |
| 310 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 311 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 312 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 313 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 314 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 315 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 316 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 317 | |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 318 | uint64_t TotalC; |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 319 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 320 | R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 321 | ASSERT_EQ(30U, VD[0].Count); |
| 322 | ASSERT_EQ(20U, VD[1].Count); |
| 323 | ASSERT_EQ(10U, VD[2].Count); |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 324 | ASSERT_EQ(60U, TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 325 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 326 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 327 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 328 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 329 | } |
| 330 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 331 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) { |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 332 | InstrProfRecord Record1("caller", 0x1234, {1, 2}); |
| 333 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 334 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 335 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 336 | |
| 337 | // 4 value sites. |
| 338 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
| 339 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 340 | {(uint64_t) "callee2", 2}, |
| 341 | {(uint64_t) "callee3", 3}}; |
| 342 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 343 | // No value profile data at the second site. |
| 344 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 345 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 346 | {(uint64_t) "callee2", 2}}; |
| 347 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
| 348 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
| 349 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
| 350 | |
| 351 | Writer.addRecord(std::move(Record1)); |
| 352 | Writer.addRecord(std::move(Record2)); |
| 353 | Writer.addRecord(std::move(Record3)); |
| 354 | Writer.addRecord(std::move(Record4)); |
| 355 | |
| 356 | // Set big endian output. |
| 357 | Writer.setValueProfDataEndianness(support::big); |
| 358 | |
| 359 | auto Profile = Writer.writeBuffer(); |
| 360 | readProfile(std::move(Profile)); |
| 361 | |
| 362 | // Set big endian input. |
| 363 | Reader->setValueProfDataEndianness(support::big); |
| 364 | |
| 365 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 366 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 367 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 368 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 369 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 370 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 371 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 372 | |
| 373 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 374 | R->getValueForSite(IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 375 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 376 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 377 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 378 | |
| 379 | // Restore little endian default: |
| 380 | Writer.setValueProfDataEndianness(support::little); |
| 381 | } |
| 382 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 383 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) { |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 384 | static const char caller[] = "caller"; |
| 385 | static const char callee1[] = "callee1"; |
| 386 | static const char callee2[] = "callee2"; |
| 387 | static const char callee3[] = "callee3"; |
| 388 | static const char callee4[] = "callee4"; |
| 389 | |
| 390 | InstrProfRecord Record11(caller, 0x1234, {1, 2}); |
| 391 | InstrProfRecord Record12(caller, 0x1234, {1, 2}); |
| 392 | InstrProfRecord Record2(callee1, 0x1235, {3, 4}); |
| 393 | InstrProfRecord Record3(callee2, 0x1235, {3, 4}); |
| 394 | InstrProfRecord Record4(callee3, 0x1235, {3, 4}); |
| 395 | InstrProfRecord Record5(callee3, 0x1235, {3, 4}); |
| 396 | InstrProfRecord Record6(callee4, 0x1235, {3, 5}); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 397 | |
| 398 | // 5 value sites. |
| 399 | Record11.reserveSites(IPVK_IndirectCallTarget, 5); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 400 | InstrProfValueData VD0[] = {{uint64_t(callee1), 1}, |
| 401 | {uint64_t(callee2), 2}, |
| 402 | {uint64_t(callee3), 3}, |
| 403 | {uint64_t(callee4), 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 404 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 405 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 406 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 407 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 408 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 409 | InstrProfValueData VD2[] = { |
| 410 | {uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 411 | Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 412 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 413 | InstrProfValueData VD3[] = {{uint64_t(callee1), 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 414 | Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 415 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 416 | InstrProfValueData VD4[] = {{uint64_t(callee1), 1}, |
| 417 | {uint64_t(callee2), 2}, |
| 418 | {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 419 | Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 420 | |
| 421 | // A differnt record for the same caller. |
| 422 | Record12.reserveSites(IPVK_IndirectCallTarget, 5); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 423 | InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 424 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 425 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 426 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 427 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 428 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 429 | InstrProfValueData VD22[] = { |
| 430 | {uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 431 | Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 432 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 433 | Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 434 | |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 435 | InstrProfValueData VD42[] = {{uint64_t(callee1), 1}, |
| 436 | {uint64_t(callee2), 2}, |
| 437 | {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 438 | Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 439 | |
| 440 | Writer.addRecord(std::move(Record11)); |
| 441 | // Merge profile data. |
| 442 | Writer.addRecord(std::move(Record12)); |
| 443 | |
| 444 | Writer.addRecord(std::move(Record2)); |
| 445 | Writer.addRecord(std::move(Record3)); |
| 446 | Writer.addRecord(std::move(Record4)); |
| 447 | Writer.addRecord(std::move(Record5)); |
| 448 | Writer.addRecord(std::move(Record6)); |
| 449 | auto Profile = Writer.writeBuffer(); |
| 450 | readProfile(std::move(Profile)); |
| 451 | |
| 452 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 453 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 454 | ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 455 | ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 456 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 457 | ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 458 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 459 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 460 | |
| 461 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 462 | R->getValueForSite(IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 463 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2")); |
| 464 | ASSERT_EQ(7U, VD[0].Count); |
| 465 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3")); |
| 466 | ASSERT_EQ(6U, VD[1].Count); |
| 467 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4")); |
| 468 | ASSERT_EQ(4U, VD[2].Count); |
| 469 | ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1")); |
| 470 | ASSERT_EQ(1U, VD[3].Count); |
| 471 | |
| 472 | std::unique_ptr<InstrProfValueData[]> VD_2( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 473 | R->getValueForSite(IPVK_IndirectCallTarget, 2)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 474 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3")); |
| 475 | ASSERT_EQ(6U, VD_2[0].Count); |
| 476 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4")); |
| 477 | ASSERT_EQ(4U, VD_2[1].Count); |
| 478 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2")); |
| 479 | ASSERT_EQ(3U, VD_2[2].Count); |
| 480 | ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1")); |
| 481 | ASSERT_EQ(1U, VD_2[3].Count); |
| 482 | |
| 483 | std::unique_ptr<InstrProfValueData[]> VD_3( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 484 | R->getValueForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 485 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1")); |
| 486 | ASSERT_EQ(1U, VD_3[0].Count); |
| 487 | |
| 488 | std::unique_ptr<InstrProfValueData[]> VD_4( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 489 | R->getValueForSite(IPVK_IndirectCallTarget, 4)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 490 | ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3")); |
| 491 | ASSERT_EQ(6U, VD_4[0].Count); |
| 492 | ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2")); |
| 493 | ASSERT_EQ(4U, VD_4[1].Count); |
| 494 | ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1")); |
| 495 | ASSERT_EQ(2U, VD_4[2].Count); |
| 496 | } |
| 497 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 498 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 499 | static const char bar[] = "bar"; |
| 500 | |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 501 | const uint64_t Max = std::numeric_limits<uint64_t>::max(); |
| 502 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 503 | InstrProfRecord Record1("foo", 0x1234, {1}); |
| 504 | auto Result1 = Writer.addRecord(std::move(Record1)); |
| 505 | ASSERT_EQ(Result1, instrprof_error::success); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 506 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 507 | // Verify counter overflow. |
| 508 | InstrProfRecord Record2("foo", 0x1234, {Max}); |
| 509 | auto Result2 = Writer.addRecord(std::move(Record2)); |
| 510 | ASSERT_EQ(Result2, instrprof_error::counter_overflow); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 511 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 512 | InstrProfRecord Record3(bar, 0x9012, {8}); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 513 | auto Result3 = Writer.addRecord(std::move(Record3)); |
| 514 | ASSERT_EQ(Result3, instrprof_error::success); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 515 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 516 | InstrProfRecord Record4("baz", 0x5678, {3, 4}); |
| 517 | Record4.reserveSites(IPVK_IndirectCallTarget, 1); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 518 | InstrProfValueData VD4[] = {{uint64_t(bar), 1}}; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 519 | Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr); |
| 520 | auto Result4 = Writer.addRecord(std::move(Record4)); |
| 521 | ASSERT_EQ(Result4, instrprof_error::success); |
| 522 | |
| 523 | // Verify value data counter overflow. |
| 524 | InstrProfRecord Record5("baz", 0x5678, {5, 6}); |
| 525 | Record5.reserveSites(IPVK_IndirectCallTarget, 1); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 526 | InstrProfValueData VD5[] = {{uint64_t(bar), Max}}; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 527 | Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr); |
| 528 | auto Result5 = Writer.addRecord(std::move(Record5)); |
| 529 | ASSERT_EQ(Result5, instrprof_error::counter_overflow); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 530 | |
| 531 | auto Profile = Writer.writeBuffer(); |
| 532 | readProfile(std::move(Profile)); |
| 533 | |
| 534 | // Verify saturation of counts. |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 535 | ErrorOr<InstrProfRecord> ReadRecord1 = |
| 536 | Reader->getInstrProfRecord("foo", 0x1234); |
| 537 | ASSERT_TRUE(NoError(ReadRecord1.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 538 | ASSERT_EQ(Max, ReadRecord1->Counts[0]); |
Nathan Slingerland | aa5702d | 2015-12-02 18:19:24 +0000 | [diff] [blame] | 539 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 540 | ErrorOr<InstrProfRecord> ReadRecord2 = |
| 541 | Reader->getInstrProfRecord("baz", 0x5678); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 542 | ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget)); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 543 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 544 | ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 545 | ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3)); |
| 546 | ASSERT_EQ(Max, VD[0].Count); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 549 | // This test tests that when there are too many values |
| 550 | // for a given site, the merged results are properly |
| 551 | // truncated. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 552 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) { |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 553 | static const char caller[] = "caller"; |
| 554 | |
| 555 | InstrProfRecord Record11(caller, 0x1234, {1, 2}); |
| 556 | InstrProfRecord Record12(caller, 0x1234, {1, 2}); |
| 557 | |
| 558 | // 2 value sites. |
| 559 | Record11.reserveSites(IPVK_IndirectCallTarget, 2); |
| 560 | InstrProfValueData VD0[255]; |
| 561 | for (int I = 0; I < 255; I++) { |
| 562 | VD0[I].Value = 2 * I; |
| 563 | VD0[I].Count = 2 * I + 1000; |
| 564 | } |
| 565 | |
| 566 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr); |
| 567 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 568 | |
| 569 | Record12.reserveSites(IPVK_IndirectCallTarget, 2); |
| 570 | InstrProfValueData VD1[255]; |
| 571 | for (int I = 0; I < 255; I++) { |
| 572 | VD1[I].Value = 2 * I + 1; |
| 573 | VD1[I].Count = 2 * I + 1001; |
| 574 | } |
| 575 | |
| 576 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr); |
| 577 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 578 | |
| 579 | Writer.addRecord(std::move(Record11)); |
| 580 | // Merge profile data. |
| 581 | Writer.addRecord(std::move(Record12)); |
| 582 | |
| 583 | auto Profile = Writer.writeBuffer(); |
| 584 | readProfile(std::move(Profile)); |
| 585 | |
| 586 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 587 | ASSERT_TRUE(NoError(R.getError())); |
| 588 | std::unique_ptr<InstrProfValueData[]> VD( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 589 | R->getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 590 | ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 591 | ASSERT_EQ(255U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
NAKAMURA Takumi | 249edf3 | 2016-01-08 07:58:20 +0000 | [diff] [blame] | 592 | for (unsigned I = 0; I < 255; I++) { |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 593 | ASSERT_EQ(VD[I].Value, 509 - I); |
| 594 | ASSERT_EQ(VD[I].Count, 1509 - I); |
| 595 | } |
| 596 | } |
| 597 | |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 598 | // Synthesize runtime value profile data. |
| 599 | ValueProfNode Site1Values[5] = {{{uint64_t("callee1"), 400}, &Site1Values[1]}, |
| 600 | {{uint64_t("callee2"), 1000}, &Site1Values[2]}, |
| 601 | {{uint64_t("callee3"), 500}, &Site1Values[3]}, |
| 602 | {{uint64_t("callee4"), 300}, &Site1Values[4]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 603 | {{uint64_t("callee5"), 100}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 604 | |
| 605 | ValueProfNode Site2Values[4] = {{{uint64_t("callee5"), 800}, &Site2Values[1]}, |
| 606 | {{uint64_t("callee3"), 1000}, &Site2Values[2]}, |
| 607 | {{uint64_t("callee2"), 2500}, &Site2Values[3]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 608 | {{uint64_t("callee1"), 1300}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 609 | |
| 610 | ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]}, |
| 611 | {{uint64_t("callee3"), 1000}, &Site3Values[2]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 612 | {{uint64_t("callee4"), 5500}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 613 | |
| 614 | ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 615 | {{uint64_t("callee3"), 2000}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 616 | |
| 617 | static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0], |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 618 | &Site3Values[0], &Site4Values[0], |
| 619 | nullptr}; |
| 620 | |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 621 | static uint16_t NumValueSites[IPVK_Last + 1] = {5}; |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 622 | TEST_P(MaybeSparseInstrProfTest, runtime_value_prof_data_read_write) { |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 623 | ValueProfRuntimeRecord RTRecord; |
| 624 | initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0], |
| 625 | &ValueProfNodes[0]); |
| 626 | |
Xinliang David Li | 0e6a36e | 2015-12-01 19:47:32 +0000 | [diff] [blame] | 627 | ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 628 | |
| 629 | InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2}); |
| 630 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 631 | VPData->deserializeTo(Record, nullptr); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 632 | |
| 633 | // Now read data from Record and sanity check the data |
| 634 | ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget)); |
| 635 | ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 636 | ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 637 | ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 638 | ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 639 | ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
| 640 | |
| 641 | auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 642 | return VD1.Count > VD2.Count; |
| 643 | }; |
| 644 | std::unique_ptr<InstrProfValueData[]> VD_0( |
| 645 | Record.getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 646 | std::sort(&VD_0[0], &VD_0[5], Cmp); |
| 647 | ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2")); |
| 648 | ASSERT_EQ(1000U, VD_0[0].Count); |
| 649 | ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3")); |
| 650 | ASSERT_EQ(500U, VD_0[1].Count); |
| 651 | ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1")); |
| 652 | ASSERT_EQ(400U, VD_0[2].Count); |
| 653 | ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4")); |
| 654 | ASSERT_EQ(300U, VD_0[3].Count); |
| 655 | ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5")); |
| 656 | ASSERT_EQ(100U, VD_0[4].Count); |
| 657 | |
| 658 | std::unique_ptr<InstrProfValueData[]> VD_1( |
| 659 | Record.getValueForSite(IPVK_IndirectCallTarget, 1)); |
| 660 | std::sort(&VD_1[0], &VD_1[4], Cmp); |
| 661 | ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2")); |
| 662 | ASSERT_EQ(2500U, VD_1[0].Count); |
| 663 | ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1")); |
| 664 | ASSERT_EQ(1300U, VD_1[1].Count); |
| 665 | ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3")); |
| 666 | ASSERT_EQ(1000U, VD_1[2].Count); |
| 667 | ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5")); |
| 668 | ASSERT_EQ(800U, VD_1[3].Count); |
| 669 | |
| 670 | std::unique_ptr<InstrProfValueData[]> VD_2( |
| 671 | Record.getValueForSite(IPVK_IndirectCallTarget, 2)); |
| 672 | std::sort(&VD_2[0], &VD_2[3], Cmp); |
| 673 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4")); |
| 674 | ASSERT_EQ(5500U, VD_2[0].Count); |
| 675 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3")); |
| 676 | ASSERT_EQ(1000U, VD_2[1].Count); |
| 677 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6")); |
| 678 | ASSERT_EQ(800U, VD_2[2].Count); |
| 679 | |
| 680 | std::unique_ptr<InstrProfValueData[]> VD_3( |
| 681 | Record.getValueForSite(IPVK_IndirectCallTarget, 3)); |
| 682 | std::sort(&VD_3[0], &VD_3[2], Cmp); |
| 683 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3")); |
| 684 | ASSERT_EQ(2000U, VD_3[0].Count); |
| 685 | ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2")); |
| 686 | ASSERT_EQ(1800U, VD_3[1].Count); |
| 687 | |
| 688 | finalizeValueProfRuntimeRecord(&RTRecord); |
| 689 | free(VPData); |
| 690 | } |
| 691 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 692 | TEST_P(MaybeSparseInstrProfTest, get_max_function_count) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 693 | InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2}); |
| 694 | InstrProfRecord Record2("bar", 0, {1ULL << 63}); |
| 695 | InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0}); |
| 696 | Writer.addRecord(std::move(Record1)); |
| 697 | Writer.addRecord(std::move(Record2)); |
| 698 | Writer.addRecord(std::move(Record3)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 699 | auto Profile = Writer.writeBuffer(); |
| 700 | readProfile(std::move(Profile)); |
| 701 | |
| 702 | ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount()); |
| 703 | } |
| 704 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 705 | TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) { |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 706 | InstrProfRecord Record1("foo", 0x1234, {1, 2}); |
| 707 | InstrProfRecord Record2("foo", 0x1235, {3, 4}); |
| 708 | Writer.addRecord(std::move(Record1), 3); |
| 709 | Writer.addRecord(std::move(Record2), 5); |
| 710 | auto Profile = Writer.writeBuffer(); |
| 711 | readProfile(std::move(Profile)); |
| 712 | |
| 713 | std::vector<uint64_t> Counts; |
| 714 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts))); |
| 715 | ASSERT_EQ(2U, Counts.size()); |
| 716 | ASSERT_EQ(3U, Counts[0]); |
| 717 | ASSERT_EQ(6U, Counts[1]); |
| 718 | |
| 719 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts))); |
| 720 | ASSERT_EQ(2U, Counts.size()); |
| 721 | ASSERT_EQ(15U, Counts[0]); |
| 722 | ASSERT_EQ(20U, Counts[1]); |
| 723 | } |
| 724 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 725 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) { |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 726 | std::vector<StringRef> FuncNames; |
| 727 | FuncNames.push_back("func1"); |
| 728 | FuncNames.push_back("func2"); |
| 729 | FuncNames.push_back("func3"); |
| 730 | FuncNames.push_back("bar1"); |
| 731 | FuncNames.push_back("bar2"); |
| 732 | FuncNames.push_back("bar3"); |
| 733 | InstrProfSymtab Symtab; |
| 734 | Symtab.create(FuncNames); |
| 735 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1")); |
| 736 | ASSERT_EQ(StringRef("func1"), R); |
| 737 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2")); |
| 738 | ASSERT_EQ(StringRef("func2"), R); |
| 739 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3")); |
| 740 | ASSERT_EQ(StringRef("func3"), R); |
| 741 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1")); |
| 742 | ASSERT_EQ(StringRef("bar1"), R); |
| 743 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2")); |
| 744 | ASSERT_EQ(StringRef("bar2"), R); |
| 745 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3")); |
| 746 | ASSERT_EQ(StringRef("bar3"), R); |
Xinliang David Li | c96d3d1 | 2015-12-19 18:20:09 +0000 | [diff] [blame] | 747 | |
| 748 | // Now incrementally update the symtab |
| 749 | Symtab.addFuncName("blah_1"); |
| 750 | Symtab.addFuncName("blah_2"); |
| 751 | Symtab.addFuncName("blah_3"); |
| 752 | // Finalize it |
| 753 | Symtab.finalizeSymtab(); |
| 754 | |
| 755 | // Check again |
| 756 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1")); |
| 757 | ASSERT_EQ(StringRef("blah_1"), R); |
| 758 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2")); |
| 759 | ASSERT_EQ(StringRef("blah_2"), R); |
| 760 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3")); |
| 761 | ASSERT_EQ(StringRef("blah_3"), R); |
| 762 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1")); |
| 763 | ASSERT_EQ(StringRef("func1"), R); |
| 764 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2")); |
| 765 | ASSERT_EQ(StringRef("func2"), R); |
| 766 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3")); |
| 767 | ASSERT_EQ(StringRef("func3"), R); |
| 768 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1")); |
| 769 | ASSERT_EQ(StringRef("bar1"), R); |
| 770 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2")); |
| 771 | ASSERT_EQ(StringRef("bar2"), R); |
| 772 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3")); |
| 773 | ASSERT_EQ(StringRef("bar3"), R); |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 774 | } |
| 775 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 776 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) { |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 777 | LLVMContext Ctx; |
| 778 | std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx); |
| 779 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 780 | /*isVarArg=*/false); |
| 781 | Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get()); |
| 782 | Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get()); |
| 783 | Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get()); |
| 784 | Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get()); |
| 785 | Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get()); |
| 786 | Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get()); |
| 787 | Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get()); |
| 788 | Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get()); |
| 789 | Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get()); |
| 790 | Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get()); |
| 791 | Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get()); |
| 792 | Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get()); |
| 793 | |
| 794 | InstrProfSymtab ProfSymtab; |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame^] | 795 | ProfSymtab.create(*M); |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 796 | |
| 797 | StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar", |
| 798 | "Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"}; |
| 799 | |
| 800 | for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) { |
| 801 | Function *F = M->getFunction(Funcs[I]); |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 802 | ASSERT_TRUE(F != nullptr); |
Xinliang David Li | da656fe | 2016-01-20 02:49:53 +0000 | [diff] [blame] | 803 | std::string PGOName = getPGOFuncName(*F); |
Xinliang David Li | 3865fdc | 2016-01-22 18:13:34 +0000 | [diff] [blame] | 804 | uint64_t Key = IndexedInstrProf::ComputeHash(PGOName); |
Xinliang David Li | da656fe | 2016-01-20 02:49:53 +0000 | [diff] [blame] | 805 | ASSERT_EQ(StringRef(PGOName), |
Xinliang David Li | 3865fdc | 2016-01-22 18:13:34 +0000 | [diff] [blame] | 806 | ProfSymtab.getFuncName(Key)); |
| 807 | ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key)); |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 811 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 812 | std::vector<std::string> FuncNames1; |
| 813 | std::vector<std::string> FuncNames2; |
Xinliang David Li | fe28ccc | 2016-01-30 01:37:32 +0000 | [diff] [blame] | 814 | for (int I = 0; I < 128; I++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 815 | std::string str; |
| 816 | raw_string_ostream OS(str); |
| 817 | OS << "func_" << I; |
| 818 | FuncNames1.push_back(OS.str()); |
| 819 | str.clear(); |
| 820 | OS << "fooooooooooooooo_" << I; |
| 821 | FuncNames1.push_back(OS.str()); |
| 822 | str.clear(); |
| 823 | OS << "BAR_" << I; |
| 824 | FuncNames2.push_back(OS.str()); |
| 825 | str.clear(); |
| 826 | OS << "BlahblahBlahblahBar_" << I; |
| 827 | FuncNames2.push_back(OS.str()); |
| 828 | } |
| 829 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 830 | for (bool DoCompression : {false, true}) { |
| 831 | // Compressing: |
| 832 | std::string FuncNameStrings1; |
| 833 | collectPGOFuncNameStrings( |
| 834 | FuncNames1, (DoCompression && zlib::isAvailable()), FuncNameStrings1); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 835 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 836 | // Compressing: |
| 837 | std::string FuncNameStrings2; |
| 838 | collectPGOFuncNameStrings( |
| 839 | FuncNames2, (DoCompression && zlib::isAvailable()), FuncNameStrings2); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 840 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 841 | for (int Padding = 0; Padding < 3; Padding++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 842 | // Join with paddings: |
| 843 | std::string FuncNameStrings = FuncNameStrings1; |
| 844 | for (int P = 0; P < Padding; P++) { |
| 845 | FuncNameStrings.push_back('\0'); |
| 846 | } |
| 847 | FuncNameStrings += FuncNameStrings2; |
| 848 | |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 849 | // Now decompress: |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 850 | InstrProfSymtab Symtab; |
| 851 | Symtab.create(StringRef(FuncNameStrings)); |
| 852 | |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 853 | // Now do the checks: |
| 854 | // First sampling some data points: |
Xinliang David Li | a8ba7af | 2016-01-29 21:26:31 +0000 | [diff] [blame] | 855 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0])); |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 856 | ASSERT_EQ(StringRef("func_0"), R); |
| 857 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1])); |
| 858 | ASSERT_EQ(StringRef("fooooooooooooooo_0"), R); |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 859 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[100])); |
| 860 | ASSERT_EQ(StringRef("BAR_50"), R); |
| 861 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames2[101])); |
| 862 | ASSERT_EQ(StringRef("BlahblahBlahblahBar_50"), R); |
Xinliang David Li | fe28ccc | 2016-01-30 01:37:32 +0000 | [diff] [blame] | 863 | for (int I = 0; I < 128; I++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 864 | std::string N[4]; |
| 865 | N[0] = FuncNames1[2 * I]; |
| 866 | N[1] = FuncNames1[2 * I + 1]; |
| 867 | N[2] = FuncNames2[2 * I]; |
| 868 | N[3] = FuncNames2[2 * I + 1]; |
| 869 | for (int J = 0; J < 4; J++) { |
| 870 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J])); |
| 871 | ASSERT_EQ(StringRef(N[J]), R); |
| 872 | } |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 878 | TEST_F(SparseInstrProfTest, preserve_no_records) { |
| 879 | InstrProfRecord Record1("foo", 0x1234, {0}); |
| 880 | InstrProfRecord Record2("bar", 0x4321, {0, 0}); |
| 881 | InstrProfRecord Record3("bar", 0x4321, {0, 0, 0}); |
| 882 | |
| 883 | Writer.addRecord(std::move(Record1)); |
| 884 | Writer.addRecord(std::move(Record2)); |
| 885 | Writer.addRecord(std::move(Record3)); |
| 886 | auto Profile = Writer.writeBuffer(); |
| 887 | readProfile(std::move(Profile)); |
| 888 | |
| 889 | auto I = Reader->begin(), E = Reader->end(); |
| 890 | ASSERT_TRUE(I == E); |
| 891 | } |
| 892 | |
| 893 | INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest, |
| 894 | ::testing::Bool()); |
| 895 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 896 | } // end anonymous namespace |