Skip to content
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

[memprof] Report an error when buildid and profile do not match #132504

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zcfh
Copy link

@zcfh zcfh commented Mar 22, 2025

Problem

When the build ids of the profile and binary do not match, the error reported by llvm-profdata is no entries in callstack map after symbolization, but the root cause of this problem is the build id mismatch.

Trigger scenario

For example, when performing memprof optimization on clang, rawprofile is collected through ninja clang. In addition to running clang, some other programs will also be executed, and these programs will also generate rawprofile. When no entries in callstack map after symbolization appears during llvm-profdata merge, users may mistakenly think that the instrumentation failed or other reasons, and will not directly realize that the binary and profile do not match.

Changed

Currently, when the build id does not match, an assert error is triggered only in debug mode. Change it to directly return an error when the build id does not match.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the PGO Profile Guided Optimizations label Mar 22, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

@llvm/pr-subscribers-pgo

Author: None (zcfh)

Changes

Problem

When the build ids of the profile and binary do not match, the error reported by llvm-profdata is no entries in callstack map after symbolization, but the root cause of this problem is the build id mismatch.

Trigger scenario

For example, when performing memprof optimization on clang, rawprofile is collected through ninja clang. In addition to running clang, some other programs will also be executed, and these programs will also generate rawprofile. When no entries in callstack map after symbolization appears during llvm-profdata merge, users may mistakenly think that the instrumentation failed or other reasons, and will not directly realize that the binary and profile do not match.

Changed

Currently, when the build id does not match, an assert error is triggered only in debug mode. Change it to directly return an error when the build id does not match.


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

2 Files Affected:

  • (modified) llvm/lib/ProfileData/MemProfReader.cpp (+5-1)
  • (modified) llvm/test/tools/llvm-profdata/memprof-buildid.test (+6)
diff --git a/llvm/lib/ProfileData/MemProfReader.cpp b/llvm/lib/ProfileData/MemProfReader.cpp
index 16502a4f1e8af..c57f9b22273d4 100644
--- a/llvm/lib/ProfileData/MemProfReader.cpp
+++ b/llvm/lib/ProfileData/MemProfReader.cpp
@@ -444,7 +444,11 @@ Error RawMemProfReader::setupForSymbolization() {
       ProfiledTextSegmentEnd = Entry.End;
     }
   }
-  assert(NumMatched != 0 && "No matching executable segments in segment info.");
+  if (NumMatched == 0)
+    return make_error<StringError>(
+        Twine("No matching executable segments found in binary ") +
+            Binary.getBinary()->getFileName(),
+        inconvertibleErrorCode());
   assert((PreferredTextSegmentAddress == 0 ||
           (PreferredTextSegmentAddress == ProfiledTextSegmentStart)) &&
          "Expect text segment address to be 0 or equal to profiled text "
diff --git a/llvm/test/tools/llvm-profdata/memprof-buildid.test b/llvm/test/tools/llvm-profdata/memprof-buildid.test
index a5abe6ea7dcb6..d604d18df61ad 100644
--- a/llvm/test/tools/llvm-profdata/memprof-buildid.test
+++ b/llvm/test/tools/llvm-profdata/memprof-buildid.test
@@ -14,3 +14,9 @@ CHECK: Build ID: [[ID:[[:xdigit:]]+]]
 
 COM: Then match it with the profdata output.
 CHECK-COUNT-1: BuildId: {{.*}}[[ID]]
+
+Test that profile build id does not match build id in the binary
+RUN: not llvm-profdata show --memory %p/Inputs/buildid.memprofraw --profiled-binary %p/Inputs/basic.memprofexe -o - 2>&1 | FileCheck %s -check-prefix=BUILDID-NOT-MATCH
+RUN: not llvm-profdata merge %p/Inputs/buildid.memprofraw %p/Inputs/basic.memprofraw  --profiled-binary %p/Inputs/basic.memprofexe -o %t4.prof 2>&1 | FileCheck %s -check-prefix=BUILDID-NOT-MATCH
+
+BUILDID-NOT-MATCH: No matching executable segments found in binary

Copy link
Contributor

@teresajohnson teresajohnson left a comment

Choose a reason for hiding this comment

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

lgtm - thanks for the fix!

@zcfh zcfh force-pushed the memprof_report_err branch from caaaafc to 12a5154 Compare March 22, 2025 11:10
@zcfh zcfh force-pushed the memprof_report_err branch from f0e4c33 to bf832a1 Compare March 22, 2025 23:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants