1 | //===--- Driver.cpp - Clang GCC Compatible Driver -------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "clang/Driver/Driver.h" |
10 | #include "ToolChains/AIX.h" |
11 | #include "ToolChains/AMDGPU.h" |
12 | #include "ToolChains/AMDGPUOpenMP.h" |
13 | #include "ToolChains/AVR.h" |
14 | #include "ToolChains/Arch/RISCV.h" |
15 | #include "ToolChains/BareMetal.h" |
16 | #include "ToolChains/CSKYToolChain.h" |
17 | #include "ToolChains/Clang.h" |
18 | #include "ToolChains/CrossWindows.h" |
19 | #include "ToolChains/Cuda.h" |
20 | #include "ToolChains/Cygwin.h" |
21 | #include "ToolChains/Darwin.h" |
22 | #include "ToolChains/DragonFly.h" |
23 | #include "ToolChains/FreeBSD.h" |
24 | #include "ToolChains/Fuchsia.h" |
25 | #include "ToolChains/Gnu.h" |
26 | #include "ToolChains/HIPAMD.h" |
27 | #include "ToolChains/HIPSPV.h" |
28 | #include "ToolChains/HLSL.h" |
29 | #include "ToolChains/Haiku.h" |
30 | #include "ToolChains/Hexagon.h" |
31 | #include "ToolChains/Hurd.h" |
32 | #include "ToolChains/Lanai.h" |
33 | #include "ToolChains/Linux.h" |
34 | #include "ToolChains/MSP430.h" |
35 | #include "ToolChains/MSVC.h" |
36 | #include "ToolChains/Managarm.h" |
37 | #include "ToolChains/MinGW.h" |
38 | #include "ToolChains/MipsLinux.h" |
39 | #include "ToolChains/NaCl.h" |
40 | #include "ToolChains/NetBSD.h" |
41 | #include "ToolChains/OHOS.h" |
42 | #include "ToolChains/OpenBSD.h" |
43 | #include "ToolChains/PPCFreeBSD.h" |
44 | #include "ToolChains/PPCLinux.h" |
45 | #include "ToolChains/PS4CPU.h" |
46 | #include "ToolChains/SPIRV.h" |
47 | #include "ToolChains/SPIRVOpenMP.h" |
48 | #include "ToolChains/SYCL.h" |
49 | #include "ToolChains/Solaris.h" |
50 | #include "ToolChains/TCE.h" |
51 | #include "ToolChains/UEFI.h" |
52 | #include "ToolChains/VEToolchain.h" |
53 | #include "ToolChains/WebAssembly.h" |
54 | #include "ToolChains/XCore.h" |
55 | #include "ToolChains/ZOS.h" |
56 | #include "clang/Basic/DiagnosticDriver.h" |
57 | #include "clang/Basic/TargetID.h" |
58 | #include "clang/Basic/Version.h" |
59 | #include "clang/Config/config.h" |
60 | #include "clang/Driver/Action.h" |
61 | #include "clang/Driver/Compilation.h" |
62 | #include "clang/Driver/InputInfo.h" |
63 | #include "clang/Driver/Job.h" |
64 | #include "clang/Driver/Options.h" |
65 | #include "clang/Driver/Phases.h" |
66 | #include "clang/Driver/SanitizerArgs.h" |
67 | #include "clang/Driver/Tool.h" |
68 | #include "clang/Driver/ToolChain.h" |
69 | #include "clang/Driver/Types.h" |
70 | #include "llvm/ADT/ArrayRef.h" |
71 | #include "llvm/ADT/STLExtras.h" |
72 | #include "llvm/ADT/StringExtras.h" |
73 | #include "llvm/ADT/StringRef.h" |
74 | #include "llvm/ADT/StringSet.h" |
75 | #include "llvm/ADT/StringSwitch.h" |
76 | #include "llvm/Config/llvm-config.h" |
77 | #include "llvm/MC/TargetRegistry.h" |
78 | #include "llvm/Option/Arg.h" |
79 | #include "llvm/Option/ArgList.h" |
80 | #include "llvm/Option/OptSpecifier.h" |
81 | #include "llvm/Option/OptTable.h" |
82 | #include "llvm/Option/Option.h" |
83 | #include "llvm/Support/CommandLine.h" |
84 | #include "llvm/Support/ErrorHandling.h" |
85 | #include "llvm/Support/ExitCodes.h" |
86 | #include "llvm/Support/FileSystem.h" |
87 | #include "llvm/Support/FormatVariadic.h" |
88 | #include "llvm/Support/MD5.h" |
89 | #include "llvm/Support/Path.h" |
90 | #include "llvm/Support/PrettyStackTrace.h" |
91 | #include "llvm/Support/Process.h" |
92 | #include "llvm/Support/Program.h" |
93 | #include "llvm/Support/Regex.h" |
94 | #include "llvm/Support/StringSaver.h" |
95 | #include "llvm/Support/VirtualFileSystem.h" |
96 | #include "llvm/Support/raw_ostream.h" |
97 | #include "llvm/TargetParser/Host.h" |
98 | #include "llvm/TargetParser/RISCVISAInfo.h" |
99 | #include <cstdlib> // ::getenv |
100 | #include <map> |
101 | #include <memory> |
102 | #include <optional> |
103 | #include <set> |
104 | #include <utility> |
105 | #if LLVM_ON_UNIX |
106 | #include <unistd.h> // getpid |
107 | #endif |
108 | |
109 | using namespace clang::driver; |
110 | using namespace clang; |
111 | using namespace llvm::opt; |
112 | |
113 | static std::optional<llvm::Triple> getOffloadTargetTriple(const Driver &D, |
114 | const ArgList &Args) { |
115 | auto OffloadTargets = Args.getAllArgValues(Id: options::OPT_offload_EQ); |
116 | // Offload compilation flow does not support multiple targets for now. We |
117 | // need the HIPActionBuilder (and possibly the CudaActionBuilder{,Base}too) |
118 | // to support multiple tool chains first. |
119 | switch (OffloadTargets.size()) { |
120 | default: |
121 | D.Diag(DiagID: diag::err_drv_only_one_offload_target_supported); |
122 | return std::nullopt; |
123 | case 0: |
124 | D.Diag(DiagID: diag::err_drv_invalid_or_unsupported_offload_target) << "" ; |
125 | return std::nullopt; |
126 | case 1: |
127 | break; |
128 | } |
129 | return llvm::Triple(OffloadTargets[0]); |
130 | } |
131 | |
132 | static std::optional<llvm::Triple> |
133 | getNVIDIAOffloadTargetTriple(const Driver &D, const ArgList &Args, |
134 | const llvm::Triple &HostTriple) { |
135 | if (!Args.hasArg(Ids: options::OPT_offload_EQ)) { |
136 | return llvm::Triple(HostTriple.isArch64Bit() ? "nvptx64-nvidia-cuda" |
137 | : "nvptx-nvidia-cuda" ); |
138 | } |
139 | auto TT = getOffloadTargetTriple(D, Args); |
140 | if (TT && (TT->getArch() == llvm::Triple::spirv32 || |
141 | TT->getArch() == llvm::Triple::spirv64)) { |
142 | if (Args.hasArg(Ids: options::OPT_emit_llvm)) |
143 | return TT; |
144 | D.Diag(DiagID: diag::err_drv_cuda_offload_only_emit_bc); |
145 | return std::nullopt; |
146 | } |
147 | D.Diag(DiagID: diag::err_drv_invalid_or_unsupported_offload_target) << TT->str(); |
148 | return std::nullopt; |
149 | } |
150 | |
151 | static std::optional<llvm::Triple> |
152 | getHIPOffloadTargetTriple(const Driver &D, const ArgList &Args) { |
153 | if (!Args.hasArg(Ids: options::OPT_offload_EQ)) { |
154 | auto OffloadArchs = Args.getAllArgValues(Id: options::OPT_offload_arch_EQ); |
155 | if (llvm::is_contained(Range&: OffloadArchs, Element: "amdgcnspirv" ) && |
156 | OffloadArchs.size() == 1) |
157 | return llvm::Triple("spirv64-amd-amdhsa" ); |
158 | return llvm::Triple("amdgcn-amd-amdhsa" ); // Default HIP triple. |
159 | } |
160 | auto TT = getOffloadTargetTriple(D, Args); |
161 | if (!TT) |
162 | return std::nullopt; |
163 | if (TT->isAMDGCN() && TT->getVendor() == llvm::Triple::AMD && |
164 | TT->getOS() == llvm::Triple::AMDHSA) |
165 | return TT; |
166 | if (TT->getArch() == llvm::Triple::spirv64) |
167 | return TT; |
168 | D.Diag(DiagID: diag::err_drv_invalid_or_unsupported_offload_target) << TT->str(); |
169 | return std::nullopt; |
170 | } |
171 | |
172 | template <typename F> static bool usesInput(const ArgList &Args, F &&Fn) { |
173 | return llvm::any_of(Args, [&](Arg *A) { |
174 | return (A->getOption().matches(ID: options::OPT_x) && |
175 | Fn(types::lookupTypeForTypeSpecifier(Name: A->getValue()))) || |
176 | (A->getOption().getKind() == Option::InputClass && |
177 | StringRef(A->getValue()).rfind(C: '.') != StringRef::npos && |
178 | Fn(types::lookupTypeForExtension( |
179 | Ext: &A->getValue()[StringRef(A->getValue()).rfind(C: '.') + 1]))); |
180 | }); |
181 | } |
182 | |
183 | // static |
184 | std::string Driver::GetResourcesPath(StringRef BinaryPath) { |
185 | // Since the resource directory is embedded in the module hash, it's important |
186 | // that all places that need it call this function, so that they get the |
187 | // exact same string ("a/../b/" and "b/" get different hashes, for example). |
188 | |
189 | // Dir is bin/ or lib/, depending on where BinaryPath is. |
190 | StringRef Dir = llvm::sys::path::parent_path(path: BinaryPath); |
191 | SmallString<128> P(Dir); |
192 | |
193 | StringRef ConfiguredResourceDir(CLANG_RESOURCE_DIR); |
194 | if (!ConfiguredResourceDir.empty()) { |
195 | // FIXME: We should fix the behavior of llvm::sys::path::append so we don't |
196 | // need to check for absolute paths here. |
197 | if (llvm::sys::path::is_absolute(path: ConfiguredResourceDir)) |
198 | P = ConfiguredResourceDir; |
199 | else |
200 | llvm::sys::path::append(path&: P, a: ConfiguredResourceDir); |
201 | } else { |
202 | // On Windows, libclang.dll is in bin/. |
203 | // On non-Windows, libclang.so/.dylib is in lib/. |
204 | // With a static-library build of libclang, LibClangPath will contain the |
205 | // path of the embedding binary, which for LLVM binaries will be in bin/. |
206 | // ../lib gets us to lib/ in both cases. |
207 | P = llvm::sys::path::parent_path(path: Dir); |
208 | // This search path is also created in the COFF driver of lld, so any |
209 | // changes here also needs to happen in lld/COFF/Driver.cpp |
210 | llvm::sys::path::append(path&: P, CLANG_INSTALL_LIBDIR_BASENAME, b: "clang" , |
211 | CLANG_VERSION_MAJOR_STRING); |
212 | } |
213 | |
214 | return std::string(P); |
215 | } |
216 | |
217 | CUIDOptions::CUIDOptions(llvm::opt::DerivedArgList &Args, const Driver &D) |
218 | : UseCUID(Kind::Hash) { |
219 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fuse_cuid_EQ)) { |
220 | StringRef UseCUIDStr = A->getValue(); |
221 | UseCUID = llvm::StringSwitch<Kind>(UseCUIDStr) |
222 | .Case(S: "hash" , Value: Kind::Hash) |
223 | .Case(S: "random" , Value: Kind::Random) |
224 | .Case(S: "none" , Value: Kind::None) |
225 | .Default(Value: Kind::Invalid); |
226 | if (UseCUID == Kind::Invalid) |
227 | D.Diag(DiagID: clang::diag::err_drv_invalid_value) |
228 | << A->getAsString(Args) << UseCUIDStr; |
229 | } |
230 | |
231 | FixedCUID = Args.getLastArgValue(Id: options::OPT_cuid_EQ); |
232 | if (!FixedCUID.empty()) |
233 | UseCUID = Kind::Fixed; |
234 | } |
235 | |
236 | std::string CUIDOptions::getCUID(StringRef InputFile, |
237 | llvm::opt::DerivedArgList &Args) const { |
238 | std::string CUID = FixedCUID.str(); |
239 | if (CUID.empty()) { |
240 | if (UseCUID == Kind::Random) |
241 | CUID = llvm::utohexstr(X: llvm::sys::Process::GetRandomNumber(), |
242 | /*LowerCase=*/true); |
243 | else if (UseCUID == Kind::Hash) { |
244 | llvm::MD5 Hasher; |
245 | llvm::MD5::MD5Result Hash; |
246 | Hasher.update(Str: InputFile); |
247 | for (auto *A : Args) { |
248 | if (A->getOption().matches(ID: options::OPT_INPUT)) |
249 | continue; |
250 | Hasher.update(Str: A->getAsString(Args)); |
251 | } |
252 | Hasher.final(Result&: Hash); |
253 | CUID = llvm::utohexstr(X: Hash.low(), /*LowerCase=*/true); |
254 | } |
255 | } |
256 | return CUID; |
257 | } |
258 | Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple, |
259 | DiagnosticsEngine &Diags, std::string Title, |
260 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) |
261 | : Diags(Diags), VFS(std::move(VFS)), Mode(GCCMode), |
262 | SaveTemps(SaveTempsNone), BitcodeEmbed(EmbedNone), |
263 | Offload(OffloadHostDevice), CXX20HeaderType(HeaderMode_None), |
264 | ModulesModeCXX20(false), LTOMode(LTOK_None), |
265 | ClangExecutable(ClangExecutable), SysRoot(DEFAULT_SYSROOT), |
266 | DriverTitle(Title), CCCPrintBindings(false), CCPrintOptions(false), |
267 | CCLogDiagnostics(false), CCGenDiagnostics(false), |
268 | CCPrintProcessStats(false), CCPrintInternalStats(false), |
269 | TargetTriple(TargetTriple), Saver(Alloc), PrependArg(nullptr), |
270 | CheckInputsExist(true), ProbePrecompiled(true), |
271 | SuppressMissingInputWarning(false) { |
272 | // Provide a sane fallback if no VFS is specified. |
273 | if (!this->VFS) |
274 | this->VFS = llvm::vfs::getRealFileSystem(); |
275 | |
276 | Name = std::string(llvm::sys::path::filename(path: ClangExecutable)); |
277 | Dir = std::string(llvm::sys::path::parent_path(path: ClangExecutable)); |
278 | |
279 | if ((!SysRoot.empty()) && llvm::sys::path::is_relative(path: SysRoot)) { |
280 | // Prepend InstalledDir if SysRoot is relative |
281 | SmallString<128> P(Dir); |
282 | llvm::sys::path::append(path&: P, a: SysRoot); |
283 | SysRoot = std::string(P); |
284 | } |
285 | |
286 | #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR) |
287 | if (llvm::sys::path::is_absolute(CLANG_CONFIG_FILE_SYSTEM_DIR)) { |
288 | SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR; |
289 | } else { |
290 | SmallString<128> configFileDir(Dir); |
291 | llvm::sys::path::append(configFileDir, CLANG_CONFIG_FILE_SYSTEM_DIR); |
292 | llvm::sys::path::remove_dots(configFileDir, true); |
293 | SystemConfigDir = static_cast<std::string>(configFileDir); |
294 | } |
295 | #endif |
296 | #if defined(CLANG_CONFIG_FILE_USER_DIR) |
297 | { |
298 | SmallString<128> P; |
299 | llvm::sys::fs::expand_tilde(CLANG_CONFIG_FILE_USER_DIR, P); |
300 | UserConfigDir = static_cast<std::string>(P); |
301 | } |
302 | #endif |
303 | |
304 | // Compute the path to the resource directory. |
305 | ResourceDir = GetResourcesPath(BinaryPath: ClangExecutable); |
306 | } |
307 | |
308 | void Driver::setDriverMode(StringRef Value) { |
309 | static StringRef OptName = |
310 | getOpts().getOption(Opt: options::OPT_driver_mode).getPrefixedName(); |
311 | if (auto M = llvm::StringSwitch<std::optional<DriverMode>>(Value) |
312 | .Case(S: "gcc" , Value: GCCMode) |
313 | .Case(S: "g++" , Value: GXXMode) |
314 | .Case(S: "cpp" , Value: CPPMode) |
315 | .Case(S: "cl" , Value: CLMode) |
316 | .Case(S: "flang" , Value: FlangMode) |
317 | .Case(S: "dxc" , Value: DXCMode) |
318 | .Default(Value: std::nullopt)) |
319 | Mode = *M; |
320 | else |
321 | Diag(DiagID: diag::err_drv_unsupported_option_argument) << OptName << Value; |
322 | } |
323 | |
324 | InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings, |
325 | bool UseDriverMode, |
326 | bool &ContainsError) const { |
327 | llvm::PrettyStackTraceString CrashInfo("Command line argument parsing" ); |
328 | ContainsError = false; |
329 | |
330 | llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(UseDriverMode); |
331 | unsigned MissingArgIndex, MissingArgCount; |
332 | InputArgList Args = getOpts().ParseArgs(Args: ArgStrings, MissingArgIndex, |
333 | MissingArgCount, VisibilityMask); |
334 | |
335 | // Check for missing argument error. |
336 | if (MissingArgCount) { |
337 | Diag(DiagID: diag::err_drv_missing_argument) |
338 | << Args.getArgString(Index: MissingArgIndex) << MissingArgCount; |
339 | ContainsError |= |
340 | Diags.getDiagnosticLevel(DiagID: diag::err_drv_missing_argument, |
341 | Loc: SourceLocation()) > DiagnosticsEngine::Warning; |
342 | } |
343 | |
344 | // Check for unsupported options. |
345 | for (const Arg *A : Args) { |
346 | if (A->getOption().hasFlag(Val: options::Unsupported)) { |
347 | Diag(DiagID: diag::err_drv_unsupported_opt) << A->getAsString(Args); |
348 | ContainsError |= Diags.getDiagnosticLevel(DiagID: diag::err_drv_unsupported_opt, |
349 | Loc: SourceLocation()) > |
350 | DiagnosticsEngine::Warning; |
351 | continue; |
352 | } |
353 | |
354 | // Warn about -mcpu= without an argument. |
355 | if (A->getOption().matches(ID: options::OPT_mcpu_EQ) && A->containsValue(Value: "" )) { |
356 | Diag(DiagID: diag::warn_drv_empty_joined_argument) << A->getAsString(Args); |
357 | ContainsError |= Diags.getDiagnosticLevel( |
358 | DiagID: diag::warn_drv_empty_joined_argument, |
359 | Loc: SourceLocation()) > DiagnosticsEngine::Warning; |
360 | } |
361 | } |
362 | |
363 | for (const Arg *A : Args.filtered(Ids: options::OPT_UNKNOWN)) { |
364 | unsigned DiagID; |
365 | auto ArgString = A->getAsString(Args); |
366 | std::string Nearest; |
367 | if (getOpts().findNearest(Option: ArgString, NearestString&: Nearest, VisibilityMask) > 1) { |
368 | if (!IsCLMode() && |
369 | getOpts().findExact(Option: ArgString, ExactString&: Nearest, |
370 | VisibilityMask: llvm::opt::Visibility(options::CC1Option))) { |
371 | DiagID = diag::err_drv_unknown_argument_with_suggestion; |
372 | Diags.Report(DiagID) << ArgString << "-Xclang " + Nearest; |
373 | } else { |
374 | DiagID = IsCLMode() ? diag::warn_drv_unknown_argument_clang_cl |
375 | : diag::err_drv_unknown_argument; |
376 | Diags.Report(DiagID) << ArgString; |
377 | } |
378 | } else { |
379 | DiagID = IsCLMode() |
380 | ? diag::warn_drv_unknown_argument_clang_cl_with_suggestion |
381 | : diag::err_drv_unknown_argument_with_suggestion; |
382 | Diags.Report(DiagID) << ArgString << Nearest; |
383 | } |
384 | ContainsError |= Diags.getDiagnosticLevel(DiagID, Loc: SourceLocation()) > |
385 | DiagnosticsEngine::Warning; |
386 | } |
387 | |
388 | for (const Arg *A : Args.filtered(Ids: options::OPT_o)) { |
389 | if (ArgStrings[A->getIndex()] == A->getSpelling()) |
390 | continue; |
391 | |
392 | // Warn on joined arguments that are similar to a long argument. |
393 | std::string ArgString = ArgStrings[A->getIndex()]; |
394 | std::string Nearest; |
395 | if (getOpts().findExact(Option: "-" + ArgString, ExactString&: Nearest, VisibilityMask)) |
396 | Diags.Report(DiagID: diag::warn_drv_potentially_misspelled_joined_argument) |
397 | << A->getAsString(Args) << Nearest; |
398 | } |
399 | |
400 | return Args; |
401 | } |
402 | |
403 | // Determine which compilation mode we are in. We look for options which |
404 | // affect the phase, starting with the earliest phases, and record which |
405 | // option we used to determine the final phase. |
406 | phases::ID Driver::getFinalPhase(const DerivedArgList &DAL, |
407 | Arg **FinalPhaseArg) const { |
408 | Arg *PhaseArg = nullptr; |
409 | phases::ID FinalPhase; |
410 | |
411 | // -{E,EP,P,M,MM} only run the preprocessor. |
412 | if (CCCIsCPP() || (PhaseArg = DAL.getLastArg(Ids: options::OPT_E)) || |
413 | (PhaseArg = DAL.getLastArg(Ids: options::OPT__SLASH_EP)) || |
414 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_M, Ids: options::OPT_MM)) || |
415 | (PhaseArg = DAL.getLastArg(Ids: options::OPT__SLASH_P)) || |
416 | CCGenDiagnostics) { |
417 | FinalPhase = phases::Preprocess; |
418 | |
419 | // --precompile only runs up to precompilation. |
420 | // Options that cause the output of C++20 compiled module interfaces or |
421 | // header units have the same effect. |
422 | } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT__precompile)) || |
423 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_extract_api)) || |
424 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_fmodule_header, |
425 | Ids: options::OPT_fmodule_header_EQ))) { |
426 | FinalPhase = phases::Precompile; |
427 | // -{fsyntax-only,-analyze,emit-ast} only run up to the compiler. |
428 | } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_fsyntax_only)) || |
429 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_print_supported_cpus)) || |
430 | (PhaseArg = |
431 | DAL.getLastArg(Ids: options::OPT_print_enabled_extensions)) || |
432 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_module_file_info)) || |
433 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_verify_pch)) || |
434 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_rewrite_objc)) || |
435 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_rewrite_legacy_objc)) || |
436 | (PhaseArg = DAL.getLastArg(Ids: options::OPT__analyze)) || |
437 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_emit_cir)) || |
438 | (PhaseArg = DAL.getLastArg(Ids: options::OPT_emit_ast))) { |
439 | FinalPhase = phases::Compile; |
440 | |
441 | // -S only runs up to the backend. |
442 | } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_S))) { |
443 | FinalPhase = phases::Backend; |
444 | |
445 | // -c compilation only runs up to the assembler. |
446 | } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_c))) { |
447 | FinalPhase = phases::Assemble; |
448 | |
449 | } else if ((PhaseArg = DAL.getLastArg(Ids: options::OPT_emit_interface_stubs))) { |
450 | FinalPhase = phases::IfsMerge; |
451 | |
452 | // Otherwise do everything. |
453 | } else |
454 | FinalPhase = phases::Link; |
455 | |
456 | if (FinalPhaseArg) |
457 | *FinalPhaseArg = PhaseArg; |
458 | |
459 | return FinalPhase; |
460 | } |
461 | |
462 | static Arg *MakeInputArg(DerivedArgList &Args, const OptTable &Opts, |
463 | StringRef Value, bool Claim = true) { |
464 | Arg *A = new Arg(Opts.getOption(Opt: options::OPT_INPUT), Value, |
465 | Args.getBaseArgs().MakeIndex(String0: Value), Value.data()); |
466 | Args.AddSynthesizedArg(A); |
467 | if (Claim) |
468 | A->claim(); |
469 | return A; |
470 | } |
471 | |
472 | DerivedArgList *Driver::TranslateInputArgs(const InputArgList &Args) const { |
473 | const llvm::opt::OptTable &Opts = getOpts(); |
474 | DerivedArgList *DAL = new DerivedArgList(Args); |
475 | |
476 | bool HasNostdlib = Args.hasArg(Ids: options::OPT_nostdlib); |
477 | bool HasNostdlibxx = Args.hasArg(Ids: options::OPT_nostdlibxx); |
478 | bool HasNodefaultlib = Args.hasArg(Ids: options::OPT_nodefaultlibs); |
479 | bool IgnoreUnused = false; |
480 | for (Arg *A : Args) { |
481 | if (IgnoreUnused) |
482 | A->claim(); |
483 | |
484 | if (A->getOption().matches(ID: options::OPT_start_no_unused_arguments)) { |
485 | IgnoreUnused = true; |
486 | continue; |
487 | } |
488 | if (A->getOption().matches(ID: options::OPT_end_no_unused_arguments)) { |
489 | IgnoreUnused = false; |
490 | continue; |
491 | } |
492 | |
493 | // Unfortunately, we have to parse some forwarding options (-Xassembler, |
494 | // -Xlinker, -Xpreprocessor) because we either integrate their functionality |
495 | // (assembler and preprocessor), or bypass a previous driver ('collect2'). |
496 | |
497 | // Rewrite linker options, to replace --no-demangle with a custom internal |
498 | // option. |
499 | if ((A->getOption().matches(ID: options::OPT_Wl_COMMA) || |
500 | A->getOption().matches(ID: options::OPT_Xlinker)) && |
501 | A->containsValue(Value: "--no-demangle" )) { |
502 | // Add the rewritten no-demangle argument. |
503 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Z_Xlinker__no_demangle)); |
504 | |
505 | // Add the remaining values as Xlinker arguments. |
506 | for (StringRef Val : A->getValues()) |
507 | if (Val != "--no-demangle" ) |
508 | DAL->AddSeparateArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Xlinker), Value: Val); |
509 | |
510 | continue; |
511 | } |
512 | |
513 | // Rewrite preprocessor options, to replace -Wp,-MD,FOO which is used by |
514 | // some build systems. We don't try to be complete here because we don't |
515 | // care to encourage this usage model. |
516 | if (A->getOption().matches(ID: options::OPT_Wp_COMMA) && |
517 | A->getNumValues() > 0 && |
518 | (A->getValue(N: 0) == StringRef("-MD" ) || |
519 | A->getValue(N: 0) == StringRef("-MMD" ))) { |
520 | // Rewrite to -MD/-MMD along with -MF. |
521 | if (A->getValue(N: 0) == StringRef("-MD" )) |
522 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MD)); |
523 | else |
524 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MMD)); |
525 | if (A->getNumValues() == 2) |
526 | DAL->AddSeparateArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_MF), Value: A->getValue(N: 1)); |
527 | continue; |
528 | } |
529 | |
530 | // Rewrite reserved library names. |
531 | if (A->getOption().matches(ID: options::OPT_l)) { |
532 | StringRef Value = A->getValue(); |
533 | |
534 | // Rewrite unless -nostdlib is present. |
535 | if (!HasNostdlib && !HasNodefaultlib && !HasNostdlibxx && |
536 | Value == "stdc++" ) { |
537 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Z_reserved_lib_stdcxx)); |
538 | continue; |
539 | } |
540 | |
541 | // Rewrite unconditionally. |
542 | if (Value == "cc_kext" ) { |
543 | DAL->AddFlagArg(BaseArg: A, Opt: Opts.getOption(Opt: options::OPT_Z_reserved_lib_cckext)); |
544 | continue; |
545 | } |
546 | } |
547 | |
548 | // Pick up inputs via the -- option. |
549 | if (A->getOption().matches(ID: options::OPT__DASH_DASH)) { |
550 | A->claim(); |
551 | for (StringRef Val : A->getValues()) |
552 | DAL->append(A: MakeInputArg(Args&: *DAL, Opts, Value: Val, Claim: false)); |
553 | continue; |
554 | } |
555 | |
556 | DAL->append(A); |
557 | } |
558 | |
559 | // DXC mode quits before assembly if an output object file isn't specified. |
560 | if (IsDXCMode() && !Args.hasArg(Ids: options::OPT_dxc_Fo)) |
561 | DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_S)); |
562 | |
563 | // Enforce -static if -miamcu is present. |
564 | if (Args.hasFlag(Pos: options::OPT_miamcu, Neg: options::OPT_mno_iamcu, Default: false)) |
565 | DAL->AddFlagArg(BaseArg: nullptr, Opt: Opts.getOption(Opt: options::OPT_static)); |
566 | |
567 | // Add a default value of -mlinker-version=, if one was given and the user |
568 | // didn't specify one. |
569 | #if defined(HOST_LINK_VERSION) |
570 | if (!Args.hasArg(options::OPT_mlinker_version_EQ) && |
571 | strlen(HOST_LINK_VERSION) > 0) { |
572 | DAL->AddJoinedArg(0, Opts.getOption(options::OPT_mlinker_version_EQ), |
573 | HOST_LINK_VERSION); |
574 | DAL->getLastArg(options::OPT_mlinker_version_EQ)->claim(); |
575 | } |
576 | #endif |
577 | |
578 | return DAL; |
579 | } |
580 | |
581 | static void setZosTargetVersion(const Driver &D, llvm::Triple &Target, |
582 | StringRef ArgTarget) { |
583 | |
584 | static bool BeSilent = false; |
585 | auto IsTooOldToBeSupported = [](int v, int r) -> bool { |
586 | return ((v < 2) || ((v == 2) && (r < 4))); |
587 | }; |
588 | |
589 | /* expect CURRENT, zOSV2R[45], or 0xnnnnnnnn */ |
590 | if (ArgTarget.equals_insensitive(RHS: "CURRENT" )) { |
591 | /* If the user gives CURRENT, then we rely on the LE to set */ |
592 | /* __TARGET_LIB__. There's nothing more we need to do. */ |
593 | } else { |
594 | unsigned int Version = 0; |
595 | unsigned int Release = 0; |
596 | unsigned int Modification = 0; |
597 | bool IsOk = true; |
598 | llvm::Regex ZOsvRegex("[zZ][oO][sS][vV]([0-9])[rR]([0-9])" ); |
599 | llvm::Regex HexRegex( |
600 | "0x4" /* product */ |
601 | "([0-9a-fA-F])" /* version */ |
602 | "([0-9a-fA-F][0-9a-fA-F])" /* release */ |
603 | "([0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F])" /* modification */); |
604 | SmallVector<StringRef> Matches; |
605 | |
606 | if (ZOsvRegex.match(String: ArgTarget, Matches: &Matches)) { |
607 | Matches[1].getAsInteger(Radix: 10, Result&: Version); |
608 | Matches[2].getAsInteger(Radix: 10, Result&: Release); |
609 | Modification = 0; |
610 | if (IsTooOldToBeSupported(Version, Release)) { |
611 | if (!BeSilent) |
612 | D.Diag(DiagID: diag::err_zos_target_release_discontinued) << ArgTarget; |
613 | IsOk = false; |
614 | } |
615 | } else if (HexRegex.match(String: ArgTarget, Matches: &Matches)) { |
616 | Matches[1].getAsInteger(Radix: 16, Result&: Version); |
617 | Matches[2].getAsInteger(Radix: 16, Result&: Release); |
618 | Matches[3].getAsInteger(Radix: 16, Result&: Modification); |
619 | if (IsTooOldToBeSupported(Version, Release)) { |
620 | if (!BeSilent) |
621 | D.Diag(DiagID: diag::err_zos_target_release_discontinued) << ArgTarget; |
622 | IsOk = false; |
623 | } |
624 | } else { |
625 | /* something else: need to report an error */ |
626 | if (!BeSilent) |
627 | D.Diag(DiagID: diag::err_zos_target_unrecognized_release) << ArgTarget; |
628 | IsOk = false; |
629 | } |
630 | |
631 | if (IsOk) { |
632 | llvm::VersionTuple V(Version, Release, Modification); |
633 | llvm::VersionTuple TV = Target.getOSVersion(); |
634 | // The goal is to pick the minimally supported version of |
635 | // the OS. Pick the lesser as the target. |
636 | if (TV.empty() || V < TV) { |
637 | SmallString<16> Str; |
638 | Str = llvm::Triple::getOSTypeName(Kind: Target.getOS()); |
639 | Str += V.getAsString(); |
640 | Target.setOSName(Str); |
641 | } |
642 | } |
643 | } |
644 | BeSilent = true; |
645 | } |
646 | |
647 | /// Compute target triple from args. |
648 | /// |
649 | /// This routine provides the logic to compute a target triple from various |
650 | /// args passed to the driver and the default triple string. |
651 | static llvm::Triple computeTargetTriple(const Driver &D, |
652 | StringRef TargetTriple, |
653 | const ArgList &Args, |
654 | StringRef DarwinArchName = "" ) { |
655 | // FIXME: Already done in Compilation *Driver::BuildCompilation |
656 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_target)) |
657 | TargetTriple = A->getValue(); |
658 | |
659 | llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple)); |
660 | |
661 | // GNU/Hurd's triples should have been -hurd-gnu*, but were historically made |
662 | // -gnu* only, and we can not change this, so we have to detect that case as |
663 | // being the Hurd OS. |
664 | if (TargetTriple.contains(Other: "-unknown-gnu" ) || TargetTriple.contains(Other: "-pc-gnu" )) |
665 | Target.setOSName("hurd" ); |
666 | |
667 | // Handle Apple-specific options available here. |
668 | if (Target.isOSBinFormatMachO()) { |
669 | // If an explicit Darwin arch name is given, that trumps all. |
670 | if (!DarwinArchName.empty()) { |
671 | tools::darwin::setTripleTypeForMachOArchName(T&: Target, Str: DarwinArchName, |
672 | Args); |
673 | return Target; |
674 | } |
675 | |
676 | // Handle the Darwin '-arch' flag. |
677 | if (Arg *A = Args.getLastArg(Ids: options::OPT_arch)) { |
678 | StringRef ArchName = A->getValue(); |
679 | tools::darwin::setTripleTypeForMachOArchName(T&: Target, Str: ArchName, Args); |
680 | } |
681 | } |
682 | |
683 | // Handle pseudo-target flags '-mlittle-endian'/'-EL' and |
684 | // '-mbig-endian'/'-EB'. |
685 | if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_mlittle_endian, |
686 | Ids: options::OPT_mbig_endian)) { |
687 | llvm::Triple T = A->getOption().matches(ID: options::OPT_mlittle_endian) |
688 | ? Target.getLittleEndianArchVariant() |
689 | : Target.getBigEndianArchVariant(); |
690 | if (T.getArch() != llvm::Triple::UnknownArch) { |
691 | Target = std::move(T); |
692 | Args.claimAllArgs(Ids: options::OPT_mlittle_endian, Ids: options::OPT_mbig_endian); |
693 | } |
694 | } |
695 | |
696 | // Skip further flag support on OSes which don't support '-m32' or '-m64'. |
697 | if (Target.getArch() == llvm::Triple::tce) |
698 | return Target; |
699 | |
700 | // On AIX, the env OBJECT_MODE may affect the resulting arch variant. |
701 | if (Target.isOSAIX()) { |
702 | if (std::optional<std::string> ObjectModeValue = |
703 | llvm::sys::Process::GetEnv(name: "OBJECT_MODE" )) { |
704 | StringRef ObjectMode = *ObjectModeValue; |
705 | llvm::Triple::ArchType AT = llvm::Triple::UnknownArch; |
706 | |
707 | if (ObjectMode == "64" ) { |
708 | AT = Target.get64BitArchVariant().getArch(); |
709 | } else if (ObjectMode == "32" ) { |
710 | AT = Target.get32BitArchVariant().getArch(); |
711 | } else { |
712 | D.Diag(DiagID: diag::err_drv_invalid_object_mode) << ObjectMode; |
713 | } |
714 | |
715 | if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) |
716 | Target.setArch(Kind: AT); |
717 | } |
718 | } |
719 | |
720 | // Currently the only architecture supported by *-uefi triples are x86_64. |
721 | if (Target.isUEFI() && Target.getArch() != llvm::Triple::x86_64) |
722 | D.Diag(DiagID: diag::err_target_unknown_triple) << Target.str(); |
723 | |
724 | // The `-maix[32|64]` flags are only valid for AIX targets. |
725 | if (Arg *A = Args.getLastArgNoClaim(Ids: options::OPT_maix32, Ids: options::OPT_maix64); |
726 | A && !Target.isOSAIX()) |
727 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
728 | << A->getAsString(Args) << Target.str(); |
729 | |
730 | // Handle pseudo-target flags '-m64', '-mx32', '-m32' and '-m16'. |
731 | Arg *A = Args.getLastArg(Ids: options::OPT_m64, Ids: options::OPT_mx32, |
732 | Ids: options::OPT_m32, Ids: options::OPT_m16, |
733 | Ids: options::OPT_maix32, Ids: options::OPT_maix64); |
734 | if (A) { |
735 | llvm::Triple::ArchType AT = llvm::Triple::UnknownArch; |
736 | |
737 | if (A->getOption().matches(ID: options::OPT_m64) || |
738 | A->getOption().matches(ID: options::OPT_maix64)) { |
739 | AT = Target.get64BitArchVariant().getArch(); |
740 | if (Target.getEnvironment() == llvm::Triple::GNUX32 || |
741 | Target.getEnvironment() == llvm::Triple::GNUT64) |
742 | Target.setEnvironment(llvm::Triple::GNU); |
743 | else if (Target.getEnvironment() == llvm::Triple::MuslX32) |
744 | Target.setEnvironment(llvm::Triple::Musl); |
745 | } else if (A->getOption().matches(ID: options::OPT_mx32) && |
746 | Target.get64BitArchVariant().getArch() == llvm::Triple::x86_64) { |
747 | AT = llvm::Triple::x86_64; |
748 | if (Target.getEnvironment() == llvm::Triple::Musl) |
749 | Target.setEnvironment(llvm::Triple::MuslX32); |
750 | else |
751 | Target.setEnvironment(llvm::Triple::GNUX32); |
752 | } else if (A->getOption().matches(ID: options::OPT_m32) || |
753 | A->getOption().matches(ID: options::OPT_maix32)) { |
754 | if (D.IsFlangMode() && !Target.isOSAIX()) { |
755 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
756 | << A->getAsString(Args) << Target.str(); |
757 | } else { |
758 | AT = Target.get32BitArchVariant().getArch(); |
759 | if (Target.getEnvironment() == llvm::Triple::GNUX32) |
760 | Target.setEnvironment(llvm::Triple::GNU); |
761 | else if (Target.getEnvironment() == llvm::Triple::MuslX32) |
762 | Target.setEnvironment(llvm::Triple::Musl); |
763 | } |
764 | } else if (A->getOption().matches(ID: options::OPT_m16) && |
765 | Target.get32BitArchVariant().getArch() == llvm::Triple::x86) { |
766 | AT = llvm::Triple::x86; |
767 | Target.setEnvironment(llvm::Triple::CODE16); |
768 | } |
769 | |
770 | if (AT != llvm::Triple::UnknownArch && AT != Target.getArch()) { |
771 | Target.setArch(Kind: AT); |
772 | if (Target.isWindowsGNUEnvironment()) |
773 | toolchains::MinGW::fixTripleArch(D, Triple&: Target, Args); |
774 | } |
775 | } |
776 | |
777 | if (Target.isOSzOS()) { |
778 | if ((A = Args.getLastArg(Ids: options::OPT_mzos_target_EQ))) { |
779 | setZosTargetVersion(D, Target, ArgTarget: A->getValue()); |
780 | } |
781 | } |
782 | |
783 | // Handle -miamcu flag. |
784 | if (Args.hasFlag(Pos: options::OPT_miamcu, Neg: options::OPT_mno_iamcu, Default: false)) { |
785 | if (Target.get32BitArchVariant().getArch() != llvm::Triple::x86) |
786 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) << "-miamcu" |
787 | << Target.str(); |
788 | |
789 | if (A && !A->getOption().matches(ID: options::OPT_m32)) |
790 | D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) |
791 | << "-miamcu" << A->getBaseArg().getAsString(Args); |
792 | |
793 | Target.setArch(Kind: llvm::Triple::x86); |
794 | Target.setArchName("i586" ); |
795 | Target.setEnvironment(llvm::Triple::UnknownEnvironment); |
796 | Target.setEnvironmentName("" ); |
797 | Target.setOS(llvm::Triple::ELFIAMCU); |
798 | Target.setVendor(llvm::Triple::UnknownVendor); |
799 | Target.setVendorName("intel" ); |
800 | } |
801 | |
802 | // If target is MIPS adjust the target triple |
803 | // accordingly to provided ABI name. |
804 | if (Target.isMIPS()) { |
805 | if ((A = Args.getLastArg(Ids: options::OPT_mabi_EQ))) { |
806 | StringRef ABIName = A->getValue(); |
807 | if (ABIName == "32" ) { |
808 | Target = Target.get32BitArchVariant(); |
809 | if (Target.getEnvironment() == llvm::Triple::GNUABI64 || |
810 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
811 | Target.setEnvironment(llvm::Triple::GNU); |
812 | } else if (ABIName == "n32" ) { |
813 | Target = Target.get64BitArchVariant(); |
814 | if (Target.getEnvironment() == llvm::Triple::GNU || |
815 | Target.getEnvironment() == llvm::Triple::GNUT64 || |
816 | Target.getEnvironment() == llvm::Triple::GNUABI64) |
817 | Target.setEnvironment(llvm::Triple::GNUABIN32); |
818 | else if (Target.getEnvironment() == llvm::Triple::Musl || |
819 | Target.getEnvironment() == llvm::Triple::MuslABI64) |
820 | Target.setEnvironment(llvm::Triple::MuslABIN32); |
821 | } else if (ABIName == "64" ) { |
822 | Target = Target.get64BitArchVariant(); |
823 | if (Target.getEnvironment() == llvm::Triple::GNU || |
824 | Target.getEnvironment() == llvm::Triple::GNUT64 || |
825 | Target.getEnvironment() == llvm::Triple::GNUABIN32) |
826 | Target.setEnvironment(llvm::Triple::GNUABI64); |
827 | else if (Target.getEnvironment() == llvm::Triple::Musl || |
828 | Target.getEnvironment() == llvm::Triple::MuslABIN32) |
829 | Target.setEnvironment(llvm::Triple::MuslABI64); |
830 | } |
831 | } |
832 | } |
833 | |
834 | // If target is RISC-V adjust the target triple according to |
835 | // provided architecture name |
836 | if (Target.isRISCV()) { |
837 | if (Args.hasArg(Ids: options::OPT_march_EQ) || |
838 | Args.hasArg(Ids: options::OPT_mcpu_EQ)) { |
839 | std::string ArchName = tools::riscv::getRISCVArch(Args, Triple: Target); |
840 | auto ISAInfo = llvm::RISCVISAInfo::parseArchString( |
841 | Arch: ArchName, /*EnableExperimentalExtensions=*/EnableExperimentalExtension: true); |
842 | if (!llvm::errorToBool(Err: ISAInfo.takeError())) { |
843 | unsigned XLen = (*ISAInfo)->getXLen(); |
844 | if (XLen == 32) |
845 | Target.setArch(Kind: llvm::Triple::riscv32); |
846 | else if (XLen == 64) |
847 | Target.setArch(Kind: llvm::Triple::riscv64); |
848 | } |
849 | } |
850 | } |
851 | |
852 | return Target; |
853 | } |
854 | |
855 | // Parse the LTO options and record the type of LTO compilation |
856 | // based on which -f(no-)?lto(=.*)? or -f(no-)?offload-lto(=.*)? |
857 | // option occurs last. |
858 | static driver::LTOKind parseLTOMode(Driver &D, const llvm::opt::ArgList &Args, |
859 | OptSpecifier OptEq, OptSpecifier OptNeg) { |
860 | if (!Args.hasFlag(Pos: OptEq, Neg: OptNeg, Default: false)) |
861 | return LTOK_None; |
862 | |
863 | const Arg *A = Args.getLastArg(Ids: OptEq); |
864 | StringRef LTOName = A->getValue(); |
865 | |
866 | driver::LTOKind LTOMode = llvm::StringSwitch<LTOKind>(LTOName) |
867 | .Case(S: "full" , Value: LTOK_Full) |
868 | .Case(S: "thin" , Value: LTOK_Thin) |
869 | .Default(Value: LTOK_Unknown); |
870 | |
871 | if (LTOMode == LTOK_Unknown) { |
872 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) |
873 | << A->getSpelling() << A->getValue(); |
874 | return LTOK_None; |
875 | } |
876 | return LTOMode; |
877 | } |
878 | |
879 | // Parse the LTO options. |
880 | void Driver::setLTOMode(const llvm::opt::ArgList &Args) { |
881 | LTOMode = |
882 | parseLTOMode(D&: *this, Args, OptEq: options::OPT_flto_EQ, OptNeg: options::OPT_fno_lto); |
883 | |
884 | OffloadLTOMode = parseLTOMode(D&: *this, Args, OptEq: options::OPT_foffload_lto_EQ, |
885 | OptNeg: options::OPT_fno_offload_lto); |
886 | |
887 | // Try to enable `-foffload-lto=full` if `-fopenmp-target-jit` is on. |
888 | if (Args.hasFlag(Pos: options::OPT_fopenmp_target_jit, |
889 | Neg: options::OPT_fno_openmp_target_jit, Default: false)) { |
890 | if (Arg *A = Args.getLastArg(Ids: options::OPT_foffload_lto_EQ, |
891 | Ids: options::OPT_fno_offload_lto)) |
892 | if (OffloadLTOMode != LTOK_Full) |
893 | Diag(DiagID: diag::err_drv_incompatible_options) |
894 | << A->getSpelling() << "-fopenmp-target-jit" ; |
895 | OffloadLTOMode = LTOK_Full; |
896 | } |
897 | } |
898 | |
899 | /// Compute the desired OpenMP runtime from the flags provided. |
900 | Driver::OpenMPRuntimeKind Driver::getOpenMPRuntime(const ArgList &Args) const { |
901 | StringRef RuntimeName(CLANG_DEFAULT_OPENMP_RUNTIME); |
902 | |
903 | const Arg *A = Args.getLastArg(Ids: options::OPT_fopenmp_EQ); |
904 | if (A) |
905 | RuntimeName = A->getValue(); |
906 | |
907 | auto RT = llvm::StringSwitch<OpenMPRuntimeKind>(RuntimeName) |
908 | .Case(S: "libomp" , Value: OMPRT_OMP) |
909 | .Case(S: "libgomp" , Value: OMPRT_GOMP) |
910 | .Case(S: "libiomp5" , Value: OMPRT_IOMP5) |
911 | .Default(Value: OMPRT_Unknown); |
912 | |
913 | if (RT == OMPRT_Unknown) { |
914 | if (A) |
915 | Diag(DiagID: diag::err_drv_unsupported_option_argument) |
916 | << A->getSpelling() << A->getValue(); |
917 | else |
918 | // FIXME: We could use a nicer diagnostic here. |
919 | Diag(DiagID: diag::err_drv_unsupported_opt) << "-fopenmp" ; |
920 | } |
921 | |
922 | return RT; |
923 | } |
924 | |
925 | static llvm::Triple getSYCLDeviceTriple(StringRef TargetArch) { |
926 | SmallVector<StringRef, 5> SYCLAlias = {"spir" , "spir64" , "spirv" , "spirv32" , |
927 | "spirv64" }; |
928 | if (llvm::is_contained(Range&: SYCLAlias, Element: TargetArch)) { |
929 | llvm::Triple TargetTriple; |
930 | TargetTriple.setArchName(TargetArch); |
931 | TargetTriple.setVendor(llvm::Triple::UnknownVendor); |
932 | TargetTriple.setOS(llvm::Triple::UnknownOS); |
933 | return TargetTriple; |
934 | } |
935 | return llvm::Triple(TargetArch); |
936 | } |
937 | |
938 | static bool addSYCLDefaultTriple(Compilation &C, |
939 | SmallVectorImpl<llvm::Triple> &SYCLTriples) { |
940 | // Check current set of triples to see if the default has already been set. |
941 | for (const auto &SYCLTriple : SYCLTriples) { |
942 | if (SYCLTriple.getSubArch() == llvm::Triple::NoSubArch && |
943 | SYCLTriple.isSPIROrSPIRV()) |
944 | return false; |
945 | } |
946 | // Add the default triple as it was not found. |
947 | llvm::Triple DefaultTriple = getSYCLDeviceTriple( |
948 | TargetArch: C.getDefaultToolChain().getTriple().isArch32Bit() ? "spirv32" |
949 | : "spirv64" ); |
950 | SYCLTriples.insert(I: SYCLTriples.begin(), Elt: DefaultTriple); |
951 | return true; |
952 | } |
953 | |
954 | void Driver::CreateOffloadingDeviceToolChains(Compilation &C, |
955 | InputList &Inputs) { |
956 | |
957 | // |
958 | // CUDA/HIP |
959 | // |
960 | // We need to generate a CUDA/HIP toolchain if any of the inputs has a CUDA |
961 | // or HIP type. However, mixed CUDA/HIP compilation is not supported. |
962 | bool IsCuda = |
963 | llvm::any_of(Range&: Inputs, P: [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
964 | return types::isCuda(Id: I.first); |
965 | }); |
966 | bool IsHIP = |
967 | llvm::any_of(Range&: Inputs, |
968 | P: [](std::pair<types::ID, const llvm::opt::Arg *> &I) { |
969 | return types::isHIP(Id: I.first); |
970 | }) || |
971 | C.getInputArgs().hasArg(Ids: options::OPT_hip_link) || |
972 | C.getInputArgs().hasArg(Ids: options::OPT_hipstdpar); |
973 | bool UseLLVMOffload = C.getInputArgs().hasArg( |
974 | Ids: options::OPT_foffload_via_llvm, Ids: options::OPT_fno_offload_via_llvm, Ids: false); |
975 | if (IsCuda && IsHIP) { |
976 | Diag(DiagID: clang::diag::err_drv_mix_cuda_hip); |
977 | return; |
978 | } |
979 | if (IsCuda && !UseLLVMOffload) { |
980 | auto CudaTriple = getNVIDIAOffloadTargetTriple( |
981 | D: *this, Args: C.getInputArgs(), HostTriple: C.getDefaultToolChain().getTriple()); |
982 | if (!CudaTriple) |
983 | return; |
984 | |
985 | auto &TC = |
986 | getOffloadToolChain(Args: C.getInputArgs(), Kind: Action::OFK_Cuda, Target: *CudaTriple, |
987 | AuxTarget: C.getDefaultToolChain().getTriple()); |
988 | |
989 | // Emit a warning if the detected CUDA version is too new. |
990 | const CudaInstallationDetector &CudaInstallation = |
991 | static_cast<const toolchains::CudaToolChain &>(TC).CudaInstallation; |
992 | if (CudaInstallation.isValid()) |
993 | CudaInstallation.WarnIfUnsupportedVersion(); |
994 | C.addOffloadDeviceToolChain(DeviceToolChain: &TC, OffloadKind: Action::OFK_Cuda); |
995 | OffloadArchs[&TC] = getOffloadArchs(C, Args: C.getArgs(), Kind: Action::OFK_Cuda, TC: &TC, |
996 | /*SpecificToolchain=*/true); |
997 | } else if (IsHIP && !UseLLVMOffload) { |
998 | if (auto *OMPTargetArg = |
999 | C.getInputArgs().getLastArg(Ids: options::OPT_fopenmp_targets_EQ)) { |
1000 | Diag(DiagID: clang::diag::err_drv_unsupported_opt_for_language_mode) |
1001 | << OMPTargetArg->getSpelling() << "HIP" ; |
1002 | return; |
1003 | } |
1004 | |
1005 | auto HIPTriple = getHIPOffloadTargetTriple(D: *this, Args: C.getInputArgs()); |
1006 | if (!HIPTriple) |
1007 | return; |
1008 | |
1009 | auto &TC = |
1010 | getOffloadToolChain(Args: C.getInputArgs(), Kind: Action::OFK_HIP, Target: *HIPTriple, |
1011 | AuxTarget: C.getDefaultToolChain().getTriple()); |
1012 | C.addOffloadDeviceToolChain(DeviceToolChain: &TC, OffloadKind: Action::OFK_HIP); |
1013 | |
1014 | // TODO: Fix 'amdgcnspirv' handling with the new driver. |
1015 | if (C.getInputArgs().hasFlag(Pos: options::OPT_offload_new_driver, |
1016 | Neg: options::OPT_no_offload_new_driver, Default: false)) |
1017 | OffloadArchs[&TC] = getOffloadArchs(C, Args: C.getArgs(), Kind: Action::OFK_HIP, TC: &TC, |
1018 | /*SpecificToolchain=*/true); |
1019 | } |
1020 | |
1021 | if (IsCuda || IsHIP) |
1022 | CUIDOpts = CUIDOptions(C.getArgs(), *this); |
1023 | |
1024 | // |
1025 | // OpenMP |
1026 | // |
1027 | // We need to generate an OpenMP toolchain if the user specified targets with |
1028 | // the -fopenmp-targets option or used --offload-arch with OpenMP enabled. |
1029 | bool IsOpenMPOffloading = |
1030 | ((IsCuda || IsHIP) && UseLLVMOffload) || |
1031 | (C.getInputArgs().hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ, |
1032 | Neg: options::OPT_fno_openmp, Default: false) && |
1033 | (C.getInputArgs().hasArg(Ids: options::OPT_fopenmp_targets_EQ) || |
1034 | C.getInputArgs().hasArg(Ids: options::OPT_offload_arch_EQ))); |
1035 | if (IsOpenMPOffloading) { |
1036 | // We expect that -fopenmp-targets is always used in conjunction with the |
1037 | // option -fopenmp specifying a valid runtime with offloading support, i.e. |
1038 | // libomp or libiomp. |
1039 | OpenMPRuntimeKind RuntimeKind = getOpenMPRuntime(Args: C.getInputArgs()); |
1040 | if (RuntimeKind != OMPRT_OMP && RuntimeKind != OMPRT_IOMP5) { |
1041 | Diag(DiagID: clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); |
1042 | return; |
1043 | } |
1044 | |
1045 | // If the user specified -fopenmp-targets= we create a toolchain for each |
1046 | // valid triple. Otherwise, if only --offload-arch= was specified we instead |
1047 | // attempt to derive the appropriate toolchains from the arguments. |
1048 | if (Arg *OpenMPTargets = |
1049 | C.getInputArgs().getLastArg(Ids: options::OPT_fopenmp_targets_EQ)) { |
1050 | if (OpenMPTargets && !OpenMPTargets->getNumValues()) { |
1051 | Diag(DiagID: clang::diag::warn_drv_empty_joined_argument) |
1052 | << OpenMPTargets->getAsString(Args: C.getInputArgs()); |
1053 | return; |
1054 | } |
1055 | |
1056 | // Make sure these show up in a deterministic order. |
1057 | std::multiset<StringRef> OpenMPTriples; |
1058 | for (StringRef T : OpenMPTargets->getValues()) |
1059 | OpenMPTriples.insert(x: T); |
1060 | |
1061 | llvm::StringMap<StringRef> FoundNormalizedTriples; |
1062 | for (StringRef T : OpenMPTriples) { |
1063 | llvm::Triple TT(ToolChain::getOpenMPTriple(TripleStr: T)); |
1064 | std::string NormalizedName = TT.normalize(); |
1065 | |
1066 | // Make sure we don't have a duplicate triple. |
1067 | auto [TripleIt, Inserted] = |
1068 | FoundNormalizedTriples.try_emplace(Key: NormalizedName, Args&: T); |
1069 | if (!Inserted) { |
1070 | Diag(DiagID: clang::diag::warn_drv_omp_offload_target_duplicate) |
1071 | << T << TripleIt->second; |
1072 | continue; |
1073 | } |
1074 | |
1075 | // If the specified target is invalid, emit a diagnostic. |
1076 | if (TT.getArch() == llvm::Triple::UnknownArch) { |
1077 | Diag(DiagID: clang::diag::err_drv_invalid_omp_target) << T; |
1078 | continue; |
1079 | } |
1080 | |
1081 | auto &TC = getOffloadToolChain(Args: C.getInputArgs(), Kind: Action::OFK_OpenMP, Target: TT, |
1082 | AuxTarget: C.getDefaultToolChain().getTriple()); |
1083 | C.addOffloadDeviceToolChain(DeviceToolChain: &TC, OffloadKind: Action::OFK_OpenMP); |
1084 | OffloadArchs[&TC] = |
1085 | getOffloadArchs(C, Args: C.getArgs(), Kind: Action::OFK_OpenMP, TC: &TC, |
1086 | /*SpecificToolchain=*/true); |
1087 | } |
1088 | } else if (C.getInputArgs().hasArg(Ids: options::OPT_offload_arch_EQ) && |
1089 | ((!IsHIP && !IsCuda) || UseLLVMOffload)) { |
1090 | llvm::Triple AMDTriple("amdgcn-amd-amdhsa" ); |
1091 | llvm::Triple NVPTXTriple("nvptx64-nvidia-cuda" ); |
1092 | |
1093 | for (StringRef Arch : |
1094 | C.getInputArgs().getAllArgValues(Id: options::OPT_offload_arch_EQ)) { |
1095 | bool IsNVPTX = IsNVIDIAOffloadArch( |
1096 | A: StringToOffloadArch(S: getProcessorFromTargetID(T: NVPTXTriple, OffloadArch: Arch))); |
1097 | bool IsAMDGPU = IsAMDOffloadArch( |
1098 | A: StringToOffloadArch(S: getProcessorFromTargetID(T: AMDTriple, OffloadArch: Arch))); |
1099 | if (!IsNVPTX && !IsAMDGPU && !Arch.empty() && |
1100 | !Arch.equals_insensitive(RHS: "native" )) { |
1101 | Diag(DiagID: clang::diag::err_drv_failed_to_deduce_target_from_arch) << Arch; |
1102 | return; |
1103 | } |
1104 | } |
1105 | |
1106 | // Attempt to deduce the offloading triple from the set of architectures. |
1107 | // We can only correctly deduce NVPTX / AMDGPU triples currently. |
1108 | for (const llvm::Triple &TT : {AMDTriple, NVPTXTriple}) { |
1109 | auto &TC = getOffloadToolChain(Args: C.getInputArgs(), Kind: Action::OFK_OpenMP, Target: TT, |
1110 | AuxTarget: C.getDefaultToolChain().getTriple()); |
1111 | |
1112 | llvm::SmallVector<StringRef> Archs = |
1113 | getOffloadArchs(C, Args: C.getArgs(), Kind: Action::OFK_OpenMP, TC: &TC, |
1114 | /*SpecificToolchain=*/false); |
1115 | if (!Archs.empty()) { |
1116 | C.addOffloadDeviceToolChain(DeviceToolChain: &TC, OffloadKind: Action::OFK_OpenMP); |
1117 | OffloadArchs[&TC] = Archs; |
1118 | } |
1119 | } |
1120 | |
1121 | // If the set is empty then we failed to find a native architecture. |
1122 | auto TCRange = C.getOffloadToolChains(Kind: Action::OFK_OpenMP); |
1123 | if (TCRange.first == TCRange.second) |
1124 | Diag(DiagID: clang::diag::err_drv_failed_to_deduce_target_from_arch) |
1125 | << "native" ; |
1126 | } |
1127 | } else if (C.getInputArgs().hasArg(Ids: options::OPT_fopenmp_targets_EQ)) { |
1128 | Diag(DiagID: clang::diag::err_drv_expecting_fopenmp_with_fopenmp_targets); |
1129 | return; |
1130 | } |
1131 | |
1132 | // We need to generate a SYCL toolchain if the user specified -fsycl. |
1133 | bool IsSYCL = C.getInputArgs().hasFlag(Pos: options::OPT_fsycl, |
1134 | Neg: options::OPT_fno_sycl, Default: false); |
1135 | |
1136 | auto argSYCLIncompatible = [&](OptSpecifier OptId) { |
1137 | if (!IsSYCL) |
1138 | return; |
1139 | if (Arg *IncompatArg = C.getInputArgs().getLastArg(Ids: OptId)) |
1140 | Diag(DiagID: clang::diag::err_drv_argument_not_allowed_with) |
1141 | << IncompatArg->getSpelling() << "-fsycl" ; |
1142 | }; |
1143 | // -static-libstdc++ is not compatible with -fsycl. |
1144 | argSYCLIncompatible(options::OPT_static_libstdcxx); |
1145 | // -ffreestanding cannot be used with -fsycl |
1146 | argSYCLIncompatible(options::OPT_ffreestanding); |
1147 | |
1148 | llvm::SmallVector<llvm::Triple, 4> UniqueSYCLTriplesVec; |
1149 | |
1150 | if (IsSYCL) { |
1151 | addSYCLDefaultTriple(C, SYCLTriples&: UniqueSYCLTriplesVec); |
1152 | |
1153 | // We'll need to use the SYCL and host triples as the key into |
1154 | // getOffloadingDeviceToolChain, because the device toolchains we're |
1155 | // going to create will depend on both. |
1156 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
1157 | for (const auto &TT : UniqueSYCLTriplesVec) { |
1158 | auto &TC = getOffloadToolChain(Args: C.getInputArgs(), Kind: Action::OFK_SYCL, Target: TT, |
1159 | AuxTarget: HostTC->getTriple()); |
1160 | C.addOffloadDeviceToolChain(DeviceToolChain: &TC, OffloadKind: Action::OFK_SYCL); |
1161 | OffloadArchs[&TC] = getOffloadArchs(C, Args: C.getArgs(), Kind: Action::OFK_SYCL, TC: &TC, |
1162 | /*SpecificToolchain=*/true); |
1163 | } |
1164 | } |
1165 | |
1166 | // |
1167 | // TODO: Add support for other offloading programming models here. |
1168 | // |
1169 | } |
1170 | |
1171 | bool Driver::loadZOSCustomizationFile(llvm::cl::ExpansionContext &ExpCtx) { |
1172 | if (IsCLMode() || IsDXCMode() || IsFlangMode()) |
1173 | return false; |
1174 | |
1175 | SmallString<128> CustomizationFile; |
1176 | StringRef PathLIBEnv = StringRef(getenv(name: "CLANG_CONFIG_PATH" )).trim(); |
1177 | // If the env var is a directory then append "/clang.cfg" and treat |
1178 | // that as the config file. Otherwise treat the env var as the |
1179 | // config file. |
1180 | if (!PathLIBEnv.empty()) { |
1181 | llvm::sys::path::append(path&: CustomizationFile, a: PathLIBEnv); |
1182 | if (llvm::sys::fs::is_directory(Path: PathLIBEnv)) |
1183 | llvm::sys::path::append(path&: CustomizationFile, a: "/clang.cfg" ); |
1184 | if (llvm::sys::fs::is_regular_file(Path: CustomizationFile)) |
1185 | return readConfigFile(FileName: CustomizationFile, ExpCtx); |
1186 | Diag(DiagID: diag::err_drv_config_file_not_found) << CustomizationFile; |
1187 | return true; |
1188 | } |
1189 | |
1190 | SmallString<128> BaseDir(llvm::sys::path::parent_path(path: Dir)); |
1191 | llvm::sys::path::append(path&: CustomizationFile, a: BaseDir + "/etc/clang.cfg" ); |
1192 | if (llvm::sys::fs::is_regular_file(Path: CustomizationFile)) |
1193 | return readConfigFile(FileName: CustomizationFile, ExpCtx); |
1194 | |
1195 | // If no customization file, just return |
1196 | return false; |
1197 | } |
1198 | |
1199 | static void appendOneArg(InputArgList &Args, const Arg *Opt) { |
1200 | // The args for config files or /clang: flags belong to different InputArgList |
1201 | // objects than Args. This copies an Arg from one of those other InputArgLists |
1202 | // to the ownership of Args. |
1203 | unsigned Index = Args.MakeIndex(String0: Opt->getSpelling()); |
1204 | Arg *Copy = new Arg(Opt->getOption(), Args.getArgString(Index), Index); |
1205 | Copy->getValues() = Opt->getValues(); |
1206 | if (Opt->isClaimed()) |
1207 | Copy->claim(); |
1208 | Copy->setOwnsValues(Opt->getOwnsValues()); |
1209 | Opt->setOwnsValues(false); |
1210 | Args.append(A: Copy); |
1211 | if (Opt->getAlias()) { |
1212 | const Arg *Alias = Opt->getAlias(); |
1213 | unsigned Index = Args.MakeIndex(String0: Alias->getSpelling()); |
1214 | auto AliasCopy = std::make_unique<Arg>(args: Alias->getOption(), |
1215 | args: Args.getArgString(Index), args&: Index); |
1216 | AliasCopy->getValues() = Alias->getValues(); |
1217 | AliasCopy->setOwnsValues(false); |
1218 | if (Alias->isClaimed()) |
1219 | AliasCopy->claim(); |
1220 | Copy->setAlias(std::move(AliasCopy)); |
1221 | } |
1222 | } |
1223 | |
1224 | bool Driver::readConfigFile(StringRef FileName, |
1225 | llvm::cl::ExpansionContext &ExpCtx) { |
1226 | // Try opening the given file. |
1227 | auto Status = getVFS().status(Path: FileName); |
1228 | if (!Status) { |
1229 | Diag(DiagID: diag::err_drv_cannot_open_config_file) |
1230 | << FileName << Status.getError().message(); |
1231 | return true; |
1232 | } |
1233 | if (Status->getType() != llvm::sys::fs::file_type::regular_file) { |
1234 | Diag(DiagID: diag::err_drv_cannot_open_config_file) |
1235 | << FileName << "not a regular file" ; |
1236 | return true; |
1237 | } |
1238 | |
1239 | // Try reading the given file. |
1240 | SmallVector<const char *, 32> NewCfgFileArgs; |
1241 | if (llvm::Error Err = ExpCtx.readConfigFile(CfgFile: FileName, Argv&: NewCfgFileArgs)) { |
1242 | Diag(DiagID: diag::err_drv_cannot_read_config_file) |
1243 | << FileName << toString(E: std::move(Err)); |
1244 | return true; |
1245 | } |
1246 | |
1247 | // Populate head and tail lists. The tail list is used only when linking. |
1248 | SmallVector<const char *, 32> NewCfgHeadArgs, NewCfgTailArgs; |
1249 | for (const char *Opt : NewCfgFileArgs) { |
1250 | // An $-prefixed option should go to the tail list. |
1251 | if (Opt[0] == '$' && Opt[1]) |
1252 | NewCfgTailArgs.push_back(Elt: Opt + 1); |
1253 | else |
1254 | NewCfgHeadArgs.push_back(Elt: Opt); |
1255 | } |
1256 | |
1257 | // Read options from config file. |
1258 | llvm::SmallString<128> CfgFileName(FileName); |
1259 | llvm::sys::path::native(path&: CfgFileName); |
1260 | bool ContainErrors = false; |
1261 | auto NewHeadOptions = std::make_unique<InputArgList>( |
1262 | args: ParseArgStrings(ArgStrings: NewCfgHeadArgs, /*UseDriverMode=*/true, ContainsError&: ContainErrors)); |
1263 | if (ContainErrors) |
1264 | return true; |
1265 | auto NewTailOptions = std::make_unique<InputArgList>( |
1266 | args: ParseArgStrings(ArgStrings: NewCfgTailArgs, /*UseDriverMode=*/true, ContainsError&: ContainErrors)); |
1267 | if (ContainErrors) |
1268 | return true; |
1269 | |
1270 | // Claim all arguments that come from a configuration file so that the driver |
1271 | // does not warn on any that is unused. |
1272 | for (Arg *A : *NewHeadOptions) |
1273 | A->claim(); |
1274 | for (Arg *A : *NewTailOptions) |
1275 | A->claim(); |
1276 | |
1277 | if (!CfgOptionsHead) |
1278 | CfgOptionsHead = std::move(NewHeadOptions); |
1279 | else { |
1280 | // If this is a subsequent config file, append options to the previous one. |
1281 | for (auto *Opt : *NewHeadOptions) |
1282 | appendOneArg(Args&: *CfgOptionsHead, Opt); |
1283 | } |
1284 | |
1285 | if (!CfgOptionsTail) |
1286 | CfgOptionsTail = std::move(NewTailOptions); |
1287 | else { |
1288 | // If this is a subsequent config file, append options to the previous one. |
1289 | for (auto *Opt : *NewTailOptions) |
1290 | appendOneArg(Args&: *CfgOptionsTail, Opt); |
1291 | } |
1292 | |
1293 | ConfigFiles.push_back(x: std::string(CfgFileName)); |
1294 | return false; |
1295 | } |
1296 | |
1297 | bool Driver::loadConfigFiles() { |
1298 | llvm::cl::ExpansionContext ExpCtx(Saver.getAllocator(), |
1299 | llvm::cl::tokenizeConfigFile); |
1300 | ExpCtx.setVFS(&getVFS()); |
1301 | |
1302 | // Process options that change search path for config files. |
1303 | if (CLOptions) { |
1304 | if (CLOptions->hasArg(Ids: options::OPT_config_system_dir_EQ)) { |
1305 | SmallString<128> CfgDir; |
1306 | CfgDir.append( |
1307 | RHS: CLOptions->getLastArgValue(Id: options::OPT_config_system_dir_EQ)); |
1308 | if (CfgDir.empty() || getVFS().makeAbsolute(Path&: CfgDir)) |
1309 | SystemConfigDir.clear(); |
1310 | else |
1311 | SystemConfigDir = static_cast<std::string>(CfgDir); |
1312 | } |
1313 | if (CLOptions->hasArg(Ids: options::OPT_config_user_dir_EQ)) { |
1314 | SmallString<128> CfgDir; |
1315 | llvm::sys::fs::expand_tilde( |
1316 | path: CLOptions->getLastArgValue(Id: options::OPT_config_user_dir_EQ), output&: CfgDir); |
1317 | if (CfgDir.empty() || getVFS().makeAbsolute(Path&: CfgDir)) |
1318 | UserConfigDir.clear(); |
1319 | else |
1320 | UserConfigDir = static_cast<std::string>(CfgDir); |
1321 | } |
1322 | } |
1323 | |
1324 | // Prepare list of directories where config file is searched for. |
1325 | StringRef CfgFileSearchDirs[] = {UserConfigDir, SystemConfigDir, Dir}; |
1326 | ExpCtx.setSearchDirs(CfgFileSearchDirs); |
1327 | |
1328 | // First try to load configuration from the default files, return on error. |
1329 | if (loadDefaultConfigFiles(ExpCtx)) |
1330 | return true; |
1331 | |
1332 | // Then load configuration files specified explicitly. |
1333 | SmallString<128> CfgFilePath; |
1334 | if (CLOptions) { |
1335 | for (auto CfgFileName : CLOptions->getAllArgValues(Id: options::OPT_config)) { |
1336 | // If argument contains directory separator, treat it as a path to |
1337 | // configuration file. |
1338 | if (llvm::sys::path::has_parent_path(path: CfgFileName)) { |
1339 | CfgFilePath.assign(RHS: CfgFileName); |
1340 | if (llvm::sys::path::is_relative(path: CfgFilePath)) { |
1341 | if (getVFS().makeAbsolute(Path&: CfgFilePath)) { |
1342 | Diag(DiagID: diag::err_drv_cannot_open_config_file) |
1343 | << CfgFilePath << "cannot get absolute path" ; |
1344 | return true; |
1345 | } |
1346 | } |
1347 | } else if (!ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath)) { |
1348 | // Report an error that the config file could not be found. |
1349 | Diag(DiagID: diag::err_drv_config_file_not_found) << CfgFileName; |
1350 | for (const StringRef &SearchDir : CfgFileSearchDirs) |
1351 | if (!SearchDir.empty()) |
1352 | Diag(DiagID: diag::note_drv_config_file_searched_in) << SearchDir; |
1353 | return true; |
1354 | } |
1355 | |
1356 | // Try to read the config file, return on error. |
1357 | if (readConfigFile(FileName: CfgFilePath, ExpCtx)) |
1358 | return true; |
1359 | } |
1360 | } |
1361 | |
1362 | // No error occurred. |
1363 | return false; |
1364 | } |
1365 | |
1366 | static bool findTripleConfigFile(llvm::cl::ExpansionContext &ExpCtx, |
1367 | SmallString<128> &ConfigFilePath, |
1368 | llvm::Triple Triple, std::string Suffix) { |
1369 | // First, try the full unmodified triple. |
1370 | if (ExpCtx.findConfigFile(FileName: Triple.str() + Suffix, FilePath&: ConfigFilePath)) |
1371 | return true; |
1372 | |
1373 | // Don't continue if we didn't find a parsable version in the triple. |
1374 | VersionTuple OSVersion = Triple.getOSVersion(); |
1375 | if (!OSVersion.getMinor().has_value()) |
1376 | return false; |
1377 | |
1378 | std::string BaseOSName = Triple.getOSTypeName(Kind: Triple.getOS()).str(); |
1379 | |
1380 | // Next try strip the version to only include the major component. |
1381 | // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin23 |
1382 | if (OSVersion.getMajor() != 0) { |
1383 | Triple.setOSName(BaseOSName + llvm::utostr(X: OSVersion.getMajor())); |
1384 | if (ExpCtx.findConfigFile(FileName: Triple.str() + Suffix, FilePath&: ConfigFilePath)) |
1385 | return true; |
1386 | } |
1387 | |
1388 | // Finally, try without any version suffix at all. |
1389 | // e.g. arm64-apple-darwin23.6.0 -> arm64-apple-darwin |
1390 | Triple.setOSName(BaseOSName); |
1391 | return ExpCtx.findConfigFile(FileName: Triple.str() + Suffix, FilePath&: ConfigFilePath); |
1392 | } |
1393 | |
1394 | bool Driver::loadDefaultConfigFiles(llvm::cl::ExpansionContext &ExpCtx) { |
1395 | // Disable default config if CLANG_NO_DEFAULT_CONFIG is set to a non-empty |
1396 | // value. |
1397 | if (const char *NoConfigEnv = ::getenv(name: "CLANG_NO_DEFAULT_CONFIG" )) { |
1398 | if (*NoConfigEnv) |
1399 | return false; |
1400 | } |
1401 | if (CLOptions && CLOptions->hasArg(Ids: options::OPT_no_default_config)) |
1402 | return false; |
1403 | |
1404 | std::string RealMode = getExecutableForDriverMode(Mode); |
1405 | llvm::Triple Triple; |
1406 | |
1407 | // If name prefix is present, no --target= override was passed via CLOptions |
1408 | // and the name prefix is not a valid triple, force it for backwards |
1409 | // compatibility. |
1410 | if (!ClangNameParts.TargetPrefix.empty() && |
1411 | computeTargetTriple(D: *this, TargetTriple: "/invalid/" , Args: *CLOptions).str() == |
1412 | "/invalid/" ) { |
1413 | llvm::Triple PrefixTriple{ClangNameParts.TargetPrefix}; |
1414 | if (PrefixTriple.getArch() == llvm::Triple::UnknownArch || |
1415 | PrefixTriple.isOSUnknown()) |
1416 | Triple = PrefixTriple; |
1417 | } |
1418 | |
1419 | // Otherwise, use the real triple as used by the driver. |
1420 | llvm::Triple RealTriple = |
1421 | computeTargetTriple(D: *this, TargetTriple, Args: *CLOptions); |
1422 | if (Triple.str().empty()) { |
1423 | Triple = RealTriple; |
1424 | assert(!Triple.str().empty()); |
1425 | } |
1426 | |
1427 | // On z/OS, start by loading the customization file before loading |
1428 | // the usual default config file(s). |
1429 | if (RealTriple.isOSzOS() && loadZOSCustomizationFile(ExpCtx)) |
1430 | return true; |
1431 | |
1432 | // Search for config files in the following order: |
1433 | // 1. <triple>-<mode>.cfg using real driver mode |
1434 | // (e.g. i386-pc-linux-gnu-clang++.cfg). |
1435 | // 2. <triple>-<mode>.cfg using executable suffix |
1436 | // (e.g. i386-pc-linux-gnu-clang-g++.cfg for *clang-g++). |
1437 | // 3. <triple>.cfg + <mode>.cfg using real driver mode |
1438 | // (e.g. i386-pc-linux-gnu.cfg + clang++.cfg). |
1439 | // 4. <triple>.cfg + <mode>.cfg using executable suffix |
1440 | // (e.g. i386-pc-linux-gnu.cfg + clang-g++.cfg for *clang-g++). |
1441 | |
1442 | // Try loading <triple>-<mode>.cfg, and return if we find a match. |
1443 | SmallString<128> CfgFilePath; |
1444 | if (findTripleConfigFile(ExpCtx, ConfigFilePath&: CfgFilePath, Triple, |
1445 | Suffix: "-" + RealMode + ".cfg" )) |
1446 | return readConfigFile(FileName: CfgFilePath, ExpCtx); |
1447 | |
1448 | bool TryModeSuffix = !ClangNameParts.ModeSuffix.empty() && |
1449 | ClangNameParts.ModeSuffix != RealMode; |
1450 | if (TryModeSuffix) { |
1451 | if (findTripleConfigFile(ExpCtx, ConfigFilePath&: CfgFilePath, Triple, |
1452 | Suffix: "-" + ClangNameParts.ModeSuffix + ".cfg" )) |
1453 | return readConfigFile(FileName: CfgFilePath, ExpCtx); |
1454 | } |
1455 | |
1456 | // Try loading <mode>.cfg, and return if loading failed. If a matching file |
1457 | // was not found, still proceed on to try <triple>.cfg. |
1458 | std::string CfgFileName = RealMode + ".cfg" ; |
1459 | if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath)) { |
1460 | if (readConfigFile(FileName: CfgFilePath, ExpCtx)) |
1461 | return true; |
1462 | } else if (TryModeSuffix) { |
1463 | CfgFileName = ClangNameParts.ModeSuffix + ".cfg" ; |
1464 | if (ExpCtx.findConfigFile(FileName: CfgFileName, FilePath&: CfgFilePath) && |
1465 | readConfigFile(FileName: CfgFilePath, ExpCtx)) |
1466 | return true; |
1467 | } |
1468 | |
1469 | // Try loading <triple>.cfg and return if we find a match. |
1470 | if (findTripleConfigFile(ExpCtx, ConfigFilePath&: CfgFilePath, Triple, Suffix: ".cfg" )) |
1471 | return readConfigFile(FileName: CfgFilePath, ExpCtx); |
1472 | |
1473 | // If we were unable to find a config file deduced from executable name, |
1474 | // that is not an error. |
1475 | return false; |
1476 | } |
1477 | |
1478 | Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) { |
1479 | llvm::PrettyStackTraceString CrashInfo("Compilation construction" ); |
1480 | |
1481 | // FIXME: Handle environment options which affect driver behavior, somewhere |
1482 | // (client?). GCC_EXEC_PREFIX, LPATH, CC_PRINT_OPTIONS. |
1483 | |
1484 | // We look for the driver mode option early, because the mode can affect |
1485 | // how other options are parsed. |
1486 | |
1487 | auto DriverMode = getDriverMode(ProgName: ClangExecutable, Args: ArgList.slice(N: 1)); |
1488 | if (!DriverMode.empty()) |
1489 | setDriverMode(DriverMode); |
1490 | |
1491 | // FIXME: What are we going to do with -V and -b? |
1492 | |
1493 | // Arguments specified in command line. |
1494 | bool ContainsError; |
1495 | CLOptions = std::make_unique<InputArgList>( |
1496 | args: ParseArgStrings(ArgStrings: ArgList.slice(N: 1), /*UseDriverMode=*/true, ContainsError)); |
1497 | |
1498 | // Try parsing configuration file. |
1499 | if (!ContainsError) |
1500 | ContainsError = loadConfigFiles(); |
1501 | bool HasConfigFileHead = !ContainsError && CfgOptionsHead; |
1502 | bool HasConfigFileTail = !ContainsError && CfgOptionsTail; |
1503 | |
1504 | // All arguments, from both config file and command line. |
1505 | InputArgList Args = |
1506 | HasConfigFileHead ? std::move(*CfgOptionsHead) : std::move(*CLOptions); |
1507 | |
1508 | if (HasConfigFileHead) |
1509 | for (auto *Opt : *CLOptions) |
1510 | if (!Opt->getOption().matches(ID: options::OPT_config)) |
1511 | appendOneArg(Args, Opt); |
1512 | |
1513 | // In CL mode, look for any pass-through arguments |
1514 | if (IsCLMode() && !ContainsError) { |
1515 | SmallVector<const char *, 16> CLModePassThroughArgList; |
1516 | for (const auto *A : Args.filtered(Ids: options::OPT__SLASH_clang)) { |
1517 | A->claim(); |
1518 | CLModePassThroughArgList.push_back(Elt: A->getValue()); |
1519 | } |
1520 | |
1521 | if (!CLModePassThroughArgList.empty()) { |
1522 | // Parse any pass through args using default clang processing rather |
1523 | // than clang-cl processing. |
1524 | auto CLModePassThroughOptions = std::make_unique<InputArgList>( |
1525 | args: ParseArgStrings(ArgStrings: CLModePassThroughArgList, /*UseDriverMode=*/false, |
1526 | ContainsError)); |
1527 | |
1528 | if (!ContainsError) |
1529 | for (auto *Opt : *CLModePassThroughOptions) |
1530 | appendOneArg(Args, Opt); |
1531 | } |
1532 | } |
1533 | |
1534 | // Check for working directory option before accessing any files |
1535 | if (Arg *WD = Args.getLastArg(Ids: options::OPT_working_directory)) |
1536 | if (VFS->setCurrentWorkingDirectory(WD->getValue())) |
1537 | Diag(DiagID: diag::err_drv_unable_to_set_working_directory) << WD->getValue(); |
1538 | |
1539 | // Check for missing include directories. |
1540 | if (!Diags.isIgnored(DiagID: diag::warn_missing_include_dirs, Loc: SourceLocation())) { |
1541 | for (auto IncludeDir : Args.getAllArgValues(Id: options::OPT_I_Group)) { |
1542 | if (!VFS->exists(Path: IncludeDir)) |
1543 | Diag(DiagID: diag::warn_missing_include_dirs) << IncludeDir; |
1544 | } |
1545 | } |
1546 | |
1547 | // FIXME: This stuff needs to go into the Compilation, not the driver. |
1548 | bool CCCPrintPhases; |
1549 | |
1550 | // -canonical-prefixes, -no-canonical-prefixes are used very early in main. |
1551 | Args.ClaimAllArgs(Id0: options::OPT_canonical_prefixes); |
1552 | Args.ClaimAllArgs(Id0: options::OPT_no_canonical_prefixes); |
1553 | |
1554 | // f(no-)integated-cc1 is also used very early in main. |
1555 | Args.ClaimAllArgs(Id0: options::OPT_fintegrated_cc1); |
1556 | Args.ClaimAllArgs(Id0: options::OPT_fno_integrated_cc1); |
1557 | |
1558 | // Ignore -pipe. |
1559 | Args.ClaimAllArgs(Id0: options::OPT_pipe); |
1560 | |
1561 | // Extract -ccc args. |
1562 | // |
1563 | // FIXME: We need to figure out where this behavior should live. Most of it |
1564 | // should be outside in the client; the parts that aren't should have proper |
1565 | // options, either by introducing new ones or by overloading gcc ones like -V |
1566 | // or -b. |
1567 | CCCPrintPhases = Args.hasArg(Ids: options::OPT_ccc_print_phases); |
1568 | CCCPrintBindings = Args.hasArg(Ids: options::OPT_ccc_print_bindings); |
1569 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_ccc_gcc_name)) |
1570 | CCCGenericGCCName = A->getValue(); |
1571 | |
1572 | // Process -fproc-stat-report options. |
1573 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_fproc_stat_report_EQ)) { |
1574 | CCPrintProcessStats = true; |
1575 | CCPrintStatReportFilename = A->getValue(); |
1576 | } |
1577 | if (Args.hasArg(Ids: options::OPT_fproc_stat_report)) |
1578 | CCPrintProcessStats = true; |
1579 | |
1580 | // FIXME: TargetTriple is used by the target-prefixed calls to as/ld |
1581 | // and getToolChain is const. |
1582 | if (IsCLMode()) { |
1583 | // clang-cl targets MSVC-style Win32. |
1584 | llvm::Triple T(TargetTriple); |
1585 | T.setOS(llvm::Triple::Win32); |
1586 | T.setVendor(llvm::Triple::PC); |
1587 | T.setEnvironment(llvm::Triple::MSVC); |
1588 | T.setObjectFormat(llvm::Triple::COFF); |
1589 | if (Args.hasArg(Ids: options::OPT__SLASH_arm64EC)) |
1590 | T.setArch(Kind: llvm::Triple::aarch64, SubArch: llvm::Triple::AArch64SubArch_arm64ec); |
1591 | TargetTriple = T.str(); |
1592 | } else if (IsDXCMode()) { |
1593 | // Build TargetTriple from target_profile option for clang-dxc. |
1594 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_target_profile)) { |
1595 | StringRef TargetProfile = A->getValue(); |
1596 | if (auto Triple = |
1597 | toolchains::HLSLToolChain::parseTargetProfile(TargetProfile)) |
1598 | TargetTriple = *Triple; |
1599 | else |
1600 | Diag(DiagID: diag::err_drv_invalid_directx_shader_module) << TargetProfile; |
1601 | |
1602 | A->claim(); |
1603 | |
1604 | if (Args.hasArg(Ids: options::OPT_spirv)) { |
1605 | const llvm::StringMap<llvm::Triple::SubArchType> ValidTargets = { |
1606 | {"vulkan1.2" , llvm::Triple::SPIRVSubArch_v15}, |
1607 | {"vulkan1.3" , llvm::Triple::SPIRVSubArch_v16}}; |
1608 | llvm::Triple T(TargetTriple); |
1609 | |
1610 | // Set specific Vulkan version. Default to vulkan1.3. |
1611 | auto TargetInfo = ValidTargets.find(Key: "vulkan1.3" ); |
1612 | assert(TargetInfo != ValidTargets.end()); |
1613 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_fspv_target_env_EQ)) { |
1614 | TargetInfo = ValidTargets.find(Key: A->getValue()); |
1615 | if (TargetInfo == ValidTargets.end()) { |
1616 | Diag(DiagID: diag::err_drv_invalid_value) |
1617 | << A->getAsString(Args) << A->getValue(); |
1618 | } |
1619 | A->claim(); |
1620 | } |
1621 | if (TargetInfo != ValidTargets.end()) { |
1622 | T.setOSName(TargetInfo->getKey()); |
1623 | T.setArch(Kind: llvm::Triple::spirv, SubArch: TargetInfo->getValue()); |
1624 | TargetTriple = T.str(); |
1625 | } |
1626 | } |
1627 | } else { |
1628 | Diag(DiagID: diag::err_drv_dxc_missing_target_profile); |
1629 | } |
1630 | } |
1631 | |
1632 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_target)) |
1633 | TargetTriple = A->getValue(); |
1634 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_ccc_install_dir)) |
1635 | Dir = Dir = A->getValue(); |
1636 | for (const Arg *A : Args.filtered(Ids: options::OPT_B)) { |
1637 | A->claim(); |
1638 | PrefixDirs.push_back(Elt: A->getValue(N: 0)); |
1639 | } |
1640 | if (std::optional<std::string> CompilerPathValue = |
1641 | llvm::sys::Process::GetEnv(name: "COMPILER_PATH" )) { |
1642 | StringRef CompilerPath = *CompilerPathValue; |
1643 | while (!CompilerPath.empty()) { |
1644 | std::pair<StringRef, StringRef> Split = |
1645 | CompilerPath.split(Separator: llvm::sys::EnvPathSeparator); |
1646 | PrefixDirs.push_back(Elt: std::string(Split.first)); |
1647 | CompilerPath = Split.second; |
1648 | } |
1649 | } |
1650 | if (const Arg *A = Args.getLastArg(Ids: options::OPT__sysroot_EQ)) |
1651 | SysRoot = A->getValue(); |
1652 | if (const Arg *A = Args.getLastArg(Ids: options::OPT__dyld_prefix_EQ)) |
1653 | DyldPrefix = A->getValue(); |
1654 | |
1655 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_resource_dir)) |
1656 | ResourceDir = A->getValue(); |
1657 | |
1658 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_save_temps_EQ)) { |
1659 | SaveTemps = llvm::StringSwitch<SaveTempsMode>(A->getValue()) |
1660 | .Case(S: "cwd" , Value: SaveTempsCwd) |
1661 | .Case(S: "obj" , Value: SaveTempsObj) |
1662 | .Default(Value: SaveTempsCwd); |
1663 | } |
1664 | |
1665 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_offload_host_only, |
1666 | Ids: options::OPT_offload_device_only, |
1667 | Ids: options::OPT_offload_host_device)) { |
1668 | if (A->getOption().matches(ID: options::OPT_offload_host_only)) |
1669 | Offload = OffloadHost; |
1670 | else if (A->getOption().matches(ID: options::OPT_offload_device_only)) |
1671 | Offload = OffloadDevice; |
1672 | else |
1673 | Offload = OffloadHostDevice; |
1674 | } |
1675 | |
1676 | setLTOMode(Args); |
1677 | |
1678 | // Process -fembed-bitcode= flags. |
1679 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fembed_bitcode_EQ)) { |
1680 | StringRef Name = A->getValue(); |
1681 | unsigned Model = llvm::StringSwitch<unsigned>(Name) |
1682 | .Case(S: "off" , Value: EmbedNone) |
1683 | .Case(S: "all" , Value: EmbedBitcode) |
1684 | .Case(S: "bitcode" , Value: EmbedBitcode) |
1685 | .Case(S: "marker" , Value: EmbedMarker) |
1686 | .Default(Value: ~0U); |
1687 | if (Model == ~0U) { |
1688 | Diags.Report(DiagID: diag::err_drv_invalid_value) << A->getAsString(Args) |
1689 | << Name; |
1690 | } else |
1691 | BitcodeEmbed = static_cast<BitcodeEmbedMode>(Model); |
1692 | } |
1693 | |
1694 | // Remove existing compilation database so that each job can append to it. |
1695 | if (Arg *A = Args.getLastArg(Ids: options::OPT_MJ)) |
1696 | llvm::sys::fs::remove(path: A->getValue()); |
1697 | |
1698 | // Setting up the jobs for some precompile cases depends on whether we are |
1699 | // treating them as PCH, implicit modules or C++20 ones. |
1700 | // TODO: inferring the mode like this seems fragile (it meets the objective |
1701 | // of not requiring anything new for operation, however). |
1702 | const Arg *Std = Args.getLastArg(Ids: options::OPT_std_EQ); |
1703 | ModulesModeCXX20 = |
1704 | !Args.hasArg(Ids: options::OPT_fmodules) && Std && |
1705 | (Std->containsValue(Value: "c++20" ) || Std->containsValue(Value: "c++2a" ) || |
1706 | Std->containsValue(Value: "c++23" ) || Std->containsValue(Value: "c++2b" ) || |
1707 | Std->containsValue(Value: "c++26" ) || Std->containsValue(Value: "c++2c" ) || |
1708 | Std->containsValue(Value: "c++latest" )); |
1709 | |
1710 | // Process -fmodule-header{=} flags. |
1711 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fmodule_header_EQ, |
1712 | Ids: options::OPT_fmodule_header)) { |
1713 | // These flags force C++20 handling of headers. |
1714 | ModulesModeCXX20 = true; |
1715 | if (A->getOption().matches(ID: options::OPT_fmodule_header)) |
1716 | CXX20HeaderType = HeaderMode_Default; |
1717 | else { |
1718 | StringRef ArgName = A->getValue(); |
1719 | unsigned Kind = llvm::StringSwitch<unsigned>(ArgName) |
1720 | .Case(S: "user" , Value: HeaderMode_User) |
1721 | .Case(S: "system" , Value: HeaderMode_System) |
1722 | .Default(Value: ~0U); |
1723 | if (Kind == ~0U) { |
1724 | Diags.Report(DiagID: diag::err_drv_invalid_value) |
1725 | << A->getAsString(Args) << ArgName; |
1726 | } else |
1727 | CXX20HeaderType = static_cast<ModuleHeaderMode>(Kind); |
1728 | } |
1729 | } |
1730 | |
1731 | std::unique_ptr<llvm::opt::InputArgList> UArgs = |
1732 | std::make_unique<InputArgList>(args: std::move(Args)); |
1733 | |
1734 | // Owned by the host. |
1735 | const ToolChain &TC = |
1736 | getToolChain(Args: *UArgs, Target: computeTargetTriple(D: *this, TargetTriple, Args: *UArgs)); |
1737 | |
1738 | { |
1739 | SmallVector<std::string> MultilibMacroDefinesStr = |
1740 | TC.getMultilibMacroDefinesStr(Args&: *UArgs); |
1741 | SmallVector<const char *> MLMacroDefinesChar( |
1742 | llvm::map_range(C&: MultilibMacroDefinesStr, F: [&UArgs](const auto &S) { |
1743 | return UArgs->MakeArgString(Str: Twine("-D" ) + Twine(S)); |
1744 | })); |
1745 | bool MLContainsError; |
1746 | auto MultilibMacroDefineList = |
1747 | std::make_unique<InputArgList>(args: ParseArgStrings( |
1748 | ArgStrings: MLMacroDefinesChar, /*UseDriverMode=*/false, ContainsError&: MLContainsError)); |
1749 | if (!MLContainsError) { |
1750 | for (auto *Opt : *MultilibMacroDefineList) { |
1751 | appendOneArg(Args&: *UArgs, Opt); |
1752 | } |
1753 | } |
1754 | } |
1755 | |
1756 | // Perform the default argument translations. |
1757 | DerivedArgList *TranslatedArgs = TranslateInputArgs(Args: *UArgs); |
1758 | |
1759 | // Check if the environment version is valid except wasm case. |
1760 | llvm::Triple Triple = TC.getTriple(); |
1761 | if (!Triple.isWasm()) { |
1762 | StringRef TripleVersionName = Triple.getEnvironmentVersionString(); |
1763 | StringRef TripleObjectFormat = |
1764 | Triple.getObjectFormatTypeName(ObjectFormat: Triple.getObjectFormat()); |
1765 | if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "" && |
1766 | TripleVersionName != TripleObjectFormat) { |
1767 | Diags.Report(DiagID: diag::err_drv_triple_version_invalid) |
1768 | << TripleVersionName << TC.getTripleString(); |
1769 | ContainsError = true; |
1770 | } |
1771 | } |
1772 | |
1773 | // Report warning when arm64EC option is overridden by specified target |
1774 | if ((TC.getTriple().getArch() != llvm::Triple::aarch64 || |
1775 | TC.getTriple().getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) && |
1776 | UArgs->hasArg(Ids: options::OPT__SLASH_arm64EC)) { |
1777 | getDiags().Report(DiagID: clang::diag::warn_target_override_arm64ec) |
1778 | << TC.getTriple().str(); |
1779 | } |
1780 | |
1781 | // A common user mistake is specifying a target of aarch64-none-eabi or |
1782 | // arm-none-elf whereas the correct names are aarch64-none-elf & |
1783 | // arm-none-eabi. Detect these cases and issue a warning. |
1784 | if (TC.getTriple().getOS() == llvm::Triple::UnknownOS && |
1785 | TC.getTriple().getVendor() == llvm::Triple::UnknownVendor) { |
1786 | switch (TC.getTriple().getArch()) { |
1787 | case llvm::Triple::arm: |
1788 | case llvm::Triple::armeb: |
1789 | case llvm::Triple::thumb: |
1790 | case llvm::Triple::thumbeb: |
1791 | if (TC.getTriple().getEnvironmentName() == "elf" ) { |
1792 | Diag(DiagID: diag::warn_target_unrecognized_env) |
1793 | << TargetTriple |
1794 | << (TC.getTriple().getArchName().str() + "-none-eabi" ); |
1795 | } |
1796 | break; |
1797 | case llvm::Triple::aarch64: |
1798 | case llvm::Triple::aarch64_be: |
1799 | case llvm::Triple::aarch64_32: |
1800 | if (TC.getTriple().getEnvironmentName().starts_with(Prefix: "eabi" )) { |
1801 | Diag(DiagID: diag::warn_target_unrecognized_env) |
1802 | << TargetTriple |
1803 | << (TC.getTriple().getArchName().str() + "-none-elf" ); |
1804 | } |
1805 | break; |
1806 | default: |
1807 | break; |
1808 | } |
1809 | } |
1810 | |
1811 | // The compilation takes ownership of Args. |
1812 | Compilation *C = new Compilation(*this, TC, UArgs.release(), TranslatedArgs, |
1813 | ContainsError); |
1814 | |
1815 | if (!HandleImmediateArgs(C&: *C)) |
1816 | return C; |
1817 | |
1818 | // Construct the list of inputs. |
1819 | InputList Inputs; |
1820 | BuildInputs(TC: C->getDefaultToolChain(), Args&: *TranslatedArgs, Inputs); |
1821 | if (HasConfigFileTail && Inputs.size()) { |
1822 | Arg *FinalPhaseArg; |
1823 | if (getFinalPhase(DAL: *TranslatedArgs, FinalPhaseArg: &FinalPhaseArg) == phases::Link) { |
1824 | DerivedArgList TranslatedLinkerIns(*CfgOptionsTail); |
1825 | for (Arg *A : *CfgOptionsTail) |
1826 | TranslatedLinkerIns.append(A); |
1827 | BuildInputs(TC: C->getDefaultToolChain(), Args&: TranslatedLinkerIns, Inputs); |
1828 | } |
1829 | } |
1830 | |
1831 | // Populate the tool chains for the offloading devices, if any. |
1832 | CreateOffloadingDeviceToolChains(C&: *C, Inputs); |
1833 | |
1834 | // Construct the list of abstract actions to perform for this compilation. On |
1835 | // MachO targets this uses the driver-driver and universal actions. |
1836 | if (TC.getTriple().isOSBinFormatMachO()) |
1837 | BuildUniversalActions(C&: *C, TC: C->getDefaultToolChain(), BAInputs: Inputs); |
1838 | else |
1839 | BuildActions(C&: *C, Args&: C->getArgs(), Inputs, Actions&: C->getActions()); |
1840 | |
1841 | if (CCCPrintPhases) { |
1842 | PrintActions(C: *C); |
1843 | return C; |
1844 | } |
1845 | |
1846 | BuildJobs(C&: *C); |
1847 | |
1848 | return C; |
1849 | } |
1850 | |
1851 | static void printArgList(raw_ostream &OS, const llvm::opt::ArgList &Args) { |
1852 | llvm::opt::ArgStringList ASL; |
1853 | for (const auto *A : Args) { |
1854 | // Use user's original spelling of flags. For example, use |
1855 | // `/source-charset:utf-8` instead of `-finput-charset=utf-8` if the user |
1856 | // wrote the former. |
1857 | while (A->getAlias()) |
1858 | A = A->getAlias(); |
1859 | A->render(Args, Output&: ASL); |
1860 | } |
1861 | |
1862 | for (auto I = ASL.begin(), E = ASL.end(); I != E; ++I) { |
1863 | if (I != ASL.begin()) |
1864 | OS << ' '; |
1865 | llvm::sys::printArg(OS, Arg: *I, Quote: true); |
1866 | } |
1867 | OS << '\n'; |
1868 | } |
1869 | |
1870 | bool Driver::getCrashDiagnosticFile(StringRef ReproCrashFilename, |
1871 | SmallString<128> &CrashDiagDir) { |
1872 | using namespace llvm::sys; |
1873 | assert(llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin() && |
1874 | "Only knows about .crash files on Darwin" ); |
1875 | |
1876 | // The .crash file can be found on at ~/Library/Logs/DiagnosticReports/ |
1877 | // (or /Library/Logs/DiagnosticReports for root) and has the filename pattern |
1878 | // clang-<VERSION>_<YYYY-MM-DD-HHMMSS>_<hostname>.crash. |
1879 | path::home_directory(result&: CrashDiagDir); |
1880 | if (CrashDiagDir.starts_with(Prefix: "/var/root" )) |
1881 | CrashDiagDir = "/" ; |
1882 | path::append(path&: CrashDiagDir, a: "Library/Logs/DiagnosticReports" ); |
1883 | int PID = |
1884 | #if LLVM_ON_UNIX |
1885 | getpid(); |
1886 | #else |
1887 | 0; |
1888 | #endif |
1889 | std::error_code EC; |
1890 | fs::file_status FileStatus; |
1891 | TimePoint<> LastAccessTime; |
1892 | SmallString<128> CrashFilePath; |
1893 | // Lookup the .crash files and get the one generated by a subprocess spawned |
1894 | // by this driver invocation. |
1895 | for (fs::directory_iterator File(CrashDiagDir, EC), FileEnd; |
1896 | File != FileEnd && !EC; File.increment(ec&: EC)) { |
1897 | StringRef FileName = path::filename(path: File->path()); |
1898 | if (!FileName.starts_with(Prefix: Name)) |
1899 | continue; |
1900 | if (fs::status(path: File->path(), result&: FileStatus)) |
1901 | continue; |
1902 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> CrashFile = |
1903 | llvm::MemoryBuffer::getFile(Filename: File->path()); |
1904 | if (!CrashFile) |
1905 | continue; |
1906 | // The first line should start with "Process:", otherwise this isn't a real |
1907 | // .crash file. |
1908 | StringRef Data = CrashFile.get()->getBuffer(); |
1909 | if (!Data.starts_with(Prefix: "Process:" )) |
1910 | continue; |
1911 | // Parse parent process pid line, e.g: "Parent Process: clang-4.0 [79141]" |
1912 | size_t ParentProcPos = Data.find(Str: "Parent Process:" ); |
1913 | if (ParentProcPos == StringRef::npos) |
1914 | continue; |
1915 | size_t LineEnd = Data.find_first_of(Chars: "\n" , From: ParentProcPos); |
1916 | if (LineEnd == StringRef::npos) |
1917 | continue; |
1918 | StringRef ParentProcess = Data.slice(Start: ParentProcPos+15, End: LineEnd).trim(); |
1919 | int OpenBracket = -1, CloseBracket = -1; |
1920 | for (size_t i = 0, e = ParentProcess.size(); i < e; ++i) { |
1921 | if (ParentProcess[i] == '[') |
1922 | OpenBracket = i; |
1923 | if (ParentProcess[i] == ']') |
1924 | CloseBracket = i; |
1925 | } |
1926 | // Extract the parent process PID from the .crash file and check whether |
1927 | // it matches this driver invocation pid. |
1928 | int CrashPID; |
1929 | if (OpenBracket < 0 || CloseBracket < 0 || |
1930 | ParentProcess.slice(Start: OpenBracket + 1, End: CloseBracket) |
1931 | .getAsInteger(Radix: 10, Result&: CrashPID) || CrashPID != PID) { |
1932 | continue; |
1933 | } |
1934 | |
1935 | // Found a .crash file matching the driver pid. To avoid getting an older |
1936 | // and misleading crash file, continue looking for the most recent. |
1937 | // FIXME: the driver can dispatch multiple cc1 invocations, leading to |
1938 | // multiple crashes poiting to the same parent process. Since the driver |
1939 | // does not collect pid information for the dispatched invocation there's |
1940 | // currently no way to distinguish among them. |
1941 | const auto FileAccessTime = FileStatus.getLastModificationTime(); |
1942 | if (FileAccessTime > LastAccessTime) { |
1943 | CrashFilePath.assign(RHS: File->path()); |
1944 | LastAccessTime = FileAccessTime; |
1945 | } |
1946 | } |
1947 | |
1948 | // If found, copy it over to the location of other reproducer files. |
1949 | if (!CrashFilePath.empty()) { |
1950 | EC = fs::copy_file(From: CrashFilePath, To: ReproCrashFilename); |
1951 | if (EC) |
1952 | return false; |
1953 | return true; |
1954 | } |
1955 | |
1956 | return false; |
1957 | } |
1958 | |
1959 | static const char BugReporMsg[] = |
1960 | "\n********************\n\n" |
1961 | "PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:\n" |
1962 | "Preprocessed source(s) and associated run script(s) are located at:" ; |
1963 | |
1964 | // When clang crashes, produce diagnostic information including the fully |
1965 | // preprocessed source file(s). Request that the developer attach the |
1966 | // diagnostic information to a bug report. |
1967 | void Driver::generateCompilationDiagnostics( |
1968 | Compilation &C, const Command &FailingCommand, |
1969 | StringRef AdditionalInformation, CompilationDiagnosticReport *Report) { |
1970 | if (C.getArgs().hasArg(Ids: options::OPT_fno_crash_diagnostics)) |
1971 | return; |
1972 | |
1973 | unsigned Level = 1; |
1974 | if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_fcrash_diagnostics_EQ)) { |
1975 | Level = llvm::StringSwitch<unsigned>(A->getValue()) |
1976 | .Case(S: "off" , Value: 0) |
1977 | .Case(S: "compiler" , Value: 1) |
1978 | .Case(S: "all" , Value: 2) |
1979 | .Default(Value: 1); |
1980 | } |
1981 | if (!Level) |
1982 | return; |
1983 | |
1984 | // Don't try to generate diagnostics for dsymutil jobs. |
1985 | if (FailingCommand.getCreator().isDsymutilJob()) |
1986 | return; |
1987 | |
1988 | bool IsLLD = false; |
1989 | ArgStringList SavedTemps; |
1990 | if (FailingCommand.getCreator().isLinkJob()) { |
1991 | C.getDefaultToolChain().GetLinkerPath(LinkerIsLLD: &IsLLD); |
1992 | if (!IsLLD || Level < 2) |
1993 | return; |
1994 | |
1995 | // If lld crashed, we will re-run the same command with the input it used |
1996 | // to have. In that case we should not remove temp files in |
1997 | // initCompilationForDiagnostics yet. They will be added back and removed |
1998 | // later. |
1999 | SavedTemps = std::move(C.getTempFiles()); |
2000 | assert(!C.getTempFiles().size()); |
2001 | } |
2002 | |
2003 | // Print the version of the compiler. |
2004 | PrintVersion(C, OS&: llvm::errs()); |
2005 | |
2006 | // Suppress driver output and emit preprocessor output to temp file. |
2007 | CCGenDiagnostics = true; |
2008 | |
2009 | // Save the original job command(s). |
2010 | Command Cmd = FailingCommand; |
2011 | |
2012 | // Keep track of whether we produce any errors while trying to produce |
2013 | // preprocessed sources. |
2014 | DiagnosticErrorTrap Trap(Diags); |
2015 | |
2016 | // Suppress tool output. |
2017 | C.initCompilationForDiagnostics(); |
2018 | |
2019 | // If lld failed, rerun it again with --reproduce. |
2020 | if (IsLLD) { |
2021 | const char *TmpName = CreateTempFile(C, Prefix: "linker-crash" , Suffix: "tar" ); |
2022 | Command NewLLDInvocation = Cmd; |
2023 | llvm::opt::ArgStringList ArgList = NewLLDInvocation.getArguments(); |
2024 | StringRef ReproduceOption = |
2025 | C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() |
2026 | ? "/reproduce:" |
2027 | : "--reproduce=" ; |
2028 | ArgList.push_back(Elt: Saver.save(S: Twine(ReproduceOption) + TmpName).data()); |
2029 | NewLLDInvocation.replaceArguments(List: std::move(ArgList)); |
2030 | |
2031 | // Redirect stdout/stderr to /dev/null. |
2032 | NewLLDInvocation.Execute(Redirects: {std::nullopt, {"" }, {"" }}, ErrMsg: nullptr, ExecutionFailed: nullptr); |
2033 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg; |
2034 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << TmpName; |
2035 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2036 | << "\n\n********************" ; |
2037 | if (Report) |
2038 | Report->TemporaryFiles.push_back(Elt: TmpName); |
2039 | return; |
2040 | } |
2041 | |
2042 | // Construct the list of inputs. |
2043 | InputList Inputs; |
2044 | BuildInputs(TC: C.getDefaultToolChain(), Args&: C.getArgs(), Inputs); |
2045 | |
2046 | for (InputList::iterator it = Inputs.begin(), ie = Inputs.end(); it != ie;) { |
2047 | bool IgnoreInput = false; |
2048 | |
2049 | // Ignore input from stdin or any inputs that cannot be preprocessed. |
2050 | // Check type first as not all linker inputs have a value. |
2051 | if (types::getPreprocessedType(Id: it->first) == types::TY_INVALID) { |
2052 | IgnoreInput = true; |
2053 | } else if (!strcmp(s1: it->second->getValue(), s2: "-" )) { |
2054 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2055 | << "Error generating preprocessed source(s) - " |
2056 | "ignoring input from stdin." ; |
2057 | IgnoreInput = true; |
2058 | } |
2059 | |
2060 | if (IgnoreInput) { |
2061 | it = Inputs.erase(CI: it); |
2062 | ie = Inputs.end(); |
2063 | } else { |
2064 | ++it; |
2065 | } |
2066 | } |
2067 | |
2068 | if (Inputs.empty()) { |
2069 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2070 | << "Error generating preprocessed source(s) - " |
2071 | "no preprocessable inputs." ; |
2072 | return; |
2073 | } |
2074 | |
2075 | // Don't attempt to generate preprocessed files if multiple -arch options are |
2076 | // used, unless they're all duplicates. |
2077 | llvm::StringSet<> ArchNames; |
2078 | for (const Arg *A : C.getArgs()) { |
2079 | if (A->getOption().matches(ID: options::OPT_arch)) { |
2080 | StringRef ArchName = A->getValue(); |
2081 | ArchNames.insert(key: ArchName); |
2082 | } |
2083 | } |
2084 | if (ArchNames.size() > 1) { |
2085 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2086 | << "Error generating preprocessed source(s) - cannot generate " |
2087 | "preprocessed source with multiple -arch options." ; |
2088 | return; |
2089 | } |
2090 | |
2091 | // Construct the list of abstract actions to perform for this compilation. On |
2092 | // Darwin OSes this uses the driver-driver and builds universal actions. |
2093 | const ToolChain &TC = C.getDefaultToolChain(); |
2094 | if (TC.getTriple().isOSBinFormatMachO()) |
2095 | BuildUniversalActions(C, TC, BAInputs: Inputs); |
2096 | else |
2097 | BuildActions(C, Args&: C.getArgs(), Inputs, Actions&: C.getActions()); |
2098 | |
2099 | BuildJobs(C); |
2100 | |
2101 | // If there were errors building the compilation, quit now. |
2102 | if (Trap.hasErrorOccurred()) { |
2103 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2104 | << "Error generating preprocessed source(s)." ; |
2105 | return; |
2106 | } |
2107 | |
2108 | // Generate preprocessed output. |
2109 | SmallVector<std::pair<int, const Command *>, 4> FailingCommands; |
2110 | C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands); |
2111 | |
2112 | // If any of the preprocessing commands failed, clean up and exit. |
2113 | if (!FailingCommands.empty()) { |
2114 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2115 | << "Error generating preprocessed source(s)." ; |
2116 | return; |
2117 | } |
2118 | |
2119 | const ArgStringList &TempFiles = C.getTempFiles(); |
2120 | if (TempFiles.empty()) { |
2121 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2122 | << "Error generating preprocessed source(s)." ; |
2123 | return; |
2124 | } |
2125 | |
2126 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << BugReporMsg; |
2127 | |
2128 | SmallString<128> VFS; |
2129 | SmallString<128> ReproCrashFilename; |
2130 | for (const char *TempFile : TempFiles) { |
2131 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << TempFile; |
2132 | if (Report) |
2133 | Report->TemporaryFiles.push_back(Elt: TempFile); |
2134 | if (ReproCrashFilename.empty()) { |
2135 | ReproCrashFilename = TempFile; |
2136 | llvm::sys::path::replace_extension(path&: ReproCrashFilename, extension: ".crash" ); |
2137 | } |
2138 | if (StringRef(TempFile).ends_with(Suffix: ".cache" )) { |
2139 | // In some cases (modules) we'll dump extra data to help with reproducing |
2140 | // the crash into a directory next to the output. |
2141 | VFS = llvm::sys::path::filename(path: TempFile); |
2142 | llvm::sys::path::append(path&: VFS, a: "vfs" , b: "vfs.yaml" ); |
2143 | } |
2144 | } |
2145 | |
2146 | for (const char *TempFile : SavedTemps) |
2147 | C.addTempFile(Name: TempFile); |
2148 | |
2149 | // Assume associated files are based off of the first temporary file. |
2150 | CrashReportInfo CrashInfo(TempFiles[0], VFS); |
2151 | |
2152 | llvm::SmallString<128> Script(CrashInfo.Filename); |
2153 | llvm::sys::path::replace_extension(path&: Script, extension: "sh" ); |
2154 | std::error_code EC; |
2155 | llvm::raw_fd_ostream ScriptOS(Script, EC, llvm::sys::fs::CD_CreateNew, |
2156 | llvm::sys::fs::FA_Write, |
2157 | llvm::sys::fs::OF_Text); |
2158 | if (EC) { |
2159 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2160 | << "Error generating run script: " << Script << " " << EC.message(); |
2161 | } else { |
2162 | ScriptOS << "# Crash reproducer for " << getClangFullVersion() << "\n" |
2163 | << "# Driver args: " ; |
2164 | printArgList(OS&: ScriptOS, Args: C.getInputArgs()); |
2165 | ScriptOS << "# Original command: " ; |
2166 | Cmd.Print(OS&: ScriptOS, Terminator: "\n" , /*Quote=*/true); |
2167 | Cmd.Print(OS&: ScriptOS, Terminator: "\n" , /*Quote=*/true, CrashInfo: &CrashInfo); |
2168 | if (!AdditionalInformation.empty()) |
2169 | ScriptOS << "\n# Additional information: " << AdditionalInformation |
2170 | << "\n" ; |
2171 | if (Report) |
2172 | Report->TemporaryFiles.push_back(Elt: std::string(Script)); |
2173 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) << Script; |
2174 | } |
2175 | |
2176 | // On darwin, provide information about the .crash diagnostic report. |
2177 | if (llvm::Triple(llvm::sys::getProcessTriple()).isOSDarwin()) { |
2178 | SmallString<128> CrashDiagDir; |
2179 | if (getCrashDiagnosticFile(ReproCrashFilename, CrashDiagDir)) { |
2180 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2181 | << ReproCrashFilename.str(); |
2182 | } else { // Suggest a directory for the user to look for .crash files. |
2183 | llvm::sys::path::append(path&: CrashDiagDir, a: Name); |
2184 | CrashDiagDir += "_<YYYY-MM-DD-HHMMSS>_<hostname>.crash" ; |
2185 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2186 | << "Crash backtrace is located in" ; |
2187 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2188 | << CrashDiagDir.str(); |
2189 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2190 | << "(choose the .crash file that corresponds to your crash)" ; |
2191 | } |
2192 | } |
2193 | |
2194 | Diag(DiagID: clang::diag::note_drv_command_failed_diag_msg) |
2195 | << "\n\n********************" ; |
2196 | } |
2197 | |
2198 | void Driver::setUpResponseFiles(Compilation &C, Command &Cmd) { |
2199 | // Since commandLineFitsWithinSystemLimits() may underestimate system's |
2200 | // capacity if the tool does not support response files, there is a chance/ |
2201 | // that things will just work without a response file, so we silently just |
2202 | // skip it. |
2203 | if (Cmd.getResponseFileSupport().ResponseKind == |
2204 | ResponseFileSupport::RF_None || |
2205 | llvm::sys::commandLineFitsWithinSystemLimits(Program: Cmd.getExecutable(), |
2206 | Args: Cmd.getArguments())) |
2207 | return; |
2208 | |
2209 | std::string TmpName = GetTemporaryPath(Prefix: "response" , Suffix: "txt" ); |
2210 | Cmd.setResponseFile(C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName))); |
2211 | } |
2212 | |
2213 | int Driver::ExecuteCompilation( |
2214 | Compilation &C, |
2215 | SmallVectorImpl<std::pair<int, const Command *>> &FailingCommands) { |
2216 | if (C.getArgs().hasArg(Ids: options::OPT_fdriver_only)) { |
2217 | if (C.getArgs().hasArg(Ids: options::OPT_v)) |
2218 | C.getJobs().Print(OS&: llvm::errs(), Terminator: "\n" , Quote: true); |
2219 | |
2220 | C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands, /*LogOnly=*/true); |
2221 | |
2222 | // If there were errors building the compilation, quit now. |
2223 | if (!FailingCommands.empty() || Diags.hasErrorOccurred()) |
2224 | return 1; |
2225 | |
2226 | return 0; |
2227 | } |
2228 | |
2229 | // Just print if -### was present. |
2230 | if (C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH)) { |
2231 | C.getJobs().Print(OS&: llvm::errs(), Terminator: "\n" , Quote: true); |
2232 | return Diags.hasErrorOccurred() ? 1 : 0; |
2233 | } |
2234 | |
2235 | // If there were errors building the compilation, quit now. |
2236 | if (Diags.hasErrorOccurred()) |
2237 | return 1; |
2238 | |
2239 | // Set up response file names for each command, if necessary. |
2240 | for (auto &Job : C.getJobs()) |
2241 | setUpResponseFiles(C, Cmd&: Job); |
2242 | |
2243 | C.ExecuteJobs(Jobs: C.getJobs(), FailingCommands); |
2244 | |
2245 | // If the command succeeded, we are done. |
2246 | if (FailingCommands.empty()) |
2247 | return 0; |
2248 | |
2249 | // Otherwise, remove result files and print extra information about abnormal |
2250 | // failures. |
2251 | int Res = 0; |
2252 | for (const auto &CmdPair : FailingCommands) { |
2253 | int CommandRes = CmdPair.first; |
2254 | const Command *FailingCommand = CmdPair.second; |
2255 | |
2256 | // Remove result files if we're not saving temps. |
2257 | if (!isSaveTempsEnabled()) { |
2258 | const JobAction *JA = cast<JobAction>(Val: &FailingCommand->getSource()); |
2259 | C.CleanupFileMap(Files: C.getResultFiles(), JA, IssueErrors: true); |
2260 | |
2261 | // Failure result files are valid unless we crashed. |
2262 | if (CommandRes < 0) |
2263 | C.CleanupFileMap(Files: C.getFailureResultFiles(), JA, IssueErrors: true); |
2264 | } |
2265 | |
2266 | // llvm/lib/Support/*/Signals.inc will exit with a special return code |
2267 | // for SIGPIPE. Do not print diagnostics for this case. |
2268 | if (CommandRes == EX_IOERR) { |
2269 | Res = CommandRes; |
2270 | continue; |
2271 | } |
2272 | |
2273 | // Print extra information about abnormal failures, if possible. |
2274 | // |
2275 | // This is ad-hoc, but we don't want to be excessively noisy. If the result |
2276 | // status was 1, assume the command failed normally. In particular, if it |
2277 | // was the compiler then assume it gave a reasonable error code. Failures |
2278 | // in other tools are less common, and they generally have worse |
2279 | // diagnostics, so always print the diagnostic there. |
2280 | const Tool &FailingTool = FailingCommand->getCreator(); |
2281 | |
2282 | if (!FailingCommand->getCreator().hasGoodDiagnostics() || CommandRes != 1) { |
2283 | // FIXME: See FIXME above regarding result code interpretation. |
2284 | if (CommandRes < 0) |
2285 | Diag(DiagID: clang::diag::err_drv_command_signalled) |
2286 | << FailingTool.getShortName(); |
2287 | else |
2288 | Diag(DiagID: clang::diag::err_drv_command_failed) |
2289 | << FailingTool.getShortName() << CommandRes; |
2290 | } |
2291 | } |
2292 | return Res; |
2293 | } |
2294 | |
2295 | void Driver::PrintHelp(bool ShowHidden) const { |
2296 | llvm::opt::Visibility VisibilityMask = getOptionVisibilityMask(); |
2297 | |
2298 | std::string Usage = llvm::formatv(Fmt: "{0} [options] file..." , Vals: Name).str(); |
2299 | getOpts().printHelp(OS&: llvm::outs(), Usage: Usage.c_str(), Title: DriverTitle.c_str(), |
2300 | ShowHidden, /*ShowAllAliases=*/false, |
2301 | VisibilityMask); |
2302 | } |
2303 | |
2304 | void Driver::PrintVersion(const Compilation &C, raw_ostream &OS) const { |
2305 | if (IsFlangMode()) { |
2306 | OS << getClangToolFullVersion(ToolName: "flang" ) << '\n'; |
2307 | } else { |
2308 | // FIXME: The following handlers should use a callback mechanism, we don't |
2309 | // know what the client would like to do. |
2310 | OS << getClangFullVersion() << '\n'; |
2311 | } |
2312 | const ToolChain &TC = C.getDefaultToolChain(); |
2313 | OS << "Target: " << TC.getTripleString() << '\n'; |
2314 | |
2315 | // Print the threading model. |
2316 | if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_mthread_model)) { |
2317 | // Don't print if the ToolChain would have barfed on it already |
2318 | if (TC.isThreadModelSupported(Model: A->getValue())) |
2319 | OS << "Thread model: " << A->getValue(); |
2320 | } else |
2321 | OS << "Thread model: " << TC.getThreadModel(); |
2322 | OS << '\n'; |
2323 | |
2324 | // Print out the install directory. |
2325 | OS << "InstalledDir: " << Dir << '\n'; |
2326 | |
2327 | // Print the build config if it's non-default. |
2328 | // Intended to help LLVM developers understand the configs of compilers |
2329 | // they're investigating. |
2330 | if (!llvm::cl::getCompilerBuildConfig().empty()) |
2331 | llvm::cl::printBuildConfig(OS); |
2332 | |
2333 | // If configuration files were used, print their paths. |
2334 | for (auto ConfigFile : ConfigFiles) |
2335 | OS << "Configuration file: " << ConfigFile << '\n'; |
2336 | } |
2337 | |
2338 | /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories |
2339 | /// option. |
2340 | static void PrintDiagnosticCategories(raw_ostream &OS) { |
2341 | // Skip the empty category. |
2342 | for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories(); i != max; |
2343 | ++i) |
2344 | OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(CategoryID: i) << '\n'; |
2345 | } |
2346 | |
2347 | void Driver::HandleAutocompletions(StringRef PassedFlags) const { |
2348 | if (PassedFlags == "" ) |
2349 | return; |
2350 | // Print out all options that start with a given argument. This is used for |
2351 | // shell autocompletion. |
2352 | std::vector<std::string> SuggestedCompletions; |
2353 | std::vector<std::string> Flags; |
2354 | |
2355 | llvm::opt::Visibility VisibilityMask(options::ClangOption); |
2356 | |
2357 | // Make sure that Flang-only options don't pollute the Clang output |
2358 | // TODO: Make sure that Clang-only options don't pollute Flang output |
2359 | if (IsFlangMode()) |
2360 | VisibilityMask = llvm::opt::Visibility(options::FlangOption); |
2361 | |
2362 | // Distinguish "--autocomplete=-someflag" and "--autocomplete=-someflag," |
2363 | // because the latter indicates that the user put space before pushing tab |
2364 | // which should end up in a file completion. |
2365 | const bool HasSpace = PassedFlags.ends_with(Suffix: "," ); |
2366 | |
2367 | // Parse PassedFlags by "," as all the command-line flags are passed to this |
2368 | // function separated by "," |
2369 | StringRef TargetFlags = PassedFlags; |
2370 | while (TargetFlags != "" ) { |
2371 | StringRef CurFlag; |
2372 | std::tie(args&: CurFlag, args&: TargetFlags) = TargetFlags.split(Separator: "," ); |
2373 | Flags.push_back(x: std::string(CurFlag)); |
2374 | } |
2375 | |
2376 | // We want to show cc1-only options only when clang is invoked with -cc1 or |
2377 | // -Xclang. |
2378 | if (llvm::is_contained(Range&: Flags, Element: "-Xclang" ) || llvm::is_contained(Range&: Flags, Element: "-cc1" )) |
2379 | VisibilityMask = llvm::opt::Visibility(options::CC1Option); |
2380 | |
2381 | const llvm::opt::OptTable &Opts = getOpts(); |
2382 | StringRef Cur; |
2383 | Cur = Flags.at(n: Flags.size() - 1); |
2384 | StringRef Prev; |
2385 | if (Flags.size() >= 2) { |
2386 | Prev = Flags.at(n: Flags.size() - 2); |
2387 | SuggestedCompletions = Opts.suggestValueCompletions(Option: Prev, Arg: Cur); |
2388 | } |
2389 | |
2390 | if (SuggestedCompletions.empty()) |
2391 | SuggestedCompletions = Opts.suggestValueCompletions(Option: Cur, Arg: "" ); |
2392 | |
2393 | // If Flags were empty, it means the user typed `clang [tab]` where we should |
2394 | // list all possible flags. If there was no value completion and the user |
2395 | // pressed tab after a space, we should fall back to a file completion. |
2396 | // We're printing a newline to be consistent with what we print at the end of |
2397 | // this function. |
2398 | if (SuggestedCompletions.empty() && HasSpace && !Flags.empty()) { |
2399 | llvm::outs() << '\n'; |
2400 | return; |
2401 | } |
2402 | |
2403 | // When flag ends with '=' and there was no value completion, return empty |
2404 | // string and fall back to the file autocompletion. |
2405 | if (SuggestedCompletions.empty() && !Cur.ends_with(Suffix: "=" )) { |
2406 | // If the flag is in the form of "--autocomplete=-foo", |
2407 | // we were requested to print out all option names that start with "-foo". |
2408 | // For example, "--autocomplete=-fsyn" is expanded to "-fsyntax-only". |
2409 | SuggestedCompletions = Opts.findByPrefix( |
2410 | Cur, VisibilityMask, |
2411 | /*DisableFlags=*/options::Unsupported | options::Ignored); |
2412 | |
2413 | // We have to query the -W flags manually as they're not in the OptTable. |
2414 | // TODO: Find a good way to add them to OptTable instead and them remove |
2415 | // this code. |
2416 | for (StringRef S : DiagnosticIDs::getDiagnosticFlags()) |
2417 | if (S.starts_with(Prefix: Cur)) |
2418 | SuggestedCompletions.push_back(x: std::string(S)); |
2419 | } |
2420 | |
2421 | // Sort the autocomplete candidates so that shells print them out in a |
2422 | // deterministic order. We could sort in any way, but we chose |
2423 | // case-insensitive sorting for consistency with the -help option |
2424 | // which prints out options in the case-insensitive alphabetical order. |
2425 | llvm::sort(C&: SuggestedCompletions, Comp: [](StringRef A, StringRef B) { |
2426 | if (int X = A.compare_insensitive(RHS: B)) |
2427 | return X < 0; |
2428 | return A.compare(RHS: B) > 0; |
2429 | }); |
2430 | |
2431 | llvm::outs() << llvm::join(R&: SuggestedCompletions, Separator: "\n" ) << '\n'; |
2432 | } |
2433 | |
2434 | bool Driver::HandleImmediateArgs(Compilation &C) { |
2435 | // The order these options are handled in gcc is all over the place, but we |
2436 | // don't expect inconsistencies w.r.t. that to matter in practice. |
2437 | |
2438 | if (C.getArgs().hasArg(Ids: options::OPT_dumpmachine)) { |
2439 | llvm::outs() << C.getDefaultToolChain().getTripleString() << '\n'; |
2440 | return false; |
2441 | } |
2442 | |
2443 | if (C.getArgs().hasArg(Ids: options::OPT_dumpversion)) { |
2444 | // Since -dumpversion is only implemented for pedantic GCC compatibility, we |
2445 | // return an answer which matches our definition of __VERSION__. |
2446 | llvm::outs() << CLANG_VERSION_STRING << "\n" ; |
2447 | return false; |
2448 | } |
2449 | |
2450 | if (C.getArgs().hasArg(Ids: options::OPT__print_diagnostic_categories)) { |
2451 | PrintDiagnosticCategories(OS&: llvm::outs()); |
2452 | return false; |
2453 | } |
2454 | |
2455 | if (C.getArgs().hasArg(Ids: options::OPT_help) || |
2456 | C.getArgs().hasArg(Ids: options::OPT__help_hidden)) { |
2457 | PrintHelp(ShowHidden: C.getArgs().hasArg(Ids: options::OPT__help_hidden)); |
2458 | return false; |
2459 | } |
2460 | |
2461 | if (C.getArgs().hasArg(Ids: options::OPT__version)) { |
2462 | // Follow gcc behavior and use stdout for --version and stderr for -v. |
2463 | PrintVersion(C, OS&: llvm::outs()); |
2464 | return false; |
2465 | } |
2466 | |
2467 | if (C.getArgs().hasArg(Ids: options::OPT_v) || |
2468 | C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH) || |
2469 | C.getArgs().hasArg(Ids: options::OPT_print_supported_cpus) || |
2470 | C.getArgs().hasArg(Ids: options::OPT_print_supported_extensions) || |
2471 | C.getArgs().hasArg(Ids: options::OPT_print_enabled_extensions)) { |
2472 | PrintVersion(C, OS&: llvm::errs()); |
2473 | SuppressMissingInputWarning = true; |
2474 | } |
2475 | |
2476 | if (C.getArgs().hasArg(Ids: options::OPT_v)) { |
2477 | if (!SystemConfigDir.empty()) |
2478 | llvm::errs() << "System configuration file directory: " |
2479 | << SystemConfigDir << "\n" ; |
2480 | if (!UserConfigDir.empty()) |
2481 | llvm::errs() << "User configuration file directory: " |
2482 | << UserConfigDir << "\n" ; |
2483 | } |
2484 | |
2485 | const ToolChain &TC = C.getDefaultToolChain(); |
2486 | |
2487 | if (C.getArgs().hasArg(Ids: options::OPT_v)) |
2488 | TC.printVerboseInfo(OS&: llvm::errs()); |
2489 | |
2490 | if (C.getArgs().hasArg(Ids: options::OPT_print_resource_dir)) { |
2491 | llvm::outs() << ResourceDir << '\n'; |
2492 | return false; |
2493 | } |
2494 | |
2495 | if (C.getArgs().hasArg(Ids: options::OPT_print_search_dirs)) { |
2496 | llvm::outs() << "programs: =" ; |
2497 | bool separator = false; |
2498 | // Print -B and COMPILER_PATH. |
2499 | for (const std::string &Path : PrefixDirs) { |
2500 | if (separator) |
2501 | llvm::outs() << llvm::sys::EnvPathSeparator; |
2502 | llvm::outs() << Path; |
2503 | separator = true; |
2504 | } |
2505 | for (const std::string &Path : TC.getProgramPaths()) { |
2506 | if (separator) |
2507 | llvm::outs() << llvm::sys::EnvPathSeparator; |
2508 | llvm::outs() << Path; |
2509 | separator = true; |
2510 | } |
2511 | llvm::outs() << "\n" ; |
2512 | llvm::outs() << "libraries: =" << ResourceDir; |
2513 | |
2514 | StringRef sysroot = C.getSysRoot(); |
2515 | |
2516 | for (const std::string &Path : TC.getFilePaths()) { |
2517 | // Always print a separator. ResourceDir was the first item shown. |
2518 | llvm::outs() << llvm::sys::EnvPathSeparator; |
2519 | // Interpretation of leading '=' is needed only for NetBSD. |
2520 | if (Path[0] == '=') |
2521 | llvm::outs() << sysroot << Path.substr(pos: 1); |
2522 | else |
2523 | llvm::outs() << Path; |
2524 | } |
2525 | llvm::outs() << "\n" ; |
2526 | return false; |
2527 | } |
2528 | |
2529 | if (C.getArgs().hasArg(Ids: options::OPT_print_std_module_manifest_path)) { |
2530 | llvm::outs() << GetStdModuleManifestPath(C, TC: C.getDefaultToolChain()) |
2531 | << '\n'; |
2532 | return false; |
2533 | } |
2534 | |
2535 | if (C.getArgs().hasArg(Ids: options::OPT_print_runtime_dir)) { |
2536 | if (std::optional<std::string> RuntimePath = TC.getRuntimePath()) |
2537 | llvm::outs() << *RuntimePath << '\n'; |
2538 | else |
2539 | llvm::outs() << TC.getCompilerRTPath() << '\n'; |
2540 | return false; |
2541 | } |
2542 | |
2543 | if (C.getArgs().hasArg(Ids: options::OPT_print_diagnostic_options)) { |
2544 | std::vector<std::string> Flags = DiagnosticIDs::getDiagnosticFlags(); |
2545 | for (std::size_t I = 0; I != Flags.size(); I += 2) |
2546 | llvm::outs() << " " << Flags[I] << "\n " << Flags[I + 1] << "\n\n" ; |
2547 | return false; |
2548 | } |
2549 | |
2550 | // FIXME: The following handlers should use a callback mechanism, we don't |
2551 | // know what the client would like to do. |
2552 | if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_print_file_name_EQ)) { |
2553 | llvm::outs() << GetFilePath(Name: A->getValue(), TC) << "\n" ; |
2554 | return false; |
2555 | } |
2556 | |
2557 | if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_print_prog_name_EQ)) { |
2558 | StringRef ProgName = A->getValue(); |
2559 | |
2560 | // Null program name cannot have a path. |
2561 | if (! ProgName.empty()) |
2562 | llvm::outs() << GetProgramPath(Name: ProgName, TC); |
2563 | |
2564 | llvm::outs() << "\n" ; |
2565 | return false; |
2566 | } |
2567 | |
2568 | if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT_autocomplete)) { |
2569 | StringRef PassedFlags = A->getValue(); |
2570 | HandleAutocompletions(PassedFlags); |
2571 | return false; |
2572 | } |
2573 | |
2574 | if (C.getArgs().hasArg(Ids: options::OPT_print_libgcc_file_name)) { |
2575 | ToolChain::RuntimeLibType RLT = TC.GetRuntimeLibType(Args: C.getArgs()); |
2576 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args: C.getArgs())); |
2577 | // The 'Darwin' toolchain is initialized only when its arguments are |
2578 | // computed. Get the default arguments for OFK_None to ensure that |
2579 | // initialization is performed before trying to access properties of |
2580 | // the toolchain in the functions below. |
2581 | // FIXME: Remove when darwin's toolchain is initialized during construction. |
2582 | // FIXME: For some more esoteric targets the default toolchain is not the |
2583 | // correct one. |
2584 | C.getArgsForToolChain(TC: &TC, BoundArch: Triple.getArchName(), DeviceOffloadKind: Action::OFK_None); |
2585 | RegisterEffectiveTriple TripleRAII(TC, Triple); |
2586 | switch (RLT) { |
2587 | case ToolChain::RLT_CompilerRT: |
2588 | llvm::outs() << TC.getCompilerRT(Args: C.getArgs(), Component: "builtins" ) << "\n" ; |
2589 | break; |
2590 | case ToolChain::RLT_Libgcc: |
2591 | llvm::outs() << GetFilePath(Name: "libgcc.a" , TC) << "\n" ; |
2592 | break; |
2593 | } |
2594 | return false; |
2595 | } |
2596 | |
2597 | if (C.getArgs().hasArg(Ids: options::OPT_print_multi_lib)) { |
2598 | for (const Multilib &Multilib : TC.getMultilibs()) |
2599 | if (!Multilib.isError()) |
2600 | llvm::outs() << Multilib << "\n" ; |
2601 | return false; |
2602 | } |
2603 | |
2604 | if (C.getArgs().hasArg(Ids: options::OPT_print_multi_flags)) { |
2605 | Multilib::flags_list ArgFlags = TC.getMultilibFlags(C.getArgs()); |
2606 | llvm::StringSet<> ExpandedFlags = TC.getMultilibs().expandFlags(ArgFlags); |
2607 | std::set<llvm::StringRef> SortedFlags; |
2608 | for (const auto &FlagEntry : ExpandedFlags) |
2609 | SortedFlags.insert(x: FlagEntry.getKey()); |
2610 | for (auto Flag : SortedFlags) |
2611 | llvm::outs() << Flag << '\n'; |
2612 | return false; |
2613 | } |
2614 | |
2615 | if (C.getArgs().hasArg(Ids: options::OPT_print_multi_directory)) { |
2616 | for (const Multilib &Multilib : TC.getSelectedMultilibs()) { |
2617 | if (Multilib.gccSuffix().empty()) |
2618 | llvm::outs() << ".\n" ; |
2619 | else { |
2620 | StringRef Suffix(Multilib.gccSuffix()); |
2621 | assert(Suffix.front() == '/'); |
2622 | llvm::outs() << Suffix.substr(Start: 1) << "\n" ; |
2623 | } |
2624 | } |
2625 | return false; |
2626 | } |
2627 | |
2628 | if (C.getArgs().hasArg(Ids: options::OPT_print_target_triple)) { |
2629 | llvm::outs() << TC.getTripleString() << "\n" ; |
2630 | return false; |
2631 | } |
2632 | |
2633 | if (C.getArgs().hasArg(Ids: options::OPT_print_effective_triple)) { |
2634 | const llvm::Triple Triple(TC.ComputeEffectiveClangTriple(Args: C.getArgs())); |
2635 | llvm::outs() << Triple.getTriple() << "\n" ; |
2636 | return false; |
2637 | } |
2638 | |
2639 | if (C.getArgs().hasArg(Ids: options::OPT_print_targets)) { |
2640 | llvm::TargetRegistry::printRegisteredTargetsForVersion(OS&: llvm::outs()); |
2641 | return false; |
2642 | } |
2643 | |
2644 | return true; |
2645 | } |
2646 | |
2647 | enum { |
2648 | TopLevelAction = 0, |
2649 | HeadSibAction = 1, |
2650 | OtherSibAction = 2, |
2651 | }; |
2652 | |
2653 | // Display an action graph human-readably. Action A is the "sink" node |
2654 | // and latest-occuring action. Traversal is in pre-order, visiting the |
2655 | // inputs to each action before printing the action itself. |
2656 | static unsigned PrintActions1(const Compilation &C, Action *A, |
2657 | std::map<Action *, unsigned> &Ids, |
2658 | Twine Indent = {}, int Kind = TopLevelAction) { |
2659 | if (auto It = Ids.find(x: A); It != Ids.end()) // A was already visited. |
2660 | return It->second; |
2661 | |
2662 | std::string str; |
2663 | llvm::raw_string_ostream os(str); |
2664 | |
2665 | auto getSibIndent = [](int K) -> Twine { |
2666 | return (K == HeadSibAction) ? " " : (K == OtherSibAction) ? "| " : "" ; |
2667 | }; |
2668 | |
2669 | Twine SibIndent = Indent + getSibIndent(Kind); |
2670 | int SibKind = HeadSibAction; |
2671 | os << Action::getClassName(AC: A->getKind()) << ", " ; |
2672 | if (InputAction *IA = dyn_cast<InputAction>(Val: A)) { |
2673 | os << "\"" << IA->getInputArg().getValue() << "\"" ; |
2674 | } else if (BindArchAction *BIA = dyn_cast<BindArchAction>(Val: A)) { |
2675 | os << '"' << BIA->getArchName() << '"' << ", {" |
2676 | << PrintActions1(C, A: *BIA->input_begin(), Ids, Indent: SibIndent, Kind: SibKind) << "}" ; |
2677 | } else if (OffloadAction *OA = dyn_cast<OffloadAction>(Val: A)) { |
2678 | bool IsFirst = true; |
2679 | OA->doOnEachDependence( |
2680 | Work: [&](Action *A, const ToolChain *TC, const char *BoundArch) { |
2681 | assert(TC && "Unknown host toolchain" ); |
2682 | // E.g. for two CUDA device dependences whose bound arch is sm_20 and |
2683 | // sm_35 this will generate: |
2684 | // "cuda-device" (nvptx64-nvidia-cuda:sm_20) {#ID}, "cuda-device" |
2685 | // (nvptx64-nvidia-cuda:sm_35) {#ID} |
2686 | if (!IsFirst) |
2687 | os << ", " ; |
2688 | os << '"'; |
2689 | os << A->getOffloadingKindPrefix(); |
2690 | os << " (" ; |
2691 | os << TC->getTriple().normalize(); |
2692 | if (BoundArch) |
2693 | os << ":" << BoundArch; |
2694 | os << ")" ; |
2695 | os << '"'; |
2696 | os << " {" << PrintActions1(C, A, Ids, Indent: SibIndent, Kind: SibKind) << "}" ; |
2697 | IsFirst = false; |
2698 | SibKind = OtherSibAction; |
2699 | }); |
2700 | } else { |
2701 | const ActionList *AL = &A->getInputs(); |
2702 | |
2703 | if (AL->size()) { |
2704 | const char *Prefix = "{" ; |
2705 | for (Action *PreRequisite : *AL) { |
2706 | os << Prefix << PrintActions1(C, A: PreRequisite, Ids, Indent: SibIndent, Kind: SibKind); |
2707 | Prefix = ", " ; |
2708 | SibKind = OtherSibAction; |
2709 | } |
2710 | os << "}" ; |
2711 | } else |
2712 | os << "{}" ; |
2713 | } |
2714 | |
2715 | // Append offload info for all options other than the offloading action |
2716 | // itself (e.g. (cuda-device, sm_20) or (cuda-host)). |
2717 | std::string offload_str; |
2718 | llvm::raw_string_ostream offload_os(offload_str); |
2719 | if (!isa<OffloadAction>(Val: A)) { |
2720 | auto S = A->getOffloadingKindPrefix(); |
2721 | if (!S.empty()) { |
2722 | offload_os << ", (" << S; |
2723 | if (A->getOffloadingArch()) |
2724 | offload_os << ", " << A->getOffloadingArch(); |
2725 | offload_os << ")" ; |
2726 | } |
2727 | } |
2728 | |
2729 | auto getSelfIndent = [](int K) -> Twine { |
2730 | return (K == HeadSibAction) ? "+- " : (K == OtherSibAction) ? "|- " : "" ; |
2731 | }; |
2732 | |
2733 | unsigned Id = Ids.size(); |
2734 | Ids[A] = Id; |
2735 | llvm::errs() << Indent + getSelfIndent(Kind) << Id << ": " << os.str() << ", " |
2736 | << types::getTypeName(Id: A->getType()) << offload_os.str() << "\n" ; |
2737 | |
2738 | return Id; |
2739 | } |
2740 | |
2741 | // Print the action graphs in a compilation C. |
2742 | // For example "clang -c file1.c file2.c" is composed of two subgraphs. |
2743 | void Driver::PrintActions(const Compilation &C) const { |
2744 | std::map<Action *, unsigned> Ids; |
2745 | for (Action *A : C.getActions()) |
2746 | PrintActions1(C, A, Ids); |
2747 | } |
2748 | |
2749 | /// Check whether the given input tree contains any compilation or |
2750 | /// assembly actions. |
2751 | static bool ContainsCompileOrAssembleAction(const Action *A) { |
2752 | if (isa<CompileJobAction>(Val: A) || isa<BackendJobAction>(Val: A) || |
2753 | isa<AssembleJobAction>(Val: A)) |
2754 | return true; |
2755 | |
2756 | return llvm::any_of(Range: A->inputs(), P: ContainsCompileOrAssembleAction); |
2757 | } |
2758 | |
2759 | void Driver::BuildUniversalActions(Compilation &C, const ToolChain &TC, |
2760 | const InputList &BAInputs) const { |
2761 | DerivedArgList &Args = C.getArgs(); |
2762 | ActionList &Actions = C.getActions(); |
2763 | llvm::PrettyStackTraceString CrashInfo("Building universal build actions" ); |
2764 | // Collect the list of architectures. Duplicates are allowed, but should only |
2765 | // be handled once (in the order seen). |
2766 | llvm::StringSet<> ArchNames; |
2767 | SmallVector<const char *, 4> Archs; |
2768 | for (Arg *A : Args) { |
2769 | if (A->getOption().matches(ID: options::OPT_arch)) { |
2770 | // Validate the option here; we don't save the type here because its |
2771 | // particular spelling may participate in other driver choices. |
2772 | llvm::Triple::ArchType Arch = |
2773 | tools::darwin::getArchTypeForMachOArchName(Str: A->getValue()); |
2774 | if (Arch == llvm::Triple::UnknownArch) { |
2775 | Diag(DiagID: clang::diag::err_drv_invalid_arch_name) << A->getAsString(Args); |
2776 | continue; |
2777 | } |
2778 | |
2779 | A->claim(); |
2780 | if (ArchNames.insert(key: A->getValue()).second) |
2781 | Archs.push_back(Elt: A->getValue()); |
2782 | } |
2783 | } |
2784 | |
2785 | // When there is no explicit arch for this platform, make sure we still bind |
2786 | // the architecture (to the default) so that -Xarch_ is handled correctly. |
2787 | if (!Archs.size()) |
2788 | Archs.push_back(Elt: Args.MakeArgString(Str: TC.getDefaultUniversalArchName())); |
2789 | |
2790 | ActionList SingleActions; |
2791 | BuildActions(C, Args, Inputs: BAInputs, Actions&: SingleActions); |
2792 | |
2793 | // Add in arch bindings for every top level action, as well as lipo and |
2794 | // dsymutil steps if needed. |
2795 | for (Action* Act : SingleActions) { |
2796 | // Make sure we can lipo this kind of output. If not (and it is an actual |
2797 | // output) then we disallow, since we can't create an output file with the |
2798 | // right name without overwriting it. We could remove this oddity by just |
2799 | // changing the output names to include the arch, which would also fix |
2800 | // -save-temps. Compatibility wins for now. |
2801 | |
2802 | if (Archs.size() > 1 && !types::canLipoType(Id: Act->getType())) |
2803 | Diag(DiagID: clang::diag::err_drv_invalid_output_with_multiple_archs) |
2804 | << types::getTypeName(Id: Act->getType()); |
2805 | |
2806 | ActionList Inputs; |
2807 | for (unsigned i = 0, e = Archs.size(); i != e; ++i) |
2808 | Inputs.push_back(Elt: C.MakeAction<BindArchAction>(Arg&: Act, Arg&: Archs[i])); |
2809 | |
2810 | // Lipo if necessary, we do it this way because we need to set the arch flag |
2811 | // so that -Xarch_ gets overwritten. |
2812 | if (Inputs.size() == 1 || Act->getType() == types::TY_Nothing) |
2813 | Actions.append(in_start: Inputs.begin(), in_end: Inputs.end()); |
2814 | else |
2815 | Actions.push_back(Elt: C.MakeAction<LipoJobAction>(Arg&: Inputs, Arg: Act->getType())); |
2816 | |
2817 | // Handle debug info queries. |
2818 | Arg *A = Args.getLastArg(Ids: options::OPT_g_Group); |
2819 | bool enablesDebugInfo = A && !A->getOption().matches(ID: options::OPT_g0) && |
2820 | !A->getOption().matches(ID: options::OPT_gstabs); |
2821 | if ((enablesDebugInfo || willEmitRemarks(Args)) && |
2822 | ContainsCompileOrAssembleAction(A: Actions.back())) { |
2823 | |
2824 | // Add a 'dsymutil' step if necessary, when debug info is enabled and we |
2825 | // have a compile input. We need to run 'dsymutil' ourselves in such cases |
2826 | // because the debug info will refer to a temporary object file which |
2827 | // will be removed at the end of the compilation process. |
2828 | if (Act->getType() == types::TY_Image) { |
2829 | ActionList Inputs; |
2830 | Inputs.push_back(Elt: Actions.back()); |
2831 | Actions.pop_back(); |
2832 | Actions.push_back( |
2833 | Elt: C.MakeAction<DsymutilJobAction>(Arg&: Inputs, Arg: types::TY_dSYM)); |
2834 | } |
2835 | |
2836 | // Verify the debug info output. |
2837 | if (Args.hasArg(Ids: options::OPT_verify_debug_info)) { |
2838 | Action *LastAction = Actions.pop_back_val(); |
2839 | Actions.push_back(Elt: C.MakeAction<VerifyDebugInfoJobAction>( |
2840 | Arg&: LastAction, Arg: types::TY_Nothing)); |
2841 | } |
2842 | } |
2843 | } |
2844 | } |
2845 | |
2846 | bool Driver::DiagnoseInputExistence(const DerivedArgList &Args, StringRef Value, |
2847 | types::ID Ty, bool TypoCorrect) const { |
2848 | if (!getCheckInputsExist()) |
2849 | return true; |
2850 | |
2851 | // stdin always exists. |
2852 | if (Value == "-" ) |
2853 | return true; |
2854 | |
2855 | // If it's a header to be found in the system or user search path, then defer |
2856 | // complaints about its absence until those searches can be done. When we |
2857 | // are definitely processing headers for C++20 header units, extend this to |
2858 | // allow the user to put "-fmodule-header -xc++-header vector" for example. |
2859 | if (Ty == types::TY_CXXSHeader || Ty == types::TY_CXXUHeader || |
2860 | (ModulesModeCXX20 && Ty == types::TY_CXXHeader)) |
2861 | return true; |
2862 | |
2863 | if (getVFS().exists(Path: Value)) |
2864 | return true; |
2865 | |
2866 | if (TypoCorrect) { |
2867 | // Check if the filename is a typo for an option flag. OptTable thinks |
2868 | // that all args that are not known options and that start with / are |
2869 | // filenames, but e.g. `/diagnostic:caret` is more likely a typo for |
2870 | // the option `/diagnostics:caret` than a reference to a file in the root |
2871 | // directory. |
2872 | std::string Nearest; |
2873 | if (getOpts().findNearest(Option: Value, NearestString&: Nearest, VisibilityMask: getOptionVisibilityMask()) <= 1) { |
2874 | Diag(DiagID: clang::diag::err_drv_no_such_file_with_suggestion) |
2875 | << Value << Nearest; |
2876 | return false; |
2877 | } |
2878 | } |
2879 | |
2880 | // In CL mode, don't error on apparently non-existent linker inputs, because |
2881 | // they can be influenced by linker flags the clang driver might not |
2882 | // understand. |
2883 | // Examples: |
2884 | // - `clang-cl main.cc ole32.lib` in a non-MSVC shell will make the driver |
2885 | // module look for an MSVC installation in the registry. (We could ask |
2886 | // the MSVCToolChain object if it can find `ole32.lib`, but the logic to |
2887 | // look in the registry might move into lld-link in the future so that |
2888 | // lld-link invocations in non-MSVC shells just work too.) |
2889 | // - `clang-cl ... /link ...` can pass arbitrary flags to the linker, |
2890 | // including /libpath:, which is used to find .lib and .obj files. |
2891 | // So do not diagnose this on the driver level. Rely on the linker diagnosing |
2892 | // it. (If we don't end up invoking the linker, this means we'll emit a |
2893 | // "'linker' input unused [-Wunused-command-line-argument]" warning instead |
2894 | // of an error.) |
2895 | // |
2896 | // Only do this skip after the typo correction step above. `/Brepo` is treated |
2897 | // as TY_Object, but it's clearly a typo for `/Brepro`. It seems fine to emit |
2898 | // an error if we have a flag that's within an edit distance of 1 from a |
2899 | // flag. (Users can use `-Wl,` or `/linker` to launder the flag past the |
2900 | // driver in the unlikely case they run into this.) |
2901 | // |
2902 | // Don't do this for inputs that start with a '/', else we'd pass options |
2903 | // like /libpath: through to the linker silently. |
2904 | // |
2905 | // Emitting an error for linker inputs can also cause incorrect diagnostics |
2906 | // with the gcc driver. The command |
2907 | // clang -fuse-ld=lld -Wl,--chroot,some/dir /file.o |
2908 | // will make lld look for some/dir/file.o, while we will diagnose here that |
2909 | // `/file.o` does not exist. However, configure scripts check if |
2910 | // `clang /GR-` compiles without error to see if the compiler is cl.exe, |
2911 | // so we can't downgrade diagnostics for `/GR-` from an error to a warning |
2912 | // in cc mode. (We can in cl mode because cl.exe itself only warns on |
2913 | // unknown flags.) |
2914 | if (IsCLMode() && Ty == types::TY_Object && !Value.starts_with(Prefix: "/" )) |
2915 | return true; |
2916 | |
2917 | Diag(DiagID: clang::diag::err_drv_no_such_file) << Value; |
2918 | return false; |
2919 | } |
2920 | |
2921 | // Get the C++20 Header Unit type corresponding to the input type. |
2922 | static types::ID (ModuleHeaderMode HM) { |
2923 | switch (HM) { |
2924 | case HeaderMode_User: |
2925 | return types::TY_CXXUHeader; |
2926 | case HeaderMode_System: |
2927 | return types::TY_CXXSHeader; |
2928 | case HeaderMode_Default: |
2929 | break; |
2930 | case HeaderMode_None: |
2931 | llvm_unreachable("should not be called in this case" ); |
2932 | } |
2933 | return types::TY_CXXHUHeader; |
2934 | } |
2935 | |
2936 | // Construct a the list of inputs and their types. |
2937 | void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args, |
2938 | InputList &Inputs) const { |
2939 | const llvm::opt::OptTable &Opts = getOpts(); |
2940 | // Track the current user specified (-x) input. We also explicitly track the |
2941 | // argument used to set the type; we only want to claim the type when we |
2942 | // actually use it, so we warn about unused -x arguments. |
2943 | types::ID InputType = types::TY_Nothing; |
2944 | Arg *InputTypeArg = nullptr; |
2945 | |
2946 | // The last /TC or /TP option sets the input type to C or C++ globally. |
2947 | if (Arg *TCTP = Args.getLastArgNoClaim(Ids: options::OPT__SLASH_TC, |
2948 | Ids: options::OPT__SLASH_TP)) { |
2949 | InputTypeArg = TCTP; |
2950 | InputType = TCTP->getOption().matches(ID: options::OPT__SLASH_TC) |
2951 | ? types::TY_C |
2952 | : types::TY_CXX; |
2953 | |
2954 | Arg *Previous = nullptr; |
2955 | bool ShowNote = false; |
2956 | for (Arg *A : |
2957 | Args.filtered(Ids: options::OPT__SLASH_TC, Ids: options::OPT__SLASH_TP)) { |
2958 | if (Previous) { |
2959 | Diag(DiagID: clang::diag::warn_drv_overriding_option) |
2960 | << Previous->getSpelling() << A->getSpelling(); |
2961 | ShowNote = true; |
2962 | } |
2963 | Previous = A; |
2964 | } |
2965 | if (ShowNote) |
2966 | Diag(DiagID: clang::diag::note_drv_t_option_is_global); |
2967 | } |
2968 | |
2969 | // Warn -x after last input file has no effect |
2970 | { |
2971 | Arg *LastXArg = Args.getLastArgNoClaim(Ids: options::OPT_x); |
2972 | Arg *LastInputArg = Args.getLastArgNoClaim(Ids: options::OPT_INPUT); |
2973 | if (LastXArg && LastInputArg && |
2974 | LastInputArg->getIndex() < LastXArg->getIndex()) |
2975 | Diag(DiagID: clang::diag::warn_drv_unused_x) << LastXArg->getValue(); |
2976 | } |
2977 | |
2978 | for (Arg *A : Args) { |
2979 | if (A->getOption().getKind() == Option::InputClass) { |
2980 | const char *Value = A->getValue(); |
2981 | types::ID Ty = types::TY_INVALID; |
2982 | |
2983 | // Infer the input type if necessary. |
2984 | if (InputType == types::TY_Nothing) { |
2985 | // If there was an explicit arg for this, claim it. |
2986 | if (InputTypeArg) |
2987 | InputTypeArg->claim(); |
2988 | |
2989 | // stdin must be handled specially. |
2990 | if (memcmp(s1: Value, s2: "-" , n: 2) == 0) { |
2991 | if (IsFlangMode()) { |
2992 | Ty = types::TY_Fortran; |
2993 | } else if (IsDXCMode()) { |
2994 | Ty = types::TY_HLSL; |
2995 | } else { |
2996 | // If running with -E, treat as a C input (this changes the |
2997 | // builtin macros, for example). This may be overridden by -ObjC |
2998 | // below. |
2999 | // |
3000 | // Otherwise emit an error but still use a valid type to avoid |
3001 | // spurious errors (e.g., no inputs). |
3002 | assert(!CCGenDiagnostics && "stdin produces no crash reproducer" ); |
3003 | if (!Args.hasArgNoClaim(Ids: options::OPT_E) && !CCCIsCPP()) |
3004 | Diag(DiagID: IsCLMode() ? clang::diag::err_drv_unknown_stdin_type_clang_cl |
3005 | : clang::diag::err_drv_unknown_stdin_type); |
3006 | Ty = types::TY_C; |
3007 | } |
3008 | } else { |
3009 | // Otherwise lookup by extension. |
3010 | // Fallback is C if invoked as C preprocessor, C++ if invoked with |
3011 | // clang-cl /E, or Object otherwise. |
3012 | // We use a host hook here because Darwin at least has its own |
3013 | // idea of what .s is. |
3014 | if (const char *Ext = strrchr(s: Value, c: '.')) |
3015 | Ty = TC.LookupTypeForExtension(Ext: Ext + 1); |
3016 | |
3017 | if (Ty == types::TY_INVALID) { |
3018 | if (IsCLMode() && (Args.hasArgNoClaim(Ids: options::OPT_E) || CCGenDiagnostics)) |
3019 | Ty = types::TY_CXX; |
3020 | else if (CCCIsCPP() || CCGenDiagnostics) |
3021 | Ty = types::TY_C; |
3022 | else if (IsDXCMode()) |
3023 | Ty = types::TY_HLSL; |
3024 | else |
3025 | Ty = types::TY_Object; |
3026 | } |
3027 | |
3028 | // If the driver is invoked as C++ compiler (like clang++ or c++) it |
3029 | // should autodetect some input files as C++ for g++ compatibility. |
3030 | if (CCCIsCXX()) { |
3031 | types::ID OldTy = Ty; |
3032 | Ty = types::lookupCXXTypeForCType(Id: Ty); |
3033 | |
3034 | // Do not complain about foo.h, when we are known to be processing |
3035 | // it as a C++20 header unit. |
3036 | if (Ty != OldTy && !(OldTy == types::TY_CHeader && hasHeaderMode())) |
3037 | Diag(DiagID: clang::diag::warn_drv_treating_input_as_cxx) |
3038 | << getTypeName(Id: OldTy) << getTypeName(Id: Ty); |
3039 | } |
3040 | |
3041 | // If running with -fthinlto-index=, extensions that normally identify |
3042 | // native object files actually identify LLVM bitcode files. |
3043 | if (Args.hasArgNoClaim(Ids: options::OPT_fthinlto_index_EQ) && |
3044 | Ty == types::TY_Object) |
3045 | Ty = types::TY_LLVM_BC; |
3046 | } |
3047 | |
3048 | // -ObjC and -ObjC++ override the default language, but only for "source |
3049 | // files". We just treat everything that isn't a linker input as a |
3050 | // source file. |
3051 | // |
3052 | // FIXME: Clean this up if we move the phase sequence into the type. |
3053 | if (Ty != types::TY_Object) { |
3054 | if (Args.hasArg(Ids: options::OPT_ObjC)) |
3055 | Ty = types::TY_ObjC; |
3056 | else if (Args.hasArg(Ids: options::OPT_ObjCXX)) |
3057 | Ty = types::TY_ObjCXX; |
3058 | } |
3059 | |
3060 | // Disambiguate headers that are meant to be header units from those |
3061 | // intended to be PCH. Avoid missing '.h' cases that are counted as |
3062 | // C headers by default - we know we are in C++ mode and we do not |
3063 | // want to issue a complaint about compiling things in the wrong mode. |
3064 | if ((Ty == types::TY_CXXHeader || Ty == types::TY_CHeader) && |
3065 | hasHeaderMode()) |
3066 | Ty = CXXHeaderUnitType(HM: CXX20HeaderType); |
3067 | } else { |
3068 | assert(InputTypeArg && "InputType set w/o InputTypeArg" ); |
3069 | if (!InputTypeArg->getOption().matches(ID: options::OPT_x)) { |
3070 | // If emulating cl.exe, make sure that /TC and /TP don't affect input |
3071 | // object files. |
3072 | const char *Ext = strrchr(s: Value, c: '.'); |
3073 | if (Ext && TC.LookupTypeForExtension(Ext: Ext + 1) == types::TY_Object) |
3074 | Ty = types::TY_Object; |
3075 | } |
3076 | if (Ty == types::TY_INVALID) { |
3077 | Ty = InputType; |
3078 | InputTypeArg->claim(); |
3079 | } |
3080 | } |
3081 | |
3082 | if ((Ty == types::TY_C || Ty == types::TY_CXX) && |
3083 | Args.hasArgNoClaim(Ids: options::OPT_hipstdpar)) |
3084 | Ty = types::TY_HIP; |
3085 | |
3086 | if (DiagnoseInputExistence(Args, Value, Ty, /*TypoCorrect=*/true)) |
3087 | Inputs.push_back(Elt: std::make_pair(x&: Ty, y&: A)); |
3088 | |
3089 | } else if (A->getOption().matches(ID: options::OPT__SLASH_Tc)) { |
3090 | StringRef Value = A->getValue(); |
3091 | if (DiagnoseInputExistence(Args, Value, Ty: types::TY_C, |
3092 | /*TypoCorrect=*/false)) { |
3093 | Arg *InputArg = MakeInputArg(Args, Opts, Value: A->getValue()); |
3094 | Inputs.push_back(Elt: std::make_pair(x: types::TY_C, y&: InputArg)); |
3095 | } |
3096 | A->claim(); |
3097 | } else if (A->getOption().matches(ID: options::OPT__SLASH_Tp)) { |
3098 | StringRef Value = A->getValue(); |
3099 | if (DiagnoseInputExistence(Args, Value, Ty: types::TY_CXX, |
3100 | /*TypoCorrect=*/false)) { |
3101 | Arg *InputArg = MakeInputArg(Args, Opts, Value: A->getValue()); |
3102 | Inputs.push_back(Elt: std::make_pair(x: types::TY_CXX, y&: InputArg)); |
3103 | } |
3104 | A->claim(); |
3105 | } else if (A->getOption().hasFlag(Val: options::LinkerInput)) { |
3106 | // Just treat as object type, we could make a special type for this if |
3107 | // necessary. |
3108 | Inputs.push_back(Elt: std::make_pair(x: types::TY_Object, y&: A)); |
3109 | |
3110 | } else if (A->getOption().matches(ID: options::OPT_x)) { |
3111 | InputTypeArg = A; |
3112 | InputType = types::lookupTypeForTypeSpecifier(Name: A->getValue()); |
3113 | A->claim(); |
3114 | |
3115 | // Follow gcc behavior and treat as linker input for invalid -x |
3116 | // options. Its not clear why we shouldn't just revert to unknown; but |
3117 | // this isn't very important, we might as well be bug compatible. |
3118 | if (!InputType) { |
3119 | Diag(DiagID: clang::diag::err_drv_unknown_language) << A->getValue(); |
3120 | InputType = types::TY_Object; |
3121 | } |
3122 | |
3123 | // If the user has put -fmodule-header{,=} then we treat C++ headers as |
3124 | // header unit inputs. So we 'promote' -xc++-header appropriately. |
3125 | if (InputType == types::TY_CXXHeader && hasHeaderMode()) |
3126 | InputType = CXXHeaderUnitType(HM: CXX20HeaderType); |
3127 | } else if (A->getOption().getID() == options::OPT_U) { |
3128 | assert(A->getNumValues() == 1 && "The /U option has one value." ); |
3129 | StringRef Val = A->getValue(N: 0); |
3130 | if (Val.find_first_of(Chars: "/\\" ) != StringRef::npos) { |
3131 | // Warn about e.g. "/Users/me/myfile.c". |
3132 | Diag(DiagID: diag::warn_slash_u_filename) << Val; |
3133 | Diag(DiagID: diag::note_use_dashdash); |
3134 | } |
3135 | } |
3136 | } |
3137 | if (CCCIsCPP() && Inputs.empty()) { |
3138 | // If called as standalone preprocessor, stdin is processed |
3139 | // if no other input is present. |
3140 | Arg *A = MakeInputArg(Args, Opts, Value: "-" ); |
3141 | Inputs.push_back(Elt: std::make_pair(x: types::TY_C, y&: A)); |
3142 | } |
3143 | } |
3144 | |
3145 | namespace { |
3146 | /// Provides a convenient interface for different programming models to generate |
3147 | /// the required device actions. |
3148 | class OffloadingActionBuilder final { |
3149 | /// Flag used to trace errors in the builder. |
3150 | bool IsValid = false; |
3151 | |
3152 | /// The compilation that is using this builder. |
3153 | Compilation &C; |
3154 | |
3155 | /// Map between an input argument and the offload kinds used to process it. |
3156 | std::map<const Arg *, unsigned> InputArgToOffloadKindMap; |
3157 | |
3158 | /// Map between a host action and its originating input argument. |
3159 | std::map<Action *, const Arg *> HostActionToInputArgMap; |
3160 | |
3161 | /// Builder interface. It doesn't build anything or keep any state. |
3162 | class DeviceActionBuilder { |
3163 | public: |
3164 | typedef const llvm::SmallVectorImpl<phases::ID> PhasesTy; |
3165 | |
3166 | enum ActionBuilderReturnCode { |
3167 | // The builder acted successfully on the current action. |
3168 | ABRT_Success, |
3169 | // The builder didn't have to act on the current action. |
3170 | ABRT_Inactive, |
3171 | // The builder was successful and requested the host action to not be |
3172 | // generated. |
3173 | ABRT_Ignore_Host, |
3174 | }; |
3175 | |
3176 | protected: |
3177 | /// Compilation associated with this builder. |
3178 | Compilation &C; |
3179 | |
3180 | /// Tool chains associated with this builder. The same programming |
3181 | /// model may have associated one or more tool chains. |
3182 | SmallVector<const ToolChain *, 2> ToolChains; |
3183 | |
3184 | /// The derived arguments associated with this builder. |
3185 | DerivedArgList &Args; |
3186 | |
3187 | /// The inputs associated with this builder. |
3188 | const Driver::InputList &Inputs; |
3189 | |
3190 | /// The associated offload kind. |
3191 | Action::OffloadKind AssociatedOffloadKind = Action::OFK_None; |
3192 | |
3193 | public: |
3194 | DeviceActionBuilder(Compilation &C, DerivedArgList &Args, |
3195 | const Driver::InputList &Inputs, |
3196 | Action::OffloadKind AssociatedOffloadKind) |
3197 | : C(C), Args(Args), Inputs(Inputs), |
3198 | AssociatedOffloadKind(AssociatedOffloadKind) {} |
3199 | virtual ~DeviceActionBuilder() {} |
3200 | |
3201 | /// Fill up the array \a DA with all the device dependences that should be |
3202 | /// added to the provided host action \a HostAction. By default it is |
3203 | /// inactive. |
3204 | virtual ActionBuilderReturnCode |
3205 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
3206 | phases::ID CurPhase, phases::ID FinalPhase, |
3207 | PhasesTy &Phases) { |
3208 | return ABRT_Inactive; |
3209 | } |
3210 | |
3211 | /// Update the state to include the provided host action \a HostAction as a |
3212 | /// dependency of the current device action. By default it is inactive. |
3213 | virtual ActionBuilderReturnCode addDeviceDependences(Action *HostAction) { |
3214 | return ABRT_Inactive; |
3215 | } |
3216 | |
3217 | /// Append top level actions generated by the builder. |
3218 | virtual void appendTopLevelActions(ActionList &AL) {} |
3219 | |
3220 | /// Append linker device actions generated by the builder. |
3221 | virtual void appendLinkDeviceActions(ActionList &AL) {} |
3222 | |
3223 | /// Append linker host action generated by the builder. |
3224 | virtual Action* appendLinkHostActions(ActionList &AL) { return nullptr; } |
3225 | |
3226 | /// Append linker actions generated by the builder. |
3227 | virtual void appendLinkDependences(OffloadAction::DeviceDependences &DA) {} |
3228 | |
3229 | /// Initialize the builder. Return true if any initialization errors are |
3230 | /// found. |
3231 | virtual bool initialize() { return false; } |
3232 | |
3233 | /// Return true if the builder can use bundling/unbundling. |
3234 | virtual bool canUseBundlerUnbundler() const { return false; } |
3235 | |
3236 | /// Return true if this builder is valid. We have a valid builder if we have |
3237 | /// associated device tool chains. |
3238 | bool isValid() { return !ToolChains.empty(); } |
3239 | |
3240 | /// Return the associated offload kind. |
3241 | Action::OffloadKind getAssociatedOffloadKind() { |
3242 | return AssociatedOffloadKind; |
3243 | } |
3244 | }; |
3245 | |
3246 | /// Base class for CUDA/HIP action builder. It injects device code in |
3247 | /// the host backend action. |
3248 | class CudaActionBuilderBase : public DeviceActionBuilder { |
3249 | protected: |
3250 | /// Flags to signal if the user requested host-only or device-only |
3251 | /// compilation. |
3252 | bool CompileHostOnly = false; |
3253 | bool CompileDeviceOnly = false; |
3254 | bool EmitLLVM = false; |
3255 | bool EmitAsm = false; |
3256 | |
3257 | /// ID to identify each device compilation. For CUDA it is simply the |
3258 | /// GPU arch string. For HIP it is either the GPU arch string or GPU |
3259 | /// arch string plus feature strings delimited by a plus sign, e.g. |
3260 | /// gfx906+xnack. |
3261 | struct TargetID { |
3262 | /// Target ID string which is persistent throughout the compilation. |
3263 | const char *ID; |
3264 | TargetID(OffloadArch Arch) { ID = OffloadArchToString(A: Arch); } |
3265 | TargetID(const char *ID) : ID(ID) {} |
3266 | operator const char *() { return ID; } |
3267 | operator StringRef() { return StringRef(ID); } |
3268 | }; |
3269 | /// List of GPU architectures to use in this compilation. |
3270 | SmallVector<TargetID, 4> GpuArchList; |
3271 | |
3272 | /// The CUDA actions for the current input. |
3273 | ActionList CudaDeviceActions; |
3274 | |
3275 | /// The CUDA fat binary if it was generated for the current input. |
3276 | Action *CudaFatBinary = nullptr; |
3277 | |
3278 | /// Flag that is set to true if this builder acted on the current input. |
3279 | bool IsActive = false; |
3280 | |
3281 | /// Flag for -fgpu-rdc. |
3282 | bool Relocatable = false; |
3283 | |
3284 | /// Default GPU architecture if there's no one specified. |
3285 | OffloadArch DefaultOffloadArch = OffloadArch::UNKNOWN; |
3286 | |
3287 | /// Compilation unit ID specified by option '-fuse-cuid=' or'-cuid='. |
3288 | const CUIDOptions &CUIDOpts; |
3289 | |
3290 | public: |
3291 | CudaActionBuilderBase(Compilation &C, DerivedArgList &Args, |
3292 | const Driver::InputList &Inputs, |
3293 | Action::OffloadKind OFKind) |
3294 | : DeviceActionBuilder(C, Args, Inputs, OFKind), |
3295 | CUIDOpts(C.getDriver().getCUIDOpts()) { |
3296 | |
3297 | CompileDeviceOnly = C.getDriver().offloadDeviceOnly(); |
3298 | Relocatable = Args.hasFlag(Pos: options::OPT_fgpu_rdc, |
3299 | Neg: options::OPT_fno_gpu_rdc, /*Default=*/false); |
3300 | } |
3301 | |
3302 | ActionBuilderReturnCode addDeviceDependences(Action *HostAction) override { |
3303 | // While generating code for CUDA, we only depend on the host input action |
3304 | // to trigger the creation of all the CUDA device actions. |
3305 | |
3306 | // If we are dealing with an input action, replicate it for each GPU |
3307 | // architecture. If we are in host-only mode we return 'success' so that |
3308 | // the host uses the CUDA offload kind. |
3309 | if (auto *IA = dyn_cast<InputAction>(Val: HostAction)) { |
3310 | assert(!GpuArchList.empty() && |
3311 | "We should have at least one GPU architecture." ); |
3312 | |
3313 | // If the host input is not CUDA or HIP, we don't need to bother about |
3314 | // this input. |
3315 | if (!(IA->getType() == types::TY_CUDA || |
3316 | IA->getType() == types::TY_HIP || |
3317 | IA->getType() == types::TY_PP_HIP)) { |
3318 | // The builder will ignore this input. |
3319 | IsActive = false; |
3320 | return ABRT_Inactive; |
3321 | } |
3322 | |
3323 | // Set the flag to true, so that the builder acts on the current input. |
3324 | IsActive = true; |
3325 | |
3326 | if (CUIDOpts.isEnabled()) |
3327 | IA->setId(CUIDOpts.getCUID(InputFile: IA->getInputArg().getValue(), Args)); |
3328 | |
3329 | if (CompileHostOnly) |
3330 | return ABRT_Success; |
3331 | |
3332 | // Replicate inputs for each GPU architecture. |
3333 | auto Ty = IA->getType() == types::TY_HIP ? types::TY_HIP_DEVICE |
3334 | : types::TY_CUDA_DEVICE; |
3335 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
3336 | CudaDeviceActions.push_back( |
3337 | Elt: C.MakeAction<InputAction>(Arg: IA->getInputArg(), Arg&: Ty, Arg: IA->getId())); |
3338 | } |
3339 | |
3340 | return ABRT_Success; |
3341 | } |
3342 | |
3343 | // If this is an unbundling action use it as is for each CUDA toolchain. |
3344 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: HostAction)) { |
3345 | |
3346 | // If -fgpu-rdc is disabled, should not unbundle since there is no |
3347 | // device code to link. |
3348 | if (UA->getType() == types::TY_Object && !Relocatable) |
3349 | return ABRT_Inactive; |
3350 | |
3351 | CudaDeviceActions.clear(); |
3352 | auto *IA = cast<InputAction>(Val: UA->getInputs().back()); |
3353 | std::string FileName = IA->getInputArg().getAsString(Args); |
3354 | // Check if the type of the file is the same as the action. Do not |
3355 | // unbundle it if it is not. Do not unbundle .so files, for example, |
3356 | // which are not object files. Files with extension ".lib" is classified |
3357 | // as TY_Object but they are actually archives, therefore should not be |
3358 | // unbundled here as objects. They will be handled at other places. |
3359 | const StringRef LibFileExt = ".lib" ; |
3360 | if (IA->getType() == types::TY_Object && |
3361 | (!llvm::sys::path::has_extension(path: FileName) || |
3362 | types::lookupTypeForExtension( |
3363 | Ext: llvm::sys::path::extension(path: FileName).drop_front()) != |
3364 | types::TY_Object || |
3365 | llvm::sys::path::extension(path: FileName) == LibFileExt)) |
3366 | return ABRT_Inactive; |
3367 | |
3368 | for (auto Arch : GpuArchList) { |
3369 | CudaDeviceActions.push_back(Elt: UA); |
3370 | UA->registerDependentActionInfo(TC: ToolChains[0], BoundArch: Arch, |
3371 | Kind: AssociatedOffloadKind); |
3372 | } |
3373 | IsActive = true; |
3374 | return ABRT_Success; |
3375 | } |
3376 | |
3377 | return IsActive ? ABRT_Success : ABRT_Inactive; |
3378 | } |
3379 | |
3380 | void appendTopLevelActions(ActionList &AL) override { |
3381 | // Utility to append actions to the top level list. |
3382 | auto AddTopLevel = [&](Action *A, TargetID TargetID) { |
3383 | OffloadAction::DeviceDependences Dep; |
3384 | Dep.add(A&: *A, TC: *ToolChains.front(), BoundArch: TargetID, OKind: AssociatedOffloadKind); |
3385 | AL.push_back(Elt: C.MakeAction<OffloadAction>(Arg&: Dep, Arg: A->getType())); |
3386 | }; |
3387 | |
3388 | // If we have a fat binary, add it to the list. |
3389 | if (CudaFatBinary) { |
3390 | AddTopLevel(CudaFatBinary, OffloadArch::UNUSED); |
3391 | CudaDeviceActions.clear(); |
3392 | CudaFatBinary = nullptr; |
3393 | return; |
3394 | } |
3395 | |
3396 | if (CudaDeviceActions.empty()) |
3397 | return; |
3398 | |
3399 | // If we have CUDA actions at this point, that's because we have a have |
3400 | // partial compilation, so we should have an action for each GPU |
3401 | // architecture. |
3402 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
3403 | "Expecting one action per GPU architecture." ); |
3404 | assert(ToolChains.size() == 1 && |
3405 | "Expecting to have a single CUDA toolchain." ); |
3406 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) |
3407 | AddTopLevel(CudaDeviceActions[I], GpuArchList[I]); |
3408 | |
3409 | CudaDeviceActions.clear(); |
3410 | } |
3411 | |
3412 | /// Get canonicalized offload arch option. \returns empty StringRef if the |
3413 | /// option is invalid. |
3414 | virtual StringRef getCanonicalOffloadArch(StringRef Arch) = 0; |
3415 | |
3416 | virtual std::optional<std::pair<llvm::StringRef, llvm::StringRef>> |
3417 | getConflictOffloadArchCombination(const std::set<StringRef> &GpuArchs) = 0; |
3418 | |
3419 | bool initialize() override { |
3420 | assert(AssociatedOffloadKind == Action::OFK_Cuda || |
3421 | AssociatedOffloadKind == Action::OFK_HIP); |
3422 | |
3423 | // We don't need to support CUDA. |
3424 | if (AssociatedOffloadKind == Action::OFK_Cuda && |
3425 | !C.hasOffloadToolChain<Action::OFK_Cuda>()) |
3426 | return false; |
3427 | |
3428 | // We don't need to support HIP. |
3429 | if (AssociatedOffloadKind == Action::OFK_HIP && |
3430 | !C.hasOffloadToolChain<Action::OFK_HIP>()) |
3431 | return false; |
3432 | |
3433 | const ToolChain *HostTC = C.getSingleOffloadToolChain<Action::OFK_Host>(); |
3434 | assert(HostTC && "No toolchain for host compilation." ); |
3435 | if (HostTC->getTriple().isNVPTX() || HostTC->getTriple().isAMDGCN()) { |
3436 | // We do not support targeting NVPTX/AMDGCN for host compilation. Throw |
3437 | // an error and abort pipeline construction early so we don't trip |
3438 | // asserts that assume device-side compilation. |
3439 | C.getDriver().Diag(DiagID: diag::err_drv_cuda_host_arch) |
3440 | << HostTC->getTriple().getArchName(); |
3441 | return true; |
3442 | } |
3443 | |
3444 | ToolChains.push_back( |
3445 | Elt: AssociatedOffloadKind == Action::OFK_Cuda |
3446 | ? C.getSingleOffloadToolChain<Action::OFK_Cuda>() |
3447 | : C.getSingleOffloadToolChain<Action::OFK_HIP>()); |
3448 | |
3449 | CompileHostOnly = C.getDriver().offloadHostOnly(); |
3450 | EmitLLVM = Args.getLastArg(Ids: options::OPT_emit_llvm); |
3451 | EmitAsm = Args.getLastArg(Ids: options::OPT_S); |
3452 | |
3453 | // --offload and --offload-arch options are mutually exclusive. |
3454 | if (Args.hasArgNoClaim(Ids: options::OPT_offload_EQ) && |
3455 | Args.hasArgNoClaim(Ids: options::OPT_offload_arch_EQ, |
3456 | Ids: options::OPT_no_offload_arch_EQ)) { |
3457 | C.getDriver().Diag(DiagID: diag::err_opt_not_valid_with_opt) << "--offload-arch" |
3458 | << "--offload" ; |
3459 | } |
3460 | |
3461 | // Collect all offload arch parameters, removing duplicates. |
3462 | std::set<StringRef> GpuArchs; |
3463 | bool Error = false; |
3464 | const ToolChain &TC = *ToolChains.front(); |
3465 | for (Arg *A : C.getArgsForToolChain(TC: &TC, /*BoundArch=*/"" , |
3466 | DeviceOffloadKind: AssociatedOffloadKind)) { |
3467 | if (!(A->getOption().matches(ID: options::OPT_offload_arch_EQ) || |
3468 | A->getOption().matches(ID: options::OPT_no_offload_arch_EQ))) |
3469 | continue; |
3470 | A->claim(); |
3471 | |
3472 | for (StringRef ArchStr : llvm::split(Str: A->getValue(), Separator: "," )) { |
3473 | if (A->getOption().matches(ID: options::OPT_no_offload_arch_EQ) && |
3474 | ArchStr == "all" ) { |
3475 | GpuArchs.clear(); |
3476 | } else if (ArchStr == "native" ) { |
3477 | auto GPUsOrErr = ToolChains.front()->getSystemGPUArchs(Args); |
3478 | if (!GPUsOrErr) { |
3479 | TC.getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch) |
3480 | << llvm::Triple::getArchTypeName(Kind: TC.getArch()) |
3481 | << llvm::toString(E: GPUsOrErr.takeError()) << "--offload-arch" ; |
3482 | continue; |
3483 | } |
3484 | |
3485 | for (auto GPU : *GPUsOrErr) { |
3486 | GpuArchs.insert(x: Args.MakeArgString(Str: GPU)); |
3487 | } |
3488 | } else { |
3489 | ArchStr = getCanonicalOffloadArch(Arch: ArchStr); |
3490 | if (ArchStr.empty()) { |
3491 | Error = true; |
3492 | } else if (A->getOption().matches(ID: options::OPT_offload_arch_EQ)) |
3493 | GpuArchs.insert(x: ArchStr); |
3494 | else if (A->getOption().matches(ID: options::OPT_no_offload_arch_EQ)) |
3495 | GpuArchs.erase(x: ArchStr); |
3496 | else |
3497 | llvm_unreachable("Unexpected option." ); |
3498 | } |
3499 | } |
3500 | } |
3501 | |
3502 | auto &&ConflictingArchs = getConflictOffloadArchCombination(GpuArchs); |
3503 | if (ConflictingArchs) { |
3504 | C.getDriver().Diag(DiagID: clang::diag::err_drv_bad_offload_arch_combo) |
3505 | << ConflictingArchs->first << ConflictingArchs->second; |
3506 | C.setContainsError(); |
3507 | return true; |
3508 | } |
3509 | |
3510 | // Collect list of GPUs remaining in the set. |
3511 | for (auto Arch : GpuArchs) |
3512 | GpuArchList.push_back(Elt: Arch.data()); |
3513 | |
3514 | // Default to sm_20 which is the lowest common denominator for |
3515 | // supported GPUs. sm_20 code should work correctly, if |
3516 | // suboptimally, on all newer GPUs. |
3517 | if (GpuArchList.empty()) { |
3518 | if (ToolChains.front()->getTriple().isSPIRV()) { |
3519 | if (ToolChains.front()->getTriple().getVendor() == llvm::Triple::AMD) |
3520 | GpuArchList.push_back(Elt: OffloadArch::AMDGCNSPIRV); |
3521 | else |
3522 | GpuArchList.push_back(Elt: OffloadArch::Generic); |
3523 | } else { |
3524 | GpuArchList.push_back(Elt: DefaultOffloadArch); |
3525 | } |
3526 | } |
3527 | |
3528 | return Error; |
3529 | } |
3530 | }; |
3531 | |
3532 | /// \brief CUDA action builder. It injects device code in the host backend |
3533 | /// action. |
3534 | class CudaActionBuilder final : public CudaActionBuilderBase { |
3535 | public: |
3536 | CudaActionBuilder(Compilation &C, DerivedArgList &Args, |
3537 | const Driver::InputList &Inputs) |
3538 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_Cuda) { |
3539 | DefaultOffloadArch = OffloadArch::CudaDefault; |
3540 | } |
3541 | |
3542 | StringRef getCanonicalOffloadArch(StringRef ArchStr) override { |
3543 | OffloadArch Arch = StringToOffloadArch(S: ArchStr); |
3544 | if (Arch == OffloadArch::UNKNOWN || !IsNVIDIAOffloadArch(A: Arch)) { |
3545 | C.getDriver().Diag(DiagID: clang::diag::err_drv_cuda_bad_gpu_arch) << ArchStr; |
3546 | return StringRef(); |
3547 | } |
3548 | return OffloadArchToString(A: Arch); |
3549 | } |
3550 | |
3551 | std::optional<std::pair<llvm::StringRef, llvm::StringRef>> |
3552 | getConflictOffloadArchCombination( |
3553 | const std::set<StringRef> &GpuArchs) override { |
3554 | return std::nullopt; |
3555 | } |
3556 | |
3557 | ActionBuilderReturnCode |
3558 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
3559 | phases::ID CurPhase, phases::ID FinalPhase, |
3560 | PhasesTy &Phases) override { |
3561 | if (!IsActive) |
3562 | return ABRT_Inactive; |
3563 | |
3564 | // If we don't have more CUDA actions, we don't have any dependences to |
3565 | // create for the host. |
3566 | if (CudaDeviceActions.empty()) |
3567 | return ABRT_Success; |
3568 | |
3569 | assert(CudaDeviceActions.size() == GpuArchList.size() && |
3570 | "Expecting one action per GPU architecture." ); |
3571 | assert(!CompileHostOnly && |
3572 | "Not expecting CUDA actions in host-only compilation." ); |
3573 | |
3574 | // If we are generating code for the device or we are in a backend phase, |
3575 | // we attempt to generate the fat binary. We compile each arch to ptx and |
3576 | // assemble to cubin, then feed the cubin *and* the ptx into a device |
3577 | // "link" action, which uses fatbinary to combine these cubins into one |
3578 | // fatbin. The fatbin is then an input to the host action if not in |
3579 | // device-only mode. |
3580 | if (CompileDeviceOnly || CurPhase == phases::Backend) { |
3581 | ActionList DeviceActions; |
3582 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
3583 | // Produce the device action from the current phase up to the assemble |
3584 | // phase. |
3585 | for (auto Ph : Phases) { |
3586 | // Skip the phases that were already dealt with. |
3587 | if (Ph < CurPhase) |
3588 | continue; |
3589 | // We have to be consistent with the host final phase. |
3590 | if (Ph > FinalPhase) |
3591 | break; |
3592 | |
3593 | CudaDeviceActions[I] = C.getDriver().ConstructPhaseAction( |
3594 | C, Args, Phase: Ph, Input: CudaDeviceActions[I], TargetDeviceOffloadKind: Action::OFK_Cuda); |
3595 | |
3596 | if (Ph == phases::Assemble) |
3597 | break; |
3598 | } |
3599 | |
3600 | // If we didn't reach the assemble phase, we can't generate the fat |
3601 | // binary. We don't need to generate the fat binary if we are not in |
3602 | // device-only mode. |
3603 | if (!isa<AssembleJobAction>(Val: CudaDeviceActions[I]) || |
3604 | CompileDeviceOnly) |
3605 | continue; |
3606 | |
3607 | Action *AssembleAction = CudaDeviceActions[I]; |
3608 | assert(AssembleAction->getType() == types::TY_Object); |
3609 | assert(AssembleAction->getInputs().size() == 1); |
3610 | |
3611 | Action *BackendAction = AssembleAction->getInputs()[0]; |
3612 | assert(BackendAction->getType() == types::TY_PP_Asm); |
3613 | |
3614 | for (auto &A : {AssembleAction, BackendAction}) { |
3615 | OffloadAction::DeviceDependences DDep; |
3616 | DDep.add(A&: *A, TC: *ToolChains.front(), BoundArch: GpuArchList[I], OKind: Action::OFK_Cuda); |
3617 | DeviceActions.push_back( |
3618 | Elt: C.MakeAction<OffloadAction>(Arg&: DDep, Arg: A->getType())); |
3619 | } |
3620 | } |
3621 | |
3622 | // We generate the fat binary if we have device input actions. |
3623 | if (!DeviceActions.empty()) { |
3624 | CudaFatBinary = |
3625 | C.MakeAction<LinkJobAction>(Arg&: DeviceActions, Arg: types::TY_CUDA_FATBIN); |
3626 | |
3627 | if (!CompileDeviceOnly) { |
3628 | DA.add(A&: *CudaFatBinary, TC: *ToolChains.front(), /*BoundArch=*/nullptr, |
3629 | OKind: Action::OFK_Cuda); |
3630 | // Clear the fat binary, it is already a dependence to an host |
3631 | // action. |
3632 | CudaFatBinary = nullptr; |
3633 | } |
3634 | |
3635 | // Remove the CUDA actions as they are already connected to an host |
3636 | // action or fat binary. |
3637 | CudaDeviceActions.clear(); |
3638 | } |
3639 | |
3640 | // We avoid creating host action in device-only mode. |
3641 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
3642 | } else if (CurPhase > phases::Backend) { |
3643 | // If we are past the backend phase and still have a device action, we |
3644 | // don't have to do anything as this action is already a device |
3645 | // top-level action. |
3646 | return ABRT_Success; |
3647 | } |
3648 | |
3649 | assert(CurPhase < phases::Backend && "Generating single CUDA " |
3650 | "instructions should only occur " |
3651 | "before the backend phase!" ); |
3652 | |
3653 | // By default, we produce an action for each device arch. |
3654 | for (Action *&A : CudaDeviceActions) |
3655 | A = C.getDriver().ConstructPhaseAction(C, Args, Phase: CurPhase, Input: A); |
3656 | |
3657 | return ABRT_Success; |
3658 | } |
3659 | }; |
3660 | /// \brief HIP action builder. It injects device code in the host backend |
3661 | /// action. |
3662 | class HIPActionBuilder final : public CudaActionBuilderBase { |
3663 | /// The linker inputs obtained for each device arch. |
3664 | SmallVector<ActionList, 8> DeviceLinkerInputs; |
3665 | // The default bundling behavior depends on the type of output, therefore |
3666 | // BundleOutput needs to be tri-value: None, true, or false. |
3667 | // Bundle code objects except --no-gpu-output is specified for device |
3668 | // only compilation. Bundle other type of output files only if |
3669 | // --gpu-bundle-output is specified for device only compilation. |
3670 | std::optional<bool> BundleOutput; |
3671 | std::optional<bool> EmitReloc; |
3672 | |
3673 | public: |
3674 | HIPActionBuilder(Compilation &C, DerivedArgList &Args, |
3675 | const Driver::InputList &Inputs) |
3676 | : CudaActionBuilderBase(C, Args, Inputs, Action::OFK_HIP) { |
3677 | |
3678 | DefaultOffloadArch = OffloadArch::HIPDefault; |
3679 | |
3680 | if (Args.hasArg(Ids: options::OPT_fhip_emit_relocatable, |
3681 | Ids: options::OPT_fno_hip_emit_relocatable)) { |
3682 | EmitReloc = Args.hasFlag(Pos: options::OPT_fhip_emit_relocatable, |
3683 | Neg: options::OPT_fno_hip_emit_relocatable, Default: false); |
3684 | |
3685 | if (*EmitReloc) { |
3686 | if (Relocatable) { |
3687 | C.getDriver().Diag(DiagID: diag::err_opt_not_valid_with_opt) |
3688 | << "-fhip-emit-relocatable" |
3689 | << "-fgpu-rdc" ; |
3690 | } |
3691 | |
3692 | if (!CompileDeviceOnly) { |
3693 | C.getDriver().Diag(DiagID: diag::err_opt_not_valid_without_opt) |
3694 | << "-fhip-emit-relocatable" |
3695 | << "--cuda-device-only" ; |
3696 | } |
3697 | } |
3698 | } |
3699 | |
3700 | if (Args.hasArg(Ids: options::OPT_gpu_bundle_output, |
3701 | Ids: options::OPT_no_gpu_bundle_output)) |
3702 | BundleOutput = Args.hasFlag(Pos: options::OPT_gpu_bundle_output, |
3703 | Neg: options::OPT_no_gpu_bundle_output, Default: true) && |
3704 | (!EmitReloc || !*EmitReloc); |
3705 | } |
3706 | |
3707 | bool canUseBundlerUnbundler() const override { return true; } |
3708 | |
3709 | StringRef getCanonicalOffloadArch(StringRef IdStr) override { |
3710 | llvm::StringMap<bool> Features; |
3711 | // getHIPOffloadTargetTriple() is known to return valid value as it has |
3712 | // been called successfully in the CreateOffloadingDeviceToolChains(). |
3713 | auto T = |
3714 | (IdStr == "amdgcnspirv" ) |
3715 | ? llvm::Triple("spirv64-amd-amdhsa" ) |
3716 | : *getHIPOffloadTargetTriple(D: C.getDriver(), Args: C.getInputArgs()); |
3717 | auto ArchStr = parseTargetID(T, OffloadArch: IdStr, FeatureMap: &Features); |
3718 | if (!ArchStr) { |
3719 | C.getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id) << IdStr; |
3720 | C.setContainsError(); |
3721 | return StringRef(); |
3722 | } |
3723 | auto CanId = getCanonicalTargetID(Processor: *ArchStr, Features); |
3724 | return Args.MakeArgStringRef(Str: CanId); |
3725 | }; |
3726 | |
3727 | std::optional<std::pair<llvm::StringRef, llvm::StringRef>> |
3728 | getConflictOffloadArchCombination( |
3729 | const std::set<StringRef> &GpuArchs) override { |
3730 | return getConflictTargetIDCombination(TargetIDs: GpuArchs); |
3731 | } |
3732 | |
3733 | ActionBuilderReturnCode |
3734 | getDeviceDependences(OffloadAction::DeviceDependences &DA, |
3735 | phases::ID CurPhase, phases::ID FinalPhase, |
3736 | PhasesTy &Phases) override { |
3737 | if (!IsActive) |
3738 | return ABRT_Inactive; |
3739 | |
3740 | // amdgcn does not support linking of object files, therefore we skip |
3741 | // backend and assemble phases to output LLVM IR. Except for generating |
3742 | // non-relocatable device code, where we generate fat binary for device |
3743 | // code and pass to host in Backend phase. |
3744 | if (CudaDeviceActions.empty()) |
3745 | return ABRT_Success; |
3746 | |
3747 | assert(((CurPhase == phases::Link && Relocatable) || |
3748 | CudaDeviceActions.size() == GpuArchList.size()) && |
3749 | "Expecting one action per GPU architecture." ); |
3750 | assert(!CompileHostOnly && |
3751 | "Not expecting HIP actions in host-only compilation." ); |
3752 | |
3753 | bool ShouldLink = !EmitReloc || !*EmitReloc; |
3754 | |
3755 | if (!Relocatable && CurPhase == phases::Backend && !EmitLLVM && |
3756 | !EmitAsm && ShouldLink) { |
3757 | // If we are in backend phase, we attempt to generate the fat binary. |
3758 | // We compile each arch to IR and use a link action to generate code |
3759 | // object containing ISA. Then we use a special "link" action to create |
3760 | // a fat binary containing all the code objects for different GPU's. |
3761 | // The fat binary is then an input to the host action. |
3762 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
3763 | if (C.getDriver().isUsingOffloadLTO()) { |
3764 | // When LTO is enabled, skip the backend and assemble phases and |
3765 | // use lld to link the bitcode. |
3766 | ActionList AL; |
3767 | AL.push_back(Elt: CudaDeviceActions[I]); |
3768 | // Create a link action to link device IR with device library |
3769 | // and generate ISA. |
3770 | CudaDeviceActions[I] = |
3771 | C.MakeAction<LinkJobAction>(Arg&: AL, Arg: types::TY_Image); |
3772 | } else { |
3773 | // When LTO is not enabled, we follow the conventional |
3774 | // compiler phases, including backend and assemble phases. |
3775 | ActionList AL; |
3776 | Action *BackendAction = nullptr; |
3777 | if (ToolChains.front()->getTriple().isSPIRV() || |
3778 | (ToolChains.front()->getTriple().isAMDGCN() && |
3779 | GpuArchList[I] == StringRef("amdgcnspirv" ))) { |
3780 | // Emit LLVM bitcode for SPIR-V targets. SPIR-V device tool chain |
3781 | // (HIPSPVToolChain or HIPAMDToolChain) runs post-link LLVM IR |
3782 | // passes. |
3783 | types::ID Output = Args.hasArg(Ids: options::OPT_S) |
3784 | ? types::TY_LLVM_IR |
3785 | : types::TY_LLVM_BC; |
3786 | BackendAction = |
3787 | C.MakeAction<BackendJobAction>(Arg&: CudaDeviceActions[I], Arg&: Output); |
3788 | } else |
3789 | BackendAction = C.getDriver().ConstructPhaseAction( |
3790 | C, Args, Phase: phases::Backend, Input: CudaDeviceActions[I], |
3791 | TargetDeviceOffloadKind: AssociatedOffloadKind); |
3792 | auto AssembleAction = C.getDriver().ConstructPhaseAction( |
3793 | C, Args, Phase: phases::Assemble, Input: BackendAction, |
3794 | TargetDeviceOffloadKind: AssociatedOffloadKind); |
3795 | AL.push_back(Elt: AssembleAction); |
3796 | // Create a link action to link device IR with device library |
3797 | // and generate ISA. |
3798 | CudaDeviceActions[I] = |
3799 | C.MakeAction<LinkJobAction>(Arg&: AL, Arg: types::TY_Image); |
3800 | } |
3801 | |
3802 | // OffloadingActionBuilder propagates device arch until an offload |
3803 | // action. Since the next action for creating fatbin does |
3804 | // not have device arch, whereas the above link action and its input |
3805 | // have device arch, an offload action is needed to stop the null |
3806 | // device arch of the next action being propagated to the above link |
3807 | // action. |
3808 | OffloadAction::DeviceDependences DDep; |
3809 | DDep.add(A&: *CudaDeviceActions[I], TC: *ToolChains.front(), BoundArch: GpuArchList[I], |
3810 | OKind: AssociatedOffloadKind); |
3811 | CudaDeviceActions[I] = C.MakeAction<OffloadAction>( |
3812 | Arg&: DDep, Arg: CudaDeviceActions[I]->getType()); |
3813 | } |
3814 | |
3815 | if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) { |
3816 | // Create HIP fat binary with a special "link" action. |
3817 | CudaFatBinary = C.MakeAction<LinkJobAction>(Arg&: CudaDeviceActions, |
3818 | Arg: types::TY_HIP_FATBIN); |
3819 | |
3820 | if (!CompileDeviceOnly) { |
3821 | DA.add(A&: *CudaFatBinary, TC: *ToolChains.front(), /*BoundArch=*/nullptr, |
3822 | OKind: AssociatedOffloadKind); |
3823 | // Clear the fat binary, it is already a dependence to an host |
3824 | // action. |
3825 | CudaFatBinary = nullptr; |
3826 | } |
3827 | |
3828 | // Remove the CUDA actions as they are already connected to an host |
3829 | // action or fat binary. |
3830 | CudaDeviceActions.clear(); |
3831 | } |
3832 | |
3833 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
3834 | } else if (CurPhase == phases::Link) { |
3835 | if (!ShouldLink) |
3836 | return ABRT_Success; |
3837 | // Save CudaDeviceActions to DeviceLinkerInputs for each GPU subarch. |
3838 | // This happens to each device action originated from each input file. |
3839 | // Later on, device actions in DeviceLinkerInputs are used to create |
3840 | // device link actions in appendLinkDependences and the created device |
3841 | // link actions are passed to the offload action as device dependence. |
3842 | DeviceLinkerInputs.resize(N: CudaDeviceActions.size()); |
3843 | auto LI = DeviceLinkerInputs.begin(); |
3844 | for (auto *A : CudaDeviceActions) { |
3845 | LI->push_back(Elt: A); |
3846 | ++LI; |
3847 | } |
3848 | |
3849 | // We will pass the device action as a host dependence, so we don't |
3850 | // need to do anything else with them. |
3851 | CudaDeviceActions.clear(); |
3852 | return CompileDeviceOnly ? ABRT_Ignore_Host : ABRT_Success; |
3853 | } |
3854 | |
3855 | // By default, we produce an action for each device arch. |
3856 | for (Action *&A : CudaDeviceActions) |
3857 | A = C.getDriver().ConstructPhaseAction(C, Args, Phase: CurPhase, Input: A, |
3858 | TargetDeviceOffloadKind: AssociatedOffloadKind); |
3859 | |
3860 | if (CompileDeviceOnly && CurPhase == FinalPhase && BundleOutput && |
3861 | *BundleOutput) { |
3862 | for (unsigned I = 0, E = GpuArchList.size(); I != E; ++I) { |
3863 | OffloadAction::DeviceDependences DDep; |
3864 | DDep.add(A&: *CudaDeviceActions[I], TC: *ToolChains.front(), BoundArch: GpuArchList[I], |
3865 | OKind: AssociatedOffloadKind); |
3866 | CudaDeviceActions[I] = C.MakeAction<OffloadAction>( |
3867 | Arg&: DDep, Arg: CudaDeviceActions[I]->getType()); |
3868 | } |
3869 | CudaFatBinary = |
3870 | C.MakeAction<OffloadBundlingJobAction>(Arg&: CudaDeviceActions); |
3871 | CudaDeviceActions.clear(); |
3872 | } |
3873 | |
3874 | return (CompileDeviceOnly && |
3875 | (CurPhase == FinalPhase || |
3876 | (!ShouldLink && CurPhase == phases::Assemble))) |
3877 | ? ABRT_Ignore_Host |
3878 | : ABRT_Success; |
3879 | } |
3880 | |
3881 | void appendLinkDeviceActions(ActionList &AL) override { |
3882 | if (DeviceLinkerInputs.size() == 0) |
3883 | return; |
3884 | |
3885 | assert(DeviceLinkerInputs.size() == GpuArchList.size() && |
3886 | "Linker inputs and GPU arch list sizes do not match." ); |
3887 | |
3888 | ActionList Actions; |
3889 | unsigned I = 0; |
3890 | // Append a new link action for each device. |
3891 | // Each entry in DeviceLinkerInputs corresponds to a GPU arch. |
3892 | for (auto &LI : DeviceLinkerInputs) { |
3893 | |
3894 | types::ID Output = Args.hasArg(Ids: options::OPT_emit_llvm) |
3895 | ? types::TY_LLVM_BC |
3896 | : types::TY_Image; |
3897 | |
3898 | auto *DeviceLinkAction = C.MakeAction<LinkJobAction>(Arg&: LI, Arg&: Output); |
3899 | // Linking all inputs for the current GPU arch. |
3900 | // LI contains all the inputs for the linker. |
3901 | OffloadAction::DeviceDependences DeviceLinkDeps; |
3902 | DeviceLinkDeps.add(A&: *DeviceLinkAction, TC: *ToolChains[0], |
3903 | BoundArch: GpuArchList[I], OKind: AssociatedOffloadKind); |
3904 | Actions.push_back(Elt: C.MakeAction<OffloadAction>( |
3905 | Arg&: DeviceLinkDeps, Arg: DeviceLinkAction->getType())); |
3906 | ++I; |
3907 | } |
3908 | DeviceLinkerInputs.clear(); |
3909 | |
3910 | // If emitting LLVM, do not generate final host/device compilation action |
3911 | if (Args.hasArg(Ids: options::OPT_emit_llvm)) { |
3912 | AL.append(RHS: Actions); |
3913 | return; |
3914 | } |
3915 | |
3916 | // Create a host object from all the device images by embedding them |
3917 | // in a fat binary for mixed host-device compilation. For device-only |
3918 | // compilation, creates a fat binary. |
3919 | OffloadAction::DeviceDependences DDeps; |
3920 | if (!CompileDeviceOnly || !BundleOutput || *BundleOutput) { |
3921 | auto *TopDeviceLinkAction = C.MakeAction<LinkJobAction>( |
3922 | Arg&: Actions, |
3923 | Arg: CompileDeviceOnly ? types::TY_HIP_FATBIN : types::TY_Object); |
3924 | DDeps.add(A&: *TopDeviceLinkAction, TC: *ToolChains[0], BoundArch: nullptr, |
3925 | OKind: AssociatedOffloadKind); |
3926 | // Offload the host object to the host linker. |
3927 | AL.push_back( |
3928 | Elt: C.MakeAction<OffloadAction>(Arg&: DDeps, Arg: TopDeviceLinkAction->getType())); |
3929 | } else { |
3930 | AL.append(RHS: Actions); |
3931 | } |
3932 | } |
3933 | |
3934 | Action* appendLinkHostActions(ActionList &AL) override { return AL.back(); } |
3935 | |
3936 | void appendLinkDependences(OffloadAction::DeviceDependences &DA) override {} |
3937 | }; |
3938 | |
3939 | /// |
3940 | /// TODO: Add the implementation for other specialized builders here. |
3941 | /// |
3942 | |
3943 | /// Specialized builders being used by this offloading action builder. |
3944 | SmallVector<DeviceActionBuilder *, 4> SpecializedBuilders; |
3945 | |
3946 | /// Flag set to true if all valid builders allow file bundling/unbundling. |
3947 | bool CanUseBundler; |
3948 | |
3949 | public: |
3950 | OffloadingActionBuilder(Compilation &C, DerivedArgList &Args, |
3951 | const Driver::InputList &Inputs) |
3952 | : C(C) { |
3953 | // Create a specialized builder for each device toolchain. |
3954 | |
3955 | IsValid = true; |
3956 | |
3957 | // Create a specialized builder for CUDA. |
3958 | SpecializedBuilders.push_back(Elt: new CudaActionBuilder(C, Args, Inputs)); |
3959 | |
3960 | // Create a specialized builder for HIP. |
3961 | SpecializedBuilders.push_back(Elt: new HIPActionBuilder(C, Args, Inputs)); |
3962 | |
3963 | // |
3964 | // TODO: Build other specialized builders here. |
3965 | // |
3966 | |
3967 | // Initialize all the builders, keeping track of errors. If all valid |
3968 | // builders agree that we can use bundling, set the flag to true. |
3969 | unsigned ValidBuilders = 0u; |
3970 | unsigned = 0u; |
3971 | for (auto *SB : SpecializedBuilders) { |
3972 | IsValid = IsValid && !SB->initialize(); |
3973 | |
3974 | // Update the counters if the builder is valid. |
3975 | if (SB->isValid()) { |
3976 | ++ValidBuilders; |
3977 | if (SB->canUseBundlerUnbundler()) |
3978 | ++ValidBuildersSupportingBundling; |
3979 | } |
3980 | } |
3981 | CanUseBundler = |
3982 | ValidBuilders && ValidBuilders == ValidBuildersSupportingBundling; |
3983 | } |
3984 | |
3985 | ~OffloadingActionBuilder() { |
3986 | for (auto *SB : SpecializedBuilders) |
3987 | delete SB; |
3988 | } |
3989 | |
3990 | /// Record a host action and its originating input argument. |
3991 | void recordHostAction(Action *HostAction, const Arg *InputArg) { |
3992 | assert(HostAction && "Invalid host action" ); |
3993 | assert(InputArg && "Invalid input argument" ); |
3994 | auto Loc = HostActionToInputArgMap.try_emplace(k: HostAction, args&: InputArg).first; |
3995 | assert(Loc->second == InputArg && |
3996 | "host action mapped to multiple input arguments" ); |
3997 | (void)Loc; |
3998 | } |
3999 | |
4000 | /// Generate an action that adds device dependences (if any) to a host action. |
4001 | /// If no device dependence actions exist, just return the host action \a |
4002 | /// HostAction. If an error is found or if no builder requires the host action |
4003 | /// to be generated, return nullptr. |
4004 | Action * |
4005 | addDeviceDependencesToHostAction(Action *HostAction, const Arg *InputArg, |
4006 | phases::ID CurPhase, phases::ID FinalPhase, |
4007 | DeviceActionBuilder::PhasesTy &Phases) { |
4008 | if (!IsValid) |
4009 | return nullptr; |
4010 | |
4011 | if (SpecializedBuilders.empty()) |
4012 | return HostAction; |
4013 | |
4014 | assert(HostAction && "Invalid host action!" ); |
4015 | recordHostAction(HostAction, InputArg); |
4016 | |
4017 | OffloadAction::DeviceDependences DDeps; |
4018 | // Check if all the programming models agree we should not emit the host |
4019 | // action. Also, keep track of the offloading kinds employed. |
4020 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
4021 | unsigned InactiveBuilders = 0u; |
4022 | unsigned IgnoringBuilders = 0u; |
4023 | for (auto *SB : SpecializedBuilders) { |
4024 | if (!SB->isValid()) { |
4025 | ++InactiveBuilders; |
4026 | continue; |
4027 | } |
4028 | auto RetCode = |
4029 | SB->getDeviceDependences(DA&: DDeps, CurPhase, FinalPhase, Phases); |
4030 | |
4031 | // If the builder explicitly says the host action should be ignored, |
4032 | // we need to increment the variable that tracks the builders that request |
4033 | // the host object to be ignored. |
4034 | if (RetCode == DeviceActionBuilder::ABRT_Ignore_Host) |
4035 | ++IgnoringBuilders; |
4036 | |
4037 | // Unless the builder was inactive for this action, we have to record the |
4038 | // offload kind because the host will have to use it. |
4039 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
4040 | OffloadKind |= SB->getAssociatedOffloadKind(); |
4041 | } |
4042 | |
4043 | // If all builders agree that the host object should be ignored, just return |
4044 | // nullptr. |
4045 | if (IgnoringBuilders && |
4046 | SpecializedBuilders.size() == (InactiveBuilders + IgnoringBuilders)) |
4047 | return nullptr; |
4048 | |
4049 | if (DDeps.getActions().empty()) |
4050 | return HostAction; |
4051 | |
4052 | // We have dependences we need to bundle together. We use an offload action |
4053 | // for that. |
4054 | OffloadAction::HostDependence HDep( |
4055 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
4056 | /*BoundArch=*/nullptr, DDeps); |
4057 | return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDeps); |
4058 | } |
4059 | |
4060 | /// Generate an action that adds a host dependence to a device action. The |
4061 | /// results will be kept in this action builder. Return true if an error was |
4062 | /// found. |
4063 | bool addHostDependenceToDeviceActions(Action *&HostAction, |
4064 | const Arg *InputArg) { |
4065 | if (!IsValid) |
4066 | return true; |
4067 | |
4068 | recordHostAction(HostAction, InputArg); |
4069 | |
4070 | // If we are supporting bundling/unbundling and the current action is an |
4071 | // input action of non-source file, we replace the host action by the |
4072 | // unbundling action. The bundler tool has the logic to detect if an input |
4073 | // is a bundle or not and if the input is not a bundle it assumes it is a |
4074 | // host file. Therefore it is safe to create an unbundling action even if |
4075 | // the input is not a bundle. |
4076 | if (CanUseBundler && isa<InputAction>(Val: HostAction) && |
4077 | InputArg->getOption().getKind() == llvm::opt::Option::InputClass && |
4078 | (!types::isSrcFile(Id: HostAction->getType()) || |
4079 | HostAction->getType() == types::TY_PP_HIP)) { |
4080 | auto UnbundlingHostAction = |
4081 | C.MakeAction<OffloadUnbundlingJobAction>(Arg&: HostAction); |
4082 | UnbundlingHostAction->registerDependentActionInfo( |
4083 | TC: C.getSingleOffloadToolChain<Action::OFK_Host>(), |
4084 | /*BoundArch=*/StringRef(), Kind: Action::OFK_Host); |
4085 | HostAction = UnbundlingHostAction; |
4086 | recordHostAction(HostAction, InputArg); |
4087 | } |
4088 | |
4089 | assert(HostAction && "Invalid host action!" ); |
4090 | |
4091 | // Register the offload kinds that are used. |
4092 | auto &OffloadKind = InputArgToOffloadKindMap[InputArg]; |
4093 | for (auto *SB : SpecializedBuilders) { |
4094 | if (!SB->isValid()) |
4095 | continue; |
4096 | |
4097 | auto RetCode = SB->addDeviceDependences(HostAction); |
4098 | |
4099 | // Host dependences for device actions are not compatible with that same |
4100 | // action being ignored. |
4101 | assert(RetCode != DeviceActionBuilder::ABRT_Ignore_Host && |
4102 | "Host dependence not expected to be ignored.!" ); |
4103 | |
4104 | // Unless the builder was inactive for this action, we have to record the |
4105 | // offload kind because the host will have to use it. |
4106 | if (RetCode != DeviceActionBuilder::ABRT_Inactive) |
4107 | OffloadKind |= SB->getAssociatedOffloadKind(); |
4108 | } |
4109 | |
4110 | // Do not use unbundler if the Host does not depend on device action. |
4111 | if (OffloadKind == Action::OFK_None && CanUseBundler) |
4112 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: HostAction)) |
4113 | HostAction = UA->getInputs().back(); |
4114 | |
4115 | return false; |
4116 | } |
4117 | |
4118 | /// Add the offloading top level actions to the provided action list. This |
4119 | /// function can replace the host action by a bundling action if the |
4120 | /// programming models allow it. |
4121 | bool appendTopLevelActions(ActionList &AL, Action *HostAction, |
4122 | const Arg *InputArg) { |
4123 | if (HostAction) |
4124 | recordHostAction(HostAction, InputArg); |
4125 | |
4126 | // Get the device actions to be appended. |
4127 | ActionList OffloadAL; |
4128 | for (auto *SB : SpecializedBuilders) { |
4129 | if (!SB->isValid()) |
4130 | continue; |
4131 | SB->appendTopLevelActions(AL&: OffloadAL); |
4132 | } |
4133 | |
4134 | // If we can use the bundler, replace the host action by the bundling one in |
4135 | // the resulting list. Otherwise, just append the device actions. For |
4136 | // device only compilation, HostAction is a null pointer, therefore only do |
4137 | // this when HostAction is not a null pointer. |
4138 | if (CanUseBundler && HostAction && |
4139 | HostAction->getType() != types::TY_Nothing && !OffloadAL.empty()) { |
4140 | // Add the host action to the list in order to create the bundling action. |
4141 | OffloadAL.push_back(Elt: HostAction); |
4142 | |
4143 | // We expect that the host action was just appended to the action list |
4144 | // before this method was called. |
4145 | assert(HostAction == AL.back() && "Host action not in the list??" ); |
4146 | HostAction = C.MakeAction<OffloadBundlingJobAction>(Arg&: OffloadAL); |
4147 | recordHostAction(HostAction, InputArg); |
4148 | AL.back() = HostAction; |
4149 | } else |
4150 | AL.append(in_start: OffloadAL.begin(), in_end: OffloadAL.end()); |
4151 | |
4152 | // Propagate to the current host action (if any) the offload information |
4153 | // associated with the current input. |
4154 | if (HostAction) |
4155 | HostAction->propagateHostOffloadInfo(OKinds: InputArgToOffloadKindMap[InputArg], |
4156 | /*BoundArch=*/OArch: nullptr); |
4157 | return false; |
4158 | } |
4159 | |
4160 | void appendDeviceLinkActions(ActionList &AL) { |
4161 | for (DeviceActionBuilder *SB : SpecializedBuilders) { |
4162 | if (!SB->isValid()) |
4163 | continue; |
4164 | SB->appendLinkDeviceActions(AL); |
4165 | } |
4166 | } |
4167 | |
4168 | Action *makeHostLinkAction() { |
4169 | // Build a list of device linking actions. |
4170 | ActionList DeviceAL; |
4171 | appendDeviceLinkActions(AL&: DeviceAL); |
4172 | if (DeviceAL.empty()) |
4173 | return nullptr; |
4174 | |
4175 | // Let builders add host linking actions. |
4176 | Action* HA = nullptr; |
4177 | for (DeviceActionBuilder *SB : SpecializedBuilders) { |
4178 | if (!SB->isValid()) |
4179 | continue; |
4180 | HA = SB->appendLinkHostActions(AL&: DeviceAL); |
4181 | // This created host action has no originating input argument, therefore |
4182 | // needs to set its offloading kind directly. |
4183 | if (HA) |
4184 | HA->propagateHostOffloadInfo(OKinds: SB->getAssociatedOffloadKind(), |
4185 | /*BoundArch=*/OArch: nullptr); |
4186 | } |
4187 | return HA; |
4188 | } |
4189 | |
4190 | /// Processes the host linker action. This currently consists of replacing it |
4191 | /// with an offload action if there are device link objects and propagate to |
4192 | /// the host action all the offload kinds used in the current compilation. The |
4193 | /// resulting action is returned. |
4194 | Action *processHostLinkAction(Action *HostAction) { |
4195 | // Add all the dependences from the device linking actions. |
4196 | OffloadAction::DeviceDependences DDeps; |
4197 | for (auto *SB : SpecializedBuilders) { |
4198 | if (!SB->isValid()) |
4199 | continue; |
4200 | |
4201 | SB->appendLinkDependences(DA&: DDeps); |
4202 | } |
4203 | |
4204 | // Calculate all the offload kinds used in the current compilation. |
4205 | unsigned ActiveOffloadKinds = 0u; |
4206 | for (auto &I : InputArgToOffloadKindMap) |
4207 | ActiveOffloadKinds |= I.second; |
4208 | |
4209 | // If we don't have device dependencies, we don't have to create an offload |
4210 | // action. |
4211 | if (DDeps.getActions().empty()) { |
4212 | // Set all the active offloading kinds to the link action. Given that it |
4213 | // is a link action it is assumed to depend on all actions generated so |
4214 | // far. |
4215 | HostAction->setHostOffloadInfo(OKinds: ActiveOffloadKinds, |
4216 | /*BoundArch=*/OArch: nullptr); |
4217 | // Propagate active offloading kinds for each input to the link action. |
4218 | // Each input may have different active offloading kind. |
4219 | for (auto *A : HostAction->inputs()) { |
4220 | auto ArgLoc = HostActionToInputArgMap.find(x: A); |
4221 | if (ArgLoc == HostActionToInputArgMap.end()) |
4222 | continue; |
4223 | auto OFKLoc = InputArgToOffloadKindMap.find(x: ArgLoc->second); |
4224 | if (OFKLoc == InputArgToOffloadKindMap.end()) |
4225 | continue; |
4226 | A->propagateHostOffloadInfo(OKinds: OFKLoc->second, /*BoundArch=*/OArch: nullptr); |
4227 | } |
4228 | return HostAction; |
4229 | } |
4230 | |
4231 | // Create the offload action with all dependences. When an offload action |
4232 | // is created the kinds are propagated to the host action, so we don't have |
4233 | // to do that explicitly here. |
4234 | OffloadAction::HostDependence HDep( |
4235 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
4236 | /*BoundArch*/ nullptr, ActiveOffloadKinds); |
4237 | return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDeps); |
4238 | } |
4239 | }; |
4240 | } // anonymous namespace. |
4241 | |
4242 | void Driver::handleArguments(Compilation &C, DerivedArgList &Args, |
4243 | const InputList &Inputs, |
4244 | ActionList &Actions) const { |
4245 | |
4246 | // Diagnose misuse of /Fo. |
4247 | if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_Fo)) { |
4248 | StringRef V = A->getValue(); |
4249 | if (Inputs.size() > 1 && !V.empty() && |
4250 | !llvm::sys::path::is_separator(value: V.back())) { |
4251 | // Check whether /Fo tries to name an output file for multiple inputs. |
4252 | Diag(DiagID: clang::diag::err_drv_out_file_argument_with_multiple_sources) |
4253 | << A->getSpelling() << V; |
4254 | Args.eraseArg(Id: options::OPT__SLASH_Fo); |
4255 | } |
4256 | } |
4257 | |
4258 | // Diagnose misuse of /Fa. |
4259 | if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_Fa)) { |
4260 | StringRef V = A->getValue(); |
4261 | if (Inputs.size() > 1 && !V.empty() && |
4262 | !llvm::sys::path::is_separator(value: V.back())) { |
4263 | // Check whether /Fa tries to name an asm file for multiple inputs. |
4264 | Diag(DiagID: clang::diag::err_drv_out_file_argument_with_multiple_sources) |
4265 | << A->getSpelling() << V; |
4266 | Args.eraseArg(Id: options::OPT__SLASH_Fa); |
4267 | } |
4268 | } |
4269 | |
4270 | // Diagnose misuse of /o. |
4271 | if (Arg *A = Args.getLastArg(Ids: options::OPT__SLASH_o)) { |
4272 | if (A->getValue()[0] == '\0') { |
4273 | // It has to have a value. |
4274 | Diag(DiagID: clang::diag::err_drv_missing_argument) << A->getSpelling() << 1; |
4275 | Args.eraseArg(Id: options::OPT__SLASH_o); |
4276 | } |
4277 | } |
4278 | |
4279 | // Ignore /Yc/Yu if both /Yc and /Yu passed but with different filenames. |
4280 | Arg *YcArg = Args.getLastArg(Ids: options::OPT__SLASH_Yc); |
4281 | Arg *YuArg = Args.getLastArg(Ids: options::OPT__SLASH_Yu); |
4282 | if (YcArg && YuArg && strcmp(s1: YcArg->getValue(), s2: YuArg->getValue()) != 0) { |
4283 | Diag(DiagID: clang::diag::warn_drv_ycyu_different_arg_clang_cl); |
4284 | Args.eraseArg(Id: options::OPT__SLASH_Yc); |
4285 | Args.eraseArg(Id: options::OPT__SLASH_Yu); |
4286 | YcArg = YuArg = nullptr; |
4287 | } |
4288 | if (YcArg && Inputs.size() > 1) { |
4289 | Diag(DiagID: clang::diag::warn_drv_yc_multiple_inputs_clang_cl); |
4290 | Args.eraseArg(Id: options::OPT__SLASH_Yc); |
4291 | YcArg = nullptr; |
4292 | } |
4293 | |
4294 | Arg *FinalPhaseArg; |
4295 | phases::ID FinalPhase = getFinalPhase(DAL: Args, FinalPhaseArg: &FinalPhaseArg); |
4296 | |
4297 | if (FinalPhase == phases::Link) { |
4298 | if (Args.hasArgNoClaim(Ids: options::OPT_hipstdpar)) { |
4299 | Args.AddFlagArg(BaseArg: nullptr, Opt: getOpts().getOption(Opt: options::OPT_hip_link)); |
4300 | Args.AddFlagArg(BaseArg: nullptr, |
4301 | Opt: getOpts().getOption(Opt: options::OPT_frtlib_add_rpath)); |
4302 | } |
4303 | // Emitting LLVM while linking disabled except in HIPAMD Toolchain |
4304 | if (Args.hasArg(Ids: options::OPT_emit_llvm) && !Args.hasArg(Ids: options::OPT_hip_link)) |
4305 | Diag(DiagID: clang::diag::err_drv_emit_llvm_link); |
4306 | if (C.getDefaultToolChain().getTriple().isWindowsMSVCEnvironment() && |
4307 | LTOMode != LTOK_None && |
4308 | !Args.getLastArgValue(Id: options::OPT_fuse_ld_EQ) |
4309 | .starts_with_insensitive(Prefix: "lld" )) |
4310 | Diag(DiagID: clang::diag::err_drv_lto_without_lld); |
4311 | |
4312 | // If -dumpdir is not specified, give a default prefix derived from the link |
4313 | // output filename. For example, `clang -g -gsplit-dwarf a.c -o x` passes |
4314 | // `-dumpdir x-` to cc1. If -o is unspecified, use |
4315 | // stem(getDefaultImageName()) (usually stem("a.out") = "a"). |
4316 | if (!Args.hasArg(Ids: options::OPT_dumpdir)) { |
4317 | Arg *FinalOutput = Args.getLastArg(Ids: options::OPT_o, Ids: options::OPT__SLASH_o); |
4318 | Arg *Arg = Args.MakeSeparateArg( |
4319 | BaseArg: nullptr, Opt: getOpts().getOption(Opt: options::OPT_dumpdir), |
4320 | Value: Args.MakeArgString( |
4321 | Str: (FinalOutput ? FinalOutput->getValue() |
4322 | : llvm::sys::path::stem(path: getDefaultImageName())) + |
4323 | "-" )); |
4324 | Arg->claim(); |
4325 | Args.append(A: Arg); |
4326 | } |
4327 | } |
4328 | |
4329 | if (FinalPhase == phases::Preprocess || Args.hasArg(Ids: options::OPT__SLASH_Y_)) { |
4330 | // If only preprocessing or /Y- is used, all pch handling is disabled. |
4331 | // Rather than check for it everywhere, just remove clang-cl pch-related |
4332 | // flags here. |
4333 | Args.eraseArg(Id: options::OPT__SLASH_Fp); |
4334 | Args.eraseArg(Id: options::OPT__SLASH_Yc); |
4335 | Args.eraseArg(Id: options::OPT__SLASH_Yu); |
4336 | YcArg = YuArg = nullptr; |
4337 | } |
4338 | |
4339 | if (Args.hasArg(Ids: options::OPT_include_pch) && |
4340 | Args.hasArg(Ids: options::OPT_ignore_pch)) { |
4341 | // If -ignore-pch is used, -include-pch is disabled. Since -emit-pch is |
4342 | // CC1option, it will not be added to command argments if -ignore-pch is |
4343 | // used. |
4344 | Args.eraseArg(Id: options::OPT_include_pch); |
4345 | } |
4346 | |
4347 | bool LinkOnly = phases::Link == FinalPhase && Inputs.size() > 0; |
4348 | for (auto &I : Inputs) { |
4349 | types::ID InputType = I.first; |
4350 | const Arg *InputArg = I.second; |
4351 | |
4352 | auto PL = types::getCompilationPhases(Id: InputType); |
4353 | |
4354 | phases::ID InitialPhase = PL[0]; |
4355 | LinkOnly = LinkOnly && phases::Link == InitialPhase && PL.size() == 1; |
4356 | |
4357 | // If the first step comes after the final phase we are doing as part of |
4358 | // this compilation, warn the user about it. |
4359 | if (InitialPhase > FinalPhase) { |
4360 | if (InputArg->isClaimed()) |
4361 | continue; |
4362 | |
4363 | // Claim here to avoid the more general unused warning. |
4364 | InputArg->claim(); |
4365 | |
4366 | // Suppress all unused style warnings with -Qunused-arguments |
4367 | if (Args.hasArg(Ids: options::OPT_Qunused_arguments)) |
4368 | continue; |
4369 | |
4370 | // Special case when final phase determined by binary name, rather than |
4371 | // by a command-line argument with a corresponding Arg. |
4372 | if (CCCIsCPP()) |
4373 | Diag(DiagID: clang::diag::warn_drv_input_file_unused_by_cpp) |
4374 | << InputArg->getAsString(Args) << getPhaseName(Id: InitialPhase); |
4375 | // Special case '-E' warning on a previously preprocessed file to make |
4376 | // more sense. |
4377 | else if (InitialPhase == phases::Compile && |
4378 | (Args.getLastArg(Ids: options::OPT__SLASH_EP, |
4379 | Ids: options::OPT__SLASH_P) || |
4380 | Args.getLastArg(Ids: options::OPT_E) || |
4381 | Args.getLastArg(Ids: options::OPT_M, Ids: options::OPT_MM)) && |
4382 | getPreprocessedType(Id: InputType) == types::TY_INVALID) |
4383 | Diag(DiagID: clang::diag::warn_drv_preprocessed_input_file_unused) |
4384 | << InputArg->getAsString(Args) << !!FinalPhaseArg |
4385 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "" ); |
4386 | else |
4387 | Diag(DiagID: clang::diag::warn_drv_input_file_unused) |
4388 | << InputArg->getAsString(Args) << getPhaseName(Id: InitialPhase) |
4389 | << !!FinalPhaseArg |
4390 | << (FinalPhaseArg ? FinalPhaseArg->getOption().getName() : "" ); |
4391 | continue; |
4392 | } |
4393 | |
4394 | if (YcArg) { |
4395 | // Add a separate precompile phase for the compile phase. |
4396 | if (FinalPhase >= phases::Compile) { |
4397 | const types::ID = lookupHeaderTypeForSourceType(Id: InputType); |
4398 | // Build the pipeline for the pch file. |
4399 | Action *ClangClPch = C.MakeAction<InputAction>(Arg: *InputArg, Arg: HeaderType); |
4400 | for (phases::ID Phase : types::getCompilationPhases(Id: HeaderType)) |
4401 | ClangClPch = ConstructPhaseAction(C, Args, Phase, Input: ClangClPch); |
4402 | assert(ClangClPch); |
4403 | Actions.push_back(Elt: ClangClPch); |
4404 | // The driver currently exits after the first failed command. This |
4405 | // relies on that behavior, to make sure if the pch generation fails, |
4406 | // the main compilation won't run. |
4407 | // FIXME: If the main compilation fails, the PCH generation should |
4408 | // probably not be considered successful either. |
4409 | } |
4410 | } |
4411 | } |
4412 | |
4413 | // Claim any options which are obviously only used for compilation. |
4414 | if (LinkOnly) { |
4415 | Args.ClaimAllArgs(Id0: options::OPT_CompileOnly_Group); |
4416 | Args.ClaimAllArgs(Id0: options::OPT_cl_compile_Group); |
4417 | } |
4418 | } |
4419 | |
4420 | void Driver::BuildActions(Compilation &C, DerivedArgList &Args, |
4421 | const InputList &Inputs, ActionList &Actions) const { |
4422 | llvm::PrettyStackTraceString CrashInfo("Building compilation actions" ); |
4423 | |
4424 | if (!SuppressMissingInputWarning && Inputs.empty()) { |
4425 | Diag(DiagID: clang::diag::err_drv_no_input_files); |
4426 | return; |
4427 | } |
4428 | |
4429 | handleArguments(C, Args, Inputs, Actions); |
4430 | |
4431 | bool UseNewOffloadingDriver = |
4432 | C.isOffloadingHostKind(Kind: Action::OFK_OpenMP) || |
4433 | C.isOffloadingHostKind(Kind: Action::OFK_SYCL) || |
4434 | Args.hasFlag(Pos: options::OPT_foffload_via_llvm, |
4435 | Neg: options::OPT_fno_offload_via_llvm, Default: false) || |
4436 | Args.hasFlag(Pos: options::OPT_offload_new_driver, |
4437 | Neg: options::OPT_no_offload_new_driver, |
4438 | Default: C.isOffloadingHostKind(Kind: Action::OFK_Cuda)); |
4439 | |
4440 | bool HIPNoRDC = |
4441 | C.isOffloadingHostKind(Kind: Action::OFK_HIP) && |
4442 | !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false); |
4443 | |
4444 | // Builder to be used to build offloading actions. |
4445 | std::unique_ptr<OffloadingActionBuilder> OffloadBuilder = |
4446 | !UseNewOffloadingDriver |
4447 | ? std::make_unique<OffloadingActionBuilder>(args&: C, args&: Args, args: Inputs) |
4448 | : nullptr; |
4449 | |
4450 | // Construct the actions to perform. |
4451 | ExtractAPIJobAction * = nullptr; |
4452 | ActionList LinkerInputs; |
4453 | ActionList MergerInputs; |
4454 | |
4455 | for (auto &I : Inputs) { |
4456 | types::ID InputType = I.first; |
4457 | const Arg *InputArg = I.second; |
4458 | |
4459 | auto PL = types::getCompilationPhases(Driver: *this, DAL&: Args, Id: InputType); |
4460 | if (PL.empty()) |
4461 | continue; |
4462 | |
4463 | auto FullPL = types::getCompilationPhases(Id: InputType); |
4464 | |
4465 | // Build the pipeline for this file. |
4466 | Action *Current = C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType); |
4467 | |
4468 | std::string CUID; |
4469 | if (CUIDOpts.isEnabled() && types::isSrcFile(Id: InputType)) { |
4470 | CUID = CUIDOpts.getCUID(InputFile: InputArg->getValue(), Args); |
4471 | cast<InputAction>(Val: Current)->setId(CUID); |
4472 | } |
4473 | |
4474 | // Use the current host action in any of the offloading actions, if |
4475 | // required. |
4476 | if (!UseNewOffloadingDriver) |
4477 | if (OffloadBuilder->addHostDependenceToDeviceActions(HostAction&: Current, InputArg)) |
4478 | break; |
4479 | |
4480 | for (phases::ID Phase : PL) { |
4481 | |
4482 | // Add any offload action the host action depends on. |
4483 | if (!UseNewOffloadingDriver) |
4484 | Current = OffloadBuilder->addDeviceDependencesToHostAction( |
4485 | HostAction: Current, InputArg, CurPhase: Phase, FinalPhase: PL.back(), Phases: FullPL); |
4486 | if (!Current) |
4487 | break; |
4488 | |
4489 | // Queue linker inputs. |
4490 | if (Phase == phases::Link) { |
4491 | assert(Phase == PL.back() && "linking must be final compilation step." ); |
4492 | // We don't need to generate additional link commands if emitting AMD |
4493 | // bitcode or compiling only for the offload device |
4494 | if (!(C.getInputArgs().hasArg(Ids: options::OPT_hip_link) && |
4495 | (C.getInputArgs().hasArg(Ids: options::OPT_emit_llvm))) && |
4496 | !offloadDeviceOnly()) |
4497 | LinkerInputs.push_back(Elt: Current); |
4498 | Current = nullptr; |
4499 | break; |
4500 | } |
4501 | |
4502 | // TODO: Consider removing this because the merged may not end up being |
4503 | // the final Phase in the pipeline. Perhaps the merged could just merge |
4504 | // and then pass an artifact of some sort to the Link Phase. |
4505 | // Queue merger inputs. |
4506 | if (Phase == phases::IfsMerge) { |
4507 | assert(Phase == PL.back() && "merging must be final compilation step." ); |
4508 | MergerInputs.push_back(Elt: Current); |
4509 | Current = nullptr; |
4510 | break; |
4511 | } |
4512 | |
4513 | if (Phase == phases::Precompile && ExtractAPIAction) { |
4514 | ExtractAPIAction->addHeaderInput(Input: Current); |
4515 | Current = nullptr; |
4516 | break; |
4517 | } |
4518 | |
4519 | // FIXME: Should we include any prior module file outputs as inputs of |
4520 | // later actions in the same command line? |
4521 | |
4522 | // Otherwise construct the appropriate action. |
4523 | Action *NewCurrent = ConstructPhaseAction(C, Args, Phase, Input: Current); |
4524 | |
4525 | // We didn't create a new action, so we will just move to the next phase. |
4526 | if (NewCurrent == Current) |
4527 | continue; |
4528 | |
4529 | if (auto *EAA = dyn_cast<ExtractAPIJobAction>(Val: NewCurrent)) |
4530 | ExtractAPIAction = EAA; |
4531 | |
4532 | Current = NewCurrent; |
4533 | |
4534 | // Try to build the offloading actions and add the result as a dependency |
4535 | // to the host. |
4536 | if (UseNewOffloadingDriver) |
4537 | Current = BuildOffloadingActions(C, Args, Input: I, CUID, HostAction: Current); |
4538 | // Use the current host action in any of the offloading actions, if |
4539 | // required. |
4540 | else if (OffloadBuilder->addHostDependenceToDeviceActions(HostAction&: Current, |
4541 | InputArg)) |
4542 | break; |
4543 | |
4544 | if (Current->getType() == types::TY_Nothing) |
4545 | break; |
4546 | } |
4547 | |
4548 | // If we ended with something, add to the output list. |
4549 | if (Current) |
4550 | Actions.push_back(Elt: Current); |
4551 | |
4552 | // Add any top level actions generated for offloading. |
4553 | if (!UseNewOffloadingDriver) |
4554 | OffloadBuilder->appendTopLevelActions(AL&: Actions, HostAction: Current, InputArg); |
4555 | else if (Current) |
4556 | Current->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(), |
4557 | /*BoundArch=*/OArch: nullptr); |
4558 | } |
4559 | |
4560 | // Add a link action if necessary. |
4561 | |
4562 | if (LinkerInputs.empty()) { |
4563 | Arg *FinalPhaseArg; |
4564 | if (getFinalPhase(DAL: Args, FinalPhaseArg: &FinalPhaseArg) == phases::Link) |
4565 | if (!UseNewOffloadingDriver) |
4566 | OffloadBuilder->appendDeviceLinkActions(AL&: Actions); |
4567 | } |
4568 | |
4569 | if (!LinkerInputs.empty()) { |
4570 | if (!UseNewOffloadingDriver) |
4571 | if (Action *Wrapper = OffloadBuilder->makeHostLinkAction()) |
4572 | LinkerInputs.push_back(Elt: Wrapper); |
4573 | Action *LA; |
4574 | // Check if this Linker Job should emit a static library. |
4575 | if (ShouldEmitStaticLibrary(Args)) { |
4576 | LA = C.MakeAction<StaticLibJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image); |
4577 | } else if ((UseNewOffloadingDriver && !HIPNoRDC) || |
4578 | Args.hasArg(Ids: options::OPT_offload_link)) { |
4579 | LA = C.MakeAction<LinkerWrapperJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image); |
4580 | LA->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(), |
4581 | /*BoundArch=*/OArch: nullptr); |
4582 | } else { |
4583 | LA = C.MakeAction<LinkJobAction>(Arg&: LinkerInputs, Arg: types::TY_Image); |
4584 | } |
4585 | if (!UseNewOffloadingDriver) |
4586 | LA = OffloadBuilder->processHostLinkAction(HostAction: LA); |
4587 | Actions.push_back(Elt: LA); |
4588 | } |
4589 | |
4590 | // Add an interface stubs merge action if necessary. |
4591 | if (!MergerInputs.empty()) |
4592 | Actions.push_back( |
4593 | Elt: C.MakeAction<IfsMergeJobAction>(Arg&: MergerInputs, Arg: types::TY_Image)); |
4594 | |
4595 | if (Args.hasArg(Ids: options::OPT_emit_interface_stubs)) { |
4596 | auto PhaseList = types::getCompilationPhases( |
4597 | Id: types::TY_IFS_CPP, |
4598 | LastPhase: Args.hasArg(Ids: options::OPT_c) ? phases::Compile : phases::IfsMerge); |
4599 | |
4600 | ActionList MergerInputs; |
4601 | |
4602 | for (auto &I : Inputs) { |
4603 | types::ID InputType = I.first; |
4604 | const Arg *InputArg = I.second; |
4605 | |
4606 | // Currently clang and the llvm assembler do not support generating symbol |
4607 | // stubs from assembly, so we skip the input on asm files. For ifs files |
4608 | // we rely on the normal pipeline setup in the pipeline setup code above. |
4609 | if (InputType == types::TY_IFS || InputType == types::TY_PP_Asm || |
4610 | InputType == types::TY_Asm) |
4611 | continue; |
4612 | |
4613 | Action *Current = C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType); |
4614 | |
4615 | for (auto Phase : PhaseList) { |
4616 | switch (Phase) { |
4617 | default: |
4618 | llvm_unreachable( |
4619 | "IFS Pipeline can only consist of Compile followed by IfsMerge." ); |
4620 | case phases::Compile: { |
4621 | // Only IfsMerge (llvm-ifs) can handle .o files by looking for ifs |
4622 | // files where the .o file is located. The compile action can not |
4623 | // handle this. |
4624 | if (InputType == types::TY_Object) |
4625 | break; |
4626 | |
4627 | Current = C.MakeAction<CompileJobAction>(Arg&: Current, Arg: types::TY_IFS_CPP); |
4628 | break; |
4629 | } |
4630 | case phases::IfsMerge: { |
4631 | assert(Phase == PhaseList.back() && |
4632 | "merging must be final compilation step." ); |
4633 | MergerInputs.push_back(Elt: Current); |
4634 | Current = nullptr; |
4635 | break; |
4636 | } |
4637 | } |
4638 | } |
4639 | |
4640 | // If we ended with something, add to the output list. |
4641 | if (Current) |
4642 | Actions.push_back(Elt: Current); |
4643 | } |
4644 | |
4645 | // Add an interface stubs merge action if necessary. |
4646 | if (!MergerInputs.empty()) |
4647 | Actions.push_back( |
4648 | Elt: C.MakeAction<IfsMergeJobAction>(Arg&: MergerInputs, Arg: types::TY_Image)); |
4649 | } |
4650 | |
4651 | for (auto Opt : {options::OPT_print_supported_cpus, |
4652 | options::OPT_print_supported_extensions, |
4653 | options::OPT_print_enabled_extensions}) { |
4654 | // If --print-supported-cpus, -mcpu=? or -mtune=? is specified, build a |
4655 | // custom Compile phase that prints out supported cpu models and quits. |
4656 | // |
4657 | // If either --print-supported-extensions or --print-enabled-extensions is |
4658 | // specified, call the corresponding helper function that prints out the |
4659 | // supported/enabled extensions and quits. |
4660 | if (Arg *A = Args.getLastArg(Ids: Opt)) { |
4661 | if (Opt == options::OPT_print_supported_extensions && |
4662 | !C.getDefaultToolChain().getTriple().isRISCV() && |
4663 | !C.getDefaultToolChain().getTriple().isAArch64() && |
4664 | !C.getDefaultToolChain().getTriple().isARM()) { |
4665 | C.getDriver().Diag(DiagID: diag::err_opt_not_valid_on_target) |
4666 | << "--print-supported-extensions" ; |
4667 | return; |
4668 | } |
4669 | if (Opt == options::OPT_print_enabled_extensions && |
4670 | !C.getDefaultToolChain().getTriple().isRISCV() && |
4671 | !C.getDefaultToolChain().getTriple().isAArch64()) { |
4672 | C.getDriver().Diag(DiagID: diag::err_opt_not_valid_on_target) |
4673 | << "--print-enabled-extensions" ; |
4674 | return; |
4675 | } |
4676 | |
4677 | // Use the -mcpu=? flag as the dummy input to cc1. |
4678 | Actions.clear(); |
4679 | Action *InputAc = C.MakeAction<InputAction>( |
4680 | Arg&: *A, Arg: IsFlangMode() ? types::TY_Fortran : types::TY_C); |
4681 | Actions.push_back( |
4682 | Elt: C.MakeAction<PrecompileJobAction>(Arg&: InputAc, Arg: types::TY_Nothing)); |
4683 | for (auto &I : Inputs) |
4684 | I.second->claim(); |
4685 | } |
4686 | } |
4687 | |
4688 | // Call validator for dxil when -Vd not in Args. |
4689 | if (C.getDefaultToolChain().getTriple().isDXIL()) { |
4690 | // Only add action when needValidation. |
4691 | const auto &TC = |
4692 | static_cast<const toolchains::HLSLToolChain &>(C.getDefaultToolChain()); |
4693 | if (TC.requiresValidation(Args)) { |
4694 | Action *LastAction = Actions.back(); |
4695 | Actions.push_back(Elt: C.MakeAction<BinaryAnalyzeJobAction>( |
4696 | Arg&: LastAction, Arg: types::TY_DX_CONTAINER)); |
4697 | } |
4698 | if (TC.requiresBinaryTranslation(Args)) { |
4699 | Action *LastAction = Actions.back(); |
4700 | // Metal shader converter runs on DXIL containers, which can either be |
4701 | // validated (in which case they are TY_DX_CONTAINER), or unvalidated |
4702 | // (TY_OBJECT). |
4703 | if (LastAction->getType() == types::TY_DX_CONTAINER || |
4704 | LastAction->getType() == types::TY_Object) |
4705 | Actions.push_back(Elt: C.MakeAction<BinaryTranslatorJobAction>( |
4706 | Arg&: LastAction, Arg: types::TY_DX_CONTAINER)); |
4707 | } |
4708 | } |
4709 | |
4710 | // Claim ignored clang-cl options. |
4711 | Args.ClaimAllArgs(Id0: options::OPT_cl_ignored_Group); |
4712 | } |
4713 | |
4714 | /// Returns the canonical name for the offloading architecture when using a HIP |
4715 | /// or CUDA architecture. |
4716 | static StringRef getCanonicalArchString(Compilation &C, |
4717 | const llvm::opt::DerivedArgList &Args, |
4718 | StringRef ArchStr, |
4719 | const llvm::Triple &Triple, |
4720 | bool SpecificToolchain) { |
4721 | // Lookup the CUDA / HIP architecture string. Only report an error if we were |
4722 | // expecting the triple to be only NVPTX / AMDGPU. |
4723 | OffloadArch Arch = |
4724 | StringToOffloadArch(S: getProcessorFromTargetID(T: Triple, OffloadArch: ArchStr)); |
4725 | if (Triple.isNVPTX() && |
4726 | (Arch == OffloadArch::UNKNOWN || !IsNVIDIAOffloadArch(A: Arch))) { |
4727 | if (SpecificToolchain) |
4728 | C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch) |
4729 | << "CUDA" << ArchStr; |
4730 | return StringRef(); |
4731 | } else if (Triple.isAMDGPU() && |
4732 | (Arch == OffloadArch::UNKNOWN || !IsAMDOffloadArch(A: Arch))) { |
4733 | if (SpecificToolchain) |
4734 | C.getDriver().Diag(DiagID: clang::diag::err_drv_offload_bad_gpu_arch) |
4735 | << "HIP" << ArchStr; |
4736 | return StringRef(); |
4737 | } |
4738 | |
4739 | if (IsNVIDIAOffloadArch(A: Arch)) |
4740 | return Args.MakeArgStringRef(Str: OffloadArchToString(A: Arch)); |
4741 | |
4742 | if (IsAMDOffloadArch(A: Arch)) { |
4743 | llvm::StringMap<bool> Features; |
4744 | std::optional<StringRef> Arch = parseTargetID(T: Triple, OffloadArch: ArchStr, FeatureMap: &Features); |
4745 | if (!Arch) { |
4746 | C.getDriver().Diag(DiagID: clang::diag::err_drv_bad_target_id) << ArchStr; |
4747 | return StringRef(); |
4748 | } |
4749 | return Args.MakeArgStringRef(Str: getCanonicalTargetID(Processor: *Arch, Features)); |
4750 | } |
4751 | |
4752 | // If the input isn't CUDA or HIP just return the architecture. |
4753 | return ArchStr; |
4754 | } |
4755 | |
4756 | /// Checks if the set offloading architectures does not conflict. Returns the |
4757 | /// incompatible pair if a conflict occurs. |
4758 | static std::optional<std::pair<llvm::StringRef, llvm::StringRef>> |
4759 | getConflictOffloadArchCombination(const llvm::DenseSet<StringRef> &Archs, |
4760 | llvm::Triple Triple) { |
4761 | if (!Triple.isAMDGPU()) |
4762 | return std::nullopt; |
4763 | |
4764 | std::set<StringRef> ArchSet; |
4765 | llvm::copy(Range: Archs, Out: std::inserter(x&: ArchSet, i: ArchSet.begin())); |
4766 | return getConflictTargetIDCombination(TargetIDs: ArchSet); |
4767 | } |
4768 | |
4769 | llvm::SmallVector<StringRef> |
4770 | Driver::getOffloadArchs(Compilation &C, const llvm::opt::DerivedArgList &Args, |
4771 | Action::OffloadKind Kind, const ToolChain *TC, |
4772 | bool SpecificToolchain) const { |
4773 | if (!TC) |
4774 | TC = &C.getDefaultToolChain(); |
4775 | |
4776 | // --offload and --offload-arch options are mutually exclusive. |
4777 | if (Args.hasArgNoClaim(Ids: options::OPT_offload_EQ) && |
4778 | Args.hasArgNoClaim(Ids: options::OPT_offload_arch_EQ, |
4779 | Ids: options::OPT_no_offload_arch_EQ)) { |
4780 | C.getDriver().Diag(DiagID: diag::err_opt_not_valid_with_opt) |
4781 | << "--offload" |
4782 | << (Args.hasArgNoClaim(Ids: options::OPT_offload_arch_EQ) |
4783 | ? "--offload-arch" |
4784 | : "--no-offload-arch" ); |
4785 | } |
4786 | |
4787 | llvm::DenseSet<StringRef> Archs; |
4788 | for (auto *Arg : C.getArgsForToolChain(TC, /*BoundArch=*/"" , DeviceOffloadKind: Kind)) { |
4789 | // Add or remove the seen architectures in order of appearance. If an |
4790 | // invalid architecture is given we simply exit. |
4791 | if (Arg->getOption().matches(ID: options::OPT_offload_arch_EQ)) { |
4792 | for (StringRef Arch : Arg->getValues()) { |
4793 | if (Arch == "native" || Arch.empty()) { |
4794 | auto GPUsOrErr = TC->getSystemGPUArchs(Args); |
4795 | if (!GPUsOrErr) { |
4796 | if (!SpecificToolchain) |
4797 | llvm::consumeError(Err: GPUsOrErr.takeError()); |
4798 | else |
4799 | TC->getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch) |
4800 | << llvm::Triple::getArchTypeName(Kind: TC->getArch()) |
4801 | << llvm::toString(E: GPUsOrErr.takeError()) << "--offload-arch" ; |
4802 | continue; |
4803 | } |
4804 | |
4805 | for (auto ArchStr : *GPUsOrErr) { |
4806 | StringRef CanonicalStr = |
4807 | getCanonicalArchString(C, Args, ArchStr: Args.MakeArgString(Str: ArchStr), |
4808 | Triple: TC->getTriple(), SpecificToolchain); |
4809 | if (!CanonicalStr.empty()) |
4810 | Archs.insert(V: CanonicalStr); |
4811 | else if (SpecificToolchain) |
4812 | return llvm::SmallVector<StringRef>(); |
4813 | } |
4814 | } else { |
4815 | StringRef CanonicalStr = getCanonicalArchString( |
4816 | C, Args, ArchStr: Arch, Triple: TC->getTriple(), SpecificToolchain); |
4817 | if (!CanonicalStr.empty()) |
4818 | Archs.insert(V: CanonicalStr); |
4819 | else if (SpecificToolchain) |
4820 | return llvm::SmallVector<StringRef>(); |
4821 | } |
4822 | } |
4823 | } else if (Arg->getOption().matches(ID: options::OPT_no_offload_arch_EQ)) { |
4824 | for (StringRef Arch : llvm::split(Str: Arg->getValue(), Separator: "," )) { |
4825 | if (Arch == "all" ) { |
4826 | Archs.clear(); |
4827 | } else { |
4828 | StringRef ArchStr = getCanonicalArchString( |
4829 | C, Args, ArchStr: Arch, Triple: TC->getTriple(), SpecificToolchain); |
4830 | Archs.erase(V: ArchStr); |
4831 | } |
4832 | } |
4833 | } |
4834 | } |
4835 | |
4836 | if (auto ConflictingArchs = |
4837 | getConflictOffloadArchCombination(Archs, Triple: TC->getTriple())) |
4838 | C.getDriver().Diag(DiagID: clang::diag::err_drv_bad_offload_arch_combo) |
4839 | << ConflictingArchs->first << ConflictingArchs->second; |
4840 | |
4841 | // Skip filling defaults if we're just querying what is availible. |
4842 | if (SpecificToolchain && Archs.empty()) { |
4843 | if (Kind == Action::OFK_Cuda) { |
4844 | Archs.insert(V: OffloadArchToString(A: OffloadArch::CudaDefault)); |
4845 | } else if (Kind == Action::OFK_HIP) { |
4846 | Archs.insert(V: OffloadArchToString(A: OffloadArch::HIPDefault)); |
4847 | } else if (Kind == Action::OFK_SYCL) { |
4848 | Archs.insert(V: StringRef()); |
4849 | } else if (Kind == Action::OFK_OpenMP) { |
4850 | // Accept legacy `-march` device arguments for OpenMP. |
4851 | if (auto *Arg = C.getArgsForToolChain(TC, /*BoundArch=*/"" , DeviceOffloadKind: Kind) |
4852 | .getLastArg(Ids: options::OPT_march_EQ)) { |
4853 | Archs.insert(V: Arg->getValue()); |
4854 | } else { |
4855 | auto ArchsOrErr = TC->getSystemGPUArchs(Args); |
4856 | if (!ArchsOrErr) { |
4857 | TC->getDriver().Diag(DiagID: diag::err_drv_undetermined_gpu_arch) |
4858 | << llvm::Triple::getArchTypeName(Kind: TC->getArch()) |
4859 | << llvm::toString(E: ArchsOrErr.takeError()) << "--offload-arch" ; |
4860 | } else if (!ArchsOrErr->empty()) { |
4861 | for (auto Arch : *ArchsOrErr) |
4862 | Archs.insert(V: Args.MakeArgStringRef(Str: Arch)); |
4863 | } else { |
4864 | Archs.insert(V: StringRef()); |
4865 | } |
4866 | } |
4867 | } |
4868 | } |
4869 | Args.ClaimAllArgs(Id0: options::OPT_offload_arch_EQ); |
4870 | Args.ClaimAllArgs(Id0: options::OPT_no_offload_arch_EQ); |
4871 | |
4872 | SmallVector<StringRef> Sorted(Archs.begin(), Archs.end()); |
4873 | llvm::sort(C&: Sorted); |
4874 | return Sorted; |
4875 | } |
4876 | |
4877 | Action *Driver::BuildOffloadingActions(Compilation &C, |
4878 | llvm::opt::DerivedArgList &Args, |
4879 | const InputTy &Input, StringRef CUID, |
4880 | Action *HostAction) const { |
4881 | // Don't build offloading actions if explicitly disabled or we do not have a |
4882 | // valid source input. |
4883 | if (offloadHostOnly() || !types::isSrcFile(Id: Input.first)) |
4884 | return HostAction; |
4885 | |
4886 | bool HIPNoRDC = |
4887 | C.isOffloadingHostKind(Kind: Action::OFK_HIP) && |
4888 | !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false); |
4889 | |
4890 | // For HIP non-rdc non-device-only compilation, create a linker wrapper |
4891 | // action for each host object to link, bundle and wrap device files in |
4892 | // it. |
4893 | if ((isa<AssembleJobAction>(Val: HostAction) || |
4894 | (isa<BackendJobAction>(Val: HostAction) && |
4895 | HostAction->getType() == types::TY_LTO_BC)) && |
4896 | HIPNoRDC && !offloadDeviceOnly()) { |
4897 | ActionList AL{HostAction}; |
4898 | HostAction = C.MakeAction<LinkerWrapperJobAction>(Arg&: AL, Arg: types::TY_Object); |
4899 | HostAction->propagateHostOffloadInfo(OKinds: C.getActiveOffloadKinds(), |
4900 | /*BoundArch=*/OArch: nullptr); |
4901 | return HostAction; |
4902 | } |
4903 | |
4904 | // Don't build offloading actions if we do not have a compile action. If |
4905 | // preprocessing only ignore embedding. |
4906 | if (!(isa<CompileJobAction>(Val: HostAction) || |
4907 | getFinalPhase(DAL: Args) == phases::Preprocess)) |
4908 | return HostAction; |
4909 | |
4910 | ActionList OffloadActions; |
4911 | OffloadAction::DeviceDependences DDeps; |
4912 | |
4913 | const Action::OffloadKind OffloadKinds[] = { |
4914 | Action::OFK_OpenMP, Action::OFK_Cuda, Action::OFK_HIP, Action::OFK_SYCL}; |
4915 | |
4916 | for (Action::OffloadKind Kind : OffloadKinds) { |
4917 | SmallVector<const ToolChain *, 2> ToolChains; |
4918 | ActionList DeviceActions; |
4919 | |
4920 | auto TCRange = C.getOffloadToolChains(Kind); |
4921 | for (auto TI = TCRange.first, TE = TCRange.second; TI != TE; ++TI) |
4922 | ToolChains.push_back(Elt: TI->second); |
4923 | |
4924 | if (ToolChains.empty()) |
4925 | continue; |
4926 | |
4927 | types::ID InputType = Input.first; |
4928 | const Arg *InputArg = Input.second; |
4929 | |
4930 | // The toolchain can be active for unsupported file types. |
4931 | if ((Kind == Action::OFK_Cuda && !types::isCuda(Id: InputType)) || |
4932 | (Kind == Action::OFK_HIP && !types::isHIP(Id: InputType))) |
4933 | continue; |
4934 | |
4935 | // Get the product of all bound architectures and toolchains. |
4936 | SmallVector<std::pair<const ToolChain *, StringRef>> TCAndArchs; |
4937 | for (const ToolChain *TC : ToolChains) { |
4938 | for (StringRef Arch : OffloadArchs.lookup(Val: TC)) { |
4939 | TCAndArchs.push_back(Elt: std::make_pair(x&: TC, y&: Arch)); |
4940 | DeviceActions.push_back( |
4941 | Elt: C.MakeAction<InputAction>(Arg: *InputArg, Arg&: InputType, Arg&: CUID)); |
4942 | } |
4943 | } |
4944 | |
4945 | if (DeviceActions.empty()) |
4946 | return HostAction; |
4947 | |
4948 | // FIXME: Do not collapse the host side for Darwin targets with SYCL offload |
4949 | // compilations. The toolchain is not properly initialized for the target. |
4950 | if (isa<CompileJobAction>(Val: HostAction) && Kind == Action::OFK_SYCL && |
4951 | HostAction->getType() != types::TY_Nothing && |
4952 | C.getSingleOffloadToolChain<Action::OFK_Host>() |
4953 | ->getTriple() |
4954 | .isOSDarwin()) |
4955 | HostAction->setCannotBeCollapsedWithNextDependentAction(); |
4956 | |
4957 | auto PL = types::getCompilationPhases(Driver: *this, DAL&: Args, Id: InputType); |
4958 | |
4959 | for (phases::ID Phase : PL) { |
4960 | if (Phase == phases::Link) { |
4961 | assert(Phase == PL.back() && "linking must be final compilation step." ); |
4962 | break; |
4963 | } |
4964 | |
4965 | // Assemble actions are not used for the SYCL device side. Both compile |
4966 | // and backend actions are used to generate IR and textual IR if needed. |
4967 | if (Kind == Action::OFK_SYCL && Phase == phases::Assemble) |
4968 | continue; |
4969 | |
4970 | auto TCAndArch = TCAndArchs.begin(); |
4971 | for (Action *&A : DeviceActions) { |
4972 | if (A->getType() == types::TY_Nothing) |
4973 | continue; |
4974 | |
4975 | // Propagate the ToolChain so we can use it in ConstructPhaseAction. |
4976 | A->propagateDeviceOffloadInfo(OKind: Kind, OArch: TCAndArch->second.data(), |
4977 | OToolChain: TCAndArch->first); |
4978 | A = ConstructPhaseAction(C, Args, Phase, Input: A, TargetDeviceOffloadKind: Kind); |
4979 | |
4980 | if (isa<CompileJobAction>(Val: A) && isa<CompileJobAction>(Val: HostAction) && |
4981 | Kind == Action::OFK_OpenMP && |
4982 | HostAction->getType() != types::TY_Nothing) { |
4983 | // OpenMP offloading has a dependency on the host compile action to |
4984 | // identify which declarations need to be emitted. This shouldn't be |
4985 | // collapsed with any other actions so we can use it in the device. |
4986 | HostAction->setCannotBeCollapsedWithNextDependentAction(); |
4987 | OffloadAction::HostDependence HDep( |
4988 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
4989 | TCAndArch->second.data(), Kind); |
4990 | OffloadAction::DeviceDependences DDep; |
4991 | DDep.add(A&: *A, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind); |
4992 | A = C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: DDep); |
4993 | } |
4994 | |
4995 | ++TCAndArch; |
4996 | } |
4997 | } |
4998 | |
4999 | // Compiling HIP in device-only non-RDC mode requires linking each action |
5000 | // individually. |
5001 | for (Action *&A : DeviceActions) { |
5002 | if ((A->getType() != types::TY_Object && |
5003 | A->getType() != types::TY_LTO_BC) || |
5004 | !HIPNoRDC || !offloadDeviceOnly()) |
5005 | continue; |
5006 | ActionList LinkerInput = {A}; |
5007 | A = C.MakeAction<LinkJobAction>(Arg&: LinkerInput, Arg: types::TY_Image); |
5008 | } |
5009 | |
5010 | auto TCAndArch = TCAndArchs.begin(); |
5011 | for (Action *A : DeviceActions) { |
5012 | DDeps.add(A&: *A, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind); |
5013 | OffloadAction::DeviceDependences DDep; |
5014 | DDep.add(A&: *A, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind); |
5015 | |
5016 | // Compiling CUDA in non-RDC mode uses the PTX output if available. |
5017 | for (Action *Input : A->getInputs()) |
5018 | if (Kind == Action::OFK_Cuda && A->getType() == types::TY_Object && |
5019 | !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, |
5020 | Default: false)) |
5021 | DDep.add(A&: *Input, TC: *TCAndArch->first, BoundArch: TCAndArch->second.data(), OKind: Kind); |
5022 | OffloadActions.push_back(Elt: C.MakeAction<OffloadAction>(Arg&: DDep, Arg: A->getType())); |
5023 | |
5024 | ++TCAndArch; |
5025 | } |
5026 | } |
5027 | |
5028 | // HIP code in device-only non-RDC mode will bundle the output if it invoked |
5029 | // the linker. |
5030 | bool ShouldBundleHIP = |
5031 | HIPNoRDC && offloadDeviceOnly() && |
5032 | Args.hasFlag(Pos: options::OPT_gpu_bundle_output, |
5033 | Neg: options::OPT_no_gpu_bundle_output, Default: true) && |
5034 | !llvm::any_of(Range&: OffloadActions, |
5035 | P: [](Action *A) { return A->getType() != types::TY_Image; }); |
5036 | |
5037 | // All kinds exit now in device-only mode except for non-RDC mode HIP. |
5038 | if (offloadDeviceOnly() && !ShouldBundleHIP) |
5039 | return C.MakeAction<OffloadAction>(Arg&: DDeps, Arg: types::TY_Nothing); |
5040 | |
5041 | if (OffloadActions.empty()) |
5042 | return HostAction; |
5043 | |
5044 | OffloadAction::DeviceDependences DDep; |
5045 | if (C.isOffloadingHostKind(Kind: Action::OFK_Cuda) && |
5046 | !Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, Default: false)) { |
5047 | // If we are not in RDC-mode we just emit the final CUDA fatbinary for |
5048 | // each translation unit without requiring any linking. |
5049 | Action *FatbinAction = |
5050 | C.MakeAction<LinkJobAction>(Arg&: OffloadActions, Arg: types::TY_CUDA_FATBIN); |
5051 | DDep.add(A&: *FatbinAction, TC: *C.getSingleOffloadToolChain<Action::OFK_Cuda>(), |
5052 | BoundArch: nullptr, OKind: Action::OFK_Cuda); |
5053 | } else if (HIPNoRDC && offloadDeviceOnly()) { |
5054 | // If we are in device-only non-RDC-mode we just emit the final HIP |
5055 | // fatbinary for each translation unit, linking each input individually. |
5056 | Action *FatbinAction = |
5057 | C.MakeAction<LinkJobAction>(Arg&: OffloadActions, Arg: types::TY_HIP_FATBIN); |
5058 | DDep.add(A&: *FatbinAction, TC: *C.getSingleOffloadToolChain<Action::OFK_HIP>(), |
5059 | BoundArch: nullptr, OKind: Action::OFK_HIP); |
5060 | } else { |
5061 | // Package all the offloading actions into a single output that can be |
5062 | // embedded in the host and linked. |
5063 | Action *PackagerAction = |
5064 | C.MakeAction<OffloadPackagerJobAction>(Arg&: OffloadActions, Arg: types::TY_Image); |
5065 | DDep.add(A&: *PackagerAction, TC: *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
5066 | BoundArch: nullptr, OffloadKindMask: C.getActiveOffloadKinds()); |
5067 | } |
5068 | |
5069 | // HIP wants '--offload-device-only' to create a fatbinary by default. |
5070 | if (offloadDeviceOnly()) |
5071 | return C.MakeAction<OffloadAction>(Arg&: DDep, Arg: types::TY_Nothing); |
5072 | |
5073 | // If we are unable to embed a single device output into the host, we need to |
5074 | // add each device output as a host dependency to ensure they are still built. |
5075 | bool SingleDeviceOutput = !llvm::any_of(Range&: OffloadActions, P: [](Action *A) { |
5076 | return A->getType() == types::TY_Nothing; |
5077 | }) && isa<CompileJobAction>(Val: HostAction); |
5078 | OffloadAction::HostDependence HDep( |
5079 | *HostAction, *C.getSingleOffloadToolChain<Action::OFK_Host>(), |
5080 | /*BoundArch=*/nullptr, SingleDeviceOutput ? DDep : DDeps); |
5081 | return C.MakeAction<OffloadAction>(Arg&: HDep, Arg&: SingleDeviceOutput ? DDep : DDeps); |
5082 | } |
5083 | |
5084 | Action *Driver::ConstructPhaseAction( |
5085 | Compilation &C, const ArgList &Args, phases::ID Phase, Action *Input, |
5086 | Action::OffloadKind TargetDeviceOffloadKind) const { |
5087 | llvm::PrettyStackTraceString CrashInfo("Constructing phase actions" ); |
5088 | |
5089 | // Some types skip the assembler phase (e.g., llvm-bc), but we can't |
5090 | // encode this in the steps because the intermediate type depends on |
5091 | // arguments. Just special case here. |
5092 | if (Phase == phases::Assemble && Input->getType() != types::TY_PP_Asm) |
5093 | return Input; |
5094 | |
5095 | // Use of --sycl-link will only allow for the link phase to occur. This is |
5096 | // for all input files. |
5097 | if (Args.hasArg(Ids: options::OPT_sycl_link) && Phase != phases::Link) |
5098 | return Input; |
5099 | |
5100 | // Build the appropriate action. |
5101 | switch (Phase) { |
5102 | case phases::Link: |
5103 | llvm_unreachable("link action invalid here." ); |
5104 | case phases::IfsMerge: |
5105 | llvm_unreachable("ifsmerge action invalid here." ); |
5106 | case phases::Preprocess: { |
5107 | types::ID OutputTy; |
5108 | // -M and -MM specify the dependency file name by altering the output type, |
5109 | // -if -MD and -MMD are not specified. |
5110 | if (Args.hasArg(Ids: options::OPT_M, Ids: options::OPT_MM) && |
5111 | !Args.hasArg(Ids: options::OPT_MD, Ids: options::OPT_MMD)) { |
5112 | OutputTy = types::TY_Dependencies; |
5113 | } else { |
5114 | OutputTy = Input->getType(); |
5115 | // For these cases, the preprocessor is only translating forms, the Output |
5116 | // still needs preprocessing. |
5117 | if (!Args.hasFlag(Pos: options::OPT_frewrite_includes, |
5118 | Neg: options::OPT_fno_rewrite_includes, Default: false) && |
5119 | !Args.hasFlag(Pos: options::OPT_frewrite_imports, |
5120 | Neg: options::OPT_fno_rewrite_imports, Default: false) && |
5121 | !Args.hasFlag(Pos: options::OPT_fdirectives_only, |
5122 | Neg: options::OPT_fno_directives_only, Default: false) && |
5123 | !CCGenDiagnostics) |
5124 | OutputTy = types::getPreprocessedType(Id: OutputTy); |
5125 | assert(OutputTy != types::TY_INVALID && |
5126 | "Cannot preprocess this input type!" ); |
5127 | } |
5128 | return C.MakeAction<PreprocessJobAction>(Arg&: Input, Arg&: OutputTy); |
5129 | } |
5130 | case phases::Precompile: { |
5131 | // API extraction should not generate an actual precompilation action. |
5132 | if (Args.hasArg(Ids: options::OPT_extract_api)) |
5133 | return C.MakeAction<ExtractAPIJobAction>(Arg&: Input, Arg: types::TY_API_INFO); |
5134 | |
5135 | // With 'fexperimental-modules-reduced-bmi', we don't want to run the |
5136 | // precompile phase unless the user specified '--precompile'. In the case |
5137 | // the '--precompile' flag is enabled, we will try to emit the reduced BMI |
5138 | // as a by product in GenerateModuleInterfaceAction. |
5139 | if (Args.hasArg(Ids: options::OPT_modules_reduced_bmi) && |
5140 | !Args.getLastArg(Ids: options::OPT__precompile)) |
5141 | return Input; |
5142 | |
5143 | types::ID OutputTy = getPrecompiledType(Id: Input->getType()); |
5144 | assert(OutputTy != types::TY_INVALID && |
5145 | "Cannot precompile this input type!" ); |
5146 | |
5147 | // If we're given a module name, precompile header file inputs as a |
5148 | // module, not as a precompiled header. |
5149 | const char *ModName = nullptr; |
5150 | if (OutputTy == types::TY_PCH) { |
5151 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fmodule_name_EQ)) |
5152 | ModName = A->getValue(); |
5153 | if (ModName) |
5154 | OutputTy = types::TY_ModuleFile; |
5155 | } |
5156 | |
5157 | if (Args.hasArg(Ids: options::OPT_fsyntax_only)) { |
5158 | // Syntax checks should not emit a PCH file |
5159 | OutputTy = types::TY_Nothing; |
5160 | } |
5161 | |
5162 | return C.MakeAction<PrecompileJobAction>(Arg&: Input, Arg&: OutputTy); |
5163 | } |
5164 | case phases::Compile: { |
5165 | if (Args.hasArg(Ids: options::OPT_fsyntax_only)) |
5166 | return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_Nothing); |
5167 | if (Args.hasArg(Ids: options::OPT_rewrite_objc)) |
5168 | return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_RewrittenObjC); |
5169 | if (Args.hasArg(Ids: options::OPT_rewrite_legacy_objc)) |
5170 | return C.MakeAction<CompileJobAction>(Arg&: Input, |
5171 | Arg: types::TY_RewrittenLegacyObjC); |
5172 | if (Args.hasArg(Ids: options::OPT__analyze)) |
5173 | return C.MakeAction<AnalyzeJobAction>(Arg&: Input, Arg: types::TY_Plist); |
5174 | if (Args.hasArg(Ids: options::OPT_emit_ast)) |
5175 | return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_AST); |
5176 | if (Args.hasArg(Ids: options::OPT_emit_cir)) |
5177 | return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_CIR); |
5178 | if (Args.hasArg(Ids: options::OPT_module_file_info)) |
5179 | return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_ModuleFile); |
5180 | if (Args.hasArg(Ids: options::OPT_verify_pch)) |
5181 | return C.MakeAction<VerifyPCHJobAction>(Arg&: Input, Arg: types::TY_Nothing); |
5182 | if (Args.hasArg(Ids: options::OPT_extract_api)) |
5183 | return C.MakeAction<ExtractAPIJobAction>(Arg&: Input, Arg: types::TY_API_INFO); |
5184 | return C.MakeAction<CompileJobAction>(Arg&: Input, Arg: types::TY_LLVM_BC); |
5185 | } |
5186 | case phases::Backend: { |
5187 | if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) { |
5188 | types::ID Output; |
5189 | if (Args.hasArg(Ids: options::OPT_ffat_lto_objects) && |
5190 | !Args.hasArg(Ids: options::OPT_emit_llvm)) |
5191 | Output = types::TY_PP_Asm; |
5192 | else if (Args.hasArg(Ids: options::OPT_S)) |
5193 | Output = types::TY_LTO_IR; |
5194 | else |
5195 | Output = types::TY_LTO_BC; |
5196 | return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output); |
5197 | } |
5198 | if (isUsingOffloadLTO() && TargetDeviceOffloadKind != Action::OFK_None) { |
5199 | types::ID Output = |
5200 | Args.hasArg(Ids: options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC; |
5201 | return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output); |
5202 | } |
5203 | if (Args.hasArg(Ids: options::OPT_emit_llvm) || |
5204 | TargetDeviceOffloadKind == Action::OFK_SYCL || |
5205 | (((Input->getOffloadingToolChain() && |
5206 | Input->getOffloadingToolChain()->getTriple().isAMDGPU()) || |
5207 | TargetDeviceOffloadKind == Action::OFK_HIP) && |
5208 | ((Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, |
5209 | Default: false) || |
5210 | (Args.hasFlag(Pos: options::OPT_offload_new_driver, |
5211 | Neg: options::OPT_no_offload_new_driver, Default: false) && |
5212 | !offloadDeviceOnly())) || |
5213 | TargetDeviceOffloadKind == Action::OFK_OpenMP))) { |
5214 | types::ID Output = |
5215 | Args.hasArg(Ids: options::OPT_S) && |
5216 | (TargetDeviceOffloadKind == Action::OFK_None || |
5217 | offloadDeviceOnly() || |
5218 | (TargetDeviceOffloadKind == Action::OFK_HIP && |
5219 | !Args.hasFlag(Pos: options::OPT_offload_new_driver, |
5220 | Neg: options::OPT_no_offload_new_driver, |
5221 | Default: C.isOffloadingHostKind(Kind: Action::OFK_Cuda)))) |
5222 | ? types::TY_LLVM_IR |
5223 | : types::TY_LLVM_BC; |
5224 | return C.MakeAction<BackendJobAction>(Arg&: Input, Arg&: Output); |
5225 | } |
5226 | return C.MakeAction<BackendJobAction>(Arg&: Input, Arg: types::TY_PP_Asm); |
5227 | } |
5228 | case phases::Assemble: |
5229 | return C.MakeAction<AssembleJobAction>(Arg: std::move(Input), Arg: types::TY_Object); |
5230 | } |
5231 | |
5232 | llvm_unreachable("invalid phase in ConstructPhaseAction" ); |
5233 | } |
5234 | |
5235 | void Driver::BuildJobs(Compilation &C) const { |
5236 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs" ); |
5237 | |
5238 | Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o); |
5239 | |
5240 | // It is an error to provide a -o option if we are making multiple output |
5241 | // files. There are exceptions: |
5242 | // |
5243 | // IfsMergeJob: when generating interface stubs enabled we want to be able to |
5244 | // generate the stub file at the same time that we generate the real |
5245 | // library/a.out. So when a .o, .so, etc are the output, with clang interface |
5246 | // stubs there will also be a .ifs and .ifso at the same location. |
5247 | // |
5248 | // CompileJob of type TY_IFS_CPP: when generating interface stubs is enabled |
5249 | // and -c is passed, we still want to be able to generate a .ifs file while |
5250 | // we are also generating .o files. So we allow more than one output file in |
5251 | // this case as well. |
5252 | // |
5253 | // OffloadClass of type TY_Nothing: device-only output will place many outputs |
5254 | // into a single offloading action. We should count all inputs to the action |
5255 | // as outputs. Also ignore device-only outputs if we're compiling with |
5256 | // -fsyntax-only. |
5257 | if (FinalOutput) { |
5258 | unsigned NumOutputs = 0; |
5259 | unsigned NumIfsOutputs = 0; |
5260 | for (const Action *A : C.getActions()) { |
5261 | // The actions below do not increase the number of outputs, when operating |
5262 | // on DX containers. |
5263 | if (A->getType() == types::TY_DX_CONTAINER && |
5264 | (A->getKind() == clang::driver::Action::BinaryAnalyzeJobClass || |
5265 | A->getKind() == clang::driver::Action::BinaryTranslatorJobClass)) |
5266 | continue; |
5267 | |
5268 | if (A->getType() != types::TY_Nothing && |
5269 | !(A->getKind() == Action::IfsMergeJobClass || |
5270 | (A->getType() == clang::driver::types::TY_IFS_CPP && |
5271 | A->getKind() == clang::driver::Action::CompileJobClass && |
5272 | 0 == NumIfsOutputs++) || |
5273 | (A->getKind() == Action::BindArchClass && A->getInputs().size() && |
5274 | A->getInputs().front()->getKind() == Action::IfsMergeJobClass))) |
5275 | ++NumOutputs; |
5276 | else if (A->getKind() == Action::OffloadClass && |
5277 | A->getType() == types::TY_Nothing && |
5278 | !C.getArgs().hasArg(Ids: options::OPT_fsyntax_only)) |
5279 | NumOutputs += A->size(); |
5280 | } |
5281 | |
5282 | if (NumOutputs > 1) { |
5283 | Diag(DiagID: clang::diag::err_drv_output_argument_with_multiple_files); |
5284 | FinalOutput = nullptr; |
5285 | } |
5286 | } |
5287 | |
5288 | const llvm::Triple &RawTriple = C.getDefaultToolChain().getTriple(); |
5289 | |
5290 | // Collect the list of architectures. |
5291 | llvm::StringSet<> ArchNames; |
5292 | if (RawTriple.isOSBinFormatMachO()) |
5293 | for (const Arg *A : C.getArgs()) |
5294 | if (A->getOption().matches(ID: options::OPT_arch)) |
5295 | ArchNames.insert(key: A->getValue()); |
5296 | |
5297 | // Set of (Action, canonical ToolChain triple) pairs we've built jobs for. |
5298 | std::map<std::pair<const Action *, std::string>, InputInfoList> CachedResults; |
5299 | for (Action *A : C.getActions()) { |
5300 | // If we are linking an image for multiple archs then the linker wants |
5301 | // -arch_multiple and -final_output <final image name>. Unfortunately, this |
5302 | // doesn't fit in cleanly because we have to pass this information down. |
5303 | // |
5304 | // FIXME: This is a hack; find a cleaner way to integrate this into the |
5305 | // process. |
5306 | const char *LinkingOutput = nullptr; |
5307 | if (isa<LipoJobAction>(Val: A)) { |
5308 | if (FinalOutput) |
5309 | LinkingOutput = FinalOutput->getValue(); |
5310 | else |
5311 | LinkingOutput = getDefaultImageName(); |
5312 | } |
5313 | |
5314 | BuildJobsForAction(C, A, TC: &C.getDefaultToolChain(), |
5315 | /*BoundArch*/ StringRef(), |
5316 | /*AtTopLevel*/ true, |
5317 | /*MultipleArchs*/ ArchNames.size() > 1, |
5318 | /*LinkingOutput*/ LinkingOutput, CachedResults, |
5319 | /*TargetDeviceOffloadKind*/ Action::OFK_None); |
5320 | } |
5321 | |
5322 | // If we have more than one job, then disable integrated-cc1 for now. Do this |
5323 | // also when we need to report process execution statistics. |
5324 | if (C.getJobs().size() > 1 || CCPrintProcessStats) |
5325 | for (auto &J : C.getJobs()) |
5326 | J.InProcess = false; |
5327 | |
5328 | if (CCPrintProcessStats) { |
5329 | C.setPostCallback([=](const Command &Cmd, int Res) { |
5330 | std::optional<llvm::sys::ProcessStatistics> ProcStat = |
5331 | Cmd.getProcessStatistics(); |
5332 | if (!ProcStat) |
5333 | return; |
5334 | |
5335 | const char *LinkingOutput = nullptr; |
5336 | if (FinalOutput) |
5337 | LinkingOutput = FinalOutput->getValue(); |
5338 | else if (!Cmd.getOutputFilenames().empty()) |
5339 | LinkingOutput = Cmd.getOutputFilenames().front().c_str(); |
5340 | else |
5341 | LinkingOutput = getDefaultImageName(); |
5342 | |
5343 | if (CCPrintStatReportFilename.empty()) { |
5344 | using namespace llvm; |
5345 | // Human readable output. |
5346 | outs() << sys::path::filename(path: Cmd.getExecutable()) << ": " |
5347 | << "output=" << LinkingOutput; |
5348 | outs() << ", total=" |
5349 | << format(Fmt: "%.3f" , Vals: ProcStat->TotalTime.count() / 1000.) << " ms" |
5350 | << ", user=" |
5351 | << format(Fmt: "%.3f" , Vals: ProcStat->UserTime.count() / 1000.) << " ms" |
5352 | << ", mem=" << ProcStat->PeakMemory << " Kb\n" ; |
5353 | } else { |
5354 | // CSV format. |
5355 | std::string Buffer; |
5356 | llvm::raw_string_ostream Out(Buffer); |
5357 | llvm::sys::printArg(OS&: Out, Arg: llvm::sys::path::filename(path: Cmd.getExecutable()), |
5358 | /*Quote*/ true); |
5359 | Out << ','; |
5360 | llvm::sys::printArg(OS&: Out, Arg: LinkingOutput, Quote: true); |
5361 | Out << ',' << ProcStat->TotalTime.count() << ',' |
5362 | << ProcStat->UserTime.count() << ',' << ProcStat->PeakMemory |
5363 | << '\n'; |
5364 | Out.flush(); |
5365 | std::error_code EC; |
5366 | llvm::raw_fd_ostream OS(CCPrintStatReportFilename, EC, |
5367 | llvm::sys::fs::OF_Append | |
5368 | llvm::sys::fs::OF_Text); |
5369 | if (EC) |
5370 | return; |
5371 | auto L = OS.lock(); |
5372 | if (!L) { |
5373 | llvm::errs() << "ERROR: Cannot lock file " |
5374 | << CCPrintStatReportFilename << ": " |
5375 | << toString(E: L.takeError()) << "\n" ; |
5376 | return; |
5377 | } |
5378 | OS << Buffer; |
5379 | OS.flush(); |
5380 | } |
5381 | }); |
5382 | } |
5383 | |
5384 | // If the user passed -Qunused-arguments or there were errors, don't |
5385 | // warn about any unused arguments. |
5386 | bool ReportUnusedArguments = |
5387 | !Diags.hasErrorOccurred() && |
5388 | !C.getArgs().hasArg(Ids: options::OPT_Qunused_arguments); |
5389 | |
5390 | // Claim -fdriver-only here. |
5391 | (void)C.getArgs().hasArg(Ids: options::OPT_fdriver_only); |
5392 | // Claim -### here. |
5393 | (void)C.getArgs().hasArg(Ids: options::OPT__HASH_HASH_HASH); |
5394 | |
5395 | // Claim --driver-mode, --rsp-quoting, it was handled earlier. |
5396 | (void)C.getArgs().hasArg(Ids: options::OPT_driver_mode); |
5397 | (void)C.getArgs().hasArg(Ids: options::OPT_rsp_quoting); |
5398 | |
5399 | bool HasAssembleJob = llvm::any_of(Range&: C.getJobs(), P: [](auto &J) { |
5400 | // Match ClangAs and other derived assemblers of Tool. ClangAs uses a |
5401 | // longer ShortName "clang integrated assembler" while other assemblers just |
5402 | // use "assembler". |
5403 | return strstr(J.getCreator().getShortName(), "assembler" ); |
5404 | }); |
5405 | for (Arg *A : C.getArgs()) { |
5406 | // FIXME: It would be nice to be able to send the argument to the |
5407 | // DiagnosticsEngine, so that extra values, position, and so on could be |
5408 | // printed. |
5409 | if (!A->isClaimed()) { |
5410 | if (A->getOption().hasFlag(Val: options::NoArgumentUnused)) |
5411 | continue; |
5412 | |
5413 | // Suppress the warning automatically if this is just a flag, and it is an |
5414 | // instance of an argument we already claimed. |
5415 | const Option &Opt = A->getOption(); |
5416 | if (Opt.getKind() == Option::FlagClass) { |
5417 | bool DuplicateClaimed = false; |
5418 | |
5419 | for (const Arg *AA : C.getArgs().filtered(Ids: &Opt)) { |
5420 | if (AA->isClaimed()) { |
5421 | DuplicateClaimed = true; |
5422 | break; |
5423 | } |
5424 | } |
5425 | |
5426 | if (DuplicateClaimed) |
5427 | continue; |
5428 | } |
5429 | |
5430 | // In clang-cl, don't mention unknown arguments here since they have |
5431 | // already been warned about. |
5432 | if (!IsCLMode() || !A->getOption().matches(ID: options::OPT_UNKNOWN)) { |
5433 | if (A->getOption().hasFlag(Val: options::TargetSpecific) && |
5434 | !A->isIgnoredTargetSpecific() && !HasAssembleJob && |
5435 | // When for example -### or -v is used |
5436 | // without a file, target specific options are not |
5437 | // consumed/validated. |
5438 | // Instead emitting an error emit a warning instead. |
5439 | !C.getActions().empty()) { |
5440 | Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
5441 | << A->getSpelling() << getTargetTriple(); |
5442 | } else if (ReportUnusedArguments) { |
5443 | Diag(DiagID: clang::diag::warn_drv_unused_argument) |
5444 | << A->getAsString(Args: C.getArgs()); |
5445 | } |
5446 | } |
5447 | } |
5448 | } |
5449 | } |
5450 | |
5451 | namespace { |
5452 | /// Utility class to control the collapse of dependent actions and select the |
5453 | /// tools accordingly. |
5454 | class ToolSelector final { |
5455 | /// The tool chain this selector refers to. |
5456 | const ToolChain &TC; |
5457 | |
5458 | /// The compilation this selector refers to. |
5459 | const Compilation &C; |
5460 | |
5461 | /// The base action this selector refers to. |
5462 | const JobAction *BaseAction; |
5463 | |
5464 | /// Set to true if the current toolchain refers to host actions. |
5465 | bool IsHostSelector; |
5466 | |
5467 | /// Set to true if save-temps and embed-bitcode functionalities are active. |
5468 | bool SaveTemps; |
5469 | bool EmbedBitcode; |
5470 | |
5471 | /// Get previous dependent action or null if that does not exist. If |
5472 | /// \a CanBeCollapsed is false, that action must be legal to collapse or |
5473 | /// null will be returned. |
5474 | const JobAction *getPrevDependentAction(const ActionList &Inputs, |
5475 | ActionList &SavedOffloadAction, |
5476 | bool CanBeCollapsed = true) { |
5477 | // An option can be collapsed only if it has a single input. |
5478 | if (Inputs.size() != 1) |
5479 | return nullptr; |
5480 | |
5481 | Action *CurAction = *Inputs.begin(); |
5482 | if (CanBeCollapsed && |
5483 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
5484 | return nullptr; |
5485 | |
5486 | // If the input action is an offload action. Look through it and save any |
5487 | // offload action that can be dropped in the event of a collapse. |
5488 | if (auto *OA = dyn_cast<OffloadAction>(Val: CurAction)) { |
5489 | // If the dependent action is a device action, we will attempt to collapse |
5490 | // only with other device actions. Otherwise, we would do the same but |
5491 | // with host actions only. |
5492 | if (!IsHostSelector) { |
5493 | if (OA->hasSingleDeviceDependence(/*DoNotConsiderHostActions=*/true)) { |
5494 | CurAction = |
5495 | OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true); |
5496 | if (CanBeCollapsed && |
5497 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
5498 | return nullptr; |
5499 | SavedOffloadAction.push_back(Elt: OA); |
5500 | return dyn_cast<JobAction>(Val: CurAction); |
5501 | } |
5502 | } else if (OA->hasHostDependence()) { |
5503 | CurAction = OA->getHostDependence(); |
5504 | if (CanBeCollapsed && |
5505 | !CurAction->isCollapsingWithNextDependentActionLegal()) |
5506 | return nullptr; |
5507 | SavedOffloadAction.push_back(Elt: OA); |
5508 | return dyn_cast<JobAction>(Val: CurAction); |
5509 | } |
5510 | return nullptr; |
5511 | } |
5512 | |
5513 | return dyn_cast<JobAction>(Val: CurAction); |
5514 | } |
5515 | |
5516 | /// Return true if an assemble action can be collapsed. |
5517 | bool canCollapseAssembleAction() const { |
5518 | return TC.useIntegratedAs() && !SaveTemps && |
5519 | !C.getArgs().hasArg(Ids: options::OPT_via_file_asm) && |
5520 | !C.getArgs().hasArg(Ids: options::OPT__SLASH_FA) && |
5521 | !C.getArgs().hasArg(Ids: options::OPT__SLASH_Fa) && |
5522 | !C.getArgs().hasArg(Ids: options::OPT_dxc_Fc); |
5523 | } |
5524 | |
5525 | /// Return true if a preprocessor action can be collapsed. |
5526 | bool canCollapsePreprocessorAction() const { |
5527 | return !C.getArgs().hasArg(Ids: options::OPT_no_integrated_cpp) && |
5528 | !C.getArgs().hasArg(Ids: options::OPT_traditional_cpp) && !SaveTemps && |
5529 | !C.getArgs().hasArg(Ids: options::OPT_rewrite_objc); |
5530 | } |
5531 | |
5532 | /// Struct that relates an action with the offload actions that would be |
5533 | /// collapsed with it. |
5534 | struct JobActionInfo final { |
5535 | /// The action this info refers to. |
5536 | const JobAction *JA = nullptr; |
5537 | /// The offload actions we need to take care off if this action is |
5538 | /// collapsed. |
5539 | ActionList SavedOffloadAction; |
5540 | }; |
5541 | |
5542 | /// Append collapsed offload actions from the give nnumber of elements in the |
5543 | /// action info array. |
5544 | static void AppendCollapsedOffloadAction(ActionList &CollapsedOffloadAction, |
5545 | ArrayRef<JobActionInfo> &ActionInfo, |
5546 | unsigned ElementNum) { |
5547 | assert(ElementNum <= ActionInfo.size() && "Invalid number of elements." ); |
5548 | for (unsigned I = 0; I < ElementNum; ++I) |
5549 | CollapsedOffloadAction.append(in_start: ActionInfo[I].SavedOffloadAction.begin(), |
5550 | in_end: ActionInfo[I].SavedOffloadAction.end()); |
5551 | } |
5552 | |
5553 | /// Functions that attempt to perform the combining. They detect if that is |
5554 | /// legal, and if so they update the inputs \a Inputs and the offload action |
5555 | /// that were collapsed in \a CollapsedOffloadAction. A tool that deals with |
5556 | /// the combined action is returned. If the combining is not legal or if the |
5557 | /// tool does not exist, null is returned. |
5558 | /// Currently three kinds of collapsing are supported: |
5559 | /// - Assemble + Backend + Compile; |
5560 | /// - Assemble + Backend ; |
5561 | /// - Backend + Compile. |
5562 | const Tool * |
5563 | combineAssembleBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
5564 | ActionList &Inputs, |
5565 | ActionList &CollapsedOffloadAction) { |
5566 | if (ActionInfo.size() < 3 || !canCollapseAssembleAction()) |
5567 | return nullptr; |
5568 | auto *AJ = dyn_cast<AssembleJobAction>(Val: ActionInfo[0].JA); |
5569 | auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[1].JA); |
5570 | auto *CJ = dyn_cast<CompileJobAction>(Val: ActionInfo[2].JA); |
5571 | if (!AJ || !BJ || !CJ) |
5572 | return nullptr; |
5573 | |
5574 | // Get compiler tool. |
5575 | const Tool *T = TC.SelectTool(JA: *CJ); |
5576 | if (!T) |
5577 | return nullptr; |
5578 | |
5579 | // Can't collapse if we don't have codegen support unless we are |
5580 | // emitting LLVM IR. |
5581 | bool OutputIsLLVM = types::isLLVMIR(Id: ActionInfo[0].JA->getType()); |
5582 | if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR())) |
5583 | return nullptr; |
5584 | |
5585 | // When using -fembed-bitcode, it is required to have the same tool (clang) |
5586 | // for both CompilerJA and BackendJA. Otherwise, combine two stages. |
5587 | if (EmbedBitcode) { |
5588 | const Tool *BT = TC.SelectTool(JA: *BJ); |
5589 | if (BT == T) |
5590 | return nullptr; |
5591 | } |
5592 | |
5593 | if (!T->hasIntegratedAssembler()) |
5594 | return nullptr; |
5595 | |
5596 | Inputs = CJ->getInputs(); |
5597 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
5598 | /*NumElements=*/ElementNum: 3); |
5599 | return T; |
5600 | } |
5601 | const Tool *combineAssembleBackend(ArrayRef<JobActionInfo> ActionInfo, |
5602 | ActionList &Inputs, |
5603 | ActionList &CollapsedOffloadAction) { |
5604 | if (ActionInfo.size() < 2 || !canCollapseAssembleAction()) |
5605 | return nullptr; |
5606 | auto *AJ = dyn_cast<AssembleJobAction>(Val: ActionInfo[0].JA); |
5607 | auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[1].JA); |
5608 | if (!AJ || !BJ) |
5609 | return nullptr; |
5610 | |
5611 | // Get backend tool. |
5612 | const Tool *T = TC.SelectTool(JA: *BJ); |
5613 | if (!T) |
5614 | return nullptr; |
5615 | |
5616 | if (!T->hasIntegratedAssembler()) |
5617 | return nullptr; |
5618 | |
5619 | Inputs = BJ->getInputs(); |
5620 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
5621 | /*NumElements=*/ElementNum: 2); |
5622 | return T; |
5623 | } |
5624 | const Tool *combineBackendCompile(ArrayRef<JobActionInfo> ActionInfo, |
5625 | ActionList &Inputs, |
5626 | ActionList &CollapsedOffloadAction) { |
5627 | if (ActionInfo.size() < 2) |
5628 | return nullptr; |
5629 | auto *BJ = dyn_cast<BackendJobAction>(Val: ActionInfo[0].JA); |
5630 | auto *CJ = dyn_cast<CompileJobAction>(Val: ActionInfo[1].JA); |
5631 | if (!BJ || !CJ) |
5632 | return nullptr; |
5633 | |
5634 | auto HasBitcodeInput = [](const JobActionInfo &AI) { |
5635 | for (auto &Input : AI.JA->getInputs()) |
5636 | if (!types::isLLVMIR(Id: Input->getType())) |
5637 | return false; |
5638 | return true; |
5639 | }; |
5640 | |
5641 | // Check if the initial input (to the compile job or its predessor if one |
5642 | // exists) is LLVM bitcode. In that case, no preprocessor step is required |
5643 | // and we can still collapse the compile and backend jobs when we have |
5644 | // -save-temps. I.e. there is no need for a separate compile job just to |
5645 | // emit unoptimized bitcode. |
5646 | bool InputIsBitcode = all_of(Range&: ActionInfo, P: HasBitcodeInput); |
5647 | if (SaveTemps && !InputIsBitcode) |
5648 | return nullptr; |
5649 | |
5650 | // Get compiler tool. |
5651 | const Tool *T = TC.SelectTool(JA: *CJ); |
5652 | if (!T) |
5653 | return nullptr; |
5654 | |
5655 | // Can't collapse if we don't have codegen support unless we are |
5656 | // emitting LLVM IR. |
5657 | bool OutputIsLLVM = types::isLLVMIR(Id: ActionInfo[0].JA->getType()); |
5658 | if (!T->hasIntegratedBackend() && !(OutputIsLLVM && T->canEmitIR())) |
5659 | return nullptr; |
5660 | |
5661 | if (T->canEmitIR() && EmbedBitcode) |
5662 | return nullptr; |
5663 | |
5664 | Inputs = CJ->getInputs(); |
5665 | AppendCollapsedOffloadAction(CollapsedOffloadAction, ActionInfo, |
5666 | /*NumElements=*/ElementNum: 2); |
5667 | return T; |
5668 | } |
5669 | |
5670 | /// Updates the inputs if the obtained tool supports combining with |
5671 | /// preprocessor action, and the current input is indeed a preprocessor |
5672 | /// action. If combining results in the collapse of offloading actions, those |
5673 | /// are appended to \a CollapsedOffloadAction. |
5674 | void combineWithPreprocessor(const Tool *T, ActionList &Inputs, |
5675 | ActionList &CollapsedOffloadAction) { |
5676 | if (!T || !canCollapsePreprocessorAction() || !T->hasIntegratedCPP()) |
5677 | return; |
5678 | |
5679 | // Attempt to get a preprocessor action dependence. |
5680 | ActionList PreprocessJobOffloadActions; |
5681 | ActionList NewInputs; |
5682 | for (Action *A : Inputs) { |
5683 | auto *PJ = getPrevDependentAction(Inputs: {A}, SavedOffloadAction&: PreprocessJobOffloadActions); |
5684 | if (!PJ || !isa<PreprocessJobAction>(Val: PJ)) { |
5685 | NewInputs.push_back(Elt: A); |
5686 | continue; |
5687 | } |
5688 | |
5689 | // This is legal to combine. Append any offload action we found and add the |
5690 | // current input to preprocessor inputs. |
5691 | CollapsedOffloadAction.append(in_start: PreprocessJobOffloadActions.begin(), |
5692 | in_end: PreprocessJobOffloadActions.end()); |
5693 | NewInputs.append(in_start: PJ->input_begin(), in_end: PJ->input_end()); |
5694 | } |
5695 | Inputs = NewInputs; |
5696 | } |
5697 | |
5698 | public: |
5699 | ToolSelector(const JobAction *BaseAction, const ToolChain &TC, |
5700 | const Compilation &C, bool SaveTemps, bool EmbedBitcode) |
5701 | : TC(TC), C(C), BaseAction(BaseAction), SaveTemps(SaveTemps), |
5702 | EmbedBitcode(EmbedBitcode) { |
5703 | assert(BaseAction && "Invalid base action." ); |
5704 | IsHostSelector = BaseAction->getOffloadingDeviceKind() == Action::OFK_None; |
5705 | } |
5706 | |
5707 | /// Check if a chain of actions can be combined and return the tool that can |
5708 | /// handle the combination of actions. The pointer to the current inputs \a |
5709 | /// Inputs and the list of offload actions \a CollapsedOffloadActions |
5710 | /// connected to collapsed actions are updated accordingly. The latter enables |
5711 | /// the caller of the selector to process them afterwards instead of just |
5712 | /// dropping them. If no suitable tool is found, null will be returned. |
5713 | const Tool *getTool(ActionList &Inputs, |
5714 | ActionList &CollapsedOffloadAction) { |
5715 | // |
5716 | // Get the largest chain of actions that we could combine. |
5717 | // |
5718 | |
5719 | SmallVector<JobActionInfo, 5> ActionChain(1); |
5720 | ActionChain.back().JA = BaseAction; |
5721 | while (ActionChain.back().JA) { |
5722 | const Action *CurAction = ActionChain.back().JA; |
5723 | |
5724 | // Grow the chain by one element. |
5725 | ActionChain.resize(N: ActionChain.size() + 1); |
5726 | JobActionInfo &AI = ActionChain.back(); |
5727 | |
5728 | // Attempt to fill it with the |
5729 | AI.JA = |
5730 | getPrevDependentAction(Inputs: CurAction->getInputs(), SavedOffloadAction&: AI.SavedOffloadAction); |
5731 | } |
5732 | |
5733 | // Pop the last action info as it could not be filled. |
5734 | ActionChain.pop_back(); |
5735 | |
5736 | // |
5737 | // Attempt to combine actions. If all combining attempts failed, just return |
5738 | // the tool of the provided action. At the end we attempt to combine the |
5739 | // action with any preprocessor action it may depend on. |
5740 | // |
5741 | |
5742 | const Tool *T = combineAssembleBackendCompile(ActionInfo: ActionChain, Inputs, |
5743 | CollapsedOffloadAction); |
5744 | if (!T) |
5745 | T = combineAssembleBackend(ActionInfo: ActionChain, Inputs, CollapsedOffloadAction); |
5746 | if (!T) |
5747 | T = combineBackendCompile(ActionInfo: ActionChain, Inputs, CollapsedOffloadAction); |
5748 | if (!T) { |
5749 | Inputs = BaseAction->getInputs(); |
5750 | T = TC.SelectTool(JA: *BaseAction); |
5751 | } |
5752 | |
5753 | combineWithPreprocessor(T, Inputs, CollapsedOffloadAction); |
5754 | return T; |
5755 | } |
5756 | }; |
5757 | } |
5758 | |
5759 | /// Return a string that uniquely identifies the result of a job. The bound arch |
5760 | /// is not necessarily represented in the toolchain's triple -- for example, |
5761 | /// armv7 and armv7s both map to the same triple -- so we need both in our map. |
5762 | /// Also, we need to add the offloading device kind, as the same tool chain can |
5763 | /// be used for host and device for some programming models, e.g. OpenMP. |
5764 | static std::string GetTriplePlusArchString(const ToolChain *TC, |
5765 | StringRef BoundArch, |
5766 | Action::OffloadKind OffloadKind) { |
5767 | std::string TriplePlusArch = TC->getTriple().normalize(); |
5768 | if (!BoundArch.empty()) { |
5769 | TriplePlusArch += "-" ; |
5770 | TriplePlusArch += BoundArch; |
5771 | } |
5772 | TriplePlusArch += "-" ; |
5773 | TriplePlusArch += Action::GetOffloadKindName(Kind: OffloadKind); |
5774 | return TriplePlusArch; |
5775 | } |
5776 | |
5777 | InputInfoList Driver::BuildJobsForAction( |
5778 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
5779 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
5780 | std::map<std::pair<const Action *, std::string>, InputInfoList> |
5781 | &CachedResults, |
5782 | Action::OffloadKind TargetDeviceOffloadKind) const { |
5783 | std::pair<const Action *, std::string> ActionTC = { |
5784 | A, GetTriplePlusArchString(TC, BoundArch, OffloadKind: TargetDeviceOffloadKind)}; |
5785 | auto CachedResult = CachedResults.find(x: ActionTC); |
5786 | if (CachedResult != CachedResults.end()) { |
5787 | return CachedResult->second; |
5788 | } |
5789 | InputInfoList Result = BuildJobsForActionNoCache( |
5790 | C, A, TC, BoundArch, AtTopLevel, MultipleArchs, LinkingOutput, |
5791 | CachedResults, TargetDeviceOffloadKind); |
5792 | CachedResults[ActionTC] = Result; |
5793 | return Result; |
5794 | } |
5795 | |
5796 | static void handleTimeTrace(Compilation &C, const ArgList &Args, |
5797 | const JobAction *JA, const char *BaseInput, |
5798 | const InputInfo &Result) { |
5799 | Arg *A = |
5800 | Args.getLastArg(Ids: options::OPT_ftime_trace, Ids: options::OPT_ftime_trace_EQ); |
5801 | if (!A) |
5802 | return; |
5803 | SmallString<128> Path; |
5804 | if (A->getOption().matches(ID: options::OPT_ftime_trace_EQ)) { |
5805 | Path = A->getValue(); |
5806 | if (llvm::sys::fs::is_directory(Path)) { |
5807 | SmallString<128> Tmp(Result.getFilename()); |
5808 | llvm::sys::path::replace_extension(path&: Tmp, extension: "json" ); |
5809 | llvm::sys::path::append(path&: Path, a: llvm::sys::path::filename(path: Tmp)); |
5810 | } |
5811 | } else { |
5812 | if (Arg *DumpDir = Args.getLastArgNoClaim(Ids: options::OPT_dumpdir)) { |
5813 | // The trace file is ${dumpdir}${basename}.json. Note that dumpdir may not |
5814 | // end with a path separator. |
5815 | Path = DumpDir->getValue(); |
5816 | Path += llvm::sys::path::filename(path: BaseInput); |
5817 | } else { |
5818 | Path = Result.getFilename(); |
5819 | } |
5820 | llvm::sys::path::replace_extension(path&: Path, extension: "json" ); |
5821 | } |
5822 | const char *ResultFile = C.getArgs().MakeArgString(Str: Path); |
5823 | C.addTimeTraceFile(Name: ResultFile, JA); |
5824 | C.addResultFile(Name: ResultFile, JA); |
5825 | } |
5826 | |
5827 | InputInfoList Driver::BuildJobsForActionNoCache( |
5828 | Compilation &C, const Action *A, const ToolChain *TC, StringRef BoundArch, |
5829 | bool AtTopLevel, bool MultipleArchs, const char *LinkingOutput, |
5830 | std::map<std::pair<const Action *, std::string>, InputInfoList> |
5831 | &CachedResults, |
5832 | Action::OffloadKind TargetDeviceOffloadKind) const { |
5833 | llvm::PrettyStackTraceString CrashInfo("Building compilation jobs" ); |
5834 | |
5835 | InputInfoList OffloadDependencesInputInfo; |
5836 | bool BuildingForOffloadDevice = TargetDeviceOffloadKind != Action::OFK_None; |
5837 | if (const OffloadAction *OA = dyn_cast<OffloadAction>(Val: A)) { |
5838 | // The 'Darwin' toolchain is initialized only when its arguments are |
5839 | // computed. Get the default arguments for OFK_None to ensure that |
5840 | // initialization is performed before processing the offload action. |
5841 | // FIXME: Remove when darwin's toolchain is initialized during construction. |
5842 | C.getArgsForToolChain(TC, BoundArch, DeviceOffloadKind: Action::OFK_None); |
5843 | |
5844 | // The offload action is expected to be used in four different situations. |
5845 | // |
5846 | // a) Set a toolchain/architecture/kind for a host action: |
5847 | // Host Action 1 -> OffloadAction -> Host Action 2 |
5848 | // |
5849 | // b) Set a toolchain/architecture/kind for a device action; |
5850 | // Device Action 1 -> OffloadAction -> Device Action 2 |
5851 | // |
5852 | // c) Specify a device dependence to a host action; |
5853 | // Device Action 1 _ |
5854 | // \ |
5855 | // Host Action 1 ---> OffloadAction -> Host Action 2 |
5856 | // |
5857 | // d) Specify a host dependence to a device action. |
5858 | // Host Action 1 _ |
5859 | // \ |
5860 | // Device Action 1 ---> OffloadAction -> Device Action 2 |
5861 | // |
5862 | // For a) and b), we just return the job generated for the dependences. For |
5863 | // c) and d) we override the current action with the host/device dependence |
5864 | // if the current toolchain is host/device and set the offload dependences |
5865 | // info with the jobs obtained from the device/host dependence(s). |
5866 | |
5867 | // If there is a single device option or has no host action, just generate |
5868 | // the job for it. |
5869 | if (OA->hasSingleDeviceDependence() || !OA->hasHostDependence()) { |
5870 | InputInfoList DevA; |
5871 | OA->doOnEachDeviceDependence(Work: [&](Action *DepA, const ToolChain *DepTC, |
5872 | const char *DepBoundArch) { |
5873 | DevA.append(RHS: BuildJobsForAction(C, A: DepA, TC: DepTC, BoundArch: DepBoundArch, AtTopLevel, |
5874 | /*MultipleArchs*/ !!DepBoundArch, |
5875 | LinkingOutput, CachedResults, |
5876 | TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind())); |
5877 | }); |
5878 | return DevA; |
5879 | } |
5880 | |
5881 | // If 'Action 2' is host, we generate jobs for the device dependences and |
5882 | // override the current action with the host dependence. Otherwise, we |
5883 | // generate the host dependences and override the action with the device |
5884 | // dependence. The dependences can't therefore be a top-level action. |
5885 | OA->doOnEachDependence( |
5886 | /*IsHostDependence=*/BuildingForOffloadDevice, |
5887 | Work: [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
5888 | OffloadDependencesInputInfo.append(RHS: BuildJobsForAction( |
5889 | C, A: DepA, TC: DepTC, BoundArch: DepBoundArch, /*AtTopLevel=*/false, |
5890 | /*MultipleArchs*/ !!DepBoundArch, LinkingOutput, CachedResults, |
5891 | TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind())); |
5892 | }); |
5893 | |
5894 | A = BuildingForOffloadDevice |
5895 | ? OA->getSingleDeviceDependence(/*DoNotConsiderHostActions=*/true) |
5896 | : OA->getHostDependence(); |
5897 | |
5898 | // We may have already built this action as a part of the offloading |
5899 | // toolchain, return the cached input if so. |
5900 | std::pair<const Action *, std::string> ActionTC = { |
5901 | OA->getHostDependence(), |
5902 | GetTriplePlusArchString(TC, BoundArch, OffloadKind: TargetDeviceOffloadKind)}; |
5903 | auto It = CachedResults.find(x: ActionTC); |
5904 | if (It != CachedResults.end()) { |
5905 | InputInfoList Inputs = It->second; |
5906 | Inputs.append(RHS: OffloadDependencesInputInfo); |
5907 | return Inputs; |
5908 | } |
5909 | } |
5910 | |
5911 | if (const InputAction *IA = dyn_cast<InputAction>(Val: A)) { |
5912 | // FIXME: It would be nice to not claim this here; maybe the old scheme of |
5913 | // just using Args was better? |
5914 | const Arg &Input = IA->getInputArg(); |
5915 | Input.claim(); |
5916 | if (Input.getOption().matches(ID: options::OPT_INPUT)) { |
5917 | const char *Name = Input.getValue(); |
5918 | return {InputInfo(A, Name, /* _BaseInput = */ Name)}; |
5919 | } |
5920 | return {InputInfo(A, &Input, /* _BaseInput = */ "" )}; |
5921 | } |
5922 | |
5923 | if (const BindArchAction *BAA = dyn_cast<BindArchAction>(Val: A)) { |
5924 | const ToolChain *TC; |
5925 | StringRef ArchName = BAA->getArchName(); |
5926 | |
5927 | if (!ArchName.empty()) |
5928 | TC = &getToolChain(Args: C.getArgs(), |
5929 | Target: computeTargetTriple(D: *this, TargetTriple, |
5930 | Args: C.getArgs(), DarwinArchName: ArchName)); |
5931 | else |
5932 | TC = &C.getDefaultToolChain(); |
5933 | |
5934 | return BuildJobsForAction(C, A: *BAA->input_begin(), TC, BoundArch: ArchName, AtTopLevel, |
5935 | MultipleArchs, LinkingOutput, CachedResults, |
5936 | TargetDeviceOffloadKind); |
5937 | } |
5938 | |
5939 | |
5940 | ActionList Inputs = A->getInputs(); |
5941 | |
5942 | const JobAction *JA = cast<JobAction>(Val: A); |
5943 | ActionList CollapsedOffloadActions; |
5944 | |
5945 | ToolSelector TS(JA, *TC, C, isSaveTempsEnabled(), |
5946 | embedBitcodeInObject() && !isUsingLTO()); |
5947 | const Tool *T = TS.getTool(Inputs, CollapsedOffloadAction&: CollapsedOffloadActions); |
5948 | |
5949 | if (!T) |
5950 | return {InputInfo()}; |
5951 | |
5952 | // If we've collapsed action list that contained OffloadAction we |
5953 | // need to build jobs for host/device-side inputs it may have held. |
5954 | for (const auto *OA : CollapsedOffloadActions) |
5955 | cast<OffloadAction>(Val: OA)->doOnEachDependence( |
5956 | /*IsHostDependence=*/BuildingForOffloadDevice, |
5957 | Work: [&](Action *DepA, const ToolChain *DepTC, const char *DepBoundArch) { |
5958 | OffloadDependencesInputInfo.append(RHS: BuildJobsForAction( |
5959 | C, A: DepA, TC: DepTC, BoundArch: DepBoundArch, /* AtTopLevel */ false, |
5960 | /*MultipleArchs=*/!!DepBoundArch, LinkingOutput, CachedResults, |
5961 | TargetDeviceOffloadKind: DepA->getOffloadingDeviceKind())); |
5962 | }); |
5963 | |
5964 | // Only use pipes when there is exactly one input. |
5965 | InputInfoList InputInfos; |
5966 | for (const Action *Input : Inputs) { |
5967 | // Treat dsymutil and verify sub-jobs as being at the top-level too, they |
5968 | // shouldn't get temporary output names. |
5969 | // FIXME: Clean this up. |
5970 | bool SubJobAtTopLevel = |
5971 | AtTopLevel && (isa<DsymutilJobAction>(Val: A) || isa<VerifyJobAction>(Val: A)); |
5972 | InputInfos.append(RHS: BuildJobsForAction( |
5973 | C, A: Input, TC, BoundArch, AtTopLevel: SubJobAtTopLevel, MultipleArchs, LinkingOutput, |
5974 | CachedResults, TargetDeviceOffloadKind: A->getOffloadingDeviceKind())); |
5975 | } |
5976 | |
5977 | // Always use the first file input as the base input. |
5978 | const char *BaseInput = InputInfos[0].getBaseInput(); |
5979 | for (auto &Info : InputInfos) { |
5980 | if (Info.isFilename()) { |
5981 | BaseInput = Info.getBaseInput(); |
5982 | break; |
5983 | } |
5984 | } |
5985 | |
5986 | // ... except dsymutil actions, which use their actual input as the base |
5987 | // input. |
5988 | if (JA->getType() == types::TY_dSYM) |
5989 | BaseInput = InputInfos[0].getFilename(); |
5990 | |
5991 | // Append outputs of offload device jobs to the input list |
5992 | if (!OffloadDependencesInputInfo.empty()) |
5993 | InputInfos.append(in_start: OffloadDependencesInputInfo.begin(), |
5994 | in_end: OffloadDependencesInputInfo.end()); |
5995 | |
5996 | // Set the effective triple of the toolchain for the duration of this job. |
5997 | llvm::Triple EffectiveTriple; |
5998 | const ToolChain &ToolTC = T->getToolChain(); |
5999 | const ArgList &Args = |
6000 | C.getArgsForToolChain(TC, BoundArch, DeviceOffloadKind: A->getOffloadingDeviceKind()); |
6001 | if (InputInfos.size() != 1) { |
6002 | EffectiveTriple = llvm::Triple(ToolTC.ComputeEffectiveClangTriple(Args)); |
6003 | } else { |
6004 | // Pass along the input type if it can be unambiguously determined. |
6005 | EffectiveTriple = llvm::Triple( |
6006 | ToolTC.ComputeEffectiveClangTriple(Args, InputType: InputInfos[0].getType())); |
6007 | } |
6008 | RegisterEffectiveTriple TripleRAII(ToolTC, EffectiveTriple); |
6009 | |
6010 | // Determine the place to write output to, if any. |
6011 | InputInfo Result; |
6012 | InputInfoList UnbundlingResults; |
6013 | if (auto *UA = dyn_cast<OffloadUnbundlingJobAction>(Val: JA)) { |
6014 | // If we have an unbundling job, we need to create results for all the |
6015 | // outputs. We also update the results cache so that other actions using |
6016 | // this unbundling action can get the right results. |
6017 | for (auto &UI : UA->getDependentActionsInfo()) { |
6018 | assert(UI.DependentOffloadKind != Action::OFK_None && |
6019 | "Unbundling with no offloading??" ); |
6020 | |
6021 | // Unbundling actions are never at the top level. When we generate the |
6022 | // offloading prefix, we also do that for the host file because the |
6023 | // unbundling action does not change the type of the output which can |
6024 | // cause a overwrite. |
6025 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
6026 | Kind: UI.DependentOffloadKind, |
6027 | NormalizedTriple: UI.DependentToolChain->getTriple().normalize(), |
6028 | /*CreatePrefixForHost=*/true); |
6029 | auto CurI = InputInfo( |
6030 | UA, |
6031 | GetNamedOutputPath(C, JA: *UA, BaseInput, BoundArch: UI.DependentBoundArch, |
6032 | /*AtTopLevel=*/false, |
6033 | MultipleArchs: MultipleArchs || |
6034 | UI.DependentOffloadKind == Action::OFK_HIP, |
6035 | NormalizedTriple: OffloadingPrefix), |
6036 | BaseInput); |
6037 | // Save the unbundling result. |
6038 | UnbundlingResults.push_back(Elt: CurI); |
6039 | |
6040 | // Get the unique string identifier for this dependence and cache the |
6041 | // result. |
6042 | StringRef Arch; |
6043 | if (TargetDeviceOffloadKind == Action::OFK_HIP) { |
6044 | if (UI.DependentOffloadKind == Action::OFK_Host) |
6045 | Arch = StringRef(); |
6046 | else |
6047 | Arch = UI.DependentBoundArch; |
6048 | } else |
6049 | Arch = BoundArch; |
6050 | |
6051 | CachedResults[{A, GetTriplePlusArchString(TC: UI.DependentToolChain, BoundArch: Arch, |
6052 | OffloadKind: UI.DependentOffloadKind)}] = { |
6053 | CurI}; |
6054 | } |
6055 | |
6056 | // Now that we have all the results generated, select the one that should be |
6057 | // returned for the current depending action. |
6058 | std::pair<const Action *, std::string> ActionTC = { |
6059 | A, GetTriplePlusArchString(TC, BoundArch, OffloadKind: TargetDeviceOffloadKind)}; |
6060 | assert(CachedResults.find(ActionTC) != CachedResults.end() && |
6061 | "Result does not exist??" ); |
6062 | Result = CachedResults[ActionTC].front(); |
6063 | } else if (JA->getType() == types::TY_Nothing) |
6064 | Result = {InputInfo(A, BaseInput)}; |
6065 | else { |
6066 | // We only have to generate a prefix for the host if this is not a top-level |
6067 | // action. |
6068 | std::string OffloadingPrefix = Action::GetOffloadingFileNamePrefix( |
6069 | Kind: A->getOffloadingDeviceKind(), NormalizedTriple: EffectiveTriple.normalize(), |
6070 | /*CreatePrefixForHost=*/isa<OffloadPackagerJobAction>(Val: A) || |
6071 | !(A->getOffloadingHostActiveKinds() == Action::OFK_None || |
6072 | AtTopLevel)); |
6073 | Result = InputInfo(A, GetNamedOutputPath(C, JA: *JA, BaseInput, BoundArch, |
6074 | AtTopLevel, MultipleArchs, |
6075 | NormalizedTriple: OffloadingPrefix), |
6076 | BaseInput); |
6077 | if (T->canEmitIR() && OffloadingPrefix.empty()) |
6078 | handleTimeTrace(C, Args, JA, BaseInput, Result); |
6079 | } |
6080 | |
6081 | if (CCCPrintBindings && !CCGenDiagnostics) { |
6082 | llvm::errs() << "# \"" << T->getToolChain().getTripleString() << '"' |
6083 | << " - \"" << T->getName() << "\", inputs: [" ; |
6084 | for (unsigned i = 0, e = InputInfos.size(); i != e; ++i) { |
6085 | llvm::errs() << InputInfos[i].getAsString(); |
6086 | if (i + 1 != e) |
6087 | llvm::errs() << ", " ; |
6088 | } |
6089 | if (UnbundlingResults.empty()) |
6090 | llvm::errs() << "], output: " << Result.getAsString() << "\n" ; |
6091 | else { |
6092 | llvm::errs() << "], outputs: [" ; |
6093 | for (unsigned i = 0, e = UnbundlingResults.size(); i != e; ++i) { |
6094 | llvm::errs() << UnbundlingResults[i].getAsString(); |
6095 | if (i + 1 != e) |
6096 | llvm::errs() << ", " ; |
6097 | } |
6098 | llvm::errs() << "] \n" ; |
6099 | } |
6100 | } else { |
6101 | if (UnbundlingResults.empty()) |
6102 | T->ConstructJob(C, JA: *JA, Output: Result, Inputs: InputInfos, TCArgs: Args, LinkingOutput); |
6103 | else |
6104 | T->ConstructJobMultipleOutputs(C, JA: *JA, Outputs: UnbundlingResults, Inputs: InputInfos, |
6105 | TCArgs: Args, LinkingOutput); |
6106 | } |
6107 | return {Result}; |
6108 | } |
6109 | |
6110 | const char *Driver::getDefaultImageName() const { |
6111 | llvm::Triple Target(llvm::Triple::normalize(Str: TargetTriple)); |
6112 | return Target.isOSWindows() ? "a.exe" : "a.out" ; |
6113 | } |
6114 | |
6115 | /// Create output filename based on ArgValue, which could either be a |
6116 | /// full filename, filename without extension, or a directory. If ArgValue |
6117 | /// does not provide a filename, then use BaseName, and use the extension |
6118 | /// suitable for FileType. |
6119 | static const char *MakeCLOutputFilename(const ArgList &Args, StringRef ArgValue, |
6120 | StringRef BaseName, |
6121 | types::ID FileType) { |
6122 | SmallString<128> Filename = ArgValue; |
6123 | |
6124 | if (ArgValue.empty()) { |
6125 | // If the argument is empty, output to BaseName in the current dir. |
6126 | Filename = BaseName; |
6127 | } else if (llvm::sys::path::is_separator(value: Filename.back())) { |
6128 | // If the argument is a directory, output to BaseName in that dir. |
6129 | llvm::sys::path::append(path&: Filename, a: BaseName); |
6130 | } |
6131 | |
6132 | if (!llvm::sys::path::has_extension(path: ArgValue)) { |
6133 | // If the argument didn't provide an extension, then set it. |
6134 | const char *Extension = types::getTypeTempSuffix(Id: FileType, CLStyle: true); |
6135 | |
6136 | if (FileType == types::TY_Image && |
6137 | Args.hasArg(Ids: options::OPT__SLASH_LD, Ids: options::OPT__SLASH_LDd)) { |
6138 | // The output file is a dll. |
6139 | Extension = "dll" ; |
6140 | } |
6141 | |
6142 | llvm::sys::path::replace_extension(path&: Filename, extension: Extension); |
6143 | } |
6144 | |
6145 | return Args.MakeArgString(Str: Filename.c_str()); |
6146 | } |
6147 | |
6148 | static bool HasPreprocessOutput(const Action &JA) { |
6149 | if (isa<PreprocessJobAction>(Val: JA)) |
6150 | return true; |
6151 | if (isa<OffloadAction>(Val: JA) && isa<PreprocessJobAction>(Val: JA.getInputs()[0])) |
6152 | return true; |
6153 | if (isa<OffloadBundlingJobAction>(Val: JA) && |
6154 | HasPreprocessOutput(JA: *(JA.getInputs()[0]))) |
6155 | return true; |
6156 | return false; |
6157 | } |
6158 | |
6159 | const char *Driver::CreateTempFile(Compilation &C, StringRef Prefix, |
6160 | StringRef Suffix, bool MultipleArchs, |
6161 | StringRef BoundArch, |
6162 | bool NeedUniqueDirectory) const { |
6163 | SmallString<128> TmpName; |
6164 | Arg *A = C.getArgs().getLastArg(Ids: options::OPT_fcrash_diagnostics_dir); |
6165 | std::optional<std::string> CrashDirectory = |
6166 | CCGenDiagnostics && A |
6167 | ? std::string(A->getValue()) |
6168 | : llvm::sys::Process::GetEnv(name: "CLANG_CRASH_DIAGNOSTICS_DIR" ); |
6169 | if (CrashDirectory) { |
6170 | if (!getVFS().exists(Path: *CrashDirectory)) |
6171 | llvm::sys::fs::create_directories(path: *CrashDirectory); |
6172 | SmallString<128> Path(*CrashDirectory); |
6173 | llvm::sys::path::append(path&: Path, a: Prefix); |
6174 | const char *Middle = !Suffix.empty() ? "-%%%%%%." : "-%%%%%%" ; |
6175 | if (std::error_code EC = |
6176 | llvm::sys::fs::createUniqueFile(Model: Path + Middle + Suffix, ResultPath&: TmpName)) { |
6177 | Diag(DiagID: clang::diag::err_unable_to_make_temp) << EC.message(); |
6178 | return "" ; |
6179 | } |
6180 | } else { |
6181 | if (MultipleArchs && !BoundArch.empty()) { |
6182 | if (NeedUniqueDirectory) { |
6183 | TmpName = GetTemporaryDirectory(Prefix); |
6184 | llvm::sys::path::append(path&: TmpName, |
6185 | a: Twine(Prefix) + "-" + BoundArch + "." + Suffix); |
6186 | } else { |
6187 | TmpName = |
6188 | GetTemporaryPath(Prefix: (Twine(Prefix) + "-" + BoundArch).str(), Suffix); |
6189 | } |
6190 | |
6191 | } else { |
6192 | TmpName = GetTemporaryPath(Prefix, Suffix); |
6193 | } |
6194 | } |
6195 | return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName)); |
6196 | } |
6197 | |
6198 | // Calculate the output path of the module file when compiling a module unit |
6199 | // with the `-fmodule-output` option or `-fmodule-output=` option specified. |
6200 | // The behavior is: |
6201 | // - If `-fmodule-output=` is specfied, then the module file is |
6202 | // writing to the value. |
6203 | // - Otherwise if the output object file of the module unit is specified, the |
6204 | // output path |
6205 | // of the module file should be the same with the output object file except |
6206 | // the corresponding suffix. This requires both `-o` and `-c` are specified. |
6207 | // - Otherwise, the output path of the module file will be the same with the |
6208 | // input with the corresponding suffix. |
6209 | static const char *GetModuleOutputPath(Compilation &C, const JobAction &JA, |
6210 | const char *BaseInput) { |
6211 | assert(isa<PrecompileJobAction>(JA) && JA.getType() == types::TY_ModuleFile && |
6212 | (C.getArgs().hasArg(options::OPT_fmodule_output) || |
6213 | C.getArgs().hasArg(options::OPT_fmodule_output_EQ))); |
6214 | |
6215 | SmallString<256> OutputPath = |
6216 | tools::getCXX20NamedModuleOutputPath(Args: C.getArgs(), BaseInput); |
6217 | |
6218 | return C.addResultFile(Name: C.getArgs().MakeArgString(Str: OutputPath.c_str()), JA: &JA); |
6219 | } |
6220 | |
6221 | const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, |
6222 | const char *BaseInput, |
6223 | StringRef OrigBoundArch, bool AtTopLevel, |
6224 | bool MultipleArchs, |
6225 | StringRef OffloadingPrefix) const { |
6226 | std::string BoundArch = OrigBoundArch.str(); |
6227 | if (is_style_windows(S: llvm::sys::path::Style::native)) { |
6228 | // BoundArch may contains ':', which is invalid in file names on Windows, |
6229 | // therefore replace it with '%'. |
6230 | llvm::replace(Range&: BoundArch, OldValue: ':', NewValue: '@'); |
6231 | } |
6232 | |
6233 | llvm::PrettyStackTraceString CrashInfo("Computing output path" ); |
6234 | // Output to a user requested destination? |
6235 | if (AtTopLevel && !isa<DsymutilJobAction>(Val: JA) && !isa<VerifyJobAction>(Val: JA)) { |
6236 | if (Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o)) |
6237 | return C.addResultFile(Name: FinalOutput->getValue(), JA: &JA); |
6238 | } |
6239 | |
6240 | // For /P, preprocess to file named after BaseInput. |
6241 | if (C.getArgs().hasArg(Ids: options::OPT__SLASH_P)) { |
6242 | assert(AtTopLevel && isa<PreprocessJobAction>(JA)); |
6243 | StringRef BaseName = llvm::sys::path::filename(path: BaseInput); |
6244 | StringRef NameArg; |
6245 | if (Arg *A = C.getArgs().getLastArg(Ids: options::OPT__SLASH_Fi)) |
6246 | NameArg = A->getValue(); |
6247 | return C.addResultFile( |
6248 | Name: MakeCLOutputFilename(Args: C.getArgs(), ArgValue: NameArg, BaseName, FileType: types::TY_PP_C), |
6249 | JA: &JA); |
6250 | } |
6251 | |
6252 | // Default to writing to stdout? |
6253 | if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput(JA)) { |
6254 | return "-" ; |
6255 | } |
6256 | |
6257 | if (JA.getType() == types::TY_ModuleFile && |
6258 | C.getArgs().getLastArg(Ids: options::OPT_module_file_info)) { |
6259 | return "-" ; |
6260 | } |
6261 | |
6262 | if (JA.getType() == types::TY_PP_Asm && |
6263 | C.getArgs().hasArg(Ids: options::OPT_dxc_Fc)) { |
6264 | StringRef FcValue = C.getArgs().getLastArgValue(Id: options::OPT_dxc_Fc); |
6265 | // TODO: Should we use `MakeCLOutputFilename` here? If so, we can probably |
6266 | // handle this as part of the SLASH_Fa handling below. |
6267 | return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FcValue.str()), JA: &JA); |
6268 | } |
6269 | |
6270 | if ((JA.getType() == types::TY_Object && |
6271 | C.getArgs().hasArg(Ids: options::OPT_dxc_Fo)) || |
6272 | JA.getType() == types::TY_DX_CONTAINER) { |
6273 | StringRef FoValue = C.getArgs().getLastArgValue(Id: options::OPT_dxc_Fo); |
6274 | // If we are targeting DXIL and not validating or translating, we should set |
6275 | // the final result file. Otherwise we should emit to a temporary. |
6276 | if (C.getDefaultToolChain().getTriple().isDXIL()) { |
6277 | const auto &TC = static_cast<const toolchains::HLSLToolChain &>( |
6278 | C.getDefaultToolChain()); |
6279 | // Fo can be empty here if the validator is running for a compiler flow |
6280 | // that is using Fc or just printing disassembly. |
6281 | if (TC.isLastJob(Args&: C.getArgs(), AC: JA.getKind()) && !FoValue.empty()) |
6282 | return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FoValue.str()), JA: &JA); |
6283 | StringRef Name = llvm::sys::path::filename(path: BaseInput); |
6284 | std::pair<StringRef, StringRef> Split = Name.split(Separator: '.'); |
6285 | const char *Suffix = types::getTypeTempSuffix(Id: JA.getType(), CLStyle: true); |
6286 | return CreateTempFile(C, Prefix: Split.first, Suffix, MultipleArchs: false); |
6287 | } |
6288 | // We don't have SPIRV-val integrated (yet), so for now we can assume this |
6289 | // is the final output. |
6290 | assert(C.getDefaultToolChain().getTriple().isSPIRV()); |
6291 | return C.addResultFile(Name: C.getArgs().MakeArgString(Str: FoValue.str()), JA: &JA); |
6292 | } |
6293 | |
6294 | // Is this the assembly listing for /FA? |
6295 | if (JA.getType() == types::TY_PP_Asm && |
6296 | (C.getArgs().hasArg(Ids: options::OPT__SLASH_FA) || |
6297 | C.getArgs().hasArg(Ids: options::OPT__SLASH_Fa))) { |
6298 | // Use /Fa and the input filename to determine the asm file name. |
6299 | StringRef BaseName = llvm::sys::path::filename(path: BaseInput); |
6300 | StringRef FaValue = C.getArgs().getLastArgValue(Id: options::OPT__SLASH_Fa); |
6301 | return C.addResultFile( |
6302 | Name: MakeCLOutputFilename(Args: C.getArgs(), ArgValue: FaValue, BaseName, FileType: JA.getType()), |
6303 | JA: &JA); |
6304 | } |
6305 | |
6306 | if (JA.getType() == types::TY_API_INFO && |
6307 | C.getArgs().hasArg(Ids: options::OPT_emit_extension_symbol_graphs) && |
6308 | C.getArgs().hasArg(Ids: options::OPT_o)) |
6309 | Diag(DiagID: clang::diag::err_drv_unexpected_symbol_graph_output) |
6310 | << C.getArgs().getLastArgValue(Id: options::OPT_o); |
6311 | |
6312 | // DXC defaults to standard out when generating assembly. We check this after |
6313 | // any DXC flags that might specify a file. |
6314 | if (AtTopLevel && JA.getType() == types::TY_PP_Asm && IsDXCMode()) |
6315 | return "-" ; |
6316 | |
6317 | bool SpecifiedModuleOutput = |
6318 | C.getArgs().hasArg(Ids: options::OPT_fmodule_output) || |
6319 | C.getArgs().hasArg(Ids: options::OPT_fmodule_output_EQ); |
6320 | if (MultipleArchs && SpecifiedModuleOutput) |
6321 | Diag(DiagID: clang::diag::err_drv_module_output_with_multiple_arch); |
6322 | |
6323 | // If we're emitting a module output with the specified option |
6324 | // `-fmodule-output`. |
6325 | if (!AtTopLevel && isa<PrecompileJobAction>(Val: JA) && |
6326 | JA.getType() == types::TY_ModuleFile && SpecifiedModuleOutput) { |
6327 | assert(!C.getArgs().hasArg(options::OPT_modules_reduced_bmi)); |
6328 | return GetModuleOutputPath(C, JA, BaseInput); |
6329 | } |
6330 | |
6331 | // Output to a temporary file? |
6332 | if ((!AtTopLevel && !isSaveTempsEnabled() && |
6333 | !C.getArgs().hasArg(Ids: options::OPT__SLASH_Fo)) || |
6334 | CCGenDiagnostics) { |
6335 | StringRef Name = llvm::sys::path::filename(path: BaseInput); |
6336 | std::pair<StringRef, StringRef> Split = Name.split(Separator: '.'); |
6337 | const char *Suffix = |
6338 | types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode()); |
6339 | // The non-offloading toolchain on Darwin requires deterministic input |
6340 | // file name for binaries to be deterministic, therefore it needs unique |
6341 | // directory. |
6342 | llvm::Triple Triple(C.getDriver().getTargetTriple()); |
6343 | bool NeedUniqueDirectory = |
6344 | (JA.getOffloadingDeviceKind() == Action::OFK_None || |
6345 | JA.getOffloadingDeviceKind() == Action::OFK_Host) && |
6346 | Triple.isOSDarwin(); |
6347 | return CreateTempFile(C, Prefix: Split.first, Suffix, MultipleArchs, BoundArch, |
6348 | NeedUniqueDirectory); |
6349 | } |
6350 | |
6351 | SmallString<128> BasePath(BaseInput); |
6352 | SmallString<128> ExternalPath("" ); |
6353 | StringRef BaseName; |
6354 | |
6355 | // Dsymutil actions should use the full path. |
6356 | if (isa<DsymutilJobAction>(Val: JA) && C.getArgs().hasArg(Ids: options::OPT_dsym_dir)) { |
6357 | ExternalPath += C.getArgs().getLastArg(Ids: options::OPT_dsym_dir)->getValue(); |
6358 | // We use posix style here because the tests (specifically |
6359 | // darwin-dsymutil.c) demonstrate that posix style paths are acceptable |
6360 | // even on Windows and if we don't then the similar test covering this |
6361 | // fails. |
6362 | llvm::sys::path::append(path&: ExternalPath, style: llvm::sys::path::Style::posix, |
6363 | a: llvm::sys::path::filename(path: BasePath)); |
6364 | BaseName = ExternalPath; |
6365 | } else if (isa<DsymutilJobAction>(Val: JA) || isa<VerifyJobAction>(Val: JA)) |
6366 | BaseName = BasePath; |
6367 | else |
6368 | BaseName = llvm::sys::path::filename(path: BasePath); |
6369 | |
6370 | // Determine what the derived output name should be. |
6371 | const char *NamedOutput; |
6372 | |
6373 | if ((JA.getType() == types::TY_Object || JA.getType() == types::TY_LTO_BC) && |
6374 | C.getArgs().hasArg(Ids: options::OPT__SLASH_Fo, Ids: options::OPT__SLASH_o)) { |
6375 | // The /Fo or /o flag decides the object filename. |
6376 | StringRef Val = |
6377 | C.getArgs() |
6378 | .getLastArg(Ids: options::OPT__SLASH_Fo, Ids: options::OPT__SLASH_o) |
6379 | ->getValue(); |
6380 | NamedOutput = |
6381 | MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Object); |
6382 | } else if (JA.getType() == types::TY_Image && |
6383 | C.getArgs().hasArg(Ids: options::OPT__SLASH_Fe, |
6384 | Ids: options::OPT__SLASH_o)) { |
6385 | // The /Fe or /o flag names the linked file. |
6386 | StringRef Val = |
6387 | C.getArgs() |
6388 | .getLastArg(Ids: options::OPT__SLASH_Fe, Ids: options::OPT__SLASH_o) |
6389 | ->getValue(); |
6390 | NamedOutput = |
6391 | MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Image); |
6392 | } else if (JA.getType() == types::TY_Image) { |
6393 | if (IsCLMode()) { |
6394 | // clang-cl uses BaseName for the executable name. |
6395 | NamedOutput = |
6396 | MakeCLOutputFilename(Args: C.getArgs(), ArgValue: "" , BaseName, FileType: types::TY_Image); |
6397 | } else { |
6398 | SmallString<128> Output(getDefaultImageName()); |
6399 | // HIP image for device compilation with -fno-gpu-rdc is per compilation |
6400 | // unit. |
6401 | bool IsHIPNoRDC = JA.getOffloadingDeviceKind() == Action::OFK_HIP && |
6402 | !C.getArgs().hasFlag(Pos: options::OPT_fgpu_rdc, |
6403 | Neg: options::OPT_fno_gpu_rdc, Default: false); |
6404 | bool UseOutExtension = IsHIPNoRDC || isa<OffloadPackagerJobAction>(Val: JA); |
6405 | if (UseOutExtension) { |
6406 | Output = BaseName; |
6407 | llvm::sys::path::replace_extension(path&: Output, extension: "" ); |
6408 | } |
6409 | Output += OffloadingPrefix; |
6410 | if (MultipleArchs && !BoundArch.empty()) { |
6411 | Output += "-" ; |
6412 | Output.append(RHS: BoundArch); |
6413 | } |
6414 | if (UseOutExtension) |
6415 | Output += ".out" ; |
6416 | NamedOutput = C.getArgs().MakeArgString(Str: Output.c_str()); |
6417 | } |
6418 | } else if (JA.getType() == types::TY_PCH && IsCLMode()) { |
6419 | NamedOutput = C.getArgs().MakeArgString(Str: GetClPchPath(C, BaseName)); |
6420 | } else if ((JA.getType() == types::TY_Plist || JA.getType() == types::TY_AST) && |
6421 | C.getArgs().hasArg(Ids: options::OPT__SLASH_o)) { |
6422 | StringRef Val = |
6423 | C.getArgs() |
6424 | .getLastArg(Ids: options::OPT__SLASH_o) |
6425 | ->getValue(); |
6426 | NamedOutput = |
6427 | MakeCLOutputFilename(Args: C.getArgs(), ArgValue: Val, BaseName, FileType: types::TY_Object); |
6428 | } else { |
6429 | const char *Suffix = |
6430 | types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode()); |
6431 | assert(Suffix && "All types used for output should have a suffix." ); |
6432 | |
6433 | std::string::size_type End = std::string::npos; |
6434 | if (!types::appendSuffixForType(Id: JA.getType())) |
6435 | End = BaseName.rfind(C: '.'); |
6436 | SmallString<128> Suffixed(BaseName.substr(Start: 0, N: End)); |
6437 | Suffixed += OffloadingPrefix; |
6438 | if (MultipleArchs && !BoundArch.empty()) { |
6439 | Suffixed += "-" ; |
6440 | Suffixed.append(RHS: BoundArch); |
6441 | } |
6442 | // When using both -save-temps and -emit-llvm, use a ".tmp.bc" suffix for |
6443 | // the unoptimized bitcode so that it does not get overwritten by the ".bc" |
6444 | // optimized bitcode output. |
6445 | auto IsAMDRDCInCompilePhase = [](const JobAction &JA, |
6446 | const llvm::opt::DerivedArgList &Args) { |
6447 | // The relocatable compilation in HIP and OpenMP implies -emit-llvm. |
6448 | // Similarly, use a ".tmp.bc" suffix for the unoptimized bitcode |
6449 | // (generated in the compile phase.) |
6450 | const ToolChain *TC = JA.getOffloadingToolChain(); |
6451 | return isa<CompileJobAction>(Val: JA) && |
6452 | ((JA.getOffloadingDeviceKind() == Action::OFK_HIP && |
6453 | Args.hasFlag(Pos: options::OPT_fgpu_rdc, Neg: options::OPT_fno_gpu_rdc, |
6454 | Default: false)) || |
6455 | (JA.getOffloadingDeviceKind() == Action::OFK_OpenMP && TC && |
6456 | TC->getTriple().isAMDGPU())); |
6457 | }; |
6458 | if (!AtTopLevel && JA.getType() == types::TY_LLVM_BC && |
6459 | (C.getArgs().hasArg(Ids: options::OPT_emit_llvm) || |
6460 | IsAMDRDCInCompilePhase(JA, C.getArgs()))) |
6461 | Suffixed += ".tmp" ; |
6462 | Suffixed += '.'; |
6463 | Suffixed += Suffix; |
6464 | NamedOutput = C.getArgs().MakeArgString(Str: Suffixed.c_str()); |
6465 | } |
6466 | |
6467 | // Prepend object file path if -save-temps=obj |
6468 | if (!AtTopLevel && isSaveTempsObj() && C.getArgs().hasArg(Ids: options::OPT_o) && |
6469 | JA.getType() != types::TY_PCH) { |
6470 | Arg *FinalOutput = C.getArgs().getLastArg(Ids: options::OPT_o); |
6471 | SmallString<128> TempPath(FinalOutput->getValue()); |
6472 | llvm::sys::path::remove_filename(path&: TempPath); |
6473 | StringRef OutputFileName = llvm::sys::path::filename(path: NamedOutput); |
6474 | llvm::sys::path::append(path&: TempPath, a: OutputFileName); |
6475 | NamedOutput = C.getArgs().MakeArgString(Str: TempPath.c_str()); |
6476 | } |
6477 | |
6478 | // If we're saving temps and the temp file conflicts with the input file, |
6479 | // then avoid overwriting input file. |
6480 | if (!AtTopLevel && isSaveTempsEnabled() && NamedOutput == BaseName) { |
6481 | bool SameFile = false; |
6482 | SmallString<256> Result; |
6483 | llvm::sys::fs::current_path(result&: Result); |
6484 | llvm::sys::path::append(path&: Result, a: BaseName); |
6485 | llvm::sys::fs::equivalent(A: BaseInput, B: Result.c_str(), result&: SameFile); |
6486 | // Must share the same path to conflict. |
6487 | if (SameFile) { |
6488 | StringRef Name = llvm::sys::path::filename(path: BaseInput); |
6489 | std::pair<StringRef, StringRef> Split = Name.split(Separator: '.'); |
6490 | std::string TmpName = GetTemporaryPath( |
6491 | Prefix: Split.first, |
6492 | Suffix: types::getTypeTempSuffix(Id: JA.getType(), CLStyle: IsCLMode() || IsDXCMode())); |
6493 | return C.addTempFile(Name: C.getArgs().MakeArgString(Str: TmpName)); |
6494 | } |
6495 | } |
6496 | |
6497 | // As an annoying special case, PCH generation doesn't strip the pathname. |
6498 | if (JA.getType() == types::TY_PCH && !IsCLMode()) { |
6499 | llvm::sys::path::remove_filename(path&: BasePath); |
6500 | if (BasePath.empty()) |
6501 | BasePath = NamedOutput; |
6502 | else |
6503 | llvm::sys::path::append(path&: BasePath, a: NamedOutput); |
6504 | return C.addResultFile(Name: C.getArgs().MakeArgString(Str: BasePath.c_str()), JA: &JA); |
6505 | } |
6506 | |
6507 | return C.addResultFile(Name: NamedOutput, JA: &JA); |
6508 | } |
6509 | |
6510 | std::string Driver::GetFilePath(StringRef Name, const ToolChain &TC) const { |
6511 | // Search for Name in a list of paths. |
6512 | auto SearchPaths = [&](const llvm::SmallVectorImpl<std::string> &P) |
6513 | -> std::optional<std::string> { |
6514 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
6515 | // attempting to use this prefix when looking for file paths. |
6516 | for (const auto &Dir : P) { |
6517 | if (Dir.empty()) |
6518 | continue; |
6519 | SmallString<128> P(Dir[0] == '=' ? SysRoot + Dir.substr(pos: 1) : Dir); |
6520 | llvm::sys::path::append(path&: P, a: Name); |
6521 | if (llvm::sys::fs::exists(Path: Twine(P))) |
6522 | return std::string(P); |
6523 | } |
6524 | return std::nullopt; |
6525 | }; |
6526 | |
6527 | if (auto P = SearchPaths(PrefixDirs)) |
6528 | return *P; |
6529 | |
6530 | SmallString<128> R(ResourceDir); |
6531 | llvm::sys::path::append(path&: R, a: Name); |
6532 | if (llvm::sys::fs::exists(Path: Twine(R))) |
6533 | return std::string(R); |
6534 | |
6535 | SmallString<128> P(TC.getCompilerRTPath()); |
6536 | llvm::sys::path::append(path&: P, a: Name); |
6537 | if (llvm::sys::fs::exists(Path: Twine(P))) |
6538 | return std::string(P); |
6539 | |
6540 | SmallString<128> D(Dir); |
6541 | llvm::sys::path::append(path&: D, a: ".." , b: Name); |
6542 | if (llvm::sys::fs::exists(Path: Twine(D))) |
6543 | return std::string(D); |
6544 | |
6545 | if (auto P = SearchPaths(TC.getLibraryPaths())) |
6546 | return *P; |
6547 | |
6548 | if (auto P = SearchPaths(TC.getFilePaths())) |
6549 | return *P; |
6550 | |
6551 | SmallString<128> R2(ResourceDir); |
6552 | llvm::sys::path::append(path&: R2, a: ".." , b: ".." , c: Name); |
6553 | if (llvm::sys::fs::exists(Path: Twine(R2))) |
6554 | return std::string(R2); |
6555 | |
6556 | return std::string(Name); |
6557 | } |
6558 | |
6559 | void Driver::generatePrefixedToolNames( |
6560 | StringRef Tool, const ToolChain &TC, |
6561 | SmallVectorImpl<std::string> &Names) const { |
6562 | // FIXME: Needs a better variable than TargetTriple |
6563 | Names.emplace_back(Args: (TargetTriple + "-" + Tool).str()); |
6564 | Names.emplace_back(Args&: Tool); |
6565 | } |
6566 | |
6567 | static bool ScanDirForExecutable(SmallString<128> &Dir, StringRef Name) { |
6568 | llvm::sys::path::append(path&: Dir, a: Name); |
6569 | if (llvm::sys::fs::can_execute(Path: Twine(Dir))) |
6570 | return true; |
6571 | llvm::sys::path::remove_filename(path&: Dir); |
6572 | return false; |
6573 | } |
6574 | |
6575 | std::string Driver::GetProgramPath(StringRef Name, const ToolChain &TC) const { |
6576 | SmallVector<std::string, 2> TargetSpecificExecutables; |
6577 | generatePrefixedToolNames(Tool: Name, TC, Names&: TargetSpecificExecutables); |
6578 | |
6579 | // Respect a limited subset of the '-Bprefix' functionality in GCC by |
6580 | // attempting to use this prefix when looking for program paths. |
6581 | for (const auto &PrefixDir : PrefixDirs) { |
6582 | if (llvm::sys::fs::is_directory(Path: PrefixDir)) { |
6583 | SmallString<128> P(PrefixDir); |
6584 | if (ScanDirForExecutable(Dir&: P, Name)) |
6585 | return std::string(P); |
6586 | } else { |
6587 | SmallString<128> P((PrefixDir + Name).str()); |
6588 | if (llvm::sys::fs::can_execute(Path: Twine(P))) |
6589 | return std::string(P); |
6590 | } |
6591 | } |
6592 | |
6593 | const ToolChain::path_list &List = TC.getProgramPaths(); |
6594 | for (const auto &TargetSpecificExecutable : TargetSpecificExecutables) { |
6595 | // For each possible name of the tool look for it in |
6596 | // program paths first, then the path. |
6597 | // Higher priority names will be first, meaning that |
6598 | // a higher priority name in the path will be found |
6599 | // instead of a lower priority name in the program path. |
6600 | // E.g. <triple>-gcc on the path will be found instead |
6601 | // of gcc in the program path |
6602 | for (const auto &Path : List) { |
6603 | SmallString<128> P(Path); |
6604 | if (ScanDirForExecutable(Dir&: P, Name: TargetSpecificExecutable)) |
6605 | return std::string(P); |
6606 | } |
6607 | |
6608 | // Fall back to the path |
6609 | if (llvm::ErrorOr<std::string> P = |
6610 | llvm::sys::findProgramByName(Name: TargetSpecificExecutable)) |
6611 | return *P; |
6612 | } |
6613 | |
6614 | return std::string(Name); |
6615 | } |
6616 | |
6617 | std::string Driver::GetStdModuleManifestPath(const Compilation &C, |
6618 | const ToolChain &TC) const { |
6619 | std::string error = "<NOT PRESENT>" ; |
6620 | |
6621 | switch (TC.GetCXXStdlibType(Args: C.getArgs())) { |
6622 | case ToolChain::CST_Libcxx: { |
6623 | auto evaluate = [&](const char *library) -> std::optional<std::string> { |
6624 | std::string lib = GetFilePath(Name: library, TC); |
6625 | |
6626 | // Note when there are multiple flavours of libc++ the module json needs |
6627 | // to look at the command-line arguments for the proper json. These |
6628 | // flavours do not exist at the moment, but there are plans to provide a |
6629 | // variant that is built with sanitizer instrumentation enabled. |
6630 | |
6631 | // For example |
6632 | // StringRef modules = [&] { |
6633 | // const SanitizerArgs &Sanitize = TC.getSanitizerArgs(C.getArgs()); |
6634 | // if (Sanitize.needsAsanRt()) |
6635 | // return "libc++.modules-asan.json"; |
6636 | // return "libc++.modules.json"; |
6637 | // }(); |
6638 | |
6639 | SmallString<128> path(lib.begin(), lib.end()); |
6640 | llvm::sys::path::remove_filename(path); |
6641 | llvm::sys::path::append(path, a: "libc++.modules.json" ); |
6642 | if (TC.getVFS().exists(Path: path)) |
6643 | return static_cast<std::string>(path); |
6644 | |
6645 | return {}; |
6646 | }; |
6647 | |
6648 | if (std::optional<std::string> result = evaluate("libc++.so" ); result) |
6649 | return *result; |
6650 | |
6651 | return evaluate("libc++.a" ).value_or(u&: error); |
6652 | } |
6653 | |
6654 | case ToolChain::CST_Libstdcxx: { |
6655 | auto evaluate = [&](const char *library) -> std::optional<std::string> { |
6656 | std::string lib = GetFilePath(Name: library, TC); |
6657 | |
6658 | SmallString<128> path(lib.begin(), lib.end()); |
6659 | llvm::sys::path::remove_filename(path); |
6660 | llvm::sys::path::append(path, a: "libstdc++.modules.json" ); |
6661 | if (TC.getVFS().exists(Path: path)) |
6662 | return static_cast<std::string>(path); |
6663 | |
6664 | return {}; |
6665 | }; |
6666 | |
6667 | if (std::optional<std::string> result = evaluate("libstdc++.so" ); result) |
6668 | return *result; |
6669 | |
6670 | return evaluate("libstdc++.a" ).value_or(u&: error); |
6671 | } |
6672 | } |
6673 | |
6674 | return error; |
6675 | } |
6676 | |
6677 | std::string Driver::GetTemporaryPath(StringRef Prefix, StringRef Suffix) const { |
6678 | SmallString<128> Path; |
6679 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, ResultPath&: Path); |
6680 | if (EC) { |
6681 | Diag(DiagID: clang::diag::err_unable_to_make_temp) << EC.message(); |
6682 | return "" ; |
6683 | } |
6684 | |
6685 | return std::string(Path); |
6686 | } |
6687 | |
6688 | std::string Driver::GetTemporaryDirectory(StringRef Prefix) const { |
6689 | SmallString<128> Path; |
6690 | std::error_code EC = llvm::sys::fs::createUniqueDirectory(Prefix, ResultPath&: Path); |
6691 | if (EC) { |
6692 | Diag(DiagID: clang::diag::err_unable_to_make_temp) << EC.message(); |
6693 | return "" ; |
6694 | } |
6695 | |
6696 | return std::string(Path); |
6697 | } |
6698 | |
6699 | std::string Driver::GetClPchPath(Compilation &C, StringRef BaseName) const { |
6700 | SmallString<128> Output; |
6701 | if (Arg *FpArg = C.getArgs().getLastArg(Ids: options::OPT__SLASH_Fp)) { |
6702 | // FIXME: If anybody needs it, implement this obscure rule: |
6703 | // "If you specify a directory without a file name, the default file name |
6704 | // is VCx0.pch., where x is the major version of Visual C++ in use." |
6705 | Output = FpArg->getValue(); |
6706 | |
6707 | // "If you do not specify an extension as part of the path name, an |
6708 | // extension of .pch is assumed. " |
6709 | if (!llvm::sys::path::has_extension(path: Output)) |
6710 | Output += ".pch" ; |
6711 | } else { |
6712 | if (Arg *YcArg = C.getArgs().getLastArg(Ids: options::OPT__SLASH_Yc)) |
6713 | Output = YcArg->getValue(); |
6714 | if (Output.empty()) |
6715 | Output = BaseName; |
6716 | llvm::sys::path::replace_extension(path&: Output, extension: ".pch" ); |
6717 | } |
6718 | return std::string(Output); |
6719 | } |
6720 | |
6721 | const ToolChain &Driver::getOffloadToolChain( |
6722 | const llvm::opt::ArgList &Args, const Action::OffloadKind Kind, |
6723 | const llvm::Triple &Target, const llvm::Triple &AuxTarget) const { |
6724 | std::unique_ptr<ToolChain> &TC = |
6725 | ToolChains[Target.str() + "/" + AuxTarget.str()]; |
6726 | std::unique_ptr<ToolChain> &HostTC = ToolChains[AuxTarget.str()]; |
6727 | |
6728 | assert(HostTC && "Host toolchain for offloading doesn't exit?" ); |
6729 | if (!TC) { |
6730 | // Detect the toolchain based off of the target operating system. |
6731 | switch (Target.getOS()) { |
6732 | case llvm::Triple::CUDA: |
6733 | TC = std::make_unique<toolchains::CudaToolChain>(args: *this, args: Target, args&: *HostTC, |
6734 | args: Args); |
6735 | break; |
6736 | case llvm::Triple::AMDHSA: |
6737 | if (Kind == Action::OFK_HIP) |
6738 | TC = std::make_unique<toolchains::HIPAMDToolChain>(args: *this, args: Target, |
6739 | args&: *HostTC, args: Args); |
6740 | else if (Kind == Action::OFK_OpenMP) |
6741 | TC = std::make_unique<toolchains::AMDGPUOpenMPToolChain>(args: *this, args: Target, |
6742 | args&: *HostTC, args: Args); |
6743 | break; |
6744 | default: |
6745 | break; |
6746 | } |
6747 | } |
6748 | if (!TC) { |
6749 | // Detect the toolchain based off of the target architecture if that failed. |
6750 | switch (Target.getArch()) { |
6751 | case llvm::Triple::spir: |
6752 | case llvm::Triple::spir64: |
6753 | case llvm::Triple::spirv: |
6754 | case llvm::Triple::spirv32: |
6755 | case llvm::Triple::spirv64: |
6756 | switch (Kind) { |
6757 | case Action::OFK_SYCL: |
6758 | TC = std::make_unique<toolchains::SYCLToolChain>(args: *this, args: Target, args&: *HostTC, |
6759 | args: Args); |
6760 | break; |
6761 | case Action::OFK_HIP: |
6762 | TC = std::make_unique<toolchains::HIPSPVToolChain>(args: *this, args: Target, |
6763 | args&: *HostTC, args: Args); |
6764 | break; |
6765 | case Action::OFK_OpenMP: |
6766 | TC = std::make_unique<toolchains::SPIRVOpenMPToolChain>(args: *this, args: Target, |
6767 | args&: *HostTC, args: Args); |
6768 | break; |
6769 | case Action::OFK_Cuda: |
6770 | TC = std::make_unique<toolchains::CudaToolChain>(args: *this, args: Target, args&: *HostTC, |
6771 | args: Args); |
6772 | break; |
6773 | default: |
6774 | break; |
6775 | } |
6776 | break; |
6777 | default: |
6778 | break; |
6779 | } |
6780 | } |
6781 | |
6782 | // If all else fails, just look up the normal toolchain for the target. |
6783 | if (!TC) |
6784 | return getToolChain(Args, Target); |
6785 | return *TC; |
6786 | } |
6787 | |
6788 | const ToolChain &Driver::getToolChain(const ArgList &Args, |
6789 | const llvm::Triple &Target) const { |
6790 | |
6791 | auto &TC = ToolChains[Target.str()]; |
6792 | if (!TC) { |
6793 | switch (Target.getOS()) { |
6794 | case llvm::Triple::AIX: |
6795 | TC = std::make_unique<toolchains::AIX>(args: *this, args: Target, args: Args); |
6796 | break; |
6797 | case llvm::Triple::Haiku: |
6798 | TC = std::make_unique<toolchains::Haiku>(args: *this, args: Target, args: Args); |
6799 | break; |
6800 | case llvm::Triple::Darwin: |
6801 | case llvm::Triple::MacOSX: |
6802 | case llvm::Triple::IOS: |
6803 | case llvm::Triple::TvOS: |
6804 | case llvm::Triple::WatchOS: |
6805 | case llvm::Triple::XROS: |
6806 | case llvm::Triple::DriverKit: |
6807 | TC = std::make_unique<toolchains::DarwinClang>(args: *this, args: Target, args: Args); |
6808 | break; |
6809 | case llvm::Triple::DragonFly: |
6810 | TC = std::make_unique<toolchains::DragonFly>(args: *this, args: Target, args: Args); |
6811 | break; |
6812 | case llvm::Triple::OpenBSD: |
6813 | TC = std::make_unique<toolchains::OpenBSD>(args: *this, args: Target, args: Args); |
6814 | break; |
6815 | case llvm::Triple::NetBSD: |
6816 | TC = std::make_unique<toolchains::NetBSD>(args: *this, args: Target, args: Args); |
6817 | break; |
6818 | case llvm::Triple::FreeBSD: |
6819 | if (Target.isPPC()) |
6820 | TC = std::make_unique<toolchains::PPCFreeBSDToolChain>(args: *this, args: Target, |
6821 | args: Args); |
6822 | else |
6823 | TC = std::make_unique<toolchains::FreeBSD>(args: *this, args: Target, args: Args); |
6824 | break; |
6825 | case llvm::Triple::Linux: |
6826 | case llvm::Triple::ELFIAMCU: |
6827 | if (Target.getArch() == llvm::Triple::hexagon) |
6828 | TC = std::make_unique<toolchains::HexagonToolChain>(args: *this, args: Target, |
6829 | args: Args); |
6830 | else if ((Target.getVendor() == llvm::Triple::MipsTechnologies) && |
6831 | !Target.hasEnvironment()) |
6832 | TC = std::make_unique<toolchains::MipsLLVMToolChain>(args: *this, args: Target, |
6833 | args: Args); |
6834 | else if (Target.isPPC()) |
6835 | TC = std::make_unique<toolchains::PPCLinuxToolChain>(args: *this, args: Target, |
6836 | args: Args); |
6837 | else if (Target.getArch() == llvm::Triple::ve) |
6838 | TC = std::make_unique<toolchains::VEToolChain>(args: *this, args: Target, args: Args); |
6839 | else if (Target.isOHOSFamily()) |
6840 | TC = std::make_unique<toolchains::OHOS>(args: *this, args: Target, args: Args); |
6841 | else |
6842 | TC = std::make_unique<toolchains::Linux>(args: *this, args: Target, args: Args); |
6843 | break; |
6844 | case llvm::Triple::NaCl: |
6845 | TC = std::make_unique<toolchains::NaClToolChain>(args: *this, args: Target, args: Args); |
6846 | break; |
6847 | case llvm::Triple::Fuchsia: |
6848 | TC = std::make_unique<toolchains::Fuchsia>(args: *this, args: Target, args: Args); |
6849 | break; |
6850 | case llvm::Triple::Managarm: |
6851 | TC = std::make_unique<toolchains::Managarm>(args: *this, args: Target, args: Args); |
6852 | break; |
6853 | case llvm::Triple::Solaris: |
6854 | TC = std::make_unique<toolchains::Solaris>(args: *this, args: Target, args: Args); |
6855 | break; |
6856 | case llvm::Triple::CUDA: |
6857 | TC = std::make_unique<toolchains::NVPTXToolChain>(args: *this, args: Target, args: Args); |
6858 | break; |
6859 | case llvm::Triple::AMDHSA: { |
6860 | if (Target.getArch() == llvm::Triple::spirv64) { |
6861 | TC = std::make_unique<toolchains::SPIRVAMDToolChain>(args: *this, args: Target, |
6862 | args: Args); |
6863 | } else { |
6864 | bool DL = usesInput(Args, Fn&: types::isOpenCL) || |
6865 | usesInput(Args, Fn&: types::isLLVMIR); |
6866 | TC = DL ? std::make_unique<toolchains::ROCMToolChain>(args: *this, args: Target, |
6867 | args: Args) |
6868 | : std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, |
6869 | args: Args); |
6870 | } |
6871 | break; |
6872 | } |
6873 | case llvm::Triple::AMDPAL: |
6874 | case llvm::Triple::Mesa3D: |
6875 | TC = std::make_unique<toolchains::AMDGPUToolChain>(args: *this, args: Target, args: Args); |
6876 | break; |
6877 | case llvm::Triple::UEFI: |
6878 | TC = std::make_unique<toolchains::UEFI>(args: *this, args: Target, args: Args); |
6879 | break; |
6880 | case llvm::Triple::Win32: |
6881 | switch (Target.getEnvironment()) { |
6882 | default: |
6883 | if (Target.isOSBinFormatELF()) |
6884 | TC = std::make_unique<toolchains::Generic_ELF>(args: *this, args: Target, args: Args); |
6885 | else if (Target.isOSBinFormatMachO()) |
6886 | TC = std::make_unique<toolchains::MachO>(args: *this, args: Target, args: Args); |
6887 | else |
6888 | TC = std::make_unique<toolchains::Generic_GCC>(args: *this, args: Target, args: Args); |
6889 | break; |
6890 | case llvm::Triple::GNU: |
6891 | TC = std::make_unique<toolchains::MinGW>(args: *this, args: Target, args: Args); |
6892 | break; |
6893 | case llvm::Triple::Cygnus: |
6894 | TC = std::make_unique<toolchains::Cygwin>(args: *this, args: Target, args: Args); |
6895 | break; |
6896 | case llvm::Triple::Itanium: |
6897 | TC = std::make_unique<toolchains::CrossWindowsToolChain>(args: *this, args: Target, |
6898 | args: Args); |
6899 | break; |
6900 | case llvm::Triple::MSVC: |
6901 | case llvm::Triple::UnknownEnvironment: |
6902 | if (Args.getLastArgValue(Id: options::OPT_fuse_ld_EQ) |
6903 | .starts_with_insensitive(Prefix: "bfd" )) |
6904 | TC = std::make_unique<toolchains::CrossWindowsToolChain>( |
6905 | args: *this, args: Target, args: Args); |
6906 | else |
6907 | TC = |
6908 | std::make_unique<toolchains::MSVCToolChain>(args: *this, args: Target, args: Args); |
6909 | break; |
6910 | } |
6911 | break; |
6912 | case llvm::Triple::PS4: |
6913 | TC = std::make_unique<toolchains::PS4CPU>(args: *this, args: Target, args: Args); |
6914 | break; |
6915 | case llvm::Triple::PS5: |
6916 | TC = std::make_unique<toolchains::PS5CPU>(args: *this, args: Target, args: Args); |
6917 | break; |
6918 | case llvm::Triple::Hurd: |
6919 | TC = std::make_unique<toolchains::Hurd>(args: *this, args: Target, args: Args); |
6920 | break; |
6921 | case llvm::Triple::LiteOS: |
6922 | TC = std::make_unique<toolchains::OHOS>(args: *this, args: Target, args: Args); |
6923 | break; |
6924 | case llvm::Triple::ZOS: |
6925 | TC = std::make_unique<toolchains::ZOS>(args: *this, args: Target, args: Args); |
6926 | break; |
6927 | case llvm::Triple::Vulkan: |
6928 | case llvm::Triple::ShaderModel: |
6929 | TC = std::make_unique<toolchains::HLSLToolChain>(args: *this, args: Target, args: Args); |
6930 | break; |
6931 | default: |
6932 | // Of these targets, Hexagon is the only one that might have |
6933 | // an OS of Linux, in which case it got handled above already. |
6934 | switch (Target.getArch()) { |
6935 | case llvm::Triple::tce: |
6936 | TC = std::make_unique<toolchains::TCEToolChain>(args: *this, args: Target, args: Args); |
6937 | break; |
6938 | case llvm::Triple::tcele: |
6939 | TC = std::make_unique<toolchains::TCELEToolChain>(args: *this, args: Target, args: Args); |
6940 | break; |
6941 | case llvm::Triple::hexagon: |
6942 | TC = std::make_unique<toolchains::HexagonToolChain>(args: *this, args: Target, |
6943 | args: Args); |
6944 | break; |
6945 | case llvm::Triple::lanai: |
6946 | TC = std::make_unique<toolchains::LanaiToolChain>(args: *this, args: Target, args: Args); |
6947 | break; |
6948 | case llvm::Triple::xcore: |
6949 | TC = std::make_unique<toolchains::XCoreToolChain>(args: *this, args: Target, args: Args); |
6950 | break; |
6951 | case llvm::Triple::wasm32: |
6952 | case llvm::Triple::wasm64: |
6953 | TC = std::make_unique<toolchains::WebAssembly>(args: *this, args: Target, args: Args); |
6954 | break; |
6955 | case llvm::Triple::avr: |
6956 | TC = std::make_unique<toolchains::AVRToolChain>(args: *this, args: Target, args: Args); |
6957 | break; |
6958 | case llvm::Triple::msp430: |
6959 | TC = std::make_unique<toolchains::MSP430ToolChain>(args: *this, args: Target, args: Args); |
6960 | break; |
6961 | case llvm::Triple::riscv32: |
6962 | case llvm::Triple::riscv64: |
6963 | TC = std::make_unique<toolchains::BareMetal>(args: *this, args: Target, args: Args); |
6964 | break; |
6965 | case llvm::Triple::ve: |
6966 | TC = std::make_unique<toolchains::VEToolChain>(args: *this, args: Target, args: Args); |
6967 | break; |
6968 | case llvm::Triple::spirv32: |
6969 | case llvm::Triple::spirv64: |
6970 | TC = std::make_unique<toolchains::SPIRVToolChain>(args: *this, args: Target, args: Args); |
6971 | break; |
6972 | case llvm::Triple::csky: |
6973 | TC = std::make_unique<toolchains::CSKYToolChain>(args: *this, args: Target, args: Args); |
6974 | break; |
6975 | default: |
6976 | if (toolchains::BareMetal::handlesTarget(Triple: Target)) |
6977 | TC = std::make_unique<toolchains::BareMetal>(args: *this, args: Target, args: Args); |
6978 | else if (Target.isOSBinFormatELF()) |
6979 | TC = std::make_unique<toolchains::Generic_ELF>(args: *this, args: Target, args: Args); |
6980 | else if (Target.isAppleMachO()) |
6981 | TC = std::make_unique<toolchains::AppleMachO>(args: *this, args: Target, args: Args); |
6982 | else if (Target.isOSBinFormatMachO()) |
6983 | TC = std::make_unique<toolchains::MachO>(args: *this, args: Target, args: Args); |
6984 | else |
6985 | TC = std::make_unique<toolchains::Generic_GCC>(args: *this, args: Target, args: Args); |
6986 | } |
6987 | } |
6988 | } |
6989 | |
6990 | return *TC; |
6991 | } |
6992 | |
6993 | bool Driver::ShouldUseClangCompiler(const JobAction &JA) const { |
6994 | // Say "no" if there is not exactly one input of a type clang understands. |
6995 | if (JA.size() != 1 || |
6996 | !types::isAcceptedByClang(Id: (*JA.input_begin())->getType())) |
6997 | return false; |
6998 | |
6999 | // And say "no" if this is not a kind of action clang understands. |
7000 | if (!isa<PreprocessJobAction>(Val: JA) && !isa<PrecompileJobAction>(Val: JA) && |
7001 | !isa<CompileJobAction>(Val: JA) && !isa<BackendJobAction>(Val: JA) && |
7002 | !isa<ExtractAPIJobAction>(Val: JA)) |
7003 | return false; |
7004 | |
7005 | return true; |
7006 | } |
7007 | |
7008 | bool Driver::ShouldUseFlangCompiler(const JobAction &JA) const { |
7009 | // Say "no" if there is not exactly one input of a type flang understands. |
7010 | if (JA.size() != 1 || |
7011 | !types::isAcceptedByFlang(Id: (*JA.input_begin())->getType())) |
7012 | return false; |
7013 | |
7014 | // And say "no" if this is not a kind of action flang understands. |
7015 | if (!isa<PreprocessJobAction>(Val: JA) && !isa<PrecompileJobAction>(Val: JA) && |
7016 | !isa<CompileJobAction>(Val: JA) && !isa<BackendJobAction>(Val: JA)) |
7017 | return false; |
7018 | |
7019 | return true; |
7020 | } |
7021 | |
7022 | bool Driver::ShouldEmitStaticLibrary(const ArgList &Args) const { |
7023 | // Only emit static library if the flag is set explicitly. |
7024 | if (Args.hasArg(Ids: options::OPT_emit_static_lib)) |
7025 | return true; |
7026 | return false; |
7027 | } |
7028 | |
7029 | /// GetReleaseVersion - Parse (([0-9]+)(.([0-9]+)(.([0-9]+)?))?)? and return the |
7030 | /// grouped values as integers. Numbers which are not provided are set to 0. |
7031 | /// |
7032 | /// \return True if the entire string was parsed (9.2), or all groups were |
7033 | /// parsed (10.3.5extrastuff). |
7034 | bool Driver::GetReleaseVersion(StringRef Str, unsigned &Major, unsigned &Minor, |
7035 | unsigned &Micro, bool &) { |
7036 | HadExtra = false; |
7037 | |
7038 | Major = Minor = Micro = 0; |
7039 | if (Str.empty()) |
7040 | return false; |
7041 | |
7042 | if (Str.consumeInteger(Radix: 10, Result&: Major)) |
7043 | return false; |
7044 | if (Str.empty()) |
7045 | return true; |
7046 | if (!Str.consume_front(Prefix: "." )) |
7047 | return false; |
7048 | |
7049 | if (Str.consumeInteger(Radix: 10, Result&: Minor)) |
7050 | return false; |
7051 | if (Str.empty()) |
7052 | return true; |
7053 | if (!Str.consume_front(Prefix: "." )) |
7054 | return false; |
7055 | |
7056 | if (Str.consumeInteger(Radix: 10, Result&: Micro)) |
7057 | return false; |
7058 | if (!Str.empty()) |
7059 | HadExtra = true; |
7060 | return true; |
7061 | } |
7062 | |
7063 | /// Parse digits from a string \p Str and fulfill \p Digits with |
7064 | /// the parsed numbers. This method assumes that the max number of |
7065 | /// digits to look for is equal to Digits.size(). |
7066 | /// |
7067 | /// \return True if the entire string was parsed and there are |
7068 | /// no extra characters remaining at the end. |
7069 | bool Driver::GetReleaseVersion(StringRef Str, |
7070 | MutableArrayRef<unsigned> Digits) { |
7071 | if (Str.empty()) |
7072 | return false; |
7073 | |
7074 | unsigned CurDigit = 0; |
7075 | while (CurDigit < Digits.size()) { |
7076 | unsigned Digit; |
7077 | if (Str.consumeInteger(Radix: 10, Result&: Digit)) |
7078 | return false; |
7079 | Digits[CurDigit] = Digit; |
7080 | if (Str.empty()) |
7081 | return true; |
7082 | if (!Str.consume_front(Prefix: "." )) |
7083 | return false; |
7084 | CurDigit++; |
7085 | } |
7086 | |
7087 | // More digits than requested, bail out... |
7088 | return false; |
7089 | } |
7090 | |
7091 | llvm::opt::Visibility |
7092 | Driver::getOptionVisibilityMask(bool UseDriverMode) const { |
7093 | if (!UseDriverMode) |
7094 | return llvm::opt::Visibility(options::ClangOption); |
7095 | if (IsCLMode()) |
7096 | return llvm::opt::Visibility(options::CLOption); |
7097 | if (IsDXCMode()) |
7098 | return llvm::opt::Visibility(options::DXCOption); |
7099 | if (IsFlangMode()) { |
7100 | return llvm::opt::Visibility(options::FlangOption); |
7101 | } |
7102 | return llvm::opt::Visibility(options::ClangOption); |
7103 | } |
7104 | |
7105 | const char *Driver::getExecutableForDriverMode(DriverMode Mode) { |
7106 | switch (Mode) { |
7107 | case GCCMode: |
7108 | return "clang" ; |
7109 | case GXXMode: |
7110 | return "clang++" ; |
7111 | case CPPMode: |
7112 | return "clang-cpp" ; |
7113 | case CLMode: |
7114 | return "clang-cl" ; |
7115 | case FlangMode: |
7116 | return "flang" ; |
7117 | case DXCMode: |
7118 | return "clang-dxc" ; |
7119 | } |
7120 | |
7121 | llvm_unreachable("Unhandled Mode" ); |
7122 | } |
7123 | |
7124 | bool clang::driver::isOptimizationLevelFast(const ArgList &Args) { |
7125 | return Args.hasFlag(Pos: options::OPT_Ofast, Neg: options::OPT_O_Group, Default: false); |
7126 | } |
7127 | |
7128 | bool clang::driver::(const ArgList &Args) { |
7129 | // -fsave-optimization-record enables it. |
7130 | if (Args.hasFlag(Pos: options::OPT_fsave_optimization_record, |
7131 | Neg: options::OPT_fno_save_optimization_record, Default: false)) |
7132 | return true; |
7133 | |
7134 | // -fsave-optimization-record=<format> enables it as well. |
7135 | if (Args.hasFlag(Pos: options::OPT_fsave_optimization_record_EQ, |
7136 | Neg: options::OPT_fno_save_optimization_record, Default: false)) |
7137 | return true; |
7138 | |
7139 | // -foptimization-record-file alone enables it too. |
7140 | if (Args.hasFlag(Pos: options::OPT_foptimization_record_file_EQ, |
7141 | Neg: options::OPT_fno_save_optimization_record, Default: false)) |
7142 | return true; |
7143 | |
7144 | // -foptimization-record-passes alone enables it too. |
7145 | if (Args.hasFlag(Pos: options::OPT_foptimization_record_passes_EQ, |
7146 | Neg: options::OPT_fno_save_optimization_record, Default: false)) |
7147 | return true; |
7148 | return false; |
7149 | } |
7150 | |
7151 | llvm::StringRef clang::driver::getDriverMode(StringRef ProgName, |
7152 | ArrayRef<const char *> Args) { |
7153 | static StringRef OptName = |
7154 | getDriverOptTable().getOption(Opt: options::OPT_driver_mode).getPrefixedName(); |
7155 | llvm::StringRef Opt; |
7156 | for (StringRef Arg : Args) { |
7157 | if (!Arg.starts_with(Prefix: OptName)) |
7158 | continue; |
7159 | Opt = Arg; |
7160 | } |
7161 | if (Opt.empty()) |
7162 | Opt = ToolChain::getTargetAndModeFromProgramName(ProgName).DriverMode; |
7163 | return Opt.consume_front(Prefix: OptName) ? Opt : "" ; |
7164 | } |
7165 | |
7166 | bool driver::IsClangCL(StringRef DriverMode) { return DriverMode == "cl" ; } |
7167 | |
7168 | llvm::Error driver::expandResponseFiles(SmallVectorImpl<const char *> &Args, |
7169 | bool ClangCLMode, |
7170 | llvm::BumpPtrAllocator &Alloc, |
7171 | llvm::vfs::FileSystem *FS) { |
7172 | // Parse response files using the GNU syntax, unless we're in CL mode. There |
7173 | // are two ways to put clang in CL compatibility mode: ProgName is either |
7174 | // clang-cl or cl, or --driver-mode=cl is on the command line. The normal |
7175 | // command line parsing can't happen until after response file parsing, so we |
7176 | // have to manually search for a --driver-mode=cl argument the hard way. |
7177 | // Finally, our -cc1 tools don't care which tokenization mode we use because |
7178 | // response files written by clang will tokenize the same way in either mode. |
7179 | enum { Default, POSIX, Windows } RSPQuoting = Default; |
7180 | for (const char *F : Args) { |
7181 | if (strcmp(s1: F, s2: "--rsp-quoting=posix" ) == 0) |
7182 | RSPQuoting = POSIX; |
7183 | else if (strcmp(s1: F, s2: "--rsp-quoting=windows" ) == 0) |
7184 | RSPQuoting = Windows; |
7185 | } |
7186 | |
7187 | // Determines whether we want nullptr markers in Args to indicate response |
7188 | // files end-of-lines. We only use this for the /LINK driver argument with |
7189 | // clang-cl.exe on Windows. |
7190 | bool MarkEOLs = ClangCLMode; |
7191 | |
7192 | llvm::cl::TokenizerCallback Tokenizer; |
7193 | if (RSPQuoting == Windows || (RSPQuoting == Default && ClangCLMode)) |
7194 | Tokenizer = &llvm::cl::TokenizeWindowsCommandLine; |
7195 | else |
7196 | Tokenizer = &llvm::cl::TokenizeGNUCommandLine; |
7197 | |
7198 | if (MarkEOLs && Args.size() > 1 && StringRef(Args[1]).starts_with(Prefix: "-cc1" )) |
7199 | MarkEOLs = false; |
7200 | |
7201 | llvm::cl::ExpansionContext ECtx(Alloc, Tokenizer); |
7202 | ECtx.setMarkEOLs(MarkEOLs); |
7203 | if (FS) |
7204 | ECtx.setVFS(FS); |
7205 | |
7206 | if (llvm::Error Err = ECtx.expandResponseFiles(Argv&: Args)) |
7207 | return Err; |
7208 | |
7209 | // If -cc1 came from a response file, remove the EOL sentinels. |
7210 | auto FirstArg = llvm::find_if(Range: llvm::drop_begin(RangeOrContainer&: Args), |
7211 | P: [](const char *A) { return A != nullptr; }); |
7212 | if (FirstArg != Args.end() && StringRef(*FirstArg).starts_with(Prefix: "-cc1" )) { |
7213 | // If -cc1 came from a response file, remove the EOL sentinels. |
7214 | if (MarkEOLs) { |
7215 | auto newEnd = std::remove(first: Args.begin(), last: Args.end(), value: nullptr); |
7216 | Args.resize(N: newEnd - Args.begin()); |
7217 | } |
7218 | } |
7219 | |
7220 | return llvm::Error::success(); |
7221 | } |
7222 | |
7223 | static const char *GetStableCStr(llvm::StringSet<> &SavedStrings, StringRef S) { |
7224 | return SavedStrings.insert(key: S).first->getKeyData(); |
7225 | } |
7226 | |
7227 | /// Apply a list of edits to the input argument lists. |
7228 | /// |
7229 | /// The input string is a space separated list of edits to perform, |
7230 | /// they are applied in order to the input argument lists. Edits |
7231 | /// should be one of the following forms: |
7232 | /// |
7233 | /// '#': Silence information about the changes to the command line arguments. |
7234 | /// |
7235 | /// '^FOO': Add FOO as a new argument at the beginning of the command line |
7236 | /// right after the name of the compiler executable. |
7237 | /// |
7238 | /// '+FOO': Add FOO as a new argument at the end of the command line. |
7239 | /// |
7240 | /// 's/XXX/YYY/': Substitute the regular expression XXX with YYY in the command |
7241 | /// line. |
7242 | /// |
7243 | /// 'xOPTION': Removes all instances of the literal argument OPTION. |
7244 | /// |
7245 | /// 'XOPTION': Removes all instances of the literal argument OPTION, |
7246 | /// and the following argument. |
7247 | /// |
7248 | /// 'Ox': Removes all flags matching 'O' or 'O[sz0-9]' and adds 'Ox' |
7249 | /// at the end of the command line. |
7250 | /// |
7251 | /// \param OS - The stream to write edit information to. |
7252 | /// \param Args - The vector of command line arguments. |
7253 | /// \param Edit - The override command to perform. |
7254 | /// \param SavedStrings - Set to use for storing string representations. |
7255 | static void applyOneOverrideOption(raw_ostream &OS, |
7256 | SmallVectorImpl<const char *> &Args, |
7257 | StringRef Edit, |
7258 | llvm::StringSet<> &SavedStrings) { |
7259 | // This does not need to be efficient. |
7260 | |
7261 | if (Edit[0] == '^') { |
7262 | const char *Str = GetStableCStr(SavedStrings, S: Edit.substr(Start: 1)); |
7263 | OS << "### Adding argument " << Str << " at beginning\n" ; |
7264 | Args.insert(I: Args.begin() + 1, Elt: Str); |
7265 | } else if (Edit[0] == '+') { |
7266 | const char *Str = GetStableCStr(SavedStrings, S: Edit.substr(Start: 1)); |
7267 | OS << "### Adding argument " << Str << " at end\n" ; |
7268 | Args.push_back(Elt: Str); |
7269 | } else if (Edit[0] == 's' && Edit[1] == '/' && Edit.ends_with(Suffix: "/" ) && |
7270 | Edit.slice(Start: 2, End: Edit.size() - 1).contains(C: '/')) { |
7271 | StringRef MatchPattern = Edit.substr(Start: 2).split(Separator: '/').first; |
7272 | StringRef ReplPattern = Edit.substr(Start: 2).split(Separator: '/').second; |
7273 | ReplPattern = ReplPattern.slice(Start: 0, End: ReplPattern.size() - 1); |
7274 | |
7275 | for (unsigned i = 1, e = Args.size(); i != e; ++i) { |
7276 | // Ignore end-of-line response file markers |
7277 | if (Args[i] == nullptr) |
7278 | continue; |
7279 | std::string Repl = llvm::Regex(MatchPattern).sub(Repl: ReplPattern, String: Args[i]); |
7280 | |
7281 | if (Repl != Args[i]) { |
7282 | OS << "### Replacing '" << Args[i] << "' with '" << Repl << "'\n" ; |
7283 | Args[i] = GetStableCStr(SavedStrings, S: Repl); |
7284 | } |
7285 | } |
7286 | } else if (Edit[0] == 'x' || Edit[0] == 'X') { |
7287 | auto Option = Edit.substr(Start: 1); |
7288 | for (unsigned i = 1; i < Args.size();) { |
7289 | if (Option == Args[i]) { |
7290 | OS << "### Deleting argument " << Args[i] << '\n'; |
7291 | Args.erase(CI: Args.begin() + i); |
7292 | if (Edit[0] == 'X') { |
7293 | if (i < Args.size()) { |
7294 | OS << "### Deleting argument " << Args[i] << '\n'; |
7295 | Args.erase(CI: Args.begin() + i); |
7296 | } else |
7297 | OS << "### Invalid X edit, end of command line!\n" ; |
7298 | } |
7299 | } else |
7300 | ++i; |
7301 | } |
7302 | } else if (Edit[0] == 'O') { |
7303 | for (unsigned i = 1; i < Args.size();) { |
7304 | const char *A = Args[i]; |
7305 | // Ignore end-of-line response file markers |
7306 | if (A == nullptr) |
7307 | continue; |
7308 | if (A[0] == '-' && A[1] == 'O' && |
7309 | (A[2] == '\0' || (A[3] == '\0' && (A[2] == 's' || A[2] == 'z' || |
7310 | ('0' <= A[2] && A[2] <= '9'))))) { |
7311 | OS << "### Deleting argument " << Args[i] << '\n'; |
7312 | Args.erase(CI: Args.begin() + i); |
7313 | } else |
7314 | ++i; |
7315 | } |
7316 | OS << "### Adding argument " << Edit << " at end\n" ; |
7317 | Args.push_back(Elt: GetStableCStr(SavedStrings, S: '-' + Edit.str())); |
7318 | } else { |
7319 | OS << "### Unrecognized edit: " << Edit << "\n" ; |
7320 | } |
7321 | } |
7322 | |
7323 | void driver::applyOverrideOptions(SmallVectorImpl<const char *> &Args, |
7324 | const char *OverrideStr, |
7325 | llvm::StringSet<> &SavedStrings, |
7326 | StringRef EnvVar, raw_ostream *OS) { |
7327 | if (!OS) |
7328 | OS = &llvm::nulls(); |
7329 | |
7330 | if (OverrideStr[0] == '#') { |
7331 | ++OverrideStr; |
7332 | OS = &llvm::nulls(); |
7333 | } |
7334 | |
7335 | *OS << "### " << EnvVar << ": " << OverrideStr << "\n" ; |
7336 | |
7337 | // This does not need to be efficient. |
7338 | |
7339 | const char *S = OverrideStr; |
7340 | while (*S) { |
7341 | const char *End = ::strchr(s: S, c: ' '); |
7342 | if (!End) |
7343 | End = S + strlen(s: S); |
7344 | if (End != S) |
7345 | applyOneOverrideOption(OS&: *OS, Args, Edit: std::string(S, End), SavedStrings); |
7346 | S = End; |
7347 | if (*S != '\0') |
7348 | ++S; |
7349 | } |
7350 | } |
7351 | |