LLVM 24.0.0git
BBAddrMapYAML.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file defines the YAMLIO mappings for the format-agnostic BB address
11/// map YAML types declared in BBAddrMapYAML.h.
12///
13//===----------------------------------------------------------------------===//
14
19
20namespace llvm {
21namespace yaml {
22
24 IO &IO, BBAddrMapYAML::BBAddrMapEntry &E) {
25 assert(IO.getContext() && "The IO context is not initialized");
26 IO.mapRequired("Version", E.Version);
27 IO.mapOptional("Feature", E.Feature, Hex16(0));
28 IO.mapOptional("NumBBRanges", E.NumBBRanges);
29 IO.mapOptional("BBRanges", E.BBRanges);
30}
31
33 IO &IO, BBAddrMapYAML::BBAddrMapEntry::BBRangeEntry &E) {
34 IO.mapOptional("BaseAddress", E.BaseAddress, Hex64(0));
35 IO.mapOptional("NumBlocks", E.NumBlocks);
36 IO.mapOptional("BBEntries", E.BBEntries);
37}
38
40 IO &IO, BBAddrMapYAML::BBAddrMapEntry::BBEntry &E) {
41 assert(IO.getContext() && "The IO context is not initialized");
42 IO.mapOptional("ID", E.ID);
43 IO.mapRequired("AddressOffset", E.AddressOffset);
44 IO.mapRequired("Size", E.Size);
45 IO.mapRequired("Metadata", E.Metadata);
46 IO.mapOptional("CallsiteEndOffsets", E.CallsiteEndOffsets);
47 IO.mapOptional("Hash", E.Hash);
48}
49
51 IO &IO, BBAddrMapYAML::PGOAnalysisMapEntry &E) {
52 assert(IO.getContext() && "The IO context is not initialized");
53 IO.mapOptional("FuncEntryCount", E.FuncEntryCount);
54 IO.mapOptional("PGOBBEntries", E.PGOBBEntries);
55}
56
58 IO &IO, BBAddrMapYAML::PGOAnalysisMapEntry::PGOBBEntry &E) {
59 assert(IO.getContext() && "The IO context is not initialized");
60 IO.mapOptional("BBFreq", E.BBFreq);
61 IO.mapOptional("PostLinkBBFreq", E.PostLinkBBFreq);
62 IO.mapOptional("Successors", E.Successors);
63}
64
65void MappingTraits<
66 BBAddrMapYAML::PGOAnalysisMapEntry::PGOBBEntry::SuccessorEntry>::
67 mapping(IO &IO,
68 BBAddrMapYAML::PGOAnalysisMapEntry::PGOBBEntry::SuccessorEntry &E) {
69 assert(IO.getContext() && "The IO context is not initialized");
70 IO.mapRequired("ID", E.ID);
71 IO.mapRequired("BrProb", E.BrProb);
72 IO.mapOptional("PostLinkBrFreq", E.PostLinkBrFreq);
73}
74
75} // end namespace yaml
76
77namespace BBAddrMapYAML {
78
80 const std::vector<PGOAnalysisMapEntry> *PGOAnalyses,
82 llvm::endianness Endian, unsigned AddressSize) {
83 assert((AddressSize == 4 || AddressSize == 8) && "invalid address size");
84 for (const auto &[Idx, E] : llvm::enumerate(Entries)) {
85 // Write version and feature values.
86 if (E.Version > 5)
87 WithColor::warning() << "unsupported BB address map version: "
88 << static_cast<int>(E.Version)
89 << "; encoding using the most recent version";
90 CBA.write(E.Version);
91 if (E.Version < 5)
92 CBA.write(static_cast<uint8_t>(E.Feature));
93 else
94 CBA.write<uint16_t>(E.Feature, Endian);
95 auto FeatureOrErr = llvm::object::BBAddrMap::Features::decode(E.Feature);
96 if (!FeatureOrErr) {
97 // Invalid feature: warn and skip the entry.
98 WithColor::warning() << toString(FeatureOrErr.takeError());
99 continue;
100 }
101 bool MultiBBRangeFeatureEnabled = FeatureOrErr->MultiBBRange;
102 bool MultiBBRange =
103 MultiBBRangeFeatureEnabled ||
104 (E.NumBBRanges.has_value() && E.NumBBRanges.value() != 1) ||
105 (E.BBRanges && E.BBRanges->size() != 1);
106 if (MultiBBRange && !MultiBBRangeFeatureEnabled)
107 WithColor::warning() << "feature value(" << E.Feature
108 << ") does not support multiple BB ranges.";
109 if (MultiBBRange) {
110 // Write the number of basic block ranges, which is overridden by the
111 // 'NumBBRanges' field when specified.
112 uint64_t NumBBRanges =
113 E.NumBBRanges.value_or(E.BBRanges ? E.BBRanges->size() : 0);
114 CBA.writeULEB128(NumBBRanges);
115 }
116 if (!E.BBRanges)
117 continue;
118 uint64_t TotalNumBlocks = 0;
119 bool EmitCallsiteEndOffsets =
120 FeatureOrErr->CallsiteEndOffsets || E.hasAnyCallsiteEndOffsets();
121 for (const BBAddrMapEntry::BBRangeEntry &BBR : *E.BBRanges) {
122 // Write the pointer-sized base address of the range.
123 if (AddressSize == 8)
124 CBA.write<uint64_t>(BBR.BaseAddress, Endian);
125 else
126 CBA.write<uint32_t>(static_cast<uint32_t>(BBR.BaseAddress), Endian);
127 // Write number of BBEntries (number of basic blocks in this basic block
128 // range). This is overridden by the 'NumBlocks' YAML field when
129 // specified.
130 uint64_t NumBlocks =
131 BBR.NumBlocks.value_or(BBR.BBEntries ? BBR.BBEntries->size() : 0);
132 CBA.writeULEB128(NumBlocks);
133 // Write all BBEntries in this BBRange.
134 if (!BBR.BBEntries || FeatureOrErr->OmitBBEntries)
135 continue;
136 for (const BBAddrMapEntry::BBEntry &BBE : *BBR.BBEntries) {
137 ++TotalNumBlocks;
138 if (E.Version > 1)
139 CBA.writeULEB128(BBE.ID);
141 if (EmitCallsiteEndOffsets) {
142 size_t NumCallsiteEndOffsets =
143 BBE.CallsiteEndOffsets ? BBE.CallsiteEndOffsets->size() : 0;
144 CBA.writeULEB128(NumCallsiteEndOffsets);
145 if (BBE.CallsiteEndOffsets) {
147 CBA.writeULEB128(Offset);
148 }
149 }
150 CBA.writeULEB128(BBE.Size);
151 CBA.writeULEB128(BBE.Metadata);
152 if (FeatureOrErr->BBHash || BBE.Hash.has_value()) {
153 uint64_t Hash =
154 BBE.Hash.has_value() ? BBE.Hash.value() : llvm::yaml::Hex64(0);
155 CBA.write<uint64_t>(Hash, Endian);
156 }
157 }
158 }
159 if (!PGOAnalyses)
160 continue;
161 const PGOAnalysisMapEntry &PGOEntry = PGOAnalyses->at(Idx);
162
163 if (PGOEntry.FuncEntryCount)
164 CBA.writeULEB128(*PGOEntry.FuncEntryCount);
165
166 if (!PGOEntry.PGOBBEntries)
167 continue;
168
169 const auto &PGOBBEntries = PGOEntry.PGOBBEntries.value();
170 if (TotalNumBlocks != PGOBBEntries.size()) {
171 WithColor::warning() << "PGOBBEntries must be the same length as "
172 "BBEntries in the BB address map.\n"
173 << "Mismatch on function with address: "
174 << E.getFunctionAddress();
175 continue;
176 }
177
178 for (const auto &PGOBBE : PGOBBEntries) {
179 if (PGOBBE.BBFreq)
180 CBA.writeULEB128(*PGOBBE.BBFreq);
181 if (FeatureOrErr->PostLinkCfg || PGOBBE.PostLinkBBFreq.has_value())
182 CBA.writeULEB128(PGOBBE.PostLinkBBFreq.value_or(0));
183 if (PGOBBE.Successors) {
184 CBA.writeULEB128(PGOBBE.Successors->size());
185 for (const auto &[ID, BrProb, PostLinkBrFreq] : *PGOBBE.Successors) {
186 CBA.writeULEB128(ID);
187 CBA.writeULEB128(BrProb);
188 if (FeatureOrErr->PostLinkCfg || PostLinkBrFreq.has_value())
189 CBA.writeULEB128(PostLinkBrFreq.value_or(0));
190 }
191 }
192 }
193 }
194}
195
196} // end namespace BBAddrMapYAML
197} // end namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares the YAML representation of BB address maps (SHT_LLVM_BB_ADDR_MAP / ....
This file declares common types and utilities for basic-block address maps.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines ContiguousBlobAccumulator, the size-limited output buffer shared by the yaml2obj em...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition WithColor.cpp:86
void write(const char *Ptr, size_t Size)
void mapOptional(StringRef Key, T &Val)
Definition YAMLTraits.h:800
void * getContext() const
void mapRequired(StringRef Key, T &Val)
Definition YAMLTraits.h:790
LLVM_ABI void encodePayload(ArrayRef< BBAddrMapEntry > Entries, const std::vector< PGOAnalysisMapEntry > *PGOAnalyses, yaml::ContiguousBlobAccumulator &CBA, llvm::endianness Endian, unsigned AddressSize)
Encodes the BBAddrMap payload into CBA.
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:578
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
endianness
Definition bit.h:71
std::optional< std::vector< llvm::yaml::Hex64 > > CallsiteEndOffsets
std::optional< llvm::yaml::Hex64 > Hash
std::optional< std::vector< BBEntry > > BBEntries
std::optional< uint64_t > FuncEntryCount
std::optional< std::vector< PGOBBEntry > > PGOBBEntries
static Expected< Features > decode(uint16_t Val)
Definition BBAddrMap.h:62
This class should be specialized by any type that needs to be converted to/from a YAML mapping.
Definition YAMLTraits.h:63