Index: clang/lib/Driver/ToolChains/Gnu.cpp =================================================================== --- clang/lib/Driver/ToolChains/Gnu.cpp +++ clang/lib/Driver/ToolChains/Gnu.cpp @@ -546,7 +546,14 @@ ToolChain.AddFilePathLibArgs(Args, CmdArgs); - if (D.isUsingLTO()) { + // If no LTO option has been specified, add the LTO options anyway. + // This has the upside of making the linking automatically work with + // LLVM bitcode files created from an LTO compile. If the input is not + // an LTO file, the LTO plugin will not be invoked. + // This is the same behavior the GCC driver uses. + if ((!Args.hasArg(options::OPT_flto_EQ) && + !Args.hasArg(options::OPT_fno_lto)) || + D.isUsingLTO()) { assert(!Inputs.empty() && "Must have at least one input."); addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0], D.getLTOMode() == LTOK_Thin); Index: clang/test/Driver/lto.c =================================================================== --- clang/test/Driver/lto.c +++ clang/test/Driver/lto.c @@ -104,4 +104,19 @@ // FLTO-THIN-NOT: "-flto" // FLTO-THIN: -flto=thin // FLTO-THIN-NOT: "-flto" -// FLTO-THIN-NOT: -flto=full \ No newline at end of file +// FLTO-THIN-NOT: -flto=full + +// When ld.bfd is being used and no lto option has been specified, the driver +// should pass the lto plugin options to the linker anyway, so LTO +// input files can be linked. +// RUN: %clang -target x86_64-unknown-linux -fuse-ld=bfd -### %s 2>&1 | \ +// RUN: FileCheck --check-prefix=FLTO-NOT-SPECIFIED-BFD %s +// +// FLTO-NOT-SPECIFIED-BFD: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}" +// +// +// But not when LLD is used: +// RUN: %clang -target x86_64-unknown-linux -fuse-ld=lld -### %s 2>&1 | \ +// RUN: FileCheck --check-prefix=FLTO-NOT-SPECIFIED-LLD %s +// +// FLTO-NOT-SPECIFIED-LLD-NOT: "-plugin" "{{.*}}{{[/\\]}}LLVMgold.{{dll|dylib|so}}"