[PATCH] D80230: [ProfileSummary] Refactor getFromMD to prepare for another optional field. NFC.

Hiroshi Yamauchi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 19 12:37:07 PDT 2020


yamauchi created this revision.
yamauchi added a reviewer: davidxl.
Herald added subscribers: hiraditya, eraman.
Herald added a project: LLVM.
yamauchi added a parent revision: D79951: [ProfileSummary] Add the PartialProfileRatio field in ProfileSummary metadata..

Rename 'i' to 'I'.
Factor out the optional field handling to getOptionalVal().
Split out of D79951 <https://reviews.llvm.org/D79951>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D80230

Files:
  llvm/lib/IR/ProfileSummary.cpp


Index: llvm/lib/IR/ProfileSummary.cpp
===================================================================
--- llvm/lib/IR/ProfileSummary.cpp
+++ llvm/lib/IR/ProfileSummary.cpp
@@ -145,13 +145,29 @@
   return true;
 }
 
+// Get the value of an optional field. Increment 'Idx' if it was present. Return
+// true if we can move onto the next field.
+template <typename ValueType>
+static bool getOptionalVal(MDTuple *Tuple, unsigned &Idx, const char *Key,
+                           ValueType &Value) {
+  if (getVal(dyn_cast<MDTuple>(Tuple->getOperand(Idx)), Key, Value)) {
+    Idx++;
+    // Need to make sure when the key is present, we won't step over the bound
+    // of Tuple operand array. Since (non-optional) DetailedSummary always comes
+    // last, the next entry in the tuple operand array must exist.
+    return Idx < Tuple->getNumOperands();
+  }
+  // It was absent, keep going.
+  return true;
+}
+
 ProfileSummary *ProfileSummary::getFromMD(Metadata *MD) {
   MDTuple *Tuple = dyn_cast_or_null<MDTuple>(MD);
-  if (!Tuple && (Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 9))
+  if (!Tuple || Tuple->getNumOperands() < 8 || Tuple->getNumOperands() > 9)
     return nullptr;
 
-  int i = 0;
-  auto &FormatMD = Tuple->getOperand(i++);
+  unsigned I = 0;
+  auto &FormatMD = Tuple->getOperand(I++);
   ProfileSummary::Kind SummaryKind;
   if (isKeyValuePair(dyn_cast_or_null<MDTuple>(FormatMD), "ProfileFormat",
                      "SampleProfile"))
@@ -167,39 +183,31 @@
 
   uint64_t NumCounts, TotalCount, NumFunctions, MaxFunctionCount, MaxCount,
       MaxInternalCount;
-  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "TotalCount",
+  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "TotalCount",
               TotalCount))
     return nullptr;
-  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxCount", MaxCount))
+  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxCount", MaxCount))
     return nullptr;
-  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxInternalCount",
+  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxInternalCount",
               MaxInternalCount))
     return nullptr;
-  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "MaxFunctionCount",
+  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "MaxFunctionCount",
               MaxFunctionCount))
     return nullptr;
-  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "NumCounts",
+  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumCounts",
               NumCounts))
     return nullptr;
-  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(i++)), "NumFunctions",
+  if (!getVal(dyn_cast<MDTuple>(Tuple->getOperand(I++)), "NumFunctions",
               NumFunctions))
     return nullptr;
-  // Initialize IsPartialProfile because the field is optional.
-  uint64_t IsPartialProfile = 0;
 
-  // IsPartialProfile is optional so it doesn't matter even if the next val
-  // is not IsPartialProfile.
-  if (getVal(dyn_cast<MDTuple>(Tuple->getOperand(i)), "IsPartialProfile",
-             IsPartialProfile)) {
-    // Need to make sure when IsPartialProfile is presented, we won't step
-    // over the bound of Tuple operand array.
-    if (Tuple->getNumOperands() < 9)
-      return nullptr;
-    i++;
-  }
+  // Optional fields. Need to initialize because the fields are optional.
+  uint64_t IsPartialProfile = 0;
+  if (!getOptionalVal(Tuple, I, "IsPartialProfile", IsPartialProfile))
+    return nullptr;
 
   SummaryEntryVector Summary;
-  if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(i++)), Summary))
+  if (!getSummaryFromMD(dyn_cast<MDTuple>(Tuple->getOperand(I++)), Summary))
     return nullptr;
   return new ProfileSummary(SummaryKind, std::move(Summary), TotalCount,
                             MaxCount, MaxInternalCount, MaxFunctionCount,


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80230.264991.patch
Type: text/x-patch
Size: 3872 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200519/29b9ac89/attachment-0001.bin>


More information about the llvm-commits mailing list