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