Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 1 | //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=// |
| 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 | // |
| 10 | // This file contains support for writing profiling data for clang's |
| 11 | // instrumentation based PGO and coverage. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "llvm/ProfileData/InstrProfWriter.h" |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/EndianStream.h" |
| 18 | #include "llvm/Support/OnDiskHashTable.h" |
Reid Kleckner | 437b1b3 | 2015-11-20 19:29:40 +0000 | [diff] [blame] | 19 | #include <tuple> |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 20 | |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 23 | // A struct to define how the data stream should be patched. For Indexed |
| 24 | // profiling, only uint64_t data type is needed. |
| 25 | struct PatchItem { |
| 26 | uint64_t Pos; // Where to patch. |
| 27 | uint64_t *D; // Pointer to an array of source data. |
| 28 | int N; // Number of elements in \c D array. |
| 29 | }; |
| 30 | |
| 31 | namespace llvm { |
| 32 | // A wrapper class to abstract writer stream with support of bytes |
| 33 | // back patching. |
Xinliang David Li | 285d7bd | 2016-01-15 19:22:41 +0000 | [diff] [blame] | 34 | class ProfOStream { |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 35 | |
Xinliang David Li | 285d7bd | 2016-01-15 19:22:41 +0000 | [diff] [blame] | 36 | public: |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 37 | ProfOStream(llvm::raw_fd_ostream &FD) : IsFDOStream(true), OS(FD), LE(FD) {} |
| 38 | ProfOStream(llvm::raw_string_ostream &STR) |
| 39 | : IsFDOStream(false), OS(STR), LE(STR) {} |
| 40 | |
| 41 | uint64_t tell() { return OS.tell(); } |
| 42 | void write(uint64_t V) { LE.write<uint64_t>(V); } |
| 43 | // \c patch can only be called when all data is written and flushed. |
| 44 | // For raw_string_ostream, the patch is done on the target string |
| 45 | // directly and it won't be reflected in the stream's internal buffer. |
| 46 | void patch(PatchItem *P, int NItems) { |
| 47 | using namespace support; |
| 48 | if (IsFDOStream) { |
| 49 | llvm::raw_fd_ostream &FDOStream = static_cast<llvm::raw_fd_ostream &>(OS); |
| 50 | for (int K = 0; K < NItems; K++) { |
| 51 | FDOStream.seek(P[K].Pos); |
| 52 | for (int I = 0; I < P[K].N; I++) |
| 53 | write(P[K].D[I]); |
| 54 | } |
| 55 | } else { |
| 56 | llvm::raw_string_ostream &SOStream = |
| 57 | static_cast<llvm::raw_string_ostream &>(OS); |
| 58 | std::string &Data = SOStream.str(); // with flush |
| 59 | for (int K = 0; K < NItems; K++) { |
| 60 | for (int I = 0; I < P[K].N; I++) { |
| 61 | uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]); |
| 62 | Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t), |
| 63 | (const char *)&Bytes, sizeof(uint64_t)); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | // If \c OS is an instance of \c raw_fd_ostream, this field will be |
| 69 | // true. Otherwise, \c OS will be an raw_string_ostream. |
| 70 | bool IsFDOStream; |
| 71 | raw_ostream &OS; |
| 72 | support::endian::Writer<support::little> LE; |
| 73 | }; |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 74 | |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 75 | class InstrProfRecordWriterTrait { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 76 | public: |
| 77 | typedef StringRef key_type; |
| 78 | typedef StringRef key_type_ref; |
| 79 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 80 | typedef const InstrProfWriter::ProfilingData *const data_type; |
| 81 | typedef const InstrProfWriter::ProfilingData *const data_type_ref; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 82 | |
| 83 | typedef uint64_t hash_value_type; |
| 84 | typedef uint64_t offset_type; |
| 85 | |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 86 | support::endianness ValueProfDataEndianness; |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 87 | ProfileSummary *TheProfileSummary; |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 88 | |
| 89 | InstrProfRecordWriterTrait() : ValueProfDataEndianness(support::little) {} |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 90 | static hash_value_type ComputeHash(key_type_ref K) { |
Xinliang David Li | 49ee76d | 2015-12-18 22:22:12 +0000 | [diff] [blame] | 91 | return IndexedInstrProf::ComputeHash(K); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static std::pair<offset_type, offset_type> |
| 95 | EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) { |
| 96 | using namespace llvm::support; |
| 97 | endian::Writer<little> LE(Out); |
| 98 | |
Justin Bogner | e808171 | 2014-04-19 00:33:15 +0000 | [diff] [blame] | 99 | offset_type N = K.size(); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 100 | LE.write<offset_type>(N); |
| 101 | |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 102 | offset_type M = 0; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 103 | for (const auto &ProfileData : *V) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 104 | const InstrProfRecord &ProfRecord = ProfileData.second; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 105 | M += sizeof(uint64_t); // The function hash |
| 106 | M += sizeof(uint64_t); // The size of the Counts vector |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 107 | M += ProfRecord.Counts.size() * sizeof(uint64_t); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 108 | |
| 109 | // Value data |
Xinliang David Li | 9955687 | 2015-11-17 23:00:40 +0000 | [diff] [blame] | 110 | M += ValueProfData::getSize(ProfileData.second); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 111 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 112 | LE.write<offset_type>(M); |
| 113 | |
| 114 | return std::make_pair(N, M); |
| 115 | } |
| 116 | |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 117 | void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 118 | Out.write(K.data(), N); |
| 119 | } |
| 120 | |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 121 | void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) { |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 122 | using namespace llvm::support; |
| 123 | endian::Writer<little> LE(Out); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 124 | for (const auto &ProfileData : *V) { |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 125 | const InstrProfRecord &ProfRecord = ProfileData.second; |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 126 | TheProfileSummary->addRecord(ProfRecord); |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 127 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 128 | LE.write<uint64_t>(ProfileData.first); // Function hash |
Xinliang David Li | 2004f00 | 2015-11-02 05:08:23 +0000 | [diff] [blame] | 129 | LE.write<uint64_t>(ProfRecord.Counts.size()); |
Xinliang David Li | 6aa216c | 2015-11-06 07:54:21 +0000 | [diff] [blame] | 130 | for (uint64_t I : ProfRecord.Counts) |
| 131 | LE.write<uint64_t>(I); |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 132 | |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 133 | // Write value data |
Xinliang David Li | 9955687 | 2015-11-17 23:00:40 +0000 | [diff] [blame] | 134 | std::unique_ptr<ValueProfData> VDataPtr = |
| 135 | ValueProfData::serializeFrom(ProfileData.second); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 136 | uint32_t S = VDataPtr->getSize(); |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 137 | VDataPtr->swapBytesFromHost(ValueProfDataEndianness); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 138 | Out.write((const char *)VDataPtr.get(), S); |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 139 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 140 | } |
| 141 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 142 | } |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 143 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 144 | InstrProfWriter::InstrProfWriter(bool Sparse) |
Rong Xu | 1288a19 | 2016-02-08 21:07:46 +0000 | [diff] [blame] | 145 | : Sparse(Sparse), FunctionData(), ProfileKind(PF_Unknown), |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 146 | InfoObj(new InstrProfRecordWriterTrait()) {} |
| 147 | |
| 148 | InstrProfWriter::~InstrProfWriter() { delete InfoObj; } |
| 149 | |
Xinliang David Li | 46ad363 | 2016-01-22 19:53:31 +0000 | [diff] [blame] | 150 | // Internal interface for testing purpose only. |
| 151 | void InstrProfWriter::setValueProfDataEndianness( |
| 152 | support::endianness Endianness) { |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 153 | InfoObj->ValueProfDataEndianness = Endianness; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 154 | } |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 155 | void InstrProfWriter::setOutputSparse(bool Sparse) { |
| 156 | this->Sparse = Sparse; |
| 157 | } |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 158 | |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 159 | std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I, |
| 160 | uint64_t Weight) { |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 161 | auto &ProfileDataMap = FunctionData[I.Name]; |
| 162 | |
Nathan Slingerland | a731829 | 2015-11-20 19:12:43 +0000 | [diff] [blame] | 163 | bool NewFunc; |
| 164 | ProfilingData::iterator Where; |
| 165 | std::tie(Where, NewFunc) = |
| 166 | ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord())); |
| 167 | InstrProfRecord &Dest = Where->second; |
Nathan Slingerland | aa5702d | 2015-12-02 18:19:24 +0000 | [diff] [blame] | 168 | |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 169 | instrprof_error Result = instrprof_error::success; |
Nathan Slingerland | a731829 | 2015-11-20 19:12:43 +0000 | [diff] [blame] | 170 | if (NewFunc) { |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 171 | // We've never seen a function with this name and hash, add it. |
Nathan Slingerland | a731829 | 2015-11-20 19:12:43 +0000 | [diff] [blame] | 172 | Dest = std::move(I); |
Xinliang David Li | 6005728 | 2015-12-20 08:46:18 +0000 | [diff] [blame] | 173 | // Fix up the name to avoid dangling reference. |
| 174 | Dest.Name = FunctionData.find(Dest.Name)->getKey(); |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 175 | if (Weight > 1) |
| 176 | Result = Dest.scale(Weight); |
Nathan Slingerland | a731829 | 2015-11-20 19:12:43 +0000 | [diff] [blame] | 177 | } else { |
| 178 | // We're updating a function we've seen before. |
Nathan Slingerland | 7f5b47d | 2015-12-15 17:37:09 +0000 | [diff] [blame] | 179 | Result = Dest.merge(I, Weight); |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Xinliang David Li | 062cde9 | 2016-01-08 05:45:21 +0000 | [diff] [blame] | 182 | Dest.sortValueData(); |
| 183 | |
Nathan Slingerland | aa5702d | 2015-12-02 18:19:24 +0000 | [diff] [blame] | 184 | return Result; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 187 | bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) { |
| 188 | if (!Sparse) |
| 189 | return true; |
| 190 | for (const auto &Func : PD) { |
| 191 | const InstrProfRecord &IPR = Func.second; |
| 192 | if (std::any_of(IPR.Counts.begin(), IPR.Counts.end(), |
| 193 | [](uint64_t Count) { return Count > 0; })) |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 199 | static void setSummary(IndexedInstrProf::Summary *TheSummary, |
| 200 | ProfileSummary &PS) { |
| 201 | using namespace IndexedInstrProf; |
| 202 | std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary(); |
| 203 | TheSummary->NumSummaryFields = Summary::NumKinds; |
| 204 | TheSummary->NumCutoffEntries = Res.size(); |
| 205 | TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount()); |
| 206 | TheSummary->set(Summary::MaxBlockCount, PS.getMaxBlockCount()); |
| 207 | TheSummary->set(Summary::MaxInternalBlockCount, |
| 208 | PS.getMaxInternalBlockCount()); |
| 209 | TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount()); |
| 210 | TheSummary->set(Summary::TotalNumBlocks, PS.getNumBlocks()); |
| 211 | TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions()); |
| 212 | for (unsigned I = 0; I < Res.size(); I++) |
| 213 | TheSummary->setEntry(I, Res[I]); |
| 214 | } |
| 215 | |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 216 | void InstrProfWriter::writeImpl(ProfOStream &OS) { |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 217 | OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator; |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 218 | |
| 219 | using namespace IndexedInstrProf; |
| 220 | std::vector<uint32_t> Cutoffs(&SummaryCutoffs[0], |
| 221 | &SummaryCutoffs[NumSummaryCutoffs]); |
| 222 | ProfileSummary PS(Cutoffs); |
| 223 | InfoObj->TheProfileSummary = &PS; |
| 224 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 225 | // Populate the hash table generator. |
Justin Bogner | 821d747 | 2014-08-01 22:50:07 +0000 | [diff] [blame] | 226 | for (const auto &I : FunctionData) |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 227 | if (shouldEncodeData(I.getValue())) |
| 228 | Generator.insert(I.getKey(), &I.getValue()); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 229 | // Write the header. |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 230 | IndexedInstrProf::Header Header; |
| 231 | Header.Magic = IndexedInstrProf::Magic; |
Xinliang David Li | a6b2c4f | 2016-01-14 02:47:01 +0000 | [diff] [blame] | 232 | Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion; |
Rong Xu | 1288a19 | 2016-02-08 21:07:46 +0000 | [diff] [blame] | 233 | if (ProfileKind == PF_IRLevel) |
| 234 | Header.Version |= VARIANT_MASK_IR_PROF; |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 235 | Header.Unused = 0; |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 236 | Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType); |
| 237 | Header.HashOffset = 0; |
| 238 | int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t); |
| 239 | |
Xinliang David Li | 3c88288 | 2016-02-03 06:24:11 +0000 | [diff] [blame] | 240 | // Only write out all the fields except 'HashOffset'. We need |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 241 | // to remember the offset of that field to allow back patching |
| 242 | // later. |
| 243 | for (int I = 0; I < N - 1; I++) |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 244 | OS.write(reinterpret_cast<uint64_t *>(&Header)[I]); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 245 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 246 | // Save the location of Header.HashOffset field in \c OS. |
| 247 | uint64_t HashTableStartFieldOffset = OS.tell(); |
Xinliang David Li | dab183ed | 2015-10-18 01:02:29 +0000 | [diff] [blame] | 248 | // Reserve the space for HashOffset field. |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 249 | OS.write(0); |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 250 | |
| 251 | // Reserve space to write profile summary data. |
| 252 | uint32_t NumEntries = Cutoffs.size(); |
| 253 | uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries); |
| 254 | // Remember the summary offset. |
| 255 | uint64_t SummaryOffset = OS.tell(); |
| 256 | for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++) |
| 257 | OS.write(0); |
| 258 | |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 259 | // Write the hash table. |
Xinliang David Li | e1574f0 | 2016-01-22 20:25:56 +0000 | [diff] [blame] | 260 | uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj); |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 261 | |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 262 | // Allocate space for data to be serialized out. |
| 263 | std::unique_ptr<IndexedInstrProf::Summary> TheSummary = |
| 264 | IndexedInstrProf::allocSummary(SummarySize); |
| 265 | // Compute the Summary and copy the data to the data |
| 266 | // structure to be serialized out (to disk or buffer). |
| 267 | setSummary(TheSummary.get(), PS); |
| 268 | InfoObj->TheProfileSummary = 0; |
| 269 | |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 270 | // Now do the final patch: |
Xinliang David Li | 6c93ee8 | 2016-02-03 04:08:18 +0000 | [diff] [blame] | 271 | PatchItem PatchItems[] = { |
| 272 | // Patch the Header.HashOffset field. |
| 273 | {HashTableStartFieldOffset, &HashTableStart, 1}, |
| 274 | // Patch the summary data. |
| 275 | {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()), |
| 276 | (int)(SummarySize / sizeof(uint64_t))}}; |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 277 | OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems)); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void InstrProfWriter::write(raw_fd_ostream &OS) { |
| 281 | // Write the hash table. |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 282 | ProfOStream POS(OS); |
| 283 | writeImpl(POS); |
| 284 | } |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 285 | |
Xinliang David Li | b606638 | 2016-01-15 19:01:04 +0000 | [diff] [blame] | 286 | std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() { |
| 287 | std::string Data; |
| 288 | llvm::raw_string_ostream OS(Data); |
| 289 | ProfOStream POS(OS); |
| 290 | // Write the hash table. |
| 291 | writeImpl(POS); |
| 292 | // Return this in an aligned memory buffer. |
| 293 | return MemoryBuffer::getMemBufferCopy(Data); |
Justin Bogner | 2b6c537 | 2015-02-18 01:58:17 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 296 | static const char *ValueProfKindStr[] = { |
| 297 | #define VALUE_PROF_KIND(Enumerator, Value) #Enumerator, |
| 298 | #include "llvm/ProfileData/InstrProfData.inc" |
| 299 | }; |
| 300 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 301 | void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 302 | InstrProfSymtab &Symtab, |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 303 | raw_fd_ostream &OS) { |
Xinliang David Li | c667683 | 2015-11-23 22:31:22 +0000 | [diff] [blame] | 304 | OS << Func.Name << "\n"; |
| 305 | OS << "# Func Hash:\n" << Func.Hash << "\n"; |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 306 | OS << "# Num Counters:\n" << Func.Counts.size() << "\n"; |
Xinliang David Li | c667683 | 2015-11-23 22:31:22 +0000 | [diff] [blame] | 307 | OS << "# Counter Values:\n"; |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 308 | for (uint64_t Count : Func.Counts) |
| 309 | OS << Count << "\n"; |
| 310 | |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 311 | uint32_t NumValueKinds = Func.getNumValueKinds(); |
| 312 | if (!NumValueKinds) { |
| 313 | OS << "\n"; |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n"; |
| 318 | for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) { |
| 319 | uint32_t NS = Func.getNumValueSites(VK); |
| 320 | if (!NS) |
| 321 | continue; |
| 322 | OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n"; |
| 323 | OS << "# NumValueSites:\n" << NS << "\n"; |
| 324 | for (uint32_t S = 0; S < NS; S++) { |
| 325 | uint32_t ND = Func.getNumValueDataForSite(VK, S); |
| 326 | OS << ND << "\n"; |
| 327 | std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S); |
| 328 | for (uint32_t I = 0; I < ND; I++) { |
| 329 | if (VK == IPVK_IndirectCallTarget) |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 330 | OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n"; |
Xinliang David Li | e3bf4fd3 | 2015-12-14 18:44:01 +0000 | [diff] [blame] | 331 | else |
| 332 | OS << VD[I].Value << ":" << VD[I].Count << "\n"; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 337 | OS << "\n"; |
| 338 | } |
| 339 | |
| 340 | void InstrProfWriter::writeText(raw_fd_ostream &OS) { |
Rong Xu | 1288a19 | 2016-02-08 21:07:46 +0000 | [diff] [blame] | 341 | if (ProfileKind == PF_IRLevel) |
| 342 | OS << "# IR level Instrumentation Flag\n:ir\n"; |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 343 | InstrProfSymtab Symtab; |
| 344 | for (const auto &I : FunctionData) |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 345 | if (shouldEncodeData(I.getValue())) |
| 346 | Symtab.addFuncName(I.getKey()); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 347 | Symtab.finalizeSymtab(); |
| 348 | |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 349 | for (const auto &I : FunctionData) |
Vedant Kumar | 00dab22 | 2016-01-29 22:54:45 +0000 | [diff] [blame] | 350 | if (shouldEncodeData(I.getValue())) |
| 351 | for (const auto &Func : I.getValue()) |
| 352 | writeRecordInText(Func.second, Symtab, OS); |
Xinliang David Li | 6f7c19a | 2015-11-23 20:47:38 +0000 | [diff] [blame] | 353 | } |