1*0b57cec5SDimitry Andric //===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file contains support for writing profiling data for clang's 10*0b57cec5SDimitry Andric // instrumentation based PGO and coverage. 11*0b57cec5SDimitry Andric // 12*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13*0b57cec5SDimitry Andric 14*0b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfWriter.h" 15*0b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 16*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 17*0b57cec5SDimitry Andric #include "llvm/IR/ProfileSummary.h" 18*0b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProf.h" 1981ad6265SDimitry Andric #include "llvm/ProfileData/MemProf.h" 20*0b57cec5SDimitry Andric #include "llvm/ProfileData/ProfileCommon.h" 21*0b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 22*0b57cec5SDimitry Andric #include "llvm/Support/EndianStream.h" 23*0b57cec5SDimitry Andric #include "llvm/Support/Error.h" 24*0b57cec5SDimitry Andric #include "llvm/Support/MemoryBuffer.h" 25*0b57cec5SDimitry Andric #include "llvm/Support/OnDiskHashTable.h" 26*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 27*0b57cec5SDimitry Andric #include <cstdint> 28*0b57cec5SDimitry Andric #include <memory> 29*0b57cec5SDimitry Andric #include <string> 30*0b57cec5SDimitry Andric #include <tuple> 31*0b57cec5SDimitry Andric #include <utility> 32*0b57cec5SDimitry Andric #include <vector> 33*0b57cec5SDimitry Andric 34*0b57cec5SDimitry Andric using namespace llvm; 35*0b57cec5SDimitry Andric 36*0b57cec5SDimitry Andric // A struct to define how the data stream should be patched. For Indexed 37*0b57cec5SDimitry Andric // profiling, only uint64_t data type is needed. 38*0b57cec5SDimitry Andric struct PatchItem { 39*0b57cec5SDimitry Andric uint64_t Pos; // Where to patch. 40*0b57cec5SDimitry Andric uint64_t *D; // Pointer to an array of source data. 41*0b57cec5SDimitry Andric int N; // Number of elements in \c D array. 42*0b57cec5SDimitry Andric }; 43*0b57cec5SDimitry Andric 44*0b57cec5SDimitry Andric namespace llvm { 45*0b57cec5SDimitry Andric 46*0b57cec5SDimitry Andric // A wrapper class to abstract writer stream with support of bytes 47*0b57cec5SDimitry Andric // back patching. 48*0b57cec5SDimitry Andric class ProfOStream { 49*0b57cec5SDimitry Andric public: 50*0b57cec5SDimitry Andric ProfOStream(raw_fd_ostream &FD) 51*0b57cec5SDimitry Andric : IsFDOStream(true), OS(FD), LE(FD, support::little) {} 52*0b57cec5SDimitry Andric ProfOStream(raw_string_ostream &STR) 53*0b57cec5SDimitry Andric : IsFDOStream(false), OS(STR), LE(STR, support::little) {} 54*0b57cec5SDimitry Andric 55*0b57cec5SDimitry Andric uint64_t tell() { return OS.tell(); } 56*0b57cec5SDimitry Andric void write(uint64_t V) { LE.write<uint64_t>(V); } 57bdd1243dSDimitry Andric void writeByte(uint8_t V) { LE.write<uint8_t>(V); } 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric // \c patch can only be called when all data is written and flushed. 60*0b57cec5SDimitry Andric // For raw_string_ostream, the patch is done on the target string 61*0b57cec5SDimitry Andric // directly and it won't be reflected in the stream's internal buffer. 62*0b57cec5SDimitry Andric void patch(PatchItem *P, int NItems) { 63*0b57cec5SDimitry Andric using namespace support; 64*0b57cec5SDimitry Andric 65*0b57cec5SDimitry Andric if (IsFDOStream) { 66*0b57cec5SDimitry Andric raw_fd_ostream &FDOStream = static_cast<raw_fd_ostream &>(OS); 6781ad6265SDimitry Andric const uint64_t LastPos = FDOStream.tell(); 68*0b57cec5SDimitry Andric for (int K = 0; K < NItems; K++) { 69*0b57cec5SDimitry Andric FDOStream.seek(P[K].Pos); 70*0b57cec5SDimitry Andric for (int I = 0; I < P[K].N; I++) 71*0b57cec5SDimitry Andric write(P[K].D[I]); 72*0b57cec5SDimitry Andric } 7381ad6265SDimitry Andric // Reset the stream to the last position after patching so that users 7481ad6265SDimitry Andric // don't accidentally overwrite data. This makes it consistent with 7581ad6265SDimitry Andric // the string stream below which replaces the data directly. 7681ad6265SDimitry Andric FDOStream.seek(LastPos); 77*0b57cec5SDimitry Andric } else { 78*0b57cec5SDimitry Andric raw_string_ostream &SOStream = static_cast<raw_string_ostream &>(OS); 79*0b57cec5SDimitry Andric std::string &Data = SOStream.str(); // with flush 80*0b57cec5SDimitry Andric for (int K = 0; K < NItems; K++) { 81*0b57cec5SDimitry Andric for (int I = 0; I < P[K].N; I++) { 82*0b57cec5SDimitry Andric uint64_t Bytes = endian::byte_swap<uint64_t, little>(P[K].D[I]); 83*0b57cec5SDimitry Andric Data.replace(P[K].Pos + I * sizeof(uint64_t), sizeof(uint64_t), 84*0b57cec5SDimitry Andric (const char *)&Bytes, sizeof(uint64_t)); 85*0b57cec5SDimitry Andric } 86*0b57cec5SDimitry Andric } 87*0b57cec5SDimitry Andric } 88*0b57cec5SDimitry Andric } 89*0b57cec5SDimitry Andric 90*0b57cec5SDimitry Andric // If \c OS is an instance of \c raw_fd_ostream, this field will be 91*0b57cec5SDimitry Andric // true. Otherwise, \c OS will be an raw_string_ostream. 92*0b57cec5SDimitry Andric bool IsFDOStream; 93*0b57cec5SDimitry Andric raw_ostream &OS; 94*0b57cec5SDimitry Andric support::endian::Writer LE; 95*0b57cec5SDimitry Andric }; 96*0b57cec5SDimitry Andric 97*0b57cec5SDimitry Andric class InstrProfRecordWriterTrait { 98*0b57cec5SDimitry Andric public: 99*0b57cec5SDimitry Andric using key_type = StringRef; 100*0b57cec5SDimitry Andric using key_type_ref = StringRef; 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric using data_type = const InstrProfWriter::ProfilingData *const; 103*0b57cec5SDimitry Andric using data_type_ref = const InstrProfWriter::ProfilingData *const; 104*0b57cec5SDimitry Andric 105*0b57cec5SDimitry Andric using hash_value_type = uint64_t; 106*0b57cec5SDimitry Andric using offset_type = uint64_t; 107*0b57cec5SDimitry Andric 108*0b57cec5SDimitry Andric support::endianness ValueProfDataEndianness = support::little; 109*0b57cec5SDimitry Andric InstrProfSummaryBuilder *SummaryBuilder; 110*0b57cec5SDimitry Andric InstrProfSummaryBuilder *CSSummaryBuilder; 111*0b57cec5SDimitry Andric 112*0b57cec5SDimitry Andric InstrProfRecordWriterTrait() = default; 113*0b57cec5SDimitry Andric 114*0b57cec5SDimitry Andric static hash_value_type ComputeHash(key_type_ref K) { 115*0b57cec5SDimitry Andric return IndexedInstrProf::ComputeHash(K); 116*0b57cec5SDimitry Andric } 117*0b57cec5SDimitry Andric 118*0b57cec5SDimitry Andric static std::pair<offset_type, offset_type> 119*0b57cec5SDimitry Andric EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) { 120*0b57cec5SDimitry Andric using namespace support; 121*0b57cec5SDimitry Andric 122*0b57cec5SDimitry Andric endian::Writer LE(Out, little); 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric offset_type N = K.size(); 125*0b57cec5SDimitry Andric LE.write<offset_type>(N); 126*0b57cec5SDimitry Andric 127*0b57cec5SDimitry Andric offset_type M = 0; 128*0b57cec5SDimitry Andric for (const auto &ProfileData : *V) { 129*0b57cec5SDimitry Andric const InstrProfRecord &ProfRecord = ProfileData.second; 130*0b57cec5SDimitry Andric M += sizeof(uint64_t); // The function hash 131*0b57cec5SDimitry Andric M += sizeof(uint64_t); // The size of the Counts vector 132*0b57cec5SDimitry Andric M += ProfRecord.Counts.size() * sizeof(uint64_t); 133*0b57cec5SDimitry Andric 134*0b57cec5SDimitry Andric // Value data 135*0b57cec5SDimitry Andric M += ValueProfData::getSize(ProfileData.second); 136*0b57cec5SDimitry Andric } 137*0b57cec5SDimitry Andric LE.write<offset_type>(M); 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric return std::make_pair(N, M); 140*0b57cec5SDimitry Andric } 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N) { 143*0b57cec5SDimitry Andric Out.write(K.data(), N); 144*0b57cec5SDimitry Andric } 145*0b57cec5SDimitry Andric 146*0b57cec5SDimitry Andric void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type) { 147*0b57cec5SDimitry Andric using namespace support; 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric endian::Writer LE(Out, little); 150*0b57cec5SDimitry Andric for (const auto &ProfileData : *V) { 151*0b57cec5SDimitry Andric const InstrProfRecord &ProfRecord = ProfileData.second; 152*0b57cec5SDimitry Andric if (NamedInstrProfRecord::hasCSFlagInHash(ProfileData.first)) 153*0b57cec5SDimitry Andric CSSummaryBuilder->addRecord(ProfRecord); 154*0b57cec5SDimitry Andric else 155*0b57cec5SDimitry Andric SummaryBuilder->addRecord(ProfRecord); 156*0b57cec5SDimitry Andric 157*0b57cec5SDimitry Andric LE.write<uint64_t>(ProfileData.first); // Function hash 158*0b57cec5SDimitry Andric LE.write<uint64_t>(ProfRecord.Counts.size()); 159*0b57cec5SDimitry Andric for (uint64_t I : ProfRecord.Counts) 160*0b57cec5SDimitry Andric LE.write<uint64_t>(I); 161*0b57cec5SDimitry Andric 162*0b57cec5SDimitry Andric // Write value data 163*0b57cec5SDimitry Andric std::unique_ptr<ValueProfData> VDataPtr = 164*0b57cec5SDimitry Andric ValueProfData::serializeFrom(ProfileData.second); 165*0b57cec5SDimitry Andric uint32_t S = VDataPtr->getSize(); 166*0b57cec5SDimitry Andric VDataPtr->swapBytesFromHost(ValueProfDataEndianness); 167*0b57cec5SDimitry Andric Out.write((const char *)VDataPtr.get(), S); 168*0b57cec5SDimitry Andric } 169*0b57cec5SDimitry Andric } 170*0b57cec5SDimitry Andric }; 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric } // end namespace llvm 173*0b57cec5SDimitry Andric 1741fd87a68SDimitry Andric InstrProfWriter::InstrProfWriter(bool Sparse) 1751fd87a68SDimitry Andric : Sparse(Sparse), InfoObj(new InstrProfRecordWriterTrait()) {} 176*0b57cec5SDimitry Andric 177*0b57cec5SDimitry Andric InstrProfWriter::~InstrProfWriter() { delete InfoObj; } 178*0b57cec5SDimitry Andric 179*0b57cec5SDimitry Andric // Internal interface for testing purpose only. 180*0b57cec5SDimitry Andric void InstrProfWriter::setValueProfDataEndianness( 181*0b57cec5SDimitry Andric support::endianness Endianness) { 182*0b57cec5SDimitry Andric InfoObj->ValueProfDataEndianness = Endianness; 183*0b57cec5SDimitry Andric } 184*0b57cec5SDimitry Andric 185*0b57cec5SDimitry Andric void InstrProfWriter::setOutputSparse(bool Sparse) { 186*0b57cec5SDimitry Andric this->Sparse = Sparse; 187*0b57cec5SDimitry Andric } 188*0b57cec5SDimitry Andric 189*0b57cec5SDimitry Andric void InstrProfWriter::addRecord(NamedInstrProfRecord &&I, uint64_t Weight, 190*0b57cec5SDimitry Andric function_ref<void(Error)> Warn) { 191*0b57cec5SDimitry Andric auto Name = I.Name; 192*0b57cec5SDimitry Andric auto Hash = I.Hash; 193*0b57cec5SDimitry Andric addRecord(Name, Hash, std::move(I), Weight, Warn); 194*0b57cec5SDimitry Andric } 195*0b57cec5SDimitry Andric 196*0b57cec5SDimitry Andric void InstrProfWriter::overlapRecord(NamedInstrProfRecord &&Other, 197*0b57cec5SDimitry Andric OverlapStats &Overlap, 198*0b57cec5SDimitry Andric OverlapStats &FuncLevelOverlap, 199*0b57cec5SDimitry Andric const OverlapFuncFilters &FuncFilter) { 200*0b57cec5SDimitry Andric auto Name = Other.Name; 201*0b57cec5SDimitry Andric auto Hash = Other.Hash; 2028bcb0991SDimitry Andric Other.accumulateCounts(FuncLevelOverlap.Test); 203*0b57cec5SDimitry Andric if (FunctionData.find(Name) == FunctionData.end()) { 204*0b57cec5SDimitry Andric Overlap.addOneUnique(FuncLevelOverlap.Test); 205*0b57cec5SDimitry Andric return; 206*0b57cec5SDimitry Andric } 207*0b57cec5SDimitry Andric if (FuncLevelOverlap.Test.CountSum < 1.0f) { 208*0b57cec5SDimitry Andric Overlap.Overlap.NumEntries += 1; 209*0b57cec5SDimitry Andric return; 210*0b57cec5SDimitry Andric } 211*0b57cec5SDimitry Andric auto &ProfileDataMap = FunctionData[Name]; 212*0b57cec5SDimitry Andric bool NewFunc; 213*0b57cec5SDimitry Andric ProfilingData::iterator Where; 214*0b57cec5SDimitry Andric std::tie(Where, NewFunc) = 215*0b57cec5SDimitry Andric ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord())); 216*0b57cec5SDimitry Andric if (NewFunc) { 217*0b57cec5SDimitry Andric Overlap.addOneMismatch(FuncLevelOverlap.Test); 218*0b57cec5SDimitry Andric return; 219*0b57cec5SDimitry Andric } 220*0b57cec5SDimitry Andric InstrProfRecord &Dest = Where->second; 221*0b57cec5SDimitry Andric 222*0b57cec5SDimitry Andric uint64_t ValueCutoff = FuncFilter.ValueCutoff; 223349cc55cSDimitry Andric if (!FuncFilter.NameFilter.empty() && Name.contains(FuncFilter.NameFilter)) 224*0b57cec5SDimitry Andric ValueCutoff = 0; 225*0b57cec5SDimitry Andric 226*0b57cec5SDimitry Andric Dest.overlap(Other, Overlap, FuncLevelOverlap, ValueCutoff); 227*0b57cec5SDimitry Andric } 228*0b57cec5SDimitry Andric 229*0b57cec5SDimitry Andric void InstrProfWriter::addRecord(StringRef Name, uint64_t Hash, 230*0b57cec5SDimitry Andric InstrProfRecord &&I, uint64_t Weight, 231*0b57cec5SDimitry Andric function_ref<void(Error)> Warn) { 232*0b57cec5SDimitry Andric auto &ProfileDataMap = FunctionData[Name]; 233*0b57cec5SDimitry Andric 234*0b57cec5SDimitry Andric bool NewFunc; 235*0b57cec5SDimitry Andric ProfilingData::iterator Where; 236*0b57cec5SDimitry Andric std::tie(Where, NewFunc) = 237*0b57cec5SDimitry Andric ProfileDataMap.insert(std::make_pair(Hash, InstrProfRecord())); 238*0b57cec5SDimitry Andric InstrProfRecord &Dest = Where->second; 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric auto MapWarn = [&](instrprof_error E) { 241*0b57cec5SDimitry Andric Warn(make_error<InstrProfError>(E)); 242*0b57cec5SDimitry Andric }; 243*0b57cec5SDimitry Andric 244*0b57cec5SDimitry Andric if (NewFunc) { 245*0b57cec5SDimitry Andric // We've never seen a function with this name and hash, add it. 246*0b57cec5SDimitry Andric Dest = std::move(I); 247*0b57cec5SDimitry Andric if (Weight > 1) 248e8d8bef9SDimitry Andric Dest.scale(Weight, 1, MapWarn); 249*0b57cec5SDimitry Andric } else { 250*0b57cec5SDimitry Andric // We're updating a function we've seen before. 251*0b57cec5SDimitry Andric Dest.merge(I, Weight, MapWarn); 252*0b57cec5SDimitry Andric } 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric Dest.sortValueData(); 255*0b57cec5SDimitry Andric } 256*0b57cec5SDimitry Andric 25781ad6265SDimitry Andric void InstrProfWriter::addMemProfRecord( 25881ad6265SDimitry Andric const Function::GUID Id, const memprof::IndexedMemProfRecord &Record) { 25981ad6265SDimitry Andric auto Result = MemProfRecordData.insert({Id, Record}); 26081ad6265SDimitry Andric // If we inserted a new record then we are done. 26181ad6265SDimitry Andric if (Result.second) { 26281ad6265SDimitry Andric return; 26381ad6265SDimitry Andric } 26481ad6265SDimitry Andric memprof::IndexedMemProfRecord &Existing = Result.first->second; 26581ad6265SDimitry Andric Existing.merge(Record); 26681ad6265SDimitry Andric } 26781ad6265SDimitry Andric 26881ad6265SDimitry Andric bool InstrProfWriter::addMemProfFrame(const memprof::FrameId Id, 26981ad6265SDimitry Andric const memprof::Frame &Frame, 27081ad6265SDimitry Andric function_ref<void(Error)> Warn) { 27181ad6265SDimitry Andric auto Result = MemProfFrameData.insert({Id, Frame}); 27281ad6265SDimitry Andric // If a mapping already exists for the current frame id and it does not 27381ad6265SDimitry Andric // match the new mapping provided then reset the existing contents and bail 27481ad6265SDimitry Andric // out. We don't support the merging of memprof data whose Frame -> Id 27581ad6265SDimitry Andric // mapping across profiles is inconsistent. 27681ad6265SDimitry Andric if (!Result.second && Result.first->second != Frame) { 27781ad6265SDimitry Andric Warn(make_error<InstrProfError>(instrprof_error::malformed, 27881ad6265SDimitry Andric "frame to id mapping mismatch")); 27981ad6265SDimitry Andric return false; 28081ad6265SDimitry Andric } 28181ad6265SDimitry Andric return true; 28281ad6265SDimitry Andric } 28381ad6265SDimitry Andric 284bdd1243dSDimitry Andric void InstrProfWriter::addBinaryIds(ArrayRef<llvm::object::BuildID> BIs) { 285bdd1243dSDimitry Andric llvm::append_range(BinaryIds, BIs); 286bdd1243dSDimitry Andric } 287bdd1243dSDimitry Andric 288*0b57cec5SDimitry Andric void InstrProfWriter::mergeRecordsFromWriter(InstrProfWriter &&IPW, 289*0b57cec5SDimitry Andric function_ref<void(Error)> Warn) { 290*0b57cec5SDimitry Andric for (auto &I : IPW.FunctionData) 291*0b57cec5SDimitry Andric for (auto &Func : I.getValue()) 292*0b57cec5SDimitry Andric addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn); 29381ad6265SDimitry Andric 294bdd1243dSDimitry Andric BinaryIds.reserve(BinaryIds.size() + IPW.BinaryIds.size()); 295bdd1243dSDimitry Andric for (auto &I : IPW.BinaryIds) 296bdd1243dSDimitry Andric addBinaryIds(I); 297bdd1243dSDimitry Andric 29881ad6265SDimitry Andric MemProfFrameData.reserve(IPW.MemProfFrameData.size()); 29981ad6265SDimitry Andric for (auto &I : IPW.MemProfFrameData) { 30081ad6265SDimitry Andric // If we weren't able to add the frame mappings then it doesn't make sense 30181ad6265SDimitry Andric // to try to merge the records from this profile. 30281ad6265SDimitry Andric if (!addMemProfFrame(I.first, I.second, Warn)) 30381ad6265SDimitry Andric return; 30481ad6265SDimitry Andric } 30581ad6265SDimitry Andric 30681ad6265SDimitry Andric MemProfRecordData.reserve(IPW.MemProfRecordData.size()); 30781ad6265SDimitry Andric for (auto &I : IPW.MemProfRecordData) { 30881ad6265SDimitry Andric addMemProfRecord(I.first, I.second); 30981ad6265SDimitry Andric } 310*0b57cec5SDimitry Andric } 311*0b57cec5SDimitry Andric 312*0b57cec5SDimitry Andric bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) { 313*0b57cec5SDimitry Andric if (!Sparse) 314*0b57cec5SDimitry Andric return true; 315*0b57cec5SDimitry Andric for (const auto &Func : PD) { 316*0b57cec5SDimitry Andric const InstrProfRecord &IPR = Func.second; 317*0b57cec5SDimitry Andric if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; })) 318*0b57cec5SDimitry Andric return true; 319*0b57cec5SDimitry Andric } 320*0b57cec5SDimitry Andric return false; 321*0b57cec5SDimitry Andric } 322*0b57cec5SDimitry Andric 323*0b57cec5SDimitry Andric static void setSummary(IndexedInstrProf::Summary *TheSummary, 324*0b57cec5SDimitry Andric ProfileSummary &PS) { 325*0b57cec5SDimitry Andric using namespace IndexedInstrProf; 326*0b57cec5SDimitry Andric 327349cc55cSDimitry Andric const std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary(); 328*0b57cec5SDimitry Andric TheSummary->NumSummaryFields = Summary::NumKinds; 329*0b57cec5SDimitry Andric TheSummary->NumCutoffEntries = Res.size(); 330*0b57cec5SDimitry Andric TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount()); 331*0b57cec5SDimitry Andric TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount()); 332*0b57cec5SDimitry Andric TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount()); 333*0b57cec5SDimitry Andric TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount()); 334*0b57cec5SDimitry Andric TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts()); 335*0b57cec5SDimitry Andric TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions()); 336*0b57cec5SDimitry Andric for (unsigned I = 0; I < Res.size(); I++) 337*0b57cec5SDimitry Andric TheSummary->setEntry(I, Res[I]); 338*0b57cec5SDimitry Andric } 339*0b57cec5SDimitry Andric 340fe6060f1SDimitry Andric Error InstrProfWriter::writeImpl(ProfOStream &OS) { 341*0b57cec5SDimitry Andric using namespace IndexedInstrProf; 342bdd1243dSDimitry Andric using namespace support; 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator; 345*0b57cec5SDimitry Andric 346*0b57cec5SDimitry Andric InstrProfSummaryBuilder ISB(ProfileSummaryBuilder::DefaultCutoffs); 347*0b57cec5SDimitry Andric InfoObj->SummaryBuilder = &ISB; 348*0b57cec5SDimitry Andric InstrProfSummaryBuilder CSISB(ProfileSummaryBuilder::DefaultCutoffs); 349*0b57cec5SDimitry Andric InfoObj->CSSummaryBuilder = &CSISB; 350*0b57cec5SDimitry Andric 351*0b57cec5SDimitry Andric // Populate the hash table generator. 352*0b57cec5SDimitry Andric for (const auto &I : FunctionData) 353*0b57cec5SDimitry Andric if (shouldEncodeData(I.getValue())) 354*0b57cec5SDimitry Andric Generator.insert(I.getKey(), &I.getValue()); 35581ad6265SDimitry Andric 356*0b57cec5SDimitry Andric // Write the header. 357*0b57cec5SDimitry Andric IndexedInstrProf::Header Header; 358*0b57cec5SDimitry Andric Header.Magic = IndexedInstrProf::Magic; 359*0b57cec5SDimitry Andric Header.Version = IndexedInstrProf::ProfVersion::CurrentVersion; 36081ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::IRInstrumentation)) 361*0b57cec5SDimitry Andric Header.Version |= VARIANT_MASK_IR_PROF; 36281ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive)) 363*0b57cec5SDimitry Andric Header.Version |= VARIANT_MASK_CSIR_PROF; 36481ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & 36581ad6265SDimitry Andric InstrProfKind::FunctionEntryInstrumentation)) 366e8d8bef9SDimitry Andric Header.Version |= VARIANT_MASK_INSTR_ENTRY; 3671fd87a68SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage)) 3681fd87a68SDimitry Andric Header.Version |= VARIANT_MASK_BYTE_COVERAGE; 3691fd87a68SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::FunctionEntryOnly)) 3701fd87a68SDimitry Andric Header.Version |= VARIANT_MASK_FUNCTION_ENTRY_ONLY; 37181ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::MemProf)) 37281ad6265SDimitry Andric Header.Version |= VARIANT_MASK_MEMPROF; 373e8d8bef9SDimitry Andric 374*0b57cec5SDimitry Andric Header.Unused = 0; 375*0b57cec5SDimitry Andric Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType); 376*0b57cec5SDimitry Andric Header.HashOffset = 0; 37781ad6265SDimitry Andric Header.MemProfOffset = 0; 378bdd1243dSDimitry Andric Header.BinaryIdOffset = 0; 379*0b57cec5SDimitry Andric int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t); 380*0b57cec5SDimitry Andric 381bdd1243dSDimitry Andric // Only write out all the fields except 'HashOffset', 'MemProfOffset' and 382bdd1243dSDimitry Andric // 'BinaryIdOffset'. We need to remember the offset of these fields to allow 383bdd1243dSDimitry Andric // back patching later. 384bdd1243dSDimitry Andric for (int I = 0; I < N - 3; I++) 385*0b57cec5SDimitry Andric OS.write(reinterpret_cast<uint64_t *>(&Header)[I]); 386*0b57cec5SDimitry Andric 387*0b57cec5SDimitry Andric // Save the location of Header.HashOffset field in \c OS. 388*0b57cec5SDimitry Andric uint64_t HashTableStartFieldOffset = OS.tell(); 389*0b57cec5SDimitry Andric // Reserve the space for HashOffset field. 390*0b57cec5SDimitry Andric OS.write(0); 391*0b57cec5SDimitry Andric 39281ad6265SDimitry Andric // Save the location of MemProf profile data. This is stored in two parts as 39381ad6265SDimitry Andric // the schema and as a separate on-disk chained hashtable. 39481ad6265SDimitry Andric uint64_t MemProfSectionOffset = OS.tell(); 39581ad6265SDimitry Andric // Reserve space for the MemProf table field to be patched later if this 39681ad6265SDimitry Andric // profile contains memory profile information. 39781ad6265SDimitry Andric OS.write(0); 39881ad6265SDimitry Andric 399bdd1243dSDimitry Andric // Save the location of binary ids section. 400bdd1243dSDimitry Andric uint64_t BinaryIdSectionOffset = OS.tell(); 401bdd1243dSDimitry Andric // Reserve space for the BinaryIdOffset field to be patched later if this 402bdd1243dSDimitry Andric // profile contains binary ids. 403bdd1243dSDimitry Andric OS.write(0); 404bdd1243dSDimitry Andric 405*0b57cec5SDimitry Andric // Reserve space to write profile summary data. 406*0b57cec5SDimitry Andric uint32_t NumEntries = ProfileSummaryBuilder::DefaultCutoffs.size(); 407*0b57cec5SDimitry Andric uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries); 408*0b57cec5SDimitry Andric // Remember the summary offset. 409*0b57cec5SDimitry Andric uint64_t SummaryOffset = OS.tell(); 410*0b57cec5SDimitry Andric for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++) 411*0b57cec5SDimitry Andric OS.write(0); 412*0b57cec5SDimitry Andric uint64_t CSSummaryOffset = 0; 413*0b57cec5SDimitry Andric uint64_t CSSummarySize = 0; 41481ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive)) { 415*0b57cec5SDimitry Andric CSSummaryOffset = OS.tell(); 416*0b57cec5SDimitry Andric CSSummarySize = SummarySize / sizeof(uint64_t); 417*0b57cec5SDimitry Andric for (unsigned I = 0; I < CSSummarySize; I++) 418*0b57cec5SDimitry Andric OS.write(0); 419*0b57cec5SDimitry Andric } 420*0b57cec5SDimitry Andric 421*0b57cec5SDimitry Andric // Write the hash table. 422*0b57cec5SDimitry Andric uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj); 423*0b57cec5SDimitry Andric 42481ad6265SDimitry Andric // Write the MemProf profile data if we have it. This includes a simple schema 42581ad6265SDimitry Andric // with the format described below followed by the hashtable: 42681ad6265SDimitry Andric // uint64_t RecordTableOffset = RecordTableGenerator.Emit 42781ad6265SDimitry Andric // uint64_t FramePayloadOffset = Stream offset before emitting the frame table 42881ad6265SDimitry Andric // uint64_t FrameTableOffset = FrameTableGenerator.Emit 42981ad6265SDimitry Andric // uint64_t Num schema entries 43081ad6265SDimitry Andric // uint64_t Schema entry 0 43181ad6265SDimitry Andric // uint64_t Schema entry 1 43281ad6265SDimitry Andric // .... 43381ad6265SDimitry Andric // uint64_t Schema entry N - 1 43481ad6265SDimitry Andric // OnDiskChainedHashTable MemProfRecordData 43581ad6265SDimitry Andric // OnDiskChainedHashTable MemProfFrameData 43681ad6265SDimitry Andric uint64_t MemProfSectionStart = 0; 43781ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::MemProf)) { 43881ad6265SDimitry Andric MemProfSectionStart = OS.tell(); 43981ad6265SDimitry Andric OS.write(0ULL); // Reserve space for the memprof record table offset. 44081ad6265SDimitry Andric OS.write(0ULL); // Reserve space for the memprof frame payload offset. 44181ad6265SDimitry Andric OS.write(0ULL); // Reserve space for the memprof frame table offset. 44281ad6265SDimitry Andric 44381ad6265SDimitry Andric auto Schema = memprof::PortableMemInfoBlock::getSchema(); 44481ad6265SDimitry Andric OS.write(static_cast<uint64_t>(Schema.size())); 44581ad6265SDimitry Andric for (const auto Id : Schema) { 44681ad6265SDimitry Andric OS.write(static_cast<uint64_t>(Id)); 44781ad6265SDimitry Andric } 44881ad6265SDimitry Andric 44981ad6265SDimitry Andric auto RecordWriter = std::make_unique<memprof::RecordWriterTrait>(); 45081ad6265SDimitry Andric RecordWriter->Schema = &Schema; 45181ad6265SDimitry Andric OnDiskChainedHashTableGenerator<memprof::RecordWriterTrait> 45281ad6265SDimitry Andric RecordTableGenerator; 45381ad6265SDimitry Andric for (auto &I : MemProfRecordData) { 45481ad6265SDimitry Andric // Insert the key (func hash) and value (memprof record). 45581ad6265SDimitry Andric RecordTableGenerator.insert(I.first, I.second); 45681ad6265SDimitry Andric } 45781ad6265SDimitry Andric 45881ad6265SDimitry Andric uint64_t RecordTableOffset = 45981ad6265SDimitry Andric RecordTableGenerator.Emit(OS.OS, *RecordWriter); 46081ad6265SDimitry Andric 46181ad6265SDimitry Andric uint64_t FramePayloadOffset = OS.tell(); 46281ad6265SDimitry Andric 46381ad6265SDimitry Andric auto FrameWriter = std::make_unique<memprof::FrameWriterTrait>(); 46481ad6265SDimitry Andric OnDiskChainedHashTableGenerator<memprof::FrameWriterTrait> 46581ad6265SDimitry Andric FrameTableGenerator; 46681ad6265SDimitry Andric for (auto &I : MemProfFrameData) { 46781ad6265SDimitry Andric // Insert the key (frame id) and value (frame contents). 46881ad6265SDimitry Andric FrameTableGenerator.insert(I.first, I.second); 46981ad6265SDimitry Andric } 47081ad6265SDimitry Andric 47181ad6265SDimitry Andric uint64_t FrameTableOffset = FrameTableGenerator.Emit(OS.OS, *FrameWriter); 47281ad6265SDimitry Andric 47381ad6265SDimitry Andric PatchItem PatchItems[] = { 47481ad6265SDimitry Andric {MemProfSectionStart, &RecordTableOffset, 1}, 47581ad6265SDimitry Andric {MemProfSectionStart + sizeof(uint64_t), &FramePayloadOffset, 1}, 47681ad6265SDimitry Andric {MemProfSectionStart + 2 * sizeof(uint64_t), &FrameTableOffset, 1}, 47781ad6265SDimitry Andric }; 47881ad6265SDimitry Andric OS.patch(PatchItems, 3); 47981ad6265SDimitry Andric } 48081ad6265SDimitry Andric 481bdd1243dSDimitry Andric // BinaryIdSection has two parts: 482bdd1243dSDimitry Andric // 1. uint64_t BinaryIdsSectionSize 483bdd1243dSDimitry Andric // 2. list of binary ids that consist of: 484bdd1243dSDimitry Andric // a. uint64_t BinaryIdLength 485bdd1243dSDimitry Andric // b. uint8_t BinaryIdData 486bdd1243dSDimitry Andric // c. uint8_t Padding (if necessary) 487bdd1243dSDimitry Andric uint64_t BinaryIdSectionStart = OS.tell(); 488bdd1243dSDimitry Andric // Calculate size of binary section. 489bdd1243dSDimitry Andric uint64_t BinaryIdsSectionSize = 0; 490bdd1243dSDimitry Andric 491bdd1243dSDimitry Andric // Remove duplicate binary ids. 492bdd1243dSDimitry Andric llvm::sort(BinaryIds); 493bdd1243dSDimitry Andric BinaryIds.erase(std::unique(BinaryIds.begin(), BinaryIds.end()), 494bdd1243dSDimitry Andric BinaryIds.end()); 495bdd1243dSDimitry Andric 496bdd1243dSDimitry Andric for (auto BI : BinaryIds) { 497bdd1243dSDimitry Andric // Increment by binary id length data type size. 498bdd1243dSDimitry Andric BinaryIdsSectionSize += sizeof(uint64_t); 499bdd1243dSDimitry Andric // Increment by binary id data length, aligned to 8 bytes. 500bdd1243dSDimitry Andric BinaryIdsSectionSize += alignToPowerOf2(BI.size(), sizeof(uint64_t)); 501bdd1243dSDimitry Andric } 502bdd1243dSDimitry Andric // Write binary ids section size. 503bdd1243dSDimitry Andric OS.write(BinaryIdsSectionSize); 504bdd1243dSDimitry Andric 505bdd1243dSDimitry Andric for (auto BI : BinaryIds) { 506bdd1243dSDimitry Andric uint64_t BILen = BI.size(); 507bdd1243dSDimitry Andric // Write binary id length. 508bdd1243dSDimitry Andric OS.write(BILen); 509bdd1243dSDimitry Andric // Write binary id data. 510bdd1243dSDimitry Andric for (unsigned K = 0; K < BILen; K++) 511bdd1243dSDimitry Andric OS.writeByte(BI[K]); 512bdd1243dSDimitry Andric // Write padding if necessary. 513bdd1243dSDimitry Andric uint64_t PaddingSize = alignToPowerOf2(BILen, sizeof(uint64_t)) - BILen; 514bdd1243dSDimitry Andric for (unsigned K = 0; K < PaddingSize; K++) 515bdd1243dSDimitry Andric OS.writeByte(0); 516bdd1243dSDimitry Andric } 517bdd1243dSDimitry Andric 518*0b57cec5SDimitry Andric // Allocate space for data to be serialized out. 519*0b57cec5SDimitry Andric std::unique_ptr<IndexedInstrProf::Summary> TheSummary = 520*0b57cec5SDimitry Andric IndexedInstrProf::allocSummary(SummarySize); 521*0b57cec5SDimitry Andric // Compute the Summary and copy the data to the data 522*0b57cec5SDimitry Andric // structure to be serialized out (to disk or buffer). 523*0b57cec5SDimitry Andric std::unique_ptr<ProfileSummary> PS = ISB.getSummary(); 524*0b57cec5SDimitry Andric setSummary(TheSummary.get(), *PS); 525*0b57cec5SDimitry Andric InfoObj->SummaryBuilder = nullptr; 526*0b57cec5SDimitry Andric 527*0b57cec5SDimitry Andric // For Context Sensitive summary. 528*0b57cec5SDimitry Andric std::unique_ptr<IndexedInstrProf::Summary> TheCSSummary = nullptr; 52981ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive)) { 530*0b57cec5SDimitry Andric TheCSSummary = IndexedInstrProf::allocSummary(SummarySize); 531*0b57cec5SDimitry Andric std::unique_ptr<ProfileSummary> CSPS = CSISB.getSummary(); 532*0b57cec5SDimitry Andric setSummary(TheCSSummary.get(), *CSPS); 533*0b57cec5SDimitry Andric } 534*0b57cec5SDimitry Andric InfoObj->CSSummaryBuilder = nullptr; 535*0b57cec5SDimitry Andric 536*0b57cec5SDimitry Andric // Now do the final patch: 537*0b57cec5SDimitry Andric PatchItem PatchItems[] = { 538*0b57cec5SDimitry Andric // Patch the Header.HashOffset field. 539*0b57cec5SDimitry Andric {HashTableStartFieldOffset, &HashTableStart, 1}, 540bdd1243dSDimitry Andric // Patch the Header.MemProfOffset (=0 for profiles without MemProf 541bdd1243dSDimitry Andric // data). 54281ad6265SDimitry Andric {MemProfSectionOffset, &MemProfSectionStart, 1}, 543bdd1243dSDimitry Andric // Patch the Header.BinaryIdSectionOffset. 544bdd1243dSDimitry Andric {BinaryIdSectionOffset, &BinaryIdSectionStart, 1}, 545*0b57cec5SDimitry Andric // Patch the summary data. 546*0b57cec5SDimitry Andric {SummaryOffset, reinterpret_cast<uint64_t *>(TheSummary.get()), 547*0b57cec5SDimitry Andric (int)(SummarySize / sizeof(uint64_t))}, 548*0b57cec5SDimitry Andric {CSSummaryOffset, reinterpret_cast<uint64_t *>(TheCSSummary.get()), 549*0b57cec5SDimitry Andric (int)CSSummarySize}}; 550*0b57cec5SDimitry Andric 551bdd1243dSDimitry Andric OS.patch(PatchItems, std::size(PatchItems)); 552fe6060f1SDimitry Andric 553fe6060f1SDimitry Andric for (const auto &I : FunctionData) 554fe6060f1SDimitry Andric for (const auto &F : I.getValue()) 555fe6060f1SDimitry Andric if (Error E = validateRecord(F.second)) 556fe6060f1SDimitry Andric return E; 557fe6060f1SDimitry Andric 558fe6060f1SDimitry Andric return Error::success(); 559*0b57cec5SDimitry Andric } 560*0b57cec5SDimitry Andric 561fe6060f1SDimitry Andric Error InstrProfWriter::write(raw_fd_ostream &OS) { 562*0b57cec5SDimitry Andric // Write the hash table. 563*0b57cec5SDimitry Andric ProfOStream POS(OS); 564fe6060f1SDimitry Andric return writeImpl(POS); 565*0b57cec5SDimitry Andric } 566*0b57cec5SDimitry Andric 567*0b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() { 568*0b57cec5SDimitry Andric std::string Data; 569*0b57cec5SDimitry Andric raw_string_ostream OS(Data); 570*0b57cec5SDimitry Andric ProfOStream POS(OS); 571*0b57cec5SDimitry Andric // Write the hash table. 572fe6060f1SDimitry Andric if (Error E = writeImpl(POS)) 573fe6060f1SDimitry Andric return nullptr; 574*0b57cec5SDimitry Andric // Return this in an aligned memory buffer. 575*0b57cec5SDimitry Andric return MemoryBuffer::getMemBufferCopy(Data); 576*0b57cec5SDimitry Andric } 577*0b57cec5SDimitry Andric 578*0b57cec5SDimitry Andric static const char *ValueProfKindStr[] = { 579*0b57cec5SDimitry Andric #define VALUE_PROF_KIND(Enumerator, Value, Descr) #Enumerator, 580*0b57cec5SDimitry Andric #include "llvm/ProfileData/InstrProfData.inc" 581*0b57cec5SDimitry Andric }; 582*0b57cec5SDimitry Andric 583fe6060f1SDimitry Andric Error InstrProfWriter::validateRecord(const InstrProfRecord &Func) { 584fe6060f1SDimitry Andric for (uint32_t VK = 0; VK <= IPVK_Last; VK++) { 585fe6060f1SDimitry Andric uint32_t NS = Func.getNumValueSites(VK); 586fe6060f1SDimitry Andric if (!NS) 587fe6060f1SDimitry Andric continue; 588fe6060f1SDimitry Andric for (uint32_t S = 0; S < NS; S++) { 589fe6060f1SDimitry Andric uint32_t ND = Func.getNumValueDataForSite(VK, S); 590fe6060f1SDimitry Andric std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S); 591bdd1243dSDimitry Andric DenseSet<uint64_t> SeenValues; 592fe6060f1SDimitry Andric for (uint32_t I = 0; I < ND; I++) 593bdd1243dSDimitry Andric if ((VK != IPVK_IndirectCallTarget) && !SeenValues.insert(VD[I].Value).second) 594fe6060f1SDimitry Andric return make_error<InstrProfError>(instrprof_error::invalid_prof); 595fe6060f1SDimitry Andric } 596fe6060f1SDimitry Andric } 597fe6060f1SDimitry Andric 598fe6060f1SDimitry Andric return Error::success(); 599fe6060f1SDimitry Andric } 600fe6060f1SDimitry Andric 601*0b57cec5SDimitry Andric void InstrProfWriter::writeRecordInText(StringRef Name, uint64_t Hash, 602*0b57cec5SDimitry Andric const InstrProfRecord &Func, 603*0b57cec5SDimitry Andric InstrProfSymtab &Symtab, 604*0b57cec5SDimitry Andric raw_fd_ostream &OS) { 605*0b57cec5SDimitry Andric OS << Name << "\n"; 606*0b57cec5SDimitry Andric OS << "# Func Hash:\n" << Hash << "\n"; 607*0b57cec5SDimitry Andric OS << "# Num Counters:\n" << Func.Counts.size() << "\n"; 608*0b57cec5SDimitry Andric OS << "# Counter Values:\n"; 609*0b57cec5SDimitry Andric for (uint64_t Count : Func.Counts) 610*0b57cec5SDimitry Andric OS << Count << "\n"; 611*0b57cec5SDimitry Andric 612*0b57cec5SDimitry Andric uint32_t NumValueKinds = Func.getNumValueKinds(); 613*0b57cec5SDimitry Andric if (!NumValueKinds) { 614*0b57cec5SDimitry Andric OS << "\n"; 615*0b57cec5SDimitry Andric return; 616*0b57cec5SDimitry Andric } 617*0b57cec5SDimitry Andric 618*0b57cec5SDimitry Andric OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n"; 619*0b57cec5SDimitry Andric for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) { 620*0b57cec5SDimitry Andric uint32_t NS = Func.getNumValueSites(VK); 621*0b57cec5SDimitry Andric if (!NS) 622*0b57cec5SDimitry Andric continue; 623*0b57cec5SDimitry Andric OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n"; 624*0b57cec5SDimitry Andric OS << "# NumValueSites:\n" << NS << "\n"; 625*0b57cec5SDimitry Andric for (uint32_t S = 0; S < NS; S++) { 626*0b57cec5SDimitry Andric uint32_t ND = Func.getNumValueDataForSite(VK, S); 627*0b57cec5SDimitry Andric OS << ND << "\n"; 628*0b57cec5SDimitry Andric std::unique_ptr<InstrProfValueData[]> VD = Func.getValueForSite(VK, S); 629*0b57cec5SDimitry Andric for (uint32_t I = 0; I < ND; I++) { 630*0b57cec5SDimitry Andric if (VK == IPVK_IndirectCallTarget) 631*0b57cec5SDimitry Andric OS << Symtab.getFuncNameOrExternalSymbol(VD[I].Value) << ":" 632*0b57cec5SDimitry Andric << VD[I].Count << "\n"; 633*0b57cec5SDimitry Andric else 634*0b57cec5SDimitry Andric OS << VD[I].Value << ":" << VD[I].Count << "\n"; 635*0b57cec5SDimitry Andric } 636*0b57cec5SDimitry Andric } 637*0b57cec5SDimitry Andric } 638*0b57cec5SDimitry Andric 639*0b57cec5SDimitry Andric OS << "\n"; 640*0b57cec5SDimitry Andric } 641*0b57cec5SDimitry Andric 642*0b57cec5SDimitry Andric Error InstrProfWriter::writeText(raw_fd_ostream &OS) { 6431fd87a68SDimitry Andric // Check CS first since it implies an IR level profile. 64481ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive)) 645*0b57cec5SDimitry Andric OS << "# CSIR level Instrumentation Flag\n:csir\n"; 64681ad6265SDimitry Andric else if (static_cast<bool>(ProfileKind & InstrProfKind::IRInstrumentation)) 6471fd87a68SDimitry Andric OS << "# IR level Instrumentation Flag\n:ir\n"; 6481fd87a68SDimitry Andric 64981ad6265SDimitry Andric if (static_cast<bool>(ProfileKind & 65081ad6265SDimitry Andric InstrProfKind::FunctionEntryInstrumentation)) 651e8d8bef9SDimitry Andric OS << "# Always instrument the function entry block\n:entry_first\n"; 652*0b57cec5SDimitry Andric InstrProfSymtab Symtab; 653*0b57cec5SDimitry Andric 654*0b57cec5SDimitry Andric using FuncPair = detail::DenseMapPair<uint64_t, InstrProfRecord>; 655*0b57cec5SDimitry Andric using RecordType = std::pair<StringRef, FuncPair>; 656*0b57cec5SDimitry Andric SmallVector<RecordType, 4> OrderedFuncData; 657*0b57cec5SDimitry Andric 658*0b57cec5SDimitry Andric for (const auto &I : FunctionData) { 659*0b57cec5SDimitry Andric if (shouldEncodeData(I.getValue())) { 660*0b57cec5SDimitry Andric if (Error E = Symtab.addFuncName(I.getKey())) 661*0b57cec5SDimitry Andric return E; 662*0b57cec5SDimitry Andric for (const auto &Func : I.getValue()) 663*0b57cec5SDimitry Andric OrderedFuncData.push_back(std::make_pair(I.getKey(), Func)); 664*0b57cec5SDimitry Andric } 665*0b57cec5SDimitry Andric } 666*0b57cec5SDimitry Andric 667*0b57cec5SDimitry Andric llvm::sort(OrderedFuncData, [](const RecordType &A, const RecordType &B) { 668*0b57cec5SDimitry Andric return std::tie(A.first, A.second.first) < 669*0b57cec5SDimitry Andric std::tie(B.first, B.second.first); 670*0b57cec5SDimitry Andric }); 671*0b57cec5SDimitry Andric 672*0b57cec5SDimitry Andric for (const auto &record : OrderedFuncData) { 673*0b57cec5SDimitry Andric const StringRef &Name = record.first; 674*0b57cec5SDimitry Andric const FuncPair &Func = record.second; 675*0b57cec5SDimitry Andric writeRecordInText(Name, Func.first, Func.second, Symtab, OS); 676*0b57cec5SDimitry Andric } 677*0b57cec5SDimitry Andric 678fe6060f1SDimitry Andric for (const auto &record : OrderedFuncData) { 679fe6060f1SDimitry Andric const FuncPair &Func = record.second; 680fe6060f1SDimitry Andric if (Error E = validateRecord(Func.second)) 681fe6060f1SDimitry Andric return E; 682fe6060f1SDimitry Andric } 683fe6060f1SDimitry Andric 684*0b57cec5SDimitry Andric return Error::success(); 685*0b57cec5SDimitry Andric } 686