1//===--- CommonArgs.cpp - Args handling for multiple toolchains -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Driver/CommonArgs.h"
10#include "Arch/AArch64.h"
11#include "Arch/ARM.h"
12#include "Arch/CSKY.h"
13#include "Arch/LoongArch.h"
14#include "Arch/M68k.h"
15#include "Arch/Mips.h"
16#include "Arch/PPC.h"
17#include "Arch/RISCV.h"
18#include "Arch/Sparc.h"
19#include "Arch/SystemZ.h"
20#include "Arch/VE.h"
21#include "Arch/X86.h"
22#include "HIPAMD.h"
23#include "Hexagon.h"
24#include "MSP430.h"
25#include "Solaris.h"
26#include "ToolChains/Cuda.h"
27#include "clang/Basic/CodeGenOptions.h"
28#include "clang/Config/config.h"
29#include "clang/Driver/Action.h"
30#include "clang/Driver/Compilation.h"
31#include "clang/Driver/Driver.h"
32#include "clang/Driver/InputInfo.h"
33#include "clang/Driver/Job.h"
34#include "clang/Driver/SanitizerArgs.h"
35#include "clang/Driver/ToolChain.h"
36#include "clang/Driver/Util.h"
37#include "clang/Driver/XRayArgs.h"
38#include "clang/Frontend/CompilerInvocation.h"
39#include "clang/Options/Options.h"
40#include "llvm/ADT/STLExtras.h"
41#include "llvm/ADT/SmallSet.h"
42#include "llvm/ADT/SmallString.h"
43#include "llvm/ADT/StringExtras.h"
44#include "llvm/ADT/StringSwitch.h"
45#include "llvm/ADT/Twine.h"
46#include "llvm/BinaryFormat/Magic.h"
47#include "llvm/Config/llvm-config.h"
48#include "llvm/Option/Arg.h"
49#include "llvm/Option/ArgList.h"
50#include "llvm/Option/Option.h"
51#include "llvm/Support/CodeGen.h"
52#include "llvm/Support/Compression.h"
53#include "llvm/Support/ErrorHandling.h"
54#include "llvm/Support/FileSystem.h"
55#include "llvm/Support/Path.h"
56#include "llvm/Support/Process.h"
57#include "llvm/Support/Program.h"
58#include "llvm/Support/Threading.h"
59#include "llvm/Support/VirtualFileSystem.h"
60#include "llvm/Support/YAMLParser.h"
61#include "llvm/TargetParser/AMDGPUTargetParser.h"
62#include "llvm/TargetParser/Host.h"
63#include "llvm/TargetParser/PPCTargetParser.h"
64#include <optional>
65
66using namespace clang::driver;
67using namespace clang::driver::tools;
68using namespace clang;
69using namespace llvm::opt;
70
71static bool useFramePointerForTargetByDefault(const llvm::opt::ArgList &Args,
72 const llvm::Triple &Triple) {
73 if (Args.hasArg(Ids: options::OPT_pg) && !Args.hasArg(Ids: options::OPT_mfentry))
74 return true;
75
76 if (Triple.isAndroid())
77 return true;
78
79 switch (Triple.getArch()) {
80 case llvm::Triple::xcore:
81 case llvm::Triple::wasm32:
82 case llvm::Triple::wasm64:
83 case llvm::Triple::msp430:
84 // XCore never wants frame pointers, regardless of OS.
85 // WebAssembly never wants frame pointers.
86 return false;
87 case llvm::Triple::ppc:
88 case llvm::Triple::ppcle:
89 case llvm::Triple::ppc64:
90 case llvm::Triple::ppc64le:
91 case llvm::Triple::riscv32:
92 case llvm::Triple::riscv64:
93 case llvm::Triple::riscv32be:
94 case llvm::Triple::riscv64be:
95 case llvm::Triple::sparc:
96 case llvm::Triple::sparcel:
97 case llvm::Triple::sparcv9:
98 case llvm::Triple::amdgcn:
99 case llvm::Triple::r600:
100 case llvm::Triple::csky:
101 case llvm::Triple::loongarch32:
102 case llvm::Triple::loongarch64:
103 case llvm::Triple::m68k:
104 case llvm::Triple::mips64:
105 case llvm::Triple::mips64el:
106 case llvm::Triple::mips:
107 case llvm::Triple::mipsel:
108 return !clang::driver::tools::areOptimizationsEnabled(Args);
109 default:
110 break;
111 }
112
113 if (Triple.isOSFuchsia() || Triple.isOSNetBSD()) {
114 return !clang::driver::tools::areOptimizationsEnabled(Args);
115 }
116
117 if (Triple.isOSLinux() || Triple.isOSHurd()) {
118 switch (Triple.getArch()) {
119 // Don't use a frame pointer on linux if optimizing for certain targets.
120 case llvm::Triple::arm:
121 case llvm::Triple::armeb:
122 case llvm::Triple::thumb:
123 case llvm::Triple::thumbeb:
124 case llvm::Triple::systemz:
125 case llvm::Triple::x86:
126 case llvm::Triple::x86_64:
127 return !clang::driver::tools::areOptimizationsEnabled(Args);
128 default:
129 return true;
130 }
131 }
132
133 if (Triple.isOSWindows()) {
134 switch (Triple.getArch()) {
135 case llvm::Triple::x86:
136 return !clang::driver::tools::areOptimizationsEnabled(Args);
137 case llvm::Triple::x86_64:
138 return Triple.isOSBinFormatMachO();
139 case llvm::Triple::arm:
140 case llvm::Triple::thumb:
141 // Windows on ARM builds with FPO disabled to aid fast stack walking
142 return true;
143 default:
144 // All other supported Windows ISAs use xdata unwind information, so frame
145 // pointers are not generally useful.
146 return false;
147 }
148 }
149
150 if (arm::isARMEABIBareMetal(Triple))
151 return false;
152
153 return true;
154}
155
156static bool useLeafFramePointerForTargetByDefault(const llvm::Triple &Triple) {
157 if (Triple.isAArch64() || Triple.isPS() || Triple.isVE() ||
158 (Triple.isAndroid() && !Triple.isARM()))
159 return false;
160
161 if ((Triple.isARM() || Triple.isThumb()) && Triple.isOSBinFormatMachO())
162 return false;
163
164 return true;
165}
166
167static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) {
168 switch (Triple.getArch()) {
169 default:
170 return false;
171 case llvm::Triple::arm:
172 case llvm::Triple::thumb:
173 // ARM Darwin targets require a frame pointer to be always present to aid
174 // offline debugging via backtraces.
175 return Triple.isOSDarwin();
176 }
177}
178
179// True if a target-specific option requires the frame chain to be preserved,
180// even if new frame records are not created.
181static bool mustMaintainValidFrameChain(const llvm::opt::ArgList &Args,
182 const llvm::Triple &Triple) {
183 switch (Triple.getArch()) {
184 default:
185 return false;
186 case llvm::Triple::arm:
187 case llvm::Triple::armeb:
188 case llvm::Triple::thumb:
189 case llvm::Triple::thumbeb:
190 // For 32-bit Arm, the -mframe-chain=aapcs and -mframe-chain=aapcs+leaf
191 // options require the frame pointer register to be reserved (or point to a
192 // new AAPCS-compilant frame record), even with -fno-omit-frame-pointer.
193 if (Arg *A = Args.getLastArg(Ids: options::OPT_mframe_chain)) {
194 StringRef V = A->getValue();
195 return V != "none";
196 }
197 return false;
198
199 case llvm::Triple::aarch64:
200 // Arm64 Windows requires that the frame chain is valid, as there is no
201 // way to indicate during a stack walk that a frame has used the frame
202 // pointer as a general purpose register.
203 return Triple.isOSWindows();
204 }
205}
206
207// True if a target-specific option causes -fno-omit-frame-pointer to also
208// cause frame records to be created in leaf functions.
209static bool framePointerImpliesLeafFramePointer(const llvm::opt::ArgList &Args,
210 const llvm::Triple &Triple) {
211 if (Triple.isARM() || Triple.isThumb()) {
212 // For 32-bit Arm, the -mframe-chain=aapcs+leaf option causes the
213 // -fno-omit-frame-pointer optiion to imply -mno-omit-leaf-frame-pointer,
214 // but does not by itself imply either option.
215 if (Arg *A = Args.getLastArg(Ids: options::OPT_mframe_chain)) {
216 StringRef V = A->getValue();
217 return V == "aapcs+leaf";
218 }
219 return false;
220 }
221 return false;
222}
223
224clang::CodeGenOptions::FramePointerKind
225getFramePointerKind(const llvm::opt::ArgList &Args,
226 const llvm::Triple &Triple) {
227 // There are four things to consider here:
228 // * Should a frame record be created for non-leaf functions?
229 // * Should a frame record be created for leaf functions?
230 // * Is the frame pointer register reserved in non-leaf functions?
231 // i.e. must it always point to either a new, valid frame record or be
232 // un-modified?
233 // * Is the frame pointer register reserved in leaf functions?
234 //
235 // Not all combinations of these are valid:
236 // * It's not useful to have leaf frame records without non-leaf ones.
237 // * It's not useful to have frame records without reserving the frame
238 // pointer.
239 //
240 // | Frame Setup | Reg Reserved |
241 // |-----------------|-----------------|
242 // | Non-leaf | Leaf | Non-Leaf | Leaf |
243 // |----------|------|----------|------|
244 // | N | N | N | N | FramePointerKind::None
245 // | N | N | N | Y | Invalid
246 // | N | N | Y | N | Invalid
247 // | N | N | Y | Y | FramePointerKind::Reserved
248 // | N | Y | N | N | Invalid
249 // | N | Y | N | Y | Invalid
250 // | N | Y | Y | N | Invalid
251 // | N | Y | Y | Y | Invalid
252 // | Y | N | N | N | Invalid
253 // | Y | N | N | Y | Invalid
254 // | Y | N | Y | N | FramePointerKind::NonLeafNoReserve
255 // | Y | N | Y | Y | FramePointerKind::NonLeaf
256 // | Y | Y | N | N | Invalid
257 // | Y | Y | N | Y | Invalid
258 // | Y | Y | Y | N | Invalid
259 // | Y | Y | Y | Y | FramePointerKind::All
260 //
261 // The FramePointerKind::Reserved case is currently only reachable for Arm,
262 // which has the -mframe-chain= option which can (in combination with
263 // -fno-omit-frame-pointer) specify that the frame chain must be valid,
264 // without requiring new frame records to be created.
265
266 bool DefaultFP = useFramePointerForTargetByDefault(Args, Triple);
267 bool EnableFP = mustUseNonLeafFramePointerForTarget(Triple) ||
268 Args.hasFlag(Pos: options::OPT_fno_omit_frame_pointer,
269 Neg: options::OPT_fomit_frame_pointer, Default: DefaultFP);
270
271 bool DefaultLeafFP =
272 useLeafFramePointerForTargetByDefault(Triple) ||
273 (EnableFP && framePointerImpliesLeafFramePointer(Args, Triple));
274 bool EnableLeafFP =
275 Args.hasFlag(Pos: options::OPT_mno_omit_leaf_frame_pointer,
276 Neg: options::OPT_momit_leaf_frame_pointer, Default: DefaultLeafFP);
277
278 bool FPRegReserved = Args.hasFlag(Pos: options::OPT_mreserve_frame_pointer_reg,
279 Neg: options::OPT_mno_reserve_frame_pointer_reg,
280 Default: mustMaintainValidFrameChain(Args, Triple));
281
282 if (EnableFP) {
283 if (EnableLeafFP)
284 return clang::CodeGenOptions::FramePointerKind::All;
285
286 if (FPRegReserved)
287 return clang::CodeGenOptions::FramePointerKind::NonLeaf;
288
289 return clang::CodeGenOptions::FramePointerKind::NonLeafNoReserve;
290 }
291 if (FPRegReserved)
292 return clang::CodeGenOptions::FramePointerKind::Reserved;
293 return clang::CodeGenOptions::FramePointerKind::None;
294}
295
296static void renderRpassOptions(const ArgList &Args, ArgStringList &CmdArgs,
297 const StringRef PluginOptPrefix) {
298 if (const Arg *A = Args.getLastArg(Ids: options::OPT_Rpass_EQ))
299 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
300 "-pass-remarks=" + A->getValue()));
301
302 if (const Arg *A = Args.getLastArg(Ids: options::OPT_Rpass_missed_EQ))
303 CmdArgs.push_back(Elt: Args.MakeArgString(
304 Str: Twine(PluginOptPrefix) + "-pass-remarks-missed=" + A->getValue()));
305
306 if (const Arg *A = Args.getLastArg(Ids: options::OPT_Rpass_analysis_EQ))
307 CmdArgs.push_back(Elt: Args.MakeArgString(
308 Str: Twine(PluginOptPrefix) + "-pass-remarks-analysis=" + A->getValue()));
309}
310
311static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
312 const llvm::Triple &Triple,
313 const InputInfo &Input,
314 const InputInfo &Output,
315 const StringRef PluginOptPrefix) {
316 StringRef Format = "yaml";
317 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fsave_optimization_record_EQ))
318 Format = A->getValue();
319
320 SmallString<128> F;
321 if (const Arg *A =
322 Args.getLastArg(Ids: options::OPT_foptimization_record_file_EQ)) {
323 F = A->getValue();
324 F += ".";
325 } else if (const Arg *A = Args.getLastArg(Ids: options::OPT_dumpdir)) {
326 F = A->getValue();
327 } else if (Output.isFilename()) {
328 F = Output.getFilename();
329 F += ".";
330 }
331
332 assert(!F.empty() && "Cannot determine remarks output name.");
333 // Append "opt.ld.<format>" to the end of the file name.
334 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
335 "opt-remarks-filename=" + F + "opt.ld." +
336 Format));
337
338 if (const Arg *A =
339 Args.getLastArg(Ids: options::OPT_foptimization_record_passes_EQ))
340 CmdArgs.push_back(Elt: Args.MakeArgString(
341 Str: Twine(PluginOptPrefix) + "opt-remarks-passes=" + A->getValue()));
342
343 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
344 "opt-remarks-format=" + Format.data()));
345}
346
347static void renderRemarksHotnessOptions(const ArgList &Args,
348 ArgStringList &CmdArgs,
349 const StringRef PluginOptPrefix) {
350 if (Args.hasFlag(Pos: options::OPT_fdiagnostics_show_hotness,
351 Neg: options::OPT_fno_diagnostics_show_hotness, Default: false))
352 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
353 "opt-remarks-with-hotness"));
354
355 if (const Arg *A =
356 Args.getLastArg(Ids: options::OPT_fdiagnostics_hotness_threshold_EQ))
357 CmdArgs.push_back(
358 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
359 "opt-remarks-hotness-threshold=" + A->getValue()));
360}
361
362static bool shouldIgnoreUnsupportedTargetFeature(const Arg &TargetFeatureArg,
363 llvm::Triple T,
364 StringRef Processor) {
365 // Warn no-cumode for AMDGCN processors not supporing WGP mode.
366 if (!T.isAMDGPU())
367 return false;
368 auto GPUKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(CPU: Processor)
369 : llvm::AMDGPU::parseArchR600(CPU: Processor);
370 auto GPUFeatures = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(AK: GPUKind)
371 : llvm::AMDGPU::getArchAttrR600(AK: GPUKind);
372 if (GPUFeatures & llvm::AMDGPU::FEATURE_WGP)
373 return false;
374 return TargetFeatureArg.getOption().matches(ID: options::OPT_mno_cumode);
375}
376
377void tools::addPathIfExists(const Driver &D, const Twine &Path,
378 ToolChain::path_list &Paths) {
379 if (D.getVFS().exists(Path))
380 Paths.push_back(Elt: Path.str());
381}
382
383void tools::handleTargetFeaturesGroup(const Driver &D,
384 const llvm::Triple &Triple,
385 const ArgList &Args,
386 std::vector<StringRef> &Features,
387 OptSpecifier Group) {
388 std::set<StringRef> Warned;
389 for (const Arg *A : Args.filtered(Ids: Group)) {
390 StringRef Name = A->getOption().getName();
391 A->claim();
392
393 // Skip over "-m".
394 assert(Name.starts_with("m") && "Invalid feature name.");
395 Name = Name.substr(Start: 1);
396
397 auto Proc = getCPUName(D, Args, T: Triple);
398 if (shouldIgnoreUnsupportedTargetFeature(TargetFeatureArg: *A, T: Triple, Processor: Proc)) {
399 if (Warned.count(x: Name) == 0) {
400 D.getDiags().Report(
401 DiagID: clang::diag::warn_drv_unsupported_option_for_processor)
402 << A->getAsString(Args) << Proc;
403 Warned.insert(x: Name);
404 }
405 continue;
406 }
407
408 bool IsNegative = Name.consume_front(Prefix: "no-");
409
410 Features.push_back(x: Args.MakeArgString(Str: (IsNegative ? "-" : "+") + Name));
411 }
412}
413
414SmallVector<StringRef>
415tools::unifyTargetFeatures(ArrayRef<StringRef> Features) {
416 // Only add a feature if it hasn't been seen before starting from the end.
417 SmallVector<StringRef> UnifiedFeatures;
418 llvm::DenseSet<StringRef> UsedFeatures;
419 for (StringRef Feature : llvm::reverse(C&: Features)) {
420 if (UsedFeatures.insert(V: Feature.drop_front()).second)
421 UnifiedFeatures.insert(I: UnifiedFeatures.begin(), Elt: Feature);
422 }
423
424 return UnifiedFeatures;
425}
426
427void tools::addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs,
428 const char *ArgName, const char *EnvVar) {
429 const char *DirList = ::getenv(name: EnvVar);
430 bool CombinedArg = false;
431
432 if (!DirList)
433 return; // Nothing to do.
434
435 StringRef Name(ArgName);
436 if (Name == "-I" || Name == "-L" || Name.empty())
437 CombinedArg = true;
438
439 StringRef Dirs(DirList);
440 if (Dirs.empty()) // Empty string should not add '.'.
441 return;
442
443 StringRef::size_type Delim;
444 while ((Delim = Dirs.find(C: llvm::sys::EnvPathSeparator)) != StringRef::npos) {
445 if (Delim == 0) { // Leading colon.
446 if (CombinedArg) {
447 CmdArgs.push_back(Elt: Args.MakeArgString(Str: std::string(ArgName) + "."));
448 } else {
449 CmdArgs.push_back(Elt: ArgName);
450 CmdArgs.push_back(Elt: ".");
451 }
452 } else {
453 if (CombinedArg) {
454 CmdArgs.push_back(
455 Elt: Args.MakeArgString(Str: std::string(ArgName) + Dirs.substr(Start: 0, N: Delim)));
456 } else {
457 CmdArgs.push_back(Elt: ArgName);
458 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Dirs.substr(Start: 0, N: Delim)));
459 }
460 }
461 Dirs = Dirs.substr(Start: Delim + 1);
462 }
463
464 if (Dirs.empty()) { // Trailing colon.
465 if (CombinedArg) {
466 CmdArgs.push_back(Elt: Args.MakeArgString(Str: std::string(ArgName) + "."));
467 } else {
468 CmdArgs.push_back(Elt: ArgName);
469 CmdArgs.push_back(Elt: ".");
470 }
471 } else { // Add the last path.
472 if (CombinedArg) {
473 CmdArgs.push_back(Elt: Args.MakeArgString(Str: std::string(ArgName) + Dirs));
474 } else {
475 CmdArgs.push_back(Elt: ArgName);
476 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Dirs));
477 }
478 }
479}
480
481void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs,
482 const ArgList &Args, ArgStringList &CmdArgs,
483 const JobAction &JA) {
484 const Driver &D = TC.getDriver();
485
486 // Add extra linker input arguments which are not treated as inputs
487 // (constructed via -Xarch_).
488 Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Zlinker_input);
489
490 // LIBRARY_PATH are included before user inputs and only supported on native
491 // toolchains.
492 if (!TC.isCrossCompiling())
493 addDirectoryList(Args, CmdArgs, ArgName: "-L", EnvVar: "LIBRARY_PATH");
494
495 for (const auto &II : Inputs) {
496 // If the current tool chain refers to an OpenMP offloading host, we
497 // should ignore inputs that refer to OpenMP offloading devices -
498 // they will be embedded according to a proper linker script.
499 if (auto *IA = II.getAction())
500 if ((JA.isHostOffloading(OKind: Action::OFK_OpenMP) &&
501 IA->isDeviceOffloading(OKind: Action::OFK_OpenMP)))
502 continue;
503
504 if (!TC.HasNativeLLVMSupport() && types::isLLVMIR(Id: II.getType()))
505 // Don't try to pass LLVM inputs unless we have native support.
506 D.Diag(DiagID: diag::err_drv_no_linker_llvm_support) << TC.getTripleString();
507
508 // Add filenames immediately.
509 if (II.isFilename()) {
510 CmdArgs.push_back(Elt: II.getFilename());
511 continue;
512 }
513
514 // In some error cases, the input could be Nothing; skip those.
515 if (II.isNothing())
516 continue;
517
518 // Otherwise, this is a linker input argument.
519 const Arg &A = II.getInputArg();
520
521 // Handle reserved library options.
522 if (A.getOption().matches(ID: options::OPT_Z_reserved_lib_stdcxx))
523 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
524 else if (A.getOption().matches(ID: options::OPT_Z_reserved_lib_cckext))
525 TC.AddCCKextLibArgs(Args, CmdArgs);
526 // Do not pass OPT_rpath to linker in AIX
527 else if (A.getOption().matches(ID: options::OPT_rpath) &&
528 TC.getTriple().isOSAIX())
529 continue;
530 else
531 A.renderAsInput(Args, Output&: CmdArgs);
532 }
533 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fveclib)) {
534 const llvm::Triple &Triple = TC.getTriple();
535 StringRef V = A->getValue();
536 if (V == "ArmPL" && (Triple.isOSLinux() || Triple.isOSDarwin())) {
537 // To support -fveclib=ArmPL we need to link against libamath. Some of the
538 // libamath functions depend on libm, at the same time, libamath exports
539 // its own implementation of some of the libm functions. These are faster
540 // and potentially less accurate implementations, hence we need to be
541 // careful what is being linked in. Since here we are interested only in
542 // the subset of libamath functions that is covered by the veclib
543 // mappings, we need to prioritize libm functions by putting -lm before
544 // -lamath (and then -lm again, to fulfill libamath requirements).
545 //
546 // Therefore we need to do the following:
547 //
548 // 1. On Linux, link only when actually needed.
549 //
550 // 2. Prefer libm functions over libamath (when no -nostdlib in use).
551 //
552 // 3. Link against libm to resolve libamath dependencies.
553 //
554 if (Triple.isOSLinux()) {
555 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--push-state"));
556 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--as-needed"));
557 }
558 if (!Args.hasArg(Ids: options::OPT_nostdlib))
559 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-lm"));
560 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-lamath"));
561 if (!Args.hasArg(Ids: options::OPT_nostdlib))
562 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-lm"));
563 if (Triple.isOSLinux())
564 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--pop-state"));
565 addArchSpecificRPath(TC, Args, CmdArgs);
566 }
567 }
568}
569
570const char *tools::getLDMOption(const llvm::Triple &T, const ArgList &Args) {
571 switch (T.getArch()) {
572 case llvm::Triple::x86:
573 if (T.isOSIAMCU())
574 return "elf_iamcu";
575 return "elf_i386";
576 case llvm::Triple::aarch64:
577 if (T.isOSManagarm())
578 return "aarch64managarm";
579 else if (aarch64::isAArch64BareMetal(Triple: T))
580 return "aarch64elf";
581 return "aarch64linux";
582 case llvm::Triple::aarch64_be:
583 if (aarch64::isAArch64BareMetal(Triple: T))
584 return "aarch64elfb";
585 return "aarch64linuxb";
586 case llvm::Triple::arm:
587 case llvm::Triple::thumb:
588 case llvm::Triple::armeb:
589 case llvm::Triple::thumbeb: {
590 bool IsBigEndian = tools::arm::isARMBigEndian(Triple: T, Args);
591 if (arm::isARMEABIBareMetal(Triple: T))
592 return IsBigEndian ? "armelfb" : "armelf";
593 return IsBigEndian ? "armelfb_linux_eabi" : "armelf_linux_eabi";
594 }
595 case llvm::Triple::m68k:
596 return "m68kelf";
597 case llvm::Triple::ppc:
598 if (T.isOSLinux())
599 return "elf32ppclinux";
600 return "elf32ppc";
601 case llvm::Triple::ppcle:
602 if (T.isOSLinux())
603 return "elf32lppclinux";
604 return "elf32lppc";
605 case llvm::Triple::ppc64:
606 return "elf64ppc";
607 case llvm::Triple::ppc64le:
608 return "elf64lppc";
609 case llvm::Triple::riscv32:
610 return "elf32lriscv";
611 case llvm::Triple::riscv64:
612 return "elf64lriscv";
613 case llvm::Triple::riscv32be:
614 return "elf32briscv";
615 case llvm::Triple::riscv64be:
616 return "elf64briscv";
617 case llvm::Triple::sparc:
618 case llvm::Triple::sparcel:
619 return "elf32_sparc";
620 case llvm::Triple::sparcv9:
621 return "elf64_sparc";
622 case llvm::Triple::loongarch32:
623 return "elf32loongarch";
624 case llvm::Triple::loongarch64:
625 return "elf64loongarch";
626 case llvm::Triple::mips:
627 return "elf32btsmip";
628 case llvm::Triple::mipsel:
629 return "elf32ltsmip";
630 case llvm::Triple::mips64:
631 if (tools::mips::hasMipsAbiArg(Args, Value: "n32") || T.isABIN32())
632 return "elf32btsmipn32";
633 return "elf64btsmip";
634 case llvm::Triple::mips64el:
635 if (tools::mips::hasMipsAbiArg(Args, Value: "n32") || T.isABIN32())
636 return "elf32ltsmipn32";
637 return "elf64ltsmip";
638 case llvm::Triple::systemz:
639 return "elf64_s390";
640 case llvm::Triple::x86_64:
641 if (T.isX32())
642 return "elf32_x86_64";
643 return "elf_x86_64";
644 case llvm::Triple::ve:
645 return "elf64ve";
646 case llvm::Triple::csky:
647 return "cskyelf_linux";
648 default:
649 return nullptr;
650 }
651}
652
653void tools::addLinkerCompressDebugSectionsOption(
654 const ToolChain &TC, const llvm::opt::ArgList &Args,
655 llvm::opt::ArgStringList &CmdArgs) {
656 // GNU ld supports --compress-debug-sections=none|zlib|zlib-gnu|zlib-gabi
657 // whereas zlib is an alias to zlib-gabi and zlib-gnu is obsoleted. Therefore
658 // -gz=none|zlib are translated to --compress-debug-sections=none|zlib. -gz
659 // is not translated since ld --compress-debug-sections option requires an
660 // argument.
661 if (const Arg *A = Args.getLastArg(Ids: options::OPT_gz_EQ)) {
662 StringRef V = A->getValue();
663 if (V == "none" || V == "zlib" || V == "zstd")
664 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--compress-debug-sections=" + V));
665 else
666 TC.getDriver().Diag(DiagID: diag::err_drv_unsupported_option_argument)
667 << A->getSpelling() << V;
668 }
669}
670
671void tools::AddTargetFeature(const ArgList &Args,
672 std::vector<StringRef> &Features,
673 OptSpecifier OnOpt, OptSpecifier OffOpt,
674 StringRef FeatureName) {
675 if (Arg *A = Args.getLastArg(Ids: OnOpt, Ids: OffOpt)) {
676 if (A->getOption().matches(ID: OnOpt))
677 Features.push_back(x: Args.MakeArgString(Str: "+" + FeatureName));
678 else
679 Features.push_back(x: Args.MakeArgString(Str: "-" + FeatureName));
680 }
681}
682
683/// Get the (LLVM) name of the AMDGPU gpu we are targeting.
684static std::string getAMDGPUTargetGPU(const llvm::Triple &T,
685 const ArgList &Args) {
686 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) {
687 auto GPUName = getProcessorFromTargetID(T, OffloadArch: A->getValue());
688 return llvm::StringSwitch<std::string>(GPUName)
689 .Cases(CaseStrings: {"rv630", "rv635"}, Value: "r600")
690 .Cases(CaseStrings: {"rv610", "rv620", "rs780"}, Value: "rs880")
691 .Case(S: "rv740", Value: "rv770")
692 .Case(S: "palm", Value: "cedar")
693 .Cases(CaseStrings: {"sumo", "sumo2"}, Value: "sumo")
694 .Case(S: "hemlock", Value: "cypress")
695 .Case(S: "aruba", Value: "cayman")
696 .Default(Value: GPUName.str());
697 }
698 return "";
699}
700
701static std::string getLanaiTargetCPU(const ArgList &Args) {
702 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) {
703 return A->getValue();
704 }
705 return "";
706}
707
708/// Get the (LLVM) name of the WebAssembly cpu we are targeting.
709static StringRef getWebAssemblyTargetCPU(const ArgList &Args) {
710 // If we have -mcpu=, use that.
711 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ)) {
712 StringRef CPU = A->getValue();
713
714#ifdef __wasm__
715 // Handle "native" by examining the host. "native" isn't meaningful when
716 // cross compiling, so only support this when the host is also WebAssembly.
717 if (CPU == "native")
718 return llvm::sys::getHostCPUName();
719#endif
720
721 return CPU;
722 }
723
724 return "generic";
725}
726
727std::string tools::getCPUName(const Driver &D, const ArgList &Args,
728 const llvm::Triple &T, bool FromAs) {
729 Arg *A;
730
731 switch (T.getArch()) {
732 default:
733 return "";
734
735 case llvm::Triple::aarch64:
736 case llvm::Triple::aarch64_32:
737 case llvm::Triple::aarch64_be:
738 return aarch64::getAArch64TargetCPU(Args, Triple: T, A);
739
740 case llvm::Triple::arm:
741 case llvm::Triple::armeb:
742 case llvm::Triple::thumb:
743 case llvm::Triple::thumbeb: {
744 StringRef MArch, MCPU;
745 arm::getARMArchCPUFromArgs(Args, Arch&: MArch, CPU&: MCPU, FromAs);
746 return arm::getARMTargetCPU(CPU: MCPU, Arch: MArch, Triple: T);
747 }
748
749 case llvm::Triple::avr:
750 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mmcu_EQ))
751 return A->getValue();
752 return "";
753
754 case llvm::Triple::m68k:
755 return m68k::getM68kTargetCPU(Args);
756
757 case llvm::Triple::mips:
758 case llvm::Triple::mipsel:
759 case llvm::Triple::mips64:
760 case llvm::Triple::mips64el: {
761 StringRef CPUName;
762 StringRef ABIName;
763 mips::getMipsCPUAndABI(Args, Triple: T, CPUName, ABIName);
764 return std::string(CPUName);
765 }
766
767 case llvm::Triple::nvptx:
768 case llvm::Triple::nvptx64:
769 if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ))
770 return A->getValue();
771 return "";
772
773 case llvm::Triple::ppc:
774 case llvm::Triple::ppcle:
775 case llvm::Triple::ppc64:
776 case llvm::Triple::ppc64le:
777 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ))
778 return std::string(
779 llvm::PPC::getNormalizedPPCTargetCPU(T, CPUName: A->getValue()));
780 return std::string(llvm::PPC::getNormalizedPPCTargetCPU(T));
781
782 case llvm::Triple::csky:
783 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ))
784 return A->getValue();
785 else if (const Arg *A = Args.getLastArg(Ids: options::OPT_march_EQ))
786 return A->getValue();
787 else
788 return "ck810";
789 case llvm::Triple::riscv32:
790 case llvm::Triple::riscv64:
791 case llvm::Triple::riscv32be:
792 case llvm::Triple::riscv64be:
793 return riscv::getRISCVTargetCPU(Args, Triple: T);
794
795 case llvm::Triple::bpfel:
796 case llvm::Triple::bpfeb:
797 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ))
798 return A->getValue();
799 return "";
800
801 case llvm::Triple::sparc:
802 case llvm::Triple::sparcel:
803 case llvm::Triple::sparcv9:
804 return sparc::getSparcTargetCPU(D, Args, Triple: T);
805
806 case llvm::Triple::x86:
807 case llvm::Triple::x86_64:
808 return x86::getX86TargetCPU(D, Args, Triple: T);
809
810 case llvm::Triple::hexagon:
811 return "hexagon" +
812 toolchains::HexagonToolChain::GetTargetCPUVersion(Args).str();
813
814 case llvm::Triple::lanai:
815 return getLanaiTargetCPU(Args);
816
817 case llvm::Triple::systemz:
818 return systemz::getSystemZTargetCPU(Args, T);
819
820 case llvm::Triple::r600:
821 case llvm::Triple::amdgcn:
822 return getAMDGPUTargetGPU(T, Args);
823
824 case llvm::Triple::wasm32:
825 case llvm::Triple::wasm64:
826 return std::string(getWebAssemblyTargetCPU(Args));
827
828 case llvm::Triple::loongarch32:
829 case llvm::Triple::loongarch64:
830 return loongarch::getLoongArchTargetCPU(Args, Triple: T);
831
832 case llvm::Triple::xtensa:
833 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mcpu_EQ))
834 return A->getValue();
835 return "";
836 }
837}
838
839static void getWebAssemblyTargetFeatures(const Driver &D,
840 const llvm::Triple &Triple,
841 const ArgList &Args,
842 std::vector<StringRef> &Features) {
843 handleTargetFeaturesGroup(D, Triple, Args, Features,
844 Group: options::OPT_m_wasm_Features_Group);
845}
846
847void tools::getTargetFeatures(const Driver &D, const llvm::Triple &Triple,
848 const ArgList &Args, ArgStringList &CmdArgs,
849 bool ForAS, bool IsAux) {
850 std::vector<StringRef> Features;
851 switch (Triple.getArch()) {
852 default:
853 break;
854 case llvm::Triple::mips:
855 case llvm::Triple::mipsel:
856 case llvm::Triple::mips64:
857 case llvm::Triple::mips64el:
858 mips::getMIPSTargetFeatures(D, Triple, Args, Features);
859 break;
860 case llvm::Triple::arm:
861 case llvm::Triple::armeb:
862 case llvm::Triple::thumb:
863 case llvm::Triple::thumbeb:
864 arm::getARMTargetFeatures(D, Triple, Args, Features, ForAS);
865 break;
866 case llvm::Triple::ppc:
867 case llvm::Triple::ppcle:
868 case llvm::Triple::ppc64:
869 case llvm::Triple::ppc64le:
870 ppc::getPPCTargetFeatures(D, Triple, Args, Features);
871 break;
872 case llvm::Triple::riscv32:
873 case llvm::Triple::riscv64:
874 case llvm::Triple::riscv32be:
875 case llvm::Triple::riscv64be:
876 riscv::getRISCVTargetFeatures(D, Triple, Args, Features);
877 break;
878 case llvm::Triple::systemz:
879 systemz::getSystemZTargetFeatures(D, Args, Features);
880 break;
881 case llvm::Triple::aarch64:
882 case llvm::Triple::aarch64_32:
883 case llvm::Triple::aarch64_be:
884 aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS);
885 break;
886 case llvm::Triple::x86:
887 case llvm::Triple::x86_64:
888 x86::getX86TargetFeatures(D, Triple, Args, Features);
889 break;
890 case llvm::Triple::hexagon:
891 hexagon::getHexagonTargetFeatures(D, Triple, Args, Features);
892 break;
893 case llvm::Triple::wasm32:
894 case llvm::Triple::wasm64:
895 getWebAssemblyTargetFeatures(D, Triple, Args, Features);
896 break;
897 case llvm::Triple::sparc:
898 case llvm::Triple::sparcel:
899 case llvm::Triple::sparcv9:
900 sparc::getSparcTargetFeatures(D, Triple, Args, Features);
901 break;
902 case llvm::Triple::r600:
903 case llvm::Triple::amdgcn:
904 amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features);
905 break;
906 case llvm::Triple::nvptx:
907 case llvm::Triple::nvptx64:
908 NVPTX::getNVPTXTargetFeatures(D, Triple, Args, Features);
909 break;
910 case llvm::Triple::m68k:
911 m68k::getM68kTargetFeatures(D, Triple, Args, Features);
912 break;
913 case llvm::Triple::msp430:
914 msp430::getMSP430TargetFeatures(D, Args, Features);
915 break;
916 case llvm::Triple::ve:
917 ve::getVETargetFeatures(D, Args, Features);
918 break;
919 case llvm::Triple::csky:
920 csky::getCSKYTargetFeatures(D, Triple, Args, CmdArgs, Features);
921 break;
922 case llvm::Triple::loongarch32:
923 case llvm::Triple::loongarch64:
924 loongarch::getLoongArchTargetFeatures(D, Triple, Args, Features);
925 break;
926 }
927
928 for (auto Feature : unifyTargetFeatures(Features)) {
929 CmdArgs.push_back(Elt: IsAux ? "-aux-target-feature" : "-target-feature");
930 CmdArgs.push_back(Elt: Feature.data());
931 }
932}
933
934llvm::StringRef tools::getLTOParallelism(const ArgList &Args, const Driver &D) {
935 Arg *LtoJobsArg = Args.getLastArg(Ids: options::OPT_flto_jobs_EQ);
936 if (!LtoJobsArg)
937 return {};
938 if (!llvm::get_threadpool_strategy(Num: LtoJobsArg->getValue()))
939 D.Diag(DiagID: diag::err_drv_invalid_int_value)
940 << LtoJobsArg->getAsString(Args) << LtoJobsArg->getValue();
941 return LtoJobsArg->getValue();
942}
943
944// PS4/PS5 uses -ffunction-sections and -fdata-sections by default.
945bool tools::isUseSeparateSections(const llvm::Triple &Triple) {
946 return Triple.isPS();
947}
948
949void tools::addSeparateSectionFlags(const llvm::Triple &Triple,
950 const ArgList &Args,
951 ArgStringList &CmdArgs) {
952 bool UseSeparateSections = isUseSeparateSections(Triple);
953 if (Args.hasFlag(Pos: options::OPT_ffunction_sections,
954 Neg: options::OPT_fno_function_sections, Default: UseSeparateSections))
955 CmdArgs.push_back(Elt: "-ffunction-sections");
956
957 bool HasDefaultDataSections = Triple.isOSBinFormatXCOFF();
958 if (Args.hasFlag(Pos: options::OPT_fdata_sections, Neg: options::OPT_fno_data_sections,
959 Default: UseSeparateSections || HasDefaultDataSections))
960 CmdArgs.push_back(Elt: "-fdata-sections");
961}
962
963bool tools::isTLSDESCEnabled(const ToolChain &TC,
964 const llvm::opt::ArgList &Args) {
965 const llvm::Triple &Triple = TC.getEffectiveTriple();
966 Arg *A = Args.getLastArg(Ids: options::OPT_mtls_dialect_EQ);
967 if (!A)
968 return Triple.hasDefaultTLSDESC();
969 StringRef V = A->getValue();
970 bool SupportedArgument = false, EnableTLSDESC = false;
971 bool Unsupported = !Triple.isOSBinFormatELF();
972 if (Triple.isLoongArch() || Triple.isRISCV()) {
973 SupportedArgument = V == "desc" || V == "trad";
974 EnableTLSDESC = V == "desc";
975 } else if (Triple.isX86()) {
976 SupportedArgument = V == "gnu" || V == "gnu2";
977 EnableTLSDESC = V == "gnu2";
978 } else {
979 Unsupported = true;
980 }
981 if (Unsupported) {
982 TC.getDriver().Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
983 << A->getSpelling() << Triple.getTriple();
984 } else if (!SupportedArgument) {
985 TC.getDriver().Diag(DiagID: diag::err_drv_unsupported_option_argument_for_target)
986 << A->getSpelling() << V << Triple.getTriple();
987 }
988 return EnableTLSDESC;
989}
990
991void tools::addDTLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
992 llvm::opt::ArgStringList &CmdArgs) {
993 if (Arg *A = Args.getLastArg(Ids: options::OPT_fthinlto_distributor_EQ)) {
994 CmdArgs.push_back(
995 Elt: Args.MakeArgString(Str: "--thinlto-distributor=" + Twine(A->getValue())));
996 const Driver &D = ToolChain.getDriver();
997 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--thinlto-remote-compiler=" +
998 Twine(D.getDriverProgramPath())));
999 if (auto *PA = D.getPrependArg())
1000 CmdArgs.push_back(Elt: Args.MakeArgString(
1001 Str: "--thinlto-remote-compiler-prepend-arg=" + Twine(PA)));
1002
1003 for (const auto &A :
1004 Args.getAllArgValues(Id: options::OPT_Xthinlto_distributor_EQ))
1005 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--thinlto-distributor-arg=" + A));
1006 }
1007}
1008
1009void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
1010 ArgStringList &CmdArgs, const InputInfo &Output,
1011 const InputInfoList &Inputs, bool IsThinLTO) {
1012 const llvm::Triple &Triple = ToolChain.getTriple();
1013 const bool IsOSAIX = Triple.isOSAIX();
1014 const bool IsAMDGCN = Triple.isAMDGCN();
1015 StringRef Linker = Args.getLastArgValue(Id: options::OPT_fuse_ld_EQ);
1016 const char *LinkerPath = Args.MakeArgString(Str: ToolChain.GetLinkerPath());
1017 const Driver &D = ToolChain.getDriver();
1018 const bool IsFatLTO = Args.hasFlag(Pos: options::OPT_ffat_lto_objects,
1019 Neg: options::OPT_fno_fat_lto_objects, Default: false);
1020 const bool IsUnifiedLTO = Args.hasArg(Ids: options::OPT_funified_lto);
1021
1022 assert(!Inputs.empty() && "Must have at least one input.");
1023
1024 auto Input = llvm::find_if(
1025 Range: Inputs, P: [](const InputInfo &II) -> bool { return II.isFilename(); });
1026 if (Input == Inputs.end()) {
1027 // For a very rare case, all of the inputs to the linker are
1028 // InputArg. If that happens, just use the first InputInfo.
1029 Input = Inputs.begin();
1030 }
1031
1032 if (Linker != "lld" && Linker != "lld-link" &&
1033 llvm::sys::path::filename(path: LinkerPath) != "ld.lld" &&
1034 llvm::sys::path::stem(path: LinkerPath) != "ld.lld" && !Triple.isOSOpenBSD()) {
1035 // Tell the linker to load the plugin. This has to come before
1036 // AddLinkerInputs as gold requires -plugin and AIX ld requires -bplugin to
1037 // come before any -plugin-opt/-bplugin_opt that -Wl might forward.
1038 const char *PluginPrefix = IsOSAIX ? "-bplugin:" : "";
1039 const char *PluginName = IsOSAIX ? "/libLTO" : "/LLVMgold";
1040
1041 if (!IsOSAIX)
1042 CmdArgs.push_back(Elt: "-plugin");
1043
1044#if defined(_WIN32)
1045 const char *Suffix = ".dll";
1046#elif defined(__APPLE__)
1047 const char *Suffix = ".dylib";
1048#else
1049 const char *Suffix = ".so";
1050#endif
1051
1052 SmallString<1024> Plugin;
1053 llvm::sys::path::native(path: Twine(D.Dir) +
1054 "/../" CLANG_INSTALL_LIBDIR_BASENAME +
1055 PluginName + Suffix,
1056 result&: Plugin);
1057 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginPrefix) + Plugin));
1058 } else {
1059 // Tell LLD to find and use .llvm.lto section in regular relocatable object
1060 // files
1061 if (IsFatLTO)
1062 CmdArgs.push_back(Elt: "--fat-lto-objects");
1063
1064 if (Args.hasArg(Ids: options::OPT_flto_partitions_EQ)) {
1065 int Value = 0;
1066 StringRef A = Args.getLastArgValue(Id: options::OPT_flto_partitions_EQ, Default: "8");
1067 if (A.getAsInteger(Radix: 10, Result&: Value) || (Value < 1)) {
1068 Arg *Arg = Args.getLastArg(Ids: options::OPT_flto_partitions_EQ);
1069 D.Diag(DiagID: diag::err_drv_invalid_int_value)
1070 << Arg->getAsString(Args) << Arg->getValue();
1071 }
1072 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--lto-partitions=" + A));
1073 }
1074 }
1075
1076 const char *PluginOptPrefix = IsOSAIX ? "-bplugin_opt:" : "-plugin-opt=";
1077 const char *ExtraDash = IsOSAIX ? "-" : "";
1078 const char *ParallelismOpt = IsOSAIX ? "-threads=" : "jobs=";
1079
1080 // Note, this solution is far from perfect, better to encode it into IR
1081 // metadata, but this may not be worth it, since it looks like aranges is on
1082 // the way out.
1083 if (Args.hasArg(Ids: options::OPT_gdwarf_aranges)) {
1084 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
1085 "-generate-arange-section"));
1086 }
1087
1088 // Pass vector library arguments to LTO.
1089 Arg *ArgVecLib = Args.getLastArg(Ids: options::OPT_fveclib);
1090 if (ArgVecLib && ArgVecLib->getNumValues() == 1) {
1091 // Map the vector library names from clang front-end to opt front-end. The
1092 // values are taken from the TargetLibraryInfo class command line options.
1093 std::optional<StringRef> OptVal =
1094 llvm::StringSwitch<std::optional<StringRef>>(ArgVecLib->getValue())
1095 .Case(S: "Accelerate", Value: "Accelerate")
1096 .Case(S: "libmvec", Value: "LIBMVEC")
1097 .Case(S: "AMDLIBM", Value: "AMDLIBM")
1098 .Case(S: "MASSV", Value: "MASSV")
1099 .Case(S: "SVML", Value: "SVML")
1100 .Case(S: "SLEEF", Value: "sleefgnuabi")
1101 .Case(S: "Darwin_libsystem_m", Value: "Darwin_libsystem_m")
1102 .Case(S: "ArmPL", Value: "ArmPL")
1103 .Case(S: "none", Value: "none")
1104 .Default(Value: std::nullopt);
1105
1106 if (OptVal)
1107 CmdArgs.push_back(Elt: Args.MakeArgString(
1108 Str: Twine(PluginOptPrefix) + "-vector-library=" + OptVal.value()));
1109 }
1110
1111 // Try to pass driver level flags relevant to LTO code generation down to
1112 // the plugin.
1113
1114 // Handle flags for selecting CPU variants.
1115 std::string CPU = getCPUName(D, Args, T: Triple);
1116 if (!CPU.empty())
1117 CmdArgs.push_back(
1118 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + ExtraDash + "mcpu=" + CPU));
1119
1120 if (Args.getLastArg(Ids: options::OPT_O_Group)) {
1121 unsigned OptimizationLevel =
1122 getOptimizationLevel(Args, IK: InputKind(), Diags&: D.getDiags());
1123 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + ExtraDash +
1124 "O" + Twine(OptimizationLevel)));
1125 if (IsAMDGCN)
1126 CmdArgs.push_back(
1127 Elt: Args.MakeArgString(Str: Twine("--lto-CGO") + Twine(OptimizationLevel)));
1128 }
1129
1130 if (Args.hasArg(Ids: options::OPT_gsplit_dwarf)) {
1131 SmallString<128> F;
1132 if (const Arg *A = Args.getLastArg(Ids: options::OPT_dumpdir)) {
1133 F = A->getValue();
1134 } else {
1135 F = Output.getFilename();
1136 F += "_";
1137 }
1138 CmdArgs.push_back(
1139 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "dwo_dir=" + F + "dwo"));
1140 }
1141
1142 if (IsThinLTO && !IsOSAIX)
1143 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "thinlto"));
1144 else if (IsThinLTO && IsOSAIX)
1145 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-bdbg:thinlto")));
1146
1147 // Matrix intrinsic lowering happens at link time with ThinLTO. Enable
1148 // LowerMatrixIntrinsicsPass, which is transitively called by
1149 // buildThinLTODefaultPipeline under EnableMatrix.
1150 if ((IsThinLTO || IsFatLTO || IsUnifiedLTO) &&
1151 Args.hasArg(Ids: options::OPT_fenable_matrix))
1152 CmdArgs.push_back(
1153 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-enable-matrix"));
1154
1155 StringRef Parallelism = getLTOParallelism(Args, D);
1156 if (!Parallelism.empty())
1157 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
1158 ParallelismOpt + Parallelism));
1159
1160 // Forward the SLP vectorization preference to the LTO backend by toggling
1161 // the existing -vectorize-slp cl::opt, which the pass honors directly. This
1162 // avoids minting dedicated linker options for what is only pipeline tuning.
1163 if (Arg *A = Args.getLastArg(Ids: options::OPT_fslp_vectorize,
1164 Ids: options::OPT_fno_slp_vectorize))
1165 CmdArgs.push_back(Elt: Args.MakeArgString(
1166 Str: Twine(PluginOptPrefix) + "-vectorize-slp=" +
1167 (A->getOption().matches(ID: options::OPT_fslp_vectorize) ? "1" : "0")));
1168
1169 // Pass down GlobalISel options.
1170 if (Arg *A = Args.getLastArg(Ids: options::OPT_fglobal_isel,
1171 Ids: options::OPT_fno_global_isel)) {
1172 // Parsing -fno-global-isel explicitly gives architectures that enable GISel
1173 // by default a chance to disable it.
1174 CmdArgs.push_back(Elt: Args.MakeArgString(
1175 Str: Twine(PluginOptPrefix) + "-global-isel=" +
1176 (A->getOption().matches(ID: options::OPT_fglobal_isel) ? "1" : "0")));
1177 }
1178
1179 // If an explicit debugger tuning argument appeared, pass it along.
1180 if (Arg *A =
1181 Args.getLastArg(Ids: options::OPT_gTune_Group, Ids: options::OPT_ggdbN_Group)) {
1182 if (A->getOption().matches(ID: options::OPT_glldb))
1183 CmdArgs.push_back(
1184 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-debugger-tune=lldb"));
1185 else if (A->getOption().matches(ID: options::OPT_gsce))
1186 CmdArgs.push_back(
1187 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-debugger-tune=sce"));
1188 else if (A->getOption().matches(ID: options::OPT_gdbx))
1189 CmdArgs.push_back(
1190 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-debugger-tune=dbx"));
1191 else
1192 CmdArgs.push_back(
1193 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-debugger-tune=gdb"));
1194 }
1195
1196 if (IsOSAIX) {
1197 if (!ToolChain.useIntegratedAs())
1198 CmdArgs.push_back(
1199 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-no-integrated-as=1"));
1200
1201 // On AIX, clang assumes strict-dwarf is true if any debug option is
1202 // specified, unless it is told explicitly not to assume so.
1203 Arg *A = Args.getLastArg(Ids: options::OPT_g_Group);
1204 bool EnableDebugInfo = A && !A->getOption().matches(ID: options::OPT_g0) &&
1205 !A->getOption().matches(ID: options::OPT_ggdb0);
1206 if (EnableDebugInfo && Args.hasFlag(Pos: options::OPT_gstrict_dwarf,
1207 Neg: options::OPT_gno_strict_dwarf, Default: true))
1208 CmdArgs.push_back(
1209 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-strict-dwarf=true"));
1210
1211 for (const Arg *A : Args.filtered_reverse(Ids: options::OPT_mabi_EQ)) {
1212 StringRef V = A->getValue();
1213 if (V == "vec-default")
1214 break;
1215 if (V == "vec-extabi") {
1216 CmdArgs.push_back(
1217 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-vec-extabi"));
1218 break;
1219 }
1220 }
1221 }
1222
1223 bool UseSeparateSections =
1224 isUseSeparateSections(Triple: ToolChain.getEffectiveTriple());
1225
1226 if (Args.hasFlag(Pos: options::OPT_ffunction_sections,
1227 Neg: options::OPT_fno_function_sections, Default: UseSeparateSections))
1228 CmdArgs.push_back(
1229 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-function-sections=1"));
1230 else if (Args.hasArg(Ids: options::OPT_fno_function_sections))
1231 CmdArgs.push_back(
1232 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-function-sections=0"));
1233
1234 bool DataSectionsTurnedOff = false;
1235 if (Args.hasFlag(Pos: options::OPT_fdata_sections, Neg: options::OPT_fno_data_sections,
1236 Default: UseSeparateSections)) {
1237 CmdArgs.push_back(
1238 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-data-sections=1"));
1239 } else if (Args.hasArg(Ids: options::OPT_fno_data_sections)) {
1240 DataSectionsTurnedOff = true;
1241 CmdArgs.push_back(
1242 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-data-sections=0"));
1243 }
1244
1245 if (Args.hasArg(Ids: options::OPT_mxcoff_roptr) ||
1246 Args.hasArg(Ids: options::OPT_mno_xcoff_roptr)) {
1247 bool HasRoptr = Args.hasFlag(Pos: options::OPT_mxcoff_roptr,
1248 Neg: options::OPT_mno_xcoff_roptr, Default: false);
1249 StringRef OptStr = HasRoptr ? "-mxcoff-roptr" : "-mno-xcoff-roptr";
1250 if (!IsOSAIX)
1251 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
1252 << OptStr << Triple.str();
1253
1254 if (HasRoptr) {
1255 // The data sections option is on by default on AIX. We only need to error
1256 // out when -fno-data-sections is specified explicitly to turn off data
1257 // sections.
1258 if (DataSectionsTurnedOff)
1259 D.Diag(DiagID: diag::err_roptr_requires_data_sections);
1260
1261 CmdArgs.push_back(
1262 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-mxcoff-roptr"));
1263 }
1264 }
1265
1266 // Pass an option to enable split machine functions.
1267 if (auto *A = Args.getLastArg(Ids: options::OPT_fsplit_machine_functions,
1268 Ids: options::OPT_fno_split_machine_functions)) {
1269 if (A->getOption().matches(ID: options::OPT_fsplit_machine_functions))
1270 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
1271 "-split-machine-functions"));
1272 }
1273
1274 if (auto *A =
1275 Args.getLastArg(Ids: options::OPT_fpartition_static_data_sections,
1276 Ids: options::OPT_fno_partition_static_data_sections)) {
1277 if (A->getOption().matches(ID: options::OPT_fpartition_static_data_sections)) {
1278 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
1279 "-partition-static-data-sections"));
1280 }
1281 }
1282
1283 if (Arg *A = getLastProfileSampleUseArg(Args)) {
1284 StringRef FName = A->getValue();
1285 if (!llvm::sys::fs::exists(Path: FName))
1286 D.Diag(DiagID: diag::err_drv_no_such_file) << FName;
1287 else
1288 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
1289 "sample-profile=" + FName));
1290 }
1291
1292 if (auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args)) {
1293 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + ExtraDash +
1294 "cs-profile-generate"));
1295 if (CSPGOGenerateArg->getOption().matches(
1296 ID: options::OPT_fcs_profile_generate_EQ)) {
1297 SmallString<128> Path(CSPGOGenerateArg->getValue());
1298 llvm::sys::path::append(path&: Path, a: "default_%m.profraw");
1299 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + ExtraDash +
1300 "cs-profile-path=" + Path));
1301 } else
1302 CmdArgs.push_back(
1303 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + ExtraDash +
1304 "cs-profile-path=default_%m.profraw"));
1305 } else if (auto *ProfileUseArg = getLastProfileUseArg(Args)) {
1306 SmallString<128> Path(
1307 ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue());
1308 if (Path.empty() || llvm::sys::fs::is_directory(Path))
1309 llvm::sys::path::append(path&: Path, a: "default.profdata");
1310 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + ExtraDash +
1311 "cs-profile-path=" + Path));
1312 }
1313
1314 // This controls whether or not we perform JustMyCode instrumentation.
1315 if (Args.hasFlag(Pos: options::OPT_fjmc, Neg: options::OPT_fno_jmc, Default: false)) {
1316 if (ToolChain.getEffectiveTriple().isOSBinFormatELF())
1317 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) +
1318 "-enable-jmc-instrument"));
1319 else
1320 D.Diag(DiagID: clang::diag::warn_drv_fjmc_for_elf_only);
1321 }
1322
1323 if (Args.hasFlag(Pos: options::OPT_femulated_tls, Neg: options::OPT_fno_emulated_tls,
1324 Default: Triple.hasDefaultEmulatedTLS())) {
1325 CmdArgs.push_back(
1326 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-emulated-tls"));
1327 }
1328 if (isTLSDESCEnabled(TC: ToolChain, Args))
1329 CmdArgs.push_back(
1330 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-enable-tlsdesc"));
1331
1332 if (Args.hasFlag(Pos: options::OPT_fstack_size_section,
1333 Neg: options::OPT_fno_stack_size_section, Default: false))
1334 CmdArgs.push_back(
1335 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-stack-size-section"));
1336
1337 if (Args.hasFlag(Pos: options::OPT_fexperimental_call_graph_section,
1338 Neg: options::OPT_fno_experimental_call_graph_section, Default: false))
1339 CmdArgs.push_back(
1340 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-call-graph-section"));
1341
1342 // Setup statistics file output.
1343 SmallString<128> StatsFile = getStatsFileName(Args, Output, Input: *Input, D);
1344 if (!StatsFile.empty())
1345 CmdArgs.push_back(
1346 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "stats-file=" + StatsFile));
1347
1348 // Setup crash diagnostics dir.
1349 if (Arg *A = Args.getLastArg(Ids: options::OPT_fcrash_diagnostics_dir))
1350 CmdArgs.push_back(Elt: Args.MakeArgString(
1351 Str: Twine(PluginOptPrefix) + "-crash-diagnostics-dir=" + A->getValue()));
1352
1353 addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/true, PluginOptPrefix);
1354
1355 // Handle remark diagnostics on screen options: '-Rpass-*'.
1356 renderRpassOptions(Args, CmdArgs, PluginOptPrefix);
1357
1358 // Handle serialized remarks options: '-fsave-optimization-record'
1359 // and '-foptimization-record-*'.
1360 if (willEmitRemarks(Args))
1361 renderRemarksOptions(Args, CmdArgs, Triple: ToolChain.getEffectiveTriple(), Input: *Input,
1362 Output, PluginOptPrefix);
1363
1364 // Handle remarks hotness/threshold related options.
1365 renderRemarksHotnessOptions(Args, CmdArgs, PluginOptPrefix);
1366
1367 addMachineOutlinerArgs(D, Args, CmdArgs, Triple: ToolChain.getEffectiveTriple(),
1368 /*IsLTO=*/true, PluginOptPrefix);
1369
1370 bool IsELF = Triple.isOSBinFormatELF();
1371 bool Crel = false;
1372 bool ImplicitMapSyms = false;
1373 for (const Arg *A : Args.filtered(Ids: options::OPT_Wa_COMMA)) {
1374 for (StringRef V : A->getValues()) {
1375 auto Equal = V.split(Separator: '=');
1376 auto checkArg = [&](bool ValidTarget,
1377 std::initializer_list<const char *> Set) {
1378 if (!ValidTarget) {
1379 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
1380 << (Twine("-Wa,") + Equal.first + "=").str()
1381 << Triple.getTriple();
1382 } else if (!llvm::is_contained(Set, Element: Equal.second)) {
1383 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
1384 << (Twine("-Wa,") + Equal.first + "=").str() << Equal.second;
1385 }
1386 };
1387 if (Equal.first == "-mmapsyms") {
1388 ImplicitMapSyms = Equal.second == "implicit";
1389 checkArg(IsELF && Triple.isAArch64(), {"default", "implicit"});
1390 } else if (V == "--crel")
1391 Crel = true;
1392 else if (V == "--no-crel")
1393 Crel = false;
1394 else
1395 continue;
1396 A->claim();
1397 }
1398 }
1399 if (Crel) {
1400 if (IsELF && !Triple.isMIPS()) {
1401 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-crel"));
1402 } else {
1403 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
1404 << "-Wa,--crel" << D.getTargetTriple();
1405 }
1406 }
1407 if (ImplicitMapSyms)
1408 CmdArgs.push_back(
1409 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-implicit-mapsyms"));
1410
1411 if (Args.hasArg(Ids: options::OPT_ftime_report))
1412 CmdArgs.push_back(
1413 Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + "-time-passes"));
1414
1415 addDTLTOOptions(ToolChain, Args, CmdArgs);
1416}
1417
1418void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
1419 const ArgList &Args,
1420 ArgStringList &CmdArgs) {
1421 // Default to clang lib / lib64 folder, i.e. the same location as device
1422 // runtime.
1423 SmallString<256> DefaultLibPath =
1424 llvm::sys::path::parent_path(path: TC.getDriver().Dir);
1425 llvm::sys::path::append(path&: DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
1426 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-L" + DefaultLibPath));
1427}
1428
1429void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args,
1430 ArgStringList &CmdArgs) {
1431 if (!Args.hasFlag(Pos: options::OPT_frtlib_add_rpath,
1432 Neg: options::OPT_fno_rtlib_add_rpath, Default: false))
1433 return;
1434
1435 if (TC.getTriple().isOSAIX()) // TODO: AIX doesn't support -rpath option.
1436 return;
1437
1438 SmallVector<std::string> CandidateRPaths(TC.getArchSpecificLibPaths());
1439 if (const auto StdlibPath = TC.getStdlibPath()) {
1440 for (const Multilib &M : llvm::reverse(C: TC.getSelectedMultilibs())) {
1441 if (M.isDefault())
1442 continue;
1443 SmallString<128> P(*StdlibPath);
1444 llvm::sys::path::append(path&: P, a: M.gccSuffix());
1445 CandidateRPaths.emplace_back(Args: std::string(P));
1446 }
1447 CandidateRPaths.emplace_back(Args: *StdlibPath);
1448 }
1449 for (const auto &CandidateRPath : CandidateRPaths) {
1450 if (TC.getVFS().exists(Path: CandidateRPath)) {
1451 CmdArgs.push_back(Elt: "-rpath");
1452 CmdArgs.push_back(Elt: Args.MakeArgString(Str: CandidateRPath));
1453 }
1454 }
1455}
1456
1457bool tools::addOpenMPRuntime(const Compilation &C, ArgStringList &CmdArgs,
1458 const ToolChain &TC, const ArgList &Args,
1459 bool ForceStaticHostRuntime, bool IsOffloadingHost,
1460 bool GompNeedsRT) {
1461 if (!Args.hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ,
1462 Neg: options::OPT_fno_openmp, Default: false)) {
1463 // We need libomptarget (liboffload) if it's the choosen offloading runtime.
1464 if (Args.hasFlag(Pos: options::OPT_foffload_via_llvm,
1465 Neg: options::OPT_fno_offload_via_llvm, Default: false))
1466 CmdArgs.push_back(Elt: "-lomptarget");
1467 return false;
1468 }
1469
1470 Driver::OpenMPRuntimeKind RTKind = TC.getDriver().getOpenMPRuntime(Args);
1471
1472 if (RTKind == Driver::OMPRT_Unknown)
1473 // Already diagnosed.
1474 return false;
1475
1476 if (ForceStaticHostRuntime)
1477 CmdArgs.push_back(Elt: "-Bstatic");
1478
1479 switch (RTKind) {
1480 case Driver::OMPRT_OMP:
1481 CmdArgs.push_back(Elt: "-lomp");
1482 break;
1483 case Driver::OMPRT_GOMP:
1484 CmdArgs.push_back(Elt: "-lgomp");
1485 break;
1486 case Driver::OMPRT_IOMP5:
1487 CmdArgs.push_back(Elt: "-liomp5");
1488 break;
1489 case Driver::OMPRT_Unknown:
1490 break;
1491 }
1492
1493 if (ForceStaticHostRuntime)
1494 CmdArgs.push_back(Elt: "-Bdynamic");
1495
1496 if (RTKind == Driver::OMPRT_GOMP && GompNeedsRT)
1497 CmdArgs.push_back(Elt: "-lrt");
1498
1499 if (IsOffloadingHost)
1500 CmdArgs.push_back(Elt: "-lomptarget");
1501
1502 addArchSpecificRPath(TC, Args, CmdArgs);
1503
1504 addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs);
1505
1506 return true;
1507}
1508
1509void tools::addOpenMPHostOffloadingArgs(const Compilation &C,
1510 const JobAction &JA,
1511 const llvm::opt::ArgList &Args,
1512 llvm::opt::ArgStringList &CmdArgs) {
1513 if (!JA.isHostOffloading(OKind: Action::OFK_OpenMP))
1514 return;
1515
1516 // For all the host OpenMP offloading compile jobs we need to pass the targets
1517 // information using -fopenmp-targets= option.
1518 constexpr llvm::StringLiteral Targets("--offload-targets=");
1519
1520 SmallVector<StringRef> Triples;
1521 auto TCRange = C.getOffloadToolChains<Action::OFK_OpenMP>();
1522 std::transform(first: TCRange.first, last: TCRange.second, result: std::back_inserter(x&: Triples),
1523 unary_op: [](auto TC) { return TC.second->getTripleString(); });
1524 CmdArgs.push_back(
1525 Elt: Args.MakeArgString(Str: Twine(Targets) + llvm::join(R&: Triples, Separator: ",")));
1526}
1527
1528static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args,
1529 ArgStringList &CmdArgs, StringRef Sanitizer,
1530 bool IsShared, bool IsWhole) {
1531 // Wrap any static runtimes that must be forced into executable in
1532 // whole-archive.
1533 if (IsWhole) CmdArgs.push_back(Elt: "--whole-archive");
1534 CmdArgs.push_back(Elt: TC.getCompilerRTArgString(
1535 Args, Component: Sanitizer, Type: IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static));
1536 if (IsWhole) CmdArgs.push_back(Elt: "--no-whole-archive");
1537
1538 if (IsShared) {
1539 addArchSpecificRPath(TC, Args, CmdArgs);
1540 }
1541}
1542
1543// Tries to use a file with the list of dynamic symbols that need to be exported
1544// from the runtime library. Returns true if the file was found.
1545static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args,
1546 ArgStringList &CmdArgs,
1547 StringRef Sanitizer) {
1548 bool LinkerIsGnuLd = solaris::isLinkerGnuLd(TC, Args);
1549
1550 // Solaris ld defaults to --export-dynamic behaviour but doesn't support
1551 // the option, so don't try to pass it.
1552 if (TC.getTriple().isOSSolaris() && !LinkerIsGnuLd)
1553 return true;
1554 SmallString<128> SanRT(TC.getCompilerRT(Args, Component: Sanitizer));
1555 if (llvm::sys::fs::exists(Path: SanRT + ".syms")) {
1556 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "--dynamic-list=" + SanRT + ".syms"));
1557 return true;
1558 }
1559 return false;
1560}
1561
1562void tools::addAsNeededOption(const ToolChain &TC,
1563 const llvm::opt::ArgList &Args,
1564 llvm::opt::ArgStringList &CmdArgs,
1565 bool as_needed) {
1566 assert(!TC.getTriple().isOSAIX() &&
1567 "AIX linker does not support any form of --as-needed option yet.");
1568 bool LinkerIsGnuLd = solaris::isLinkerGnuLd(TC, Args);
1569
1570 // While the Solaris 11.2 ld added --as-needed/--no-as-needed as aliases
1571 // for the native forms -z ignore/-z record, they are missing in Illumos,
1572 // so always use the native form.
1573 // GNU ld doesn't support -z ignore/-z record, so don't use them even on
1574 // Solaris.
1575 if (TC.getTriple().isOSSolaris() && !LinkerIsGnuLd) {
1576 CmdArgs.push_back(Elt: "-z");
1577 CmdArgs.push_back(Elt: as_needed ? "ignore" : "record");
1578 } else {
1579 CmdArgs.push_back(Elt: as_needed ? "--as-needed" : "--no-as-needed");
1580 }
1581}
1582
1583void tools::linkSanitizerRuntimeDeps(const ToolChain &TC,
1584 const llvm::opt::ArgList &Args,
1585 ArgStringList &CmdArgs) {
1586 // Force linking against the system libraries sanitizers depends on
1587 // (see PR15823 why this is necessary).
1588 addAsNeededOption(TC, Args, CmdArgs, as_needed: false);
1589 // There's no libpthread or librt on RTEMS & Android.
1590 if (TC.getTriple().getOS() != llvm::Triple::RTEMS &&
1591 !TC.getTriple().isAndroid() && !TC.getTriple().isOHOSFamily()) {
1592 CmdArgs.push_back(Elt: "-lpthread");
1593 if (!TC.getTriple().isOSOpenBSD() && !TC.getTriple().isOSHaiku())
1594 CmdArgs.push_back(Elt: "-lrt");
1595 }
1596 CmdArgs.push_back(Elt: "-lm");
1597 // There's no libdl on all OSes.
1598 if (!TC.getTriple().isOSFreeBSD() && !TC.getTriple().isOSNetBSD() &&
1599 !TC.getTriple().isOSOpenBSD() && !TC.getTriple().isOSDragonFly() &&
1600 !TC.getTriple().isOSHaiku() &&
1601 TC.getTriple().getOS() != llvm::Triple::RTEMS)
1602 CmdArgs.push_back(Elt: "-ldl");
1603 // Required for backtrace on some OSes
1604 if (TC.getTriple().isOSFreeBSD() || TC.getTriple().isOSNetBSD() ||
1605 TC.getTriple().isOSOpenBSD() || TC.getTriple().isOSDragonFly())
1606 CmdArgs.push_back(Elt: "-lexecinfo");
1607 if (TC.getTriple().isOSHaiku())
1608 CmdArgs.push_back(Elt: "-lbsd");
1609 // There is no libresolv on Android, FreeBSD, OpenBSD, etc. On musl
1610 // libresolv.a, even if exists, is an empty archive to satisfy POSIX -lresolv
1611 // requirement.
1612 if (TC.getTriple().isOSLinux() && !TC.getTriple().isAndroid() &&
1613 !TC.getTriple().isMusl())
1614 CmdArgs.push_back(Elt: "-lresolv");
1615}
1616
1617static void
1618collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
1619 SmallVectorImpl<StringRef> &SharedRuntimes,
1620 SmallVectorImpl<StringRef> &StaticRuntimes,
1621 SmallVectorImpl<StringRef> &NonWholeStaticRuntimes,
1622 SmallVectorImpl<StringRef> &HelperStaticRuntimes,
1623 SmallVectorImpl<StringRef> &RequiredSymbols) {
1624 assert(!TC.getTriple().isOSDarwin() && "it's not used by Darwin");
1625 const SanitizerArgs &SanArgs = TC.getSanitizerArgs(JobArgs: Args);
1626 // Collect shared runtimes.
1627 if (SanArgs.needsSharedRt()) {
1628 if (SanArgs.needsAsanRt()) {
1629 SharedRuntimes.push_back(Elt: "asan");
1630 if (!Args.hasArg(Ids: options::OPT_shared) && !TC.getTriple().isAndroid())
1631 HelperStaticRuntimes.push_back(Elt: "asan-preinit");
1632 }
1633 if (SanArgs.needsMemProfRt()) {
1634 SharedRuntimes.push_back(Elt: "memprof");
1635 if (!Args.hasArg(Ids: options::OPT_shared) && !TC.getTriple().isAndroid())
1636 HelperStaticRuntimes.push_back(Elt: "memprof-preinit");
1637 }
1638 if (SanArgs.needsNsanRt())
1639 SharedRuntimes.push_back(Elt: "nsan");
1640 if (SanArgs.needsUbsanRt()) {
1641 if (SanArgs.requiresMinimalRuntime())
1642 SharedRuntimes.push_back(Elt: "ubsan_minimal");
1643 else
1644 SharedRuntimes.push_back(Elt: "ubsan_standalone");
1645 }
1646 if (SanArgs.needsScudoRt()) {
1647 SharedRuntimes.push_back(Elt: "scudo_standalone");
1648 }
1649 if (SanArgs.needsTsanRt())
1650 SharedRuntimes.push_back(Elt: "tsan");
1651 if (SanArgs.needsTysanRt())
1652 SharedRuntimes.push_back(Elt: "tysan");
1653 if (SanArgs.needsHwasanRt()) {
1654 if (SanArgs.needsHwasanAliasesRt())
1655 SharedRuntimes.push_back(Elt: "hwasan_aliases");
1656 else
1657 SharedRuntimes.push_back(Elt: "hwasan");
1658 if (!Args.hasArg(Ids: options::OPT_shared))
1659 HelperStaticRuntimes.push_back(Elt: "hwasan-preinit");
1660 }
1661 if (SanArgs.needsRtsanRt() && SanArgs.linkRuntimes())
1662 SharedRuntimes.push_back(Elt: "rtsan");
1663 }
1664
1665 // The stats_client library is also statically linked into DSOs.
1666 if (SanArgs.needsStatsRt())
1667 StaticRuntimes.push_back(Elt: "stats_client");
1668
1669 // Always link the static runtime regardless of DSO or executable.
1670 if (SanArgs.needsAsanRt())
1671 HelperStaticRuntimes.push_back(Elt: "asan_static");
1672
1673 // Collect static runtimes.
1674 if (Args.hasArg(Ids: options::OPT_shared)) {
1675 // Don't link static runtimes into DSOs.
1676 return;
1677 }
1678
1679 // Each static runtime that has a DSO counterpart above is excluded below,
1680 // but runtimes that exist only as static are not affected by needsSharedRt.
1681
1682 if (!SanArgs.needsSharedRt() && SanArgs.needsAsanRt()) {
1683 StaticRuntimes.push_back(Elt: "asan");
1684 if (SanArgs.linkCXXRuntimes())
1685 StaticRuntimes.push_back(Elt: "asan_cxx");
1686 }
1687
1688 if (!SanArgs.needsSharedRt() && SanArgs.needsRtsanRt() &&
1689 SanArgs.linkRuntimes())
1690 StaticRuntimes.push_back(Elt: "rtsan");
1691
1692 if (!SanArgs.needsSharedRt() && SanArgs.needsMemProfRt()) {
1693 StaticRuntimes.push_back(Elt: "memprof");
1694 if (SanArgs.linkCXXRuntimes())
1695 StaticRuntimes.push_back(Elt: "memprof_cxx");
1696 }
1697
1698 if (!SanArgs.needsSharedRt() && SanArgs.needsHwasanRt()) {
1699 if (SanArgs.needsHwasanAliasesRt()) {
1700 StaticRuntimes.push_back(Elt: "hwasan_aliases");
1701 if (SanArgs.linkCXXRuntimes())
1702 StaticRuntimes.push_back(Elt: "hwasan_aliases_cxx");
1703 } else {
1704 StaticRuntimes.push_back(Elt: "hwasan");
1705 if (SanArgs.linkCXXRuntimes())
1706 StaticRuntimes.push_back(Elt: "hwasan_cxx");
1707 }
1708 }
1709 if (SanArgs.needsDfsanRt())
1710 StaticRuntimes.push_back(Elt: "dfsan");
1711 if (SanArgs.needsLsanRt())
1712 StaticRuntimes.push_back(Elt: "lsan");
1713 if (SanArgs.needsMsanRt()) {
1714 StaticRuntimes.push_back(Elt: "msan");
1715 if (SanArgs.linkCXXRuntimes())
1716 StaticRuntimes.push_back(Elt: "msan_cxx");
1717 }
1718 if (!SanArgs.needsSharedRt() && SanArgs.needsNsanRt())
1719 StaticRuntimes.push_back(Elt: "nsan");
1720 if (!SanArgs.needsSharedRt() && SanArgs.needsTsanRt()) {
1721 StaticRuntimes.push_back(Elt: "tsan");
1722 if (SanArgs.linkCXXRuntimes())
1723 StaticRuntimes.push_back(Elt: "tsan_cxx");
1724 }
1725 if (!SanArgs.needsSharedRt() && SanArgs.needsTysanRt())
1726 StaticRuntimes.push_back(Elt: "tysan");
1727 if (!SanArgs.needsSharedRt() && SanArgs.needsUbsanRt()) {
1728 if (SanArgs.requiresMinimalRuntime()) {
1729 StaticRuntimes.push_back(Elt: "ubsan_minimal");
1730 } else {
1731 StaticRuntimes.push_back(Elt: "ubsan_standalone");
1732 }
1733 }
1734 if (SanArgs.needsSafeStackRt()) {
1735 NonWholeStaticRuntimes.push_back(Elt: "safestack");
1736 RequiredSymbols.push_back(Elt: "__safestack_init");
1737 }
1738 if (!(SanArgs.needsSharedRt() && SanArgs.needsUbsanRt())) {
1739 if (SanArgs.needsCfiCrossDsoRt())
1740 StaticRuntimes.push_back(Elt: "cfi");
1741 if (SanArgs.needsCfiCrossDsoDiagRt())
1742 StaticRuntimes.push_back(Elt: "cfi_diag");
1743 }
1744 if (SanArgs.linkCXXRuntimes() && !SanArgs.requiresMinimalRuntime() &&
1745 ((!SanArgs.needsSharedRt() && SanArgs.needsUbsanCXXRt()) ||
1746 SanArgs.needsCfiCrossDsoDiagRt())) {
1747 StaticRuntimes.push_back(Elt: "ubsan_standalone_cxx");
1748 }
1749 if (SanArgs.needsStatsRt()) {
1750 NonWholeStaticRuntimes.push_back(Elt: "stats");
1751 RequiredSymbols.push_back(Elt: "__sanitizer_stats_register");
1752 }
1753 if (!SanArgs.needsSharedRt() && SanArgs.needsScudoRt()) {
1754 StaticRuntimes.push_back(Elt: "scudo_standalone");
1755 if (SanArgs.linkCXXRuntimes())
1756 StaticRuntimes.push_back(Elt: "scudo_standalone_cxx");
1757 }
1758 if (SanArgs.needsUbsanLoopDetectRt())
1759 NonWholeStaticRuntimes.push_back(Elt: "ubsan_loop_detect");
1760}
1761
1762// Should be called before we add system libraries (C++ ABI, libstdc++/libc++,
1763// C runtime, etc). Returns true if sanitizer system deps need to be linked in.
1764bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args,
1765 ArgStringList &CmdArgs) {
1766 const SanitizerArgs &SanArgs = TC.getSanitizerArgs(JobArgs: Args);
1767 SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes,
1768 NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols;
1769 if (SanArgs.linkRuntimes()) {
1770 collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes,
1771 NonWholeStaticRuntimes, HelperStaticRuntimes,
1772 RequiredSymbols);
1773 }
1774
1775 // -u options must be added before the runtime libs that resolve them.
1776 for (auto S : RequiredSymbols) {
1777 CmdArgs.push_back(Elt: "-u");
1778 CmdArgs.push_back(Elt: Args.MakeArgString(Str: S));
1779 }
1780
1781 // Add shared runtimes before adding fuzzer and its dependencies.
1782 for (auto RT : SharedRuntimes)
1783 addSanitizerRuntime(TC, Args, CmdArgs, Sanitizer: RT, IsShared: true, IsWhole: false);
1784
1785 // Inject libfuzzer dependencies.
1786 bool FuzzerNeedsSanitizerDeps = false;
1787 if (SanArgs.needsFuzzer() && SanArgs.linkRuntimes() &&
1788 !Args.hasArg(Ids: options::OPT_shared)) {
1789
1790 addSanitizerRuntime(TC, Args, CmdArgs, Sanitizer: "fuzzer", IsShared: false, IsWhole: true);
1791 FuzzerNeedsSanitizerDeps = true;
1792 if (SanArgs.needsFuzzerInterceptors())
1793 addSanitizerRuntime(TC, Args, CmdArgs, Sanitizer: "fuzzer_interceptors", IsShared: false,
1794 IsWhole: true);
1795 if (!Args.hasArg(Ids: options::OPT_nostdlibxx)) {
1796 bool OnlyLibstdcxxStatic = Args.hasArg(Ids: options::OPT_static_libstdcxx) &&
1797 !Args.hasArg(Ids: options::OPT_static);
1798 if (OnlyLibstdcxxStatic)
1799 CmdArgs.push_back(Elt: "-Bstatic");
1800 TC.AddCXXStdlibLibArgs(Args, CmdArgs);
1801 if (OnlyLibstdcxxStatic)
1802 CmdArgs.push_back(Elt: "-Bdynamic");
1803 }
1804 }
1805
1806 for (auto RT : HelperStaticRuntimes)
1807 addSanitizerRuntime(TC, Args, CmdArgs, Sanitizer: RT, IsShared: false, IsWhole: true);
1808 bool AddExportDynamic = false;
1809 for (auto RT : StaticRuntimes) {
1810 addSanitizerRuntime(TC, Args, CmdArgs, Sanitizer: RT, IsShared: false, IsWhole: true);
1811 AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, Sanitizer: RT);
1812 }
1813 for (auto RT : NonWholeStaticRuntimes) {
1814 addSanitizerRuntime(TC, Args, CmdArgs, Sanitizer: RT, IsShared: false, IsWhole: false);
1815 AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, Sanitizer: RT);
1816 }
1817 // If there is a static runtime with no dynamic list, force all the symbols
1818 // to be dynamic to be sure we export sanitizer interface functions.
1819 if (AddExportDynamic && !TC.getTriple().isNVPTX())
1820 CmdArgs.push_back(Elt: "--export-dynamic");
1821
1822 if (SanArgs.hasCrossDsoCfi() && !AddExportDynamic)
1823 CmdArgs.push_back(Elt: "--export-dynamic-symbol=__cfi_check");
1824
1825 if (SanArgs.hasMemTag()) {
1826 CmdArgs.push_back(Elt: "-z");
1827 CmdArgs.push_back(
1828 Elt: Args.MakeArgString(Str: "memtag-mode=" + SanArgs.getMemtagMode()));
1829
1830 if (SanArgs.hasMemtagHeap()) {
1831 CmdArgs.push_back(Elt: "-z");
1832 CmdArgs.push_back(Elt: "memtag-heap");
1833 }
1834
1835 if (SanArgs.hasMemtagStack()) {
1836 CmdArgs.push_back(Elt: "-z");
1837 CmdArgs.push_back(Elt: "memtag-stack");
1838 }
1839
1840 if (TC.getTriple().isAndroid())
1841 CmdArgs.push_back(Elt: "--android-memtag-note");
1842 }
1843
1844 return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty() ||
1845 FuzzerNeedsSanitizerDeps;
1846}
1847
1848bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) {
1849 const XRayArgs &XRay = TC.getXRayArgs(Args);
1850 if (Args.hasArg(Ids: options::OPT_shared)) {
1851 if (XRay.needsXRayDSORt()) {
1852 CmdArgs.push_back(Elt: "--whole-archive");
1853 CmdArgs.push_back(Elt: TC.getCompilerRTArgString(Args, Component: "xray-dso"));
1854 CmdArgs.push_back(Elt: "--no-whole-archive");
1855 return true;
1856 }
1857 } else if (XRay.needsXRayRt()) {
1858 CmdArgs.push_back(Elt: "--whole-archive");
1859 CmdArgs.push_back(Elt: TC.getCompilerRTArgString(Args, Component: "xray"));
1860 for (const auto &Mode : XRay.modeList())
1861 CmdArgs.push_back(Elt: TC.getCompilerRTArgString(Args, Component: Mode));
1862 CmdArgs.push_back(Elt: "--no-whole-archive");
1863 return true;
1864 }
1865
1866 return false;
1867}
1868
1869void tools::linkXRayRuntimeDeps(const ToolChain &TC,
1870 const llvm::opt::ArgList &Args,
1871 ArgStringList &CmdArgs) {
1872 addAsNeededOption(TC, Args, CmdArgs, as_needed: false);
1873 CmdArgs.push_back(Elt: "-lpthread");
1874 if (!TC.getTriple().isOSOpenBSD())
1875 CmdArgs.push_back(Elt: "-lrt");
1876 CmdArgs.push_back(Elt: "-lm");
1877
1878 if (!TC.getTriple().isOSFreeBSD() &&
1879 !TC.getTriple().isOSNetBSD() &&
1880 !TC.getTriple().isOSOpenBSD())
1881 CmdArgs.push_back(Elt: "-ldl");
1882}
1883
1884bool tools::areOptimizationsEnabled(const ArgList &Args) {
1885 // Find the last -O arg and see if it is non-zero.
1886 if (Arg *A = Args.getLastArg(Ids: options::OPT_O_Group))
1887 return !A->getOption().matches(ID: options::OPT_O0);
1888 // Defaults to -O0.
1889 return false;
1890}
1891
1892const char *tools::SplitDebugName(const JobAction &JA, const ArgList &Args,
1893 const InputInfo &Input,
1894 const InputInfo &Output) {
1895 auto AddPostfix = [JA](auto &F) {
1896 if (JA.getOffloadingDeviceKind() == Action::OFK_HIP)
1897 F += (Twine("_") + JA.getOffloadingArch().ArchName).str();
1898 F += ".dwo";
1899 };
1900 if (Arg *A = Args.getLastArg(Ids: options::OPT_gsplit_dwarf_EQ))
1901 if (StringRef(A->getValue()) == "single" && Output.isFilename())
1902 return Args.MakeArgString(Str: Output.getFilename());
1903
1904 SmallString<128> T;
1905 if (const Arg *A = Args.getLastArg(Ids: options::OPT_dumpdir)) {
1906 T = A->getValue();
1907 } else {
1908 if (Args.hasArg(Ids: options::OPT_o, Ids: options::OPT__SLASH_o,
1909 Ids: options::OPT__SLASH_Fo) &&
1910 Args.hasArg(Ids: options::OPT_c) && Output.isFilename()) {
1911 // The driver has resolved /Fo<dir>/ into a concrete obj path in Output.
1912 StringRef Obj = Output.getFilename();
1913 T = Obj;
1914 llvm::sys::path::remove_filename(path&: T);
1915 llvm::sys::path::append(path&: T, a: llvm::sys::path::stem(path: Obj));
1916 AddPostfix(T);
1917 return Args.MakeArgString(Str: T);
1918 }
1919 }
1920
1921 T += llvm::sys::path::stem(path: Input.getBaseInput());
1922 AddPostfix(T);
1923 return Args.MakeArgString(Str: T);
1924}
1925
1926void tools::SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T,
1927 const JobAction &JA, const ArgList &Args,
1928 const InputInfo &Output, const char *OutFile) {
1929 ArgStringList ExtractArgs;
1930 ExtractArgs.push_back(Elt: "--extract-dwo");
1931
1932 ArgStringList StripArgs;
1933 StripArgs.push_back(Elt: "--strip-dwo");
1934
1935 // Grabbing the output of the earlier compile step.
1936 StripArgs.push_back(Elt: Output.getFilename());
1937 ExtractArgs.push_back(Elt: Output.getFilename());
1938 ExtractArgs.push_back(Elt: OutFile);
1939
1940 const char *Exec =
1941 Args.MakeArgString(Str: TC.GetProgramPath(CLANG_DEFAULT_OBJCOPY));
1942 InputInfo II(types::TY_Object, Output.getFilename(), Output.getFilename());
1943
1944 // First extract the dwo sections.
1945 C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: T,
1946 args: ResponseFileSupport::AtFileCurCP(),
1947 args&: Exec, args&: ExtractArgs, args&: II, args: Output));
1948
1949 // Then remove them from the original .o file.
1950 C.addCommand(Cmd: std::make_unique<Command>(
1951 args: JA, args: T, args: ResponseFileSupport::AtFileCurCP(), args&: Exec, args&: StripArgs, args&: II, args: Output));
1952}
1953
1954// Claim options we don't want to warn if they are unused. We do this for
1955// options that build systems might add but are unused when assembling or only
1956// running the preprocessor for example.
1957void tools::claimNoWarnArgs(const ArgList &Args) {
1958 // Don't warn about unused -f(no-)?lto. This can happen when we're
1959 // preprocessing, precompiling or assembling.
1960 Args.ClaimAllArgs(Id0: options::OPT_flto_EQ);
1961 Args.ClaimAllArgs(Id0: options::OPT_flto);
1962 Args.ClaimAllArgs(Id0: options::OPT_fno_lto);
1963}
1964
1965Arg *tools::getLastCSProfileGenerateArg(const ArgList &Args) {
1966 auto *CSPGOGenerateArg = Args.getLastArg(Ids: options::OPT_fcs_profile_generate,
1967 Ids: options::OPT_fcs_profile_generate_EQ,
1968 Ids: options::OPT_fno_profile_generate);
1969 if (CSPGOGenerateArg &&
1970 CSPGOGenerateArg->getOption().matches(ID: options::OPT_fno_profile_generate))
1971 CSPGOGenerateArg = nullptr;
1972
1973 return CSPGOGenerateArg;
1974}
1975
1976Arg *tools::getLastProfileUseArg(const ArgList &Args) {
1977 auto *ProfileUseArg = Args.getLastArg(
1978 Ids: options::OPT_fprofile_instr_use, Ids: options::OPT_fprofile_instr_use_EQ,
1979 Ids: options::OPT_fprofile_use, Ids: options::OPT_fprofile_use_EQ,
1980 Ids: options::OPT_fno_profile_instr_use);
1981
1982 if (ProfileUseArg &&
1983 ProfileUseArg->getOption().matches(ID: options::OPT_fno_profile_instr_use))
1984 ProfileUseArg = nullptr;
1985
1986 return ProfileUseArg;
1987}
1988
1989Arg *tools::getLastProfileSampleUseArg(const ArgList &Args) {
1990 auto *ProfileSampleUseArg = Args.getLastArg(
1991 Ids: options::OPT_fprofile_sample_use_EQ, Ids: options::OPT_fno_profile_sample_use);
1992
1993 if (ProfileSampleUseArg && (ProfileSampleUseArg->getOption().matches(
1994 ID: options::OPT_fno_profile_sample_use)))
1995 return nullptr;
1996
1997 return Args.getLastArg(Ids: options::OPT_fprofile_sample_use_EQ);
1998}
1999
2000const char *tools::RelocationModelName(llvm::Reloc::Model Model) {
2001 switch (Model) {
2002 case llvm::Reloc::Static:
2003 return "static";
2004 case llvm::Reloc::PIC_:
2005 return "pic";
2006 case llvm::Reloc::DynamicNoPIC:
2007 return "dynamic-no-pic";
2008 case llvm::Reloc::ROPI:
2009 return "ropi";
2010 case llvm::Reloc::RWPI:
2011 return "rwpi";
2012 case llvm::Reloc::ROPI_RWPI:
2013 return "ropi-rwpi";
2014 }
2015 llvm_unreachable("Unknown Reloc::Model kind");
2016}
2017
2018/// Parses the various -fpic/-fPIC/-fpie/-fPIE arguments. Then,
2019/// smooshes them together with platform defaults, to decide whether
2020/// this compile should be using PIC mode or not. Returns a tuple of
2021/// (RelocationModel, PICLevel, IsPIE).
2022std::tuple<llvm::Reloc::Model, unsigned, bool>
2023tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) {
2024 const llvm::Triple &EffectiveTriple = ToolChain.getEffectiveTriple();
2025 const llvm::Triple &Triple = ToolChain.getTriple();
2026
2027 bool PIE = ToolChain.isPIEDefault(Args);
2028 bool PIC = PIE || ToolChain.isPICDefault();
2029 // The Darwin/MachO default to use PIC does not apply when using -static.
2030 if (Triple.isOSBinFormatMachO() && Args.hasArg(Ids: options::OPT_static))
2031 PIE = PIC = false;
2032 bool IsPICLevelTwo = PIC;
2033
2034 bool KernelOrKext =
2035 Args.hasArg(Ids: options::OPT_mkernel, Ids: options::OPT_fapple_kext);
2036
2037 // Android-specific defaults for PIC/PIE
2038 if (Triple.isAndroid()) {
2039 switch (Triple.getArch()) {
2040 case llvm::Triple::x86:
2041 case llvm::Triple::x86_64:
2042 PIC = true; // "-fPIC"
2043 IsPICLevelTwo = true;
2044 break;
2045
2046 default:
2047 PIC = true; // "-fpic"
2048 break;
2049 }
2050 }
2051
2052 // OHOS-specific defaults for PIC/PIE
2053 if (Triple.isOHOSFamily() && Triple.getArch() == llvm::Triple::aarch64)
2054 PIC = true;
2055
2056 // OpenBSD-specific defaults for PIE
2057 if (Triple.isOSOpenBSD()) {
2058 switch (ToolChain.getArch()) {
2059 case llvm::Triple::arm:
2060 case llvm::Triple::aarch64:
2061 case llvm::Triple::mips64:
2062 case llvm::Triple::mips64el:
2063 case llvm::Triple::x86:
2064 case llvm::Triple::x86_64:
2065 IsPICLevelTwo = false; // "-fpie"
2066 break;
2067
2068 case llvm::Triple::ppc:
2069 case llvm::Triple::sparcv9:
2070 IsPICLevelTwo = true; // "-fPIE"
2071 break;
2072
2073 default:
2074 break;
2075 }
2076 }
2077
2078 // The last argument relating to either PIC or PIE wins, and no
2079 // other argument is used. If the last argument is any flavor of the
2080 // '-fno-...' arguments, both PIC and PIE are disabled. Any PIE
2081 // option implicitly enables PIC at the same level.
2082 Arg *LastPICArg = Args.getLastArg(Ids: options::OPT_fPIC, Ids: options::OPT_fno_PIC,
2083 Ids: options::OPT_fpic, Ids: options::OPT_fno_pic,
2084 Ids: options::OPT_fPIE, Ids: options::OPT_fno_PIE,
2085 Ids: options::OPT_fpie, Ids: options::OPT_fno_pie);
2086 if (Triple.isOSWindows() && !Triple.isOSCygMing() && LastPICArg &&
2087 LastPICArg == Args.getLastArg(Ids: options::OPT_fPIC, Ids: options::OPT_fpic,
2088 Ids: options::OPT_fPIE, Ids: options::OPT_fpie)) {
2089 ToolChain.getDriver().Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2090 << LastPICArg->getSpelling() << Triple.str();
2091 if (Triple.getArch() == llvm::Triple::x86_64)
2092 return std::make_tuple(args: llvm::Reloc::PIC_, args: 2U, args: false);
2093 return std::make_tuple(args: llvm::Reloc::Static, args: 0U, args: false);
2094 }
2095
2096 // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness
2097 // is forced, then neither PIC nor PIE flags will have no effect.
2098 if (!ToolChain.isPICDefaultForced()) {
2099 if (LastPICArg) {
2100 Option O = LastPICArg->getOption();
2101 if (O.matches(ID: options::OPT_fPIC) || O.matches(ID: options::OPT_fpic) ||
2102 O.matches(ID: options::OPT_fPIE) || O.matches(ID: options::OPT_fpie)) {
2103 PIE = O.matches(ID: options::OPT_fPIE) || O.matches(ID: options::OPT_fpie);
2104 PIC =
2105 PIE || O.matches(ID: options::OPT_fPIC) || O.matches(ID: options::OPT_fpic);
2106 IsPICLevelTwo =
2107 O.matches(ID: options::OPT_fPIE) || O.matches(ID: options::OPT_fPIC);
2108 } else {
2109 PIE = PIC = false;
2110 if (EffectiveTriple.isPS()) {
2111 Arg *ModelArg = Args.getLastArg(Ids: options::OPT_mcmodel_EQ);
2112 StringRef Model = ModelArg ? ModelArg->getValue() : "";
2113 if (Model != "kernel") {
2114 PIC = true;
2115 ToolChain.getDriver().Diag(DiagID: diag::warn_drv_ps_force_pic)
2116 << LastPICArg->getSpelling()
2117 << (EffectiveTriple.isPS4() ? "PS4" : "PS5");
2118 }
2119 }
2120 }
2121 }
2122 }
2123
2124 // Introduce a Darwin and PS4/PS5-specific hack. If the default is PIC, but
2125 // the PIC level would've been set to level 1, force it back to level 2 PIC
2126 // instead.
2127 if (PIC && (Triple.isOSDarwin() || EffectiveTriple.isPS()))
2128 IsPICLevelTwo |= ToolChain.isPICDefault();
2129
2130 // This kernel flags are a trump-card: they will disable PIC/PIE
2131 // generation, independent of the argument order.
2132 if (KernelOrKext &&
2133 ((!EffectiveTriple.isiOS() || EffectiveTriple.isOSVersionLT(Major: 6)) &&
2134 !EffectiveTriple.isWatchOS() && !EffectiveTriple.isDriverKit()))
2135 PIC = PIE = false;
2136
2137 if (Arg *A = Args.getLastArg(Ids: options::OPT_mdynamic_no_pic)) {
2138 // This is a very special mode. It trumps the other modes, almost no one
2139 // uses it, and it isn't even valid on any OS but Darwin.
2140 if (!Triple.isOSDarwin())
2141 ToolChain.getDriver().Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2142 << A->getSpelling() << Triple.str();
2143
2144 // FIXME: Warn when this flag trumps some other PIC or PIE flag.
2145
2146 // Only a forced PIC mode can cause the actual compile to have PIC defines
2147 // etc., no flags are sufficient. This behavior was selected to closely
2148 // match that of llvm-gcc and Apple GCC before that.
2149 PIC = ToolChain.isPICDefault() && ToolChain.isPICDefaultForced();
2150
2151 return std::make_tuple(args: llvm::Reloc::DynamicNoPIC, args: PIC ? 2U : 0U, args: false);
2152 }
2153
2154 bool EmbeddedPISupported;
2155 switch (Triple.getArch()) {
2156 case llvm::Triple::arm:
2157 case llvm::Triple::armeb:
2158 case llvm::Triple::thumb:
2159 case llvm::Triple::thumbeb:
2160 EmbeddedPISupported = true;
2161 break;
2162 default:
2163 EmbeddedPISupported = false;
2164 break;
2165 }
2166
2167 bool ROPI = false, RWPI = false;
2168 Arg* LastROPIArg = Args.getLastArg(Ids: options::OPT_fropi, Ids: options::OPT_fno_ropi);
2169 if (LastROPIArg && LastROPIArg->getOption().matches(ID: options::OPT_fropi)) {
2170 if (!EmbeddedPISupported)
2171 ToolChain.getDriver().Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2172 << LastROPIArg->getSpelling() << Triple.str();
2173 ROPI = true;
2174 }
2175 Arg *LastRWPIArg = Args.getLastArg(Ids: options::OPT_frwpi, Ids: options::OPT_fno_rwpi);
2176 if (LastRWPIArg && LastRWPIArg->getOption().matches(ID: options::OPT_frwpi)) {
2177 if (!EmbeddedPISupported)
2178 ToolChain.getDriver().Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2179 << LastRWPIArg->getSpelling() << Triple.str();
2180 RWPI = true;
2181 }
2182
2183 // ROPI and RWPI are not compatible with PIC or PIE.
2184 if ((ROPI || RWPI) && (PIC || PIE))
2185 ToolChain.getDriver().Diag(DiagID: diag::err_drv_ropi_rwpi_incompatible_with_pic);
2186
2187 if (Triple.isMIPS()) {
2188 StringRef CPUName;
2189 StringRef ABIName;
2190 mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName);
2191 // When targeting the N64 ABI, PIC is the default, except in the case
2192 // when the -mno-abicalls option is used. In that case we exit
2193 // at next check regardless of PIC being set below.
2194 if (ABIName == "n64")
2195 PIC = true;
2196 // When targettng MIPS with -mno-abicalls, it's always static.
2197 if(Args.hasArg(Ids: options::OPT_mno_abicalls))
2198 return std::make_tuple(args: llvm::Reloc::Static, args: 0U, args: false);
2199 // Unlike other architectures, MIPS, even with -fPIC/-mxgot/multigot,
2200 // does not use PIC level 2 for historical reasons.
2201 IsPICLevelTwo = false;
2202 }
2203
2204 if (PIC)
2205 return std::make_tuple(args: llvm::Reloc::PIC_, args: IsPICLevelTwo ? 2U : 1U, args&: PIE);
2206
2207 llvm::Reloc::Model RelocM = llvm::Reloc::Static;
2208 if (ROPI && RWPI)
2209 RelocM = llvm::Reloc::ROPI_RWPI;
2210 else if (ROPI)
2211 RelocM = llvm::Reloc::ROPI;
2212 else if (RWPI)
2213 RelocM = llvm::Reloc::RWPI;
2214
2215 return std::make_tuple(args&: RelocM, args: 0U, args: false);
2216}
2217
2218bool tools::getStaticPIE(const ArgList &Args, const ToolChain &TC) {
2219 bool HasStaticPIE = Args.hasArg(Ids: options::OPT_static_pie);
2220 if (HasStaticPIE && Args.hasArg(Ids: options::OPT_no_pie)) {
2221 const Driver &D = TC.getDriver();
2222 const llvm::opt::OptTable &Opts = D.getOpts();
2223 StringRef StaticPIEName = Opts.getOptionName(id: options::OPT_static_pie);
2224 StringRef NoPIEName = Opts.getOptionName(id: options::OPT_nopie);
2225 D.Diag(DiagID: diag::err_drv_cannot_mix_options) << StaticPIEName << NoPIEName;
2226 }
2227 return HasStaticPIE;
2228}
2229
2230// `-falign-functions` indicates that the functions should be aligned to the
2231// backend's preferred alignment.
2232//
2233// `-falign-functions=1` is the same as `-fno-align-functions`.
2234//
2235// The scalar `n` in `-falign-functions=n` must be an integral value between
2236// [0, 65536]. If the value is not a power-of-two, it will be rounded up to
2237// the nearest power-of-two.
2238//
2239// If we return `0`, the frontend will default to the backend's preferred
2240// alignment.
2241//
2242// NOTE: icc only allows values between [0, 4096]. icc uses `-falign-functions`
2243// to mean `-falign-functions=16`. GCC defaults to the backend's preferred
2244// alignment. For unaligned functions, we default to the backend's preferred
2245// alignment.
2246unsigned tools::ParseFunctionAlignment(const ToolChain &TC,
2247 const ArgList &Args) {
2248 const Arg *A = Args.getLastArg(Ids: options::OPT_falign_functions,
2249 Ids: options::OPT_falign_functions_EQ,
2250 Ids: options::OPT_fno_align_functions);
2251 if (!A || A->getOption().matches(ID: options::OPT_fno_align_functions))
2252 return 0;
2253
2254 if (A->getOption().matches(ID: options::OPT_falign_functions))
2255 return 0;
2256
2257 unsigned Value = 0;
2258 if (StringRef(A->getValue()).getAsInteger(Radix: 10, Result&: Value) || Value > 65536)
2259 TC.getDriver().Diag(DiagID: diag::err_drv_invalid_int_value)
2260 << A->getAsString(Args) << A->getValue();
2261 return Value ? llvm::Log2_32_Ceil(Value: std::min(a: Value, b: 65536u)) : Value;
2262}
2263
2264void tools::addDebugInfoKind(
2265 ArgStringList &CmdArgs, llvm::codegenoptions::DebugInfoKind DebugInfoKind) {
2266 switch (DebugInfoKind) {
2267 case llvm::codegenoptions::DebugDirectivesOnly:
2268 CmdArgs.push_back(Elt: "-debug-info-kind=line-directives-only");
2269 break;
2270 case llvm::codegenoptions::DebugLineTablesOnly:
2271 CmdArgs.push_back(Elt: "-debug-info-kind=line-tables-only");
2272 break;
2273 case llvm::codegenoptions::DebugInfoConstructor:
2274 CmdArgs.push_back(Elt: "-debug-info-kind=constructor");
2275 break;
2276 case llvm::codegenoptions::LimitedDebugInfo:
2277 CmdArgs.push_back(Elt: "-debug-info-kind=limited");
2278 break;
2279 case llvm::codegenoptions::FullDebugInfo:
2280 CmdArgs.push_back(Elt: "-debug-info-kind=standalone");
2281 break;
2282 case llvm::codegenoptions::UnusedTypeInfo:
2283 CmdArgs.push_back(Elt: "-debug-info-kind=unused-types");
2284 break;
2285 default:
2286 break;
2287 }
2288}
2289
2290// Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases
2291// to the corresponding DebugInfoKind.
2292llvm::codegenoptions::DebugInfoKind tools::debugLevelToInfoKind(const Arg &A) {
2293 assert(A.getOption().matches(options::OPT_gN_Group) &&
2294 "Not a -g option that specifies a debug-info level");
2295 if (A.getOption().matches(ID: options::OPT_g0) ||
2296 A.getOption().matches(ID: options::OPT_ggdb0))
2297 return llvm::codegenoptions::NoDebugInfo;
2298 if (A.getOption().matches(ID: options::OPT_gline_tables_only) ||
2299 A.getOption().matches(ID: options::OPT_ggdb1))
2300 return llvm::codegenoptions::DebugLineTablesOnly;
2301 if (A.getOption().matches(ID: options::OPT_gline_directives_only))
2302 return llvm::codegenoptions::DebugDirectivesOnly;
2303 return llvm::codegenoptions::DebugInfoConstructor;
2304}
2305
2306static unsigned ParseDebugDefaultVersion(const ToolChain &TC,
2307 const ArgList &Args) {
2308 const Arg *A = Args.getLastArg(Ids: options::OPT_fdebug_default_version);
2309
2310 if (!A)
2311 return 0;
2312
2313 unsigned Value = 0;
2314 if (StringRef(A->getValue()).getAsInteger(Radix: 10, Result&: Value) || Value > 6 ||
2315 Value < 2)
2316 TC.getDriver().Diag(DiagID: diag::err_drv_invalid_int_value)
2317 << A->getAsString(Args) << A->getValue();
2318 return Value;
2319}
2320
2321unsigned tools::DwarfVersionNum(StringRef ArgValue) {
2322 return llvm::StringSwitch<unsigned>(ArgValue)
2323 .Case(S: "-gdwarf-2", Value: 2)
2324 .Case(S: "-gdwarf-3", Value: 3)
2325 .Case(S: "-gdwarf-4", Value: 4)
2326 .Case(S: "-gdwarf-5", Value: 5)
2327 .Case(S: "-gdwarf-6", Value: 6)
2328 .Default(Value: 0);
2329}
2330
2331const Arg *tools::getDwarfNArg(const ArgList &Args) {
2332 return Args.getLastArg(Ids: options::OPT_gdwarf_2, Ids: options::OPT_gdwarf_3,
2333 Ids: options::OPT_gdwarf_4, Ids: options::OPT_gdwarf_5,
2334 Ids: options::OPT_gdwarf_6, Ids: options::OPT_gdwarf);
2335}
2336
2337unsigned tools::getDwarfVersion(const ToolChain &TC,
2338 const llvm::opt::ArgList &Args) {
2339 unsigned DwarfVersion = ParseDebugDefaultVersion(TC, Args);
2340 if (const Arg *GDwarfN = getDwarfNArg(Args))
2341 if (int N = DwarfVersionNum(ArgValue: GDwarfN->getSpelling())) {
2342 DwarfVersion = N;
2343 if (DwarfVersion == 5 && TC.getTriple().isOSAIX())
2344 TC.getDriver().Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
2345 << GDwarfN->getSpelling() << TC.getTriple().str();
2346 }
2347 if (DwarfVersion == 0) {
2348 DwarfVersion = TC.GetDefaultDwarfVersion();
2349 assert(DwarfVersion && "toolchain default DWARF version must be nonzero");
2350 }
2351 return DwarfVersion;
2352}
2353
2354DwarfFissionKind tools::getDebugFissionKind(const Driver &D,
2355 const ArgList &Args, Arg *&Arg) {
2356 Arg = Args.getLastArg(Ids: options::OPT_gsplit_dwarf, Ids: options::OPT_gsplit_dwarf_EQ,
2357 Ids: options::OPT_gno_split_dwarf);
2358 if (!Arg || Arg->getOption().matches(ID: options::OPT_gno_split_dwarf))
2359 return DwarfFissionKind::None;
2360
2361 if (Arg->getOption().matches(ID: options::OPT_gsplit_dwarf))
2362 return DwarfFissionKind::Split;
2363
2364 StringRef Value = Arg->getValue();
2365 if (Value == "split")
2366 return DwarfFissionKind::Split;
2367 if (Value == "single")
2368 return DwarfFissionKind::Single;
2369
2370 D.Diag(DiagID: diag::err_drv_unsupported_option_argument)
2371 << Arg->getSpelling() << Arg->getValue();
2372 return DwarfFissionKind::None;
2373}
2374
2375bool tools::checkDebugInfoOption(const Arg *A, const ArgList &Args,
2376 const Driver &D, const ToolChain &TC) {
2377 assert(A && "Expected non-nullptr argument.");
2378 if (TC.supportsDebugInfoOption(A))
2379 return true;
2380 D.Diag(DiagID: diag::warn_drv_unsupported_debug_info_opt_for_target)
2381 << A->getAsString(Args) << TC.getTripleString();
2382 return false;
2383}
2384
2385void tools::addDebugInfoForProfilingArgs(const Driver &D, const ToolChain &TC,
2386 const ArgList &Args,
2387 ArgStringList &CmdArgs) {
2388 if (Args.hasFlag(Pos: options::OPT_fdebug_info_for_profiling,
2389 Neg: options::OPT_fno_debug_info_for_profiling, Default: false) &&
2390 checkDebugInfoOption(
2391 A: Args.getLastArg(Ids: options::OPT_fdebug_info_for_profiling), Args, D, TC))
2392 CmdArgs.push_back(Elt: "-fdebug-info-for-profiling");
2393}
2394
2395void tools::AddAssemblerKPIC(const ToolChain &ToolChain, const ArgList &Args,
2396 ArgStringList &CmdArgs) {
2397 llvm::Reloc::Model RelocationModel;
2398 unsigned PICLevel;
2399 bool IsPIE;
2400 std::tie(args&: RelocationModel, args&: PICLevel, args&: IsPIE) = ParsePICArgs(ToolChain, Args);
2401
2402 if (RelocationModel != llvm::Reloc::Static)
2403 CmdArgs.push_back(Elt: "-KPIC");
2404}
2405
2406/// Determine whether Objective-C automated reference counting is
2407/// enabled.
2408bool tools::isObjCAutoRefCount(const ArgList &Args) {
2409 return Args.hasFlag(Pos: options::OPT_fobjc_arc, Neg: options::OPT_fno_objc_arc, Default: false);
2410}
2411
2412enum class LibGccType { UnspecifiedLibGcc, StaticLibGcc, SharedLibGcc };
2413
2414static LibGccType getLibGccType(const ToolChain &TC, const Driver &D,
2415 const ArgList &Args) {
2416 if (Args.hasArg(Ids: options::OPT_static_libgcc) ||
2417 Args.hasArg(Ids: options::OPT_static) || Args.hasArg(Ids: options::OPT_static_pie) ||
2418 // The Android NDK only provides libunwind.a, not libunwind.so.
2419 TC.getTriple().isAndroid())
2420 return LibGccType::StaticLibGcc;
2421 if (Args.hasArg(Ids: options::OPT_shared_libgcc))
2422 return LibGccType::SharedLibGcc;
2423 return LibGccType::UnspecifiedLibGcc;
2424}
2425
2426// Gcc adds libgcc arguments in various ways:
2427//
2428// gcc <none>: -lgcc --as-needed -lgcc_s --no-as-needed
2429// g++ <none>: -lgcc_s -lgcc
2430// gcc shared: -lgcc_s -lgcc
2431// g++ shared: -lgcc_s -lgcc
2432// gcc static: -lgcc -lgcc_eh
2433// g++ static: -lgcc -lgcc_eh
2434// gcc static-pie: -lgcc -lgcc_eh
2435// g++ static-pie: -lgcc -lgcc_eh
2436//
2437// Also, certain targets need additional adjustments.
2438
2439static void AddUnwindLibrary(const ToolChain &TC, const Driver &D,
2440 ArgStringList &CmdArgs, const ArgList &Args) {
2441 ToolChain::UnwindLibType UNW = TC.GetUnwindLibType(Args);
2442 // By default OHOS binaries are linked statically to libunwind.
2443 if (TC.getTriple().isOHOSFamily() && UNW == ToolChain::UNW_CompilerRT) {
2444 CmdArgs.push_back(Elt: "-l:libunwind.a");
2445 return;
2446 }
2447
2448 // Targets that don't use unwind libraries.
2449 if ((TC.getTriple().isAndroid() && UNW == ToolChain::UNW_Libgcc) ||
2450 TC.getTriple().isOSIAMCU() || TC.getTriple().isOSBinFormatWasm() ||
2451 TC.getTriple().isWindowsMSVCEnvironment() || UNW == ToolChain::UNW_None)
2452 return;
2453
2454 LibGccType LGT = getLibGccType(TC, D, Args);
2455 bool AsNeeded = LGT == LibGccType::UnspecifiedLibGcc &&
2456 (UNW == ToolChain::UNW_CompilerRT || !D.CCCIsCXX()) &&
2457 !TC.getTriple().isAndroid() &&
2458 !TC.getTriple().isOSCygMing() && !TC.getTriple().isOSAIX();
2459 if (AsNeeded)
2460 addAsNeededOption(TC, Args, CmdArgs, as_needed: true);
2461
2462 switch (UNW) {
2463 case ToolChain::UNW_None:
2464 return;
2465 case ToolChain::UNW_Libgcc: {
2466 if (LGT == LibGccType::StaticLibGcc)
2467 CmdArgs.push_back(Elt: "-lgcc_eh");
2468 else
2469 CmdArgs.push_back(Elt: "-lgcc_s");
2470 break;
2471 }
2472 case ToolChain::UNW_CompilerRT:
2473 if (TC.getTriple().isOSAIX()) {
2474 // AIX only has libunwind as a shared library. So do not pass
2475 // anything in if -static is specified.
2476 if (LGT != LibGccType::StaticLibGcc)
2477 CmdArgs.push_back(Elt: "-lunwind");
2478 } else if (LGT == LibGccType::StaticLibGcc) {
2479 CmdArgs.push_back(Elt: "-l:libunwind.a");
2480 } else if (LGT == LibGccType::SharedLibGcc) {
2481 if (TC.getTriple().isOSCygMing())
2482 CmdArgs.push_back(Elt: "-l:libunwind.dll.a");
2483 else
2484 CmdArgs.push_back(Elt: "-l:libunwind.so");
2485 } else {
2486 // Let the linker choose between libunwind.so and libunwind.a
2487 // depending on what's available, and depending on the -static flag
2488 CmdArgs.push_back(Elt: "-lunwind");
2489 }
2490 break;
2491 }
2492
2493 if (AsNeeded)
2494 addAsNeededOption(TC, Args, CmdArgs, as_needed: false);
2495}
2496
2497static void AddLibgcc(const ToolChain &TC, const Driver &D,
2498 ArgStringList &CmdArgs, const ArgList &Args) {
2499 LibGccType LGT = getLibGccType(TC, D, Args);
2500 if (LGT == LibGccType::StaticLibGcc ||
2501 (LGT == LibGccType::UnspecifiedLibGcc && !D.CCCIsCXX()))
2502 CmdArgs.push_back(Elt: "-lgcc");
2503 AddUnwindLibrary(TC, D, CmdArgs, Args);
2504 if (LGT == LibGccType::SharedLibGcc ||
2505 (LGT == LibGccType::UnspecifiedLibGcc && D.CCCIsCXX()))
2506 CmdArgs.push_back(Elt: "-lgcc");
2507 // compiler-rt is needed after libgcc for flang on AArch64 for the
2508 // __trampoline_setup symbol
2509 if (D.IsFlangMode() && TC.getArch() == llvm::Triple::aarch64) {
2510 CmdArgs.push_back(Elt: "--as-needed");
2511 CmdArgs.push_back(Elt: TC.getCompilerRTArgString(Args, Component: "builtins"));
2512 CmdArgs.push_back(Elt: "--no-as-needed");
2513 }
2514}
2515
2516void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D,
2517 ArgStringList &CmdArgs, const ArgList &Args) {
2518 // Make use of compiler-rt if --rtlib option is used
2519 ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args);
2520
2521 switch (RLT) {
2522 case ToolChain::RLT_CompilerRT:
2523 CmdArgs.push_back(Elt: TC.getCompilerRTArgString(Args, Component: "builtins"));
2524 AddUnwindLibrary(TC, D, CmdArgs, Args);
2525 break;
2526 case ToolChain::RLT_Libgcc:
2527 // Make sure libgcc is not used under MSVC environment by default
2528 if (TC.getTriple().isKnownWindowsMSVCEnvironment()) {
2529 // Issue error diagnostic if libgcc is explicitly specified
2530 // through command line as --rtlib option argument.
2531 Arg *A = Args.getLastArg(Ids: options::OPT_rtlib_EQ);
2532 if (A && A->getValue() != StringRef("platform")) {
2533 TC.getDriver().Diag(DiagID: diag::err_drv_unsupported_rtlib_for_platform)
2534 << A->getValue() << "MSVC";
2535 }
2536 } else
2537 AddLibgcc(TC, D, CmdArgs, Args);
2538 break;
2539 }
2540
2541 // On Android, the unwinder uses dl_iterate_phdr (or one of
2542 // dl_unwind_find_exidx/__gnu_Unwind_Find_exidx on arm32) from libdl.so. For
2543 // statically-linked executables, these functions come from libc.a instead.
2544 if (TC.getTriple().isAndroid() && !Args.hasArg(Ids: options::OPT_static) &&
2545 !Args.hasArg(Ids: options::OPT_static_pie))
2546 CmdArgs.push_back(Elt: "-ldl");
2547}
2548
2549SmallString<128> tools::getStatsFileName(const llvm::opt::ArgList &Args,
2550 const InputInfo &Output,
2551 const InputInfo &Input,
2552 const Driver &D) {
2553 const Arg *A = Args.getLastArg(Ids: options::OPT_save_stats_EQ);
2554 if (!A && !D.CCPrintInternalStats)
2555 return {};
2556
2557 SmallString<128> StatsFile;
2558 if (A) {
2559 StringRef SaveStats = A->getValue();
2560 if (SaveStats == "obj" && Output.isFilename()) {
2561 StatsFile.assign(RHS: Output.getFilename());
2562 llvm::sys::path::remove_filename(path&: StatsFile);
2563 } else if (SaveStats != "cwd") {
2564 D.Diag(DiagID: diag::err_drv_invalid_value) << A->getAsString(Args) << SaveStats;
2565 return {};
2566 }
2567
2568 StringRef BaseName = llvm::sys::path::filename(path: Input.getBaseInput());
2569 llvm::sys::path::append(path&: StatsFile, a: BaseName);
2570 llvm::sys::path::replace_extension(path&: StatsFile, extension: "stats");
2571 } else {
2572 assert(D.CCPrintInternalStats);
2573 StatsFile.assign(RHS: D.CCPrintInternalStatReportFilename.empty()
2574 ? "-"
2575 : D.CCPrintInternalStatReportFilename);
2576 }
2577 return StatsFile;
2578}
2579
2580void tools::addMultilibFlag(bool Enabled, const StringRef Flag,
2581 Multilib::flags_list &Flags) {
2582 assert(Flag.front() == '-');
2583 if (Enabled) {
2584 Flags.push_back(x: Flag.str());
2585 } else {
2586 Flags.push_back(x: ("!" + Flag.substr(Start: 1)).str());
2587 }
2588}
2589
2590void tools::addX86AlignBranchArgs(const Driver &D, const ArgList &Args,
2591 ArgStringList &CmdArgs, bool IsLTO,
2592 const StringRef PluginOptPrefix) {
2593 auto addArg = [&, IsLTO](const Twine &Arg) {
2594 if (IsLTO) {
2595 assert(!PluginOptPrefix.empty() && "Cannot have empty PluginOptPrefix!");
2596 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + Arg));
2597 } else {
2598 CmdArgs.push_back(Elt: "-mllvm");
2599 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Arg));
2600 }
2601 };
2602
2603 if (Args.hasArg(Ids: options::OPT_mbranches_within_32B_boundaries)) {
2604 addArg(Twine("-x86-branches-within-32B-boundaries"));
2605 }
2606 if (const Arg *A = Args.getLastArg(Ids: options::OPT_malign_branch_boundary_EQ)) {
2607 StringRef Value = A->getValue();
2608 unsigned Boundary;
2609 if (Value.getAsInteger(Radix: 10, Result&: Boundary) || Boundary < 16 ||
2610 !llvm::isPowerOf2_64(Value: Boundary)) {
2611 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
2612 << Value << A->getOption().getName();
2613 } else {
2614 addArg("-x86-align-branch-boundary=" + Twine(Boundary));
2615 }
2616 }
2617 if (const Arg *A = Args.getLastArg(Ids: options::OPT_malign_branch_EQ)) {
2618 std::string AlignBranch;
2619 for (StringRef T : A->getValues()) {
2620 if (T != "fused" && T != "jcc" && T != "jmp" && T != "call" &&
2621 T != "ret" && T != "indirect")
2622 D.Diag(DiagID: diag::err_drv_invalid_malign_branch_EQ)
2623 << T << "fused, jcc, jmp, call, ret, indirect";
2624 if (!AlignBranch.empty())
2625 AlignBranch += '+';
2626 AlignBranch += T;
2627 }
2628 addArg("-x86-align-branch=" + Twine(AlignBranch));
2629 }
2630 if (const Arg *A = Args.getLastArg(Ids: options::OPT_mpad_max_prefix_size_EQ)) {
2631 StringRef Value = A->getValue();
2632 unsigned PrefixSize;
2633 if (Value.getAsInteger(Radix: 10, Result&: PrefixSize)) {
2634 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
2635 << Value << A->getOption().getName();
2636 } else {
2637 addArg("-x86-pad-max-prefix-size=" + Twine(PrefixSize));
2638 }
2639 }
2640}
2641
2642/// SDLSearch: Search for Static Device Library
2643/// The search for SDL bitcode files is consistent with how static host
2644/// libraries are discovered. That is, the -l option triggers a search for
2645/// files in a set of directories called the LINKPATH. The host library search
2646/// procedure looks for a specific filename in the LINKPATH. The filename for
2647/// a host library is lib<libname>.a or lib<libname>.so. For SDLs, there is an
2648/// ordered-set of filenames that are searched. We call this ordered-set of
2649/// filenames as SEARCH-ORDER. Since an SDL can either be device-type specific,
2650/// architecture specific, or generic across all architectures, a naming
2651/// convention and search order is used where the file name embeds the
2652/// architecture name <arch-name> (nvptx or amdgcn) and the GPU device type
2653/// <device-name> such as sm_30 and gfx906. <device-name> is absent in case of
2654/// device-independent SDLs. To reduce congestion in host library directories,
2655/// the search first looks for files in the “libdevice” subdirectory. SDLs that
2656/// are bc files begin with the prefix “lib”.
2657///
2658/// Machine-code SDLs can also be managed as an archive (*.a file). The
2659/// convention has been to use the prefix “lib”. To avoid confusion with host
2660/// archive libraries, we use prefix "libbc-" for the bitcode SDL archives.
2661///
2662static bool SDLSearch(const Driver &D, const llvm::opt::ArgList &DriverArgs,
2663 llvm::opt::ArgStringList &CC1Args,
2664 const SmallVectorImpl<std::string> &LibraryPaths,
2665 StringRef Lib, StringRef Arch, StringRef Target,
2666 bool isBitCodeSDL) {
2667 SmallVector<std::string, 12> SDLs;
2668
2669 std::string LibDeviceLoc = "/libdevice";
2670 std::string LibBcPrefix = "/libbc-";
2671 std::string LibPrefix = "/lib";
2672
2673 if (isBitCodeSDL) {
2674 // SEARCH-ORDER for Bitcode SDLs:
2675 // libdevice/libbc-<libname>-<arch-name>-<device-type>.a
2676 // libbc-<libname>-<arch-name>-<device-type>.a
2677 // libdevice/libbc-<libname>-<arch-name>.a
2678 // libbc-<libname>-<arch-name>.a
2679 // libdevice/libbc-<libname>.a
2680 // libbc-<libname>.a
2681 // libdevice/lib<libname>-<arch-name>-<device-type>.bc
2682 // lib<libname>-<arch-name>-<device-type>.bc
2683 // libdevice/lib<libname>-<arch-name>.bc
2684 // lib<libname>-<arch-name>.bc
2685 // libdevice/lib<libname>.bc
2686 // lib<libname>.bc
2687
2688 for (StringRef Base : {LibBcPrefix, LibPrefix}) {
2689 const auto *Ext = Base.contains(Other: LibBcPrefix) ? ".a" : ".bc";
2690
2691 for (auto Suffix : {Twine(Lib + "-" + Arch + "-" + Target).str(),
2692 Twine(Lib + "-" + Arch).str(), Twine(Lib).str()}) {
2693 SDLs.push_back(Elt: Twine(LibDeviceLoc + Base + Suffix + Ext).str());
2694 SDLs.push_back(Elt: Twine(Base + Suffix + Ext).str());
2695 }
2696 }
2697 } else {
2698 // SEARCH-ORDER for Machine-code SDLs:
2699 // libdevice/lib<libname>-<arch-name>-<device-type>.a
2700 // lib<libname>-<arch-name>-<device-type>.a
2701 // libdevice/lib<libname>-<arch-name>.a
2702 // lib<libname>-<arch-name>.a
2703
2704 const auto *Ext = ".a";
2705
2706 for (auto Suffix : {Twine(Lib + "-" + Arch + "-" + Target).str(),
2707 Twine(Lib + "-" + Arch).str()}) {
2708 SDLs.push_back(Elt: Twine(LibDeviceLoc + LibPrefix + Suffix + Ext).str());
2709 SDLs.push_back(Elt: Twine(LibPrefix + Suffix + Ext).str());
2710 }
2711 }
2712
2713 // The CUDA toolchain does not use a global device llvm-link before the LLVM
2714 // backend generates ptx. So currently, the use of bitcode SDL for nvptx is
2715 // only possible with post-clang-cc1 linking. Clang cc1 has a feature that
2716 // will link libraries after clang compilation while the LLVM IR is still in
2717 // memory. This utilizes a clang cc1 option called “-mlink-builtin-bitcode”.
2718 // This is a clang -cc1 option that is generated by the clang driver. The
2719 // option value must a full path to an existing file.
2720 bool FoundSDL = false;
2721 for (auto LPath : LibraryPaths) {
2722 for (auto SDL : SDLs) {
2723 auto FullName = Twine(LPath + SDL).str();
2724 if (llvm::sys::fs::exists(Path: FullName)) {
2725 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: FullName));
2726 FoundSDL = true;
2727 break;
2728 }
2729 }
2730 if (FoundSDL)
2731 break;
2732 }
2733 return FoundSDL;
2734}
2735
2736/// Search if a user provided archive file lib<libname>.a exists in any of
2737/// the library paths. If so, add a new command to clang-offload-bundler to
2738/// unbundle this archive and create a temporary device specific archive. Name
2739/// of this SDL is passed to the llvm-link tool.
2740static void GetSDLFromOffloadArchive(
2741 Compilation &C, const Driver &D, const Tool &T, const JobAction &JA,
2742 const InputInfoList &Inputs, const llvm::opt::ArgList &DriverArgs,
2743 llvm::opt::ArgStringList &CC1Args,
2744 const SmallVectorImpl<std::string> &LibraryPaths, StringRef Lib,
2745 StringRef Arch, StringRef Target, bool isBitCodeSDL) {
2746
2747 // We don't support bitcode archive bundles for nvptx
2748 if (isBitCodeSDL && Arch.contains(Other: "nvptx"))
2749 return;
2750
2751 bool FoundAOB = false;
2752 std::string ArchiveOfBundles;
2753
2754 llvm::Triple Triple(D.getTargetTriple());
2755 bool IsMSVC = Triple.isWindowsMSVCEnvironment();
2756 auto Ext = IsMSVC ? ".lib" : ".a";
2757 if (!Lib.starts_with(Prefix: ":") && !Lib.starts_with(Prefix: "-l")) {
2758 if (llvm::sys::fs::exists(Path: Lib)) {
2759 ArchiveOfBundles = Lib;
2760 FoundAOB = true;
2761 }
2762 } else {
2763 Lib.consume_front(Prefix: "-l");
2764 for (auto LPath : LibraryPaths) {
2765 ArchiveOfBundles.clear();
2766 auto LibFile = (Lib.starts_with(Prefix: ":") ? Lib.drop_front()
2767 : IsMSVC ? Lib + Ext
2768 : "lib" + Lib + Ext)
2769 .str();
2770 for (auto Prefix : {"/libdevice/", "/"}) {
2771 auto AOB = Twine(LPath + Prefix + LibFile).str();
2772 if (llvm::sys::fs::exists(Path: AOB)) {
2773 ArchiveOfBundles = AOB;
2774 FoundAOB = true;
2775 break;
2776 }
2777 }
2778 if (FoundAOB)
2779 break;
2780 }
2781 }
2782
2783 if (!FoundAOB)
2784 return;
2785
2786 llvm::file_magic Magic;
2787 auto EC = llvm::identify_magic(path: ArchiveOfBundles, result&: Magic);
2788 if (EC || Magic != llvm::file_magic::archive)
2789 return;
2790
2791 StringRef Prefix = isBitCodeSDL ? "libbc-" : "lib";
2792 std::string OutputLib =
2793 D.GetTemporaryPath(Prefix: Twine(Prefix + llvm::sys::path::filename(path: Lib) + "-" +
2794 Arch + "-" + Target)
2795 .str(),
2796 Suffix: "a");
2797
2798 C.addTempFile(Name: C.getArgs().MakeArgString(Str: OutputLib));
2799
2800 SmallString<128> DeviceTriple;
2801 DeviceTriple += Action::GetOffloadKindName(Kind: JA.getOffloadingDeviceKind());
2802 DeviceTriple += '-';
2803 std::string NormalizedTriple =
2804 T.getToolChain().getEffectiveTriple().normalize(
2805 Form: llvm::Triple::CanonicalForm::FOUR_IDENT);
2806 DeviceTriple += NormalizedTriple;
2807 if (!Target.empty()) {
2808 DeviceTriple += '-';
2809 DeviceTriple += Target;
2810 }
2811
2812 std::string UnbundleArg("-unbundle");
2813 std::string TypeArg("-type=a");
2814 std::string InputArg("-input=" + ArchiveOfBundles);
2815 std::string OffloadArg("-targets=" + std::string(DeviceTriple));
2816 std::string OutputArg("-output=" + OutputLib);
2817
2818 const char *UBProgram = DriverArgs.MakeArgString(
2819 Str: T.getToolChain().GetProgramPath(Name: "clang-offload-bundler"));
2820
2821 ArgStringList UBArgs;
2822 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: UnbundleArg));
2823 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: TypeArg));
2824 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: InputArg));
2825 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: OffloadArg));
2826 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: OutputArg));
2827
2828 // Add this flag to not exit from clang-offload-bundler if no compatible
2829 // code object is found in heterogenous archive library.
2830 std::string AdditionalArgs("-allow-missing-bundles");
2831 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: AdditionalArgs));
2832
2833 // Add this flag to treat hip and hipv4 offload kinds as compatible with
2834 // openmp offload kind while extracting code objects from a heterogenous
2835 // archive library. Vice versa is also considered compatible.
2836 std::string HipCompatibleArgs("-hip-openmp-compatible");
2837 UBArgs.push_back(Elt: C.getArgs().MakeArgString(Str: HipCompatibleArgs));
2838
2839 C.addCommand(Cmd: std::make_unique<Command>(
2840 args: JA, args: T, args: ResponseFileSupport::AtFileCurCP(), args&: UBProgram, args&: UBArgs, args: Inputs,
2841 args: InputInfo(&JA, C.getArgs().MakeArgString(Str: OutputLib))));
2842
2843 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: OutputLib));
2844}
2845
2846// Wrapper function used by driver for adding SDLs during link phase.
2847void tools::AddStaticDeviceLibsLinking(Compilation &C, const Tool &T,
2848 const JobAction &JA,
2849 const InputInfoList &Inputs,
2850 const llvm::opt::ArgList &DriverArgs,
2851 llvm::opt::ArgStringList &CC1Args,
2852 StringRef Arch, StringRef Target,
2853 bool isBitCodeSDL) {
2854 AddStaticDeviceLibs(C: &C, T: &T, JA: &JA, Inputs: &Inputs, D: C.getDriver(), DriverArgs, CmdArgs&: CC1Args,
2855 Arch, Target, isBitCodeSDL);
2856}
2857
2858// User defined Static Device Libraries(SDLs) can be passed to clang for
2859// offloading GPU compilers. Like static host libraries, the use of a SDL is
2860// specified with the -l command line option. The primary difference between
2861// host and SDLs is the filenames for SDLs (refer SEARCH-ORDER for Bitcode SDLs
2862// and SEARCH-ORDER for Machine-code SDLs for the naming convention).
2863// SDLs are of following types:
2864//
2865// * Bitcode SDLs: They can either be a *.bc file or an archive of *.bc files.
2866// For NVPTX, these libraries are post-clang linked following each
2867// compilation. For AMDGPU, these libraries are linked one time
2868// during the application link phase.
2869//
2870// * Machine-code SDLs: They are archive files. For AMDGPU, the process for
2871// machine code SDLs is still in development. But they will be linked
2872// by the LLVM tool lld.
2873//
2874// * Bundled objects that contain both host and device codes: Bundled objects
2875// may also contain library code compiled from source. For NVPTX, the
2876// bundle contains cubin. For AMDGPU, the bundle contains bitcode.
2877//
2878// For Bitcode and Machine-code SDLs, current compiler toolchains hardcode the
2879// inclusion of specific SDLs such as math libraries and the OpenMP device
2880// library libomptarget.
2881void tools::AddStaticDeviceLibs(Compilation *C, const Tool *T,
2882 const JobAction *JA,
2883 const InputInfoList *Inputs, const Driver &D,
2884 const llvm::opt::ArgList &DriverArgs,
2885 llvm::opt::ArgStringList &CC1Args,
2886 StringRef Arch, StringRef Target,
2887 bool isBitCodeSDL) {
2888
2889 SmallVector<std::string, 8> LibraryPaths;
2890 // Add search directories from LIBRARY_PATH env variable
2891 std::optional<std::string> LibPath =
2892 llvm::sys::Process::GetEnv(name: "LIBRARY_PATH");
2893 if (LibPath) {
2894 SmallVector<StringRef, 8> Frags;
2895 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
2896 llvm::SplitString(Source: *LibPath, OutFragments&: Frags, Delimiters: EnvPathSeparatorStr);
2897 for (StringRef Path : Frags)
2898 LibraryPaths.emplace_back(Args: Path.trim());
2899 }
2900
2901 // Add directories from user-specified -L options
2902 for (std::string Search_Dir : DriverArgs.getAllArgValues(Id: options::OPT_L))
2903 LibraryPaths.emplace_back(Args&: Search_Dir);
2904
2905 // Add path to lib-debug folders
2906 SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(path: D.Dir);
2907 llvm::sys::path::append(path&: DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME);
2908 LibraryPaths.emplace_back(Args: DefaultLibPath.c_str());
2909
2910 // Build list of Static Device Libraries SDLs specified by -l option
2911 llvm::SmallSet<std::string, 16> SDLNames;
2912 static const StringRef HostOnlyArchives[] = {
2913 "omp", "cudart", "m", "gcc", "gcc_s", "pthread", "hip_hcc"};
2914 for (auto SDLName : DriverArgs.getAllArgValues(Id: options::OPT_l)) {
2915 if (!llvm::is_contained(Range: HostOnlyArchives, Element: SDLName)) {
2916 SDLNames.insert(V: std::string("-l") + SDLName);
2917 }
2918 }
2919
2920 for (auto Input : DriverArgs.getAllArgValues(Id: options::OPT_INPUT)) {
2921 auto FileName = StringRef(Input);
2922 // Clang treats any unknown file types as archives and passes them to the
2923 // linker. Files with extension 'lib' are classified as TY_Object by clang
2924 // but they are usually archives. It is OK if the file is not really an
2925 // archive since GetSDLFromOffloadArchive will check the magic of the file
2926 // and only unbundle it if it is really an archive.
2927 const StringRef LibFileExt = ".lib";
2928 if (!llvm::sys::path::has_extension(path: FileName) ||
2929 types::lookupTypeForExtension(
2930 Ext: llvm::sys::path::extension(path: FileName).drop_front()) ==
2931 types::TY_INVALID ||
2932 llvm::sys::path::extension(path: FileName) == LibFileExt)
2933 SDLNames.insert(V: Input);
2934 }
2935
2936 // The search stops as soon as an SDL file is found. The driver then provides
2937 // the full filename of the SDL to the llvm-link command. If no SDL is found
2938 // after searching each LINKPATH with SEARCH-ORDER, it is possible that an
2939 // archive file lib<libname>.a exists and may contain bundled object files.
2940 for (auto SDLName : SDLNames) {
2941 // This is the only call to SDLSearch
2942 if (!SDLSearch(D, DriverArgs, CC1Args, LibraryPaths, Lib: SDLName, Arch, Target,
2943 isBitCodeSDL)) {
2944 GetSDLFromOffloadArchive(C&: *C, D, T: *T, JA: *JA, Inputs: *Inputs, DriverArgs, CC1Args,
2945 LibraryPaths, Lib: SDLName, Arch, Target,
2946 isBitCodeSDL);
2947 }
2948 }
2949}
2950
2951static llvm::opt::Arg *
2952getAMDGPUCodeObjectArgument(const Driver &D, const llvm::opt::ArgList &Args) {
2953 return Args.getLastArg(Ids: options::OPT_mcode_object_version_EQ);
2954}
2955
2956void tools::checkAMDGPUCodeObjectVersion(const Driver &D,
2957 const llvm::opt::ArgList &Args) {
2958 const unsigned MinCodeObjVer = 4;
2959 const unsigned MaxCodeObjVer = 6;
2960
2961 if (auto *CodeObjArg = getAMDGPUCodeObjectArgument(D, Args)) {
2962 if (CodeObjArg->getOption().getID() ==
2963 options::OPT_mcode_object_version_EQ) {
2964 unsigned CodeObjVer = MaxCodeObjVer;
2965 auto Remnant =
2966 StringRef(CodeObjArg->getValue()).getAsInteger(Radix: 0, Result&: CodeObjVer);
2967 if (Remnant || CodeObjVer < MinCodeObjVer || CodeObjVer > MaxCodeObjVer)
2968 D.Diag(DiagID: diag::err_drv_invalid_int_value)
2969 << CodeObjArg->getAsString(Args) << CodeObjArg->getValue();
2970 }
2971 }
2972}
2973
2974unsigned tools::getAMDGPUCodeObjectVersion(const Driver &D,
2975 const llvm::opt::ArgList &Args) {
2976 unsigned CodeObjVer = 6; // default
2977 if (auto *CodeObjArg = getAMDGPUCodeObjectArgument(D, Args))
2978 StringRef(CodeObjArg->getValue()).getAsInteger(Radix: 0, Result&: CodeObjVer);
2979 return CodeObjVer;
2980}
2981
2982bool tools::haveAMDGPUCodeObjectVersionArgument(
2983 const Driver &D, const llvm::opt::ArgList &Args) {
2984 return getAMDGPUCodeObjectArgument(D, Args) != nullptr;
2985}
2986
2987void tools::addMachineOutlinerArgs(const Driver &D,
2988 const llvm::opt::ArgList &Args,
2989 llvm::opt::ArgStringList &CmdArgs,
2990 const llvm::Triple &Triple, bool IsLTO,
2991 const StringRef PluginOptPrefix) {
2992 auto addArg = [&, IsLTO](const Twine &Arg) {
2993 if (IsLTO) {
2994 assert(!PluginOptPrefix.empty() && "Cannot have empty PluginOptPrefix!");
2995 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine(PluginOptPrefix) + Arg));
2996 } else {
2997 CmdArgs.push_back(Elt: "-mllvm");
2998 CmdArgs.push_back(Elt: Args.MakeArgString(Str: Arg));
2999 }
3000 };
3001
3002 if (Arg *A = Args.getLastArg(Ids: options::OPT_moutline,
3003 Ids: options::OPT_mno_outline)) {
3004 if (A->getOption().matches(ID: options::OPT_moutline)) {
3005 // We only support -moutline in AArch64, ARM, RISC-V and X86 targets right
3006 // now. If we're compiling for these, add the proper mllvm flags.
3007 // Otherwise, emit a warning and ignore the flag.
3008 if (Triple.isARM() || Triple.isThumb() || Triple.isAArch64() ||
3009 Triple.isRISCV() || Triple.isX86()) {
3010 addArg(Twine("-enable-machine-outliner"));
3011 } else {
3012 D.Diag(DiagID: diag::warn_drv_moutline_unsupported_opt) << Triple.getArchName();
3013 }
3014 } else {
3015 if (!IsLTO)
3016 // Disable all outlining behaviour using `nooutline` attribute, in case
3017 // Linker Invocation lacks `-mno-outline`.
3018 CmdArgs.push_back(Elt: "-mno-outline");
3019
3020 // Disable Pass in Pipeline
3021 addArg(Twine("-enable-machine-outliner=never"));
3022 }
3023 }
3024
3025 auto *CodeGenDataGenArg =
3026 Args.getLastArg(Ids: options::OPT_fcodegen_data_generate_EQ);
3027 auto *CodeGenDataUseArg = Args.getLastArg(Ids: options::OPT_fcodegen_data_use_EQ);
3028
3029 // We only allow one of them to be specified.
3030 if (CodeGenDataGenArg && CodeGenDataUseArg)
3031 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
3032 << CodeGenDataGenArg->getAsString(Args)
3033 << CodeGenDataUseArg->getAsString(Args);
3034
3035 // For codegen data gen, the output file is passed to the linker
3036 // while a boolean flag is passed to the LLVM backend.
3037 if (CodeGenDataGenArg)
3038 addArg(Twine("-codegen-data-generate"));
3039
3040 // For codegen data use, the input file is passed to the LLVM backend.
3041 if (CodeGenDataUseArg)
3042 addArg(Twine("-codegen-data-use-path=") + CodeGenDataUseArg->getValue());
3043}
3044
3045void tools::addOpenMPDeviceRTL(const Driver &D,
3046 const llvm::opt::ArgList &DriverArgs,
3047 llvm::opt::ArgStringList &CC1Args,
3048 StringRef BitcodeSuffix,
3049 const llvm::Triple &Triple,
3050 const ToolChain &HostTC) {
3051 SmallVector<StringRef, 8> LibraryPaths;
3052
3053 // Add user defined library paths from LIBRARY_PATH.
3054 std::optional<std::string> LibPath =
3055 llvm::sys::Process::GetEnv(name: "LIBRARY_PATH");
3056 if (LibPath) {
3057 SmallVector<StringRef, 8> Frags;
3058 const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'};
3059 llvm::SplitString(Source: *LibPath, OutFragments&: Frags, Delimiters: EnvPathSeparatorStr);
3060 for (StringRef Path : Frags)
3061 LibraryPaths.emplace_back(Args: Path.trim());
3062 }
3063
3064 // Check all of the standard library search paths used by the compiler.
3065 for (const auto &LibPath : HostTC.getFilePaths())
3066 LibraryPaths.emplace_back(Args: LibPath);
3067
3068 // Check the target specific library path for the triple as well.
3069 SmallString<128> P(D.Dir);
3070 llvm::sys::path::append(path&: P, a: "..", b: "lib", c: Triple.getTriple());
3071 LibraryPaths.emplace_back(Args&: P);
3072
3073 OptSpecifier LibomptargetBCPathOpt =
3074 Triple.isAMDGCN() ? options::OPT_libomptarget_amdgpu_bc_path_EQ
3075 : Triple.isNVPTX() ? options::OPT_libomptarget_nvptx_bc_path_EQ
3076 : options::OPT_libomptarget_spirv_bc_path_EQ;
3077
3078 StringRef ArchPrefix = Triple.isAMDGCN() ? "amdgpu"
3079 : Triple.isNVPTX() ? "nvptx"
3080 : "spirv";
3081 std::string LibOmpTargetName = ("libomptarget-" + ArchPrefix + ".bc").str();
3082
3083 // First check whether user specifies bc library
3084 if (const Arg *A = DriverArgs.getLastArg(Ids: LibomptargetBCPathOpt)) {
3085 SmallString<128> LibOmpTargetFile(A->getValue());
3086 if (llvm::sys::fs::exists(Path: LibOmpTargetFile) &&
3087 llvm::sys::fs::is_directory(Path: LibOmpTargetFile)) {
3088 llvm::sys::path::append(path&: LibOmpTargetFile, a: LibOmpTargetName);
3089 }
3090
3091 if (llvm::sys::fs::exists(Path: LibOmpTargetFile)) {
3092 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
3093 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: LibOmpTargetFile));
3094 } else {
3095 D.Diag(DiagID: diag::err_drv_omp_offload_target_bcruntime_not_found)
3096 << LibOmpTargetFile;
3097 }
3098 } else {
3099 bool FoundBCLibrary = false;
3100
3101 for (StringRef LibraryPath : LibraryPaths) {
3102 SmallString<128> LibOmpTargetFile(LibraryPath);
3103 llvm::sys::path::append(path&: LibOmpTargetFile, a: LibOmpTargetName);
3104 if (llvm::sys::fs::exists(Path: LibOmpTargetFile)) {
3105 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
3106 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: LibOmpTargetFile));
3107 FoundBCLibrary = true;
3108 break;
3109 }
3110 }
3111
3112 if (!FoundBCLibrary)
3113 D.Diag(DiagID: diag::err_drv_omp_offload_target_missingbcruntime)
3114 << LibOmpTargetName << ArchPrefix;
3115 }
3116}
3117
3118void tools::addOpenCLBuiltinsLib(const Driver &D, const llvm::Triple &TT,
3119 const llvm::opt::ArgList &DriverArgs,
3120 llvm::opt::ArgStringList &CC1Args) {
3121
3122 StringRef LibclcNamespec;
3123 const Arg *A = DriverArgs.getLastArg(Ids: options::OPT_libclc_lib_EQ);
3124 if (A) {
3125 // If the namespec is of the form :filename we use it exactly.
3126 LibclcNamespec = A->getValue();
3127 } else {
3128 if (!TT.isAMDGPU() || TT.getEnvironment() != llvm::Triple::LLVM)
3129 return;
3130
3131 // TODO: Should this accept following -stdlib to override?
3132 if (DriverArgs.hasArg(Ids: options::OPT_no_offloadlib,
3133 Ids: options::OPT_nodefaultlibs, Ids: options::OPT_nostdlib))
3134 return;
3135 }
3136
3137 bool FilenameSearch = LibclcNamespec.consume_front(Prefix: ":");
3138 if (FilenameSearch) {
3139 SmallString<128> LibclcFile(LibclcNamespec);
3140 if (D.getVFS().exists(Path: LibclcFile)) {
3141 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
3142 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: LibclcFile));
3143 return;
3144 }
3145 D.Diag(DiagID: diag::err_drv_libclc_not_found) << LibclcFile;
3146 return;
3147 }
3148
3149 // The OpenCL libraries are stored in <ResourceDir>/lib/<triple>.
3150 SmallString<128> BasePath(D.ResourceDir);
3151 llvm::sys::path::append(path&: BasePath, a: "lib");
3152 llvm::sys::path::append(path&: BasePath, a: D.getTargetTriple());
3153
3154 // First check for a CPU-specific library in <ResourceDir>/lib/<triple>/<CPU>.
3155 // TODO: Factor this into common logic that checks for valid subtargets.
3156 if (const Arg *CPUArg = DriverArgs.getLastArg(Ids: options::OPT_mcpu_EQ)) {
3157 StringRef CPU = CPUArg->getValue();
3158 if (!CPU.empty()) {
3159 SmallString<128> CPUPath(BasePath);
3160 llvm::sys::path::append(path&: CPUPath, a: CPU, b: "libclc.bc");
3161 if (D.getVFS().exists(Path: CPUPath)) {
3162 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
3163 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: CPUPath));
3164 return;
3165 }
3166 }
3167 }
3168
3169 // Fall back to the generic library for the triple.
3170 SmallString<128> GenericPath(BasePath);
3171 llvm::sys::path::append(path&: GenericPath, a: "libclc.bc");
3172 if (D.getVFS().exists(Path: GenericPath)) {
3173 CC1Args.push_back(Elt: "-mlink-builtin-bitcode");
3174 CC1Args.push_back(Elt: DriverArgs.MakeArgString(Str: GenericPath));
3175 return;
3176 }
3177
3178 D.Diag(DiagID: diag::err_drv_libclc_not_found) << "libclc.bc";
3179}
3180
3181void tools::addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC,
3182 const llvm::opt::ArgList &Args,
3183 llvm::opt::ArgStringList &CmdArgs,
3184 const llvm::Triple &Triple) {
3185 if (Arg *A = Args.getLastArg(Ids: options::OPT_moutline_atomics,
3186 Ids: options::OPT_mno_outline_atomics)) {
3187 // Option -moutline-atomics supported for AArch64 target only.
3188 if (!Triple.isAArch64()) {
3189 D.Diag(DiagID: diag::warn_drv_moutline_atomics_unsupported_opt)
3190 << Triple.getArchName() << A->getOption().getName();
3191 } else {
3192 if (A->getOption().matches(ID: options::OPT_moutline_atomics)) {
3193 CmdArgs.push_back(Elt: "-target-feature");
3194 CmdArgs.push_back(Elt: "+outline-atomics");
3195 } else {
3196 CmdArgs.push_back(Elt: "-target-feature");
3197 CmdArgs.push_back(Elt: "-outline-atomics");
3198 }
3199 }
3200 } else if (Triple.isAArch64() && TC.IsAArch64OutlineAtomicsDefault(Args)) {
3201 CmdArgs.push_back(Elt: "-target-feature");
3202 CmdArgs.push_back(Elt: "+outline-atomics");
3203 }
3204}
3205
3206void tools::addOffloadCompressArgs(const llvm::opt::ArgList &TCArgs,
3207 llvm::opt::ArgStringList &CmdArgs) {
3208 if (TCArgs.hasFlag(Pos: options::OPT_offload_compress,
3209 Neg: options::OPT_no_offload_compress, Default: false))
3210 CmdArgs.push_back(Elt: "--compress");
3211 if (TCArgs.hasArg(Ids: options::OPT_v))
3212 CmdArgs.push_back(Elt: "--verbose");
3213 if (auto *Arg = TCArgs.getLastArg(Ids: options::OPT_offload_compression_level_EQ))
3214 CmdArgs.push_back(
3215 Elt: TCArgs.MakeArgString(Str: Twine("--compression-level=") + Arg->getValue()));
3216}
3217
3218void tools::addMCModel(const Driver &D, const llvm::opt::ArgList &Args,
3219 const llvm::Triple &Triple,
3220 const llvm::Reloc::Model &RelocationModel,
3221 llvm::opt::ArgStringList &CmdArgs) {
3222 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcmodel_EQ)) {
3223 StringRef CM = A->getValue();
3224 bool Ok = false;
3225 if (Triple.isOSAIX() && CM == "medium")
3226 CM = "large";
3227 if (Triple.isAArch64(PointerWidth: 64)) {
3228 Ok = CM == "tiny" || CM == "small" || CM == "large";
3229 if (CM == "large" && !Triple.isOSBinFormatMachO() &&
3230 RelocationModel != llvm::Reloc::Static)
3231 D.Diag(DiagID: diag::err_drv_argument_only_allowed_with)
3232 << A->getAsString(Args) << "-fno-pic";
3233 } else if (Triple.isLoongArch()) {
3234 if (CM == "extreme" &&
3235 Args.hasFlagNoClaim(Pos: options::OPT_fplt, Neg: options::OPT_fno_plt, Default: false))
3236 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
3237 << A->getAsString(Args) << "-fplt";
3238 Ok = CM == "normal" || CM == "medium" || CM == "extreme";
3239 // Convert to LLVM recognizable names.
3240 if (Ok)
3241 CM = llvm::StringSwitch<StringRef>(CM)
3242 .Case(S: "normal", Value: "small")
3243 .Case(S: "extreme", Value: "large")
3244 .Default(Value: CM);
3245 } else if (Triple.isPPC64() || Triple.isOSAIX()) {
3246 Ok = CM == "small" || CM == "medium" || CM == "large";
3247 } else if (Triple.isRISCV()) {
3248 // Large code model is disallowed to be used with PIC code model.
3249 if (CM == "large" && RelocationModel != llvm::Reloc::Static)
3250 D.Diag(DiagID: diag::err_drv_argument_not_allowed_with)
3251 << A->getAsString(Args) << "-fpic";
3252 if (CM == "medlow")
3253 CM = "small";
3254 else if (CM == "medany")
3255 CM = "medium";
3256 Ok = CM == "small" || CM == "medium" ||
3257 (CM == "large" && Triple.isRISCV64());
3258 } else if (Triple.getArch() == llvm::Triple::x86_64) {
3259 Ok = llvm::is_contained(Set: {"small", "kernel", "medium", "large"}, Element: CM);
3260 } else if (Triple.isNVPTX() || Triple.isAMDGPU() || Triple.isSPIRV()) {
3261 // NVPTX/AMDGPU/SPIRV does not care about the code model and will accept
3262 // whatever works for the host.
3263 Ok = true;
3264 } else if (Triple.isSPARC64()) {
3265 if (CM == "medlow")
3266 CM = "small";
3267 else if (CM == "medmid")
3268 CM = "medium";
3269 else if (CM == "medany")
3270 CM = "large";
3271 Ok = CM == "small" || CM == "medium" || CM == "large";
3272 } else if (Triple.getArch() == llvm::Triple::lanai) {
3273 Ok = llvm::is_contained(Set: {"small", "medium", "large"}, Element: CM);
3274 }
3275 if (Ok) {
3276 CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mcmodel=" + CM));
3277 } else {
3278 D.Diag(DiagID: diag::err_drv_unsupported_option_argument_for_target)
3279 << A->getSpelling() << CM << Triple.getTriple();
3280 }
3281 }
3282
3283 if (Triple.getArch() == llvm::Triple::x86_64) {
3284 bool IsMediumCM = false;
3285 bool IsLargeCM = false;
3286 if (Arg *A = Args.getLastArg(Ids: options::OPT_mcmodel_EQ)) {
3287 IsMediumCM = StringRef(A->getValue()) == "medium";
3288 IsLargeCM = StringRef(A->getValue()) == "large";
3289 }
3290 if (Arg *A = Args.getLastArg(Ids: options::OPT_mlarge_data_threshold_EQ)) {
3291 if (!IsMediumCM && !IsLargeCM) {
3292 D.Diag(DiagID: diag::warn_drv_large_data_threshold_invalid_code_model)
3293 << A->getOption().getRenderName();
3294 } else {
3295 A->render(Args, Output&: CmdArgs);
3296 }
3297 } else if (IsMediumCM) {
3298 CmdArgs.push_back(Elt: "-mlarge-data-threshold=65536");
3299 } else if (IsLargeCM) {
3300 CmdArgs.push_back(Elt: "-mlarge-data-threshold=0");
3301 }
3302 }
3303}
3304
3305void tools::handleColorDiagnosticsArgs(const Driver &D, const ArgList &Args,
3306 ArgStringList &CmdArgs) {
3307 // Color diagnostics are parsed by the driver directly from argv and later
3308 // re-parsed to construct this job; claim any possible color diagnostic here
3309 // to avoid warn_drv_unused_argument and diagnose bad
3310 // OPT_fdiagnostics_color_EQ values.
3311 Args.getLastArg(Ids: options::OPT_fcolor_diagnostics,
3312 Ids: options::OPT_fno_color_diagnostics);
3313 if (const Arg *A = Args.getLastArg(Ids: options::OPT_fdiagnostics_color_EQ)) {
3314 StringRef Value(A->getValue());
3315 if (Value != "always" && Value != "never" && Value != "auto")
3316 D.Diag(DiagID: diag::err_drv_invalid_argument_to_option)
3317 << Value << A->getOption().getName();
3318 }
3319
3320 if (D.getDiags().getDiagnosticOptions().ShowColors)
3321 CmdArgs.push_back(Elt: "-fcolor-diagnostics");
3322}
3323
3324void tools::escapeSpacesAndBackslashes(const char *Arg,
3325 llvm::SmallVectorImpl<char> &Res) {
3326 for (; *Arg; ++Arg) {
3327 switch (*Arg) {
3328 default:
3329 break;
3330 case ' ':
3331 case '\\':
3332 Res.push_back(Elt: '\\');
3333 break;
3334 }
3335 Res.push_back(Elt: *Arg);
3336 }
3337}
3338
3339const char *tools::renderEscapedCommandLine(const ToolChain &TC,
3340 const llvm::opt::ArgList &Args) {
3341 const Driver &D = TC.getDriver();
3342 const char *Exec = D.getDriverProgramPath();
3343
3344 llvm::opt::ArgStringList OriginalArgs;
3345 for (const auto &Arg : Args)
3346 Arg->render(Args, Output&: OriginalArgs);
3347
3348 llvm::SmallString<256> Flags;
3349 escapeSpacesAndBackslashes(Arg: Exec, Res&: Flags);
3350 for (const char *OriginalArg : OriginalArgs) {
3351 llvm::SmallString<128> EscapedArg;
3352 escapeSpacesAndBackslashes(Arg: OriginalArg, Res&: EscapedArg);
3353 Flags += " ";
3354 Flags += EscapedArg;
3355 }
3356
3357 return Args.MakeArgString(Str: Flags);
3358}
3359
3360bool tools::shouldRecordCommandLine(const ToolChain &TC,
3361 const llvm::opt::ArgList &Args,
3362 bool &FRecordCommandLine,
3363 bool &GRecordCommandLine,
3364 bool &DXRecordCommandLine) {
3365 const Driver &D = TC.getDriver();
3366 const llvm::Triple &Triple = TC.getEffectiveTriple();
3367 const std::string &TripleStr = Triple.getTriple();
3368
3369 FRecordCommandLine =
3370 Args.hasFlag(Pos: options::OPT_frecord_command_line,
3371 Neg: options::OPT_fno_record_command_line, Default: false);
3372 GRecordCommandLine =
3373 Args.hasFlag(Pos: options::OPT_grecord_command_line,
3374 Neg: options::OPT_gno_record_command_line, Default: false);
3375 DXRecordCommandLine = Triple.isDXIL() && Args.hasArg(Ids: options::OPT_g_Flag);
3376 if (FRecordCommandLine && !Triple.isOSBinFormatELF() &&
3377 !Triple.isOSBinFormatXCOFF() && !Triple.isOSBinFormatMachO())
3378 D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target)
3379 << Args.getLastArg(Ids: options::OPT_frecord_command_line)->getAsString(Args)
3380 << TripleStr;
3381
3382 return FRecordCommandLine || TC.UseDwarfDebugFlags() || GRecordCommandLine ||
3383 DXRecordCommandLine;
3384}
3385
3386void tools::renderGlobalISelOptions(const Driver &D, const ArgList &Args,
3387 ArgStringList &CmdArgs,
3388 const llvm::Triple &Triple) {
3389 if (Arg *A = Args.getLastArg(Ids: options::OPT_fglobal_isel,
3390 Ids: options::OPT_fno_global_isel)) {
3391 CmdArgs.push_back(Elt: "-mllvm");
3392 if (A->getOption().matches(ID: options::OPT_fglobal_isel)) {
3393 CmdArgs.push_back(Elt: "-global-isel=1");
3394
3395 // GISel is on by default on AArch64 -O0, so don't bother adding
3396 // the fallback remarks for it. Other combinations will add a warning of
3397 // some kind.
3398 bool IsArchSupported = Triple.getArch() == llvm::Triple::aarch64;
3399 bool IsOptLevelSupported = false;
3400
3401 Arg *A = Args.getLastArg(Ids: options::OPT_O_Group);
3402 if (IsArchSupported) {
3403 if (!A || A->getOption().matches(ID: options::OPT_O0))
3404 IsOptLevelSupported = true;
3405 }
3406 if (!IsArchSupported || !IsOptLevelSupported) {
3407 CmdArgs.push_back(Elt: "-mllvm");
3408 CmdArgs.push_back(Elt: "-global-isel-abort=2");
3409
3410 if (!IsArchSupported)
3411 D.Diag(DiagID: diag::warn_drv_global_isel_incomplete) << Triple.getArchName();
3412 else
3413 D.Diag(DiagID: diag::warn_drv_global_isel_incomplete_opt);
3414 }
3415 } else {
3416 CmdArgs.push_back(Elt: "-global-isel=0");
3417 }
3418 }
3419}
3420
3421void tools::renderCommonIntegerOverflowOptions(const ArgList &Args,
3422 ArgStringList &CmdArgs,
3423 bool IsMSVCCompat) {
3424 bool use_fwrapv = IsMSVCCompat;
3425 bool use_fwrapv_pointer = false;
3426 for (const Arg *A : Args.filtered(
3427 Ids: options::OPT_fstrict_overflow, Ids: options::OPT_fno_strict_overflow,
3428 Ids: options::OPT_fwrapv, Ids: options::OPT_fno_wrapv,
3429 Ids: options::OPT_fwrapv_pointer, Ids: options::OPT_fno_wrapv_pointer)) {
3430 A->claim();
3431 switch (A->getOption().getID()) {
3432 case options::OPT_fstrict_overflow:
3433 use_fwrapv = false;
3434 use_fwrapv_pointer = false;
3435 break;
3436 case options::OPT_fno_strict_overflow:
3437 use_fwrapv = true;
3438 use_fwrapv_pointer = true;
3439 break;
3440 case options::OPT_fwrapv:
3441 use_fwrapv = true;
3442 break;
3443 case options::OPT_fno_wrapv:
3444 use_fwrapv = false;
3445 break;
3446 case options::OPT_fwrapv_pointer:
3447 use_fwrapv_pointer = true;
3448 break;
3449 case options::OPT_fno_wrapv_pointer:
3450 use_fwrapv_pointer = false;
3451 break;
3452 }
3453 }
3454
3455 if (use_fwrapv)
3456 CmdArgs.push_back(Elt: "-fwrapv");
3457 if (!use_fwrapv && IsMSVCCompat)
3458 CmdArgs.push_back(Elt: "-fno-wrapv");
3459 if (use_fwrapv_pointer)
3460 CmdArgs.push_back(Elt: "-fwrapv-pointer");
3461}
3462
3463/// Vectorize at all optimization levels greater than 1 except for -Oz.
3464/// For -Oz the loop vectorizer is disabled, while the slp vectorizer is
3465/// enabled.
3466bool tools::shouldEnableVectorizerAtOLevel(const ArgList &Args, bool isSlpVec) {
3467 if (Arg *A = Args.getLastArg(Ids: options::OPT_O_Group)) {
3468 if (A->getOption().matches(ID: options::OPT_O4) ||
3469 A->getOption().matches(ID: options::OPT_Ofast))
3470 return true;
3471
3472 if (A->getOption().matches(ID: options::OPT_O0))
3473 return false;
3474
3475 assert(A->getOption().matches(options::OPT_O) && "Must have a -O flag");
3476
3477 // Vectorize -Os.
3478 StringRef S(A->getValue());
3479 if (S == "s")
3480 return true;
3481
3482 // Don't vectorize -Oz, unless it's the slp vectorizer.
3483 if (S == "z")
3484 return isSlpVec;
3485
3486 unsigned OptLevel = 0;
3487 if (S.getAsInteger(Radix: 10, Result&: OptLevel))
3488 return false;
3489
3490 return OptLevel > 1;
3491 }
3492
3493 return false;
3494}
3495
3496void tools::handleVectorizeLoopsArgs(const ArgList &Args,
3497 ArgStringList &CmdArgs) {
3498 bool EnableVec = shouldEnableVectorizerAtOLevel(Args, isSlpVec: false);
3499 if (Args.hasFlag(Pos: options::OPT_fvectorize, Neg: options::OPT_fno_vectorize,
3500 Default: EnableVec))
3501 CmdArgs.push_back(Elt: "-vectorize-loops");
3502}
3503
3504void tools::handleVectorizeSLPArgs(const ArgList &Args,
3505 ArgStringList &CmdArgs) {
3506 bool EnableSLPVec = shouldEnableVectorizerAtOLevel(Args, isSlpVec: true);
3507 if (Args.hasFlag(Pos: options::OPT_fslp_vectorize, Neg: options::OPT_fno_slp_vectorize,
3508 Default: EnableSLPVec))
3509 CmdArgs.push_back(Elt: "-vectorize-slp");
3510}
3511
3512void tools::handleInterchangeLoopsArgs(const ArgList &Args,
3513 ArgStringList &CmdArgs) {
3514 if (Args.hasFlag(Pos: options::OPT_floop_interchange,
3515 Neg: options::OPT_fno_loop_interchange, Default: false))
3516 CmdArgs.push_back(Elt: "-floop-interchange");
3517}
3518
3519std::string tools::complexRangeKindToStr(LangOptions::ComplexRangeKind Range) {
3520 switch (Range) {
3521 case LangOptions::ComplexRangeKind::CX_Full:
3522 return "full";
3523 break;
3524 case LangOptions::ComplexRangeKind::CX_Basic:
3525 return "basic";
3526 break;
3527 case LangOptions::ComplexRangeKind::CX_Improved:
3528 return "improved";
3529 break;
3530 case LangOptions::ComplexRangeKind::CX_Promoted:
3531 return "promoted";
3532 break;
3533 case LangOptions::ComplexRangeKind::CX_None:
3534 return "none";
3535 break;
3536 }
3537 llvm_unreachable("Fully covered switch above");
3538}
3539
3540std::string
3541tools::renderComplexRangeOption(LangOptionsBase::ComplexRangeKind Range) {
3542 std::string ComplexRangeStr = complexRangeKindToStr(Range);
3543 if (!ComplexRangeStr.empty())
3544 return "-complex-range=" + ComplexRangeStr;
3545 return ComplexRangeStr;
3546}
3547
3548static void emitComplexRangeDiag(const Driver &D, StringRef LastOpt,
3549 LangOptions::ComplexRangeKind Range,
3550 StringRef NewOpt,
3551 LangOptions::ComplexRangeKind NewRange) {
3552 // Do not emit a warning if NewOpt overrides LastOpt in the following cases.
3553 //
3554 // | LastOpt | NewOpt |
3555 // |-----------------------|-----------------------|
3556 // | -fcx-limited-range | -fno-cx-limited-range |
3557 // | -fno-cx-limited-range | -fcx-limited-range |
3558 // | -fcx-fortran-rules | -fno-cx-fortran-rules |
3559 // | -fno-cx-fortran-rules | -fcx-fortran-rules |
3560 // | -ffast-math | -fno-fast-math |
3561 // | -ffp-model= | -ffast-math |
3562 // | -ffp-model= | -fno-fast-math |
3563 // | -ffp-model= | -ffp-model= |
3564 // | -fcomplex-arithmetic= | -fcomplex-arithmetic= |
3565 if (LastOpt == NewOpt || NewOpt.empty() || LastOpt.empty() ||
3566 (LastOpt == "-fcx-limited-range" && NewOpt == "-fno-cx-limited-range") ||
3567 (LastOpt == "-fno-cx-limited-range" && NewOpt == "-fcx-limited-range") ||
3568 (LastOpt == "-fcx-fortran-rules" && NewOpt == "-fno-cx-fortran-rules") ||
3569 (LastOpt == "-fno-cx-fortran-rules" && NewOpt == "-fcx-fortran-rules") ||
3570 (LastOpt == "-ffast-math" && NewOpt == "-fno-fast-math") ||
3571 (LastOpt.starts_with(Prefix: "-ffp-model=") && NewOpt == "-ffast-math") ||
3572 (LastOpt.starts_with(Prefix: "-ffp-model=") && NewOpt == "-fno-fast-math") ||
3573 (LastOpt.starts_with(Prefix: "-ffp-model=") &&
3574 NewOpt.starts_with(Prefix: "-ffp-model=")) ||
3575 (LastOpt.starts_with(Prefix: "-fcomplex-arithmetic=") &&
3576 NewOpt.starts_with(Prefix: "-fcomplex-arithmetic=")))
3577 return;
3578
3579 D.Diag(DiagID: clang::diag::warn_drv_overriding_complex_range)
3580 << LastOpt << NewOpt << complexRangeKindToStr(Range)
3581 << complexRangeKindToStr(Range: NewRange);
3582}
3583
3584void tools::setComplexRange(const Driver &D, StringRef NewOpt,
3585 LangOptions::ComplexRangeKind NewRange,
3586 StringRef &LastOpt,
3587 LangOptions::ComplexRangeKind &Range) {
3588 // Warn if user overrides the previously set complex number
3589 // multiplication/division option.
3590 if (Range != LangOptions::ComplexRangeKind::CX_None && Range != NewRange)
3591 emitComplexRangeDiag(D, LastOpt, Range, NewOpt, NewRange);
3592 LastOpt = NewOpt;
3593 Range = NewRange;
3594}
3595
3596void tools::constructLLVMLinkCommand(Compilation &C, const Tool &T,
3597 const JobAction &JA,
3598 const InputInfoList &JobInputs,
3599 const ArgStringList &LinkerInputs,
3600 const InputInfo &Output,
3601 const llvm::opt::ArgList &Args,
3602 const char *OutputFilename) {
3603 // Construct llvm-link command.
3604 // The output from llvm-link is a bitcode file.
3605
3606 assert(!LinkerInputs.empty() && !JobInputs.empty() &&
3607 "Must have at least one input.");
3608
3609 ArgStringList LlvmLinkArgs(
3610 {"-o", OutputFilename ? OutputFilename : Output.getFilename()});
3611
3612 LlvmLinkArgs.append(RHS: LinkerInputs);
3613
3614 const ToolChain &TC = T.getToolChain();
3615 const char *LlvmLink = Args.MakeArgString(Str: TC.GetProgramPath(Name: "llvm-link"));
3616 C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: T, args: ResponseFileSupport::None(),
3617 args&: LlvmLink, args&: LlvmLinkArgs, args: JobInputs,
3618 args: Output));
3619}
3620