From 8e8fe859e059ce0d0704596c5c6a7ab02f39c5ee Mon Sep 17 00:00:00 2001 From: Rong Xu Date: Fri, 1 Apr 2016 16:43:30 +0000 Subject: [PATCH] [PGO] Refactor PGOFuncName meta data code to be used in clang Refactor the code that gets and creates PGOFuncName meta data so that it can be used in clang's value profile annotation. Differential Revision: http://reviews.llvm.org/D18623 llvm-svn: 265149 --- llvm/include/llvm/ProfileData/InstrProf.h | 14 ++++++++++++++ llvm/lib/ProfileData/InstrProf.cpp | 18 ++++++++++++++---- .../Transforms/Instrumentation/PGOInstrumentation.cpp | 10 ++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h index 545d9e6..e2bc706 100644 --- a/llvm/include/llvm/ProfileData/InstrProf.h +++ b/llvm/include/llvm/ProfileData/InstrProf.h @@ -19,7 +19,9 @@ #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/StringSet.h" +#include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" +#include "llvm/IR/Metadata.h" #include "llvm/ProfileData/InstrProfData.inc" #include "llvm/ProfileData/ProfileCommon.h" #include "llvm/Support/Endian.h" @@ -245,6 +247,18 @@ bool getValueProfDataFromInst(const Instruction &Inst, InstrProfValueData ValueData[], uint32_t &ActualNumValueData, uint64_t &TotalC); +inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; } + +/// Return the PGOFuncName meta data associated with a function. +inline MDNode *getPGOFuncNameMetadata(const Function &F) { + return F.getMetadata(getPGOFuncNameMetadataName()); +} + +/// Create the PGOFuncName meta data if PGOFuncName is different from +/// function's raw name. This should only apply to internal linkage functions +/// declared by users only. +void createPGOFuncNameMetadata(Function &F); + const std::error_category &instrprof_category(); enum class instrprof_error { diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp index 4a023f7..618bceb 100644 --- a/llvm/lib/ProfileData/InstrProf.cpp +++ b/llvm/lib/ProfileData/InstrProf.cpp @@ -98,9 +98,8 @@ std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) { return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(), Version); - // InLTO mode. First check if these is a meta data. - MDNode *MD = F.getMetadata("PGOFuncName"); - if (MD != nullptr) { + // In LTO mode (when InLTO is true), first check if there is a meta data. + if (MDNode *MD = getPGOFuncNameMetadata(F)) { StringRef S = cast(MD->getOperand(0))->getString(); return S.str(); } @@ -108,7 +107,7 @@ std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) { // If there is no meta data, the function must be a global before the value // profile annotation pass. Its current linkage may be internal if it is // internalized in LTO mode. - return getPGOFuncName (F.getName(), GlobalValue::ExternalLinkage, ""); + return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, ""); } StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) { @@ -720,4 +719,15 @@ bool getValueProfDataFromInst(const Instruction &Inst, } return true; } + +void createPGOFuncNameMetadata(Function &F) { + const std::string &FuncName = getPGOFuncName(F); + if (FuncName == F.getName()) + return; + + LLVMContext &C = F.getContext(); + MDNode *N = MDNode::get(C, MDString::get(C, FuncName.c_str())); + F.setMetadata(getPGOFuncNameMetadataName(), N); +} + } // end namespace llvm diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index 50a65b9..5feeccb 100644 --- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -752,14 +752,8 @@ void PGOUseFunc::annotateIndirectCallSites() { if (DisableValueProfiling) return; - // Write out the PGOFuncName if this is different from it's raw name. - // This should only apply to internal linkage functions only. - const std::string &FuncName = getPGOFuncName(F); - if (FuncName != F.getName()) { - LLVMContext &C = F.getContext(); - MDNode *N = MDNode::get(C, MDString::get(C, FuncName.c_str())); - F.setMetadata("PGOFuncName", N); - } + // Create the PGOFuncName meta data. + createPGOFuncNameMetadata(F); unsigned IndirectCallSiteIndex = 0; PGOIndirectCallSiteVisitor ICV; -- 2.7.4