This is an archive of the discontinued LLVM Phabricator instance.

[lto] add getLinkerOpts()
ClosedPublic

Authored by inglorion on Jan 26 2017, 5:39 PM.

Details

Summary

Some compilers, including MSVC and Clang, allow linker options to be specified in source files. In the legacy LTO API, there is a getLinkerOpts() method that returns linker options for the bitcode module being processed. This change adds that method to the new API, so that the COFF linker can get the right linker options when using the new LTO API.

Event Timeline

inglorion created this revision.Jan 26 2017, 5:39 PM
pcc added inline comments.Jan 26 2017, 7:13 PM
lib/LTO/LTO.cpp
257

We deliberately enable lazy loading of metadata here for performance reasons (debug metadata can be expensive to load).

Eventually I think we will want to add the linker options to the bitcode symbol table (see pr27551), so that we will not need to load metadata at all in the COFF linker at symbol resolution time.

In the meantime, I think it would be best to produce the linker options string on demand in getLinkerOpts after calling materializeMetadata. That way, linkers for other object formats don't pay the cost of loading metadata. Can you please also add a FIXME to add this to the symbol table?

277

This loop (and the one below) can be a range for loop.

286

It's probably best to emit the /export flags from here as well, see TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal.

mehdi_amini added inline comments.Jan 26 2017, 8:23 PM
lib/LTO/LTO.cpp
257

I agree.

I'll add that technically if it is really important for you to get cheaply this you could hook it up to the MD lazy-loading scheme to load this without loading all the MDs.

inglorion updated this revision to Diff 86551.Jan 31 2017, 6:40 PM

Thanks for the comments! I've changed it so that metadata is only materialized if someone actually calls getLinkerOpts, and converted the loops to use ranges.

pcc added inline comments.Jan 31 2017, 6:42 PM
lib/LTO/LTO.cpp
311

This will lead to a use after free I think.

pcc added inline comments.Jan 31 2017, 6:44 PM
lib/LTO/LTO.cpp
311

Sorry, no, I didn't notice that this was a field.

I would just remove the field and make this return a std::string.

inglorion added inline comments.Jan 31 2017, 6:45 PM
lib/LTO/LTO.cpp
311

Hmm, I don't think so. The StringRef here is a reference to LinkerOpts, which is a std::string stored on the InputFile. So that should still be valid after this function returns. LOS.str() just calls flush() on the ostream and returns the StringRef. I think this is ok.

inglorion updated this revision to Diff 86553.Jan 31 2017, 6:55 PM

remove LinkerOpts field and use std::string in the return value

inglorion updated this revision to Diff 86749.Feb 1 2017, 4:48 PM

Made getLinkerOpts() also sythesize /export flags for symbols with dllexport linkage, as @pcc suggested

inglorion added inline comments.Feb 1 2017, 4:52 PM
lib/CodeGen/TargetLoweringObjectFileImpl.cpp
922

I moved this into a separate function so that we don't have to do the whole TargetTriple, TargetRegistry, SubtargetFeatures, etc. etc. dance to create a TargetLOweringObjectFileImpl, just to call one method that doesn't need all that information.

pcc added inline comments.Feb 1 2017, 5:08 PM
lib/LTO/LTO.cpp
305–309

Remove braces

314

The Mods list should never be empty, nor should the module's target triple, so you can initialize TT with just Mods[0].Mod->getTargetTriple().

319

You could enumerate SymTab.symbols() instead to avoid needing to befriend InputFile.

inglorion updated this revision to Diff 86757.Feb 1 2017, 5:49 PM

Removed braces, friend declaration, and defensive programming.
Iterate ModuleSymbolTable instead of using symbol_iterator.

pcc accepted this revision.Feb 1 2017, 6:18 PM

LGTM, thanks

This revision is now accepted and ready to land.Feb 1 2017, 6:18 PM
This revision was automatically updated to reflect the committed changes.