[PATCH] compiler_rt: add low level versions of profile buffer functions.

larry at elder-gods.org larry at elder-gods.org
Tue Dec 9 11:29:59 PST 2014


From: Lawrence D'Anna <lawrence_danna at apple.com>

On Darwin, compiler_rt uses magic linker symbols to find the profile counters in
the __DATA segment.  This is a reasonable method for normal, hosted, userspace
programs.  However programs with custom memory layouts, such as the kernel, will
need to tell compiler_rt explicitly where to find these sections.
---
 lib/profile/InstrProfiling.h         |  3 ---
 lib/profile/InstrProfilingBuffer.c   | 49 +++++++++++++++++++++++++++++++++---
 lib/profile/InstrProfilingInternal.h | 48 +++++++++++++++++++++++++++++++++++
 3 files changed, 94 insertions(+), 6 deletions(-)
 create mode 100644 lib/profile/InstrProfilingInternal.h

diff --git a/lib/profile/InstrProfiling.h b/lib/profile/InstrProfiling.h
index a086f3d..2b1bd00 100644
--- a/lib/profile/InstrProfiling.h
+++ b/lib/profile/InstrProfiling.h
@@ -57,9 +57,6 @@ const char *__llvm_profile_end_names(void);
 uint64_t *__llvm_profile_begin_counters(void);
 uint64_t *__llvm_profile_end_counters(void);
 
-#define PROFILE_RANGE_SIZE(Range) \
-  (__llvm_profile_end_ ## Range () - __llvm_profile_begin_ ## Range ())
-
 /*!
  * \brief Write instrumentation data to the current file.
  *
diff --git a/lib/profile/InstrProfilingBuffer.c b/lib/profile/InstrProfilingBuffer.c
index 3351b07..47bb9a6 100644
--- a/lib/profile/InstrProfilingBuffer.c
+++ b/lib/profile/InstrProfilingBuffer.c
@@ -8,19 +8,43 @@
 \*===----------------------------------------------------------------------===*/
 
 #include "InstrProfiling.h"
+#include "InstrProfilingInternal.h"
+
 #include <string.h>
 
 __attribute__((visibility("hidden")))
 uint64_t __llvm_profile_get_size_for_buffer(void) {
+  const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
+  const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
+  const uint64_t *CountersBegin = __llvm_profile_begin_counters();
+  const uint64_t *CountersEnd   = __llvm_profile_end_counters();
+  const char *NamesBegin = __llvm_profile_begin_names();
+  const char *NamesEnd   = __llvm_profile_end_names();
+
+  return __llvm_profile_get_size_for_buffer_internal(DataBegin, DataEnd,
+					      CountersBegin, CountersEnd,
+					      NamesBegin, NamesEnd);
+}
+
+#define PROFILE_RANGE_SIZE(Range) (Range##End - Range##Begin)
+
+__attribute__((visibility("hidden")))
+uint64_t __llvm_profile_get_size_for_buffer_internal(const __llvm_profile_data *DataBegin,
+						     const __llvm_profile_data *DataEnd,
+						     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 = PROFILE_RANGE_SIZE(Names) * sizeof(char);
   const uint64_t Padding = sizeof(uint64_t) - NamesSize % sizeof(uint64_t);
   return sizeof(uint64_t) * PROFILE_HEADER_SIZE +
-     PROFILE_RANGE_SIZE(data) * sizeof(__llvm_profile_data) +
-     PROFILE_RANGE_SIZE(counters) * sizeof(uint64_t) +
+     PROFILE_RANGE_SIZE(Data) * sizeof(__llvm_profile_data) +
+     PROFILE_RANGE_SIZE(Counters) * sizeof(uint64_t) +
      NamesSize + Padding;
 }
 
+
 __attribute__((visibility("hidden")))
 int __llvm_profile_write_buffer(char *Buffer) {
   /* Match logic in __llvm_profile_get_size_for_buffer().
@@ -33,6 +57,25 @@ int __llvm_profile_write_buffer(char *Buffer) {
   const char *NamesBegin = __llvm_profile_begin_names();
   const char *NamesEnd   = __llvm_profile_end_names();
 
+  return __llvm_profile_write_buffer_internal(Buffer,
+				       DataBegin, DataEnd,
+				       CountersBegin, CountersEnd,
+				       NamesBegin, NamesEnd);
+}
+
+
+__attribute__((visibility("hidden")))
+int __llvm_profile_write_buffer_internal(char *Buffer,
+					 const __llvm_profile_data *DataBegin,
+					 const __llvm_profile_data *DataEnd,
+					 const uint64_t *CountersBegin,
+					 const uint64_t *CountersEnd  ,
+					 const char *NamesBegin,
+					 const char *NamesEnd) {
+  /* Match logic in __llvm_profile_get_size_for_buffer().
+   * Match logic in __llvm_profile_write_file().
+   */
+
   /* Calculate size of sections. */
   const uint64_t DataSize = DataEnd - DataBegin;
   const uint64_t CountersSize = CountersEnd - CountersBegin;
diff --git a/lib/profile/InstrProfilingInternal.h b/lib/profile/InstrProfilingInternal.h
new file mode 100644
index 0000000..894f7e4
--- /dev/null
+++ b/lib/profile/InstrProfilingInternal.h
@@ -0,0 +1,48 @@
+/*===- InstrProfiling.h- Support library for PGO instrumentation ----------===*\
+|*
+|*                     The LLVM Compiler Infrastructure
+|*
+|* This file is distributed under the University of Illinois Open Source
+|* License. See LICENSE.TXT for details.
+|*
+\*===----------------------------------------------------------------------===*/
+
+#ifndef PROFILE_INSTRPROFILING_INTERNALH_
+#define PROFILE_INSTRPROFILING_INTERNALH_
+
+#include "InstrProfiling.h"
+
+/*!
+ * \brief Write instrumentation data to the given buffer, given explicit
+ * pointers to the live data in memory.  This function is probably not what you
+ * want.  Use __llvm_profile_get_size_for_buffer instead.  Use this function if
+ * your program has a custom memory layout.
+ */
+uint64_t __llvm_profile_get_size_for_buffer_internal(
+	const __llvm_profile_data *DataBegin,
+	const __llvm_profile_data *DataEnd,
+	const uint64_t *CountersBegin,
+	const uint64_t *CountersEnd ,
+	const char *NamesBegin,
+	const char *NamesEnd);
+
+/*!
+ * \brief Write instrumentation data to the given buffer, given explicit
+ * pointers to the live data in memory.  This function is probably not what you
+ * want.  Use __llvm_profile_write_buffer instead.  Use this function if your
+ * program has a custom memory layout.
+ *
+ * \pre \c Buffer is the start of a buffer at least as big as \a
+ * __llvm_profile_get_size_for_buffer_internal().
+ */
+int __llvm_profile_write_buffer_internal(
+	char *Buffer,
+	const __llvm_profile_data *DataBegin,
+	const __llvm_profile_data *DataEnd,
+	const uint64_t *CountersBegin,
+	const uint64_t *CountersEnd ,
+	const char *NamesBegin,
+	const char *NamesEnd);
+
+
+#endif
-- 
1.9.3 (Apple Git-50)




More information about the llvm-commits mailing list