Skip to content

Commit c113f78

Browse files
committed
Bump feature value to a 16byte value.
1 parent ffd870c commit c113f78

File tree

10 files changed

+34
-17
lines changed

10 files changed

+34
-17
lines changed

llvm/include/llvm/Object/ELFTypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ struct BBAddrMap {
841841
bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }
842842

843843
// Encodes to minimum bit width representation.
844-
uint8_t encode() const {
844+
uint16_t encode() const {
845845
return (static_cast<uint8_t>(FuncEntryCount) << 0) |
846846
(static_cast<uint8_t>(BBFreq) << 1) |
847847
(static_cast<uint8_t>(BrProb) << 2) |
@@ -854,7 +854,7 @@ struct BBAddrMap {
854854

855855
// Decodes from minimum bit width representation and validates no
856856
// unnecessary bits are used.
857-
static Expected<Features> decode(uint8_t Val) {
857+
static Expected<Features> decode(uint16_t Val) {
858858
Features Feat{
859859
static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
860860
static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3)),

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ struct BBAddrMapEntry {
166166
std::optional<llvm::yaml::Hex64> Hash;
167167
};
168168
uint8_t Version;
169-
llvm::yaml::Hex8 Feature;
169+
llvm::yaml::Hex16 Feature;
170170

171171
struct BBRangeEntry {
172172
llvm::yaml::Hex64 BaseAddress;

llvm/lib/Object/ELF.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
831831
};
832832

833833
uint8_t Version = 0;
834-
uint8_t Feature = 0;
834+
uint16_t Feature = 0;
835835
BBAddrMap::Features FeatEnable{};
836836
while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
837837
Cur.tell() < Content.size()) {
@@ -841,7 +841,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
841841
if (Version < 2 || Version > 5)
842842
return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
843843
Twine(static_cast<int>(Version)));
844-
Feature = Data.getU8(Cur); // Feature byte
844+
Feature = Version < 5 ? Data.getU8(Cur) : Data.getU16(Cur);
845845
if (!Cur)
846846
break;
847847
auto FeatEnableOrErr = BBAddrMap::Features::decode(Feature);
@@ -860,7 +860,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
860860
" feature = " + Twine(static_cast<int>(Feature)));
861861
if (FeatEnable.PostLinkCfg && Version < 5)
862862
return createError("version should be >= 5 for SHT_LLVM_BB_ADDR_MAP when "
863-
"basic block hash feature is enabled: version = " +
863+
"post link cfg feature is enabled: version = " +
864864
Twine(static_cast<int>(Version)) +
865865
" feature = " + Twine(static_cast<int>(Feature)));
866866
uint32_t NumBlocksInBBRange = 0;

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,8 +1470,14 @@ void ELFState<ELFT>::writeSectionContent(
14701470
<< static_cast<int>(E.Version)
14711471
<< "; encoding using the most recent version";
14721472
CBA.write(E.Version);
1473-
CBA.write(E.Feature);
1474-
SHeader.sh_size += 2;
1473+
SHeader.sh_size += 1;
1474+
if (E.Version < 5) {
1475+
CBA.write(static_cast<uint8_t>(E.Feature));
1476+
SHeader.sh_size += 1;
1477+
} else {
1478+
CBA.write<uint16_t>(E.Feature, ELFT::Endianness);
1479+
SHeader.sh_size += 2;
1480+
}
14751481
}
14761482
auto FeatureOrErr = llvm::object::BBAddrMap::Features::decode(E.Feature);
14771483
bool MultiBBRangeFeatureEnabled = false;

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1886,7 +1886,7 @@ void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping(
18861886
IO &IO, ELFYAML::BBAddrMapEntry &E) {
18871887
assert(IO.getContext() && "The IO context is not initialized");
18881888
IO.mapRequired("Version", E.Version);
1889-
IO.mapOptional("Feature", E.Feature, Hex8(0));
1889+
IO.mapOptional("Feature", E.Feature, Hex16(0));
18901890
IO.mapOptional("NumBBRanges", E.NumBBRanges);
18911891
IO.mapOptional("BBRanges", E.BBRanges);
18921892
}

llvm/test/tools/llvm-readobj/ELF/bb-addr-map-pgo-analysis-map.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
## Check that a malformed section can be handled.
1717
# RUN: yaml2obj %s -DBITS=32 -DSIZE=24 -o %t2.o
18-
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s -DOFFSET=0x00000014 -DFILE=%t2.o --check-prefix=TRUNCATED
18+
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s -DOFFSET=0x00000015 -DFILE=%t2.o --check-prefix=TRUNCATED
1919

2020
## Check that missing features can be handled.
2121
# RUN: yaml2obj %s -DBITS=32 -DFEATURE=0x2 -o %t3.o

llvm/test/tools/yaml2obj/ELF/bb-addr-map-pgo-analysis-map.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Sections:
2727
## Test the following cases:
2828

2929
## 1) We can produce an .llvm_bb_addr_map section from a description with
30-
## Entries and PGO Analysis data.
30+
## Entries and PGO Analysis and Post Link data.
3131
- Name: '.llvm_bb_addr_map (1)'
3232
Type: SHT_LLVM_BB_ADDR_MAP
3333
Entries:
@@ -68,8 +68,8 @@ Sections:
6868
Metadata: 0x00000003
6969

7070
## Check that yaml2obj generates a warning when we use unsupported feature.
71-
# RUN: not yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=INVALID-FEATURE
72-
# INVALID-FEATURE: error: out of range hex8 number
71+
# RUN: yaml2obj --docnum=2 %s 2>&1 | FileCheck %s --check-prefix=INVALID-FEATURE
72+
# INVALID-FEATURE: warning: invalid encoding for BBAddrMap::Features: 0x100
7373

7474
--- !ELF
7575
FileHeader:

llvm/tools/obj2yaml/elf2yaml.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ ELFDumper<ELFT>::dumpBBAddrMapSection(const Elf_Shdr *Shdr) {
895895
std::vector<ELFYAML::PGOAnalysisMapEntry> PGOAnalyses;
896896
DataExtractor::Cursor Cur(0);
897897
uint8_t Version = 0;
898-
uint8_t Feature = 0;
898+
uint16_t Feature = 0;
899899
uint64_t Address = 0;
900900
while (Cur && Cur.tell() < Content.size()) {
901901
if (Shdr->sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) {
@@ -905,7 +905,7 @@ ELFDumper<ELFT>::dumpBBAddrMapSection(const Elf_Shdr *Shdr) {
905905
errc::invalid_argument,
906906
"invalid SHT_LLVM_BB_ADDR_MAP section version: " +
907907
Twine(static_cast<int>(Version)));
908-
Feature = Data.getU8(Cur);
908+
Feature = Version < 5 ? Data.getU8(Cur) : Data.getU16(Cur);
909909
}
910910
uint64_t NumBBRanges = 1;
911911
uint64_t NumBlocks = 0;

llvm/unittests/Object/ELFObjectFileTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ TEST(ELFObjectFileTest, ReadPGOAnalysisMap) {
14741474
DoCheckFails(
14751475
TruncatedYamlString, /*TextSectionIndex=*/std::nullopt,
14761476
"unable to read SHT_LLVM_BB_ADDR_MAP section with index 6: "
1477-
"unexpected end of data at offset 0xa while reading [0x3, 0xb)");
1477+
"unexpected end of data at offset 0xa while reading [0x4, 0xc)");
14781478
// Check that we can read the other section's bb-address-maps which are
14791479
// valid.
14801480
DoCheckSucceeds(TruncatedYamlString, /*TextSectionIndex=*/2,

llvm/unittests/Object/ELFTypesTest.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TEST(ELFTypesTest, BBAddrMapFeaturesEncodingTest) {
116116
{false, false, false, false, false, false, true, false},
117117
{false, false, false, false, false, false, false, true},
118118
{false, false, false, false, false, false, true, true}}};
119-
const std::array<uint8_t, 14> Encoded = {
119+
const std::array<uint16_t, 14> Encoded = {
120120
{0b0000, 0b0001, 0b0010, 0b0100, 0b1000, 0b0011, 0b0110, 0b1110, 0b1111,
121121
0b1'0000, 0b10'0000, 0b100'0000, 0b1000'0000, 0b1100'0000}};
122122
for (const auto &[Feat, EncodedVal] : llvm::zip(Decoded, Encoded))
@@ -128,3 +128,14 @@ TEST(ELFTypesTest, BBAddrMapFeaturesEncodingTest) {
128128
EXPECT_EQ(*FeatEnableOrError, Feat);
129129
}
130130
}
131+
132+
TEST(ELFTypesTest, BBAddrMapFeaturesInvalidEncodingTest) {
133+
const std::array<std::string, 2> Errors = {
134+
"invalid encoding for BBAddrMap::Features: 0x100",
135+
"invalid encoding for BBAddrMap::Features: 0x1000"};
136+
const std::array<uint16_t, 2> Values = {{0b1'0000'0000, 0b1'0000'0000'0000}};
137+
for (const auto &[Val, Error] : llvm::zip(Values, Errors)) {
138+
EXPECT_THAT_ERROR(BBAddrMap::Features::decode(Val).takeError(),
139+
FailedWithMessage(Error));
140+
}
141+
}

0 commit comments

Comments
 (0)