This is an archive of the discontinued LLVM Phabricator instance.

[lib/LTO] Fix the interaction between linker redefined symbols and ThinLTO
ClosedPublic

Authored by davide on Jul 6 2017, 9:17 AM.

Details

Summary

This is the same of https://reviews.llvm.org/rL304719 but for ThinLTO.
The substantial difference is that in this case we don't have whole visibility, just the summary.
In the LTO case, when we got the resolution for the input file we could just see if the linker told us whether a symbol was linker redefined (using --wrap or --defsym) and switch the linkage directly for the GV.

Here, we have the summary. So, we record that the linkage changed from <whatever it was> to $weakany to prevent IPOs across this symbol boundaries and actually just switch the linkage at FunctionImport time.

This patch should also fix the lld bits (as all the scaffolding for communicating if a symbol is linker redefined should be there & should be the same), but I'll make sure to add some tests there as well.

Fixes https://bugs.llvm.org/show_bug.cgi?id=33192

Diff Detail

Repository
rL LLVM

Event Timeline

davide created this revision.Jul 6 2017, 9:17 AM
tejohnson added inline comments.Jul 6 2017, 9:49 AM
lib/Transforms/IPO/FunctionImport.cpp
544 ↗(On Diff #105448)

The comment doesn't seem to match the check. And don't we want to be able to convert renamed externally visible symbols to WeakAny (like in the attached test case)?

560 ↗(On Diff #105448)

Will this do the wrong thing (i.e. would we potentially convert something to a declaration when we wanted to convert it to WeakAny (the routine was previously only used to convert to available_externally).

davide planned changes to this revision.Jul 6 2017, 10:13 AM

This has a known problem, I'll upload a new version in a bit.

davide updated this revision to Diff 105477.Jul 6 2017, 10:52 AM

Actually, I now think I understand this slightly better.
As this was handling only the update of linkage to available_externally, I think the easiest solution is that of moving the early return later and in case the original and the linkage in the summary don't match, and the one in the summary is weak, just switch to it.
I think this is safe, but maybe we might want to do the switch only for some kind of (original) linkages, maybe.

tejohnson added inline comments.Jul 6 2017, 11:34 AM
lib/LTO/LTO.cpp
673 ↗(On Diff #105477)

Suggest doing this in thinLTOResolveWeakForLinkerGUID/thinLTOResolveWeakForLinkerInIndex for 2 reasons:

  1. avoids extra combined index hash lookup to find the summary
  2. since we are doing linkage update in the corresponding LTO backend routine hinLTOResolveWeakForLinkerModule

The names of all 3 of these routines should probably be changed to reflect the additional scope (not just handling linker resolution of weak symbols anymore). Not sure of a good name offhand though...

lib/Transforms/IPO/FunctionImport.cpp
555 ↗(On Diff #105477)

Actually we may be switching to either available_externally (for the non-prevailing copies) or converting linkonce->weak for the prevailing copy (sorry, my earlier comment was misleading). So probably change comment to reflect that we are doing weak for linker resolution below.

davide added inline comments.Jul 6 2017, 11:41 AM
lib/LTO/LTO.cpp
673 ↗(On Diff #105477)

In order to find which symbols are LinkerRedefined we need to access the resolution vector, so if we move this logic to thinLTOResolveWeakForLinkerGUID we need to add an extra symbol walk for all the input files, I guess, which I'm not entirely sure it will be faster than having extra summary lookups.

I'll leave the call to you, but this is something I wanted to point out anyway :)

lib/Transforms/IPO/FunctionImport.cpp
555 ↗(On Diff #105477)

I'll remove this.

pcc added inline comments.Jul 6 2017, 11:47 AM
lib/LTO/LTO.cpp
673 ↗(On Diff #105477)

You could also reduce the number of summary lookups by changing the code to look like this:

if (Res.LinkerRedefined)
  if (auto S = ...)
    S->setLinkage(...);
tejohnson added inline comments.Jul 6 2017, 11:51 AM
lib/LTO/LTO.cpp
673 ↗(On Diff #105477)

You're right, forgot we don't have that flag handy later. Maybe ok for now then, but we may want to reevaluate at some point - I've seen the index lookups get hot before for really large apps. I guess we could stash in the PrevailingModuleForGUID map (make the result a tuple), and then change isPrevailing to return this flag. But perhaps not critical for now.

If it is left as is here, then in thinLTOResolveWeakForLinkerModule it should probably be documented where the new linkage is set up in the summary (since not in the corresponding "InIndex" version of that routine).

tejohnson added inline comments.Jul 6 2017, 11:53 AM
lib/LTO/LTO.cpp
673 ↗(On Diff #105477)

Good point Peter. The LinkerRedefined should be uncommon, so let's just reorder the checks as you suggested.

davide updated this revision to Diff 105501.Jul 6 2017, 12:01 PM

Thanks for the comments, Peter & Teresa. New version available.

tejohnson accepted this revision.Jul 6 2017, 12:19 PM

Lgtm with one request below.

lib/Transforms/IPO/FunctionImport.cpp
549 ↗(On Diff #105501)

Please add comment here indicating where this is set on the summary, since it isn't in the corresponding InIndex version of this function.

This revision is now accepted and ready to land.Jul 6 2017, 12:19 PM
This revision was automatically updated to reflect the committed changes.