1 //===--- CommonArgs.h - Args handling for multiple toolchains ---*- 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 #ifndef LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H 10 #define LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H 11 12 #include "clang/Basic/CodeGenOptions.h" 13 #include "clang/Driver/Driver.h" 14 #include "clang/Driver/InputInfo.h" 15 #include "clang/Driver/Multilib.h" 16 #include "clang/Driver/Tool.h" 17 #include "clang/Driver/ToolChain.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/Option/Arg.h" 20 #include "llvm/Option/ArgList.h" 21 #include "llvm/Support/CodeGen.h" 22 23 namespace clang { 24 namespace driver { 25 namespace tools { 26 27 void addPathIfExists(const Driver &D, const Twine &Path, 28 ToolChain::path_list &Paths); 29 30 void AddLinkerInputs(const ToolChain &TC, const InputInfoList &Inputs, 31 const llvm::opt::ArgList &Args, 32 llvm::opt::ArgStringList &CmdArgs, const JobAction &JA); 33 34 const char *getLDMOption(const llvm::Triple &T, const llvm::opt::ArgList &Args); 35 36 void addLinkerCompressDebugSectionsOption(const ToolChain &TC, 37 const llvm::opt::ArgList &Args, 38 llvm::opt::ArgStringList &CmdArgs); 39 40 void claimNoWarnArgs(const llvm::opt::ArgList &Args); 41 42 bool addSanitizerRuntimes(const ToolChain &TC, const llvm::opt::ArgList &Args, 43 llvm::opt::ArgStringList &CmdArgs); 44 45 void linkSanitizerRuntimeDeps(const ToolChain &TC, 46 const llvm::opt::ArgList &Args, 47 llvm::opt::ArgStringList &CmdArgs); 48 49 bool addXRayRuntime(const ToolChain &TC, const llvm::opt::ArgList &Args, 50 llvm::opt::ArgStringList &CmdArgs); 51 52 void linkXRayRuntimeDeps(const ToolChain &TC, const llvm::opt::ArgList &Args, 53 llvm::opt::ArgStringList &CmdArgs); 54 55 void AddRunTimeLibs(const ToolChain &TC, const Driver &D, 56 llvm::opt::ArgStringList &CmdArgs, 57 const llvm::opt::ArgList &Args); 58 59 void AddStaticDeviceLibsLinking(Compilation &C, const Tool &T, 60 const JobAction &JA, 61 const InputInfoList &Inputs, 62 const llvm::opt::ArgList &DriverArgs, 63 llvm::opt::ArgStringList &CmdArgs, 64 StringRef Arch, StringRef Target, 65 bool isBitCodeSDL); 66 void AddStaticDeviceLibs(Compilation *C, const Tool *T, const JobAction *JA, 67 const InputInfoList *Inputs, const Driver &D, 68 const llvm::opt::ArgList &DriverArgs, 69 llvm::opt::ArgStringList &CmdArgs, StringRef Arch, 70 StringRef Target, bool isBitCodeSDL); 71 72 const char *SplitDebugName(const JobAction &JA, const llvm::opt::ArgList &Args, 73 const InputInfo &Input, const InputInfo &Output); 74 75 void SplitDebugInfo(const ToolChain &TC, Compilation &C, const Tool &T, 76 const JobAction &JA, const llvm::opt::ArgList &Args, 77 const InputInfo &Output, const char *OutFile); 78 79 void addLTOOptions(const ToolChain &ToolChain, const llvm::opt::ArgList &Args, 80 llvm::opt::ArgStringList &CmdArgs, const InputInfo &Output, 81 const InputInfoList &Inputs, bool IsThinLTO); 82 83 const char *RelocationModelName(llvm::Reloc::Model Model); 84 85 std::tuple<llvm::Reloc::Model, unsigned, bool> 86 ParsePICArgs(const ToolChain &ToolChain, const llvm::opt::ArgList &Args); 87 88 unsigned ParseFunctionAlignment(const ToolChain &TC, 89 const llvm::opt::ArgList &Args); 90 91 void addDebugInfoKind(llvm::opt::ArgStringList &CmdArgs, 92 llvm::codegenoptions::DebugInfoKind DebugInfoKind); 93 94 llvm::codegenoptions::DebugInfoKind 95 debugLevelToInfoKind(const llvm::opt::Arg &A); 96 97 // Extract the integer N from a string spelled "-dwarf-N", returning 0 98 // on mismatch. The StringRef input (rather than an Arg) allows 99 // for use by the "-Xassembler" option parser. 100 unsigned DwarfVersionNum(StringRef ArgValue); 101 // Find a DWARF format version option. 102 // This function is a complementary for DwarfVersionNum(). 103 const llvm::opt::Arg *getDwarfNArg(const llvm::opt::ArgList &Args); 104 unsigned getDwarfVersion(const ToolChain &TC, const llvm::opt::ArgList &Args); 105 106 void AddAssemblerKPIC(const ToolChain &ToolChain, 107 const llvm::opt::ArgList &Args, 108 llvm::opt::ArgStringList &CmdArgs); 109 110 void addArchSpecificRPath(const ToolChain &TC, const llvm::opt::ArgList &Args, 111 llvm::opt::ArgStringList &CmdArgs); 112 void addOpenMPRuntimeLibraryPath(const ToolChain &TC, 113 const llvm::opt::ArgList &Args, 114 llvm::opt::ArgStringList &CmdArgs); 115 /// Returns true, if an OpenMP runtime has been added. 116 bool addOpenMPRuntime(const Compilation &C, llvm::opt::ArgStringList &CmdArgs, 117 const ToolChain &TC, const llvm::opt::ArgList &Args, 118 bool ForceStaticHostRuntime = false, 119 bool IsOffloadingHost = false, bool GompNeedsRT = false); 120 121 /// Adds offloading options for OpenMP host compilation to \p CmdArgs. 122 void addOpenMPHostOffloadingArgs(const Compilation &C, const JobAction &JA, 123 const llvm::opt::ArgList &Args, 124 llvm::opt::ArgStringList &CmdArgs); 125 126 void addHIPRuntimeLibArgs(const ToolChain &TC, Compilation &C, 127 const llvm::opt::ArgList &Args, 128 llvm::opt::ArgStringList &CmdArgs); 129 130 void addAsNeededOption(const ToolChain &TC, const llvm::opt::ArgList &Args, 131 llvm::opt::ArgStringList &CmdArgs, bool as_needed); 132 133 llvm::opt::Arg *getLastCSProfileGenerateArg(const llvm::opt::ArgList &Args); 134 llvm::opt::Arg *getLastProfileUseArg(const llvm::opt::ArgList &Args); 135 llvm::opt::Arg *getLastProfileSampleUseArg(const llvm::opt::ArgList &Args); 136 137 bool isObjCAutoRefCount(const llvm::opt::ArgList &Args); 138 139 llvm::StringRef getLTOParallelism(const llvm::opt::ArgList &Args, 140 const Driver &D); 141 142 bool areOptimizationsEnabled(const llvm::opt::ArgList &Args); 143 144 bool isUseSeparateSections(const llvm::Triple &Triple); 145 // Parse -mtls-dialect=. Return true if the target supports both general-dynamic 146 // and TLSDESC, and TLSDESC is requested. 147 bool isTLSDESCEnabled(const ToolChain &TC, const llvm::opt::ArgList &Args); 148 149 /// \p EnvVar is split by system delimiter for environment variables. 150 /// If \p ArgName is "-I", "-L", or an empty string, each entry from \p EnvVar 151 /// is prefixed by \p ArgName then added to \p Args. Otherwise, for each 152 /// entry of \p EnvVar, \p ArgName is added to \p Args first, then the entry 153 /// itself is added. 154 void addDirectoryList(const llvm::opt::ArgList &Args, 155 llvm::opt::ArgStringList &CmdArgs, const char *ArgName, 156 const char *EnvVar); 157 158 void AddTargetFeature(const llvm::opt::ArgList &Args, 159 std::vector<StringRef> &Features, 160 llvm::opt::OptSpecifier OnOpt, 161 llvm::opt::OptSpecifier OffOpt, StringRef FeatureName); 162 163 std::string getCPUName(const Driver &D, const llvm::opt::ArgList &Args, 164 const llvm::Triple &T, bool FromAs = false); 165 166 void getTargetFeatures(const Driver &D, const llvm::Triple &Triple, 167 const llvm::opt::ArgList &Args, 168 llvm::opt::ArgStringList &CmdArgs, bool ForAS, 169 bool IsAux = false); 170 171 /// Iterate \p Args and convert -mxxx to +xxx and -mno-xxx to -xxx and 172 /// append it to \p Features. 173 /// 174 /// Note: Since \p Features may contain default values before calling 175 /// this function, or may be appended with entries to override arguments, 176 /// entries in \p Features are not unique. 177 void handleTargetFeaturesGroup(const Driver &D, const llvm::Triple &Triple, 178 const llvm::opt::ArgList &Args, 179 std::vector<StringRef> &Features, 180 llvm::opt::OptSpecifier Group); 181 182 /// If there are multiple +xxx or -xxx features, keep the last one. 183 SmallVector<StringRef> unifyTargetFeatures(ArrayRef<StringRef> Features); 184 185 /// Handles the -save-stats option and returns the filename to save statistics 186 /// to. 187 SmallString<128> getStatsFileName(const llvm::opt::ArgList &Args, 188 const InputInfo &Output, 189 const InputInfo &Input, const Driver &D); 190 191 /// \p Flag must be a flag accepted by the driver. 192 void addMultilibFlag(bool Enabled, const StringRef Flag, 193 Multilib::flags_list &Flags); 194 195 void addX86AlignBranchArgs(const Driver &D, const llvm::opt::ArgList &Args, 196 llvm::opt::ArgStringList &CmdArgs, bool IsLTO, 197 const StringRef PluginOptPrefix = ""); 198 199 void checkAMDGPUCodeObjectVersion(const Driver &D, 200 const llvm::opt::ArgList &Args); 201 202 unsigned getAMDGPUCodeObjectVersion(const Driver &D, 203 const llvm::opt::ArgList &Args); 204 205 bool haveAMDGPUCodeObjectVersionArgument(const Driver &D, 206 const llvm::opt::ArgList &Args); 207 208 void addMachineOutlinerArgs(const Driver &D, const llvm::opt::ArgList &Args, 209 llvm::opt::ArgStringList &CmdArgs, 210 const llvm::Triple &Triple, bool IsLTO, 211 const StringRef PluginOptPrefix = ""); 212 213 void addOpenMPDeviceRTL(const Driver &D, const llvm::opt::ArgList &DriverArgs, 214 llvm::opt::ArgStringList &CC1Args, 215 StringRef BitcodeSuffix, const llvm::Triple &Triple, 216 const ToolChain &HostTC); 217 218 void addOutlineAtomicsArgs(const Driver &D, const ToolChain &TC, 219 const llvm::opt::ArgList &Args, 220 llvm::opt::ArgStringList &CmdArgs, 221 const llvm::Triple &Triple); 222 void addOffloadCompressArgs(const llvm::opt::ArgList &TCArgs, 223 llvm::opt::ArgStringList &CmdArgs); 224 void addMCModel(const Driver &D, const llvm::opt::ArgList &Args, 225 const llvm::Triple &Triple, 226 const llvm::Reloc::Model &RelocationModel, 227 llvm::opt::ArgStringList &CmdArgs); 228 229 /// Handle the -f{no}-color-diagnostics and -f{no}-diagnostics-colors options. 230 void handleColorDiagnosticsArgs(const Driver &D, const llvm::opt::ArgList &Args, 231 llvm::opt::ArgStringList &CmdArgs); 232 233 /// Add backslashes to escape spaces and other backslashes. 234 /// This is used for the space-separated argument list specified with 235 /// the -dwarf-debug-flags option. 236 void escapeSpacesAndBackslashes(const char *Arg, 237 llvm::SmallVectorImpl<char> &Res); 238 239 /// Join the args in the given ArgList, escape spaces and backslashes and 240 /// return the joined string. This is used when saving the command line as a 241 /// result of using either the -frecord-command-line or -grecord-command-line 242 /// options. The lifetime of the returned c-string will match that of the Args 243 /// argument. 244 const char *renderEscapedCommandLine(const ToolChain &TC, 245 const llvm::opt::ArgList &Args); 246 247 /// Check if the command line should be recorded in the object file. This is 248 /// done if either -frecord-command-line or -grecord-command-line options have 249 /// been passed. This also does some error checking since -frecord-command-line 250 /// is currently only supported on ELF platforms. The last two boolean 251 /// arguments are out parameters and will be set depending on the command 252 /// line options that were passed. 253 bool shouldRecordCommandLine(const ToolChain &TC, 254 const llvm::opt::ArgList &Args, 255 bool &FRecordCommandLine, 256 bool &GRecordCommandLine); 257 258 void renderCommonIntegerOverflowOptions(const llvm::opt::ArgList &Args, 259 llvm::opt::ArgStringList &CmdArgs); 260 261 bool shouldEnableVectorizerAtOLevel(const llvm::opt::ArgList &Args, 262 bool isSlpVec); 263 264 /// Enable -floop-interchange based on the optimization level selected. 265 void handleInterchangeLoopsArgs(const llvm::opt::ArgList &Args, 266 llvm::opt::ArgStringList &CmdArgs); 267 268 /// Enable -fvectorize based on the optimization level selected. 269 void handleVectorizeLoopsArgs(const llvm::opt::ArgList &Args, 270 llvm::opt::ArgStringList &CmdArgs); 271 272 /// Enable -fslp-vectorize based on the optimization level selected. 273 void handleVectorizeSLPArgs(const llvm::opt::ArgList &Args, 274 llvm::opt::ArgStringList &CmdArgs); 275 276 // Parse -mprefer-vector-width=. Return the Value string if well-formed. 277 // Otherwise, return an empty string and issue a diagnosic message if needed. 278 StringRef parseMPreferVectorWidthOption(clang::DiagnosticsEngine &Diags, 279 const llvm::opt::ArgList &Args); 280 281 // Parse -mrecip. Return the Value string if well-formed. 282 // Otherwise, return an empty string and issue a diagnosic message if needed. 283 StringRef parseMRecipOption(clang::DiagnosticsEngine &Diags, 284 const llvm::opt::ArgList &Args); 285 286 // Convert ComplexRangeKind to a string that can be passed as a frontend option. 287 std::string complexRangeKindToStr(LangOptions::ComplexRangeKind Range); 288 289 // Render a frontend option corresponding to ComplexRangeKind. 290 std::string renderComplexRangeOption(LangOptions::ComplexRangeKind Range); 291 292 } // end namespace tools 293 } // end namespace driver 294 } // end namespace clang 295 296 clang::CodeGenOptions::FramePointerKind 297 getFramePointerKind(const llvm::opt::ArgList &Args, const llvm::Triple &Triple); 298 299 #endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_COMMONARGS_H 300