[compiler-rt] r204415 - PGO: Indicate errors in profile runtime API

Duncan P. N. Exon Smith dexonsmith at apple.com
Thu Mar 20 17:27:50 PDT 2014


Author: dexonsmith
Date: Thu Mar 20 19:27:50 2014
New Revision: 204415

URL: http://llvm.org/viewvc/llvm-project?rev=204415&view=rev
Log:
PGO: Indicate errors in profile runtime API

Return 0 for success, non-0 for failure.

Modified:
    compiler-rt/trunk/lib/profile/InstrProfiling.c
    compiler-rt/trunk/lib/profile/InstrProfiling.h
    compiler-rt/trunk/lib/profile/InstrProfilingExtras.c
    compiler-rt/trunk/lib/profile/InstrProfilingExtras.h

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.c?rev=204415&r1=204414&r2=204415&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.c Thu Mar 20 19:27:50 2014
@@ -12,20 +12,27 @@
 
 /* TODO: void __llvm_profile_get_size_for_buffer(void);  */
 
-static void writeFunction(FILE *OutputFile, const __llvm_profile_data *Data) {
+static int writeFunction(FILE *OutputFile, const __llvm_profile_data *Data) {
   /* TODO: Requires libc: break requirement by writing directly to a buffer
    * instead of a FILE stream.
    */
   uint32_t I;
   for (I = 0; I < Data->NameSize; ++I)
-    fputc(Data->Name[I], OutputFile);
-  fprintf(OutputFile, "\n%" PRIu64 "\n%u\n", Data->FuncHash, Data->NumCounters);
+    if (fputc(Data->Name[I], OutputFile) != Data->Name[I])
+      return -1;
+  if (fprintf(OutputFile, "\n%" PRIu64 "\n%u\n", Data->FuncHash,
+              Data->NumCounters) < 0)
+    return -1;
   for (I = 0; I < Data->NumCounters; ++I)
-    fprintf(OutputFile, "%" PRIu64 "\n", Data->Counters[I]);
-  fprintf(OutputFile, "\n");
+    if (fprintf(OutputFile, "%" PRIu64 "\n", Data->Counters[I]) < 0)
+      return -1;
+  if (fprintf(OutputFile, "\n") < 0)
+    return -1;
+
+  return 0;
 }
 
-void __llvm_profile_write_buffer(FILE *OutputFile) {
+int __llvm_profile_write_buffer(FILE *OutputFile) {
   /* TODO: Requires libc: break requirement by taking a char* buffer instead of
    * a FILE stream.
    */
@@ -33,7 +40,10 @@ void __llvm_profile_write_buffer(FILE *O
 
   for (I = __llvm_profile_data_begin(), E = __llvm_profile_data_end();
        I != E; ++I)
-    writeFunction(OutputFile, I);
+    if (writeFunction(OutputFile, I))
+      return -1;
+
+  return 0;
 }
 
 void __llvm_profile_reset_counters(void) {

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=204415&r1=204414&r2=204415&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Thu Mar 20 19:27:50 2014
@@ -49,7 +49,7 @@ typedef struct __llvm_profile_data {
  * It should be changed to take a char* buffer, and write binary data directly
  * to it.
  */
-void __llvm_profile_write_buffer(FILE *OutputFile);
+int __llvm_profile_write_buffer(FILE *OutputFile);
 
 const __llvm_profile_data *__llvm_profile_data_begin(void);
 const __llvm_profile_data *__llvm_profile_data_end(void);

Modified: compiler-rt/trunk/lib/profile/InstrProfilingExtras.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingExtras.c?rev=204415&r1=204414&r2=204415&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingExtras.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingExtras.c Thu Mar 20 19:27:50 2014
@@ -10,19 +10,22 @@
 #include "InstrProfiling.h"
 #include <string.h>
 
-static void __llvm_profile_write_file_with_name(const char *OutputName) {
+static int __llvm_profile_write_file_with_name(const char *OutputName) {
+  int RetVal;
   FILE *OutputFile;
   if (!OutputName || !OutputName[0])
-    return;
+    return -1;
   OutputFile = fopen(OutputName, "w");
-  if (!OutputFile) return;
+  if (!OutputFile)
+    return -1;
 
   /* TODO: mmap file to buffer of size __llvm_profile_get_size_for_buffer() and
    * pass the buffer in, instead of the file.
    */
-  __llvm_profile_write_buffer(OutputFile);
+  RetVal = __llvm_profile_write_buffer(OutputFile);
 
   fclose(OutputFile);
+  return RetVal;
 }
 
 static const char *CurrentFilename = NULL;
@@ -31,9 +34,10 @@ void __llvm_profile_set_filename(const c
 }
 
 int getpid(void);
-void __llvm_profile_write_file(void) {
+int __llvm_profile_write_file(void) {
   char *AllocatedFilename = NULL;
   int I, J;
+  int RetVal;
 
 #define MAX_PID_SIZE 16
   char PidChars[MAX_PID_SIZE] = { 0 };
@@ -54,13 +58,13 @@ void __llvm_profile_write_file(void) {
       if (!NumPids++) {
         PidLength = snprintf(PidChars, MAX_PID_SIZE, "%d", getpid());
         if (PidLength <= 0)
-          return;
+          return -1;
       }
   if (NumPids) {
     // Allocate enough space for the substituted filename.
     AllocatedFilename = (char*)malloc(I + NumPids*(PidLength - 2) + 1);
     if (!AllocatedFilename)
-      return;
+      return -1;
 
     // Construct the new filename.
     for (I = 0, J = 0; Filename[I]; ++I)
@@ -79,18 +83,23 @@ void __llvm_profile_write_file(void) {
   }
 
   // Write the file.
-  __llvm_profile_write_file_with_name(Filename);
+  RetVal = __llvm_profile_write_file_with_name(Filename);
 
   // Free the filename.
   if (AllocatedFilename)
     free(AllocatedFilename);
+
+  return RetVal;
 }
 
+static void writeFileWithoutReturn(void) {
+  __llvm_profile_write_file();
+}
 void __llvm_profile_register_write_file_atexit(void) {
   static int HasBeenRegistered = 0;
 
   if (!HasBeenRegistered) {
     HasBeenRegistered = 1;
-    atexit(__llvm_profile_write_file);
+    atexit(writeFileWithoutReturn);
   }
 }

Modified: compiler-rt/trunk/lib/profile/InstrProfilingExtras.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingExtras.h?rev=204415&r1=204414&r2=204415&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingExtras.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingExtras.h Thu Mar 20 19:27:50 2014
@@ -14,15 +14,18 @@
  * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
  * or if that's not set, \c "default.profdata".
  */
-void __llvm_profile_write_file(void);
+int __llvm_profile_write_file(void);
 
 /*!
  * \brief Set the filename for writing instrumentation data.
  *
  * Sets the filename to be used for subsequent calls to
  * \a __llvm_profile_write_file().
+ *
+ * \c Name is not copied, so it must remain valid.  Passing NULL resets the
+ * filename logic to the default behaviour.
  */
 void __llvm_profile_set_filename(const char *Name);
 
 /*! \brief Register to write instrumentation data to file at exit. */
-void __llvm_profile_register_write_file_atexit(void);
+int __llvm_profile_register_write_file_atexit(void);





More information about the llvm-commits mailing list