[Driver][LTO] Move common code for LTO to addLTOOptions() (#74178)
diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp
index 5dc80bc..e44b0a7 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -259,19 +259,9 @@
// Specify linker input file(s).
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 998106c..4ecb01d 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -560,7 +560,7 @@
if (C.getDriver().isUsingLTO()) {
const bool ThinLTO = (C.getDriver().getLTOMode() == LTOK_Thin);
- addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0], ThinLTO);
+ addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs, ThinLTO);
} else if (Args.hasArg(options::OPT_mcpu_EQ)) {
CmdArgs.push_back(Args.MakeArgString(
"-plugin-opt=mcpu=" +
diff --git a/clang/lib/Driver/ToolChains/AVR.cpp b/clang/lib/Driver/ToolChains/AVR.cpp
index 8b8956a..53e952a 100644
--- a/clang/lib/Driver/ToolChains/AVR.cpp
+++ b/clang/lib/Driver/ToolChains/AVR.cpp
@@ -520,19 +520,9 @@
}
}
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(TC, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
// If the family name is known, we can link with the device-specific libgcc.
// Without it, libgcc will simply not be linked. This matches avr-gcc
diff --git a/clang/lib/Driver/ToolChains/BareMetal.cpp b/clang/lib/Driver/ToolChains/BareMetal.cpp
index ffb1c6e..7fd5d6c2 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -503,19 +503,10 @@
CmdArgs.push_back("-lc");
}
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(TC, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
+
if (TC.getTriple().isRISCV())
CmdArgs.push_back("-X");
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index fa641b5..07656fa 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -860,7 +860,7 @@
void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
ArgStringList &CmdArgs, const InputInfo &Output,
- const InputInfo &Input, bool IsThinLTO) {
+ const InputInfoList &Inputs, bool IsThinLTO) {
const llvm::Triple &Triple = ToolChain.getTriple();
const bool IsOSAIX = Triple.isOSAIX();
const bool IsAMDGCN = Triple.isAMDGCN();
@@ -870,6 +870,17 @@
const bool IsFatLTO = Args.hasFlag(options::OPT_ffat_lto_objects,
options::OPT_fno_fat_lto_objects, false);
const bool IsUnifiedLTO = Args.hasArg(options::OPT_funified_lto);
+
+ assert(!Inputs.empty() && "Must have at least one input.");
+
+ auto Input = llvm::find_if(
+ Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
+ if (Input == Inputs.end()) {
+ // For a very rare case, all of the inputs to the linker are
+ // InputArg. If that happens, just use the first InputInfo.
+ Input = Inputs.begin();
+ }
+
if (Linker != "lld" && Linker != "lld-link" &&
llvm::sys::path::filename(LinkerPath) != "ld.lld" &&
llvm::sys::path::stem(LinkerPath) != "ld.lld" && !Triple.isOSOpenBSD()) {
@@ -1163,7 +1174,7 @@
Args.MakeArgString(Twine(PluginOptPrefix) + "-stack-size-section"));
// Setup statistics file output.
- SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
+ SmallString<128> StatsFile = getStatsFileName(Args, Output, *Input, D);
if (!StatsFile.empty())
CmdArgs.push_back(
Args.MakeArgString(Twine(PluginOptPrefix) + "stats-file=" + StatsFile));
@@ -1181,7 +1192,7 @@
// Handle serialized remarks options: '-fsave-optimization-record'
// and '-foptimization-record-*'.
if (willEmitRemarks(Args))
- renderRemarksOptions(Args, CmdArgs, ToolChain.getEffectiveTriple(), Input,
+ renderRemarksOptions(Args, CmdArgs, ToolChain.getEffectiveTriple(), *Input,
Output, PluginOptPrefix);
// Handle remarks hotness/threshold related options.
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index 6d36a0e..b5b9c1e 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -76,7 +76,7 @@
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output,
- const InputInfo &Input, bool IsThinLTO);
+ const InputInfoList &Inputs, bool IsThinLTO);
const char *RelocationModelName(llvm::Reloc::Model Model);
diff --git a/clang/lib/Driver/ToolChains/Cuda.cpp b/clang/lib/Driver/ToolChains/Cuda.cpp
index 06b0b09..69b3c00 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -630,7 +630,7 @@
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
if (C.getDriver().isUsingLTO())
- addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
+ addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs,
C.getDriver().getLTOMode() == LTOK_Thin);
// Forward the PTX features if the nvlink-wrapper needs it.
diff --git a/clang/lib/Driver/ToolChains/FreeBSD.cpp b/clang/lib/Driver/ToolChains/FreeBSD.cpp
index e7efe22..25c1c6f 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -273,19 +273,9 @@
Args.addAllArgs(CmdArgs,
{options::OPT_T_Group, options::OPT_s, options::OPT_t});
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);
diff --git a/clang/lib/Driver/ToolChains/Fuchsia.cpp b/clang/lib/Driver/ToolChains/Fuchsia.cpp
index c2badc8..beb38d5 100644
--- a/clang/lib/Driver/ToolChains/Fuchsia.cpp
+++ b/clang/lib/Driver/ToolChains/Fuchsia.cpp
@@ -139,19 +139,9 @@
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 388eb48..fd21675 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -521,19 +521,9 @@
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
CmdArgs.push_back("--no-demangle");
diff --git a/clang/lib/Driver/ToolChains/HIPAMD.cpp b/clang/lib/Driver/ToolChains/HIPAMD.cpp
index abda4eb..bf1ba29 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -82,9 +82,8 @@
auto &TC = getToolChain();
auto &D = TC.getDriver();
- assert(!Inputs.empty() && "Must have at least one input.");
bool IsThinLTO = D.getOffloadLTOMode() == LTOK_Thin;
- addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
+ addLTOOptions(TC, Args, LldArgs, Output, Inputs, IsThinLTO);
// Extract all the -m options
std::vector<llvm::StringRef> Features;
diff --git a/clang/lib/Driver/ToolChains/Haiku.cpp b/clang/lib/Driver/ToolChains/Haiku.cpp
index a6f9582..5d0d8b7 100644
--- a/clang/lib/Driver/ToolChains/Haiku.cpp
+++ b/clang/lib/Driver/ToolChains/Haiku.cpp
@@ -87,19 +87,9 @@
options::OPT_s, options::OPT_t});
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
diff --git a/clang/lib/Driver/ToolChains/MinGW.cpp b/clang/lib/Driver/ToolChains/MinGW.cpp
index 0312406..7bf2291 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -251,11 +251,9 @@
AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- addLTOOptions(TC, Args, CmdArgs, Output, Inputs[0],
+ if (D.isUsingLTO())
+ addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
if (C.getDriver().IsFlangMode() &&
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
diff --git a/clang/lib/Driver/ToolChains/OpenBSD.cpp b/clang/lib/Driver/ToolChains/OpenBSD.cpp
index a5b1f06..6686262 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -199,19 +199,9 @@
Args.addAllArgs(CmdArgs,
{options::OPT_T_Group, options::OPT_s, options::OPT_t});
- if (D.isUsingLTO()) {
- assert(!Inputs.empty() && "Must have at least one input.");
- // Find the first filename InputInfo object.
- auto Input = llvm::find_if(
- Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
- if (Input == Inputs.end())
- // For a very rare case, all of the inputs to the linker are
- // InputArg. If that happens, just use the first InputInfo.
- Input = Inputs.begin();
-
- addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
+ if (D.isUsingLTO())
+ addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
D.getLTOMode() == LTOK_Thin);
- }
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);