[compiler-rt] r241824 - Add support for generating profiles in a given directory.

Diego Novillo dnovillo at google.com
Thu Jul 9 10:21:52 PDT 2015


Author: dnovillo
Date: Thu Jul  9 12:21:52 2015
New Revision: 241824

URL: http://llvm.org/viewvc/llvm-project?rev=241824&view=rev
Log:
Add support for generating profiles in a given directory.

When the file is initialized, this patch checks whether the path
specifies a directory. If so, it creates the directory tree before
truncating the file.

Use default.profdata instead of pgo-data for default indexed profile name.

Added:
    compiler-rt/trunk/lib/profile/InstrProfilingUtil.c
    compiler-rt/trunk/lib/profile/InstrProfilingUtil.h
    compiler-rt/trunk/test/profile/Inputs/gcc-flag-compatibility.c
    compiler-rt/trunk/test/profile/gcc-flag-compatibility.test
Modified:
    compiler-rt/trunk/lib/profile/CMakeLists.txt
    compiler-rt/trunk/lib/profile/GCDAProfiling.c
    compiler-rt/trunk/lib/profile/InstrProfiling.h
    compiler-rt/trunk/lib/profile/InstrProfilingFile.c
    compiler-rt/trunk/test/profile/lit.cfg

Modified: compiler-rt/trunk/lib/profile/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/CMakeLists.txt?rev=241824&r1=241823&r2=241824&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/CMakeLists.txt (original)
+++ compiler-rt/trunk/lib/profile/CMakeLists.txt Thu Jul  9 12:21:52 2015
@@ -7,7 +7,8 @@ set(PROFILE_SOURCES
   InstrProfilingFile.c
   InstrProfilingPlatformDarwin.c
   InstrProfilingPlatformOther.c
-  InstrProfilingRuntime.cc)
+  InstrProfilingRuntime.cc
+  InstrProfilingUtil.c)
 
 if(APPLE)
   add_compiler_rt_osx_static_runtime(clang_rt.profile_osx

Modified: compiler-rt/trunk/lib/profile/GCDAProfiling.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/GCDAProfiling.c?rev=241824&r1=241823&r2=241824&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/GCDAProfiling.c (original)
+++ compiler-rt/trunk/lib/profile/GCDAProfiling.c Thu Jul  9 12:21:52 2015
@@ -20,6 +20,8 @@
 |*
 \*===----------------------------------------------------------------------===*/
 
+#include "InstrProfilingUtil.h"
+
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -27,17 +29,9 @@
 #include <string.h>
 #include <sys/mman.h>
 #include <sys/file.h>
-#ifdef _WIN32
-#include <direct.h>
-#endif
 
 #define I386_FREEBSD (defined(__FreeBSD__) && defined(__i386__))
 
-#if !I386_FREEBSD
-#include <sys/stat.h>
-#include <sys/types.h>
-#endif
-
 #if !defined(_MSC_VER) && !I386_FREEBSD
 #include <stdint.h>
 #endif
@@ -52,7 +46,6 @@ typedef unsigned long long uint64_t;
 typedef unsigned char uint8_t;
 typedef unsigned int uint32_t;
 typedef unsigned long long uint64_t;
-int mkdir(const char*, unsigned short);
 #endif
 
 /* #define DEBUG_GCDAPROFILING */
@@ -209,21 +202,6 @@ static char *mangle_filename(const char
   return new_filename;
 }
 
-static void recursive_mkdir(char *path) {
-  int i;
-
-  for (i = 1; path[i] != '\0'; ++i) {
-    if (path[i] != '/') continue;
-    path[i] = '\0';
-#ifdef _WIN32
-    _mkdir(path);
-#else
-    mkdir(path, 0755);  /* Some of these will fail, ignore it. */
-#endif
-    path[i] = '/';
-  }
-}
-
 static int map_file() {
   fseek(output_file, 0L, SEEK_END);
   file_size = ftell(output_file);
@@ -283,7 +261,7 @@ void llvm_gcda_start_file(const char *or
     fd = open(filename, O_RDWR | O_CREAT, 0644);
     if (fd == -1) {
       /* Try creating the directories first then opening the file. */
-      recursive_mkdir(filename);
+      __llvm_profile_recursive_mkdir(filename);
       fd = open(filename, O_RDWR | O_CREAT, 0644);
       if (fd == -1) {
         /* Bah! It's hopeless. */

Modified: compiler-rt/trunk/lib/profile/InstrProfiling.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfiling.h?rev=241824&r1=241823&r2=241824&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfiling.h (original)
+++ compiler-rt/trunk/lib/profile/InstrProfiling.h Thu Jul  9 12:21:52 2015
@@ -64,7 +64,7 @@ uint64_t *__llvm_profile_end_counters(vo
  * or if it hasn't been called, the \c LLVM_PROFILE_FILE environment variable,
  * or if that's not set, the last name given to
  * \a __llvm_profile_override_default_filename(), or if that's not set,
- * \c "default.profdata".
+ * \c "default.profraw".
  */
 int __llvm_profile_write_file(void);
 

Modified: compiler-rt/trunk/lib/profile/InstrProfilingFile.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingFile.c?rev=241824&r1=241823&r2=241824&view=diff
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingFile.c (original)
+++ compiler-rt/trunk/lib/profile/InstrProfilingFile.c Thu Jul  9 12:21:52 2015
@@ -8,6 +8,7 @@
 \*===----------------------------------------------------------------------===*/
 
 #include "InstrProfiling.h"
+#include "InstrProfilingUtil.h"
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -84,6 +85,13 @@ static void truncateCurrentFile(void) {
   if (!Filename || !Filename[0])
     return;
 
+  /* Create the directory holding the file, if needed. */
+  if (strchr(Filename, '/')) {
+    char *Copy = malloc(strlen(Filename) + 1);
+    strcpy(Copy, Filename);
+    __llvm_profile_recursive_mkdir(Copy);
+  }
+
   /* Truncate the file.  Later we'll reopen and append. */
   File = fopen(Filename, "w");
   if (!File)

Added: compiler-rt/trunk/lib/profile/InstrProfilingUtil.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingUtil.c?rev=241824&view=auto
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingUtil.c (added)
+++ compiler-rt/trunk/lib/profile/InstrProfilingUtil.c Thu Jul  9 12:21:52 2015
@@ -0,0 +1,35 @@
+/*===- InstrProfilingUtil.c - Support library for PGO instrumentation -----===*\
+|*
+|*                     The LLVM Compiler Infrastructure
+|*
+|* This file is distributed under the University of Illinois Open Source
+|* License. See LICENSE.TXT for details.
+|*
+\*===----------------------------------------------------------------------===*/
+
+#include "InstrProfilingUtil.h"
+
+#ifdef _WIN32
+#include <direct.h>
+#elif I386_FREEBSD
+int mkdir(const char*, unsigned short);
+#else
+#include <sys/stat.h>
+#include <sys/types.h>
+#endif
+
+__attribute__((visibility("hidden")))
+void __llvm_profile_recursive_mkdir(char *path) {
+  int i;
+
+  for (i = 1; path[i] != '\0'; ++i) {
+    if (path[i] != '/') continue;
+    path[i] = '\0';
+#ifdef _WIN32
+    _mkdir(path);
+#else
+    mkdir(path, 0755);  /* Some of these will fail, ignore it. */
+#endif
+    path[i] = '/';
+  }
+}

Added: compiler-rt/trunk/lib/profile/InstrProfilingUtil.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/profile/InstrProfilingUtil.h?rev=241824&view=auto
==============================================================================
--- compiler-rt/trunk/lib/profile/InstrProfilingUtil.h (added)
+++ compiler-rt/trunk/lib/profile/InstrProfilingUtil.h Thu Jul  9 12:21:52 2015
@@ -0,0 +1,16 @@
+/*===- InstrProfilingUtil.h - Support library for PGO instrumentation -----===*\
+|*
+|*                     The LLVM Compiler Infrastructure
+|*
+|* This file is distributed under the University of Illinois Open Source
+|* License. See LICENSE.TXT for details.
+|*
+\*===----------------------------------------------------------------------===*/
+
+#ifndef PROFILE_INSTRPROFILINGUTIL_H
+#define PROFILE_INSTRPROFILINGUTIL_H
+
+/*! \brief Create a directory tree. */
+void __llvm_profile_recursive_mkdir(char *Pathname);
+
+#endif  /* PROFILE_INSTRPROFILINGUTIL_H */

Added: compiler-rt/trunk/test/profile/Inputs/gcc-flag-compatibility.c
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/Inputs/gcc-flag-compatibility.c?rev=241824&view=auto
==============================================================================
--- compiler-rt/trunk/test/profile/Inputs/gcc-flag-compatibility.c (added)
+++ compiler-rt/trunk/test/profile/Inputs/gcc-flag-compatibility.c Thu Jul  9 12:21:52 2015
@@ -0,0 +1,8 @@
+int X = 0;
+
+int main() {
+  int i;
+  for (i = 0; i < 100; i++)
+    X += i;
+  return 0;
+}

Added: compiler-rt/trunk/test/profile/gcc-flag-compatibility.test
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/gcc-flag-compatibility.test?rev=241824&view=auto
==============================================================================
--- compiler-rt/trunk/test/profile/gcc-flag-compatibility.test (added)
+++ compiler-rt/trunk/test/profile/gcc-flag-compatibility.test Thu Jul  9 12:21:52 2015
@@ -0,0 +1,17 @@
+RUN: mkdir -p %t.d
+RUN: %clang_profgen_gcc=%t.d/d1/d2 -o %t.d/code %S/Inputs/gcc-flag-compatibility.c
+
+# Test that the instrumented code writes to %t.d/d1/d2/default.profraw
+RUN: %run %t.d/code
+RUN: llvm-profdata merge -o %t.profdata %t.d/d1/d2/default.profraw
+
+# Test that we can override the directory and file name with LLVM_PROFILE_FILE.
+RUN: env LLVM_PROFILE_FILE=%t.d/x1/prof.raw %run %t.d/code
+RUN: llvm-profdata merge -o %t.profdata %t.d/x1/prof.raw
+
+# Test that we can specify a directory with -fprofile-use.
+RUN: llvm-profdata merge -o %t.d/default.profdata %t.d/x1/prof.raw
+RUN: %clang_profuse_gcc=%t.d -o %t.d/code %S/Inputs/gcc-flag-compatibility.c
+
+# Test that we can specify a file with -fprofile-use.
+RUN: %clang_profuse_gcc=%t.profdata -o %t.d/code %S/Inputs/gcc-flag-compatibility.c

Modified: compiler-rt/trunk/test/profile/lit.cfg
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/profile/lit.cfg?rev=241824&r1=241823&r2=241824&view=diff
==============================================================================
--- compiler-rt/trunk/test/profile/lit.cfg (original)
+++ compiler-rt/trunk/test/profile/lit.cfg Thu Jul  9 12:21:52 2015
@@ -45,6 +45,8 @@ def build_invocation(compile_flags):
 config.substitutions.append( ("%clang ", build_invocation(clang_cflags)) )
 config.substitutions.append( ("%clang_profgen ", build_invocation(clang_cflags) + " -fprofile-instr-generate ") )
 config.substitutions.append( ("%clang_profuse=", build_invocation(clang_cflags) + " -fprofile-instr-use=") )
+config.substitutions.append( ("%clang_profgen_gcc=", build_invocation(clang_cflags) + " -fprofile-generate=") )
+config.substitutions.append( ("%clang_profuse_gcc=", build_invocation(clang_cflags) + " -fprofile-use=") )
 
 if config.host_os not in ['Darwin', 'FreeBSD', 'Linux']:
   config.unsupported = True





More information about the llvm-commits mailing list