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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang/lib/Driver/ToolChains/CommonArgs.cpp