1 //===-- Optimizer/CodeGen/CodeGen.h -- code generation ----------*- 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 FORTRAN_OPTIMIZER_CODEGEN_CODEGEN_H
10 #define FORTRAN_OPTIMIZER_CODEGEN_CODEGEN_H
11 
12 #include "mlir/IR/BuiltinOps.h"
13 #include "mlir/Pass/Pass.h"
14 #include "mlir/Pass/PassRegistry.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Support/raw_ostream.h"
17 #include <memory>
18 
19 namespace fir {
20 
21 struct NameUniquer;
22 
23 /// Prerequiste pass for code gen. Perform intermediate rewrites to perform
24 /// the code gen (to LLVM-IR dialect) conversion.
25 std::unique_ptr<mlir::Pass> createFirCodeGenRewritePass();
26 
27 /// FirTargetRewritePass options.
28 struct TargetRewriteOptions {
29   bool noCharacterConversion{};
30   bool noComplexConversion{};
31 };
32 
33 /// Prerequiste pass for code gen. Perform intermediate rewrites to tailor the
34 /// FIR for the chosen target.
35 std::unique_ptr<mlir::OperationPass<mlir::ModuleOp>> createFirTargetRewritePass(
36     const TargetRewriteOptions &options = TargetRewriteOptions());
37 
38 /// FIR to LLVM translation pass options.
39 struct FIRToLLVMPassOptions {
40   // Do not fail when type descriptors are not found when translating
41   // operations that use them at the LLVM level like fir.embox. Instead,
42   // just use a null pointer.
43   // This is useful to test translating programs manually written where a
44   // frontend did not generate type descriptor data structures. However, note
45   // that such programs would crash at runtime if the derived type descriptors
46   // are required by the runtime, so this is only an option to help debugging.
47   bool ignoreMissingTypeDescriptors = false;
48 };
49 
50 /// Convert FIR to the LLVM IR dialect with default options.
51 std::unique_ptr<mlir::Pass> createFIRToLLVMPass();
52 
53 /// Convert FIR to the LLVM IR dialect
54 std::unique_ptr<mlir::Pass> createFIRToLLVMPass(FIRToLLVMPassOptions options);
55 
56 using LLVMIRLoweringPrinter =
57     std::function<void(llvm::Module &, llvm::raw_ostream &)>;
58 
59 /// Convert the LLVM IR dialect to LLVM-IR proper
60 std::unique_ptr<mlir::Pass> createLLVMDialectToLLVMPass(
61     llvm::raw_ostream &output,
62     LLVMIRLoweringPrinter printer =
63         [](llvm::Module &m, llvm::raw_ostream &out) { m.print(out, nullptr); });
64 
65 /// Convert boxproc values to a lower level representation. The default is to
66 /// use function pointers and thunks.
67 std::unique_ptr<mlir::Pass> createBoxedProcedurePass();
68 std::unique_ptr<mlir::Pass> createBoxedProcedurePass(bool useThunks);
69 
70 // declarative passes
71 #define GEN_PASS_REGISTRATION
72 #include "flang/Optimizer/CodeGen/CGPasses.h.inc"
73 
74 } // namespace fir
75 
76 #endif // FORTRAN_OPTIMIZER_CODEGEN_CODEGEN_H
77