This is an archive of the discontinued LLVM Phabricator instance.

[Darwin] Do not error on '-lto_library' option
ClosedPublic

Authored by modocache on Jun 10 2018, 11:33 AM.

Details

Summary

Any invocation of clang -fuse-ld=lld that results in a link command
on a macOS host currently fails, because the Darwin lld driver does not
recognize the -lto_library option that Clang passes it. Fix the error
by having the Darwin driver ignore the option.

The Clang driver's macOS toolchain is written such that it will always
pass the -lto_library option to the linker invocation on a macOS host.
And although the DarwinLdDriver is written to ignore any unknown arguments,
because -lto_library begins with -l, the DarwinLdDriver interprets it
as a library search command, for a library named "to_library". When the
DarwinLdDriver is unable to find a library specified via -l, it exits
with a hard error. This causes any invocation of clang -fuse-ld=lld
that results in a link command on a macOS host to fail with an error.

To fix the issue, I considered two alternatives:

  1. Modify the Clang Darwin toolchain to only pass -lto_library if lld is *not* being used. lld doesn't support LTO on Darwin anyway, so it can't use the option. However, I opted against this because, if and when lld *does* support LTO on Darwin, I'll have to make another commit to Clang in order to get it to pass the option to lld again.
  2. Modify the Darwin lld driver to ignore the -lto_library option. Just in case users may take this to mean LTO is supported, I also added a warning. If and when lld supports LTO on Darwin, the same commit that adds support for this option can remove the warning.

Option (2) seemed better to me, and is the rationale behind this commit.

Test Plan: check-lld

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

modocache created this revision.Jun 10 2018, 11:33 AM

I am not working on lld but this looks good for me. I like this solution better than the alternative you mentioned.

A quick comment is that -lto_library doesn't imply enable LTO (it is not an optimization flag). It simply tells ld64 which libLTO to load for reading llvm bitcode object and perform LTO optimization.

smeenai requested changes to this revision.Jun 11 2018, 12:18 PM
smeenai added a reviewer: pcc.
smeenai added a subscriber: pcc.

From what I understand, the way LTO works on LLD COFF and ELF is that the appropriate LLVM libraries are just (statically) linked into LLD itself, so there's no concept of an LTO library. I assume that LTO support in LLD Mach-O will work the same way when it's implemented, so the option should never be needed there either. I think it makes more sense to change the clang driver to not pass the option to LLD, but I'm adding @pcc to confirm that my understanding of LTO is correct.

This revision now requires changes to proceed.Jun 11 2018, 12:18 PM
pcc added a comment.Jun 11 2018, 12:33 PM

ELF linkers support a similar flag, -plugin, for passing the path to an LTO plugin. At least on Linux we can't always tell whether the linker is lld so we just pass -plugin all the time, and then in the linker we ignore it. We can do the same for -lto_library.

Right now we just want to error out if someone actually tries to do LTO by passing a bitcode fiel to the linker. I imagine that Mach-O lld is already erroring out if it sees a bitcode file, so probably all that needs to happen here is to add the flag to lib/Driver/DarwinLdOptions.td. You probably don't want to print a warning because you don't want to see this warning on ~every link.

modocache updated this revision to Diff 150885.Jun 11 2018, 8:23 PM

Thanks for the reviews! I adopted @pcc's suggestion to ignore the option without printing a warning. (I also considered calling llvm::opt::Arg::claim, but it looks like no other arguments are claimed, and lld doesn't print warnings for unclaimed arguments anyway.) I updated the help text and the test as well.

Friendly ping! Is this alright by you, @smeenai and @pcc?

This revision is now accepted and ready to land.Jun 13 2018, 10:11 AM

Great! Thanks for the review :)

This revision was automatically updated to reflect the committed changes.