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