Skip to content

[PseudoProbe] Warn on illegal guid #148564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2025
Merged

[PseudoProbe] Warn on illegal guid #148564

merged 1 commit into from
Jul 21, 2025

Conversation

HaohaiWen
Copy link
Contributor

Check whether guid exists in pseudo probe desc when emitting pseudo
probe.

Check whether guid exists in pseudo probe desc when emitting pseudo
probe.
@llvmbot
Copy link
Member

llvmbot commented Jul 14, 2025

@llvm/pr-subscribers-backend-x86

Author: Haohai Wen (HaohaiWen)

Changes

Check whether guid exists in pseudo probe desc when emitting pseudo
probe.


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

3 Files Affected:

  • (modified) llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp (+32)
  • (modified) llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h (+11)
  • (added) llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll (+46)
diff --git a/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp
index 618deef2a74ea..4bf3bdfb5adfd 100644
--- a/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.cpp
@@ -18,6 +18,11 @@
 #include "llvm/MC/MCPseudoProbe.h"
 #include "llvm/MC/MCStreamer.h"
 
+#ifndef NDEBUG
+#include "llvm/IR/Module.h"
+#include "llvm/Support/WithColor.h"
+#endif
+
 using namespace llvm;
 
 void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
@@ -35,6 +40,9 @@ void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
     uint64_t &CallerGuid = NameGuidMap[Name];
     if (!CallerGuid)
       CallerGuid = Function::getGUIDAssumingExternalLinkage(Name);
+#ifndef NDEBUG
+    verifyGuidExistenceInDesc(CallerGuid, Name);
+#endif
     uint64_t CallerProbeId = PseudoProbeDwarfDiscriminator::extractProbeIndex(
         InlinedAt->getDiscriminator());
     ReversedInlineStack.emplace_back(CallerGuid, CallerProbeId);
@@ -51,4 +59,28 @@ void PseudoProbeHandler::emitPseudoProbe(uint64_t Guid, uint64_t Index,
   SmallVector<InlineSite, 8> InlineStack(llvm::reverse(ReversedInlineStack));
   Asm->OutStreamer->emitPseudoProbe(Guid, Index, Type, Attr, Discriminator,
                                     InlineStack, Asm->CurrentFnSym);
+#ifndef NDEBUG
+  verifyGuidExistenceInDesc(
+      Guid, DebugLoc ? DebugLoc->getSubprogramLinkageName() : "");
+#endif
+}
+
+#ifndef NDEBUG
+void PseudoProbeHandler::verifyGuidExistenceInDesc(uint64_t Guid,
+                                                   StringRef FuncName) {
+  NamedMDNode *Desc = Asm->MF->getFunction().getParent()->getNamedMetadata(
+      PseudoProbeDescMetadataName);
+  assert(Desc && "pseudo probe does not exist");
+
+  // Keep DescGuidSet up to date.
+  for (size_t I = DescGuidSet.size(), E = Desc->getNumOperands(); I != E; ++I) {
+    const auto *MD = cast<MDNode>(Desc->getOperand(I));
+    auto *ID = mdconst::extract<ConstantInt>(MD->getOperand(0));
+    DescGuidSet.insert(ID->getZExtValue());
+  }
+
+  if (!DescGuidSet.contains(Guid))
+    WithColor::warning() << "Guid:" << Guid << " Name:" << FuncName
+                         << " does not exist in pseudo probe desc\n";
 }
+#endif
diff --git a/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h b/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h
index f11b552387501..e950b23bb4280 100644
--- a/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h
+++ b/llvm/lib/CodeGen/AsmPrinter/PseudoProbePrinter.h
@@ -15,6 +15,10 @@
 
 #include "llvm/ADT/DenseMap.h"
 
+#ifndef NDEBUG
+#include "llvm/ADT/DenseSet.h"
+#endif
+
 namespace llvm {
 
 class AsmPrinter;
@@ -26,6 +30,13 @@ class PseudoProbeHandler {
   // Name to GUID map, used as caching/memoization for speed.
   DenseMap<StringRef, uint64_t> NameGuidMap;
 
+#ifndef NDEBUG
+  // All GUID in llvm.pseudo_probe_desc.
+  DenseSet<uint64_t> DescGuidSet;
+
+  void verifyGuidExistenceInDesc(uint64_t Guid, StringRef FuncName);
+#endif
+
 public:
   PseudoProbeHandler(AsmPrinter *A) : Asm(A) {};
 
diff --git a/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll b/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll
new file mode 100644
index 0000000000000..0b1d16d33b5f0
--- /dev/null
+++ b/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll
@@ -0,0 +1,46 @@
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu < %s 2>&1 | FileCheck %s
+; RUN: llc -mtriple=x86_64-unknown-windows-msvc < %s 2>&1 | FileCheck %s
+
+; CHECK: warning: Guid:8314849053352128226 Name:inlinee does not exist in pseudo probe desc
+; CHECK: warning: Guid:6492337042787843907 Name:extract2 does not exist in pseudo probe desc
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+
+define void @extract1() !dbg !8 {
+entry:
+  call void @llvm.pseudoprobe(i64 6028998432455395745, i64 1, i32 0, i64 -1), !dbg !11
+  call void @llvm.pseudoprobe(i64 8314849053352128226, i64 1, i32 0, i64 -1), !dbg !12
+  ret void, !dbg !16
+}
+
+define void @extract2() !dbg !17 {
+entry:
+  call void @llvm.pseudoprobe(i64 6492337042787843907, i64 1, i32 0, i64 -1), !dbg !18
+  ret void, !dbg !18
+}
+
+declare void @llvm.pseudoprobe(i64, i64, i32, i64)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5, !6}
+!llvm.pseudo_probe_desc = !{!7}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang", isOptimized: false, emissionKind: LineTablesOnly, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.c", directory: "/home/foo")
+!2 = !{i32 7, !"Dwarf Version", i32 4}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = !{i32 7, !"uwtable", i32 2}
+!6 = !{i32 7, !"frame-pointer", i32 2}
+!7 = !{i64 6028998432455395745, i64 281479271677951, !"extract1"}
+!8 = distinct !DISubprogram(name: "extract1", scope: !1, file: !1, line: 4, type: !9, scopeLine: 4, spFlags: DISPFlagDefinition, unit: !0)
+!9 = !DISubroutineType(types: !10)
+!10 = !{}
+!11 = !DILocation(line: 5, column: 3, scope: !8)
+!12 = !DILocation(line: 2, column: 1, scope: !13, inlinedAt: !14)
+!13 = distinct !DISubprogram(name: "inlinee", scope: !1, file: !1, line: 1, type: !9, scopeLine: 1, spFlags: DISPFlagDefinition, unit: !0)
+!14 = distinct !DILocation(line: 5, column: 3, scope: !15)
+!15 = !DILexicalBlockFile(scope: !8, file: !1, discriminator: 455082007)
+!16 = !DILocation(line: 6, column: 1, scope: !8)
+!17 = distinct !DISubprogram(name: "extract2", scope: !1, file: !1, line: 8, type: !9, scopeLine: 8, spFlags: DISPFlagDefinition, unit: !0)
+!18 = !DILocation(line: 9, column: 1, scope: !17)

@HaohaiWen HaohaiWen requested review from wlei-llvm and aengelke July 14, 2025 23:56
@@ -26,6 +30,13 @@ class PseudoProbeHandler {
// Name to GUID map, used as caching/memoization for speed.
DenseMap<StringRef, uint64_t> NameGuidMap;

#ifndef NDEBUG
// All GUID in llvm.pseudo_probe_desc.
DenseSet<uint64_t> DescGuidSet;
Copy link
Contributor

Choose a reason for hiding this comment

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

why don't prefer "SparseSet" here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

SparseSet is more suitable for small key universe. It would be very big for GUID.
I think DenseSet is a greater candidate here.

Copy link
Contributor

@williamweixiao williamweixiao left a comment

Choose a reason for hiding this comment

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

LGTM.

@HaohaiWen HaohaiWen merged commit 6193dd5 into llvm:main Jul 21, 2025
12 checks passed
@HaohaiWen HaohaiWen deleted the probe-fix branch July 21, 2025 05:56
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 21, 2025

LLVM Buildbot has detected a new failure on builder fuchsia-x86_64-linux running on fuchsia-debian-64-us-central1-b-1 while building llvm at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/11/builds/19973

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/fuchsia-linux.py ...' (failure)
...
  Passed           : 46650 (97.77%)
  Expectedly Failed:    27 (0.06%)
[1390/1392] Linking CXX executable unittests/Transforms/Scalar/ScalarTests
[1391/1392] Running the LLVM regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/wasm-ld
-- Testing: 60231 tests, 60 workers --
Testing:  0.. 10.. 20.. 30.. 40.
FAIL: LLVM :: CodeGen/X86/pseudo-probe-desc-check.ll (28186 of 60231)
******************** TEST 'LLVM :: CodeGen/X86/pseudo-probe-desc-check.ll' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/llc -mtriple=x86_64-unknown-linux-gnu < /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll 2>&1 | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/FileCheck /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll # RUN: at line 1
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/llc -mtriple=x86_64-unknown-linux-gnu
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/FileCheck /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll
/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll:4:10: error: CHECK: expected string not found in input
; CHECK: warning: Guid:8314849053352128226 Name:inlinee does not exist in pseudo probe desc
         ^
<stdin>:1:1: note: scanning from here
 .file "<stdin>"
^

Input file: <stdin>
Check file: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll

-dump-input=help explains the following input dump.

Input was:
<<<<<<
         1:  .file "<stdin>" 
check:4     X~~~~~~~~~~~~~~~~ error: no match found
         2:  .text 
check:4     ~~~~~~~
         3:  .globl extract1 # -- Begin function extract1 
check:4     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         4:  .p2align 4 
check:4     ~~~~~~~~~~~~
         5:  .type extract1,@function 
check:4     ~~~~~~~~~~~~~~~~~~~~~~~~~~
         6: extract1: # @extract1 
check:4     ~~~~~~~~~~~~~~~~~~~~~~
         .
         .
         .
>>>>>>
Step 7 (check) failure: check (failure)
...
  Passed           : 46650 (97.77%)
  Expectedly Failed:    27 (0.06%)
[1390/1392] Linking CXX executable unittests/Transforms/Scalar/ScalarTests
[1391/1392] Running the LLVM regression tests
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/ld.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using lld-link: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/lld-link
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using ld64.lld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/ld64.lld
llvm-lit: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/utils/lit/lit/llvm/config.py:520: note: using wasm-ld: /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/wasm-ld
-- Testing: 60231 tests, 60 workers --
Testing:  0.. 10.. 20.. 30.. 40.
FAIL: LLVM :: CodeGen/X86/pseudo-probe-desc-check.ll (28186 of 60231)
******************** TEST 'LLVM :: CodeGen/X86/pseudo-probe-desc-check.ll' FAILED ********************
Exit Code: 1

Command Output (stderr):
--
/var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/llc -mtriple=x86_64-unknown-linux-gnu < /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll 2>&1 | /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/FileCheck /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll # RUN: at line 1
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/llc -mtriple=x86_64-unknown-linux-gnu
+ /var/lib/buildbot/fuchsia-x86_64-linux/build/llvm-build-rfu5bpyu/bin/FileCheck /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll
/var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll:4:10: error: CHECK: expected string not found in input
; CHECK: warning: Guid:8314849053352128226 Name:inlinee does not exist in pseudo probe desc
         ^
<stdin>:1:1: note: scanning from here
 .file "<stdin>"
^

Input file: <stdin>
Check file: /var/lib/buildbot/fuchsia-x86_64-linux/llvm-project/llvm/test/CodeGen/X86/pseudo-probe-desc-check.ll

-dump-input=help explains the following input dump.

Input was:
<<<<<<
         1:  .file "<stdin>" 
check:4     X~~~~~~~~~~~~~~~~ error: no match found
         2:  .text 
check:4     ~~~~~~~
         3:  .globl extract1 # -- Begin function extract1 
check:4     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         4:  .p2align 4 
check:4     ~~~~~~~~~~~~
         5:  .type extract1,@function 
check:4     ~~~~~~~~~~~~~~~~~~~~~~~~~~
         6: extract1: # @extract1 
check:4     ~~~~~~~~~~~~~~~~~~~~~~
         .
         .
         .
>>>>>>

@dyung
Copy link
Collaborator

dyung commented Jul 21, 2025

Hi @HaohaiWen, the test you added seems to be failing on this bot that builds without assertions enabled. Can you take a look and revert if you need time to investigate?

https://lab.llvm.org/buildbot/#/builders/202/builds/2396

@HaohaiWen
Copy link
Contributor Author

Hi @HaohaiWen, the test you added seems to be failing on this bot that builds without assertions enabled. Can you take a look and revert if you need time to investigate?

https://lab.llvm.org/buildbot/#/builders/202/builds/2396

Sure. Let me take a look.

@HaohaiWen
Copy link
Contributor Author

Hi @HaohaiWen, the test you added seems to be failing on this bot that builds without assertions enabled. Can you take a look and revert if you need time to investigate?

https://lab.llvm.org/buildbot/#/builders/202/builds/2396

Fix: #149800

HaohaiWen added a commit that referenced this pull request Jul 21, 2025
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
Check whether guid exists in pseudo probe desc when emitting pseudo
probe.
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants