blob: 18b9deec158f4113a2c2c1e882c80a7852ac2bcc [file] [log] [blame]
Eugene Zelenkof4f67a02017-03-03 01:07:34 +00001//===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===//
Justin Bogner496d7f62014-03-21 17:46:22 +00002//
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 Carruthe3e43d92017-06-06 11:49:48 +000015#include "llvm/ProfileData/InstrProfWriter.h"
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000016#include "llvm/ADT/STLExtras.h"
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/ProfileSummary.h"
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000019#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000020#include "llvm/ProfileData/ProfileCommon.h"
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000021#include "llvm/Support/Endian.h"
Justin Bognere153fb32014-04-18 21:48:40 +000022#include "llvm/Support/EndianStream.h"
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000023#include "llvm/Support/Error.h"
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000024#include "llvm/Support/MemoryBuffer.h"
Justin Bognere153fb32014-04-18 21:48:40 +000025#include "llvm/Support/OnDiskHashTable.h"
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000026#include "llvm/Support/raw_ostream.h"
27#include <algorithm>
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000028#include <cstdint>
29#include <memory>
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000030#include <string>
Reid Kleckner368c7cb2015-11-20 19:29:40 +000031#include <tuple>
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000032#include <utility>
33#include <vector>
Justin Bognere153fb32014-04-18 21:48:40 +000034
Justin Bogner496d7f62014-03-21 17:46:22 +000035using namespace llvm;
36
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000037// A struct to define how the data stream should be patched. For Indexed
38// profiling, only uint64_t data type is needed.
39struct 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
45namespace llvm {
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000046
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000047// A wrapper class to abstract writer stream with support of bytes
48// back patching.
Xinliang David Lif223b512016-01-15 19:22:41 +000049class ProfOStream {
Xinliang David Lif223b512016-01-15 19:22:41 +000050public:
Peter Collingbournea68d4cc2018-05-18 19:46:24 +000051 ProfOStream(raw_fd_ostream &FD)
52 : IsFDOStream(true), OS(FD), LE(FD, support::little) {}
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000053 ProfOStream(raw_string_ostream &STR)
Peter Collingbournea68d4cc2018-05-18 19:46:24 +000054 : IsFDOStream(false), OS(STR), LE(STR, support::little) {}
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000055
56 uint64_t tell() { return OS.tell(); }
57 void write(uint64_t V) { LE.write<uint64_t>(V); }
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000058
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000059 // \c patch can only be called when all data is written and flushed.
60 // For raw_string_ostream, the patch is done on the target string
61 // directly and it won't be reflected in the stream's internal buffer.
62 void patch(PatchItem *P, int NItems) {
63 using namespace support;
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000064
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000065 if (IsFDOStream) {
Eugene Zelenkof4f67a02017-03-03 01:07:34 +000066 raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000067 for (int K = 0; K < NItems; K++) {
68 FDOStream.seek(P[K].Pos);
69 for (int I = 0; I < P[K].N; I++)
70 write(P[K].D[I]);
71 }
72 } else {
Eugene Zelenkodfaebc42017-06-21 23:19:47 +000073 raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000074 std::string &Data = SOStream.str(); // with flush
75 for (int K = 0; K < NItems; K++) {
76 for (int I = 0; I < P[K].N; I++) {
77 uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]);
78 Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t),
79 (const char *)&Bytes, sizeof(uint64_t));
80 }
81 }
82 }
83 }
Eugene Zelenkoc986ab22016-08-11 17:20:18 +000084
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000085 // If \c OS is an instance of \c raw_fd_ostream, this field will be
86 // true. Otherwise, \c OS will be an raw_string_ostream.
87 bool IsFDOStream;
88 raw_ostream &OS;
Peter Collingbournea68d4cc2018-05-18 19:46:24 +000089 support::endian::Writer LE;
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000090};
Xinliang David Li48b8a6c2016-01-15 19:01:04 +000091
Xinliang David Li5c327492016-01-22 20:25:56 +000092class InstrProfRecordWriterTrait {
Justin Bognere153fb32014-04-18 21:48:40 +000093public:
Eugene Zelenkodfaebc42017-06-21 23:19:47 +000094 using key_type = StringRef;
95 using key_type_ref = StringRef;
Justin Bognere153fb32014-04-18 21:48:40 +000096
Eugene Zelenkodfaebc42017-06-21 23:19:47 +000097 using data_type = const InstrProfWriter::ProfilingData *const;
98 using data_type_ref = const InstrProfWriter::ProfilingData *const;
Justin Bognere153fb32014-04-18 21:48:40 +000099
Eugene Zelenkodfaebc42017-06-21 23:19:47 +0000100 using hash_value_type = uint64_t;
101 using offset_type = uint64_t;
Justin Bognere153fb32014-04-18 21:48:40 +0000102
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000103 support::endianness ValueProfDataEndianness = support::little;
Easwaran Raman17e7f1192016-05-19 21:07:12 +0000104 InstrProfSummaryBuilder *SummaryBuilder;
Xinliang David Li5c327492016-01-22 20:25:56 +0000105
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000106 InstrProfRecordWriterTrait() = default;
107
Justin Bognere153fb32014-04-18 21:48:40 +0000108 static hash_value_type ComputeHash(key_type_ref K) {
Xinliang David Li5c542532015-12-18 22:22:12 +0000109 return IndexedInstrProf::ComputeHash(K);
Justin Bognere153fb32014-04-18 21:48:40 +0000110 }
111
112 static std::pair<offset_type, offset_type>
113 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000114 using namespace support;
115
Peter Collingbournea68d4cc2018-05-18 19:46:24 +0000116 endian::Writer LE(Out, little);
Justin Bognere153fb32014-04-18 21:48:40 +0000117
Justin Bogner55c1e1b2014-04-19 00:33:15 +0000118 offset_type N = K.size();
Justin Bognere153fb32014-04-18 21:48:40 +0000119 LE.write<offset_type>(N);
120
Justin Bogner4a6fa132014-08-01 22:50:07 +0000121 offset_type M = 0;
Justin Bognerc96f87a2015-09-29 22:13:58 +0000122 for (const auto &ProfileData : *V) {
Xinliang David Li61c7e682015-11-02 05:08:23 +0000123 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bognerc96f87a2015-09-29 22:13:58 +0000124 M += sizeof(uint64_t); // The function hash
125 M += sizeof(uint64_t); // The size of the Counts vector
Xinliang David Li61c7e682015-11-02 05:08:23 +0000126 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bognerc96f87a2015-09-29 22:13:58 +0000127
128 // Value data
Xinliang David Lie34401d2015-11-17 23:00:40 +0000129 M += ValueProfData::getSize(ProfileData.second);
Justin Bognerc96f87a2015-09-29 22:13:58 +0000130 }
Justin Bognere153fb32014-04-18 21:48:40 +0000131 LE.write<offset_type>(M);
132
133 return std::make_pair(N, M);
134 }
135
Xinliang David Li5c327492016-01-22 20:25:56 +0000136 void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) {
Justin Bognere153fb32014-04-18 21:48:40 +0000137 Out.write(K.data(), N);
138 }
139
Xinliang David Li5c327492016-01-22 20:25:56 +0000140 void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) {
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000141 using namespace support;
142
Peter Collingbournea68d4cc2018-05-18 19:46:24 +0000143 endian::Writer LE(Out, little);
Justin Bognerc96f87a2015-09-29 22:13:58 +0000144 for (const auto &ProfileData : *V) {
Xinliang David Li61c7e682015-11-02 05:08:23 +0000145 const InstrProfRecord &ProfRecord = ProfileData.second;
Easwaran Raman17e7f1192016-05-19 21:07:12 +0000146 SummaryBuilder->addRecord(ProfRecord);
Xinliang David Li61c7e682015-11-02 05:08:23 +0000147
Justin Bognerc96f87a2015-09-29 22:13:58 +0000148 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li61c7e682015-11-02 05:08:23 +0000149 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li4d774a32015-11-06 07:54:21 +0000150 for (uint64_t I : ProfRecord.Counts)
151 LE.write<uint64_t>(I);
Justin Bognerc96f87a2015-09-29 22:13:58 +0000152
Justin Bognerc96f87a2015-09-29 22:13:58 +0000153 // Write value data
Xinliang David Lie34401d2015-11-17 23:00:40 +0000154 std::unique_ptr<ValueProfData> VDataPtr =
155 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Libcd8e0a2015-11-10 00:24:45 +0000156 uint32_t S = VDataPtr->getSize();
Xinliang David Li8c37fa82016-01-22 19:53:31 +0000157 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
Xinliang David Libcd8e0a2015-11-10 00:24:45 +0000158 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner4a6fa132014-08-01 22:50:07 +0000159 }
Justin Bognere153fb32014-04-18 21:48:40 +0000160 }
161};
Eugene Zelenkoc986ab22016-08-11 17:20:18 +0000162
163} // end namespace llvm
Justin Bognere153fb32014-04-18 21:48:40 +0000164
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000165InstrProfWriter::InstrProfWriter(bool Sparse)
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000166 : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
Xinliang David Li5c327492016-01-22 20:25:56 +0000167
168InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
169
Xinliang David Li8c37fa82016-01-22 19:53:31 +0000170// Internal interface for testing purpose only.
171void InstrProfWriter::setValueProfDataEndianness(
172 support::endianness Endianness) {
Xinliang David Li5c327492016-01-22 20:25:56 +0000173 InfoObj->ValueProfDataEndianness = Endianness;
Xinliang David Libcd8e0a2015-11-10 00:24:45 +0000174}
Eugene Zelenkoc986ab22016-08-11 17:20:18 +0000175
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000176void InstrProfWriter::setOutputSparse(bool Sparse) {
177 this->Sparse = Sparse;
178}
Xinliang David Libcd8e0a2015-11-10 00:24:45 +0000179
David Blaikiecb160612017-07-10 03:04:59 +0000180void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
181 function_ref<void(Error)> Warn) {
David Blaikie450ef2a2017-07-06 19:00:12 +0000182 auto Name = I.Name;
183 auto Hash = I.Hash;
David Blaikiecb160612017-07-10 03:04:59 +0000184 addRecord(Name, Hash, std::move(I), Weight, Warn);
David Blaikie450ef2a2017-07-06 19:00:12 +0000185}
186
David Blaikiecb160612017-07-10 03:04:59 +0000187void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
188 InstrProfRecord &&I, uint64_t Weight,
189 function_ref<void(Error)> Warn) {
David Blaikie450ef2a2017-07-06 19:00:12 +0000190 auto &ProfileDataMap = FunctionData[Name];
Justin Bognerc96f87a2015-09-29 22:13:58 +0000191
Nathan Slingerlande0a7f282015-11-20 19:12:43 +0000192 bool NewFunc;
193 ProfilingData::iterator Where;
194 std::tie(Where, NewFunc) =
David Blaikie450ef2a2017-07-06 19:00:12 +0000195 ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
Nathan Slingerlande0a7f282015-11-20 19:12:43 +0000196 InstrProfRecord &Dest = Where->second;
Nathan Slingerland1c2b9982015-12-02 18:19:24 +0000197
David Blaikiecb160612017-07-10 03:04:59 +0000198 auto MapWarn = [&](instrprof_error E) {
199 Warn(make_error<InstrProfError>(E));
200 };
201
Nathan Slingerlande0a7f282015-11-20 19:12:43 +0000202 if (NewFunc) {
Justin Bogner4a6fa132014-08-01 22:50:07 +0000203 // We've never seen a function with this name and hash, add it.
Nathan Slingerlande0a7f282015-11-20 19:12:43 +0000204 Dest = std::move(I);
Xinliang David Li4f87d122016-01-08 03:49:59 +0000205 if (Weight > 1)
David Blaikiecb160612017-07-10 03:04:59 +0000206 Dest.scale(Weight, MapWarn);
Nathan Slingerlande0a7f282015-11-20 19:12:43 +0000207 } else {
208 // We're updating a function we've seen before.
David Blaikiecb160612017-07-10 03:04:59 +0000209 Dest.merge(I, Weight, MapWarn);
Justin Bogner496d7f62014-03-21 17:46:22 +0000210 }
211
Xinliang David Li36838fe2016-01-08 05:45:21 +0000212 Dest.sortValueData();
Justin Bogner496d7f62014-03-21 17:46:22 +0000213}
214
David Blaikiecb160612017-07-10 03:04:59 +0000215void InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW,
216 function_ref<void(Error)> Warn) {
Vedant Kumard91d3782016-07-19 01:17:20 +0000217 for (auto &I : IPW.FunctionData)
218 for (auto &Func : I.getValue())
David Blaikiecb160612017-07-10 03:04:59 +0000219 addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
Vedant Kumard91d3782016-07-19 01:17:20 +0000220}
221
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000222bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
223 if (!Sparse)
224 return true;
225 for (const auto &Func : PD) {
226 const InstrProfRecord &IPR = Func.second;
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000227 if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000228 return true;
229 }
230 return false;
231}
232
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000233static void setSummary(IndexedInstrProf::Summary *TheSummary,
Easwaran Raman30c760d2016-05-19 21:53:28 +0000234 ProfileSummary &PS) {
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000235 using namespace IndexedInstrProf;
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000236
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000237 std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
238 TheSummary->NumSummaryFields = Summary::NumKinds;
239 TheSummary->NumCutoffEntries = Res.size();
240 TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
Easwaran Raman30c760d2016-05-19 21:53:28 +0000241 TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
242 TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000243 TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
Easwaran Raman30c760d2016-05-19 21:53:28 +0000244 TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000245 TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
246 for (unsigned I = 0; I < Res.size(); I++)
247 TheSummary->setEntry(I, Res[I]);
248}
249
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000250void InstrProfWriter::writeImpl(ProfOStream &OS) {
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000251 using namespace IndexedInstrProf;
252
Xinliang David Li5c327492016-01-22 20:25:56 +0000253 OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000254
Easwaran Raman17e7f1192016-05-19 21:07:12 +0000255 InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
256 InfoObj->SummaryBuilder = &ISB;
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000257
Justin Bognere153fb32014-04-18 21:48:40 +0000258 // Populate the hash table generator.
Justin Bogner4a6fa132014-08-01 22:50:07 +0000259 for (const auto &I : FunctionData)
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000260 if (shouldEncodeData(I.getValue()))
261 Generator.insert(I.getKey(), &I.getValue());
Justin Bognere153fb32014-04-18 21:48:40 +0000262 // Write the header.
Xinliang David Li09717fa2015-10-18 01:02:29 +0000263 IndexedInstrProf::Header Header;
264 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Li5fd06e22016-01-14 02:47:01 +0000265 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Rong Xuc7d7fb02016-02-10 17:18:30 +0000266 if (ProfileKind == PF_IRLevel)
267 Header.Version |= VARIANT_MASK_IR_PROF;
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000268 Header.Unused = 0;
Xinliang David Li09717fa2015-10-18 01:02:29 +0000269 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
270 Header.HashOffset = 0;
271 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
272
Xinliang David Li916e1f12016-02-03 06:24:11 +0000273 // Only write out all the fields except 'HashOffset'. We need
Xinliang David Li09717fa2015-10-18 01:02:29 +0000274 // to remember the offset of that field to allow back patching
275 // later.
276 for (int I = 0; I < N - 1; I++)
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000277 OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognere153fb32014-04-18 21:48:40 +0000278
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000279 // Save the location of Header.HashOffset field in \c OS.
280 uint64_t HashTableStartFieldOffset = OS.tell();
Xinliang David Li09717fa2015-10-18 01:02:29 +0000281 // Reserve the space for HashOffset field.
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000282 OS.write(0);
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000283
284 // Reserve space to write profile summary data.
Easwaran Raman17e7f1192016-05-19 21:07:12 +0000285 uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000286 uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
287 // Remember the summary offset.
288 uint64_t SummaryOffset = OS.tell();
289 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
290 OS.write(0);
291
Justin Bognere153fb32014-04-18 21:48:40 +0000292 // Write the hash table.
Xinliang David Li5c327492016-01-22 20:25:56 +0000293 uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
Justin Bognere153fb32014-04-18 21:48:40 +0000294
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000295 // Allocate space for data to be serialized out.
296 std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
297 IndexedInstrProf::allocSummary(SummarySize);
298 // Compute the Summary and copy the data to the data
299 // structure to be serialized out (to disk or buffer).
Benjamin Kramere3bf6642016-05-20 09:18:37 +0000300 std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
Easwaran Raman30c760d2016-05-19 21:53:28 +0000301 setSummary(TheSummary.get(), *PS);
Eugene Zelenkoc986ab22016-08-11 17:20:18 +0000302 InfoObj->SummaryBuilder = nullptr;
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000303
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000304 // Now do the final patch:
Xinliang David Li8fd9cfb2016-02-03 04:08:18 +0000305 PatchItem PatchItems[] = {
306 // Patch the Header.HashOffset field.
307 {HashTableStartFieldOffset, &HashTableStart, 1},
308 // Patch the summary data.
309 {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
310 (int)(SummarySize / sizeof(uint64_t))}};
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000311 OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
Justin Bogner09e5af72015-02-18 01:58:17 +0000312}
313
314void InstrProfWriter::write(raw_fd_ostream &OS) {
315 // Write the hash table.
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000316 ProfOStream POS(OS);
317 writeImpl(POS);
318}
Justin Bogner09e5af72015-02-18 01:58:17 +0000319
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000320std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
321 std::string Data;
Eugene Zelenkof4f67a02017-03-03 01:07:34 +0000322 raw_string_ostream OS(Data);
Xinliang David Li48b8a6c2016-01-15 19:01:04 +0000323 ProfOStream POS(OS);
324 // Write the hash table.
325 writeImpl(POS);
326 // Return this in an aligned memory buffer.
327 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bogner09e5af72015-02-18 01:58:17 +0000328}
329
Xinliang David Liea0570f2015-12-14 18:44:01 +0000330static const char *ValueProfKindStr[] = {
331#define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
332#include "llvm/ProfileData/InstrProfData.inc"
333};
334
David Blaikie450ef2a2017-07-06 19:00:12 +0000335void InstrProfWriter::writeRecordInText(StringRef Name, uint64_t Hash,
336 const InstrProfRecord &Func,
Xinliang David Licfbaf112015-12-20 06:22:13 +0000337 InstrProfSymtab &Symtab,
Xinliang David Li55f78332015-11-23 20:47:38 +0000338 raw_fd_ostream &OS) {
David Blaikie450ef2a2017-07-06 19:00:12 +0000339 OS << Name << "\n";
340 OS << "# Func Hash:\n" << Hash << "\n";
Xinliang David Liea0570f2015-12-14 18:44:01 +0000341 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Li54daffc2015-11-23 22:31:22 +0000342 OS << "# Counter Values:\n";
Xinliang David Li55f78332015-11-23 20:47:38 +0000343 for (uint64_t Count : Func.Counts)
344 OS << Count << "\n";
345
Xinliang David Liea0570f2015-12-14 18:44:01 +0000346 uint32_t NumValueKinds = Func.getNumValueKinds();
347 if (!NumValueKinds) {
348 OS << "\n";
349 return;
350 }
351
352 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
353 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
354 uint32_t NS = Func.getNumValueSites(VK);
355 if (!NS)
356 continue;
357 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
358 OS << "# NumValueSites:\n" << NS << "\n";
359 for (uint32_t S = 0; S < NS; S++) {
360 uint32_t ND = Func.getNumValueDataForSite(VK, S);
361 OS << ND << "\n";
362 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
363 for (uint32_t I = 0; I < ND; I++) {
364 if (VK == IPVK_IndirectCallTarget)
Mircea Trofin21d9c7a2018-03-22 21:26:52 +0000365 OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":"
366 << VD[I].Count << "\n";
Xinliang David Liea0570f2015-12-14 18:44:01 +0000367 else
368 OS << VD[I].Value << ":" << VD[I].Count << "\n";
369 }
370 }
371 }
372
Xinliang David Li55f78332015-11-23 20:47:38 +0000373 OS << "\n";
374}
375
Vedant Kumarb9454632017-06-20 01:38:56 +0000376Error InstrProfWriter::writeText(raw_fd_ostream &OS) {
Rong Xuc7d7fb02016-02-10 17:18:30 +0000377 if (ProfileKind == PF_IRLevel)
378 OS << "# IR level Instrumentation Flag\n:ir\n";
Xinliang David Licfbaf112015-12-20 06:22:13 +0000379 InstrProfSymtab Symtab;
380 for (const auto &I : FunctionData)
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000381 if (shouldEncodeData(I.getValue()))
Vedant Kumarb9454632017-06-20 01:38:56 +0000382 if (Error E = Symtab.addFuncName(I.getKey()))
383 return E;
Xinliang David Licfbaf112015-12-20 06:22:13 +0000384
Xinliang David Li55f78332015-11-23 20:47:38 +0000385 for (const auto &I : FunctionData)
Vedant Kumar0c94d7d2016-01-29 22:54:45 +0000386 if (shouldEncodeData(I.getValue()))
387 for (const auto &Func : I.getValue())
David Blaikie450ef2a2017-07-06 19:00:12 +0000388 writeRecordInText(I.getKey(), Func.first, Func.second, Symtab, OS);
Vedant Kumarb9454632017-06-20 01:38:56 +0000389 return Error::success();
Xinliang David Li55f78332015-11-23 20:47:38 +0000390}