Skip to content

[InstrProf] Do not emit metadata for zero values with zero counts#199380

Merged
boomanaiden154 merged 2 commits into
llvm:mainfrom
boomanaiden154:fix-vp-metadata-zero-value-zero-count
May 26, 2026
Merged

[InstrProf] Do not emit metadata for zero values with zero counts#199380
boomanaiden154 merged 2 commits into
llvm:mainfrom
boomanaiden154:fix-vp-metadata-zero-value-zero-count

Conversation

@boomanaiden154
Copy link
Copy Markdown
Contributor

If we have a indirect call site with a profile that has VP information for said callsite that only contains zero values with zero counts, we would start to emit invalid profile information after 1d14696. VP metadata in this case is at best redundant with BFI. So we restrict metadata emission to only if we have a sufficient number of values for the VP metadata to be valid.

If we have a indirect call site with a profile that has VP information
for said callsite that only contains zero values with zero counts, we
would start to emit invalid profile information after
1d14696. VP metadata in this case is at
best redundant with BFI. So we restrict metadata emission to only if we
have a sufficient number of values for the VP metadata to be valid.
@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented May 23, 2026

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-pgo

Author: Aiden Grossman (boomanaiden154)

Changes

If we have a indirect call site with a profile that has VP information for said callsite that only contains zero values with zero counts, we would start to emit invalid profile information after 1d14696. VP metadata in this case is at best redundant with BFI. So we restrict metadata emission to only if we have a sufficient number of values for the VP metadata to be valid.


Full diff: https://github.com/llvm/llvm-project/pull/199380.diff

3 Files Affected:

  • (modified) llvm/lib/ProfileData/InstrProf.cpp (+5-1)
  • (added) llvm/test/Transforms/PGOProfile/Inputs/indirect-call-vp-zeros.ll (+16)
  • (added) llvm/test/Transforms/PGOProfile/indirect-call-vp-zeros.ll (+14)
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index 4892095ca05a0..d9ae1a6437bb1 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -1393,7 +1393,11 @@ void annotateValueSite(Module &M, Instruction &Inst,
     Vals.push_back(MDHelper.createConstant(
         ConstantInt::get(Type::getInt64Ty(Ctx), ZeroCount)));
   }
-  Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
+  // Only add metadata if we have at least one value. Otherwise we will end
+  // up adding invalid metadata in the case where the profile only has a
+  // zero value with a zero count.
+  if (Vals.size() >= 5)
+    Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
 }
 
 MDNode *mayHaveValueProfileOfKind(const Instruction &Inst,
diff --git a/llvm/test/Transforms/PGOProfile/Inputs/indirect-call-vp-zeros.ll b/llvm/test/Transforms/PGOProfile/Inputs/indirect-call-vp-zeros.ll
new file mode 100644
index 0000000000000..fd0445305882b
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/Inputs/indirect-call-vp-zeros.ll
@@ -0,0 +1,16 @@
+:ir
+test_call
+# Func Hash:
+170957022131388415
+# Num Counters:
+1
+# Counter Values:
+130
+# Num Value Kinds:
+1
+# ValueKind = IPVK_IndirectCallTarget:
+0
+# NumValueSites:
+1
+1
+** External Symbol **:0
diff --git a/llvm/test/Transforms/PGOProfile/indirect-call-vp-zeros.ll b/llvm/test/Transforms/PGOProfile/indirect-call-vp-zeros.ll
new file mode 100644
index 0000000000000..40c9f48f7c382
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/indirect-call-vp-zeros.ll
@@ -0,0 +1,14 @@
+; RUN: llvm-profdata merge %S/Inputs/indirect-call-vp-zeros.ll -o %t.profdata
+; RUN: opt < %s -passes=pgo-instr-use -pgo-test-profile-file=%t.profdata -S
+
+;; Check that if we have a profile with VP metadat that has only zero values
+;; with zero counts, we do not emit invalid VP metadata.
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @test_call(ptr %fptr) {
+entry:
+  call void %fptr()
+  ret void
+}

Copy link
Copy Markdown
Contributor

@snehasish snehasish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

# NumValueSites:
1
1
** External Symbol **:0
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the '**' format valid prof text?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is standard for the External Symbol lines.

;; Check that if we have a profile with VP metadat that has only zero values
;; with zero counts, we do not emit invalid VP metadata.

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we skip the data layout and triple?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this should be unnecessary.

@boomanaiden154 boomanaiden154 enabled auto-merge (squash) May 25, 2026 23:54
@boomanaiden154 boomanaiden154 merged commit 97e7ee2 into llvm:main May 26, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llvm:transforms PGO Profile Guided Optimizations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants