This is an archive of the discontinued LLVM Phabricator instance.

[CSSPGO] Split context string to deduplicate function name used in the context.
ClosedPublic

Authored by hoy on Aug 2 2021, 10:58 AM.

Details

Summary

Currently context strings contain a lot of duplicated function names and that significantly increase the profile size. This change split the context into a series of {name, offset, discriminator} tuples so function names used in the context can be replaced by the index into the name table and that significantly reduce the size consumed by context.

A follow-up improvement made in the compiler and profiling tools is to avoid reconstructing full context strings which is time- and memory- consuming. Instead a context vector of StringRef is adopted to represent the full context in all scenarios. As a result, the previous prevalent profile map which was implemented as a StringRef is now engineered as an unordered map keyed by SampleContext. SampleContext is reshaped to using an ArrayRef to represent a full context for CS profile. For non-CS profile, it falls back to use StringRef to represent a contextless function name. Both the ArrayRef and StringRef objects are underpinned by real array and string objects that are stored in producer buffers. For compiler, they are maintained by the sample reader. For llvm-profgen, they are maintained in ProfiledBinary and ProfileGenerator. Full context strings can be generated only in those cases of debugging and printing.

When it comes to profile format, nothing has changed to the text format, though internally CS context is implemented as a vector. Extbinary format is only changed for CS profile, with an additional SecCSNameTable section which stores all full contexts logically in the form of vector<int>, which each element as an offset points to SecNameTable. All occurrences of contexts elsewhere are redirected to using the offset of SecCSNameTable.

Testing
This is no-diff change in terms of code quality and profile content (for text profile).

For our internal large service (aka ads), the profile generation is cut to half, with a 20x smaller string-based extbinary format generated.

The compile time of ads is dropped by 25%.

Diff Detail

Repository
rL LLVM

Event Timeline

wmi created this revision.Aug 2 2021, 10:58 AM
wmi requested review of this revision.Aug 2 2021, 10:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 2 2021, 10:58 AM
hoy commandeered this revision.Aug 17 2021, 8:58 AM
hoy added a reviewer: wmi.
hoy updated this revision to Diff 366914.Aug 17 2021, 8:59 AM

Updating D107299: [SampleFDO] split context string to deduplicate function name used in the context.

hoy retitled this revision from [SampleFDO] split context string to deduplicate function name used in the context. to [CSSPGO] Split context string to deduplicate function name used in the context..Aug 17 2021, 9:28 AM
hoy edited the summary of this revision. (Show Details)
wmi added a comment.Aug 19 2021, 9:59 AM

Thanks Hongtao. The compile time result looks great. The patch is large. Is it possible to split it into smaller ones, SampleContext related change and SecCSNameTable related change for example?

hoy added a comment.Aug 19 2021, 12:25 PM

Thanks Hongtao. The compile time result looks great. The patch is large. Is it possible to split it into smaller ones, SampleContext related change and SecCSNameTable related change for example?

Thanks for the suggestion. The separation may not be complete. The point is that the previous StringRef context is used in both reader/writer, and now we are changing it to be an ArrayRef which relies on an underlying array buffer (i.e., the CSName the reader builds up). Separating SecCSNameTable from this patch will still require the reader to build the underlying CSName table to bridge full string contexts in profile to ArrayRef contexts used elsewhere. Does this sound good to you?

wmi added a comment.Aug 19 2021, 2:28 PM

Thanks Hongtao. The compile time result looks great. The patch is large. Is it possible to split it into smaller ones, SampleContext related change and SecCSNameTable related change for example?

Thanks for the suggestion. The separation may not be complete. The point is that the previous StringRef context is used in both reader/writer, and now we are changing it to be an ArrayRef which relies on an underlying array buffer (i.e., the CSName the reader builds up). Separating SecCSNameTable from this patch will still require the reader to build the underlying CSName table to bridge full string contexts in profile to ArrayRef contexts used elsewhere. Does this sound good to you?

I understand it is not easy to split them into two patches to be committed separately. It needs the bridge you described above which is only useful to make the temporary first part of the patch work and will be immediately thrown away after the rest of the patch is committed. That is unnecessary. Maybe just split the patches for easier review. After all the parts are ok, still commit all the parts in one shot.

hoy updated this revision to Diff 367690.Aug 19 2021, 7:15 PM
This comment was removed by hoy.
hoy updated this revision to Diff 367691.Aug 19 2021, 7:17 PM

Updating D107299: [CSSPGO] Split context string to deduplicate function name used in the context.

hoy added a comment.Aug 19 2021, 7:21 PM

Thanks Hongtao. The compile time result looks great. The patch is large. Is it possible to split it into smaller ones, SampleContext related change and SecCSNameTable related change for example?

Thanks for the suggestion. The separation may not be complete. The point is that the previous StringRef context is used in both reader/writer, and now we are changing it to be an ArrayRef which relies on an underlying array buffer (i.e., the CSName the reader builds up). Separating SecCSNameTable from this patch will still require the reader to build the underlying CSName table to bridge full string contexts in profile to ArrayRef contexts used elsewhere. Does this sound good to you?

I understand it is not easy to split them into two patches to be committed separately. It needs the bridge you described above which is only useful to make the temporary first part of the patch work and will be immediately thrown away after the rest of the patch is committed. That is unnecessary. Maybe just split the patches for easier review. After all the parts are ok, still commit all the parts in one shot.

Sounds good. I separated this patch into three D108433 D108435 D108437. Please let me know if they are good for review.

hoy updated this revision to Diff 369360.Aug 29 2021, 7:09 PM

Updating D107299: [CSSPGO] Split context string to deduplicate function name used in the context.

hoy updated this revision to Diff 369599.Aug 30 2021, 6:57 PM

Updating D107299: [CSSPGO] Split context string to deduplicate function name used in the context.

This revision was not accepted when it landed; it landed in state Needs Review.Aug 30 2021, 8:10 PM
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.