[PATCH] D110188: [WIP][compiler-rt][profile] Fixes for failing profile tests

Leonard Chan via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 21 11:32:12 PDT 2021


leonardchan updated this revision to Diff 373996.
leonardchan added reviewers: gulfem, phosek.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D110188/new/

https://reviews.llvm.org/D110188

Files:
  compiler-rt/lib/profile/InstrProfilingMerge.c
  compiler-rt/lib/profile/InstrProfilingWriter.c
  compiler-rt/test/profile/Linux/corrupted-profile.c
  llvm/lib/ProfileData/InstrProfReader.cpp


Index: llvm/lib/ProfileData/InstrProfReader.cpp
===================================================================
--- llvm/lib/ProfileData/InstrProfReader.cpp
+++ llvm/lib/ProfileData/InstrProfReader.cpp
@@ -380,7 +380,9 @@
   auto PaddingSize = getNumPaddingBytes(NamesSize);
 
   // Profile data starts after profile header and binary ids if exist.
-  ptrdiff_t DataOffset = sizeof(RawInstrProf::Header) + BinaryIdsSize;
+  ptrdiff_t DataOffset = sizeof(RawInstrProf::Header) + BinaryIdsSize +
+                         getNumPaddingBytes(BinaryIdsSize);
+
   ptrdiff_t CountersOffset =
       DataOffset + DataSizeInBytes + PaddingBytesBeforeCounters;
   ptrdiff_t NamesOffset = CountersOffset + (sizeof(uint64_t) * CountersSize) +
Index: compiler-rt/test/profile/Linux/corrupted-profile.c
===================================================================
--- compiler-rt/test/profile/Linux/corrupted-profile.c
+++ compiler-rt/test/profile/Linux/corrupted-profile.c
@@ -1,6 +1,6 @@
 // RUN: rm -f %t.profraw
 // RUN: touch %t.profraw
-// RUN: %clang_profgen -o %t %s
+// RUN: %clang_profgen -Wl,--build-id=none -o %t %s
 // RUN: %t %t.profraw
 // RUN: %t %t.profraw modifyfile
 // RUN: cp %t.profraw %t.profraw.old
Index: compiler-rt/lib/profile/InstrProfilingWriter.c
===================================================================
--- compiler-rt/lib/profile/InstrProfilingWriter.c
+++ compiler-rt/lib/profile/InstrProfilingWriter.c
@@ -295,16 +295,28 @@
     return -1;
 
   /* Write the binary id lengths and data. */
-  if (__llvm_write_binary_ids(Writer) == -1)
+  int BinaryIdsSize;
+  if ((BinaryIdsSize = __llvm_write_binary_ids(Writer)) == -1)
     return -1;
 
   /* Write the profile data. */
   ProfDataIOVec IOVecData[] = {
+      {NULL, sizeof(uint8_t), __llvm_profile_get_num_padding_bytes(BinaryIdsSize), 1},
       {DataBegin, sizeof(__llvm_profile_data), DataSize, 0},
       {NULL, sizeof(uint8_t), PaddingBytesBeforeCounters, 1},
       {CountersBegin, sizeof(uint64_t), CountersSize, 0},
       {NULL, sizeof(uint8_t), PaddingBytesAfterCounters, 1},
-      {SkipNameDataWrite ? NULL : NamesBegin, sizeof(uint8_t), NamesSize, 0},
+      // FIXME: If I were to uncomment this, then Linux/instrprof-value-merge.c
+      // will fail to zlib::uncompress the Names data. This will be because the
+      // last two bytes in the compressed Names data will be zero'd out for some
+      // inexplicable reason. I do not know why. This only happens if we were to
+      // run the executable again with the same LIT_PROFILE_FILE env variable.
+      // At some point after opening the profile file for merging, and before
+      // closing it, the last two bytes are overwritten with zeros. I'm almost
+      // 100% confident it's not being explicitly overwritten, but I cannot
+      // figure out why it's happenning. As an undesireable workaround, this can
+      // be fixed by simply re-writing the Names data back here.
+      {/*SkipNameDataWrite ? NULL :*/ NamesBegin, sizeof(uint8_t), NamesSize, 0},
       {NULL, sizeof(uint8_t), PaddingBytesAfterNames, 1}};
   if (Writer->Write(Writer, IOVecData, sizeof(IOVecData) / sizeof(*IOVecData)))
     return -1;
Index: compiler-rt/lib/profile/InstrProfilingMerge.c
===================================================================
--- compiler-rt/lib/profile/InstrProfilingMerge.c
+++ compiler-rt/lib/profile/InstrProfilingMerge.c
@@ -44,9 +44,10 @@
   /* Check profile header only for now  */
   __llvm_profile_header *Header = (__llvm_profile_header *)ProfileData;
   __llvm_profile_data *SrcDataStart, *SrcDataEnd, *SrcData, *DstData;
+  int BinaryIdsPadding = __llvm_profile_get_num_padding_bytes(Header->BinaryIdsSize);
   SrcDataStart =
       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
-                              Header->BinaryIdsSize);
+                              Header->BinaryIdsSize + BinaryIdsPadding);
   SrcDataEnd = SrcDataStart + Header->DataSize;
 
   if (ProfileSize < sizeof(__llvm_profile_header))
@@ -101,9 +102,10 @@
   const char *SrcValueProfDataStart, *SrcValueProfData;
   uintptr_t CountersDelta = Header->CountersDelta;
 
+  int BinaryIdsPadding = __llvm_profile_get_num_padding_bytes(Header->BinaryIdsSize);
   SrcDataStart =
       (__llvm_profile_data *)(ProfileData + sizeof(__llvm_profile_header) +
-                              Header->BinaryIdsSize);
+                              Header->BinaryIdsSize + BinaryIdsPadding);
   SrcDataEnd = SrcDataStart + Header->DataSize;
   SrcCountersStart = (uint64_t *)SrcDataEnd;
   SrcNameStart = (const char *)(SrcCountersStart + Header->CountersSize);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D110188.373996.patch
Type: text/x-patch
Size: 4647 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210921/a59a7dd0/attachment.bin>


More information about the llvm-commits mailing list