Index: lib/IR/Globals.cpp =================================================================== --- lib/IR/Globals.cpp +++ lib/IR/Globals.cpp @@ -24,6 +24,7 @@ #include "llvm/IR/Operator.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/Path.h" #include "LLVMContextImpl.h" using namespace llvm; @@ -31,6 +32,11 @@ // GlobalValue Class //===----------------------------------------------------------------------===// +static cl::opt StaticFuncStripDirNamePrefix( + "static-func-strip-dirname-prefix", cl::init(0), + cl::desc("Strip specified level of directory name from source paths in " + "the global identifier for static functions.")); + // GlobalValue should be a Constant, plus a type, a module, some flags, and an // intrinsic ID. Add an assert to prevent people from accidentally growing // GlobalValue while adding flags. @@ -113,6 +119,24 @@ } } +// Strip NumPrefix level of directory name from PathNameStr. If the number of +// directory separators is less then NumPrefix, strip all the directories and +// leave base file name only. +static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) { + uint32_t Count = NumPrefix; + uint32_t Pos = 0, LastPos = 0; + for (auto & CI : PathNameStr) { + ++Pos; + if (llvm::sys::path::is_separator(CI)) { + LastPos = Pos; + --Count; + } + if (Count == 0) + break; + } + return PathNameStr.substr(LastPos); +} + std::string GlobalValue::getGlobalIdentifier(StringRef Name, GlobalValue::LinkageTypes Linkage, StringRef FileName) { @@ -129,10 +153,13 @@ // Do not include the full path in the file name since there's no guarantee // that it will stay the same, e.g., if the files are checked out from // version control in different locations. - if (FileName.empty()) + if (FileName.empty()) NewName = NewName.insert(0, ":"); - else + else { + if (StaticFuncStripDirNamePrefix != 0) + FileName = stripDirPrefix(FileName, StaticFuncStripDirNamePrefix); NewName = NewName.insert(0, FileName.str() + ":"); + } } return NewName; } Index: lib/ProfileData/InstrProf.cpp =================================================================== --- lib/ProfileData/InstrProf.cpp +++ lib/ProfileData/InstrProf.cpp @@ -29,7 +29,7 @@ using namespace llvm; static cl::opt StaticFuncFullModulePrefix( - "static-func-full-module-prefix", cl::init(false), + "static-func-full-module-prefix", cl::init(true), cl::desc("Use full module build paths in the profile counter names for " "static functions.")); Index: test/Transforms/PGOProfile/statics_counter_naming.ll =================================================================== --- test/Transforms/PGOProfile/statics_counter_naming.ll +++ test/Transforms/PGOProfile/statics_counter_naming.ll @@ -1,5 +1,7 @@ -; RUN: opt %s -pgo-instr-gen -S | FileCheck %s --check-prefix=GEN -; RUN: opt %s -passes=pgo-instr-gen -S | FileCheck %s --check-prefix=GEN +; RUN: opt %s -pgo-instr-gen -static-func-full-module-prefix=false -S | FileCheck %s --check-prefix=GEN +; RUN: opt %s -passes=pgo-instr-gen -static-func-full-module-prefix=false -S | FileCheck %s --check-prefix=GEN +; RUN: opt %s --pgo-instr-gen -static-func-strip-dirname-prefix=1000 -S | FileCheck %s --check-prefix=GEN +; RUN: opt %s -passes=pgo-instr-gen -static-func-strip-dirname-prefix=1000 -S | FileCheck %s --check-prefix=GEN target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"