Add support for emitting profiles into a directory other than '.'

Diego Novillo dnovillo at google.com
Thu Jun 25 16:00:05 PDT 2015


This add support for the front end to override the directory where the
profile will be emitted:

    1- If only a directory DIRNAME is given, the compiler will override the
       default profile name with <DIRNAME>/default.profraw.

    2- If the directory is empty, then the existing behaviour is preserved:
       the call to override the default is only emitted if a filename is
       provided.

This supports the upcoming GCC-compatible flag -fprofile-generate in
Clang.


Diego.
-------------- next part --------------
commit f9300c58384d07b1330decf8300786ecaae8b7a7
Author: Diego Novillo <dnovillo at google.com>
Date:   Thu Jun 25 18:23:02 2015 -0400

    Add support for emitting profiles into a directory other than '.'
    
    This add support for the front end to override the directory where the
    profile will be emitted:
    
    1- If only a directory DIRNAME is given, the compiler will override the
       default profile name with <DIRNAME>/default.profraw.
    
    2- If the directory is empty, then the existing behaviour is preserved:
       the call to override the default is only emitted if a filename is
       provided.
    
    This supports the upcoming GCC-compatible flag -fprofile-generate in
    Clang.

diff --git a/include/llvm/Transforms/Instrumentation.h b/include/llvm/Transforms/Instrumentation.h
index 250e389..d3abd2d 100644
--- a/include/llvm/Transforms/Instrumentation.h
+++ b/include/llvm/Transforms/Instrumentation.h
@@ -77,6 +77,9 @@ struct InstrProfOptions {
 
   // Name of the profile file to use as output
   std::string InstrProfileOutput;
+
+  // Name of the directory where the profile file will reside.
+  std::string InstrProfileDir;
 };
 
 /// Insert frontend instrumentation based profiling.
diff --git a/lib/Transforms/Instrumentation/InstrProfiling.cpp b/lib/Transforms/Instrumentation/InstrProfiling.cpp
index 05a9c8a..9582c3f 100644
--- a/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -336,9 +336,10 @@ void InstrProfiling::emitUses() {
 
 void InstrProfiling::emitInitialization() {
   std::string InstrProfileOutput = Options.InstrProfileOutput;
+  std::string InstrProfileDir = Options.InstrProfileDir;
 
   Constant *RegisterF = M->getFunction("__llvm_profile_register_functions");
-  if (!RegisterF && InstrProfileOutput.empty())
+  if (!RegisterF && InstrProfileOutput.empty() && InstrProfileDir.empty())
     return;
 
   // Create the initialization function.
@@ -355,16 +356,31 @@ void InstrProfiling::emitInitialization() {
   IRBuilder<> IRB(BasicBlock::Create(M->getContext(), "", F));
   if (RegisterF)
     IRB.CreateCall(RegisterF, {});
-  if (!InstrProfileOutput.empty()) {
+  if (!InstrProfileOutput.empty() || !InstrProfileDir.empty()) {
     auto *Int8PtrTy = Type::getInt8PtrTy(M->getContext());
     auto *SetNameTy = FunctionType::get(VoidTy, Int8PtrTy, false);
     auto *SetNameF =
         Function::Create(SetNameTy, GlobalValue::ExternalLinkage,
                          "__llvm_profile_override_default_filename", M);
 
-    // Create variable for profile name
+    // If the directory is the only component set, then choose a default
+    // filename. Note that we do not need to handle the opposite case, since
+    // that will simply mean that the file is produced in the current directory.
+    //
+    // FIXME: This setting needs to keep in sync with the default chosen in
+    // compiler-rt/lib/profile/InstrProfilingFile.c:resetFilenameToDefault().
+    // Perhaps there's a better way?
+    if (InstrProfileOutput.empty())
+      InstrProfileOutput = "default.profraw";
+
+    // Create the full path name by joining the directory and file name.
+    std::string Pathname = (InstrProfileDir.empty())
+                               ? InstrProfileOutput
+                               : InstrProfileDir + "/" + InstrProfileOutput;
+
+    // Create variable for profile name.
     Constant *ProfileNameConst =
-        ConstantDataArray::getString(M->getContext(), InstrProfileOutput, true);
+        ConstantDataArray::getString(M->getContext(), Pathname, true);
     GlobalVariable *ProfileName =
         new GlobalVariable(*M, ProfileNameConst->getType(), true,
                            GlobalValue::PrivateLinkage, ProfileNameConst);


More information about the llvm-commits mailing list