clang 24.0.0git
Flang.cpp
Go to the documentation of this file.
1//===-- Flang.cpp - Flang+LLVM ToolChain Implementations --------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Flang.h"
10#include "Arch/RISCV.h"
11#include "Cuda.h"
12
18#include "llvm/Frontend/Debug/Options.h"
19#include "llvm/Support/Path.h"
20#include "llvm/TargetParser/Host.h"
21#include "llvm/TargetParser/RISCVISAInfo.h"
22#include "llvm/TargetParser/RISCVTargetParser.h"
23
24#include <cassert>
25
26using namespace clang::driver;
27using namespace clang::driver::tools;
28using namespace clang;
29using namespace llvm::opt;
30
31/// Add -x lang to \p CmdArgs for \p Input.
32static void addDashXForInput(const ArgList &Args, const InputInfo &Input,
33 ArgStringList &CmdArgs) {
34 CmdArgs.push_back("-x");
35 // Map the driver type to the frontend type.
36 CmdArgs.push_back(types::getTypeName(Input.getType()));
37}
38
39// Translate the dependency-file options into the arguments understood by
40// `flang -fc1`. The options handled here:
41// -M Emit only the dependencies and skip code generation. They are
42// written to stdout unless -MF redirects them.
43// -MM Treated identically to -M. The -MM/-M split exists to omit system
44// headers, but Fortran has no notion of system vs user headers, so
45// there is nothing for -MM to exclude.
46// -MD Compile normally and produce the object file, while also writing the
47// dependency file. Its name defaults to the -o value, or the input
48// file name when -o is absent, with the extension replaced by .d.
49// -MMD Treated identically to -MD, for the same reason -MM equals -M.
50// -MF Set the path of the dependency file to write.
51// -MT Set the dependency target name (the part before the colon).
52// -MQ Like -MT, but additionally quotes characters special to Make.
54 const JobAction &JA,
55 const ArgList &Args,
56 const InputInfo &Output,
57 const InputInfoList &Inputs,
58 ArgStringList &CmdArgs) {
59 Arg *ArgM = Args.getLastArg(options::OPT_M, options::OPT_MM);
60 Arg *ArgMD = Args.getLastArg(options::OPT_MD, options::OPT_MMD);
61
62 if (!ArgM && !ArgMD)
63 return;
64
65 // Drop warnings for -M/-MM so they don't mix into the dependency output.
66 if (ArgM)
67 CmdArgs.push_back("-w");
68 else
69 ArgM = ArgMD;
70
71 // Emit "-MT <target>", quoting Make metacharacters when requested.
72 auto addTarget = [&](StringRef Target, bool Quote) {
73 CmdArgs.push_back("-MT");
74 if (Quote) {
75 SmallString<128> Quoted;
77 CmdArgs.push_back(Args.MakeArgString(Quoted));
78 } else {
79 CmdArgs.push_back(Args.MakeArgString(Target));
80 }
81 };
82
83 // Decide where to write the dependency file.
84 const char *DepFile;
85 if (Arg *MF = Args.getLastArg(options::OPT_MF)) {
86 // -MF gives the path explicitly.
87 DepFile = MF->getValue();
88 C.addFailureResultFile(DepFile, &JA);
89 } else if (Output.getType() == types::TY_Dependencies) {
90 // Plain -M/-MM: the dependency file is the output, so use its name
91 DepFile = Output.getFilename();
92 } else if (!ArgMD) {
93 // -M/-MM with no -o: write the dependencies to stdout.
94 DepFile = "-";
95 } else {
96 // -MD/-MMD: name it after -o, else the input, with a .d extension.
98 if (Arg *OutputOpt = Args.getLastArg(options::OPT_o))
99 P = OutputOpt->getValue();
100 else
101 P = llvm::sys::path::filename(Inputs[0].getBaseInput());
102 llvm::sys::path::replace_extension(P, "d");
103 DepFile = Args.MakeArgString(P);
104 C.addFailureResultFile(DepFile, &JA);
105 }
106 CmdArgs.push_back("-dependency-file");
107 CmdArgs.push_back(DepFile);
108
109 // Render the explicit target(s). -MT is verbatim, -MQ is Make-quoted.
110 bool HasTarget = false;
111 for (const Arg *A : Args.filtered(options::OPT_MT, options::OPT_MQ)) {
112 HasTarget = true;
113 A->claim();
114 addTarget(A->getValue(), A->getOption().matches(options::OPT_MQ));
115 }
116
117 // With no explicit target, default to the object file. In -M/-MM mode -o
118 // names the dependency file, not the target, so derive <base>.o instead.
119 if (!HasTarget) {
120 Arg *OutputOpt = Args.getLastArg(options::OPT_o);
121 if (OutputOpt && Output.getType() != types::TY_Dependencies) {
122 addTarget(OutputOpt->getValue(), /*Quote=*/true);
123 } else {
124 SmallString<128> P(llvm::sys::path::filename(Inputs[0].getBaseInput()));
125 llvm::sys::path::replace_extension(P, "o");
126 addTarget(P, /*Quote=*/true);
127 }
128 }
129}
130
131void Flang::addFortranDialectOptions(const ArgList &Args,
132 ArgStringList &CmdArgs) const {
133 Args.addAllArgs(CmdArgs,
134 {options::OPT_ffixed_form,
135 options::OPT_ffree_form,
136 options::OPT_ffixed_line_length_EQ,
137 options::OPT_fopenacc,
138 options::OPT_finput_charset_EQ,
139 options::OPT_fimplicit_none,
140 options::OPT_fimplicit_none_ext,
141 options::OPT_fno_implicit_none,
142 options::OPT_fbackslash,
143 options::OPT_fno_backslash,
144 options::OPT_flogical_abbreviations,
145 options::OPT_fno_logical_abbreviations,
146 options::OPT_fxor_operator,
147 options::OPT_fno_xor_operator,
148 options::OPT_falternative_parameter_statement,
149 options::OPT_fdefault_integer_4,
150 options::OPT_fdefault_real_4,
151 options::OPT_fdefault_real_8,
152 options::OPT_fdefault_integer_8,
153 options::OPT_fdefault_double_8,
154 options::OPT_flarge_sizes,
155 options::OPT_fno_automatic,
156 options::OPT_fhermetic_module_files,
157 options::OPT_frealloc_lhs,
158 options::OPT_fno_realloc_lhs,
159 options::OPT_fsave_main_program,
160 options::OPT_fd_lines_as_code,
161 options::OPT_fd_lines_as_comments,
162 options::OPT_fno_save_main_program,
163 options::OPT_fprefer_intrinsic_module_use_association,
164 options::OPT_fno_prefer_intrinsic_module_use_association});
165}
166
167void Flang::addPreprocessingOptions(const ArgList &Args,
168 ArgStringList &CmdArgs) const {
169 Args.addAllArgs(CmdArgs,
170 {options::OPT_P, options::OPT_D, options::OPT_U,
171 options::OPT_I, options::OPT_cpp, options::OPT_nocpp});
172}
173
174/// @C shouldLoopVersion
175///
176/// Check if Loop Versioning should be enabled.
177/// We look for the last of one of the following:
178/// -Ofast, -O4, -O<number> and -f[no-]version-loops-for-stride.
179/// Loop versioning is disabled if the last option is
180/// -fno-version-loops-for-stride.
181/// Loop versioning is enabled if the last option is one of:
182/// -floop-versioning
183/// -Ofast
184/// -O4
185/// -O3
186/// For all other cases, loop versioning is disabled.
187///
188/// The gfortran compiler automatically enables the option for -O3 or -Ofast.
189///
190/// @return true if loop-versioning should be enabled, otherwise false.
191static bool shouldLoopVersion(const ArgList &Args) {
192 const Arg *LoopVersioningArg = Args.getLastArg(
193 options::OPT_Ofast, options::OPT_O, options::OPT_O4,
194 options::OPT_floop_versioning, options::OPT_fno_loop_versioning);
195 if (!LoopVersioningArg)
196 return false;
197
198 if (LoopVersioningArg->getOption().matches(options::OPT_fno_loop_versioning))
199 return false;
200
201 if (LoopVersioningArg->getOption().matches(options::OPT_floop_versioning))
202 return true;
203
204 if (LoopVersioningArg->getOption().matches(options::OPT_Ofast) ||
205 LoopVersioningArg->getOption().matches(options::OPT_O4))
206 return true;
207
208 if (LoopVersioningArg->getOption().matches(options::OPT_O)) {
209 StringRef S(LoopVersioningArg->getValue());
210 unsigned OptLevel = 0;
211 // Note -Os or Oz woould "fail" here, so return false. Which is the
212 // desiered behavior.
213 if (S.getAsInteger(10, OptLevel))
214 return false;
215
216 return OptLevel > 2;
217 }
218
219 llvm_unreachable("We should not end up here");
220 return false;
221}
222
223void Flang::addDebugOptions(const llvm::opt::ArgList &Args, const JobAction &JA,
224 const InputInfo &Output, const InputInfo &Input,
225 llvm::opt::ArgStringList &CmdArgs) const {
226 const auto &TC = getToolChain();
227 const Driver &D = TC.getDriver();
228 Args.addAllArgs(CmdArgs,
229 {options::OPT_module_dir, options::OPT_fdebug_module_writer,
230 options::OPT_fintrinsic_modules_path, options::OPT_pedantic,
231 options::OPT_std_EQ, options::OPT_W_Joined,
232 options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ,
233 options::OPT_funderscoring, options::OPT_fno_underscoring,
234 options::OPT_funsigned, options::OPT_fno_unsigned,
235 options::OPT_fenumeration_type,
236 options::OPT_fno_enumeration_type,
237 options::OPT_fopenacc_default_none_scalars_strict,
238 options::OPT_fno_openacc_default_none_scalars_strict,
239 options::OPT_fopenacc_multiple_names_in_routine,
240 options::OPT_fno_openacc_multiple_names_in_routine,
241 options::OPT_finstrument_functions});
242
243 llvm::codegenoptions::DebugInfoKind DebugInfoKind;
244 bool hasDwarfNArg = getDwarfNArg(Args) != nullptr;
245 if (Args.hasArg(options::OPT_gN_Group)) {
246 Arg *gNArg = Args.getLastArg(options::OPT_gN_Group);
247 DebugInfoKind = debugLevelToInfoKind(*gNArg);
248 } else if (Args.hasArg(options::OPT_g_Flag) || hasDwarfNArg) {
249 DebugInfoKind = llvm::codegenoptions::FullDebugInfo;
250 } else {
251 DebugInfoKind = llvm::codegenoptions::NoDebugInfo;
252 }
253 addDebugInfoKind(CmdArgs, DebugInfoKind);
254 // Pass on the DWARF version when debug information is being generated, or
255 // when -gdwarf-N names a version. Leaving it out means the version stays
256 // unset and the backend falls back to dwarf::DWARF_VERSION (4) instead of
257 // honouring toolchain default like clang does.
258 //
259 // Note that both conditions are needed to match clang for cases like
260 // "-gdwarf-5 -g0".
261 if (hasDwarfNArg || DebugInfoKind != llvm::codegenoptions::NoDebugInfo) {
262 const unsigned DwarfVersion = getDwarfVersion(getToolChain(), Args);
263 CmdArgs.push_back(
264 Args.MakeArgString("-dwarf-version=" + Twine(DwarfVersion)));
265 }
266 if (Args.hasArg(options::OPT_gsplit_dwarf) ||
267 Args.hasArg(options::OPT_gsplit_dwarf_EQ)) {
268 // FIXME: -gsplit-dwarf on AIX is currently unimplemented.
269 if (TC.getTriple().isOSAIX()) {
270 D.Diag(diag::err_drv_unsupported_opt_for_target)
271 << Args.getLastArg(options::OPT_gsplit_dwarf)->getSpelling()
272 << TC.getTriple().str();
273 return;
274 }
275 if (DebugInfoKind == llvm::codegenoptions::NoDebugInfo)
276 return;
277
278 Arg *SplitDWARFArg;
279 DwarfFissionKind DwarfFission = getDebugFissionKind(D, Args, SplitDWARFArg);
280
281 if (DwarfFission == DwarfFissionKind::None ||
282 !checkDebugInfoOption(SplitDWARFArg, Args, D, TC))
283 return;
284
285 if (!TC.getTriple().isOSBinFormatELF() &&
286 !TC.getTriple().isOSBinFormatWasm() &&
287 !TC.getTriple().isOSBinFormatCOFF()) {
288 D.Diag(diag::warn_drv_unsupported_debug_info_opt_for_target)
289 << SplitDWARFArg->getSpelling() << TC.getTriple().str();
290 return;
291 }
292
295 return;
296
297 const char *SplitDWARFOut = SplitDebugName(JA, Args, Input, Output);
298 CmdArgs.push_back("-split-dwarf-file");
299 CmdArgs.push_back(SplitDWARFOut);
300 if (DwarfFission == DwarfFissionKind::Split) {
301 CmdArgs.push_back("-split-dwarf-output");
302 CmdArgs.push_back(SplitDWARFOut);
303 }
304 }
305
306 // Handle compressed debug sections (-gz).
307 renderDebugInfoCompressionArgs(Args, CmdArgs, D, TC);
308
309 addDebugInfoForProfilingArgs(D, TC, Args, CmdArgs);
310}
311
312void Flang::addCodegenOptions(const ArgList &Args,
313 ArgStringList &CmdArgs) const {
314 Arg *stackArrays =
315 Args.getLastArg(options::OPT_Ofast, options::OPT_fstack_arrays,
316 options::OPT_fno_stack_arrays);
317 if (stackArrays &&
318 !stackArrays->getOption().matches(options::OPT_fno_stack_arrays))
319 CmdArgs.push_back("-fstack-arrays");
320
321 if (Args.hasFlag(options::OPT_fsafe_trampoline,
322 options::OPT_fno_safe_trampoline, false)) {
323 const llvm::Triple &T = getToolChain().getTriple();
324 if (T.getArch() == llvm::Triple::x86_64 ||
325 T.getArch() == llvm::Triple::aarch64 ||
326 T.getArch() == llvm::Triple::aarch64_be) {
327 CmdArgs.push_back("-fsafe-trampoline");
328 } else {
330 diag::warn_drv_unsupported_option_for_target)
331 << "-fsafe-trampoline" << T.str();
332 }
333 }
334
335 // -fno-protect-parens is the default for -Ofast.
336 if (!Args.hasFlag(options::OPT_fprotect_parens,
337 options::OPT_fno_protect_parens,
338 /*Default=*/!Args.hasArg(options::OPT_Ofast)))
339 CmdArgs.push_back("-fno-protect-parens");
340
341 if (Args.hasFlag(options::OPT_funsafe_cray_pointers,
342 options::OPT_fno_unsafe_cray_pointers, false)) {
343 // TODO: currently passed as MLIR option
344 CmdArgs.push_back("-mmlir");
345 CmdArgs.push_back("-unsafe-cray-pointers");
346 }
347
348 Args.addOptInFlag(CmdArgs, options::OPT_fexperimental_loop_fusion,
349 options::OPT_fno_experimental_loop_fusion);
350 Args.AddLastArg(CmdArgs, options::OPT_ffp_sum_reassociation,
351 options::OPT_fno_fp_sum_reassociation);
352
353 handleInterchangeLoopsArgs(Args, CmdArgs);
354 handleVectorizeLoopsArgs(Args, CmdArgs);
355 handleVectorizeSLPArgs(Args, CmdArgs);
356
357 if (shouldLoopVersion(Args))
358 CmdArgs.push_back("-fversion-loops-for-stride");
359
360 for (const auto &arg :
361 Args.getAllArgValues(options::OPT_frepack_arrays_contiguity_EQ))
362 if (arg != "whole" && arg != "innermost") {
363 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
364 << "-frepack-arrays-contiguity=" << arg;
365 }
366
367 Args.addAllArgs(
368 CmdArgs,
369 {options::OPT_fdo_concurrent_to_openmp_EQ,
370 options::OPT_fno_ppc_native_vec_elem_order,
371 options::OPT_fppc_native_vec_elem_order, options::OPT_finit_global_zero,
372 options::OPT_fno_init_global_zero, options::OPT_frepack_arrays,
373 options::OPT_fno_repack_arrays,
374 options::OPT_frepack_arrays_contiguity_EQ,
375 options::OPT_fstack_repack_arrays, options::OPT_fno_stack_repack_arrays,
376 options::OPT_ftime_report, options::OPT_ftime_report_EQ,
377 options::OPT_funroll_loops, options::OPT_fno_unroll_loops,
378 options::OPT_relaxed_c_loc});
379
380 const llvm::Triple &Triple = getToolChain().getEffectiveTriple();
381 addSeparateSectionFlags(Triple, Args, CmdArgs);
382
383 if (Args.hasArg(options::OPT_fcoarray))
384 CmdArgs.push_back("-fcoarray");
385}
386
387void Flang::addLTOOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
388 const ToolChain &TC = getToolChain();
389 LTOKind LTOMode = TC.getLTOMode(Args);
390 // LTO mode is parsed by the Clang driver library.
391 assert(LTOMode != LTOK_Unknown && "Unknown LTO mode.");
392 if (LTOMode == LTOK_Full)
393 CmdArgs.push_back("-flto=full");
394 else if (LTOMode == LTOK_Thin)
395 CmdArgs.push_back("-flto=thin");
396
397 if (Args.hasFlag(options::OPT_fsplit_lto_unit,
398 options::OPT_fno_split_lto_unit, /*Default=*/false))
399 CmdArgs.push_back("-fsplit-lto-unit");
400
401 Args.addAllArgs(CmdArgs, {options::OPT_ffat_lto_objects,
402 options::OPT_fno_fat_lto_objects});
403}
404
405void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const {
406 // ParsePICArgs parses -fPIC/-fPIE and their variants and returns a tuple of
407 // (RelocationModel, PICLevel, IsPIE).
408 llvm::Reloc::Model RelocationModel;
409 unsigned PICLevel;
410 bool IsPIE;
411 std::tie(RelocationModel, PICLevel, IsPIE) =
412 ParsePICArgs(getToolChain(), Args);
413
414 if (auto *RMName = RelocationModelName(RelocationModel)) {
415 CmdArgs.push_back("-mrelocation-model");
416 CmdArgs.push_back(RMName);
417 }
418 if (PICLevel > 0) {
419 CmdArgs.push_back("-pic-level");
420 CmdArgs.push_back(PICLevel == 1 ? "1" : "2");
421 if (IsPIE)
422 CmdArgs.push_back("-pic-is-pie");
423 }
424}
425
426void Flang::AddAArch64TargetArgs(const ArgList &Args,
427 ArgStringList &CmdArgs) const {
428 // Handle -msve_vector_bits=<bits>
429 if (Arg *A = Args.getLastArg(options::OPT_msve_vector_bits_EQ)) {
430 StringRef Val = A->getValue();
431 const Driver &D = getToolChain().getDriver();
432 if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" ||
433 Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" ||
434 Val == "1024+" || Val == "2048+") {
435 unsigned Bits = 0;
436 if (!Val.consume_back("+")) {
437 [[maybe_unused]] bool Invalid = Val.getAsInteger(10, Bits);
438 assert(!Invalid && "Failed to parse value");
439 CmdArgs.push_back(
440 Args.MakeArgString("-mvscale-max=" + llvm::Twine(Bits / 128)));
441 }
442
443 [[maybe_unused]] bool Invalid = Val.getAsInteger(10, Bits);
444 assert(!Invalid && "Failed to parse value");
445 CmdArgs.push_back(
446 Args.MakeArgString("-mvscale-min=" + llvm::Twine(Bits / 128)));
447 // Silently drop requests for vector-length agnostic code as it's implied.
448 } else if (Val != "scalable")
449 // Handle the unsupported values passed to msve-vector-bits.
450 D.Diag(diag::err_drv_unsupported_option_argument)
451 << A->getSpelling() << Val;
452 }
453}
454
455void Flang::AddLoongArch64TargetArgs(const ArgList &Args,
456 ArgStringList &CmdArgs) const {
457 const Driver &D = getToolChain().getDriver();
458 // Currently, flang only support `-mabi=lp64d` in LoongArch64.
459 if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
460 StringRef V = A->getValue();
461 if (V != "lp64d") {
462 D.Diag(diag::err_drv_argument_not_allowed_with) << "-mabi" << V;
463 }
464 }
465
466 if (const Arg *A = Args.getLastArg(options::OPT_mannotate_tablejump,
467 options::OPT_mno_annotate_tablejump)) {
468 if (A->getOption().matches(options::OPT_mannotate_tablejump)) {
469 CmdArgs.push_back("-mllvm");
470 CmdArgs.push_back("-loongarch-annotate-tablejump");
471 }
472 }
473}
474
475void Flang::AddPPCTargetArgs(const ArgList &Args,
476 ArgStringList &CmdArgs) const {
477 const Driver &D = getToolChain().getDriver();
478 bool VecExtabi = false;
479
480 if (const Arg *A = Args.getLastArg(options::OPT_mabi_EQ)) {
481 StringRef V = A->getValue();
482 if (V == "vec-extabi")
483 VecExtabi = true;
484 else if (V == "vec-default")
485 VecExtabi = false;
486 else
487 D.Diag(diag::err_drv_unsupported_option_argument)
488 << A->getSpelling() << V;
489 }
490
491 const llvm::Triple &T = getToolChain().getTriple();
492 if (VecExtabi) {
493 if (!T.isOSAIX()) {
494 D.Diag(diag::err_drv_unsupported_opt_for_target)
495 << "-mabi=vec-extabi" << T.str();
496 }
497 CmdArgs.push_back("-mabi=vec-extabi");
498 }
499}
500
501void Flang::AddRISCVTargetArgs(const ArgList &Args,
502 ArgStringList &CmdArgs) const {
503 const Driver &D = getToolChain().getDriver();
504 const llvm::Triple &Triple = getToolChain().getTriple();
505
506 StringRef ABIName = riscv::getRISCVABI(Args, Triple);
507 if (ABIName == "lp64" || ABIName == "lp64f" || ABIName == "lp64d")
508 CmdArgs.push_back(Args.MakeArgString("-mabi=" + ABIName));
509 else
510 D.Diag(diag::err_drv_unsupported_option_argument) << "-mabi=" << ABIName;
511
512 // Handle -mrvv-vector-bits=<bits>
513 if (Arg *A = Args.getLastArg(options::OPT_mrvv_vector_bits_EQ)) {
514 StringRef Val = A->getValue();
515
516 // Get minimum VLen from march.
517 unsigned MinVLen = 0;
518 std::string Arch = riscv::getRISCVArch(Args, Triple);
519 auto ISAInfo = llvm::RISCVISAInfo::parseArchString(
520 Arch, /*EnableExperimentalExtensions*/ true);
521 // Ignore parsing error.
522 if (!errorToBool(ISAInfo.takeError()))
523 MinVLen = (*ISAInfo)->getMinVLen();
524
525 // If the value is "zvl", use MinVLen from march. Otherwise, try to parse
526 // as integer as long as we have a MinVLen.
527 unsigned Bits = 0;
528 if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) {
529 Bits = MinVLen;
530 } else if (!Val.getAsInteger(10, Bits)) {
531 // Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that
532 // at least MinVLen.
533 if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock ||
534 Bits > 65536 || !llvm::isPowerOf2_32(Bits))
535 Bits = 0;
536 }
537
538 // If we got a valid value try to use it.
539 if (Bits != 0) {
540 unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock;
541 CmdArgs.push_back(
542 Args.MakeArgString("-mvscale-max=" + llvm::Twine(VScaleMin)));
543 CmdArgs.push_back(
544 Args.MakeArgString("-mvscale-min=" + llvm::Twine(VScaleMin)));
545 } else if (Val != "scalable") {
546 // Handle the unsupported values passed to mrvv-vector-bits.
547 D.Diag(diag::err_drv_unsupported_option_argument)
548 << A->getSpelling() << Val;
549 }
550 }
551}
552
553void Flang::AddX86_64TargetArgs(const ArgList &Args,
554 ArgStringList &CmdArgs) const {
555 if (Arg *A = Args.getLastArg(options::OPT_masm_EQ)) {
556 StringRef Value = A->getValue();
557 if (Value == "intel" || Value == "att") {
558 CmdArgs.push_back(Args.MakeArgString("-mllvm"));
559 CmdArgs.push_back(Args.MakeArgString("-x86-asm-syntax=" + Value));
560 } else {
561 getToolChain().getDriver().Diag(diag::err_drv_unsupported_option_argument)
562 << A->getSpelling() << Value;
563 }
564 }
565}
566
567static void addVSDefines(const ToolChain &TC, const ArgList &Args,
568 ArgStringList &CmdArgs) {
569
570 unsigned ver = 0;
571 const VersionTuple vt = TC.computeMSVCVersion(nullptr, Args);
572 ver = vt.getMajor() * 10000000 + vt.getMinor().value_or(0) * 100000 +
573 vt.getSubminor().value_or(0);
574 CmdArgs.push_back(Args.MakeArgString("-D_MSC_VER=" + Twine(ver / 100000)));
575 CmdArgs.push_back(Args.MakeArgString("-D_MSC_FULL_VER=" + Twine(ver)));
576 CmdArgs.push_back(Args.MakeArgString("-D_WIN32"));
577
578 const llvm::Triple &triple = TC.getTriple();
579 if (triple.isAArch64()) {
580 CmdArgs.push_back("-D_M_ARM64=1");
581 } else if (triple.isX86() && triple.isArch32Bit()) {
582 CmdArgs.push_back("-D_M_IX86=600");
583 } else if (triple.isX86() && triple.isArch64Bit()) {
584 CmdArgs.push_back("-D_M_X64=100");
585 } else {
586 llvm_unreachable(
587 "Flang on Windows only supports X86_32, X86_64 and AArch64");
588 }
589}
590
591static void processVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args,
592 ArgStringList &CmdArgs) {
593 assert(TC.getTriple().isKnownWindowsMSVCEnvironment() &&
594 "can only add VS runtime library on Windows!");
595
596 // Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI
597 // should only depend on msv(u)crt. LLVM still emits libgcc/compiler-rt
598 // functions in some cases like 128-bit integer math (__udivti3, __modti3,
599 // __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a
600 // dependency to Compiler-RT's builtin library where these are implemented.
601 CmdArgs.push_back(Args.MakeArgString(
602 "--dependent-lib=" + TC.getCompilerRTBasename(Args, "builtins")));
603
604 unsigned RTOptionID = options::OPT__SLASH_MT;
605 if (auto *rtl = Args.getLastArg(options::OPT_fms_runtime_lib_EQ)) {
606 RTOptionID = llvm::StringSwitch<unsigned>(rtl->getValue())
607 .Case("static", options::OPT__SLASH_MT)
608 .Case("static_dbg", options::OPT__SLASH_MTd)
609 .Case("dll", options::OPT__SLASH_MD)
610 .Case("dll_dbg", options::OPT__SLASH_MDd)
611 .Default(options::OPT__SLASH_MT);
612 }
613 switch (RTOptionID) {
614 case options::OPT__SLASH_MT:
615 CmdArgs.push_back("-D_MT");
616 CmdArgs.push_back("--dependent-lib=libcmt");
617 CmdArgs.push_back("--dependent-lib=flang_rt.runtime.static.lib");
618 break;
619 case options::OPT__SLASH_MTd:
620 CmdArgs.push_back("-D_MT");
621 CmdArgs.push_back("-D_DEBUG");
622 CmdArgs.push_back("--dependent-lib=libcmtd");
623 CmdArgs.push_back("--dependent-lib=flang_rt.runtime.static_dbg.lib");
624 break;
625 case options::OPT__SLASH_MD:
626 CmdArgs.push_back("-D_MT");
627 CmdArgs.push_back("-D_DLL");
628 CmdArgs.push_back("--dependent-lib=msvcrt");
629 CmdArgs.push_back("--dependent-lib=flang_rt.runtime.dynamic.lib");
630 break;
631 case options::OPT__SLASH_MDd:
632 CmdArgs.push_back("-D_MT");
633 CmdArgs.push_back("-D_DEBUG");
634 CmdArgs.push_back("-D_DLL");
635 CmdArgs.push_back("--dependent-lib=msvcrtd");
636 CmdArgs.push_back("--dependent-lib=flang_rt.runtime.dynamic_dbg.lib");
637 break;
638 }
639}
640
641void Flang::AddAMDGPUTargetArgs(const ArgList &Args, ArgStringList &CmdArgs,
642 BoundArch BA,
643 Action::OffloadKind DeviceOffloadKind) const {
644 if (Arg *A = Args.getLastArg(options::OPT_mcode_object_version_EQ)) {
645 StringRef Val = A->getValue();
646 CmdArgs.push_back(Args.MakeArgString("-mcode-object-version=" + Val));
647 CmdArgs.push_back(Args.MakeArgString("-mllvm"));
648 CmdArgs.push_back(
649 Args.MakeArgString("--amdhsa-code-object-version=" + Val));
650 }
651
652 const ToolChain &TC = getToolChain();
653 TC.addClangTargetOptions(Args, CmdArgs, BA, DeviceOffloadKind);
654}
655
656void Flang::AddNVPTXTargetArgs(const ArgList &Args, ArgStringList &CmdArgs,
657 BoundArch BA,
658 Action::OffloadKind DeviceOffloadKind) const {
659 // we cannot use addClangTargetOptions, as it appends unsupported args for
660 // flang: -fcuda-is-device, -fno-threadsafe-statics,
661 // -fcuda-allow-variadic-functions and -target-sdk-version Instead we manually
662 // detect the CUDA installation and link libdevice
663 const ToolChain &TC = getToolChain();
664 const Driver &D = TC.getDriver();
665 const llvm::Triple &Triple = TC.getEffectiveTriple();
666
667 if (!Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib, true))
668 return;
669
670 // Detect CUDA installation and link libdevice
671 CudaInstallationDetector CudaInstallation(D, Triple, Args);
672 if (!CudaInstallation.isValid()) {
673 D.Diag(diag::err_drv_no_cuda_installation);
674 return;
675 }
676
677 StringRef GpuArch = Args.getLastArgValue(options::OPT_march_EQ);
678 if (GpuArch.empty()) {
679 D.Diag(diag::err_drv_offload_missing_gpu_arch) << "NVPTX" << "flang";
680 return;
681 }
682
683 std::string LibDeviceFile = CudaInstallation.getLibDeviceFile(GpuArch);
684 if (LibDeviceFile.empty()) {
685 D.Diag(diag::err_drv_no_cuda_libdevice) << GpuArch;
686 return;
687 }
688
689 CmdArgs.push_back("-mlink-builtin-bitcode");
690 CmdArgs.push_back(Args.MakeArgString(LibDeviceFile));
691}
692
693void Flang::addTargetOptions(const ArgList &Args, ArgStringList &CmdArgs,
694 BoundArch BA,
695 Action::OffloadKind DeviceOffloadKind) const {
696 const ToolChain &TC = getToolChain();
697 const llvm::Triple &Triple = TC.getEffectiveTriple();
698 const Driver &D = TC.getDriver();
699
700 std::string CPU = getCPUName(D, Args, Triple);
701 if (!CPU.empty()) {
702 CmdArgs.push_back("-target-cpu");
703 CmdArgs.push_back(Args.MakeArgString(CPU));
704 }
705
706 addOutlineAtomicsArgs(D, getToolChain(), Args, CmdArgs, Triple);
707
708 // Add the target features.
709 switch (TC.getArch()) {
710 default:
711 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
712 break;
713 case llvm::Triple::aarch64:
714 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
715 AddAArch64TargetArgs(Args, CmdArgs);
716 break;
717 case llvm::Triple::amdgpu:
718 case llvm::Triple::r600:
719 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
720 AddAMDGPUTargetArgs(Args, CmdArgs, BA, DeviceOffloadKind);
721 break;
722 case llvm::Triple::nvptx:
723 case llvm::Triple::nvptx64:
724 AddNVPTXTargetArgs(Args, CmdArgs, BA, DeviceOffloadKind);
725 break;
726 case llvm::Triple::riscv64:
727 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
728 AddRISCVTargetArgs(Args, CmdArgs);
729 break;
730 case llvm::Triple::x86_64:
731 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
732 AddX86_64TargetArgs(Args, CmdArgs);
733 break;
734 case llvm::Triple::ppc:
735 case llvm::Triple::ppc64:
736 case llvm::Triple::ppc64le:
737 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
738 AddPPCTargetArgs(Args, CmdArgs);
739 break;
740 case llvm::Triple::loongarch64:
741 getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ false);
742 AddLoongArch64TargetArgs(Args, CmdArgs);
743 break;
744 }
745
746 if (Arg *A = Args.getLastArg(options::OPT_fveclib)) {
747 StringRef Name = A->getValue();
748 if (Name == "SVML") {
749 if (Triple.getArch() != llvm::Triple::x86 &&
750 Triple.getArch() != llvm::Triple::x86_64)
751 D.Diag(diag::err_drv_unsupported_opt_for_target)
752 << Name << Triple.getArchName();
753 } else if (Name == "AMDLIBM") {
754 if (Triple.getArch() != llvm::Triple::x86 &&
755 Triple.getArch() != llvm::Triple::x86_64)
756 D.Diag(diag::err_drv_unsupported_opt_for_target)
757 << Name << Triple.getArchName();
758 } else if (Name == "libmvec") {
759 if (Triple.getArch() != llvm::Triple::x86 &&
760 Triple.getArch() != llvm::Triple::x86_64 &&
761 Triple.getArch() != llvm::Triple::aarch64 &&
762 Triple.getArch() != llvm::Triple::aarch64_be)
763 D.Diag(diag::err_drv_unsupported_opt_for_target)
764 << Name << Triple.getArchName();
765 } else if (Name == "SLEEF" || Name == "ArmPL") {
766 if (Triple.getArch() != llvm::Triple::aarch64 &&
767 Triple.getArch() != llvm::Triple::aarch64_be)
768 D.Diag(diag::err_drv_unsupported_opt_for_target)
769 << Name << Triple.getArchName();
770 }
771
772 if (Triple.isOSDarwin()) {
773 // flang doesn't currently suport nostdlib, nodefaultlibs. Adding these
774 // here incase they are added someday
775 if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
776 if (A->getValue() == StringRef{"Accelerate"}) {
777 CmdArgs.push_back("-framework");
778 CmdArgs.push_back("Accelerate");
779 }
780 }
781 }
782 A->render(Args, CmdArgs);
783 }
784
785 if (Triple.isKnownWindowsMSVCEnvironment()) {
786 processVSRuntimeLibrary(TC, Args, CmdArgs);
787 addVSDefines(TC, Args, CmdArgs);
788 }
789
790 // TODO: Add target specific flags, ABI, mtune option etc.
791 if (const Arg *A = Args.getLastArg(options::OPT_mtune_EQ)) {
792 CmdArgs.push_back("-tune-cpu");
793 if (A->getValue() == StringRef{"native"})
794 CmdArgs.push_back(Args.MakeArgString(llvm::sys::getHostCPUName()));
795 else
796 CmdArgs.push_back(A->getValue());
797 }
798
799 Args.addAllArgs(CmdArgs,
800 {options::OPT_fverbose_asm, options::OPT_fno_verbose_asm,
801 options::OPT_fatomic_ignore_denormal_mode,
802 options::OPT_fno_atomic_ignore_denormal_mode,
803 options::OPT_fatomic_fine_grained_memory,
804 options::OPT_fno_atomic_fine_grained_memory,
805 options::OPT_fatomic_remote_memory,
806 options::OPT_fno_atomic_remote_memory,
807 options::OPT_munsafe_fp_atomics});
808}
809
810void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs,
811 const JobAction &JA, const ArgList &Args,
812 ArgStringList &CmdArgs) const {
813 bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
814 bool IsHostOffloadingAction = JA.isHostOffloading(Action::OFK_OpenMP) ||
815 JA.isHostOffloading(C.getActiveOffloadKinds());
816
817 // Tell the frontend when it is compiling for an offloading device, regardless
818 // of offloading programming model.
820 CmdArgs.push_back("-foffload-device");
821
822 // Skips the primary input file, which is the input file that the compilation
823 // proccess will be executed upon (e.g. the host bitcode file) and
824 // adds other secondary input (e.g. device bitcode files for embedding to the
825 // -fembed-offload-object argument or the host IR file for proccessing
826 // during device compilation to the fopenmp-host-ir-file-path argument via
827 // OpenMPDeviceInput). This is condensed logic from the ConstructJob
828 // function inside of the Clang driver for pushing on further input arguments
829 // needed for offloading during various phases of compilation.
830 for (size_t i = 1; i < Inputs.size(); ++i) {
831 if (Inputs[i].getType() == types::TY_Nothing) {
832 // contains nothing, so it's skippable
833 } else if (IsHostOffloadingAction) {
834 CmdArgs.push_back(
835 Args.MakeArgString("-fembed-offload-object=" +
836 getToolChain().getInputFilename(Inputs[i])));
837 } else if (IsOpenMPDevice) {
838 if (Inputs[i].getFilename()) {
839 CmdArgs.push_back("-fopenmp-host-ir-file-path");
840 CmdArgs.push_back(Args.MakeArgString(Inputs[i].getFilename()));
841 } else {
842 llvm_unreachable("missing openmp host-ir file for device offloading");
843 }
844 } else {
845 llvm_unreachable(
846 "unexpectedly given multiple inputs or given unknown input");
847 }
848 }
849
850 // When in OpenMP offloading mode, forward assumptions information about
851 // thread and team counts in the target device. The host needs to know about
852 // this to prevent the SPMD to SPMD-no-loop promotion being done differently
853 // for host and device on the same target region.
854 if (Args.hasFlag(options::OPT_fopenmp_assume_teams_oversubscription,
855 options::OPT_fno_openmp_assume_teams_oversubscription,
856 /*Default=*/false))
857 CmdArgs.push_back("-fopenmp-assume-teams-oversubscription");
858 if (Args.hasFlag(options::OPT_fopenmp_assume_threads_oversubscription,
859 options::OPT_fno_openmp_assume_threads_oversubscription,
860 /*Default=*/false))
861 CmdArgs.push_back("-fopenmp-assume-threads-oversubscription");
862
863 if (IsOpenMPDevice) {
864 // -fopenmp-is-target-device is passed along to tell the frontend that it is
865 // generating code for a device, so that only the relevant code is emitted.
866 CmdArgs.push_back("-fopenmp-is-target-device");
867
868 // -fopenmp-target-fast implies -fopenmp-assume-no-thread-state and
869 // -fopenmp-assume-no-nested-parallelism, and forces -O3 unless an
870 // explicit optimization level was requested.
871 bool TargetFastUsed =
872 Args.hasFlag(options::OPT_fopenmp_target_fast,
873 options::OPT_fno_openmp_target_fast, false);
874
875 if (TargetFastUsed && !Args.hasArg(options::OPT_O_Group))
876 CmdArgs.push_back("-O3");
877
878 // When in OpenMP offloading mode, enable debugging on the device.
879 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_target_debug_EQ);
880 if (Args.hasFlag(options::OPT_fopenmp_target_debug,
881 options::OPT_fno_openmp_target_debug, /*Default=*/false))
882 CmdArgs.push_back("-fopenmp-target-debug");
883
884 // Handle -fopenmp-assume-no-thread-state (implied by target-fast)
885 if (Args.hasFlag(options::OPT_fopenmp_assume_no_thread_state,
886 options::OPT_fno_openmp_assume_no_thread_state,
887 /*Default=*/TargetFastUsed))
888 CmdArgs.push_back("-fopenmp-assume-no-thread-state");
889
890 // Handle -fopenmp-assume-no-nested-parallelism (implied by target-fast)
891 if (Args.hasFlag(options::OPT_fopenmp_assume_no_nested_parallelism,
892 options::OPT_fno_openmp_assume_no_nested_parallelism,
893 /*Default=*/TargetFastUsed))
894 CmdArgs.push_back("-fopenmp-assume-no-nested-parallelism");
895
896 if (!Args.hasFlag(options::OPT_offloadlib, options::OPT_no_offloadlib,
897 true))
898 CmdArgs.push_back("-nogpulib");
899 }
900
901 addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs);
902}
903
904static void addFloatingPointOptions(const Driver &D, const ArgList &Args,
905 ArgStringList &CmdArgs) {
906 StringRef FPContract;
907 StringRef LastSeenFfpContractOption;
908 StringRef LastFpContractOverrideOption;
909 bool HonorINFs = true;
910 bool HonorNaNs = true;
911 bool ApproxFunc = false;
912 bool SignedZeros = true;
913 bool AssociativeMath = false;
914 bool ReciprocalMath = false;
915
916 StringRef LastComplexRangeOption;
918
919 for (const Arg *A : Args) {
920 auto optId = A->getOption().getID();
921 switch (optId) {
922 // if this isn't an FP option, skip the claim below
923 default:
924 continue;
925
926 case options::OPT_fcomplex_arithmetic_EQ: {
928 StringRef Val = A->getValue();
929 if (Val == "full")
931 else if (Val == "improved")
933 else if (Val == "basic")
935 else {
936 D.Diag(diag::err_drv_unsupported_option_argument)
937 << A->getSpelling() << Val;
938 break;
939 }
940
941 setComplexRange(D, Args.MakeArgString(A->getSpelling() + Val), NewRange,
942 LastComplexRangeOption, Range);
943 break;
944 }
945 case options::OPT_fhonor_infinities:
946 HonorINFs = true;
947 break;
948 case options::OPT_fno_honor_infinities:
949 HonorINFs = false;
950 break;
951 case options::OPT_fhonor_nans:
952 HonorNaNs = true;
953 break;
954 case options::OPT_fno_honor_nans:
955 HonorNaNs = false;
956 break;
957 case options::OPT_fapprox_func:
958 ApproxFunc = true;
959 break;
960 case options::OPT_fno_approx_func:
961 ApproxFunc = false;
962 break;
963 case options::OPT_fsigned_zeros:
964 SignedZeros = true;
965 break;
966 case options::OPT_fno_signed_zeros:
967 SignedZeros = false;
968 break;
969 case options::OPT_fassociative_math:
970 AssociativeMath = true;
971 break;
972 case options::OPT_fno_associative_math:
973 AssociativeMath = false;
974 break;
975 case options::OPT_freciprocal_math:
976 ReciprocalMath = true;
977 break;
978 case options::OPT_fno_reciprocal_math:
979 ReciprocalMath = false;
980 break;
981 case options::OPT_ffp_contract: {
982 StringRef Val = A->getValue();
983 if (Val == "fast" || Val == "off") {
984 if (Val != FPContract && LastFpContractOverrideOption != "") {
985 D.Diag(clang::diag::warn_drv_overriding_option)
986 << LastFpContractOverrideOption
987 << Args.MakeArgString("-ffp-contract=" + Val);
988 }
989 FPContract = Val;
990 LastSeenFfpContractOption = Val;
991 } else if (Val == "on") {
992 // Warn instead of error because users might have makefiles written for
993 // gfortran (which accepts -ffp-contract=on)
994 D.Diag(diag::warn_drv_unsupported_option_for_flang)
995 << Val << A->getOption().getName() << "off";
996 FPContract = "off";
997 LastSeenFfpContractOption = "off";
998 } else {
999 // Clang's "fast-honor-pragmas" option is not supported because it is
1000 // non-standard
1001 D.Diag(diag::err_drv_unsupported_option_argument)
1002 << A->getSpelling() << Val;
1003 }
1004 LastFpContractOverrideOption = "";
1005 break;
1006 }
1007 case options::OPT_Ofast:
1008 [[fallthrough]];
1009 case options::OPT_ffast_math:
1010 HonorINFs = false;
1011 HonorNaNs = false;
1012 AssociativeMath = true;
1013 ReciprocalMath = true;
1014 ApproxFunc = true;
1015 SignedZeros = false;
1016 FPContract = "fast";
1017 if (A->getOption().getID() == options::OPT_Ofast)
1018 LastFpContractOverrideOption = "-Ofast";
1019 else
1020 LastFpContractOverrideOption = "-ffast-math";
1021 setComplexRange(D, A->getSpelling(),
1023 LastComplexRangeOption, Range);
1024 break;
1025 case options::OPT_fno_fast_math:
1026 HonorINFs = true;
1027 HonorNaNs = true;
1028 AssociativeMath = false;
1029 ReciprocalMath = false;
1030 ApproxFunc = false;
1031 SignedZeros = true;
1032 // -fno-fast-math should undo -ffast-math so I return FPContract to the
1033 // default. If -ffp-contract= was explicitly specified, restore the
1034 // user-requested value from LastSeenFfpContractOption so that
1035 // -ffp-contract=off -fno-fast-math --> -ffp-contract=off
1036 if (LastSeenFfpContractOption != "")
1037 FPContract = LastSeenFfpContractOption;
1038 else
1039 FPContract = "";
1040 setComplexRange(D, A->getSpelling(),
1042 LastComplexRangeOption, Range);
1043 LastFpContractOverrideOption = "";
1044 break;
1045 }
1046
1047 // If we handled this option claim it
1048 A->claim();
1049 }
1050
1051 StringRef Recip = parseMRecipOption(D.getDiags(), Args);
1052 if (!Recip.empty())
1053 CmdArgs.push_back(Args.MakeArgString("-mrecip=" + Recip));
1054
1056 std::string ComplexRangeStr = renderComplexRangeOption(Range);
1057 CmdArgs.push_back(Args.MakeArgString(ComplexRangeStr));
1058 CmdArgs.push_back(Args.MakeArgString("-fcomplex-arithmetic=" +
1059 complexRangeKindToStr(Range)));
1060 }
1061
1062 if (llvm::opt::Arg *A =
1063 Args.getLastArg(clang::options::OPT_ffast_real_mod,
1064 clang::options::OPT_fno_fast_real_mod)) {
1065 if (A->getOption().matches(clang::options::OPT_ffast_real_mod))
1066 CmdArgs.push_back("-ffast-real-mod");
1067 else if (A->getOption().matches(clang::options::OPT_fno_fast_real_mod))
1068 CmdArgs.push_back("-fno-fast-real-mod");
1069 }
1070
1071 if (!HonorINFs && !HonorNaNs && AssociativeMath && ReciprocalMath &&
1072 ApproxFunc && !SignedZeros &&
1073 (FPContract == "fast" || FPContract.empty())) {
1074 CmdArgs.push_back("-ffast-math");
1075 return;
1076 }
1077
1078 if (!FPContract.empty())
1079 CmdArgs.push_back(Args.MakeArgString("-ffp-contract=" + FPContract));
1080
1081 if (!HonorINFs)
1082 CmdArgs.push_back("-menable-no-infs");
1083
1084 if (!HonorNaNs)
1085 CmdArgs.push_back("-menable-no-nans");
1086
1087 if (ApproxFunc)
1088 CmdArgs.push_back("-fapprox-func");
1089
1090 if (!SignedZeros)
1091 CmdArgs.push_back("-fno-signed-zeros");
1092
1093 if (AssociativeMath && !SignedZeros)
1094 CmdArgs.push_back("-mreassociate");
1095
1096 if (ReciprocalMath)
1097 CmdArgs.push_back("-freciprocal-math");
1098}
1099
1100// Add options related to IEEE Floating point modes
1101//
1102// Initial halting mode:
1103// Validate -ffpe-trap= and forward it to -fc1. This is handled separately from
1104// addFloatingPointOptions() on purpose: -ffpe-trap= is not part of the
1105// fast-math option set, so it must not be skipped by that function's
1106// -ffast-math fast path. The value check and the target-support warnings depend
1107// only on the option value and the target triple (no frontend-only state), so
1108// they are done here in the driver rather than deferred to -fc1; -fc1 only
1109// translates the list into its LangOptions bitmask.
1110//
1111// TODO:
1112// Rounding modes
1113// Underflow mode
1114static void addIEEEFPModesOptions(const Driver &D, const ArgList &Args,
1115 ArgStringList &CmdArgs,
1116 const llvm::Triple &Triple) {
1117 const Arg *A = Args.getLastArg(options::OPT_ffpe_trap_EQ);
1118 if (!A)
1119 return;
1120
1121 // The value is a comma-separated list of exception mnemonics. "none" and an
1122 // empty list request no halting and reset any earlier request in the list;
1123 // any other unrecognized mnemonic is an error.
1125 StringRef(A->getValue())
1126 .split(Traps, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
1127
1128 bool RequestsTrap = false;
1129 bool RequestsDenormal = false;
1130 for (StringRef Trap : Traps) {
1131 if (Trap == "none") {
1132 RequestsTrap = false;
1133 RequestsDenormal = false;
1134 continue;
1135 }
1136 bool IsKnown = llvm::StringSwitch<bool>(Trap)
1137 .Cases({"invalid", "zero", "overflow", "underflow",
1138 "inexact", "denormal"},
1139 true)
1140 .Default(false);
1141 if (!IsKnown) {
1142 D.Diag(diag::err_drv_unsupported_option_argument)
1143 << A->getSpelling() << Trap;
1144 return;
1145 }
1146 RequestsTrap = true;
1147 RequestsDenormal |= (Trap == "denormal");
1148 }
1149
1150 // Run-time halting is implemented in flang-rt only where the target's
1151 // floating-point environment can trap: it relies on glibc's feenableexcept
1152 // (in practice Linux), and "denormal" additionally requires an x86 target.
1153 // Warn (conservatively) when the target cannot honor the request; the runtime
1154 // otherwise ignores it. The denormal-specific warning names just
1155 // "-ffpe-trap=denormal" to point at the unsupported mnemonic.
1156 if (RequestsTrap && !Triple.isX86() && !Triple.isOSLinux())
1157 D.Diag(diag::warn_drv_unsupported_option_for_target)
1158 << A->getAsString(Args) << Triple.str();
1159 else if (RequestsDenormal && !Triple.isX86())
1160 D.Diag(diag::warn_drv_unsupported_option_for_target)
1161 << "-ffpe-trap=denormal" << Triple.str();
1162
1163 A->render(Args, CmdArgs);
1164}
1165
1166static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs,
1167 const InputInfo &Input) {
1168 StringRef Format = "yaml";
1169 if (const Arg *A = Args.getLastArg(options::OPT_fsave_optimization_record_EQ))
1170 Format = A->getValue();
1171
1172 CmdArgs.push_back("-opt-record-file");
1173
1174 const Arg *A = Args.getLastArg(options::OPT_foptimization_record_file_EQ);
1175 if (A) {
1176 CmdArgs.push_back(A->getValue());
1177 } else {
1179
1180 if (Args.hasArg(options::OPT_c) || Args.hasArg(options::OPT_S)) {
1181 if (Arg *FinalOutput = Args.getLastArg(options::OPT_o))
1182 F = FinalOutput->getValue();
1183 }
1184
1185 if (F.empty()) {
1186 // Use the input filename.
1187 F = llvm::sys::path::stem(Input.getBaseInput());
1188 }
1189
1190 SmallString<32> Extension;
1191 Extension += "opt.";
1192 Extension += Format;
1193
1194 llvm::sys::path::replace_extension(F, Extension);
1195 CmdArgs.push_back(Args.MakeArgString(F));
1196 }
1197
1198 if (const Arg *A =
1199 Args.getLastArg(options::OPT_foptimization_record_passes_EQ)) {
1200 CmdArgs.push_back("-opt-record-passes");
1201 CmdArgs.push_back(A->getValue());
1202 }
1203
1204 if (!Format.empty()) {
1205 CmdArgs.push_back("-opt-record-format");
1206 CmdArgs.push_back(Format.data());
1207 }
1208}
1209
1210static void addPGOAndCoverageFlags(const ToolChain &TC, const JobAction &JA,
1211 const ArgList &Args,
1212 ArgStringList &CmdArgs) {
1213 const Driver &D = TC.getDriver();
1214 const llvm::Triple &T = TC.getTriple();
1215
1216 bool IsCudaDevice = JA.isDeviceOffloading(Action::OFK_Cuda);
1217 bool IsHIPDevice = JA.isDeviceOffloading(Action::OFK_HIP);
1218
1219 if (T.isOSAIX()) {
1220 if (Arg *ProfileSampleUseArg = getLastProfileSampleUseArg(Args))
1221 D.Diag(diag::err_drv_unsupported_opt_for_target)
1222 << ProfileSampleUseArg->getSpelling() << TC.getTriple().str();
1223 }
1224
1225 if (!(IsCudaDevice || IsHIPDevice)) {
1226 // recognise options: -fprofile-sample-use= and -fno-profile-sample-use=
1227 if (Arg *A = getLastProfileSampleUseArg(Args)) {
1228 if (Arg *PGOArg = Args.getLastArg(options::OPT_fprofile_generate,
1229 options::OPT_fprofile_generate_EQ)) {
1230 D.Diag(diag::err_drv_argument_not_allowed_with)
1231 << PGOArg->getAsString(Args) << A->getAsString(Args);
1232 }
1233
1234 StringRef fname = A->getValue();
1235 if (!llvm::sys::fs::exists(fname))
1236 D.Diag(diag::err_drv_no_such_file) << fname;
1237 else
1238 A->render(Args, CmdArgs);
1239 }
1240 }
1241
1242 //-fpseudo-probe-for-profiling
1243 if (Args.hasFlag(options::OPT_fpseudo_probe_for_profiling,
1244 options::OPT_fno_pseudo_probe_for_profiling, false))
1245 CmdArgs.push_back("-fpseudo-probe-for-profiling");
1246
1247 // TODO: Consider reusing Clang's addPGOAndCoverageFlags() for
1248 // -fprofile-generate and other similar options handling instead of
1249 // duplicating driver logic here.
1250 if (Arg *PGOGenerateArg = Args.getLastArg(
1251 options::OPT_fprofile_generate, options::OPT_fprofile_generate_EQ,
1252 options::OPT_fno_profile_generate)) {
1253 if (!PGOGenerateArg->getOption().matches(options::OPT_fno_profile_generate))
1254 PGOGenerateArg->render(Args, CmdArgs);
1255 }
1256
1257 addSplitMachineFunctionsArgs(TC.getDriver(), Args, CmdArgs, TC.getTriple());
1258 Args.addAllArgs(CmdArgs, {options::OPT_fprofile_use_EQ});
1259}
1260
1262 const InputInfo &Output, const InputInfoList &Inputs,
1263 const ArgList &Args, const char *LinkingOutput) const {
1264 const auto &TC = getToolChain();
1265 const llvm::Triple &Triple = TC.getEffectiveTriple();
1266 const std::string &TripleStr = Triple.getTriple();
1267
1268 const Driver &D = TC.getDriver();
1269 ArgStringList CmdArgs;
1270
1271 // Invoke ourselves in -fc1 mode.
1272 CmdArgs.push_back("-fc1");
1273
1274 // Add the "effective" target triple.
1275 CmdArgs.push_back("-triple");
1276 CmdArgs.push_back(Args.MakeArgString(TripleStr));
1277
1278 if (isa<PreprocessJobAction>(JA)) {
1279 if (Output.getType() == types::TY_Dependencies) {
1280 CmdArgs.push_back("-fsyntax-only");
1281 } else {
1282 CmdArgs.push_back("-E");
1283 if (Args.getLastArg(options::OPT_dM))
1284 CmdArgs.push_back("-dM");
1285 }
1286 } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) {
1287 if (JA.getType() == types::TY_Nothing) {
1288 CmdArgs.push_back("-fsyntax-only");
1289 } else if (JA.getType() == types::TY_AST) {
1290 CmdArgs.push_back("-emit-ast");
1291 } else if (JA.getType() == types::TY_LLVM_IR ||
1292 JA.getType() == types::TY_LTO_IR) {
1293 CmdArgs.push_back("-emit-llvm");
1294 } else if (JA.getType() == types::TY_LLVM_BC ||
1295 JA.getType() == types::TY_LTO_BC) {
1296 CmdArgs.push_back("-emit-llvm-bc");
1297 } else if (JA.getType() == types::TY_PP_Asm) {
1298 CmdArgs.push_back("-S");
1299 } else {
1300 assert(false && "Unexpected output type!");
1301 }
1302 } else if (isa<AssembleJobAction>(JA)) {
1303 CmdArgs.push_back("-emit-obj");
1304 } else if (isa<PrecompileJobAction>(JA)) {
1305 // The precompile job action is only needed for options such as -mcpu=help.
1306 // Those will already have been handled by the fc1 driver.
1307 } else {
1308 assert(false && "Unexpected action class for Flang tool.");
1309 }
1310
1311 // We support some options that are invalid for Fortran and have no effect.
1312 // These are solely for compatibility with other compilers. Emit a warning if
1313 // any such options are provided, then proceed normally.
1314 for (options::ID Opt : {options::OPT_fbuiltin, options::OPT_fno_builtin})
1315 if (const Arg *A = Args.getLastArg(Opt))
1316 D.Diag(diag::warn_drv_invalid_argument_for_flang) << A->getSpelling();
1317
1318 // Warn about options that are ignored by flang. These are options that are
1319 // accepted by gfortran, but have no equivalent in flang.
1320 for (const Arg *A :
1321 Args.filtered(options::OPT_clang_ignored_gcc_optimization_f_Group)) {
1322 D.Diag(diag::warn_ignored_gcc_optimization) << A->getAsString(Args);
1323 A->claim();
1324 }
1325
1326 const InputInfo &Input = Inputs[0];
1327 types::ID InputType = Input.getType();
1328
1329 // Add preprocessing options like -I, -D, etc. if we are using the
1330 // preprocessor (i.e. skip when dealing with e.g. binary files).
1332 addPreprocessingOptions(Args, CmdArgs);
1333
1334 addFortranDialectOptions(Args, CmdArgs);
1335
1336 // 'flang -E' always produces output that is suitable for use as fixed form
1337 // Fortran. However it is only valid free form source if the original is also
1338 // free form. Ensure this logic does not incorrectly assume fixed-form for
1339 // cases where it shouldn't, such as `flang -x f95 foo.f90`.
1340 bool isAtemporaryPreprocessedFile =
1341 Input.isFilename() &&
1342 llvm::sys::path::extension(Input.getFilename())
1343 .ends_with(types::getTypeTempSuffix(InputType, /*CLStyle=*/false));
1344 if (InputType == types::TY_PP_Fortran && isAtemporaryPreprocessedFile &&
1345 !Args.getLastArg(options::OPT_ffixed_form, options::OPT_ffree_form))
1346 CmdArgs.push_back("-ffixed-form");
1347
1348 handleColorDiagnosticsArgs(D, Args, CmdArgs);
1349
1350 addLTOOptions(Args, CmdArgs);
1351
1352 // -fPIC and related options.
1353 addPicOptions(Args, CmdArgs);
1354
1355 // Floating point related options
1356 addFloatingPointOptions(D, Args, CmdArgs);
1357
1358 // Initial floating-point exception halting mode. Handled separately so it is
1359 // not skipped by the -ffast-math fast path in addFloatingPointOptions().
1360 addIEEEFPModesOptions(D, Args, CmdArgs, Triple);
1361
1362 // Add target args, features, etc.
1363 addTargetOptions(Args, CmdArgs, JA.getOffloadingArch(),
1365
1366 if (!TC.useIntegratedAs())
1367 CmdArgs.push_back("-no-integrated-as");
1368
1369 llvm::Reloc::Model RelocationModel =
1370 std::get<0>(ParsePICArgs(getToolChain(), Args));
1371 // Add MCModel information
1372 addMCModel(D, Args, Triple, RelocationModel, CmdArgs);
1373
1374 // Add Codegen options
1375 addCodegenOptions(Args, CmdArgs);
1376
1377 // Add R Group options
1378 Args.AddAllArgs(CmdArgs, options::OPT_R_Group);
1379
1380 // Remarks can be enabled with any of the `-f.*optimization-record.*` flags.
1381 if (willEmitRemarks(Args))
1382 renderRemarksOptions(Args, CmdArgs, Input);
1383
1384 // Add debug compile options
1385 addDebugOptions(Args, JA, Output, Input, CmdArgs);
1386
1387 // Disable all warnings
1388 // TODO: Handle interactions between -w, -pedantic, -Wall, -WOption
1389 Args.AddLastArg(CmdArgs, options::OPT_w);
1390
1391 addPGOAndCoverageFlags(TC, JA, Args, CmdArgs);
1392
1393 // Forward flags for OpenMP. We don't do this if the current action is an
1394 // device offloading action other than OpenMP.
1395 if (Args.hasFlag(options::OPT_fopenmp, options::OPT_fopenmp_EQ,
1396 options::OPT_fno_openmp, false) &&
1399 switch (D.getOpenMPRuntime(Args)) {
1400 case Driver::OMPRT_OMP:
1402 // Clang can generate useful OpenMP code for these two runtime libraries.
1403 CmdArgs.push_back("-fopenmp");
1404 Args.AddAllArgs(CmdArgs, options::OPT_fopenmp_version_EQ);
1405
1406 if (Args.hasArg(options::OPT_fopenmp_force_usm))
1407 CmdArgs.push_back("-fopenmp-force-usm");
1408 Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
1409 options::OPT_fno_openmp_simd);
1410
1411 // FIXME: Clang supports a whole bunch more flags here.
1412 break;
1413 default:
1414 // By default, if Clang doesn't know how to generate useful OpenMP code
1415 // for a specific runtime library, we just don't pass the '-fopenmp' flag
1416 // down to the actual compilation.
1417 // FIXME: It would be better to have a mode which *only* omits IR
1418 // generation based on the OpenMP support so that we get consistent
1419 // semantic analysis, etc.
1420 const Arg *A = Args.getLastArg(options::OPT_fopenmp_EQ);
1421 D.Diag(diag::warn_drv_unsupported_openmp_library)
1422 << A->getSpelling() << A->getValue();
1423 break;
1424 }
1425 } else {
1426 Args.AddLastArg(CmdArgs, options::OPT_fopenmp_simd,
1427 options::OPT_fno_openmp_simd);
1428 }
1429
1430 // Pass the path to compiler resource files.
1431 CmdArgs.push_back("-resource-dir");
1432 CmdArgs.push_back(D.ResourceDir.c_str());
1433
1434 // Default intrinsic module dirs must be added after any user-provided dirs in
1435 // -fintrinsic-modules-path since the default dirs have lower precedence than
1436 // user-provided dirs
1437 if (std::optional<std::string> IntrModPath =
1439 CmdArgs.push_back("-fintrinsic-modules-path");
1440 CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
1441 }
1442
1443 // Ideally, every target triple has its own set of builtin modules since they
1444 // are compiled with platform-dependent conditionals such as `#if __x86_64__`.
1445 // However, getting the builtin modules for offload targets requires building
1446 // the flang-rt and openmp for those targets as well:
1447 // -DLLVM_RUNTIME_TARGETS=default;amdgcn-amd-amdhsa;nvptx64-nvidia-cuda.
1448 // To reduce friction when build systems have not yet been updated, we also
1449 // add the host's builtin module to the search path (with lower priority), in
1450 // case a module file has not been found for the offload targets itself.
1451 // FIXME: This workaround may mix module files targeting different triples and
1452 // should eventually be removed.
1453 auto &&HostTCs =
1454 C.getOffloadToolChains<clang::driver::OffloadAction ::OFK_Host>();
1455 for (auto [OKind, HostTC] : llvm::make_range(HostTCs.first, HostTCs.second)) {
1456 if (HostTC == &TC)
1457 continue;
1458
1459 if (std::optional<std::string> IntrModPath =
1460 HostTC->getDefaultIntrinsicModuleDir()) {
1461 CmdArgs.push_back("-fintrinsic-modules-path");
1462 CmdArgs.push_back(Args.MakeArgString(*IntrModPath));
1463 }
1464 }
1465
1466 // Offloading related options
1467 addOffloadOptions(C, Inputs, JA, Args, CmdArgs);
1468
1469 // Forward -Xflang arguments to -fc1
1470 Args.AddAllArgValues(CmdArgs, options::OPT_Xflang);
1471
1473 getFramePointerKind(Args, Triple);
1474
1475 const char *FPKeepKindStr = nullptr;
1476 switch (FPKeepKind) {
1478 FPKeepKindStr = "-mframe-pointer=none";
1479 break;
1481 FPKeepKindStr = "-mframe-pointer=reserved";
1482 break;
1484 FPKeepKindStr = "-mframe-pointer=non-leaf-no-reserve";
1485 break;
1487 FPKeepKindStr = "-mframe-pointer=non-leaf";
1488 break;
1490 FPKeepKindStr = "-mframe-pointer=all";
1491 break;
1492 }
1493 assert(FPKeepKindStr && "unknown FramePointerKind");
1494 CmdArgs.push_back(FPKeepKindStr);
1495
1496 // Forward -mllvm options to the LLVM option parser. In practice, this means
1497 // forwarding to `-fc1` as that's where the LLVM parser is run.
1498 for (const Arg *A : Args.filtered(options::OPT_mllvm)) {
1499 A->claim();
1500 A->render(Args, CmdArgs);
1501 }
1502
1503 for (const Arg *A : Args.filtered(options::OPT_mmlir)) {
1504 A->claim();
1505 A->render(Args, CmdArgs);
1506 }
1507
1508 // Remove any unsupported gfortran diagnostic options
1509 for (const Arg *A : Args.filtered(options::OPT_flang_ignored_w_Group)) {
1510 A->claim();
1511 D.Diag(diag::warn_drv_unsupported_diag_option_for_flang)
1512 << A->getOption().getName();
1513 }
1514
1515 // Optimization level for CodeGen.
1516 if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) {
1517 if (A->getOption().matches(options::OPT_O4)) {
1518 CmdArgs.push_back("-O3");
1519 D.Diag(diag::warn_O4_is_O3);
1520 } else if (A->getOption().matches(options::OPT_Ofast)) {
1521 CmdArgs.push_back("-O3");
1522 D.Diag(diag::warn_drv_deprecated_arg_ofast_for_flang);
1523 } else {
1524 A->render(Args, CmdArgs);
1525 }
1526 }
1527
1528 renderGlobalISelOptions(D, Args, CmdArgs, Triple);
1529 renderCommonIntegerOverflowOptions(Args, CmdArgs, false);
1530
1531 assert((Output.isFilename() || Output.isNothing()) && "Invalid output.");
1532 if (Output.isFilename()) {
1533 CmdArgs.push_back("-o");
1534 CmdArgs.push_back(Output.getFilename());
1535 }
1536
1537 if (Args.getLastArg(options::OPT_save_temps_EQ))
1538 Args.AddLastArg(CmdArgs, options::OPT_save_temps_EQ);
1539
1540 renderDependencyGenerationOptions(C, JA, Args, Output, Inputs, CmdArgs);
1541
1542 addDashXForInput(Args, Input, CmdArgs);
1543
1544 bool FRecordCmdLine = false;
1545 bool GRecordCmdLine = false;
1546 bool DXRecordCmdLine = false;
1547 if (shouldRecordCommandLine(TC, Args, FRecordCmdLine, GRecordCmdLine,
1548 DXRecordCmdLine)) {
1549 const char *CmdLine = renderEscapedCommandLine(TC, Args);
1550 if (FRecordCmdLine) {
1551 CmdArgs.push_back("-record-command-line");
1552 CmdArgs.push_back(CmdLine);
1553 }
1554 if (TC.UseDwarfDebugFlags() || GRecordCmdLine) {
1555 CmdArgs.push_back("-dwarf-debug-flags");
1556 CmdArgs.push_back(CmdLine);
1557 }
1558 }
1559
1560 // The input could be Ty_Nothing when "querying" options such as -mcpu=help
1561 // are used.
1562 ArrayRef<InputInfo> FrontendInputs = Input;
1563 if (Input.isNothing())
1564 FrontendInputs = {};
1565
1566 for (const InputInfo &Input : FrontendInputs) {
1567 if (Input.isFilename())
1568 CmdArgs.push_back(Input.getFilename());
1569 else
1570 Input.getInputArg().renderAsInput(Args, CmdArgs);
1571 }
1572
1573 // Handle "clang --driver-mode=flang" case
1574 bool isClangDriverWithFlangMode = false;
1575 std::string DriverName = D.Name;
1576 if (const char *PA = D.getPrependArg())
1577 DriverName = PA;
1578 if (DriverName.find("clang") != std::string::npos && D.IsFlangMode())
1579 isClangDriverWithFlangMode = true;
1580
1581 const char *Exec = isClangDriverWithFlangMode
1582 ? Args.MakeArgString(D.GetProgramPath("flang", TC))
1584 C.addCommand(std::make_unique<Command>(JA, *this,
1586 Exec, CmdArgs, Inputs, Output));
1587}
1588
1589Flang::Flang(const ToolChain &TC) : Tool("flang", "flang frontend", TC) {}
1590
#define V(N, I)
static void addDashXForInput(const ArgList &Args, const InputInfo &Input, ArgStringList &CmdArgs)
Add -x lang to CmdArgs for Input.
Definition Clang.cpp:337
static void renderRemarksOptions(const ArgList &Args, ArgStringList &CmdArgs, const llvm::Triple &Triple, const InputInfo &Input, const InputInfo &Output, const JobAction &JA)
Definition Clang.cpp:1268
static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C, const JobAction &JA, const InputInfo &Output, const ArgList &Args, SanitizerArgs &SanArgs, ArgStringList &CmdArgs)
Definition Clang.cpp:368
clang::CodeGenOptions::FramePointerKind getFramePointerKind(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
static void processVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs)
Definition Flang.cpp:591
static void addVSDefines(const ToolChain &TC, const ArgList &Args, ArgStringList &CmdArgs)
Definition Flang.cpp:567
static bool shouldLoopVersion(const ArgList &Args)
@C shouldLoopVersion
Definition Flang.cpp:191
static void addDashXForInput(const ArgList &Args, const InputInfo &Input, ArgStringList &CmdArgs)
Add -x lang to CmdArgs for Input.
Definition Flang.cpp:32
static void addIEEEFPModesOptions(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs, const llvm::Triple &Triple)
Definition Flang.cpp:1114
static void addFloatingPointOptions(const Driver &D, const ArgList &Args, ArgStringList &CmdArgs)
Definition Flang.cpp:904
static void renderDependencyGenerationOptions(Compilation &C, const JobAction &JA, const ArgList &Args, const InputInfo &Output, const InputInfoList &Inputs, ArgStringList &CmdArgs)
Definition Flang.cpp:53
TokenType getType() const
Returns the token's type, e.g.
llvm::MachO::Target Target
Definition MachO.h:51
ComplexRangeKind
Controls the various implementations for complex multiplication and.
@ CX_Full
Implementation of complex division and multiplication using a call to runtime library functions(gener...
@ CX_Basic
Implementation of complex division and multiplication using algebraic formulas at source precision.
@ CX_None
No range rule is enabled.
@ CX_Improved
Implementation of complex division offering an improved handling for overflow in intermediate calcula...
types::ID getType() const
Definition Action.h:154
BoundArch getOffloadingArch() const
Definition Action.h:217
OffloadKind getOffloadingDeviceKind() const
Definition Action.h:216
bool isHostOffloading(unsigned int OKind) const
Check if this action have any offload kinds.
Definition Action.h:224
bool isDeviceOffloading(OffloadKind OKind) const
Definition Action.h:227
Compilation - A set of tasks to perform for a single driver invocation.
Definition Compilation.h:46
Driver - Encapsulate logic for constructing compilation processes from a set of gcc-driver-like comma...
Definition Driver.h:96
DiagnosticsEngine & getDiags() const
Definition Driver.h:410
const char * getPrependArg() const
Definition Driver.h:421
OpenMPRuntimeKind getOpenMPRuntime(const llvm::opt::ArgList &Args) const
Compute the desired OpenMP runtime from the flags provided.
Definition Driver.cpp:896
DiagnosticBuilder Diag(unsigned DiagID) const
Definition Driver.h:160
std::string Name
The name the driver was invoked as.
Definition Driver.h:167
const char * getDriverProgramPath() const
Get the path to the main driver executable.
Definition Driver.h:432
std::string GetProgramPath(StringRef Name, const ToolChain &TC) const
GetProgramPath - Lookup Name in the list of program search paths.
Definition Driver.cpp:6988
std::string ResourceDir
The path to the compiler resource directory.
Definition Driver.h:180
@ OMPRT_IOMP5
The legacy name for the LLVM OpenMP runtime from when it was the Intel OpenMP runtime.
Definition Driver.h:156
@ OMPRT_OMP
The LLVM OpenMP runtime.
Definition Driver.h:146
bool IsFlangMode() const
Whether the driver should invoke flang for fortran inputs.
Definition Driver.h:236
InputInfo - Wrapper for information about an input source.
Definition InputInfo.h:22
const char * getBaseInput() const
Definition InputInfo.h:78
const llvm::opt::Arg & getInputArg() const
Definition InputInfo.h:87
const char * getFilename() const
Definition InputInfo.h:83
bool isNothing() const
Definition InputInfo.h:74
bool isFilename() const
Definition InputInfo.h:75
types::ID getType() const
Definition InputInfo.h:77
ToolChain - Access to tools for a single platform.
Definition ToolChain.h:96
virtual bool useIntegratedAs() const
Check if the toolchain should use the integrated assembler.
llvm::Triple::ArchType getArch() const
Definition ToolChain.h:302
const Driver & getDriver() const
Definition ToolChain.h:286
virtual LTOKind getLTOMode(const llvm::opt::ArgList &Args, Action::OffloadKind Kind=Action::OFK_None) const
Resolve the requested LTO mode for this toolchain.
virtual void addClangTargetOptions(const llvm::opt::ArgList &DriverArgs, llvm::opt::ArgStringList &CC1Args, BoundArch BA, Action::OffloadKind DeviceOffloadKind) const
Add options that need to be passed to cc1 for this target.
const llvm::Triple & getEffectiveTriple() const
Get the toolchain's effective clang triple.
Definition ToolChain.h:314
const llvm::Triple & getTriple() const
Definition ToolChain.h:288
virtual bool UseDwarfDebugFlags() const
UseDwarfDebugFlags - Embed the compile options to clang into the Dwarf compile unit information.
Definition ToolChain.h:655
virtual VersionTuple computeMSVCVersion(const Driver *D, const llvm::opt::ArgList &Args) const
On Windows, returns the MSVC compatibility version.
std::string getCompilerRTBasename(const llvm::opt::ArgList &Args, StringRef Component, FileType Type=ToolChain::FT_Static) const
std::optional< std::string > getDefaultIntrinsicModuleDir() const
Returns the target-specific path for Flang's intrinsic modules in the resource directory if it exists...
const ToolChain & getToolChain() const
Definition Tool.h:52
Tool(const char *Name, const char *ShortName, const ToolChain &TC)
Definition Tool.cpp:14
Flang(const ToolChain &TC)
Definition Flang.cpp:1589
void ConstructJob(Compilation &C, const JobAction &JA, const InputInfo &Output, const InputInfoList &Inputs, const llvm::opt::ArgList &TCArgs, const char *LinkingOutput) const override
ConstructJob - Construct jobs to perform the action JA, writing to Output and with Inputs,...
Definition Flang.cpp:1261
std::string getRISCVArch(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
Definition RISCV.cpp:305
StringRef getRISCVABI(const llvm::opt::ArgList &Args, const llvm::Triple &Triple)
void addMCModel(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &Triple, const llvm::Reloc::Model &RelocationModel, llvm::opt::ArgStringList &CmdArgs)
llvm::opt::Arg * getLastProfileSampleUseArg(const llvm::opt::ArgList &Args)
void handleVectorizeSLPArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Enable -fslp-vectorize based on the optimization level selected.
const char * SplitDebugName(const JobAction &JA, const llvm::opt::ArgList &Args, const InputInfo &Input, const InputInfo &Output)
void addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const llvm::Triple &Triple)
void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, bool ForAS, bool IsAux=false)
void renderDebugInfoCompressionArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const Driver &D, const ToolChain &TC)
std::string complexRangeKindToStr(LangOptions::ComplexRangeKind Range)
void handleColorDiagnosticsArgs(const Driver &D, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Handle the -f{no}-color-diagnostics and -f{no}-diagnostics-colors options.
std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, const llvm::Triple &T, bool FromAs=false)
void setComplexRange(const Driver &D, StringRef NewOpt, LangOptions::ComplexRangeKind NewRange, StringRef &LastOpt, LangOptions::ComplexRangeKind &Range)
void addDebugInfoForProfilingArgs(const Driver &D, const ToolChain &TC, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
void addDebugInfoKind(llvm::opt::ArgStringList &CmdArgs, llvm::codegenoptions::DebugInfoKind DebugInfoKind)
llvm::codegenoptions::DebugInfoKind debugLevelToInfoKind(const llvm::opt::Arg &A)
void renderGlobalISelOptions(const Driver &D, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const llvm::Triple &Triple)
std::string renderComplexRangeOption(LangOptions::ComplexRangeKind Range)
DwarfFissionKind getDebugFissionKind(const Driver &D, const llvm::opt::ArgList &Args, llvm::opt::Arg *&Arg)
void handleInterchangeLoopsArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Enable -floop-interchange based on the optimization level selected.
const char * renderEscapedCommandLine(const ToolChain &TC, const llvm::opt::ArgList &Args)
Join the args in the given ArgList, escape spaces and backslashes and return the joined string.
void renderCommonIntegerOverflowOptions(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, bool IsMSVCCompat)
void addSplitMachineFunctionsArgs(const Driver &D, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs, const llvm::Triple &Triple)
bool checkDebugInfoOption(const llvm::opt::Arg *A, const llvm::opt::ArgList &Args, const Driver &D, const ToolChain &TC)
std::tuple< llvm::Reloc::Model, unsigned, bool > ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args)
unsigned getDwarfVersion(const ToolChain &TC, const llvm::opt::ArgList &Args)
bool shouldRecordCommandLine(const ToolChain &TC, const llvm::opt::ArgList &Args, bool &FRecordCommandLine, bool &GRecordCommandLine, bool &DXRecordCommandLine)
Check if the command line should be recorded in the object file.
const llvm::opt::Arg * getDwarfNArg(const llvm::opt::ArgList &Args)
void addSeparateSectionFlags(const llvm::Triple &Triple, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Append -ffunction-sections / -fdata-sections to CmdArgs when the corresponding flags are enabled (exp...
void handleVectorizeLoopsArgs(const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Enable -fvectorize based on the optimization level selected.
const char * RelocationModelName(llvm::Reloc::Model Model)
void addOpenMPHostOffloadingArgs(const Compilation &C, const JobAction &JA, const llvm::opt::ArgList &Args, llvm::opt::ArgStringList &CmdArgs)
Adds offloading options for OpenMP host compilation to CmdArgs.
ID getPreprocessedType(ID Id)
getPreprocessedType - Get the ID of the type for this input when it has been preprocessed,...
Definition Types.cpp:53
const char * getTypeName(ID Id)
getTypeName - Return the name of the type for Id.
Definition Types.cpp:49
const char * getTypeTempSuffix(ID Id, bool CLStyle=false)
getTypeTempSuffix - Return the suffix to use when creating a temp file of this type,...
Definition Types.cpp:81
LTOKind
Describes the kind of LTO mode selected via -f(no-)?lto(=.*)? options.
Definition Driver.h:60
SmallVector< InputInfo, 4 > InputInfoList
Definition Driver.h:52
bool willEmitRemarks(const llvm::opt::ArgList &Args)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
void quoteMakeTarget(StringRef Target, SmallVectorImpl< char > &Res)
Quote target names for inclusion in GNU Make dependency files.
@ Default
Set to the current date and time.
const FunctionProtoType * T
StringRef parseMRecipOption(clang::DiagnosticsEngine &Diags, const llvm::opt::ArgList &Args)
bool(*)(llvm::ArrayRef< const char * >, llvm::raw_ostream &, llvm::raw_ostream &, bool, bool) Driver
Definition Wasm.cpp:36
__DEVICE__ _Tp arg(const std::complex< _Tp > &__c)
static constexpr ResponseFileSupport AtFileUTF8()
Definition Job.h:86