Skip to content

Commit bfb955c

Browse files
committed
[DTLTO][Clang][LLD] Fix DTLTO for multi-call LLVM driver toolchain
Add DTLTO linker option `--thinlto-remote-compiler-prepend-arg` to enable support for the multi-call LLVM driver that requires an additional option to specify the subcommand, e.g. "llvm clang ...". Fixes #159125.
1 parent ff9cdbd commit bfb955c

File tree

21 files changed

+133
-55
lines changed

21 files changed

+133
-55
lines changed

clang/include/clang/Driver/CommonArgs.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
7676
const JobAction &JA, const llvm::opt::ArgList &Args,
7777
const InputInfo &Output, const char *OutFile);
7878

79+
void addDTLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args,
80+
llvm::opt::ArgStringList &CmdArgs);
81+
7982
void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args,
8083
llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output,
8184
const InputInfoList &Inputs, bool IsThinLTO);

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,24 @@ bool tools::isTLSDESCEnabled(const ToolChain &TC,
949949
return EnableTLSDESC;
950950
}
951951

952+
void tools::addDTLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
953+
llvm::opt::ArgStringList &CmdArgs) {
954+
if (Arg *A = Args.getLastArg(options::OPT_fthinlto_distributor_EQ)) {
955+
CmdArgs.push_back(
956+
Args.MakeArgString("--thinlto-distributor=" + Twine(A->getValue())));
957+
const Driver &D = ToolChain.getDriver();
958+
CmdArgs.push_back(Args.MakeArgString("--thinlto-remote-compiler=" +
959+
Twine(D.getClangProgramPath())));
960+
if (auto *PA = D.getPrependArg())
961+
CmdArgs.push_back(Args.MakeArgString(
962+
"--thinlto-remote-compiler-prepend-arg=" + Twine(PA)));
963+
964+
for (const auto &A :
965+
Args.getAllArgValues(options::OPT_Xthinlto_distributor_EQ))
966+
CmdArgs.push_back(Args.MakeArgString("--thinlto-distributor-arg=" + A));
967+
}
968+
}
969+
952970
void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
953971
ArgStringList &CmdArgs, const InputInfo &Output,
954972
const InputInfoList &Inputs, bool IsThinLTO) {
@@ -1345,16 +1363,7 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
13451363
CmdArgs.push_back(
13461364
Args.MakeArgString(Twine(PluginOptPrefix) + "-time-passes"));
13471365

1348-
if (Arg *A = Args.getLastArg(options::OPT_fthinlto_distributor_EQ)) {
1349-
CmdArgs.push_back(
1350-
Args.MakeArgString("--thinlto-distributor=" + Twine(A->getValue())));
1351-
CmdArgs.push_back(
1352-
Args.MakeArgString("--thinlto-remote-compiler=" +
1353-
Twine(ToolChain.getDriver().getClangProgramPath())));
1354-
1355-
for (auto A : Args.getAllArgValues(options::OPT_Xthinlto_distributor_EQ))
1356-
CmdArgs.push_back(Args.MakeArgString("--thinlto-distributor-arg=" + A));
1357-
}
1366+
addDTLTOOptions(ToolChain, Args, CmdArgs);
13581367
}
13591368

13601369
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,

clang/lib/Driver/ToolChains/PS4CPU.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,16 +344,7 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
344344
// pass LTO options to ensure proper codegen, metadata production, etc if
345345
// LTO indeed occurs.
346346

347-
if (const Arg *A = Args.getLastArg(options::OPT_fthinlto_distributor_EQ)) {
348-
CmdArgs.push_back(
349-
Args.MakeArgString("--thinlto-distributor=" + Twine(A->getValue())));
350-
CmdArgs.push_back(Args.MakeArgString("--thinlto-remote-compiler=" +
351-
Twine(D.getClangProgramPath())));
352-
353-
for (const auto &A :
354-
Args.getAllArgValues(options::OPT_Xthinlto_distributor_EQ))
355-
CmdArgs.push_back(Args.MakeArgString("--thinlto-distributor-arg=" + A));
356-
}
347+
tools::addDTLTOOptions(TC, Args, CmdArgs);
357348

358349
if (Args.hasFlag(options::OPT_funified_lto, options::OPT_fno_unified_lto,
359350
true))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from pathlib import Path
2+
import sys
3+
4+
# Arg 1: "clang" path.
5+
p = Path(sys.argv[1])
6+
print(f"clang-name:{p.resolve().name}")
7+
# Arg 2: Non-zero for LLVM driver.
8+
if sys.argv[2] != "0":
9+
print(f"prepend-arg:\"--thinlto-remote-compiler-prepend-arg={p.name}\"")
10+
else:
11+
print("prepend-arg: ")

clang/test/Driver/DTLTO/dtlto.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
/// Check DTLTO options are forwarded to the linker.
44

55
/// Check that options are forwarded as expected with --thinlto-distributor=.
6-
// RUN: %python %S/filename.py %clang > %t_forward.log
6+
// RUN: %python %S/dtlto-helper.py %clang %llvm-driver > %t_forward.log
77
// RUN: %clang -flto=thin %s -### -fuse-ld=lld --target=x86_64-linux-gnu \
88
// RUN: -Xthinlto-distributor=a1 -Xthinlto-distributor=a2,a3 \
99
// RUN: -fthinlto-distributor=d.exe -Werror >>%t_forward.log 2>&1
1010
// RUN: FileCheck %s --input-file=%t_forward.log --check-prefix=FORWARD
1111

12-
// FORWARD: filename.py:[[CLANG:.*]]
13-
// FORWARD: ld.lld
12+
// FORWARD: clang-name:[[CLANG:.*]]
13+
// FORWARD-NEXT: prepend-arg:[[PREPEND_ARG:.*]]
14+
// FORWARD: ld.lld
1415
// FORWARD-SAME: "--thinlto-distributor=d.exe"
1516
// FORWARD-SAME: "--thinlto-remote-compiler={{[^"]*}}[[CLANG]]"
17+
// FORWARD-SAME: [[PREPEND_ARG]]
1618
// FORWARD-SAME: "--thinlto-distributor-arg=a1"
1719
// FORWARD-SAME: "--thinlto-distributor-arg=a2"
1820
// FORWARD-SAME: "--thinlto-distributor-arg=a3"
@@ -30,24 +32,25 @@
3032

3133
/// Check the expected arguments are forwarded by default with only
3234
/// --thinlto-distributor=.
33-
// RUN: %python %S/filename.py %clang > %t_default.log
35+
// RUN: %python %S/dtlto-helper.py %clang %llvm-driver > %t_default.log
3436
// RUN: %clang -flto=thin %s -### -fuse-ld=lld --target=x86_64-linux-gnu \
3537
// RUN: -fthinlto-distributor=d.exe -Werror >>%t_default.log 2>&1
3638
// RUN: FileCheck %s --input-file=%t_default.log --check-prefix=DEFAULT \
3739
// RUN: --implicit-check-not=distributor --implicit-check-not=remote-compiler
3840

39-
// DEFAULT: filename.py:[[CLANG:.*]]
40-
// DEFAULT: ld.lld
41+
// DEFAULT: clang-name:[[CLANG:.*]]
42+
// DEFAULT-NEXT: prepend-arg:[[PREPEND_ARG:.*]]
43+
// DEFAULT: ld.lld
4144
// DEFAULT-SAME: "--thinlto-distributor=d.exe"
4245
// DEFAULT-SAME: "--thinlto-remote-compiler={{[^"]*}}[[CLANG]]"
46+
// DEFAULT-SAME: [[PREPEND_ARG]]
4347

4448
/// Check that nothing is forwarded when the compiler is not in LTO mode, and that
4549
/// appropriate unused option warnings are issued.
46-
// RUN: %python %S/filename.py %clang > %t_noflto.log
4750
// RUN: %clang %s -### -fuse-ld=lld --target=x86_64-linux-gnu \
48-
// RUN: -fthinlto-distributor=d.exe >>%t_noflto.log 2>&1
49-
// RUN: FileCheck %s --input-file=%t_noflto.log --check-prefix=NOFLTO \
50-
// RUN: --implicit-check-not=distributor --implicit-check-not=remote-compiler
51+
// RUN: -fthinlto-distributor=d.exe 2>&1 | \
52+
// RUN: FileCheck %s --check-prefix=NOFLTO --implicit-check-not=distributor \
53+
// RUN: --implicit-check-not=remote-compiler
5154

5255
// NOFLTO: warning: argument unused during compilation: '-fthinlto-distributor=d.exe'
5356
// NOFLTO: ld.lld

clang/test/Driver/DTLTO/filename.py

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from lit.llvm import llvm_config
2+
3+
config.substitutions.append(("%llvm-driver", "1" if "llvm-driver" in config.available_features else "0"))

clang/test/Driver/DTLTO/ps5-dtlto.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@
33
/// Check DTLTO options are forwarded to the linker.
44

55
/// Check that options are forwarded as expected with --thinlto-distributor=.
6-
// RUN: %python %S/filename.py %clang > %t_forward.log
6+
// RUN: %python %S/dtlto-helper.py %clang %llvm-driver > %t_forward.log
77
// RUN: %clang -flto=thin %s -### --target=x86_64-sie-ps5 \
88
// RUN: -Xthinlto-distributor=a1 -Xthinlto-distributor=a2,a3 \
99
// RUN: -fthinlto-distributor=d.exe -Werror >>%t_forward.log 2>&1
1010
// RUN: FileCheck %s --input-file=%t_forward.log --check-prefix=FORWARD
1111

12-
// FORWARD: filename.py:[[CLANG:.*]]
13-
// FORWARD: prospero-lld
12+
// FORWARD: clang-name:[[CLANG:.*]]
13+
// FORWARD-NEXT: prepend-arg:[[PREPEND_ARG:.*]]
14+
// FORWARD: prospero-lld
1415
// FORWARD-SAME: "--thinlto-distributor=d.exe"
1516
// FORWARD-SAME: "--thinlto-remote-compiler={{[^"]*}}[[CLANG]]"
17+
// FORWARD-SAME: [[PREPEND_ARG]]
1618
// FORWARD-SAME: "--thinlto-distributor-arg=a1"
1719
// FORWARD-SAME: "--thinlto-distributor-arg=a2"
1820
// FORWARD-SAME: "--thinlto-distributor-arg=a3"
@@ -30,20 +32,22 @@
3032

3133
/// Check the expected arguments are forwarded by default with only
3234
/// --thinlto-distributor=.
33-
// RUN: %python %S/filename.py %clang > %t_default.log
35+
// RUN: %python %S/dtlto-helper.py %clang %llvm-driver > %t_default.log
3436
// RUN: %clang -flto=thin %s -### --target=x86_64-sie-ps5 \
3537
// RUN: -fthinlto-distributor=d.exe -Werror >>%t_default.log 2>&1
3638
// RUN: FileCheck %s --input-file=%t_default.log --check-prefix=DEFAULT \
3739
// RUN: --implicit-check-not=distributor --implicit-check-not=remote-compiler
3840

39-
// DEFAULT: filename.py:[[CLANG:.*]]
40-
// DEFAULT: prospero-lld
41+
// DEFAULT: clang-name:[[CLANG:.*]]
42+
// DEFAULT-NEXT: prepend-arg:[[PREPEND_ARG:.*]]
43+
// DEFAULT: prospero-lld
4144
// DEFAULT-SAME: "--thinlto-distributor=d.exe"
4245
// DEFAULT-SAME: "--thinlto-remote-compiler={{[^"]*}}[[CLANG]]"
46+
// DEFAULT-SAME: [[PREPEND_ARG]]
4347

4448
/// Check that the arguments are forwarded unconditionally even when the
4549
/// compiler is not in LTO mode.
46-
// RUN: %python %S/filename.py %clang > %t_noflto.log
50+
// RUN: %python %S/dtlto-helper.py %clang %llvm-driver > %t_noflto.log
4751
// RUN: %clang %s -### --target=x86_64-sie-ps5 \
4852
// RUN: -fthinlto-distributor=d.exe -Werror >>%t_noflto.log 2>&1
4953
// RUN: FileCheck %s --input-file=%t_noflto.log --check-prefix=DEFAULT \

lld/COFF/Config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ struct Configuration {
201201
// Used for /thinlto-remote-compiler:<path>
202202
StringRef dtltoCompiler;
203203

204+
// Used for /thinlto-remote-compiler-prepend-arg:<arg>
205+
llvm::SmallVector<llvm::StringRef, 0> dtltoCompilerPrependArgs;
206+
204207
// Used for /thinlto-remote-compiler-arg:<arg>
205208
llvm::SmallVector<llvm::StringRef, 0> dtltoCompilerArgs;
206209

lld/COFF/Driver.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
21132113
Err(ctx) << "A value must be specified for /thinlto-remote-compiler if "
21142114
"/thinlto-distributor is specified.";
21152115

2116+
// Handle /thinlto-remote-compiler-prepend-arg:<arg>
2117+
config->dtltoCompilerPrependArgs =
2118+
args::getStrings(args, OPT_thinlto_remote_compiler_prepend_arg);
2119+
21162120
// Handle /thinlto-remote-compiler-arg:<arg>
21172121
config->dtltoCompilerArgs =
21182122
args::getStrings(args, OPT_thinlto_remote_compiler_arg);

0 commit comments

Comments
 (0)