Warning: This file is not a C or C++ file. It does not have highlighting.
1 | //===- FrontendActions.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 | // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef FORTRAN_FRONTEND_FRONTENDACTIONS_H |
14 | #define FORTRAN_FRONTEND_FRONTENDACTIONS_H |
15 | |
16 | #include "flang/Frontend/CodeGenOptions.h" |
17 | #include "flang/Frontend/FrontendAction.h" |
18 | #include "flang/Parser/parsing.h" |
19 | #include "flang/Semantics/semantics.h" |
20 | |
21 | #include "mlir/IR/BuiltinOps.h" |
22 | #include "llvm/ADT/StringRef.h" |
23 | #include "llvm/IR/Module.h" |
24 | #include <memory> |
25 | |
26 | namespace Fortran::frontend { |
27 | |
28 | // TODO: This is a copy from f18.cpp. It doesn't really belong here and should |
29 | // be moved to a more suitable place in future. |
30 | struct MeasurementVisitor { |
31 | template <typename A> |
32 | bool Pre(const A &) { |
33 | return true; |
34 | } |
35 | template <typename A> |
36 | void Post(const A &) { |
37 | ++objects; |
38 | bytes += sizeof(A); |
39 | } |
40 | size_t objects{0}, bytes{0}; |
41 | }; |
42 | |
43 | //===----------------------------------------------------------------------===// |
44 | // Custom Consumer Actions |
45 | //===----------------------------------------------------------------------===// |
46 | |
47 | class InputOutputTestAction : public FrontendAction { |
48 | void executeAction() override; |
49 | }; |
50 | |
51 | class InitOnlyAction : public FrontendAction { |
52 | void executeAction() override; |
53 | }; |
54 | |
55 | //===----------------------------------------------------------------------===// |
56 | // Prescan Actions |
57 | //===----------------------------------------------------------------------===// |
58 | class PrescanAction : public FrontendAction { |
59 | void executeAction() override = 0; |
60 | bool beginSourceFileAction() override; |
61 | }; |
62 | |
63 | class PrintPreprocessedAction : public PrescanAction { |
64 | void executeAction() override; |
65 | }; |
66 | |
67 | class DebugDumpProvenanceAction : public PrescanAction { |
68 | void executeAction() override; |
69 | }; |
70 | |
71 | class DebugDumpParsingLogAction : public PrescanAction { |
72 | void executeAction() override; |
73 | }; |
74 | |
75 | class DebugMeasureParseTreeAction : public PrescanAction { |
76 | void executeAction() override; |
77 | }; |
78 | |
79 | //===----------------------------------------------------------------------===// |
80 | // PrescanAndParse Actions |
81 | //===----------------------------------------------------------------------===// |
82 | class PrescanAndParseAction : public FrontendAction { |
83 | void executeAction() override = 0; |
84 | bool beginSourceFileAction() override; |
85 | }; |
86 | |
87 | class DebugUnparseNoSemaAction : public PrescanAndParseAction { |
88 | void executeAction() override; |
89 | }; |
90 | |
91 | class DebugDumpParseTreeNoSemaAction : public PrescanAndParseAction { |
92 | void executeAction() override; |
93 | }; |
94 | |
95 | //===----------------------------------------------------------------------===// |
96 | // PrescanAndSema Actions |
97 | // |
98 | // These actions will parse the input, run the semantic checks and execute |
99 | // their actions provided that no parsing or semantic errors were found. |
100 | //===----------------------------------------------------------------------===// |
101 | class PrescanAndSemaAction : public FrontendAction { |
102 | |
103 | void executeAction() override = 0; |
104 | bool beginSourceFileAction() override; |
105 | }; |
106 | |
107 | class DebugUnparseWithSymbolsAction : public PrescanAndSemaAction { |
108 | void executeAction() override; |
109 | }; |
110 | |
111 | class DebugUnparseAction : public PrescanAndSemaAction { |
112 | void executeAction() override; |
113 | }; |
114 | |
115 | class DebugDumpSymbolsAction : public PrescanAndSemaAction { |
116 | void executeAction() override; |
117 | }; |
118 | |
119 | class DebugDumpParseTreeAction : public PrescanAndSemaAction { |
120 | void executeAction() override; |
121 | }; |
122 | |
123 | class DebugDumpPFTAction : public PrescanAndSemaAction { |
124 | void executeAction() override; |
125 | }; |
126 | |
127 | class DebugPreFIRTreeAction : public PrescanAndSemaAction { |
128 | void executeAction() override; |
129 | }; |
130 | |
131 | class GetDefinitionAction : public PrescanAndSemaAction { |
132 | void executeAction() override; |
133 | }; |
134 | |
135 | class GetSymbolsSourcesAction : public PrescanAndSemaAction { |
136 | void executeAction() override; |
137 | }; |
138 | |
139 | class ParseSyntaxOnlyAction : public PrescanAndSemaAction { |
140 | void executeAction() override; |
141 | }; |
142 | |
143 | class PluginParseTreeAction : public PrescanAndSemaAction { |
144 | void executeAction() override = 0; |
145 | |
146 | public: |
147 | Fortran::parser::Parsing &getParsing(); |
148 | /// Creates an output file. This is just a wrapper for calling |
149 | /// CreateDefaultOutputFile from CompilerInstance. Use it to make sure that |
150 | /// your plugin respects driver's `-o` flag. |
151 | /// \param extension The extension to use for the output file (ignored when |
152 | /// the user decides to print to stdout via `-o -`) |
153 | /// \return Null on error, ostream for the output file otherwise |
154 | std::unique_ptr<llvm::raw_pwrite_stream> |
155 | createOutputFile(llvm::StringRef extension); |
156 | }; |
157 | |
158 | //===----------------------------------------------------------------------===// |
159 | // PrescanAndSemaDebug Actions |
160 | // |
161 | // These actions will parse the input, run the semantic checks and execute |
162 | // their actions _regardless of_ whether any semantic errors have been found. |
163 | // This can be useful when adding new languge feature and when you wish to |
164 | // investigate compiler output (e.g. the parse tree) despite any semantic |
165 | // errors. |
166 | // |
167 | // NOTE: Use with care and for development only! |
168 | //===----------------------------------------------------------------------===// |
169 | class PrescanAndSemaDebugAction : public FrontendAction { |
170 | |
171 | void executeAction() override = 0; |
172 | bool beginSourceFileAction() override; |
173 | }; |
174 | |
175 | class DebugDumpAllAction : public PrescanAndSemaDebugAction { |
176 | void executeAction() override; |
177 | }; |
178 | |
179 | //===----------------------------------------------------------------------===// |
180 | // CodeGen Actions |
181 | //===----------------------------------------------------------------------===// |
182 | /// Represents the type of "backend" action to perform by the corresponding |
183 | /// CodeGenAction. Note that from Flang's perspective, both LLVM and MLIR are |
184 | /// "backends" that are used for generating LLVM IR/BC, assembly files or |
185 | /// machine code. This enum captures "what" exactly one of these backends is to |
186 | /// do. The names are similar to what is used in Clang - this allows us to |
187 | /// maintain some level of consistency/similarity between the drivers. |
188 | enum class BackendActionTy { |
189 | Backend_EmitAssembly, ///< Emit native assembly files |
190 | Backend_EmitObj, ///< Emit native object files |
191 | Backend_EmitBC, ///< Emit LLVM bitcode files |
192 | Backend_EmitLL, ///< Emit human-readable LLVM assembly |
193 | Backend_EmitFIR, ///< Emit FIR files, possibly lowering via HLFIR |
194 | Backend_EmitHLFIR, ///< Emit HLFIR files before any passes run |
195 | }; |
196 | |
197 | /// Abstract base class for actions that generate code (MLIR, LLVM IR, assembly |
198 | /// and machine code). Every action that inherits from this class will at |
199 | /// least run the prescanning, parsing, semantic checks and lower the parse |
200 | /// tree to an MLIR module. |
201 | class CodeGenAction : public FrontendAction { |
202 | |
203 | void executeAction() override; |
204 | /// Runs prescan, parsing, sema and lowers to MLIR. |
205 | bool beginSourceFileAction() override; |
206 | /// Runs the optimization (aka middle-end) pipeline on the LLVM module |
207 | /// associated with this action. |
208 | void runOptimizationPipeline(llvm::raw_pwrite_stream &os); |
209 | |
210 | protected: |
211 | CodeGenAction(BackendActionTy act) : action{act} {}; |
212 | /// @name MLIR |
213 | /// { |
214 | std::unique_ptr<mlir::ModuleOp> mlirModule; |
215 | std::unique_ptr<mlir::MLIRContext> mlirCtx; |
216 | /// } |
217 | |
218 | /// @name LLVM IR |
219 | std::unique_ptr<llvm::LLVMContext> llvmCtx; |
220 | std::unique_ptr<llvm::Module> llvmModule; |
221 | |
222 | /// Embeds offload objects given with specified with -fembed-offload-object |
223 | void embedOffloadObjects(); |
224 | |
225 | /// Runs pass pipeline to lower HLFIR into FIR |
226 | void lowerHLFIRToFIR(); |
227 | |
228 | /// Generates an LLVM IR module from CodeGenAction::mlirModule and saves it |
229 | /// in CodeGenAction::llvmModule. |
230 | void generateLLVMIR(); |
231 | |
232 | BackendActionTy action; |
233 | |
234 | /// } |
235 | public: |
236 | ~CodeGenAction() override; |
237 | }; |
238 | |
239 | class EmitFIRAction : public CodeGenAction { |
240 | public: |
241 | EmitFIRAction() : CodeGenAction(BackendActionTy::Backend_EmitFIR) {} |
242 | }; |
243 | |
244 | class EmitHLFIRAction : public CodeGenAction { |
245 | public: |
246 | EmitHLFIRAction() : CodeGenAction(BackendActionTy::Backend_EmitHLFIR) {} |
247 | }; |
248 | |
249 | class EmitLLVMAction : public CodeGenAction { |
250 | public: |
251 | EmitLLVMAction() : CodeGenAction(BackendActionTy::Backend_EmitLL) {} |
252 | }; |
253 | |
254 | class EmitLLVMBitcodeAction : public CodeGenAction { |
255 | public: |
256 | EmitLLVMBitcodeAction() : CodeGenAction(BackendActionTy::Backend_EmitBC) {} |
257 | }; |
258 | |
259 | class EmitObjAction : public CodeGenAction { |
260 | public: |
261 | EmitObjAction() : CodeGenAction(BackendActionTy::Backend_EmitObj) {} |
262 | }; |
263 | |
264 | class EmitAssemblyAction : public CodeGenAction { |
265 | public: |
266 | EmitAssemblyAction() : CodeGenAction(BackendActionTy::Backend_EmitAssembly) {} |
267 | }; |
268 | |
269 | } // namespace Fortran::frontend |
270 | |
271 | #endif // FORTRAN_FRONTEND_FRONTENDACTIONS_H |
272 |
Warning: This file is not a C or C++ file. It does not have highlighting.