Skip to content

Add hierarchical discriminator multiplicity support#257

Open
kugan-nv wants to merge 1 commit into
google:masterfrom
kugan-nv:hd1
Open

Add hierarchical discriminator multiplicity support#257
kugan-nv wants to merge 1 commit into
google:masterfrom
kugan-nv:hd1

Conversation

@kugan-nv
Copy link
Copy Markdown
Contributor

@kugan-nv kugan-nv commented Nov 14, 2025

Added support for mutiplicity and copy_id for gcc gcov. This should matches the GCC
implementation. I am posting patches to GCC to match this.

This patch also adds support for two-pass aggregation when using hierarchical
discriminator encoding (gcov_version=3). The two-pass approach correctly
handles duplicate samples from code duplication (loop unrolling, etc.).

  • GetBaseDiscriminator() - Extract bits 0-7
  • GetMultiplicity() - Extract bits 8-14 (duplication factor)
  • GetCopyID() - Extract bits 15-25 (unused but reserved)

With the patch we now:

  1. Extract multiplicity and copy_id from discriminator
  2. Multiply sample count by multiplicity
  3. Add samples using copy_id.

Copy link
Copy Markdown
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.

LGTM with one question

Comment thread source_info.h Outdated
}

// Return the full 64-bit offset for 32-bit hierarchical discriminators.
static constexpr uint64_t GenerateFullOffset(uint64_t Offset) {
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.

This is not called from anywhere , is it?

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.

Removed this

@kugan-nv
Copy link
Copy Markdown
Contributor Author

Thanks for the review @erozenfeld.

I have also added support for aggregating samples with same with same [line + base discriminator] as a second pass as GCC doesn't aggregate. This is only done for create_gcov (for gcc) and supported with a flag. This is also enabled only for gcov version 3.

GCC patches are now comitted.

@kugan-nv kugan-nv force-pushed the hd1 branch 3 times, most recently from 03a7e16 to 28f8a93 Compare January 16, 2026 11:40
@kugan-nv
Copy link
Copy Markdown
Contributor Author

kugan-nv commented Feb 4, 2026

@erozenfeld: Ping ?

@snehasish
Copy link
Copy Markdown
Collaborator

Is it possible to add some basic tests for this?

@kugan-nv
Copy link
Copy Markdown
Contributor Author

Updated the patch with a test. Added a binary and perf.data generated on x86 machine with the gcc 16. The test checks for the hierarchical discriminator processing for GCC.

Copy link
Copy Markdown
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 with some minor comments.


for (i = 0; i < 32; i++) {
for (j = 0; j < 4; j++)
sum += a[i + j*4] * i;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

indentation here is misleading

Comment on lines +19 to +21
float vx[2048];
float vy[2048];
float vz[2048];
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think if we use this without initialization then the compiler may optimize it away.

Comment thread symbol_map.h Outdated
// Whether to use discriminator encoding.
ABSL_DECLARE_FLAG(bool, use_discriminator_encoding);

// Whether to use two-pass aggregation for copy_id:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

note that this is gcov only?

Comment thread profile_writer.cc Outdated
Comment on lines +260 to +263
// Pass 2: Collapse copy_ids before writing (SUM aggregation)
if (absl::GetFlag(FLAGS_use_two_pass_aggregation)) {
symbol_map_->CollapseCopyIDs();
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think this is the reason why we have change the profile writer constructors to non-const. Is there a way to preserve the original const contract by refactoring the implementation?

Added support for mutiplicity and copy_id for gcc gcov. This should matches the GCC
implementation. I am posting patches to GCC to match this.

This patch also adds support for two-pass aggregation when using hierarchical
discriminator encoding (gcov_version=3). The two-pass approach correctly
handles duplicate samples from code duplication (loop unrolling, etc.).

  * GetBaseDiscriminator() - Extract bits 0-7
  * GetMultiplicity() - Extract bits 8-14 (duplication factor)
  * GetCopyID() - Extract bits 15-25 (unused but reserved)

With the patch we now:
1. Extract multiplicity and copy_id from discriminator
2. Multiply sample count by multiplicity
4. Add samples  using copy_id.

This was already supported for LLVM.
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