blob: ccb270e0b719b1afe3822b25e62197d801a60606 [file] [log] [blame]
Eugene Zelenkoe78d1312017-03-03 01:07:34 +00001//===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===//
Justin Bognerb9bd7f82014-03-21 17:46:22 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Justin Bognerb9bd7f82014-03-21 17:46:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing profiling data for clang's
10// instrumentation based PGO and coverage.
11//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/ProfileData/InstrProfWriter.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000015#include "llvm/ADT/STLExtras.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000016#include "llvm/ADT/StringRef.h"
17#include "llvm/IR/ProfileSummary.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000018#include "llvm/ProfileData/InstrProf.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000019#include "llvm/ProfileData/ProfileCommon.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000020#include "llvm/Support/Endian.h"
Justin Bognerb7aa2632014-04-18 21:48:40 +000021#include "llvm/Support/EndianStream.h"
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000022#include "llvm/Support/Error.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000023#include "llvm/Support/MemoryBuffer.h"
Justin Bognerb7aa2632014-04-18 21:48:40 +000024#include "llvm/Support/OnDiskHashTable.h"
Eugene Zelenkocdc71612016-08-11 17:20:18 +000025#include "llvm/Support/raw_ostream.h"
26#include <algorithm>
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000027#include <cstdint>
28#include <memory>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000029#include <string>
Reid Kleckner437b1b32015-11-20 19:29:40 +000030#include <tuple>
Eugene Zelenkocdc71612016-08-11 17:20:18 +000031#include <utility>
32#include <vector>
Justin Bognerb7aa2632014-04-18 21:48:40 +000033
Justin Bognerb9bd7f82014-03-21 17:46:22 +000034using namespace llvm;
35
Xinliang David Lib6066382016-01-15 19:01:04 +000036// A struct to define how the data stream should be patched. For Indexed
37// profiling, only uint64_t data type is needed.
38struct PatchItem {
39 uint64_t Pos; // Where to patch.
40 uint64_t *D; // Pointer to an array of source data.
41 int N; // Number of elements in \c D array.
42};
43
44namespace llvm {
Eugene Zelenkocdc71612016-08-11 17:20:18 +000045
Xinliang David Lib6066382016-01-15 19:01:04 +000046// A wrapper class to abstract writer stream with support of bytes
47// back patching.
Xinliang David Li285d7bd2016-01-15 19:22:41 +000048class ProfOStream {
Xinliang David Li285d7bd2016-01-15 19:22:41 +000049public:
Peter Collingbournee3f65292018-05-18 19:46:24 +000050 ProfOStream(raw_fd_ostream &FD)
51 : IsFDOStream(true), OS(FD), LE(FD, support::little) {}
Eugene Zelenkoe78d1312017-03-03 01:07:34 +000052 ProfOStream(raw_string_ostream &STR)
Peter Collingbournee3f65292018-05-18 19:46:24 +000053 : IsFDOStream(false), OS(STR), LE(STR, support::little) {}
Xinliang David Lib6066382016-01-15 19:01:04 +000054
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;
Peter Collingbournee3f65292018-05-18 19:46:24 +000088 support::endian::Writer LE;
Xinliang David Lib6066382016-01-15 19:01:04 +000089};
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;
Rong Xua6ff69f2019-02-28 19:55:07 +0000104 InstrProfSummaryBuilder *CSSummaryBuilder;
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
Peter Collingbournee3f65292018-05-18 19:46:24 +0000116 endian::Writer LE(Out, little);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000117
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
Peter Collingbournee3f65292018-05-18 19:46:24 +0000143 endian::Writer LE(Out, little);
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;
Rong Xua6ff69f2019-02-28 19:55:07 +0000146 if (NamedInstrProfRecord::hasCSFlagInHash(ProfileData.first))
147 CSSummaryBuilder->addRecord(ProfRecord);
148 else
149 SummaryBuilder->addRecord(ProfRecord);
Xinliang David Li2004f002015-11-02 05:08:23 +0000150
Justin Bogner9e9a0572015-09-29 22:13:58 +0000151 LE.write<uint64_t>(ProfileData.first); // Function hash
Xinliang David Li2004f002015-11-02 05:08:23 +0000152 LE.write<uint64_t>(ProfRecord.Counts.size());
Xinliang David Li6aa216c2015-11-06 07:54:21 +0000153 for (uint64_t I : ProfRecord.Counts)
154 LE.write<uint64_t>(I);
Justin Bogner9e9a0572015-09-29 22:13:58 +0000155
Justin Bogner9e9a0572015-09-29 22:13:58 +0000156 // Write value data
Xinliang David Li99556872015-11-17 23:00:40 +0000157 std::unique_ptr<ValueProfData> VDataPtr =
158 ValueProfData::serializeFrom(ProfileData.second);
Xinliang David Liee415892015-11-10 00:24:45 +0000159 uint32_t S = VDataPtr->getSize();
Xinliang David Li46ad3632016-01-22 19:53:31 +0000160 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000161 Out.write((const char *)VDataPtr.get(), S);
Justin Bogner821d7472014-08-01 22:50:07 +0000162 }
Justin Bognerb7aa2632014-04-18 21:48:40 +0000163 }
164};
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000165
166} // end namespace llvm
Justin Bognerb7aa2632014-04-18 21:48:40 +0000167
Vedant Kumar00dab222016-01-29 22:54:45 +0000168InstrProfWriter::InstrProfWriter(bool Sparse)
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000169 : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {}
Xinliang David Lie1574f02016-01-22 20:25:56 +0000170
171InstrProfWriter::~InstrProfWriter() { delete InfoObj; }
172
Xinliang David Li46ad3632016-01-22 19:53:31 +0000173// Internal interface for testing purpose only.
174void InstrProfWriter::setValueProfDataEndianness(
175 support::endianness Endianness) {
Xinliang David Lie1574f02016-01-22 20:25:56 +0000176 InfoObj->ValueProfDataEndianness = Endianness;
Xinliang David Liee415892015-11-10 00:24:45 +0000177}
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000178
Vedant Kumar00dab222016-01-29 22:54:45 +0000179void InstrProfWriter::setOutputSparse(bool Sparse) {
180 this->Sparse = Sparse;
181}
Xinliang David Liee415892015-11-10 00:24:45 +0000182
David Blaikie98cce002017-07-10 03:04:59 +0000183void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight,
184 function_ref<void(Error)> Warn) {
David Blaikiecf9d52c2017-07-06 19:00:12 +0000185 auto Name = I.Name;
186 auto Hash = I.Hash;
David Blaikie98cce002017-07-10 03:04:59 +0000187 addRecord(Name, Hash, std::move(I), Weight, Warn);
David Blaikiecf9d52c2017-07-06 19:00:12 +0000188}
189
Rong Xu998b97f2019-04-30 21:19:12 +0000190void InstrProfWriter::overlapRecord(NamedInstrProfRecord &&Other,
191 OverlapStats &Overlap,
192 OverlapStats &FuncLevelOverlap,
193 const OverlapFuncFilters &FuncFilter) {
194 auto Name = Other.Name;
195 auto Hash = Other.Hash;
Rong Xue0fa2682019-10-01 18:06:50 +0000196 Other.accumulateCounts(FuncLevelOverlap.Test);
Rong Xu998b97f2019-04-30 21:19:12 +0000197 if (FunctionData.find(Name) == FunctionData.end()) {
198 Overlap.addOneUnique(FuncLevelOverlap.Test);
199 return;
200 }
201 if (FuncLevelOverlap.Test.CountSum < 1.0f) {
202 Overlap.Overlap.NumEntries += 1;
203 return;
204 }
205 auto &ProfileDataMap = FunctionData[Name];
206 bool NewFunc;
207 ProfilingData::iterator Where;
208 std::tie(Where, NewFunc) =
209 ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
210 if (NewFunc) {
211 Overlap.addOneMismatch(FuncLevelOverlap.Test);
212 return;
213 }
214 InstrProfRecord &Dest = Where->second;
215
216 uint64_t ValueCutoff = FuncFilter.ValueCutoff;
217 if (!FuncFilter.NameFilter.empty() &&
218 Name.find(FuncFilter.NameFilter) != Name.npos)
219 ValueCutoff = 0;
220
221 Dest.overlap(Other, Overlap, FuncLevelOverlap, ValueCutoff);
222}
223
David Blaikie98cce002017-07-10 03:04:59 +0000224void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash,
225 InstrProfRecord &&I, uint64_t Weight,
226 function_ref<void(Error)> Warn) {
David Blaikiecf9d52c2017-07-06 19:00:12 +0000227 auto &ProfileDataMap = FunctionData[Name];
Justin Bogner9e9a0572015-09-29 22:13:58 +0000228
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000229 bool NewFunc;
230 ProfilingData::iterator Where;
231 std::tie(Where, NewFunc) =
David Blaikiecf9d52c2017-07-06 19:00:12 +0000232 ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord()));
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000233 InstrProfRecord &Dest = Where->second;
Nathan Slingerlandaa5702d2015-12-02 18:19:24 +0000234
David Blaikie98cce002017-07-10 03:04:59 +0000235 auto MapWarn = [&](instrprof_error E) {
236 Warn(make_error<InstrProfError>(E));
237 };
238
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000239 if (NewFunc) {
Justin Bogner821d7472014-08-01 22:50:07 +0000240 // We've never seen a function with this name and hash, add it.
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000241 Dest = std::move(I);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000242 if (Weight > 1)
David Blaikie98cce002017-07-10 03:04:59 +0000243 Dest.scale(Weight, MapWarn);
Nathan Slingerlanda7318292015-11-20 19:12:43 +0000244 } else {
245 // We're updating a function we've seen before.
David Blaikie98cce002017-07-10 03:04:59 +0000246 Dest.merge(I, Weight, MapWarn);
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000247 }
248
Xinliang David Li062cde92016-01-08 05:45:21 +0000249 Dest.sortValueData();
Justin Bognerb9bd7f82014-03-21 17:46:22 +0000250}
251
David Blaikie98cce002017-07-10 03:04:59 +0000252void InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW,
253 function_ref<void(Error)> Warn) {
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000254 for (auto &I : IPW.FunctionData)
255 for (auto &Func : I.getValue())
David Blaikie98cce002017-07-10 03:04:59 +0000256 addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
Vedant Kumare3a0bf52016-07-19 01:17:20 +0000257}
258
Vedant Kumar00dab222016-01-29 22:54:45 +0000259bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
260 if (!Sparse)
261 return true;
262 for (const auto &Func : PD) {
263 const InstrProfRecord &IPR = Func.second;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000264 if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
Vedant Kumar00dab222016-01-29 22:54:45 +0000265 return true;
266 }
267 return false;
268}
269
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000270static void setSummary(IndexedInstrProf::Summary *TheSummary,
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000271 ProfileSummary &PS) {
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000272 using namespace IndexedInstrProf;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000273
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000274 std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
275 TheSummary->NumSummaryFields = Summary::NumKinds;
276 TheSummary->NumCutoffEntries = Res.size();
277 TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000278 TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
279 TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000280 TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000281 TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000282 TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
283 for (unsigned I = 0; I < Res.size(); I++)
284 TheSummary->setEntry(I, Res[I]);
285}
286
Xinliang David Lib6066382016-01-15 19:01:04 +0000287void InstrProfWriter::writeImpl(ProfOStream &OS) {
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000288 using namespace IndexedInstrProf;
289
Xinliang David Lie1574f02016-01-22 20:25:56 +0000290 OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000291
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000292 InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs);
293 InfoObj->SummaryBuilder = &ISB;
Rong Xua6ff69f2019-02-28 19:55:07 +0000294 InstrProfSummaryBuilder CSISB(ProfileSummaryBuilder::DefaultCutoffs);
295 InfoObj->CSSummaryBuilder = &CSISB;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000296
Justin Bognerb7aa2632014-04-18 21:48:40 +0000297 // Populate the hash table generator.
Justin Bogner821d7472014-08-01 22:50:07 +0000298 for (const auto &I : FunctionData)
Vedant Kumar00dab222016-01-29 22:54:45 +0000299 if (shouldEncodeData(I.getValue()))
300 Generator.insert(I.getKey(), &I.getValue());
Justin Bognerb7aa2632014-04-18 21:48:40 +0000301 // Write the header.
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000302 IndexedInstrProf::Header Header;
303 Header.Magic = IndexedInstrProf::Magic;
Xinliang David Lia6b2c4f2016-01-14 02:47:01 +0000304 Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion;
Rong Xu33c76c02016-02-10 17:18:30 +0000305 if (ProfileKind == PF_IRLevel)
306 Header.Version |= VARIANT_MASK_IR_PROF;
Rong Xua6ff69f2019-02-28 19:55:07 +0000307 if (ProfileKind == PF_IRLevelWithCS) {
308 Header.Version |= VARIANT_MASK_IR_PROF;
309 Header.Version |= VARIANT_MASK_CSIR_PROF;
310 }
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000311 Header.Unused = 0;
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000312 Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
313 Header.HashOffset = 0;
314 int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
315
Xinliang David Li3c882882016-02-03 06:24:11 +0000316 // Only write out all the fields except 'HashOffset'. We need
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000317 // to remember the offset of that field to allow back patching
318 // later.
319 for (int I = 0; I < N - 1; I++)
Xinliang David Lib6066382016-01-15 19:01:04 +0000320 OS.write(reinterpret_cast<uint64_t *>(&Header)[I]);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000321
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000322 // Save the location of Header.HashOffset field in \c OS.
323 uint64_t HashTableStartFieldOffset = OS.tell();
Xinliang David Lidab183ed2015-10-18 01:02:29 +0000324 // Reserve the space for HashOffset field.
Xinliang David Lib6066382016-01-15 19:01:04 +0000325 OS.write(0);
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000326
327 // Reserve space to write profile summary data.
Easwaran Ramane5a17e32016-05-19 21:07:12 +0000328 uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size();
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000329 uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
330 // Remember the summary offset.
331 uint64_t SummaryOffset = OS.tell();
332 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
333 OS.write(0);
Rong Xua6ff69f2019-02-28 19:55:07 +0000334 uint64_t CSSummaryOffset = 0;
335 uint64_t CSSummarySize = 0;
336 if (ProfileKind == PF_IRLevelWithCS) {
337 CSSummaryOffset = OS.tell();
338 CSSummarySize = SummarySize / sizeof(uint64_t);
339 for (unsigned I = 0; I < CSSummarySize; I++)
340 OS.write(0);
341 }
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000342
Justin Bognerb7aa2632014-04-18 21:48:40 +0000343 // Write the hash table.
Xinliang David Lie1574f02016-01-22 20:25:56 +0000344 uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
Justin Bognerb7aa2632014-04-18 21:48:40 +0000345
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000346 // Allocate space for data to be serialized out.
347 std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
348 IndexedInstrProf::allocSummary(SummarySize);
349 // Compute the Summary and copy the data to the data
350 // structure to be serialized out (to disk or buffer).
Benjamin Kramer38de59e2016-05-20 09:18:37 +0000351 std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
Easwaran Raman7cefdb82016-05-19 21:53:28 +0000352 setSummary(TheSummary.get(), *PS);
Eugene Zelenkocdc71612016-08-11 17:20:18 +0000353 InfoObj->SummaryBuilder = nullptr;
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000354
Rong Xua6ff69f2019-02-28 19:55:07 +0000355 // For Context Sensitive summary.
356 std::unique_ptr<IndexedInstrProf::Summary> TheCSSummary = nullptr;
357 if (ProfileKind == PF_IRLevelWithCS) {
358 TheCSSummary = IndexedInstrProf::allocSummary(SummarySize);
359 std::unique_ptr<ProfileSummary> CSPS = CSISB.getSummary();
360 setSummary(TheCSSummary.get(), *CSPS);
361 }
362 InfoObj->CSSummaryBuilder = nullptr;
363
Xinliang David Lib6066382016-01-15 19:01:04 +0000364 // Now do the final patch:
Xinliang David Li6c93ee82016-02-03 04:08:18 +0000365 PatchItem PatchItems[] = {
366 // Patch the Header.HashOffset field.
367 {HashTableStartFieldOffset, &HashTableStart, 1},
368 // Patch the summary data.
369 {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()),
Rong Xua6ff69f2019-02-28 19:55:07 +0000370 (int)(SummarySize / sizeof(uint64_t))},
371 {CSSummaryOffset, reinterpret_cast<uint64_t *>(TheCSSummary.get()),
372 (int)CSSummarySize}};
373
Xinliang David Lib6066382016-01-15 19:01:04 +0000374 OS.patch(PatchItems, sizeof(PatchItems) / sizeof(*PatchItems));
Justin Bogner2b6c5372015-02-18 01:58:17 +0000375}
376
377void InstrProfWriter::write(raw_fd_ostream &OS) {
378 // Write the hash table.
Xinliang David Lib6066382016-01-15 19:01:04 +0000379 ProfOStream POS(OS);
380 writeImpl(POS);
381}
Justin Bogner2b6c5372015-02-18 01:58:17 +0000382
Xinliang David Lib6066382016-01-15 19:01:04 +0000383std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
384 std::string Data;
Eugene Zelenkoe78d1312017-03-03 01:07:34 +0000385 raw_string_ostream OS(Data);
Xinliang David Lib6066382016-01-15 19:01:04 +0000386 ProfOStream POS(OS);
387 // Write the hash table.
388 writeImpl(POS);
389 // Return this in an aligned memory buffer.
390 return MemoryBuffer::getMemBufferCopy(Data);
Justin Bogner2b6c5372015-02-18 01:58:17 +0000391}
392
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000393static const char *ValueProfKindStr[] = {
Dmitry Mikulin312b5f82019-04-23 22:26:55 +0000394#define VALUE_PROF_KIND(Enumerator, Value, Descr) #Enumerator,
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000395#include "llvm/ProfileData/InstrProfData.inc"
396};
397
David Blaikiecf9d52c2017-07-06 19:00:12 +0000398void InstrProfWriter::writeRecordInText(StringRef Name, uint64_t Hash,
399 const InstrProfRecord &Func,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000400 InstrProfSymtab &Symtab,
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000401 raw_fd_ostream &OS) {
David Blaikiecf9d52c2017-07-06 19:00:12 +0000402 OS << Name << "\n";
403 OS << "# Func Hash:\n" << Hash << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000404 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
Xinliang David Lic6676832015-11-23 22:31:22 +0000405 OS << "# Counter Values:\n";
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000406 for (uint64_t Count : Func.Counts)
407 OS << Count << "\n";
408
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000409 uint32_t NumValueKinds = Func.getNumValueKinds();
410 if (!NumValueKinds) {
411 OS << "\n";
412 return;
413 }
414
415 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
416 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
417 uint32_t NS = Func.getNumValueSites(VK);
418 if (!NS)
419 continue;
420 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
421 OS << "# NumValueSites:\n" << NS << "\n";
422 for (uint32_t S = 0; S < NS; S++) {
423 uint32_t ND = Func.getNumValueDataForSite(VK, S);
424 OS << ND << "\n";
425 std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S);
426 for (uint32_t I = 0; I < ND; I++) {
427 if (VK == IPVK_IndirectCallTarget)
Mircea Trofin29a21ba2018-03-22 21:26:52 +0000428 OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":"
429 << VD[I].Count << "\n";
Xinliang David Lie3bf4fd32015-12-14 18:44:01 +0000430 else
431 OS << VD[I].Value << ":" << VD[I].Count << "\n";
432 }
433 }
434 }
435
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000436 OS << "\n";
437}
438
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000439Error InstrProfWriter::writeText(raw_fd_ostream &OS) {
Rong Xu33c76c02016-02-10 17:18:30 +0000440 if (ProfileKind == PF_IRLevel)
441 OS << "# IR level Instrumentation Flag\n:ir\n";
Rong Xua6ff69f2019-02-28 19:55:07 +0000442 else if (ProfileKind == PF_IRLevelWithCS)
443 OS << "# CSIR level Instrumentation Flag\n:csir\n";
Xinliang David Lia716cc52015-12-20 06:22:13 +0000444 InstrProfSymtab Symtab;
Mandeep Singh Granga14f20c2019-03-02 00:47:43 +0000445
446 using FuncPair = detail::DenseMapPair<uint64_t, InstrProfRecord>;
447 using RecordType = std::pair<StringRef, FuncPair>;
448 SmallVector<RecordType, 4> OrderedFuncData;
449
450 for (const auto &I : FunctionData) {
451 if (shouldEncodeData(I.getValue())) {
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000452 if (Error E = Symtab.addFuncName(I.getKey()))
453 return E;
Vedant Kumar00dab222016-01-29 22:54:45 +0000454 for (const auto &Func : I.getValue())
Mandeep Singh Granga14f20c2019-03-02 00:47:43 +0000455 OrderedFuncData.push_back(std::make_pair(I.getKey(), Func));
456 }
457 }
458
459 llvm::sort(OrderedFuncData, [](const RecordType &A, const RecordType &B) {
460 return std::tie(A.first, A.second.first) <
461 std::tie(B.first, B.second.first);
462 });
463
464 for (const auto &record : OrderedFuncData) {
465 const StringRef &Name = record.first;
466 const FuncPair &Func = record.second;
467 writeRecordInText(Name, Func.first, Func.second, Symtab, OS);
468 }
469
Vedant Kumarb5794ca2017-06-20 01:38:56 +0000470 return Error::success();
Xinliang David Li6f7c19a2015-11-23 20:47:38 +0000471}