/src/llvm-project/clang/lib/Driver/ToolChains/CommonArgs.cpp
Line | Count | Source (jump to first uncovered line) |
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 "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/CharInfo.h" |
27 | | #include "clang/Basic/CodeGenOptions.h" |
28 | | #include "clang/Basic/LangOptions.h" |
29 | | #include "clang/Basic/ObjCRuntime.h" |
30 | | #include "clang/Basic/Version.h" |
31 | | #include "clang/Config/config.h" |
32 | | #include "clang/Driver/Action.h" |
33 | | #include "clang/Driver/Compilation.h" |
34 | | #include "clang/Driver/Driver.h" |
35 | | #include "clang/Driver/DriverDiagnostic.h" |
36 | | #include "clang/Driver/InputInfo.h" |
37 | | #include "clang/Driver/Job.h" |
38 | | #include "clang/Driver/Options.h" |
39 | | #include "clang/Driver/SanitizerArgs.h" |
40 | | #include "clang/Driver/ToolChain.h" |
41 | | #include "clang/Driver/Util.h" |
42 | | #include "clang/Driver/XRayArgs.h" |
43 | | #include "llvm/ADT/STLExtras.h" |
44 | | #include "llvm/ADT/SmallSet.h" |
45 | | #include "llvm/ADT/SmallString.h" |
46 | | #include "llvm/ADT/StringExtras.h" |
47 | | #include "llvm/ADT/StringSwitch.h" |
48 | | #include "llvm/ADT/Twine.h" |
49 | | #include "llvm/BinaryFormat/Magic.h" |
50 | | #include "llvm/Config/llvm-config.h" |
51 | | #include "llvm/Option/Arg.h" |
52 | | #include "llvm/Option/ArgList.h" |
53 | | #include "llvm/Option/Option.h" |
54 | | #include "llvm/Support/CodeGen.h" |
55 | | #include "llvm/Support/Compression.h" |
56 | | #include "llvm/Support/Debug.h" |
57 | | #include "llvm/Support/ErrorHandling.h" |
58 | | #include "llvm/Support/FileSystem.h" |
59 | | #include "llvm/Support/Path.h" |
60 | | #include "llvm/Support/Process.h" |
61 | | #include "llvm/Support/Program.h" |
62 | | #include "llvm/Support/ScopedPrinter.h" |
63 | | #include "llvm/Support/Threading.h" |
64 | | #include "llvm/Support/VirtualFileSystem.h" |
65 | | #include "llvm/Support/YAMLParser.h" |
66 | | #include "llvm/TargetParser/Host.h" |
67 | | #include "llvm/TargetParser/TargetParser.h" |
68 | | #include <optional> |
69 | | |
70 | | using namespace clang::driver; |
71 | | using namespace clang::driver::tools; |
72 | | using namespace clang; |
73 | | using namespace llvm::opt; |
74 | | |
75 | | static bool useFramePointerForTargetByDefault(const llvm::opt::ArgList &Args, |
76 | 0 | const llvm::Triple &Triple) { |
77 | 0 | if (Args.hasArg(clang::driver::options::OPT_pg) && |
78 | 0 | !Args.hasArg(clang::driver::options::OPT_mfentry)) |
79 | 0 | return true; |
80 | | |
81 | 0 | if (Triple.isAndroid()) { |
82 | 0 | switch (Triple.getArch()) { |
83 | 0 | case llvm::Triple::aarch64: |
84 | 0 | case llvm::Triple::arm: |
85 | 0 | case llvm::Triple::armeb: |
86 | 0 | case llvm::Triple::thumb: |
87 | 0 | case llvm::Triple::thumbeb: |
88 | 0 | case llvm::Triple::riscv64: |
89 | 0 | return true; |
90 | 0 | default: |
91 | 0 | break; |
92 | 0 | } |
93 | 0 | } |
94 | | |
95 | 0 | switch (Triple.getArch()) { |
96 | 0 | case llvm::Triple::xcore: |
97 | 0 | case llvm::Triple::wasm32: |
98 | 0 | case llvm::Triple::wasm64: |
99 | 0 | case llvm::Triple::msp430: |
100 | | // XCore never wants frame pointers, regardless of OS. |
101 | | // WebAssembly never wants frame pointers. |
102 | 0 | return false; |
103 | 0 | case llvm::Triple::ppc: |
104 | 0 | case llvm::Triple::ppcle: |
105 | 0 | case llvm::Triple::ppc64: |
106 | 0 | case llvm::Triple::ppc64le: |
107 | 0 | case llvm::Triple::riscv32: |
108 | 0 | case llvm::Triple::riscv64: |
109 | 0 | case llvm::Triple::sparc: |
110 | 0 | case llvm::Triple::sparcel: |
111 | 0 | case llvm::Triple::sparcv9: |
112 | 0 | case llvm::Triple::amdgcn: |
113 | 0 | case llvm::Triple::r600: |
114 | 0 | case llvm::Triple::csky: |
115 | 0 | case llvm::Triple::loongarch32: |
116 | 0 | case llvm::Triple::loongarch64: |
117 | 0 | return !clang::driver::tools::areOptimizationsEnabled(Args); |
118 | 0 | default: |
119 | 0 | break; |
120 | 0 | } |
121 | | |
122 | 0 | if (Triple.isOSFuchsia() || Triple.isOSNetBSD()) { |
123 | 0 | return !clang::driver::tools::areOptimizationsEnabled(Args); |
124 | 0 | } |
125 | | |
126 | 0 | if (Triple.isOSLinux() || Triple.isOSHurd()) { |
127 | 0 | switch (Triple.getArch()) { |
128 | | // Don't use a frame pointer on linux if optimizing for certain targets. |
129 | 0 | case llvm::Triple::arm: |
130 | 0 | case llvm::Triple::armeb: |
131 | 0 | case llvm::Triple::thumb: |
132 | 0 | case llvm::Triple::thumbeb: |
133 | 0 | case llvm::Triple::mips64: |
134 | 0 | case llvm::Triple::mips64el: |
135 | 0 | case llvm::Triple::mips: |
136 | 0 | case llvm::Triple::mipsel: |
137 | 0 | case llvm::Triple::systemz: |
138 | 0 | case llvm::Triple::x86: |
139 | 0 | case llvm::Triple::x86_64: |
140 | 0 | return !clang::driver::tools::areOptimizationsEnabled(Args); |
141 | 0 | default: |
142 | 0 | return true; |
143 | 0 | } |
144 | 0 | } |
145 | | |
146 | 0 | if (Triple.isOSWindows()) { |
147 | 0 | switch (Triple.getArch()) { |
148 | 0 | case llvm::Triple::x86: |
149 | 0 | return !clang::driver::tools::areOptimizationsEnabled(Args); |
150 | 0 | case llvm::Triple::x86_64: |
151 | 0 | return Triple.isOSBinFormatMachO(); |
152 | 0 | case llvm::Triple::arm: |
153 | 0 | case llvm::Triple::thumb: |
154 | | // Windows on ARM builds with FPO disabled to aid fast stack walking |
155 | 0 | return true; |
156 | 0 | default: |
157 | | // All other supported Windows ISAs use xdata unwind information, so frame |
158 | | // pointers are not generally useful. |
159 | 0 | return false; |
160 | 0 | } |
161 | 0 | } |
162 | | |
163 | 0 | return true; |
164 | 0 | } |
165 | | |
166 | 0 | static bool mustUseNonLeafFramePointerForTarget(const llvm::Triple &Triple) { |
167 | 0 | switch (Triple.getArch()) { |
168 | 0 | default: |
169 | 0 | return false; |
170 | 0 | case llvm::Triple::arm: |
171 | 0 | case llvm::Triple::thumb: |
172 | | // ARM Darwin targets require a frame pointer to be always present to aid |
173 | | // offline debugging via backtraces. |
174 | 0 | return Triple.isOSDarwin(); |
175 | 0 | } |
176 | 0 | } |
177 | | |
178 | | clang::CodeGenOptions::FramePointerKind |
179 | | getFramePointerKind(const llvm::opt::ArgList &Args, |
180 | 0 | const llvm::Triple &Triple) { |
181 | | // We have 4 states: |
182 | | // |
183 | | // 00) leaf retained, non-leaf retained |
184 | | // 01) leaf retained, non-leaf omitted (this is invalid) |
185 | | // 10) leaf omitted, non-leaf retained |
186 | | // (what -momit-leaf-frame-pointer was designed for) |
187 | | // 11) leaf omitted, non-leaf omitted |
188 | | // |
189 | | // "omit" options taking precedence over "no-omit" options is the only way |
190 | | // to make 3 valid states representable |
191 | 0 | llvm::opt::Arg *A = |
192 | 0 | Args.getLastArg(clang::driver::options::OPT_fomit_frame_pointer, |
193 | 0 | clang::driver::options::OPT_fno_omit_frame_pointer); |
194 | |
|
195 | 0 | bool OmitFP = A && A->getOption().matches( |
196 | 0 | clang::driver::options::OPT_fomit_frame_pointer); |
197 | 0 | bool NoOmitFP = A && A->getOption().matches( |
198 | 0 | clang::driver::options::OPT_fno_omit_frame_pointer); |
199 | 0 | bool OmitLeafFP = |
200 | 0 | Args.hasFlag(clang::driver::options::OPT_momit_leaf_frame_pointer, |
201 | 0 | clang::driver::options::OPT_mno_omit_leaf_frame_pointer, |
202 | 0 | Triple.isAArch64() || Triple.isPS() || Triple.isVE() || |
203 | 0 | (Triple.isAndroid() && Triple.isRISCV64())); |
204 | 0 | if (NoOmitFP || mustUseNonLeafFramePointerForTarget(Triple) || |
205 | 0 | (!OmitFP && useFramePointerForTargetByDefault(Args, Triple))) { |
206 | 0 | if (OmitLeafFP) |
207 | 0 | return clang::CodeGenOptions::FramePointerKind::NonLeaf; |
208 | 0 | return clang::CodeGenOptions::FramePointerKind::All; |
209 | 0 | } |
210 | 0 | return clang::CodeGenOptions::FramePointerKind::None; |
211 | 0 | } |
212 | | |
213 | | static void renderRpassOptions(const ArgList &Args, ArgStringList &CmdArgs, |
214 | 0 | const StringRef PluginOptPrefix) { |
215 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_Rpass_EQ)) |
216 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
217 | 0 | "-pass-remarks=" + A->getValue())); |
218 | |
|
219 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_Rpass_missed_EQ)) |
220 | 0 | CmdArgs.push_back(Args.MakeArgString( |
221 | 0 | Twine(PluginOptPrefix) + "-pass-remarks-missed=" + A->getValue())); |
222 | |
|
223 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_Rpass_analysis_EQ)) |
224 | 0 | CmdArgs.push_back(Args.MakeArgString( |
225 | 0 | Twine(PluginOptPrefix) + "-pass-remarks-analysis=" + A->getValue())); |
226 | 0 | } |
227 | | |
228 | | static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, |
229 | | const llvm::Triple &Triple, |
230 | | const InputInfo &Input, |
231 | | const InputInfo &Output, |
232 | 0 | const StringRef PluginOptPrefix) { |
233 | 0 | StringRef Format = "yaml"; |
234 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ)) |
235 | 0 | Format = A->getValue(); |
236 | |
|
237 | 0 | SmallString<128> F; |
238 | 0 | const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ); |
239 | 0 | if (A) |
240 | 0 | F = A->getValue(); |
241 | 0 | else if (Output.isFilename()) |
242 | 0 | F = Output.getFilename(); |
243 | |
|
244 | 0 | assert(!F.empty() && "Cannot determine remarks output name."); |
245 | | // Append "opt.ld.<format>" to the end of the file name. |
246 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
247 | 0 | "opt-remarks-filename=" + F + |
248 | 0 | ".opt.ld." + Format)); |
249 | |
|
250 | 0 | if (const Arg *A = |
251 | 0 | Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) |
252 | 0 | CmdArgs.push_back(Args.MakeArgString( |
253 | 0 | Twine(PluginOptPrefix) + "opt-remarks-passes=" + A->getValue())); |
254 | |
|
255 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
256 | 0 | "opt-remarks-format=" + Format.data())); |
257 | 0 | } |
258 | | |
259 | | static void renderRemarksHotnessOptions(const ArgList &Args, |
260 | | ArgStringList &CmdArgs, |
261 | 0 | const StringRef PluginOptPrefix) { |
262 | 0 | if (Args.hasFlag(options::OPT_fdiagnostics_show_hotness, |
263 | 0 | options::OPT_fno_diagnostics_show_hotness, false)) |
264 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
265 | 0 | "opt-remarks-with-hotness")); |
266 | |
|
267 | 0 | if (const Arg *A = |
268 | 0 | Args.getLastArg(options::OPT_fdiagnostics_hotness_threshold_EQ)) |
269 | 0 | CmdArgs.push_back( |
270 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + |
271 | 0 | "opt-remarks-hotness-threshold=" + A->getValue())); |
272 | 0 | } |
273 | | |
274 | | static bool shouldIgnoreUnsupportedTargetFeature(const Arg &TargetFeatureArg, |
275 | | llvm::Triple T, |
276 | 0 | StringRef Processor) { |
277 | | // Warn no-cumode for AMDGCN processors not supporing WGP mode. |
278 | 0 | if (!T.isAMDGPU()) |
279 | 0 | return false; |
280 | 0 | auto GPUKind = T.isAMDGCN() ? llvm::AMDGPU::parseArchAMDGCN(Processor) |
281 | 0 | : llvm::AMDGPU::parseArchR600(Processor); |
282 | 0 | auto GPUFeatures = T.isAMDGCN() ? llvm::AMDGPU::getArchAttrAMDGCN(GPUKind) |
283 | 0 | : llvm::AMDGPU::getArchAttrR600(GPUKind); |
284 | 0 | if (GPUFeatures & llvm::AMDGPU::FEATURE_WGP) |
285 | 0 | return false; |
286 | 0 | return TargetFeatureArg.getOption().matches(options::OPT_mno_cumode); |
287 | 0 | } |
288 | | |
289 | | void tools::addPathIfExists(const Driver &D, const Twine &Path, |
290 | 0 | ToolChain::path_list &Paths) { |
291 | 0 | if (D.getVFS().exists(Path)) |
292 | 0 | Paths.push_back(Path.str()); |
293 | 0 | } |
294 | | |
295 | | void tools::handleTargetFeaturesGroup(const Driver &D, |
296 | | const llvm::Triple &Triple, |
297 | | const ArgList &Args, |
298 | | std::vector<StringRef> &Features, |
299 | 0 | OptSpecifier Group) { |
300 | 0 | std::set<StringRef> Warned; |
301 | 0 | for (const Arg *A : Args.filtered(Group)) { |
302 | 0 | StringRef Name = A->getOption().getName(); |
303 | 0 | A->claim(); |
304 | | |
305 | | // Skip over "-m". |
306 | 0 | assert(Name.starts_with("m") && "Invalid feature name."); |
307 | 0 | Name = Name.substr(1); |
308 | |
|
309 | 0 | auto Proc = getCPUName(D, Args, Triple); |
310 | 0 | if (shouldIgnoreUnsupportedTargetFeature(*A, Triple, Proc)) { |
311 | 0 | if (Warned.count(Name) == 0) { |
312 | 0 | D.getDiags().Report( |
313 | 0 | clang::diag::warn_drv_unsupported_option_for_processor) |
314 | 0 | << A->getAsString(Args) << Proc; |
315 | 0 | Warned.insert(Name); |
316 | 0 | } |
317 | 0 | continue; |
318 | 0 | } |
319 | | |
320 | 0 | bool IsNegative = Name.starts_with("no-"); |
321 | 0 | if (IsNegative) |
322 | 0 | Name = Name.substr(3); |
323 | |
|
324 | 0 | Features.push_back(Args.MakeArgString((IsNegative ? "-" : "+") + Name)); |
325 | 0 | } |
326 | 0 | } |
327 | | |
328 | | SmallVector<StringRef> |
329 | 0 | tools::unifyTargetFeatures(ArrayRef<StringRef> Features) { |
330 | | // Only add a feature if it hasn't been seen before starting from the end. |
331 | 0 | SmallVector<StringRef> UnifiedFeatures; |
332 | 0 | llvm::DenseSet<StringRef> UsedFeatures; |
333 | 0 | for (StringRef Feature : llvm::reverse(Features)) { |
334 | 0 | if (UsedFeatures.insert(Feature.drop_front()).second) |
335 | 0 | UnifiedFeatures.insert(UnifiedFeatures.begin(), Feature); |
336 | 0 | } |
337 | |
|
338 | 0 | return UnifiedFeatures; |
339 | 0 | } |
340 | | |
341 | | void tools::addDirectoryList(const ArgList &Args, ArgStringList &CmdArgs, |
342 | 0 | const char *ArgName, const char *EnvVar) { |
343 | 0 | const char *DirList = ::getenv(EnvVar); |
344 | 0 | bool CombinedArg = false; |
345 | |
|
346 | 0 | if (!DirList) |
347 | 0 | return; // Nothing to do. |
348 | | |
349 | 0 | StringRef Name(ArgName); |
350 | 0 | if (Name.equals("-I") || Name.equals("-L") || Name.empty()) |
351 | 0 | CombinedArg = true; |
352 | |
|
353 | 0 | StringRef Dirs(DirList); |
354 | 0 | if (Dirs.empty()) // Empty string should not add '.'. |
355 | 0 | return; |
356 | | |
357 | 0 | StringRef::size_type Delim; |
358 | 0 | while ((Delim = Dirs.find(llvm::sys::EnvPathSeparator)) != StringRef::npos) { |
359 | 0 | if (Delim == 0) { // Leading colon. |
360 | 0 | if (CombinedArg) { |
361 | 0 | CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); |
362 | 0 | } else { |
363 | 0 | CmdArgs.push_back(ArgName); |
364 | 0 | CmdArgs.push_back("."); |
365 | 0 | } |
366 | 0 | } else { |
367 | 0 | if (CombinedArg) { |
368 | 0 | CmdArgs.push_back( |
369 | 0 | Args.MakeArgString(std::string(ArgName) + Dirs.substr(0, Delim))); |
370 | 0 | } else { |
371 | 0 | CmdArgs.push_back(ArgName); |
372 | 0 | CmdArgs.push_back(Args.MakeArgString(Dirs.substr(0, Delim))); |
373 | 0 | } |
374 | 0 | } |
375 | 0 | Dirs = Dirs.substr(Delim + 1); |
376 | 0 | } |
377 | |
|
378 | 0 | if (Dirs.empty()) { // Trailing colon. |
379 | 0 | if (CombinedArg) { |
380 | 0 | CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + ".")); |
381 | 0 | } else { |
382 | 0 | CmdArgs.push_back(ArgName); |
383 | 0 | CmdArgs.push_back("."); |
384 | 0 | } |
385 | 0 | } else { // Add the last path. |
386 | 0 | if (CombinedArg) { |
387 | 0 | CmdArgs.push_back(Args.MakeArgString(std::string(ArgName) + Dirs)); |
388 | 0 | } else { |
389 | 0 | CmdArgs.push_back(ArgName); |
390 | 0 | CmdArgs.push_back(Args.MakeArgString(Dirs)); |
391 | 0 | } |
392 | 0 | } |
393 | 0 | } |
394 | | |
395 | | void tools::AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, |
396 | | const ArgList &Args, ArgStringList &CmdArgs, |
397 | 0 | const JobAction &JA) { |
398 | 0 | const Driver &D = TC.getDriver(); |
399 | | |
400 | | // Add extra linker input arguments which are not treated as inputs |
401 | | // (constructed via -Xarch_). |
402 | 0 | Args.AddAllArgValues(CmdArgs, options::OPT_Zlinker_input); |
403 | | |
404 | | // LIBRARY_PATH are included before user inputs and only supported on native |
405 | | // toolchains. |
406 | 0 | if (!TC.isCrossCompiling()) |
407 | 0 | addDirectoryList(Args, CmdArgs, "-L", "LIBRARY_PATH"); |
408 | |
|
409 | 0 | for (const auto &II : Inputs) { |
410 | | // If the current tool chain refers to an OpenMP offloading host, we |
411 | | // should ignore inputs that refer to OpenMP offloading devices - |
412 | | // they will be embedded according to a proper linker script. |
413 | 0 | if (auto *IA = II.getAction()) |
414 | 0 | if ((JA.isHostOffloading(Action::OFK_OpenMP) && |
415 | 0 | IA->isDeviceOffloading(Action::OFK_OpenMP))) |
416 | 0 | continue; |
417 | | |
418 | 0 | if (!TC.HasNativeLLVMSupport() && types::isLLVMIR(II.getType())) |
419 | | // Don't try to pass LLVM inputs unless we have native support. |
420 | 0 | D.Diag(diag::err_drv_no_linker_llvm_support) << TC.getTripleString(); |
421 | | |
422 | | // Add filenames immediately. |
423 | 0 | if (II.isFilename()) { |
424 | 0 | CmdArgs.push_back(II.getFilename()); |
425 | 0 | continue; |
426 | 0 | } |
427 | | |
428 | | // In some error cases, the input could be Nothing; skip those. |
429 | 0 | if (II.isNothing()) |
430 | 0 | continue; |
431 | | |
432 | | // Otherwise, this is a linker input argument. |
433 | 0 | const Arg &A = II.getInputArg(); |
434 | | |
435 | | // Handle reserved library options. |
436 | 0 | if (A.getOption().matches(options::OPT_Z_reserved_lib_stdcxx)) |
437 | 0 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
438 | 0 | else if (A.getOption().matches(options::OPT_Z_reserved_lib_cckext)) |
439 | 0 | TC.AddCCKextLibArgs(Args, CmdArgs); |
440 | 0 | else |
441 | 0 | A.renderAsInput(Args, CmdArgs); |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | | void tools::addLinkerCompressDebugSectionsOption( |
446 | | const ToolChain &TC, const llvm::opt::ArgList &Args, |
447 | 0 | llvm::opt::ArgStringList &CmdArgs) { |
448 | | // GNU ld supports --compress-debug-sections=none|zlib|zlib-gnu|zlib-gabi |
449 | | // whereas zlib is an alias to zlib-gabi and zlib-gnu is obsoleted. Therefore |
450 | | // -gz=none|zlib are translated to --compress-debug-sections=none|zlib. -gz |
451 | | // is not translated since ld --compress-debug-sections option requires an |
452 | | // argument. |
453 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_gz_EQ)) { |
454 | 0 | StringRef V = A->getValue(); |
455 | 0 | if (V == "none" || V == "zlib" || V == "zstd") |
456 | 0 | CmdArgs.push_back(Args.MakeArgString("--compress-debug-sections=" + V)); |
457 | 0 | else |
458 | 0 | TC.getDriver().Diag(diag::err_drv_unsupported_option_argument) |
459 | 0 | << A->getSpelling() << V; |
460 | 0 | } |
461 | 0 | } |
462 | | |
463 | | void tools::AddTargetFeature(const ArgList &Args, |
464 | | std::vector<StringRef> &Features, |
465 | | OptSpecifier OnOpt, OptSpecifier OffOpt, |
466 | 0 | StringRef FeatureName) { |
467 | 0 | if (Arg *A = Args.getLastArg(OnOpt, OffOpt)) { |
468 | 0 | if (A->getOption().matches(OnOpt)) |
469 | 0 | Features.push_back(Args.MakeArgString("+" + FeatureName)); |
470 | 0 | else |
471 | 0 | Features.push_back(Args.MakeArgString("-" + FeatureName)); |
472 | 0 | } |
473 | 0 | } |
474 | | |
475 | | /// Get the (LLVM) name of the AMDGPU gpu we are targeting. |
476 | | static std::string getAMDGPUTargetGPU(const llvm::Triple &T, |
477 | 0 | const ArgList &Args) { |
478 | 0 | Arg *MArch = Args.getLastArg(options::OPT_march_EQ); |
479 | 0 | if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { |
480 | 0 | auto GPUName = getProcessorFromTargetID(T, A->getValue()); |
481 | 0 | return llvm::StringSwitch<std::string>(GPUName) |
482 | 0 | .Cases("rv630", "rv635", "r600") |
483 | 0 | .Cases("rv610", "rv620", "rs780", "rs880") |
484 | 0 | .Case("rv740", "rv770") |
485 | 0 | .Case("palm", "cedar") |
486 | 0 | .Cases("sumo", "sumo2", "sumo") |
487 | 0 | .Case("hemlock", "cypress") |
488 | 0 | .Case("aruba", "cayman") |
489 | 0 | .Default(GPUName.str()); |
490 | 0 | } |
491 | 0 | if (MArch) |
492 | 0 | return getProcessorFromTargetID(T, MArch->getValue()).str(); |
493 | 0 | return ""; |
494 | 0 | } |
495 | | |
496 | 0 | static std::string getLanaiTargetCPU(const ArgList &Args) { |
497 | 0 | if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { |
498 | 0 | return A->getValue(); |
499 | 0 | } |
500 | 0 | return ""; |
501 | 0 | } |
502 | | |
503 | | /// Get the (LLVM) name of the WebAssembly cpu we are targeting. |
504 | 0 | static StringRef getWebAssemblyTargetCPU(const ArgList &Args) { |
505 | | // If we have -mcpu=, use that. |
506 | 0 | if (Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) { |
507 | 0 | StringRef CPU = A->getValue(); |
508 | |
|
509 | | #ifdef __wasm__ |
510 | | // Handle "native" by examining the host. "native" isn't meaningful when |
511 | | // cross compiling, so only support this when the host is also WebAssembly. |
512 | | if (CPU == "native") |
513 | | return llvm::sys::getHostCPUName(); |
514 | | #endif |
515 | |
|
516 | 0 | return CPU; |
517 | 0 | } |
518 | | |
519 | 0 | return "generic"; |
520 | 0 | } |
521 | | |
522 | | std::string tools::getCPUName(const Driver &D, const ArgList &Args, |
523 | 0 | const llvm::Triple &T, bool FromAs) { |
524 | 0 | Arg *A; |
525 | |
|
526 | 0 | switch (T.getArch()) { |
527 | 0 | default: |
528 | 0 | return ""; |
529 | | |
530 | 0 | case llvm::Triple::aarch64: |
531 | 0 | case llvm::Triple::aarch64_32: |
532 | 0 | case llvm::Triple::aarch64_be: |
533 | 0 | return aarch64::getAArch64TargetCPU(Args, T, A); |
534 | | |
535 | 0 | case llvm::Triple::arm: |
536 | 0 | case llvm::Triple::armeb: |
537 | 0 | case llvm::Triple::thumb: |
538 | 0 | case llvm::Triple::thumbeb: { |
539 | 0 | StringRef MArch, MCPU; |
540 | 0 | arm::getARMArchCPUFromArgs(Args, MArch, MCPU, FromAs); |
541 | 0 | return arm::getARMTargetCPU(MCPU, MArch, T); |
542 | 0 | } |
543 | | |
544 | 0 | case llvm::Triple::avr: |
545 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_mmcu_EQ)) |
546 | 0 | return A->getValue(); |
547 | 0 | return ""; |
548 | | |
549 | 0 | case llvm::Triple::m68k: |
550 | 0 | return m68k::getM68kTargetCPU(Args); |
551 | | |
552 | 0 | case llvm::Triple::mips: |
553 | 0 | case llvm::Triple::mipsel: |
554 | 0 | case llvm::Triple::mips64: |
555 | 0 | case llvm::Triple::mips64el: { |
556 | 0 | StringRef CPUName; |
557 | 0 | StringRef ABIName; |
558 | 0 | mips::getMipsCPUAndABI(Args, T, CPUName, ABIName); |
559 | 0 | return std::string(CPUName); |
560 | 0 | } |
561 | | |
562 | 0 | case llvm::Triple::nvptx: |
563 | 0 | case llvm::Triple::nvptx64: |
564 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) |
565 | 0 | return A->getValue(); |
566 | 0 | return ""; |
567 | | |
568 | 0 | case llvm::Triple::ppc: |
569 | 0 | case llvm::Triple::ppcle: |
570 | 0 | case llvm::Triple::ppc64: |
571 | 0 | case llvm::Triple::ppc64le: |
572 | 0 | return ppc::getPPCTargetCPU(D, Args, T); |
573 | | |
574 | 0 | case llvm::Triple::csky: |
575 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) |
576 | 0 | return A->getValue(); |
577 | 0 | else if (const Arg *A = Args.getLastArg(options::OPT_march_EQ)) |
578 | 0 | return A->getValue(); |
579 | 0 | else |
580 | 0 | return "ck810"; |
581 | 0 | case llvm::Triple::riscv32: |
582 | 0 | case llvm::Triple::riscv64: |
583 | 0 | return riscv::getRISCVTargetCPU(Args, T); |
584 | | |
585 | 0 | case llvm::Triple::bpfel: |
586 | 0 | case llvm::Triple::bpfeb: |
587 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_mcpu_EQ)) |
588 | 0 | return A->getValue(); |
589 | 0 | return ""; |
590 | | |
591 | 0 | case llvm::Triple::sparc: |
592 | 0 | case llvm::Triple::sparcel: |
593 | 0 | case llvm::Triple::sparcv9: |
594 | 0 | return sparc::getSparcTargetCPU(D, Args, T); |
595 | | |
596 | 0 | case llvm::Triple::x86: |
597 | 0 | case llvm::Triple::x86_64: |
598 | 0 | return x86::getX86TargetCPU(D, Args, T); |
599 | | |
600 | 0 | case llvm::Triple::hexagon: |
601 | 0 | return "hexagon" + |
602 | 0 | toolchains::HexagonToolChain::GetTargetCPUVersion(Args).str(); |
603 | | |
604 | 0 | case llvm::Triple::lanai: |
605 | 0 | return getLanaiTargetCPU(Args); |
606 | | |
607 | 0 | case llvm::Triple::systemz: |
608 | 0 | return systemz::getSystemZTargetCPU(Args); |
609 | | |
610 | 0 | case llvm::Triple::r600: |
611 | 0 | case llvm::Triple::amdgcn: |
612 | 0 | return getAMDGPUTargetGPU(T, Args); |
613 | | |
614 | 0 | case llvm::Triple::wasm32: |
615 | 0 | case llvm::Triple::wasm64: |
616 | 0 | return std::string(getWebAssemblyTargetCPU(Args)); |
617 | | |
618 | 0 | case llvm::Triple::loongarch32: |
619 | 0 | case llvm::Triple::loongarch64: |
620 | 0 | return loongarch::getLoongArchTargetCPU(Args, T); |
621 | 0 | } |
622 | 0 | } |
623 | | |
624 | | static void getWebAssemblyTargetFeatures(const Driver &D, |
625 | | const llvm::Triple &Triple, |
626 | | const ArgList &Args, |
627 | 0 | std::vector<StringRef> &Features) { |
628 | 0 | handleTargetFeaturesGroup(D, Triple, Args, Features, |
629 | 0 | options::OPT_m_wasm_Features_Group); |
630 | 0 | } |
631 | | |
632 | | void tools::getTargetFeatures(const Driver &D, const llvm::Triple &Triple, |
633 | | const ArgList &Args, ArgStringList &CmdArgs, |
634 | 0 | bool ForAS, bool IsAux) { |
635 | 0 | std::vector<StringRef> Features; |
636 | 0 | switch (Triple.getArch()) { |
637 | 0 | default: |
638 | 0 | break; |
639 | 0 | case llvm::Triple::mips: |
640 | 0 | case llvm::Triple::mipsel: |
641 | 0 | case llvm::Triple::mips64: |
642 | 0 | case llvm::Triple::mips64el: |
643 | 0 | mips::getMIPSTargetFeatures(D, Triple, Args, Features); |
644 | 0 | break; |
645 | 0 | case llvm::Triple::arm: |
646 | 0 | case llvm::Triple::armeb: |
647 | 0 | case llvm::Triple::thumb: |
648 | 0 | case llvm::Triple::thumbeb: |
649 | 0 | arm::getARMTargetFeatures(D, Triple, Args, Features, ForAS); |
650 | 0 | break; |
651 | 0 | case llvm::Triple::ppc: |
652 | 0 | case llvm::Triple::ppcle: |
653 | 0 | case llvm::Triple::ppc64: |
654 | 0 | case llvm::Triple::ppc64le: |
655 | 0 | ppc::getPPCTargetFeatures(D, Triple, Args, Features); |
656 | 0 | break; |
657 | 0 | case llvm::Triple::riscv32: |
658 | 0 | case llvm::Triple::riscv64: |
659 | 0 | riscv::getRISCVTargetFeatures(D, Triple, Args, Features); |
660 | 0 | break; |
661 | 0 | case llvm::Triple::systemz: |
662 | 0 | systemz::getSystemZTargetFeatures(D, Args, Features); |
663 | 0 | break; |
664 | 0 | case llvm::Triple::aarch64: |
665 | 0 | case llvm::Triple::aarch64_32: |
666 | 0 | case llvm::Triple::aarch64_be: |
667 | 0 | aarch64::getAArch64TargetFeatures(D, Triple, Args, Features, ForAS); |
668 | 0 | break; |
669 | 0 | case llvm::Triple::x86: |
670 | 0 | case llvm::Triple::x86_64: |
671 | 0 | x86::getX86TargetFeatures(D, Triple, Args, Features); |
672 | 0 | break; |
673 | 0 | case llvm::Triple::hexagon: |
674 | 0 | hexagon::getHexagonTargetFeatures(D, Triple, Args, Features); |
675 | 0 | break; |
676 | 0 | case llvm::Triple::wasm32: |
677 | 0 | case llvm::Triple::wasm64: |
678 | 0 | getWebAssemblyTargetFeatures(D, Triple, Args, Features); |
679 | 0 | break; |
680 | 0 | case llvm::Triple::sparc: |
681 | 0 | case llvm::Triple::sparcel: |
682 | 0 | case llvm::Triple::sparcv9: |
683 | 0 | sparc::getSparcTargetFeatures(D, Args, Features); |
684 | 0 | break; |
685 | 0 | case llvm::Triple::r600: |
686 | 0 | case llvm::Triple::amdgcn: |
687 | 0 | amdgpu::getAMDGPUTargetFeatures(D, Triple, Args, Features); |
688 | 0 | break; |
689 | 0 | case llvm::Triple::nvptx: |
690 | 0 | case llvm::Triple::nvptx64: |
691 | 0 | NVPTX::getNVPTXTargetFeatures(D, Triple, Args, Features); |
692 | 0 | break; |
693 | 0 | case llvm::Triple::m68k: |
694 | 0 | m68k::getM68kTargetFeatures(D, Triple, Args, Features); |
695 | 0 | break; |
696 | 0 | case llvm::Triple::msp430: |
697 | 0 | msp430::getMSP430TargetFeatures(D, Args, Features); |
698 | 0 | break; |
699 | 0 | case llvm::Triple::ve: |
700 | 0 | ve::getVETargetFeatures(D, Args, Features); |
701 | 0 | break; |
702 | 0 | case llvm::Triple::csky: |
703 | 0 | csky::getCSKYTargetFeatures(D, Triple, Args, CmdArgs, Features); |
704 | 0 | break; |
705 | 0 | case llvm::Triple::loongarch32: |
706 | 0 | case llvm::Triple::loongarch64: |
707 | 0 | loongarch::getLoongArchTargetFeatures(D, Triple, Args, Features); |
708 | 0 | break; |
709 | 0 | } |
710 | | |
711 | 0 | for (auto Feature : unifyTargetFeatures(Features)) { |
712 | 0 | CmdArgs.push_back(IsAux ? "-aux-target-feature" : "-target-feature"); |
713 | 0 | CmdArgs.push_back(Feature.data()); |
714 | 0 | } |
715 | 0 | } |
716 | | |
717 | 0 | llvm::StringRef tools::getLTOParallelism(const ArgList &Args, const Driver &D) { |
718 | 0 | Arg *LtoJobsArg = Args.getLastArg(options::OPT_flto_jobs_EQ); |
719 | 0 | if (!LtoJobsArg) |
720 | 0 | return {}; |
721 | 0 | if (!llvm::get_threadpool_strategy(LtoJobsArg->getValue())) |
722 | 0 | D.Diag(diag::err_drv_invalid_int_value) |
723 | 0 | << LtoJobsArg->getAsString(Args) << LtoJobsArg->getValue(); |
724 | 0 | return LtoJobsArg->getValue(); |
725 | 0 | } |
726 | | |
727 | | // PS4/PS5 uses -ffunction-sections and -fdata-sections by default. |
728 | 0 | bool tools::isUseSeparateSections(const llvm::Triple &Triple) { |
729 | 0 | return Triple.isPS(); |
730 | 0 | } |
731 | | |
732 | | void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args, |
733 | | ArgStringList &CmdArgs, const InputInfo &Output, |
734 | 0 | const InputInfo &Input, bool IsThinLTO) { |
735 | 0 | const bool IsOSAIX = ToolChain.getTriple().isOSAIX(); |
736 | 0 | const bool IsAMDGCN = ToolChain.getTriple().isAMDGCN(); |
737 | 0 | const char *Linker = Args.MakeArgString(ToolChain.GetLinkerPath()); |
738 | 0 | const Driver &D = ToolChain.getDriver(); |
739 | 0 | const bool IsFatLTO = Args.hasArg(options::OPT_ffat_lto_objects); |
740 | 0 | const bool IsUnifiedLTO = Args.hasArg(options::OPT_funified_lto); |
741 | 0 | if (llvm::sys::path::filename(Linker) != "ld.lld" && |
742 | 0 | llvm::sys::path::stem(Linker) != "ld.lld" && |
743 | 0 | !ToolChain.getTriple().isOSOpenBSD()) { |
744 | | // Tell the linker to load the plugin. This has to come before |
745 | | // AddLinkerInputs as gold requires -plugin and AIX ld requires -bplugin to |
746 | | // come before any -plugin-opt/-bplugin_opt that -Wl might forward. |
747 | 0 | const char *PluginPrefix = IsOSAIX ? "-bplugin:" : ""; |
748 | 0 | const char *PluginName = IsOSAIX ? "/libLTO" : "/LLVMgold"; |
749 | |
|
750 | 0 | if (!IsOSAIX) |
751 | 0 | CmdArgs.push_back("-plugin"); |
752 | |
|
753 | | #if defined(_WIN32) |
754 | | const char *Suffix = ".dll"; |
755 | | #elif defined(__APPLE__) |
756 | | const char *Suffix = ".dylib"; |
757 | | #else |
758 | 0 | const char *Suffix = ".so"; |
759 | 0 | #endif |
760 | |
|
761 | 0 | SmallString<1024> Plugin; |
762 | 0 | llvm::sys::path::native(Twine(D.Dir) + |
763 | 0 | "/../" CLANG_INSTALL_LIBDIR_BASENAME + |
764 | 0 | PluginName + Suffix, |
765 | 0 | Plugin); |
766 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginPrefix) + Plugin)); |
767 | 0 | } else { |
768 | | // Tell LLD to find and use .llvm.lto section in regular relocatable object |
769 | | // files |
770 | 0 | if (IsFatLTO) |
771 | 0 | CmdArgs.push_back("--fat-lto-objects"); |
772 | 0 | } |
773 | |
|
774 | 0 | const char *PluginOptPrefix = IsOSAIX ? "-bplugin_opt:" : "-plugin-opt="; |
775 | 0 | const char *ExtraDash = IsOSAIX ? "-" : ""; |
776 | 0 | const char *ParallelismOpt = IsOSAIX ? "-threads=" : "jobs="; |
777 | | |
778 | | // Note, this solution is far from perfect, better to encode it into IR |
779 | | // metadata, but this may not be worth it, since it looks like aranges is on |
780 | | // the way out. |
781 | 0 | if (Args.hasArg(options::OPT_gdwarf_aranges)) { |
782 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
783 | 0 | "-generate-arange-section")); |
784 | 0 | } |
785 | | |
786 | | // Try to pass driver level flags relevant to LTO code generation down to |
787 | | // the plugin. |
788 | | |
789 | | // Handle flags for selecting CPU variants. |
790 | 0 | std::string CPU = getCPUName(D, Args, ToolChain.getTriple()); |
791 | 0 | if (!CPU.empty()) |
792 | 0 | CmdArgs.push_back( |
793 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + "mcpu=" + CPU)); |
794 | |
|
795 | 0 | if (Arg *A = Args.getLastArg(options::OPT_O_Group)) { |
796 | | // The optimization level matches |
797 | | // CompilerInvocation.cpp:getOptimizationLevel(). |
798 | 0 | StringRef OOpt; |
799 | 0 | if (A->getOption().matches(options::OPT_O4) || |
800 | 0 | A->getOption().matches(options::OPT_Ofast)) |
801 | 0 | OOpt = "3"; |
802 | 0 | else if (A->getOption().matches(options::OPT_O)) { |
803 | 0 | OOpt = A->getValue(); |
804 | 0 | if (OOpt == "g") |
805 | 0 | OOpt = "1"; |
806 | 0 | else if (OOpt == "s" || OOpt == "z") |
807 | 0 | OOpt = "2"; |
808 | 0 | } else if (A->getOption().matches(options::OPT_O0)) |
809 | 0 | OOpt = "0"; |
810 | 0 | if (!OOpt.empty()) { |
811 | 0 | CmdArgs.push_back( |
812 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + "O" + OOpt)); |
813 | 0 | if (IsAMDGCN) |
814 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine("--lto-CGO") + OOpt)); |
815 | 0 | } |
816 | 0 | } |
817 | |
|
818 | 0 | if (Args.hasArg(options::OPT_gsplit_dwarf)) |
819 | 0 | CmdArgs.push_back(Args.MakeArgString( |
820 | 0 | Twine(PluginOptPrefix) + "dwo_dir=" + Output.getFilename() + "_dwo")); |
821 | |
|
822 | 0 | if (IsThinLTO && !IsOSAIX) |
823 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + "thinlto")); |
824 | 0 | else if (IsThinLTO && IsOSAIX) |
825 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine("-bdbg:thinlto"))); |
826 | | |
827 | | // Matrix intrinsic lowering happens at link time with ThinLTO. Enable |
828 | | // LowerMatrixIntrinsicsPass, which is transitively called by |
829 | | // buildThinLTODefaultPipeline under EnableMatrix. |
830 | 0 | if ((IsThinLTO || IsFatLTO || IsUnifiedLTO) && |
831 | 0 | Args.hasArg(options::OPT_fenable_matrix)) |
832 | 0 | CmdArgs.push_back( |
833 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-enable-matrix")); |
834 | |
|
835 | 0 | StringRef Parallelism = getLTOParallelism(Args, D); |
836 | 0 | if (!Parallelism.empty()) |
837 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
838 | 0 | ParallelismOpt + Parallelism)); |
839 | | |
840 | | // Pass down GlobalISel options. |
841 | 0 | if (Arg *A = Args.getLastArg(options::OPT_fglobal_isel, |
842 | 0 | options::OPT_fno_global_isel)) { |
843 | | // Parsing -fno-global-isel explicitly gives architectures that enable GISel |
844 | | // by default a chance to disable it. |
845 | 0 | CmdArgs.push_back(Args.MakeArgString( |
846 | 0 | Twine(PluginOptPrefix) + "-global-isel=" + |
847 | 0 | (A->getOption().matches(options::OPT_fglobal_isel) ? "1" : "0"))); |
848 | 0 | } |
849 | | |
850 | | // If an explicit debugger tuning argument appeared, pass it along. |
851 | 0 | if (Arg *A = |
852 | 0 | Args.getLastArg(options::OPT_gTune_Group, options::OPT_ggdbN_Group)) { |
853 | 0 | if (A->getOption().matches(options::OPT_glldb)) |
854 | 0 | CmdArgs.push_back( |
855 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-debugger-tune=lldb")); |
856 | 0 | else if (A->getOption().matches(options::OPT_gsce)) |
857 | 0 | CmdArgs.push_back( |
858 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-debugger-tune=sce")); |
859 | 0 | else if (A->getOption().matches(options::OPT_gdbx)) |
860 | 0 | CmdArgs.push_back( |
861 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-debugger-tune=dbx")); |
862 | 0 | else |
863 | 0 | CmdArgs.push_back( |
864 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-debugger-tune=gdb")); |
865 | 0 | } |
866 | |
|
867 | 0 | if (IsOSAIX) { |
868 | 0 | if (!ToolChain.useIntegratedAs()) |
869 | 0 | CmdArgs.push_back( |
870 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-no-integrated-as=1")); |
871 | | |
872 | | // On AIX, clang assumes strict-dwarf is true if any debug option is |
873 | | // specified, unless it is told explicitly not to assume so. |
874 | 0 | Arg *A = Args.getLastArg(options::OPT_g_Group); |
875 | 0 | bool EnableDebugInfo = A && !A->getOption().matches(options::OPT_g0) && |
876 | 0 | !A->getOption().matches(options::OPT_ggdb0); |
877 | 0 | if (EnableDebugInfo && Args.hasFlag(options::OPT_gstrict_dwarf, |
878 | 0 | options::OPT_gno_strict_dwarf, true)) |
879 | 0 | CmdArgs.push_back( |
880 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-strict-dwarf=true")); |
881 | |
|
882 | 0 | for (const Arg *A : Args.filtered_reverse(options::OPT_mabi_EQ)) { |
883 | 0 | StringRef V = A->getValue(); |
884 | 0 | if (V == "vec-default") |
885 | 0 | break; |
886 | 0 | if (V == "vec-extabi") { |
887 | 0 | CmdArgs.push_back( |
888 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-vec-extabi")); |
889 | 0 | break; |
890 | 0 | } |
891 | 0 | } |
892 | 0 | } |
893 | |
|
894 | 0 | bool UseSeparateSections = |
895 | 0 | isUseSeparateSections(ToolChain.getEffectiveTriple()); |
896 | |
|
897 | 0 | if (Args.hasFlag(options::OPT_ffunction_sections, |
898 | 0 | options::OPT_fno_function_sections, UseSeparateSections)) |
899 | 0 | CmdArgs.push_back( |
900 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-function-sections=1")); |
901 | 0 | else if (Args.hasArg(options::OPT_fno_function_sections)) |
902 | 0 | CmdArgs.push_back( |
903 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-function-sections=0")); |
904 | |
|
905 | 0 | bool DataSectionsTurnedOff = false; |
906 | 0 | if (Args.hasFlag(options::OPT_fdata_sections, options::OPT_fno_data_sections, |
907 | 0 | UseSeparateSections)) { |
908 | 0 | CmdArgs.push_back( |
909 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-data-sections=1")); |
910 | 0 | } else if (Args.hasArg(options::OPT_fno_data_sections)) { |
911 | 0 | DataSectionsTurnedOff = true; |
912 | 0 | CmdArgs.push_back( |
913 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-data-sections=0")); |
914 | 0 | } |
915 | |
|
916 | 0 | if (Args.hasArg(options::OPT_mxcoff_roptr) || |
917 | 0 | Args.hasArg(options::OPT_mno_xcoff_roptr)) { |
918 | 0 | bool HasRoptr = Args.hasFlag(options::OPT_mxcoff_roptr, |
919 | 0 | options::OPT_mno_xcoff_roptr, false); |
920 | 0 | StringRef OptStr = HasRoptr ? "-mxcoff-roptr" : "-mno-xcoff-roptr"; |
921 | |
|
922 | 0 | if (!IsOSAIX) |
923 | 0 | D.Diag(diag::err_drv_unsupported_opt_for_target) |
924 | 0 | << OptStr << ToolChain.getTriple().str(); |
925 | |
|
926 | 0 | if (HasRoptr) { |
927 | | // The data sections option is on by default on AIX. We only need to error |
928 | | // out when -fno-data-sections is specified explicitly to turn off data |
929 | | // sections. |
930 | 0 | if (DataSectionsTurnedOff) |
931 | 0 | D.Diag(diag::err_roptr_requires_data_sections); |
932 | |
|
933 | 0 | CmdArgs.push_back( |
934 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-mxcoff-roptr")); |
935 | 0 | } |
936 | 0 | } |
937 | | |
938 | | // Pass an option to enable split machine functions. |
939 | 0 | if (auto *A = Args.getLastArg(options::OPT_fsplit_machine_functions, |
940 | 0 | options::OPT_fno_split_machine_functions)) { |
941 | 0 | if (A->getOption().matches(options::OPT_fsplit_machine_functions)) |
942 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
943 | 0 | "-split-machine-functions")); |
944 | 0 | } |
945 | |
|
946 | 0 | if (Arg *A = getLastProfileSampleUseArg(Args)) { |
947 | 0 | StringRef FName = A->getValue(); |
948 | 0 | if (!llvm::sys::fs::exists(FName)) |
949 | 0 | D.Diag(diag::err_drv_no_such_file) << FName; |
950 | 0 | else |
951 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
952 | 0 | "sample-profile=" + FName)); |
953 | 0 | } |
954 | |
|
955 | 0 | if (auto *CSPGOGenerateArg = getLastCSProfileGenerateArg(Args)) { |
956 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + |
957 | 0 | "cs-profile-generate")); |
958 | 0 | if (CSPGOGenerateArg->getOption().matches( |
959 | 0 | options::OPT_fcs_profile_generate_EQ)) { |
960 | 0 | SmallString<128> Path(CSPGOGenerateArg->getValue()); |
961 | 0 | llvm::sys::path::append(Path, "default_%m.profraw"); |
962 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + |
963 | 0 | "cs-profile-path=" + Path)); |
964 | 0 | } else |
965 | 0 | CmdArgs.push_back( |
966 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + |
967 | 0 | "cs-profile-path=default_%m.profraw")); |
968 | 0 | } else if (auto *ProfileUseArg = getLastProfileUseArg(Args)) { |
969 | 0 | SmallString<128> Path( |
970 | 0 | ProfileUseArg->getNumValues() == 0 ? "" : ProfileUseArg->getValue()); |
971 | 0 | if (Path.empty() || llvm::sys::fs::is_directory(Path)) |
972 | 0 | llvm::sys::path::append(Path, "default.profdata"); |
973 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + ExtraDash + |
974 | 0 | "cs-profile-path=" + Path)); |
975 | 0 | } |
976 | | |
977 | | // This controls whether or not we perform JustMyCode instrumentation. |
978 | 0 | if (Args.hasFlag(options::OPT_fjmc, options::OPT_fno_jmc, false)) { |
979 | 0 | if (ToolChain.getEffectiveTriple().isOSBinFormatELF()) |
980 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + |
981 | 0 | "-enable-jmc-instrument")); |
982 | 0 | else |
983 | 0 | D.Diag(clang::diag::warn_drv_fjmc_for_elf_only); |
984 | 0 | } |
985 | |
|
986 | 0 | if (Args.hasFlag(options::OPT_femulated_tls, options::OPT_fno_emulated_tls, |
987 | 0 | ToolChain.getTriple().hasDefaultEmulatedTLS())) { |
988 | 0 | CmdArgs.push_back( |
989 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-emulated-tls")); |
990 | 0 | } |
991 | |
|
992 | 0 | if (Args.hasFlag(options::OPT_fstack_size_section, |
993 | 0 | options::OPT_fno_stack_size_section, false)) |
994 | 0 | CmdArgs.push_back( |
995 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "-stack-size-section")); |
996 | | |
997 | | // Setup statistics file output. |
998 | 0 | SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D); |
999 | 0 | if (!StatsFile.empty()) |
1000 | 0 | CmdArgs.push_back( |
1001 | 0 | Args.MakeArgString(Twine(PluginOptPrefix) + "stats-file=" + StatsFile)); |
1002 | | |
1003 | | // Setup crash diagnostics dir. |
1004 | 0 | if (Arg *A = Args.getLastArg(options::OPT_fcrash_diagnostics_dir)) |
1005 | 0 | CmdArgs.push_back(Args.MakeArgString( |
1006 | 0 | Twine(PluginOptPrefix) + "-crash-diagnostics-dir=" + A->getValue())); |
1007 | |
|
1008 | 0 | addX86AlignBranchArgs(D, Args, CmdArgs, /*IsLTO=*/true, PluginOptPrefix); |
1009 | | |
1010 | | // Handle remark diagnostics on screen options: '-Rpass-*'. |
1011 | 0 | renderRpassOptions(Args, CmdArgs, PluginOptPrefix); |
1012 | | |
1013 | | // Handle serialized remarks options: '-fsave-optimization-record' |
1014 | | // and '-foptimization-record-*'. |
1015 | 0 | if (willEmitRemarks(Args)) |
1016 | 0 | renderRemarksOptions(Args, CmdArgs, ToolChain.getEffectiveTriple(), Input, |
1017 | 0 | Output, PluginOptPrefix); |
1018 | | |
1019 | | // Handle remarks hotness/threshold related options. |
1020 | 0 | renderRemarksHotnessOptions(Args, CmdArgs, PluginOptPrefix); |
1021 | |
|
1022 | 0 | addMachineOutlinerArgs(D, Args, CmdArgs, ToolChain.getEffectiveTriple(), |
1023 | 0 | /*IsLTO=*/true, PluginOptPrefix); |
1024 | 0 | } |
1025 | | |
1026 | | /// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the |
1027 | | /// LLVM C library for GPUs. |
1028 | | static void addOpenMPDeviceLibC(const ToolChain &TC, const ArgList &Args, |
1029 | 0 | ArgStringList &CmdArgs) { |
1030 | 0 | if (Args.hasArg(options::OPT_nogpulib) || Args.hasArg(options::OPT_nolibc)) |
1031 | 0 | return; |
1032 | | |
1033 | | // Check the resource directory for the LLVM libc GPU declarations. If it's |
1034 | | // found we can assume that LLVM was built with support for the GPU libc. |
1035 | 0 | SmallString<256> LibCDecls(TC.getDriver().ResourceDir); |
1036 | 0 | llvm::sys::path::append(LibCDecls, "include", "llvm_libc_wrappers", |
1037 | 0 | "llvm-libc-decls"); |
1038 | 0 | bool HasLibC = llvm::sys::fs::exists(LibCDecls) && |
1039 | 0 | llvm::sys::fs::is_directory(LibCDecls); |
1040 | 0 | if (Args.hasFlag(options::OPT_gpulibc, options::OPT_nogpulibc, HasLibC)) { |
1041 | 0 | CmdArgs.push_back("-lcgpu"); |
1042 | 0 | CmdArgs.push_back("-lmgpu"); |
1043 | 0 | } |
1044 | 0 | } |
1045 | | |
1046 | | void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC, |
1047 | | const ArgList &Args, |
1048 | 0 | ArgStringList &CmdArgs) { |
1049 | | // Default to clang lib / lib64 folder, i.e. the same location as device |
1050 | | // runtime. |
1051 | 0 | SmallString<256> DefaultLibPath = |
1052 | 0 | llvm::sys::path::parent_path(TC.getDriver().Dir); |
1053 | 0 | llvm::sys::path::append(DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME); |
1054 | 0 | CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath)); |
1055 | 0 | } |
1056 | | |
1057 | | void tools::addArchSpecificRPath(const ToolChain &TC, const ArgList &Args, |
1058 | 0 | ArgStringList &CmdArgs) { |
1059 | 0 | if (!Args.hasFlag(options::OPT_frtlib_add_rpath, |
1060 | 0 | options::OPT_fno_rtlib_add_rpath, false)) |
1061 | 0 | return; |
1062 | | |
1063 | 0 | for (const auto &CandidateRPath : TC.getArchSpecificLibPaths()) { |
1064 | 0 | if (TC.getVFS().exists(CandidateRPath)) { |
1065 | 0 | CmdArgs.push_back("-rpath"); |
1066 | 0 | CmdArgs.push_back(Args.MakeArgString(CandidateRPath)); |
1067 | 0 | } |
1068 | 0 | } |
1069 | 0 | } |
1070 | | |
1071 | | bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC, |
1072 | | const ArgList &Args, bool ForceStaticHostRuntime, |
1073 | 0 | bool IsOffloadingHost, bool GompNeedsRT) { |
1074 | 0 | if (!Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
1075 | 0 | options::OPT_fno_openmp, false)) |
1076 | 0 | return false; |
1077 | | |
1078 | 0 | Driver::OpenMPRuntimeKind RTKind = TC.getDriver().getOpenMPRuntime(Args); |
1079 | |
|
1080 | 0 | if (RTKind == Driver::OMPRT_Unknown) |
1081 | | // Already diagnosed. |
1082 | 0 | return false; |
1083 | | |
1084 | 0 | if (ForceStaticHostRuntime) |
1085 | 0 | CmdArgs.push_back("-Bstatic"); |
1086 | |
|
1087 | 0 | switch (RTKind) { |
1088 | 0 | case Driver::OMPRT_OMP: |
1089 | 0 | CmdArgs.push_back("-lomp"); |
1090 | 0 | break; |
1091 | 0 | case Driver::OMPRT_GOMP: |
1092 | 0 | CmdArgs.push_back("-lgomp"); |
1093 | 0 | break; |
1094 | 0 | case Driver::OMPRT_IOMP5: |
1095 | 0 | CmdArgs.push_back("-liomp5"); |
1096 | 0 | break; |
1097 | 0 | case Driver::OMPRT_Unknown: |
1098 | 0 | break; |
1099 | 0 | } |
1100 | | |
1101 | 0 | if (ForceStaticHostRuntime) |
1102 | 0 | CmdArgs.push_back("-Bdynamic"); |
1103 | |
|
1104 | 0 | if (RTKind == Driver::OMPRT_GOMP && GompNeedsRT) |
1105 | 0 | CmdArgs.push_back("-lrt"); |
1106 | |
|
1107 | 0 | if (IsOffloadingHost) |
1108 | 0 | CmdArgs.push_back("-lomptarget"); |
1109 | |
|
1110 | 0 | if (IsOffloadingHost && !Args.hasArg(options::OPT_nogpulib)) |
1111 | 0 | CmdArgs.push_back("-lomptarget.devicertl"); |
1112 | |
|
1113 | 0 | if (IsOffloadingHost) |
1114 | 0 | addOpenMPDeviceLibC(TC, Args, CmdArgs); |
1115 | |
|
1116 | 0 | addArchSpecificRPath(TC, Args, CmdArgs); |
1117 | 0 | addOpenMPRuntimeLibraryPath(TC, Args, CmdArgs); |
1118 | |
|
1119 | 0 | return true; |
1120 | 0 | } |
1121 | | |
1122 | | /// Determines if --whole-archive is active in the list of arguments. |
1123 | 0 | static bool isWholeArchivePresent(const ArgList &Args) { |
1124 | 0 | bool WholeArchiveActive = false; |
1125 | 0 | for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA)) { |
1126 | 0 | if (Arg) { |
1127 | 0 | for (StringRef ArgValue : Arg->getValues()) { |
1128 | 0 | if (ArgValue == "--whole-archive") |
1129 | 0 | WholeArchiveActive = true; |
1130 | 0 | if (ArgValue == "--no-whole-archive") |
1131 | 0 | WholeArchiveActive = false; |
1132 | 0 | } |
1133 | 0 | } |
1134 | 0 | } |
1135 | |
|
1136 | 0 | return WholeArchiveActive; |
1137 | 0 | } |
1138 | | |
1139 | | /// Determine if driver is invoked to create a shared object library (-static) |
1140 | 0 | static bool isSharedLinkage(const ArgList &Args) { |
1141 | 0 | return Args.hasArg(options::OPT_shared); |
1142 | 0 | } |
1143 | | |
1144 | | /// Determine if driver is invoked to create a static object library (-shared) |
1145 | 0 | static bool isStaticLinkage(const ArgList &Args) { |
1146 | 0 | return Args.hasArg(options::OPT_static); |
1147 | 0 | } |
1148 | | |
1149 | | /// Add Fortran runtime libs for MSVC |
1150 | | static void addFortranRuntimeLibsMSVC(const ArgList &Args, |
1151 | 0 | llvm::opt::ArgStringList &CmdArgs) { |
1152 | 0 | unsigned RTOptionID = options::OPT__SLASH_MT; |
1153 | 0 | if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) { |
1154 | 0 | RTOptionID = llvm::StringSwitch<unsigned>(rtl->getValue()) |
1155 | 0 | .Case("static", options::OPT__SLASH_MT) |
1156 | 0 | .Case("static_dbg", options::OPT__SLASH_MTd) |
1157 | 0 | .Case("dll", options::OPT__SLASH_MD) |
1158 | 0 | .Case("dll_dbg", options::OPT__SLASH_MDd) |
1159 | 0 | .Default(options::OPT__SLASH_MT); |
1160 | 0 | } |
1161 | 0 | switch (RTOptionID) { |
1162 | 0 | case options::OPT__SLASH_MT: |
1163 | 0 | CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.static.lib"); |
1164 | 0 | break; |
1165 | 0 | case options::OPT__SLASH_MTd: |
1166 | 0 | CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.static_dbg.lib"); |
1167 | 0 | break; |
1168 | 0 | case options::OPT__SLASH_MD: |
1169 | 0 | CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.dynamic.lib"); |
1170 | 0 | break; |
1171 | 0 | case options::OPT__SLASH_MDd: |
1172 | 0 | CmdArgs.push_back("/WHOLEARCHIVE:Fortran_main.dynamic_dbg.lib"); |
1173 | 0 | break; |
1174 | 0 | } |
1175 | 0 | } |
1176 | | |
1177 | | // Add FortranMain runtime lib |
1178 | | static void addFortranMain(const ToolChain &TC, const ArgList &Args, |
1179 | 0 | llvm::opt::ArgStringList &CmdArgs) { |
1180 | | // 0. Shared-library linkage |
1181 | | // If we are attempting to link a library, we should not add |
1182 | | // -lFortran_main.a to the link line, as the `main` symbol is not |
1183 | | // required for a library and should also be provided by one of |
1184 | | // the translation units of the code that this shared library |
1185 | | // will be linked against eventually. |
1186 | 0 | if (isSharedLinkage(Args) || isStaticLinkage(Args)) { |
1187 | 0 | return; |
1188 | 0 | } |
1189 | | |
1190 | | // 1. MSVC |
1191 | 0 | if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { |
1192 | 0 | addFortranRuntimeLibsMSVC(Args, CmdArgs); |
1193 | 0 | return; |
1194 | 0 | } |
1195 | | |
1196 | | // 2. GNU and similar |
1197 | | // The --whole-archive option needs to be part of the link line to make |
1198 | | // sure that the main() function from Fortran_main.a is pulled in by the |
1199 | | // linker. However, it shouldn't be used if it's already active. |
1200 | | // TODO: Find an equivalent of `--whole-archive` for Darwin and AIX. |
1201 | 0 | if (!isWholeArchivePresent(Args) && !TC.getTriple().isMacOSX() && |
1202 | 0 | !TC.getTriple().isOSAIX()) { |
1203 | 0 | CmdArgs.push_back("--whole-archive"); |
1204 | 0 | CmdArgs.push_back("-lFortran_main"); |
1205 | 0 | CmdArgs.push_back("--no-whole-archive"); |
1206 | 0 | return; |
1207 | 0 | } |
1208 | | |
1209 | 0 | CmdArgs.push_back("-lFortran_main"); |
1210 | 0 | } |
1211 | | |
1212 | | /// Add Fortran runtime libs |
1213 | | void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args, |
1214 | 0 | llvm::opt::ArgStringList &CmdArgs) { |
1215 | | // 1. Link FortranMain |
1216 | | // FortranMain depends on FortranRuntime, so needs to be listed first. If |
1217 | | // -fno-fortran-main has been passed, skip linking Fortran_main.a |
1218 | 0 | if (!Args.hasArg(options::OPT_no_fortran_main)) |
1219 | 0 | addFortranMain(TC, Args, CmdArgs); |
1220 | | |
1221 | | // 2. Link FortranRuntime and FortranDecimal |
1222 | | // These are handled earlier on Windows by telling the frontend driver to |
1223 | | // add the correct libraries to link against as dependents in the object |
1224 | | // file. |
1225 | 0 | if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) { |
1226 | 0 | CmdArgs.push_back("-lFortranRuntime"); |
1227 | 0 | CmdArgs.push_back("-lFortranDecimal"); |
1228 | 0 | } |
1229 | 0 | } |
1230 | | |
1231 | | void tools::addFortranRuntimeLibraryPath(const ToolChain &TC, |
1232 | | const llvm::opt::ArgList &Args, |
1233 | 0 | ArgStringList &CmdArgs) { |
1234 | | // Default to the <driver-path>/../lib directory. This works fine on the |
1235 | | // platforms that we have tested so far. We will probably have to re-fine |
1236 | | // this in the future. In particular, on some platforms, we may need to use |
1237 | | // lib64 instead of lib. |
1238 | 0 | SmallString<256> DefaultLibPath = |
1239 | 0 | llvm::sys::path::parent_path(TC.getDriver().Dir); |
1240 | 0 | llvm::sys::path::append(DefaultLibPath, "lib"); |
1241 | 0 | if (TC.getTriple().isKnownWindowsMSVCEnvironment()) |
1242 | 0 | CmdArgs.push_back(Args.MakeArgString("-libpath:" + DefaultLibPath)); |
1243 | 0 | else |
1244 | 0 | CmdArgs.push_back(Args.MakeArgString("-L" + DefaultLibPath)); |
1245 | 0 | } |
1246 | | |
1247 | | static void addSanitizerRuntime(const ToolChain &TC, const ArgList &Args, |
1248 | | ArgStringList &CmdArgs, StringRef Sanitizer, |
1249 | 0 | bool IsShared, bool IsWhole) { |
1250 | | // Wrap any static runtimes that must be forced into executable in |
1251 | | // whole-archive. |
1252 | 0 | if (IsWhole) CmdArgs.push_back("--whole-archive"); |
1253 | 0 | CmdArgs.push_back(TC.getCompilerRTArgString( |
1254 | 0 | Args, Sanitizer, IsShared ? ToolChain::FT_Shared : ToolChain::FT_Static)); |
1255 | 0 | if (IsWhole) CmdArgs.push_back("--no-whole-archive"); |
1256 | |
|
1257 | 0 | if (IsShared) { |
1258 | 0 | addArchSpecificRPath(TC, Args, CmdArgs); |
1259 | 0 | } |
1260 | 0 | } |
1261 | | |
1262 | | // Tries to use a file with the list of dynamic symbols that need to be exported |
1263 | | // from the runtime library. Returns true if the file was found. |
1264 | | static bool addSanitizerDynamicList(const ToolChain &TC, const ArgList &Args, |
1265 | | ArgStringList &CmdArgs, |
1266 | 0 | StringRef Sanitizer) { |
1267 | 0 | bool LinkerIsGnuLd = solaris::isLinkerGnuLd(TC, Args); |
1268 | | |
1269 | | // Solaris ld defaults to --export-dynamic behaviour but doesn't support |
1270 | | // the option, so don't try to pass it. |
1271 | 0 | if (TC.getTriple().isOSSolaris() && !LinkerIsGnuLd) |
1272 | 0 | return true; |
1273 | 0 | SmallString<128> SanRT(TC.getCompilerRT(Args, Sanitizer)); |
1274 | 0 | if (llvm::sys::fs::exists(SanRT + ".syms")) { |
1275 | 0 | CmdArgs.push_back(Args.MakeArgString("--dynamic-list=" + SanRT + ".syms")); |
1276 | 0 | return true; |
1277 | 0 | } |
1278 | 0 | return false; |
1279 | 0 | } |
1280 | | |
1281 | | void tools::addAsNeededOption(const ToolChain &TC, |
1282 | | const llvm::opt::ArgList &Args, |
1283 | | llvm::opt::ArgStringList &CmdArgs, |
1284 | 0 | bool as_needed) { |
1285 | 0 | assert(!TC.getTriple().isOSAIX() && |
1286 | 0 | "AIX linker does not support any form of --as-needed option yet."); |
1287 | 0 | bool LinkerIsGnuLd = solaris::isLinkerGnuLd(TC, Args); |
1288 | | |
1289 | | // While the Solaris 11.2 ld added --as-needed/--no-as-needed as aliases |
1290 | | // for the native forms -z ignore/-z record, they are missing in Illumos, |
1291 | | // so always use the native form. |
1292 | | // GNU ld doesn't support -z ignore/-z record, so don't use them even on |
1293 | | // Solaris. |
1294 | 0 | if (TC.getTriple().isOSSolaris() && !LinkerIsGnuLd) { |
1295 | 0 | CmdArgs.push_back("-z"); |
1296 | 0 | CmdArgs.push_back(as_needed ? "ignore" : "record"); |
1297 | 0 | } else { |
1298 | 0 | CmdArgs.push_back(as_needed ? "--as-needed" : "--no-as-needed"); |
1299 | 0 | } |
1300 | 0 | } |
1301 | | |
1302 | | void tools::linkSanitizerRuntimeDeps(const ToolChain &TC, |
1303 | | const llvm::opt::ArgList &Args, |
1304 | 0 | ArgStringList &CmdArgs) { |
1305 | | // Force linking against the system libraries sanitizers depends on |
1306 | | // (see PR15823 why this is necessary). |
1307 | 0 | addAsNeededOption(TC, Args, CmdArgs, false); |
1308 | | // There's no libpthread or librt on RTEMS & Android. |
1309 | 0 | if (TC.getTriple().getOS() != llvm::Triple::RTEMS && |
1310 | 0 | !TC.getTriple().isAndroid() && !TC.getTriple().isOHOSFamily()) { |
1311 | 0 | CmdArgs.push_back("-lpthread"); |
1312 | 0 | if (!TC.getTriple().isOSOpenBSD()) |
1313 | 0 | CmdArgs.push_back("-lrt"); |
1314 | 0 | } |
1315 | 0 | CmdArgs.push_back("-lm"); |
1316 | | // There's no libdl on all OSes. |
1317 | 0 | if (!TC.getTriple().isOSFreeBSD() && !TC.getTriple().isOSNetBSD() && |
1318 | 0 | !TC.getTriple().isOSOpenBSD() && |
1319 | 0 | TC.getTriple().getOS() != llvm::Triple::RTEMS) |
1320 | 0 | CmdArgs.push_back("-ldl"); |
1321 | | // Required for backtrace on some OSes |
1322 | 0 | if (TC.getTriple().isOSFreeBSD() || |
1323 | 0 | TC.getTriple().isOSNetBSD() || |
1324 | 0 | TC.getTriple().isOSOpenBSD()) |
1325 | 0 | CmdArgs.push_back("-lexecinfo"); |
1326 | | // There is no libresolv on Android, FreeBSD, OpenBSD, etc. On musl |
1327 | | // libresolv.a, even if exists, is an empty archive to satisfy POSIX -lresolv |
1328 | | // requirement. |
1329 | 0 | if (TC.getTriple().isOSLinux() && !TC.getTriple().isAndroid() && |
1330 | 0 | !TC.getTriple().isMusl()) |
1331 | 0 | CmdArgs.push_back("-lresolv"); |
1332 | 0 | } |
1333 | | |
1334 | | static void |
1335 | | collectSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, |
1336 | | SmallVectorImpl<StringRef> &SharedRuntimes, |
1337 | | SmallVectorImpl<StringRef> &StaticRuntimes, |
1338 | | SmallVectorImpl<StringRef> &NonWholeStaticRuntimes, |
1339 | | SmallVectorImpl<StringRef> &HelperStaticRuntimes, |
1340 | 0 | SmallVectorImpl<StringRef> &RequiredSymbols) { |
1341 | 0 | const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); |
1342 | | // Collect shared runtimes. |
1343 | 0 | if (SanArgs.needsSharedRt()) { |
1344 | 0 | if (SanArgs.needsAsanRt()) { |
1345 | 0 | SharedRuntimes.push_back("asan"); |
1346 | 0 | if (!Args.hasArg(options::OPT_shared) && !TC.getTriple().isAndroid()) |
1347 | 0 | HelperStaticRuntimes.push_back("asan-preinit"); |
1348 | 0 | } |
1349 | 0 | if (SanArgs.needsMemProfRt()) { |
1350 | 0 | SharedRuntimes.push_back("memprof"); |
1351 | 0 | if (!Args.hasArg(options::OPT_shared) && !TC.getTriple().isAndroid()) |
1352 | 0 | HelperStaticRuntimes.push_back("memprof-preinit"); |
1353 | 0 | } |
1354 | 0 | if (SanArgs.needsUbsanRt()) { |
1355 | 0 | if (SanArgs.requiresMinimalRuntime()) |
1356 | 0 | SharedRuntimes.push_back("ubsan_minimal"); |
1357 | 0 | else |
1358 | 0 | SharedRuntimes.push_back("ubsan_standalone"); |
1359 | 0 | } |
1360 | 0 | if (SanArgs.needsScudoRt()) { |
1361 | 0 | SharedRuntimes.push_back("scudo_standalone"); |
1362 | 0 | } |
1363 | 0 | if (SanArgs.needsTsanRt()) |
1364 | 0 | SharedRuntimes.push_back("tsan"); |
1365 | 0 | if (SanArgs.needsHwasanRt()) { |
1366 | 0 | if (SanArgs.needsHwasanAliasesRt()) |
1367 | 0 | SharedRuntimes.push_back("hwasan_aliases"); |
1368 | 0 | else |
1369 | 0 | SharedRuntimes.push_back("hwasan"); |
1370 | 0 | if (!Args.hasArg(options::OPT_shared)) |
1371 | 0 | HelperStaticRuntimes.push_back("hwasan-preinit"); |
1372 | 0 | } |
1373 | 0 | } |
1374 | | |
1375 | | // The stats_client library is also statically linked into DSOs. |
1376 | 0 | if (SanArgs.needsStatsRt()) |
1377 | 0 | StaticRuntimes.push_back("stats_client"); |
1378 | | |
1379 | | // Always link the static runtime regardless of DSO or executable. |
1380 | 0 | if (SanArgs.needsAsanRt()) |
1381 | 0 | HelperStaticRuntimes.push_back("asan_static"); |
1382 | | |
1383 | | // Collect static runtimes. |
1384 | 0 | if (Args.hasArg(options::OPT_shared)) { |
1385 | | // Don't link static runtimes into DSOs. |
1386 | 0 | return; |
1387 | 0 | } |
1388 | | |
1389 | | // Each static runtime that has a DSO counterpart above is excluded below, |
1390 | | // but runtimes that exist only as static are not affected by needsSharedRt. |
1391 | | |
1392 | 0 | if (!SanArgs.needsSharedRt() && SanArgs.needsAsanRt()) { |
1393 | 0 | StaticRuntimes.push_back("asan"); |
1394 | 0 | if (SanArgs.linkCXXRuntimes()) |
1395 | 0 | StaticRuntimes.push_back("asan_cxx"); |
1396 | 0 | } |
1397 | |
|
1398 | 0 | if (!SanArgs.needsSharedRt() && SanArgs.needsMemProfRt()) { |
1399 | 0 | StaticRuntimes.push_back("memprof"); |
1400 | 0 | if (SanArgs.linkCXXRuntimes()) |
1401 | 0 | StaticRuntimes.push_back("memprof_cxx"); |
1402 | 0 | } |
1403 | |
|
1404 | 0 | if (!SanArgs.needsSharedRt() && SanArgs.needsHwasanRt()) { |
1405 | 0 | if (SanArgs.needsHwasanAliasesRt()) { |
1406 | 0 | StaticRuntimes.push_back("hwasan_aliases"); |
1407 | 0 | if (SanArgs.linkCXXRuntimes()) |
1408 | 0 | StaticRuntimes.push_back("hwasan_aliases_cxx"); |
1409 | 0 | } else { |
1410 | 0 | StaticRuntimes.push_back("hwasan"); |
1411 | 0 | if (SanArgs.linkCXXRuntimes()) |
1412 | 0 | StaticRuntimes.push_back("hwasan_cxx"); |
1413 | 0 | } |
1414 | 0 | } |
1415 | 0 | if (SanArgs.needsDfsanRt()) |
1416 | 0 | StaticRuntimes.push_back("dfsan"); |
1417 | 0 | if (SanArgs.needsLsanRt()) |
1418 | 0 | StaticRuntimes.push_back("lsan"); |
1419 | 0 | if (SanArgs.needsMsanRt()) { |
1420 | 0 | StaticRuntimes.push_back("msan"); |
1421 | 0 | if (SanArgs.linkCXXRuntimes()) |
1422 | 0 | StaticRuntimes.push_back("msan_cxx"); |
1423 | 0 | } |
1424 | 0 | if (!SanArgs.needsSharedRt() && SanArgs.needsTsanRt()) { |
1425 | 0 | StaticRuntimes.push_back("tsan"); |
1426 | 0 | if (SanArgs.linkCXXRuntimes()) |
1427 | 0 | StaticRuntimes.push_back("tsan_cxx"); |
1428 | 0 | } |
1429 | 0 | if (!SanArgs.needsSharedRt() && SanArgs.needsUbsanRt()) { |
1430 | 0 | if (SanArgs.requiresMinimalRuntime()) { |
1431 | 0 | StaticRuntimes.push_back("ubsan_minimal"); |
1432 | 0 | } else { |
1433 | 0 | StaticRuntimes.push_back("ubsan_standalone"); |
1434 | 0 | if (SanArgs.linkCXXRuntimes()) |
1435 | 0 | StaticRuntimes.push_back("ubsan_standalone_cxx"); |
1436 | 0 | } |
1437 | 0 | } |
1438 | 0 | if (SanArgs.needsSafeStackRt()) { |
1439 | 0 | NonWholeStaticRuntimes.push_back("safestack"); |
1440 | 0 | RequiredSymbols.push_back("__safestack_init"); |
1441 | 0 | } |
1442 | 0 | if (!(SanArgs.needsSharedRt() && SanArgs.needsUbsanRt())) { |
1443 | 0 | if (SanArgs.needsCfiRt()) |
1444 | 0 | StaticRuntimes.push_back("cfi"); |
1445 | 0 | if (SanArgs.needsCfiDiagRt()) { |
1446 | 0 | StaticRuntimes.push_back("cfi_diag"); |
1447 | 0 | if (SanArgs.linkCXXRuntimes()) |
1448 | 0 | StaticRuntimes.push_back("ubsan_standalone_cxx"); |
1449 | 0 | } |
1450 | 0 | } |
1451 | 0 | if (SanArgs.needsStatsRt()) { |
1452 | 0 | NonWholeStaticRuntimes.push_back("stats"); |
1453 | 0 | RequiredSymbols.push_back("__sanitizer_stats_register"); |
1454 | 0 | } |
1455 | 0 | if (!SanArgs.needsSharedRt() && SanArgs.needsScudoRt()) { |
1456 | 0 | StaticRuntimes.push_back("scudo_standalone"); |
1457 | 0 | if (SanArgs.linkCXXRuntimes()) |
1458 | 0 | StaticRuntimes.push_back("scudo_standalone_cxx"); |
1459 | 0 | } |
1460 | 0 | } |
1461 | | |
1462 | | // Should be called before we add system libraries (C++ ABI, libstdc++/libc++, |
1463 | | // C runtime, etc). Returns true if sanitizer system deps need to be linked in. |
1464 | | bool tools::addSanitizerRuntimes(const ToolChain &TC, const ArgList &Args, |
1465 | 0 | ArgStringList &CmdArgs) { |
1466 | 0 | const SanitizerArgs &SanArgs = TC.getSanitizerArgs(Args); |
1467 | 0 | SmallVector<StringRef, 4> SharedRuntimes, StaticRuntimes, |
1468 | 0 | NonWholeStaticRuntimes, HelperStaticRuntimes, RequiredSymbols; |
1469 | 0 | if (SanArgs.linkRuntimes()) { |
1470 | 0 | collectSanitizerRuntimes(TC, Args, SharedRuntimes, StaticRuntimes, |
1471 | 0 | NonWholeStaticRuntimes, HelperStaticRuntimes, |
1472 | 0 | RequiredSymbols); |
1473 | 0 | } |
1474 | | |
1475 | | // Inject libfuzzer dependencies. |
1476 | 0 | if (SanArgs.needsFuzzer() && SanArgs.linkRuntimes() && |
1477 | 0 | !Args.hasArg(options::OPT_shared)) { |
1478 | |
|
1479 | 0 | addSanitizerRuntime(TC, Args, CmdArgs, "fuzzer", false, true); |
1480 | 0 | if (SanArgs.needsFuzzerInterceptors()) |
1481 | 0 | addSanitizerRuntime(TC, Args, CmdArgs, "fuzzer_interceptors", false, |
1482 | 0 | true); |
1483 | 0 | if (!Args.hasArg(clang::driver::options::OPT_nostdlibxx)) { |
1484 | 0 | bool OnlyLibstdcxxStatic = Args.hasArg(options::OPT_static_libstdcxx) && |
1485 | 0 | !Args.hasArg(options::OPT_static); |
1486 | 0 | if (OnlyLibstdcxxStatic) |
1487 | 0 | CmdArgs.push_back("-Bstatic"); |
1488 | 0 | TC.AddCXXStdlibLibArgs(Args, CmdArgs); |
1489 | 0 | if (OnlyLibstdcxxStatic) |
1490 | 0 | CmdArgs.push_back("-Bdynamic"); |
1491 | 0 | } |
1492 | 0 | } |
1493 | |
|
1494 | 0 | for (auto RT : SharedRuntimes) |
1495 | 0 | addSanitizerRuntime(TC, Args, CmdArgs, RT, true, false); |
1496 | 0 | for (auto RT : HelperStaticRuntimes) |
1497 | 0 | addSanitizerRuntime(TC, Args, CmdArgs, RT, false, true); |
1498 | 0 | bool AddExportDynamic = false; |
1499 | 0 | for (auto RT : StaticRuntimes) { |
1500 | 0 | addSanitizerRuntime(TC, Args, CmdArgs, RT, false, true); |
1501 | 0 | AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT); |
1502 | 0 | } |
1503 | 0 | for (auto RT : NonWholeStaticRuntimes) { |
1504 | 0 | addSanitizerRuntime(TC, Args, CmdArgs, RT, false, false); |
1505 | 0 | AddExportDynamic |= !addSanitizerDynamicList(TC, Args, CmdArgs, RT); |
1506 | 0 | } |
1507 | 0 | for (auto S : RequiredSymbols) { |
1508 | 0 | CmdArgs.push_back("-u"); |
1509 | 0 | CmdArgs.push_back(Args.MakeArgString(S)); |
1510 | 0 | } |
1511 | | // If there is a static runtime with no dynamic list, force all the symbols |
1512 | | // to be dynamic to be sure we export sanitizer interface functions. |
1513 | 0 | if (AddExportDynamic) |
1514 | 0 | CmdArgs.push_back("--export-dynamic"); |
1515 | |
|
1516 | 0 | if (SanArgs.hasCrossDsoCfi() && !AddExportDynamic) |
1517 | 0 | CmdArgs.push_back("--export-dynamic-symbol=__cfi_check"); |
1518 | |
|
1519 | 0 | if (SanArgs.hasMemTag()) { |
1520 | 0 | if (!TC.getTriple().isAndroid()) { |
1521 | 0 | TC.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
1522 | 0 | << "-fsanitize=memtag*" << TC.getTriple().str(); |
1523 | 0 | } |
1524 | 0 | CmdArgs.push_back( |
1525 | 0 | Args.MakeArgString("--android-memtag-mode=" + SanArgs.getMemtagMode())); |
1526 | 0 | if (SanArgs.hasMemtagHeap()) |
1527 | 0 | CmdArgs.push_back("--android-memtag-heap"); |
1528 | 0 | if (SanArgs.hasMemtagStack()) |
1529 | 0 | CmdArgs.push_back("--android-memtag-stack"); |
1530 | 0 | } |
1531 | |
|
1532 | 0 | return !StaticRuntimes.empty() || !NonWholeStaticRuntimes.empty(); |
1533 | 0 | } |
1534 | | |
1535 | 0 | bool tools::addXRayRuntime(const ToolChain&TC, const ArgList &Args, ArgStringList &CmdArgs) { |
1536 | 0 | if (Args.hasArg(options::OPT_shared)) |
1537 | 0 | return false; |
1538 | | |
1539 | 0 | if (TC.getXRayArgs().needsXRayRt()) { |
1540 | 0 | CmdArgs.push_back("--whole-archive"); |
1541 | 0 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, "xray")); |
1542 | 0 | for (const auto &Mode : TC.getXRayArgs().modeList()) |
1543 | 0 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, Mode)); |
1544 | 0 | CmdArgs.push_back("--no-whole-archive"); |
1545 | 0 | return true; |
1546 | 0 | } |
1547 | | |
1548 | 0 | return false; |
1549 | 0 | } |
1550 | | |
1551 | | void tools::linkXRayRuntimeDeps(const ToolChain &TC, |
1552 | | const llvm::opt::ArgList &Args, |
1553 | 0 | ArgStringList &CmdArgs) { |
1554 | 0 | addAsNeededOption(TC, Args, CmdArgs, false); |
1555 | 0 | CmdArgs.push_back("-lpthread"); |
1556 | 0 | if (!TC.getTriple().isOSOpenBSD()) |
1557 | 0 | CmdArgs.push_back("-lrt"); |
1558 | 0 | CmdArgs.push_back("-lm"); |
1559 | |
|
1560 | 0 | if (!TC.getTriple().isOSFreeBSD() && |
1561 | 0 | !TC.getTriple().isOSNetBSD() && |
1562 | 0 | !TC.getTriple().isOSOpenBSD()) |
1563 | 0 | CmdArgs.push_back("-ldl"); |
1564 | 0 | } |
1565 | | |
1566 | 0 | bool tools::areOptimizationsEnabled(const ArgList &Args) { |
1567 | | // Find the last -O arg and see if it is non-zero. |
1568 | 0 | if (Arg *A = Args.getLastArg(options::OPT_O_Group)) |
1569 | 0 | return !A->getOption().matches(options::OPT_O0); |
1570 | | // Defaults to -O0. |
1571 | 0 | return false; |
1572 | 0 | } |
1573 | | |
1574 | | const char *tools::SplitDebugName(const JobAction &JA, const ArgList &Args, |
1575 | | const InputInfo &Input, |
1576 | 0 | const InputInfo &Output) { |
1577 | 0 | auto AddPostfix = [JA](auto &F) { |
1578 | 0 | if (JA.getOffloadingDeviceKind() == Action::OFK_HIP) |
1579 | 0 | F += (Twine("_") + JA.getOffloadingArch()).str(); |
1580 | 0 | F += ".dwo"; |
1581 | 0 | }; |
1582 | 0 | if (Arg *A = Args.getLastArg(options::OPT_gsplit_dwarf_EQ)) |
1583 | 0 | if (StringRef(A->getValue()) == "single" && Output.isFilename()) |
1584 | 0 | return Args.MakeArgString(Output.getFilename()); |
1585 | | |
1586 | 0 | SmallString<128> T; |
1587 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_dumpdir)) { |
1588 | 0 | T = A->getValue(); |
1589 | 0 | } else { |
1590 | 0 | Arg *FinalOutput = Args.getLastArg(options::OPT_o, options::OPT__SLASH_o); |
1591 | 0 | if (FinalOutput && Args.hasArg(options::OPT_c)) { |
1592 | 0 | T = FinalOutput->getValue(); |
1593 | 0 | llvm::sys::path::remove_filename(T); |
1594 | 0 | llvm::sys::path::append(T, |
1595 | 0 | llvm::sys::path::stem(FinalOutput->getValue())); |
1596 | 0 | AddPostfix(T); |
1597 | 0 | return Args.MakeArgString(T); |
1598 | 0 | } |
1599 | 0 | } |
1600 | | |
1601 | 0 | T += llvm::sys::path::stem(Input.getBaseInput()); |
1602 | 0 | AddPostfix(T); |
1603 | 0 | return Args.MakeArgString(T); |
1604 | 0 | } |
1605 | | |
1606 | | void tools::SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T, |
1607 | | const JobAction &JA, const ArgList &Args, |
1608 | 0 | const InputInfo &Output, const char *OutFile) { |
1609 | 0 | ArgStringList ExtractArgs; |
1610 | 0 | ExtractArgs.push_back("--extract-dwo"); |
1611 | |
|
1612 | 0 | ArgStringList StripArgs; |
1613 | 0 | StripArgs.push_back("--strip-dwo"); |
1614 | | |
1615 | | // Grabbing the output of the earlier compile step. |
1616 | 0 | StripArgs.push_back(Output.getFilename()); |
1617 | 0 | ExtractArgs.push_back(Output.getFilename()); |
1618 | 0 | ExtractArgs.push_back(OutFile); |
1619 | |
|
1620 | 0 | const char *Exec = |
1621 | 0 | Args.MakeArgString(TC.GetProgramPath(CLANG_DEFAULT_OBJCOPY)); |
1622 | 0 | InputInfo II(types::TY_Object, Output.getFilename(), Output.getFilename()); |
1623 | | |
1624 | | // First extract the dwo sections. |
1625 | 0 | C.addCommand(std::make_unique<Command>(JA, T, |
1626 | 0 | ResponseFileSupport::AtFileCurCP(), |
1627 | 0 | Exec, ExtractArgs, II, Output)); |
1628 | | |
1629 | | // Then remove them from the original .o file. |
1630 | 0 | C.addCommand(std::make_unique<Command>( |
1631 | 0 | JA, T, ResponseFileSupport::AtFileCurCP(), Exec, StripArgs, II, Output)); |
1632 | 0 | } |
1633 | | |
1634 | | // Claim options we don't want to warn if they are unused. We do this for |
1635 | | // options that build systems might add but are unused when assembling or only |
1636 | | // running the preprocessor for example. |
1637 | 0 | void tools::claimNoWarnArgs(const ArgList &Args) { |
1638 | | // Don't warn about unused -f(no-)?lto. This can happen when we're |
1639 | | // preprocessing, precompiling or assembling. |
1640 | 0 | Args.ClaimAllArgs(options::OPT_flto_EQ); |
1641 | 0 | Args.ClaimAllArgs(options::OPT_flto); |
1642 | 0 | Args.ClaimAllArgs(options::OPT_fno_lto); |
1643 | 0 | } |
1644 | | |
1645 | 0 | Arg *tools::getLastCSProfileGenerateArg(const ArgList &Args) { |
1646 | 0 | auto *CSPGOGenerateArg = Args.getLastArg(options::OPT_fcs_profile_generate, |
1647 | 0 | options::OPT_fcs_profile_generate_EQ, |
1648 | 0 | options::OPT_fno_profile_generate); |
1649 | 0 | if (CSPGOGenerateArg && |
1650 | 0 | CSPGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate)) |
1651 | 0 | CSPGOGenerateArg = nullptr; |
1652 | |
|
1653 | 0 | return CSPGOGenerateArg; |
1654 | 0 | } |
1655 | | |
1656 | 0 | Arg *tools::getLastProfileUseArg(const ArgList &Args) { |
1657 | 0 | auto *ProfileUseArg = Args.getLastArg( |
1658 | 0 | options::OPT_fprofile_instr_use, options::OPT_fprofile_instr_use_EQ, |
1659 | 0 | options::OPT_fprofile_use, options::OPT_fprofile_use_EQ, |
1660 | 0 | options::OPT_fno_profile_instr_use); |
1661 | |
|
1662 | 0 | if (ProfileUseArg && |
1663 | 0 | ProfileUseArg->getOption().matches(options::OPT_fno_profile_instr_use)) |
1664 | 0 | ProfileUseArg = nullptr; |
1665 | |
|
1666 | 0 | return ProfileUseArg; |
1667 | 0 | } |
1668 | | |
1669 | 0 | Arg *tools::getLastProfileSampleUseArg(const ArgList &Args) { |
1670 | 0 | auto *ProfileSampleUseArg = Args.getLastArg( |
1671 | 0 | options::OPT_fprofile_sample_use, options::OPT_fprofile_sample_use_EQ, |
1672 | 0 | options::OPT_fauto_profile, options::OPT_fauto_profile_EQ, |
1673 | 0 | options::OPT_fno_profile_sample_use, options::OPT_fno_auto_profile); |
1674 | |
|
1675 | 0 | if (ProfileSampleUseArg && |
1676 | 0 | (ProfileSampleUseArg->getOption().matches( |
1677 | 0 | options::OPT_fno_profile_sample_use) || |
1678 | 0 | ProfileSampleUseArg->getOption().matches(options::OPT_fno_auto_profile))) |
1679 | 0 | return nullptr; |
1680 | | |
1681 | 0 | return Args.getLastArg(options::OPT_fprofile_sample_use_EQ, |
1682 | 0 | options::OPT_fauto_profile_EQ); |
1683 | 0 | } |
1684 | | |
1685 | 0 | const char *tools::RelocationModelName(llvm::Reloc::Model Model) { |
1686 | 0 | switch (Model) { |
1687 | 0 | case llvm::Reloc::Static: |
1688 | 0 | return "static"; |
1689 | 0 | case llvm::Reloc::PIC_: |
1690 | 0 | return "pic"; |
1691 | 0 | case llvm::Reloc::DynamicNoPIC: |
1692 | 0 | return "dynamic-no-pic"; |
1693 | 0 | case llvm::Reloc::ROPI: |
1694 | 0 | return "ropi"; |
1695 | 0 | case llvm::Reloc::RWPI: |
1696 | 0 | return "rwpi"; |
1697 | 0 | case llvm::Reloc::ROPI_RWPI: |
1698 | 0 | return "ropi-rwpi"; |
1699 | 0 | } |
1700 | 0 | llvm_unreachable("Unknown Reloc::Model kind"); |
1701 | 0 | } |
1702 | | |
1703 | | /// Parses the various -fpic/-fPIC/-fpie/-fPIE arguments. Then, |
1704 | | /// smooshes them together with platform defaults, to decide whether |
1705 | | /// this compile should be using PIC mode or not. Returns a tuple of |
1706 | | /// (RelocationModel, PICLevel, IsPIE). |
1707 | | std::tuple<llvm::Reloc::Model, unsigned, bool> |
1708 | 0 | tools::ParsePICArgs(const ToolChain &ToolChain, const ArgList &Args) { |
1709 | 0 | const llvm::Triple &EffectiveTriple = ToolChain.getEffectiveTriple(); |
1710 | 0 | const llvm::Triple &Triple = ToolChain.getTriple(); |
1711 | |
|
1712 | 0 | bool PIE = ToolChain.isPIEDefault(Args); |
1713 | 0 | bool PIC = PIE || ToolChain.isPICDefault(); |
1714 | | // The Darwin/MachO default to use PIC does not apply when using -static. |
1715 | 0 | if (Triple.isOSBinFormatMachO() && Args.hasArg(options::OPT_static)) |
1716 | 0 | PIE = PIC = false; |
1717 | 0 | bool IsPICLevelTwo = PIC; |
1718 | |
|
1719 | 0 | bool KernelOrKext = |
1720 | 0 | Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext); |
1721 | | |
1722 | | // Android-specific defaults for PIC/PIE |
1723 | 0 | if (Triple.isAndroid()) { |
1724 | 0 | switch (Triple.getArch()) { |
1725 | 0 | case llvm::Triple::arm: |
1726 | 0 | case llvm::Triple::armeb: |
1727 | 0 | case llvm::Triple::thumb: |
1728 | 0 | case llvm::Triple::thumbeb: |
1729 | 0 | case llvm::Triple::aarch64: |
1730 | 0 | case llvm::Triple::mips: |
1731 | 0 | case llvm::Triple::mipsel: |
1732 | 0 | case llvm::Triple::mips64: |
1733 | 0 | case llvm::Triple::mips64el: |
1734 | 0 | PIC = true; // "-fpic" |
1735 | 0 | break; |
1736 | | |
1737 | 0 | case llvm::Triple::x86: |
1738 | 0 | case llvm::Triple::x86_64: |
1739 | 0 | PIC = true; // "-fPIC" |
1740 | 0 | IsPICLevelTwo = true; |
1741 | 0 | break; |
1742 | | |
1743 | 0 | default: |
1744 | 0 | break; |
1745 | 0 | } |
1746 | 0 | } |
1747 | | |
1748 | | // OHOS-specific defaults for PIC/PIE |
1749 | 0 | if (Triple.isOHOSFamily() && Triple.getArch() == llvm::Triple::aarch64) |
1750 | 0 | PIC = true; |
1751 | | |
1752 | | // OpenBSD-specific defaults for PIE |
1753 | 0 | if (Triple.isOSOpenBSD()) { |
1754 | 0 | switch (ToolChain.getArch()) { |
1755 | 0 | case llvm::Triple::arm: |
1756 | 0 | case llvm::Triple::aarch64: |
1757 | 0 | case llvm::Triple::mips64: |
1758 | 0 | case llvm::Triple::mips64el: |
1759 | 0 | case llvm::Triple::x86: |
1760 | 0 | case llvm::Triple::x86_64: |
1761 | 0 | IsPICLevelTwo = false; // "-fpie" |
1762 | 0 | break; |
1763 | | |
1764 | 0 | case llvm::Triple::ppc: |
1765 | 0 | case llvm::Triple::sparcv9: |
1766 | 0 | IsPICLevelTwo = true; // "-fPIE" |
1767 | 0 | break; |
1768 | | |
1769 | 0 | default: |
1770 | 0 | break; |
1771 | 0 | } |
1772 | 0 | } |
1773 | | |
1774 | | // The last argument relating to either PIC or PIE wins, and no |
1775 | | // other argument is used. If the last argument is any flavor of the |
1776 | | // '-fno-...' arguments, both PIC and PIE are disabled. Any PIE |
1777 | | // option implicitly enables PIC at the same level. |
1778 | 0 | Arg *LastPICArg = Args.getLastArg(options::OPT_fPIC, options::OPT_fno_PIC, |
1779 | 0 | options::OPT_fpic, options::OPT_fno_pic, |
1780 | 0 | options::OPT_fPIE, options::OPT_fno_PIE, |
1781 | 0 | options::OPT_fpie, options::OPT_fno_pie); |
1782 | 0 | if (Triple.isOSWindows() && !Triple.isOSCygMing() && LastPICArg && |
1783 | 0 | LastPICArg == Args.getLastArg(options::OPT_fPIC, options::OPT_fpic, |
1784 | 0 | options::OPT_fPIE, options::OPT_fpie)) { |
1785 | 0 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
1786 | 0 | << LastPICArg->getSpelling() << Triple.str(); |
1787 | 0 | if (Triple.getArch() == llvm::Triple::x86_64) |
1788 | 0 | return std::make_tuple(llvm::Reloc::PIC_, 2U, false); |
1789 | 0 | return std::make_tuple(llvm::Reloc::Static, 0U, false); |
1790 | 0 | } |
1791 | | |
1792 | | // Check whether the tool chain trumps the PIC-ness decision. If the PIC-ness |
1793 | | // is forced, then neither PIC nor PIE flags will have no effect. |
1794 | 0 | if (!ToolChain.isPICDefaultForced()) { |
1795 | 0 | if (LastPICArg) { |
1796 | 0 | Option O = LastPICArg->getOption(); |
1797 | 0 | if (O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic) || |
1798 | 0 | O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie)) { |
1799 | 0 | PIE = O.matches(options::OPT_fPIE) || O.matches(options::OPT_fpie); |
1800 | 0 | PIC = |
1801 | 0 | PIE || O.matches(options::OPT_fPIC) || O.matches(options::OPT_fpic); |
1802 | 0 | IsPICLevelTwo = |
1803 | 0 | O.matches(options::OPT_fPIE) || O.matches(options::OPT_fPIC); |
1804 | 0 | } else { |
1805 | 0 | PIE = PIC = false; |
1806 | 0 | if (EffectiveTriple.isPS()) { |
1807 | 0 | Arg *ModelArg = Args.getLastArg(options::OPT_mcmodel_EQ); |
1808 | 0 | StringRef Model = ModelArg ? ModelArg->getValue() : ""; |
1809 | 0 | if (Model != "kernel") { |
1810 | 0 | PIC = true; |
1811 | 0 | ToolChain.getDriver().Diag(diag::warn_drv_ps_force_pic) |
1812 | 0 | << LastPICArg->getSpelling() |
1813 | 0 | << (EffectiveTriple.isPS4() ? "PS4" : "PS5"); |
1814 | 0 | } |
1815 | 0 | } |
1816 | 0 | } |
1817 | 0 | } |
1818 | 0 | } |
1819 | | |
1820 | | // Introduce a Darwin and PS4/PS5-specific hack. If the default is PIC, but |
1821 | | // the PIC level would've been set to level 1, force it back to level 2 PIC |
1822 | | // instead. |
1823 | 0 | if (PIC && (Triple.isOSDarwin() || EffectiveTriple.isPS())) |
1824 | 0 | IsPICLevelTwo |= ToolChain.isPICDefault(); |
1825 | | |
1826 | | // This kernel flags are a trump-card: they will disable PIC/PIE |
1827 | | // generation, independent of the argument order. |
1828 | 0 | if (KernelOrKext && |
1829 | 0 | ((!EffectiveTriple.isiOS() || EffectiveTriple.isOSVersionLT(6)) && |
1830 | 0 | !EffectiveTriple.isWatchOS() && !EffectiveTriple.isDriverKit())) |
1831 | 0 | PIC = PIE = false; |
1832 | |
|
1833 | 0 | if (Arg *A = Args.getLastArg(options::OPT_mdynamic_no_pic)) { |
1834 | | // This is a very special mode. It trumps the other modes, almost no one |
1835 | | // uses it, and it isn't even valid on any OS but Darwin. |
1836 | 0 | if (!Triple.isOSDarwin()) |
1837 | 0 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
1838 | 0 | << A->getSpelling() << Triple.str(); |
1839 | | |
1840 | | // FIXME: Warn when this flag trumps some other PIC or PIE flag. |
1841 | | |
1842 | | // Only a forced PIC mode can cause the actual compile to have PIC defines |
1843 | | // etc., no flags are sufficient. This behavior was selected to closely |
1844 | | // match that of llvm-gcc and Apple GCC before that. |
1845 | 0 | PIC = ToolChain.isPICDefault() && ToolChain.isPICDefaultForced(); |
1846 | |
|
1847 | 0 | return std::make_tuple(llvm::Reloc::DynamicNoPIC, PIC ? 2U : 0U, false); |
1848 | 0 | } |
1849 | | |
1850 | 0 | bool EmbeddedPISupported; |
1851 | 0 | switch (Triple.getArch()) { |
1852 | 0 | case llvm::Triple::arm: |
1853 | 0 | case llvm::Triple::armeb: |
1854 | 0 | case llvm::Triple::thumb: |
1855 | 0 | case llvm::Triple::thumbeb: |
1856 | 0 | EmbeddedPISupported = true; |
1857 | 0 | break; |
1858 | 0 | default: |
1859 | 0 | EmbeddedPISupported = false; |
1860 | 0 | break; |
1861 | 0 | } |
1862 | | |
1863 | 0 | bool ROPI = false, RWPI = false; |
1864 | 0 | Arg* LastROPIArg = Args.getLastArg(options::OPT_fropi, options::OPT_fno_ropi); |
1865 | 0 | if (LastROPIArg && LastROPIArg->getOption().matches(options::OPT_fropi)) { |
1866 | 0 | if (!EmbeddedPISupported) |
1867 | 0 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
1868 | 0 | << LastROPIArg->getSpelling() << Triple.str(); |
1869 | 0 | ROPI = true; |
1870 | 0 | } |
1871 | 0 | Arg *LastRWPIArg = Args.getLastArg(options::OPT_frwpi, options::OPT_fno_rwpi); |
1872 | 0 | if (LastRWPIArg && LastRWPIArg->getOption().matches(options::OPT_frwpi)) { |
1873 | 0 | if (!EmbeddedPISupported) |
1874 | 0 | ToolChain.getDriver().Diag(diag::err_drv_unsupported_opt_for_target) |
1875 | 0 | << LastRWPIArg->getSpelling() << Triple.str(); |
1876 | 0 | RWPI = true; |
1877 | 0 | } |
1878 | | |
1879 | | // ROPI and RWPI are not compatible with PIC or PIE. |
1880 | 0 | if ((ROPI || RWPI) && (PIC || PIE)) |
1881 | 0 | ToolChain.getDriver().Diag(diag::err_drv_ropi_rwpi_incompatible_with_pic); |
1882 | |
|
1883 | 0 | if (Triple.isMIPS()) { |
1884 | 0 | StringRef CPUName; |
1885 | 0 | StringRef ABIName; |
1886 | 0 | mips::getMipsCPUAndABI(Args, Triple, CPUName, ABIName); |
1887 | | // When targeting the N64 ABI, PIC is the default, except in the case |
1888 | | // when the -mno-abicalls option is used. In that case we exit |
1889 | | // at next check regardless of PIC being set below. |
1890 | 0 | if (ABIName == "n64") |
1891 | 0 | PIC = true; |
1892 | | // When targettng MIPS with -mno-abicalls, it's always static. |
1893 | 0 | if(Args.hasArg(options::OPT_mno_abicalls)) |
1894 | 0 | return std::make_tuple(llvm::Reloc::Static, 0U, false); |
1895 | | // Unlike other architectures, MIPS, even with -fPIC/-mxgot/multigot, |
1896 | | // does not use PIC level 2 for historical reasons. |
1897 | 0 | IsPICLevelTwo = false; |
1898 | 0 | } |
1899 | | |
1900 | 0 | if (PIC) |
1901 | 0 | return std::make_tuple(llvm::Reloc::PIC_, IsPICLevelTwo ? 2U : 1U, PIE); |
1902 | | |
1903 | 0 | llvm::Reloc::Model RelocM = llvm::Reloc::Static; |
1904 | 0 | if (ROPI && RWPI) |
1905 | 0 | RelocM = llvm::Reloc::ROPI_RWPI; |
1906 | 0 | else if (ROPI) |
1907 | 0 | RelocM = llvm::Reloc::ROPI; |
1908 | 0 | else if (RWPI) |
1909 | 0 | RelocM = llvm::Reloc::RWPI; |
1910 | |
|
1911 | 0 | return std::make_tuple(RelocM, 0U, false); |
1912 | 0 | } |
1913 | | |
1914 | | // `-falign-functions` indicates that the functions should be aligned to a |
1915 | | // 16-byte boundary. |
1916 | | // |
1917 | | // `-falign-functions=1` is the same as `-fno-align-functions`. |
1918 | | // |
1919 | | // The scalar `n` in `-falign-functions=n` must be an integral value between |
1920 | | // [0, 65536]. If the value is not a power-of-two, it will be rounded up to |
1921 | | // the nearest power-of-two. |
1922 | | // |
1923 | | // If we return `0`, the frontend will default to the backend's preferred |
1924 | | // alignment. |
1925 | | // |
1926 | | // NOTE: icc only allows values between [0, 4096]. icc uses `-falign-functions` |
1927 | | // to mean `-falign-functions=16`. GCC defaults to the backend's preferred |
1928 | | // alignment. For unaligned functions, we default to the backend's preferred |
1929 | | // alignment. |
1930 | | unsigned tools::ParseFunctionAlignment(const ToolChain &TC, |
1931 | 0 | const ArgList &Args) { |
1932 | 0 | const Arg *A = Args.getLastArg(options::OPT_falign_functions, |
1933 | 0 | options::OPT_falign_functions_EQ, |
1934 | 0 | options::OPT_fno_align_functions); |
1935 | 0 | if (!A || A->getOption().matches(options::OPT_fno_align_functions)) |
1936 | 0 | return 0; |
1937 | | |
1938 | 0 | if (A->getOption().matches(options::OPT_falign_functions)) |
1939 | 0 | return 0; |
1940 | | |
1941 | 0 | unsigned Value = 0; |
1942 | 0 | if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 65536) |
1943 | 0 | TC.getDriver().Diag(diag::err_drv_invalid_int_value) |
1944 | 0 | << A->getAsString(Args) << A->getValue(); |
1945 | 0 | return Value ? llvm::Log2_32_Ceil(std::min(Value, 65536u)) : Value; |
1946 | 0 | } |
1947 | | |
1948 | | void tools::addDebugInfoKind( |
1949 | 0 | ArgStringList &CmdArgs, llvm::codegenoptions::DebugInfoKind DebugInfoKind) { |
1950 | 0 | switch (DebugInfoKind) { |
1951 | 0 | case llvm::codegenoptions::DebugDirectivesOnly: |
1952 | 0 | CmdArgs.push_back("-debug-info-kind=line-directives-only"); |
1953 | 0 | break; |
1954 | 0 | case llvm::codegenoptions::DebugLineTablesOnly: |
1955 | 0 | CmdArgs.push_back("-debug-info-kind=line-tables-only"); |
1956 | 0 | break; |
1957 | 0 | case llvm::codegenoptions::DebugInfoConstructor: |
1958 | 0 | CmdArgs.push_back("-debug-info-kind=constructor"); |
1959 | 0 | break; |
1960 | 0 | case llvm::codegenoptions::LimitedDebugInfo: |
1961 | 0 | CmdArgs.push_back("-debug-info-kind=limited"); |
1962 | 0 | break; |
1963 | 0 | case llvm::codegenoptions::FullDebugInfo: |
1964 | 0 | CmdArgs.push_back("-debug-info-kind=standalone"); |
1965 | 0 | break; |
1966 | 0 | case llvm::codegenoptions::UnusedTypeInfo: |
1967 | 0 | CmdArgs.push_back("-debug-info-kind=unused-types"); |
1968 | 0 | break; |
1969 | 0 | default: |
1970 | 0 | break; |
1971 | 0 | } |
1972 | 0 | } |
1973 | | |
1974 | | // Convert an arg of the form "-gN" or "-ggdbN" or one of their aliases |
1975 | | // to the corresponding DebugInfoKind. |
1976 | 0 | llvm::codegenoptions::DebugInfoKind tools::debugLevelToInfoKind(const Arg &A) { |
1977 | 0 | assert(A.getOption().matches(options::OPT_gN_Group) && |
1978 | 0 | "Not a -g option that specifies a debug-info level"); |
1979 | 0 | if (A.getOption().matches(options::OPT_g0) || |
1980 | 0 | A.getOption().matches(options::OPT_ggdb0)) |
1981 | 0 | return llvm::codegenoptions::NoDebugInfo; |
1982 | 0 | if (A.getOption().matches(options::OPT_gline_tables_only) || |
1983 | 0 | A.getOption().matches(options::OPT_ggdb1)) |
1984 | 0 | return llvm::codegenoptions::DebugLineTablesOnly; |
1985 | 0 | if (A.getOption().matches(options::OPT_gline_directives_only)) |
1986 | 0 | return llvm::codegenoptions::DebugDirectivesOnly; |
1987 | 0 | return llvm::codegenoptions::DebugInfoConstructor; |
1988 | 0 | } |
1989 | | |
1990 | | static unsigned ParseDebugDefaultVersion(const ToolChain &TC, |
1991 | 0 | const ArgList &Args) { |
1992 | 0 | const Arg *A = Args.getLastArg(options::OPT_fdebug_default_version); |
1993 | |
|
1994 | 0 | if (!A) |
1995 | 0 | return 0; |
1996 | | |
1997 | 0 | unsigned Value = 0; |
1998 | 0 | if (StringRef(A->getValue()).getAsInteger(10, Value) || Value > 5 || |
1999 | 0 | Value < 2) |
2000 | 0 | TC.getDriver().Diag(diag::err_drv_invalid_int_value) |
2001 | 0 | << A->getAsString(Args) << A->getValue(); |
2002 | 0 | return Value; |
2003 | 0 | } |
2004 | | |
2005 | 0 | unsigned tools::DwarfVersionNum(StringRef ArgValue) { |
2006 | 0 | return llvm::StringSwitch<unsigned>(ArgValue) |
2007 | 0 | .Case("-gdwarf-2", 2) |
2008 | 0 | .Case("-gdwarf-3", 3) |
2009 | 0 | .Case("-gdwarf-4", 4) |
2010 | 0 | .Case("-gdwarf-5", 5) |
2011 | 0 | .Default(0); |
2012 | 0 | } |
2013 | | |
2014 | 0 | const Arg *tools::getDwarfNArg(const ArgList &Args) { |
2015 | 0 | return Args.getLastArg(options::OPT_gdwarf_2, options::OPT_gdwarf_3, |
2016 | 0 | options::OPT_gdwarf_4, options::OPT_gdwarf_5, |
2017 | 0 | options::OPT_gdwarf); |
2018 | 0 | } |
2019 | | |
2020 | | unsigned tools::getDwarfVersion(const ToolChain &TC, |
2021 | 0 | const llvm::opt::ArgList &Args) { |
2022 | 0 | unsigned DwarfVersion = ParseDebugDefaultVersion(TC, Args); |
2023 | 0 | if (const Arg *GDwarfN = getDwarfNArg(Args)) |
2024 | 0 | if (int N = DwarfVersionNum(GDwarfN->getSpelling())) |
2025 | 0 | DwarfVersion = N; |
2026 | 0 | if (DwarfVersion == 0) { |
2027 | 0 | DwarfVersion = TC.GetDefaultDwarfVersion(); |
2028 | 0 | assert(DwarfVersion && "toolchain default DWARF version must be nonzero"); |
2029 | 0 | } |
2030 | 0 | return DwarfVersion; |
2031 | 0 | } |
2032 | | |
2033 | | void tools::AddAssemblerKPIC(const ToolChain &ToolChain, const ArgList &Args, |
2034 | 0 | ArgStringList &CmdArgs) { |
2035 | 0 | llvm::Reloc::Model RelocationModel; |
2036 | 0 | unsigned PICLevel; |
2037 | 0 | bool IsPIE; |
2038 | 0 | std::tie(RelocationModel, PICLevel, IsPIE) = ParsePICArgs(ToolChain, Args); |
2039 | |
|
2040 | 0 | if (RelocationModel != llvm::Reloc::Static) |
2041 | 0 | CmdArgs.push_back("-KPIC"); |
2042 | 0 | } |
2043 | | |
2044 | | /// Determine whether Objective-C automated reference counting is |
2045 | | /// enabled. |
2046 | 0 | bool tools::isObjCAutoRefCount(const ArgList &Args) { |
2047 | 0 | return Args.hasFlag(options::OPT_fobjc_arc, options::OPT_fno_objc_arc, false); |
2048 | 0 | } |
2049 | | |
2050 | | enum class LibGccType { UnspecifiedLibGcc, StaticLibGcc, SharedLibGcc }; |
2051 | | |
2052 | | static LibGccType getLibGccType(const ToolChain &TC, const Driver &D, |
2053 | 0 | const ArgList &Args) { |
2054 | 0 | if (Args.hasArg(options::OPT_static_libgcc) || |
2055 | 0 | Args.hasArg(options::OPT_static) || Args.hasArg(options::OPT_static_pie) || |
2056 | | // The Android NDK only provides libunwind.a, not libunwind.so. |
2057 | 0 | TC.getTriple().isAndroid()) |
2058 | 0 | return LibGccType::StaticLibGcc; |
2059 | 0 | if (Args.hasArg(options::OPT_shared_libgcc)) |
2060 | 0 | return LibGccType::SharedLibGcc; |
2061 | 0 | return LibGccType::UnspecifiedLibGcc; |
2062 | 0 | } |
2063 | | |
2064 | | // Gcc adds libgcc arguments in various ways: |
2065 | | // |
2066 | | // gcc <none>: -lgcc --as-needed -lgcc_s --no-as-needed |
2067 | | // g++ <none>: -lgcc_s -lgcc |
2068 | | // gcc shared: -lgcc_s -lgcc |
2069 | | // g++ shared: -lgcc_s -lgcc |
2070 | | // gcc static: -lgcc -lgcc_eh |
2071 | | // g++ static: -lgcc -lgcc_eh |
2072 | | // gcc static-pie: -lgcc -lgcc_eh |
2073 | | // g++ static-pie: -lgcc -lgcc_eh |
2074 | | // |
2075 | | // Also, certain targets need additional adjustments. |
2076 | | |
2077 | | static void AddUnwindLibrary(const ToolChain &TC, const Driver &D, |
2078 | 0 | ArgStringList &CmdArgs, const ArgList &Args) { |
2079 | 0 | ToolChain::UnwindLibType UNW = TC.GetUnwindLibType(Args); |
2080 | | // By default OHOS binaries are linked statically to libunwind. |
2081 | 0 | if (TC.getTriple().isOHOSFamily() && UNW == ToolChain::UNW_CompilerRT) { |
2082 | 0 | CmdArgs.push_back("-l:libunwind.a"); |
2083 | 0 | return; |
2084 | 0 | } |
2085 | | |
2086 | | // Targets that don't use unwind libraries. |
2087 | 0 | if ((TC.getTriple().isAndroid() && UNW == ToolChain::UNW_Libgcc) || |
2088 | 0 | TC.getTriple().isOSIAMCU() || TC.getTriple().isOSBinFormatWasm() || |
2089 | 0 | TC.getTriple().isWindowsMSVCEnvironment() || UNW == ToolChain::UNW_None) |
2090 | 0 | return; |
2091 | | |
2092 | 0 | LibGccType LGT = getLibGccType(TC, D, Args); |
2093 | 0 | bool AsNeeded = LGT == LibGccType::UnspecifiedLibGcc && |
2094 | 0 | (UNW == ToolChain::UNW_CompilerRT || !D.CCCIsCXX()) && |
2095 | 0 | !TC.getTriple().isAndroid() && |
2096 | 0 | !TC.getTriple().isOSCygMing() && !TC.getTriple().isOSAIX(); |
2097 | 0 | if (AsNeeded) |
2098 | 0 | addAsNeededOption(TC, Args, CmdArgs, true); |
2099 | |
|
2100 | 0 | switch (UNW) { |
2101 | 0 | case ToolChain::UNW_None: |
2102 | 0 | return; |
2103 | 0 | case ToolChain::UNW_Libgcc: { |
2104 | 0 | if (LGT == LibGccType::StaticLibGcc) |
2105 | 0 | CmdArgs.push_back("-lgcc_eh"); |
2106 | 0 | else |
2107 | 0 | CmdArgs.push_back("-lgcc_s"); |
2108 | 0 | break; |
2109 | 0 | } |
2110 | 0 | case ToolChain::UNW_CompilerRT: |
2111 | 0 | if (TC.getTriple().isOSAIX()) { |
2112 | | // AIX only has libunwind as a shared library. So do not pass |
2113 | | // anything in if -static is specified. |
2114 | 0 | if (LGT != LibGccType::StaticLibGcc) |
2115 | 0 | CmdArgs.push_back("-lunwind"); |
2116 | 0 | } else if (LGT == LibGccType::StaticLibGcc) { |
2117 | 0 | CmdArgs.push_back("-l:libunwind.a"); |
2118 | 0 | } else if (LGT == LibGccType::SharedLibGcc) { |
2119 | 0 | if (TC.getTriple().isOSCygMing()) |
2120 | 0 | CmdArgs.push_back("-l:libunwind.dll.a"); |
2121 | 0 | else |
2122 | 0 | CmdArgs.push_back("-l:libunwind.so"); |
2123 | 0 | } else { |
2124 | | // Let the linker choose between libunwind.so and libunwind.a |
2125 | | // depending on what's available, and depending on the -static flag |
2126 | 0 | CmdArgs.push_back("-lunwind"); |
2127 | 0 | } |
2128 | 0 | break; |
2129 | 0 | } |
2130 | | |
2131 | 0 | if (AsNeeded) |
2132 | 0 | addAsNeededOption(TC, Args, CmdArgs, false); |
2133 | 0 | } |
2134 | | |
2135 | | static void AddLibgcc(const ToolChain &TC, const Driver &D, |
2136 | 0 | ArgStringList &CmdArgs, const ArgList &Args) { |
2137 | 0 | LibGccType LGT = getLibGccType(TC, D, Args); |
2138 | 0 | if (LGT == LibGccType::StaticLibGcc || |
2139 | 0 | (LGT == LibGccType::UnspecifiedLibGcc && !D.CCCIsCXX())) |
2140 | 0 | CmdArgs.push_back("-lgcc"); |
2141 | 0 | AddUnwindLibrary(TC, D, CmdArgs, Args); |
2142 | 0 | if (LGT == LibGccType::SharedLibGcc || |
2143 | 0 | (LGT == LibGccType::UnspecifiedLibGcc && D.CCCIsCXX())) |
2144 | 0 | CmdArgs.push_back("-lgcc"); |
2145 | 0 | } |
2146 | | |
2147 | | void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D, |
2148 | 0 | ArgStringList &CmdArgs, const ArgList &Args) { |
2149 | | // Make use of compiler-rt if --rtlib option is used |
2150 | 0 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args); |
2151 | |
|
2152 | 0 | switch (RLT) { |
2153 | 0 | case ToolChain::RLT_CompilerRT: |
2154 | 0 | CmdArgs.push_back(TC.getCompilerRTArgString(Args, "builtins")); |
2155 | 0 | AddUnwindLibrary(TC, D, CmdArgs, Args); |
2156 | 0 | break; |
2157 | 0 | case ToolChain::RLT_Libgcc: |
2158 | | // Make sure libgcc is not used under MSVC environment by default |
2159 | 0 | if (TC.getTriple().isKnownWindowsMSVCEnvironment()) { |
2160 | | // Issue error diagnostic if libgcc is explicitly specified |
2161 | | // through command line as --rtlib option argument. |
2162 | 0 | Arg *A = Args.getLastArg(options::OPT_rtlib_EQ); |
2163 | 0 | if (A && A->getValue() != StringRef("platform")) { |
2164 | 0 | TC.getDriver().Diag(diag::err_drv_unsupported_rtlib_for_platform) |
2165 | 0 | << A->getValue() << "MSVC"; |
2166 | 0 | } |
2167 | 0 | } else |
2168 | 0 | AddLibgcc(TC, D, CmdArgs, Args); |
2169 | 0 | break; |
2170 | 0 | } |
2171 | | |
2172 | | // On Android, the unwinder uses dl_iterate_phdr (or one of |
2173 | | // dl_unwind_find_exidx/__gnu_Unwind_Find_exidx on arm32) from libdl.so. For |
2174 | | // statically-linked executables, these functions come from libc.a instead. |
2175 | 0 | if (TC.getTriple().isAndroid() && !Args.hasArg(options::OPT_static) && |
2176 | 0 | !Args.hasArg(options::OPT_static_pie)) |
2177 | 0 | CmdArgs.push_back("-ldl"); |
2178 | 0 | } |
2179 | | |
2180 | | SmallString<128> tools::getStatsFileName(const llvm::opt::ArgList &Args, |
2181 | | const InputInfo &Output, |
2182 | | const InputInfo &Input, |
2183 | 0 | const Driver &D) { |
2184 | 0 | const Arg *A = Args.getLastArg(options::OPT_save_stats_EQ); |
2185 | 0 | if (!A && !D.CCPrintInternalStats) |
2186 | 0 | return {}; |
2187 | | |
2188 | 0 | SmallString<128> StatsFile; |
2189 | 0 | if (A) { |
2190 | 0 | StringRef SaveStats = A->getValue(); |
2191 | 0 | if (SaveStats == "obj" && Output.isFilename()) { |
2192 | 0 | StatsFile.assign(Output.getFilename()); |
2193 | 0 | llvm::sys::path::remove_filename(StatsFile); |
2194 | 0 | } else if (SaveStats != "cwd") { |
2195 | 0 | D.Diag(diag::err_drv_invalid_value) << A->getAsString(Args) << SaveStats; |
2196 | 0 | return {}; |
2197 | 0 | } |
2198 | | |
2199 | 0 | StringRef BaseName = llvm::sys::path::filename(Input.getBaseInput()); |
2200 | 0 | llvm::sys::path::append(StatsFile, BaseName); |
2201 | 0 | llvm::sys::path::replace_extension(StatsFile, "stats"); |
2202 | 0 | } else { |
2203 | 0 | assert(D.CCPrintInternalStats); |
2204 | 0 | StatsFile.assign(D.CCPrintInternalStatReportFilename.empty() |
2205 | 0 | ? "-" |
2206 | 0 | : D.CCPrintInternalStatReportFilename); |
2207 | 0 | } |
2208 | 0 | return StatsFile; |
2209 | 0 | } |
2210 | | |
2211 | | void tools::addMultilibFlag(bool Enabled, const StringRef Flag, |
2212 | 0 | Multilib::flags_list &Flags) { |
2213 | 0 | assert(Flag.front() == '-'); |
2214 | 0 | if (Enabled) { |
2215 | 0 | Flags.push_back(Flag.str()); |
2216 | 0 | } else { |
2217 | 0 | Flags.push_back(("!" + Flag.substr(1)).str()); |
2218 | 0 | } |
2219 | 0 | } |
2220 | | |
2221 | | void tools::addX86AlignBranchArgs(const Driver &D, const ArgList &Args, |
2222 | | ArgStringList &CmdArgs, bool IsLTO, |
2223 | 0 | const StringRef PluginOptPrefix) { |
2224 | 0 | auto addArg = [&, IsLTO](const Twine &Arg) { |
2225 | 0 | if (IsLTO) { |
2226 | 0 | assert(!PluginOptPrefix.empty() && "Cannot have empty PluginOptPrefix!"); |
2227 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + Arg)); |
2228 | 0 | } else { |
2229 | 0 | CmdArgs.push_back("-mllvm"); |
2230 | 0 | CmdArgs.push_back(Args.MakeArgString(Arg)); |
2231 | 0 | } |
2232 | 0 | }; |
2233 | |
|
2234 | 0 | if (Args.hasArg(options::OPT_mbranches_within_32B_boundaries)) { |
2235 | 0 | addArg(Twine("-x86-branches-within-32B-boundaries")); |
2236 | 0 | } |
2237 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_malign_branch_boundary_EQ)) { |
2238 | 0 | StringRef Value = A->getValue(); |
2239 | 0 | unsigned Boundary; |
2240 | 0 | if (Value.getAsInteger(10, Boundary) || Boundary < 16 || |
2241 | 0 | !llvm::isPowerOf2_64(Boundary)) { |
2242 | 0 | D.Diag(diag::err_drv_invalid_argument_to_option) |
2243 | 0 | << Value << A->getOption().getName(); |
2244 | 0 | } else { |
2245 | 0 | addArg("-x86-align-branch-boundary=" + Twine(Boundary)); |
2246 | 0 | } |
2247 | 0 | } |
2248 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_malign_branch_EQ)) { |
2249 | 0 | std::string AlignBranch; |
2250 | 0 | for (StringRef T : A->getValues()) { |
2251 | 0 | if (T != "fused" && T != "jcc" && T != "jmp" && T != "call" && |
2252 | 0 | T != "ret" && T != "indirect") |
2253 | 0 | D.Diag(diag::err_drv_invalid_malign_branch_EQ) |
2254 | 0 | << T << "fused, jcc, jmp, call, ret, indirect"; |
2255 | 0 | if (!AlignBranch.empty()) |
2256 | 0 | AlignBranch += '+'; |
2257 | 0 | AlignBranch += T; |
2258 | 0 | } |
2259 | 0 | addArg("-x86-align-branch=" + Twine(AlignBranch)); |
2260 | 0 | } |
2261 | 0 | if (const Arg *A = Args.getLastArg(options::OPT_mpad_max_prefix_size_EQ)) { |
2262 | 0 | StringRef Value = A->getValue(); |
2263 | 0 | unsigned PrefixSize; |
2264 | 0 | if (Value.getAsInteger(10, PrefixSize)) { |
2265 | 0 | D.Diag(diag::err_drv_invalid_argument_to_option) |
2266 | 0 | << Value << A->getOption().getName(); |
2267 | 0 | } else { |
2268 | 0 | addArg("-x86-pad-max-prefix-size=" + Twine(PrefixSize)); |
2269 | 0 | } |
2270 | 0 | } |
2271 | 0 | } |
2272 | | |
2273 | | /// SDLSearch: Search for Static Device Library |
2274 | | /// The search for SDL bitcode files is consistent with how static host |
2275 | | /// libraries are discovered. That is, the -l option triggers a search for |
2276 | | /// files in a set of directories called the LINKPATH. The host library search |
2277 | | /// procedure looks for a specific filename in the LINKPATH. The filename for |
2278 | | /// a host library is lib<libname>.a or lib<libname>.so. For SDLs, there is an |
2279 | | /// ordered-set of filenames that are searched. We call this ordered-set of |
2280 | | /// filenames as SEARCH-ORDER. Since an SDL can either be device-type specific, |
2281 | | /// architecture specific, or generic across all architectures, a naming |
2282 | | /// convention and search order is used where the file name embeds the |
2283 | | /// architecture name <arch-name> (nvptx or amdgcn) and the GPU device type |
2284 | | /// <device-name> such as sm_30 and gfx906. <device-name> is absent in case of |
2285 | | /// device-independent SDLs. To reduce congestion in host library directories, |
2286 | | /// the search first looks for files in the “libdevice” subdirectory. SDLs that |
2287 | | /// are bc files begin with the prefix “lib”. |
2288 | | /// |
2289 | | /// Machine-code SDLs can also be managed as an archive (*.a file). The |
2290 | | /// convention has been to use the prefix “lib”. To avoid confusion with host |
2291 | | /// archive libraries, we use prefix "libbc-" for the bitcode SDL archives. |
2292 | | /// |
2293 | | static bool SDLSearch(const Driver &D, const llvm::opt::ArgList &DriverArgs, |
2294 | | llvm::opt::ArgStringList &CC1Args, |
2295 | | const SmallVectorImpl<std::string> &LibraryPaths, |
2296 | | StringRef Lib, StringRef Arch, StringRef Target, |
2297 | 0 | bool isBitCodeSDL) { |
2298 | 0 | SmallVector<std::string, 12> SDLs; |
2299 | |
|
2300 | 0 | std::string LibDeviceLoc = "/libdevice"; |
2301 | 0 | std::string LibBcPrefix = "/libbc-"; |
2302 | 0 | std::string LibPrefix = "/lib"; |
2303 | |
|
2304 | 0 | if (isBitCodeSDL) { |
2305 | | // SEARCH-ORDER for Bitcode SDLs: |
2306 | | // libdevice/libbc-<libname>-<arch-name>-<device-type>.a |
2307 | | // libbc-<libname>-<arch-name>-<device-type>.a |
2308 | | // libdevice/libbc-<libname>-<arch-name>.a |
2309 | | // libbc-<libname>-<arch-name>.a |
2310 | | // libdevice/libbc-<libname>.a |
2311 | | // libbc-<libname>.a |
2312 | | // libdevice/lib<libname>-<arch-name>-<device-type>.bc |
2313 | | // lib<libname>-<arch-name>-<device-type>.bc |
2314 | | // libdevice/lib<libname>-<arch-name>.bc |
2315 | | // lib<libname>-<arch-name>.bc |
2316 | | // libdevice/lib<libname>.bc |
2317 | | // lib<libname>.bc |
2318 | |
|
2319 | 0 | for (StringRef Base : {LibBcPrefix, LibPrefix}) { |
2320 | 0 | const auto *Ext = Base.contains(LibBcPrefix) ? ".a" : ".bc"; |
2321 | |
|
2322 | 0 | for (auto Suffix : {Twine(Lib + "-" + Arch + "-" + Target).str(), |
2323 | 0 | Twine(Lib + "-" + Arch).str(), Twine(Lib).str()}) { |
2324 | 0 | SDLs.push_back(Twine(LibDeviceLoc + Base + Suffix + Ext).str()); |
2325 | 0 | SDLs.push_back(Twine(Base + Suffix + Ext).str()); |
2326 | 0 | } |
2327 | 0 | } |
2328 | 0 | } else { |
2329 | | // SEARCH-ORDER for Machine-code SDLs: |
2330 | | // libdevice/lib<libname>-<arch-name>-<device-type>.a |
2331 | | // lib<libname>-<arch-name>-<device-type>.a |
2332 | | // libdevice/lib<libname>-<arch-name>.a |
2333 | | // lib<libname>-<arch-name>.a |
2334 | |
|
2335 | 0 | const auto *Ext = ".a"; |
2336 | |
|
2337 | 0 | for (auto Suffix : {Twine(Lib + "-" + Arch + "-" + Target).str(), |
2338 | 0 | Twine(Lib + "-" + Arch).str()}) { |
2339 | 0 | SDLs.push_back(Twine(LibDeviceLoc + LibPrefix + Suffix + Ext).str()); |
2340 | 0 | SDLs.push_back(Twine(LibPrefix + Suffix + Ext).str()); |
2341 | 0 | } |
2342 | 0 | } |
2343 | | |
2344 | | // The CUDA toolchain does not use a global device llvm-link before the LLVM |
2345 | | // backend generates ptx. So currently, the use of bitcode SDL for nvptx is |
2346 | | // only possible with post-clang-cc1 linking. Clang cc1 has a feature that |
2347 | | // will link libraries after clang compilation while the LLVM IR is still in |
2348 | | // memory. This utilizes a clang cc1 option called “-mlink-builtin-bitcode”. |
2349 | | // This is a clang -cc1 option that is generated by the clang driver. The |
2350 | | // option value must a full path to an existing file. |
2351 | 0 | bool FoundSDL = false; |
2352 | 0 | for (auto LPath : LibraryPaths) { |
2353 | 0 | for (auto SDL : SDLs) { |
2354 | 0 | auto FullName = Twine(LPath + SDL).str(); |
2355 | 0 | if (llvm::sys::fs::exists(FullName)) { |
2356 | 0 | CC1Args.push_back(DriverArgs.MakeArgString(FullName)); |
2357 | 0 | FoundSDL = true; |
2358 | 0 | break; |
2359 | 0 | } |
2360 | 0 | } |
2361 | 0 | if (FoundSDL) |
2362 | 0 | break; |
2363 | 0 | } |
2364 | 0 | return FoundSDL; |
2365 | 0 | } |
2366 | | |
2367 | | /// Search if a user provided archive file lib<libname>.a exists in any of |
2368 | | /// the library paths. If so, add a new command to clang-offload-bundler to |
2369 | | /// unbundle this archive and create a temporary device specific archive. Name |
2370 | | /// of this SDL is passed to the llvm-link tool. |
2371 | | static void GetSDLFromOffloadArchive( |
2372 | | Compilation &C, const Driver &D, const Tool &T, const JobAction &JA, |
2373 | | const InputInfoList &Inputs, const llvm::opt::ArgList &DriverArgs, |
2374 | | llvm::opt::ArgStringList &CC1Args, |
2375 | | const SmallVectorImpl<std::string> &LibraryPaths, StringRef Lib, |
2376 | 0 | StringRef Arch, StringRef Target, bool isBitCodeSDL) { |
2377 | | |
2378 | | // We don't support bitcode archive bundles for nvptx |
2379 | 0 | if (isBitCodeSDL && Arch.contains("nvptx")) |
2380 | 0 | return; |
2381 | | |
2382 | 0 | bool FoundAOB = false; |
2383 | 0 | std::string ArchiveOfBundles; |
2384 | |
|
2385 | 0 | llvm::Triple Triple(D.getTargetTriple()); |
2386 | 0 | bool IsMSVC = Triple.isWindowsMSVCEnvironment(); |
2387 | 0 | auto Ext = IsMSVC ? ".lib" : ".a"; |
2388 | 0 | if (!Lib.starts_with(":") && !Lib.starts_with("-l")) { |
2389 | 0 | if (llvm::sys::fs::exists(Lib)) { |
2390 | 0 | ArchiveOfBundles = Lib; |
2391 | 0 | FoundAOB = true; |
2392 | 0 | } |
2393 | 0 | } else { |
2394 | 0 | Lib.consume_front("-l"); |
2395 | 0 | for (auto LPath : LibraryPaths) { |
2396 | 0 | ArchiveOfBundles.clear(); |
2397 | 0 | auto LibFile = (Lib.starts_with(":") ? Lib.drop_front() |
2398 | 0 | : IsMSVC ? Lib + Ext |
2399 | 0 | : "lib" + Lib + Ext) |
2400 | 0 | .str(); |
2401 | 0 | for (auto Prefix : {"/libdevice/", "/"}) { |
2402 | 0 | auto AOB = Twine(LPath + Prefix + LibFile).str(); |
2403 | 0 | if (llvm::sys::fs::exists(AOB)) { |
2404 | 0 | ArchiveOfBundles = AOB; |
2405 | 0 | FoundAOB = true; |
2406 | 0 | break; |
2407 | 0 | } |
2408 | 0 | } |
2409 | 0 | if (FoundAOB) |
2410 | 0 | break; |
2411 | 0 | } |
2412 | 0 | } |
2413 | |
|
2414 | 0 | if (!FoundAOB) |
2415 | 0 | return; |
2416 | | |
2417 | 0 | llvm::file_magic Magic; |
2418 | 0 | auto EC = llvm::identify_magic(ArchiveOfBundles, Magic); |
2419 | 0 | if (EC || Magic != llvm::file_magic::archive) |
2420 | 0 | return; |
2421 | | |
2422 | 0 | StringRef Prefix = isBitCodeSDL ? "libbc-" : "lib"; |
2423 | 0 | std::string OutputLib = |
2424 | 0 | D.GetTemporaryPath(Twine(Prefix + llvm::sys::path::filename(Lib) + "-" + |
2425 | 0 | Arch + "-" + Target) |
2426 | 0 | .str(), |
2427 | 0 | "a"); |
2428 | |
|
2429 | 0 | C.addTempFile(C.getArgs().MakeArgString(OutputLib)); |
2430 | |
|
2431 | 0 | ArgStringList CmdArgs; |
2432 | 0 | SmallString<128> DeviceTriple; |
2433 | 0 | DeviceTriple += Action::GetOffloadKindName(JA.getOffloadingDeviceKind()); |
2434 | 0 | DeviceTriple += '-'; |
2435 | 0 | std::string NormalizedTriple = T.getToolChain().getTriple().normalize(); |
2436 | 0 | DeviceTriple += NormalizedTriple; |
2437 | 0 | if (!Target.empty()) { |
2438 | 0 | DeviceTriple += '-'; |
2439 | 0 | DeviceTriple += Target; |
2440 | 0 | } |
2441 | |
|
2442 | 0 | std::string UnbundleArg("-unbundle"); |
2443 | 0 | std::string TypeArg("-type=a"); |
2444 | 0 | std::string InputArg("-input=" + ArchiveOfBundles); |
2445 | 0 | std::string OffloadArg("-targets=" + std::string(DeviceTriple)); |
2446 | 0 | std::string OutputArg("-output=" + OutputLib); |
2447 | |
|
2448 | 0 | const char *UBProgram = DriverArgs.MakeArgString( |
2449 | 0 | T.getToolChain().GetProgramPath("clang-offload-bundler")); |
2450 | |
|
2451 | 0 | ArgStringList UBArgs; |
2452 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(UnbundleArg)); |
2453 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(TypeArg)); |
2454 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(InputArg)); |
2455 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(OffloadArg)); |
2456 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(OutputArg)); |
2457 | | |
2458 | | // Add this flag to not exit from clang-offload-bundler if no compatible |
2459 | | // code object is found in heterogenous archive library. |
2460 | 0 | std::string AdditionalArgs("-allow-missing-bundles"); |
2461 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(AdditionalArgs)); |
2462 | | |
2463 | | // Add this flag to treat hip and hipv4 offload kinds as compatible with |
2464 | | // openmp offload kind while extracting code objects from a heterogenous |
2465 | | // archive library. Vice versa is also considered compatible. |
2466 | 0 | std::string HipCompatibleArgs("-hip-openmp-compatible"); |
2467 | 0 | UBArgs.push_back(C.getArgs().MakeArgString(HipCompatibleArgs)); |
2468 | |
|
2469 | 0 | C.addCommand(std::make_unique<Command>( |
2470 | 0 | JA, T, ResponseFileSupport::AtFileCurCP(), UBProgram, UBArgs, Inputs, |
2471 | 0 | InputInfo(&JA, C.getArgs().MakeArgString(OutputLib)))); |
2472 | |
|
2473 | 0 | CC1Args.push_back(DriverArgs.MakeArgString(OutputLib)); |
2474 | |
|
2475 | 0 | return; |
2476 | 0 | } |
2477 | | |
2478 | | // Wrapper function used by driver for adding SDLs during link phase. |
2479 | | void tools::AddStaticDeviceLibsLinking(Compilation &C, const Tool &T, |
2480 | | const JobAction &JA, |
2481 | | const InputInfoList &Inputs, |
2482 | | const llvm::opt::ArgList &DriverArgs, |
2483 | | llvm::opt::ArgStringList &CC1Args, |
2484 | | StringRef Arch, StringRef Target, |
2485 | 0 | bool isBitCodeSDL) { |
2486 | 0 | AddStaticDeviceLibs(&C, &T, &JA, &Inputs, C.getDriver(), DriverArgs, CC1Args, |
2487 | 0 | Arch, Target, isBitCodeSDL); |
2488 | 0 | } |
2489 | | |
2490 | | // User defined Static Device Libraries(SDLs) can be passed to clang for |
2491 | | // offloading GPU compilers. Like static host libraries, the use of a SDL is |
2492 | | // specified with the -l command line option. The primary difference between |
2493 | | // host and SDLs is the filenames for SDLs (refer SEARCH-ORDER for Bitcode SDLs |
2494 | | // and SEARCH-ORDER for Machine-code SDLs for the naming convention). |
2495 | | // SDLs are of following types: |
2496 | | // |
2497 | | // * Bitcode SDLs: They can either be a *.bc file or an archive of *.bc files. |
2498 | | // For NVPTX, these libraries are post-clang linked following each |
2499 | | // compilation. For AMDGPU, these libraries are linked one time |
2500 | | // during the application link phase. |
2501 | | // |
2502 | | // * Machine-code SDLs: They are archive files. For AMDGPU, the process for |
2503 | | // machine code SDLs is still in development. But they will be linked |
2504 | | // by the LLVM tool lld. |
2505 | | // |
2506 | | // * Bundled objects that contain both host and device codes: Bundled objects |
2507 | | // may also contain library code compiled from source. For NVPTX, the |
2508 | | // bundle contains cubin. For AMDGPU, the bundle contains bitcode. |
2509 | | // |
2510 | | // For Bitcode and Machine-code SDLs, current compiler toolchains hardcode the |
2511 | | // inclusion of specific SDLs such as math libraries and the OpenMP device |
2512 | | // library libomptarget. |
2513 | | void tools::AddStaticDeviceLibs(Compilation *C, const Tool *T, |
2514 | | const JobAction *JA, |
2515 | | const InputInfoList *Inputs, const Driver &D, |
2516 | | const llvm::opt::ArgList &DriverArgs, |
2517 | | llvm::opt::ArgStringList &CC1Args, |
2518 | | StringRef Arch, StringRef Target, |
2519 | 0 | bool isBitCodeSDL) { |
2520 | |
|
2521 | 0 | SmallVector<std::string, 8> LibraryPaths; |
2522 | | // Add search directories from LIBRARY_PATH env variable |
2523 | 0 | std::optional<std::string> LibPath = |
2524 | 0 | llvm::sys::Process::GetEnv("LIBRARY_PATH"); |
2525 | 0 | if (LibPath) { |
2526 | 0 | SmallVector<StringRef, 8> Frags; |
2527 | 0 | const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; |
2528 | 0 | llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr); |
2529 | 0 | for (StringRef Path : Frags) |
2530 | 0 | LibraryPaths.emplace_back(Path.trim()); |
2531 | 0 | } |
2532 | | |
2533 | | // Add directories from user-specified -L options |
2534 | 0 | for (std::string Search_Dir : DriverArgs.getAllArgValues(options::OPT_L)) |
2535 | 0 | LibraryPaths.emplace_back(Search_Dir); |
2536 | | |
2537 | | // Add path to lib-debug folders |
2538 | 0 | SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(D.Dir); |
2539 | 0 | llvm::sys::path::append(DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME); |
2540 | 0 | LibraryPaths.emplace_back(DefaultLibPath.c_str()); |
2541 | | |
2542 | | // Build list of Static Device Libraries SDLs specified by -l option |
2543 | 0 | llvm::SmallSet<std::string, 16> SDLNames; |
2544 | 0 | static const StringRef HostOnlyArchives[] = { |
2545 | 0 | "omp", "cudart", "m", "gcc", "gcc_s", "pthread", "hip_hcc"}; |
2546 | 0 | for (auto SDLName : DriverArgs.getAllArgValues(options::OPT_l)) { |
2547 | 0 | if (!llvm::is_contained(HostOnlyArchives, SDLName)) { |
2548 | 0 | SDLNames.insert(std::string("-l") + SDLName); |
2549 | 0 | } |
2550 | 0 | } |
2551 | |
|
2552 | 0 | for (auto Input : DriverArgs.getAllArgValues(options::OPT_INPUT)) { |
2553 | 0 | auto FileName = StringRef(Input); |
2554 | | // Clang treats any unknown file types as archives and passes them to the |
2555 | | // linker. Files with extension 'lib' are classified as TY_Object by clang |
2556 | | // but they are usually archives. It is OK if the file is not really an |
2557 | | // archive since GetSDLFromOffloadArchive will check the magic of the file |
2558 | | // and only unbundle it if it is really an archive. |
2559 | 0 | const StringRef LibFileExt = ".lib"; |
2560 | 0 | if (!llvm::sys::path::has_extension(FileName) || |
2561 | 0 | types::lookupTypeForExtension( |
2562 | 0 | llvm::sys::path::extension(FileName).drop_front()) == |
2563 | 0 | types::TY_INVALID || |
2564 | 0 | llvm::sys::path::extension(FileName) == LibFileExt) |
2565 | 0 | SDLNames.insert(Input); |
2566 | 0 | } |
2567 | | |
2568 | | // The search stops as soon as an SDL file is found. The driver then provides |
2569 | | // the full filename of the SDL to the llvm-link command. If no SDL is found |
2570 | | // after searching each LINKPATH with SEARCH-ORDER, it is possible that an |
2571 | | // archive file lib<libname>.a exists and may contain bundled object files. |
2572 | 0 | for (auto SDLName : SDLNames) { |
2573 | | // This is the only call to SDLSearch |
2574 | 0 | if (!SDLSearch(D, DriverArgs, CC1Args, LibraryPaths, SDLName, Arch, Target, |
2575 | 0 | isBitCodeSDL)) { |
2576 | 0 | GetSDLFromOffloadArchive(*C, D, *T, *JA, *Inputs, DriverArgs, CC1Args, |
2577 | 0 | LibraryPaths, SDLName, Arch, Target, |
2578 | 0 | isBitCodeSDL); |
2579 | 0 | } |
2580 | 0 | } |
2581 | 0 | } |
2582 | | |
2583 | | static llvm::opt::Arg * |
2584 | 0 | getAMDGPUCodeObjectArgument(const Driver &D, const llvm::opt::ArgList &Args) { |
2585 | 0 | return Args.getLastArg(options::OPT_mcode_object_version_EQ); |
2586 | 0 | } |
2587 | | |
2588 | | void tools::checkAMDGPUCodeObjectVersion(const Driver &D, |
2589 | 0 | const llvm::opt::ArgList &Args) { |
2590 | 0 | const unsigned MinCodeObjVer = 4; |
2591 | 0 | const unsigned MaxCodeObjVer = 5; |
2592 | |
|
2593 | 0 | if (auto *CodeObjArg = getAMDGPUCodeObjectArgument(D, Args)) { |
2594 | 0 | if (CodeObjArg->getOption().getID() == |
2595 | 0 | options::OPT_mcode_object_version_EQ) { |
2596 | 0 | unsigned CodeObjVer = MaxCodeObjVer; |
2597 | 0 | auto Remnant = |
2598 | 0 | StringRef(CodeObjArg->getValue()).getAsInteger(0, CodeObjVer); |
2599 | 0 | if (Remnant || CodeObjVer < MinCodeObjVer || CodeObjVer > MaxCodeObjVer) |
2600 | 0 | D.Diag(diag::err_drv_invalid_int_value) |
2601 | 0 | << CodeObjArg->getAsString(Args) << CodeObjArg->getValue(); |
2602 | 0 | } |
2603 | 0 | } |
2604 | 0 | } |
2605 | | |
2606 | | unsigned tools::getAMDGPUCodeObjectVersion(const Driver &D, |
2607 | 0 | const llvm::opt::ArgList &Args) { |
2608 | 0 | unsigned CodeObjVer = 4; // default |
2609 | 0 | if (auto *CodeObjArg = getAMDGPUCodeObjectArgument(D, Args)) |
2610 | 0 | StringRef(CodeObjArg->getValue()).getAsInteger(0, CodeObjVer); |
2611 | 0 | return CodeObjVer; |
2612 | 0 | } |
2613 | | |
2614 | | bool tools::haveAMDGPUCodeObjectVersionArgument( |
2615 | 0 | const Driver &D, const llvm::opt::ArgList &Args) { |
2616 | 0 | return getAMDGPUCodeObjectArgument(D, Args) != nullptr; |
2617 | 0 | } |
2618 | | |
2619 | | void tools::addMachineOutlinerArgs(const Driver &D, |
2620 | | const llvm::opt::ArgList &Args, |
2621 | | llvm::opt::ArgStringList &CmdArgs, |
2622 | | const llvm::Triple &Triple, bool IsLTO, |
2623 | 0 | const StringRef PluginOptPrefix) { |
2624 | 0 | auto addArg = [&, IsLTO](const Twine &Arg) { |
2625 | 0 | if (IsLTO) { |
2626 | 0 | assert(!PluginOptPrefix.empty() && "Cannot have empty PluginOptPrefix!"); |
2627 | 0 | CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + Arg)); |
2628 | 0 | } else { |
2629 | 0 | CmdArgs.push_back("-mllvm"); |
2630 | 0 | CmdArgs.push_back(Args.MakeArgString(Arg)); |
2631 | 0 | } |
2632 | 0 | }; |
2633 | |
|
2634 | 0 | if (Arg *A = Args.getLastArg(options::OPT_moutline, |
2635 | 0 | options::OPT_mno_outline)) { |
2636 | 0 | if (A->getOption().matches(options::OPT_moutline)) { |
2637 | | // We only support -moutline in AArch64 and ARM targets right now. If |
2638 | | // we're not compiling for these, emit a warning and ignore the flag. |
2639 | | // Otherwise, add the proper mllvm flags. |
2640 | 0 | if (!(Triple.isARM() || Triple.isThumb() || Triple.isAArch64())) { |
2641 | 0 | D.Diag(diag::warn_drv_moutline_unsupported_opt) << Triple.getArchName(); |
2642 | 0 | } else { |
2643 | 0 | addArg(Twine("-enable-machine-outliner")); |
2644 | 0 | } |
2645 | 0 | } else { |
2646 | | // Disable all outlining behaviour. |
2647 | 0 | addArg(Twine("-enable-machine-outliner=never")); |
2648 | 0 | } |
2649 | 0 | } |
2650 | 0 | } |
2651 | | |
2652 | | void tools::addOpenMPDeviceRTL(const Driver &D, |
2653 | | const llvm::opt::ArgList &DriverArgs, |
2654 | | llvm::opt::ArgStringList &CC1Args, |
2655 | | StringRef BitcodeSuffix, |
2656 | 0 | const llvm::Triple &Triple) { |
2657 | 0 | SmallVector<StringRef, 8> LibraryPaths; |
2658 | | |
2659 | | // Add path to clang lib / lib64 folder. |
2660 | 0 | SmallString<256> DefaultLibPath = llvm::sys::path::parent_path(D.Dir); |
2661 | 0 | llvm::sys::path::append(DefaultLibPath, CLANG_INSTALL_LIBDIR_BASENAME); |
2662 | 0 | LibraryPaths.emplace_back(DefaultLibPath.c_str()); |
2663 | | |
2664 | | // Add user defined library paths from LIBRARY_PATH. |
2665 | 0 | std::optional<std::string> LibPath = |
2666 | 0 | llvm::sys::Process::GetEnv("LIBRARY_PATH"); |
2667 | 0 | if (LibPath) { |
2668 | 0 | SmallVector<StringRef, 8> Frags; |
2669 | 0 | const char EnvPathSeparatorStr[] = {llvm::sys::EnvPathSeparator, '\0'}; |
2670 | 0 | llvm::SplitString(*LibPath, Frags, EnvPathSeparatorStr); |
2671 | 0 | for (StringRef Path : Frags) |
2672 | 0 | LibraryPaths.emplace_back(Path.trim()); |
2673 | 0 | } |
2674 | |
|
2675 | 0 | OptSpecifier LibomptargetBCPathOpt = |
2676 | 0 | Triple.isAMDGCN() ? options::OPT_libomptarget_amdgpu_bc_path_EQ |
2677 | 0 | : options::OPT_libomptarget_nvptx_bc_path_EQ; |
2678 | |
|
2679 | 0 | StringRef ArchPrefix = Triple.isAMDGCN() ? "amdgpu" : "nvptx"; |
2680 | 0 | std::string LibOmpTargetName = |
2681 | 0 | ("libomptarget-" + ArchPrefix + "-" + BitcodeSuffix + ".bc").str(); |
2682 | | |
2683 | | // First check whether user specifies bc library |
2684 | 0 | if (const Arg *A = DriverArgs.getLastArg(LibomptargetBCPathOpt)) { |
2685 | 0 | SmallString<128> LibOmpTargetFile(A->getValue()); |
2686 | 0 | if (llvm::sys::fs::exists(LibOmpTargetFile) && |
2687 | 0 | llvm::sys::fs::is_directory(LibOmpTargetFile)) { |
2688 | 0 | llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName); |
2689 | 0 | } |
2690 | |
|
2691 | 0 | if (llvm::sys::fs::exists(LibOmpTargetFile)) { |
2692 | 0 | CC1Args.push_back("-mlink-builtin-bitcode"); |
2693 | 0 | CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile)); |
2694 | 0 | } else { |
2695 | 0 | D.Diag(diag::err_drv_omp_offload_target_bcruntime_not_found) |
2696 | 0 | << LibOmpTargetFile; |
2697 | 0 | } |
2698 | 0 | } else { |
2699 | 0 | bool FoundBCLibrary = false; |
2700 | |
|
2701 | 0 | for (StringRef LibraryPath : LibraryPaths) { |
2702 | 0 | SmallString<128> LibOmpTargetFile(LibraryPath); |
2703 | 0 | llvm::sys::path::append(LibOmpTargetFile, LibOmpTargetName); |
2704 | 0 | if (llvm::sys::fs::exists(LibOmpTargetFile)) { |
2705 | 0 | CC1Args.push_back("-mlink-builtin-bitcode"); |
2706 | 0 | CC1Args.push_back(DriverArgs.MakeArgString(LibOmpTargetFile)); |
2707 | 0 | FoundBCLibrary = true; |
2708 | 0 | break; |
2709 | 0 | } |
2710 | 0 | } |
2711 | |
|
2712 | 0 | if (!FoundBCLibrary) |
2713 | 0 | D.Diag(diag::err_drv_omp_offload_target_missingbcruntime) |
2714 | 0 | << LibOmpTargetName << ArchPrefix; |
2715 | 0 | } |
2716 | 0 | } |
2717 | | void tools::addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C, |
2718 | | const llvm::opt::ArgList &Args, |
2719 | 0 | llvm::opt::ArgStringList &CmdArgs) { |
2720 | 0 | if ((C.getActiveOffloadKinds() & Action::OFK_HIP) && |
2721 | 0 | !Args.hasArg(options::OPT_nostdlib) && |
2722 | 0 | !Args.hasArg(options::OPT_no_hip_rt)) { |
2723 | 0 | TC.AddHIPRuntimeLibArgs(Args, CmdArgs); |
2724 | 0 | } else { |
2725 | | // Claim "no HIP libraries" arguments if any |
2726 | 0 | for (auto *Arg : Args.filtered(options::OPT_no_hip_rt)) { |
2727 | 0 | Arg->claim(); |
2728 | 0 | } |
2729 | 0 | } |
2730 | 0 | } |