| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "clang/Driver/Driver.h" |
| 10 | #include "InputInfo.h" |
| 11 | #include "ToolChains/AMDGPU.h" |
| 12 | #include "ToolChains/AVR.h" |
| 13 | #include "ToolChains/Ananas.h" |
| 14 | #include "ToolChains/BareMetal.h" |
| 15 | #include "ToolChains/Clang.h" |
| 16 | #include "ToolChains/CloudABI.h" |
| 17 | #include "ToolChains/Contiki.h" |
| 18 | #include "ToolChains/CrossWindows.h" |
| 19 | #include "ToolChains/Cuda.h" |
| 20 | #include "ToolChains/Darwin.h" |
| 21 | #include "ToolChains/DragonFly.h" |
| 22 | #include "ToolChains/FreeBSD.h" |
| 23 | #include "ToolChains/Fuchsia.h" |
| 24 | #include "ToolChains/Gnu.h" |
| 25 | #include "ToolChains/HIP.h" |
| 26 | #include "ToolChains/Haiku.h" |
| 27 | #include "ToolChains/Hexagon.h" |
| 28 | #include "ToolChains/Hurd.h" |
| 29 | #include "ToolChains/Lanai.h" |
| 30 | #include "ToolChains/Linux.h" |
| 31 | #include "ToolChains/MSP430.h" |
| 32 | #include "ToolChains/MSVC.h" |
| 33 | #include "ToolChains/MinGW.h" |
| 34 | #include "ToolChains/Minix.h" |
| 35 | #include "ToolChains/MipsLinux.h" |
| 36 | #include "ToolChains/Myriad.h" |
| 37 | #include "ToolChains/NaCl.h" |
| 38 | #include "ToolChains/NetBSD.h" |
| 39 | #include "ToolChains/OpenBSD.h" |
| 40 | #include "ToolChains/PS4CPU.h" |
| 41 | #include "ToolChains/RISCVToolchain.h" |
| 42 | #include "ToolChains/Solaris.h" |
| 43 | #include "ToolChains/TCE.h" |
| 44 | #include "ToolChains/WebAssembly.h" |
| 45 | #include "ToolChains/XCore.h" |
| 46 | #include "clang/Basic/Version.h" |
| 47 | #include "clang/Config/config.h" |
| 48 | #include "clang/Driver/Action.h" |
| 49 | #include "clang/Driver/Compilation.h" |
| 50 | #include "clang/Driver/DriverDiagnostic.h" |
| 51 | #include "clang/Driver/Job.h" |
| 52 | #include "clang/Driver/Options.h" |
| 53 | #include "clang/Driver/SanitizerArgs.h" |
| 54 | #include "clang/Driver/Tool.h" |
| 55 | #include "clang/Driver/ToolChain.h" |
| 56 | #include "llvm/ADT/ArrayRef.h" |
| 57 | #include "llvm/ADT/STLExtras.h" |
| 58 | #include "llvm/ADT/SmallSet.h" |
| 59 | #include "llvm/ADT/StringExtras.h" |
| 60 | #include "llvm/ADT/StringSet.h" |
| 61 | #include "llvm/ADT/StringSwitch.h" |
| 62 | #include "llvm/Config/llvm-config.h" |
| 63 | #include "llvm/Option/Arg.h" |
| 64 | #include "llvm/Option/ArgList.h" |
| 65 | #include "llvm/Option/OptSpecifier.h" |
| 66 | #include "llvm/Option/OptTable.h" |
| 67 | #include "llvm/Option/Option.h" |
| 68 | #include "llvm/Support/CommandLine.h" |
| 69 | #include "llvm/Support/ErrorHandling.h" |
| 70 | #include "llvm/Support/FileSystem.h" |
| 71 | #include "llvm/Support/FormatVariadic.h" |
| 72 | #include "llvm/Support/Path.h" |
| 73 | #include "llvm/Support/PrettyStackTrace.h" |
| 74 | #include "llvm/Support/Process.h" |
| 75 | #include "llvm/Support/Program.h" |
| 76 | #include "llvm/Support/StringSaver.h" |
| 77 | #include "llvm/Support/TargetRegistry.h" |
| 78 | #include "llvm/Support/VirtualFileSystem.h" |
| 79 | #include "llvm/Support/raw_ostream.h" |
| 80 | #include <map> |
| 81 | #include <memory> |
| 82 | #include <utility> |
| 83 | #if LLVM_ON_UNIX |
| 84 | #include <unistd.h> |
| 85 | #include <sysexits.h> |
| 86 | #endif |
| 87 | |
| 88 | using namespace clang::driver; |
| 89 | using namespace clang; |
| 90 | using namespace llvm::opt; |
| 91 | |
| 92 | |
| 93 | std::string Driver::GetResourcesPath(StringRef BinaryPath, |
| 94 | StringRef CustomResourceDir) { |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | std::string Dir = llvm::sys::path::parent_path(BinaryPath); |
| 101 | |
| 102 | SmallString<128> P(Dir); |
| 103 | if (CustomResourceDir != "") { |
| 104 | llvm::sys::path::append(P, CustomResourceDir); |
| 105 | } else { |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | P = llvm::sys::path::parent_path(Dir); |
| 112 | llvm::sys::path::append(P, Twine("lib") + CLANG_LIBDIR_SUFFIX, "clang", |
| 113 | CLANG_VERSION_STRING); |
| 114 | } |
| 115 | |
| 116 | return P.str(); |
| 117 | } |
| 118 | |
| 119 | Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple, |
| 120 | DiagnosticsEngine &Diags, |
| 121 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) |
| 122 | : Opts(createDriverOptTable()), Diags(Diags), VFS(std::move(VFS)), |
| 123 | Mode(GCCMode), SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone), |
| 124 | LTOMode(LTOK_None), ClangExecutable(ClangExecutable), |
| 125 | SysRoot(DEFAULT_SYSROOT), DriverTitle("clang LLVM compiler"), |
| 126 | CCPrintOptionsFilename(nullptr), CCPrintHeadersFilename(nullptr), |
| 127 | CCLogDiagnosticsFilename(nullptr), CCCPrintBindings(false), |
| 128 | CCPrintOptions(false), CCPrintHeaders(false), CCLogDiagnostics(false), |
| 129 | CCGenDiagnostics(false), TargetTriple(TargetTriple), |
| 130 | CCCGenericGCCName(""), Saver(Alloc), CheckInputsExist(true), |
| 131 | GenReproducer(false), SuppressMissingInputWarning(false) { |
| 132 | |
| 133 | |
| 134 | if (!this->VFS) |
| 135 | this->VFS = llvm::vfs::getRealFileSystem(); |
| 136 | |
| 137 | Name = llvm::sys::path::filename(ClangExecutable); |
| 138 | Dir = llvm::sys::path::parent_path(ClangExecutable); |
| 139 | InstalledDir = Dir; |
| 140 | |
| 141 | #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR) |
| 142 | SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR; |
| 143 | #endif |
| 144 | #if defined(CLANG_CONFIG_FILE_USER_DIR) |
| 145 | UserConfigDir = CLANG_CONFIG_FILE_USER_DIR; |
| 146 | #endif |
| 147 | |
| 148 | |
| 149 | ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR); |
| 150 | } |
| 151 | |
| 152 | void Driver::ParseDriverMode(StringRef ProgramName, |
| 153 | ArrayRef<const char *> Args) { |
| 154 | if (ClangNameParts.isEmpty()) |
| 155 | ClangNameParts = ToolChain::getTargetAndModeFromProgramName(ProgramName); |
| 156 | setDriverModeFromOption(ClangNameParts.DriverMode); |
| 157 | |
| 158 | for (const char *ArgPtr : Args) { |
| 159 | |
| 160 | if (ArgPtr == nullptr) |
| 161 | continue; |
| 162 | const StringRef Arg = ArgPtr; |
| 163 | setDriverModeFromOption(Arg); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void Driver::setDriverModeFromOption(StringRef Opt) { |
| 168 | const std::string OptName = |
| 169 | getOpts().getOption(options::OPT_driver_mode).getPrefixedName(); |
| 170 | if (!Opt.startswith(OptName)) |
| 171 | return; |
| 172 | StringRef Value = Opt.drop_front(OptName.size()); |
| 173 | |
| 174 | if (auto M = llvm::StringSwitch<llvm::Optional<DriverMode>>(Value) |
| 175 | .Case("gcc", GCCMode) |
| 176 | .Case("g++", GXXMode) |
| 177 | .Case("cpp", CPPMode) |
| 178 | .Case("cl", CLMode) |
| 179 | .Default(None)) |
| 180 | Mode = *M; |
| 181 | else |
| 182 | Diag(diag::err_drv_unsupported_option_argument) << OptName << Value; |
| 183 | } |
| 184 | |
| 185 | InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, |
| 186 | bool IsClCompatMode, |
| 187 | bool &ContainsError) { |
| 188 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing"); |
| 189 | ContainsError = false; |
| 190 | |
| 191 | unsigned IncludedFlagsBitmask; |
| 192 | unsigned ExcludedFlagsBitmask; |
| 193 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
| 194 | getIncludeExcludeOptionFlagMasks(IsClCompatMode); |
| 195 | |
| 196 | unsigned MissingArgIndex, MissingArgCount; |
| 197 | InputArgList Args = |
| 198 | getOpts().ParseArgs(ArgStrings, MissingArgIndex, MissingArgCount, |
| 199 | IncludedFlagsBitmask, ExcludedFlagsBitmask); |
| 200 | |
| 201 | |
| 202 | if (MissingArgCount) { |
| 203 | Diag(diag::err_drv_missing_argument) |
| 204 | << Args.getArgString(MissingArgIndex) << MissingArgCount; |
| 205 | ContainsError |= |
| 206 | Diags.getDiagnosticLevel(diag::err_drv_missing_argument, |
| 207 | SourceLocation()) > DiagnosticsEngine::Warning; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | for (const Arg *A : Args) { |
| 212 | if (A->getOption().hasFlag(options::Unsupported)) { |
| 213 | unsigned DiagID; |
| 214 | auto ArgString = A->getAsString(Args); |
| 215 | std::string Nearest; |
| 216 | if (getOpts().findNearest( |
| 217 | ArgString, Nearest, IncludedFlagsBitmask, |
| 218 | ExcludedFlagsBitmask | options::Unsupported) > 1) { |
| 219 | DiagID = diag::err_drv_unsupported_opt; |
| 220 | Diag(DiagID) << ArgString; |
| 221 | } else { |
| 222 | DiagID = diag::err_drv_unsupported_opt_with_suggestion; |
| 223 | Diag(DiagID) << ArgString << Nearest; |
| 224 | } |
| 225 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
| 226 | DiagnosticsEngine::Warning; |
| 227 | continue; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | if (A->getOption().matches(options::OPT_mcpu_EQ) && A->containsValue("")) { |
| 232 | Diag(diag::warn_drv_empty_joined_argument) << A->getAsString(Args); |
| 233 | ContainsError |= Diags.getDiagnosticLevel( |
| 234 | diag::warn_drv_empty_joined_argument, |
| 235 | SourceLocation()) > DiagnosticsEngine::Warning; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | for (const Arg *A : Args.filtered(options::OPT_UNKNOWN)) { |
| 240 | unsigned DiagID; |
| 241 | auto ArgString = A->getAsString(Args); |
| 242 | std::string Nearest; |
| 243 | if (getOpts().findNearest( |
| 244 | ArgString, Nearest, IncludedFlagsBitmask, ExcludedFlagsBitmask) > 1) { |
| 245 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl |
| 246 | : diag::err_drv_unknown_argument; |
| 247 | Diags.Report(DiagID) << ArgString; |
| 248 | } else { |
| 249 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion |
| 250 | : diag::err_drv_unknown_argument_with_suggestion; |
| 251 | Diags.Report(DiagID) << ArgString << Nearest; |
| 252 | } |
| 253 | ContainsError |= Diags.getDiagnosticLevel(DiagID, SourceLocation()) > |
| 254 | DiagnosticsEngine::Warning; |
| 255 | } |
| 256 | |
| 257 | return Args; |
| 258 | } |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, |
| 264 | Arg **FinalPhaseArg) const { |
| 265 | Arg *PhaseArg = nullptr; |
| 266 | phases::ID FinalPhase; |
| 267 | |
| 268 | |
| 269 | if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(options::OPT_E)) || |
| 270 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_EP)) || |
| 271 | (PhaseArg = DAL.getLastArg(options::OPT_M, options::OPT_MM)) || |
| 272 | (PhaseArg = DAL.getLastArg(options::OPT__SLASH_P))) { |
| 273 | FinalPhase = phases::Preprocess; |
| 274 | |
| 275 | |
| 276 | } else if ((PhaseArg = DAL.getLastArg(options::OPT__precompile))) { |
| 277 | FinalPhase = phases::Precompile; |
| 278 | |
| 279 | |
| 280 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_fsyntax_only)) || |
| 281 | (PhaseArg = DAL.getLastArg(options::OPT_module_file_info)) || |
| 282 | (PhaseArg = DAL.getLastArg(options::OPT_verify_pch)) || |
| 283 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_objc)) || |
| 284 | (PhaseArg = DAL.getLastArg(options::OPT_rewrite_legacy_objc)) || |
| 285 | (PhaseArg = DAL.getLastArg(options::OPT__migrate)) || |
| 286 | (PhaseArg = DAL.getLastArg(options::OPT__analyze, |
| 287 | options::OPT__analyze_auto)) || |
| 288 | (PhaseArg = DAL.getLastArg(options::OPT_emit_ast))) { |
| 289 | FinalPhase = phases::Compile; |
| 290 | |
| 291 | |
| 292 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_S))) { |
| 293 | FinalPhase = phases::Backend; |
| 294 | |
| 295 | |
| 296 | } else if ((PhaseArg = DAL.getLastArg(options::OPT_c))) { |
| 297 | FinalPhase = phases::Assemble; |
| 298 | |
| 299 | |
| 300 | } else |
| 301 | FinalPhase = phases::Link; |
| 302 | |
| 303 | if (FinalPhaseArg) |
| 304 | *FinalPhaseArg = PhaseArg; |
| 305 | |
| 306 | return FinalPhase; |
| 307 | } |
| 308 | |
| 309 | static Arg *MakeInputArg(DerivedArgList &Args, OptTable &Opts, |
| 310 | StringRef Value, bool Claim = true) { |
| 311 | Arg *A = new Arg(Opts.getOption(options::OPT_INPUT), Value, |
| 312 | Args.getBaseArgs().MakeIndex(Value), Value.data()); |
| 313 | Args.AddSynthesizedArg(A); |
| 314 | if (Claim) |
| 315 | A->claim(); |
| 316 | return A; |
| 317 | } |
| 318 | |
| 319 | DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const { |
| 320 | DerivedArgList *DAL = new DerivedArgList(Args); |
| 321 | |
| 322 | bool HasNostdlib = Args.hasArg(options::OPT_nostdlib); |
| 323 | bool HasNostdlibxx = Args.hasArg(options::OPT_nostdlibxx); |
| 324 | bool HasNodefaultlib = Args.hasArg(options::OPT_nodefaultlibs); |
| 325 | for (Arg *A : Args) { |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | if ((A->getOption().matches(options::OPT_Wl_COMMA) || |
| 333 | A->getOption().matches(options::OPT_Xlinker)) && |
| 334 | A->containsValue("--no-demangle")) { |
| 335 | |
| 336 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_Xlinker__no_demangle)); |
| 337 | |
| 338 | |
| 339 | for (StringRef Val : A->getValues()) |
| 340 | if (Val != "--no-demangle") |
| 341 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_Xlinker), Val); |
| 342 | |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | if (A->getOption().matches(options::OPT_Wp_COMMA) && |
| 350 | (A->getValue(0) == StringRef("-MD") || |
| 351 | A->getValue(0) == StringRef("-MMD"))) { |
| 352 | |
| 353 | if (A->getValue(0) == StringRef("-MD")) |
| 354 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MD)); |
| 355 | else |
| 356 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_MMD)); |
| 357 | if (A->getNumValues() == 2) |
| 358 | DAL->AddSeparateArg(A, Opts->getOption(options::OPT_MF), |
| 359 | A->getValue(1)); |
| 360 | continue; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | if (A->getOption().matches(options::OPT_l)) { |
| 365 | StringRef Value = A->getValue(); |
| 366 | |
| 367 | |
| 368 | if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx && |
| 369 | Value == "stdc++") { |
| 370 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_stdcxx)); |
| 371 | continue; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | if (Value == "cc_kext") { |
| 376 | DAL->AddFlagArg(A, Opts->getOption(options::OPT_Z_reserved_lib_cckext)); |
| 377 | continue; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | |
| 382 | if (A->getOption().matches(options::OPT__DASH_DASH)) { |
| 383 | A->claim(); |
| 384 | for (StringRef Val : A->getValues()) |
| 385 | DAL->append(MakeInputArg(*DAL, *Opts, Val, false)); |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | DAL->append(A); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) |
| 394 | DAL->AddFlagArg(0, Opts->getOption(options::OPT_static)); |
| 395 | |
| 396 | |
| 397 | |
| 398 | #if defined(HOST_LINK_VERSION) |
| 399 | if (!Args.hasArg(options::OPT_mlinker_version_EQ) && |
| 400 | strlen(HOST_LINK_VERSION) > 0) { |
| 401 | DAL->AddJoinedArg(0, Opts->getOption(options::OPT_mlinker_version_EQ), |
| 402 | HOST_LINK_VERSION); |
| 403 | DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim(); |
| 404 | } |
| 405 | #endif |
| 406 | |
| 407 | return DAL; |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | static llvm::Triple computeTargetTriple(const Driver &D, |
| 415 | StringRef TargetTriple, |
| 416 | const ArgList &Args, |
| 417 | StringRef DarwinArchName = "") { |
| 418 | |
| 419 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
| 420 | TargetTriple = A->getValue(); |
| 421 | |
| 422 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | if (TargetTriple.find("-unknown-gnu") != StringRef::npos || |
| 428 | TargetTriple.find("-pc-gnu") != StringRef::npos) |
| 429 | Target.setOSName("hurd"); |
| 430 | |
| 431 | |
| 432 | if (Target.isOSBinFormatMachO()) { |
| 433 | |
| 434 | if (!DarwinArchName.empty()) { |
| 435 | tools::darwin::setTripleTypeForMachOArchName(Target, DarwinArchName); |
| 436 | return Target; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | if (Arg *A = Args.getLastArg(options::OPT_arch)) { |
| 441 | StringRef ArchName = A->getValue(); |
| 442 | tools::darwin::setTripleTypeForMachOArchName(Target, ArchName); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
| 447 | |
| 448 | if (Arg *A = Args.getLastArg(options::OPT_mlittle_endian, |
| 449 | options::OPT_mbig_endian)) { |
| 450 | if (A->getOption().matches(options::OPT_mlittle_endian)) { |
| 451 | llvm::Triple LE = Target.getLittleEndianArchVariant(); |
| 452 | if (LE.getArch() != llvm::Triple::UnknownArch) |
| 453 | Target = std::move(LE); |
| 454 | } else { |
| 455 | llvm::Triple BE = Target.getBigEndianArchVariant(); |
| 456 | if (BE.getArch() != llvm::Triple::UnknownArch) |
| 457 | Target = std::move(BE); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | |
| 462 | if (Target.getArch() == llvm::Triple::tce || |
| 463 | Target.getOS() == llvm::Triple::Minix) |
| 464 | return Target; |
| 465 | |
| 466 | |
| 467 | Arg *A = Args.getLastArg(options::OPT_m64, options::OPT_mx32, |
| 468 | options::OPT_m32, options::OPT_m16); |
| 469 | if (A) { |
| 470 | llvm::Triple::ArchType AT = llvm::Triple::UnknownArch; |
| 471 | |
| 472 | if (A->getOption().matches(options::OPT_m64)) { |
| 473 | AT = Target.get64BitArchVariant().getArch(); |
| 474 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 475 | Target.setEnvironment(llvm::Triple::GNU); |
| 476 | } else if (A->getOption().matches(options::OPT_mx32) && |
| 477 | Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) { |
| 478 | AT = llvm::Triple::x86_64; |
| 479 | Target.setEnvironment(llvm::Triple::GNUX32); |
| 480 | } else if (A->getOption().matches(options::OPT_m32)) { |
| 481 | AT = Target.get32BitArchVariant().getArch(); |
| 482 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
| 483 | Target.setEnvironment(llvm::Triple::GNU); |
| 484 | } else if (A->getOption().matches(options::OPT_m16) && |
| 485 | Target.get32BitArchVariant().getArch() == llvm::Triple::x86) { |
| 486 | AT = llvm::Triple::x86; |
| 487 | Target.setEnvironment(llvm::Triple::CODE16); |
| 488 | } |
| 489 | |
| 490 | if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) |
| 491 | Target.setArch(AT); |
| 492 | } |
| 493 | |
| 494 | |
| 495 | if (Args.hasFlag(options::OPT_miamcu, options::OPT_mno_iamcu, false)) { |
| 496 | if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86) |
| 497 | D.Diag(diag::err_drv_unsupported_opt_for_target) << "-miamcu" |
| 498 | << Target.str(); |
| 499 | |
| 500 | if (A && !A->getOption().matches(options::OPT_m32)) |
| 501 | D.Diag(diag::err_drv_argument_not_allowed_with) |
| 502 | << "-miamcu" << A->getBaseArg().getAsString(Args); |
| 503 | |
| 504 | Target.setArch(llvm::Triple::x86); |
| 505 | Target.setArchName("i586"); |
| 506 | Target.setEnvironment(llvm::Triple::UnknownEnvironment); |
| 507 | Target.setEnvironmentName(""); |
| 508 | Target.setOS(llvm::Triple::ELFIAMCU); |
| 509 | Target.setVendor(llvm::Triple::UnknownVendor); |
| 510 | Target.setVendorName("intel"); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | |
| 515 | A = Args.getLastArg(options::OPT_mabi_EQ); |
| 516 | if (A && Target.isMIPS()) { |
| 517 | StringRef ABIName = A->getValue(); |
| 518 | if (ABIName == "32") { |
| 519 | Target = Target.get32BitArchVariant(); |
| 520 | if (Target.getEnvironment() == llvm::Triple::GNUABI64 || |
| 521 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
| 522 | Target.setEnvironment(llvm::Triple::GNU); |
| 523 | } else if (ABIName == "n32") { |
| 524 | Target = Target.get64BitArchVariant(); |
| 525 | if (Target.getEnvironment() == llvm::Triple::GNU || |
| 526 | Target.getEnvironment() == llvm::Triple::GNUABI64) |
| 527 | Target.setEnvironment(llvm::Triple::GNUABIN32); |
| 528 | } else if (ABIName == "64") { |
| 529 | Target = Target.get64BitArchVariant(); |
| 530 | if (Target.getEnvironment() == llvm::Triple::GNU || |
| 531 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
| 532 | Target.setEnvironment(llvm::Triple::GNUABI64); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | return Target; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | |
| 541 | void Driver::setLTOMode(const llvm::opt::ArgList &Args) { |
| 542 | LTOMode = LTOK_None; |
| 543 | if (!Args.hasFlag(options::OPT_flto, options::OPT_flto_EQ, |
| 544 | options::OPT_fno_lto, false)) |
| 545 | return; |
| 546 | |
| 547 | StringRef LTOName("full"); |
| 548 | |
| 549 | const Arg *A = Args.getLastArg(options::OPT_flto_EQ); |
| 550 | if (A) |
| 551 | LTOName = A->getValue(); |
| 552 | |
| 553 | LTOMode = llvm::StringSwitch<LTOKind>(LTOName) |
| 554 | .Case("full", LTOK_Full) |
| 555 | .Case("thin", LTOK_Thin) |
| 556 | .Default(LTOK_Unknown); |
| 557 | |
| 558 | if (LTOMode == LTOK_Unknown) { |
| 559 | assert(A); |
| 560 | Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() |
| 561 | << A->getValue(); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | |
| 566 | Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const { |
| 567 | StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME); |
| 568 | |
| 569 | const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ); |
| 570 | if (A) |
| 571 | RuntimeName = A->getValue(); |
| 572 | |
| 573 | auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName) |
| 574 | .Case("libomp", OMPRT_OMP) |
| 575 | .Case("libgomp", OMPRT_GOMP) |
| 576 | .Case("libiomp5", OMPRT_IOMP5) |
| 577 | .Default(OMPRT_Unknown); |
| 578 | |
| 579 | if (RT == OMPRT_Unknown) { |
| 580 | if (A) |
| 581 | Diag(diag::err_drv_unsupported_option_argument) |
| 582 | << A->getOption().getName() << A->getValue(); |
| 583 | else |
| 584 | |
| 585 | Diag(diag::err_drv_unsupported_opt) << "-fopenmp"; |
| 586 | } |
| 587 | |
| 588 | return RT; |
| 589 | } |
| 590 | |
| 591 | void Driver::CreateOffloadingDeviceToolChains(Compilation &C, |
| 592 | InputList &Inputs) { |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | bool IsCuda = |
| 600 | llvm::any_of(Inputs, [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
| 601 | return types::isCuda(I.first); |
| 602 | }); |
| 603 | bool IsHIP = |
| 604 | llvm::any_of(Inputs, |
| 605 | [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
| 606 | return types::isHIP(I.first); |
| 607 | }) || |
| 608 | C.getInputArgs().hasArg(options::OPT_hip_link); |
| 609 | if (IsCuda && IsHIP) { |
| 610 | Diag(clang::diag::err_drv_mix_cuda_hip); |
| 611 | return; |
| 612 | } |
| 613 | if (IsCuda) { |
| 614 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 615 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
| 616 | StringRef DeviceTripleStr; |
| 617 | auto OFK = Action::OFK_Cuda; |
| 618 | DeviceTripleStr = |
| 619 | HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" : "nvptx-nvidia-cuda"; |
| 620 | llvm::Triple CudaTriple(DeviceTripleStr); |
| 621 | |
| 622 | |
| 623 | auto &CudaTC = ToolChains[CudaTriple.str() + "/" + HostTriple.str()]; |
| 624 | if (!CudaTC) { |
| 625 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
| 626 | *this, CudaTriple, *HostTC, C.getInputArgs(), OFK); |
| 627 | } |
| 628 | C.addOffloadDeviceToolChain(CudaTC.get(), OFK); |
| 629 | } else if (IsHIP) { |
| 630 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 631 | const llvm::Triple &HostTriple = HostTC->getTriple(); |
| 632 | StringRef DeviceTripleStr; |
| 633 | auto OFK = Action::OFK_HIP; |
| 634 | DeviceTripleStr = "amdgcn-amd-amdhsa"; |
| 635 | llvm::Triple HIPTriple(DeviceTripleStr); |
| 636 | |
| 637 | |
| 638 | auto &HIPTC = ToolChains[HIPTriple.str() + "/" + HostTriple.str()]; |
| 639 | if (!HIPTC) { |
| 640 | HIPTC = llvm::make_unique<toolchains::HIPToolChain>( |
| 641 | *this, HIPTriple, *HostTC, C.getInputArgs()); |
| 642 | } |
| 643 | C.addOffloadDeviceToolChain(HIPTC.get(), OFK); |
| 644 | } |
| 645 | |
| 646 | |
| 647 | |
| 648 | |
| 649 | |
| 650 | |
| 651 | if (Arg *OpenMPTargets = |
| 652 | C.getInputArgs().getLastArg(options::OPT_fopenmp_targets_EQ)) { |
| 653 | if (OpenMPTargets->getNumValues()) { |
| 654 | |
| 655 | |
| 656 | |
| 657 | bool HasValidOpenMPRuntime = C.getInputArgs().hasFlag( |
| 658 | options::OPT_fopenmp, options::OPT_fopenmp_EQ, |
| 659 | options::OPT_fno_openmp, false); |
| 660 | if (HasValidOpenMPRuntime) { |
| 661 | OpenMPRuntimeKind OpenMPKind = getOpenMPRuntime(C.getInputArgs()); |
| 662 | HasValidOpenMPRuntime = |
| 663 | OpenMPKind == OMPRT_OMP || OpenMPKind == OMPRT_IOMP5; |
| 664 | } |
| 665 | |
| 666 | if (HasValidOpenMPRuntime) { |
| 667 | llvm::StringMap<const char *> FoundNormalizedTriples; |
| 668 | for (const char *Val : OpenMPTargets->getValues()) { |
| 669 | llvm::Triple TT(Val); |
| 670 | std::string NormalizedName = TT.normalize(); |
| 671 | |
| 672 | |
| 673 | auto Duplicate = FoundNormalizedTriples.find(NormalizedName); |
| 674 | if (Duplicate != FoundNormalizedTriples.end()) { |
| 675 | Diag(clang::diag::warn_drv_omp_offload_target_duplicate) |
| 676 | << Val << Duplicate->second; |
| 677 | continue; |
| 678 | } |
| 679 | |
| 680 | |
| 681 | |
| 682 | FoundNormalizedTriples[NormalizedName] = Val; |
| 683 | |
| 684 | |
| 685 | if (TT.getArch() == llvm::Triple::UnknownArch) |
| 686 | Diag(clang::diag::err_drv_invalid_omp_target) << Val; |
| 687 | else { |
| 688 | const ToolChain *TC; |
| 689 | |
| 690 | |
| 691 | if (TT.isNVPTX()) { |
| 692 | const ToolChain *HostTC = |
| 693 | C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 694 | (0) . __assert_fail ("HostTC && \"Host toolchain should be always defined.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 694, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HostTC && "Host toolchain should be always defined."); |
| 695 | auto &CudaTC = |
| 696 | ToolChains[TT.str() + "/" + HostTC->getTriple().normalize()]; |
| 697 | if (!CudaTC) |
| 698 | CudaTC = llvm::make_unique<toolchains::CudaToolChain>( |
| 699 | *this, TT, *HostTC, C.getInputArgs(), Action::OFK_OpenMP); |
| 700 | TC = CudaTC.get(); |
| 701 | } else |
| 702 | TC = &getToolChain(C.getInputArgs(), TT); |
| 703 | C.addOffloadDeviceToolChain(TC, Action::OFK_OpenMP); |
| 704 | } |
| 705 | } |
| 706 | } else |
| 707 | Diag(clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); |
| 708 | } else |
| 709 | Diag(clang::diag::warn_drv_empty_joined_argument) |
| 710 | << OpenMPTargets->getAsString(C.getInputArgs()); |
| 711 | } |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | } |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | static bool searchForFile(SmallVectorImpl<char> &FilePath, |
| 729 | ArrayRef<std::string> Dirs, |
| 730 | StringRef FileName) { |
| 731 | SmallString<128> WPath; |
| 732 | for (const StringRef &Dir : Dirs) { |
| 733 | if (Dir.empty()) |
| 734 | continue; |
| 735 | WPath.clear(); |
| 736 | llvm::sys::path::append(WPath, Dir, FileName); |
| 737 | llvm::sys::path::native(WPath); |
| 738 | if (llvm::sys::fs::is_regular_file(WPath)) { |
| 739 | FilePath = std::move(WPath); |
| 740 | return true; |
| 741 | } |
| 742 | } |
| 743 | return false; |
| 744 | } |
| 745 | |
| 746 | bool Driver::readConfigFile(StringRef FileName) { |
| 747 | |
| 748 | SmallVector<const char *, 32> NewCfgArgs; |
| 749 | if (!llvm::cl::readConfigFile(FileName, Saver, NewCfgArgs)) { |
| 750 | Diag(diag::err_drv_cannot_read_config_file) << FileName; |
| 751 | return true; |
| 752 | } |
| 753 | |
| 754 | |
| 755 | llvm::SmallString<128> CfgFileName(FileName); |
| 756 | llvm::sys::path::native(CfgFileName); |
| 757 | ConfigFile = CfgFileName.str(); |
| 758 | bool ContainErrors; |
| 759 | CfgOptions = llvm::make_unique<InputArgList>( |
| 760 | ParseArgStrings(NewCfgArgs, IsCLMode(), ContainErrors)); |
| 761 | if (ContainErrors) { |
| 762 | CfgOptions.reset(); |
| 763 | return true; |
| 764 | } |
| 765 | |
| 766 | if (CfgOptions->hasArg(options::OPT_config)) { |
| 767 | CfgOptions.reset(); |
| 768 | Diag(diag::err_drv_nested_config_file); |
| 769 | return true; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | |
| 774 | for (Arg *A : *CfgOptions) |
| 775 | A->claim(); |
| 776 | return false; |
| 777 | } |
| 778 | |
| 779 | bool Driver::loadConfigFile() { |
| 780 | std::string CfgFileName; |
| 781 | bool FileSpecifiedExplicitly = false; |
| 782 | |
| 783 | |
| 784 | if (CLOptions) { |
| 785 | if (CLOptions->hasArg(options::OPT_config_system_dir_EQ)) { |
| 786 | SmallString<128> CfgDir; |
| 787 | CfgDir.append( |
| 788 | CLOptions->getLastArgValue(options::OPT_config_system_dir_EQ)); |
| 789 | if (!CfgDir.empty()) { |
| 790 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
| 791 | SystemConfigDir.clear(); |
| 792 | else |
| 793 | SystemConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 794 | } |
| 795 | } |
| 796 | if (CLOptions->hasArg(options::OPT_config_user_dir_EQ)) { |
| 797 | SmallString<128> CfgDir; |
| 798 | CfgDir.append( |
| 799 | CLOptions->getLastArgValue(options::OPT_config_user_dir_EQ)); |
| 800 | if (!CfgDir.empty()) { |
| 801 | if (llvm::sys::fs::make_absolute(CfgDir).value() != 0) |
| 802 | UserConfigDir.clear(); |
| 803 | else |
| 804 | UserConfigDir = std::string(CfgDir.begin(), CfgDir.end()); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | |
| 810 | if (CLOptions) { |
| 811 | std::vector<std::string> ConfigFiles = |
| 812 | CLOptions->getAllArgValues(options::OPT_config); |
| 813 | if (ConfigFiles.size() > 1) { |
| 814 | Diag(diag::err_drv_duplicate_config); |
| 815 | return true; |
| 816 | } |
| 817 | |
| 818 | if (!ConfigFiles.empty()) { |
| 819 | CfgFileName = ConfigFiles.front(); |
| 820 | assert(!CfgFileName.empty()); |
| 821 | |
| 822 | |
| 823 | |
| 824 | if (llvm::sys::path::has_parent_path(CfgFileName)) { |
| 825 | SmallString<128> CfgFilePath; |
| 826 | if (llvm::sys::path::is_relative(CfgFileName)) |
| 827 | llvm::sys::fs::current_path(CfgFilePath); |
| 828 | llvm::sys::path::append(CfgFilePath, CfgFileName); |
| 829 | if (!llvm::sys::fs::is_regular_file(CfgFilePath)) { |
| 830 | Diag(diag::err_drv_config_file_not_exist) << CfgFilePath; |
| 831 | return true; |
| 832 | } |
| 833 | return readConfigFile(CfgFilePath); |
| 834 | } |
| 835 | |
| 836 | FileSpecifiedExplicitly = true; |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | if (CfgFileName.empty() && !ClangNameParts.TargetPrefix.empty()) |
| 844 | CfgFileName = ClangNameParts.TargetPrefix + '-' + ClangNameParts.ModeSuffix; |
| 845 | |
| 846 | if (CfgFileName.empty()) |
| 847 | return false; |
| 848 | |
| 849 | |
| 850 | StringRef CfgFileArch = CfgFileName; |
| 851 | size_t ArchPrefixLen = CfgFileArch.find('-'); |
| 852 | if (ArchPrefixLen == StringRef::npos) |
| 853 | ArchPrefixLen = CfgFileArch.size(); |
| 854 | llvm::Triple CfgTriple; |
| 855 | CfgFileArch = CfgFileArch.take_front(ArchPrefixLen); |
| 856 | CfgTriple = llvm::Triple(llvm::Triple::normalize(CfgFileArch)); |
| 857 | if (CfgTriple.getArch() == llvm::Triple::ArchType::UnknownArch) |
| 858 | ArchPrefixLen = 0; |
| 859 | |
| 860 | if (!StringRef(CfgFileName).endswith(".cfg")) |
| 861 | CfgFileName += ".cfg"; |
| 862 | |
| 863 | |
| 864 | |
| 865 | |
| 866 | SmallString<128> FixedConfigFile; |
| 867 | size_t FixedArchPrefixLen = 0; |
| 868 | if (ArchPrefixLen) { |
| 869 | |
| 870 | |
| 871 | |
| 872 | llvm::Triple EffectiveTriple = computeTargetTriple(*this, |
| 873 | CfgTriple.getTriple(), *CLOptions); |
| 874 | if (CfgTriple.getArch() != EffectiveTriple.getArch()) { |
| 875 | FixedConfigFile = EffectiveTriple.getArchName(); |
| 876 | FixedArchPrefixLen = FixedConfigFile.size(); |
| 877 | |
| 878 | |
| 879 | if (ArchPrefixLen < CfgFileName.size()) |
| 880 | FixedConfigFile += CfgFileName.substr(ArchPrefixLen); |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | |
| 885 | SmallVector<std::string, 3> CfgFileSearchDirs; |
| 886 | CfgFileSearchDirs.push_back(UserConfigDir); |
| 887 | CfgFileSearchDirs.push_back(SystemConfigDir); |
| 888 | CfgFileSearchDirs.push_back(Dir); |
| 889 | |
| 890 | |
| 891 | llvm::SmallString<128> CfgFilePath; |
| 892 | if (!FixedConfigFile.empty()) { |
| 893 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 894 | return readConfigFile(CfgFilePath); |
| 895 | |
| 896 | FixedConfigFile.resize(FixedArchPrefixLen); |
| 897 | FixedConfigFile.append(".cfg"); |
| 898 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, FixedConfigFile)) |
| 899 | return readConfigFile(CfgFilePath); |
| 900 | } |
| 901 | |
| 902 | |
| 903 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 904 | return readConfigFile(CfgFilePath); |
| 905 | |
| 906 | |
| 907 | if (!ClangNameParts.ModeSuffix.empty() && |
| 908 | !ClangNameParts.TargetPrefix.empty()) { |
| 909 | CfgFileName.assign(ClangNameParts.TargetPrefix); |
| 910 | CfgFileName.append(".cfg"); |
| 911 | if (searchForFile(CfgFilePath, CfgFileSearchDirs, CfgFileName)) |
| 912 | return readConfigFile(CfgFilePath); |
| 913 | } |
| 914 | |
| 915 | |
| 916 | |
| 917 | if (FileSpecifiedExplicitly) { |
| 918 | Diag(diag::err_drv_config_file_not_found) << CfgFileName; |
| 919 | for (const std::string &SearchDir : CfgFileSearchDirs) |
| 920 | if (!SearchDir.empty()) |
| 921 | Diag(diag::note_drv_config_file_searched_in) << SearchDir; |
| 922 | return true; |
| 923 | } |
| 924 | |
| 925 | return false; |
| 926 | } |
| 927 | |
| 928 | Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { |
| 929 | llvm::PrettyStackTraceString CrashInfo("Compilation construction"); |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | if (Optional<std::string> CompilerPathValue = |
| 935 | llvm::sys::Process::GetEnv("COMPILER_PATH")) { |
| 936 | StringRef CompilerPath = *CompilerPathValue; |
| 937 | while (!CompilerPath.empty()) { |
| 938 | std::pair<StringRef, StringRef> Split = |
| 939 | CompilerPath.split(llvm::sys::EnvPathSeparator); |
| 940 | PrefixDirs.push_back(Split.first); |
| 941 | CompilerPath = Split.second; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | |
| 946 | |
| 947 | ParseDriverMode(ClangExecutable, ArgList.slice(1)); |
| 948 | |
| 949 | |
| 950 | |
| 951 | |
| 952 | bool ContainsError; |
| 953 | CLOptions = llvm::make_unique<InputArgList>( |
| 954 | ParseArgStrings(ArgList.slice(1), IsCLMode(), ContainsError)); |
| 955 | |
| 956 | |
| 957 | if (!ContainsError) |
| 958 | ContainsError = loadConfigFile(); |
| 959 | bool HasConfigFile = !ContainsError && (CfgOptions.get() != nullptr); |
| 960 | |
| 961 | |
| 962 | InputArgList Args = std::move(HasConfigFile ? std::move(*CfgOptions) |
| 963 | : std::move(*CLOptions)); |
| 964 | |
| 965 | auto appendOneArg = [&Args](const Arg *Opt, const Arg *BaseArg) { |
| 966 | unsigned Index = Args.MakeIndex(Opt->getSpelling()); |
| 967 | Arg *Copy = new llvm::opt::Arg(Opt->getOption(), Opt->getSpelling(), |
| 968 | Index, BaseArg); |
| 969 | Copy->getValues() = Opt->getValues(); |
| 970 | if (Opt->isClaimed()) |
| 971 | Copy->claim(); |
| 972 | Args.append(Copy); |
| 973 | }; |
| 974 | |
| 975 | if (HasConfigFile) |
| 976 | for (auto *Opt : *CLOptions) { |
| 977 | if (Opt->getOption().matches(options::OPT_config)) |
| 978 | continue; |
| 979 | const Arg *BaseArg = &Opt->getBaseArg(); |
| 980 | if (BaseArg == Opt) |
| 981 | BaseArg = nullptr; |
| 982 | appendOneArg(Opt, BaseArg); |
| 983 | } |
| 984 | |
| 985 | |
| 986 | if (IsCLMode() && !ContainsError) { |
| 987 | SmallVector<const char *, 16> CLModePassThroughArgList; |
| 988 | for (const auto *A : Args.filtered(options::OPT__SLASH_clang)) { |
| 989 | A->claim(); |
| 990 | CLModePassThroughArgList.push_back(A->getValue()); |
| 991 | } |
| 992 | |
| 993 | if (!CLModePassThroughArgList.empty()) { |
| 994 | |
| 995 | |
| 996 | auto CLModePassThroughOptions = llvm::make_unique<InputArgList>( |
| 997 | ParseArgStrings(CLModePassThroughArgList, false, ContainsError)); |
| 998 | |
| 999 | if (!ContainsError) |
| 1000 | for (auto *Opt : *CLModePassThroughOptions) { |
| 1001 | appendOneArg(Opt, nullptr); |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | bool CCCPrintPhases; |
| 1008 | |
| 1009 | |
| 1010 | Diags.setIgnoreAllWarnings(Args.hasArg(options::OPT_w)); |
| 1011 | |
| 1012 | |
| 1013 | Args.ClaimAllArgs(options::OPT_no_canonical_prefixes); |
| 1014 | |
| 1015 | |
| 1016 | Args.ClaimAllArgs(options::OPT_pipe); |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | CCCPrintPhases = Args.hasArg(options::OPT_ccc_print_phases); |
| 1025 | CCCPrintBindings = Args.hasArg(options::OPT_ccc_print_bindings); |
| 1026 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_gcc_name)) |
| 1027 | CCCGenericGCCName = A->getValue(); |
| 1028 | GenReproducer = Args.hasFlag(options::OPT_gen_reproducer, |
| 1029 | options::OPT_fno_crash_diagnostics, |
| 1030 | !!::getenv("FORCE_CLANG_DIAGNOSTICS_CRASH")); |
| 1031 | |
| 1032 | |
| 1033 | if (IsCLMode()) { |
| 1034 | |
| 1035 | llvm::Triple T(TargetTriple); |
| 1036 | T.setOS(llvm::Triple::Win32); |
| 1037 | T.setVendor(llvm::Triple::PC); |
| 1038 | T.setEnvironment(llvm::Triple::MSVC); |
| 1039 | T.setObjectFormat(llvm::Triple::COFF); |
| 1040 | TargetTriple = T.str(); |
| 1041 | } |
| 1042 | if (const Arg *A = Args.getLastArg(options::OPT_target)) |
| 1043 | TargetTriple = A->getValue(); |
| 1044 | if (const Arg *A = Args.getLastArg(options::OPT_ccc_install_dir)) |
| 1045 | Dir = InstalledDir = A->getValue(); |
| 1046 | for (const Arg *A : Args.filtered(options::OPT_B)) { |
| 1047 | A->claim(); |
| 1048 | PrefixDirs.push_back(A->getValue(0)); |
| 1049 | } |
| 1050 | if (const Arg *A = Args.getLastArg(options::OPT__sysroot_EQ)) |
| 1051 | SysRoot = A->getValue(); |
| 1052 | if (const Arg *A = Args.getLastArg(options::OPT__dyld_prefix_EQ)) |
| 1053 | DyldPrefix = A->getValue(); |
| 1054 | |
| 1055 | if (const Arg *A = Args.getLastArg(options::OPT_resource_dir)) |
| 1056 | ResourceDir = A->getValue(); |
| 1057 | |
| 1058 | if (const Arg *A = Args.getLastArg(options::OPT_save_temps_EQ)) { |
| 1059 | SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue()) |
| 1060 | .Case("cwd", SaveTempsCwd) |
| 1061 | .Case("obj", SaveTempsObj) |
| 1062 | .Default(SaveTempsCwd); |
| 1063 | } |
| 1064 | |
| 1065 | setLTOMode(Args); |
| 1066 | |
| 1067 | |
| 1068 | if (Arg *A = Args.getLastArg(options::OPT_fembed_bitcode_EQ)) { |
| 1069 | StringRef Name = A->getValue(); |
| 1070 | unsigned Model = llvm::StringSwitch<unsigned>(Name) |
| 1071 | .Case("off", EmbedNone) |
| 1072 | .Case("all", EmbedBitcode) |
| 1073 | .Case("bitcode", EmbedBitcode) |
| 1074 | .Case("marker", EmbedMarker) |
| 1075 | .Default(~0U); |
| 1076 | if (Model == ~0U) { |
| 1077 | Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) |
| 1078 | << Name; |
| 1079 | } else |
| 1080 | BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); |
| 1081 | } |
| 1082 | |
| 1083 | std::unique_ptr<llvm::opt::InputArgList> UArgs = |
| 1084 | llvm::make_unique<InputArgList>(std::move(Args)); |
| 1085 | |
| 1086 | |
| 1087 | DerivedArgList *TranslatedArgs = TranslateInputArgs(*UArgs); |
| 1088 | |
| 1089 | |
| 1090 | const ToolChain &TC = getToolChain( |
| 1091 | *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs)); |
| 1092 | |
| 1093 | |
| 1094 | Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs, |
| 1095 | ContainsError); |
| 1096 | |
| 1097 | if (!HandleImmediateArgs(*C)) |
| 1098 | return C; |
| 1099 | |
| 1100 | |
| 1101 | InputList Inputs; |
| 1102 | BuildInputs(C->getDefaultToolChain(), *TranslatedArgs, Inputs); |
| 1103 | |
| 1104 | |
| 1105 | CreateOffloadingDeviceToolChains(*C, Inputs); |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | if (TC.getTriple().isOSBinFormatMachO()) |
| 1110 | BuildUniversalActions(*C, C->getDefaultToolChain(), Inputs); |
| 1111 | else |
| 1112 | BuildActions(*C, C->getArgs(), Inputs, C->getActions()); |
| 1113 | |
| 1114 | if (CCCPrintPhases) { |
| 1115 | PrintActions(*C); |
| 1116 | return C; |
| 1117 | } |
| 1118 | |
| 1119 | BuildJobs(*C); |
| 1120 | |
| 1121 | return C; |
| 1122 | } |
| 1123 | |
| 1124 | static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) { |
| 1125 | llvm::opt::ArgStringList ASL; |
| 1126 | for (const auto *A : Args) |
| 1127 | A->render(Args, ASL); |
| 1128 | |
| 1129 | for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) { |
| 1130 | if (I != ASL.begin()) |
| 1131 | OS << ' '; |
| 1132 | Command::printArg(OS, *I, true); |
| 1133 | } |
| 1134 | OS << '\n'; |
| 1135 | } |
| 1136 | |
| 1137 | bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename, |
| 1138 | SmallString<128> &CrashDiagDir) { |
| 1139 | using namespace llvm::sys; |
| 1140 | (0) . __assert_fail ("llvm..Triple(llvm..sys..getProcessTriple()).isOSDarwin() && \"Only knows about .crash files on Darwin\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 1141, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() && |
| 1141 | (0) . __assert_fail ("llvm..Triple(llvm..sys..getProcessTriple()).isOSDarwin() && \"Only knows about .crash files on Darwin\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 1141, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Only knows about .crash files on Darwin"); |
| 1142 | |
| 1143 | |
| 1144 | |
| 1145 | |
| 1146 | path::home_directory(CrashDiagDir); |
| 1147 | if (CrashDiagDir.startswith("/var/root")) |
| 1148 | CrashDiagDir = "/"; |
| 1149 | path::append(CrashDiagDir, "Library/Logs/DiagnosticReports"); |
| 1150 | int PID = |
| 1151 | #if LLVM_ON_UNIX |
| 1152 | getpid(); |
| 1153 | #else |
| 1154 | 0; |
| 1155 | #endif |
| 1156 | std::error_code EC; |
| 1157 | fs::file_status FileStatus; |
| 1158 | TimePoint<> LastAccessTime; |
| 1159 | SmallString<128> CrashFilePath; |
| 1160 | |
| 1161 | |
| 1162 | for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd; |
| 1163 | File != FileEnd && !EC; File.increment(EC)) { |
| 1164 | StringRef FileName = path::filename(File->path()); |
| 1165 | if (!FileName.startswith(Name)) |
| 1166 | continue; |
| 1167 | if (fs::status(File->path(), FileStatus)) |
| 1168 | continue; |
| 1169 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile = |
| 1170 | llvm::MemoryBuffer::getFile(File->path()); |
| 1171 | if (!CrashFile) |
| 1172 | continue; |
| 1173 | |
| 1174 | |
| 1175 | StringRef Data = CrashFile.get()->getBuffer(); |
| 1176 | if (!Data.startswith("Process:")) |
| 1177 | continue; |
| 1178 | |
| 1179 | size_t ParentProcPos = Data.find("Parent Process:"); |
| 1180 | if (ParentProcPos == StringRef::npos) |
| 1181 | continue; |
| 1182 | size_t LineEnd = Data.find_first_of("\n", ParentProcPos); |
| 1183 | if (LineEnd == StringRef::npos) |
| 1184 | continue; |
| 1185 | StringRef ParentProcess = Data.slice(ParentProcPos+15, LineEnd).trim(); |
| 1186 | int OpenBracket = -1, CloseBracket = -1; |
| 1187 | for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) { |
| 1188 | if (ParentProcess[i] == '[') |
| 1189 | OpenBracket = i; |
| 1190 | if (ParentProcess[i] == ']') |
| 1191 | CloseBracket = i; |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | int CrashPID; |
| 1196 | if (OpenBracket < 0 || CloseBracket < 0 || |
| 1197 | ParentProcess.slice(OpenBracket + 1, CloseBracket) |
| 1198 | .getAsInteger(10, CrashPID) || CrashPID != PID) { |
| 1199 | continue; |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | const auto FileAccessTime = FileStatus.getLastModificationTime(); |
| 1209 | if (FileAccessTime > LastAccessTime) { |
| 1210 | CrashFilePath.assign(File->path()); |
| 1211 | LastAccessTime = FileAccessTime; |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | |
| 1216 | if (!CrashFilePath.empty()) { |
| 1217 | EC = fs::copy_file(CrashFilePath, ReproCrashFilename); |
| 1218 | if (EC) |
| 1219 | return false; |
| 1220 | return true; |
| 1221 | } |
| 1222 | |
| 1223 | return false; |
| 1224 | } |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | |
| 1229 | void Driver::generateCompilationDiagnostics( |
| 1230 | Compilation &C, const Command &FailingCommand, |
| 1231 | StringRef AdditionalInformation, CompilationDiagnosticReport *Report) { |
| 1232 | if (C.getArgs().hasArg(options::OPT_fno_crash_diagnostics)) |
| 1233 | return; |
| 1234 | |
| 1235 | |
| 1236 | if (FailingCommand.getCreator().isLinkJob() || |
| 1237 | FailingCommand.getCreator().isDsymutilJob()) |
| 1238 | return; |
| 1239 | |
| 1240 | |
| 1241 | PrintVersion(C, llvm::errs()); |
| 1242 | |
| 1243 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1244 | << "PLEASE submit a bug report to " BUG_REPORT_URL " and include the " |
| 1245 | "crash backtrace, preprocessed source, and associated run script."; |
| 1246 | |
| 1247 | |
| 1248 | Mode = CPPMode; |
| 1249 | CCGenDiagnostics = true; |
| 1250 | |
| 1251 | |
| 1252 | Command Cmd = FailingCommand; |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | DiagnosticErrorTrap Trap(Diags); |
| 1257 | |
| 1258 | |
| 1259 | C.initCompilationForDiagnostics(); |
| 1260 | |
| 1261 | |
| 1262 | InputList Inputs; |
| 1263 | BuildInputs(C.getDefaultToolChain(), C.getArgs(), Inputs); |
| 1264 | |
| 1265 | for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) { |
| 1266 | bool IgnoreInput = false; |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | if (types::getPreprocessedType(it->first) == types::TY_INVALID) { |
| 1271 | IgnoreInput = true; |
| 1272 | } else if (!strcmp(it->second->getValue(), "-")) { |
| 1273 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1274 | << "Error generating preprocessed source(s) - " |
| 1275 | "ignoring input from stdin."; |
| 1276 | IgnoreInput = true; |
| 1277 | } |
| 1278 | |
| 1279 | if (IgnoreInput) { |
| 1280 | it = Inputs.erase(it); |
| 1281 | ie = Inputs.end(); |
| 1282 | } else { |
| 1283 | ++it; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | if (Inputs.empty()) { |
| 1288 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1289 | << "Error generating preprocessed source(s) - " |
| 1290 | "no preprocessable inputs."; |
| 1291 | return; |
| 1292 | } |
| 1293 | |
| 1294 | |
| 1295 | |
| 1296 | llvm::StringSet<> ArchNames; |
| 1297 | for (const Arg *A : C.getArgs()) { |
| 1298 | if (A->getOption().matches(options::OPT_arch)) { |
| 1299 | StringRef ArchName = A->getValue(); |
| 1300 | ArchNames.insert(ArchName); |
| 1301 | } |
| 1302 | } |
| 1303 | if (ArchNames.size() > 1) { |
| 1304 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1305 | << "Error generating preprocessed source(s) - cannot generate " |
| 1306 | "preprocessed source with multiple -arch options."; |
| 1307 | return; |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | |
| 1312 | const ToolChain &TC = C.getDefaultToolChain(); |
| 1313 | if (TC.getTriple().isOSBinFormatMachO()) |
| 1314 | BuildUniversalActions(C, TC, Inputs); |
| 1315 | else |
| 1316 | BuildActions(C, C.getArgs(), Inputs, C.getActions()); |
| 1317 | |
| 1318 | BuildJobs(C); |
| 1319 | |
| 1320 | |
| 1321 | if (Trap.hasErrorOccurred()) { |
| 1322 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1323 | << "Error generating preprocessed source(s)."; |
| 1324 | return; |
| 1325 | } |
| 1326 | |
| 1327 | |
| 1328 | SmallVector<std::pair<int, const Command *>, 4> FailingCommands; |
| 1329 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
| 1330 | |
| 1331 | |
| 1332 | if (!FailingCommands.empty()) { |
| 1333 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1334 | << "Error generating preprocessed source(s)."; |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | const ArgStringList &TempFiles = C.getTempFiles(); |
| 1339 | if (TempFiles.empty()) { |
| 1340 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1341 | << "Error generating preprocessed source(s)."; |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1346 | << "\n********************\n\n" |
| 1347 | "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n" |
| 1348 | "Preprocessed source(s) and associated run script(s) are located at:"; |
| 1349 | |
| 1350 | SmallString<128> VFS; |
| 1351 | SmallString<128> ReproCrashFilename; |
| 1352 | for (const char *TempFile : TempFiles) { |
| 1353 | Diag(clang::diag::note_drv_command_failed_diag_msg) << TempFile; |
| 1354 | if (Report) |
| 1355 | Report->TemporaryFiles.push_back(TempFile); |
| 1356 | if (ReproCrashFilename.empty()) { |
| 1357 | ReproCrashFilename = TempFile; |
| 1358 | llvm::sys::path::replace_extension(ReproCrashFilename, ".crash"); |
| 1359 | } |
| 1360 | if (StringRef(TempFile).endswith(".cache")) { |
| 1361 | |
| 1362 | |
| 1363 | VFS = llvm::sys::path::filename(TempFile); |
| 1364 | llvm::sys::path::append(VFS, "vfs", "vfs.yaml"); |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | |
| 1369 | CrashReportInfo CrashInfo(TempFiles[0], VFS); |
| 1370 | |
| 1371 | llvm::SmallString<128> Script(CrashInfo.Filename); |
| 1372 | llvm::sys::path::replace_extension(Script, "sh"); |
| 1373 | std::error_code EC; |
| 1374 | llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew); |
| 1375 | if (EC) { |
| 1376 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1377 | << "Error generating run script: " << Script << " " << EC.message(); |
| 1378 | } else { |
| 1379 | ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n" |
| 1380 | << "# Driver args: "; |
| 1381 | printArgList(ScriptOS, C.getInputArgs()); |
| 1382 | ScriptOS << "# Original command: "; |
| 1383 | Cmd.Print(ScriptOS, "\n", ); |
| 1384 | Cmd.Print(ScriptOS, "\n", , &CrashInfo); |
| 1385 | if (!AdditionalInformation.empty()) |
| 1386 | ScriptOS << "\n# Additional information: " << AdditionalInformation |
| 1387 | << "\n"; |
| 1388 | if (Report) |
| 1389 | Report->TemporaryFiles.push_back(Script.str()); |
| 1390 | Diag(clang::diag::note_drv_command_failed_diag_msg) << Script; |
| 1391 | } |
| 1392 | |
| 1393 | |
| 1394 | if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) { |
| 1395 | SmallString<128> CrashDiagDir; |
| 1396 | if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) { |
| 1397 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1398 | << ReproCrashFilename.str(); |
| 1399 | } else { |
| 1400 | llvm::sys::path::append(CrashDiagDir, Name); |
| 1401 | CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash"; |
| 1402 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1403 | << "Crash backtrace is located in"; |
| 1404 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1405 | << CrashDiagDir.str(); |
| 1406 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1407 | << "(choose the .crash file that corresponds to your crash)"; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | for (const auto &A : C.getArgs().filtered(options::OPT_frewrite_map_file, |
| 1412 | options::OPT_frewrite_map_file_EQ)) |
| 1413 | Diag(clang::diag::note_drv_command_failed_diag_msg) << A->getValue(); |
| 1414 | |
| 1415 | Diag(clang::diag::note_drv_command_failed_diag_msg) |
| 1416 | << "\n\n********************"; |
| 1417 | } |
| 1418 | |
| 1419 | void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) { |
| 1420 | |
| 1421 | |
| 1422 | |
| 1423 | if (Cmd.getCreator().getResponseFilesSupport() == Tool::RF_None || |
| 1424 | llvm::sys::commandLineFitsWithinSystemLimits(Cmd.getExecutable(), Cmd.getArguments())) |
| 1425 | return; |
| 1426 | |
| 1427 | std::string TmpName = GetTemporaryPath("response", "txt"); |
| 1428 | Cmd.setResponseFile(C.addTempFile(C.getArgs().MakeArgString(TmpName))); |
| 1429 | } |
| 1430 | |
| 1431 | int Driver::ExecuteCompilation( |
| 1432 | Compilation &C, |
| 1433 | SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) { |
| 1434 | |
| 1435 | if (C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
| 1436 | C.getJobs().Print(llvm::errs(), "\n", true); |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | if (Diags.hasErrorOccurred()) |
| 1442 | return 1; |
| 1443 | |
| 1444 | |
| 1445 | for (auto &Job : C.getJobs()) |
| 1446 | setUpResponseFiles(C, Job); |
| 1447 | |
| 1448 | C.ExecuteJobs(C.getJobs(), FailingCommands); |
| 1449 | |
| 1450 | |
| 1451 | if (FailingCommands.empty()) |
| 1452 | return 0; |
| 1453 | |
| 1454 | |
| 1455 | |
| 1456 | int Res = 0; |
| 1457 | for (const auto &CmdPair : FailingCommands) { |
| 1458 | int CommandRes = CmdPair.first; |
| 1459 | const Command *FailingCommand = CmdPair.second; |
| 1460 | |
| 1461 | |
| 1462 | if (!isSaveTempsEnabled()) { |
| 1463 | const JobAction *JA = cast<JobAction>(&FailingCommand->getSource()); |
| 1464 | C.CleanupFileMap(C.getResultFiles(), JA, true); |
| 1465 | |
| 1466 | |
| 1467 | if (CommandRes < 0) |
| 1468 | C.CleanupFileMap(C.getFailureResultFiles(), JA, true); |
| 1469 | } |
| 1470 | |
| 1471 | #if LLVM_ON_UNIX |
| 1472 | |
| 1473 | |
| 1474 | if (CommandRes == EX_IOERR) { |
| 1475 | Res = CommandRes; |
| 1476 | continue; |
| 1477 | } |
| 1478 | #endif |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | |
| 1484 | |
| 1485 | |
| 1486 | |
| 1487 | const Tool &FailingTool = FailingCommand->getCreator(); |
| 1488 | |
| 1489 | if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) { |
| 1490 | |
| 1491 | if (CommandRes < 0) |
| 1492 | Diag(clang::diag::err_drv_command_signalled) |
| 1493 | << FailingTool.getShortName(); |
| 1494 | else |
| 1495 | Diag(clang::diag::err_drv_command_failed) |
| 1496 | << FailingTool.getShortName() << CommandRes; |
| 1497 | } |
| 1498 | } |
| 1499 | return Res; |
| 1500 | } |
| 1501 | |
| 1502 | void Driver::PrintHelp(bool ShowHidden) const { |
| 1503 | unsigned IncludedFlagsBitmask; |
| 1504 | unsigned ExcludedFlagsBitmask; |
| 1505 | std::tie(IncludedFlagsBitmask, ExcludedFlagsBitmask) = |
| 1506 | getIncludeExcludeOptionFlagMasks(IsCLMode()); |
| 1507 | |
| 1508 | ExcludedFlagsBitmask |= options::NoDriverOption; |
| 1509 | if (!ShowHidden) |
| 1510 | ExcludedFlagsBitmask |= HelpHidden; |
| 1511 | |
| 1512 | std::string Usage = llvm::formatv("{0} [options] file...", Name).str(); |
| 1513 | getOpts().PrintHelp(llvm::outs(), Usage.c_str(), DriverTitle.c_str(), |
| 1514 | IncludedFlagsBitmask, ExcludedFlagsBitmask, |
| 1515 | ); |
| 1516 | } |
| 1517 | |
| 1518 | void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const { |
| 1519 | |
| 1520 | |
| 1521 | OS << getClangFullVersion() << '\n'; |
| 1522 | const ToolChain &TC = C.getDefaultToolChain(); |
| 1523 | OS << "Target: " << TC.getTripleString() << '\n'; |
| 1524 | |
| 1525 | |
| 1526 | if (Arg *A = C.getArgs().getLastArg(options::OPT_mthread_model)) { |
| 1527 | |
| 1528 | if (TC.isThreadModelSupported(A->getValue())) |
| 1529 | OS << "Thread model: " << A->getValue(); |
| 1530 | } else |
| 1531 | OS << "Thread model: " << TC.getThreadModel(); |
| 1532 | OS << '\n'; |
| 1533 | |
| 1534 | |
| 1535 | OS << "InstalledDir: " << InstalledDir << '\n'; |
| 1536 | |
| 1537 | |
| 1538 | if (!ConfigFile.empty()) |
| 1539 | OS << "Configuration file: " << ConfigFile << '\n'; |
| 1540 | } |
| 1541 | |
| 1542 | |
| 1543 | |
| 1544 | static void PrintDiagnosticCategories(raw_ostream &OS) { |
| 1545 | |
| 1546 | for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max; |
| 1547 | ++i) |
| 1548 | OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n'; |
| 1549 | } |
| 1550 | |
| 1551 | void Driver::HandleAutocompletions(StringRef PassedFlags) const { |
| 1552 | if (PassedFlags == "") |
| 1553 | return; |
| 1554 | |
| 1555 | |
| 1556 | std::vector<std::string> SuggestedCompletions; |
| 1557 | std::vector<std::string> Flags; |
| 1558 | |
| 1559 | unsigned short DisableFlags = |
| 1560 | options::NoDriverOption | options::Unsupported | options::Ignored; |
| 1561 | |
| 1562 | |
| 1563 | |
| 1564 | |
| 1565 | const bool HasSpace = PassedFlags.endswith(","); |
| 1566 | |
| 1567 | |
| 1568 | |
| 1569 | StringRef TargetFlags = PassedFlags; |
| 1570 | while (TargetFlags != "") { |
| 1571 | StringRef CurFlag; |
| 1572 | std::tie(CurFlag, TargetFlags) = TargetFlags.split(","); |
| 1573 | Flags.push_back(std::string(CurFlag)); |
| 1574 | } |
| 1575 | |
| 1576 | |
| 1577 | |
| 1578 | if (llvm::is_contained(Flags, "-Xclang") || llvm::is_contained(Flags, "-cc1")) |
| 1579 | DisableFlags &= ~options::NoDriverOption; |
| 1580 | |
| 1581 | StringRef Cur; |
| 1582 | Cur = Flags.at(Flags.size() - 1); |
| 1583 | StringRef Prev; |
| 1584 | if (Flags.size() >= 2) { |
| 1585 | Prev = Flags.at(Flags.size() - 2); |
| 1586 | SuggestedCompletions = Opts->suggestValueCompletions(Prev, Cur); |
| 1587 | } |
| 1588 | |
| 1589 | if (SuggestedCompletions.empty()) |
| 1590 | SuggestedCompletions = Opts->suggestValueCompletions(Cur, ""); |
| 1591 | |
| 1592 | |
| 1593 | |
| 1594 | |
| 1595 | |
| 1596 | |
| 1597 | if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) { |
| 1598 | llvm::outs() << '\n'; |
| 1599 | return; |
| 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | |
| 1604 | if (SuggestedCompletions.empty() && !Cur.endswith("=")) { |
| 1605 | |
| 1606 | |
| 1607 | |
| 1608 | SuggestedCompletions = Opts->findByPrefix(Cur, DisableFlags); |
| 1609 | |
| 1610 | |
| 1611 | |
| 1612 | |
| 1613 | for (StringRef S : DiagnosticIDs::getDiagnosticFlags()) |
| 1614 | if (S.startswith(Cur)) |
| 1615 | SuggestedCompletions.push_back(S); |
| 1616 | } |
| 1617 | |
| 1618 | |
| 1619 | |
| 1620 | |
| 1621 | |
| 1622 | llvm::sort(SuggestedCompletions, [](StringRef A, StringRef B) { |
| 1623 | if (int X = A.compare_lower(B)) |
| 1624 | return X < 0; |
| 1625 | return A.compare(B) > 0; |
| 1626 | }); |
| 1627 | |
| 1628 | llvm::outs() << llvm::join(SuggestedCompletions, "\n") << '\n'; |
| 1629 | } |
| 1630 | |
| 1631 | bool Driver::HandleImmediateArgs(const Compilation &C) { |
| 1632 | |
| 1633 | |
| 1634 | |
| 1635 | if (C.getArgs().hasArg(options::OPT_dumpmachine)) { |
| 1636 | llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n'; |
| 1637 | return false; |
| 1638 | } |
| 1639 | |
| 1640 | if (C.getArgs().hasArg(options::OPT_dumpversion)) { |
| 1641 | |
| 1642 | |
| 1643 | llvm::outs() << CLANG_VERSION_STRING << "\n"; |
| 1644 | return false; |
| 1645 | } |
| 1646 | |
| 1647 | if (C.getArgs().hasArg(options::OPT__print_diagnostic_categories)) { |
| 1648 | PrintDiagnosticCategories(llvm::outs()); |
| 1649 | return false; |
| 1650 | } |
| 1651 | |
| 1652 | if (C.getArgs().hasArg(options::OPT_help) || |
| 1653 | C.getArgs().hasArg(options::OPT__help_hidden)) { |
| 1654 | PrintHelp(C.getArgs().hasArg(options::OPT__help_hidden)); |
| 1655 | return false; |
| 1656 | } |
| 1657 | |
| 1658 | if (C.getArgs().hasArg(options::OPT__version)) { |
| 1659 | |
| 1660 | PrintVersion(C, llvm::outs()); |
| 1661 | return false; |
| 1662 | } |
| 1663 | |
| 1664 | if (C.getArgs().hasArg(options::OPT_v) || |
| 1665 | C.getArgs().hasArg(options::OPT__HASH_HASH_HASH)) { |
| 1666 | PrintVersion(C, llvm::errs()); |
| 1667 | SuppressMissingInputWarning = true; |
| 1668 | } |
| 1669 | |
| 1670 | if (C.getArgs().hasArg(options::OPT_v)) { |
| 1671 | if (!SystemConfigDir.empty()) |
| 1672 | llvm::errs() << "System configuration file directory: " |
| 1673 | << SystemConfigDir << "\n"; |
| 1674 | if (!UserConfigDir.empty()) |
| 1675 | llvm::errs() << "User configuration file directory: " |
| 1676 | << UserConfigDir << "\n"; |
| 1677 | } |
| 1678 | |
| 1679 | const ToolChain &TC = C.getDefaultToolChain(); |
| 1680 | |
| 1681 | if (C.getArgs().hasArg(options::OPT_v)) |
| 1682 | TC.printVerboseInfo(llvm::errs()); |
| 1683 | |
| 1684 | if (C.getArgs().hasArg(options::OPT_print_resource_dir)) { |
| 1685 | llvm::outs() << ResourceDir << '\n'; |
| 1686 | return false; |
| 1687 | } |
| 1688 | |
| 1689 | if (C.getArgs().hasArg(options::OPT_print_search_dirs)) { |
| 1690 | llvm::outs() << "programs: ="; |
| 1691 | bool separator = false; |
| 1692 | for (const std::string &Path : TC.getProgramPaths()) { |
| 1693 | if (separator) |
| 1694 | llvm::outs() << ':'; |
| 1695 | llvm::outs() << Path; |
| 1696 | separator = true; |
| 1697 | } |
| 1698 | llvm::outs() << "\n"; |
| 1699 | llvm::outs() << "libraries: =" << ResourceDir; |
| 1700 | |
| 1701 | StringRef sysroot = C.getSysRoot(); |
| 1702 | |
| 1703 | for (const std::string &Path : TC.getFilePaths()) { |
| 1704 | |
| 1705 | llvm::outs() << ':'; |
| 1706 | |
| 1707 | if (Path[0] == '=') |
| 1708 | llvm::outs() << sysroot << Path.substr(1); |
| 1709 | else |
| 1710 | llvm::outs() << Path; |
| 1711 | } |
| 1712 | llvm::outs() << "\n"; |
| 1713 | return false; |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { |
| 1719 | llvm::outs() << GetFilePath(A->getValue(), TC) << "\n"; |
| 1720 | return false; |
| 1721 | } |
| 1722 | |
| 1723 | if (Arg *A = C.getArgs().getLastArg(options::OPT_print_prog_name_EQ)) { |
| 1724 | StringRef ProgName = A->getValue(); |
| 1725 | |
| 1726 | |
| 1727 | if (! ProgName.empty()) |
| 1728 | llvm::outs() << GetProgramPath(ProgName, TC); |
| 1729 | |
| 1730 | llvm::outs() << "\n"; |
| 1731 | return false; |
| 1732 | } |
| 1733 | |
| 1734 | if (Arg *A = C.getArgs().getLastArg(options::OPT_autocomplete)) { |
| 1735 | StringRef PassedFlags = A->getValue(); |
| 1736 | HandleAutocompletions(PassedFlags); |
| 1737 | return false; |
| 1738 | } |
| 1739 | |
| 1740 | if (C.getArgs().hasArg(options::OPT_print_libgcc_file_name)) { |
| 1741 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(C.getArgs()); |
| 1742 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1743 | RegisterEffectiveTriple TripleRAII(TC, Triple); |
| 1744 | switch (RLT) { |
| 1745 | case ToolChain::RLT_CompilerRT: |
| 1746 | llvm::outs() << TC.getCompilerRT(C.getArgs(), "builtins") << "\n"; |
| 1747 | break; |
| 1748 | case ToolChain::RLT_Libgcc: |
| 1749 | llvm::outs() << GetFilePath("libgcc.a", TC) << "\n"; |
| 1750 | break; |
| 1751 | } |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
| 1755 | if (C.getArgs().hasArg(options::OPT_print_multi_lib)) { |
| 1756 | for (const Multilib &Multilib : TC.getMultilibs()) |
| 1757 | llvm::outs() << Multilib << "\n"; |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
| 1761 | if (C.getArgs().hasArg(options::OPT_print_multi_directory)) { |
| 1762 | const Multilib &Multilib = TC.getMultilib(); |
| 1763 | if (Multilib.gccSuffix().empty()) |
| 1764 | llvm::outs() << ".\n"; |
| 1765 | else { |
| 1766 | StringRef Suffix(Multilib.gccSuffix()); |
| 1767 | assert(Suffix.front() == '/'); |
| 1768 | llvm::outs() << Suffix.substr(1) << "\n"; |
| 1769 | } |
| 1770 | return false; |
| 1771 | } |
| 1772 | |
| 1773 | if (C.getArgs().hasArg(options::OPT_print_target_triple)) { |
| 1774 | llvm::outs() << TC.getTripleString() << "\n"; |
| 1775 | return false; |
| 1776 | } |
| 1777 | |
| 1778 | if (C.getArgs().hasArg(options::OPT_print_effective_triple)) { |
| 1779 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(C.getArgs())); |
| 1780 | llvm::outs() << Triple.getTriple() << "\n"; |
| 1781 | return false; |
| 1782 | } |
| 1783 | |
| 1784 | return true; |
| 1785 | } |
| 1786 | |
| 1787 | |
| 1788 | |
| 1789 | |
| 1790 | static unsigned PrintActions1(const Compilation &C, Action *A, |
| 1791 | std::map<Action *, unsigned> &Ids) { |
| 1792 | if (Ids.count(A)) |
| 1793 | return Ids[A]; |
| 1794 | |
| 1795 | std::string str; |
| 1796 | llvm::raw_string_ostream os(str); |
| 1797 | |
| 1798 | os << Action::getClassName(A->getKind()) << ", "; |
| 1799 | if (InputAction *IA = dyn_cast<InputAction>(A)) { |
| 1800 | os << "\"" << IA->getInputArg().getValue() << "\""; |
| 1801 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(A)) { |
| 1802 | os << '"' << BIA->getArchName() << '"' << ", {" |
| 1803 | << PrintActions1(C, *BIA->input_begin(), Ids) << "}"; |
| 1804 | } else if (OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
| 1805 | bool IsFirst = true; |
| 1806 | OA->doOnEachDependence( |
| 1807 | [&](Action *A, const ToolChain *TC, const char *BoundArch) { |
| 1808 | |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | if (!IsFirst) |
| 1813 | os << ", "; |
| 1814 | os << '"'; |
| 1815 | if (TC) |
| 1816 | os << A->getOffloadingKindPrefix(); |
| 1817 | else |
| 1818 | os << "host"; |
| 1819 | os << " ("; |
| 1820 | os << TC->getTriple().normalize(); |
| 1821 | |
| 1822 | if (BoundArch) |
| 1823 | os << ":" << BoundArch; |
| 1824 | os << ")"; |
| 1825 | os << '"'; |
| 1826 | os << " {" << PrintActions1(C, A, Ids) << "}"; |
| 1827 | IsFirst = false; |
| 1828 | }); |
| 1829 | } else { |
| 1830 | const ActionList *AL = &A->getInputs(); |
| 1831 | |
| 1832 | if (AL->size()) { |
| 1833 | const char *Prefix = "{"; |
| 1834 | for (Action *PreRequisite : *AL) { |
| 1835 | os << Prefix << PrintActions1(C, PreRequisite, Ids); |
| 1836 | Prefix = ", "; |
| 1837 | } |
| 1838 | os << "}"; |
| 1839 | } else |
| 1840 | os << "{}"; |
| 1841 | } |
| 1842 | |
| 1843 | |
| 1844 | |
| 1845 | std::string offload_str; |
| 1846 | llvm::raw_string_ostream offload_os(offload_str); |
| 1847 | if (!isa<OffloadAction>(A)) { |
| 1848 | auto S = A->getOffloadingKindPrefix(); |
| 1849 | if (!S.empty()) { |
| 1850 | offload_os << ", (" << S; |
| 1851 | if (A->getOffloadingArch()) |
| 1852 | offload_os << ", " << A->getOffloadingArch(); |
| 1853 | offload_os << ")"; |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | unsigned Id = Ids.size(); |
| 1858 | Ids[A] = Id; |
| 1859 | llvm::errs() << Id << ": " << os.str() << ", " |
| 1860 | << types::getTypeName(A->getType()) << offload_os.str() << "\n"; |
| 1861 | |
| 1862 | return Id; |
| 1863 | } |
| 1864 | |
| 1865 | |
| 1866 | |
| 1867 | void Driver::PrintActions(const Compilation &C) const { |
| 1868 | std::map<Action *, unsigned> Ids; |
| 1869 | for (Action *A : C.getActions()) |
| 1870 | PrintActions1(C, A, Ids); |
| 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | |
| 1875 | static bool ContainsCompileOrAssembleAction(const Action *A) { |
| 1876 | if (isa<CompileJobAction>(A) || isa<BackendJobAction>(A) || |
| 1877 | isa<AssembleJobAction>(A)) |
| 1878 | return true; |
| 1879 | |
| 1880 | for (const Action *Input : A->inputs()) |
| 1881 | if (ContainsCompileOrAssembleAction(Input)) |
| 1882 | return true; |
| 1883 | |
| 1884 | return false; |
| 1885 | } |
| 1886 | |
| 1887 | void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC, |
| 1888 | const InputList &BAInputs) const { |
| 1889 | DerivedArgList &Args = C.getArgs(); |
| 1890 | ActionList &Actions = C.getActions(); |
| 1891 | llvm::PrettyStackTraceString CrashInfo("Building universal build actions"); |
| 1892 | |
| 1893 | |
| 1894 | llvm::StringSet<> ArchNames; |
| 1895 | SmallVector<const char *, 4> Archs; |
| 1896 | for (Arg *A : Args) { |
| 1897 | if (A->getOption().matches(options::OPT_arch)) { |
| 1898 | |
| 1899 | |
| 1900 | llvm::Triple::ArchType Arch = |
| 1901 | tools::darwin::getArchTypeForMachOArchName(A->getValue()); |
| 1902 | if (Arch == llvm::Triple::UnknownArch) { |
| 1903 | Diag(clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args); |
| 1904 | continue; |
| 1905 | } |
| 1906 | |
| 1907 | A->claim(); |
| 1908 | if (ArchNames.insert(A->getValue()).second) |
| 1909 | Archs.push_back(A->getValue()); |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | |
| 1914 | |
| 1915 | if (!Archs.size()) |
| 1916 | Archs.push_back(Args.MakeArgString(TC.getDefaultUniversalArchName())); |
| 1917 | |
| 1918 | ActionList SingleActions; |
| 1919 | BuildActions(C, Args, BAInputs, SingleActions); |
| 1920 | |
| 1921 | |
| 1922 | |
| 1923 | for (Action* Act : SingleActions) { |
| 1924 | |
| 1925 | |
| 1926 | |
| 1927 | |
| 1928 | |
| 1929 | |
| 1930 | if (Archs.size() > 1 && !types::canLipoType(Act->getType())) |
| 1931 | Diag(clang::diag::err_drv_invalid_output_with_multiple_archs) |
| 1932 | << types::getTypeName(Act->getType()); |
| 1933 | |
| 1934 | ActionList Inputs; |
| 1935 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
| 1936 | Inputs.push_back(C.MakeAction<BindArchAction>(Act, Archs[i])); |
| 1937 | |
| 1938 | |
| 1939 | |
| 1940 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
| 1941 | Actions.append(Inputs.begin(), Inputs.end()); |
| 1942 | else |
| 1943 | Actions.push_back(C.MakeAction<LipoJobAction>(Inputs, Act->getType())); |
| 1944 | |
| 1945 | |
| 1946 | Arg *A = Args.getLastArg(options::OPT_g_Group); |
| 1947 | if (A && !A->getOption().matches(options::OPT_g0) && |
| 1948 | !A->getOption().matches(options::OPT_gstabs) && |
| 1949 | ContainsCompileOrAssembleAction(Actions.back())) { |
| 1950 | |
| 1951 | |
| 1952 | |
| 1953 | |
| 1954 | |
| 1955 | if (Act->getType() == types::TY_Image) { |
| 1956 | ActionList Inputs; |
| 1957 | Inputs.push_back(Actions.back()); |
| 1958 | Actions.pop_back(); |
| 1959 | Actions.push_back( |
| 1960 | C.MakeAction<DsymutilJobAction>(Inputs, types::TY_dSYM)); |
| 1961 | } |
| 1962 | |
| 1963 | |
| 1964 | if (Args.hasArg(options::OPT_verify_debug_info)) { |
| 1965 | Action* LastAction = Actions.back(); |
| 1966 | Actions.pop_back(); |
| 1967 | Actions.push_back(C.MakeAction<VerifyDebugInfoJobAction>( |
| 1968 | LastAction, types::TY_Nothing)); |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | } |
| 1973 | |
| 1974 | |
| 1975 | |
| 1976 | static bool DiagnoseInputExistence(const Driver &D, const DerivedArgList &Args, |
| 1977 | StringRef Value, types::ID Ty) { |
| 1978 | if (!D.getCheckInputsExist()) |
| 1979 | return true; |
| 1980 | |
| 1981 | |
| 1982 | if (Value == "-") |
| 1983 | return true; |
| 1984 | |
| 1985 | SmallString<64> Path(Value); |
| 1986 | if (Arg *WorkDir = Args.getLastArg(options::OPT_working_directory)) { |
| 1987 | if (!llvm::sys::path::is_absolute(Path)) { |
| 1988 | SmallString<64> Directory(WorkDir->getValue()); |
| 1989 | llvm::sys::path::append(Directory, Value); |
| 1990 | Path.assign(Directory); |
| 1991 | } |
| 1992 | } |
| 1993 | |
| 1994 | if (D.getVFS().exists(Path)) |
| 1995 | return true; |
| 1996 | |
| 1997 | if (D.IsCLMode()) { |
| 1998 | if (!llvm::sys::path::is_absolute(Twine(Path)) && |
| 1999 | llvm::sys::Process::FindInEnvPath("LIB", Value)) |
| 2000 | return true; |
| 2001 | |
| 2002 | if (Args.hasArg(options::OPT__SLASH_link) && Ty == types::TY_Object) { |
| 2003 | |
| 2004 | |
| 2005 | |
| 2006 | return true; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | D.Diag(clang::diag::err_drv_no_such_file) << Path; |
| 2011 | return false; |
| 2012 | } |
| 2013 | |
| 2014 | |
| 2015 | void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args, |
| 2016 | InputList &Inputs) const { |
| 2017 | |
| 2018 | |
| 2019 | |
| 2020 | types::ID InputType = types::TY_Nothing; |
| 2021 | Arg *InputTypeArg = nullptr; |
| 2022 | |
| 2023 | |
| 2024 | if (Arg *TCTP = Args.getLastArgNoClaim(options::OPT__SLASH_TC, |
| 2025 | options::OPT__SLASH_TP)) { |
| 2026 | InputTypeArg = TCTP; |
| 2027 | InputType = TCTP->getOption().matches(options::OPT__SLASH_TC) |
| 2028 | ? types::TY_C |
| 2029 | : types::TY_CXX; |
| 2030 | |
| 2031 | Arg *Previous = nullptr; |
| 2032 | bool ShowNote = false; |
| 2033 | for (Arg *A : Args.filtered(options::OPT__SLASH_TC, options::OPT__SLASH_TP)) { |
| 2034 | if (Previous) { |
| 2035 | Diag(clang::diag::warn_drv_overriding_flag_option) |
| 2036 | << Previous->getSpelling() << A->getSpelling(); |
| 2037 | ShowNote = true; |
| 2038 | } |
| 2039 | Previous = A; |
| 2040 | } |
| 2041 | if (ShowNote) |
| 2042 | Diag(clang::diag::note_drv_t_option_is_global); |
| 2043 | |
| 2044 | |
| 2045 | (0) . __assert_fail ("!Args.hasArg(options..OPT_x) && \"-x and /TC or /TP is not allowed\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2045, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed"); |
| 2046 | } |
| 2047 | |
| 2048 | for (Arg *A : Args) { |
| 2049 | if (A->getOption().getKind() == Option::InputClass) { |
| 2050 | const char *Value = A->getValue(); |
| 2051 | types::ID Ty = types::TY_INVALID; |
| 2052 | |
| 2053 | |
| 2054 | if (InputType == types::TY_Nothing) { |
| 2055 | |
| 2056 | if (InputTypeArg) |
| 2057 | InputTypeArg->claim(); |
| 2058 | |
| 2059 | |
| 2060 | if (memcmp(Value, "-", 2) == 0) { |
| 2061 | |
| 2062 | |
| 2063 | |
| 2064 | |
| 2065 | |
| 2066 | if (!Args.hasArgNoClaim(options::OPT_E) && !CCCIsCPP()) |
| 2067 | Diag(IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl |
| 2068 | : clang::diag::err_drv_unknown_stdin_type); |
| 2069 | Ty = types::TY_C; |
| 2070 | } else { |
| 2071 | |
| 2072 | |
| 2073 | |
| 2074 | |
| 2075 | |
| 2076 | if (const char *Ext = strrchr(Value, '.')) |
| 2077 | Ty = TC.LookupTypeForExtension(Ext + 1); |
| 2078 | |
| 2079 | if (Ty == types::TY_INVALID) { |
| 2080 | if (CCCIsCPP()) |
| 2081 | Ty = types::TY_C; |
| 2082 | else if (IsCLMode() && Args.hasArgNoClaim(options::OPT_E)) |
| 2083 | Ty = types::TY_CXX; |
| 2084 | else |
| 2085 | Ty = types::TY_Object; |
| 2086 | } |
| 2087 | |
| 2088 | |
| 2089 | |
| 2090 | if (CCCIsCXX()) { |
| 2091 | types::ID OldTy = Ty; |
| 2092 | Ty = types::lookupCXXTypeForCType(Ty); |
| 2093 | |
| 2094 | if (Ty != OldTy) |
| 2095 | Diag(clang::diag::warn_drv_treating_input_as_cxx) |
| 2096 | << getTypeName(OldTy) << getTypeName(Ty); |
| 2097 | } |
| 2098 | } |
| 2099 | |
| 2100 | |
| 2101 | |
| 2102 | |
| 2103 | |
| 2104 | |
| 2105 | if (Ty != types::TY_Object) { |
| 2106 | if (Args.hasArg(options::OPT_ObjC)) |
| 2107 | Ty = types::TY_ObjC; |
| 2108 | else if (Args.hasArg(options::OPT_ObjCXX)) |
| 2109 | Ty = types::TY_ObjCXX; |
| 2110 | } |
| 2111 | } else { |
| 2112 | (0) . __assert_fail ("InputTypeArg && \"InputType set w/o InputTypeArg\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2112, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(InputTypeArg && "InputType set w/o InputTypeArg"); |
| 2113 | if (!InputTypeArg->getOption().matches(options::OPT_x)) { |
| 2114 | |
| 2115 | |
| 2116 | const char *Ext = strrchr(Value, '.'); |
| 2117 | if (Ext && TC.LookupTypeForExtension(Ext + 1) == types::TY_Object) |
| 2118 | Ty = types::TY_Object; |
| 2119 | } |
| 2120 | if (Ty == types::TY_INVALID) { |
| 2121 | Ty = InputType; |
| 2122 | InputTypeArg->claim(); |
| 2123 | } |
| 2124 | } |
| 2125 | |
| 2126 | if (DiagnoseInputExistence(*this, Args, Value, Ty)) |
| 2127 | Inputs.push_back(std::make_pair(Ty, A)); |
| 2128 | |
| 2129 | } else if (A->getOption().matches(options::OPT__SLASH_Tc)) { |
| 2130 | StringRef Value = A->getValue(); |
| 2131 | if (DiagnoseInputExistence(*this, Args, Value, types::TY_C)) { |
| 2132 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
| 2133 | Inputs.push_back(std::make_pair(types::TY_C, InputArg)); |
| 2134 | } |
| 2135 | A->claim(); |
| 2136 | } else if (A->getOption().matches(options::OPT__SLASH_Tp)) { |
| 2137 | StringRef Value = A->getValue(); |
| 2138 | if (DiagnoseInputExistence(*this, Args, Value, types::TY_CXX)) { |
| 2139 | Arg *InputArg = MakeInputArg(Args, *Opts, A->getValue()); |
| 2140 | Inputs.push_back(std::make_pair(types::TY_CXX, InputArg)); |
| 2141 | } |
| 2142 | A->claim(); |
| 2143 | } else if (A->getOption().hasFlag(options::LinkerInput)) { |
| 2144 | |
| 2145 | |
| 2146 | Inputs.push_back(std::make_pair(types::TY_Object, A)); |
| 2147 | |
| 2148 | } else if (A->getOption().matches(options::OPT_x)) { |
| 2149 | InputTypeArg = A; |
| 2150 | InputType = types::lookupTypeForTypeSpecifier(A->getValue()); |
| 2151 | A->claim(); |
| 2152 | |
| 2153 | |
| 2154 | |
| 2155 | |
| 2156 | if (!InputType) { |
| 2157 | Diag(clang::diag::err_drv_unknown_language) << A->getValue(); |
| 2158 | InputType = types::TY_Object; |
| 2159 | } |
| 2160 | } else if (A->getOption().getID() == options::OPT__SLASH_U) { |
| 2161 | (0) . __assert_fail ("A->getNumValues() == 1 && \"The /U option has one value.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2161, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(A->getNumValues() == 1 && "The /U option has one value."); |
| 2162 | StringRef Val = A->getValue(0); |
| 2163 | if (Val.find_first_of("/\\") != StringRef::npos) { |
| 2164 | |
| 2165 | Diag(diag::warn_slash_u_filename) << Val; |
| 2166 | Diag(diag::note_use_dashdash); |
| 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | if (CCCIsCPP() && Inputs.empty()) { |
| 2171 | |
| 2172 | |
| 2173 | Arg *A = MakeInputArg(Args, *Opts, "-"); |
| 2174 | Inputs.push_back(std::make_pair(types::TY_C, A)); |
| 2175 | } |
| 2176 | } |
| 2177 | |
| 2178 | namespace { |
| 2179 | |
| 2180 | |
| 2181 | class OffloadingActionBuilder final { |
| 2182 | |
| 2183 | bool IsValid = false; |
| 2184 | |
| 2185 | |
| 2186 | Compilation &C; |
| 2187 | |
| 2188 | |
| 2189 | std::map<const Arg *, unsigned> InputArgToOffloadKindMap; |
| 2190 | |
| 2191 | |
| 2192 | class DeviceActionBuilder { |
| 2193 | public: |
| 2194 | typedef llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PhasesTy; |
| 2195 | |
| 2196 | enum ActionBuilderReturnCode { |
| 2197 | |
| 2198 | ABRT_Success, |
| 2199 | |
| 2200 | ABRT_Inactive, |
| 2201 | |
| 2202 | |
| 2203 | ABRT_Ignore_Host, |
| 2204 | }; |
| 2205 | |
| 2206 | protected: |
| 2207 | |
| 2208 | Compilation &C; |
| 2209 | |
| 2210 | |
| 2211 | |
| 2212 | SmallVector<const ToolChain *, 2> ToolChains; |
| 2213 | |
| 2214 | |
| 2215 | DerivedArgList &Args; |
| 2216 | |
| 2217 | |
| 2218 | const Driver::InputList &Inputs; |
| 2219 | |
| 2220 | |
| 2221 | Action::OffloadKind AssociatedOffloadKind = Action::OFK_None; |
| 2222 | |
| 2223 | public: |
| 2224 | DeviceActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2225 | const Driver::InputList &Inputs, |
| 2226 | Action::OffloadKind AssociatedOffloadKind) |
| 2227 | : C(C), Args(Args), Inputs(Inputs), |
| 2228 | AssociatedOffloadKind(AssociatedOffloadKind) {} |
| 2229 | virtual ~DeviceActionBuilder() {} |
| 2230 | |
| 2231 | |
| 2232 | |
| 2233 | |
| 2234 | virtual ActionBuilderReturnCode |
| 2235 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2236 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2237 | PhasesTy &Phases) { |
| 2238 | return ABRT_Inactive; |
| 2239 | } |
| 2240 | |
| 2241 | |
| 2242 | |
| 2243 | virtual ActionBuilderReturnCode addDeviceDepences(Action *HostAction) { |
| 2244 | return ABRT_Inactive; |
| 2245 | } |
| 2246 | |
| 2247 | |
| 2248 | |
| 2249 | virtual void appendTopLevelActions(ActionList &AL) {} |
| 2250 | |
| 2251 | |
| 2252 | |
| 2253 | virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {} |
| 2254 | |
| 2255 | |
| 2256 | |
| 2257 | virtual bool initialize() { return false; } |
| 2258 | |
| 2259 | |
| 2260 | virtual bool canUseBundlerUnbundler() const { return false; } |
| 2261 | |
| 2262 | |
| 2263 | |
| 2264 | bool isValid() { return !ToolChains.empty(); } |
| 2265 | |
| 2266 | |
| 2267 | Action::OffloadKind getAssociatedOffloadKind() { |
| 2268 | return AssociatedOffloadKind; |
| 2269 | } |
| 2270 | }; |
| 2271 | |
| 2272 | |
| 2273 | |
| 2274 | class CudaActionBuilderBase : public DeviceActionBuilder { |
| 2275 | protected: |
| 2276 | |
| 2277 | |
| 2278 | bool CompileHostOnly = false; |
| 2279 | bool CompileDeviceOnly = false; |
| 2280 | |
| 2281 | |
| 2282 | SmallVector<CudaArch, 4> GpuArchList; |
| 2283 | |
| 2284 | |
| 2285 | ActionList CudaDeviceActions; |
| 2286 | |
| 2287 | |
| 2288 | Action *CudaFatBinary = nullptr; |
| 2289 | |
| 2290 | |
| 2291 | bool IsActive = false; |
| 2292 | |
| 2293 | |
| 2294 | bool Relocatable = false; |
| 2295 | public: |
| 2296 | CudaActionBuilderBase(Compilation &C, DerivedArgList &Args, |
| 2297 | const Driver::InputList &Inputs, |
| 2298 | Action::OffloadKind OFKind) |
| 2299 | : DeviceActionBuilder(C, Args, Inputs, OFKind) {} |
| 2300 | |
| 2301 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2302 | |
| 2303 | |
| 2304 | |
| 2305 | |
| 2306 | |
| 2307 | |
| 2308 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2309 | (0) . __assert_fail ("!GpuArchList.empty() && \"We should have at least one GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2310, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!GpuArchList.empty() && |
| 2310 | (0) . __assert_fail ("!GpuArchList.empty() && \"We should have at least one GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2310, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "We should have at least one GPU architecture."); |
| 2311 | |
| 2312 | |
| 2313 | |
| 2314 | if (IA->getType() != types::TY_CUDA && |
| 2315 | IA->getType() != types::TY_HIP) { |
| 2316 | |
| 2317 | IsActive = false; |
| 2318 | return ABRT_Inactive; |
| 2319 | } |
| 2320 | |
| 2321 | |
| 2322 | IsActive = true; |
| 2323 | |
| 2324 | if (CompileHostOnly) |
| 2325 | return ABRT_Success; |
| 2326 | |
| 2327 | |
| 2328 | auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE |
| 2329 | : types::TY_CUDA_DEVICE; |
| 2330 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2331 | CudaDeviceActions.push_back( |
| 2332 | C.MakeAction<InputAction>(IA->getInputArg(), Ty)); |
| 2333 | } |
| 2334 | |
| 2335 | return ABRT_Success; |
| 2336 | } |
| 2337 | |
| 2338 | |
| 2339 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2340 | |
| 2341 | |
| 2342 | |
| 2343 | if (!Relocatable) |
| 2344 | return ABRT_Inactive; |
| 2345 | |
| 2346 | CudaDeviceActions.clear(); |
| 2347 | auto *IA = cast<InputAction>(UA->getInputs().back()); |
| 2348 | std::string FileName = IA->getInputArg().getAsString(Args); |
| 2349 | |
| 2350 | |
| 2351 | |
| 2352 | if (IA->getType() == types::TY_Object && |
| 2353 | (!llvm::sys::path::has_extension(FileName) || |
| 2354 | types::lookupTypeForExtension( |
| 2355 | llvm::sys::path::extension(FileName).drop_front()) != |
| 2356 | types::TY_Object)) |
| 2357 | return ABRT_Inactive; |
| 2358 | |
| 2359 | for (auto Arch : GpuArchList) { |
| 2360 | CudaDeviceActions.push_back(UA); |
| 2361 | UA->registerDependentActionInfo(ToolChains[0], CudaArchToString(Arch), |
| 2362 | AssociatedOffloadKind); |
| 2363 | } |
| 2364 | return ABRT_Success; |
| 2365 | } |
| 2366 | |
| 2367 | return IsActive ? ABRT_Success : ABRT_Inactive; |
| 2368 | } |
| 2369 | |
| 2370 | void appendTopLevelActions(ActionList &AL) override { |
| 2371 | |
| 2372 | auto AddTopLevel = [&](Action *A, CudaArch BoundArch) { |
| 2373 | OffloadAction::DeviceDependences Dep; |
| 2374 | Dep.add(*A, *ToolChains.front(), CudaArchToString(BoundArch), |
| 2375 | AssociatedOffloadKind); |
| 2376 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2377 | }; |
| 2378 | |
| 2379 | |
| 2380 | if (CudaFatBinary) { |
| 2381 | AddTopLevel(CudaFatBinary, CudaArch::UNKNOWN); |
| 2382 | CudaDeviceActions.clear(); |
| 2383 | CudaFatBinary = nullptr; |
| 2384 | return; |
| 2385 | } |
| 2386 | |
| 2387 | if (CudaDeviceActions.empty()) |
| 2388 | return; |
| 2389 | |
| 2390 | |
| 2391 | |
| 2392 | |
| 2393 | (0) . __assert_fail ("CudaDeviceActions.size() == GpuArchList.size() && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2394, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2394 | (0) . __assert_fail ("CudaDeviceActions.size() == GpuArchList.size() && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2394, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expecting one action per GPU architecture."); |
| 2395 | (0) . __assert_fail ("ToolChains.size() == 1 && \"Expecting to have a sing CUDA toolchain.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ToolChains.size() == 1 && |
| 2396 | (0) . __assert_fail ("ToolChains.size() == 1 && \"Expecting to have a sing CUDA toolchain.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expecting to have a sing CUDA toolchain."); |
| 2397 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) |
| 2398 | AddTopLevel(CudaDeviceActions[I], GpuArchList[I]); |
| 2399 | |
| 2400 | CudaDeviceActions.clear(); |
| 2401 | } |
| 2402 | |
| 2403 | bool initialize() override { |
| 2404 | assert(AssociatedOffloadKind == Action::OFK_Cuda || |
| 2405 | AssociatedOffloadKind == Action::OFK_HIP); |
| 2406 | |
| 2407 | |
| 2408 | if (AssociatedOffloadKind == Action::OFK_Cuda && |
| 2409 | !C.hasOffloadToolChain<Action::OFK_Cuda>()) |
| 2410 | return false; |
| 2411 | |
| 2412 | |
| 2413 | if (AssociatedOffloadKind == Action::OFK_HIP && |
| 2414 | !C.hasOffloadToolChain<Action::OFK_HIP>()) |
| 2415 | return false; |
| 2416 | |
| 2417 | Relocatable = Args.hasFlag(options::OPT_fgpu_rdc, |
| 2418 | options::OPT_fno_gpu_rdc, ); |
| 2419 | |
| 2420 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
| 2421 | (0) . __assert_fail ("HostTC && \"No toolchain for host compilation.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2421, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HostTC && "No toolchain for host compilation."); |
| 2422 | if (HostTC->getTriple().isNVPTX() || |
| 2423 | HostTC->getTriple().getArch() == llvm::Triple::amdgcn) { |
| 2424 | |
| 2425 | |
| 2426 | |
| 2427 | C.getDriver().Diag(diag::err_drv_cuda_host_arch) |
| 2428 | << HostTC->getTriple().getArchName(); |
| 2429 | return true; |
| 2430 | } |
| 2431 | |
| 2432 | ToolChains.push_back( |
| 2433 | AssociatedOffloadKind == Action::OFK_Cuda |
| 2434 | ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() |
| 2435 | : C.getSingleOffloadToolChain<Action::OFK_HIP>()); |
| 2436 | |
| 2437 | Arg *PartialCompilationArg = Args.getLastArg( |
| 2438 | options::OPT_cuda_host_only, options::OPT_cuda_device_only, |
| 2439 | options::OPT_cuda_compile_host_device); |
| 2440 | CompileHostOnly = PartialCompilationArg && |
| 2441 | PartialCompilationArg->getOption().matches( |
| 2442 | options::OPT_cuda_host_only); |
| 2443 | CompileDeviceOnly = PartialCompilationArg && |
| 2444 | PartialCompilationArg->getOption().matches( |
| 2445 | options::OPT_cuda_device_only); |
| 2446 | |
| 2447 | |
| 2448 | std::set<CudaArch> GpuArchs; |
| 2449 | bool Error = false; |
| 2450 | for (Arg *A : Args) { |
| 2451 | if (!(A->getOption().matches(options::OPT_cuda_gpu_arch_EQ) || |
| 2452 | A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ))) |
| 2453 | continue; |
| 2454 | A->claim(); |
| 2455 | |
| 2456 | const StringRef ArchStr = A->getValue(); |
| 2457 | if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ) && |
| 2458 | ArchStr == "all") { |
| 2459 | GpuArchs.clear(); |
| 2460 | continue; |
| 2461 | } |
| 2462 | CudaArch Arch = StringToCudaArch(ArchStr); |
| 2463 | if (Arch == CudaArch::UNKNOWN) { |
| 2464 | C.getDriver().Diag(clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr; |
| 2465 | Error = true; |
| 2466 | } else if (A->getOption().matches(options::OPT_cuda_gpu_arch_EQ)) |
| 2467 | GpuArchs.insert(Arch); |
| 2468 | else if (A->getOption().matches(options::OPT_no_cuda_gpu_arch_EQ)) |
| 2469 | GpuArchs.erase(Arch); |
| 2470 | else |
| 2471 | llvm_unreachable("Unexpected option."); |
| 2472 | } |
| 2473 | |
| 2474 | |
| 2475 | for (CudaArch Arch : GpuArchs) |
| 2476 | GpuArchList.push_back(Arch); |
| 2477 | |
| 2478 | |
| 2479 | |
| 2480 | |
| 2481 | if (GpuArchList.empty()) |
| 2482 | GpuArchList.push_back(CudaArch::SM_20); |
| 2483 | |
| 2484 | return Error; |
| 2485 | } |
| 2486 | }; |
| 2487 | |
| 2488 | |
| 2489 | |
| 2490 | class CudaActionBuilder final : public CudaActionBuilderBase { |
| 2491 | public: |
| 2492 | CudaActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2493 | const Driver::InputList &Inputs) |
| 2494 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) {} |
| 2495 | |
| 2496 | ActionBuilderReturnCode |
| 2497 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2498 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2499 | PhasesTy &Phases) override { |
| 2500 | if (!IsActive) |
| 2501 | return ABRT_Inactive; |
| 2502 | |
| 2503 | |
| 2504 | |
| 2505 | if (CudaDeviceActions.empty()) |
| 2506 | return ABRT_Success; |
| 2507 | |
| 2508 | (0) . __assert_fail ("CudaDeviceActions.size() == GpuArchList.size() && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2509, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CudaDeviceActions.size() == GpuArchList.size() && |
| 2509 | (0) . __assert_fail ("CudaDeviceActions.size() == GpuArchList.size() && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2509, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expecting one action per GPU architecture."); |
| 2510 | (0) . __assert_fail ("!CompileHostOnly && \"Not expecting CUDA actions in host-only compilation.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2511, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CompileHostOnly && |
| 2511 | (0) . __assert_fail ("!CompileHostOnly && \"Not expecting CUDA actions in host-only compilation.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2511, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Not expecting CUDA actions in host-only compilation."); |
| 2512 | |
| 2513 | |
| 2514 | |
| 2515 | |
| 2516 | |
| 2517 | |
| 2518 | |
| 2519 | if (CompileDeviceOnly || CurPhase == phases::Backend) { |
| 2520 | ActionList DeviceActions; |
| 2521 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2522 | |
| 2523 | |
| 2524 | for (auto Ph : Phases) { |
| 2525 | |
| 2526 | if (Ph < CurPhase) |
| 2527 | continue; |
| 2528 | |
| 2529 | if (Ph > FinalPhase) |
| 2530 | break; |
| 2531 | |
| 2532 | CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction( |
| 2533 | C, Args, Ph, CudaDeviceActions[I], Action::OFK_Cuda); |
| 2534 | |
| 2535 | if (Ph == phases::Assemble) |
| 2536 | break; |
| 2537 | } |
| 2538 | |
| 2539 | |
| 2540 | |
| 2541 | |
| 2542 | if (!isa<AssembleJobAction>(CudaDeviceActions[I]) || |
| 2543 | CompileDeviceOnly) |
| 2544 | continue; |
| 2545 | |
| 2546 | Action *AssembleAction = CudaDeviceActions[I]; |
| 2547 | getType() == types..TY_Object", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2547, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AssembleAction->getType() == types::TY_Object); |
| 2548 | getInputs().size() == 1", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2548, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AssembleAction->getInputs().size() == 1); |
| 2549 | |
| 2550 | Action *BackendAction = AssembleAction->getInputs()[0]; |
| 2551 | getType() == types..TY_PP_Asm", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2551, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BackendAction->getType() == types::TY_PP_Asm); |
| 2552 | |
| 2553 | for (auto &A : {AssembleAction, BackendAction}) { |
| 2554 | OffloadAction::DeviceDependences DDep; |
| 2555 | DDep.add(*A, *ToolChains.front(), CudaArchToString(GpuArchList[I]), |
| 2556 | Action::OFK_Cuda); |
| 2557 | DeviceActions.push_back( |
| 2558 | C.MakeAction<OffloadAction>(DDep, A->getType())); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | |
| 2563 | if (!DeviceActions.empty()) { |
| 2564 | CudaFatBinary = |
| 2565 | C.MakeAction<LinkJobAction>(DeviceActions, types::TY_CUDA_FATBIN); |
| 2566 | |
| 2567 | if (!CompileDeviceOnly) { |
| 2568 | DA.add(*CudaFatBinary, *ToolChains.front(), , |
| 2569 | Action::OFK_Cuda); |
| 2570 | |
| 2571 | |
| 2572 | CudaFatBinary = nullptr; |
| 2573 | } |
| 2574 | |
| 2575 | |
| 2576 | |
| 2577 | CudaDeviceActions.clear(); |
| 2578 | } |
| 2579 | |
| 2580 | |
| 2581 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
| 2582 | } else if (CurPhase > phases::Backend) { |
| 2583 | |
| 2584 | |
| 2585 | |
| 2586 | return ABRT_Success; |
| 2587 | } |
| 2588 | |
| 2589 | (0) . __assert_fail ("CurPhase < phases..Backend && \"Generating single CUDA \" \"instructions should only occur \" \"before the backend phase!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2591, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurPhase < phases::Backend && "Generating single CUDA " |
| 2590 | (0) . __assert_fail ("CurPhase < phases..Backend && \"Generating single CUDA \" \"instructions should only occur \" \"before the backend phase!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2591, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "instructions should only occur " |
| 2591 | (0) . __assert_fail ("CurPhase < phases..Backend && \"Generating single CUDA \" \"instructions should only occur \" \"before the backend phase!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2591, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "before the backend phase!"); |
| 2592 | |
| 2593 | |
| 2594 | for (Action *&A : CudaDeviceActions) |
| 2595 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2596 | |
| 2597 | return ABRT_Success; |
| 2598 | } |
| 2599 | }; |
| 2600 | |
| 2601 | |
| 2602 | class HIPActionBuilder final : public CudaActionBuilderBase { |
| 2603 | |
| 2604 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
| 2605 | |
| 2606 | public: |
| 2607 | HIPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2608 | const Driver::InputList &Inputs) |
| 2609 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) {} |
| 2610 | |
| 2611 | bool canUseBundlerUnbundler() const override { return true; } |
| 2612 | |
| 2613 | ActionBuilderReturnCode |
| 2614 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2615 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2616 | PhasesTy &Phases) override { |
| 2617 | |
| 2618 | |
| 2619 | |
| 2620 | |
| 2621 | if (CudaDeviceActions.empty() || |
| 2622 | (CurPhase == phases::Backend && Relocatable) || |
| 2623 | CurPhase == phases::Assemble) |
| 2624 | return ABRT_Success; |
| 2625 | |
| 2626 | (0) . __assert_fail ("((CurPhase == phases..Link && Relocatable) || CudaDeviceActions.size() == GpuArchList.size()) && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2628, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(((CurPhase == phases::Link && Relocatable) || |
| 2627 | (0) . __assert_fail ("((CurPhase == phases..Link && Relocatable) || CudaDeviceActions.size() == GpuArchList.size()) && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2628, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> CudaDeviceActions.size() == GpuArchList.size()) && |
| 2628 | (0) . __assert_fail ("((CurPhase == phases..Link && Relocatable) || CudaDeviceActions.size() == GpuArchList.size()) && \"Expecting one action per GPU architecture.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2628, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expecting one action per GPU architecture."); |
| 2629 | (0) . __assert_fail ("!CompileHostOnly && \"Not expecting CUDA actions in host-only compilation.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2630, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CompileHostOnly && |
| 2630 | (0) . __assert_fail ("!CompileHostOnly && \"Not expecting CUDA actions in host-only compilation.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2630, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Not expecting CUDA actions in host-only compilation."); |
| 2631 | |
| 2632 | if (!Relocatable && CurPhase == phases::Backend) { |
| 2633 | |
| 2634 | |
| 2635 | |
| 2636 | |
| 2637 | |
| 2638 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
| 2639 | |
| 2640 | |
| 2641 | ActionList AL; |
| 2642 | AL.push_back(CudaDeviceActions[I]); |
| 2643 | CudaDeviceActions[I] = |
| 2644 | C.MakeAction<LinkJobAction>(AL, types::TY_Image); |
| 2645 | |
| 2646 | |
| 2647 | |
| 2648 | |
| 2649 | |
| 2650 | |
| 2651 | |
| 2652 | OffloadAction::DeviceDependences DDep; |
| 2653 | DDep.add(*CudaDeviceActions[I], *ToolChains.front(), |
| 2654 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2655 | CudaDeviceActions[I] = C.MakeAction<OffloadAction>( |
| 2656 | DDep, CudaDeviceActions[I]->getType()); |
| 2657 | } |
| 2658 | |
| 2659 | CudaFatBinary = |
| 2660 | C.MakeAction<LinkJobAction>(CudaDeviceActions, |
| 2661 | types::TY_HIP_FATBIN); |
| 2662 | |
| 2663 | if (!CompileDeviceOnly) { |
| 2664 | DA.add(*CudaFatBinary, *ToolChains.front(), , |
| 2665 | AssociatedOffloadKind); |
| 2666 | |
| 2667 | |
| 2668 | CudaFatBinary = nullptr; |
| 2669 | } |
| 2670 | |
| 2671 | |
| 2672 | |
| 2673 | CudaDeviceActions.clear(); |
| 2674 | |
| 2675 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
| 2676 | } else if (CurPhase == phases::Link) { |
| 2677 | |
| 2678 | |
| 2679 | |
| 2680 | |
| 2681 | |
| 2682 | DeviceLinkerInputs.resize(CudaDeviceActions.size()); |
| 2683 | auto LI = DeviceLinkerInputs.begin(); |
| 2684 | for (auto *A : CudaDeviceActions) { |
| 2685 | LI->push_back(A); |
| 2686 | ++LI; |
| 2687 | } |
| 2688 | |
| 2689 | |
| 2690 | |
| 2691 | CudaDeviceActions.clear(); |
| 2692 | return ABRT_Success; |
| 2693 | } |
| 2694 | |
| 2695 | |
| 2696 | for (Action *&A : CudaDeviceActions) |
| 2697 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A, |
| 2698 | AssociatedOffloadKind); |
| 2699 | |
| 2700 | return ABRT_Success; |
| 2701 | } |
| 2702 | |
| 2703 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2704 | |
| 2705 | unsigned I = 0; |
| 2706 | for (auto &LI : DeviceLinkerInputs) { |
| 2707 | auto *DeviceLinkAction = |
| 2708 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2709 | DA.add(*DeviceLinkAction, *ToolChains[0], |
| 2710 | CudaArchToString(GpuArchList[I]), AssociatedOffloadKind); |
| 2711 | ++I; |
| 2712 | } |
| 2713 | } |
| 2714 | }; |
| 2715 | |
| 2716 | |
| 2717 | |
| 2718 | class OpenMPActionBuilder final : public DeviceActionBuilder { |
| 2719 | |
| 2720 | ActionList OpenMPDeviceActions; |
| 2721 | |
| 2722 | |
| 2723 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
| 2724 | |
| 2725 | public: |
| 2726 | OpenMPActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2727 | const Driver::InputList &Inputs) |
| 2728 | : DeviceActionBuilder(C, Args, Inputs, Action::OFK_OpenMP) {} |
| 2729 | |
| 2730 | ActionBuilderReturnCode |
| 2731 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
| 2732 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2733 | PhasesTy &Phases) override { |
| 2734 | if (OpenMPDeviceActions.empty()) |
| 2735 | return ABRT_Inactive; |
| 2736 | |
| 2737 | |
| 2738 | (0) . __assert_fail ("OpenMPDeviceActions.size() == ToolChains.size() && \"Number of OpenMP actions and toolchains do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2739, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2739 | (0) . __assert_fail ("OpenMPDeviceActions.size() == ToolChains.size() && \"Number of OpenMP actions and toolchains do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2739, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Number of OpenMP actions and toolchains do not match."); |
| 2740 | |
| 2741 | |
| 2742 | |
| 2743 | if (CurPhase == phases::Link) { |
| 2744 | (0) . __assert_fail ("ToolChains.size() == DeviceLinkerInputs.size() && \"Toolchains and linker inputs sizes do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2745, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2745 | (0) . __assert_fail ("ToolChains.size() == DeviceLinkerInputs.size() && \"Toolchains and linker inputs sizes do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2745, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Toolchains and linker inputs sizes do not match."); |
| 2746 | auto LI = DeviceLinkerInputs.begin(); |
| 2747 | for (auto *A : OpenMPDeviceActions) { |
| 2748 | LI->push_back(A); |
| 2749 | ++LI; |
| 2750 | } |
| 2751 | |
| 2752 | |
| 2753 | |
| 2754 | OpenMPDeviceActions.clear(); |
| 2755 | return ABRT_Success; |
| 2756 | } |
| 2757 | |
| 2758 | |
| 2759 | for (Action *&A : OpenMPDeviceActions) |
| 2760 | A = C.getDriver().ConstructPhaseAction(C, Args, CurPhase, A); |
| 2761 | |
| 2762 | return ABRT_Success; |
| 2763 | } |
| 2764 | |
| 2765 | ActionBuilderReturnCode addDeviceDepences(Action *HostAction) override { |
| 2766 | |
| 2767 | |
| 2768 | if (auto *IA = dyn_cast<InputAction>(HostAction)) { |
| 2769 | OpenMPDeviceActions.clear(); |
| 2770 | for (unsigned I = 0; I < ToolChains.size(); ++I) |
| 2771 | OpenMPDeviceActions.push_back( |
| 2772 | C.MakeAction<InputAction>(IA->getInputArg(), IA->getType())); |
| 2773 | return ABRT_Success; |
| 2774 | } |
| 2775 | |
| 2776 | |
| 2777 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) { |
| 2778 | OpenMPDeviceActions.clear(); |
| 2779 | auto *IA = cast<InputAction>(UA->getInputs().back()); |
| 2780 | std::string FileName = IA->getInputArg().getAsString(Args); |
| 2781 | |
| 2782 | |
| 2783 | |
| 2784 | if (IA->getType() == types::TY_Object && |
| 2785 | (!llvm::sys::path::has_extension(FileName) || |
| 2786 | types::lookupTypeForExtension( |
| 2787 | llvm::sys::path::extension(FileName).drop_front()) != |
| 2788 | types::TY_Object)) |
| 2789 | return ABRT_Inactive; |
| 2790 | for (unsigned I = 0; I < ToolChains.size(); ++I) { |
| 2791 | OpenMPDeviceActions.push_back(UA); |
| 2792 | UA->registerDependentActionInfo( |
| 2793 | ToolChains[I], StringRef(), Action::OFK_OpenMP); |
| 2794 | } |
| 2795 | return ABRT_Success; |
| 2796 | } |
| 2797 | |
| 2798 | |
| 2799 | |
| 2800 | |
| 2801 | |
| 2802 | if (isa<CompileJobAction>(HostAction)) { |
| 2803 | HostAction->setCannotBeCollapsedWithNextDependentAction(); |
| 2804 | (0) . __assert_fail ("ToolChains.size() == OpenMPDeviceActions.size() && \"Toolchains and device action sizes do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2805, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ToolChains.size() == OpenMPDeviceActions.size() && |
| 2805 | (0) . __assert_fail ("ToolChains.size() == OpenMPDeviceActions.size() && \"Toolchains and device action sizes do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2805, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Toolchains and device action sizes do not match."); |
| 2806 | OffloadAction::HostDependence HDep( |
| 2807 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2808 | , Action::OFK_OpenMP); |
| 2809 | auto TC = ToolChains.begin(); |
| 2810 | for (Action *&A : OpenMPDeviceActions) { |
| 2811 | (A)", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2811, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<CompileJobAction>(A)); |
| 2812 | OffloadAction::DeviceDependences DDep; |
| 2813 | DDep.add(*A, **TC, , Action::OFK_OpenMP); |
| 2814 | A = C.MakeAction<OffloadAction>(HDep, DDep); |
| 2815 | ++TC; |
| 2816 | } |
| 2817 | } |
| 2818 | return ABRT_Success; |
| 2819 | } |
| 2820 | |
| 2821 | void appendTopLevelActions(ActionList &AL) override { |
| 2822 | if (OpenMPDeviceActions.empty()) |
| 2823 | return; |
| 2824 | |
| 2825 | |
| 2826 | (0) . __assert_fail ("OpenMPDeviceActions.size() == ToolChains.size() && \"Number of OpenMP actions and toolchains do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2827, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OpenMPDeviceActions.size() == ToolChains.size() && |
| 2827 | (0) . __assert_fail ("OpenMPDeviceActions.size() == ToolChains.size() && \"Number of OpenMP actions and toolchains do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2827, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Number of OpenMP actions and toolchains do not match."); |
| 2828 | |
| 2829 | |
| 2830 | auto TI = ToolChains.begin(); |
| 2831 | for (auto *A : OpenMPDeviceActions) { |
| 2832 | OffloadAction::DeviceDependences Dep; |
| 2833 | Dep.add(*A, **TI, , Action::OFK_OpenMP); |
| 2834 | AL.push_back(C.MakeAction<OffloadAction>(Dep, A->getType())); |
| 2835 | ++TI; |
| 2836 | } |
| 2837 | |
| 2838 | OpenMPDeviceActions.clear(); |
| 2839 | } |
| 2840 | |
| 2841 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override { |
| 2842 | (0) . __assert_fail ("ToolChains.size() == DeviceLinkerInputs.size() && \"Toolchains and linker inputs sizes do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2843, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ToolChains.size() == DeviceLinkerInputs.size() && |
| 2843 | (0) . __assert_fail ("ToolChains.size() == DeviceLinkerInputs.size() && \"Toolchains and linker inputs sizes do not match.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2843, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Toolchains and linker inputs sizes do not match."); |
| 2844 | |
| 2845 | |
| 2846 | auto TC = ToolChains.begin(); |
| 2847 | for (auto &LI : DeviceLinkerInputs) { |
| 2848 | auto *DeviceLinkAction = |
| 2849 | C.MakeAction<LinkJobAction>(LI, types::TY_Image); |
| 2850 | DA.add(*DeviceLinkAction, **TC, , |
| 2851 | Action::OFK_OpenMP); |
| 2852 | ++TC; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | bool initialize() override { |
| 2857 | |
| 2858 | |
| 2859 | auto OpenMPTCRange = C.getOffloadToolChains<Action::OFK_OpenMP>(); |
| 2860 | for (auto TI = OpenMPTCRange.first, TE = OpenMPTCRange.second; TI != TE; |
| 2861 | ++TI) |
| 2862 | ToolChains.push_back(TI->second); |
| 2863 | |
| 2864 | DeviceLinkerInputs.resize(ToolChains.size()); |
| 2865 | return false; |
| 2866 | } |
| 2867 | |
| 2868 | bool canUseBundlerUnbundler() const override { |
| 2869 | |
| 2870 | return true; |
| 2871 | } |
| 2872 | }; |
| 2873 | |
| 2874 | |
| 2875 | |
| 2876 | |
| 2877 | |
| 2878 | |
| 2879 | SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders; |
| 2880 | |
| 2881 | |
| 2882 | bool CanUseBundler; |
| 2883 | |
| 2884 | public: |
| 2885 | OffloadingActionBuilder(Compilation &C, DerivedArgList &Args, |
| 2886 | const Driver::InputList &Inputs) |
| 2887 | : C(C) { |
| 2888 | |
| 2889 | |
| 2890 | IsValid = true; |
| 2891 | |
| 2892 | |
| 2893 | SpecializedBuilders.push_back(new CudaActionBuilder(C, Args, Inputs)); |
| 2894 | |
| 2895 | |
| 2896 | SpecializedBuilders.push_back(new HIPActionBuilder(C, Args, Inputs)); |
| 2897 | |
| 2898 | |
| 2899 | SpecializedBuilders.push_back(new OpenMPActionBuilder(C, Args, Inputs)); |
| 2900 | |
| 2901 | |
| 2902 | |
| 2903 | |
| 2904 | |
| 2905 | |
| 2906 | |
| 2907 | unsigned ValidBuilders = 0u; |
| 2908 | unsigned = 0u; |
| 2909 | for (auto *SB : SpecializedBuilders) { |
| 2910 | IsValid = IsValid && !SB->initialize(); |
| 2911 | |
| 2912 | |
| 2913 | if (SB->isValid()) { |
| 2914 | ++ValidBuilders; |
| 2915 | if (SB->canUseBundlerUnbundler()) |
| 2916 | ++ValidBuildersSupportingBundling; |
| 2917 | } |
| 2918 | } |
| 2919 | CanUseBundler = |
| 2920 | ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling; |
| 2921 | } |
| 2922 | |
| 2923 | ~OffloadingActionBuilder() { |
| 2924 | for (auto *SB : SpecializedBuilders) |
| 2925 | delete SB; |
| 2926 | } |
| 2927 | |
| 2928 | |
| 2929 | |
| 2930 | |
| 2931 | |
| 2932 | Action * |
| 2933 | addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg, |
| 2934 | phases::ID CurPhase, phases::ID FinalPhase, |
| 2935 | DeviceActionBuilder::PhasesTy &Phases) { |
| 2936 | if (!IsValid) |
| 2937 | return nullptr; |
| 2938 | |
| 2939 | if (SpecializedBuilders.empty()) |
| 2940 | return HostAction; |
| 2941 | |
| 2942 | (0) . __assert_fail ("HostAction && \"Invalid host action!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 2942, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HostAction && "Invalid host action!"); |
| 2943 | |
| 2944 | OffloadAction::DeviceDependences DDeps; |
| 2945 | |
| 2946 | |
| 2947 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 2948 | unsigned InactiveBuilders = 0u; |
| 2949 | unsigned IgnoringBuilders = 0u; |
| 2950 | for (auto *SB : SpecializedBuilders) { |
| 2951 | if (!SB->isValid()) { |
| 2952 | ++InactiveBuilders; |
| 2953 | continue; |
| 2954 | } |
| 2955 | |
| 2956 | auto RetCode = |
| 2957 | SB->getDeviceDependences(DDeps, CurPhase, FinalPhase, Phases); |
| 2958 | |
| 2959 | |
| 2960 | |
| 2961 | |
| 2962 | if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host) |
| 2963 | ++IgnoringBuilders; |
| 2964 | |
| 2965 | |
| 2966 | |
| 2967 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 2968 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 2969 | } |
| 2970 | |
| 2971 | |
| 2972 | |
| 2973 | if (IgnoringBuilders && |
| 2974 | SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders)) |
| 2975 | return nullptr; |
| 2976 | |
| 2977 | if (DDeps.getActions().empty()) |
| 2978 | return HostAction; |
| 2979 | |
| 2980 | |
| 2981 | |
| 2982 | OffloadAction::HostDependence HDep( |
| 2983 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 2984 | , DDeps); |
| 2985 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
| 2986 | } |
| 2987 | |
| 2988 | |
| 2989 | |
| 2990 | |
| 2991 | bool addHostDependenceToDeviceActions(Action *&HostAction, |
| 2992 | const Arg *InputArg) { |
| 2993 | if (!IsValid) |
| 2994 | return true; |
| 2995 | |
| 2996 | |
| 2997 | |
| 2998 | |
| 2999 | |
| 3000 | |
| 3001 | |
| 3002 | if (CanUseBundler && isa<InputAction>(HostAction) && |
| 3003 | InputArg->getOption().getKind() == llvm::opt::Option::InputClass && |
| 3004 | !types::isSrcFile(HostAction->getType())) { |
| 3005 | auto UnbundlingHostAction = |
| 3006 | C.MakeAction<OffloadUnbundlingJobAction>(HostAction); |
| 3007 | UnbundlingHostAction->registerDependentActionInfo( |
| 3008 | C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 3009 | StringRef(), Action::OFK_Host); |
| 3010 | HostAction = UnbundlingHostAction; |
| 3011 | } |
| 3012 | |
| 3013 | (0) . __assert_fail ("HostAction && \"Invalid host action!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3013, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HostAction && "Invalid host action!"); |
| 3014 | |
| 3015 | |
| 3016 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
| 3017 | for (auto *SB : SpecializedBuilders) { |
| 3018 | if (!SB->isValid()) |
| 3019 | continue; |
| 3020 | |
| 3021 | auto RetCode = SB->addDeviceDepences(HostAction); |
| 3022 | |
| 3023 | |
| 3024 | |
| 3025 | (0) . __assert_fail ("RetCode != DeviceActionBuilder..ABRT_Ignore_Host && \"Host dependence not expected to be ignored.!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3026, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host && |
| 3026 | (0) . __assert_fail ("RetCode != DeviceActionBuilder..ABRT_Ignore_Host && \"Host dependence not expected to be ignored.!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3026, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Host dependence not expected to be ignored.!"); |
| 3027 | |
| 3028 | |
| 3029 | |
| 3030 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
| 3031 | OffloadKind |= SB->getAssociatedOffloadKind(); |
| 3032 | } |
| 3033 | |
| 3034 | |
| 3035 | if (OffloadKind == Action::OFK_None && CanUseBundler) |
| 3036 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(HostAction)) |
| 3037 | HostAction = UA->getInputs().back(); |
| 3038 | |
| 3039 | return false; |
| 3040 | } |
| 3041 | |
| 3042 | |
| 3043 | |
| 3044 | |
| 3045 | bool appendTopLevelActions(ActionList &AL, Action *HostAction, |
| 3046 | const Arg *InputArg) { |
| 3047 | |
| 3048 | ActionList OffloadAL; |
| 3049 | for (auto *SB : SpecializedBuilders) { |
| 3050 | if (!SB->isValid()) |
| 3051 | continue; |
| 3052 | SB->appendTopLevelActions(OffloadAL); |
| 3053 | } |
| 3054 | |
| 3055 | |
| 3056 | |
| 3057 | |
| 3058 | |
| 3059 | if (CanUseBundler && HostAction && !OffloadAL.empty()) { |
| 3060 | |
| 3061 | OffloadAL.push_back(HostAction); |
| 3062 | |
| 3063 | |
| 3064 | |
| 3065 | (0) . __assert_fail ("HostAction == AL.back() && \"Host action not in the list??\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3065, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HostAction == AL.back() && "Host action not in the list??"); |
| 3066 | HostAction = C.MakeAction<OffloadBundlingJobAction>(OffloadAL); |
| 3067 | AL.back() = HostAction; |
| 3068 | } else |
| 3069 | AL.append(OffloadAL.begin(), OffloadAL.end()); |
| 3070 | |
| 3071 | |
| 3072 | |
| 3073 | if (HostAction) |
| 3074 | HostAction->propagateHostOffloadInfo(InputArgToOffloadKindMap[InputArg], |
| 3075 | ); |
| 3076 | return false; |
| 3077 | } |
| 3078 | |
| 3079 | |
| 3080 | |
| 3081 | |
| 3082 | |
| 3083 | Action *processHostLinkAction(Action *HostAction) { |
| 3084 | |
| 3085 | OffloadAction::DeviceDependences DDeps; |
| 3086 | for (auto *SB : SpecializedBuilders) { |
| 3087 | if (!SB->isValid()) |
| 3088 | continue; |
| 3089 | |
| 3090 | SB->appendLinkDependences(DDeps); |
| 3091 | } |
| 3092 | |
| 3093 | |
| 3094 | unsigned ActiveOffloadKinds = 0u; |
| 3095 | for (auto &I : InputArgToOffloadKindMap) |
| 3096 | ActiveOffloadKinds |= I.second; |
| 3097 | |
| 3098 | |
| 3099 | |
| 3100 | if (DDeps.getActions().empty()) { |
| 3101 | |
| 3102 | |
| 3103 | HostAction->propagateHostOffloadInfo(ActiveOffloadKinds, |
| 3104 | ); |
| 3105 | return HostAction; |
| 3106 | } |
| 3107 | |
| 3108 | |
| 3109 | |
| 3110 | |
| 3111 | OffloadAction::HostDependence HDep( |
| 3112 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
| 3113 | nullptr, ActiveOffloadKinds); |
| 3114 | return C.MakeAction<OffloadAction>(HDep, DDeps); |
| 3115 | } |
| 3116 | }; |
| 3117 | } |
| 3118 | |
| 3119 | void Driver::BuildActions(Compilation &C, DerivedArgList &Args, |
| 3120 | const InputList &Inputs, ActionList &Actions) const { |
| 3121 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions"); |
| 3122 | |
| 3123 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
| 3124 | Diag(clang::diag::err_drv_no_input_files); |
| 3125 | return; |
| 3126 | } |
| 3127 | |
| 3128 | Arg *FinalPhaseArg; |
| 3129 | phases::ID FinalPhase = getFinalPhase(Args, &FinalPhaseArg); |
| 3130 | |
| 3131 | if (FinalPhase == phases::Link) { |
| 3132 | if (Args.hasArg(options::OPT_emit_llvm)) |
| 3133 | Diag(clang::diag::err_drv_emit_llvm_link); |
| 3134 | if (IsCLMode() && LTOMode != LTOK_None && |
| 3135 | !Args.getLastArgValue(options::OPT_fuse_ld_EQ).equals_lower("lld")) |
| 3136 | Diag(clang::diag::err_drv_lto_without_lld); |
| 3137 | } |
| 3138 | |
| 3139 | |
| 3140 | |
| 3141 | if (Arg *A = Args.getLastArg(options::OPT_Z_Joined)) |
| 3142 | Diag(clang::diag::err_drv_use_of_Z_option) << A->getAsString(Args); |
| 3143 | |
| 3144 | |
| 3145 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fo)) { |
| 3146 | StringRef V = A->getValue(); |
| 3147 | if (Inputs.size() > 1 && !V.empty() && |
| 3148 | !llvm::sys::path::is_separator(V.back())) { |
| 3149 | |
| 3150 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
| 3151 | << A->getSpelling() << V; |
| 3152 | Args.eraseArg(options::OPT__SLASH_Fo); |
| 3153 | } |
| 3154 | } |
| 3155 | |
| 3156 | |
| 3157 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_Fa)) { |
| 3158 | StringRef V = A->getValue(); |
| 3159 | if (Inputs.size() > 1 && !V.empty() && |
| 3160 | !llvm::sys::path::is_separator(V.back())) { |
| 3161 | |
| 3162 | Diag(clang::diag::err_drv_out_file_argument_with_multiple_sources) |
| 3163 | << A->getSpelling() << V; |
| 3164 | Args.eraseArg(options::OPT__SLASH_Fa); |
| 3165 | } |
| 3166 | } |
| 3167 | |
| 3168 | |
| 3169 | if (Arg *A = Args.getLastArg(options::OPT__SLASH_o)) { |
| 3170 | if (A->getValue()[0] == '\0') { |
| 3171 | |
| 3172 | Diag(clang::diag::err_drv_missing_argument) << A->getSpelling() << 1; |
| 3173 | Args.eraseArg(options::OPT__SLASH_o); |
| 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | |
| 3178 | Arg *YcArg = Args.getLastArg(options::OPT__SLASH_Yc); |
| 3179 | Arg *YuArg = Args.getLastArg(options::OPT__SLASH_Yu); |
| 3180 | if (YcArg && YuArg && strcmp(YcArg->getValue(), YuArg->getValue()) != 0) { |
| 3181 | Diag(clang::diag::warn_drv_ycyu_different_arg_clang_cl); |
| 3182 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3183 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3184 | YcArg = YuArg = nullptr; |
| 3185 | } |
| 3186 | if (YcArg && Inputs.size() > 1) { |
| 3187 | Diag(clang::diag::warn_drv_yc_multiple_inputs_clang_cl); |
| 3188 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3189 | YcArg = nullptr; |
| 3190 | } |
| 3191 | if (FinalPhase == phases::Preprocess || Args.hasArg(options::OPT__SLASH_Y_)) { |
| 3192 | |
| 3193 | |
| 3194 | |
| 3195 | Args.eraseArg(options::OPT__SLASH_Fp); |
| 3196 | Args.eraseArg(options::OPT__SLASH_Yc); |
| 3197 | Args.eraseArg(options::OPT__SLASH_Yu); |
| 3198 | YcArg = YuArg = nullptr; |
| 3199 | } |
| 3200 | |
| 3201 | |
| 3202 | OffloadingActionBuilder OffloadBuilder(C, Args, Inputs); |
| 3203 | |
| 3204 | |
| 3205 | HeaderModulePrecompileJobAction * = nullptr; |
| 3206 | ActionList LinkerInputs; |
| 3207 | |
| 3208 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PL; |
| 3209 | for (auto &I : Inputs) { |
| 3210 | types::ID InputType = I.first; |
| 3211 | const Arg *InputArg = I.second; |
| 3212 | |
| 3213 | PL.clear(); |
| 3214 | types::getCompilationPhases(InputType, PL); |
| 3215 | |
| 3216 | |
| 3217 | |
| 3218 | phases::ID InitialPhase = PL[0]; |
| 3219 | if (InitialPhase > FinalPhase) { |
| 3220 | if (InputArg->isClaimed()) |
| 3221 | continue; |
| 3222 | |
| 3223 | |
| 3224 | InputArg->claim(); |
| 3225 | |
| 3226 | |
| 3227 | if (Args.hasArg(options::OPT_Qunused_arguments)) |
| 3228 | continue; |
| 3229 | |
| 3230 | |
| 3231 | |
| 3232 | if (CCCIsCPP()) |
| 3233 | Diag(clang::diag::warn_drv_input_file_unused_by_cpp) |
| 3234 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase); |
| 3235 | |
| 3236 | |
| 3237 | else if (InitialPhase == phases::Compile && |
| 3238 | FinalPhase == phases::Preprocess && |
| 3239 | getPreprocessedType(InputType) == types::TY_INVALID) |
| 3240 | Diag(clang::diag::warn_drv_preprocessed_input_file_unused) |
| 3241 | << InputArg->getAsString(Args) << !!FinalPhaseArg |
| 3242 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
| 3243 | else |
| 3244 | Diag(clang::diag::warn_drv_input_file_unused) |
| 3245 | << InputArg->getAsString(Args) << getPhaseName(InitialPhase) |
| 3246 | << !!FinalPhaseArg |
| 3247 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : ""); |
| 3248 | continue; |
| 3249 | } |
| 3250 | |
| 3251 | if (YcArg) { |
| 3252 | |
| 3253 | if (FinalPhase >= phases::Compile) { |
| 3254 | const types::ID HeaderType = lookupHeaderTypeForSourceType(InputType); |
| 3255 | llvm::SmallVector<phases::ID, phases::MaxNumberOfPhases> PCHPL; |
| 3256 | types::getCompilationPhases(HeaderType, PCHPL); |
| 3257 | |
| 3258 | Action *ClangClPch = |
| 3259 | C.MakeAction<InputAction>(*InputArg, HeaderType); |
| 3260 | for (phases::ID Phase : PCHPL) |
| 3261 | ClangClPch = ConstructPhaseAction(C, Args, Phase, ClangClPch); |
| 3262 | assert(ClangClPch); |
| 3263 | Actions.push_back(ClangClPch); |
| 3264 | |
| 3265 | |
| 3266 | |
| 3267 | |
| 3268 | |
| 3269 | } |
| 3270 | } |
| 3271 | |
| 3272 | |
| 3273 | Action *Current = C.MakeAction<InputAction>(*InputArg, InputType); |
| 3274 | |
| 3275 | |
| 3276 | |
| 3277 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3278 | break; |
| 3279 | |
| 3280 | for (SmallVectorImpl<phases::ID>::iterator i = PL.begin(), e = PL.end(); |
| 3281 | i != e; ++i) { |
| 3282 | phases::ID Phase = *i; |
| 3283 | |
| 3284 | |
| 3285 | if (Phase > FinalPhase) |
| 3286 | break; |
| 3287 | |
| 3288 | |
| 3289 | Current = OffloadBuilder.addDeviceDependencesToHostAction( |
| 3290 | Current, InputArg, Phase, FinalPhase, PL); |
| 3291 | if (!Current) |
| 3292 | break; |
| 3293 | |
| 3294 | |
| 3295 | if (Phase == phases::Link) { |
| 3296 | (0) . __assert_fail ("(i + 1) == e && \"linking must be final compilation step.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3296, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((i + 1) == e && "linking must be final compilation step."); |
| 3297 | LinkerInputs.push_back(Current); |
| 3298 | Current = nullptr; |
| 3299 | break; |
| 3300 | } |
| 3301 | |
| 3302 | |
| 3303 | |
| 3304 | |
| 3305 | if (Phase == phases::Precompile && HeaderModuleAction && |
| 3306 | getPrecompiledType(InputType) == types::TY_PCH) { |
| 3307 | HeaderModuleAction->addModuleHeaderInput(Current); |
| 3308 | Current = nullptr; |
| 3309 | break; |
| 3310 | } |
| 3311 | |
| 3312 | |
| 3313 | |
| 3314 | |
| 3315 | |
| 3316 | Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Current); |
| 3317 | |
| 3318 | |
| 3319 | if (NewCurrent == Current) |
| 3320 | continue; |
| 3321 | |
| 3322 | if (auto *HMA = dyn_cast<HeaderModulePrecompileJobAction>(NewCurrent)) |
| 3323 | HeaderModuleAction = HMA; |
| 3324 | |
| 3325 | Current = NewCurrent; |
| 3326 | |
| 3327 | |
| 3328 | |
| 3329 | if (OffloadBuilder.addHostDependenceToDeviceActions(Current, InputArg)) |
| 3330 | break; |
| 3331 | |
| 3332 | if (Current->getType() == types::TY_Nothing) |
| 3333 | break; |
| 3334 | } |
| 3335 | |
| 3336 | |
| 3337 | if (Current) |
| 3338 | Actions.push_back(Current); |
| 3339 | |
| 3340 | |
| 3341 | OffloadBuilder.appendTopLevelActions(Actions, Current, InputArg); |
| 3342 | } |
| 3343 | |
| 3344 | |
| 3345 | if (!LinkerInputs.empty()) { |
| 3346 | Action *LA = C.MakeAction<LinkJobAction>(LinkerInputs, types::TY_Image); |
| 3347 | LA = OffloadBuilder.processHostLinkAction(LA); |
| 3348 | Actions.push_back(LA); |
| 3349 | } |
| 3350 | |
| 3351 | |
| 3352 | |
| 3353 | if (FinalPhase == phases::Link && PL.size() == 1) { |
| 3354 | Args.ClaimAllArgs(options::OPT_CompileOnly_Group); |
| 3355 | Args.ClaimAllArgs(options::OPT_cl_compile_Group); |
| 3356 | } |
| 3357 | |
| 3358 | |
| 3359 | Args.ClaimAllArgs(options::OPT_cl_ignored_Group); |
| 3360 | |
| 3361 | |
| 3362 | |
| 3363 | Args.ClaimAllArgs(options::OPT_cuda_host_only); |
| 3364 | Args.ClaimAllArgs(options::OPT_cuda_compile_host_device); |
| 3365 | } |
| 3366 | |
| 3367 | Action *Driver::ConstructPhaseAction( |
| 3368 | Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input, |
| 3369 | Action::OffloadKind TargetDeviceOffloadKind) const { |
| 3370 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions"); |
| 3371 | |
| 3372 | |
| 3373 | |
| 3374 | |
| 3375 | if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm) |
| 3376 | return Input; |
| 3377 | |
| 3378 | |
| 3379 | switch (Phase) { |
| 3380 | case phases::Link: |
| 3381 | llvm_unreachable("link action invalid here."); |
| 3382 | case phases::Preprocess: { |
| 3383 | types::ID OutputTy; |
| 3384 | |
| 3385 | if (Args.hasArg(options::OPT_M, options::OPT_MM)) { |
| 3386 | OutputTy = types::TY_Dependencies; |
| 3387 | } else { |
| 3388 | OutputTy = Input->getType(); |
| 3389 | if (!Args.hasFlag(options::OPT_frewrite_includes, |
| 3390 | options::OPT_fno_rewrite_includes, false) && |
| 3391 | !Args.hasFlag(options::OPT_frewrite_imports, |
| 3392 | options::OPT_fno_rewrite_imports, false) && |
| 3393 | !CCGenDiagnostics) |
| 3394 | OutputTy = types::getPreprocessedType(OutputTy); |
| 3395 | (0) . __assert_fail ("OutputTy != types..TY_INVALID && \"Cannot preprocess this input type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OutputTy != types::TY_INVALID && |
| 3396 | (0) . __assert_fail ("OutputTy != types..TY_INVALID && \"Cannot preprocess this input type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3396, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Cannot preprocess this input type!"); |
| 3397 | } |
| 3398 | return C.MakeAction<PreprocessJobAction>(Input, OutputTy); |
| 3399 | } |
| 3400 | case phases::Precompile: { |
| 3401 | types::ID OutputTy = getPrecompiledType(Input->getType()); |
| 3402 | (0) . __assert_fail ("OutputTy != types..TY_INVALID && \"Cannot precompile this input type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3403, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(OutputTy != types::TY_INVALID && |
| 3403 | (0) . __assert_fail ("OutputTy != types..TY_INVALID && \"Cannot precompile this input type!\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3403, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Cannot precompile this input type!"); |
| 3404 | |
| 3405 | |
| 3406 | |
| 3407 | const char *ModName = nullptr; |
| 3408 | if (OutputTy == types::TY_PCH) { |
| 3409 | if (Arg *A = Args.getLastArg(options::OPT_fmodule_name_EQ)) |
| 3410 | ModName = A->getValue(); |
| 3411 | if (ModName) |
| 3412 | OutputTy = types::TY_ModuleFile; |
| 3413 | } |
| 3414 | |
| 3415 | if (Args.hasArg(options::OPT_fsyntax_only)) { |
| 3416 | |
| 3417 | OutputTy = types::TY_Nothing; |
| 3418 | } |
| 3419 | |
| 3420 | if (ModName) |
| 3421 | return C.MakeAction<HeaderModulePrecompileJobAction>(Input, OutputTy, |
| 3422 | ModName); |
| 3423 | return C.MakeAction<PrecompileJobAction>(Input, OutputTy); |
| 3424 | } |
| 3425 | case phases::Compile: { |
| 3426 | if (Args.hasArg(options::OPT_fsyntax_only)) |
| 3427 | return C.MakeAction<CompileJobAction>(Input, types::TY_Nothing); |
| 3428 | if (Args.hasArg(options::OPT_rewrite_objc)) |
| 3429 | return C.MakeAction<CompileJobAction>(Input, types::TY_RewrittenObjC); |
| 3430 | if (Args.hasArg(options::OPT_rewrite_legacy_objc)) |
| 3431 | return C.MakeAction<CompileJobAction>(Input, |
| 3432 | types::TY_RewrittenLegacyObjC); |
| 3433 | if (Args.hasArg(options::OPT__analyze, options::OPT__analyze_auto)) |
| 3434 | return C.MakeAction<AnalyzeJobAction>(Input, types::TY_Plist); |
| 3435 | if (Args.hasArg(options::OPT__migrate)) |
| 3436 | return C.MakeAction<MigrateJobAction>(Input, types::TY_Remap); |
| 3437 | if (Args.hasArg(options::OPT_emit_ast)) |
| 3438 | return C.MakeAction<CompileJobAction>(Input, types::TY_AST); |
| 3439 | if (Args.hasArg(options::OPT_module_file_info)) |
| 3440 | return C.MakeAction<CompileJobAction>(Input, types::TY_ModuleFile); |
| 3441 | if (Args.hasArg(options::OPT_verify_pch)) |
| 3442 | return C.MakeAction<VerifyPCHJobAction>(Input, types::TY_Nothing); |
| 3443 | return C.MakeAction<CompileJobAction>(Input, types::TY_LLVM_BC); |
| 3444 | } |
| 3445 | case phases::Backend: { |
| 3446 | if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) { |
| 3447 | types::ID Output = |
| 3448 | Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC; |
| 3449 | return C.MakeAction<BackendJobAction>(Input, Output); |
| 3450 | } |
| 3451 | if (Args.hasArg(options::OPT_emit_llvm)) { |
| 3452 | types::ID Output = |
| 3453 | Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC; |
| 3454 | return C.MakeAction<BackendJobAction>(Input, Output); |
| 3455 | } |
| 3456 | return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm); |
| 3457 | } |
| 3458 | case phases::Assemble: |
| 3459 | return C.MakeAction<AssembleJobAction>(std::move(Input), types::TY_Object); |
| 3460 | } |
| 3461 | |
| 3462 | llvm_unreachable("invalid phase in ConstructPhaseAction"); |
| 3463 | } |
| 3464 | |
| 3465 | void Driver::BuildJobs(Compilation &C) const { |
| 3466 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
| 3467 | |
| 3468 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 3469 | |
| 3470 | |
| 3471 | |
| 3472 | if (FinalOutput) { |
| 3473 | unsigned NumOutputs = 0; |
| 3474 | for (const Action *A : C.getActions()) |
| 3475 | if (A->getType() != types::TY_Nothing) |
| 3476 | ++NumOutputs; |
| 3477 | |
| 3478 | if (NumOutputs > 1) { |
| 3479 | Diag(clang::diag::err_drv_output_argument_with_multiple_files); |
| 3480 | FinalOutput = nullptr; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | |
| 3485 | llvm::StringSet<> ArchNames; |
| 3486 | if (C.getDefaultToolChain().getTriple().isOSBinFormatMachO()) |
| 3487 | for (const Arg *A : C.getArgs()) |
| 3488 | if (A->getOption().matches(options::OPT_arch)) |
| 3489 | ArchNames.insert(A->getValue()); |
| 3490 | |
| 3491 | |
| 3492 | std::map<std::pair<const Action *, std::string>, InputInfo> CachedResults; |
| 3493 | for (Action *A : C.getActions()) { |
| 3494 | |
| 3495 | |
| 3496 | |
| 3497 | |
| 3498 | |
| 3499 | |
| 3500 | const char *LinkingOutput = nullptr; |
| 3501 | if (isa<LipoJobAction>(A)) { |
| 3502 | if (FinalOutput) |
| 3503 | LinkingOutput = FinalOutput->getValue(); |
| 3504 | else |
| 3505 | LinkingOutput = getDefaultImageName(); |
| 3506 | } |
| 3507 | |
| 3508 | BuildJobsForAction(C, A, &C.getDefaultToolChain(), |
| 3509 | StringRef(), |
| 3510 | true, |
| 3511 | ArchNames.size() > 1, |
| 3512 | LinkingOutput, CachedResults, |
| 3513 | Action::OFK_None); |
| 3514 | } |
| 3515 | |
| 3516 | |
| 3517 | |
| 3518 | if (Diags.hasErrorOccurred() || |
| 3519 | C.getArgs().hasArg(options::OPT_Qunused_arguments)) |
| 3520 | return; |
| 3521 | |
| 3522 | |
| 3523 | (void)C.getArgs().hasArg(options::OPT__HASH_HASH_HASH); |
| 3524 | |
| 3525 | |
| 3526 | (void)C.getArgs().hasArg(options::OPT_driver_mode); |
| 3527 | (void)C.getArgs().hasArg(options::OPT_rsp_quoting); |
| 3528 | |
| 3529 | for (Arg *A : C.getArgs()) { |
| 3530 | |
| 3531 | |
| 3532 | |
| 3533 | if (!A->isClaimed()) { |
| 3534 | if (A->getOption().hasFlag(options::NoArgumentUnused)) |
| 3535 | continue; |
| 3536 | |
| 3537 | |
| 3538 | |
| 3539 | const Option &Opt = A->getOption(); |
| 3540 | if (Opt.getKind() == Option::FlagClass) { |
| 3541 | bool DuplicateClaimed = false; |
| 3542 | |
| 3543 | for (const Arg *AA : C.getArgs().filtered(&Opt)) { |
| 3544 | if (AA->isClaimed()) { |
| 3545 | DuplicateClaimed = true; |
| 3546 | break; |
| 3547 | } |
| 3548 | } |
| 3549 | |
| 3550 | if (DuplicateClaimed) |
| 3551 | continue; |
| 3552 | } |
| 3553 | |
| 3554 | |
| 3555 | |
| 3556 | if (!IsCLMode() || !A->getOption().matches(options::OPT_UNKNOWN)) |
| 3557 | Diag(clang::diag::warn_drv_unused_argument) |
| 3558 | << A->getAsString(C.getArgs()); |
| 3559 | } |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | namespace { |
| 3564 | |
| 3565 | |
| 3566 | class ToolSelector final { |
| 3567 | |
| 3568 | const ToolChain &TC; |
| 3569 | |
| 3570 | |
| 3571 | const Compilation &C; |
| 3572 | |
| 3573 | |
| 3574 | const JobAction *BaseAction; |
| 3575 | |
| 3576 | |
| 3577 | bool IsHostSelector; |
| 3578 | |
| 3579 | |
| 3580 | bool SaveTemps; |
| 3581 | bool EmbedBitcode; |
| 3582 | |
| 3583 | |
| 3584 | |
| 3585 | |
| 3586 | const JobAction *getPrevDependentAction(const ActionList &Inputs, |
| 3587 | ActionList &SavedOffloadAction, |
| 3588 | bool CanBeCollapsed = true) { |
| 3589 | |
| 3590 | if (Inputs.size() != 1) |
| 3591 | return nullptr; |
| 3592 | |
| 3593 | Action *CurAction = *Inputs.begin(); |
| 3594 | if (CanBeCollapsed && |
| 3595 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3596 | return nullptr; |
| 3597 | |
| 3598 | |
| 3599 | |
| 3600 | if (auto *OA = dyn_cast<OffloadAction>(CurAction)) { |
| 3601 | |
| 3602 | |
| 3603 | |
| 3604 | if (!IsHostSelector) { |
| 3605 | if (OA->hasSingleDeviceDependence()) { |
| 3606 | CurAction = |
| 3607 | OA->getSingleDeviceDependence(); |
| 3608 | if (CanBeCollapsed && |
| 3609 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3610 | return nullptr; |
| 3611 | SavedOffloadAction.push_back(OA); |
| 3612 | return dyn_cast<JobAction>(CurAction); |
| 3613 | } |
| 3614 | } else if (OA->hasHostDependence()) { |
| 3615 | CurAction = OA->getHostDependence(); |
| 3616 | if (CanBeCollapsed && |
| 3617 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
| 3618 | return nullptr; |
| 3619 | SavedOffloadAction.push_back(OA); |
| 3620 | return dyn_cast<JobAction>(CurAction); |
| 3621 | } |
| 3622 | return nullptr; |
| 3623 | } |
| 3624 | |
| 3625 | return dyn_cast<JobAction>(CurAction); |
| 3626 | } |
| 3627 | |
| 3628 | |
| 3629 | bool canCollapseAssembleAction() const { |
| 3630 | return TC.useIntegratedAs() && !SaveTemps && |
| 3631 | !C.getArgs().hasArg(options::OPT_via_file_asm) && |
| 3632 | !C.getArgs().hasArg(options::OPT__SLASH_FA) && |
| 3633 | !C.getArgs().hasArg(options::OPT__SLASH_Fa); |
| 3634 | } |
| 3635 | |
| 3636 | |
| 3637 | bool canCollapsePreprocessorAction() const { |
| 3638 | return !C.getArgs().hasArg(options::OPT_no_integrated_cpp) && |
| 3639 | !C.getArgs().hasArg(options::OPT_traditional_cpp) && !SaveTemps && |
| 3640 | !C.getArgs().hasArg(options::OPT_rewrite_objc); |
| 3641 | } |
| 3642 | |
| 3643 | |
| 3644 | |
| 3645 | struct JobActionInfo final { |
| 3646 | |
| 3647 | const JobAction *JA = nullptr; |
| 3648 | |
| 3649 | |
| 3650 | ActionList SavedOffloadAction; |
| 3651 | }; |
| 3652 | |
| 3653 | |
| 3654 | |
| 3655 | static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction, |
| 3656 | ArrayRef<JobActionInfo> &ActionInfo, |
| 3657 | unsigned ElementNum) { |
| 3658 | (0) . __assert_fail ("ElementNum <= ActionInfo.size() && \"Invalid number of elements.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3658, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ElementNum <= ActionInfo.size() && "Invalid number of elements."); |
| 3659 | for (unsigned I = 0; I < ElementNum; ++I) |
| 3660 | CollapsedOffloadAction.append(ActionInfo[I].SavedOffloadAction.begin(), |
| 3661 | ActionInfo[I].SavedOffloadAction.end()); |
| 3662 | } |
| 3663 | |
| 3664 | |
| 3665 | |
| 3666 | |
| 3667 | |
| 3668 | |
| 3669 | |
| 3670 | |
| 3671 | |
| 3672 | |
| 3673 | const Tool * |
| 3674 | combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
| 3675 | ActionList &Inputs, |
| 3676 | ActionList &CollapsedOffloadAction) { |
| 3677 | if (ActionInfo.size() < 3 || !canCollapseAssembleAction()) |
| 3678 | return nullptr; |
| 3679 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3680 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3681 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[2].JA); |
| 3682 | if (!AJ || !BJ || !CJ) |
| 3683 | return nullptr; |
| 3684 | |
| 3685 | |
| 3686 | const Tool *T = TC.SelectTool(*CJ); |
| 3687 | if (!T) |
| 3688 | return nullptr; |
| 3689 | |
| 3690 | |
| 3691 | |
| 3692 | if (EmbedBitcode) { |
| 3693 | const Tool *BT = TC.SelectTool(*BJ); |
| 3694 | if (BT == T) |
| 3695 | return nullptr; |
| 3696 | } |
| 3697 | |
| 3698 | if (!T->hasIntegratedAssembler()) |
| 3699 | return nullptr; |
| 3700 | |
| 3701 | Inputs = CJ->getInputs(); |
| 3702 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3703 | ); |
| 3704 | return T; |
| 3705 | } |
| 3706 | const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo, |
| 3707 | ActionList &Inputs, |
| 3708 | ActionList &CollapsedOffloadAction) { |
| 3709 | if (ActionInfo.size() < 2 || !canCollapseAssembleAction()) |
| 3710 | return nullptr; |
| 3711 | auto *AJ = dyn_cast<AssembleJobAction>(ActionInfo[0].JA); |
| 3712 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[1].JA); |
| 3713 | if (!AJ || !BJ) |
| 3714 | return nullptr; |
| 3715 | |
| 3716 | |
| 3717 | ActionList CompileJobOffloadActions; |
| 3718 | auto *CJ = getPrevDependentAction(BJ->getInputs(), CompileJobOffloadActions, |
| 3719 | ); |
| 3720 | if (!AJ || !BJ || !CJ) |
| 3721 | return nullptr; |
| 3722 | |
| 3723 | (0) . __assert_fail ("isa(CJ) && \"Expecting compile job preceding backend job.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3724, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isa<CompileJobAction>(CJ) && |
| 3724 | (0) . __assert_fail ("isa(CJ) && \"Expecting compile job preceding backend job.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3724, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Expecting compile job preceding backend job."); |
| 3725 | |
| 3726 | |
| 3727 | const Tool *T = TC.SelectTool(*CJ); |
| 3728 | if (!T) |
| 3729 | return nullptr; |
| 3730 | |
| 3731 | if (!T->hasIntegratedAssembler()) |
| 3732 | return nullptr; |
| 3733 | |
| 3734 | Inputs = BJ->getInputs(); |
| 3735 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3736 | ); |
| 3737 | return T; |
| 3738 | } |
| 3739 | const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
| 3740 | ActionList &Inputs, |
| 3741 | ActionList &CollapsedOffloadAction) { |
| 3742 | if (ActionInfo.size() < 2) |
| 3743 | return nullptr; |
| 3744 | auto *BJ = dyn_cast<BackendJobAction>(ActionInfo[0].JA); |
| 3745 | auto *CJ = dyn_cast<CompileJobAction>(ActionInfo[1].JA); |
| 3746 | if (!BJ || !CJ) |
| 3747 | return nullptr; |
| 3748 | |
| 3749 | |
| 3750 | |
| 3751 | |
| 3752 | |
| 3753 | |
| 3754 | bool InputIsBitcode = true; |
| 3755 | for (size_t i = 1; i < ActionInfo.size(); i++) |
| 3756 | if (ActionInfo[i].JA->getType() != types::TY_LLVM_BC && |
| 3757 | ActionInfo[i].JA->getType() != types::TY_LTO_BC) { |
| 3758 | InputIsBitcode = false; |
| 3759 | break; |
| 3760 | } |
| 3761 | if (!InputIsBitcode && !canCollapsePreprocessorAction()) |
| 3762 | return nullptr; |
| 3763 | |
| 3764 | |
| 3765 | const Tool *T = TC.SelectTool(*CJ); |
| 3766 | if (!T) |
| 3767 | return nullptr; |
| 3768 | |
| 3769 | if (T->canEmitIR() && ((SaveTemps && !InputIsBitcode) || EmbedBitcode)) |
| 3770 | return nullptr; |
| 3771 | |
| 3772 | Inputs = CJ->getInputs(); |
| 3773 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
| 3774 | ); |
| 3775 | return T; |
| 3776 | } |
| 3777 | |
| 3778 | |
| 3779 | |
| 3780 | |
| 3781 | |
| 3782 | void combineWithPreprocessor(const Tool *T, ActionList &Inputs, |
| 3783 | ActionList &CollapsedOffloadAction) { |
| 3784 | if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP()) |
| 3785 | return; |
| 3786 | |
| 3787 | |
| 3788 | ActionList PreprocessJobOffloadActions; |
| 3789 | ActionList NewInputs; |
| 3790 | for (Action *A : Inputs) { |
| 3791 | auto *PJ = getPrevDependentAction({A}, PreprocessJobOffloadActions); |
| 3792 | if (!PJ || !isa<PreprocessJobAction>(PJ)) { |
| 3793 | NewInputs.push_back(A); |
| 3794 | continue; |
| 3795 | } |
| 3796 | |
| 3797 | |
| 3798 | |
| 3799 | CollapsedOffloadAction.append(PreprocessJobOffloadActions.begin(), |
| 3800 | PreprocessJobOffloadActions.end()); |
| 3801 | NewInputs.append(PJ->input_begin(), PJ->input_end()); |
| 3802 | } |
| 3803 | Inputs = NewInputs; |
| 3804 | } |
| 3805 | |
| 3806 | public: |
| 3807 | ToolSelector(const JobAction *BaseAction, const ToolChain &TC, |
| 3808 | const Compilation &C, bool SaveTemps, bool EmbedBitcode) |
| 3809 | : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps), |
| 3810 | EmbedBitcode(EmbedBitcode) { |
| 3811 | (0) . __assert_fail ("BaseAction && \"Invalid base action.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 3811, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(BaseAction && "Invalid base action."); |
| 3812 | IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None; |
| 3813 | } |
| 3814 | |
| 3815 | |
| 3816 | |
| 3817 | |
| 3818 | |
| 3819 | |
| 3820 | |
| 3821 | const Tool *getTool(ActionList &Inputs, |
| 3822 | ActionList &CollapsedOffloadAction) { |
| 3823 | |
| 3824 | |
| 3825 | |
| 3826 | |
| 3827 | SmallVector<JobActionInfo, 5> ActionChain(1); |
| 3828 | ActionChain.back().JA = BaseAction; |
| 3829 | while (ActionChain.back().JA) { |
| 3830 | const Action *CurAction = ActionChain.back().JA; |
| 3831 | |
| 3832 | |
| 3833 | ActionChain.resize(ActionChain.size() + 1); |
| 3834 | JobActionInfo &AI = ActionChain.back(); |
| 3835 | |
| 3836 | |
| 3837 | AI.JA = |
| 3838 | getPrevDependentAction(CurAction->getInputs(), AI.SavedOffloadAction); |
| 3839 | } |
| 3840 | |
| 3841 | |
| 3842 | ActionChain.pop_back(); |
| 3843 | |
| 3844 | |
| 3845 | |
| 3846 | |
| 3847 | |
| 3848 | |
| 3849 | |
| 3850 | const Tool *T = combineAssembleBackendCompile(ActionChain, Inputs, |
| 3851 | CollapsedOffloadAction); |
| 3852 | if (!T) |
| 3853 | T = combineAssembleBackend(ActionChain, Inputs, CollapsedOffloadAction); |
| 3854 | if (!T) |
| 3855 | T = combineBackendCompile(ActionChain, Inputs, CollapsedOffloadAction); |
| 3856 | if (!T) { |
| 3857 | Inputs = BaseAction->getInputs(); |
| 3858 | T = TC.SelectTool(*BaseAction); |
| 3859 | } |
| 3860 | |
| 3861 | combineWithPreprocessor(T, Inputs, CollapsedOffloadAction); |
| 3862 | return T; |
| 3863 | } |
| 3864 | }; |
| 3865 | } |
| 3866 | |
| 3867 | |
| 3868 | |
| 3869 | |
| 3870 | |
| 3871 | |
| 3872 | static std::string GetTriplePlusArchString(const ToolChain *TC, |
| 3873 | StringRef BoundArch, |
| 3874 | Action::OffloadKind OffloadKind) { |
| 3875 | std::string TriplePlusArch = TC->getTriple().normalize(); |
| 3876 | if (!BoundArch.empty()) { |
| 3877 | TriplePlusArch += "-"; |
| 3878 | TriplePlusArch += BoundArch; |
| 3879 | } |
| 3880 | TriplePlusArch += "-"; |
| 3881 | TriplePlusArch += Action::GetOffloadKindName(OffloadKind); |
| 3882 | return TriplePlusArch; |
| 3883 | } |
| 3884 | |
| 3885 | InputInfo Driver::BuildJobsForAction( |
| 3886 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
| 3887 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
| 3888 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
| 3889 | Action::OffloadKind TargetDeviceOffloadKind) const { |
| 3890 | std::pair<const Action *, std::string> ActionTC = { |
| 3891 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
| 3892 | auto CachedResult = CachedResults.find(ActionTC); |
| 3893 | if (CachedResult != CachedResults.end()) { |
| 3894 | return CachedResult->second; |
| 3895 | } |
| 3896 | InputInfo Result = BuildJobsForActionNoCache( |
| 3897 | C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput, |
| 3898 | CachedResults, TargetDeviceOffloadKind); |
| 3899 | CachedResults[ActionTC] = Result; |
| 3900 | return Result; |
| 3901 | } |
| 3902 | |
| 3903 | InputInfo Driver::BuildJobsForActionNoCache( |
| 3904 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
| 3905 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
| 3906 | std::map<std::pair<const Action *, std::string>, InputInfo> &CachedResults, |
| 3907 | Action::OffloadKind TargetDeviceOffloadKind) const { |
| 3908 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs"); |
| 3909 | |
| 3910 | InputInfoList OffloadDependencesInputInfo; |
| 3911 | bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None; |
| 3912 | if (const OffloadAction *OA = dyn_cast<OffloadAction>(A)) { |
| 3913 | |
| 3914 | |
| 3915 | |
| 3916 | |
| 3917 | C.getArgsForToolChain(TC, BoundArch, Action::OFK_None); |
| 3918 | |
| 3919 | |
| 3920 | |
| 3921 | |
| 3922 | |
| 3923 | |
| 3924 | |
| 3925 | |
| 3926 | |
| 3927 | |
| 3928 | |
| 3929 | |
| 3930 | |
| 3931 | |
| 3932 | |
| 3933 | |
| 3934 | |
| 3935 | |
| 3936 | |
| 3937 | |
| 3938 | |
| 3939 | |
| 3940 | |
| 3941 | |
| 3942 | |
| 3943 | if (OA->hasSingleDeviceDependence()) { |
| 3944 | InputInfo DevA; |
| 3945 | OA->doOnEachDeviceDependence([&](Action *DepA, const ToolChain *DepTC, |
| 3946 | const char *DepBoundArch) { |
| 3947 | DevA = |
| 3948 | BuildJobsForAction(C, DepA, DepTC, DepBoundArch, AtTopLevel, |
| 3949 | !!DepBoundArch, LinkingOutput, |
| 3950 | CachedResults, DepA->getOffloadingDeviceKind()); |
| 3951 | }); |
| 3952 | return DevA; |
| 3953 | } |
| 3954 | |
| 3955 | |
| 3956 | |
| 3957 | |
| 3958 | |
| 3959 | OA->doOnEachDependence( |
| 3960 | , |
| 3961 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 3962 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
| 3963 | C, DepA, DepTC, DepBoundArch, , |
| 3964 | !!DepBoundArch, LinkingOutput, CachedResults, |
| 3965 | DepA->getOffloadingDeviceKind())); |
| 3966 | }); |
| 3967 | |
| 3968 | A = BuildingForOffloadDevice |
| 3969 | ? OA->getSingleDeviceDependence() |
| 3970 | : OA->getHostDependence(); |
| 3971 | } |
| 3972 | |
| 3973 | if (const InputAction *IA = dyn_cast<InputAction>(A)) { |
| 3974 | |
| 3975 | |
| 3976 | const Arg &Input = IA->getInputArg(); |
| 3977 | Input.claim(); |
| 3978 | if (Input.getOption().matches(options::OPT_INPUT)) { |
| 3979 | const char *Name = Input.getValue(); |
| 3980 | return InputInfo(A, Name, Name); |
| 3981 | } |
| 3982 | return InputInfo(A, &Input, ""); |
| 3983 | } |
| 3984 | |
| 3985 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(A)) { |
| 3986 | const ToolChain *TC; |
| 3987 | StringRef ArchName = BAA->getArchName(); |
| 3988 | |
| 3989 | if (!ArchName.empty()) |
| 3990 | TC = &getToolChain(C.getArgs(), |
| 3991 | computeTargetTriple(*this, TargetTriple, |
| 3992 | C.getArgs(), ArchName)); |
| 3993 | else |
| 3994 | TC = &C.getDefaultToolChain(); |
| 3995 | |
| 3996 | return BuildJobsForAction(C, *BAA->input_begin(), TC, ArchName, AtTopLevel, |
| 3997 | MultipleArchs, LinkingOutput, CachedResults, |
| 3998 | TargetDeviceOffloadKind); |
| 3999 | } |
| 4000 | |
| 4001 | |
| 4002 | ActionList Inputs = A->getInputs(); |
| 4003 | |
| 4004 | const JobAction *JA = cast<JobAction>(A); |
| 4005 | ActionList CollapsedOffloadActions; |
| 4006 | |
| 4007 | ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), |
| 4008 | embedBitcodeInObject() && !isUsingLTO()); |
| 4009 | const Tool *T = TS.getTool(Inputs, CollapsedOffloadActions); |
| 4010 | |
| 4011 | if (!T) |
| 4012 | return InputInfo(); |
| 4013 | |
| 4014 | |
| 4015 | |
| 4016 | for (const auto *OA : CollapsedOffloadActions) |
| 4017 | cast<OffloadAction>(OA)->doOnEachDependence( |
| 4018 | BuildingForOffloadDevice, |
| 4019 | [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
| 4020 | OffloadDependencesInputInfo.push_back(BuildJobsForAction( |
| 4021 | C, DepA, DepTC, DepBoundArch, false, |
| 4022 | !!DepBoundArch, LinkingOutput, CachedResults, |
| 4023 | DepA->getOffloadingDeviceKind())); |
| 4024 | }); |
| 4025 | |
| 4026 | |
| 4027 | InputInfoList InputInfos; |
| 4028 | for (const Action *Input : Inputs) { |
| 4029 | |
| 4030 | |
| 4031 | |
| 4032 | bool SubJobAtTopLevel = |
| 4033 | AtTopLevel && (isa<DsymutilJobAction>(A) || isa<VerifyJobAction>(A)); |
| 4034 | InputInfos.push_back(BuildJobsForAction( |
| 4035 | C, Input, TC, BoundArch, SubJobAtTopLevel, MultipleArchs, LinkingOutput, |
| 4036 | CachedResults, A->getOffloadingDeviceKind())); |
| 4037 | } |
| 4038 | |
| 4039 | |
| 4040 | const char *BaseInput = InputInfos[0].getBaseInput(); |
| 4041 | |
| 4042 | |
| 4043 | |
| 4044 | if (JA->getType() == types::TY_dSYM) |
| 4045 | BaseInput = InputInfos[0].getFilename(); |
| 4046 | |
| 4047 | |
| 4048 | if (auto *ModuleJA = dyn_cast<HeaderModulePrecompileJobAction>(JA)) |
| 4049 | BaseInput = ModuleJA->getModuleName(); |
| 4050 | |
| 4051 | |
| 4052 | if (!OffloadDependencesInputInfo.empty()) |
| 4053 | InputInfos.append(OffloadDependencesInputInfo.begin(), |
| 4054 | OffloadDependencesInputInfo.end()); |
| 4055 | |
| 4056 | |
| 4057 | llvm::Triple EffectiveTriple; |
| 4058 | const ToolChain &ToolTC = T->getToolChain(); |
| 4059 | const ArgList &Args = |
| 4060 | C.getArgsForToolChain(TC, BoundArch, A->getOffloadingDeviceKind()); |
| 4061 | if (InputInfos.size() != 1) { |
| 4062 | EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args)); |
| 4063 | } else { |
| 4064 | |
| 4065 | EffectiveTriple = llvm::Triple( |
| 4066 | ToolTC.ComputeEffectiveClangTriple(Args, InputInfos[0].getType())); |
| 4067 | } |
| 4068 | RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple); |
| 4069 | |
| 4070 | |
| 4071 | InputInfo Result; |
| 4072 | InputInfoList UnbundlingResults; |
| 4073 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(JA)) { |
| 4074 | |
| 4075 | |
| 4076 | |
| 4077 | for (auto &UI : UA->getDependentActionsInfo()) { |
| 4078 | (0) . __assert_fail ("UI.DependentOffloadKind != Action..OFK_None && \"Unbundling with no offloading??\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 4079, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(UI.DependentOffloadKind != Action::OFK_None && |
| 4079 | (0) . __assert_fail ("UI.DependentOffloadKind != Action..OFK_None && \"Unbundling with no offloading??\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 4079, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Unbundling with no offloading??"); |
| 4080 | |
| 4081 | |
| 4082 | |
| 4083 | |
| 4084 | |
| 4085 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 4086 | UI.DependentOffloadKind, |
| 4087 | UI.DependentToolChain->getTriple().normalize(), |
| 4088 | ); |
| 4089 | auto CurI = InputInfo( |
| 4090 | UA, |
| 4091 | GetNamedOutputPath(C, *UA, BaseInput, UI.DependentBoundArch, |
| 4092 | , |
| 4093 | MultipleArchs || |
| 4094 | UI.DependentOffloadKind == Action::OFK_HIP, |
| 4095 | OffloadingPrefix), |
| 4096 | BaseInput); |
| 4097 | |
| 4098 | UnbundlingResults.push_back(CurI); |
| 4099 | |
| 4100 | |
| 4101 | |
| 4102 | StringRef Arch; |
| 4103 | if (TargetDeviceOffloadKind == Action::OFK_HIP) { |
| 4104 | if (UI.DependentOffloadKind == Action::OFK_Host) |
| 4105 | Arch = StringRef(); |
| 4106 | else |
| 4107 | Arch = UI.DependentBoundArch; |
| 4108 | } else |
| 4109 | Arch = BoundArch; |
| 4110 | |
| 4111 | CachedResults[{A, GetTriplePlusArchString(UI.DependentToolChain, Arch, |
| 4112 | UI.DependentOffloadKind)}] = |
| 4113 | CurI; |
| 4114 | } |
| 4115 | |
| 4116 | |
| 4117 | |
| 4118 | std::pair<const Action *, std::string> ActionTC = { |
| 4119 | A, GetTriplePlusArchString(TC, BoundArch, TargetDeviceOffloadKind)}; |
| 4120 | (0) . __assert_fail ("CachedResults.find(ActionTC) != CachedResults.end() && \"Result does not exist??\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 4121, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CachedResults.find(ActionTC) != CachedResults.end() && |
| 4121 | (0) . __assert_fail ("CachedResults.find(ActionTC) != CachedResults.end() && \"Result does not exist??\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 4121, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Result does not exist??"); |
| 4122 | Result = CachedResults[ActionTC]; |
| 4123 | } else if (JA->getType() == types::TY_Nothing) |
| 4124 | Result = InputInfo(A, BaseInput); |
| 4125 | else { |
| 4126 | |
| 4127 | |
| 4128 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
| 4129 | A->getOffloadingDeviceKind(), TC->getTriple().normalize(), |
| 4130 | !!A->getOffloadingHostActiveKinds() && |
| 4131 | !AtTopLevel); |
| 4132 | Result = InputInfo(A, GetNamedOutputPath(C, *JA, BaseInput, BoundArch, |
| 4133 | AtTopLevel, MultipleArchs, |
| 4134 | OffloadingPrefix), |
| 4135 | BaseInput); |
| 4136 | } |
| 4137 | |
| 4138 | if (CCCPrintBindings && !CCGenDiagnostics) { |
| 4139 | llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"' |
| 4140 | << " - \"" << T->getName() << "\", inputs: ["; |
| 4141 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
| 4142 | llvm::errs() << InputInfos[i].getAsString(); |
| 4143 | if (i + 1 != e) |
| 4144 | llvm::errs() << ", "; |
| 4145 | } |
| 4146 | if (UnbundlingResults.empty()) |
| 4147 | llvm::errs() << "], output: " << Result.getAsString() << "\n"; |
| 4148 | else { |
| 4149 | llvm::errs() << "], outputs: ["; |
| 4150 | for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) { |
| 4151 | llvm::errs() << UnbundlingResults[i].getAsString(); |
| 4152 | if (i + 1 != e) |
| 4153 | llvm::errs() << ", "; |
| 4154 | } |
| 4155 | llvm::errs() << "] \n"; |
| 4156 | } |
| 4157 | } else { |
| 4158 | if (UnbundlingResults.empty()) |
| 4159 | T->ConstructJob( |
| 4160 | C, *JA, Result, InputInfos, |
| 4161 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 4162 | LinkingOutput); |
| 4163 | else |
| 4164 | T->ConstructJobMultipleOutputs( |
| 4165 | C, *JA, UnbundlingResults, InputInfos, |
| 4166 | C.getArgsForToolChain(TC, BoundArch, JA->getOffloadingDeviceKind()), |
| 4167 | LinkingOutput); |
| 4168 | } |
| 4169 | return Result; |
| 4170 | } |
| 4171 | |
| 4172 | const char *Driver::getDefaultImageName() const { |
| 4173 | llvm::Triple Target(llvm::Triple::normalize(TargetTriple)); |
| 4174 | return Target.isOSWindows() ? "a.exe" : "a.out"; |
| 4175 | } |
| 4176 | |
| 4177 | |
| 4178 | |
| 4179 | |
| 4180 | |
| 4181 | static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue, |
| 4182 | StringRef BaseName, |
| 4183 | types::ID FileType) { |
| 4184 | SmallString<128> Filename = ArgValue; |
| 4185 | |
| 4186 | if (ArgValue.empty()) { |
| 4187 | |
| 4188 | Filename = BaseName; |
| 4189 | } else if (llvm::sys::path::is_separator(Filename.back())) { |
| 4190 | |
| 4191 | llvm::sys::path::append(Filename, BaseName); |
| 4192 | } |
| 4193 | |
| 4194 | if (!llvm::sys::path::has_extension(ArgValue)) { |
| 4195 | |
| 4196 | const char *Extension = types::getTypeTempSuffix(FileType, true); |
| 4197 | |
| 4198 | if (FileType == types::TY_Image && |
| 4199 | Args.hasArg(options::OPT__SLASH_LD, options::OPT__SLASH_LDd)) { |
| 4200 | |
| 4201 | Extension = "dll"; |
| 4202 | } |
| 4203 | |
| 4204 | llvm::sys::path::replace_extension(Filename, Extension); |
| 4205 | } |
| 4206 | |
| 4207 | return Args.MakeArgString(Filename.c_str()); |
| 4208 | } |
| 4209 | |
| 4210 | const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, |
| 4211 | const char *BaseInput, |
| 4212 | StringRef BoundArch, bool AtTopLevel, |
| 4213 | bool MultipleArchs, |
| 4214 | StringRef OffloadingPrefix) const { |
| 4215 | llvm::PrettyStackTraceString CrashInfo("Computing output path"); |
| 4216 | |
| 4217 | if (AtTopLevel && !isa<DsymutilJobAction>(JA) && !isa<VerifyJobAction>(JA)) { |
| 4218 | if (Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o)) |
| 4219 | return C.addResultFile(FinalOutput->getValue(), &JA); |
| 4220 | } |
| 4221 | |
| 4222 | |
| 4223 | if (C.getArgs().hasArg(options::OPT__SLASH_P)) { |
| 4224 | (JA)", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 4224, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(AtTopLevel && isa<PreprocessJobAction>(JA)); |
| 4225 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
| 4226 | StringRef NameArg; |
| 4227 | if (Arg *A = C.getArgs().getLastArg(options::OPT__SLASH_Fi)) |
| 4228 | NameArg = A->getValue(); |
| 4229 | return C.addResultFile( |
| 4230 | MakeCLOutputFilename(C.getArgs(), NameArg, BaseName, types::TY_PP_C), |
| 4231 | &JA); |
| 4232 | } |
| 4233 | |
| 4234 | |
| 4235 | if (AtTopLevel && !CCGenDiagnostics && isa<PreprocessJobAction>(JA)) |
| 4236 | return "-"; |
| 4237 | |
| 4238 | |
| 4239 | if (JA.getType() == types::TY_PP_Asm && |
| 4240 | (C.getArgs().hasArg(options::OPT__SLASH_FA) || |
| 4241 | C.getArgs().hasArg(options::OPT__SLASH_Fa))) { |
| 4242 | |
| 4243 | StringRef BaseName = llvm::sys::path::filename(BaseInput); |
| 4244 | StringRef FaValue = C.getArgs().getLastArgValue(options::OPT__SLASH_Fa); |
| 4245 | return C.addResultFile( |
| 4246 | MakeCLOutputFilename(C.getArgs(), FaValue, BaseName, JA.getType()), |
| 4247 | &JA); |
| 4248 | } |
| 4249 | |
| 4250 | |
| 4251 | if ((!AtTopLevel && !isSaveTempsEnabled() && |
| 4252 | !C.getArgs().hasArg(options::OPT__SLASH_Fo)) || |
| 4253 | CCGenDiagnostics) { |
| 4254 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4255 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
| 4256 | SmallString<128> TmpName; |
| 4257 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
| 4258 | Arg *A = C.getArgs().getLastArg(options::OPT_fcrash_diagnostics_dir); |
| 4259 | if (CCGenDiagnostics && A) { |
| 4260 | SmallString<128> CrashDirectory(A->getValue()); |
| 4261 | llvm::sys::path::append(CrashDirectory, Split.first); |
| 4262 | const char *Middle = Suffix ? "-%%%%%%." : "-%%%%%%"; |
| 4263 | std::error_code EC = |
| 4264 | llvm::sys::fs::createUniqueFile(CrashDirectory + Middle + Suffix, TmpName); |
| 4265 | if (EC) { |
| 4266 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4267 | return ""; |
| 4268 | } |
| 4269 | } else { |
| 4270 | TmpName = GetTemporaryPath(Split.first, Suffix); |
| 4271 | } |
| 4272 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
| 4273 | } |
| 4274 | |
| 4275 | SmallString<128> BasePath(BaseInput); |
| 4276 | StringRef BaseName; |
| 4277 | |
| 4278 | |
| 4279 | if (isa<DsymutilJobAction>(JA) || isa<VerifyJobAction>(JA)) |
| 4280 | BaseName = BasePath; |
| 4281 | else |
| 4282 | BaseName = llvm::sys::path::filename(BasePath); |
| 4283 | |
| 4284 | |
| 4285 | const char *NamedOutput; |
| 4286 | |
| 4287 | if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) && |
| 4288 | C.getArgs().hasArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o)) { |
| 4289 | |
| 4290 | StringRef Val = |
| 4291 | C.getArgs() |
| 4292 | .getLastArg(options::OPT__SLASH_Fo, options::OPT__SLASH_o) |
| 4293 | ->getValue(); |
| 4294 | NamedOutput = |
| 4295 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Object); |
| 4296 | } else if (JA.getType() == types::TY_Image && |
| 4297 | C.getArgs().hasArg(options::OPT__SLASH_Fe, |
| 4298 | options::OPT__SLASH_o)) { |
| 4299 | |
| 4300 | StringRef Val = |
| 4301 | C.getArgs() |
| 4302 | .getLastArg(options::OPT__SLASH_Fe, options::OPT__SLASH_o) |
| 4303 | ->getValue(); |
| 4304 | NamedOutput = |
| 4305 | MakeCLOutputFilename(C.getArgs(), Val, BaseName, types::TY_Image); |
| 4306 | } else if (JA.getType() == types::TY_Image) { |
| 4307 | if (IsCLMode()) { |
| 4308 | |
| 4309 | NamedOutput = |
| 4310 | MakeCLOutputFilename(C.getArgs(), "", BaseName, types::TY_Image); |
| 4311 | } else { |
| 4312 | SmallString<128> Output(getDefaultImageName()); |
| 4313 | Output += OffloadingPrefix; |
| 4314 | if (MultipleArchs && !BoundArch.empty()) { |
| 4315 | Output += "-"; |
| 4316 | Output.append(BoundArch); |
| 4317 | } |
| 4318 | NamedOutput = C.getArgs().MakeArgString(Output.c_str()); |
| 4319 | } |
| 4320 | } else if (JA.getType() == types::TY_PCH && IsCLMode()) { |
| 4321 | NamedOutput = C.getArgs().MakeArgString(GetClPchPath(C, BaseName)); |
| 4322 | } else { |
| 4323 | const char *Suffix = types::getTypeTempSuffix(JA.getType(), IsCLMode()); |
| 4324 | (0) . __assert_fail ("Suffix && \"All types used for output should have a suffix.\"", "/home/seafit/code_projects/clang_source/clang/lib/Driver/Driver.cpp", 4324, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Suffix && "All types used for output should have a suffix."); |
| 4325 | |
| 4326 | std::string::size_type End = std::string::npos; |
| 4327 | if (!types::appendSuffixForType(JA.getType())) |
| 4328 | End = BaseName.rfind('.'); |
| 4329 | SmallString<128> Suffixed(BaseName.substr(0, End)); |
| 4330 | Suffixed += OffloadingPrefix; |
| 4331 | if (MultipleArchs && !BoundArch.empty()) { |
| 4332 | Suffixed += "-"; |
| 4333 | Suffixed.append(BoundArch); |
| 4334 | } |
| 4335 | |
| 4336 | |
| 4337 | |
| 4338 | if (!AtTopLevel && C.getArgs().hasArg(options::OPT_emit_llvm) && |
| 4339 | JA.getType() == types::TY_LLVM_BC) |
| 4340 | Suffixed += ".tmp"; |
| 4341 | Suffixed += '.'; |
| 4342 | Suffixed += Suffix; |
| 4343 | NamedOutput = C.getArgs().MakeArgString(Suffixed.c_str()); |
| 4344 | } |
| 4345 | |
| 4346 | |
| 4347 | if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(options::OPT_o) && |
| 4348 | JA.getType() != types::TY_PCH) { |
| 4349 | Arg *FinalOutput = C.getArgs().getLastArg(options::OPT_o); |
| 4350 | SmallString<128> TempPath(FinalOutput->getValue()); |
| 4351 | llvm::sys::path::remove_filename(TempPath); |
| 4352 | StringRef OutputFileName = llvm::sys::path::filename(NamedOutput); |
| 4353 | llvm::sys::path::append(TempPath, OutputFileName); |
| 4354 | NamedOutput = C.getArgs().MakeArgString(TempPath.c_str()); |
| 4355 | } |
| 4356 | |
| 4357 | |
| 4358 | |
| 4359 | if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) { |
| 4360 | bool SameFile = false; |
| 4361 | SmallString<256> Result; |
| 4362 | llvm::sys::fs::current_path(Result); |
| 4363 | llvm::sys::path::append(Result, BaseName); |
| 4364 | llvm::sys::fs::equivalent(BaseInput, Result.c_str(), SameFile); |
| 4365 | |
| 4366 | if (SameFile) { |
| 4367 | StringRef Name = llvm::sys::path::filename(BaseInput); |
| 4368 | std::pair<StringRef, StringRef> Split = Name.split('.'); |
| 4369 | std::string TmpName = GetTemporaryPath( |
| 4370 | Split.first, types::getTypeTempSuffix(JA.getType(), IsCLMode())); |
| 4371 | return C.addTempFile(C.getArgs().MakeArgString(TmpName)); |
| 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | |
| 4376 | if (JA.getType() == types::TY_PCH && !IsCLMode()) { |
| 4377 | llvm::sys::path::remove_filename(BasePath); |
| 4378 | if (BasePath.empty()) |
| 4379 | BasePath = NamedOutput; |
| 4380 | else |
| 4381 | llvm::sys::path::append(BasePath, NamedOutput); |
| 4382 | return C.addResultFile(C.getArgs().MakeArgString(BasePath.c_str()), &JA); |
| 4383 | } else { |
| 4384 | return C.addResultFile(NamedOutput, &JA); |
| 4385 | } |
| 4386 | } |
| 4387 | |
| 4388 | std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const { |
| 4389 | |
| 4390 | auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P) |
| 4391 | -> llvm::Optional<std::string> { |
| 4392 | |
| 4393 | |
| 4394 | for (const auto &Dir : P) { |
| 4395 | if (Dir.empty()) |
| 4396 | continue; |
| 4397 | SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(1) : Dir); |
| 4398 | llvm::sys::path::append(P, Name); |
| 4399 | if (llvm::sys::fs::exists(Twine(P))) |
| 4400 | return P.str().str(); |
| 4401 | } |
| 4402 | return None; |
| 4403 | }; |
| 4404 | |
| 4405 | if (auto P = SearchPaths(PrefixDirs)) |
| 4406 | return *P; |
| 4407 | |
| 4408 | SmallString<128> R(ResourceDir); |
| 4409 | llvm::sys::path::append(R, Name); |
| 4410 | if (llvm::sys::fs::exists(Twine(R))) |
| 4411 | return R.str(); |
| 4412 | |
| 4413 | SmallString<128> P(TC.getCompilerRTPath()); |
| 4414 | llvm::sys::path::append(P, Name); |
| 4415 | if (llvm::sys::fs::exists(Twine(P))) |
| 4416 | return P.str(); |
| 4417 | |
| 4418 | if (auto P = SearchPaths(TC.getLibraryPaths())) |
| 4419 | return *P; |
| 4420 | |
| 4421 | if (auto P = SearchPaths(TC.getFilePaths())) |
| 4422 | return *P; |
| 4423 | |
| 4424 | return Name; |
| 4425 | } |
| 4426 | |
| 4427 | void Driver::generatePrefixedToolNames( |
| 4428 | StringRef Tool, const ToolChain &TC, |
| 4429 | SmallVectorImpl<std::string> &Names) const { |
| 4430 | |
| 4431 | Names.emplace_back((TargetTriple + "-" + Tool).str()); |
| 4432 | Names.emplace_back(Tool); |
| 4433 | |
| 4434 | |
| 4435 | std::string DefaultTargetTriple = llvm::sys::getDefaultTargetTriple(); |
| 4436 | if (DefaultTargetTriple != TargetTriple) |
| 4437 | Names.emplace_back((DefaultTargetTriple + "-" + Tool).str()); |
| 4438 | } |
| 4439 | |
| 4440 | static bool ScanDirForExecutable(SmallString<128> &Dir, |
| 4441 | ArrayRef<std::string> Names) { |
| 4442 | for (const auto &Name : Names) { |
| 4443 | llvm::sys::path::append(Dir, Name); |
| 4444 | if (llvm::sys::fs::can_execute(Twine(Dir))) |
| 4445 | return true; |
| 4446 | llvm::sys::path::remove_filename(Dir); |
| 4447 | } |
| 4448 | return false; |
| 4449 | } |
| 4450 | |
| 4451 | std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const { |
| 4452 | SmallVector<std::string, 2> TargetSpecificExecutables; |
| 4453 | generatePrefixedToolNames(Name, TC, TargetSpecificExecutables); |
| 4454 | |
| 4455 | |
| 4456 | |
| 4457 | for (const auto &PrefixDir : PrefixDirs) { |
| 4458 | if (llvm::sys::fs::is_directory(PrefixDir)) { |
| 4459 | SmallString<128> P(PrefixDir); |
| 4460 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
| 4461 | return P.str(); |
| 4462 | } else { |
| 4463 | SmallString<128> P((PrefixDir + Name).str()); |
| 4464 | if (llvm::sys::fs::can_execute(Twine(P))) |
| 4465 | return P.str(); |
| 4466 | } |
| 4467 | } |
| 4468 | |
| 4469 | const ToolChain::path_list &List = TC.getProgramPaths(); |
| 4470 | for (const auto &Path : List) { |
| 4471 | SmallString<128> P(Path); |
| 4472 | if (ScanDirForExecutable(P, TargetSpecificExecutables)) |
| 4473 | return P.str(); |
| 4474 | } |
| 4475 | |
| 4476 | |
| 4477 | for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) |
| 4478 | if (llvm::ErrorOr<std::string> P = |
| 4479 | llvm::sys::findProgramByName(TargetSpecificExecutable)) |
| 4480 | return *P; |
| 4481 | |
| 4482 | return Name; |
| 4483 | } |
| 4484 | |
| 4485 | std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const { |
| 4486 | SmallString<128> Path; |
| 4487 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, Path); |
| 4488 | if (EC) { |
| 4489 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4490 | return ""; |
| 4491 | } |
| 4492 | |
| 4493 | return Path.str(); |
| 4494 | } |
| 4495 | |
| 4496 | std::string Driver::GetTemporaryDirectory(StringRef Prefix) const { |
| 4497 | SmallString<128> Path; |
| 4498 | std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, Path); |
| 4499 | if (EC) { |
| 4500 | Diag(clang::diag::err_unable_to_make_temp) << EC.message(); |
| 4501 | return ""; |
| 4502 | } |
| 4503 | |
| 4504 | return Path.str(); |
| 4505 | } |
| 4506 | |
| 4507 | std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const { |
| 4508 | SmallString<128> Output; |
| 4509 | if (Arg *FpArg = C.getArgs().getLastArg(options::OPT__SLASH_Fp)) { |
| 4510 | |
| 4511 | |
| 4512 | |
| 4513 | Output = FpArg->getValue(); |
| 4514 | |
| 4515 | |
| 4516 | |
| 4517 | if (!llvm::sys::path::has_extension(Output)) |
| 4518 | Output += ".pch"; |
| 4519 | } else { |
| 4520 | if (Arg *YcArg = C.getArgs().getLastArg(options::OPT__SLASH_Yc)) |
| 4521 | Output = YcArg->getValue(); |
| 4522 | if (Output.empty()) |
| 4523 | Output = BaseName; |
| 4524 | llvm::sys::path::replace_extension(Output, ".pch"); |
| 4525 | } |
| 4526 | return Output.str(); |
| 4527 | } |
| 4528 | |
| 4529 | const ToolChain &Driver::getToolChain(const ArgList &Args, |
| 4530 | const llvm::Triple &Target) const { |
| 4531 | |
| 4532 | auto &TC = ToolChains[Target.str()]; |
| 4533 | if (!TC) { |
| 4534 | switch (Target.getOS()) { |
| 4535 | case llvm::Triple::Haiku: |
| 4536 | TC = llvm::make_unique<toolchains::Haiku>(*this, Target, Args); |
| 4537 | break; |
| 4538 | case llvm::Triple::Ananas: |
| 4539 | TC = llvm::make_unique<toolchains::Ananas>(*this, Target, Args); |
| 4540 | break; |
| 4541 | case llvm::Triple::CloudABI: |
| 4542 | TC = llvm::make_unique<toolchains::CloudABI>(*this, Target, Args); |
| 4543 | break; |
| 4544 | case llvm::Triple::Darwin: |
| 4545 | case llvm::Triple::MacOSX: |
| 4546 | case llvm::Triple::IOS: |
| 4547 | case llvm::Triple::TvOS: |
| 4548 | case llvm::Triple::WatchOS: |
| 4549 | TC = llvm::make_unique<toolchains::DarwinClang>(*this, Target, Args); |
| 4550 | break; |
| 4551 | case llvm::Triple::DragonFly: |
| 4552 | TC = llvm::make_unique<toolchains::DragonFly>(*this, Target, Args); |
| 4553 | break; |
| 4554 | case llvm::Triple::OpenBSD: |
| 4555 | TC = llvm::make_unique<toolchains::OpenBSD>(*this, Target, Args); |
| 4556 | break; |
| 4557 | case llvm::Triple::NetBSD: |
| 4558 | TC = llvm::make_unique<toolchains::NetBSD>(*this, Target, Args); |
| 4559 | break; |
| 4560 | case llvm::Triple::FreeBSD: |
| 4561 | TC = llvm::make_unique<toolchains::FreeBSD>(*this, Target, Args); |
| 4562 | break; |
| 4563 | case llvm::Triple::Minix: |
| 4564 | TC = llvm::make_unique<toolchains::Minix>(*this, Target, Args); |
| 4565 | break; |
| 4566 | case llvm::Triple::Linux: |
| 4567 | case llvm::Triple::ELFIAMCU: |
| 4568 | if (Target.getArch() == llvm::Triple::hexagon) |
| 4569 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4570 | Args); |
| 4571 | else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) && |
| 4572 | !Target.hasEnvironment()) |
| 4573 | TC = llvm::make_unique<toolchains::MipsLLVMToolChain>(*this, Target, |
| 4574 | Args); |
| 4575 | else |
| 4576 | TC = llvm::make_unique<toolchains::Linux>(*this, Target, Args); |
| 4577 | break; |
| 4578 | case llvm::Triple::NaCl: |
| 4579 | TC = llvm::make_unique<toolchains::NaClToolChain>(*this, Target, Args); |
| 4580 | break; |
| 4581 | case llvm::Triple::Fuchsia: |
| 4582 | TC = llvm::make_unique<toolchains::Fuchsia>(*this, Target, Args); |
| 4583 | break; |
| 4584 | case llvm::Triple::Solaris: |
| 4585 | TC = llvm::make_unique<toolchains::Solaris>(*this, Target, Args); |
| 4586 | break; |
| 4587 | case llvm::Triple::AMDHSA: |
| 4588 | TC = llvm::make_unique<toolchains::AMDGPUToolChain>(*this, Target, Args); |
| 4589 | break; |
| 4590 | case llvm::Triple::Win32: |
| 4591 | switch (Target.getEnvironment()) { |
| 4592 | default: |
| 4593 | if (Target.isOSBinFormatELF()) |
| 4594 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
| 4595 | else if (Target.isOSBinFormatMachO()) |
| 4596 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
| 4597 | else |
| 4598 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
| 4599 | break; |
| 4600 | case llvm::Triple::GNU: |
| 4601 | TC = llvm::make_unique<toolchains::MinGW>(*this, Target, Args); |
| 4602 | break; |
| 4603 | case llvm::Triple::Itanium: |
| 4604 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>(*this, Target, |
| 4605 | Args); |
| 4606 | break; |
| 4607 | case llvm::Triple::MSVC: |
| 4608 | case llvm::Triple::UnknownEnvironment: |
| 4609 | if (Args.getLastArgValue(options::OPT_fuse_ld_EQ) |
| 4610 | .startswith_lower("bfd")) |
| 4611 | TC = llvm::make_unique<toolchains::CrossWindowsToolChain>( |
| 4612 | *this, Target, Args); |
| 4613 | else |
| 4614 | TC = |
| 4615 | llvm::make_unique<toolchains::MSVCToolChain>(*this, Target, Args); |
| 4616 | break; |
| 4617 | } |
| 4618 | break; |
| 4619 | case llvm::Triple::PS4: |
| 4620 | TC = llvm::make_unique<toolchains::PS4CPU>(*this, Target, Args); |
| 4621 | break; |
| 4622 | case llvm::Triple::Contiki: |
| 4623 | TC = llvm::make_unique<toolchains::Contiki>(*this, Target, Args); |
| 4624 | break; |
| 4625 | case llvm::Triple::Hurd: |
| 4626 | TC = llvm::make_unique<toolchains::Hurd>(*this, Target, Args); |
| 4627 | break; |
| 4628 | default: |
| 4629 | |
| 4630 | |
| 4631 | switch (Target.getArch()) { |
| 4632 | case llvm::Triple::tce: |
| 4633 | TC = llvm::make_unique<toolchains::TCEToolChain>(*this, Target, Args); |
| 4634 | break; |
| 4635 | case llvm::Triple::tcele: |
| 4636 | TC = llvm::make_unique<toolchains::TCELEToolChain>(*this, Target, Args); |
| 4637 | break; |
| 4638 | case llvm::Triple::hexagon: |
| 4639 | TC = llvm::make_unique<toolchains::HexagonToolChain>(*this, Target, |
| 4640 | Args); |
| 4641 | break; |
| 4642 | case llvm::Triple::lanai: |
| 4643 | TC = llvm::make_unique<toolchains::LanaiToolChain>(*this, Target, Args); |
| 4644 | break; |
| 4645 | case llvm::Triple::xcore: |
| 4646 | TC = llvm::make_unique<toolchains::XCoreToolChain>(*this, Target, Args); |
| 4647 | break; |
| 4648 | case llvm::Triple::wasm32: |
| 4649 | case llvm::Triple::wasm64: |
| 4650 | TC = llvm::make_unique<toolchains::WebAssembly>(*this, Target, Args); |
| 4651 | break; |
| 4652 | case llvm::Triple::avr: |
| 4653 | TC = llvm::make_unique<toolchains::AVRToolChain>(*this, Target, Args); |
| 4654 | break; |
| 4655 | case llvm::Triple::msp430: |
| 4656 | TC = |
| 4657 | llvm::make_unique<toolchains::MSP430ToolChain>(*this, Target, Args); |
| 4658 | break; |
| 4659 | case llvm::Triple::riscv32: |
| 4660 | case llvm::Triple::riscv64: |
| 4661 | TC = llvm::make_unique<toolchains::RISCVToolChain>(*this, Target, Args); |
| 4662 | break; |
| 4663 | default: |
| 4664 | if (Target.getVendor() == llvm::Triple::Myriad) |
| 4665 | TC = llvm::make_unique<toolchains::MyriadToolChain>(*this, Target, |
| 4666 | Args); |
| 4667 | else if (toolchains::BareMetal::handlesTarget(Target)) |
| 4668 | TC = llvm::make_unique<toolchains::BareMetal>(*this, Target, Args); |
| 4669 | else if (Target.isOSBinFormatELF()) |
| 4670 | TC = llvm::make_unique<toolchains::Generic_ELF>(*this, Target, Args); |
| 4671 | else if (Target.isOSBinFormatMachO()) |
| 4672 | TC = llvm::make_unique<toolchains::MachO>(*this, Target, Args); |
| 4673 | else |
| 4674 | TC = llvm::make_unique<toolchains::Generic_GCC>(*this, Target, Args); |
| 4675 | } |
| 4676 | } |
| 4677 | } |
| 4678 | |
| 4679 | |
| 4680 | |
| 4681 | |
| 4682 | |
| 4683 | |
| 4684 | return *TC; |
| 4685 | } |
| 4686 | |
| 4687 | bool Driver::ShouldUseClangCompiler(const JobAction &JA) const { |
| 4688 | |
| 4689 | if (JA.size() != 1 || |
| 4690 | !types::isAcceptedByClang((*JA.input_begin())->getType())) |
| 4691 | return false; |
| 4692 | |
| 4693 | |
| 4694 | if (!isa<PreprocessJobAction>(JA) && !isa<PrecompileJobAction>(JA) && |
| 4695 | !isa<CompileJobAction>(JA) && !isa<BackendJobAction>(JA)) |
| 4696 | return false; |
| 4697 | |
| 4698 | return true; |
| 4699 | } |
| 4700 | |
| 4701 | |
| 4702 | |
| 4703 | |
| 4704 | |
| 4705 | |
| 4706 | bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, |
| 4707 | unsigned &Micro, bool &) { |
| 4708 | HadExtra = false; |
| 4709 | |
| 4710 | Major = Minor = Micro = 0; |
| 4711 | if (Str.empty()) |
| 4712 | return false; |
| 4713 | |
| 4714 | if (Str.consumeInteger(10, Major)) |
| 4715 | return false; |
| 4716 | if (Str.empty()) |
| 4717 | return true; |
| 4718 | if (Str[0] != '.') |
| 4719 | return false; |
| 4720 | |
| 4721 | Str = Str.drop_front(1); |
| 4722 | |
| 4723 | if (Str.consumeInteger(10, Minor)) |
| 4724 | return false; |
| 4725 | if (Str.empty()) |
| 4726 | return true; |
| 4727 | if (Str[0] != '.') |
| 4728 | return false; |
| 4729 | Str = Str.drop_front(1); |
| 4730 | |
| 4731 | if (Str.consumeInteger(10, Micro)) |
| 4732 | return false; |
| 4733 | if (!Str.empty()) |
| 4734 | HadExtra = true; |
| 4735 | return true; |
| 4736 | } |
| 4737 | |
| 4738 | |
| 4739 | |
| 4740 | |
| 4741 | |
| 4742 | |
| 4743 | |
| 4744 | bool Driver::GetReleaseVersion(StringRef Str, |
| 4745 | MutableArrayRef<unsigned> Digits) { |
| 4746 | if (Str.empty()) |
| 4747 | return false; |
| 4748 | |
| 4749 | unsigned CurDigit = 0; |
| 4750 | while (CurDigit < Digits.size()) { |
| 4751 | unsigned Digit; |
| 4752 | if (Str.consumeInteger(10, Digit)) |
| 4753 | return false; |
| 4754 | Digits[CurDigit] = Digit; |
| 4755 | if (Str.empty()) |
| 4756 | return true; |
| 4757 | if (Str[0] != '.') |
| 4758 | return false; |
| 4759 | Str = Str.drop_front(1); |
| 4760 | CurDigit++; |
| 4761 | } |
| 4762 | |
| 4763 | |
| 4764 | return false; |
| 4765 | } |
| 4766 | |
| 4767 | std::pair<unsigned, unsigned> Driver::getIncludeExcludeOptionFlagMasks(bool IsClCompatMode) const { |
| 4768 | unsigned IncludedFlagsBitmask = 0; |
| 4769 | unsigned ExcludedFlagsBitmask = options::NoDriverOption; |
| 4770 | |
| 4771 | if (IsClCompatMode) { |
| 4772 | |
| 4773 | IncludedFlagsBitmask |= options::CLOption; |
| 4774 | IncludedFlagsBitmask |= options::CoreOption; |
| 4775 | } else { |
| 4776 | ExcludedFlagsBitmask |= options::CLOption; |
| 4777 | } |
| 4778 | |
| 4779 | return std::make_pair(IncludedFlagsBitmask, ExcludedFlagsBitmask); |
| 4780 | } |
| 4781 | |
| 4782 | bool clang::driver::isOptimizationLevelFast(const ArgList &Args) { |
| 4783 | return Args.hasFlag(options::OPT_Ofast, options::OPT_O_Group, false); |
| 4784 | } |
| 4785 | |