[PATCH] D36648: [PGO] Add support for profile dumping dir relocation

David Li via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 12 13:56:32 PDT 2017


davidxl created this revision.
Herald added a subscriber: sanjoy.

This patch adds support in profile runtime to allow user change the root directory of the profile. It uses a machanism compatible to libgcc: GCOV_PREFIX and GCOV_PREFIX_STRIP.

It also helps users to easily migrate to use LLVM PGO.


https://reviews.llvm.org/D36648

Files:
  lib/profile/InstrProfilingFile.c
  lib/profile/InstrProfilingUtil.c
  test/profile/instrprof-path.c


Index: test/profile/instrprof-path.c
===================================================================
--- test/profile/instrprof-path.c
+++ test/profile/instrprof-path.c
@@ -1,14 +1,29 @@
 // RUN: %clang_pgogen -O2 -o %t.0 %s
 // RUN: %clang_pgogen=%t.d1 -O2 -o %t.1 %s
 // RUN: %clang_pgogen=%t.d1/%t.d2 -O2 -o %t.2 %s
+// RUN: %clang_pgogen=a/b/c/d -O2 -o %t.3 %s
+// RUN: %clang_pgogen=/a/b/c/d -O2 -o %t.4 %s
 //
 // RUN: %run %t.0  ""
 // RUN: env LLVM_PROFILE_FILE=%t.d1/default.profraw %run %t.0  %t.d1/
 // RUN: env LLVM_PROFILE_FILE=%t.d1/%t.d2/default.profraw %run %t.0 %t.d1/%t.d2/
 // RUN: %run %t.1 %t.d1/
 // RUN: %run %t.2 %t.d1/%t.d2/
 // RUN: %run %t.2 %t.d1/%t.d2/ %t.d1/%t.d2/%t.d3/blah.profraw %t.d1/%t.d2/%t.d3/
 
+// RUN: env GCOV_PREFIX=%t.prefix %run %t.3  %t.prefix/a/b/c/d/
+// RUN: env GCOV_PREFIX=%t.prefix env GCOV_PREFIX_STRIP=1 %run %t.3
+// %t.prefix/b/c/d/ RUN: env GCOV_PREFIX=%t.prefix env GCOV_PREFIX_STRIP=2 %run
+// %t.3  %t.prefix/c/d/ RUN: env GCOV_PREFIX=%t.prefix env GCOV_PREFIX_STRIP=3
+// %run %t.3  %t.prefix/d/
+
+// RUN: env GCOV_PREFIX=%t.prefix %run %t.4  %t.prefix/a/b/c/d/
+// RUN: env GCOV_PREFIX=%t.prefix env GCOV_PREFIX_STRIP=1 %run %t.4
+// %t.prefix/b/c/d/ RUN: env GCOV_PREFIX=%t.prefix env GCOV_PREFIX_STRIP=2 %run
+// %t.4  %t.prefix/c/d/ RUN: env GCOV_PREFIX=%t.prefix env GCOV_PREFIX_STRIP=3
+// %run %t.4  %t.prefix/d/
+
+#include <stdio.h>
 #include <string.h>
 
 const char *__llvm_profile_get_path_prefix();
@@ -24,8 +39,11 @@
   expected = argv[1];
   prefix = __llvm_profile_get_path_prefix();
 
-  if (strcmp(prefix, expected))
+  if (strcmp(prefix, expected)) {
+    fprintf(stderr, "Expected = %s\n", expected);
+    fprintf(stderr, "  Actual = %s\n", prefix);
     return 1;
+  }
 
   if (argc == 4) {
     __llvm_profile_set_filename(argv[2]);
Index: lib/profile/InstrProfilingUtil.c
===================================================================
--- lib/profile/InstrProfilingUtil.c
+++ lib/profile/InstrProfilingUtil.c
@@ -196,7 +196,8 @@
 
   memcpy(Dest, Prefix, PrefixLen);
 
-  if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
+  if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]) &&
+      !IS_DIR_SEPARATOR(StrippedPathStr[0]))
     Dest[PrefixLen++] = DIR_SEPARATOR;
 
   memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
Index: lib/profile/InstrProfilingFile.c
===================================================================
--- lib/profile/InstrProfilingFile.c
+++ lib/profile/InstrProfilingFile.c
@@ -521,15 +521,28 @@
   if (EnvFilenamePat) {
     SelectedPat = EnvFilenamePat;
     PNS = PNS_environment;
+    parseAndSetFilename(SelectedPat, PNS, 0);
   } else if (hasCommandLineOverrider) {
     SelectedPat = INSTR_PROF_PROFILE_NAME_VAR;
     PNS = PNS_command_line;
+
+    size_t PrefixLen;
+    int StripLen;
+    const char *Prefix = lprofGetPathPrefix(&StripLen, &PrefixLen);
+    if (Prefix != NULL) {
+      char *StripPat =
+          COMPILER_RT_ALLOCA(PrefixLen + 1 + strlen(SelectedPat) + 1);
+      lprofApplyPathPrefix(StripPat, SelectedPat, Prefix, PrefixLen, StripLen);
+      SelectedPat = StripPat;
+    }
+
+    parseAndSetFilename(SelectedPat, PNS, Prefix ? 1 : 0);
   } else {
     SelectedPat = NULL;
     PNS = PNS_default;
+    parseAndSetFilename(SelectedPat, PNS, 0);
   }
 
-  parseAndSetFilename(SelectedPat, PNS, 0);
 }
 
 /* This API is directly called by the user application code. It has the


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36648.110856.patch
Type: text/x-patch
Size: 3438 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170812/85acbb21/attachment.bin>


More information about the llvm-commits mailing list