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