Skip to content

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

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 2 commits into from
Apr 3, 2025

Conversation

zcfh
Copy link
Contributor

@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
@zcfh
Copy link
Contributor Author

zcfh commented Apr 3, 2025

Excuse me, when will this PR be merged? This is my first PR. I am looking forward to it being merged.

@snehasish
Copy link
Contributor

I'll take a look soon.

@snehasish snehasish merged commit 229ca7d into llvm:main Apr 3, 2025
9 of 11 checks passed
Copy link

github-actions bot commented Apr 3, 2025

@zcfh Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@teresajohnson
Copy link
Contributor

Excuse me, when will this PR be merged? This is my first PR. I am looking forward to it being merged.

I didn't see a request for it to be merged - if you don't have commit access please ask for PRs to be merged for you after they are approved.

@zcfh
Copy link
Contributor Author

zcfh commented Apr 11, 2025

Thanks for your reply. I thought it would be merged after all the comments were resolved. I know how to do it next time submit pr(hopefully there will be a next time soon).

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.

4 participants