Skip to content

Commit 2873060

Browse files
authored
[SHT_LLVM_BB_ADDR_MAP] Fixes two bugs in decoding of PGOAnalyses in BBAddrMap. (#77139)
We had specified that `readBBAddrMap` will always keep PGOAnalyses and BBAddrMaps the same length on success. https://github.com/llvm/llvm-project/blob/365fbbfbcfefb8766f7716109b9c3767b58e6058/llvm/include/llvm/Object/ELFObjectFile.h#L116-L117 It turns out that this is not currently the case when no analyses exist in a function. No test had caught it. We also should not append PGOBBEntries when there is no BBFreq or BrProb. This patch adds: * tests that PGOAnalyses and BBAddrMaps are same length even when no analyses are enabled * fixes decode so that PGOAnalyses and BBAddrMaps are same length * updates test to not emit unnecessary PGOBBEntries * fixes decode to not emit PGOBBEntries when unnecessary
1 parent dbea538 commit 2873060

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

llvm/include/llvm/Object/ELFTypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,9 @@ struct PGOAnalysisMap {
885885
bool BBFreq : 1;
886886
bool BrProb : 1;
887887

888+
// True if at least one feature is enabled
889+
bool anyEnabled() const { return FuncEntryCount || BBFreq || BrProb; }
890+
888891
// Encodes to minimum bit width representation.
889892
uint8_t encode() const {
890893
return (static_cast<uint8_t>(FuncEntryCount) << 0) |

llvm/lib/Object/ELF.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,16 +774,17 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
774774
}
775775
FunctionEntries.emplace_back(Address, std::move(BBEntries));
776776

777-
if (FeatEnable.FuncEntryCount || FeatEnable.BBFreq || FeatEnable.BrProb) {
777+
if (PGOAnalyses || FeatEnable.anyEnabled()) {
778778
// Function entry count
779779
uint64_t FuncEntryCount =
780780
FeatEnable.FuncEntryCount
781781
? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
782782
: 0;
783783

784784
std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
785-
for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
786-
(BlockIndex < NumBlocks);
785+
for (uint32_t BlockIndex = 0;
786+
(FeatEnable.BBFreq || FeatEnable.BrProb) && !MetadataDecodeErr &&
787+
!ULEBSizeErr && Cur && (BlockIndex < NumBlocks);
787788
++BlockIndex) {
788789
// Block frequency
789790
uint64_t BBF = FeatEnable.BBFreq

llvm/unittests/Object/ELFObjectFileTest.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,10 +1017,23 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
10171017
BrProb: 0xffffffff
10181018
- BBFreq: 1000
10191019
Successors: []
1020-
)");
1020+
- Name: .llvm_bb_addr_map_5
1021+
Type: SHT_LLVM_BB_ADDR_MAP
1022+
# Link: 0 (by default, can be overriden)
1023+
Entries:
1024+
- Version: 2
1025+
Address: 0x55555
1026+
Feature: 0x0
1027+
BBEntries:
1028+
- ID: 2
1029+
AddressOffset: 0x0
1030+
Size: 0x2
1031+
Metadata: 0x4
1032+
PGOAnalyses: [{}]
1033+
)");
10211034

10221035
BBAddrMap E1(0x11111, {{1, 0x0, 0x1, {false, true, false, false, false}}});
1023-
PGOAnalysisMap P1 = {892, {{}}, {true, false, false}};
1036+
PGOAnalysisMap P1 = {892, {}, {true, false, false}};
10241037
BBAddrMap E2(0x22222, {{2, 0x0, 0x2, {false, false, true, false, false}}});
10251038
PGOAnalysisMap P2 = {{}, {{BlockFrequency(343), {}}}, {false, true, false}};
10261039
BBAddrMap E3(0x33333, {{0, 0x0, 0x3, {false, true, true, false, false}},
@@ -1049,16 +1062,18 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
10491062
{BlockFrequency(18), {{3, BranchProbability::getRaw(0xffff'ffff)}}},
10501063
{BlockFrequency(1000), {}}},
10511064
{true, true, true}};
1065+
BBAddrMap E5(0x55555, {{2, 0x0, 0x2, {false, false, true, false, false}}});
1066+
PGOAnalysisMap P5 = {{}, {}, {false, false, false}};
10521067

1053-
std::vector<BBAddrMap> Section0BBAddrMaps = {E4};
1068+
std::vector<BBAddrMap> Section0BBAddrMaps = {E4, E5};
10541069
std::vector<BBAddrMap> Section1BBAddrMaps = {E3};
10551070
std::vector<BBAddrMap> Section2BBAddrMaps = {E1, E2};
1056-
std::vector<BBAddrMap> AllBBAddrMaps = {E1, E2, E3, E4};
1071+
std::vector<BBAddrMap> AllBBAddrMaps = {E1, E2, E3, E4, E5};
10571072

1058-
std::vector<PGOAnalysisMap> Section0PGOAnalysisMaps = {P4};
1073+
std::vector<PGOAnalysisMap> Section0PGOAnalysisMaps = {P4, P5};
10591074
std::vector<PGOAnalysisMap> Section1PGOAnalysisMaps = {P3};
10601075
std::vector<PGOAnalysisMap> Section2PGOAnalysisMaps = {P1, P2};
1061-
std::vector<PGOAnalysisMap> AllPGOAnalysisMaps = {P1, P2, P3, P4};
1076+
std::vector<PGOAnalysisMap> AllPGOAnalysisMaps = {P1, P2, P3, P4, P5};
10621077

10631078
auto DoCheckSucceeds =
10641079
[&](StringRef YamlString, std::optional<unsigned> TextSectionIndex,
@@ -1081,6 +1096,10 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
10811096
if (ExpectedPGO) {
10821097
EXPECT_EQ(BBAddrMaps->size(), PGOAnalyses.size());
10831098
EXPECT_EQ(PGOAnalyses, *ExpectedPGO);
1099+
for (auto &&[BB, PGO] : llvm::zip(*BBAddrMaps, PGOAnalyses)) {
1100+
if (PGO.FeatEnable.BBFreq || PGO.FeatEnable.BrProb)
1101+
EXPECT_EQ(BB.getBBEntries().size(), PGO.BBEntries.size());
1102+
}
10841103
}
10851104
};
10861105

@@ -1132,9 +1151,9 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
11321151
Link: 10
11331152
)";
11341153

1135-
DoCheckFails(InvalidLinkedYamlString, /*TextSectionIndex=*/4,
1154+
DoCheckFails(InvalidLinkedYamlString, /*TextSectionIndex=*/5,
11361155
"unable to get the linked-to section for "
1137-
"SHT_LLVM_BB_ADDR_MAP section with index 4: invalid section "
1156+
"SHT_LLVM_BB_ADDR_MAP section with index 5: invalid section "
11381157
"index: 10");
11391158
// Linked sections are not checked when we don't target a specific text
11401159
// section.
@@ -1150,7 +1169,7 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
11501169
)";
11511170

11521171
DoCheckFails(TruncatedYamlString, /*TextSectionIndex=*/std::nullopt,
1153-
"unable to read SHT_LLVM_BB_ADDR_MAP section with index 4: "
1172+
"unable to read SHT_LLVM_BB_ADDR_MAP section with index 5: "
11541173
"unable to decode LEB128 at offset 0x0000000a: malformed "
11551174
"uleb128, extends past end");
11561175
// Check that we can read the other section's bb-address-maps which are

0 commit comments

Comments
 (0)