@@ -1427,12 +1427,15 @@ static bool isSDKTooOld(StringRef sdkPath, const llvm::Triple &target) {
14271427void Driver::buildOutputInfo (const ToolChain &TC, const DerivedArgList &Args,
14281428 const bool BatchMode, const InputFileList &Inputs,
14291429 OutputInfo &OI) const {
1430+ auto LinkerInputType = Args.hasArg (options::OPT_lto)
1431+ ? file_types::TY_LLVM_BC
1432+ : file_types::TY_Object;
14301433 // By default, the driver does not link its output; this will be updated
14311434 // appropriately below if linking is required.
14321435
14331436 OI.CompilerOutputType = driverKind == DriverKind::Interactive
14341437 ? file_types::TY_Nothing
1435- : file_types::TY_Object ;
1438+ : LinkerInputType ;
14361439
14371440 if (const Arg *A = Args.getLastArg (options::OPT_num_threads)) {
14381441 if (BatchMode) {
@@ -1462,14 +1465,14 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
14621465 diag::error_static_emit_executable_disallowed);
14631466
14641467 OI.LinkAction = LinkKind::Executable;
1465- OI.CompilerOutputType = file_types::TY_Object ;
1468+ OI.CompilerOutputType = LinkerInputType ;
14661469 break ;
14671470
14681471 case options::OPT_emit_library:
14691472 OI.LinkAction = Args.hasArg (options::OPT_static) ?
14701473 LinkKind::StaticLibrary :
14711474 LinkKind::DynamicLibrary;
1472- OI.CompilerOutputType = file_types::TY_Object ;
1475+ OI.CompilerOutputType = LinkerInputType ;
14731476 break ;
14741477
14751478 case options::OPT_static:
@@ -1779,6 +1782,18 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
17791782
17801783 }
17811784
1785+ if (const Arg *A = Args.getLastArg (options::OPT_lto)) {
1786+ auto LTOVariant = llvm::StringSwitch<Optional<OutputInfo::LTOKind>>(A->getValue ())
1787+ .Case (" llvm" , OutputInfo::LTOKind::LLVMThin)
1788+ .Case (" llvm-full" , OutputInfo::LTOKind::LLVMFull)
1789+ .Default (llvm::None);
1790+ if (LTOVariant)
1791+ OI.LTOVariant = LTOVariant.getValue ();
1792+ else
1793+ Diags.diagnose (SourceLoc (), diag::error_invalid_arg_value,
1794+ A->getAsString (Args), A->getValue ());
1795+ }
1796+
17821797 if (TC.getTriple ().isOSWindows ()) {
17831798 if (const Arg *A = Args.getLastArg (options::OPT_libc)) {
17841799 OI.RuntimeVariant =
@@ -2113,15 +2128,17 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
21132128 MergeModuleAction = C.createAction <MergeModuleJobAction>(AllModuleInputs);
21142129 }
21152130
2131+ auto PerformLTO = Args.hasArg (options::OPT_lto);
21162132 if (OI.shouldLink () && !AllLinkerInputs.empty ()) {
21172133 JobAction *LinkAction = nullptr ;
21182134
21192135 if (OI.LinkAction == LinkKind::StaticLibrary) {
21202136 LinkAction = C.createAction <StaticLinkJobAction>(AllLinkerInputs,
2121- OI.LinkAction );
2137+ OI.LinkAction );
21222138 } else {
21232139 LinkAction = C.createAction <DynamicLinkJobAction>(AllLinkerInputs,
2124- OI.LinkAction );
2140+ OI.LinkAction ,
2141+ PerformLTO);
21252142 }
21262143
21272144 // On ELF platforms there's no built in autolinking mechanism, so we
@@ -2130,7 +2147,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
21302147 const auto &Triple = TC.getTriple ();
21312148 SmallVector<const Action *, 2 > AutolinkExtractInputs;
21322149 for (const Action *A : AllLinkerInputs)
2133- if (A->getType () == file_types::TY_Object ) {
2150+ if (A->getType () == OI. CompilerOutputType ) {
21342151 // Shared objects on ELF platforms don't have a swift1_autolink_entries
21352152 // section in them because the section in the .o files is marked as
21362153 // SHF_EXCLUDE.
@@ -2146,7 +2163,7 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
21462163 (Triple.getObjectFormat () == llvm::Triple::ELF && !Triple.isPS4 ()) ||
21472164 Triple.getObjectFormat () == llvm::Triple::Wasm ||
21482165 Triple.isOSCygMing ();
2149- if (!AutolinkExtractInputs.empty () && AutolinkExtractRequired) {
2166+ if (!AutolinkExtractInputs.empty () && AutolinkExtractRequired && !PerformLTO ) {
21502167 auto *AutolinkExtractAction =
21512168 C.createAction <AutolinkExtractJobAction>(AutolinkExtractInputs);
21522169 // Takes the same inputs as the linker...
0 commit comments