LLVM 22.0.0git
InstrProfWriter.cpp
Go to the documentation of this file.
1//===- InstrProfWriter.cpp - Instrumented profiling writer ----------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains support for writing profiling data for clang's
10// instrumentation based PGO and coverage.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/STLExtras.h"
16#include "llvm/ADT/StringRef.h"
24#include "llvm/Support/Error.h"
28#include <cstdint>
29#include <ctime>
30#include <memory>
31#include <string>
32#include <tuple>
33#include <utility>
34#include <vector>
35
36using namespace llvm;
37
38namespace llvm {
39
41public:
44
47
50
54
56
59 }
60
61 static std::pair<offset_type, offset_type>
63 using namespace support;
64
66
67 offset_type N = K.size();
68 LE.write<offset_type>(N);
69
70 offset_type M = 0;
71 for (const auto &ProfileData : *V) {
72 const InstrProfRecord &ProfRecord = ProfileData.second;
73 M += sizeof(uint64_t); // The function hash
74 M += sizeof(uint64_t); // The size of the Counts vector
75 M += ProfRecord.Counts.size() * sizeof(uint64_t);
76 M += sizeof(uint64_t); // The size of the Bitmap vector
77 M += ProfRecord.BitmapBytes.size() * sizeof(uint64_t);
78
79 // Value data
80 M += ValueProfData::getSize(ProfileData.second);
81 }
82 LE.write<offset_type>(M);
83
84 return std::make_pair(N, M);
85 }
86
88 Out.write(K.data(), N);
89 }
90
92 using namespace support;
93
95 for (const auto &ProfileData : *V) {
96 const InstrProfRecord &ProfRecord = ProfileData.second;
97 if (NamedInstrProfRecord::hasCSFlagInHash(ProfileData.first))
98 CSSummaryBuilder->addRecord(ProfRecord);
99 else
100 SummaryBuilder->addRecord(ProfRecord);
101
102 LE.write<uint64_t>(ProfileData.first); // Function hash
103 LE.write<uint64_t>(ProfRecord.Counts.size());
104 for (uint64_t I : ProfRecord.Counts)
105 LE.write<uint64_t>(I);
106
107 LE.write<uint64_t>(ProfRecord.BitmapBytes.size());
108 for (uint64_t I : ProfRecord.BitmapBytes)
109 LE.write<uint64_t>(I);
110
111 // Write value data
112 std::unique_ptr<ValueProfData> VDataPtr =
113 ValueProfData::serializeFrom(ProfileData.second);
114 uint32_t S = VDataPtr->getSize();
115 VDataPtr->swapBytesFromHost(ValueProfDataEndianness);
116 Out.write((const char *)VDataPtr.get(), S);
117 }
118 }
119};
120
121} // end namespace llvm
122
124 bool Sparse, uint64_t TemporalProfTraceReservoirSize,
125 uint64_t MaxTemporalProfTraceLength, bool WritePrevVersion,
126 memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema,
127 bool MemprofGenerateRandomHotness,
128 unsigned MemprofGenerateRandomHotnessSeed)
129 : Sparse(Sparse), MaxTemporalProfTraceLength(MaxTemporalProfTraceLength),
130 TemporalProfTraceReservoirSize(TemporalProfTraceReservoirSize),
131 InfoObj(new InstrProfRecordWriterTrait()),
132 WritePrevVersion(WritePrevVersion),
133 MemProfVersionRequested(MemProfVersionRequested),
134 MemProfFullSchema(MemProfFullSchema),
135 MemprofGenerateRandomHotness(MemprofGenerateRandomHotness) {
136 // Set up the random number seed if requested.
137 if (MemprofGenerateRandomHotness) {
138 unsigned seed = MemprofGenerateRandomHotnessSeed
139 ? MemprofGenerateRandomHotnessSeed
140 : std::time(nullptr);
141 errs() << "random hotness seed = " << seed << "\n";
142 std::srand(seed);
143 }
144}
145
147
148// Internal interface for testing purpose only.
150 InfoObj->ValueProfDataEndianness = Endianness;
151}
152
153void InstrProfWriter::setOutputSparse(bool Sparse) { this->Sparse = Sparse; }
154
156 function_ref<void(Error)> Warn) {
157 auto Name = I.Name;
158 auto Hash = I.Hash;
159 addRecord(Name, Hash, std::move(I), Weight, Warn);
160}
161
163 OverlapStats &Overlap,
164 OverlapStats &FuncLevelOverlap,
165 const OverlapFuncFilters &FuncFilter) {
166 auto Name = Other.Name;
167 auto Hash = Other.Hash;
168 Other.accumulateCounts(FuncLevelOverlap.Test);
169 auto It = FunctionData.find(Name);
170 if (It == FunctionData.end()) {
171 Overlap.addOneUnique(FuncLevelOverlap.Test);
172 return;
173 }
174 if (FuncLevelOverlap.Test.CountSum < 1.0f) {
175 Overlap.Overlap.NumEntries += 1;
176 return;
177 }
178 auto &ProfileDataMap = It->second;
179 auto [Where, NewFunc] = ProfileDataMap.try_emplace(Hash);
180 if (NewFunc) {
181 Overlap.addOneMismatch(FuncLevelOverlap.Test);
182 return;
183 }
184 InstrProfRecord &Dest = Where->second;
185
186 uint64_t ValueCutoff = FuncFilter.ValueCutoff;
187 if (!FuncFilter.NameFilter.empty() && Name.contains(FuncFilter.NameFilter))
188 ValueCutoff = 0;
189
190 Dest.overlap(Other, Overlap, FuncLevelOverlap, ValueCutoff);
191}
192
194 InstrProfRecord &&I, uint64_t Weight,
195 function_ref<void(Error)> Warn) {
196 auto &ProfileDataMap = FunctionData[Name];
197
198 auto [Where, NewFunc] = ProfileDataMap.try_emplace(Hash);
199 InstrProfRecord &Dest = Where->second;
200
201 auto MapWarn = [&](instrprof_error E) {
202 Warn(make_error<InstrProfError>(E));
203 };
204
205 if (NewFunc) {
206 // We've never seen a function with this name and hash, add it.
207 Dest = std::move(I);
208 if (Weight > 1)
209 Dest.scale(Weight, 1, MapWarn);
210 } else {
211 // We're updating a function we've seen before.
212 Dest.merge(I, Weight, MapWarn);
213 }
214
215 Dest.sortValueData();
216}
217
218void InstrProfWriter::addMemProfRecord(
220 auto NewRecord = Record;
221 // Provoke random hotness values if requested. We specify the lifetime access
222 // density and lifetime length that will result in a cold or not cold hotness.
223 // See the logic in getAllocType() in Analysis/MemoryProfileInfo.cpp.
224 if (MemprofGenerateRandomHotness) {
225 for (auto &Alloc : NewRecord.AllocSites) {
226 // To get a not cold context, set the lifetime access density to the
227 // maximum value and the lifetime to 0.
228 uint64_t NewTLAD = std::numeric_limits<uint64_t>::max();
229 uint64_t NewTL = 0;
230 bool IsCold = std::rand() % 2;
231 if (IsCold) {
232 // To get a cold context, set the lifetime access density to 0 and the
233 // lifetime to the maximum value.
234 NewTLAD = 0;
235 NewTL = std::numeric_limits<uint64_t>::max();
236 }
237 Alloc.Info.setTotalLifetimeAccessDensity(NewTLAD);
238 Alloc.Info.setTotalLifetime(NewTL);
239 }
240 }
241 MemProfSumBuilder.addRecord(NewRecord);
242 auto [Iter, Inserted] = MemProfData.Records.insert({Id, NewRecord});
243 // If we inserted a new record then we are done.
244 if (Inserted) {
245 return;
246 }
247 memprof::IndexedMemProfRecord &Existing = Iter->second;
248 Existing.merge(NewRecord);
249}
250
251bool InstrProfWriter::addMemProfFrame(const memprof::FrameId Id,
252 const memprof::Frame &Frame,
253 function_ref<void(Error)> Warn) {
254 auto [Iter, Inserted] = MemProfData.Frames.insert({Id, Frame});
255 // If a mapping already exists for the current frame id and it does not
256 // match the new mapping provided then reset the existing contents and bail
257 // out. We don't support the merging of memprof data whose Frame -> Id
258 // mapping across profiles is inconsistent.
259 if (!Inserted && Iter->second != Frame) {
260 Warn(make_error<InstrProfError>(instrprof_error::malformed,
261 "frame to id mapping mismatch"));
262 return false;
263 }
264 return true;
265}
266
267bool InstrProfWriter::addMemProfCallStack(
268 const memprof::CallStackId CSId,
270 function_ref<void(Error)> Warn) {
271 auto [Iter, Inserted] = MemProfData.CallStacks.insert({CSId, CallStack});
272 // If a mapping already exists for the current call stack id and it does not
273 // match the new mapping provided then reset the existing contents and bail
274 // out. We don't support the merging of memprof data whose CallStack -> Id
275 // mapping across profiles is inconsistent.
276 if (!Inserted && Iter->second != CallStack) {
277 Warn(make_error<InstrProfError>(instrprof_error::malformed,
278 "call stack to id mapping mismatch"));
279 return false;
280 }
281 return true;
282}
283
285 function_ref<void(Error)> Warn) {
286 // Return immediately if everything is empty.
287 if (Incoming.Frames.empty() && Incoming.CallStacks.empty() &&
288 Incoming.Records.empty())
289 return true;
290
291 // Otherwise, every component must be non-empty.
292 assert(!Incoming.Frames.empty() && !Incoming.CallStacks.empty() &&
293 !Incoming.Records.empty());
294
295 if (MemProfData.Frames.empty())
296 MemProfData.Frames = std::move(Incoming.Frames);
297 else
298 for (const auto &[Id, F] : Incoming.Frames)
299 if (addMemProfFrame(Id, F, Warn))
300 return false;
301
302 if (MemProfData.CallStacks.empty())
303 MemProfData.CallStacks = std::move(Incoming.CallStacks);
304 else
305 for (const auto &[CSId, CS] : Incoming.CallStacks)
306 if (addMemProfCallStack(CSId, CS, Warn))
307 return false;
308
309 // Add one record at a time if randomization is requested.
310 if (MemProfData.Records.empty() && !MemprofGenerateRandomHotness) {
311 // Need to manually add each record to the builder, which is otherwise done
312 // in addMemProfRecord.
313 for (const auto &[GUID, Record] : Incoming.Records)
314 MemProfSumBuilder.addRecord(Record);
315 MemProfData.Records = std::move(Incoming.Records);
316 } else {
317 for (const auto &[GUID, Record] : Incoming.Records)
318 addMemProfRecord(GUID, Record);
319 }
320
321 return true;
322}
323
325 llvm::append_range(BinaryIds, BIs);
326}
327
329 std::unique_ptr<memprof::DataAccessProfData> DataAccessProfDataIn) {
330 DataAccessProfileData = std::move(DataAccessProfDataIn);
331}
332
334 SmallVectorImpl<TemporalProfTraceTy> &SrcTraces, uint64_t SrcStreamSize) {
335 if (TemporalProfTraces.size() > TemporalProfTraceReservoirSize)
336 TemporalProfTraces.truncate(TemporalProfTraceReservoirSize);
337 for (auto &Trace : SrcTraces)
338 if (Trace.FunctionNameRefs.size() > MaxTemporalProfTraceLength)
339 Trace.FunctionNameRefs.resize(MaxTemporalProfTraceLength);
340 llvm::erase_if(SrcTraces, [](auto &T) { return T.FunctionNameRefs.empty(); });
341 // If there are no source traces, it is probably because
342 // --temporal-profile-max-trace-length=0 was set to deliberately remove all
343 // traces. In that case, we do not want to increase the stream size
344 if (SrcTraces.empty())
345 return;
346 // Add traces until our reservoir is full or we run out of source traces
347 auto SrcTraceIt = SrcTraces.begin();
348 while (TemporalProfTraces.size() < TemporalProfTraceReservoirSize &&
349 SrcTraceIt < SrcTraces.end())
350 TemporalProfTraces.push_back(*SrcTraceIt++);
351 // Our reservoir is full, we need to sample the source stream
352 llvm::shuffle(SrcTraceIt, SrcTraces.end(), RNG);
353 for (uint64_t I = TemporalProfTraces.size();
354 I < SrcStreamSize && SrcTraceIt < SrcTraces.end(); I++) {
355 std::uniform_int_distribution<uint64_t> Distribution(0, I);
356 uint64_t RandomIndex = Distribution(RNG);
357 if (RandomIndex < TemporalProfTraces.size())
358 TemporalProfTraces[RandomIndex] = *SrcTraceIt++;
359 }
360 TemporalProfTraceStreamSize += SrcStreamSize;
361}
362
364 function_ref<void(Error)> Warn) {
365 for (auto &I : IPW.FunctionData)
366 for (auto &Func : I.getValue())
367 addRecord(I.getKey(), Func.first, std::move(Func.second), 1, Warn);
368
369 BinaryIds.reserve(BinaryIds.size() + IPW.BinaryIds.size());
370 for (auto &I : IPW.BinaryIds)
372
373 addTemporalProfileTraces(IPW.TemporalProfTraces,
374 IPW.TemporalProfTraceStreamSize);
375
376 MemProfData.Frames.reserve(IPW.MemProfData.Frames.size());
377 for (auto &[FrameId, Frame] : IPW.MemProfData.Frames) {
378 // If we weren't able to add the frame mappings then it doesn't make sense
379 // to try to merge the records from this profile.
380 if (!addMemProfFrame(FrameId, Frame, Warn))
381 return;
382 }
383
384 MemProfData.CallStacks.reserve(IPW.MemProfData.CallStacks.size());
385 for (auto &[CSId, CallStack] : IPW.MemProfData.CallStacks) {
386 if (!addMemProfCallStack(CSId, CallStack, Warn))
387 return;
388 }
389
390 MemProfData.Records.reserve(IPW.MemProfData.Records.size());
391 for (auto &[GUID, Record] : IPW.MemProfData.Records) {
392 addMemProfRecord(GUID, Record);
393 }
394}
395
396bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
397 if (!Sparse)
398 return true;
399 for (const auto &Func : PD) {
400 const InstrProfRecord &IPR = Func.second;
401 if (llvm::any_of(IPR.Counts, [](uint64_t Count) { return Count > 0; }))
402 return true;
403 if (llvm::any_of(IPR.BitmapBytes, [](uint8_t Byte) { return Byte > 0; }))
404 return true;
405 }
406 return false;
407}
408
409static void setSummary(IndexedInstrProf::Summary *TheSummary,
410 ProfileSummary &PS) {
411 using namespace IndexedInstrProf;
412
413 const std::vector<ProfileSummaryEntry> &Res = PS.getDetailedSummary();
414 TheSummary->NumSummaryFields = Summary::NumKinds;
415 TheSummary->NumCutoffEntries = Res.size();
416 TheSummary->set(Summary::MaxFunctionCount, PS.getMaxFunctionCount());
417 TheSummary->set(Summary::MaxBlockCount, PS.getMaxCount());
418 TheSummary->set(Summary::MaxInternalBlockCount, PS.getMaxInternalCount());
419 TheSummary->set(Summary::TotalBlockCount, PS.getTotalCount());
420 TheSummary->set(Summary::TotalNumBlocks, PS.getNumCounts());
421 TheSummary->set(Summary::TotalNumFunctions, PS.getNumFunctions());
422 for (unsigned I = 0; I < Res.size(); I++)
423 TheSummary->setEntry(I, Res[I]);
424}
425
426uint64_t InstrProfWriter::writeHeader(const IndexedInstrProf::Header &Header,
427 const bool WritePrevVersion,
428 ProfOStream &OS) {
429 // Only write out the first four fields.
430 for (int I = 0; I < 4; I++)
431 OS.write(reinterpret_cast<const uint64_t *>(&Header)[I]);
432
433 // Remember the offset of the remaining fields to allow back patching later.
434 auto BackPatchStartOffset = OS.tell();
435
436 // Reserve the space for back patching later.
437 OS.write(0); // HashOffset
438 OS.write(0); // MemProfOffset
439 OS.write(0); // BinaryIdOffset
440 OS.write(0); // TemporalProfTracesOffset
441 if (!WritePrevVersion)
442 OS.write(0); // VTableNamesOffset
443
444 return BackPatchStartOffset;
445}
446
447Error InstrProfWriter::writeBinaryIds(ProfOStream &OS) {
448 // BinaryIdSection has two parts:
449 // 1. uint64_t BinaryIdsSectionSize
450 // 2. list of binary ids that consist of:
451 // a. uint64_t BinaryIdLength
452 // b. uint8_t BinaryIdData
453 // c. uint8_t Padding (if necessary)
454 // Calculate size of binary section.
455 uint64_t BinaryIdsSectionSize = 0;
456
457 // Remove duplicate binary ids.
458 llvm::sort(BinaryIds);
459 BinaryIds.erase(llvm::unique(BinaryIds), BinaryIds.end());
460
461 for (const auto &BI : BinaryIds) {
462 // Increment by binary id length data type size.
463 BinaryIdsSectionSize += sizeof(uint64_t);
464 // Increment by binary id data length, aligned to 8 bytes.
465 BinaryIdsSectionSize += alignToPowerOf2(BI.size(), sizeof(uint64_t));
466 }
467 // Write binary ids section size.
468 OS.write(BinaryIdsSectionSize);
469
470 for (const auto &BI : BinaryIds) {
471 uint64_t BILen = BI.size();
472 // Write binary id length.
473 OS.write(BILen);
474 // Write binary id data.
475 for (unsigned K = 0; K < BILen; K++)
476 OS.writeByte(BI[K]);
477 // Write padding if necessary.
478 uint64_t PaddingSize = alignToPowerOf2(BILen, sizeof(uint64_t)) - BILen;
479 for (unsigned K = 0; K < PaddingSize; K++)
480 OS.writeByte(0);
481 }
482
483 return Error::success();
484}
485
486Error InstrProfWriter::writeVTableNames(ProfOStream &OS) {
487 std::vector<std::string> VTableNameStrs;
488 for (StringRef VTableName : VTableNames.keys())
489 VTableNameStrs.push_back(VTableName.str());
490
491 std::string CompressedVTableNames;
492 if (!VTableNameStrs.empty())
494 VTableNameStrs, compression::zlib::isAvailable(),
495 CompressedVTableNames))
496 return E;
497
498 const uint64_t CompressedStringLen = CompressedVTableNames.length();
499
500 // Record the length of compressed string.
501 OS.write(CompressedStringLen);
502
503 // Write the chars in compressed strings.
504 for (auto &c : CompressedVTableNames)
505 OS.writeByte(static_cast<uint8_t>(c));
506
507 // Pad up to a multiple of 8.
508 // InstrProfReader could read bytes according to 'CompressedStringLen'.
509 const uint64_t PaddedLength = alignTo(CompressedStringLen, 8);
510
511 for (uint64_t K = CompressedStringLen; K < PaddedLength; K++)
512 OS.writeByte(0);
513
514 return Error::success();
515}
516
517Error InstrProfWriter::writeImpl(ProfOStream &OS) {
518 using namespace IndexedInstrProf;
519 using namespace support;
520
522
524 InfoObj->SummaryBuilder = &ISB;
526 InfoObj->CSSummaryBuilder = &CSISB;
527
528 // Populate the hash table generator.
530 for (const auto &I : FunctionData)
531 if (shouldEncodeData(I.getValue()))
532 OrderedData.emplace_back((I.getKey()), &I.getValue());
533 llvm::sort(OrderedData, less_first());
534 for (const auto &I : OrderedData)
535 Generator.insert(I.first, I.second);
536
537 // Write the header.
539 Header.Version = WritePrevVersion
542 // The WritePrevVersion handling will either need to be removed or updated
543 // if the version is advanced beyond 12.
546 if (static_cast<bool>(ProfileKind & InstrProfKind::IRInstrumentation))
547 Header.Version |= VARIANT_MASK_IR_PROF;
548 if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive))
549 Header.Version |= VARIANT_MASK_CSIR_PROF;
550 if (static_cast<bool>(ProfileKind &
552 Header.Version |= VARIANT_MASK_INSTR_ENTRY;
553 if (static_cast<bool>(ProfileKind &
555 Header.Version |= VARIANT_MASK_INSTR_LOOP_ENTRIES;
556 if (static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage))
557 Header.Version |= VARIANT_MASK_BYTE_COVERAGE;
558 if (static_cast<bool>(ProfileKind & InstrProfKind::FunctionEntryOnly))
559 Header.Version |= VARIANT_MASK_FUNCTION_ENTRY_ONLY;
560 if (static_cast<bool>(ProfileKind & InstrProfKind::MemProf))
561 Header.Version |= VARIANT_MASK_MEMPROF;
562 if (static_cast<bool>(ProfileKind & InstrProfKind::TemporalProfile))
563 Header.Version |= VARIANT_MASK_TEMPORAL_PROF;
564
565 const uint64_t BackPatchStartOffset =
566 writeHeader(Header, WritePrevVersion, OS);
567
568 // Reserve space to write profile summary data.
570 uint32_t SummarySize = Summary::getSize(Summary::NumKinds, NumEntries);
571 // Remember the summary offset.
572 uint64_t SummaryOffset = OS.tell();
573 for (unsigned I = 0; I < SummarySize / sizeof(uint64_t); I++)
574 OS.write(0);
575 uint64_t CSSummaryOffset = 0;
576 uint64_t CSSummarySize = 0;
577 if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive)) {
578 CSSummaryOffset = OS.tell();
579 CSSummarySize = SummarySize / sizeof(uint64_t);
580 for (unsigned I = 0; I < CSSummarySize; I++)
581 OS.write(0);
582 }
583
584 // Write the hash table.
585 uint64_t HashTableStart = Generator.Emit(OS.OS, *InfoObj);
586
587 // Write the MemProf profile data if we have it.
588 uint64_t MemProfSectionStart = 0;
589 if (static_cast<bool>(ProfileKind & InstrProfKind::MemProf)) {
590 MemProfSectionStart = OS.tell();
591
592 if (auto E = writeMemProf(
593 OS, MemProfData, MemProfVersionRequested, MemProfFullSchema,
594 std::move(DataAccessProfileData), MemProfSumBuilder.getSummary()))
595 return E;
596 }
597
598 uint64_t BinaryIdSectionStart = OS.tell();
599 if (auto E = writeBinaryIds(OS))
600 return E;
601
602 uint64_t VTableNamesSectionStart = OS.tell();
603
604 if (!WritePrevVersion)
605 if (Error E = writeVTableNames(OS))
606 return E;
607
608 uint64_t TemporalProfTracesSectionStart = 0;
609 if (static_cast<bool>(ProfileKind & InstrProfKind::TemporalProfile)) {
610 TemporalProfTracesSectionStart = OS.tell();
611 OS.write(TemporalProfTraces.size());
612 OS.write(TemporalProfTraceStreamSize);
613 for (auto &Trace : TemporalProfTraces) {
614 OS.write(Trace.Weight);
615 OS.write(Trace.FunctionNameRefs.size());
616 for (auto &NameRef : Trace.FunctionNameRefs)
617 OS.write(NameRef);
618 }
619 }
620
621 // Allocate space for data to be serialized out.
622 std::unique_ptr<IndexedInstrProf::Summary> TheSummary =
624 // Compute the Summary and copy the data to the data
625 // structure to be serialized out (to disk or buffer).
626 std::unique_ptr<ProfileSummary> PS = ISB.getSummary();
627 setSummary(TheSummary.get(), *PS);
628 InfoObj->SummaryBuilder = nullptr;
629
630 // For Context Sensitive summary.
631 std::unique_ptr<IndexedInstrProf::Summary> TheCSSummary = nullptr;
632 if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive)) {
633 TheCSSummary = IndexedInstrProf::allocSummary(SummarySize);
634 std::unique_ptr<ProfileSummary> CSPS = CSISB.getSummary();
635 setSummary(TheCSSummary.get(), *CSPS);
636 }
637 InfoObj->CSSummaryBuilder = nullptr;
638
639 SmallVector<uint64_t, 8> HeaderOffsets = {HashTableStart, MemProfSectionStart,
640 BinaryIdSectionStart,
641 TemporalProfTracesSectionStart};
642 if (!WritePrevVersion)
643 HeaderOffsets.push_back(VTableNamesSectionStart);
644
645 PatchItem PatchItems[] = {
646 // Patch the Header fields
647 {BackPatchStartOffset, HeaderOffsets},
648 // Patch the summary data.
649 {SummaryOffset,
650 ArrayRef<uint64_t>(reinterpret_cast<uint64_t *>(TheSummary.get()),
651 SummarySize / sizeof(uint64_t))},
652 {CSSummaryOffset,
653 ArrayRef<uint64_t>(reinterpret_cast<uint64_t *>(TheCSSummary.get()),
654 CSSummarySize)}};
655
656 OS.patch(PatchItems);
657
658 for (const auto &I : FunctionData)
659 for (const auto &F : I.getValue())
660 if (Error E = validateRecord(F.second))
661 return E;
662
663 return Error::success();
664}
665
667 // Write the hash table.
668 ProfOStream POS(OS);
669 return writeImpl(POS);
670}
671
673 ProfOStream POS(OS);
674 return writeImpl(POS);
675}
676
677std::unique_ptr<MemoryBuffer> InstrProfWriter::writeBuffer() {
678 std::string Data;
680 // Write the hash table.
681 if (Error E = write(OS))
682 return nullptr;
683 // Return this in an aligned memory buffer.
685}
686
687static const char *ValueProfKindStr[] = {
688#define VALUE_PROF_KIND(Enumerator, Value, Descr) #Enumerator,
690};
691
693 for (uint32_t VK = 0; VK <= IPVK_Last; VK++) {
694 if (VK == IPVK_IndirectCallTarget || VK == IPVK_VTableTarget)
695 continue;
696 uint32_t NS = Func.getNumValueSites(VK);
697 for (uint32_t S = 0; S < NS; S++) {
698 DenseSet<uint64_t> SeenValues;
699 for (const auto &V : Func.getValueArrayForSite(VK, S))
700 if (!SeenValues.insert(V.Value).second)
701 return make_error<InstrProfError>(instrprof_error::invalid_prof);
702 }
703 }
704
705 return Error::success();
706}
707
709 const InstrProfRecord &Func,
710 InstrProfSymtab &Symtab,
712 OS << Name << "\n";
713 OS << "# Func Hash:\n" << Hash << "\n";
714 OS << "# Num Counters:\n" << Func.Counts.size() << "\n";
715 OS << "# Counter Values:\n";
716 for (uint64_t Count : Func.Counts)
717 OS << Count << "\n";
718
719 if (Func.BitmapBytes.size() > 0) {
720 OS << "# Num Bitmap Bytes:\n$" << Func.BitmapBytes.size() << "\n";
721 OS << "# Bitmap Byte Values:\n";
722 for (uint8_t Byte : Func.BitmapBytes) {
723 OS << "0x";
724 OS.write_hex(Byte);
725 OS << "\n";
726 }
727 OS << "\n";
728 }
729
730 uint32_t NumValueKinds = Func.getNumValueKinds();
731 if (!NumValueKinds) {
732 OS << "\n";
733 return;
734 }
735
736 OS << "# Num Value Kinds:\n" << Func.getNumValueKinds() << "\n";
737 for (uint32_t VK = 0; VK < IPVK_Last + 1; VK++) {
738 uint32_t NS = Func.getNumValueSites(VK);
739 if (!NS)
740 continue;
741 OS << "# ValueKind = " << ValueProfKindStr[VK] << ":\n" << VK << "\n";
742 OS << "# NumValueSites:\n" << NS << "\n";
743 for (uint32_t S = 0; S < NS; S++) {
744 auto VD = Func.getValueArrayForSite(VK, S);
745 OS << VD.size() << "\n";
746 for (const auto &V : VD) {
747 if (VK == IPVK_IndirectCallTarget || VK == IPVK_VTableTarget)
748 OS << Symtab.getFuncOrVarNameIfDefined(V.Value) << ":" << V.Count
749 << "\n";
750 else
751 OS << V.Value << ":" << V.Count << "\n";
752 }
753 }
754 }
755
756 OS << "\n";
757}
758
760 // Check CS first since it implies an IR level profile.
761 if (static_cast<bool>(ProfileKind & InstrProfKind::ContextSensitive))
762 OS << "# CSIR level Instrumentation Flag\n:csir\n";
763 else if (static_cast<bool>(ProfileKind & InstrProfKind::IRInstrumentation))
764 OS << "# IR level Instrumentation Flag\n:ir\n";
765
766 if (static_cast<bool>(ProfileKind &
768 OS << "# Always instrument the function entry block\n:entry_first\n";
769 if (static_cast<bool>(ProfileKind &
771 OS << "# Always instrument the loop entry "
772 "blocks\n:instrument_loop_entries\n";
773 if (static_cast<bool>(ProfileKind & InstrProfKind::SingleByteCoverage))
774 OS << "# Instrument block coverage\n:single_byte_coverage\n";
775 InstrProfSymtab Symtab;
776
778 using RecordType = std::pair<StringRef, FuncPair>;
779 SmallVector<RecordType, 4> OrderedFuncData;
780
781 for (const auto &I : FunctionData) {
782 if (shouldEncodeData(I.getValue())) {
783 if (Error E = Symtab.addFuncName(I.getKey()))
784 return E;
785 for (const auto &Func : I.getValue())
786 OrderedFuncData.push_back(std::make_pair(I.getKey(), Func));
787 }
788 }
789
790 for (const auto &VTableName : VTableNames)
791 if (Error E = Symtab.addVTableName(VTableName.getKey()))
792 return E;
793
794 if (static_cast<bool>(ProfileKind & InstrProfKind::TemporalProfile))
796
797 llvm::sort(OrderedFuncData, [](const RecordType &A, const RecordType &B) {
798 return std::tie(A.first, A.second.first) <
799 std::tie(B.first, B.second.first);
800 });
801
802 for (const auto &record : OrderedFuncData) {
803 const StringRef &Name = record.first;
804 const FuncPair &Func = record.second;
805 writeRecordInText(Name, Func.first, Func.second, Symtab, OS);
806 }
807
808 for (const auto &record : OrderedFuncData) {
809 const FuncPair &Func = record.second;
810 if (Error E = validateRecord(Func.second))
811 return E;
812 }
813
814 return Error::success();
815}
816
818 InstrProfSymtab &Symtab) {
819 OS << ":temporal_prof_traces\n";
820 OS << "# Num Temporal Profile Traces:\n" << TemporalProfTraces.size() << "\n";
821 OS << "# Temporal Profile Trace Stream Size:\n"
822 << TemporalProfTraceStreamSize << "\n";
823 for (auto &Trace : TemporalProfTraces) {
824 OS << "# Weight:\n" << Trace.Weight << "\n";
825 for (auto &NameRef : Trace.FunctionNameRefs)
826 OS << Symtab.getFuncOrVarName(NameRef) << ",";
827 OS << "\n";
828 }
829 OS << "\n";
830}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
std::string Name
static void setSummary(IndexedInstrProf::Summary *TheSummary, ProfileSummary &PS)
static const char * ValueProfKindStr[]
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
Defines facilities for reading and writing on-disk hash tables.
if(PassOpts->AAPipeline)
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:147
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
static ErrorSuccess success()
Create a success value.
Definition: Error.h:336
static std::pair< offset_type, offset_type > EmitKeyDataLength(raw_ostream &Out, key_type_ref K, data_type_ref V)
const InstrProfWriter::ProfilingData *const data_type_ref
InstrProfSummaryBuilder * SummaryBuilder
static hash_value_type ComputeHash(key_type_ref K)
InstrProfSummaryBuilder * CSSummaryBuilder
void EmitKey(raw_ostream &Out, key_type_ref K, offset_type N)
void EmitData(raw_ostream &Out, key_type_ref, data_type_ref V, offset_type)
const InstrProfWriter::ProfilingData *const data_type
LLVM_ABI void addRecord(const InstrProfRecord &)
A symbol table used for function [IR]PGO name look-up with keys (such as pointers,...
Definition: InstrProf.h:506
StringRef getFuncOrVarName(uint64_t ValMD5Hash) const
Return name of functions or global variables from the name's md5 hash value.
Definition: InstrProf.h:778
StringRef getFuncOrVarNameIfDefined(uint64_t ValMD5Hash) const
Just like getFuncOrVarName, except that it will return literal string 'External Symbol' if the functi...
Definition: InstrProf.h:771
Error addVTableName(StringRef VTableName)
Adds VTableName as a known symbol, and inserts it to a map that tracks all vtable names.
Definition: InstrProf.h:658
Error addFuncName(StringRef FuncName)
The method name is kept since there are many callers.
Definition: InstrProf.h:654
LLVM_ABI InstrProfWriter(bool Sparse=false, uint64_t TemporalProfTraceReservoirSize=0, uint64_t MaxTemporalProfTraceLength=0, bool WritePrevVersion=false, memprof::IndexedVersion MemProfVersionRequested=static_cast< memprof::IndexedVersion >(memprof::MinimumSupportedVersion), bool MemProfFullSchema=false, bool MemprofGenerateRandomHotness=false, unsigned MemprofGenerateRandomHotnessSeed=0)
LLVM_ABI Error write(raw_fd_ostream &OS)
Write the profile to OS.
LLVM_ABI void addTemporalProfileTraces(SmallVectorImpl< TemporalProfTraceTy > &SrcTraces, uint64_t SrcStreamSize)
Add SrcTraces using reservoir sampling where SrcStreamSize is the total number of temporal profiling ...
LLVM_ABI void overlapRecord(NamedInstrProfRecord &&Other, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap, const OverlapFuncFilters &FuncFilter)
LLVM_ABI Error writeText(raw_fd_ostream &OS)
Write the profile in text format to OS.
LLVM_ABI void addBinaryIds(ArrayRef< llvm::object::BuildID > BIs)
static LLVM_ABI void writeRecordInText(StringRef Name, uint64_t Hash, const InstrProfRecord &Counters, InstrProfSymtab &Symtab, raw_fd_ostream &OS)
Write Record in text format to OS.
LLVM_ABI void setValueProfDataEndianness(llvm::endianness Endianness)
LLVM_ABI void addRecord(NamedInstrProfRecord &&I, uint64_t Weight, function_ref< void(Error)> Warn)
Add function counts for the given function.
LLVM_ABI void mergeRecordsFromWriter(InstrProfWriter &&IPW, function_ref< void(Error)> Warn)
Merge existing function counts from the given writer.
LLVM_ABI void writeTextTemporalProfTraceData(raw_fd_ostream &OS, InstrProfSymtab &Symtab)
Write temporal profile trace data to the header in text format to OS.
LLVM_ABI std::unique_ptr< MemoryBuffer > writeBuffer()
Write the profile, returning the raw data. For testing.
LLVM_ABI void setOutputSparse(bool Sparse)
LLVM_ABI bool addMemProfData(memprof::IndexedMemProfData Incoming, function_ref< void(Error)> Warn)
Add the entire MemProfData Incoming to the writer context.
LLVM_ABI void addDataAccessProfData(std::unique_ptr< memprof::DataAccessProfData > DataAccessProfile)
LLVM_ABI Error validateRecord(const InstrProfRecord &Func)
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
Generates an on disk hash table.
offset_type Emit(raw_ostream &Out)
Emit the table to Out, which must not be at offset 0.
static LLVM_ABI const ArrayRef< uint32_t > DefaultCutoffs
A vector of useful cutoff values for detailed summary.
Definition: ProfileCommon.h:71
uint64_t getTotalCount() const
uint64_t getMaxCount() const
const SummaryEntryVector & getDetailedSummary()
uint32_t getNumCounts() const
uint64_t getMaxInternalCount() const
uint64_t getMaxFunctionCount() const
uint32_t getNumFunctions() const
bool empty() const
Definition: SmallVector.h:82
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:938
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
iterator_range< StringMapKeyIterator< ValueTy > > keys() const
Definition: StringMap.h:232
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
unsigned size() const
Definition: Trace.h:95
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:194
An efficient, type-erasing, non-owning reference to a callable.
LLVM_ABI std::unique_ptr< MemProfSummary > getSummary()
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:461
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:148
raw_ostream & write_hex(unsigned long long N)
Output N in hexadecimal, without any prefix or padding.
raw_ostream & write(unsigned char C)
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:662
std::unique_ptr< Summary > allocSummary(uint32_t TotalSize)
Definition: InstrProf.h:1303
uint64_t ComputeHash(StringRef K)
Definition: InstrProf.h:1184
LLVM_ABI bool isAvailable()
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
void shuffle(Iterator first, Iterator last, RNG &&g)
Definition: STLExtras.h:1563
auto unique(Range &&R, Predicate P)
Definition: STLExtras.h:2095
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1751
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1669
constexpr T alignToPowerOf2(U Value, V Align)
Will overflow only if result is not representable in T.
Definition: MathExtras.h:498
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
@ Other
Any other memory.
instrprof_error
Definition: InstrProf.h:399
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
LLVM_ABI Error collectGlobalObjectNameStrings(ArrayRef< std::string > NameStrs, bool doCompression, std::string &Result)
Given a vector of strings (names of global objects like functions or, virtual tables) NameStrs,...
Definition: InstrProf.cpp:714
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:2139
LLVM_ABI Error writeMemProf(ProfOStream &OS, memprof::IndexedMemProfData &MemProfData, memprof::IndexedVersion MemProfVersionRequested, bool MemProfFullSchema, std::unique_ptr< memprof::DataAccessProfData > DataAccessProfileData, std::unique_ptr< memprof::MemProfSummary > MemProfSum)
endianness
Definition: bit.h:71
#define N
Helper object to track which of three possible relocation mechanisms are used for a particular value ...
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
void set(SummaryFieldKind K, uint64_t V)
Definition: InstrProf.h:1289
void setEntry(uint32_t I, const ProfileSummaryEntry &E)
Definition: InstrProf.h:1295
Profiling information for a single function.
Definition: InstrProf.h:895
std::vector< uint64_t > Counts
Definition: InstrProf.h:896
LLVM_ABI void merge(InstrProfRecord &Other, uint64_t Weight, function_ref< void(instrprof_error)> Warn)
Merge the counts in Other into this one.
Definition: InstrProf.cpp:957
LLVM_ABI void overlap(InstrProfRecord &Other, OverlapStats &Overlap, OverlapStats &FuncLevelOverlap, uint64_t ValueCutoff)
Compute the overlap b/w this IntrprofRecord and Other.
Definition: InstrProf.cpp:846
void sortValueData()
Sort value profile data (per site) by count.
Definition: InstrProf.h:958
std::vector< uint8_t > BitmapBytes
Definition: InstrProf.h:897
LLVM_ABI void scale(uint64_t N, uint64_t D, function_ref< void(instrprof_error)> Warn)
Scale up profile counts (including value profile data) by a factor of (N / D).
Definition: InstrProf.cpp:1020
static bool hasCSFlagInHash(uint64_t FuncHash)
Definition: InstrProf.h:1074
const std::string NameFilter
Definition: InstrProf.h:860
LLVM_ABI void addOneMismatch(const CountSumOrPercent &MismatchFunc)
Definition: InstrProf.cpp:1584
CountSumOrPercent Overlap
Definition: InstrProf.h:824
LLVM_ABI void addOneUnique(const CountSumOrPercent &UniqueFunc)
Definition: InstrProf.cpp:1594
CountSumOrPercent Test
Definition: InstrProf.h:822
Function object to check whether the first component of a container supported by std::get (like std::...
Definition: STLExtras.h:1472
llvm::MapVector< CallStackId, llvm::SmallVector< FrameId > > CallStacks
llvm::MapVector< GlobalValue::GUID, IndexedMemProfRecord > Records
llvm::MapVector< FrameId, Frame > Frames
void merge(const IndexedMemProfRecord &Other)
Definition: MemProf.h:451
Adapter to write values to a stream in a particular byte order.
Definition: EndianStream.h:67