Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 1 | //===- unittest/ProfileData/InstrProfTest.cpp -------------------*- C++ -*-===// |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 10 | #include "llvm/IR/Function.h" |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 11 | #include "llvm/IR/IRBuilder.h" |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 12 | #include "llvm/IR/LLVMContext.h" |
| 13 | #include "llvm/IR/Module.h" |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 14 | #include "llvm/ProfileData/InstrProfReader.h" |
| 15 | #include "llvm/ProfileData/InstrProfWriter.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compression.h" |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 17 | #include "llvm/Testing/Support/Error.h" |
| 18 | #include "llvm/Testing/Support/SupportHelpers.h" |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 19 | #include "gtest/gtest.h" |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 20 | #include <cstdarg> |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 24 | LLVM_NODISCARD static ::testing::AssertionResult |
| 25 | ErrorEquals(instrprof_error Expected, Error E) { |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 26 | instrprof_error Found; |
| 27 | std::string FoundMsg; |
| 28 | handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { |
| 29 | Found = IPE.get(); |
| 30 | FoundMsg = IPE.message(); |
| 31 | }); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 32 | if (Expected == Found) |
| 33 | return ::testing::AssertionSuccess(); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 34 | return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n"; |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | struct InstrProfTest : ::testing::Test { |
| 40 | InstrProfWriter Writer; |
| 41 | std::unique_ptr<IndexedInstrProfReader> Reader; |
| 42 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 43 | void SetUp() { Writer.setOutputSparse(false); } |
| 44 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 45 | void readProfile(std::unique_ptr<MemoryBuffer> Profile) { |
| 46 | auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 47 | EXPECT_THAT_ERROR(ReaderOrErr.takeError(), Succeeded()); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 48 | Reader = std::move(ReaderOrErr.get()); |
| 49 | } |
| 50 | }; |
| 51 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 52 | struct SparseInstrProfTest : public InstrProfTest { |
| 53 | void SetUp() { Writer.setOutputSparse(true); } |
| 54 | }; |
| 55 | |
| 56 | struct MaybeSparseInstrProfTest : public InstrProfTest, |
| 57 | public ::testing::WithParamInterface<bool> { |
Vedant Kumar | 0259b7b | 2016-03-22 15:14:18 +0000 | [diff] [blame] | 58 | void SetUp() { Writer.setOutputSparse(GetParam()); } |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | TEST_P(MaybeSparseInstrProfTest, write_and_read_empty_profile) { |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 62 | auto Profile = Writer.writeBuffer(); |
| 63 | readProfile(std::move(Profile)); |
| 64 | ASSERT_TRUE(Reader->begin() == Reader->end()); |
| 65 | } |
| 66 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 67 | static const auto Err = [](Error E) { |
| 68 | consumeError(std::move(E)); |
| 69 | FAIL(); |
| 70 | }; |
| 71 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 72 | TEST_P(MaybeSparseInstrProfTest, write_and_read_one_function) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 73 | Writer.addRecord({"foo", 0x1234, {1, 2, 3, 4}}, Err); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 74 | auto Profile = Writer.writeBuffer(); |
| 75 | readProfile(std::move(Profile)); |
| 76 | |
| 77 | auto I = Reader->begin(), E = Reader->end(); |
| 78 | ASSERT_TRUE(I != E); |
| 79 | ASSERT_EQ(StringRef("foo"), I->Name); |
| 80 | ASSERT_EQ(0x1234U, I->Hash); |
| 81 | ASSERT_EQ(4U, I->Counts.size()); |
| 82 | ASSERT_EQ(1U, I->Counts[0]); |
| 83 | ASSERT_EQ(2U, I->Counts[1]); |
| 84 | ASSERT_EQ(3U, I->Counts[2]); |
| 85 | ASSERT_EQ(4U, I->Counts[3]); |
| 86 | ASSERT_TRUE(++I == E); |
| 87 | } |
| 88 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 89 | TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 90 | Writer.addRecord({"foo", 0x1234, {1, 2}}, Err); |
| 91 | Writer.addRecord({"foo", 0x1235, {3, 4}}, Err); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 92 | auto Profile = Writer.writeBuffer(); |
| 93 | readProfile(std::move(Profile)); |
| 94 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 95 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("foo", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 96 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 97 | ASSERT_EQ(2U, R->Counts.size()); |
| 98 | ASSERT_EQ(1U, R->Counts[0]); |
| 99 | ASSERT_EQ(2U, R->Counts[1]); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 100 | |
| 101 | R = Reader->getInstrProfRecord("foo", 0x1235); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 102 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 103 | ASSERT_EQ(2U, R->Counts.size()); |
| 104 | ASSERT_EQ(3U, R->Counts[0]); |
| 105 | ASSERT_EQ(4U, R->Counts[1]); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 106 | |
| 107 | R = Reader->getInstrProfRecord("foo", 0x5678); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 108 | ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.takeError())); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 109 | |
| 110 | R = Reader->getInstrProfRecord("bar", 0x1234); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 111 | ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError())); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 114 | TEST_P(MaybeSparseInstrProfTest, get_function_counts) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 115 | Writer.addRecord({"foo", 0x1234, {1, 2}}, Err); |
| 116 | Writer.addRecord({"foo", 0x1235, {3, 4}}, Err); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 117 | auto Profile = Writer.writeBuffer(); |
| 118 | readProfile(std::move(Profile)); |
| 119 | |
| 120 | std::vector<uint64_t> Counts; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 121 | EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1234, Counts), |
| 122 | Succeeded()); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 123 | ASSERT_EQ(2U, Counts.size()); |
| 124 | ASSERT_EQ(1U, Counts[0]); |
| 125 | ASSERT_EQ(2U, Counts[1]); |
| 126 | |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 127 | EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1235, Counts), |
| 128 | Succeeded()); |
Justin Bogner | 09829f4 | 2015-06-22 23:56:53 +0000 | [diff] [blame] | 129 | ASSERT_EQ(2U, Counts.size()); |
| 130 | ASSERT_EQ(3U, Counts[0]); |
| 131 | ASSERT_EQ(4U, Counts[1]); |
| 132 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 133 | Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts); |
| 134 | ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, std::move(E1))); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 135 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 136 | Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts); |
| 137 | ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, std::move(E2))); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 140 | // Profile data is copied from general.proftext |
| 141 | TEST_F(InstrProfTest, get_profile_summary) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 142 | Writer.addRecord({"func1", 0x1234, {97531}}, Err); |
| 143 | Writer.addRecord({"func2", 0x1234, {0, 0}}, Err); |
| 144 | Writer.addRecord( |
| 145 | {"func3", |
| 146 | 0x1234, |
| 147 | {2305843009213693952, 1152921504606846976, 576460752303423488, |
| 148 | 288230376151711744, 144115188075855872, 72057594037927936}}, |
| 149 | Err); |
| 150 | Writer.addRecord({"func4", 0x1234, {0}}, Err); |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 151 | auto Profile = Writer.writeBuffer(); |
| 152 | readProfile(std::move(Profile)); |
| 153 | |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 154 | auto VerifySummary = [](ProfileSummary &IPS) mutable { |
| 155 | ASSERT_EQ(ProfileSummary::PSK_Instr, IPS.getKind()); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 156 | ASSERT_EQ(2305843009213693952U, IPS.getMaxFunctionCount()); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 157 | ASSERT_EQ(2305843009213693952U, IPS.getMaxCount()); |
| 158 | ASSERT_EQ(10U, IPS.getNumCounts()); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 159 | ASSERT_EQ(4539628424389557499U, IPS.getTotalCount()); |
| 160 | std::vector<ProfileSummaryEntry> &Details = IPS.getDetailedSummary(); |
| 161 | uint32_t Cutoff = 800000; |
| 162 | auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) { |
| 163 | return PE.Cutoff == Cutoff; |
| 164 | }; |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 165 | auto EightyPerc = find_if(Details, Predicate); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 166 | Cutoff = 900000; |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 167 | auto NinetyPerc = find_if(Details, Predicate); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 168 | Cutoff = 950000; |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 169 | auto NinetyFivePerc = find_if(Details, Predicate); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 170 | Cutoff = 990000; |
David Majnemer | 562e829 | 2016-08-12 00:18:03 +0000 | [diff] [blame] | 171 | auto NinetyNinePerc = find_if(Details, Predicate); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 172 | ASSERT_EQ(576460752303423488U, EightyPerc->MinCount); |
| 173 | ASSERT_EQ(288230376151711744U, NinetyPerc->MinCount); |
| 174 | ASSERT_EQ(288230376151711744U, NinetyFivePerc->MinCount); |
| 175 | ASSERT_EQ(72057594037927936U, NinetyNinePerc->MinCount); |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 176 | }; |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 177 | ProfileSummary &PS = Reader->getSummary(); |
Easwaran Raman | f9709ee | 2016-03-14 22:23:28 +0000 | [diff] [blame] | 178 | VerifySummary(PS); |
Easwaran Raman | 26628d3 | 2016-03-18 21:29:30 +0000 | [diff] [blame] | 179 | |
| 180 | // Test that conversion of summary to and from Metadata works. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 181 | LLVMContext Context; |
| 182 | Metadata *MD = PS.getMD(Context); |
Easwaran Raman | 7c4f25d | 2016-03-01 18:30:58 +0000 | [diff] [blame] | 183 | ASSERT_TRUE(MD); |
| 184 | ProfileSummary *PSFromMD = ProfileSummary::getFromMD(MD); |
| 185 | ASSERT_TRUE(PSFromMD); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 186 | VerifySummary(*PSFromMD); |
| 187 | delete PSFromMD; |
Easwaran Raman | 26628d3 | 2016-03-18 21:29:30 +0000 | [diff] [blame] | 188 | |
| 189 | // Test that summary can be attached to and read back from module. |
Mehdi Amini | 03b42e4 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 190 | Module M("my_module", Context); |
Easwaran Raman | 26628d3 | 2016-03-18 21:29:30 +0000 | [diff] [blame] | 191 | M.setProfileSummary(MD); |
| 192 | MD = M.getProfileSummary(); |
| 193 | ASSERT_TRUE(MD); |
| 194 | PSFromMD = ProfileSummary::getFromMD(MD); |
| 195 | ASSERT_TRUE(PSFromMD); |
Easwaran Raman | 7cefdb8 | 2016-05-19 21:53:28 +0000 | [diff] [blame] | 196 | VerifySummary(*PSFromMD); |
| 197 | delete PSFromMD; |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 200 | TEST_F(InstrProfTest, test_writer_merge) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 201 | Writer.addRecord({"func1", 0x1234, {42}}, Err); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 202 | |
| 203 | InstrProfWriter Writer2; |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 204 | Writer2.addRecord({"func2", 0x1234, {0, 0}}, Err); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 205 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 206 | Writer.mergeRecordsFromWriter(std::move(Writer2), Err); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 207 | |
| 208 | auto Profile = Writer.writeBuffer(); |
| 209 | readProfile(std::move(Profile)); |
| 210 | |
| 211 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("func1", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 212 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 213 | ASSERT_EQ(1U, R->Counts.size()); |
| 214 | ASSERT_EQ(42U, R->Counts[0]); |
| 215 | |
| 216 | R = Reader->getInstrProfRecord("func2", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 217 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
Vedant Kumar | e3a0bf5 | 2016-07-19 01:17:20 +0000 | [diff] [blame] | 218 | ASSERT_EQ(2U, R->Counts.size()); |
| 219 | ASSERT_EQ(0U, R->Counts[0]); |
| 220 | ASSERT_EQ(0U, R->Counts[1]); |
| 221 | } |
| 222 | |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 223 | static const char callee1[] = "callee1"; |
| 224 | static const char callee2[] = "callee2"; |
| 225 | static const char callee3[] = "callee3"; |
| 226 | static const char callee4[] = "callee4"; |
| 227 | static const char callee5[] = "callee5"; |
| 228 | static const char callee6[] = "callee6"; |
| 229 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 230 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) { |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 231 | NamedInstrProfRecord Record1("caller", 0x1234, {1, 2}); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 232 | |
| 233 | // 4 value sites. |
| 234 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 235 | InstrProfValueData VD0[] = { |
| 236 | {(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 237 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 238 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 239 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 240 | InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 241 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 242 | InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 243 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 244 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 245 | Writer.addRecord(std::move(Record1), Err); |
| 246 | Writer.addRecord({"callee1", 0x1235, {3, 4}}, Err); |
| 247 | Writer.addRecord({"callee2", 0x1235, {3, 4}}, Err); |
| 248 | Writer.addRecord({"callee3", 0x1235, {3, 4}}, Err); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 249 | auto Profile = Writer.writeBuffer(); |
| 250 | readProfile(std::move(Profile)); |
| 251 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 252 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 253 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 254 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 255 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 256 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 257 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 258 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 259 | |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 260 | uint64_t TotalC; |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 261 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 262 | R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 263 | |
| 264 | ASSERT_EQ(3U, VD[0].Count); |
| 265 | ASSERT_EQ(2U, VD[1].Count); |
| 266 | ASSERT_EQ(1U, VD[2].Count); |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 267 | ASSERT_EQ(6U, TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 268 | |
| 269 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 270 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 271 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 272 | } |
| 273 | |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 274 | TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) { |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 275 | NamedInstrProfRecord Record("caller", 0x1234, {1, 2}); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 276 | Record.reserveSites(IPVK_IndirectCallTarget, 1); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 277 | InstrProfValueData VD0[] = {{1000, 1}, {2000, 2}, {3000, 3}, {5000, 5}, |
| 278 | {4000, 4}, {6000, 6}}; |
| 279 | Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 6, nullptr); |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 280 | Writer.addRecord(std::move(Record), Err); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 281 | auto Profile = Writer.writeBuffer(); |
| 282 | readProfile(std::move(Profile)); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 283 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 284 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 285 | |
| 286 | LLVMContext Ctx; |
| 287 | std::unique_ptr<Module> M(new Module("MyModule", Ctx)); |
| 288 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 289 | /*isVarArg=*/false); |
| 290 | Function *F = |
| 291 | Function::Create(FTy, Function::ExternalLinkage, "caller", M.get()); |
| 292 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 293 | |
| 294 | IRBuilder<> Builder(BB); |
| 295 | BasicBlock *TBB = BasicBlock::Create(Ctx, "", F); |
| 296 | BasicBlock *FBB = BasicBlock::Create(Ctx, "", F); |
| 297 | |
| 298 | // Use branch instruction to annotate with value profile data for simplicity |
| 299 | Instruction *Inst = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
| 300 | Instruction *Inst2 = Builder.CreateCondBr(Builder.getTrue(), TBB, FBB); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 301 | annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 302 | |
| 303 | InstrProfValueData ValueData[5]; |
| 304 | uint32_t N; |
| 305 | uint64_t T; |
| 306 | bool Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5, |
| 307 | ValueData, N, T); |
| 308 | ASSERT_TRUE(Res); |
| 309 | ASSERT_EQ(3U, N); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 310 | ASSERT_EQ(21U, T); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 311 | // The result should be sorted already: |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 312 | ASSERT_EQ(6000U, ValueData[0].Value); |
| 313 | ASSERT_EQ(6U, ValueData[0].Count); |
| 314 | ASSERT_EQ(5000U, ValueData[1].Value); |
| 315 | ASSERT_EQ(5U, ValueData[1].Count); |
| 316 | ASSERT_EQ(4000U, ValueData[2].Value); |
| 317 | ASSERT_EQ(4U, ValueData[2].Count); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 318 | Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 1, ValueData, |
| 319 | N, T); |
| 320 | ASSERT_TRUE(Res); |
| 321 | ASSERT_EQ(1U, N); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 322 | ASSERT_EQ(21U, T); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 323 | |
| 324 | Res = getValueProfDataFromInst(*Inst2, IPVK_IndirectCallTarget, 5, ValueData, |
| 325 | N, T); |
| 326 | ASSERT_FALSE(Res); |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 327 | |
| 328 | // Remove the MD_prof metadata |
| 329 | Inst->setMetadata(LLVMContext::MD_prof, 0); |
| 330 | // Annotate 5 records this time. |
| 331 | annotateValueSite(*M, *Inst, R.get(), IPVK_IndirectCallTarget, 0, 5); |
| 332 | Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5, |
| 333 | ValueData, N, T); |
| 334 | ASSERT_TRUE(Res); |
| 335 | ASSERT_EQ(5U, N); |
| 336 | ASSERT_EQ(21U, T); |
| 337 | ASSERT_EQ(6000U, ValueData[0].Value); |
| 338 | ASSERT_EQ(6U, ValueData[0].Count); |
| 339 | ASSERT_EQ(5000U, ValueData[1].Value); |
| 340 | ASSERT_EQ(5U, ValueData[1].Count); |
| 341 | ASSERT_EQ(4000U, ValueData[2].Value); |
| 342 | ASSERT_EQ(4U, ValueData[2].Count); |
| 343 | ASSERT_EQ(3000U, ValueData[3].Value); |
| 344 | ASSERT_EQ(3U, ValueData[3].Count); |
| 345 | ASSERT_EQ(2000U, ValueData[4].Value); |
| 346 | ASSERT_EQ(2U, ValueData[4].Count); |
Rong Xu | bb49490 | 2016-02-12 21:36:17 +0000 | [diff] [blame] | 347 | |
| 348 | // Remove the MD_prof metadata |
| 349 | Inst->setMetadata(LLVMContext::MD_prof, 0); |
| 350 | // Annotate with 4 records. |
| 351 | InstrProfValueData VD0Sorted[] = {{1000, 6}, {2000, 5}, {3000, 4}, {4000, 3}, |
| 352 | {5000, 2}, {6000, 1}}; |
Rong Xu | 311ada1 | 2016-03-30 16:56:31 +0000 | [diff] [blame] | 353 | annotateValueSite(*M, *Inst, makeArrayRef(VD0Sorted).slice(2), 10, |
| 354 | IPVK_IndirectCallTarget, 5); |
Rong Xu | bb49490 | 2016-02-12 21:36:17 +0000 | [diff] [blame] | 355 | Res = getValueProfDataFromInst(*Inst, IPVK_IndirectCallTarget, 5, |
| 356 | ValueData, N, T); |
| 357 | ASSERT_TRUE(Res); |
| 358 | ASSERT_EQ(4U, N); |
| 359 | ASSERT_EQ(10U, T); |
| 360 | ASSERT_EQ(3000U, ValueData[0].Value); |
| 361 | ASSERT_EQ(4U, ValueData[0].Count); |
| 362 | ASSERT_EQ(4000U, ValueData[1].Value); |
| 363 | ASSERT_EQ(3U, ValueData[1].Count); |
| 364 | ASSERT_EQ(5000U, ValueData[2].Value); |
| 365 | ASSERT_EQ(2U, ValueData[2].Count); |
| 366 | ASSERT_EQ(6000U, ValueData[3].Value); |
| 367 | ASSERT_EQ(1U, ValueData[3].Count); |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 370 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) { |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 371 | NamedInstrProfRecord Record1("caller", 0x1234, {1, 2}); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 372 | |
| 373 | // 4 value sites. |
| 374 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 375 | InstrProfValueData VD0[] = { |
| 376 | {(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}}; |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 377 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 378 | // No value profile data at the second site. |
| 379 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 380 | InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}}; |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 381 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 382 | InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}}; |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 383 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
| 384 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 385 | Writer.addRecord(std::move(Record1), 10, Err); |
| 386 | Writer.addRecord({"callee1", 0x1235, {3, 4}}, Err); |
| 387 | Writer.addRecord({"callee2", 0x1235, {3, 4}}, Err); |
| 388 | Writer.addRecord({"callee3", 0x1235, {3, 4}}, Err); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 389 | auto Profile = Writer.writeBuffer(); |
| 390 | readProfile(std::move(Profile)); |
| 391 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 392 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 393 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 394 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 395 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 396 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 397 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 398 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 399 | |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 400 | uint64_t TotalC; |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 401 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 402 | R->getValueForSite(IPVK_IndirectCallTarget, 0, &TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 403 | ASSERT_EQ(30U, VD[0].Count); |
| 404 | ASSERT_EQ(20U, VD[1].Count); |
| 405 | ASSERT_EQ(10U, VD[2].Count); |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 406 | ASSERT_EQ(60U, TotalC); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 407 | |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 408 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 409 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 410 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 411 | } |
| 412 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 413 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) { |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 414 | NamedInstrProfRecord Record1("caller", 0x1234, {1, 2}); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 415 | |
| 416 | // 4 value sites. |
| 417 | Record1.reserveSites(IPVK_IndirectCallTarget, 4); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 418 | InstrProfValueData VD0[] = { |
| 419 | {(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}, {(uint64_t)callee3, 3}}; |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 420 | Record1.addValueData(IPVK_IndirectCallTarget, 0, VD0, 3, nullptr); |
| 421 | // No value profile data at the second site. |
| 422 | Record1.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 423 | InstrProfValueData VD2[] = {{(uint64_t)callee1, 1}, {(uint64_t)callee2, 2}}; |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 424 | Record1.addValueData(IPVK_IndirectCallTarget, 2, VD2, 2, nullptr); |
Xinliang David Li | a3e0d45 | 2016-04-10 02:35:53 +0000 | [diff] [blame] | 425 | InstrProfValueData VD3[] = {{(uint64_t)callee1, 1}}; |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 426 | Record1.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
| 427 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 428 | Writer.addRecord(std::move(Record1), Err); |
| 429 | Writer.addRecord({"callee1", 0x1235, {3, 4}}, Err); |
| 430 | Writer.addRecord({"callee2", 0x1235, {3, 4}}, Err); |
| 431 | Writer.addRecord({"callee3", 0x1235, {3, 4}}, Err); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 432 | |
| 433 | // Set big endian output. |
| 434 | Writer.setValueProfDataEndianness(support::big); |
| 435 | |
| 436 | auto Profile = Writer.writeBuffer(); |
| 437 | readProfile(std::move(Profile)); |
| 438 | |
| 439 | // Set big endian input. |
| 440 | Reader->setValueProfDataEndianness(support::big); |
| 441 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 442 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 443 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 444 | ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 445 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 446 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 447 | ASSERT_EQ(2U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 448 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 449 | |
| 450 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 451 | R->getValueForSite(IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 452 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee3")); |
| 453 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee2")); |
| 454 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee1")); |
| 455 | |
| 456 | // Restore little endian default: |
| 457 | Writer.setValueProfDataEndianness(support::little); |
| 458 | } |
| 459 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 460 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) { |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 461 | static const char caller[] = "caller"; |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 462 | NamedInstrProfRecord Record11(caller, 0x1234, {1, 2}); |
| 463 | NamedInstrProfRecord Record12(caller, 0x1234, {1, 2}); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 464 | |
| 465 | // 5 value sites. |
| 466 | Record11.reserveSites(IPVK_IndirectCallTarget, 5); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 467 | InstrProfValueData VD0[] = {{uint64_t(callee1), 1}, |
| 468 | {uint64_t(callee2), 2}, |
| 469 | {uint64_t(callee3), 3}, |
| 470 | {uint64_t(callee4), 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 471 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 4, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 472 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 473 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 474 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 475 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 476 | InstrProfValueData VD2[] = { |
| 477 | {uint64_t(callee1), 1}, {uint64_t(callee2), 2}, {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 478 | Record11.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 479 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 480 | InstrProfValueData VD3[] = {{uint64_t(callee1), 1}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 481 | Record11.addValueData(IPVK_IndirectCallTarget, 3, VD3, 1, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 482 | |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 483 | InstrProfValueData VD4[] = {{uint64_t(callee1), 1}, |
| 484 | {uint64_t(callee2), 2}, |
| 485 | {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 486 | Record11.addValueData(IPVK_IndirectCallTarget, 4, VD4, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 487 | |
David Majnemer | 68318e0 | 2016-04-22 06:37:48 +0000 | [diff] [blame] | 488 | // A different record for the same caller. |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 489 | Record12.reserveSites(IPVK_IndirectCallTarget, 5); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 490 | InstrProfValueData VD02[] = {{uint64_t(callee2), 5}, {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 491 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD02, 2, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 492 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 493 | // No value profile data at the second site. |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 494 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 495 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 496 | InstrProfValueData VD22[] = { |
| 497 | {uint64_t(callee2), 1}, {uint64_t(callee3), 3}, {uint64_t(callee4), 4}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 498 | Record12.addValueData(IPVK_IndirectCallTarget, 2, VD22, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 499 | |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 500 | Record12.addValueData(IPVK_IndirectCallTarget, 3, nullptr, 0, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 501 | |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 502 | InstrProfValueData VD42[] = {{uint64_t(callee1), 1}, |
| 503 | {uint64_t(callee2), 2}, |
| 504 | {uint64_t(callee3), 3}}; |
Eugene Zelenko | ffec81c | 2015-11-04 22:32:32 +0000 | [diff] [blame] | 505 | Record12.addValueData(IPVK_IndirectCallTarget, 4, VD42, 3, nullptr); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 506 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 507 | Writer.addRecord(std::move(Record11), Err); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 508 | // Merge profile data. |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 509 | Writer.addRecord(std::move(Record12), Err); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 510 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 511 | Writer.addRecord({callee1, 0x1235, {3, 4}}, Err); |
| 512 | Writer.addRecord({callee2, 0x1235, {3, 4}}, Err); |
| 513 | Writer.addRecord({callee3, 0x1235, {3, 4}}, Err); |
| 514 | Writer.addRecord({callee3, 0x1235, {3, 4}}, Err); |
| 515 | Writer.addRecord({callee4, 0x1235, {3, 5}}, Err); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 516 | auto Profile = Writer.writeBuffer(); |
| 517 | readProfile(std::move(Profile)); |
| 518 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 519 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 520 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 521 | ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 522 | ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 523 | ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 524 | ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 525 | ASSERT_EQ(1U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 526 | ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 527 | |
| 528 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 529 | R->getValueForSite(IPVK_IndirectCallTarget, 0); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 530 | ASSERT_EQ(StringRef((const char *)VD[0].Value, 7), StringRef("callee2")); |
| 531 | ASSERT_EQ(7U, VD[0].Count); |
| 532 | ASSERT_EQ(StringRef((const char *)VD[1].Value, 7), StringRef("callee3")); |
| 533 | ASSERT_EQ(6U, VD[1].Count); |
| 534 | ASSERT_EQ(StringRef((const char *)VD[2].Value, 7), StringRef("callee4")); |
| 535 | ASSERT_EQ(4U, VD[2].Count); |
| 536 | ASSERT_EQ(StringRef((const char *)VD[3].Value, 7), StringRef("callee1")); |
| 537 | ASSERT_EQ(1U, VD[3].Count); |
| 538 | |
| 539 | std::unique_ptr<InstrProfValueData[]> VD_2( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 540 | R->getValueForSite(IPVK_IndirectCallTarget, 2)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 541 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee3")); |
| 542 | ASSERT_EQ(6U, VD_2[0].Count); |
| 543 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee4")); |
| 544 | ASSERT_EQ(4U, VD_2[1].Count); |
| 545 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee2")); |
| 546 | ASSERT_EQ(3U, VD_2[2].Count); |
| 547 | ASSERT_EQ(StringRef((const char *)VD_2[3].Value, 7), StringRef("callee1")); |
| 548 | ASSERT_EQ(1U, VD_2[3].Count); |
| 549 | |
| 550 | std::unique_ptr<InstrProfValueData[]> VD_3( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 551 | R->getValueForSite(IPVK_IndirectCallTarget, 3)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 552 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee1")); |
| 553 | ASSERT_EQ(1U, VD_3[0].Count); |
| 554 | |
| 555 | std::unique_ptr<InstrProfValueData[]> VD_4( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 556 | R->getValueForSite(IPVK_IndirectCallTarget, 4)); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 557 | ASSERT_EQ(StringRef((const char *)VD_4[0].Value, 7), StringRef("callee3")); |
| 558 | ASSERT_EQ(6U, VD_4[0].Count); |
| 559 | ASSERT_EQ(StringRef((const char *)VD_4[1].Value, 7), StringRef("callee2")); |
| 560 | ASSERT_EQ(4U, VD_4[1].Count); |
| 561 | ASSERT_EQ(StringRef((const char *)VD_4[2].Value, 7), StringRef("callee1")); |
| 562 | ASSERT_EQ(2U, VD_4[2].Count); |
| 563 | } |
| 564 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 565 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 566 | static const char bar[] = "bar"; |
| 567 | |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 568 | const uint64_t Max = std::numeric_limits<uint64_t>::max(); |
| 569 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 570 | instrprof_error Result; |
| 571 | auto Err = [&](Error E) { Result = InstrProfError::take(std::move(E)); }; |
| 572 | Result = instrprof_error::success; |
| 573 | Writer.addRecord({"foo", 0x1234, {1}}, Err); |
| 574 | ASSERT_EQ(Result, instrprof_error::success); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 575 | |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 576 | // Verify counter overflow. |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 577 | Result = instrprof_error::success; |
| 578 | Writer.addRecord({"foo", 0x1234, {Max}}, Err); |
| 579 | ASSERT_EQ(Result, instrprof_error::counter_overflow); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 580 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 581 | Result = instrprof_error::success; |
| 582 | Writer.addRecord({bar, 0x9012, {8}}, Err); |
| 583 | ASSERT_EQ(Result, instrprof_error::success); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 584 | |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 585 | NamedInstrProfRecord Record4("baz", 0x5678, {3, 4}); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 586 | Record4.reserveSites(IPVK_IndirectCallTarget, 1); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 587 | InstrProfValueData VD4[] = {{uint64_t(bar), 1}}; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 588 | Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr); |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 589 | Result = instrprof_error::success; |
| 590 | Writer.addRecord(std::move(Record4), Err); |
| 591 | ASSERT_EQ(Result, instrprof_error::success); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 592 | |
| 593 | // Verify value data counter overflow. |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 594 | NamedInstrProfRecord Record5("baz", 0x5678, {5, 6}); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 595 | Record5.reserveSites(IPVK_IndirectCallTarget, 1); |
NAKAMURA Takumi | 7345ac0 | 2015-12-27 06:18:57 +0000 | [diff] [blame] | 596 | InstrProfValueData VD5[] = {{uint64_t(bar), Max}}; |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 597 | Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr); |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 598 | Result = instrprof_error::success; |
| 599 | Writer.addRecord(std::move(Record5), Err); |
| 600 | ASSERT_EQ(Result, instrprof_error::counter_overflow); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 601 | |
| 602 | auto Profile = Writer.writeBuffer(); |
| 603 | readProfile(std::move(Profile)); |
| 604 | |
| 605 | // Verify saturation of counts. |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 606 | Expected<InstrProfRecord> ReadRecord1 = |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 607 | Reader->getInstrProfRecord("foo", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 608 | EXPECT_THAT_ERROR(ReadRecord1.takeError(), Succeeded()); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 609 | ASSERT_EQ(Max, ReadRecord1->Counts[0]); |
Nathan Slingerland | aa5702d | 2015-12-02 18:19:24 +0000 | [diff] [blame] | 610 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 611 | Expected<InstrProfRecord> ReadRecord2 = |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 612 | Reader->getInstrProfRecord("baz", 0x5678); |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 613 | ASSERT_TRUE(bool(ReadRecord2)); |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 614 | ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget)); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 615 | std::unique_ptr<InstrProfValueData[]> VD = |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 616 | ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0); |
Nathan Slingerland | 48dd080 | 2015-12-16 21:45:43 +0000 | [diff] [blame] | 617 | ASSERT_EQ(StringRef("bar"), StringRef((const char *)VD[0].Value, 3)); |
| 618 | ASSERT_EQ(Max, VD[0].Count); |
Daniel Sanders | be9db3c | 2015-11-20 13:13:53 +0000 | [diff] [blame] | 619 | } |
| 620 | |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 621 | // This test tests that when there are too many values |
| 622 | // for a given site, the merged results are properly |
| 623 | // truncated. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 624 | TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) { |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 625 | static const char caller[] = "caller"; |
| 626 | |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 627 | NamedInstrProfRecord Record11(caller, 0x1234, {1, 2}); |
| 628 | NamedInstrProfRecord Record12(caller, 0x1234, {1, 2}); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 629 | |
| 630 | // 2 value sites. |
| 631 | Record11.reserveSites(IPVK_IndirectCallTarget, 2); |
| 632 | InstrProfValueData VD0[255]; |
| 633 | for (int I = 0; I < 255; I++) { |
| 634 | VD0[I].Value = 2 * I; |
| 635 | VD0[I].Count = 2 * I + 1000; |
| 636 | } |
| 637 | |
| 638 | Record11.addValueData(IPVK_IndirectCallTarget, 0, VD0, 255, nullptr); |
| 639 | Record11.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 640 | |
| 641 | Record12.reserveSites(IPVK_IndirectCallTarget, 2); |
| 642 | InstrProfValueData VD1[255]; |
| 643 | for (int I = 0; I < 255; I++) { |
| 644 | VD1[I].Value = 2 * I + 1; |
| 645 | VD1[I].Count = 2 * I + 1001; |
| 646 | } |
| 647 | |
| 648 | Record12.addValueData(IPVK_IndirectCallTarget, 0, VD1, 255, nullptr); |
| 649 | Record12.addValueData(IPVK_IndirectCallTarget, 1, nullptr, 0, nullptr); |
| 650 | |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 651 | Writer.addRecord(std::move(Record11), Err); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 652 | // Merge profile data. |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 653 | Writer.addRecord(std::move(Record12), Err); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 654 | |
| 655 | auto Profile = Writer.writeBuffer(); |
| 656 | readProfile(std::move(Profile)); |
| 657 | |
Vedant Kumar | 9152fd1 | 2016-05-19 03:54:45 +0000 | [diff] [blame] | 658 | Expected<InstrProfRecord> R = Reader->getInstrProfRecord("caller", 0x1234); |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 659 | EXPECT_THAT_ERROR(R.takeError(), Succeeded()); |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 660 | std::unique_ptr<InstrProfValueData[]> VD( |
David Blaikie | fed557e | 2016-02-09 01:02:24 +0000 | [diff] [blame] | 661 | R->getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 662 | ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget)); |
| 663 | ASSERT_EQ(255U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
NAKAMURA Takumi | 249edf3 | 2016-01-08 07:58:20 +0000 | [diff] [blame] | 664 | for (unsigned I = 0; I < 255; I++) { |
Xinliang David Li | 872df22 | 2016-01-08 06:54:27 +0000 | [diff] [blame] | 665 | ASSERT_EQ(VD[I].Value, 509 - I); |
| 666 | ASSERT_EQ(VD[I].Count, 1509 - I); |
| 667 | } |
| 668 | } |
| 669 | |
Xinliang David Li | 8e6b917 | 2016-05-13 00:23:49 +0000 | [diff] [blame] | 670 | static void addValueProfData(InstrProfRecord &Record) { |
| 671 | Record.reserveSites(IPVK_IndirectCallTarget, 5); |
| 672 | InstrProfValueData VD0[] = {{uint64_t(callee1), 400}, |
| 673 | {uint64_t(callee2), 1000}, |
| 674 | {uint64_t(callee3), 500}, |
| 675 | {uint64_t(callee4), 300}, |
| 676 | {uint64_t(callee5), 100}}; |
| 677 | Record.addValueData(IPVK_IndirectCallTarget, 0, VD0, 5, nullptr); |
| 678 | InstrProfValueData VD1[] = {{uint64_t(callee5), 800}, |
| 679 | {uint64_t(callee3), 1000}, |
| 680 | {uint64_t(callee2), 2500}, |
| 681 | {uint64_t(callee1), 1300}}; |
| 682 | Record.addValueData(IPVK_IndirectCallTarget, 1, VD1, 4, nullptr); |
| 683 | InstrProfValueData VD2[] = {{uint64_t(callee6), 800}, |
| 684 | {uint64_t(callee3), 1000}, |
| 685 | {uint64_t(callee4), 5500}}; |
| 686 | Record.addValueData(IPVK_IndirectCallTarget, 2, VD2, 3, nullptr); |
| 687 | InstrProfValueData VD3[] = {{uint64_t(callee2), 1800}, |
| 688 | {uint64_t(callee3), 2000}}; |
| 689 | Record.addValueData(IPVK_IndirectCallTarget, 3, VD3, 2, nullptr); |
| 690 | Record.addValueData(IPVK_IndirectCallTarget, 4, nullptr, 0, nullptr); |
| 691 | } |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 692 | |
Xinliang David Li | 8e6b917 | 2016-05-13 00:23:49 +0000 | [diff] [blame] | 693 | TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write) { |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 694 | InstrProfRecord SrcRecord({1ULL << 31, 2}); |
Xinliang David Li | 8e6b917 | 2016-05-13 00:23:49 +0000 | [diff] [blame] | 695 | addValueProfData(SrcRecord); |
| 696 | std::unique_ptr<ValueProfData> VPData = |
| 697 | ValueProfData::serializeFrom(SrcRecord); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 698 | |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 699 | InstrProfRecord Record({1ULL << 31, 2}); |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 700 | VPData->deserializeTo(Record, nullptr); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 701 | |
| 702 | // Now read data from Record and sanity check the data |
| 703 | ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget)); |
| 704 | ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 705 | ASSERT_EQ(4U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); |
| 706 | ASSERT_EQ(3U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 2)); |
| 707 | ASSERT_EQ(2U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 3)); |
| 708 | ASSERT_EQ(0U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 4)); |
| 709 | |
| 710 | auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 711 | return VD1.Count > VD2.Count; |
| 712 | }; |
| 713 | std::unique_ptr<InstrProfValueData[]> VD_0( |
| 714 | Record.getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 715 | std::sort(&VD_0[0], &VD_0[5], Cmp); |
| 716 | ASSERT_EQ(StringRef((const char *)VD_0[0].Value, 7), StringRef("callee2")); |
| 717 | ASSERT_EQ(1000U, VD_0[0].Count); |
| 718 | ASSERT_EQ(StringRef((const char *)VD_0[1].Value, 7), StringRef("callee3")); |
| 719 | ASSERT_EQ(500U, VD_0[1].Count); |
| 720 | ASSERT_EQ(StringRef((const char *)VD_0[2].Value, 7), StringRef("callee1")); |
| 721 | ASSERT_EQ(400U, VD_0[2].Count); |
| 722 | ASSERT_EQ(StringRef((const char *)VD_0[3].Value, 7), StringRef("callee4")); |
| 723 | ASSERT_EQ(300U, VD_0[3].Count); |
| 724 | ASSERT_EQ(StringRef((const char *)VD_0[4].Value, 7), StringRef("callee5")); |
| 725 | ASSERT_EQ(100U, VD_0[4].Count); |
| 726 | |
| 727 | std::unique_ptr<InstrProfValueData[]> VD_1( |
| 728 | Record.getValueForSite(IPVK_IndirectCallTarget, 1)); |
| 729 | std::sort(&VD_1[0], &VD_1[4], Cmp); |
| 730 | ASSERT_EQ(StringRef((const char *)VD_1[0].Value, 7), StringRef("callee2")); |
| 731 | ASSERT_EQ(2500U, VD_1[0].Count); |
| 732 | ASSERT_EQ(StringRef((const char *)VD_1[1].Value, 7), StringRef("callee1")); |
| 733 | ASSERT_EQ(1300U, VD_1[1].Count); |
| 734 | ASSERT_EQ(StringRef((const char *)VD_1[2].Value, 7), StringRef("callee3")); |
| 735 | ASSERT_EQ(1000U, VD_1[2].Count); |
| 736 | ASSERT_EQ(StringRef((const char *)VD_1[3].Value, 7), StringRef("callee5")); |
| 737 | ASSERT_EQ(800U, VD_1[3].Count); |
| 738 | |
| 739 | std::unique_ptr<InstrProfValueData[]> VD_2( |
| 740 | Record.getValueForSite(IPVK_IndirectCallTarget, 2)); |
| 741 | std::sort(&VD_2[0], &VD_2[3], Cmp); |
| 742 | ASSERT_EQ(StringRef((const char *)VD_2[0].Value, 7), StringRef("callee4")); |
| 743 | ASSERT_EQ(5500U, VD_2[0].Count); |
| 744 | ASSERT_EQ(StringRef((const char *)VD_2[1].Value, 7), StringRef("callee3")); |
| 745 | ASSERT_EQ(1000U, VD_2[1].Count); |
| 746 | ASSERT_EQ(StringRef((const char *)VD_2[2].Value, 7), StringRef("callee6")); |
| 747 | ASSERT_EQ(800U, VD_2[2].Count); |
| 748 | |
| 749 | std::unique_ptr<InstrProfValueData[]> VD_3( |
| 750 | Record.getValueForSite(IPVK_IndirectCallTarget, 3)); |
| 751 | std::sort(&VD_3[0], &VD_3[2], Cmp); |
| 752 | ASSERT_EQ(StringRef((const char *)VD_3[0].Value, 7), StringRef("callee3")); |
| 753 | ASSERT_EQ(2000U, VD_3[0].Count); |
| 754 | ASSERT_EQ(StringRef((const char *)VD_3[1].Value, 7), StringRef("callee2")); |
| 755 | ASSERT_EQ(1800U, VD_3[1].Count); |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Xinliang David Li | 8e6b917 | 2016-05-13 00:23:49 +0000 | [diff] [blame] | 758 | TEST_P(MaybeSparseInstrProfTest, value_prof_data_read_write_mapping) { |
Xinliang David Li | 2846448 | 2016-04-10 03:32:02 +0000 | [diff] [blame] | 759 | |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 760 | NamedInstrProfRecord SrcRecord("caller", 0x1234, {1ULL << 31, 2}); |
Xinliang David Li | 8e6b917 | 2016-05-13 00:23:49 +0000 | [diff] [blame] | 761 | addValueProfData(SrcRecord); |
| 762 | std::unique_ptr<ValueProfData> VPData = |
| 763 | ValueProfData::serializeFrom(SrcRecord); |
Xinliang David Li | 2846448 | 2016-04-10 03:32:02 +0000 | [diff] [blame] | 764 | |
David Blaikie | cf9d52c | 2017-07-06 19:00:12 +0000 | [diff] [blame] | 765 | NamedInstrProfRecord Record("caller", 0x1234, {1ULL << 31, 2}); |
Xinliang David Li | 2846448 | 2016-04-10 03:32:02 +0000 | [diff] [blame] | 766 | InstrProfSymtab Symtab; |
| 767 | Symtab.mapAddress(uint64_t(callee1), 0x1000ULL); |
| 768 | Symtab.mapAddress(uint64_t(callee2), 0x2000ULL); |
| 769 | Symtab.mapAddress(uint64_t(callee3), 0x3000ULL); |
| 770 | Symtab.mapAddress(uint64_t(callee4), 0x4000ULL); |
| 771 | // Missing mapping for callee5 |
| 772 | Symtab.finalizeSymtab(); |
| 773 | |
| 774 | VPData->deserializeTo(Record, &Symtab.getAddrHashMap()); |
| 775 | |
| 776 | // Now read data from Record and sanity check the data |
Xinliang David Li | 8e6b917 | 2016-05-13 00:23:49 +0000 | [diff] [blame] | 777 | ASSERT_EQ(5U, Record.getNumValueSites(IPVK_IndirectCallTarget)); |
Xinliang David Li | 2846448 | 2016-04-10 03:32:02 +0000 | [diff] [blame] | 778 | ASSERT_EQ(5U, Record.getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); |
| 779 | |
| 780 | auto Cmp = [](const InstrProfValueData &VD1, const InstrProfValueData &VD2) { |
| 781 | return VD1.Count > VD2.Count; |
| 782 | }; |
| 783 | std::unique_ptr<InstrProfValueData[]> VD_0( |
| 784 | Record.getValueForSite(IPVK_IndirectCallTarget, 0)); |
| 785 | std::sort(&VD_0[0], &VD_0[5], Cmp); |
| 786 | ASSERT_EQ(VD_0[0].Value, 0x2000ULL); |
| 787 | ASSERT_EQ(1000U, VD_0[0].Count); |
| 788 | ASSERT_EQ(VD_0[1].Value, 0x3000ULL); |
| 789 | ASSERT_EQ(500U, VD_0[1].Count); |
| 790 | ASSERT_EQ(VD_0[2].Value, 0x1000ULL); |
| 791 | ASSERT_EQ(400U, VD_0[2].Count); |
| 792 | |
| 793 | // callee5 does not have a mapped value -- default to 0. |
| 794 | ASSERT_EQ(VD_0[4].Value, 0ULL); |
| 795 | } |
| 796 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 797 | TEST_P(MaybeSparseInstrProfTest, get_max_function_count) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 798 | Writer.addRecord({"foo", 0x1234, {1ULL << 31, 2}}, Err); |
| 799 | Writer.addRecord({"bar", 0, {1ULL << 63}}, Err); |
| 800 | Writer.addRecord({"baz", 0x5678, {0, 0, 0, 0}}, Err); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 801 | auto Profile = Writer.writeBuffer(); |
| 802 | readProfile(std::move(Profile)); |
| 803 | |
| 804 | ASSERT_EQ(1ULL << 63, Reader->getMaximumFunctionCount()); |
| 805 | } |
| 806 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 807 | TEST_P(MaybeSparseInstrProfTest, get_weighted_function_counts) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 808 | Writer.addRecord({"foo", 0x1234, {1, 2}}, 3, Err); |
| 809 | Writer.addRecord({"foo", 0x1235, {3, 4}}, 5, Err); |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 810 | auto Profile = Writer.writeBuffer(); |
| 811 | readProfile(std::move(Profile)); |
| 812 | |
| 813 | std::vector<uint64_t> Counts; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 814 | EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1234, Counts), |
| 815 | Succeeded()); |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 816 | ASSERT_EQ(2U, Counts.size()); |
| 817 | ASSERT_EQ(3U, Counts[0]); |
| 818 | ASSERT_EQ(6U, Counts[1]); |
| 819 | |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 820 | EXPECT_THAT_ERROR(Reader->getFunctionCounts("foo", 0x1235, Counts), |
| 821 | Succeeded()); |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 822 | ASSERT_EQ(2U, Counts.size()); |
| 823 | ASSERT_EQ(15U, Counts[0]); |
| 824 | ASSERT_EQ(20U, Counts[1]); |
| 825 | } |
| 826 | |
Xinliang David Li | a0601e7 | 2016-02-09 05:47:08 +0000 | [diff] [blame] | 827 | // Testing symtab creator interface used by indexed profile reader. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 828 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_test) { |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 829 | std::vector<StringRef> FuncNames; |
| 830 | FuncNames.push_back("func1"); |
| 831 | FuncNames.push_back("func2"); |
| 832 | FuncNames.push_back("func3"); |
| 833 | FuncNames.push_back("bar1"); |
| 834 | FuncNames.push_back("bar2"); |
| 835 | FuncNames.push_back("bar3"); |
| 836 | InstrProfSymtab Symtab; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 837 | EXPECT_THAT_ERROR(Symtab.create(FuncNames), Succeeded()); |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 838 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1")); |
| 839 | ASSERT_EQ(StringRef("func1"), R); |
| 840 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2")); |
| 841 | ASSERT_EQ(StringRef("func2"), R); |
| 842 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3")); |
| 843 | ASSERT_EQ(StringRef("func3"), R); |
| 844 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1")); |
| 845 | ASSERT_EQ(StringRef("bar1"), R); |
| 846 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2")); |
| 847 | ASSERT_EQ(StringRef("bar2"), R); |
| 848 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3")); |
| 849 | ASSERT_EQ(StringRef("bar3"), R); |
Xinliang David Li | c96d3d1 | 2015-12-19 18:20:09 +0000 | [diff] [blame] | 850 | |
Xinliang David Li | 86b4b91 | 2016-02-10 06:36:55 +0000 | [diff] [blame] | 851 | // negative tests |
| 852 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar4")); |
| 853 | ASSERT_EQ(StringRef(), R); |
| 854 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("foo4")); |
| 855 | ASSERT_EQ(StringRef(), R); |
| 856 | |
Xinliang David Li | c96d3d1 | 2015-12-19 18:20:09 +0000 | [diff] [blame] | 857 | // Now incrementally update the symtab |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 858 | EXPECT_THAT_ERROR(Symtab.addFuncName("blah_1"), Succeeded()); |
| 859 | EXPECT_THAT_ERROR(Symtab.addFuncName("blah_2"), Succeeded()); |
| 860 | EXPECT_THAT_ERROR(Symtab.addFuncName("blah_3"), Succeeded()); |
Xinliang David Li | c96d3d1 | 2015-12-19 18:20:09 +0000 | [diff] [blame] | 861 | // Finalize it |
| 862 | Symtab.finalizeSymtab(); |
| 863 | |
| 864 | // Check again |
| 865 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_1")); |
| 866 | ASSERT_EQ(StringRef("blah_1"), R); |
| 867 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_2")); |
| 868 | ASSERT_EQ(StringRef("blah_2"), R); |
| 869 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("blah_3")); |
| 870 | ASSERT_EQ(StringRef("blah_3"), R); |
| 871 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func1")); |
| 872 | ASSERT_EQ(StringRef("func1"), R); |
| 873 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func2")); |
| 874 | ASSERT_EQ(StringRef("func2"), R); |
| 875 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("func3")); |
| 876 | ASSERT_EQ(StringRef("func3"), R); |
| 877 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar1")); |
| 878 | ASSERT_EQ(StringRef("bar1"), R); |
| 879 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar2")); |
| 880 | ASSERT_EQ(StringRef("bar2"), R); |
| 881 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash("bar3")); |
| 882 | ASSERT_EQ(StringRef("bar3"), R); |
Xinliang David Li | 2ee5c4d | 2015-12-19 07:44:57 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Vedant Kumar | b5794ca | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 885 | // Test that we get an error when creating a bogus symtab. |
| 886 | TEST_P(MaybeSparseInstrProfTest, instr_prof_bogus_symtab_empty_func_name) { |
| 887 | InstrProfSymtab Symtab; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 888 | EXPECT_TRUE(ErrorEquals(instrprof_error::malformed, Symtab.addFuncName(""))); |
Vedant Kumar | b5794ca | 2017-06-20 01:38:56 +0000 | [diff] [blame] | 889 | } |
| 890 | |
Xinliang David Li | a0601e7 | 2016-02-09 05:47:08 +0000 | [diff] [blame] | 891 | // Testing symtab creator interface used by value profile transformer. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 892 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_module_test) { |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 893 | LLVMContext Ctx; |
| 894 | std::unique_ptr<Module> M = llvm::make_unique<Module>("MyModule.cpp", Ctx); |
| 895 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(Ctx), |
| 896 | /*isVarArg=*/false); |
| 897 | Function::Create(FTy, Function::ExternalLinkage, "Gfoo", M.get()); |
| 898 | Function::Create(FTy, Function::ExternalLinkage, "Gblah", M.get()); |
| 899 | Function::Create(FTy, Function::ExternalLinkage, "Gbar", M.get()); |
| 900 | Function::Create(FTy, Function::InternalLinkage, "Ifoo", M.get()); |
| 901 | Function::Create(FTy, Function::InternalLinkage, "Iblah", M.get()); |
| 902 | Function::Create(FTy, Function::InternalLinkage, "Ibar", M.get()); |
| 903 | Function::Create(FTy, Function::PrivateLinkage, "Pfoo", M.get()); |
| 904 | Function::Create(FTy, Function::PrivateLinkage, "Pblah", M.get()); |
| 905 | Function::Create(FTy, Function::PrivateLinkage, "Pbar", M.get()); |
| 906 | Function::Create(FTy, Function::WeakODRLinkage, "Wfoo", M.get()); |
| 907 | Function::Create(FTy, Function::WeakODRLinkage, "Wblah", M.get()); |
| 908 | Function::Create(FTy, Function::WeakODRLinkage, "Wbar", M.get()); |
| 909 | |
| 910 | InstrProfSymtab ProfSymtab; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 911 | EXPECT_THAT_ERROR(ProfSymtab.create(*M), Succeeded()); |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 912 | |
| 913 | StringRef Funcs[] = {"Gfoo", "Gblah", "Gbar", "Ifoo", "Iblah", "Ibar", |
| 914 | "Pfoo", "Pblah", "Pbar", "Wfoo", "Wblah", "Wbar"}; |
| 915 | |
| 916 | for (unsigned I = 0; I < sizeof(Funcs) / sizeof(*Funcs); I++) { |
| 917 | Function *F = M->getFunction(Funcs[I]); |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 918 | ASSERT_TRUE(F != nullptr); |
Xinliang David Li | da656fe | 2016-01-20 02:49:53 +0000 | [diff] [blame] | 919 | std::string PGOName = getPGOFuncName(*F); |
Xinliang David Li | 3865fdc | 2016-01-22 18:13:34 +0000 | [diff] [blame] | 920 | uint64_t Key = IndexedInstrProf::ComputeHash(PGOName); |
Xinliang David Li | da656fe | 2016-01-20 02:49:53 +0000 | [diff] [blame] | 921 | ASSERT_EQ(StringRef(PGOName), |
Xinliang David Li | 3865fdc | 2016-01-22 18:13:34 +0000 | [diff] [blame] | 922 | ProfSymtab.getFuncName(Key)); |
| 923 | ASSERT_EQ(StringRef(Funcs[I]), ProfSymtab.getOrigFuncName(Key)); |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Xinliang David Li | a0601e7 | 2016-02-09 05:47:08 +0000 | [diff] [blame] | 927 | // Testing symtab serialization and creator/deserialization interface |
| 928 | // used by coverage map reader, and raw profile reader. |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 929 | TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 930 | std::vector<std::string> FuncNames1; |
| 931 | std::vector<std::string> FuncNames2; |
Xinliang David Li | c2f25cc4 | 2016-02-09 05:36:57 +0000 | [diff] [blame] | 932 | for (int I = 0; I < 3; I++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 933 | std::string str; |
| 934 | raw_string_ostream OS(str); |
| 935 | OS << "func_" << I; |
| 936 | FuncNames1.push_back(OS.str()); |
| 937 | str.clear(); |
Vedant Kumar | 86705ba | 2016-03-28 21:06:42 +0000 | [diff] [blame] | 938 | OS << "f oooooooooooooo_" << I; |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 939 | FuncNames1.push_back(OS.str()); |
| 940 | str.clear(); |
| 941 | OS << "BAR_" << I; |
| 942 | FuncNames2.push_back(OS.str()); |
| 943 | str.clear(); |
| 944 | OS << "BlahblahBlahblahBar_" << I; |
| 945 | FuncNames2.push_back(OS.str()); |
| 946 | } |
| 947 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 948 | for (bool DoCompression : {false, true}) { |
| 949 | // Compressing: |
| 950 | std::string FuncNameStrings1; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 951 | EXPECT_THAT_ERROR(collectPGOFuncNameStrings( |
| 952 | FuncNames1, (DoCompression && zlib::isAvailable()), |
| 953 | FuncNameStrings1), |
| 954 | Succeeded()); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 955 | |
Xinliang David Li | dd4ae7b | 2016-01-29 22:29:15 +0000 | [diff] [blame] | 956 | // Compressing: |
| 957 | std::string FuncNameStrings2; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 958 | EXPECT_THAT_ERROR(collectPGOFuncNameStrings( |
| 959 | FuncNames2, (DoCompression && zlib::isAvailable()), |
| 960 | FuncNameStrings2), |
| 961 | Succeeded()); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 962 | |
Xinliang David Li | c2f25cc4 | 2016-02-09 05:36:57 +0000 | [diff] [blame] | 963 | for (int Padding = 0; Padding < 2; Padding++) { |
| 964 | // Join with paddings : |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 965 | std::string FuncNameStrings = FuncNameStrings1; |
| 966 | for (int P = 0; P < Padding; P++) { |
| 967 | FuncNameStrings.push_back('\0'); |
| 968 | } |
| 969 | FuncNameStrings += FuncNameStrings2; |
| 970 | |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 971 | // Now decompress: |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 972 | InstrProfSymtab Symtab; |
David Blaikie | 94b98b2 | 2017-07-07 21:02:59 +0000 | [diff] [blame] | 973 | EXPECT_THAT_ERROR(Symtab.create(StringRef(FuncNameStrings)), Succeeded()); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 974 | |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 975 | // Now do the checks: |
| 976 | // First sampling some data points: |
Xinliang David Li | a8ba7af | 2016-01-29 21:26:31 +0000 | [diff] [blame] | 977 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[0])); |
Xinliang David Li | 8dec8b1 | 2016-01-04 23:59:14 +0000 | [diff] [blame] | 978 | ASSERT_EQ(StringRef("func_0"), R); |
| 979 | R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(FuncNames1[1])); |
Vedant Kumar | 86705ba | 2016-03-28 21:06:42 +0000 | [diff] [blame] | 980 | ASSERT_EQ(StringRef("f oooooooooooooo_0"), R); |
Xinliang David Li | c2f25cc4 | 2016-02-09 05:36:57 +0000 | [diff] [blame] | 981 | for (int I = 0; I < 3; I++) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 982 | std::string N[4]; |
| 983 | N[0] = FuncNames1[2 * I]; |
| 984 | N[1] = FuncNames1[2 * I + 1]; |
| 985 | N[2] = FuncNames2[2 * I]; |
| 986 | N[3] = FuncNames2[2 * I + 1]; |
| 987 | for (int J = 0; J < 4; J++) { |
| 988 | StringRef R = Symtab.getFuncName(IndexedInstrProf::ComputeHash(N[J])); |
| 989 | ASSERT_EQ(StringRef(N[J]), R); |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 996 | TEST_F(SparseInstrProfTest, preserve_no_records) { |
David Blaikie | 98cce00 | 2017-07-10 03:04:59 +0000 | [diff] [blame] | 997 | Writer.addRecord({"foo", 0x1234, {0}}, Err); |
| 998 | Writer.addRecord({"bar", 0x4321, {0, 0}}, Err); |
Vedant Kumar | 910d3d0 | 2017-07-10 21:44:43 +0000 | [diff] [blame] | 999 | Writer.addRecord({"baz", 0x4321, {0, 0, 0}}, Err); |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 1000 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 1001 | auto Profile = Writer.writeBuffer(); |
| 1002 | readProfile(std::move(Profile)); |
| 1003 | |
| 1004 | auto I = Reader->begin(), E = Reader->end(); |
| 1005 | ASSERT_TRUE(I == E); |
| 1006 | } |
| 1007 | |
| 1008 | INSTANTIATE_TEST_CASE_P(MaybeSparse, MaybeSparseInstrProfTest, |
Galina Kistanova | 9417735 | 2017-06-04 05:30:26 +0000 | [diff] [blame] | 1009 | ::testing::Bool(),); |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 1010 | |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 1011 | } // end anonymous namespace |