[compiler-rt] r278092 - [Profile] Implement new API __llvm_profile_dump

Xinliang David Li via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 8 21:21:14 PDT 2016


Author: davidxl
Date: Mon Aug  8 23:21:14 2016
New Revision: 278092

URL: http://llvm.org/viewvc/llvm-project?rev=278092&view=rev
Log:
[Profile] Implement new API __llvm_profile_dump

The API is intended to be used by user to do fine
grained (per-region) control of profile dumping.

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



Added:
    compiler-rt/trunk/test/profile/instrprof-dump.c
Modified:
    compiler-rt/trunk/lib/profile/InstrProfiling.c
    compiler-rt/trunk/lib/profile/InstrProfiling.h
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c
    compiler-rt/trunk/lib/profile/InstrProfilingInternal.h

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.c?rev=278092&r1=278091&r2=278092&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.c Mon Aug  8 23:21:14 2016
@@ -27,6 +27,16 @@ COMPILER_RT_VISIBILITY uint64_t __llvm_p
                                             : (INSTR_PROF_RAW_MAGIC_32);
 }
 
+static unsigned ProfileDumped = 0;
+
+COMPILER_RT_VISIBILITY unsigned lprofProfileDumped() {
+  return ProfileDumped;
+}
+
+COMPILER_RT_VISIBILITY void lprofSetProfileDumped() {
+  ProfileDumped = 1;
+}
+
 /* Return the number of bytes needed to add to SizeInBytes to make it
  *   the result a multiple of 8.
  */
@@ -68,4 +78,5 @@ COMPILER_RT_VISIBILITY void __llvm_profi
       }
     }
   }
+  ProfileDumped = 0;
 }

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=278092&r1=278091&r2=278092&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Mon Aug  8 23:21:14 2016
@@ -118,6 +118,28 @@ void INSTR_PROF_VALUE_PROF_FUNC(
 int __llvm_profile_write_file(void);
 
 /*!
+ * \brief this is a wrapper interface to \c __llvm_profile_write_file.
+ * After this interface is invoked, a arleady dumped flag will be set
+ * so that profile won't be dumped again during program exit. 
+ * Invocation of interface __llvm_profile_reset_counters will clear
+ * the flag. This interface is designed to be used to collect profile
+ * data from user selected hot regions. The use model is
+ *      __llvm_profile_reset_counters();
+ *      ... hot region 1
+ *      __llvm_profile_dump();
+ *      .. some other code
+ *      __llvm_profile_reset_counters();
+ *       ... hot region 2
+ *      __llvm_profile_dump();
+ *
+ *  It is expected that on-line profile merging is on with \c %m specifier
+ *  used in profile filename . If merging is  not turned on, user is expected
+ *  to invoke __llvm_profile_set_filename  to specify different profile names
+ *  for different regions before dumping to avoid profile write clobbering.
+ */
+int __llvm_profile_dump(void);
+
+/*!
  * \brief Set the filename for writing instrumentation data.
  *
  * Sets the filename to be used for subsequent calls to

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=278092&r1=278091&r2=278092&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Mon Aug  8 23:21:14 2016
@@ -522,6 +522,12 @@ int __llvm_profile_write_file(void) {
   const char *Filename;
   char *FilenameBuf;
 
+  if (lprofProfileDumped()) {
+    PROF_NOTE("Profile data not written to file: %s.\n", 
+              "already written");
+    return 0;
+  }
+
   Length = getCurFilenameLength();
   FilenameBuf = (char *)COMPILER_RT_ALLOCA(Length + 1);
   Filename = getCurFilename(FilenameBuf);
@@ -548,6 +554,18 @@ int __llvm_profile_write_file(void) {
   return rc;
 }
 
+COMPILER_RT_VISIBILITY
+int __llvm_profile_dump(void) {
+  if (!doMerging())
+    PROF_WARN("Later invocation of __llvm_profile_dump can lead to clobbering "
+              " of previously dumped profile data : %s. Either use \%m "
+              "in profile name or change profile name before dumping.\n",
+              "online profile merging is not on");
+  int rc = __llvm_profile_write_file();
+  lprofSetProfileDumped();
+  return rc;
+}
+
 static void writeFileWithoutReturn(void) { __llvm_profile_write_file(); }
 
 COMPILER_RT_VISIBILITY

Modified: compiler-rt/trunk/lib/profile/InstrProfilingInternal.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingInternal.h?rev=278092&r1=278091&r2=278092&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingInternal.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingInternal.h Mon Aug  8 23:21:14 2016
@@ -163,6 +163,13 @@ void lprofSetupValueProfiler();
  * to dump merged profile data into its own profile file. */
 uint64_t lprofGetLoadModuleSignature();
 
+/* 
+ * Return non zero value if the profile data has already been
+ * dumped to the file.
+ */
+unsigned lprofProfileDumped();
+void lprofSetProfileDumped();
+
 COMPILER_RT_VISIBILITY extern char *(*GetEnvHook)(const char *);
 COMPILER_RT_VISIBILITY extern void (*FreeHook)(void *);
 COMPILER_RT_VISIBILITY extern uint8_t *DynamicBufferIOBuffer;

Added: compiler-rt/trunk/test/profile/instrprof-dump.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/instrprof-dump.c?rev=278092&view=auto
==============================================================================
--- compiler-rt/trunk/test/profile/instrprof-dump.c (added)
+++ compiler-rt/trunk/test/profile/instrprof-dump.c Mon Aug  8 23:21:14 2016
@@ -0,0 +1,62 @@
+/*
+RUN: rm -fr %t.profdir
+RUN: %clang_profgen=%t.profdir/default_%m.profraw -o %t -O2 %s
+RUN: %run %t  2>&1 | FileCheck %s --check-prefix=NO_EXIT_WRITE
+RUN: llvm-profdata merge -o %t.profdata %t.profdir
+RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s  --check-prefix=PROF
+
+NO_EXIT_WRITE: Profile data not written to file: already written
+*/
+
+int __llvm_profile_dump(void);
+void __llvm_profile_reset_counters(void);
+int foo(int);
+int bar(int);
+int skip(int);
+
+int main(int argc, const char *argv[]) {
+  int Ret = foo(0); /* region 1 */
+  __llvm_profile_dump();
+
+  /* not profiled -- cleared later. */
+  skip(0);   /* skipped region */
+  
+  __llvm_profile_reset_counters();
+  Ret += bar(0);  /* region 2 */
+  __llvm_profile_dump();
+
+  skip(1);
+
+  __llvm_profile_reset_counters();
+  /* foo's profile will be merged.  */
+  foo(1);  /* region 3 */
+  __llvm_profile_dump();
+
+  return Ret;
+}
+
+__attribute__((noinline)) int foo(int X) {
+  /* PROF: define {{.*}} @foo({{.*}}!prof ![[ENT:[0-9]+]]
+     PROF: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]]
+  */
+  return X <= 0 ? -X : X;
+}
+
+__attribute__((noinline)) int skip(int X) {
+  /* PROF: define {{.*}} @skip(
+     PROF: br i1 %{{.*}}, label %{{.*}}, label %{{[^,]+$}}
+  */
+  return X <= 0 ? -X : X;
+}
+
+__attribute__((noinline)) int bar(int X) {
+  /* PROF-LABEL: define {{.*}} @bar(
+     PROF: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD2:[0-9]+]]
+  */
+  return X <= 0 ? -X : X;
+}
+
+/*
+PROF: ![[ENT]] = !{!"function_entry_count", i64 2}  
+PROF: ![[PD1]] = !{!"branch_weights", i32 2, i32 2}
+*/




More information about the llvm-commits mailing list