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