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}
29
31 // Write the current number of fields first, which helps enable backwards and
32 // forwards compatibility (see comment in header).
34 auto StartPos = OS.tell();
35 (void)StartPos;
36 OS.write(NumContexts);
37 OS.write(NumColdContexts);
38 OS.write(NumHotContexts);
39 OS.write(MaxColdTotalSize);
40 OS.write(MaxWarmTotalSize);
41 OS.write(MaxHotTotalSize);
42 // Sanity check that the number of fields was kept in sync with actual fields.
43 assert((OS.tell() - StartPos) / 8 == MemProfSummary::getNumSummaryFields());
44}
45
46std::unique_ptr<MemProfSummary>
47MemProfSummary::deserialize(const unsigned char *&Ptr) {
48 auto NumSummaryFields =
50 // The initial version of the summary contains 6 fields. To support backwards
51 // compatibility with older profiles, if new summary fields are added (until a
52 // version bump) this code will need to check NumSummaryFields against the
53 // current value of MemProfSummary::getNumSummaryFields(). If NumSummaryFields
54 // is lower then default values will need to be filled in for the newer fields
55 // instead of trying to read them from the profile.
56 //
57 // For now, assert that the profile contains at least as many fields as
58 // expected by the code.
59 assert(NumSummaryFields >= MemProfSummary::getNumSummaryFields());
60
61 auto MemProfSum = std::make_unique<MemProfSummary>(
68
69 // Enable forwards compatibility by skipping past any additional fields in the
70 // profile's summary.
71 Ptr += NumSummaryFields * sizeof(uint64_t);
72
73 return MemProfSum;
74}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
LLVM_ABI uint64_t tell() const
LLVM_ABI void write32(uint32_t V)
LLVM_ABI void write(uint64_t V)
LLVM_ABI void printSummaryYaml(raw_ostream &OS) const
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:58
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:77
This is an optimization pass for GlobalISel generic memory operations.