blob: 9efea78ed2a89a5dc0a7c82bba1f72ff20263516 [file] [log] [blame]
Eugene Zelenkoe78d1312017-03-03 01:07:34 +00001//===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===//
Justin Bognerb9bd7f82014-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 Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/ProfileData/InstrProfWriter.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000016#include "llvm/ADT/STLExtras.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/ProfileSummary.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000019#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000020#include "llvm/ProfileData/ProfileCommon.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000021#include "llvm/Support/Endian.h"
Justin Bognerb7aa2632014-04-18 21:48:40 +000022#include "llvm/Support/EndianStream.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000023#include "llvm/Support/Error.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000024#include "llvm/Support/MemoryBuffer.h"
Justin Bognerb7aa2632014-04-18 21:48:40 +000025#include "llvm/Support/OnDiskHashTable.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000026#include "llvm/Support/raw_ostream.h"
27#include <algorithm>
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000028#include <cstdint>
29#include <memory>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000030#include <string>
Reid Kleckner437b1b32015-11-20 19:29:40 +000031#include <tuple>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000032#include <utility>
33#include <vector>
Justin Bognerb7aa2632014-04-18 21:48:40 +000034
Justin Bognerb9bd7f82014-03-21 17:46:22 +000035using namespace llvm;
36
Xinliang David Lib6066382016-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 Zelenkocdc71612016-08-11 17:20:18 +000046
Xinliang David Lib6066382016-01-15 19:01:04 +000047// A wrapper class to abstract writer stream with support of bytes
48// back patching.
Xinliang David Li285d7bd2016-01-15 19:22:41 +000049class ProfOStream {
Xinliang David Li285d7bd2016-01-15 19:22:41 +000050public:
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000051 ProfOStream(raw_fd_ostream &FD) : IsFDOStream(true), OS(FD), LE(FD) {}
52 ProfOStream(raw_string_ostream &STR)
Xinliang David Lib6066382016-01-15 19:01:04 +000053 : 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 Zelenkocdc71612016-08-11 17:20:18 +000057
Xinliang David Lib6066382016-01-15 19:01:04 +000058 // \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 Zelenkoe78d1312017-03-03 01:07:34 +000063
Xinliang David Lib6066382016-01-15 19:01:04 +000064 if (IsFDOStream) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000065 raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS);
Xinliang David Lib6066382016-01-15 19:01:04 +000066 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 Zelenko72208a82017-06-21 23:19:47 +000072 raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS);
Xinliang David Lib6066382016-01-15 19:01:04 +000073 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 Zelenkocdc71612016-08-11 17:20:18 +000083
Xinliang David Lib6066382016-01-15 19:01:04 +000084 // 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 Lib6066382016-01-15 19:01:04 +000090
Xinliang David Lie1574f02016-01-22 20:25:56 +000091class InstrProfRecordWriterTrait {
Justin Bognerb7aa2632014-04-18 21:48:40 +000092public:
Eugene Zelenko72208a82017-06-21 23:19:47 +000093 using key_type = StringRef;
94 using key_type_ref = StringRef;
Justin Bognerb7aa2632014-04-18 21:48:40 +000095
Eugene Zelenko72208a82017-06-21 23:19:47 +000096 using data_type = const InstrProfWriter::ProfilingData *const;
97 using data_type_ref = const InstrProfWriter::ProfilingData *const;
Justin Bognerb7aa2632014-04-18 21:48:40 +000098
Eugene Zelenko72208a82017-06-21 23:19:47 +000099 using hash_value_type = uint64_t;
100 using offset_type = uint64_t;
Justin Bognerb7aa2632014-04-18 21:48:40 +0000101
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000102 support::endianness ValueProfDataEndianness = support::little;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000103 InstrProfSummaryBuilder *SummaryBuilder;
Xinliang David Lie1574f02016-01-22 20:25:56 +0000104
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000105 InstrProfRecordWriterTrait() = default;
106
Justin Bognerb7aa2632014-04-18 21:48:40 +0000107 static hash_value_type ComputeHash(key_type_ref K) {
Xinliang David Li49ee76d2015-12-18 22:22:12 +0000108 return IndexedInstrProf::ComputeHash(K);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000109 }
110
111 static std::pair<offset_type, offset_type>
112 EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000113 using namespace support;
114
Justin Bognerb7aa2632014-04-18 21:48:40 +0000115 endian::Writer<little> LE(Out);
116
Justin Bognere8081712014-04-19 00:33:15 +0000117 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000118 LE.write<offset_type>(N);
119
Justin Bogner821d7472014-08-01 22:50:07 +0000120 offset_type M = 0;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000121 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000122 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000123 M += sizeof(uint64_t); // The function hash
124 M += sizeof(uint64_t); // The size of the Counts vector
Xinliang David Li2004f002015-11-02 05:08:23 +0000125 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000126
127 // Value data
Xinliang David Li99556872015-11-17 23:00:40 +0000128 M += ValueProfData::getSize(ProfileData.second);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000129 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000130 LE.write<offset_type>(M);
131
132 return std::make_pair(N, M);
133 }
134
Xinliang David Lie1574f02016-01-22 20:25:56 +0000135 void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000136 Out.write(K.data(), N);
137 }
138
Xinliang David Lie1574f02016-01-22 20:25:56 +0000139 void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000140 using namespace support;
141
Justin Bognerb7aa2632014-04-18 21:48:40 +0000142 endian::Writer<little> LE(Out);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000143 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000144 const InstrProfRecord &ProfRecord = ProfileData.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000145 SummaryBuilder->addRecord(ProfRecord);
Xinliang David Li2004f002015-11-02 05:08:23 +0000146
Justin Bogner9e9a0572015-09-29 22:13:58 +0000147 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +0000148 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000149 for (uint64_t I : ProfRecord.Counts)
150 LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000151
Justin Bogner9e9a0572015-09-29 22:13:58 +0000152 // Write value data
Xinliang David Li99556872015-11-17 23:00:40 +0000153 std::unique_ptr<ValueProfData> VDataPtr =
154 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Liee415892015-11-10 00:24:45 +0000155 uint32_t S = VDataPtr->getSize();
Xinliang David Li46ad3632016-01-22 19:53:31 +0000156 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000157 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner821d7472014-08-01 22:50:07 +0000158 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000159 }
160};
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000161
162} // end namespace llvm
Justin Bognerb7aa2632014-04-18 21:48:40 +0000163
Vedant Kumar00dab222016-01-29 22:54:45 +0000164InstrProfWriter::InstrProfWriter(bool Sparse)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000165 : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
Xinliang David Lie1574f02016-01-22 20:25:56 +0000166
167InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
168
Xinliang David Li46ad3632016-01-22 19:53:31 +0000169// Internal interface for testing purpose only.
170void InstrProfWriter::setValueProfDataEndianness(
171 support::endianness Endianness) {
Xinliang David Lie1574f02016-01-22 20:25:56 +0000172 InfoObj->ValueProfDataEndianness = Endianness;
Xinliang David Liee415892015-11-10 00:24:45 +0000173}
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000174
Vedant Kumar00dab222016-01-29 22:54:45 +0000175void InstrProfWriter::setOutputSparse(bool Sparse) {
176 this->Sparse = Sparse;
177}
Xinliang David Liee415892015-11-10 00:24:45 +0000178
Vedant Kumar9152fd12016-05-19 03:54:45 +0000179Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000180 auto &ProfileDataMap = FunctionData[I.Name];
181
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000182 bool NewFunc;
183 ProfilingData::iterator Where;
184 std::tie(Where, NewFunc) =
185 ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
186 InstrProfRecord &Dest = Where->second;
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000187
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000188 if (NewFunc) {
Justin Bogner821d7472014-08-01 22:50:07 +0000189 // We've never seen a function with this name and hash, add it.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000190 Dest = std::move(I);
Xinliang David Li60057282015-12-20 08:46:18 +0000191 // Fix up the name to avoid dangling reference.
192 Dest.Name = FunctionData.find(Dest.Name)->getKey();
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000193 if (Weight > 1)
Vedant Kumar42369db2016-05-11 19:42:19 +0000194 Dest.scale(Weight);
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000195 } else {
196 // We're updating a function we've seen before.
Vedant Kumar42369db2016-05-11 19:42:19 +0000197 Dest.merge(I, Weight);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000198 }
199
Xinliang David Li062cde92016-01-08 05:45:21 +0000200 Dest.sortValueData();
201
Vedant Kumar9152fd12016-05-19 03:54:45 +0000202 return Dest.takeError();
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000203}
204
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000205Error InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW) {
206 for (auto &I : IPW.FunctionData)
207 for (auto &Func : I.getValue())
208 if (Error E = addRecord(std::move(Func.second), 1))
209 return E;
210 return Error::success();
211}
212
Vedant Kumar00dab222016-01-29 22:54:45 +0000213bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
214 if (!Sparse)
215 return true;
216 for (const auto &Func : PD) {
217 const InstrProfRecord &IPR = Func.second;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000218 if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
Vedant Kumar00dab222016-01-29 22:54:45 +0000219 return true;
220 }
221 return false;
222}
223
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000224static void setSummary(IndexedInstrProf::Summary *TheSummary,
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000225 ProfileSummary &PS) {
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000226 using namespace IndexedInstrProf;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000227
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000228 std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
229 TheSummary->NumSummaryFields = Summary::NumKinds;
230 TheSummary->NumCutoffEntries = Res.size();
231 TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000232 TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
233 TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000234 TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000235 TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000236 TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
237 for (unsigned I = 0; I < Res.size(); I++)
238 TheSummary->setEntry(I, Res[I]);
239}
240
Xinliang David Lib6066382016-01-15 19:01:04 +0000241void InstrProfWriter::writeImpl(ProfOStream &OS) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000242 using namespace IndexedInstrProf;
243
Xinliang David Lie1574f02016-01-22 20:25:56 +0000244 OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000245
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000246 InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
247 InfoObj->SummaryBuilder = &ISB;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000248
Justin Bognerb7aa2632014-04-18 21:48:40 +0000249 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000250 for (const auto &I : FunctionData)
Vedant Kumar00dab222016-01-29 22:54:45 +0000251 if (shouldEncodeData(I.getValue()))
252 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000253 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000254 IndexedInstrProf::Header Header;
255 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000256 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Rong Xu33c76c02016-02-10 17:18:30 +0000257 if (ProfileKind == PF_IRLevel)
258 Header.Version |= VARIANT_MASK_IR_PROF;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000259 Header.Unused = 0;
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000260 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
261 Header.HashOffset = 0;
262 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
263
Xinliang David Li3c882882016-02-03 06:24:11 +0000264 // Only write out all the fields except 'HashOffset'. We need
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000265 // to remember the offset of that field to allow back patching
266 // later.
267 for (int I = 0; I < N - 1; I++)
Xinliang David Lib6066382016-01-15 19:01:04 +0000268 OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000269
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000270 // Save the location of Header.HashOffset field in \c OS.
271 uint64_t HashTableStartFieldOffset = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000272 // Reserve the space for HashOffset field.
Xinliang David Lib6066382016-01-15 19:01:04 +0000273 OS.write(0);
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000274
275 // Reserve space to write profile summary data.
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000276 uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000277 uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
278 // Remember the summary offset.
279 uint64_t SummaryOffset = OS.tell();
280 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
281 OS.write(0);
282
Justin Bognerb7aa2632014-04-18 21:48:40 +0000283 // Write the hash table.
Xinliang David Lie1574f02016-01-22 20:25:56 +0000284 uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000285
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000286 // Allocate space for data to be serialized out.
287 std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
288 IndexedInstrProf::allocSummary(SummarySize);
289 // Compute the Summary and copy the data to the data
290 // structure to be serialized out (to disk or buffer).
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000291 std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000292 setSummary(TheSummary.get(), *PS);
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000293 InfoObj->SummaryBuilder = nullptr;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000294
Xinliang David Lib6066382016-01-15 19:01:04 +0000295 // Now do the final patch:
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000296 PatchItem PatchItems[] = {
297 // Patch the Header.HashOffset field.
298 {HashTableStartFieldOffset, &HashTableStart, 1},
299 // Patch the summary data.
300 {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
301 (int)(SummarySize / sizeof(uint64_t))}};
Xinliang David Lib6066382016-01-15 19:01:04 +0000302 OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000303}
304
305void InstrProfWriter::write(raw_fd_ostream &OS) {
306 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000307 ProfOStream POS(OS);
308 writeImpl(POS);
309}
Justin Bogner2b6c5372015-02-18 01:58:17 +0000310
Xinliang David Lib6066382016-01-15 19:01:04 +0000311std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
312 std::string Data;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000313 raw_string_ostream OS(Data);
Xinliang David Lib6066382016-01-15 19:01:04 +0000314 ProfOStream POS(OS);
315 // Write the hash table.
316 writeImpl(POS);
317 // Return this in an aligned memory buffer.
318 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bogner2b6c5372015-02-18 01:58:17 +0000319}
320
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000321static const char *ValueProfKindStr[] = {
322#define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
323#include "llvm/ProfileData/InstrProfData.inc"
324};
325
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000326void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000327 InstrProfSymtab &Symtab,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000328 raw_fd_ostream &OS) {
Xinliang David Lic6676832015-11-23 22:31:22 +0000329 OS << Func.Name << "\n";
330 OS << "# Func Hash:\n" << Func.Hash << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000331 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Lic6676832015-11-23 22:31:22 +0000332 OS << "# Counter Values:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000333 for (uint64_t Count : Func.Counts)
334 OS << Count << "\n";
335
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000336 uint32_t NumValueKinds = Func.getNumValueKinds();
337 if (!NumValueKinds) {
338 OS << "\n";
339 return;
340 }
341
342 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
343 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
344 uint32_t NS = Func.getNumValueSites(VK);
345 if (!NS)
346 continue;
347 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
348 OS << "# NumValueSites:\n" << NS << "\n";
349 for (uint32_t S = 0; S < NS; S++) {
350 uint32_t ND = Func.getNumValueDataForSite(VK, S);
351 OS << ND << "\n";
352 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
353 for (uint32_t I = 0; I < ND; I++) {
354 if (VK == IPVK_IndirectCallTarget)
Xinliang David Lia716cc52015-12-20 06:22:13 +0000355 OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000356 else
357 OS << VD[I].Value << ":" << VD[I].Count << "\n";
358 }
359 }
360 }
361
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000362 OS << "\n";
363}
364
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000365Error InstrProfWriter::writeText(raw_fd_ostream &OS) {
Rong Xu33c76c02016-02-10 17:18:30 +0000366 if (ProfileKind == PF_IRLevel)
367 OS << "# IR level Instrumentation Flag\n:ir\n";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000368 InstrProfSymtab Symtab;
369 for (const auto &I : FunctionData)
Vedant Kumar00dab222016-01-29 22:54:45 +0000370 if (shouldEncodeData(I.getValue()))
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000371 if (Error E = Symtab.addFuncName(I.getKey()))
372 return E;
Xinliang David Lia716cc52015-12-20 06:22:13 +0000373 Symtab.finalizeSymtab();
374
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000375 for (const auto &I : FunctionData)
Vedant Kumar00dab222016-01-29 22:54:45 +0000376 if (shouldEncodeData(I.getValue()))
377 for (const auto &Func : I.getValue())
378 writeRecordInText(Func.second, Symtab, OS);
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000379 return Error::success();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000380}