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