[llvm] 9a81a4e - Emit metadata when instr. profiles hash mismatch occurs.
Sriraman Tallam via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 29 13:10:37 PST 2021
Author: Sriraman Tallam
Date: 2021-01-29T12:56:01-08:00
New Revision: 9a81a4ef79cf283fcf55a25a0bea67ba34dc0d53
URL: https://github.com/llvm/llvm-project/commit/9a81a4ef79cf283fcf55a25a0bea67ba34dc0d53
DIFF: https://github.com/llvm/llvm-project/commit/9a81a4ef79cf283fcf55a25a0bea67ba34dc0d53.diff
LOG: Emit metadata when instr. profiles hash mismatch occurs.
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.
Differential Revision: https://reviews.llvm.org/D95495
Added:
llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext
llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll
Modified:
llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
index be6c8c631001..46e5b75a611e 100644
--- a/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
+++ b/llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
@@ -1245,6 +1245,28 @@ void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) {
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";
+ SmallVector<Metadata *, 2> Names;
+ // If this metadata already exists, ignore.
+ auto *Existing = F.getMetadata(LLVMContext::MD_annotation);
+ if (Existing) {
+ MDTuple *Tuple = cast<MDTuple>(Existing);
+ for (auto &N : Tuple->operands()) {
+ if (cast<MDString>(N.get())->getString() == MetadataName)
+ return;
+ Names.push_back(N.get());
+ }
+ }
+
+ 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 +1294,8 @@ bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader, bool &AllZeros,
(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");
diff --git a/llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext b/llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext
new file mode 100644
index 000000000000..f2e6f4d964d2
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/Inputs/hash_mismatch_metadata.proftext
@@ -0,0 +1,17 @@
+:ir
+:entry_first
+foo
+# Func Hash:
+11111
+# Num Counters:
+1
+# Counter Values:
+100
+
+bar
+# Func Hash:
+11111
+# Num Counters:
+1
+# Counter Values:
+100
diff --git a/llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll b/llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll
new file mode 100644
index 000000000000..561865c4e63b
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/hash_mismatch_metadata.ll
@@ -0,0 +1,18 @@
+; 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() !annotation !1 {
+entry:
+ ret void
+}
+
+define void @bar() !annotation !2 {
+entry:
+ ret void
+}
+
+!1 = !{!"fake_metadata"}
+!2 = !{!"instr_prof_hash_mismatch"}
+
+; CHECK-DAG: !{{[0-9]+}} = !{!"fake_metadata", !"instr_prof_hash_mismatch"}
+; CHECK-DAG: !{{[0-9]+}} = !{!"instr_prof_hash_mismatch"}
More information about the llvm-commits
mailing list