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