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