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