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