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