Skip to content

Commit dddcbc2

Browse files
authored
[Driver][LTO] Move common code for LTO to addLTOOptions() (#74178)
1 parent 7e09a00 commit dddcbc2

File tree

14 files changed

+37
-108
lines changed

14 files changed

+37
-108
lines changed

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,9 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
259259
// Specify linker input file(s).
260260
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
261261

262-
if (D.isUsingLTO()) {
263-
assert(!Inputs.empty() && "Must have at least one input.");
264-
// Find the first filename InputInfo object.
265-
auto Input = llvm::find_if(
266-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
267-
if (Input == Inputs.end())
268-
// For a very rare case, all of the inputs to the linker are
269-
// InputArg. If that happens, just use the first InputInfo.
270-
Input = Inputs.begin();
271-
272-
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
262+
if (D.isUsingLTO())
263+
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
273264
D.getLTOMode() == LTOK_Thin);
274-
}
275265

276266
if (Args.hasArg(options::OPT_shared) && !hasExportListLinkerOpts(CmdArgs)) {
277267

clang/lib/Driver/ToolChains/AMDGPU.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ void amdgpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
560560

561561
if (C.getDriver().isUsingLTO()) {
562562
const bool ThinLTO = (C.getDriver().getLTOMode() == LTOK_Thin);
563-
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0], ThinLTO);
563+
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs, ThinLTO);
564564
} else if (Args.hasArg(options::OPT_mcpu_EQ)) {
565565
CmdArgs.push_back(Args.MakeArgString(
566566
"-plugin-opt=mcpu=" +

clang/lib/Driver/ToolChains/AVR.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -520,19 +520,9 @@ void AVR::Linker::ConstructJob(Compilation &C, const JobAction &JA,
520520
}
521521
}
522522

523-
if (D.isUsingLTO()) {
524-
assert(!Inputs.empty() && "Must have at least one input.");
525-
// Find the first filename InputInfo object.
526-
auto Input = llvm::find_if(
527-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
528-
if (Input == Inputs.end())
529-
// For a very rare case, all of the inputs to the linker are
530-
// InputArg. If that happens, just use the first InputInfo.
531-
Input = Inputs.begin();
532-
533-
addLTOOptions(TC, Args, CmdArgs, Output, *Input,
523+
if (D.isUsingLTO())
524+
addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
534525
D.getLTOMode() == LTOK_Thin);
535-
}
536526

537527
// If the family name is known, we can link with the device-specific libgcc.
538528
// Without it, libgcc will simply not be linked. This matches avr-gcc

clang/lib/Driver/ToolChains/BareMetal.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -503,19 +503,10 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
503503
CmdArgs.push_back("-lc");
504504
}
505505

506-
if (D.isUsingLTO()) {
507-
assert(!Inputs.empty() && "Must have at least one input.");
508-
// Find the first filename InputInfo object.
509-
auto Input = llvm::find_if(
510-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
511-
if (Input == Inputs.end())
512-
// For a very rare case, all of the inputs to the linker are
513-
// InputArg. If that happens, just use the first InputInfo.
514-
Input = Inputs.begin();
515-
516-
addLTOOptions(TC, Args, CmdArgs, Output, *Input,
506+
if (D.isUsingLTO())
507+
addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
517508
D.getLTOMode() == LTOK_Thin);
518-
}
509+
519510
if (TC.getTriple().isRISCV())
520511
CmdArgs.push_back("-X");
521512

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
860860

861861
void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
862862
ArgStringList &CmdArgs, const InputInfo &Output,
863-
const InputInfo &Input, bool IsThinLTO) {
863+
const InputInfoList &Inputs, bool IsThinLTO) {
864864
const llvm::Triple &Triple = ToolChain.getTriple();
865865
const bool IsOSAIX = Triple.isOSAIX();
866866
const bool IsAMDGCN = Triple.isAMDGCN();
@@ -870,6 +870,17 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
870870
const bool IsFatLTO = Args.hasFlag(options::OPT_ffat_lto_objects,
871871
options::OPT_fno_fat_lto_objects, false);
872872
const bool IsUnifiedLTO = Args.hasArg(options::OPT_funified_lto);
873+
874+
assert(!Inputs.empty() && "Must have at least one input.");
875+
876+
auto Input = llvm::find_if(
877+
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
878+
if (Input == Inputs.end()) {
879+
// For a very rare case, all of the inputs to the linker are
880+
// InputArg. If that happens, just use the first InputInfo.
881+
Input = Inputs.begin();
882+
}
883+
873884
if (Linker != "lld" && Linker != "lld-link" &&
874885
llvm::sys::path::filename(LinkerPath) != "ld.lld" &&
875886
llvm::sys::path::stem(LinkerPath) != "ld.lld" && !Triple.isOSOpenBSD()) {
@@ -1163,7 +1174,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11631174
Args.MakeArgString(Twine(PluginOptPrefix) + "-stack-size-section"));
11641175

11651176
// Setup statistics file output.
1166-
SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
1177+
SmallString<128> StatsFile = getStatsFileName(Args, Output, *Input, D);
11671178
if (!StatsFile.empty())
11681179
CmdArgs.push_back(
11691180
Args.MakeArgString(Twine(PluginOptPrefix) + "stats-file=" + StatsFile));
@@ -1181,7 +1192,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11811192
// Handle serialized remarks options: '-fsave-optimization-record'
11821193
// and '-foptimization-record-*'.
11831194
if (willEmitRemarks(Args))
1184-
renderRemarksOptions(Args, CmdArgs, ToolChain.getEffectiveTriple(), Input,
1195+
renderRemarksOptions(Args, CmdArgs, ToolChain.getEffectiveTriple(), *Input,
11851196
Output, PluginOptPrefix);
11861197

11871198
// Handle remarks hotness/threshold related options.

clang/lib/Driver/ToolChains/CommonArgs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
7676

7777
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args,
7878
llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output,
79-
const InputInfo &Input, bool IsThinLTO);
79+
const InputInfoList &Inputs, bool IsThinLTO);
8080

8181
const char *RelocationModelName(llvm::Reloc::Model Model);
8282

clang/lib/Driver/ToolChains/Cuda.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ void NVPTX::Linker::ConstructJob(Compilation &C, const JobAction &JA,
630630
AddLinkerInputs(getToolChain(), Inputs, Args, CmdArgs, JA);
631631

632632
if (C.getDriver().isUsingLTO())
633-
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs[0],
633+
addLTOOptions(getToolChain(), Args, CmdArgs, Output, Inputs,
634634
C.getDriver().getLTOMode() == LTOK_Thin);
635635

636636
// Forward the PTX features if the nvlink-wrapper needs it.

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -273,19 +273,9 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
273273
Args.addAllArgs(CmdArgs,
274274
{options::OPT_T_Group, options::OPT_s, options::OPT_t});
275275

276-
if (D.isUsingLTO()) {
277-
assert(!Inputs.empty() && "Must have at least one input.");
278-
// Find the first filename InputInfo object.
279-
auto Input = llvm::find_if(
280-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
281-
if (Input == Inputs.end())
282-
// For a very rare case, all of the inputs to the linker are
283-
// InputArg. If that happens, just use the first InputInfo.
284-
Input = Inputs.begin();
285-
286-
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
276+
if (D.isUsingLTO())
277+
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
287278
D.getLTOMode() == LTOK_Thin);
288-
}
289279

290280
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
291281
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);

clang/lib/Driver/ToolChains/Fuchsia.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,9 @@ void fuchsia::Linker::ConstructJob(Compilation &C, const JobAction &JA,
139139

140140
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
141141

142-
if (D.isUsingLTO()) {
143-
assert(!Inputs.empty() && "Must have at least one input.");
144-
// Find the first filename InputInfo object.
145-
auto Input = llvm::find_if(
146-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
147-
if (Input == Inputs.end())
148-
// For a very rare case, all of the inputs to the linker are
149-
// InputArg. If that happens, just use the first InputInfo.
150-
Input = Inputs.begin();
151-
152-
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
142+
if (D.isUsingLTO())
143+
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
153144
D.getLTOMode() == LTOK_Thin);
154-
}
155145

156146
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);
157147
AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -521,19 +521,9 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
521521

522522
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
523523

524-
if (D.isUsingLTO()) {
525-
assert(!Inputs.empty() && "Must have at least one input.");
526-
// Find the first filename InputInfo object.
527-
auto Input = llvm::find_if(
528-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
529-
if (Input == Inputs.end())
530-
// For a very rare case, all of the inputs to the linker are
531-
// InputArg. If that happens, just use the first InputInfo.
532-
Input = Inputs.begin();
533-
534-
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
524+
if (D.isUsingLTO())
525+
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
535526
D.getLTOMode() == LTOK_Thin);
536-
}
537527

538528
if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
539529
CmdArgs.push_back("--no-demangle");

clang/lib/Driver/ToolChains/HIPAMD.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ void AMDGCN::Linker::constructLldCommand(Compilation &C, const JobAction &JA,
8282

8383
auto &TC = getToolChain();
8484
auto &D = TC.getDriver();
85-
assert(!Inputs.empty() && "Must have at least one input.");
8685
bool IsThinLTO = D.getOffloadLTOMode() == LTOK_Thin;
87-
addLTOOptions(TC, Args, LldArgs, Output, Inputs[0], IsThinLTO);
86+
addLTOOptions(TC, Args, LldArgs, Output, Inputs, IsThinLTO);
8887

8988
// Extract all the -m options
9089
std::vector<llvm::StringRef> Features;

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,9 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
8787
options::OPT_s, options::OPT_t});
8888
ToolChain.AddFilePathLibArgs(Args, CmdArgs);
8989

90-
if (D.isUsingLTO()) {
91-
assert(!Inputs.empty() && "Must have at least one input.");
92-
// Find the first filename InputInfo object.
93-
auto Input = llvm::find_if(
94-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
95-
if (Input == Inputs.end())
96-
// For a very rare case, all of the inputs to the linker are
97-
// InputArg. If that happens, just use the first InputInfo.
98-
Input = Inputs.begin();
99-
100-
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
90+
if (D.isUsingLTO())
91+
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
10192
D.getLTOMode() == LTOK_Thin);
102-
}
10393

10494
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
10595
addLinkerCompressDebugSectionsOption(ToolChain, Args, CmdArgs);

clang/lib/Driver/ToolChains/MinGW.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,9 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
251251

252252
AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
253253

254-
if (D.isUsingLTO()) {
255-
assert(!Inputs.empty() && "Must have at least one input.");
256-
addLTOOptions(TC, Args, CmdArgs, Output, Inputs[0],
254+
if (D.isUsingLTO())
255+
addLTOOptions(TC, Args, CmdArgs, Output, Inputs,
257256
D.getLTOMode() == LTOK_Thin);
258-
}
259257

260258
if (C.getDriver().IsFlangMode() &&
261259
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,9 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
199199
Args.addAllArgs(CmdArgs,
200200
{options::OPT_T_Group, options::OPT_s, options::OPT_t});
201201

202-
if (D.isUsingLTO()) {
203-
assert(!Inputs.empty() && "Must have at least one input.");
204-
// Find the first filename InputInfo object.
205-
auto Input = llvm::find_if(
206-
Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
207-
if (Input == Inputs.end())
208-
// For a very rare case, all of the inputs to the linker are
209-
// InputArg. If that happens, just use the first InputInfo.
210-
Input = Inputs.begin();
211-
212-
addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
202+
if (D.isUsingLTO())
203+
addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs,
213204
D.getLTOMode() == LTOK_Thin);
214-
}
215205

216206
bool NeedsSanitizerDeps = addSanitizerRuntimes(ToolChain, Args, CmdArgs);
217207
bool NeedsXRayDeps = addXRayRuntime(ToolChain, Args, CmdArgs);

0 commit comments

Comments
 (0)