1b9bd7f85SJustin Bogner //=-- InstrProfWriter.cpp - Instrumented profiling writer -------------------=//
2b9bd7f85SJustin Bogner //
3b9bd7f85SJustin Bogner //                     The LLVM Compiler Infrastructure
4b9bd7f85SJustin Bogner //
5b9bd7f85SJustin Bogner // This file is distributed under the University of Illinois Open Source
6b9bd7f85SJustin Bogner // License. See LICENSE.TXT for details.
7b9bd7f85SJustin Bogner //
8b9bd7f85SJustin Bogner //===----------------------------------------------------------------------===//
9b9bd7f85SJustin Bogner //
10b9bd7f85SJustin Bogner // This file contains support for writing profiling data for clang's
11b9bd7f85SJustin Bogner // instrumentation based PGO and coverage.
12b9bd7f85SJustin Bogner //
13b9bd7f85SJustin Bogner //===----------------------------------------------------------------------===//
14b9bd7f85SJustin Bogner 
15b9bd7f85SJustin Bogner #include "llvm/ProfileData/InstrProfWriter.h"
16b7aa2630SJustin Bogner #include "llvm/ADT/StringExtras.h"
17b7aa2630SJustin Bogner #include "llvm/Support/EndianStream.h"
18b7aa2630SJustin Bogner #include "llvm/Support/OnDiskHashTable.h"
19437b1b3eSReid Kleckner #include <tuple>
20b7aa2630SJustin Bogner 
21b9bd7f85SJustin Bogner using namespace llvm;
22b9bd7f85SJustin Bogner 
23b7aa2630SJustin Bogner namespace {
24ee415895SXinliang David Li static support::endianness ValueProfDataEndianness = support::little;
25ee415895SXinliang David Li 
26b7aa2630SJustin Bogner class InstrProfRecordTrait {
27b7aa2630SJustin Bogner public:
28b7aa2630SJustin Bogner   typedef StringRef key_type;
29b7aa2630SJustin Bogner   typedef StringRef key_type_ref;
30b7aa2630SJustin Bogner 
319e9a057aSJustin Bogner   typedef const InstrProfWriter::ProfilingData *const data_type;
329e9a057aSJustin Bogner   typedef const InstrProfWriter::ProfilingData *const data_type_ref;
33b7aa2630SJustin Bogner 
34b7aa2630SJustin Bogner   typedef uint64_t hash_value_type;
35b7aa2630SJustin Bogner   typedef uint64_t offset_type;
36b7aa2630SJustin Bogner 
37b7aa2630SJustin Bogner   static hash_value_type ComputeHash(key_type_ref K) {
38b7aa2630SJustin Bogner     return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, K);
39b7aa2630SJustin Bogner   }
40b7aa2630SJustin Bogner 
41b7aa2630SJustin Bogner   static std::pair<offset_type, offset_type>
42b7aa2630SJustin Bogner   EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V) {
43b7aa2630SJustin Bogner     using namespace llvm::support;
44b7aa2630SJustin Bogner     endian::Writer<little> LE(Out);
45b7aa2630SJustin Bogner 
46e8081716SJustin Bogner     offset_type N = K.size();
47b7aa2630SJustin Bogner     LE.write<offset_type>(N);
48b7aa2630SJustin Bogner 
49821d7471SJustin Bogner     offset_type M = 0;
509e9a057aSJustin Bogner     for (const auto &ProfileData : *V) {
512004f003SXinliang David Li       const InstrProfRecord &ProfRecord = ProfileData.second;
529e9a057aSJustin Bogner       M += sizeof(uint64_t); // The function hash
539e9a057aSJustin Bogner       M += sizeof(uint64_t); // The size of the Counts vector
542004f003SXinliang David Li       M += ProfRecord.Counts.size() * sizeof(uint64_t);
559e9a057aSJustin Bogner 
569e9a057aSJustin Bogner       // Value data
5799556877SXinliang David Li       M += ValueProfData::getSize(ProfileData.second);
589e9a057aSJustin Bogner     }
59b7aa2630SJustin Bogner     LE.write<offset_type>(M);
60b7aa2630SJustin Bogner 
61b7aa2630SJustin Bogner     return std::make_pair(N, M);
62b7aa2630SJustin Bogner   }
63b7aa2630SJustin Bogner 
64e8081716SJustin Bogner   static void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N){
65b7aa2630SJustin Bogner     Out.write(K.data(), N);
66b7aa2630SJustin Bogner   }
67b7aa2630SJustin Bogner 
68b7aa2630SJustin Bogner   static void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V,
69e8081716SJustin Bogner                        offset_type) {
70b7aa2630SJustin Bogner     using namespace llvm::support;
71b7aa2630SJustin Bogner     endian::Writer<little> LE(Out);
729e9a057aSJustin Bogner     for (const auto &ProfileData : *V) {
732004f003SXinliang David Li       const InstrProfRecord &ProfRecord = ProfileData.second;
742004f003SXinliang David Li 
759e9a057aSJustin Bogner       LE.write<uint64_t>(ProfileData.first); // Function hash
762004f003SXinliang David Li       LE.write<uint64_t>(ProfRecord.Counts.size());
776aa216c2SXinliang David Li       for (uint64_t I : ProfRecord.Counts)
786aa216c2SXinliang David Li         LE.write<uint64_t>(I);
799e9a057aSJustin Bogner 
809e9a057aSJustin Bogner       // Write value data
8199556877SXinliang David Li       std::unique_ptr<ValueProfData> VDataPtr =
8299556877SXinliang David Li           ValueProfData::serializeFrom(ProfileData.second);
83ee415895SXinliang David Li       uint32_t S = VDataPtr->getSize();
84ee415895SXinliang David Li       VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
85ee415895SXinliang David Li       Out.write((const char *)VDataPtr.get(), S);
86b7aa2630SJustin Bogner     }
87821d7471SJustin Bogner   }
88b7aa2630SJustin Bogner };
89f00654e3SAlexander Kornienko }
90b7aa2630SJustin Bogner 
91ee415895SXinliang David Li // Internal interface for testing purpose only.
92ee415895SXinliang David Li void InstrProfWriter::setValueProfDataEndianness(
93ee415895SXinliang David Li     support::endianness Endianness) {
94ee415895SXinliang David Li   ValueProfDataEndianness = Endianness;
95ee415895SXinliang David Li }
96ee415895SXinliang David Li 
979e9a057aSJustin Bogner void InstrProfWriter::updateStringTableReferences(InstrProfRecord &I) {
982004f003SXinliang David Li   I.updateStrings(&StringTable);
999e9a057aSJustin Bogner }
1009e9a057aSJustin Bogner 
1019e9a057aSJustin Bogner std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I) {
1029e9a057aSJustin Bogner   updateStringTableReferences(I);
1039e9a057aSJustin Bogner   auto &ProfileDataMap = FunctionData[I.Name];
1049e9a057aSJustin Bogner 
105a7318297SNathan Slingerland   bool NewFunc;
106a7318297SNathan Slingerland   ProfilingData::iterator Where;
107a7318297SNathan Slingerland   std::tie(Where, NewFunc) =
108a7318297SNathan Slingerland       ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord()));
109a7318297SNathan Slingerland   InstrProfRecord &Dest = Where->second;
110a7318297SNathan Slingerland   if (NewFunc) {
1119e9a057aSJustin Bogner     // We've never seen a function with this name and hash, add it.
112a7318297SNathan Slingerland     Dest = std::move(I);
113a7318297SNathan Slingerland   } else {
114a7318297SNathan Slingerland     // We're updating a function we've seen before.
115a7318297SNathan Slingerland     instrprof_error MergeResult = Dest.merge(I);
116a7318297SNathan Slingerland     if (MergeResult != instrprof_error::success) {
117a7318297SNathan Slingerland       return MergeResult;
118a7318297SNathan Slingerland     }
119b9bd7f85SJustin Bogner   }
120b9bd7f85SJustin Bogner 
121a7318297SNathan Slingerland   // We keep track of the max function count as we go for simplicity.
122a7318297SNathan Slingerland   if (Dest.Counts[0] > MaxFunctionCount)
123a7318297SNathan Slingerland     MaxFunctionCount = Dest.Counts[0];
124a7318297SNathan Slingerland 
125a7318297SNathan Slingerland   return instrprof_error::success;
126b9bd7f85SJustin Bogner }
127b9bd7f85SJustin Bogner 
1282b6c537bSJustin Bogner std::pair<uint64_t, uint64_t> InstrProfWriter::writeImpl(raw_ostream &OS) {
129b7aa2630SJustin Bogner   OnDiskChainedHashTableGenerator<InstrProfRecordTrait> Generator;
130b9bd7f85SJustin Bogner 
131b7aa2630SJustin Bogner   // Populate the hash table generator.
132821d7471SJustin Bogner   for (const auto &I : FunctionData)
133fa5b013dSJustin Bogner     Generator.insert(I.getKey(), &I.getValue());
134b7aa2630SJustin Bogner 
135b7aa2630SJustin Bogner   using namespace llvm::support;
136b7aa2630SJustin Bogner   endian::Writer<little> LE(OS);
137b7aa2630SJustin Bogner 
138b7aa2630SJustin Bogner   // Write the header.
139dab183edSXinliang David Li   IndexedInstrProf::Header Header;
140dab183edSXinliang David Li   Header.Magic = IndexedInstrProf::Magic;
141dab183edSXinliang David Li   Header.Version = IndexedInstrProf::Version;
142dab183edSXinliang David Li   Header.MaxFunctionCount = MaxFunctionCount;
143dab183edSXinliang David Li   Header.HashType = static_cast<uint64_t>(IndexedInstrProf::HashType);
144dab183edSXinliang David Li   Header.HashOffset = 0;
145dab183edSXinliang David Li   int N = sizeof(IndexedInstrProf::Header) / sizeof(uint64_t);
146dab183edSXinliang David Li 
147dab183edSXinliang David Li   // Only write out all the fields execpt 'HashOffset'. We need
148dab183edSXinliang David Li   // to remember the offset of that field to allow back patching
149dab183edSXinliang David Li   // later.
150dab183edSXinliang David Li   for (int I = 0; I < N - 1; I++)
151dab183edSXinliang David Li     LE.write<uint64_t>(reinterpret_cast<uint64_t *>(&Header)[I]);
152b7aa2630SJustin Bogner 
153b7aa2630SJustin Bogner   // Save a space to write the hash table start location.
154b7aa2630SJustin Bogner   uint64_t HashTableStartLoc = OS.tell();
155dab183edSXinliang David Li   // Reserve the space for HashOffset field.
156b7aa2630SJustin Bogner   LE.write<uint64_t>(0);
157b7aa2630SJustin Bogner   // Write the hash table.
158b7aa2630SJustin Bogner   uint64_t HashTableStart = Generator.Emit(OS);
159b7aa2630SJustin Bogner 
1602b6c537bSJustin Bogner   return std::make_pair(HashTableStartLoc, HashTableStart);
1612b6c537bSJustin Bogner }
1622b6c537bSJustin Bogner 
1632b6c537bSJustin Bogner void InstrProfWriter::write(raw_fd_ostream &OS) {
1642b6c537bSJustin Bogner   // Write the hash table.
1652b6c537bSJustin Bogner   auto TableStart = writeImpl(OS);
1662b6c537bSJustin Bogner 
167b7aa2630SJustin Bogner   // Go back and fill in the hash table start.
1682b6c537bSJustin Bogner   using namespace support;
1692b6c537bSJustin Bogner   OS.seek(TableStart.first);
170dab183edSXinliang David Li   // Now patch the HashOffset field previously reserved.
1712b6c537bSJustin Bogner   endian::Writer<little>(OS).write<uint64_t>(TableStart.second);
1722b6c537bSJustin Bogner }
1732b6c537bSJustin Bogner 
174*6f7c19a4SXinliang David Li void InstrProfWriter::writeRecordInText(const InstrProfRecord &Func,
175*6f7c19a4SXinliang David Li                                         raw_fd_ostream &OS) {
176*6f7c19a4SXinliang David Li   OS << Func.Name << "\n" << Func.Hash << "\n" << Func.Counts.size() << "\n";
177*6f7c19a4SXinliang David Li   for (uint64_t Count : Func.Counts)
178*6f7c19a4SXinliang David Li     OS << Count << "\n";
179*6f7c19a4SXinliang David Li 
180*6f7c19a4SXinliang David Li   OS << "\n";
181*6f7c19a4SXinliang David Li }
182*6f7c19a4SXinliang David Li 
183*6f7c19a4SXinliang David Li void InstrProfWriter::writeText(raw_fd_ostream &OS) {
184*6f7c19a4SXinliang David Li   for (const auto &I : FunctionData)
185*6f7c19a4SXinliang David Li     for (const auto &Func : I.getValue())
186*6f7c19a4SXinliang David Li       writeRecordInText(Func.second, OS);
187*6f7c19a4SXinliang David Li }
188*6f7c19a4SXinliang David Li 
1892b6c537bSJustin Bogner std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
1902b6c537bSJustin Bogner   std::string Data;
1912b6c537bSJustin Bogner   llvm::raw_string_ostream OS(Data);
1922b6c537bSJustin Bogner   // Write the hash table.
1932b6c537bSJustin Bogner   auto TableStart = writeImpl(OS);
1942b6c537bSJustin Bogner   OS.flush();
1952b6c537bSJustin Bogner 
1962b6c537bSJustin Bogner   // Go back and fill in the hash table start.
1972b6c537bSJustin Bogner   using namespace support;
1982b6c537bSJustin Bogner   uint64_t Bytes = endian::byte_swap<uint64_t, little>(TableStart.second);
1992b6c537bSJustin Bogner   Data.replace(TableStart.first, sizeof(uint64_t), (const char *)&Bytes,
2002b6c537bSJustin Bogner                sizeof(uint64_t));
2012b6c537bSJustin Bogner 
2022b6c537bSJustin Bogner   // Return this in an aligned memory buffer.
2032b6c537bSJustin Bogner   return MemoryBuffer::getMemBufferCopy(Data);
204b9bd7f85SJustin Bogner }
205