Skip to content

Commit d82df1b

Browse files
[PS4/PS5][Driver] Always pass LTO options to the linker (#100423)
The driver doesn't know if LTO will occur at link time. That's determined by the presence or absence of LLVM bitcode objects among those ingested by the linker. For this reason, LTO options for codegen etc must be passed to the linker unconditionally. If LTO does not occur, these options have no effect. Also simplify the way LTO options are supplied to the PS4 linker. `-lto-debug-options` and `-lto-thin-debug-options` are combined and routed to the same place. So, always use the former, regardless of full/thin LTO mode. SIE tracker: TOOLCHAIN-16575
1 parent 6a5a64c commit d82df1b

File tree

5 files changed

+70
-65
lines changed

5 files changed

+70
-65
lines changed

clang/lib/Driver/ToolChains/PS4CPU.cpp

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -152,48 +152,36 @@ void tools::PS4cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
152152
CmdArgs.push_back(Output.getFilename());
153153
}
154154

155-
const bool UseLTO = D.isUsingLTO();
156155
const bool UseJMC =
157156
Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false);
158157

159158
const char *LTOArgs = "";
160-
auto AddCodeGenFlag = [&](Twine Flag) {
159+
auto AddLTOFlag = [&](Twine Flag) {
161160
LTOArgs = Args.MakeArgString(Twine(LTOArgs) + " " + Flag);
162161
};
163162

164-
if (UseLTO) {
165-
// This tells LTO to perform JustMyCode instrumentation.
166-
if (UseJMC)
167-
AddCodeGenFlag("-enable-jmc-instrument");
163+
// If the linker sees bitcode objects it will perform LTO. We can't tell
164+
// whether or not that will be the case at this point. So, unconditionally
165+
// pass LTO options to ensure proper codegen, metadata production, etc if
166+
// LTO indeed occurs.
167+
if (Args.hasFlag(options::OPT_funified_lto, options::OPT_fno_unified_lto,
168+
true))
169+
CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
170+
: "--lto=full");
171+
if (UseJMC)
172+
AddLTOFlag("-enable-jmc-instrument");
168173

169-
if (Arg *A = Args.getLastArg(options::OPT_fcrash_diagnostics_dir))
170-
AddCodeGenFlag(Twine("-crash-diagnostics-dir=") + A->getValue());
174+
if (Arg *A = Args.getLastArg(options::OPT_fcrash_diagnostics_dir))
175+
AddLTOFlag(Twine("-crash-diagnostics-dir=") + A->getValue());
171176

172-
StringRef Parallelism = getLTOParallelism(Args, D);
173-
if (!Parallelism.empty())
174-
AddCodeGenFlag(Twine("-threads=") + Parallelism);
177+
if (StringRef Threads = getLTOParallelism(Args, D); !Threads.empty())
178+
AddLTOFlag(Twine("-threads=") + Threads);
175179

176-
const char *Prefix = nullptr;
177-
if (D.getLTOMode() == LTOK_Thin)
178-
Prefix = "-lto-thin-debug-options=";
179-
else if (D.getLTOMode() == LTOK_Full)
180-
Prefix = "-lto-debug-options=";
181-
else
182-
llvm_unreachable("new LTO mode?");
183-
184-
CmdArgs.push_back(Args.MakeArgString(Twine(Prefix) + LTOArgs));
185-
}
180+
CmdArgs.push_back(Args.MakeArgString(Twine("-lto-debug-options=") + LTOArgs));
186181

187182
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
188183
TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
189184

190-
if (D.isUsingLTO() && Args.hasArg(options::OPT_funified_lto)) {
191-
if (D.getLTOMode() == LTOK_Thin)
192-
CmdArgs.push_back("--lto=thin");
193-
else if (D.getLTOMode() == LTOK_Full)
194-
CmdArgs.push_back("--lto=full");
195-
}
196-
197185
Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
198186
options::OPT_s, options::OPT_t});
199187

@@ -259,37 +247,34 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
259247
CmdArgs.push_back(Output.getFilename());
260248
}
261249

262-
const bool UseLTO = D.isUsingLTO();
263250
const bool UseJMC =
264251
Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false);
265252

266-
auto AddCodeGenFlag = [&](Twine Flag) {
253+
auto AddLTOFlag = [&](Twine Flag) {
267254
CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=") + Flag));
268255
};
269256

270-
if (UseLTO) {
271-
// This tells LTO to perform JustMyCode instrumentation.
272-
if (UseJMC)
273-
AddCodeGenFlag("-enable-jmc-instrument");
257+
// If the linker sees bitcode objects it will perform LTO. We can't tell
258+
// whether or not that will be the case at this point. So, unconditionally
259+
// pass LTO options to ensure proper codegen, metadata production, etc if
260+
// LTO indeed occurs.
261+
if (Args.hasFlag(options::OPT_funified_lto, options::OPT_fno_unified_lto,
262+
true))
263+
CmdArgs.push_back(D.getLTOMode() == LTOK_Thin ? "--lto=thin"
264+
: "--lto=full");
274265

275-
if (Arg *A = Args.getLastArg(options::OPT_fcrash_diagnostics_dir))
276-
AddCodeGenFlag(Twine("-crash-diagnostics-dir=") + A->getValue());
266+
if (UseJMC)
267+
AddLTOFlag("-enable-jmc-instrument");
277268

278-
StringRef Parallelism = getLTOParallelism(Args, D);
279-
if (!Parallelism.empty())
280-
CmdArgs.push_back(Args.MakeArgString(Twine("-plugin-opt=jobs=") + Parallelism));
281-
}
269+
if (Arg *A = Args.getLastArg(options::OPT_fcrash_diagnostics_dir))
270+
AddLTOFlag(Twine("-crash-diagnostics-dir=") + A->getValue());
271+
272+
if (StringRef Jobs = getLTOParallelism(Args, D); !Jobs.empty())
273+
AddLTOFlag(Twine("jobs=") + Jobs);
282274

283275
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
284276
TC.addSanitizerArgs(Args, CmdArgs, "-l", "");
285277

286-
if (D.isUsingLTO() && Args.hasArg(options::OPT_funified_lto)) {
287-
if (D.getLTOMode() == LTOK_Thin)
288-
CmdArgs.push_back("--lto=thin");
289-
else if (D.getLTOMode() == LTOK_Full)
290-
CmdArgs.push_back("--lto=full");
291-
}
292-
293278
Args.addAllArgs(CmdArgs, {options::OPT_L, options::OPT_T_Group,
294279
options::OPT_s, options::OPT_t});
295280

clang/test/Driver/lto-jobs.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
// RUN: %clang --target=x86_64-sie-ps5 -### %s -flto=thin -flto-jobs=5 2> %t
77
// RUN: FileCheck -check-prefix=CHECK-LINK-THIN-JOBS-ACTION < %t %s
88
//
9+
// RUN: %clang --target=x86_64-sie-ps5 -### %s -flto-jobs=5 2> %t
10+
// RUN: FileCheck -check-prefix=CHECK-LINK-THIN-JOBS-ACTION < %t %s
11+
//
912
// CHECK-LINK-THIN-JOBS-ACTION: "-plugin-opt=jobs=5"
1013
//
1114
// RUN: %clang --target=x86_64-scei-ps4 -### %s -flto=thin -flto-jobs=5 2> %t
1215
// RUN: FileCheck -check-prefix=CHECK-PS4-LINK-THIN-JOBS-ACTION < %t %s
1316
//
14-
// CHECK-PS4-LINK-THIN-JOBS-ACTION: "-lto-thin-debug-options= -threads=5"
17+
// CHECK-PS4-LINK-THIN-JOBS-ACTION: "-lto-debug-options= -threads=5"
1518

1619
// RUN: %clang --target=x86_64-apple-darwin13.3.0 -### %s -flto=thin -flto-jobs=5 2> %t
1720
// RUN: FileCheck -check-prefix=CHECK-LINK-THIN-JOBS2-ACTION < %t %s

clang/test/Driver/ps4-linker.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
// Test the driver's control over the JustMyCode behavior with linker flags.
22

3-
// RUN: %clang --target=x86_64-scei-ps4 -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-LIB %s
4-
// RUN: %clang --target=x86_64-scei-ps4 -flto=thin -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-THIN-LTO,CHECK-LIB %s
5-
// RUN: %clang --target=x86_64-scei-ps4 -flto=full -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-FULL-LTO,CHECK-LIB %s
3+
// RUN: %clang --target=x86_64-scei-ps4 -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-LTO,CHECK-LIB %s
4+
// RUN: %clang --target=x86_64-scei-ps4 -flto=thin -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-LTO,CHECK-LIB %s
5+
// RUN: %clang --target=x86_64-scei-ps4 -flto=full -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-LTO,CHECK-LIB %s
66

7-
// CHECK-NOT: -enable-jmc-instrument
8-
// CHECK-THIN-LTO: "-lto-thin-debug-options= -enable-jmc-instrument"
9-
// CHECK-FULL-LTO: "-lto-debug-options= -enable-jmc-instrument"
7+
// CHECK-LTO: "-lto-debug-options= -enable-jmc-instrument"
108

119
// Check the default library name.
1210
// CHECK-LIB: "--whole-archive" "-lSceDbgJmc" "--no-whole-archive"
1311

1412
// Test the driver's control over the -fcrash-diagnostics-dir behavior with linker flags.
1513

16-
// RUN: %clang --target=x86_64-scei-ps4 -flto=thin -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG-THIN-LTO %s
17-
// RUN: %clang --target=x86_64-scei-ps4 -flto=full -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG-FULL-LTO %s
14+
// RUN: %clang --target=x86_64-scei-ps4 -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG-LTO %s
15+
// RUN: %clang --target=x86_64-scei-ps4 -flto=thin -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG-LTO %s
16+
// RUN: %clang --target=x86_64-scei-ps4 -flto=full -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG-LTO %s
1817

19-
// CHECK-DIAG-THIN-LTO: "-lto-thin-debug-options= -crash-diagnostics-dir=mydumps"
20-
// CHECK-DIAG-FULL-LTO: "-lto-debug-options= -crash-diagnostics-dir=mydumps"
18+
// CHECK-DIAG-LTO: "-lto-debug-options= -crash-diagnostics-dir=mydumps"

clang/test/Driver/ps5-linker.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
// Test the driver's control over the JustMyCode behavior with linker flags.
22

33
// RUN: %clang --target=x86_64-scei-ps5 -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-LIB %s
4-
// RUN: %clang --target=x86_64-scei-ps5 -flto -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK-LTO,CHECK-LIB %s
4+
// RUN: %clang --target=x86_64-scei-ps5 -flto -fjmc %s -### 2>&1 | FileCheck --check-prefixes=CHECK,CHECK-LIB %s
55

6-
// CHECK-NOT: -plugin-opt=-enable-jmc-instrument
7-
// CHECK-LTO: -plugin-opt=-enable-jmc-instrument
6+
// CHECK: -plugin-opt=-enable-jmc-instrument
87

98
// Check the default library name.
109
// CHECK-LIB: "--whole-archive" "-lSceJmc_nosubmission" "--no-whole-archive"
1110

1211
// Test the driver's control over the -fcrash-diagnostics-dir behavior with linker flags.
1312

1413
// RUN: %clang --target=x86_64-scei-ps5 -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG %s
15-
// RUN: %clang --target=x86_64-scei-ps5 -flto -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG-LTO %s
14+
// RUN: %clang --target=x86_64-scei-ps5 -flto -fcrash-diagnostics-dir=mydumps %s -### 2>&1 | FileCheck --check-prefixes=CHECK-DIAG %s
1615

17-
// CHECK-DIAG-NOT: -plugin-opt=-crash-diagnostics-dir=mydumps
18-
// CHECK-DIAG-LTO: -plugin-opt=-crash-diagnostics-dir=mydumps
16+
// CHECK-DIAG: -plugin-opt=-crash-diagnostics-dir=mydumps

clang/test/Driver/unified-lto.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@
77
// NOUNIT-NOT: "-flto-unit"
88

99
// RUN: %clang --target=x86_64-sie-ps5 -### %s -funified-lto 2>&1 | FileCheck --check-prefix=NOUNILTO %s
10-
// NOUNILTO: clang: warning: argument unused during compilation: '-funified-lto'
1110
// NOUNILTO: "-cc1"
1211
// NOUNILTO-NOT: "-funified-lto
12+
13+
// On PlayStation -funified-lto is the default. `-flto(=...)` influences the
14+
// `--lto=...` option passed to linker, unless `-fno-unified-lto` is supplied.
15+
// PS4:
16+
// RUN: %clang --target=x86_64-sie-ps4 -### %s 2>&1 | FileCheck --check-prefixes=LD,LTOFULL %s
17+
// RUN: %clang --target=x86_64-sie-ps4 -### %s -flto 2>&1 | FileCheck --check-prefixes=LD,LTOFULL %s
18+
// RUN: %clang --target=x86_64-sie-ps4 -### %s -flto=full 2>&1 | FileCheck --check-prefixes=LD,LTOFULL %s
19+
// RUN: %clang --target=x86_64-sie-ps4 -### %s -flto=thin 2>&1 | FileCheck --check-prefixes=LD,LTOTHIN %s
20+
// RUN: %clang --target=x86_64-sie-ps4 -### %s -fno-unified-lto -flto=full 2>&1 | FileCheck --check-prefixes=LD,NOLTO %s
21+
// RUN: %clang --target=x86_64-sie-ps4 -### %s -fno-unified-lto -flto=thin 2>&1 | FileCheck --check-prefixes=LD,NOLTO %s
22+
// PS5:
23+
// RUN: %clang --target=x86_64-sie-ps5 -### %s 2>&1 | FileCheck --check-prefixes=LD,LTOFULL %s
24+
// RUN: %clang --target=x86_64-sie-ps5 -### %s -flto 2>&1 | FileCheck --check-prefixes=LD,LTOFULL %s
25+
// RUN: %clang --target=x86_64-sie-ps5 -### %s -flto=full 2>&1 | FileCheck --check-prefixes=LD,LTOFULL %s
26+
// RUN: %clang --target=x86_64-sie-ps5 -### %s -flto=thin 2>&1 | FileCheck --check-prefixes=LD,LTOTHIN %s
27+
// RUN: %clang --target=x86_64-sie-ps5 -### %s -fno-unified-lto -flto=full 2>&1 | FileCheck --check-prefixes=LD,NOLTO %s
28+
// RUN: %clang --target=x86_64-sie-ps5 -### %s -fno-unified-lto -flto=thin 2>&1 | FileCheck --check-prefixes=LD,NOLTO %s
29+
30+
// LD: {{.*ld}}"
31+
// LTOFULL-SAME: "--lto=full"
32+
// LTOTHIN-SAME: "--lto=thin"
33+
// NOLTO-NOT: "--lto

0 commit comments

Comments
 (0)