| 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 | |
| 12 | #include "clang/Basic/CodeGenOptions.h" |
| 13 | #include "clang/Driver/CommonArgs.h" |
| 14 | #include "clang/Driver/Options.h" |
| 15 | #include "llvm/Frontend/Debug/Options.h" |
| 16 | #include "llvm/Support/Path.h" |
| 17 | #include "llvm/TargetParser/Host.h" |
| 18 | #include "llvm/TargetParser/RISCVISAInfo.h" |
| 19 | #include "llvm/TargetParser/RISCVTargetParser.h" |
| 20 | |
| 21 | #include <cassert> |
| 22 | |
| 23 | using namespace clang::driver; |
| 24 | using namespace clang::driver::tools; |
| 25 | using namespace clang; |
| 26 | using namespace llvm::opt; |
| 27 | |
| 28 | /// Add -x lang to \p CmdArgs for \p Input. |
| 29 | static void addDashXForInput(const ArgList &Args, const InputInfo &Input, |
| 30 | ArgStringList &CmdArgs) { |
| 31 | CmdArgs.push_back(Elt: "-x" ); |
| 32 | // Map the driver type to the frontend type. |
| 33 | CmdArgs.push_back(Elt: types::getTypeName(Id: Input.getType())); |
| 34 | } |
| 35 | |
| 36 | void Flang::addFortranDialectOptions(const ArgList &Args, |
| 37 | ArgStringList &CmdArgs) const { |
| 38 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_ffixed_form, |
| 39 | options::OPT_ffree_form, |
| 40 | options::OPT_ffixed_line_length_EQ, |
| 41 | options::OPT_fopenacc, |
| 42 | options::OPT_finput_charset_EQ, |
| 43 | options::OPT_fimplicit_none, |
| 44 | options::OPT_fimplicit_none_ext, |
| 45 | options::OPT_fno_implicit_none, |
| 46 | options::OPT_fbackslash, |
| 47 | options::OPT_fno_backslash, |
| 48 | options::OPT_flogical_abbreviations, |
| 49 | options::OPT_fno_logical_abbreviations, |
| 50 | options::OPT_fxor_operator, |
| 51 | options::OPT_fno_xor_operator, |
| 52 | options::OPT_falternative_parameter_statement, |
| 53 | options::OPT_fdefault_real_8, |
| 54 | options::OPT_fdefault_integer_8, |
| 55 | options::OPT_fdefault_double_8, |
| 56 | options::OPT_flarge_sizes, |
| 57 | options::OPT_fno_automatic, |
| 58 | options::OPT_fhermetic_module_files, |
| 59 | options::OPT_frealloc_lhs, |
| 60 | options::OPT_fno_realloc_lhs, |
| 61 | options::OPT_fsave_main_program, |
| 62 | options::OPT_fd_lines_as_code, |
| 63 | options::OPT_fd_lines_as_comments, |
| 64 | options::OPT_fno_save_main_program}); |
| 65 | } |
| 66 | |
| 67 | void Flang::addPreprocessingOptions(const ArgList &Args, |
| 68 | ArgStringList &CmdArgs) const { |
| 69 | Args.addAllArgs(Output&: CmdArgs, |
| 70 | Ids: {options::OPT_P, options::OPT_D, options::OPT_U, |
| 71 | options::OPT_I, options::OPT_cpp, options::OPT_nocpp}); |
| 72 | } |
| 73 | |
| 74 | /// @C shouldLoopVersion |
| 75 | /// |
| 76 | /// Check if Loop Versioning should be enabled. |
| 77 | /// We look for the last of one of the following: |
| 78 | /// -Ofast, -O4, -O<number> and -f[no-]version-loops-for-stride. |
| 79 | /// Loop versioning is disabled if the last option is |
| 80 | /// -fno-version-loops-for-stride. |
| 81 | /// Loop versioning is enabled if the last option is one of: |
| 82 | /// -floop-versioning |
| 83 | /// -Ofast |
| 84 | /// -O4 |
| 85 | /// -O3 |
| 86 | /// For all other cases, loop versioning is is disabled. |
| 87 | /// |
| 88 | /// The gfortran compiler automatically enables the option for -O3 or -Ofast. |
| 89 | /// |
| 90 | /// @return true if loop-versioning should be enabled, otherwise false. |
| 91 | static bool shouldLoopVersion(const ArgList &Args) { |
| 92 | const Arg *LoopVersioningArg = Args.getLastArg( |
| 93 | Ids: options::OPT_Ofast, Ids: options::OPT_O, Ids: options::OPT_O4, |
| 94 | Ids: options::OPT_floop_versioning, Ids: options::OPT_fno_loop_versioning); |
| 95 | if (!LoopVersioningArg) |
| 96 | return false; |
| 97 | |
| 98 | if (LoopVersioningArg->getOption().matches(ID: options::OPT_fno_loop_versioning)) |
| 99 | return false; |
| 100 | |
| 101 | if (LoopVersioningArg->getOption().matches(ID: options::OPT_floop_versioning)) |
| 102 | return true; |
| 103 | |
| 104 | if (LoopVersioningArg->getOption().matches(ID: options::OPT_Ofast) || |
| 105 | LoopVersioningArg->getOption().matches(ID: options::OPT_O4)) |
| 106 | return true; |
| 107 | |
| 108 | if (LoopVersioningArg->getOption().matches(ID: options::OPT_O)) { |
| 109 | StringRef S(LoopVersioningArg->getValue()); |
| 110 | unsigned OptLevel = 0; |
| 111 | // Note -Os or Oz woould "fail" here, so return false. Which is the |
| 112 | // desiered behavior. |
| 113 | if (S.getAsInteger(Radix: 10, Result&: OptLevel)) |
| 114 | return false; |
| 115 | |
| 116 | return OptLevel > 2; |
| 117 | } |
| 118 | |
| 119 | llvm_unreachable("We should not end up here" ); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | void Flang::addOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { |
| 124 | Args.addAllArgs(Output&: CmdArgs, |
| 125 | Ids: {options::OPT_module_dir, options::OPT_fdebug_module_writer, |
| 126 | options::OPT_fintrinsic_modules_path, options::OPT_pedantic, |
| 127 | options::OPT_std_EQ, options::OPT_W_Joined, |
| 128 | options::OPT_fconvert_EQ, options::OPT_fpass_plugin_EQ, |
| 129 | options::OPT_funderscoring, options::OPT_fno_underscoring, |
| 130 | options::OPT_funsigned, options::OPT_fno_unsigned, |
| 131 | options::OPT_finstrument_functions}); |
| 132 | |
| 133 | llvm::codegenoptions::DebugInfoKind DebugInfoKind; |
| 134 | if (Args.hasArg(Ids: options::OPT_gN_Group)) { |
| 135 | Arg *gNArg = Args.getLastArg(Ids: options::OPT_gN_Group); |
| 136 | DebugInfoKind = debugLevelToInfoKind(A: *gNArg); |
| 137 | } else if (Args.hasArg(Ids: options::OPT_g_Flag)) { |
| 138 | DebugInfoKind = llvm::codegenoptions::FullDebugInfo; |
| 139 | } else { |
| 140 | DebugInfoKind = llvm::codegenoptions::NoDebugInfo; |
| 141 | } |
| 142 | addDebugInfoKind(CmdArgs, DebugInfoKind); |
| 143 | } |
| 144 | |
| 145 | void Flang::addCodegenOptions(const ArgList &Args, |
| 146 | ArgStringList &CmdArgs) const { |
| 147 | Arg *stackArrays = |
| 148 | Args.getLastArg(Ids: options::OPT_Ofast, Ids: options::OPT_fstack_arrays, |
| 149 | Ids: options::OPT_fno_stack_arrays); |
| 150 | if (stackArrays && |
| 151 | !stackArrays->getOption().matches(ID: options::OPT_fno_stack_arrays)) |
| 152 | CmdArgs.push_back(Elt: "-fstack-arrays" ); |
| 153 | |
| 154 | handleInterchangeLoopsArgs(Args, CmdArgs); |
| 155 | handleVectorizeLoopsArgs(Args, CmdArgs); |
| 156 | handleVectorizeSLPArgs(Args, CmdArgs); |
| 157 | |
| 158 | if (shouldLoopVersion(Args)) |
| 159 | CmdArgs.push_back(Elt: "-fversion-loops-for-stride" ); |
| 160 | |
| 161 | for (const auto &arg : |
| 162 | Args.getAllArgValues(Id: options::OPT_frepack_arrays_contiguity_EQ)) |
| 163 | if (arg != "whole" && arg != "innermost" ) { |
| 164 | getToolChain().getDriver().Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 165 | << "-frepack-arrays-contiguity=" << arg; |
| 166 | } |
| 167 | |
| 168 | Args.addAllArgs( |
| 169 | Output&: CmdArgs, |
| 170 | Ids: {options::OPT_fdo_concurrent_to_openmp_EQ, |
| 171 | options::OPT_flang_experimental_hlfir, |
| 172 | options::OPT_flang_deprecated_no_hlfir, |
| 173 | options::OPT_fno_ppc_native_vec_elem_order, |
| 174 | options::OPT_fppc_native_vec_elem_order, options::OPT_finit_global_zero, |
| 175 | options::OPT_fno_init_global_zero, options::OPT_frepack_arrays, |
| 176 | options::OPT_fno_repack_arrays, |
| 177 | options::OPT_frepack_arrays_contiguity_EQ, |
| 178 | options::OPT_fstack_repack_arrays, options::OPT_fno_stack_repack_arrays, |
| 179 | options::OPT_ftime_report, options::OPT_ftime_report_EQ, |
| 180 | options::OPT_funroll_loops, options::OPT_fno_unroll_loops}); |
| 181 | } |
| 182 | |
| 183 | void Flang::addPicOptions(const ArgList &Args, ArgStringList &CmdArgs) const { |
| 184 | // ParsePICArgs parses -fPIC/-fPIE and their variants and returns a tuple of |
| 185 | // (RelocationModel, PICLevel, IsPIE). |
| 186 | llvm::Reloc::Model RelocationModel; |
| 187 | unsigned PICLevel; |
| 188 | bool IsPIE; |
| 189 | std::tie(args&: RelocationModel, args&: PICLevel, args&: IsPIE) = |
| 190 | ParsePICArgs(ToolChain: getToolChain(), Args); |
| 191 | |
| 192 | if (auto *RMName = RelocationModelName(Model: RelocationModel)) { |
| 193 | CmdArgs.push_back(Elt: "-mrelocation-model" ); |
| 194 | CmdArgs.push_back(Elt: RMName); |
| 195 | } |
| 196 | if (PICLevel > 0) { |
| 197 | CmdArgs.push_back(Elt: "-pic-level" ); |
| 198 | CmdArgs.push_back(Elt: PICLevel == 1 ? "1" : "2" ); |
| 199 | if (IsPIE) |
| 200 | CmdArgs.push_back(Elt: "-pic-is-pie" ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void Flang::AddAArch64TargetArgs(const ArgList &Args, |
| 205 | ArgStringList &CmdArgs) const { |
| 206 | // Handle -msve_vector_bits=<bits> |
| 207 | if (Arg *A = Args.getLastArg(Ids: options::OPT_msve_vector_bits_EQ)) { |
| 208 | StringRef Val = A->getValue(); |
| 209 | const Driver &D = getToolChain().getDriver(); |
| 210 | if (Val == "128" || Val == "256" || Val == "512" || Val == "1024" || |
| 211 | Val == "2048" || Val == "128+" || Val == "256+" || Val == "512+" || |
| 212 | Val == "1024+" || Val == "2048+" ) { |
| 213 | unsigned Bits = 0; |
| 214 | if (!Val.consume_back(Suffix: "+" )) { |
| 215 | [[maybe_unused]] bool Invalid = Val.getAsInteger(Radix: 10, Result&: Bits); |
| 216 | assert(!Invalid && "Failed to parse value" ); |
| 217 | CmdArgs.push_back( |
| 218 | Elt: Args.MakeArgString(Str: "-mvscale-max=" + llvm::Twine(Bits / 128))); |
| 219 | } |
| 220 | |
| 221 | [[maybe_unused]] bool Invalid = Val.getAsInteger(Radix: 10, Result&: Bits); |
| 222 | assert(!Invalid && "Failed to parse value" ); |
| 223 | CmdArgs.push_back( |
| 224 | Elt: Args.MakeArgString(Str: "-mvscale-min=" + llvm::Twine(Bits / 128))); |
| 225 | // Silently drop requests for vector-length agnostic code as it's implied. |
| 226 | } else if (Val != "scalable" ) |
| 227 | // Handle the unsupported values passed to msve-vector-bits. |
| 228 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 229 | << A->getSpelling() << Val; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | void Flang::AddLoongArch64TargetArgs(const ArgList &Args, |
| 234 | ArgStringList &CmdArgs) const { |
| 235 | const Driver &D = getToolChain().getDriver(); |
| 236 | // Currently, flang only support `-mabi=lp64d` in LoongArch64. |
| 237 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_mabi_EQ)) { |
| 238 | StringRef V = A->getValue(); |
| 239 | if (V != "lp64d" ) { |
| 240 | D.Diag(DiagID: diag::err_drv_argument_not_allowed_with) << "-mabi" << V; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_mannotate_tablejump, |
| 245 | Ids: options::OPT_mno_annotate_tablejump)) { |
| 246 | if (A->getOption().matches(ID: options::OPT_mannotate_tablejump)) { |
| 247 | CmdArgs.push_back(Elt: "-mllvm" ); |
| 248 | CmdArgs.push_back(Elt: "-loongarch-annotate-tablejump" ); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void Flang::AddPPCTargetArgs(const ArgList &Args, |
| 254 | ArgStringList &CmdArgs) const { |
| 255 | const Driver &D = getToolChain().getDriver(); |
| 256 | bool VecExtabi = false; |
| 257 | |
| 258 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_mabi_EQ)) { |
| 259 | StringRef V = A->getValue(); |
| 260 | if (V == "vec-extabi" ) |
| 261 | VecExtabi = true; |
| 262 | else if (V == "vec-default" ) |
| 263 | VecExtabi = false; |
| 264 | else |
| 265 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 266 | << A->getSpelling() << V; |
| 267 | } |
| 268 | |
| 269 | const llvm::Triple &T = getToolChain().getTriple(); |
| 270 | if (VecExtabi) { |
| 271 | if (!T.isOSAIX()) { |
| 272 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 273 | << "-mabi=vec-extabi" << T.str(); |
| 274 | } |
| 275 | CmdArgs.push_back(Elt: "-mabi=vec-extabi" ); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void Flang::AddRISCVTargetArgs(const ArgList &Args, |
| 280 | ArgStringList &CmdArgs) const { |
| 281 | const Driver &D = getToolChain().getDriver(); |
| 282 | const llvm::Triple &Triple = getToolChain().getTriple(); |
| 283 | |
| 284 | StringRef ABIName = riscv::getRISCVABI(Args, Triple); |
| 285 | if (ABIName == "lp64" || ABIName == "lp64f" || ABIName == "lp64d" ) |
| 286 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mabi=" + ABIName)); |
| 287 | else |
| 288 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) << "-mabi=" << ABIName; |
| 289 | |
| 290 | // Handle -mrvv-vector-bits=<bits> |
| 291 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mrvv_vector_bits_EQ)) { |
| 292 | StringRef Val = A->getValue(); |
| 293 | |
| 294 | // Get minimum VLen from march. |
| 295 | unsigned MinVLen = 0; |
| 296 | std::string Arch = riscv::getRISCVArch(Args, Triple); |
| 297 | auto ISAInfo = llvm::RISCVISAInfo::parseArchString( |
| 298 | Arch, /*EnableExperimentalExtensions*/ EnableExperimentalExtension: true); |
| 299 | // Ignore parsing error. |
| 300 | if (!errorToBool(Err: ISAInfo.takeError())) |
| 301 | MinVLen = (*ISAInfo)->getMinVLen(); |
| 302 | |
| 303 | // If the value is "zvl", use MinVLen from march. Otherwise, try to parse |
| 304 | // as integer as long as we have a MinVLen. |
| 305 | unsigned Bits = 0; |
| 306 | if (Val == "zvl" && MinVLen >= llvm::RISCV::RVVBitsPerBlock) { |
| 307 | Bits = MinVLen; |
| 308 | } else if (!Val.getAsInteger(Radix: 10, Result&: Bits)) { |
| 309 | // Only accept power of 2 values beteen RVVBitsPerBlock and 65536 that |
| 310 | // at least MinVLen. |
| 311 | if (Bits < MinVLen || Bits < llvm::RISCV::RVVBitsPerBlock || |
| 312 | Bits > 65536 || !llvm::isPowerOf2_32(Value: Bits)) |
| 313 | Bits = 0; |
| 314 | } |
| 315 | |
| 316 | // If we got a valid value try to use it. |
| 317 | if (Bits != 0) { |
| 318 | unsigned VScaleMin = Bits / llvm::RISCV::RVVBitsPerBlock; |
| 319 | CmdArgs.push_back( |
| 320 | Elt: Args.MakeArgString(Str: "-mvscale-max=" + llvm::Twine(VScaleMin))); |
| 321 | CmdArgs.push_back( |
| 322 | Elt: Args.MakeArgString(Str: "-mvscale-min=" + llvm::Twine(VScaleMin))); |
| 323 | } else if (Val != "scalable" ) { |
| 324 | // Handle the unsupported values passed to mrvv-vector-bits. |
| 325 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 326 | << A->getSpelling() << Val; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | void Flang::AddX86_64TargetArgs(const ArgList &Args, |
| 332 | ArgStringList &CmdArgs) const { |
| 333 | if (Arg *A = Args.getLastArg(Ids: options::OPT_masm_EQ)) { |
| 334 | StringRef Value = A->getValue(); |
| 335 | if (Value == "intel" || Value == "att" ) { |
| 336 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mllvm" )); |
| 337 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-x86-asm-syntax=" + Value)); |
| 338 | } else { |
| 339 | getToolChain().getDriver().Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 340 | << A->getSpelling() << Value; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | static void addVSDefines(const ToolChain &TC, const ArgList &Args, |
| 346 | ArgStringList &CmdArgs) { |
| 347 | |
| 348 | unsigned ver = 0; |
| 349 | const VersionTuple vt = TC.computeMSVCVersion(D: nullptr, Args); |
| 350 | ver = vt.getMajor() * 10000000 + vt.getMinor().value_or(u: 0) * 100000 + |
| 351 | vt.getSubminor().value_or(u: 0); |
| 352 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-D_MSC_VER=" + Twine(ver / 100000))); |
| 353 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-D_MSC_FULL_VER=" + Twine(ver))); |
| 354 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-D_WIN32" )); |
| 355 | |
| 356 | const llvm::Triple &triple = TC.getTriple(); |
| 357 | if (triple.isAArch64()) { |
| 358 | CmdArgs.push_back(Elt: "-D_M_ARM64=1" ); |
| 359 | } else if (triple.isX86() && triple.isArch32Bit()) { |
| 360 | CmdArgs.push_back(Elt: "-D_M_IX86=600" ); |
| 361 | } else if (triple.isX86() && triple.isArch64Bit()) { |
| 362 | CmdArgs.push_back(Elt: "-D_M_X64=100" ); |
| 363 | } else { |
| 364 | llvm_unreachable( |
| 365 | "Flang on Windows only supports X86_32, X86_64 and AArch64" ); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | static void processVSRuntimeLibrary(const ToolChain &TC, const ArgList &Args, |
| 370 | ArgStringList &CmdArgs) { |
| 371 | assert(TC.getTriple().isKnownWindowsMSVCEnvironment() && |
| 372 | "can only add VS runtime library on Windows!" ); |
| 373 | |
| 374 | // Flang/Clang (including clang-cl) -compiled programs targeting the MSVC ABI |
| 375 | // should only depend on msv(u)crt. LLVM still emits libgcc/compiler-rt |
| 376 | // functions in some cases like 128-bit integer math (__udivti3, __modti3, |
| 377 | // __fixsfti, __floattidf, ...) that msvc does not support. We are injecting a |
| 378 | // dependency to Compiler-RT's builtin library where these are implemented. |
| 379 | CmdArgs.push_back(Elt: Args.MakeArgString( |
| 380 | Str: "--dependent-lib=" + TC.getCompilerRTBasename(Args, Component: "builtins" ))); |
| 381 | |
| 382 | unsigned RTOptionID = options::OPT__SLASH_MT; |
| 383 | if (auto *rtl = Args.getLastArg(Ids: options::OPT_fms_runtime_lib_EQ)) { |
| 384 | RTOptionID = llvm::StringSwitch<unsigned>(rtl->getValue()) |
| 385 | .Case(S: "static" , Value: options::OPT__SLASH_MT) |
| 386 | .Case(S: "static_dbg" , Value: options::OPT__SLASH_MTd) |
| 387 | .Case(S: "dll" , Value: options::OPT__SLASH_MD) |
| 388 | .Case(S: "dll_dbg" , Value: options::OPT__SLASH_MDd) |
| 389 | .Default(Value: options::OPT__SLASH_MT); |
| 390 | } |
| 391 | switch (RTOptionID) { |
| 392 | case options::OPT__SLASH_MT: |
| 393 | CmdArgs.push_back(Elt: "-D_MT" ); |
| 394 | CmdArgs.push_back(Elt: "--dependent-lib=libcmt" ); |
| 395 | CmdArgs.push_back(Elt: "--dependent-lib=flang_rt.runtime.static.lib" ); |
| 396 | break; |
| 397 | case options::OPT__SLASH_MTd: |
| 398 | CmdArgs.push_back(Elt: "-D_MT" ); |
| 399 | CmdArgs.push_back(Elt: "-D_DEBUG" ); |
| 400 | CmdArgs.push_back(Elt: "--dependent-lib=libcmtd" ); |
| 401 | CmdArgs.push_back(Elt: "--dependent-lib=flang_rt.runtime.static_dbg.lib" ); |
| 402 | break; |
| 403 | case options::OPT__SLASH_MD: |
| 404 | CmdArgs.push_back(Elt: "-D_MT" ); |
| 405 | CmdArgs.push_back(Elt: "-D_DLL" ); |
| 406 | CmdArgs.push_back(Elt: "--dependent-lib=msvcrt" ); |
| 407 | CmdArgs.push_back(Elt: "--dependent-lib=flang_rt.runtime.dynamic.lib" ); |
| 408 | break; |
| 409 | case options::OPT__SLASH_MDd: |
| 410 | CmdArgs.push_back(Elt: "-D_MT" ); |
| 411 | CmdArgs.push_back(Elt: "-D_DEBUG" ); |
| 412 | CmdArgs.push_back(Elt: "-D_DLL" ); |
| 413 | CmdArgs.push_back(Elt: "--dependent-lib=msvcrtd" ); |
| 414 | CmdArgs.push_back(Elt: "--dependent-lib=flang_rt.runtime.dynamic_dbg.lib" ); |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void Flang::AddAMDGPUTargetArgs(const ArgList &Args, |
| 420 | ArgStringList &CmdArgs) const { |
| 421 | if (Arg *A = Args.getLastArg(Ids: options::OPT_mcode_object_version_EQ)) { |
| 422 | StringRef Val = A->getValue(); |
| 423 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mcode-object-version=" + Val)); |
| 424 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mllvm" )); |
| 425 | CmdArgs.push_back( |
| 426 | Elt: Args.MakeArgString(Str: "--amdhsa-code-object-version=" + Val)); |
| 427 | } |
| 428 | |
| 429 | const ToolChain &TC = getToolChain(); |
| 430 | TC.addClangTargetOptions(DriverArgs: Args, CC1Args&: CmdArgs, DeviceOffloadKind: Action::OffloadKind::OFK_OpenMP); |
| 431 | } |
| 432 | |
| 433 | void Flang::addTargetOptions(const ArgList &Args, |
| 434 | ArgStringList &CmdArgs) const { |
| 435 | const ToolChain &TC = getToolChain(); |
| 436 | const llvm::Triple &Triple = TC.getEffectiveTriple(); |
| 437 | const Driver &D = TC.getDriver(); |
| 438 | |
| 439 | std::string CPU = getCPUName(D, Args, T: Triple); |
| 440 | if (!CPU.empty()) { |
| 441 | CmdArgs.push_back(Elt: "-target-cpu" ); |
| 442 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: CPU)); |
| 443 | } |
| 444 | |
| 445 | addOutlineAtomicsArgs(D, TC: getToolChain(), Args, CmdArgs, Triple); |
| 446 | |
| 447 | // Add the target features. |
| 448 | switch (TC.getArch()) { |
| 449 | default: |
| 450 | break; |
| 451 | case llvm::Triple::aarch64: |
| 452 | getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ ForAS: false); |
| 453 | AddAArch64TargetArgs(Args, CmdArgs); |
| 454 | break; |
| 455 | |
| 456 | case llvm::Triple::r600: |
| 457 | case llvm::Triple::amdgcn: |
| 458 | getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ ForAS: false); |
| 459 | AddAMDGPUTargetArgs(Args, CmdArgs); |
| 460 | break; |
| 461 | case llvm::Triple::riscv64: |
| 462 | getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ ForAS: false); |
| 463 | AddRISCVTargetArgs(Args, CmdArgs); |
| 464 | break; |
| 465 | case llvm::Triple::x86_64: |
| 466 | getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ ForAS: false); |
| 467 | AddX86_64TargetArgs(Args, CmdArgs); |
| 468 | break; |
| 469 | case llvm::Triple::ppc: |
| 470 | case llvm::Triple::ppc64: |
| 471 | case llvm::Triple::ppc64le: |
| 472 | AddPPCTargetArgs(Args, CmdArgs); |
| 473 | break; |
| 474 | case llvm::Triple::loongarch64: |
| 475 | getTargetFeatures(D, Triple, Args, CmdArgs, /*ForAs*/ ForAS: false); |
| 476 | AddLoongArch64TargetArgs(Args, CmdArgs); |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fveclib)) { |
| 481 | StringRef Name = A->getValue(); |
| 482 | if (Name == "SVML" ) { |
| 483 | if (Triple.getArch() != llvm::Triple::x86 && |
| 484 | Triple.getArch() != llvm::Triple::x86_64) |
| 485 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 486 | << Name << Triple.getArchName(); |
| 487 | } else if (Name == "AMDLIBM" ) { |
| 488 | if (Triple.getArch() != llvm::Triple::x86 && |
| 489 | Triple.getArch() != llvm::Triple::x86_64) |
| 490 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 491 | << Name << Triple.getArchName(); |
| 492 | } else if (Name == "libmvec" ) { |
| 493 | if (Triple.getArch() != llvm::Triple::x86 && |
| 494 | Triple.getArch() != llvm::Triple::x86_64 && |
| 495 | Triple.getArch() != llvm::Triple::aarch64 && |
| 496 | Triple.getArch() != llvm::Triple::aarch64_be) |
| 497 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 498 | << Name << Triple.getArchName(); |
| 499 | } else if (Name == "SLEEF" || Name == "ArmPL" ) { |
| 500 | if (Triple.getArch() != llvm::Triple::aarch64 && |
| 501 | Triple.getArch() != llvm::Triple::aarch64_be) |
| 502 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 503 | << Name << Triple.getArchName(); |
| 504 | } |
| 505 | |
| 506 | if (Triple.isOSDarwin()) { |
| 507 | // flang doesn't currently suport nostdlib, nodefaultlibs. Adding these |
| 508 | // here incase they are added someday |
| 509 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) { |
| 510 | if (A->getValue() == StringRef{"Accelerate" }) { |
| 511 | CmdArgs.push_back(Elt: "-framework" ); |
| 512 | CmdArgs.push_back(Elt: "Accelerate" ); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | A->render(Args, Output&: CmdArgs); |
| 517 | } |
| 518 | |
| 519 | if (Triple.isKnownWindowsMSVCEnvironment()) { |
| 520 | processVSRuntimeLibrary(TC, Args, CmdArgs); |
| 521 | addVSDefines(TC, Args, CmdArgs); |
| 522 | } |
| 523 | |
| 524 | // TODO: Add target specific flags, ABI, mtune option etc. |
| 525 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_mtune_EQ)) { |
| 526 | CmdArgs.push_back(Elt: "-tune-cpu" ); |
| 527 | if (A->getValue() == StringRef{"native" }) |
| 528 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: llvm::sys::getHostCPUName())); |
| 529 | else |
| 530 | CmdArgs.push_back(Elt: A->getValue()); |
| 531 | } |
| 532 | |
| 533 | Args.addAllArgs(Output&: CmdArgs, |
| 534 | Ids: {options::OPT_fverbose_asm, options::OPT_fno_verbose_asm}); |
| 535 | } |
| 536 | |
| 537 | void Flang::addOffloadOptions(Compilation &C, const InputInfoList &Inputs, |
| 538 | const JobAction &JA, const ArgList &Args, |
| 539 | ArgStringList &CmdArgs) const { |
| 540 | bool IsOpenMPDevice = JA.isDeviceOffloading(OKind: Action::OFK_OpenMP); |
| 541 | bool IsHostOffloadingAction = JA.isHostOffloading(OKind: Action::OFK_OpenMP) || |
| 542 | JA.isHostOffloading(OKind: C.getActiveOffloadKinds()); |
| 543 | |
| 544 | // Skips the primary input file, which is the input file that the compilation |
| 545 | // proccess will be executed upon (e.g. the host bitcode file) and |
| 546 | // adds other secondary input (e.g. device bitcode files for embedding to the |
| 547 | // -fembed-offload-object argument or the host IR file for proccessing |
| 548 | // during device compilation to the fopenmp-host-ir-file-path argument via |
| 549 | // OpenMPDeviceInput). This is condensed logic from the ConstructJob |
| 550 | // function inside of the Clang driver for pushing on further input arguments |
| 551 | // needed for offloading during various phases of compilation. |
| 552 | for (size_t i = 1; i < Inputs.size(); ++i) { |
| 553 | if (Inputs[i].getType() == types::TY_Nothing) { |
| 554 | // contains nothing, so it's skippable |
| 555 | } else if (IsHostOffloadingAction) { |
| 556 | CmdArgs.push_back( |
| 557 | Elt: Args.MakeArgString(Str: "-fembed-offload-object=" + |
| 558 | getToolChain().getInputFilename(Input: Inputs[i]))); |
| 559 | } else if (IsOpenMPDevice) { |
| 560 | if (Inputs[i].getFilename()) { |
| 561 | CmdArgs.push_back(Elt: "-fopenmp-host-ir-file-path" ); |
| 562 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Inputs[i].getFilename())); |
| 563 | } else { |
| 564 | llvm_unreachable("missing openmp host-ir file for device offloading" ); |
| 565 | } |
| 566 | } else { |
| 567 | llvm_unreachable( |
| 568 | "unexpectedly given multiple inputs or given unknown input" ); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (IsOpenMPDevice) { |
| 573 | // -fopenmp-is-target-device is passed along to tell the frontend that it is |
| 574 | // generating code for a device, so that only the relevant code is emitted. |
| 575 | CmdArgs.push_back(Elt: "-fopenmp-is-target-device" ); |
| 576 | |
| 577 | // When in OpenMP offloading mode, enable debugging on the device. |
| 578 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_target_debug_EQ); |
| 579 | if (Args.hasFlag(Pos: options::OPT_fopenmp_target_debug, |
| 580 | Neg: options::OPT_fno_openmp_target_debug, /*Default=*/false)) |
| 581 | CmdArgs.push_back(Elt: "-fopenmp-target-debug" ); |
| 582 | |
| 583 | // When in OpenMP offloading mode, forward assumptions information about |
| 584 | // thread and team counts in the device. |
| 585 | if (Args.hasFlag(Pos: options::OPT_fopenmp_assume_teams_oversubscription, |
| 586 | Neg: options::OPT_fno_openmp_assume_teams_oversubscription, |
| 587 | /*Default=*/false)) |
| 588 | CmdArgs.push_back(Elt: "-fopenmp-assume-teams-oversubscription" ); |
| 589 | if (Args.hasFlag(Pos: options::OPT_fopenmp_assume_threads_oversubscription, |
| 590 | Neg: options::OPT_fno_openmp_assume_threads_oversubscription, |
| 591 | /*Default=*/false)) |
| 592 | CmdArgs.push_back(Elt: "-fopenmp-assume-threads-oversubscription" ); |
| 593 | if (Args.hasArg(Ids: options::OPT_fopenmp_assume_no_thread_state)) |
| 594 | CmdArgs.push_back(Elt: "-fopenmp-assume-no-thread-state" ); |
| 595 | if (Args.hasArg(Ids: options::OPT_fopenmp_assume_no_nested_parallelism)) |
| 596 | CmdArgs.push_back(Elt: "-fopenmp-assume-no-nested-parallelism" ); |
| 597 | if (!Args.hasFlag(Pos: options::OPT_offloadlib, Neg: options::OPT_no_offloadlib, |
| 598 | Default: true)) |
| 599 | CmdArgs.push_back(Elt: "-nogpulib" ); |
| 600 | } |
| 601 | |
| 602 | addOpenMPHostOffloadingArgs(C, JA, Args, CmdArgs); |
| 603 | } |
| 604 | |
| 605 | static void addFloatingPointOptions(const Driver &D, const ArgList &Args, |
| 606 | ArgStringList &CmdArgs) { |
| 607 | StringRef FPContract; |
| 608 | bool HonorINFs = true; |
| 609 | bool HonorNaNs = true; |
| 610 | bool ApproxFunc = false; |
| 611 | bool SignedZeros = true; |
| 612 | bool AssociativeMath = false; |
| 613 | bool ReciprocalMath = false; |
| 614 | |
| 615 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_ffp_contract)) { |
| 616 | const StringRef Val = A->getValue(); |
| 617 | if (Val == "fast" || Val == "off" ) { |
| 618 | FPContract = Val; |
| 619 | } else if (Val == "on" ) { |
| 620 | // Warn instead of error because users might have makefiles written for |
| 621 | // gfortran (which accepts -ffp-contract=on) |
| 622 | D.Diag(DiagID: diag::warn_drv_unsupported_option_for_flang) |
| 623 | << Val << A->getOption().getName() << "off" ; |
| 624 | FPContract = "off" ; |
| 625 | } else |
| 626 | // Clang's "fast-honor-pragmas" option is not supported because it is |
| 627 | // non-standard |
| 628 | D.Diag(DiagID: diag::err_drv_unsupported_option_argument) |
| 629 | << A->getSpelling() << Val; |
| 630 | } |
| 631 | |
| 632 | for (const Arg *A : Args) { |
| 633 | auto optId = A->getOption().getID(); |
| 634 | switch (optId) { |
| 635 | // if this isn't an FP option, skip the claim below |
| 636 | default: |
| 637 | continue; |
| 638 | |
| 639 | case options::OPT_fhonor_infinities: |
| 640 | HonorINFs = true; |
| 641 | break; |
| 642 | case options::OPT_fno_honor_infinities: |
| 643 | HonorINFs = false; |
| 644 | break; |
| 645 | case options::OPT_fhonor_nans: |
| 646 | HonorNaNs = true; |
| 647 | break; |
| 648 | case options::OPT_fno_honor_nans: |
| 649 | HonorNaNs = false; |
| 650 | break; |
| 651 | case options::OPT_fapprox_func: |
| 652 | ApproxFunc = true; |
| 653 | break; |
| 654 | case options::OPT_fno_approx_func: |
| 655 | ApproxFunc = false; |
| 656 | break; |
| 657 | case options::OPT_fsigned_zeros: |
| 658 | SignedZeros = true; |
| 659 | break; |
| 660 | case options::OPT_fno_signed_zeros: |
| 661 | SignedZeros = false; |
| 662 | break; |
| 663 | case options::OPT_fassociative_math: |
| 664 | AssociativeMath = true; |
| 665 | break; |
| 666 | case options::OPT_fno_associative_math: |
| 667 | AssociativeMath = false; |
| 668 | break; |
| 669 | case options::OPT_freciprocal_math: |
| 670 | ReciprocalMath = true; |
| 671 | break; |
| 672 | case options::OPT_fno_reciprocal_math: |
| 673 | ReciprocalMath = false; |
| 674 | break; |
| 675 | case options::OPT_Ofast: |
| 676 | [[fallthrough]]; |
| 677 | case options::OPT_ffast_math: |
| 678 | HonorINFs = false; |
| 679 | HonorNaNs = false; |
| 680 | AssociativeMath = true; |
| 681 | ReciprocalMath = true; |
| 682 | ApproxFunc = true; |
| 683 | SignedZeros = false; |
| 684 | FPContract = "fast" ; |
| 685 | break; |
| 686 | case options::OPT_fno_fast_math: |
| 687 | HonorINFs = true; |
| 688 | HonorNaNs = true; |
| 689 | AssociativeMath = false; |
| 690 | ReciprocalMath = false; |
| 691 | ApproxFunc = false; |
| 692 | SignedZeros = true; |
| 693 | // -fno-fast-math should undo -ffast-math so I return FPContract to the |
| 694 | // default. It is important to check it is "fast" (the default) so that |
| 695 | // --ffp-contract=off -fno-fast-math --> -ffp-contract=off |
| 696 | if (FPContract == "fast" ) |
| 697 | FPContract = "" ; |
| 698 | break; |
| 699 | } |
| 700 | |
| 701 | // If we handled this option claim it |
| 702 | A->claim(); |
| 703 | } |
| 704 | |
| 705 | StringRef Recip = parseMRecipOption(Diags&: D.getDiags(), Args); |
| 706 | if (!Recip.empty()) |
| 707 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-mrecip=" + Recip)); |
| 708 | |
| 709 | if (!HonorINFs && !HonorNaNs && AssociativeMath && ReciprocalMath && |
| 710 | ApproxFunc && !SignedZeros && |
| 711 | (FPContract == "fast" || FPContract.empty())) { |
| 712 | CmdArgs.push_back(Elt: "-ffast-math" ); |
| 713 | return; |
| 714 | } |
| 715 | |
| 716 | if (!FPContract.empty()) |
| 717 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-ffp-contract=" + FPContract)); |
| 718 | |
| 719 | if (!HonorINFs) |
| 720 | CmdArgs.push_back(Elt: "-menable-no-infs" ); |
| 721 | |
| 722 | if (!HonorNaNs) |
| 723 | CmdArgs.push_back(Elt: "-menable-no-nans" ); |
| 724 | |
| 725 | if (ApproxFunc) |
| 726 | CmdArgs.push_back(Elt: "-fapprox-func" ); |
| 727 | |
| 728 | if (!SignedZeros) |
| 729 | CmdArgs.push_back(Elt: "-fno-signed-zeros" ); |
| 730 | |
| 731 | if (AssociativeMath && !SignedZeros) |
| 732 | CmdArgs.push_back(Elt: "-mreassociate" ); |
| 733 | |
| 734 | if (ReciprocalMath) |
| 735 | CmdArgs.push_back(Elt: "-freciprocal-math" ); |
| 736 | } |
| 737 | |
| 738 | static void (const ArgList &Args, ArgStringList &CmdArgs, |
| 739 | const InputInfo &Input) { |
| 740 | StringRef Format = "yaml" ; |
| 741 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_fsave_optimization_record_EQ)) |
| 742 | Format = A->getValue(); |
| 743 | |
| 744 | CmdArgs.push_back(Elt: "-opt-record-file" ); |
| 745 | |
| 746 | const Arg *A = Args.getLastArg(Ids: options::OPT_foptimization_record_file_EQ); |
| 747 | if (A) { |
| 748 | CmdArgs.push_back(Elt: A->getValue()); |
| 749 | } else { |
| 750 | SmallString<128> F; |
| 751 | |
| 752 | if (Args.hasArg(Ids: options::OPT_c) || Args.hasArg(Ids: options::OPT_S)) { |
| 753 | if (Arg *FinalOutput = Args.getLastArg(Ids: options::OPT_o)) |
| 754 | F = FinalOutput->getValue(); |
| 755 | } |
| 756 | |
| 757 | if (F.empty()) { |
| 758 | // Use the input filename. |
| 759 | F = llvm::sys::path::stem(path: Input.getBaseInput()); |
| 760 | } |
| 761 | |
| 762 | SmallString<32> Extension; |
| 763 | Extension += "opt." ; |
| 764 | Extension += Format; |
| 765 | |
| 766 | llvm::sys::path::replace_extension(path&: F, extension: Extension); |
| 767 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: F)); |
| 768 | } |
| 769 | |
| 770 | if (const Arg *A = |
| 771 | Args.getLastArg(Ids: options::OPT_foptimization_record_passes_EQ)) { |
| 772 | CmdArgs.push_back(Elt: "-opt-record-passes" ); |
| 773 | CmdArgs.push_back(Elt: A->getValue()); |
| 774 | } |
| 775 | |
| 776 | if (!Format.empty()) { |
| 777 | CmdArgs.push_back(Elt: "-opt-record-format" ); |
| 778 | CmdArgs.push_back(Elt: Format.data()); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | void Flang::ConstructJob(Compilation &C, const JobAction &JA, |
| 783 | const InputInfo &Output, const InputInfoList &Inputs, |
| 784 | const ArgList &Args, const char *LinkingOutput) const { |
| 785 | const auto &TC = getToolChain(); |
| 786 | const llvm::Triple &Triple = TC.getEffectiveTriple(); |
| 787 | const std::string &TripleStr = Triple.getTriple(); |
| 788 | |
| 789 | const Driver &D = TC.getDriver(); |
| 790 | ArgStringList CmdArgs; |
| 791 | DiagnosticsEngine &Diags = D.getDiags(); |
| 792 | |
| 793 | // Invoke ourselves in -fc1 mode. |
| 794 | CmdArgs.push_back(Elt: "-fc1" ); |
| 795 | |
| 796 | // Add the "effective" target triple. |
| 797 | CmdArgs.push_back(Elt: "-triple" ); |
| 798 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: TripleStr)); |
| 799 | |
| 800 | if (isa<PreprocessJobAction>(Val: JA)) { |
| 801 | CmdArgs.push_back(Elt: "-E" ); |
| 802 | if (Args.getLastArg(Ids: options::OPT_dM)) { |
| 803 | CmdArgs.push_back(Elt: "-dM" ); |
| 804 | } |
| 805 | } else if (isa<CompileJobAction>(Val: JA) || isa<BackendJobAction>(Val: JA)) { |
| 806 | if (JA.getType() == types::TY_Nothing) { |
| 807 | CmdArgs.push_back(Elt: "-fsyntax-only" ); |
| 808 | } else if (JA.getType() == types::TY_AST) { |
| 809 | CmdArgs.push_back(Elt: "-emit-ast" ); |
| 810 | } else if (JA.getType() == types::TY_LLVM_IR || |
| 811 | JA.getType() == types::TY_LTO_IR) { |
| 812 | CmdArgs.push_back(Elt: "-emit-llvm" ); |
| 813 | } else if (JA.getType() == types::TY_LLVM_BC || |
| 814 | JA.getType() == types::TY_LTO_BC) { |
| 815 | CmdArgs.push_back(Elt: "-emit-llvm-bc" ); |
| 816 | } else if (JA.getType() == types::TY_PP_Asm) { |
| 817 | CmdArgs.push_back(Elt: "-S" ); |
| 818 | } else { |
| 819 | assert(false && "Unexpected output type!" ); |
| 820 | } |
| 821 | } else if (isa<AssembleJobAction>(Val: JA)) { |
| 822 | CmdArgs.push_back(Elt: "-emit-obj" ); |
| 823 | } else if (isa<PrecompileJobAction>(Val: JA)) { |
| 824 | // The precompile job action is only needed for options such as -mcpu=help. |
| 825 | // Those will already have been handled by the fc1 driver. |
| 826 | } else { |
| 827 | assert(false && "Unexpected action class for Flang tool." ); |
| 828 | } |
| 829 | |
| 830 | const InputInfo &Input = Inputs[0]; |
| 831 | types::ID InputType = Input.getType(); |
| 832 | |
| 833 | // Add preprocessing options like -I, -D, etc. if we are using the |
| 834 | // preprocessor (i.e. skip when dealing with e.g. binary files). |
| 835 | if (types::getPreprocessedType(Id: InputType) != types::TY_INVALID) |
| 836 | addPreprocessingOptions(Args, CmdArgs); |
| 837 | |
| 838 | addFortranDialectOptions(Args, CmdArgs); |
| 839 | |
| 840 | // 'flang -E' always produces output that is suitable for use as fixed form |
| 841 | // Fortran. However it is only valid free form source if the original is also |
| 842 | // free form. Ensure this logic does not incorrectly assume fixed-form for |
| 843 | // cases where it shouldn't, such as `flang -x f95 foo.f90`. |
| 844 | bool isAtemporaryPreprocessedFile = |
| 845 | Input.isFilename() && |
| 846 | llvm::sys::path::extension(path: Input.getFilename()) |
| 847 | .ends_with(Suffix: types::getTypeTempSuffix(Id: InputType, /*CLStyle=*/false)); |
| 848 | if (InputType == types::TY_PP_Fortran && isAtemporaryPreprocessedFile && |
| 849 | !Args.getLastArg(Ids: options::OPT_ffixed_form, Ids: options::OPT_ffree_form)) |
| 850 | CmdArgs.push_back(Elt: "-ffixed-form" ); |
| 851 | |
| 852 | handleColorDiagnosticsArgs(D, Args, CmdArgs); |
| 853 | |
| 854 | // LTO mode is parsed by the Clang driver library. |
| 855 | LTOKind LTOMode = D.getLTOMode(); |
| 856 | assert(LTOMode != LTOK_Unknown && "Unknown LTO mode." ); |
| 857 | if (LTOMode == LTOK_Full) |
| 858 | CmdArgs.push_back(Elt: "-flto=full" ); |
| 859 | else if (LTOMode == LTOK_Thin) { |
| 860 | Diags.Report( |
| 861 | DiagID: Diags.getCustomDiagID(L: DiagnosticsEngine::Warning, |
| 862 | FormatString: "the option '-flto=thin' is a work in progress" )); |
| 863 | CmdArgs.push_back(Elt: "-flto=thin" ); |
| 864 | } |
| 865 | |
| 866 | // -fPIC and related options. |
| 867 | addPicOptions(Args, CmdArgs); |
| 868 | |
| 869 | // Floating point related options |
| 870 | addFloatingPointOptions(D, Args, CmdArgs); |
| 871 | |
| 872 | // Add target args, features, etc. |
| 873 | addTargetOptions(Args, CmdArgs); |
| 874 | |
| 875 | llvm::Reloc::Model RelocationModel = |
| 876 | std::get<0>(t: ParsePICArgs(ToolChain: getToolChain(), Args)); |
| 877 | // Add MCModel information |
| 878 | addMCModel(D, Args, Triple, RelocationModel, CmdArgs); |
| 879 | |
| 880 | // Add Codegen options |
| 881 | addCodegenOptions(Args, CmdArgs); |
| 882 | |
| 883 | // Add R Group options |
| 884 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_R_Group); |
| 885 | |
| 886 | // Remarks can be enabled with any of the `-f.*optimization-record.*` flags. |
| 887 | if (willEmitRemarks(Args)) |
| 888 | renderRemarksOptions(Args, CmdArgs, Input); |
| 889 | |
| 890 | // Add other compile options |
| 891 | addOtherOptions(Args, CmdArgs); |
| 892 | |
| 893 | // Disable all warnings |
| 894 | // TODO: Handle interactions between -w, -pedantic, -Wall, -WOption |
| 895 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_w); |
| 896 | |
| 897 | // recognise options: fprofile-generate -fprofile-use= |
| 898 | Args.addAllArgs( |
| 899 | Output&: CmdArgs, Ids: {options::OPT_fprofile_generate, options::OPT_fprofile_use_EQ}); |
| 900 | |
| 901 | // Forward flags for OpenMP. We don't do this if the current action is an |
| 902 | // device offloading action other than OpenMP. |
| 903 | if (Args.hasFlag(Pos: options::OPT_fopenmp, PosAlias: options::OPT_fopenmp_EQ, |
| 904 | Neg: options::OPT_fno_openmp, Default: false) && |
| 905 | (JA.isDeviceOffloading(OKind: Action::OFK_None) || |
| 906 | JA.isDeviceOffloading(OKind: Action::OFK_OpenMP))) { |
| 907 | switch (D.getOpenMPRuntime(Args)) { |
| 908 | case Driver::OMPRT_OMP: |
| 909 | case Driver::OMPRT_IOMP5: |
| 910 | // Clang can generate useful OpenMP code for these two runtime libraries. |
| 911 | CmdArgs.push_back(Elt: "-fopenmp" ); |
| 912 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_fopenmp_version_EQ); |
| 913 | |
| 914 | if (Args.hasArg(Ids: options::OPT_fopenmp_force_usm)) |
| 915 | CmdArgs.push_back(Elt: "-fopenmp-force-usm" ); |
| 916 | |
| 917 | // FIXME: Clang supports a whole bunch more flags here. |
| 918 | break; |
| 919 | default: |
| 920 | // By default, if Clang doesn't know how to generate useful OpenMP code |
| 921 | // for a specific runtime library, we just don't pass the '-fopenmp' flag |
| 922 | // down to the actual compilation. |
| 923 | // FIXME: It would be better to have a mode which *only* omits IR |
| 924 | // generation based on the OpenMP support so that we get consistent |
| 925 | // semantic analysis, etc. |
| 926 | const Arg *A = Args.getLastArg(Ids: options::OPT_fopenmp_EQ); |
| 927 | D.Diag(DiagID: diag::warn_drv_unsupported_openmp_library) |
| 928 | << A->getSpelling() << A->getValue(); |
| 929 | break; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | // Pass the path to compiler resource files. |
| 934 | CmdArgs.push_back(Elt: "-resource-dir" ); |
| 935 | CmdArgs.push_back(Elt: D.ResourceDir.c_str()); |
| 936 | |
| 937 | // Offloading related options |
| 938 | addOffloadOptions(C, Inputs, JA, Args, CmdArgs); |
| 939 | |
| 940 | // Forward -Xflang arguments to -fc1 |
| 941 | Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Xflang); |
| 942 | |
| 943 | CodeGenOptions::FramePointerKind FPKeepKind = |
| 944 | getFramePointerKind(Args, Triple); |
| 945 | |
| 946 | const char *FPKeepKindStr = nullptr; |
| 947 | switch (FPKeepKind) { |
| 948 | case CodeGenOptions::FramePointerKind::None: |
| 949 | FPKeepKindStr = "-mframe-pointer=none" ; |
| 950 | break; |
| 951 | case CodeGenOptions::FramePointerKind::Reserved: |
| 952 | FPKeepKindStr = "-mframe-pointer=reserved" ; |
| 953 | break; |
| 954 | case CodeGenOptions::FramePointerKind::NonLeaf: |
| 955 | FPKeepKindStr = "-mframe-pointer=non-leaf" ; |
| 956 | break; |
| 957 | case CodeGenOptions::FramePointerKind::All: |
| 958 | FPKeepKindStr = "-mframe-pointer=all" ; |
| 959 | break; |
| 960 | } |
| 961 | assert(FPKeepKindStr && "unknown FramePointerKind" ); |
| 962 | CmdArgs.push_back(Elt: FPKeepKindStr); |
| 963 | |
| 964 | // Forward -mllvm options to the LLVM option parser. In practice, this means |
| 965 | // forwarding to `-fc1` as that's where the LLVM parser is run. |
| 966 | for (const Arg *A : Args.filtered(Ids: options::OPT_mllvm)) { |
| 967 | A->claim(); |
| 968 | A->render(Args, Output&: CmdArgs); |
| 969 | } |
| 970 | |
| 971 | for (const Arg *A : Args.filtered(Ids: options::OPT_mmlir)) { |
| 972 | A->claim(); |
| 973 | A->render(Args, Output&: CmdArgs); |
| 974 | } |
| 975 | |
| 976 | // Remove any unsupported gfortran diagnostic options |
| 977 | for (const Arg *A : Args.filtered(Ids: options::OPT_flang_ignored_w_Group)) { |
| 978 | A->claim(); |
| 979 | D.Diag(DiagID: diag::warn_drv_unsupported_diag_option_for_flang) |
| 980 | << A->getOption().getName(); |
| 981 | } |
| 982 | |
| 983 | // Optimization level for CodeGen. |
| 984 | if (const Arg *A = Args.getLastArg(Ids: options::OPT_O_Group)) { |
| 985 | if (A->getOption().matches(ID: options::OPT_O4)) { |
| 986 | CmdArgs.push_back(Elt: "-O3" ); |
| 987 | D.Diag(DiagID: diag::warn_O4_is_O3); |
| 988 | } else if (A->getOption().matches(ID: options::OPT_Ofast)) { |
| 989 | CmdArgs.push_back(Elt: "-O3" ); |
| 990 | D.Diag(DiagID: diag::warn_drv_deprecated_arg_ofast_for_flang); |
| 991 | } else { |
| 992 | A->render(Args, Output&: CmdArgs); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | renderCommonIntegerOverflowOptions(Args, CmdArgs); |
| 997 | |
| 998 | assert((Output.isFilename() || Output.isNothing()) && "Invalid output." ); |
| 999 | if (Output.isFilename()) { |
| 1000 | CmdArgs.push_back(Elt: "-o" ); |
| 1001 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 1002 | } |
| 1003 | |
| 1004 | if (Args.getLastArg(Ids: options::OPT_save_temps_EQ)) |
| 1005 | Args.AddLastArg(Output&: CmdArgs, Ids: options::OPT_save_temps_EQ); |
| 1006 | |
| 1007 | addDashXForInput(Args, Input, CmdArgs); |
| 1008 | |
| 1009 | bool FRecordCmdLine = false; |
| 1010 | bool GRecordCmdLine = false; |
| 1011 | if (shouldRecordCommandLine(TC, Args, FRecordCommandLine&: FRecordCmdLine, GRecordCommandLine&: GRecordCmdLine)) { |
| 1012 | const char *CmdLine = renderEscapedCommandLine(TC, Args); |
| 1013 | if (FRecordCmdLine) { |
| 1014 | CmdArgs.push_back(Elt: "-record-command-line" ); |
| 1015 | CmdArgs.push_back(Elt: CmdLine); |
| 1016 | } |
| 1017 | if (TC.UseDwarfDebugFlags() || GRecordCmdLine) { |
| 1018 | CmdArgs.push_back(Elt: "-dwarf-debug-flags" ); |
| 1019 | CmdArgs.push_back(Elt: CmdLine); |
| 1020 | } |
| 1021 | } |
| 1022 | |
| 1023 | // The input could be Ty_Nothing when "querying" options such as -mcpu=help |
| 1024 | // are used. |
| 1025 | ArrayRef<InputInfo> FrontendInputs = Input; |
| 1026 | if (Input.isNothing()) |
| 1027 | FrontendInputs = {}; |
| 1028 | |
| 1029 | for (const InputInfo &Input : FrontendInputs) { |
| 1030 | if (Input.isFilename()) |
| 1031 | CmdArgs.push_back(Elt: Input.getFilename()); |
| 1032 | else |
| 1033 | Input.getInputArg().renderAsInput(Args, Output&: CmdArgs); |
| 1034 | } |
| 1035 | |
| 1036 | const char *Exec = Args.MakeArgString(Str: D.GetProgramPath(Name: "flang" , TC)); |
| 1037 | C.addCommand(C: std::make_unique<Command>(args: JA, args: *this, |
| 1038 | args: ResponseFileSupport::AtFileUTF8(), |
| 1039 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 1040 | } |
| 1041 | |
| 1042 | Flang::Flang(const ToolChain &TC) : Tool("flang" , "flang frontend" , TC) {} |
| 1043 | |
| 1044 | Flang::~Flang() {} |
| 1045 | |