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); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame^] | 232 | InstrProfValueData VD0[] = {{1000, 1}, {2000, 2}, {3000, 3}, {5000, 5}, |
| 233 | {4000, 4}, {6000, 6}}; |
| 234 | Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 6, nullptr); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 235 | Writer.addRecord(std::move(Record)); |
| 236 | auto Profile = Writer.writeBuffer(); |
| 237 | readProfile(std::move(Profile)); |
| 238 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 239 | ASSERT_TRUE(NoError(R.getError())); |
| 240 | |
| 241 | LLVMContext Ctx; |
| 242 | std::unique_ptr<Module> M(new Module("MyModule", Ctx)); |
| 243 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 244 | /*isVarArg=*/false); |
| 245 | Function *F = |
| 246 | Function::Create(FTy, Function::ExternalLinkage, "caller", M.get()); |
| 247 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 248 | |
| 249 | IRBuilder<> Builder(BB); |
| 250 | BasicBlock *TBB = BasicBlock::Create(Ctx, "", F); |
| 251 | BasicBlock *FBB = BasicBlock::Create(Ctx, "", F); |
| 252 | |
| 253 | // Use branch instruction to annotate with value profile data for simplicity |
| 254 | Instruction *Inst = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
| 255 | Instruction *Inst2 = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 256 | annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 257 | |
| 258 | InstrProfValueData ValueData[5]; |
| 259 | uint32_t N; |
| 260 | uint64_t T; |
| 261 | bool Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5, |
| 262 | ValueData, N, T); |
| 263 | ASSERT_TRUE(Res); |
| 264 | ASSERT_EQ(3U, N); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame^] | 265 | ASSERT_EQ(21U, T); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 266 | // The result should be sorted already: |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame^] | 267 | ASSERT_EQ(6000U, ValueData[0].Value); |
| 268 | ASSERT_EQ(6U, ValueData[0].Count); |
| 269 | ASSERT_EQ(5000U, ValueData[1].Value); |
| 270 | ASSERT_EQ(5U, ValueData[1].Count); |
| 271 | ASSERT_EQ(4000U, ValueData[2].Value); |
| 272 | ASSERT_EQ(4U, ValueData[2].Count); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 273 | Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 1, ValueData, |
| 274 | N, T); |
| 275 | ASSERT_TRUE(Res); |
| 276 | ASSERT_EQ(1U, N); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame^] | 277 | ASSERT_EQ(21U, T); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 278 | |
| 279 | Res = getValueProfDataFromInst(*Inst2, IPVK_IndirectCallTarget, 5, ValueData, |
| 280 | N, T); |
| 281 | ASSERT_FALSE(Res); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame^] | 282 | |
| 283 | // Remove the MD_prof metadata |
| 284 | Inst->setMetadata(LLVMContext::MD_prof, 0); |
| 285 | // Annotate 5 records this time. |
| 286 | annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0, 5); |
| 287 | Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5, |
| 288 | ValueData, N, T); |
| 289 | ASSERT_TRUE(Res); |
| 290 | ASSERT_EQ(5U, N); |
| 291 | ASSERT_EQ(21U, T); |
| 292 | ASSERT_EQ(6000U, ValueData[0].Value); |
| 293 | ASSERT_EQ(6U, ValueData[0].Count); |
| 294 | ASSERT_EQ(5000U, ValueData[1].Value); |
| 295 | ASSERT_EQ(5U, ValueData[1].Count); |
| 296 | ASSERT_EQ(4000U, ValueData[2].Value); |
| 297 | ASSERT_EQ(4U, ValueData[2].Count); |
| 298 | ASSERT_EQ(3000U, ValueData[3].Value); |
| 299 | ASSERT_EQ(3U, ValueData[3].Count); |
| 300 | ASSERT_EQ(2000U, ValueData[4].Value); |
| 301 | ASSERT_EQ(2U, ValueData[4].Count); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 304 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) { |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 305 | InstrProfRecord Record1("caller", 0x1234, {1, 2}); |
| 306 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 307 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 308 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 309 | |
| 310 | // 4 value sites. |
| 311 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
| 312 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 313 | {(uint64_t) "callee2", 2}, |
| 314 | {(uint64_t) "callee3", 3}}; |
| 315 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 316 | // No value profile data at the second site. |
| 317 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 318 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 319 | {(uint64_t) "callee2", 2}}; |
| 320 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
| 321 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
| 322 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
| 323 | |
| 324 | Writer.addRecord(std::move(Record1), 10); |
| 325 | Writer.addRecord(std::move(Record2)); |
| 326 | Writer.addRecord(std::move(Record3)); |
| 327 | Writer.addRecord(std::move(Record4)); |
| 328 | auto Profile = Writer.writeBuffer(); |
| 329 | readProfile(std::move(Profile)); |
| 330 | |
| 331 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 332 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 333 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 334 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 335 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 336 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 337 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 338 | |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 339 | uint64_t TotalC; |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 340 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 341 | R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 342 | ASSERT_EQ(30U, VD[0].Count); |
| 343 | ASSERT_EQ(20U, VD[1].Count); |
| 344 | ASSERT_EQ(10U, VD[2].Count); |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 345 | ASSERT_EQ(60U, TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 346 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 347 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 348 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 349 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 350 | } |
| 351 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 352 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) { |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 353 | InstrProfRecord Record1("caller", 0x1234, {1, 2}); |
| 354 | InstrProfRecord Record2("callee1", 0x1235, {3, 4}); |
| 355 | InstrProfRecord Record3("callee2", 0x1235, {3, 4}); |
| 356 | InstrProfRecord Record4("callee3", 0x1235, {3, 4}); |
| 357 | |
| 358 | // 4 value sites. |
| 359 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
| 360 | InstrProfValueData VD0[] = {{(uint64_t) "callee1", 1}, |
| 361 | {(uint64_t) "callee2", 2}, |
| 362 | {(uint64_t) "callee3", 3}}; |
| 363 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 364 | // No value profile data at the second site. |
| 365 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 366 | InstrProfValueData VD2[] = {{(uint64_t) "callee1", 1}, |
| 367 | {(uint64_t) "callee2", 2}}; |
| 368 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
| 369 | InstrProfValueData VD3[] = {{(uint64_t) "callee1", 1}}; |
| 370 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
| 371 | |
| 372 | Writer.addRecord(std::move(Record1)); |
| 373 | Writer.addRecord(std::move(Record2)); |
| 374 | Writer.addRecord(std::move(Record3)); |
| 375 | Writer.addRecord(std::move(Record4)); |
| 376 | |
| 377 | // Set big endian output. |
| 378 | Writer.setValueProfDataEndianness(support::big); |
| 379 | |
| 380 | auto Profile = Writer.writeBuffer(); |
| 381 | readProfile(std::move(Profile)); |
| 382 | |
| 383 | // Set big endian input. |
| 384 | Reader->setValueProfDataEndianness(support::big); |
| 385 | |
| 386 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 387 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 388 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 389 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 390 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 391 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 392 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 393 | |
| 394 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 395 | R->getValueForSite(IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 396 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 397 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 398 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 399 | |
| 400 | // Restore little endian default: |
| 401 | Writer.setValueProfDataEndianness(support::little); |
| 402 | } |
| 403 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 404 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) { |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 405 | static const char caller[] = "caller"; |
| 406 | static const char callee1[] = "callee1"; |
| 407 | static const char callee2[] = "callee2"; |
| 408 | static const char callee3[] = "callee3"; |
| 409 | static const char callee4[] = "callee4"; |
| 410 | |
| 411 | InstrProfRecord Record11(caller, 0x1234, {1, 2}); |
| 412 | InstrProfRecord Record12(caller, 0x1234, {1, 2}); |
| 413 | InstrProfRecord Record2(callee1, 0x1235, {3, 4}); |
| 414 | InstrProfRecord Record3(callee2, 0x1235, {3, 4}); |
| 415 | InstrProfRecord Record4(callee3, 0x1235, {3, 4}); |
| 416 | InstrProfRecord Record5(callee3, 0x1235, {3, 4}); |
| 417 | InstrProfRecord Record6(callee4, 0x1235, {3, 5}); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 418 | |
| 419 | // 5 value sites. |
| 420 | Record11.reserveSites(IPVK_IndirectCallTarget, 5); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 421 | InstrProfValueData VD0[] = {{uint64_t(callee1), 1}, |
| 422 | {uint64_t(callee2), 2}, |
| 423 | {uint64_t(callee3), 3}, |
| 424 | {uint64_t(callee4), 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 425 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 426 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 427 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 428 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 429 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 430 | InstrProfValueData VD2[] = { |
| 431 | {uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 432 | Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 433 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 434 | InstrProfValueData VD3[] = {{uint64_t(callee1), 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 435 | Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 436 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 437 | InstrProfValueData VD4[] = {{uint64_t(callee1), 1}, |
| 438 | {uint64_t(callee2), 2}, |
| 439 | {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 440 | Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 441 | |
| 442 | // A differnt record for the same caller. |
| 443 | Record12.reserveSites(IPVK_IndirectCallTarget, 5); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 444 | InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 445 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 446 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 447 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 448 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 449 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 450 | InstrProfValueData VD22[] = { |
| 451 | {uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 452 | Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 453 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 454 | Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 455 | |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 456 | InstrProfValueData VD42[] = {{uint64_t(callee1), 1}, |
| 457 | {uint64_t(callee2), 2}, |
| 458 | {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 459 | Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 460 | |
| 461 | Writer.addRecord(std::move(Record11)); |
| 462 | // Merge profile data. |
| 463 | Writer.addRecord(std::move(Record12)); |
| 464 | |
| 465 | Writer.addRecord(std::move(Record2)); |
| 466 | Writer.addRecord(std::move(Record3)); |
| 467 | Writer.addRecord(std::move(Record4)); |
| 468 | Writer.addRecord(std::move(Record5)); |
| 469 | Writer.addRecord(std::move(Record6)); |
| 470 | auto Profile = Writer.writeBuffer(); |
| 471 | readProfile(std::move(Profile)); |
| 472 | |
| 473 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 474 | ASSERT_TRUE(NoError(R.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 475 | ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 476 | ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 477 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 478 | ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 479 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 480 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 481 | |
| 482 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 483 | R->getValueForSite(IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 484 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2")); |
| 485 | ASSERT_EQ(7U, VD[0].Count); |
| 486 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3")); |
| 487 | ASSERT_EQ(6U, VD[1].Count); |
| 488 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4")); |
| 489 | ASSERT_EQ(4U, VD[2].Count); |
| 490 | ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1")); |
| 491 | ASSERT_EQ(1U, VD[3].Count); |
| 492 | |
| 493 | std::unique_ptr<InstrProfValueData[]> VD_2( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 494 | R->getValueForSite(IPVK_IndirectCallTarget, 2)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 495 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3")); |
| 496 | ASSERT_EQ(6U, VD_2[0].Count); |
| 497 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4")); |
| 498 | ASSERT_EQ(4U, VD_2[1].Count); |
| 499 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2")); |
| 500 | ASSERT_EQ(3U, VD_2[2].Count); |
| 501 | ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1")); |
| 502 | ASSERT_EQ(1U, VD_2[3].Count); |
| 503 | |
| 504 | std::unique_ptr<InstrProfValueData[]> VD_3( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 505 | R->getValueForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 506 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1")); |
| 507 | ASSERT_EQ(1U, VD_3[0].Count); |
| 508 | |
| 509 | std::unique_ptr<InstrProfValueData[]> VD_4( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 510 | R->getValueForSite(IPVK_IndirectCallTarget, 4)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 511 | ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3")); |
| 512 | ASSERT_EQ(6U, VD_4[0].Count); |
| 513 | ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2")); |
| 514 | ASSERT_EQ(4U, VD_4[1].Count); |
| 515 | ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1")); |
| 516 | ASSERT_EQ(2U, VD_4[2].Count); |
| 517 | } |
| 518 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 519 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 520 | static const char bar[] = "bar"; |
| 521 | |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 522 | const uint64_t Max = std::numeric_limits<uint64_t>::max(); |
| 523 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 524 | InstrProfRecord Record1("foo", 0x1234, {1}); |
| 525 | auto Result1 = Writer.addRecord(std::move(Record1)); |
| 526 | ASSERT_EQ(Result1, instrprof_error::success); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 527 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 528 | // Verify counter overflow. |
| 529 | InstrProfRecord Record2("foo", 0x1234, {Max}); |
| 530 | auto Result2 = Writer.addRecord(std::move(Record2)); |
| 531 | ASSERT_EQ(Result2, instrprof_error::counter_overflow); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 532 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 533 | InstrProfRecord Record3(bar, 0x9012, {8}); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 534 | auto Result3 = Writer.addRecord(std::move(Record3)); |
| 535 | ASSERT_EQ(Result3, instrprof_error::success); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 536 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 537 | InstrProfRecord Record4("baz", 0x5678, {3, 4}); |
| 538 | Record4.reserveSites(IPVK_IndirectCallTarget, 1); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 539 | InstrProfValueData VD4[] = {{uint64_t(bar), 1}}; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 540 | Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr); |
| 541 | auto Result4 = Writer.addRecord(std::move(Record4)); |
| 542 | ASSERT_EQ(Result4, instrprof_error::success); |
| 543 | |
| 544 | // Verify value data counter overflow. |
| 545 | InstrProfRecord Record5("baz", 0x5678, {5, 6}); |
| 546 | Record5.reserveSites(IPVK_IndirectCallTarget, 1); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 547 | InstrProfValueData VD5[] = {{uint64_t(bar), Max}}; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 548 | Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr); |
| 549 | auto Result5 = Writer.addRecord(std::move(Record5)); |
| 550 | ASSERT_EQ(Result5, instrprof_error::counter_overflow); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 551 | |
| 552 | auto Profile = Writer.writeBuffer(); |
| 553 | readProfile(std::move(Profile)); |
| 554 | |
| 555 | // Verify saturation of counts. |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 556 | ErrorOr<InstrProfRecord> ReadRecord1 = |
| 557 | Reader->getInstrProfRecord("foo", 0x1234); |
| 558 | ASSERT_TRUE(NoError(ReadRecord1.getError())); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 559 | ASSERT_EQ(Max, ReadRecord1->Counts[0]); |
Nathan Slingerland | aa5702d | 2015-12-02 18:19:24 +0000 | [diff] [blame] | 560 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 561 | ErrorOr<InstrProfRecord> ReadRecord2 = |
| 562 | Reader->getInstrProfRecord("baz", 0x5678); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 563 | ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget)); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 564 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 565 | ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 566 | ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3)); |
| 567 | ASSERT_EQ(Max, VD[0].Count); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 570 | // This test tests that when there are too many values |
| 571 | // for a given site, the merged results are properly |
| 572 | // truncated. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 573 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) { |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 574 | static const char caller[] = "caller"; |
| 575 | |
| 576 | InstrProfRecord Record11(caller, 0x1234, {1, 2}); |
| 577 | InstrProfRecord Record12(caller, 0x1234, {1, 2}); |
| 578 | |
| 579 | // 2 value sites. |
| 580 | Record11.reserveSites(IPVK_IndirectCallTarget, 2); |
| 581 | InstrProfValueData VD0[255]; |
| 582 | for (int I = 0; I < 255; I++) { |
| 583 | VD0[I].Value = 2 * I; |
| 584 | VD0[I].Count = 2 * I + 1000; |
| 585 | } |
| 586 | |
| 587 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr); |
| 588 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 589 | |
| 590 | Record12.reserveSites(IPVK_IndirectCallTarget, 2); |
| 591 | InstrProfValueData VD1[255]; |
| 592 | for (int I = 0; I < 255; I++) { |
| 593 | VD1[I].Value = 2 * I + 1; |
| 594 | VD1[I].Count = 2 * I + 1001; |
| 595 | } |
| 596 | |
| 597 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr); |
| 598 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 599 | |
| 600 | Writer.addRecord(std::move(Record11)); |
| 601 | // Merge profile data. |
| 602 | Writer.addRecord(std::move(Record12)); |
| 603 | |
| 604 | auto Profile = Writer.writeBuffer(); |
| 605 | readProfile(std::move(Profile)); |
| 606 | |
| 607 | ErrorOr<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
| 608 | ASSERT_TRUE(NoError(R.getError())); |
| 609 | std::unique_ptr<InstrProfValueData[]> VD( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 610 | R->getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 611 | ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 612 | ASSERT_EQ(255U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
NAKAMURA Takumi | 249edf3 | 2016-01-08 07:58:20 +0000 | [diff] [blame] | 613 | for (unsigned I = 0; I < 255; I++) { |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 614 | ASSERT_EQ(VD[I].Value, 509 - I); |
| 615 | ASSERT_EQ(VD[I].Count, 1509 - I); |
| 616 | } |
| 617 | } |
| 618 | |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 619 | // Synthesize runtime value profile data. |
| 620 | ValueProfNode Site1Values[5] = {{{uint64_t("callee1"), 400}, &Site1Values[1]}, |
| 621 | {{uint64_t("callee2"), 1000}, &Site1Values[2]}, |
| 622 | {{uint64_t("callee3"), 500}, &Site1Values[3]}, |
| 623 | {{uint64_t("callee4"), 300}, &Site1Values[4]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 624 | {{uint64_t("callee5"), 100}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 625 | |
| 626 | ValueProfNode Site2Values[4] = {{{uint64_t("callee5"), 800}, &Site2Values[1]}, |
| 627 | {{uint64_t("callee3"), 1000}, &Site2Values[2]}, |
| 628 | {{uint64_t("callee2"), 2500}, &Site2Values[3]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 629 | {{uint64_t("callee1"), 1300}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 630 | |
| 631 | ValueProfNode Site3Values[3] = {{{uint64_t("callee6"), 800}, &Site3Values[1]}, |
| 632 | {{uint64_t("callee3"), 1000}, &Site3Values[2]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 633 | {{uint64_t("callee4"), 5500}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 634 | |
| 635 | ValueProfNode Site4Values[2] = {{{uint64_t("callee2"), 1800}, &Site4Values[1]}, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 636 | {{uint64_t("callee3"), 2000}, nullptr}}; |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 637 | |
| 638 | static ValueProfNode *ValueProfNodes[5] = {&Site1Values[0], &Site2Values[0], |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 639 | &Site3Values[0], &Site4Values[0], |
| 640 | nullptr}; |
| 641 | |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 642 | static uint16_t NumValueSites[IPVK_Last + 1] = {5}; |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 643 | TEST_P(MaybeSparseInstrProfTest, runtime_value_prof_data_read_write) { |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 644 | ValueProfRuntimeRecord RTRecord; |
| 645 | initializeValueProfRuntimeRecord(&RTRecord, &NumValueSites[0], |
| 646 | &ValueProfNodes[0]); |
| 647 | |
Xinliang David Li | 0e6a36e | 2015-12-01 19:47:32 +0000 | [diff] [blame] | 648 | ValueProfData *VPData = serializeValueProfDataFromRT(&RTRecord, nullptr); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 649 | |
| 650 | InstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2}); |
| 651 | |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 652 | VPData->deserializeTo(Record, nullptr); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 653 | |
| 654 | // Now read data from Record and sanity check the data |
| 655 | ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget)); |
| 656 | ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 657 | ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 658 | ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 659 | ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 660 | ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
| 661 | |
| 662 | auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 663 | return VD1.Count > VD2.Count; |
| 664 | }; |
| 665 | std::unique_ptr<InstrProfValueData[]> VD_0( |
| 666 | Record.getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 667 | std::sort(&VD_0[0], &VD_0[5], Cmp); |
| 668 | ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2")); |
| 669 | ASSERT_EQ(1000U, VD_0[0].Count); |
| 670 | ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3")); |
| 671 | ASSERT_EQ(500U, VD_0[1].Count); |
| 672 | ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1")); |
| 673 | ASSERT_EQ(400U, VD_0[2].Count); |
| 674 | ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4")); |
| 675 | ASSERT_EQ(300U, VD_0[3].Count); |
| 676 | ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5")); |
| 677 | ASSERT_EQ(100U, VD_0[4].Count); |
| 678 | |
| 679 | std::unique_ptr<InstrProfValueData[]> VD_1( |
| 680 | Record.getValueForSite(IPVK_IndirectCallTarget, 1)); |
| 681 | std::sort(&VD_1[0], &VD_1[4], Cmp); |
| 682 | ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2")); |
| 683 | ASSERT_EQ(2500U, VD_1[0].Count); |
| 684 | ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1")); |
| 685 | ASSERT_EQ(1300U, VD_1[1].Count); |
| 686 | ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3")); |
| 687 | ASSERT_EQ(1000U, VD_1[2].Count); |
| 688 | ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5")); |
| 689 | ASSERT_EQ(800U, VD_1[3].Count); |
| 690 | |
| 691 | std::unique_ptr<InstrProfValueData[]> VD_2( |
| 692 | Record.getValueForSite(IPVK_IndirectCallTarget, 2)); |
| 693 | std::sort(&VD_2[0], &VD_2[3], Cmp); |
| 694 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4")); |
| 695 | ASSERT_EQ(5500U, VD_2[0].Count); |
| 696 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3")); |
| 697 | ASSERT_EQ(1000U, VD_2[1].Count); |
| 698 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6")); |
| 699 | ASSERT_EQ(800U, VD_2[2].Count); |
| 700 | |
| 701 | std::unique_ptr<InstrProfValueData[]> VD_3( |
| 702 | Record.getValueForSite(IPVK_IndirectCallTarget, 3)); |
| 703 | std::sort(&VD_3[0], &VD_3[2], Cmp); |
| 704 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3")); |
| 705 | ASSERT_EQ(2000U, VD_3[0].Count); |
| 706 | ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2")); |
| 707 | ASSERT_EQ(1800U, VD_3[1].Count); |
| 708 | |
| 709 | finalizeValueProfRuntimeRecord(&RTRecord); |
| 710 | free(VPData); |
| 711 | } |
| 712 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 713 | TEST_P(MaybeSparseInstrProfTest, get_max_function_count) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 714 | InstrProfRecord Record1("foo", 0x1234, {1ULL << 31, 2}); |
| 715 | InstrProfRecord Record2("bar", 0, {1ULL << 63}); |
| 716 | InstrProfRecord Record3("baz", 0x5678, {0, 0, 0, 0}); |
| 717 | Writer.addRecord(std::move(Record1)); |
| 718 | Writer.addRecord(std::move(Record2)); |
| 719 | Writer.addRecord(std::move(Record3)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 720 | auto Profile = Writer.writeBuffer(); |
| 721 | readProfile(std::move(Profile)); |
| 722 | |
| 723 | ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount()); |
| 724 | } |
| 725 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 726 | TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) { |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 727 | InstrProfRecord Record1("foo", 0x1234, {1, 2}); |
| 728 | InstrProfRecord Record2("foo", 0x1235, {3, 4}); |
| 729 | Writer.addRecord(std::move(Record1), 3); |
| 730 | Writer.addRecord(std::move(Record2), 5); |
| 731 | auto Profile = Writer.writeBuffer(); |
| 732 | readProfile(std::move(Profile)); |
| 733 | |
| 734 | std::vector<uint64_t> Counts; |
| 735 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1234, Counts))); |
| 736 | ASSERT_EQ(2U, Counts.size()); |
| 737 | ASSERT_EQ(3U, Counts[0]); |
| 738 | ASSERT_EQ(6U, Counts[1]); |
| 739 | |
| 740 | ASSERT_TRUE(NoError(Reader->getFunctionCounts("foo", 0x1235, Counts))); |
| 741 | ASSERT_EQ(2U, Counts.size()); |
| 742 | ASSERT_EQ(15U, Counts[0]); |
| 743 | ASSERT_EQ(20U, Counts[1]); |
| 744 | } |
| 745 | |
Xinliang David Li | a0601e7 | 2016-02-09 05:47:08 +0000 | [diff] [blame] | 746 | // Testing symtab creator interface used by indexed profile reader. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 747 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) { |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 748 | std::vector<StringRef> FuncNames; |
| 749 | FuncNames.push_back("func1"); |
| 750 | FuncNames.push_back("func2"); |
| 751 | FuncNames.push_back("func3"); |
| 752 | FuncNames.push_back("bar1"); |
| 753 | FuncNames.push_back("bar2"); |
| 754 | FuncNames.push_back("bar3"); |
| 755 | InstrProfSymtab Symtab; |
| 756 | Symtab.create(FuncNames); |
| 757 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1")); |
| 758 | ASSERT_EQ(StringRef("func1"), R); |
| 759 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2")); |
| 760 | ASSERT_EQ(StringRef("func2"), R); |
| 761 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3")); |
| 762 | ASSERT_EQ(StringRef("func3"), R); |
| 763 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1")); |
| 764 | ASSERT_EQ(StringRef("bar1"), R); |
| 765 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2")); |
| 766 | ASSERT_EQ(StringRef("bar2"), R); |
| 767 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3")); |
| 768 | ASSERT_EQ(StringRef("bar3"), R); |
Xinliang David Li | c96d3d1 | 2015-12-19 18:20:09 +0000 | [diff] [blame] | 769 | |
Xinliang David Li | 86b4b91 | 2016-02-10 06:36:55 +0000 | [diff] [blame] | 770 | // negative tests |
| 771 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar4")); |
| 772 | ASSERT_EQ(StringRef(), R); |
| 773 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("foo4")); |
| 774 | ASSERT_EQ(StringRef(), R); |
| 775 | |
Xinliang David Li | c96d3d1 | 2015-12-19 18:20:09 +0000 | [diff] [blame] | 776 | // Now incrementally update the symtab |
| 777 | Symtab.addFuncName("blah_1"); |
| 778 | Symtab.addFuncName("blah_2"); |
| 779 | Symtab.addFuncName("blah_3"); |
| 780 | // Finalize it |
| 781 | Symtab.finalizeSymtab(); |
| 782 | |
| 783 | // Check again |
| 784 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1")); |
| 785 | ASSERT_EQ(StringRef("blah_1"), R); |
| 786 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2")); |
| 787 | ASSERT_EQ(StringRef("blah_2"), R); |
| 788 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3")); |
| 789 | ASSERT_EQ(StringRef("blah_3"), R); |
| 790 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1")); |
| 791 | ASSERT_EQ(StringRef("func1"), R); |
| 792 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2")); |
| 793 | ASSERT_EQ(StringRef("func2"), R); |
| 794 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3")); |
| 795 | ASSERT_EQ(StringRef("func3"), R); |
| 796 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1")); |
| 797 | ASSERT_EQ(StringRef("bar1"), R); |
| 798 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2")); |
| 799 | ASSERT_EQ(StringRef("bar2"), R); |
| 800 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3")); |
| 801 | ASSERT_EQ(StringRef("bar3"), R); |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Xinliang David Li | a0601e7 | 2016-02-09 05:47:08 +0000 | [diff] [blame] | 804 | // Testing symtab creator interface used by value profile transformer. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 805 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) { |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 806 | LLVMContext Ctx; |
| 807 | std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx); |
| 808 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 809 | /*isVarArg=*/false); |
| 810 | Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get()); |
| 811 | Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get()); |
| 812 | Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get()); |
| 813 | Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get()); |
| 814 | Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get()); |
| 815 | Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get()); |
| 816 | Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get()); |
| 817 | Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get()); |
| 818 | Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get()); |
| 819 | Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get()); |
| 820 | Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get()); |
| 821 | Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get()); |
| 822 | |
| 823 | InstrProfSymtab ProfSymtab; |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 824 | ProfSymtab.create(*M); |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 825 | |
| 826 | StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar", |
| 827 | "Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"}; |
| 828 | |
| 829 | for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) { |
| 830 | Function *F = M->getFunction(Funcs[I]); |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 831 | ASSERT_TRUE(F != nullptr); |
Xinliang David Li | da656fe | 2016-01-20 02:49:53 +0000 | [diff] [blame] | 832 | std::string PGOName = getPGOFuncName(*F); |
Xinliang David Li | 3865fdc | 2016-01-22 18:13:34 +0000 | [diff] [blame] | 833 | uint64_t Key = IndexedInstrProf::ComputeHash(PGOName); |
Xinliang David Li | da656fe | 2016-01-20 02:49:53 +0000 | [diff] [blame] | 834 | ASSERT_EQ(StringRef(PGOName), |
Xinliang David Li | 3865fdc | 2016-01-22 18:13:34 +0000 | [diff] [blame] | 835 | ProfSymtab.getFuncName(Key)); |
| 836 | ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key)); |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
Xinliang David Li | a0601e7 | 2016-02-09 05:47:08 +0000 | [diff] [blame] | 840 | // Testing symtab serialization and creator/deserialization interface |
| 841 | // used by coverage map reader, and raw profile reader. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 842 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 843 | std::vector<std::string> FuncNames1; |
| 844 | std::vector<std::string> FuncNames2; |
Xinliang David Li | c2f25cc4 | 2016-02-09 05:36:57 +0000 | [diff] [blame] | 845 | for (int I = 0; I < 3; I++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 846 | std::string str; |
| 847 | raw_string_ostream OS(str); |
| 848 | OS << "func_" << I; |
| 849 | FuncNames1.push_back(OS.str()); |
| 850 | str.clear(); |
| 851 | OS << "fooooooooooooooo_" << I; |
| 852 | FuncNames1.push_back(OS.str()); |
| 853 | str.clear(); |
| 854 | OS << "BAR_" << I; |
| 855 | FuncNames2.push_back(OS.str()); |
| 856 | str.clear(); |
| 857 | OS << "BlahblahBlahblahBar_" << I; |
| 858 | FuncNames2.push_back(OS.str()); |
| 859 | } |
| 860 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 861 | for (bool DoCompression : {false, true}) { |
| 862 | // Compressing: |
| 863 | std::string FuncNameStrings1; |
| 864 | collectPGOFuncNameStrings( |
| 865 | FuncNames1, (DoCompression && zlib::isAvailable()), FuncNameStrings1); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 866 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 867 | // Compressing: |
| 868 | std::string FuncNameStrings2; |
| 869 | collectPGOFuncNameStrings( |
| 870 | FuncNames2, (DoCompression && zlib::isAvailable()), FuncNameStrings2); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 871 | |
Xinliang David Li | c2f25cc4 | 2016-02-09 05:36:57 +0000 | [diff] [blame] | 872 | for (int Padding = 0; Padding < 2; Padding++) { |
| 873 | // Join with paddings : |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 874 | std::string FuncNameStrings = FuncNameStrings1; |
| 875 | for (int P = 0; P < Padding; P++) { |
| 876 | FuncNameStrings.push_back('\0'); |
| 877 | } |
| 878 | FuncNameStrings += FuncNameStrings2; |
| 879 | |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 880 | // Now decompress: |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 881 | InstrProfSymtab Symtab; |
| 882 | Symtab.create(StringRef(FuncNameStrings)); |
| 883 | |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 884 | // Now do the checks: |
| 885 | // First sampling some data points: |
Xinliang David Li | a8ba7af | 2016-01-29 21:26:31 +0000 | [diff] [blame] | 886 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0])); |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 887 | ASSERT_EQ(StringRef("func_0"), R); |
| 888 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1])); |
| 889 | ASSERT_EQ(StringRef("fooooooooooooooo_0"), R); |
Xinliang David Li | c2f25cc4 | 2016-02-09 05:36:57 +0000 | [diff] [blame] | 890 | for (int I = 0; I < 3; I++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 891 | std::string N[4]; |
| 892 | N[0] = FuncNames1[2 * I]; |
| 893 | N[1] = FuncNames1[2 * I + 1]; |
| 894 | N[2] = FuncNames2[2 * I]; |
| 895 | N[3] = FuncNames2[2 * I + 1]; |
| 896 | for (int J = 0; J < 4; J++) { |
| 897 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J])); |
| 898 | ASSERT_EQ(StringRef(N[J]), R); |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 905 | TEST_F(SparseInstrProfTest, preserve_no_records) { |
| 906 | InstrProfRecord Record1("foo", 0x1234, {0}); |
| 907 | InstrProfRecord Record2("bar", 0x4321, {0, 0}); |
| 908 | InstrProfRecord Record3("bar", 0x4321, {0, 0, 0}); |
| 909 | |
| 910 | Writer.addRecord(std::move(Record1)); |
| 911 | Writer.addRecord(std::move(Record2)); |
| 912 | Writer.addRecord(std::move(Record3)); |
| 913 | auto Profile = Writer.writeBuffer(); |
| 914 | readProfile(std::move(Profile)); |
| 915 | |
| 916 | auto I = Reader->begin(), E = Reader->end(); |
| 917 | ASSERT_TRUE(I == E); |
| 918 | } |
| 919 | |
| 920 | INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest, |
| 921 | ::testing::Bool()); |
| 922 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 923 | } // end anonymous namespace |