CodeGenOptions.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- CodeGenOptions.h ---------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the CodeGenOptions interface.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_BASIC_CODEGENOPTIONS_H
  18. #define LLVM_CLANG_BASIC_CODEGENOPTIONS_H
  19. #include "clang/Basic/DebugInfoOptions.h"
  20. #include "clang/Basic/Sanitizers.h"
  21. #include "clang/Basic/XRayInstr.h"
  22. #include "llvm/ADT/FloatingPointMode.h"
  23. #include "llvm/Support/CodeGen.h"
  24. #include "llvm/Support/Regex.h"
  25. #include "llvm/Target/TargetOptions.h"
  26. #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
  27. #include <map>
  28. #include <memory>
  29. #include <string>
  30. #include <vector>
  31. namespace clang {
  32. /// Bitfields of CodeGenOptions, split out from CodeGenOptions to ensure
  33. /// that this large collection of bitfields is a trivial class type.
  34. class CodeGenOptionsBase {
  35. friend class CompilerInvocation;
  36. public:
  37. #define CODEGENOPT(Name, Bits, Default) unsigned Name : Bits;
  38. #define ENUM_CODEGENOPT(Name, Type, Bits, Default)
  39. #include "clang/Basic/CodeGenOptions.def"
  40. protected:
  41. #define CODEGENOPT(Name, Bits, Default)
  42. #define ENUM_CODEGENOPT(Name, Type, Bits, Default) unsigned Name : Bits;
  43. #include "clang/Basic/CodeGenOptions.def"
  44. };
  45. /// CodeGenOptions - Track various options which control how the code
  46. /// is optimized and passed to the backend.
  47. class CodeGenOptions : public CodeGenOptionsBase {
  48. public:
  49. enum InliningMethod {
  50. NormalInlining, // Use the standard function inlining pass.
  51. OnlyHintInlining, // Inline only (implicitly) hinted functions.
  52. OnlyAlwaysInlining // Only run the always inlining pass.
  53. };
  54. enum VectorLibrary {
  55. NoLibrary, // Don't use any vector library.
  56. Accelerate, // Use the Accelerate framework.
  57. LIBMVEC, // GLIBC vector math library.
  58. MASSV, // IBM MASS vector library.
  59. SVML, // Intel short vector math library.
  60. Darwin_libsystem_m // Use Darwin's libsytem_m vector functions.
  61. };
  62. enum ObjCDispatchMethodKind {
  63. Legacy = 0,
  64. NonLegacy = 1,
  65. Mixed = 2
  66. };
  67. enum TLSModel {
  68. GeneralDynamicTLSModel,
  69. LocalDynamicTLSModel,
  70. InitialExecTLSModel,
  71. LocalExecTLSModel
  72. };
  73. enum StructReturnConventionKind {
  74. SRCK_Default, // No special option was passed.
  75. SRCK_OnStack, // Small structs on the stack (-fpcc-struct-return).
  76. SRCK_InRegs // Small structs in registers (-freg-struct-return).
  77. };
  78. enum ProfileInstrKind {
  79. ProfileNone, // Profile instrumentation is turned off.
  80. ProfileClangInstr, // Clang instrumentation to generate execution counts
  81. // to use with PGO.
  82. ProfileIRInstr, // IR level PGO instrumentation in LLVM.
  83. ProfileCSIRInstr, // IR level PGO context sensitive instrumentation in LLVM.
  84. };
  85. enum EmbedBitcodeKind {
  86. Embed_Off, // No embedded bitcode.
  87. Embed_All, // Embed both bitcode and commandline in the output.
  88. Embed_Bitcode, // Embed just the bitcode in the output.
  89. Embed_Marker // Embed a marker as a placeholder for bitcode.
  90. };
  91. enum InlineAsmDialectKind {
  92. IAD_ATT,
  93. IAD_Intel,
  94. };
  95. // This field stores one of the allowed values for the option
  96. // -fbasic-block-sections=. The allowed values with this option are:
  97. // {"labels", "all", "list=<file>", "none"}.
  98. //
  99. // "labels": Only generate basic block symbols (labels) for all basic
  100. // blocks, do not generate unique sections for basic blocks.
  101. // Use the machine basic block id in the symbol name to
  102. // associate profile info from virtual address to machine
  103. // basic block.
  104. // "all" : Generate basic block sections for all basic blocks.
  105. // "list=<file>": Generate basic block sections for a subset of basic blocks.
  106. // The functions and the machine basic block ids are specified
  107. // in the file.
  108. // "none": Disable sections/labels for basic blocks.
  109. std::string BBSections;
  110. // If set, override the default value of MCAsmInfo::BinutilsVersion. If
  111. // DisableIntegratedAS is specified, the assembly output will consider GNU as
  112. // support. "none" means that all ELF features can be used, regardless of
  113. // binutils support.
  114. std::string BinutilsVersion;
  115. enum class FramePointerKind {
  116. None, // Omit all frame pointers.
  117. NonLeaf, // Keep non-leaf frame pointers.
  118. All, // Keep all frame pointers.
  119. };
  120. enum class SwiftAsyncFramePointerKind {
  121. Auto, // Choose Swift async extended frame info based on deployment target.
  122. Always, // Unconditionally emit Swift async extended frame info.
  123. Never, // Don't emit Swift async extended frame info.
  124. Default = Always,
  125. };
  126. enum FiniteLoopsKind {
  127. Language, // Not specified, use language standard.
  128. Always, // All loops are assumed to be finite.
  129. Never, // No loop is assumed to be finite.
  130. };
  131. /// The code model to use (-mcmodel).
  132. std::string CodeModel;
  133. /// The filename with path we use for coverage data files. The runtime
  134. /// allows further manipulation with the GCOV_PREFIX and GCOV_PREFIX_STRIP
  135. /// environment variables.
  136. std::string CoverageDataFile;
  137. /// The filename with path we use for coverage notes files.
  138. std::string CoverageNotesFile;
  139. /// Regexes separated by a semi-colon to filter the files to instrument.
  140. std::string ProfileFilterFiles;
  141. /// Regexes separated by a semi-colon to filter the files to not instrument.
  142. std::string ProfileExcludeFiles;
  143. /// The version string to put into coverage files.
  144. char CoverageVersion[4];
  145. /// Enable additional debugging information.
  146. std::string DebugPass;
  147. /// The string to embed in debug information as the current working directory.
  148. std::string DebugCompilationDir;
  149. /// The string to embed in coverage mapping as the current working directory.
  150. std::string CoverageCompilationDir;
  151. /// The string to embed in the debug information for the compile unit, if
  152. /// non-empty.
  153. std::string DwarfDebugFlags;
  154. /// The string containing the commandline for the llvm.commandline metadata,
  155. /// if non-empty.
  156. std::string RecordCommandLine;
  157. std::map<std::string, std::string> DebugPrefixMap;
  158. std::map<std::string, std::string> CoveragePrefixMap;
  159. /// The ABI to use for passing floating point arguments.
  160. std::string FloatABI;
  161. /// The file to use for dumping bug report by `Debugify` for original
  162. /// debug info.
  163. std::string DIBugsReportFilePath;
  164. /// The floating-point denormal mode to use.
  165. llvm::DenormalMode FPDenormalMode = llvm::DenormalMode::getIEEE();
  166. /// The floating-point denormal mode to use, for float.
  167. llvm::DenormalMode FP32DenormalMode = llvm::DenormalMode::getIEEE();
  168. /// The float precision limit to use, if non-empty.
  169. std::string LimitFloatPrecision;
  170. struct BitcodeFileToLink {
  171. /// The filename of the bitcode file to link in.
  172. std::string Filename;
  173. /// If true, we set attributes functions in the bitcode library according to
  174. /// our CodeGenOptions, much as we set attrs on functions that we generate
  175. /// ourselves.
  176. bool PropagateAttrs = false;
  177. /// If true, we use LLVM module internalizer.
  178. bool Internalize = false;
  179. /// Bitwise combination of llvm::Linker::Flags, passed to the LLVM linker.
  180. unsigned LinkFlags = 0;
  181. };
  182. /// The files specified here are linked in to the module before optimizations.
  183. std::vector<BitcodeFileToLink> LinkBitcodeFiles;
  184. /// The user provided name for the "main file", if non-empty. This is useful
  185. /// in situations where the input file name does not match the original input
  186. /// file, for example with -save-temps.
  187. std::string MainFileName;
  188. /// The name for the split debug info file used for the DW_AT_[GNU_]dwo_name
  189. /// attribute in the skeleton CU.
  190. std::string SplitDwarfFile;
  191. /// Output filename for the split debug info, not used in the skeleton CU.
  192. std::string SplitDwarfOutput;
  193. /// Output filename used in the COFF debug information.
  194. std::string ObjectFilenameForDebug;
  195. /// The name of the relocation model to use.
  196. llvm::Reloc::Model RelocationModel;
  197. /// If not an empty string, trap intrinsics are lowered to calls to this
  198. /// function instead of to trap instructions.
  199. std::string TrapFuncName;
  200. /// A list of dependent libraries.
  201. std::vector<std::string> DependentLibraries;
  202. /// A list of linker options to embed in the object file.
  203. std::vector<std::string> LinkerOptions;
  204. /// Name of the profile file to use as output for -fprofile-instr-generate,
  205. /// -fprofile-generate, and -fcs-profile-generate.
  206. std::string InstrProfileOutput;
  207. /// Name of the profile file to use with -fprofile-sample-use.
  208. std::string SampleProfileFile;
  209. /// Name of the profile file to use as output for with -fmemory-profile.
  210. std::string MemoryProfileOutput;
  211. /// Name of the profile file to use as input for -fprofile-instr-use
  212. std::string ProfileInstrumentUsePath;
  213. /// Name of the profile remapping file to apply to the profile data supplied
  214. /// by -fprofile-sample-use or -fprofile-instr-use.
  215. std::string ProfileRemappingFile;
  216. /// Name of the function summary index file to use for ThinLTO function
  217. /// importing.
  218. std::string ThinLTOIndexFile;
  219. /// Name of a file that can optionally be written with minimized bitcode
  220. /// to be used as input for the ThinLTO thin link step, which only needs
  221. /// the summary and module symbol table (and not, e.g. any debug metadata).
  222. std::string ThinLinkBitcodeFile;
  223. /// Prefix to use for -save-temps output.
  224. std::string SaveTempsFilePrefix;
  225. /// Name of file passed with -fcuda-include-gpubinary option to forward to
  226. /// CUDA runtime back-end for incorporating them into host-side object file.
  227. std::string CudaGpuBinaryFileName;
  228. /// List of filenames and section name pairs passed in using the
  229. /// -fembed-offload-object option to embed device-side offloading objects into
  230. /// the host as a named section. Input passed in as '<filename>,<section>'
  231. std::vector<std::string> OffloadObjects;
  232. /// The name of the file to which the backend should save YAML optimization
  233. /// records.
  234. std::string OptRecordFile;
  235. /// The regex that filters the passes that should be saved to the optimization
  236. /// records.
  237. std::string OptRecordPasses;
  238. /// The format used for serializing remarks (default: YAML)
  239. std::string OptRecordFormat;
  240. /// The name of the partition that symbols are assigned to, specified with
  241. /// -fsymbol-partition (see https://lld.llvm.org/Partitions.html).
  242. std::string SymbolPartition;
  243. enum RemarkKind {
  244. RK_Missing, // Remark argument not present on the command line.
  245. RK_Enabled, // Remark enabled via '-Rgroup'.
  246. RK_EnabledEverything, // Remark enabled via '-Reverything'.
  247. RK_Disabled, // Remark disabled via '-Rno-group'.
  248. RK_DisabledEverything, // Remark disabled via '-Rno-everything'.
  249. RK_WithPattern, // Remark pattern specified via '-Rgroup=regexp'.
  250. };
  251. /// Optimization remark with an optional regular expression pattern.
  252. struct OptRemark {
  253. RemarkKind Kind;
  254. std::string Pattern;
  255. std::shared_ptr<llvm::Regex> Regex;
  256. /// By default, optimization remark is missing.
  257. OptRemark() : Kind(RK_Missing), Regex(nullptr) {}
  258. /// Returns true iff the optimization remark holds a valid regular
  259. /// expression.
  260. bool hasValidPattern() const { return Regex != nullptr; }
  261. /// Matches the given string against the regex, if there is some.
  262. bool patternMatches(StringRef String) const {
  263. return hasValidPattern() && Regex->match(String);
  264. }
  265. };
  266. /// Selected optimizations for which we should enable optimization remarks.
  267. /// Transformation passes whose name matches the contained (optional) regular
  268. /// expression (and support this feature), will emit a diagnostic whenever
  269. /// they perform a transformation.
  270. OptRemark OptimizationRemark;
  271. /// Selected optimizations for which we should enable missed optimization
  272. /// remarks. Transformation passes whose name matches the contained (optional)
  273. /// regular expression (and support this feature), will emit a diagnostic
  274. /// whenever they tried but failed to perform a transformation.
  275. OptRemark OptimizationRemarkMissed;
  276. /// Selected optimizations for which we should enable optimization analyses.
  277. /// Transformation passes whose name matches the contained (optional) regular
  278. /// expression (and support this feature), will emit a diagnostic whenever
  279. /// they want to explain why they decided to apply or not apply a given
  280. /// transformation.
  281. OptRemark OptimizationRemarkAnalysis;
  282. /// Set of files defining the rules for the symbol rewriting.
  283. std::vector<std::string> RewriteMapFiles;
  284. /// Set of sanitizer checks that are non-fatal (i.e. execution should be
  285. /// continued when possible).
  286. SanitizerSet SanitizeRecover;
  287. /// Set of sanitizer checks that trap rather than diagnose.
  288. SanitizerSet SanitizeTrap;
  289. /// List of backend command-line options for -fembed-bitcode.
  290. std::vector<uint8_t> CmdArgs;
  291. /// A list of all -fno-builtin-* function names (e.g., memset).
  292. std::vector<std::string> NoBuiltinFuncs;
  293. std::vector<std::string> Reciprocals;
  294. /// The preferred width for auto-vectorization transforms. This is intended to
  295. /// override default transforms based on the width of the architected vector
  296. /// registers.
  297. std::string PreferVectorWidth;
  298. /// Set of XRay instrumentation kinds to emit.
  299. XRayInstrSet XRayInstrumentationBundle;
  300. std::vector<std::string> DefaultFunctionAttrs;
  301. /// List of dynamic shared object files to be loaded as pass plugins.
  302. std::vector<std::string> PassPlugins;
  303. /// Path to allowlist file specifying which objects
  304. /// (files, functions) should exclusively be instrumented
  305. /// by sanitizer coverage pass.
  306. std::vector<std::string> SanitizeCoverageAllowlistFiles;
  307. /// The guard style used for stack protector to get a initial value, this
  308. /// value usually be gotten from TLS or get from __stack_chk_guard, or some
  309. /// other styles we may implement in the future.
  310. std::string StackProtectorGuard;
  311. /// The TLS base register when StackProtectorGuard is "tls", or register used
  312. /// to store the stack canary for "sysreg".
  313. /// On x86 this can be "fs" or "gs".
  314. /// On AArch64 this can only be "sp_el0".
  315. std::string StackProtectorGuardReg;
  316. /// Path to ignorelist file specifying which objects
  317. /// (files, functions) listed for instrumentation by sanitizer
  318. /// coverage pass should actually not be instrumented.
  319. std::vector<std::string> SanitizeCoverageIgnorelistFiles;
  320. /// Name of the stack usage file (i.e., .su file) if user passes
  321. /// -fstack-usage. If empty, it can be implied that -fstack-usage is not
  322. /// passed on the command line.
  323. std::string StackUsageOutput;
  324. /// Executable and command-line used to create a given CompilerInvocation.
  325. /// Most of the time this will be the full -cc1 command.
  326. const char *Argv0 = nullptr;
  327. std::vector<std::string> CommandLineArgs;
  328. /// The minimum hotness value a diagnostic needs in order to be included in
  329. /// optimization diagnostics.
  330. ///
  331. /// The threshold is an Optional value, which maps to one of the 3 states:
  332. /// 1. 0 => threshold disabled. All remarks will be printed.
  333. /// 2. positive int => manual threshold by user. Remarks with hotness exceed
  334. /// threshold will be printed.
  335. /// 3. None => 'auto' threshold by user. The actual value is not
  336. /// available at command line, but will be synced with
  337. /// hotness threshold from profile summary during
  338. /// compilation.
  339. ///
  340. /// If threshold option is not specified, it is disabled by default.
  341. Optional<uint64_t> DiagnosticsHotnessThreshold = 0;
  342. public:
  343. // Define accessors/mutators for code generation options of enumeration type.
  344. #define CODEGENOPT(Name, Bits, Default)
  345. #define ENUM_CODEGENOPT(Name, Type, Bits, Default) \
  346. Type get##Name() const { return static_cast<Type>(Name); } \
  347. void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
  348. #include "clang/Basic/CodeGenOptions.def"
  349. CodeGenOptions();
  350. const std::vector<std::string> &getNoBuiltinFuncs() const {
  351. return NoBuiltinFuncs;
  352. }
  353. /// Check if Clang profile instrumenation is on.
  354. bool hasProfileClangInstr() const {
  355. return getProfileInstr() == ProfileClangInstr;
  356. }
  357. /// Check if IR level profile instrumentation is on.
  358. bool hasProfileIRInstr() const {
  359. return getProfileInstr() == ProfileIRInstr;
  360. }
  361. /// Check if CS IR level profile instrumentation is on.
  362. bool hasProfileCSIRInstr() const {
  363. return getProfileInstr() == ProfileCSIRInstr;
  364. }
  365. /// Check if Clang profile use is on.
  366. bool hasProfileClangUse() const {
  367. return getProfileUse() == ProfileClangInstr;
  368. }
  369. /// Check if IR level profile use is on.
  370. bool hasProfileIRUse() const {
  371. return getProfileUse() == ProfileIRInstr ||
  372. getProfileUse() == ProfileCSIRInstr;
  373. }
  374. /// Check if CSIR profile use is on.
  375. bool hasProfileCSIRUse() const { return getProfileUse() == ProfileCSIRInstr; }
  376. /// Check if type and variable info should be emitted.
  377. bool hasReducedDebugInfo() const {
  378. return getDebugInfo() >= codegenoptions::DebugInfoConstructor;
  379. }
  380. /// Check if maybe unused type info should be emitted.
  381. bool hasMaybeUnusedDebugInfo() const {
  382. return getDebugInfo() >= codegenoptions::UnusedTypeInfo;
  383. }
  384. // Check if any one of SanitizeCoverage* is enabled.
  385. bool hasSanitizeCoverage() const {
  386. return SanitizeCoverageType || SanitizeCoverageIndirectCalls ||
  387. SanitizeCoverageTraceCmp || SanitizeCoverageTraceLoads ||
  388. SanitizeCoverageTraceStores;
  389. }
  390. };
  391. } // end namespace clang
  392. #endif
  393. #ifdef __GNUC__
  394. #pragma GCC diagnostic pop
  395. #endif