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