[compiler-rt] r261957 - [profile] Compute number of data entries correctly

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 25 18:49:42 PST 2016


Author: vedantk
Date: Thu Feb 25 20:49:41 2016
New Revision: 261957

URL: http://llvm.org/viewvc/llvm-project?rev=261957&view=rev
Log:
[profile] Compute number of data entries correctly

Compiler-rt miscalculates the number of entries in the __llvm_prf_data section
on i386 Darwin. This results in a number of test failures (which we started
catching after r261344).

The fix we attempted earlier is insufficient (r261683). It caused some tests to
start passing again, but that hid the fact that we drop some data entries.

This patch should fix the real problem. It fixes the way we compute DataSize by
taking into account the way the Darwin linker lays out __llvm_prf_data.

Differential Revision: http://reviews.llvm.org/D17623

Modified:
    compiler-rt/trunk/lib/profile/InstrProfiling.h
    compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c
    compiler-rt/trunk/lib/profile/InstrProfilingValue.c
    compiler-rt/trunk/lib/profile/InstrProfilingWriter.c

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=261957&r1=261956&r2=261957&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Thu Feb 25 20:49:41 2016
@@ -131,4 +131,8 @@ uint64_t __llvm_profile_get_magic(void);
 /*! \brief Get the version of the file format. */
 uint64_t __llvm_profile_get_version(void);
 
+/*! \brief Get the number of entries in the profile data section. */
+uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
+                                      const __llvm_profile_data *End);
+
 #endif /* PROFILE_INSTRPROFILING_H_ */

Modified: compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c?rev=261957&r1=261956&r2=261957&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingBuffer.c Thu Feb 25 20:49:41 2016
@@ -23,7 +23,13 @@ uint64_t __llvm_profile_get_size_for_buf
       DataBegin, DataEnd, CountersBegin, CountersEnd, NamesBegin, NamesEnd);
 }
 
-#define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin)
+COMPILER_RT_VISIBILITY
+uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
+                                      const __llvm_profile_data *End) {
+  intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
+  return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
+         sizeof(__llvm_profile_data);
+}
 
 COMPILER_RT_VISIBILITY
 uint64_t __llvm_profile_get_size_for_buffer_internal(
@@ -31,11 +37,12 @@ uint64_t __llvm_profile_get_size_for_buf
     const uint64_t *CountersBegin, const uint64_t *CountersEnd,
     const char *NamesBegin, const char *NamesEnd) {
   /* Match logic in __llvm_profile_write_buffer(). */
-  const uint64_t NamesSize = PROFILE_RANGE_SIZE(Names) * sizeof(char);
+  const uint64_t NamesSize = (NamesEnd - NamesBegin) * sizeof(char);
   const uint8_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);
   return sizeof(__llvm_profile_header) +
-         PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) +
-         PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) + NamesSize + Padding;
+         (__llvm_profile_get_data_size(DataBegin, DataEnd) *
+          sizeof(__llvm_profile_data)) +
+         (CountersEnd - CountersBegin) * sizeof(uint64_t) + NamesSize + Padding;
 }
 
 COMPILER_RT_VISIBILITY int __llvm_profile_write_buffer(char *Buffer) {

Modified: compiler-rt/trunk/lib/profile/InstrProfilingValue.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingValue.c?rev=261957&r1=261956&r2=261957&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingValue.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingValue.c Thu Feb 25 20:49:41 2016
@@ -142,8 +142,8 @@ __llvm_profile_gather_value_data(uint64_
   if (!ValueDataSize)
     return NULL;
 
-  ValueDataArray =
-      (ValueProfData **)calloc(DataEnd - DataBegin, sizeof(void *));
+  ValueDataArray = (ValueProfData **)calloc(
+      __llvm_profile_get_data_size(DataBegin, DataEnd), sizeof(void *));
   if (!ValueDataArray)
     PROF_OOM_RETURN("Failed to write value profile data ");
 

Modified: compiler-rt/trunk/lib/profile/InstrProfilingWriter.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingWriter.c?rev=261957&r1=261956&r2=261957&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingWriter.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingWriter.c Thu Feb 25 20:49:41 2016
@@ -144,7 +144,7 @@ COMPILER_RT_VISIBILITY int llvmWriteProf
     const char *NamesBegin, const char *NamesEnd) {
 
   /* Calculate size of sections. */
-  const uint64_t DataSize = DataEnd - DataBegin;
+  const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
   const uint64_t CountersSize = CountersEnd - CountersBegin;
   const uint64_t NamesSize = NamesEnd - NamesBegin;
   const uint64_t Padding = __llvm_profile_get_num_padding_bytes(NamesSize);




More information about the llvm-commits mailing list