Skip to content

Commit b66779b

Browse files
[nfc][InstrProfReader]Store header fields in native endianness (#92947)
- Use `Header.Version` directly and remove Header::formatVersion --------- Co-authored-by: Kazu Hirata <[email protected]>
1 parent d53c6cd commit b66779b

File tree

3 files changed

+28
-51
lines changed

3 files changed

+28
-51
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,16 +1203,13 @@ struct Header {
12031203
// computation is correct. The methods below need to be updated to ensure that
12041204
// the new field is read correctly.
12051205

1206-
// Reads a header struct from the buffer.
1206+
// Reads a header struct from the buffer. Header fields are in machine native
1207+
// endianness.
12071208
static Expected<Header> readFromBuffer(const unsigned char *Buffer);
12081209

12091210
// Returns the size of the header in bytes for all valid fields based on the
12101211
// version. I.e a older version header will return a smaller size.
12111212
size_t size() const;
1212-
1213-
// Returns the format version in little endian. The header retains the version
1214-
// in native endian of the compiler runtime.
1215-
uint64_t formatVersion() const;
12161213
};
12171214

12181215
// Profile summary data recorded in the profile data file in indexed

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,13 +1620,12 @@ inline size_t constexpr offsetOf(T1 T2::*Member) {
16201620
return size_t(&(Object.*Member)) - size_t(&Object);
16211621
}
16221622

1623+
// Read a uint64_t from the specified buffer offset, and swap the bytes in
1624+
// native endianness if necessary.
16231625
static inline uint64_t read(const unsigned char *Buffer, size_t Offset) {
1624-
return *reinterpret_cast<const uint64_t *>(Buffer + Offset);
1625-
}
1626-
1627-
uint64_t Header::formatVersion() const {
1628-
using namespace support;
1629-
return endian::byte_swap<uint64_t, llvm::endianness::little>(Version);
1626+
using namespace ::support;
1627+
return endian::read<uint64_t, llvm::endianness::little, unaligned>(Buffer +
1628+
Offset);
16301629
}
16311630

16321631
Expected<Header> Header::readFromBuffer(const unsigned char *Buffer) {
@@ -1638,18 +1637,15 @@ Expected<Header> Header::readFromBuffer(const unsigned char *Buffer) {
16381637

16391638
H.Magic = read(Buffer, offsetOf(&Header::Magic));
16401639
// Check the magic number.
1641-
uint64_t Magic =
1642-
endian::byte_swap<uint64_t, llvm::endianness::little>(H.Magic);
1643-
if (Magic != IndexedInstrProf::Magic)
1640+
if (H.Magic != IndexedInstrProf::Magic)
16441641
return make_error<InstrProfError>(instrprof_error::bad_magic);
16451642

16461643
// Read the version.
16471644
H.Version = read(Buffer, offsetOf(&Header::Version));
1648-
if (GET_VERSION(H.formatVersion()) >
1649-
IndexedInstrProf::ProfVersion::CurrentVersion)
1645+
if (GET_VERSION(H.Version) > IndexedInstrProf::ProfVersion::CurrentVersion)
16501646
return make_error<InstrProfError>(instrprof_error::unsupported_version);
16511647

1652-
switch (GET_VERSION(H.formatVersion())) {
1648+
switch (GET_VERSION(H.Version)) {
16531649
// When a new field is added in the header add a case statement here to
16541650
// populate it.
16551651
static_assert(
@@ -1680,7 +1676,7 @@ Expected<Header> Header::readFromBuffer(const unsigned char *Buffer) {
16801676
}
16811677

16821678
size_t Header::size() const {
1683-
switch (GET_VERSION(formatVersion())) {
1679+
switch (GET_VERSION(Version)) {
16841680
// When a new field is added to the header add a case statement here to
16851681
// compute the size as offset of the new field + size of the new field. This
16861682
// relies on the field being added to the end of the list.

llvm/lib/ProfileData/InstrProfReader.cpp

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,43 +1311,33 @@ Error IndexedInstrProfReader::readHeader() {
13111311
const IndexedInstrProf::Header *Header = &HeaderOr.get();
13121312
Cur += Header->size();
13131313

1314-
Cur = readSummary((IndexedInstrProf::ProfVersion)Header->formatVersion(), Cur,
1314+
Cur = readSummary((IndexedInstrProf::ProfVersion)Header->Version, Cur,
13151315
/* UseCS */ false);
1316-
if (Header->formatVersion() & VARIANT_MASK_CSIR_PROF)
1317-
Cur =
1318-
readSummary((IndexedInstrProf::ProfVersion)Header->formatVersion(), Cur,
1319-
/* UseCS */ true);
1316+
if (Header->Version & VARIANT_MASK_CSIR_PROF)
1317+
Cur = readSummary((IndexedInstrProf::ProfVersion)Header->Version, Cur,
1318+
/* UseCS */ true);
13201319
// Read the hash type and start offset.
1321-
IndexedInstrProf::HashT HashType = static_cast<IndexedInstrProf::HashT>(
1322-
endian::byte_swap<uint64_t, llvm::endianness::little>(Header->HashType));
1320+
IndexedInstrProf::HashT HashType =
1321+
static_cast<IndexedInstrProf::HashT>(Header->HashType);
13231322
if (HashType > IndexedInstrProf::HashT::Last)
13241323
return error(instrprof_error::unsupported_hash_type);
13251324

1326-
uint64_t HashOffset =
1327-
endian::byte_swap<uint64_t, llvm::endianness::little>(Header->HashOffset);
1328-
13291325
// The hash table with profile counts comes next.
13301326
auto IndexPtr = std::make_unique<InstrProfReaderIndex<OnDiskHashTableImplV3>>(
1331-
Start + HashOffset, Cur, Start, HashType, Header->formatVersion());
1327+
Start + Header->HashOffset, Cur, Start, HashType, Header->Version);
13321328

13331329
// The MemProfOffset field in the header is only valid when the format
13341330
// version is higher than 8 (when it was introduced).
1335-
if (GET_VERSION(Header->formatVersion()) >= 8 &&
1336-
Header->formatVersion() & VARIANT_MASK_MEMPROF) {
1337-
uint64_t MemProfOffset =
1338-
endian::byte_swap<uint64_t, llvm::endianness::little>(
1339-
Header->MemProfOffset);
1340-
if (Error E = MemProfReader.deserialize(Start, MemProfOffset))
1331+
if (GET_VERSION(Header->Version) >= 8 &&
1332+
Header->Version & VARIANT_MASK_MEMPROF) {
1333+
if (Error E = MemProfReader.deserialize(Start, Header->MemProfOffset))
13411334
return E;
13421335
}
13431336

13441337
// BinaryIdOffset field in the header is only valid when the format version
13451338
// is higher than 9 (when it was introduced).
1346-
if (GET_VERSION(Header->formatVersion()) >= 9) {
1347-
uint64_t BinaryIdOffset =
1348-
endian::byte_swap<uint64_t, llvm::endianness::little>(
1349-
Header->BinaryIdOffset);
1350-
const unsigned char *Ptr = Start + BinaryIdOffset;
1339+
if (GET_VERSION(Header->Version) >= 9) {
1340+
const unsigned char *Ptr = Start + Header->BinaryIdOffset;
13511341
// Read binary ids size.
13521342
BinaryIdsSize =
13531343
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
@@ -1360,11 +1350,8 @@ Error IndexedInstrProfReader::readHeader() {
13601350
"corrupted binary ids");
13611351
}
13621352

1363-
if (GET_VERSION(Header->formatVersion()) >= 12) {
1364-
uint64_t VTableNamesOffset =
1365-
endian::byte_swap<uint64_t, llvm::endianness::little>(
1366-
Header->VTableNamesOffset);
1367-
const unsigned char *Ptr = Start + VTableNamesOffset;
1353+
if (GET_VERSION(Header->Version) >= 12) {
1354+
const unsigned char *Ptr = Start + Header->VTableNamesOffset;
13681355

13691356
CompressedVTableNamesLen =
13701357
support::endian::readNext<uint64_t, llvm::endianness::little>(Ptr);
@@ -1376,12 +1363,9 @@ Error IndexedInstrProfReader::readHeader() {
13761363
return make_error<InstrProfError>(instrprof_error::truncated);
13771364
}
13781365

1379-
if (GET_VERSION(Header->formatVersion()) >= 10 &&
1380-
Header->formatVersion() & VARIANT_MASK_TEMPORAL_PROF) {
1381-
uint64_t TemporalProfTracesOffset =
1382-
endian::byte_swap<uint64_t, llvm::endianness::little>(
1383-
Header->TemporalProfTracesOffset);
1384-
const unsigned char *Ptr = Start + TemporalProfTracesOffset;
1366+
if (GET_VERSION(Header->Version) >= 10 &&
1367+
Header->Version & VARIANT_MASK_TEMPORAL_PROF) {
1368+
const unsigned char *Ptr = Start + Header->TemporalProfTracesOffset;
13851369
const auto *PtrEnd = (const unsigned char *)DataBuffer->getBufferEnd();
13861370
// Expect at least two 64 bit fields: NumTraces, and TraceStreamSize
13871371
if (Ptr + 2 * sizeof(uint64_t) > PtrEnd)

0 commit comments

Comments
 (0)