[PATCH] D18623: [PGO] refactor PGOFuncName meta data code to be used in clang

Rong Xu via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 30 15:06:39 PDT 2016


xur updated this revision to Diff 52133.
xur added a comment.

Updated the patch with vsk@'s review comments.

Thanks!

-Rong


http://reviews.llvm.org/D18623

Files:
  include/llvm/ProfileData/InstrProf.h
  lib/ProfileData/InstrProf.cpp
  lib/Transforms/Instrumentation/PGOInstrumentation.cpp

Index: lib/Transforms/Instrumentation/PGOInstrumentation.cpp
===================================================================
--- lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -750,14 +750,8 @@
   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;
Index: lib/ProfileData/InstrProf.cpp
===================================================================
--- lib/ProfileData/InstrProf.cpp
+++ lib/ProfileData/InstrProf.cpp
@@ -98,17 +98,16 @@
     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) {
+  // InLTO mode. First check if there is a meta data.
+  if (MDNode *MD = getPGOFuncNameMetadata(F)) {
     StringRef S = cast<MDString>(MD->getOperand(0))->getString();
     return S.str();
   }
 
   // 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 @@
   }
   return true;
 }
+
+bool createPGOFuncNameMetadata(Function &F) {
+  const std::string &FuncName = getPGOFuncName(F);
+  if (FuncName == F.getName())
+    return false;
+
+  LLVMContext &C = F.getContext();
+  MDNode *N = MDNode::get(C, MDString::get(C, FuncName.c_str()));
+  F.setMetadata(getPGOFuncNameMetadataName(), N);
+  return true;
+}
 } // end namespace llvm
Index: include/llvm/ProfileData/InstrProf.h
===================================================================
--- include/llvm/ProfileData/InstrProf.h
+++ include/llvm/ProfileData/InstrProf.h
@@ -19,6 +19,8 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Metadata.h"
 #include "llvm/IR/GlobalValue.h"
 #include "llvm/ProfileData/InstrProfData.inc"
 #include "llvm/ProfileData/ProfileCommon.h"
@@ -245,6 +247,18 @@
                               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
+/// only. Return true if the meta data is written out, and false if not.
+bool createPGOFuncNameMetadata(Function &F);
+
 const std::error_category &instrprof_category();
 
 enum class instrprof_error {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18623.52133.patch
Type: text/x-patch
Size: 3569 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160330/c3c3a342/attachment.bin>


More information about the llvm-commits mailing list