This is an archive of the discontinued LLVM Phabricator instance.

[SampleFDO] Add indexing for function profiles so they can be loaded on demand in ExtBinary format
ClosedPublic

Authored by wmi on Oct 7 2019, 3:13 PM.

Details

Summary

Currently for Text, Binary and ExtBinary format profiles, when we compile a module with samplefdo, even if there is no function showing up in the profile, we have to load all the function profiles from the profile input. That is a waste of compile time.

CompactBinary format profile has already had the support of loading function profiles on demand. In this patch, we add the indexing in ExtBinary format too. It will work no matter the sections in ExtBinary format profile are compressed or not. Experiment shows it reduces the time to compile a server benchmark by 30%.

When profile remapping and loading function profiles on demand are both used, extra work needs to be done so that the loading on demand process will take the name remapping into consideration. It will be addressed in a follow-up patch.

Diff Detail

Repository
rL LLVM

Event Timeline

wmi created this revision.Oct 7 2019, 3:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 7 2019, 3:13 PM
wmi added a subscriber: congliu.Oct 7 2019, 3:13 PM

Thanks for making indexing available for extended binary format. We've recently adding indexing to binary format as well in an internal patch, and also observed similar (~30%) build time reduction for some large services.

We also have have the need to differentiate dead/cold symbols vs new symbols, so symbol list in extended binary format is useful to us as well - will try it out. (I noticed that a small change in profile generation tool (the equivalent of https://github.com/google/autofdo) is needed to populate that list though, it'd be nice if these are all part of LLVM)

Left some comments inline. In addition, I think llvm-profdata/Inputs/sample-profile.proftext need to be updated as well to include the new section for roundtrip.test

llvm/include/llvm/ProfileData/SampleProfWriter.h
206

With the addition of offset table at the end of sections but in the middle of section header, I found the name SectionLayout a bit confusing. This array actually represents the layout/order of section header (e.g. the reader order), but not the section (payload) layout (the writer order). I guess renaming it SectionHdrLayout or something alike may help..

Besides, some comments explaining why SecFuncOffsetTable need to be in the middle could help too, it looks like a hidden trick until I saw the comments in SampleProfileReaderExtBinaryBase::getFileSize().

llvm/lib/ProfileData/SampleProfReader.cpp
547–548

nit: similar to line 539, we could assert the random access here never attempts to touch anything beyond Size?

wmi marked 2 inline comments as done.Oct 8 2019, 3:12 PM

Thanks for making indexing available for extended binary format. We've recently adding indexing to binary format as well in an internal patch, and also observed similar (~30%) build time reduction for some large services.

Thanks for providing the data!

We also have have the need to differentiate dead/cold symbols vs new symbols, so symbol list in extended binary format is useful to us as well - will try it out. (I noticed that a small change in profile generation tool (the equivalent of https://github.com/google/autofdo) is needed to populate that list though, it'd be nice if these are all part of LLVM)

You can use llvm-profdata to attach the profile symbol list section. Symbol list can be provided to llvm-profdata in a plain text file.

Left some comments inline. In addition, I think llvm-profdata/Inputs/sample-profile.proftext need to be updated as well to include the new section for roundtrip.test

The roundtrip test doesn't contain any golden file in extbinary format. It generates the extbinary format profile on the fly. The input sample-profile.proftext doesn't need change.

llvm/include/llvm/ProfileData/SampleProfWriter.h
206

Indeed SectionHdrLayout is a better name. I changed it and added comment to explain why SecFuncOffsetTable is after SecLBRProfile in the profile but is before SecLBRProfile in the section header table.

llvm/lib/ProfileData/SampleProfReader.cpp
547–548

assertion added.

wmi updated this revision to Diff 223953.Oct 8 2019, 3:14 PM

Address Wenlei's comment.

wenlei accepted this revision.Oct 8 2019, 3:39 PM

LGTM. Thanks!

Symbol list can be provided to llvm-profdata in a plain text file.

I thought it's more convenient to have PSL auto-populated by the tool that generates AutoFDO profile, or is there any reason for not using auto-generated PSL, and instead providing a plain text file as side input?

This revision is now accepted and ready to land.Oct 8 2019, 3:39 PM
davidxl added inline comments.Oct 8 2019, 3:50 PM
llvm/include/llvm/ProfileData/SampleProfReader.h
554

Nit: collectFuncsFrom(const Module &M)

llvm/lib/ProfileData/SampleProfReader.cpp
507

Skip declarations?

533

End = Data + Size

536

It is more readable if readFuncProfile is taking the pointer to the data address:

readFuncProfile(&Data);
wmi added a comment.Oct 9 2019, 10:54 AM

LGTM. Thanks!

Symbol list can be provided to llvm-profdata in a plain text file.

I thought it's more convenient to have PSL auto-populated by the tool that generates AutoFDO profile, or is there any reason for not using auto-generated PSL, and instead providing a plain text file as side input?

Putting the list in a plain text file gives more flexibility so user can order the list and strip some of them. You are right it is convenient for create_llvm_prof to get the list from binary directly. I just wanted to point out there is an existing alternative support before we port the support of create_llvm_prof to github.

wmi marked 4 inline comments as done.Oct 9 2019, 10:56 AM
wmi added inline comments.
llvm/include/llvm/ProfileData/SampleProfReader.h
554

Fixed

llvm/lib/ProfileData/SampleProfReader.cpp
507

Fixed

533

It is set in the parent function: readOneSection.

536

Fixed.

wmi updated this revision to Diff 224104.Oct 9 2019, 11:00 AM

Address David's comment.

davidxl added inline comments.Oct 9 2019, 11:04 AM
llvm/lib/ProfileData/SampleProfReader.cpp
533

What I meant is to define a local variable 'End' and use it instead of of Start. It makes code slightly more readable.

wmi marked an inline comment as done.Oct 9 2019, 11:27 AM
wmi added inline comments.
llvm/lib/ProfileData/SampleProfReader.cpp
533

I see. Actually the class member "End" is also set to be end of section in readOneSection, and it is a little confusing to have two "End" variables, so I clean it further to remove the param "Size" and use class member "End" instead.

wmi updated this revision to Diff 224113.Oct 9 2019, 11:29 AM

Address David's comment.

davidxl accepted this revision.Oct 9 2019, 11:33 AM

lgtm

This revision was automatically updated to reflect the committed changes.