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