Skip to content

Add profile summary information to GCOV#251

Merged
snehasish merged 7 commits intogoogle:masterfrom
dc03-work:gcov-summary-info
Jan 23, 2026
Merged

Add profile summary information to GCOV#251
snehasish merged 7 commits intogoogle:masterfrom
dc03-work:gcov-summary-info

Conversation

@dc03-work
Copy link
Contributor

This patch adds supports for writing out profile summaries and histogram-based percentile information for samples to the GCOV profile, in a similar manner to how this information is written out for LLVM profiles. It also bumps the GCOV version to 3. This is part of the work to add support for the same to GCC.

gcov.cc Outdated
Comment on lines +153 to +154
if (absl::GetFlag(FLAGS_gcov_version) == 2 ||
absl::GetFlag(FLAGS_gcov_version) == 3) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (absl::GetFlag(FLAGS_gcov_version) == 2 ||
absl::GetFlag(FLAGS_gcov_version) == 3) {
if (absl::GetFlag(FLAGS_gcov_version) >= 2) {

gcov.cc Outdated
Comment on lines +234 to +235
if (absl::GetFlag(FLAGS_gcov_version) == 2 ||
absl::GetFlag(FLAGS_gcov_version) == 3) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (absl::GetFlag(FLAGS_gcov_version) == 2 ||
absl::GetFlag(FLAGS_gcov_version) == 3) {
if (absl::GetFlag(FLAGS_gcov_version) >= 2) {

@erozenfeld
Copy link
Contributor

@dc03-work I saw that you made some changes to your GCC patch. Does that require corresponding changes in this PR?

@dc03-work
Copy link
Contributor Author

@dc03-work I saw that you made some changes to your GCC patch. Does that require corresponding changes in this PR?

No, but I will be rewriting the histogram calculation as a set of standalone routines.

@dc03-work
Copy link
Contributor Author

@erozenfeld Hi, I have implemented the calculation as a standalone visitor and verified that it produces the same values as LLVM's implementation. Does this look OK now?

@dc03-work
Copy link
Contributor Author

@erozenfeld Similar to #244, the GCC patch corresponding to this has landed as r16-6349-gdbe8e0efb7b029. Can we please land this if there are no immediate issues as well? This is also required for the AutoFDO flow to work.

hubot pushed a commit to gcc-mirror/gcc that referenced this pull request Dec 23, 2025
This patch aims to implement summary support in auto-profile, similar to
LLVM. The summary information stores various information about the
profile being read such as the number of functions, the maximum sample
count, the total number of samples and so on.

It also adds a section called the "detailed summary" which contains a
histogram-based calculation of the minimum execution count for a sample
needed to belong to a specific percentile of samples. This is used to
decide the hot count threshold (which can be controlled with a command
line parameter). The default is any sample belonging to the 99th percentile
being marked as hot.

This patch requires the changes from google/autofdo#251
to work correctly.

Signed-off-by: Dhruv Chawla <dhruvc@nvidia.com>

gcc/ChangeLog:

	* auto-profile.cc (struct summary_info): New struct.
	(summary_info::read): New function.
	(summary_info::get_threshold_count): Likewise.
	(function_instance::read_function_instance): Read
	afdo_profile_info->sum_max directly from summary info.
	(autofdo_source_profile::read): Set afdo_hot_bb_threshold from
	param_hot_bb_count_ws_permille.
	(read_profile): Call summary_info->read.
	(end_auto_profile): Free afdo_summary_info.
	* gcov-io.h (GCOV_TAG_AFDO_SUMMARY): New define.
Copy link
Contributor

@erozenfeld erozenfeld left a comment

Choose a reason for hiding this comment

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

Looks good, just a couple of minor suggestions.

for (unsigned i = 0; i < num; i++) {
gcov_read_unsigned(); // Cutoff
gcov_read_counter(); // Min count
gcov_read_counter(); // Num cutoffs
Copy link
Contributor

Choose a reason for hiding this comment

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

Should the comment say
// Num counts
?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.


ProfileSummaryInformation info = ProfileSummaryComputer::Compute(
*symbol_map_, {std::begin(ProfileSummaryInformation::default_cutoffs),
std::end(ProfileSummaryInformation::default_cutoffs)});
Copy link
Contributor

@erozenfeld erozenfeld Jan 6, 2026

Choose a reason for hiding this comment

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

The call to ProfileSummaryComputer::Compute can be moved to under
if (absl::GetFlag(FLAGS_gcov_version) >= 3)
check below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@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.

Is it possible to add a test for this?

@dc03-work
Copy link
Contributor Author

Is it possible to add a test for this?

@snehasish I've added the test case. I tried to make it work with the existing binaries / perf data in testdata/ but it seems like there's no binaries with mmap events + build id + debug info, so I hardcoded the values. I hope that is okay.

@dc03-work dc03-work changed the title [RFC] Add profile summary information to GCOV Add profile summary information to GCOV Jan 19, 2026
};
symbol_map.AddSourceCount("boo", boo_stack3, 150, 2);

ProfileSummaryInformation info = ProfileSummaryComputer::Compute(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we test the summary reading and writing by serializing it to a simple profile and then reading it back in?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I will add that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Collaborator

@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, just a couple of optional suggestions. Let me know when you are ready to merge.

profile_writer.h Outdated
StringIndexMap *map_;
};

class ProfileSummaryInformation {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If everything is public maybe this is better off as a struct?


// Make sure the summary that was written out is the same as that which is
// read back in.
EXPECT_EQ(info_1.total_count_, info_2->total_count_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you define operator== for ProfileSummaryInformation and DetailedSummary then I think you can simplify all of these expectations to just EXPECT_THAT(info_1, Eq(info_2)).

This patch adds supports for writing out profile summaries and histogram-based
percentile information for samples to the GCOV profile, in a similar manner
to how this information is written out for LLVM profiles. It also bumps the
GCOV version to 3.
@dc03-work
Copy link
Contributor Author

Addressed comments and rebased on master. Feel free to merge :) Thanks for the reviews!

@snehasish snehasish merged commit e954df8 into google:master Jan 23, 2026
2 of 3 checks passed

// Write out the GCOV_TAG_AFDO_SUMMARY section.
if (absl::GetFlag(FLAGS_gcov_version) >= 3) {
assert(summary != nullptr);
Copy link
Contributor Author

@dc03-work dc03-work Jan 27, 2026

Choose a reason for hiding this comment

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

Looks like I accidentally left an assert in here from the first commit that doesn't work anymore. I think it didn't fail to compile because the assert get preprocessed out - the summary variable doesn't exist anywhere anymore.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Send a PR to cleanup?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR put up as #261.

dc03-work added a commit to dc03-work/autofdo that referenced this pull request Jan 30, 2026
I forgot to remove this assert as it was not compiled in Release mode.
dc03-work added a commit to dc03-work/autofdo that referenced this pull request Feb 5, 2026
I forgot to remove this assert as it was not compiled in Release mode.
snehasish pushed a commit that referenced this pull request Feb 11, 2026
I forgot to remove this assert as it was not compiled in Release mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants