xref: /llvm-project/flang/include/flang/Frontend/CodeGenOptions.h (revision 839344f025fb7eff529735873f327330618b2ebb)
1 //===--- CodeGenOptions.h ---------------------------------------*- 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 //  This file defines the CodeGenOptions interface, which holds the
10 //  configuration for LLVM's middle-end and back-end. It controls LLVM's code
11 //  generation into assembly or machine code.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef FORTRAN_FRONTEND_CODEGENOPTIONS_H
16 #define FORTRAN_FRONTEND_CODEGENOPTIONS_H
17 
18 #include "llvm/Frontend/Debug/Options.h"
19 #include "llvm/Frontend/Driver/CodeGenOptions.h"
20 #include "llvm/Support/CodeGen.h"
21 #include "llvm/Support/Regex.h"
22 #include "llvm/Target/TargetOptions.h"
23 #include "llvm/Transforms/Instrumentation/AddressSanitizerOptions.h"
24 #include <map>
25 #include <memory>
26 #include <optional>
27 #include <string>
28 #include <vector>
29 
30 namespace Fortran::frontend {
31 
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 
36 public:
37 #define CODEGENOPT(Name, Bits, Default) unsigned Name : Bits;
38 #define ENUM_CODEGENOPT(Name, Type, Bits, Default)
39 #include "flang/Frontend/CodeGenOptions.def"
40 
41 protected:
42 #define CODEGENOPT(Name, Bits, Default)
43 #define ENUM_CODEGENOPT(Name, Type, Bits, Default) unsigned Name : Bits;
44 #include "flang/Frontend/CodeGenOptions.def"
45 };
46 
47 /// Tracks various options which control how the code is optimized and passed
48 /// to the LLVM backend.
49 class CodeGenOptions : public CodeGenOptionsBase {
50 
51 public:
52   /// The paths to the pass plugins that were registered using -fpass-plugin.
53   std::vector<std::string> LLVMPassPlugins;
54 
55   /// List of filenames passed in using the -fembed-offload-object option. These
56   /// are offloading binaries containing device images and metadata.
57   std::vector<std::string> OffloadObjects;
58 
59   /// List of filenames passed in using the -mlink-builtin-bitcode. These
60   /// are bc libraries that should be linked in and internalized;
61   std::vector<std::string> BuiltinBCLibs;
62 
63   /// The directory where temp files are stored if specified by -save-temps
64   std::optional<std::string> SaveTempsDir;
65 
66   /// The string containing the commandline for the llvm.commandline metadata.
67   std::optional<std::string> RecordCommandLine;
68 
69   /// The name of the file to which the backend should save YAML optimization
70   /// records.
71   std::string OptRecordFile;
72 
73   /// The regex that filters the passes that should be saved to the optimization
74   /// records.
75   std::string OptRecordPasses;
76 
77   /// The format used for serializing remarks (default: YAML)
78   std::string OptRecordFormat;
79 
80   /// Options to add to the linker for the object file
81   std::vector<std::string> DependentLibs;
82 
83   // The RemarkKind enum class and OptRemark struct are identical to what Clang
84   // has
85   // TODO: Share with clang instead of re-implementing here
86   enum class RemarkKind {
87     RK_Missing,     // Remark argument not present on the command line.
88     RK_Enabled,     // Remark enabled via '-Rgroup', i.e. -Rpass, -Rpass-missed,
89                     // -Rpass-analysis
90     RK_Disabled,    // Remark disabled via '-Rno-group', i.e. -Rno-pass,
91                     // -Rno-pass-missed, -Rno-pass-analysis.
92     RK_WithPattern, // Remark pattern specified via '-Rgroup=regexp'.
93   };
94 
95   /// \brief Code object version for AMDGPU.
96   llvm::CodeObjectVersionKind CodeObjectVersion =
97       llvm::CodeObjectVersionKind::COV_5;
98 
99   /// Optimization remark with an optional regular expression pattern.
100   struct OptRemark {
101     RemarkKind Kind = RemarkKind::RK_Missing;
102     std::string Pattern;
103     std::shared_ptr<llvm::Regex> Regex;
104 
105     /// By default, optimization remark is missing.
106     OptRemark() = default;
107 
108     /// Returns true iff the optimization remark holds a valid regular
109     /// expression.
110     bool hasValidPattern() const { return Regex != nullptr; }
111 
112     /// Matches the given string against the regex, if there is some.
113     bool patternMatches(llvm::StringRef string) const {
114       return hasValidPattern() && Regex->match(string);
115     }
116   };
117 
118   // The OptRemark fields provided here are identical to Clang.
119 
120   /// Selected optimizations for which we should enable optimization remarks.
121   /// Transformation passes whose name matches the contained (optional) regular
122   /// expression (and support this feature), will emit a diagnostic whenever
123   /// they perform a transformation.
124   OptRemark OptimizationRemark;
125 
126   /// Selected optimizations for which we should enable missed optimization
127   /// remarks. Transformation passes whose name matches the contained (optional)
128   /// regular expression (and support this feature), will emit a diagnostic
129   /// whenever they tried but failed to perform a transformation.
130   OptRemark OptimizationRemarkMissed;
131 
132   /// Selected optimizations for which we should enable optimization analyses.
133   /// Transformation passes whose name matches the contained (optional) regular
134   /// expression (and support this feature), will emit a diagnostic whenever
135   /// they want to explain why they decided to apply or not apply a given
136   /// transformation.
137   OptRemark OptimizationRemarkAnalysis;
138 
139   /// The code model to use (-mcmodel).
140   std::string CodeModel;
141 
142   /// The code model-specific large data threshold to use
143   /// (-mlarge-data-threshold).
144   uint64_t LargeDataThreshold;
145 
146   // Define accessors/mutators for code generation options of enumeration type.
147 #define CODEGENOPT(Name, Bits, Default)
148 #define ENUM_CODEGENOPT(Name, Type, Bits, Default)                             \
149   Type get##Name() const { return static_cast<Type>(Name); }                   \
150   void set##Name(Type Value) { Name = static_cast<unsigned>(Value); }
151 #include "flang/Frontend/CodeGenOptions.def"
152 
153   CodeGenOptions();
154 };
155 
156 std::optional<llvm::CodeModel::Model> getCodeModel(llvm::StringRef string);
157 
158 } // end namespace Fortran::frontend
159 
160 #endif // FORTRAN_FRONTEND_CODEGENOPTIONS_H
161