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