LLVM 22.0.0git
MemProfSummary.cpp
Go to the documentation of this file.
1//=-- MemProfSummary.cpp - MemProf summary support ---------------=//
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 MemProf summary support.
10//
11//===----------------------------------------------------------------------===//
12
14
15using namespace llvm;
16using namespace llvm::memprof;
17
19 // For now emit as YAML comments, since they aren't read on input.
20 OS << "---\n";
21 OS << "# MemProfSummary:\n";
22 OS << "# Total contexts: " << NumContexts << "\n";
23 OS << "# Total cold contexts: " << NumColdContexts << "\n";
24 OS << "# Total hot contexts: " << NumHotContexts << "\n";
25 OS << "# Maximum cold context total size: " << MaxColdTotalSize << "\n";
26 OS << "# Maximum warm context total size: " << MaxWarmTotalSize << "\n";
27 OS << "# Maximum hot context total size: " << MaxHotTotalSize << "\n";
28 if (HasDataAccessProfile) {
29 OS << "# Num hot symbols and string literals: "
30 << NumHotSymbolsAndStringLiterals << "\n";
31 OS << "# Num known cold symbols: " << NumKnownColdSymbols << "\n";
32 OS << "# Num known cold string literals: " << NumKnownColdStringLiterals
33 << "\n";
34 }
35}
36
38 // Write the current number of fields first, which helps enable backwards and
39 // forwards compatibility (see comment in header).
41 auto StartPos = OS.tell();
42 (void)StartPos;
43 OS.write(NumContexts);
44 OS.write(NumColdContexts);
45 OS.write(NumHotContexts);
46 OS.write(MaxColdTotalSize);
47 OS.write(MaxWarmTotalSize);
48 OS.write(MaxHotTotalSize);
49 // Sanity check that the number of fields was kept in sync with actual fields.
50 assert((OS.tell() - StartPos) / 8 == MemProfSummary::getNumSummaryFields());
51}
52
53std::unique_ptr<MemProfSummary>
54MemProfSummary::deserialize(const unsigned char *&Ptr) {
55 auto NumSummaryFields =
57 // The initial version of the summary contains 6 fields. To support backwards
58 // compatibility with older profiles, if new summary fields are added (until a
59 // version bump) this code will need to check NumSummaryFields against the
60 // current value of MemProfSummary::getNumSummaryFields(). If NumSummaryFields
61 // is lower then default values will need to be filled in for the newer fields
62 // instead of trying to read them from the profile.
63 //
64 // For now, assert that the profile contains at least as many fields as
65 // expected by the code.
66 assert(NumSummaryFields >= MemProfSummary::getNumSummaryFields());
67
68 auto MemProfSum = std::make_unique<MemProfSummary>(
75
76 // Enable forwards compatibility by skipping past any additional fields in the
77 // profile's summary.
78 Ptr += NumSummaryFields * sizeof(uint64_t);
79
80 return MemProfSum;
81}
82
83// FIXME: Consider to serialize the data access summary fields, ideally
84// batch this together with more substantial profile format change
85// and bump version once.
87 const DataAccessProfData &DataAccessProfile) {
88 HasDataAccessProfile = true;
89 NumHotSymbolsAndStringLiterals = DataAccessProfile.getRecords().size();
90 NumKnownColdSymbols = DataAccessProfile.getKnownColdSymbols().size();
91 NumKnownColdStringLiterals = DataAccessProfile.getKnownColdHashes().size();
92}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
LLVM_ABI uint64_t tell() const
LLVM_ABI void write32(uint32_t V)
LLVM_ABI void write(uint64_t V)
Encapsulates the data access profile data and the methods to operate on it.
ArrayRef< MapVector< SymbolHandleRef, internal::DataAccessProfRecordRef >::value_type > getRecords() const
ArrayRef< StringRef > getKnownColdSymbols() const
ArrayRef< uint64_t > getKnownColdHashes() const
LLVM_ABI void printSummaryYaml(raw_ostream &OS) const
LLVM_ABI void buildDataAccessSummary(const DataAccessProfData &DataAccessProfile)
Build data access profile summary from DataAccessProfile.
LLVM_ABI void write(ProfOStream &OS) const
Write to indexed MemProf profile.
static LLVM_ABI std::unique_ptr< MemProfSummary > deserialize(const unsigned char *&)
Read from indexed MemProf profile.
static constexpr unsigned getNumSummaryFields()
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
value_type read(const void *memory, endianness endian)
Read a value of a particular endianness from memory.
Definition Endian.h:60
value_type readNext(const CharT *&memory, endianness endian)
Read a value of a particular endianness from a buffer, and increment the buffer past that value.
Definition Endian.h:81
This is an optimization pass for GlobalISel generic memory operations.