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