[PATCH] D95495: Emit metadata if there is a profile hash mismatch

Sriraman Tallam via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 26 19:19:01 PST 2021


tmsriram created this revision.
tmsriram added reviewers: xur, davidxl, efriedma.
Herald added subscribers: wenlei, hiraditya.
tmsriram requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This patch emits "instr_prof_hash_mismatch" function annotation metadata if there is a hash mismatch while applying instrumented profiles.

During the PGO optimized build using instrumented profiles,  if the CFG of the function has changed since generating the profile, a hash mismatch is encountered.  This patch emits this information as annotation metadata.  We plan to use this with Propeller which is done at the machine IR level.  Propeller is usually applied on top of PGO and a hash mismatch during PGO could be used to detect source drift.


https://reviews.llvm.org/D95495

Files:
  llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
  llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext
  llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll


Index: llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll
@@ -0,0 +1,9 @@
+; RUN: llvm-profdata merge %S/Inputs/hash_mismatch_metadata.proftext -o %t.profdata
+; RUN: opt < %s -mtriple=x86_64-linux-gnu -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S | FileCheck %s
+
+define void @foo() {
+entry:
+   ret void
+}
+
+; CHECK: !{{[0-9]+}} = !{!"instr_prof_hash_mismatch"}
Index: llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext
===================================================================
--- /dev/null
+++ llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext
@@ -0,0 +1,9 @@
+:ir
+:entry_first
+foo
+# Func Hash:
+11111
+# Num Counters:
+1
+# Counter Values:
+100
Index: llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1245,6 +1245,21 @@
   llvm_unreachable("Cannot find the unknown count edge");
 }
 
+// Emit function metadata indicating PGO profile mismatch.
+static void annotateFunctionWithHashMismatch(Function &F,
+                                             LLVMContext &ctx) {
+  const char MetadataName[] = "instr_prof_hash_mismatch";
+  // If this metadata already exists, ignore.
+  if (F.getMetadata(LLVMContext::MD_annotation))
+    return;
+
+  SmallVector<Metadata *, 1> Names;
+  MDBuilder MDB(ctx);
+  Names.push_back(MDB.createString(MetadataName));
+  MDNode *MD = MDTuple::get(ctx, Names);
+  F.setMetadata(LLVMContext::MD_annotation, MD);
+}
+
 // Read the profile from ProfileFileName and assign the value to the
 // instrumented BB and the edges. This function also updates ProgramMaxCount.
 // Return true if the profile are successfully read, and false on errors.
@@ -1272,6 +1287,8 @@
              (F.hasComdat() ||
               F.getLinkage() == GlobalValue::AvailableExternallyLinkage));
         LLVM_DEBUG(dbgs() << "hash mismatch (skip=" << SkipWarning << ")");
+        // Emit function metadata indicating PGO profile mismatch.
+        annotateFunctionWithHashMismatch(F, M->getContext());
       }
 
       LLVM_DEBUG(dbgs() << " IsCS=" << IsCS << "\n");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95495.319449.patch
Type: text/x-patch
Size: 2413 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210127/2fccd583/attachment.bin>


More information about the llvm-commits mailing list