Skip to content

Commit 989c084

Browse files
committed
Reworks the design to use same ELF section and not superset data structure.
1 parent 6374da5 commit 989c084

File tree

15 files changed

+404
-646
lines changed

15 files changed

+404
-646
lines changed

llvm/include/llvm/BinaryFormat/ELF.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,6 @@ enum : unsigned {
10371037
SHT_LLVM_BB_ADDR_MAP = 0x6fff4c0a, // LLVM Basic Block Address Map.
10381038
SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
10391039
SHT_LLVM_LTO = 0x6fff4c0c, // .llvm.lto for fat LTO.
1040-
SHT_LLVM_PGO_BB_ADDR_MAP = 0x6fff4c0d, // LLVM PGO extended BB Addr Map.
10411040
// Android's experimental support for SHT_RELR sections.
10421041
// https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
10431042
SHT_ANDROID_RELR = 0x6fffff00, // Relocation entries; only offsets.

llvm/include/llvm/Object/ELF.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,12 @@ class ELFFile {
413413
/// within the text section that the SHT_LLVM_BB_ADDR_MAP section \p Sec
414414
/// is associated with. If the current ELFFile is relocatable, a corresponding
415415
/// \p RelaSec must be passed in as an argument.
416+
/// Optional out variable to all collect PGO Analyses. New elements are only
417+
/// added if no error occurs. If not provided, the PGO Analyses are decoded
418+
/// then ignored.
416419
Expected<std::vector<BBAddrMap>>
417-
decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec = nullptr) const;
418-
419-
/// Decodes same as decodeBBAddrMap but also decodes extra information from
420-
/// features which are enabled.
421-
Expected<std::vector<PGOBBAddrMap>>
422-
decodePGOBBAddrMap(const Elf_Shdr &Sec,
423-
const Elf_Shdr *RelaSec = nullptr) const;
420+
decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec = nullptr,
421+
std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
424422

425423
/// Returns a map from every section matching \p IsMatch to its relocation
426424
/// section, or \p nullptr if it has no relocation section. This function

llvm/include/llvm/Object/ELFObjectFile.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,13 @@ class ELFObjectFileBase : public ObjectFile {
110110

111111
/// Returns a vector of all BB address maps in the object file. When
112112
// `TextSectionIndex` is specified, only returns the BB address maps
113-
// corresponding to the section with that index.
113+
// corresponding to the section with that index. When `PGOAnalyses`is
114+
// specified, the vector is cleared then filled with extra PGO data if the
115+
// feature when enabled in the ELF section. `PGOAnalyses` will always be the
116+
// same length as the return value on success, otherwise it is empty.
114117
Expected<std::vector<BBAddrMap>>
115-
readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt) const;
116-
117-
Expected<std::vector<PGOBBAddrMap>> readPGOBBAddrMap(
118-
std::optional<unsigned> TextSectionIndex = std::nullopt) const;
118+
readBBAddrMap(std::optional<unsigned> TextSectionIndex = std::nullopt,
119+
std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;
119120
};
120121

121122
class ELFSectionRef : public SectionRef {

llvm/include/llvm/Object/ELFTypes.h

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#define LLVM_OBJECT_ELFTYPES_H
1111

1212
#include "llvm/ADT/ArrayRef.h"
13-
#include "llvm/ADT/BitmaskEnum.h"
1413
#include "llvm/ADT/StringRef.h"
1514
#include "llvm/BinaryFormat/ELF.h"
1615
#include "llvm/Object/Error.h"
@@ -883,26 +882,24 @@ struct BBAddrMap {
883882
std::vector<BBEntry> BBEntries; // Basic block entries for this function.
884883
};
885884

886-
/// An extension of BBAddrMap that holds information relevant to PGO.
887-
struct PGOBBAddrMap {
885+
/// A feature extension of BBAddrMap that holds information relevant to PGO.
886+
struct PGOAnalysisMap {
888887
/// Bitmask of optional features to include in the PGO extended map.
889888
enum class Features {
890-
None = 0,
891889
FuncEntryCnt = (1 << 0),
892890
BBFreq = (1 << 1),
893891
BrProb = (1 << 2),
894-
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/BrProb),
895892
};
896893

897894
/// Super-set of BBAddrMap::BBEntry with additional fields for block frequency
898895
/// and branch probability.
899-
struct BBEntry {
896+
struct PGOBBEntry {
900897
using BaseMetadata = BBAddrMap::BBEntry::Metadata;
901898

902899
/// Enum indicating the how many successors a block has. This enum must fit
903900
/// into two bits.
904901
enum class SuccessorsType {
905-
/// None should be present if PGOBBAddrMap has disabled branch
902+
/// None should be present if BBAddrMap.feature has disabled branch
906903
/// probability.
907904
None = 0,
908905
/// Single successor blocks are not present in the successor entries.
@@ -926,8 +923,6 @@ struct PGOBBAddrMap {
926923
}
927924
};
928925

929-
/// Reuse of the fields provided by regular BBAddrMap
930-
BBAddrMap::BBEntry Base;
931926
/// Block frequency taken from MBFI
932927
BlockFrequency BlockFreq;
933928
/// List of successors of the current block
@@ -958,26 +953,25 @@ struct PGOBBAddrMap {
958953
return std::make_pair(MD, SuccType);
959954
}
960955

961-
bool operator==(const BBEntry &Other) const {
962-
return std::tie(Base, BlockFreq, Successors) ==
963-
std::tie(Other.Base, Other.BlockFreq, Other.Successors);
956+
bool operator==(const PGOBBEntry &Other) const {
957+
return std::tie(BlockFreq, Successors) ==
958+
std::tie(Other.BlockFreq, Other.Successors);
964959
}
965960
};
966961
// This field is duplicated from BBAddrMap since this class needs a different
967962
// type for the vector of entries.
968-
uint64_t Addr; // Function address
969-
std::vector<BBEntry> BBEntries; // Extended basic block entries
970-
uint64_t FuncEntryCount; // Prof count from IR function
963+
uint64_t FuncEntryCount; // Prof count from IR function
964+
std::vector<PGOBBEntry> BBEntries; // Extended basic block entries
971965

972966
// Flags to indicate if each PGO related info was enabled in this function
973967
bool FuncEntryCountEnabled : 1;
974968
bool BBFreqEnabled : 1;
975969
bool BBSuccProbEnabled : 1;
976970

977-
bool operator==(const PGOBBAddrMap &Other) const {
978-
return std::tie(Addr, FuncEntryCount, BBEntries, FuncEntryCountEnabled,
971+
bool operator==(const PGOAnalysisMap &Other) const {
972+
return std::tie(FuncEntryCount, BBEntries, FuncEntryCountEnabled,
979973
BBFreqEnabled, BBSuccProbEnabled) ==
980-
std::tie(Other.Addr, Other.FuncEntryCount, Other.BBEntries,
974+
std::tie(Other.FuncEntryCount, Other.BBEntries,
981975
Other.FuncEntryCountEnabled, Other.BBFreqEnabled,
982976
Other.BBSuccProbEnabled);
983977
}

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,31 @@ struct DynamicEntry {
156156
llvm::yaml::Hex64 Val;
157157
};
158158

159-
struct BBAddrMapCommonBase {
160-
uint8_t Version;
161-
llvm::yaml::Hex8 Feature;
162-
llvm::yaml::Hex64 Address;
163-
std::optional<uint64_t> NumBlocks;
164-
};
165-
166159
struct BBAddrMapEntry {
167160
struct BBEntry {
168161
uint32_t ID;
169162
llvm::yaml::Hex64 AddressOffset;
170163
llvm::yaml::Hex64 Size;
171164
llvm::yaml::Hex64 Metadata;
172165
};
173-
BBAddrMapCommonBase Common;
166+
uint8_t Version;
167+
llvm::yaml::Hex8 Feature;
168+
llvm::yaml::Hex64 Address;
169+
std::optional<uint64_t> NumBlocks;
174170
std::optional<std::vector<BBEntry>> BBEntries;
175171
};
176172

177-
struct PGOBBAddrMapEntry {
178-
struct BBEntry {
173+
struct PGOAnalysisMapEntry {
174+
struct PGOBBEntry {
179175
struct SuccessorEntry {
180176
uint32_t ID;
181177
llvm::yaml::Hex32 BrProb;
182178
};
183-
BBAddrMapEntry::BBEntry Base;
184179
std::optional<uint64_t> BBFreq;
185180
std::optional<std::vector<SuccessorEntry>> Successors;
186181
};
187-
BBAddrMapCommonBase Common;
188182
std::optional<uint64_t> FuncEntryCount;
189-
std::optional<std::vector<BBEntry>> BBEntries;
183+
std::optional<std::vector<PGOBBEntry>> PGOBBEntries;
190184
};
191185

192186
struct StackSizeEntry {
@@ -223,7 +217,6 @@ struct Chunk {
223217
DependentLibraries,
224218
CallGraphProfile,
225219
BBAddrMap,
226-
PGOBBAddrMap,
227220

228221
// Special chunks.
229222
SpecialChunksStart,
@@ -337,6 +330,7 @@ struct SectionHeaderTable : Chunk {
337330

338331
struct BBAddrMapSection : Section {
339332
std::optional<std::vector<BBAddrMapEntry>> Entries;
333+
std::optional<std::vector<PGOAnalysisMapEntry>> PGOAnalyses;
340334

341335
BBAddrMapSection() : Section(ChunkKind::BBAddrMap) {}
342336

@@ -349,20 +343,6 @@ struct BBAddrMapSection : Section {
349343
}
350344
};
351345

352-
struct PGOBBAddrMapSection : Section {
353-
std::optional<std::vector<PGOBBAddrMapEntry>> Entries;
354-
355-
PGOBBAddrMapSection() : Section(ChunkKind::PGOBBAddrMap) {}
356-
357-
std::vector<std::pair<StringRef, bool>> getEntries() const override {
358-
return {{"Entries", Entries.has_value()}};
359-
};
360-
361-
static bool classof(const Chunk *S) {
362-
return S->Kind == ChunkKind::PGOBBAddrMap;
363-
}
364-
};
365-
366346
struct StackSizesSection : Section {
367347
std::optional<std::vector<StackSizeEntry>> Entries;
368348

@@ -771,10 +751,10 @@ bool shouldAllocateFileSpace(ArrayRef<ProgramHeader> Phdrs,
771751
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::StackSizeEntry)
772752
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry)
773753
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry::BBEntry)
774-
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::PGOBBAddrMapEntry)
775-
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::PGOBBAddrMapEntry::BBEntry)
754+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::PGOAnalysisMapEntry)
755+
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::PGOAnalysisMapEntry::PGOBBEntry)
776756
LLVM_YAML_IS_SEQUENCE_VECTOR(
777-
llvm::ELFYAML::PGOBBAddrMapEntry::BBEntry::SuccessorEntry)
757+
llvm::ELFYAML::PGOAnalysisMapEntry::PGOBBEntry::SuccessorEntry)
778758
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
779759
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::LinkerOption)
780760
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::CallGraphEntryWeight)
@@ -943,18 +923,19 @@ template <> struct MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry> {
943923
static void mapping(IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &Rel);
944924
};
945925

946-
template <> struct MappingTraits<ELFYAML::PGOBBAddrMapEntry> {
947-
static void mapping(IO &IO, ELFYAML::PGOBBAddrMapEntry &Rel);
926+
template <> struct MappingTraits<ELFYAML::PGOAnalysisMapEntry> {
927+
static void mapping(IO &IO, ELFYAML::PGOAnalysisMapEntry &Rel);
948928
};
949929

950-
template <> struct MappingTraits<ELFYAML::PGOBBAddrMapEntry::BBEntry> {
951-
static void mapping(IO &IO, ELFYAML::PGOBBAddrMapEntry::BBEntry &Rel);
930+
template <> struct MappingTraits<ELFYAML::PGOAnalysisMapEntry::PGOBBEntry> {
931+
static void mapping(IO &IO, ELFYAML::PGOAnalysisMapEntry::PGOBBEntry &Rel);
952932
};
953933

954934
template <>
955-
struct MappingTraits<ELFYAML::PGOBBAddrMapEntry::BBEntry::SuccessorEntry> {
956-
static void mapping(IO &IO,
957-
ELFYAML::PGOBBAddrMapEntry::BBEntry::SuccessorEntry &Rel);
935+
struct MappingTraits<ELFYAML::PGOAnalysisMapEntry::PGOBBEntry::SuccessorEntry> {
936+
static void
937+
mapping(IO &IO,
938+
ELFYAML::PGOAnalysisMapEntry::PGOBBEntry::SuccessorEntry &Rel);
958939
};
959940

960941
template <> struct MappingTraits<ELFYAML::GnuHashHeader> {

llvm/lib/MC/MCParser/ELFAsmParser.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -673,8 +673,6 @@ bool ELFAsmParser::ParseSectionArguments(bool IsPush, SMLoc loc) {
673673
Type = ELF::SHT_LLVM_SYMPART;
674674
else if (TypeName == "llvm_bb_addr_map")
675675
Type = ELF::SHT_LLVM_BB_ADDR_MAP;
676-
else if (TypeName == "llvm_pgo_bb_addr_map")
677-
Type = ELF::SHT_LLVM_PGO_BB_ADDR_MAP;
678676
else if (TypeName == "llvm_offloading")
679677
Type = ELF::SHT_LLVM_OFFLOADING;
680678
else if (TypeName == "llvm_lto")

llvm/lib/MC/MCSectionELF.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
170170
OS << "llvm_bb_addr_map";
171171
else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP_V0)
172172
OS << "llvm_bb_addr_map_v0";
173-
else if (Type == ELF::SHT_LLVM_PGO_BB_ADDR_MAP)
174-
OS << "llvm_pgo_bb_addr_map";
175173
else if (Type == ELF::SHT_LLVM_OFFLOADING)
176174
OS << "llvm_offloading";
177175
else if (Type == ELF::SHT_LLVM_LTO)

0 commit comments

Comments
 (0)