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 class LLVMTypeConverter; 22 23 struct NameUniquer; 24 25 #define GEN_PASS_DECL_FIRTOLLVMLOWERING 26 #define GEN_PASS_DECL_CODEGENREWRITE 27 #define GEN_PASS_DECL_TARGETREWRITEPASS 28 #define GEN_PASS_DECL_BOXEDPROCEDUREPASS 29 #include "flang/Optimizer/CodeGen/CGPasses.h.inc" 30 31 /// FIR to LLVM translation pass options. 32 struct FIRToLLVMPassOptions { 33 // Do not fail when type descriptors are not found when translating 34 // operations that use them at the LLVM level like fir.embox. Instead, 35 // just use a null pointer. 36 // This is useful to test translating programs manually written where a 37 // frontend did not generate type descriptor data structures. However, note 38 // that such programs would crash at runtime if the derived type descriptors 39 // are required by the runtime, so this is only an option to help debugging. 40 bool ignoreMissingTypeDescriptors = false; 41 42 // Generate TBAA information for FIR types and memory accessing operations. 43 bool applyTBAA = false; 44 45 // Force the usage of a unified tbaa tree in TBAABuilder. 46 bool forceUnifiedTBAATree = false; 47 48 // If set to true, then the global variables created 49 // for the derived types have been renamed to avoid usage 50 // of special symbols that may not be supported by all targets. 51 // The renaming is done by the CompilerGeneratedNamesConversion pass. 52 // If it is true, FIR-to-LLVM pass has to use 53 // fir::NameUniquer::getTypeDescriptorAssemblyName() to take 54 // the name of the global variable corresponding to a derived 55 // type's descriptor. 56 bool typeDescriptorsRenamedForAssembly = false; 57 }; 58 59 /// Convert FIR to the LLVM IR dialect with default options. 60 std::unique_ptr<mlir::Pass> createFIRToLLVMPass(); 61 62 /// Convert FIR to the LLVM IR dialect 63 std::unique_ptr<mlir::Pass> createFIRToLLVMPass(FIRToLLVMPassOptions options); 64 65 using LLVMIRLoweringPrinter = 66 std::function<void(llvm::Module &, llvm::raw_ostream &)>; 67 68 /// Convert the LLVM IR dialect to LLVM-IR proper 69 std::unique_ptr<mlir::Pass> createLLVMDialectToLLVMPass( 70 llvm::raw_ostream &output, 71 LLVMIRLoweringPrinter printer = 72 [](llvm::Module &m, llvm::raw_ostream &out) { m.print(out, nullptr); }); 73 74 /// Populate the given list with patterns that convert from FIR to LLVM. 75 void populateFIRToLLVMConversionPatterns( 76 const fir::LLVMTypeConverter &converter, mlir::RewritePatternSet &patterns, 77 fir::FIRToLLVMPassOptions &options); 78 79 /// Populate the pattern set with the PreCGRewrite patterns. 80 void populatePreCGRewritePatterns(mlir::RewritePatternSet &patterns, 81 bool preserveDeclare); 82 83 // declarative passes 84 #define GEN_PASS_REGISTRATION 85 #include "flang/Optimizer/CodeGen/CGPasses.h.inc" 86 87 } // namespace fir 88 89 #endif // FORTRAN_OPTIMIZER_CODEGEN_CODEGEN_H 90