[clang] [Driver][LTO] Move common code for LTO to addLTOOptions() (PR #74178)
Brad Smith via cfe-commits
cfe-commits at lists.llvm.org
Fri May 23 19:09:04 PDT 2025
https://github.com/brad0 updated https://github.com/llvm/llvm-project/pull/74178
>From bd513daa03bd6304ab064063775f30e6c820de0d Mon Sep 17 00:00:00 2001
From: Brad Smith <brad at comstyle.com>
Date: Sat, 11 Nov 2023 20:02:40 -0500
Subject: [PATCH] [Driver][LTO] Move common code for LTO to addLTOOptions()
---
clang/lib/Driver/ToolChains/AIX.cpp | 14 ++------------
clang/lib/Driver/ToolChains/AMDGPU.cpp | 2 +-
clang/lib/Driver/ToolChains/AVR.cpp | 14 ++------------
clang/lib/Driver/ToolChains/BareMetal.cpp | 15 +++------------
clang/lib/Driver/ToolChains/CommonArgs.cpp | 17 ++++++++++++++---
clang/lib/Driver/ToolChains/CommonArgs.h | 2 +-
clang/lib/Driver/ToolChains/Cuda.cpp | 2 +-
clang/lib/Driver/ToolChains/FreeBSD.cpp | 14 ++------------
clang/lib/Driver/ToolChains/Fuchsia.cpp | 14 ++------------
clang/lib/Driver/ToolChains/Gnu.cpp | 14 ++------------
clang/lib/Driver/ToolChains/HIPAMD.cpp | 3 +--
clang/lib/Driver/ToolChains/Haiku.cpp | 14 ++------------
clang/lib/Driver/ToolChains/MinGW.cpp | 6 ++----
clang/lib/Driver/ToolChains/OpenBSD.cpp | 14 ++------------
14 files changed, 37 insertions(+), 108 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/AIX.cpp b/clang/lib/Driver/ToolChains/AIX.cpp
index 5dc80bc5a3d25..e44b0a76b3eb9 100644
--- a/clang/lib/Driver/ToolChains/AIX.cpp
+++ b/clang/lib/Driver/ToolChains/AIX.cpp
@@ -259,19 +259,9 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
// 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 998106cf52d66..4ecb01d0f0bea 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -560,7 +560,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 8b8956a0a15ef..53e952a853005 100644
--- a/clang/lib/Driver/ToolChains/AVR.cpp
+++ b/clang/lib/Driver/ToolChains/AVR.cpp
@@ -520,19 +520,9 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &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(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 ffb1c6e34d603..7fd5d6c2fed0f 100644
--- a/clang/lib/Driver/ToolChains/BareMetal.cpp
+++ b/clang/lib/Driver/ToolChains/BareMetal.cpp
@@ -503,19 +503,10 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 632027c4a944c..e2b4efd5c1956 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -860,7 +860,7 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
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 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
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()) {
@@ -1162,7 +1173,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
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));
@@ -1180,7 +1191,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
// 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 96bc0619dcbc0..4c31e7f3af810 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -76,7 +76,7 @@ void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
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 06b0b0913d24e..69b3c00f9ee02 100644
--- a/clang/lib/Driver/ToolChains/Cuda.cpp
+++ b/clang/lib/Driver/ToolChains/Cuda.cpp
@@ -630,7 +630,7 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 e7efe22aa59a8..25c1c6f6690de 100644
--- a/clang/lib/Driver/ToolChains/FreeBSD.cpp
+++ b/clang/lib/Driver/ToolChains/FreeBSD.cpp
@@ -273,19 +273,9 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 c2badc80a7b45..beb38d544206e 100644
--- a/clang/lib/Driver/ToolChains/Fuchsia.cpp
+++ b/clang/lib/Driver/ToolChains/Fuchsia.cpp
@@ -139,19 +139,9 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 388eb48277ba7..fd216757dd362 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -521,19 +521,9 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 abda4eb453387..bf1ba296e080c 100644
--- a/clang/lib/Driver/ToolChains/HIPAMD.cpp
+++ b/clang/lib/Driver/ToolChains/HIPAMD.cpp
@@ -82,9 +82,8 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
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 a6f9582a66662..5d0d8b7f5024c 100644
--- a/clang/lib/Driver/ToolChains/Haiku.cpp
+++ b/clang/lib/Driver/ToolChains/Haiku.cpp
@@ -87,19 +87,9 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 031240610eef3..7bf2291b90db7 100644
--- a/clang/lib/Driver/ToolChains/MinGW.cpp
+++ b/clang/lib/Driver/ToolChains/MinGW.cpp
@@ -251,11 +251,9 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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 a5b1f06449b73..668626259809e 100644
--- a/clang/lib/Driver/ToolChains/OpenBSD.cpp
+++ b/clang/lib/Driver/ToolChains/OpenBSD.cpp
@@ -199,19 +199,9 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
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);
More information about the cfe-commits
mailing list