[compiler-rt] 5c20d7d - [MemProf] Allow the binary to specify the profile output filename

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 22 08:30:33 PDT 2020


Author: Teresa Johnson
Date: 2020-10-22T08:30:19-07:00
New Revision: 5c20d7db9f2791367b9311130eb44afecb16829c

URL: https://github.com/llvm/llvm-project/commit/5c20d7db9f2791367b9311130eb44afecb16829c
DIFF: https://github.com/llvm/llvm-project/commit/5c20d7db9f2791367b9311130eb44afecb16829c.diff

LOG: [MemProf] Allow the binary to specify the profile output filename

This will allow the output directory to be specified by a build time
option, similar to the directory specified for regular PGO profiles via
-fprofile-generate=. The memory profiling instrumentation pass will
set up the variable. This is the same mechanism used by the PGO
instrumentation and runtime.

Depends on D87120 and D89629.

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

Added: 
    

Modified: 
    compiler-rt/lib/memprof/memprof_interface_internal.h
    compiler-rt/lib/memprof/memprof_rtl.cpp
    compiler-rt/lib/memprof/weak_symbols.txt
    compiler-rt/test/memprof/TestCases/log_path_test.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/memprof/memprof_interface_internal.h b/compiler-rt/lib/memprof/memprof_interface_internal.h
index ea410f5cab06..a51d83a2521c 100644
--- a/compiler-rt/lib/memprof/memprof_interface_internal.h
+++ b/compiler-rt/lib/memprof/memprof_interface_internal.h
@@ -46,6 +46,9 @@ const char *__memprof_default_options();
 SANITIZER_INTERFACE_ATTRIBUTE
 extern uptr __memprof_shadow_memory_dynamic_address;
 
+SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE extern char
+    __memprof_profile_filename[1];
+
 SANITIZER_INTERFACE_ATTRIBUTE void __memprof_load(uptr p);
 SANITIZER_INTERFACE_ATTRIBUTE void __memprof_store(uptr p);
 

diff  --git a/compiler-rt/lib/memprof/memprof_rtl.cpp b/compiler-rt/lib/memprof/memprof_rtl.cpp
index 61270406e48c..93264dd562d0 100644
--- a/compiler-rt/lib/memprof/memprof_rtl.cpp
+++ b/compiler-rt/lib/memprof/memprof_rtl.cpp
@@ -27,6 +27,9 @@
 
 uptr __memprof_shadow_memory_dynamic_address; // Global interface symbol.
 
+// Allow the user to specify a profile output file via the binary.
+SANITIZER_WEAK_ATTRIBUTE char __memprof_profile_filename[1];
+
 namespace __memprof {
 
 static void MemprofDie() {
@@ -172,7 +175,12 @@ static void MemprofInitInternal() {
   AddDieCallback(MemprofDie);
   SetCheckFailedCallback(MemprofCheckFailed);
 
-  __sanitizer_set_report_path(common_flags()->log_path);
+  // Use profile name specified via the binary itself if it exists, and hasn't
+  // been overrriden by a flag at runtime.
+  if (__memprof_profile_filename[0] != 0 && !common_flags()->log_path)
+    __sanitizer_set_report_path(__memprof_profile_filename);
+  else
+    __sanitizer_set_report_path(common_flags()->log_path);
 
   __sanitizer::InitializePlatformEarly();
 

diff  --git a/compiler-rt/lib/memprof/weak_symbols.txt b/compiler-rt/lib/memprof/weak_symbols.txt
index bb2dea85574f..271813612ab6 100644
--- a/compiler-rt/lib/memprof/weak_symbols.txt
+++ b/compiler-rt/lib/memprof/weak_symbols.txt
@@ -1 +1 @@
-___memprof_default_options
+___memprof_default_options __memprof_profile_filename

diff  --git a/compiler-rt/test/memprof/TestCases/log_path_test.cpp b/compiler-rt/test/memprof/TestCases/log_path_test.cpp
index 0b1d4982f11c..fbc05185af23 100644
--- a/compiler-rt/test/memprof/TestCases/log_path_test.cpp
+++ b/compiler-rt/test/memprof/TestCases/log_path_test.cpp
@@ -21,6 +21,24 @@
 // RUN:   not %run %t 2> %t.out
 // RUN: FileCheck %s --check-prefix=CHECK-LONG < %t.out
 
+// Specifying the log name via the __memprof_profile_filename variable.
+// RUN: %clangxx_memprof  %s -o %t -DPROFILE_NAME_VAR="%t.log2"
+// RUN: rm -f %t.log2.*
+// RUN: %run %t 2> %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-GOOD < %t.log2.*
+
+// Check that the log_path option overrides the log name set via the
+// __memprof_profile_filename variable.
+// RUN: rm -f %t.log.*
+// RUN: %env_memprof_opts=log_path=%t.log %run %t 2> %t.out
+// RUN: FileCheck %s --check-prefix=CHECK-GOOD < %t.log.*
+
+#ifdef PROFILE_NAME_VAR
+#define xstr(s) str(s)
+#define str(s) #s
+char __memprof_profile_filename[] = xstr(PROFILE_NAME_VAR);
+#endif
+
 #include <stdlib.h>
 #include <string.h>
 int main(int argc, char **argv) {


        


More information about the llvm-commits mailing list