blob: b3402a6ea956c992b76445b30d16adc8206eace5 [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 Zelenkoe78d1312017-03-03 01:07:34 +000072 raw_string_ostream &SOStream =
Xinliang David Lib6066382016-01-15 19:01:04 +000073 static_cast<llvm::raw_string_ostream &>(OS);
74 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 Zelenkocdc71612016-08-11 17:20:18 +000084
Xinliang David Lib6066382016-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;
89 support::endian::Writer<support::little> LE;
90};
Xinliang David Lib6066382016-01-15 19:01:04 +000091
Xinliang David Lie1574f02016-01-22 20:25:56 +000092class InstrProfRecordWriterTrait {
Justin Bognerb7aa2632014-04-18 21:48:40 +000093public:
94 typedef StringRef key_type;
95 typedef StringRef key_type_ref;
96
Justin Bogner9e9a0572015-09-29 22:13:58 +000097 typedef const InstrProfWriter::ProfilingData *const data_type;
98 typedef const InstrProfWriter::ProfilingData *const data_type_ref;
Justin Bognerb7aa2632014-04-18 21:48:40 +000099
100 typedef uint64_t hash_value_type;
101 typedef uint64_t offset_type;
102
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000103 support::endianness ValueProfDataEndianness = support::little;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000104 InstrProfSummaryBuilder *SummaryBuilder;
Xinliang David Lie1574f02016-01-22 20:25:56 +0000105
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000106 InstrProfRecordWriterTrait() = default;
107
Justin Bognerb7aa2632014-04-18 21:48:40 +0000108 static hash_value_type ComputeHash(key_type_ref K) {
Xinliang David Li49ee76d2015-12-18 22:22:12 +0000109 return IndexedInstrProf::ComputeHash(K);
Justin Bognerb7aa2632014-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 Zelenkoe78d1312017-03-03 01:07:34 +0000114 using namespace support;
115
Justin Bognerb7aa2632014-04-18 21:48:40 +0000116 endian::Writer<little> LE(Out);
117
Justin Bognere8081712014-04-19 00:33:15 +0000118 offset_type N = K.size();
Justin Bognerb7aa2632014-04-18 21:48:40 +0000119 LE.write<offset_type>(N);
120
Justin Bogner821d7472014-08-01 22:50:07 +0000121 offset_type M = 0;
Justin Bogner9e9a0572015-09-29 22:13:58 +0000122 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000123 const InstrProfRecord &ProfRecord = ProfileData.second;
Justin Bogner9e9a0572015-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 Li2004f002015-11-02 05:08:23 +0000126 M += ProfRecord.Counts.size() * sizeof(uint64_t);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000127
128 // Value data
Xinliang David Li99556872015-11-17 23:00:40 +0000129 M += ValueProfData::getSize(ProfileData.second);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000130 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000131 LE.write<offset_type>(M);
132
133 return std::make_pair(N, M);
134 }
135
Xinliang David Lie1574f02016-01-22 20:25:56 +0000136 void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) {
Justin Bognerb7aa2632014-04-18 21:48:40 +0000137 Out.write(K.data(), N);
138 }
139
Xinliang David Lie1574f02016-01-22 20:25:56 +0000140 void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000141 using namespace support;
142
Justin Bognerb7aa2632014-04-18 21:48:40 +0000143 endian::Writer<little> LE(Out);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000144 for (const auto &ProfileData : *V) {
Xinliang David Li2004f002015-11-02 05:08:23 +0000145 const InstrProfRecord &ProfRecord = ProfileData.second;
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000146 SummaryBuilder->addRecord(ProfRecord);
Xinliang David Li2004f002015-11-02 05:08:23 +0000147
Justin Bogner9e9a0572015-09-29 22:13:58 +0000148 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +0000149 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000150 for (uint64_t I : ProfRecord.Counts)
151 LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000152
Justin Bogner9e9a0572015-09-29 22:13:58 +0000153 // Write value data
Xinliang David Li99556872015-11-17 23:00:40 +0000154 std::unique_ptr<ValueProfData> VDataPtr =
155 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Liee415892015-11-10 00:24:45 +0000156 uint32_t S = VDataPtr->getSize();
Xinliang David Li46ad3632016-01-22 19:53:31 +0000157 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000158 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner821d7472014-08-01 22:50:07 +0000159 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000160 }
161};
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000162
163} // end namespace llvm
Justin Bognerb7aa2632014-04-18 21:48:40 +0000164
Vedant Kumar00dab222016-01-29 22:54:45 +0000165InstrProfWriter::InstrProfWriter(bool Sparse)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000166 : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
Xinliang David Lie1574f02016-01-22 20:25:56 +0000167
168InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
169
Xinliang David Li46ad3632016-01-22 19:53:31 +0000170// Internal interface for testing purpose only.
171void InstrProfWriter::setValueProfDataEndianness(
172 support::endianness Endianness) {
Xinliang David Lie1574f02016-01-22 20:25:56 +0000173 InfoObj->ValueProfDataEndianness = Endianness;
Xinliang David Liee415892015-11-10 00:24:45 +0000174}
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000175
Vedant Kumar00dab222016-01-29 22:54:45 +0000176void InstrProfWriter::setOutputSparse(bool Sparse) {
177 this->Sparse = Sparse;
178}
Xinliang David Liee415892015-11-10 00:24:45 +0000179
Vedant Kumar9152fd12016-05-19 03:54:45 +0000180Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) {
Justin Bogner9e9a0572015-09-29 22:13:58 +0000181 auto &ProfileDataMap = FunctionData[I.Name];
182
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000183 bool NewFunc;
184 ProfilingData::iterator Where;
185 std::tie(Where, NewFunc) =
186 ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
187 InstrProfRecord &Dest = Where->second;
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000188
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000189 if (NewFunc) {
Justin Bogner821d7472014-08-01 22:50:07 +0000190 // We've never seen a function with this name and hash, add it.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000191 Dest = std::move(I);
Xinliang David Li60057282015-12-20 08:46:18 +0000192 // Fix up the name to avoid dangling reference.
193 Dest.Name = FunctionData.find(Dest.Name)->getKey();
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000194 if (Weight > 1)
Vedant Kumar42369db2016-05-11 19:42:19 +0000195 Dest.scale(Weight);
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000196 } else {
197 // We're updating a function we've seen before.
Vedant Kumar42369db2016-05-11 19:42:19 +0000198 Dest.merge(I, Weight);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000199 }
200
Xinliang David Li062cde92016-01-08 05:45:21 +0000201 Dest.sortValueData();
202
Vedant Kumar9152fd12016-05-19 03:54:45 +0000203 return Dest.takeError();
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000204}
205
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000206Error InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW) {
207 for (auto &I : IPW.FunctionData)
208 for (auto &Func : I.getValue())
209 if (Error E = addRecord(std::move(Func.second), 1))
210 return E;
211 return Error::success();
212}
213
Vedant Kumar00dab222016-01-29 22:54:45 +0000214bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
215 if (!Sparse)
216 return true;
217 for (const auto &Func : PD) {
218 const InstrProfRecord &IPR = Func.second;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000219 if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
Vedant Kumar00dab222016-01-29 22:54:45 +0000220 return true;
221 }
222 return false;
223}
224
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000225static void setSummary(IndexedInstrProf::Summary *TheSummary,
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000226 ProfileSummary &PS) {
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000227 using namespace IndexedInstrProf;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000228
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000229 std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
230 TheSummary->NumSummaryFields = Summary::NumKinds;
231 TheSummary->NumCutoffEntries = Res.size();
232 TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000233 TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
234 TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000235 TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000236 TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000237 TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
238 for (unsigned I = 0; I < Res.size(); I++)
239 TheSummary->setEntry(I, Res[I]);
240}
241
Xinliang David Lib6066382016-01-15 19:01:04 +0000242void InstrProfWriter::writeImpl(ProfOStream &OS) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000243 using namespace IndexedInstrProf;
244
Xinliang David Lie1574f02016-01-22 20:25:56 +0000245 OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000246
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000247 InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
248 InfoObj->SummaryBuilder = &ISB;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000249
Justin Bognerb7aa2632014-04-18 21:48:40 +0000250 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000251 for (const auto &I : FunctionData)
Vedant Kumar00dab222016-01-29 22:54:45 +0000252 if (shouldEncodeData(I.getValue()))
253 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000254 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000255 IndexedInstrProf::Header Header;
256 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000257 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Rong Xu33c76c02016-02-10 17:18:30 +0000258 if (ProfileKind == PF_IRLevel)
259 Header.Version |= VARIANT_MASK_IR_PROF;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000260 Header.Unused = 0;
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000261 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
262 Header.HashOffset = 0;
263 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
264
Xinliang David Li3c882882016-02-03 06:24:11 +0000265 // Only write out all the fields except 'HashOffset'. We need
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000266 // to remember the offset of that field to allow back patching
267 // later.
268 for (int I = 0; I < N - 1; I++)
Xinliang David Lib6066382016-01-15 19:01:04 +0000269 OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000270
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000271 // Save the location of Header.HashOffset field in \c OS.
272 uint64_t HashTableStartFieldOffset = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000273 // Reserve the space for HashOffset field.
Xinliang David Lib6066382016-01-15 19:01:04 +0000274 OS.write(0);
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000275
276 // Reserve space to write profile summary data.
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000277 uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000278 uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
279 // Remember the summary offset.
280 uint64_t SummaryOffset = OS.tell();
281 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
282 OS.write(0);
283
Justin Bognerb7aa2632014-04-18 21:48:40 +0000284 // Write the hash table.
Xinliang David Lie1574f02016-01-22 20:25:56 +0000285 uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000286
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000287 // Allocate space for data to be serialized out.
288 std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
289 IndexedInstrProf::allocSummary(SummarySize);
290 // Compute the Summary and copy the data to the data
291 // structure to be serialized out (to disk or buffer).
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000292 std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000293 setSummary(TheSummary.get(), *PS);
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000294 InfoObj->SummaryBuilder = nullptr;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000295
Xinliang David Lib6066382016-01-15 19:01:04 +0000296 // Now do the final patch:
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000297 PatchItem PatchItems[] = {
298 // Patch the Header.HashOffset field.
299 {HashTableStartFieldOffset, &HashTableStart, 1},
300 // Patch the summary data.
301 {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
302 (int)(SummarySize / sizeof(uint64_t))}};
Xinliang David Lib6066382016-01-15 19:01:04 +0000303 OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000304}
305
306void InstrProfWriter::write(raw_fd_ostream &OS) {
307 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000308 ProfOStream POS(OS);
309 writeImpl(POS);
310}
Justin Bogner2b6c5372015-02-18 01:58:17 +0000311
Xinliang David Lib6066382016-01-15 19:01:04 +0000312std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
313 std::string Data;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000314 raw_string_ostream OS(Data);
Xinliang David Lib6066382016-01-15 19:01:04 +0000315 ProfOStream POS(OS);
316 // Write the hash table.
317 writeImpl(POS);
318 // Return this in an aligned memory buffer.
319 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bogner2b6c5372015-02-18 01:58:17 +0000320}
321
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000322static const char *ValueProfKindStr[] = {
323#define VALUE_PROF_KIND(Enumerator, Value) #Enumerator,
324#include "llvm/ProfileData/InstrProfData.inc"
325};
326
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000327void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000328 InstrProfSymtab &Symtab,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000329 raw_fd_ostream &OS) {
Xinliang David Lic6676832015-11-23 22:31:22 +0000330 OS << Func.Name << "\n";
331 OS << "# Func Hash:\n" << Func.Hash << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000332 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Lic6676832015-11-23 22:31:22 +0000333 OS << "# Counter Values:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000334 for (uint64_t Count : Func.Counts)
335 OS << Count << "\n";
336
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000337 uint32_t NumValueKinds = Func.getNumValueKinds();
338 if (!NumValueKinds) {
339 OS << "\n";
340 return;
341 }
342
343 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
344 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
345 uint32_t NS = Func.getNumValueSites(VK);
346 if (!NS)
347 continue;
348 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
349 OS << "# NumValueSites:\n" << NS << "\n";
350 for (uint32_t S = 0; S < NS; S++) {
351 uint32_t ND = Func.getNumValueDataForSite(VK, S);
352 OS << ND << "\n";
353 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
354 for (uint32_t I = 0; I < ND; I++) {
355 if (VK == IPVK_IndirectCallTarget)
Xinliang David Lia716cc52015-12-20 06:22:13 +0000356 OS << Symtab.getFuncName(VD[I].Value) << ":" << VD[I].Count << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000357 else
358 OS << VD[I].Value << ":" << VD[I].Count << "\n";
359 }
360 }
361 }
362
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000363 OS << "\n";
364}
365
366void InstrProfWriter::writeText(raw_fd_ostream &OS) {
Rong Xu33c76c02016-02-10 17:18:30 +0000367 if (ProfileKind == PF_IRLevel)
368 OS << "# IR level Instrumentation Flag\n:ir\n";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000369 InstrProfSymtab Symtab;
370 for (const auto &I : FunctionData)
Vedant Kumar00dab222016-01-29 22:54:45 +0000371 if (shouldEncodeData(I.getValue()))
372 Symtab.addFuncName(I.getKey());
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);
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000379}