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