This is an archive of the discontinued LLVM Phabricator instance.

Properly add out-of-module functions to the import list
ClosedPublic

Authored by lxfind on May 4 2020, 5:04 PM.

Details

Summary

This patch addresses two issues related to adding inline functions to the import list while recursively going through the profiling data.

  1. For callsite samples, only add an inlined function to the import list if it's from outside of the module (i.e. only has a declaration inside the module).
  2. For body samples, add each target function to the import list if it's from outside of the module (i.e. only has a declaration inside the module). Previously we were using getSubProgram() to check whether it has dbg info, which is inaccurate. This fix properly add imports and could improve the quality of the pass.

Added a few changes to the test to catch these cases.

Diff Detail

Event Timeline

lxfind created this revision.May 4 2020, 5:04 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2020, 5:05 PM
lxfind updated this revision to Diff 261974.May 4 2020, 5:39 PM

Keep test for the original purpose by forcing function out of module

wenlei added inline comments.May 4 2020, 5:57 PM
llvm/include/llvm/ProfileData/SampleProf.h
531

I think we need F->isDeclaration() here, getSubprogram() tells us whether debug metadata (!dbg) is available for this function.

lxfind marked an inline comment as done.May 4 2020, 7:49 PM
lxfind added inline comments.
llvm/include/llvm/ProfileData/SampleProf.h
543

@wenlei Do you know why we are checking for getSubprogram() here?

wenlei added inline comments.May 4 2020, 9:09 PM
llvm/include/llvm/ProfileData/SampleProf.h
543

I didn't notice this, but it looks fishy to me. We could have declaration with !dbg (see metadata-function-dbg.ll) which we should still import. We could also have definition without !dbg.. I'm not sure why we check getSubprogram() - I guess we can verify by trying to contrive a case that this logic would miss for importing.. @wmi is this intentional?

lxfind marked an inline comment as done.May 5 2020, 1:07 PM
lxfind added inline comments.
llvm/include/llvm/ProfileData/SampleProf.h
543

@wenlei I was able to reproduce both of the cases you described, that a function isn't added to import list when it doesn't have !dbg annotation, indicating that we shouldn't rely on getSubProgram here. This is probably also intended to mimic the behavior of isDeclaration.
I will fix this part as well, add a test, while waiting for @wmi to confirm.

lxfind updated this revision to Diff 262266.May 5 2020, 5:10 PM

Also use isDeclaration for body samples, add test case

wmi added inline comments.May 5 2020, 8:43 PM
llvm/include/llvm/ProfileData/SampleProf.h
531

Indeed when F->isDeclaration() is true, getSubprogram could also be true. That will potentially miss importing.

I am not very sure about the original intention of using getSubprogram. I am thinking about the case when a comdat function doesn't have debug information in current module, is it possible to import another version with debug information from other module? Need Teresa's opinion on it. I will ask her about it.

wmi added inline comments.May 5 2020, 10:19 PM
llvm/include/llvm/ProfileData/SampleProf.h
531

I checked with Teresa. Currently if there has been a function definition in the module, no import of the same function from other module will be done, no matter how the debug information looks like. So using getSubProgram here is not intended to import comdat function with debug information. I agree we should change it to isDeclaration.

542–543

We can create a lambda so the code snippet here and above can share it.

lxfind updated this revision to Diff 262293.May 5 2020, 10:55 PM

use lambda to reuse code

hoyFB added a subscriber: hoyFB.May 5 2020, 11:49 PM
hoyFB added inline comments.
llvm/include/llvm/ProfileData/SampleProf.h
531

Should isDeclarationForLinker be checked instead? I thought when a function is imported, it has definition in the current module and it's not longer a declaration. An imported function is a definition in the module it's imported into but since it is also a isDeclarationForLinker (or hasAvailableExternallyLinkage) it won't be emitted into the current object file.

llvm/test/Transforms/SampleProfile/function_metadata.ll
56–57

Nit: remove !11 or replace it with !13?

tejohnson added inline comments.May 6 2020, 8:40 AM
llvm/include/llvm/ProfileData/SampleProf.h
531

IIUC this code is deciding what to tell ThinLTO to import. So it makes sense to see if it is an actual declaration at this point. If there is a def in the module (regardless of linkage type), when we get to the thin link we won't bother importing the callee.

wenlei accepted this revision.May 6 2020, 9:13 AM

Thanks! LGTM other than the test tweak hoy pointed out. You may also want to update the summary to reflect the additional fix.

Since most of the time we actually have !dbg for declarations, the additional fix would enable proper profile based importing/inlining for cross-module callee's that were not previously inlined. This could have a positive performance impact (especially for multiple iterations of AutoFDO) because we were unintentionally restricted from growing cross-module inlining based on profile beyond what can be replayed by sample loader (which was from first build without profile).

llvm/include/llvm/ProfileData/SampleProf.h
531

I thought when a function is imported, it has definition in the current module and it's not longer a declaration.

This GUID list here is collected only in pre-link to guide importing for ThinLink, so at this point, isDeclaration is accurate.

This revision is now accepted and ready to land.May 6 2020, 9:13 AM
hoyFB added inline comments.May 6 2020, 9:41 AM
llvm/include/llvm/ProfileData/SampleProf.h
531

Yeah, that's true. The code is only run in ThinLTOPreLink, so checking isDeclaration is fine here.

lxfind updated this revision to Diff 262509.May 6 2020, 5:33 PM

Address test feedback

lxfind retitled this revision from Don't add function to import list if it's defined in the same module to Properly add out-of-module functions to the import list.May 6 2020, 5:37 PM
lxfind edited the summary of this revision. (Show Details)
hoyFB accepted this revision.May 6 2020, 6:02 PM

LGTM.

This revision was automatically updated to reflect the committed changes.