[compiler-rt] r375301 - [profile] Do not cache __llvm_profile_get_filename result

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 18 16:33:40 PDT 2019


Author: vedantk
Date: Fri Oct 18 16:33:40 2019
New Revision: 375301

URL: http://llvm.org/viewvc/llvm-project?rev=375301&view=rev
Log:
[profile] Do not cache __llvm_profile_get_filename result

When the %m filename pattern is used, the filename is unique to each
image, so the cached value is wrong.

It struck me that the full filename isn't something that's recomputed
often, so perhaps it doesn't need to be cached at all. David Li pointed
out we can go further and just hide lprofCurFilename. This may regress
workflows that depend on using the set-filename API to change filenames
across all loaded DSOs, but this is expected to be very rare.

rdar://55137071

Differential Revision: https://reviews.llvm.org/D69137

Added:
    compiler-rt/trunk/test/profile/Inputs/instrprof-get-filename-dso.c
    compiler-rt/trunk/test/profile/instrprof-get-filename-merge-mode.c
Removed:
    compiler-rt/trunk/test/profile/Posix/instrprof-set-filename-shared.test
Modified:
    compiler-rt/trunk/lib/profile/InstrProfiling.h
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=375301&r1=375300&r2=375301&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Fri Oct 18 16:33:40 2019
@@ -155,6 +155,10 @@ int __llvm_orderfile_dump(void);
  *
  * \c Name is not copied, so it must remain valid.  Passing NULL resets the
  * filename logic to the default behaviour.
+ *
+ * Note: There may be multiple copies of the profile runtime (one for each
+ * instrumented image/DSO). This API only modifies the filename within the
+ * copy of the runtime available to the calling image.
  */
 void __llvm_profile_set_filename(const char *Name);
 
@@ -173,6 +177,10 @@ void __llvm_profile_set_filename(const c
  * with the contents of the profiling file. If EnableMerge is zero, the runtime
  * may still merge the data if it would have merged for another reason (for
  * example, because of a %m specifier in the file name).
+ *
+ * Note: There may be multiple copies of the profile runtime (one for each
+ * instrumented image/DSO). This API only modifies the file object within the
+ * copy of the runtime available to the calling image.
  */
 void __llvm_profile_set_file_object(FILE *File, int EnableMerge);
 
@@ -196,7 +204,12 @@ const char *__llvm_profile_get_path_pref
  * \brief Return filename (including path) of the profile data. Note that if the
  * user calls __llvm_profile_set_filename later after invoking this interface,
  * the actual file name may differ from what is returned here.
- * Side-effect: this API call will invoke malloc with dynamic memory allocation.
+ * Side-effect: this API call will invoke malloc with dynamic memory allocation
+ * (the returned pointer must be passed to `free` to avoid a leak).
+ *
+ * Note: There may be multiple copies of the profile runtime (one for each
+ * instrumented image/DSO). This API only retrieves the filename from the copy
+ * of the runtime available to the calling image.
  */
 const char *__llvm_profile_get_filename();
 

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=375301&r1=375300&r2=375301&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Fri Oct 18 16:33:40 2019
@@ -70,7 +70,6 @@ typedef struct lprofFilename {
    * by runtime. */
   unsigned OwnsFilenamePat;
   const char *ProfilePathPrefix;
-  const char *Filename;
   char PidChars[MAX_PID_SIZE];
   char Hostname[COMPILER_RT_MAX_HOSTLEN];
   unsigned NumPids;
@@ -86,8 +85,8 @@ typedef struct lprofFilename {
   ProfileNameSpecifier PNS;
 } lprofFilename;
 
-COMPILER_RT_WEAK lprofFilename lprofCurFilename = {0,   0, 0, 0, {0},
-                                                   {0}, 0, 0, 0, PNS_unknown};
+static lprofFilename lprofCurFilename = {0, 0, 0, {0},        {0},
+                                         0, 0, 0, PNS_unknown};
 
 static int ProfileMergeRequested = 0;
 static int isProfileMergeRequested() { return ProfileMergeRequested; }
@@ -387,8 +386,6 @@ static int parseFilenamePattern(const ch
   /* Clean up cached prefix and filename.  */
   if (lprofCurFilename.ProfilePathPrefix)
     free((void *)lprofCurFilename.ProfilePathPrefix);
-  if (lprofCurFilename.Filename)
-    free((void *)lprofCurFilename.Filename);
 
   if (lprofCurFilename.FilenamePat && lprofCurFilename.OwnsFilenamePat) {
     free((void *)lprofCurFilename.FilenamePat);
@@ -602,9 +599,6 @@ const char *__llvm_profile_get_filename(
   char *FilenameBuf;
   const char *Filename;
 
-  if (lprofCurFilename.Filename)
-    return lprofCurFilename.Filename;
-
   Length = getCurFilenameLength();
   FilenameBuf = (char *)malloc(Length + 1);
   if (!FilenameBuf) {
@@ -615,7 +609,6 @@ const char *__llvm_profile_get_filename(
   if (!Filename)
     return "\0";
 
-  lprofCurFilename.Filename = FilenameBuf;
   return FilenameBuf;
 }
 

Added: compiler-rt/trunk/test/profile/Inputs/instrprof-get-filename-dso.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/Inputs/instrprof-get-filename-dso.c?rev=375301&view=auto
==============================================================================
--- compiler-rt/trunk/test/profile/Inputs/instrprof-get-filename-dso.c (added)
+++ compiler-rt/trunk/test/profile/Inputs/instrprof-get-filename-dso.c Fri Oct 18 16:33:40 2019
@@ -0,0 +1,5 @@
+const char *__llvm_profile_get_filename(void);
+
+const char *get_filename_from_DSO(void) {
+  return __llvm_profile_get_filename();
+}

Removed: compiler-rt/trunk/test/profile/Posix/instrprof-set-filename-shared.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/Posix/instrprof-set-filename-shared.test?rev=375300&view=auto
==============================================================================
--- compiler-rt/trunk/test/profile/Posix/instrprof-set-filename-shared.test (original)
+++ compiler-rt/trunk/test/profile/Posix/instrprof-set-filename-shared.test (removed)
@@ -1,8 +0,0 @@
-# Test that __llvm_profile_set_filename is honored by shared libary too.
-RUN: mkdir -p %t.d
-RUN: %clang_profgen=%t.shared.profraw -fPIC -shared -o %t.d/t.shared %S/../Inputs/instrprof-dlopen-func.c
-RUN: %clang_profgen -DCALL_SHARED -o %t.m -O3 -rpath %t.d %t.d/t.shared %S/../instrprof-set-filename.c
-RUN: %run %t.m %t.main.profraw
-RUN: llvm-profdata show %t.main.profraw | FileCheck --check-prefix=SHARED %s
-
-# SHARED: Total functions: 2

Added: compiler-rt/trunk/test/profile/instrprof-get-filename-merge-mode.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/instrprof-get-filename-merge-mode.c?rev=375301&view=auto
==============================================================================
--- compiler-rt/trunk/test/profile/instrprof-get-filename-merge-mode.c (added)
+++ compiler-rt/trunk/test/profile/instrprof-get-filename-merge-mode.c Fri Oct 18 16:33:40 2019
@@ -0,0 +1,18 @@
+// Test __llvm_profile_get_filename when the on-line merging mode is enabled.
+//
+// RUN: %clang_pgogen -dynamiclib -o %t.dso %p/Inputs/instrprof-get-filename-dso.c
+// RUN: %clang_pgogen -o %t %s %t.dso
+// RUN: env LLVM_PROFILE_FILE="%t-%m.profraw" %run %t
+
+#include <string.h>
+
+const char *__llvm_profile_get_filename(void);
+extern const char *get_filename_from_DSO(void);
+
+int main(int argc, const char *argv[]) {
+  const char *filename1 = __llvm_profile_get_filename();
+  const char *filename2 = get_filename_from_DSO();
+
+  // Exit with code 1 if the two filenames are the same.
+  return strcmp(filename1, filename2) == 0;
+}




More information about the llvm-commits mailing list