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 10 #include "Flang.h" 11 #include "CommonArgs.h" 12 13 #include "clang/Driver/Options.h" 14 15 #include <cassert> 16 17 using namespace clang::driver; 18 using namespace clang::driver::tools; 19 using namespace clang; 20 using namespace llvm::opt; 21 22 /// Add -x lang to \p CmdArgs for \p Input. 23 static void addDashXForInput(const ArgList &Args, const InputInfo &Input, 24 ArgStringList &CmdArgs) { 25 CmdArgs.push_back("-x"); 26 // Map the driver type to the frontend type. 27 CmdArgs.push_back(types::getTypeName(Input.getType())); 28 } 29 30 void Flang::AddFortranDialectOptions(const ArgList &Args, 31 ArgStringList &CmdArgs) const { 32 Args.AddAllArgs( 33 CmdArgs, {options::OPT_ffixed_form, options::OPT_ffree_form, 34 options::OPT_ffixed_line_length_EQ, options::OPT_fopenmp, 35 options::OPT_fopenacc, options::OPT_finput_charset_EQ, 36 options::OPT_fimplicit_none, options::OPT_fno_implicit_none, 37 options::OPT_fbackslash, options::OPT_fno_backslash, 38 options::OPT_flogical_abbreviations, 39 options::OPT_fno_logical_abbreviations, 40 options::OPT_fxor_operator, options::OPT_fno_xor_operator, 41 options::OPT_falternative_parameter_statement, 42 options::OPT_fdefault_real_8, options::OPT_fdefault_integer_8, 43 options::OPT_fdefault_double_8, options::OPT_flarge_sizes, 44 options::OPT_fno_automatic}); 45 } 46 47 void Flang::AddPreprocessingOptions(const ArgList &Args, 48 ArgStringList &CmdArgs) const { 49 Args.AddAllArgs(CmdArgs, 50 {options::OPT_P, options::OPT_D, options::OPT_U, 51 options::OPT_I, options::OPT_cpp, options::OPT_nocpp}); 52 } 53 54 void Flang::AddOtherOptions(const ArgList &Args, ArgStringList &CmdArgs) const { 55 Args.AddAllArgs(CmdArgs, 56 {options::OPT_module_dir, options::OPT_fdebug_module_writer, 57 options::OPT_fintrinsic_modules_path, options::OPT_pedantic, 58 options::OPT_std_EQ, options::OPT_W_Joined}); 59 } 60 61 void Flang::ConstructJob(Compilation &C, const JobAction &JA, 62 const InputInfo &Output, const InputInfoList &Inputs, 63 const ArgList &Args, const char *LinkingOutput) const { 64 const auto &TC = getToolChain(); 65 const llvm::Triple &Triple = TC.getEffectiveTriple(); 66 const std::string &TripleStr = Triple.getTriple(); 67 68 const Driver &D = TC.getDriver(); 69 ArgStringList CmdArgs; 70 71 // Invoke ourselves in -fc1 mode. 72 CmdArgs.push_back("-fc1"); 73 74 // Add the "effective" target triple. 75 CmdArgs.push_back("-triple"); 76 CmdArgs.push_back(Args.MakeArgString(TripleStr)); 77 78 if (isa<PreprocessJobAction>(JA)) { 79 CmdArgs.push_back("-E"); 80 } else if (isa<CompileJobAction>(JA) || isa<BackendJobAction>(JA)) { 81 if (JA.getType() == types::TY_Nothing) { 82 CmdArgs.push_back("-fsyntax-only"); 83 } else if (JA.getType() == types::TY_AST) { 84 CmdArgs.push_back("-emit-ast"); 85 } else if (JA.getType() == types::TY_LLVM_IR || 86 JA.getType() == types::TY_LTO_IR) { 87 CmdArgs.push_back("-emit-llvm"); 88 } else if (JA.getType() == types::TY_LLVM_BC || 89 JA.getType() == types::TY_LTO_BC) { 90 CmdArgs.push_back("-emit-llvm-bc"); 91 } else if (JA.getType() == types::TY_PP_Asm) { 92 CmdArgs.push_back("-S"); 93 } else { 94 assert(false && "Unexpected output type!"); 95 } 96 } else if (isa<AssembleJobAction>(JA)) { 97 CmdArgs.push_back("-emit-obj"); 98 } else { 99 assert(false && "Unexpected action class for Flang tool."); 100 } 101 102 const InputInfo &Input = Inputs[0]; 103 types::ID InputType = Input.getType(); 104 105 // Add preprocessing options like -I, -D, etc. if we are using the 106 // preprocessor (i.e. skip when dealing with e.g. binary files). 107 if (types::getPreprocessedType(InputType) != types::TY_INVALID) 108 AddPreprocessingOptions(Args, CmdArgs); 109 110 AddFortranDialectOptions(Args, CmdArgs); 111 112 // Color diagnostics are parsed by the driver directly from argv and later 113 // re-parsed to construct this job; claim any possible color diagnostic here 114 // to avoid warn_drv_unused_argument. 115 Args.getLastArg(options::OPT_fcolor_diagnostics, 116 options::OPT_fno_color_diagnostics); 117 if (D.getDiags().getDiagnosticOptions().ShowColors) 118 CmdArgs.push_back("-fcolor-diagnostics"); 119 120 // Add other compile options 121 AddOtherOptions(Args, CmdArgs); 122 123 // Forward -Xflang arguments to -fc1 124 Args.AddAllArgValues(CmdArgs, options::OPT_Xflang); 125 126 // Forward -mllvm options to the LLVM option parser. In practice, this means 127 // forwarding to `-fc1` as that's where the LLVM parser is run. 128 for (const Arg *A : Args.filtered(options::OPT_mllvm)) { 129 A->claim(); 130 A->render(Args, CmdArgs); 131 } 132 133 for (const Arg *A : Args.filtered(options::OPT_mmlir)) { 134 A->claim(); 135 A->render(Args, CmdArgs); 136 } 137 138 // Optimization level for CodeGen. 139 if (const Arg *A = Args.getLastArg(options::OPT_O_Group)) { 140 if (A->getOption().matches(options::OPT_O4)) { 141 CmdArgs.push_back("-O3"); 142 D.Diag(diag::warn_O4_is_O3); 143 } else { 144 A->render(Args, CmdArgs); 145 } 146 } 147 148 if (Output.isFilename()) { 149 CmdArgs.push_back("-o"); 150 CmdArgs.push_back(Output.getFilename()); 151 } else { 152 assert(Output.isNothing() && "Invalid output."); 153 } 154 155 assert(Input.isFilename() && "Invalid input."); 156 157 addDashXForInput(Args, Input, CmdArgs); 158 159 CmdArgs.push_back(Input.getFilename()); 160 161 // TODO: Replace flang-new with flang once the new driver replaces the 162 // throwaway driver 163 const char *Exec = Args.MakeArgString(D.GetProgramPath("flang-new", TC)); 164 C.addCommand(std::make_unique<Command>(JA, *this, 165 ResponseFileSupport::AtFileUTF8(), 166 Exec, CmdArgs, Inputs, Output)); 167 } 168 169 Flang::Flang(const ToolChain &TC) : Tool("flang-new", "flang frontend", TC) {} 170 171 Flang::~Flang() {} 172