<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, May 8, 2015 at 4:04 PM, Diego Novillo <span dir="ltr"><<a href="mailto:dnovillo@google.com" target="_blank">dnovillo@google.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi dexonsmith, bogner,<br>
<br>
This adds three Function methods to handle function entry counts:<br>
setEntryCount(), hasEntryCount() and getEntryCount().<br>
<br>
Entry counts are stored under the MD_prof metadata node with the name<br>
"function_entry_count". They are unsigned 64 bit values set by profilers<br>
(instrumentation and sample profiler changes coming up).<br>
<br>
I had some internal debate about what to return when<br>
Function::getEntryCount() is called without the function having its<br>
metadata set. I don't want to return a special sentinel value, because<br>
those are typically problematic down the road.<br></blockquote><div><br>What sort of problems?<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">So, I added a Function::hasEntryCount() predicate. If<br>
Function::getEntryCount() is called with no metadata present, it will<br>
cause a compiler assertion.<br></blockquote><div><br>If any caller would need to call hasEntryCount before getEntryCount, perhaps have getEntryCount return Optional<uint64_t> (& returns 'None' when not present)?<br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<a href="http://reviews.llvm.org/D9628" target="_blank">http://reviews.llvm.org/D9628</a><br>
<br>
Files:<br>
  include/llvm/IR/Function.h<br>
  include/llvm/IR/MDBuilder.h<br>
  lib/IR/MDBuilder.cpp<br>
  unittests/IR/MetadataTest.cpp<br>
<br>
Index: include/llvm/IR/Function.h<br>
===================================================================<br>
--- include/llvm/IR/Function.h<br>
+++ include/llvm/IR/Function.h<br>
@@ -23,7 +23,11 @@<br>
 #include "llvm/IR/Attributes.h"<br>
 #include "llvm/IR/BasicBlock.h"<br>
 #include "llvm/IR/CallingConv.h"<br>
+#include "llvm/IR/Constants.h"<br>
 #include "llvm/IR/GlobalObject.h"<br>
+#include "llvm/IR/LLVMContext.h"<br>
+#include "llvm/IR/MDBuilder.h"<br>
+#include "llvm/IR/Metadata.h"<br>
 #include "llvm/Support/Compiler.h"<br>
<br>
 namespace llvm {<br>
@@ -194,6 +198,29 @@<br>
                                  AttributeSet::FunctionIndex, Kind, Value));<br>
   }<br>
<br>
+  /// @brief Set the entry count for this function.<br>
+  void setEntryCount(uint64_t Count) {<br>
+    MDBuilder MDB(getContext());<br>
+    setMetadata(LLVMContext::MD_prof, MDB.createFunctionEntryCount(Count));<br>
+  }<br>
+<br>
+  /// @brief Return true if the function has a known entry count.<br>
+  bool hasEntryCount() const {<br>
+    MDNode *MD = getMetadata(LLVMContext::MD_prof);<br>
+    if (MD && MD->getOperand(0))<br>
+      if (MDString* MDS = dyn_cast<MDString>(MD->getOperand(0)))<br>
+        return MDS->getString().equals("function_entry_count");<br>
+    return false;<br>
+  }<br>
+<br>
+  /// @brief Get the entry count for this function.<br>
+  uint64_t getEntryCount() const {<br>
+    assert(hasEntryCount() && "No entry count available for this function");<br>
+    MDNode *MD = getMetadata(LLVMContext::MD_prof);<br>
+    ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(1));<br>
+    return CI->getValue().getZExtValue();<br>
+  }<br>
+<br>
   /// @brief Return true if the function has the attribute.<br>
   bool hasFnAttribute(Attribute::AttrKind Kind) const {<br>
     return AttributeSets.hasAttribute(AttributeSet::FunctionIndex, Kind);<br>
Index: include/llvm/IR/MDBuilder.h<br>
===================================================================<br>
--- include/llvm/IR/MDBuilder.h<br>
+++ include/llvm/IR/MDBuilder.h<br>
@@ -60,6 +60,9 @@<br>
   /// \brief Return metadata containing a number of branch weights.<br>
   MDNode *createBranchWeights(ArrayRef<uint32_t> Weights);<br>
<br>
+  /// \brief Return metadata containing the entry count for a function.<br>
+  MDNode *createFunctionEntryCount(uint64_t Count);<br>
+<br>
   //===------------------------------------------------------------------===//<br>
   // Range metadata.<br>
   //===------------------------------------------------------------------===//<br>
Index: lib/IR/MDBuilder.cpp<br>
===================================================================<br>
--- lib/IR/MDBuilder.cpp<br>
+++ lib/IR/MDBuilder.cpp<br>
@@ -53,6 +53,16 @@<br>
   return MDNode::get(Context, Vals);<br>
 }<br>
<br>
+MDNode *MDBuilder::createFunctionEntryCount(uint64_t Count) {<br>
+  SmallVector<Metadata *, 2> Vals(2);<br>
+  Vals[0] = createString("function_entry_count");<br>
+<br>
+  Type *Int64Ty = Type::getInt64Ty(Context);<br>
+  Vals[1] = createConstant(ConstantInt::get(Int64Ty, Count));<br>
+<br>
+  return MDNode::get(Context, Vals);<br>
+}<br>
+<br>
 MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {<br>
   assert(Lo.getBitWidth() == Hi.getBitWidth() && "Mismatched bitwidths!");<br>
<br>
Index: unittests/IR/MetadataTest.cpp<br>
===================================================================<br>
--- unittests/IR/MetadataTest.cpp<br>
+++ unittests/IR/MetadataTest.cpp<br>
@@ -2272,4 +2272,12 @@<br>
   EXPECT_FALSE(verifyFunction(*F));<br>
 }<br>
<br>
+TEST_F(FunctionAttachmentTest, EntryCount) {<br>
+  Function *F = getFunction("foo");<br>
+  EXPECT_FALSE(F->hasEntryCount());<br>
+  F->setEntryCount(12304);<br>
+  EXPECT_TRUE(F->hasEntryCount());<br>
+  EXPECT_EQ(12304u, F->getEntryCount());<br>
+}<br>
+<br>
 }<br>
<br>
EMAIL PREFERENCES<br>
  <a href="http://reviews.llvm.org/settings/panel/emailpreferences/" target="_blank">http://reviews.llvm.org/settings/panel/emailpreferences/</a><br>
<br>_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div></div>