1 //===-- Lower/Bridge.h -- main interface to lowering ------------*- 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_LOWER_BRIDGE_H 14 #define FORTRAN_LOWER_BRIDGE_H 15 16 #include "flang/Common/Fortran.h" 17 #include "flang/Frontend/CodeGenOptions.h" 18 #include "flang/Frontend/TargetOptions.h" 19 #include "flang/Lower/AbstractConverter.h" 20 #include "flang/Lower/EnvironmentDefault.h" 21 #include "flang/Lower/LoweringOptions.h" 22 #include "flang/Lower/StatementContext.h" 23 #include "flang/Optimizer/Builder/FIRBuilder.h" 24 #include "flang/Optimizer/Dialect/Support/KindMapping.h" 25 #include "mlir/IR/BuiltinOps.h" 26 #include "mlir/IR/OwningOpRef.h" 27 #include <set> 28 29 namespace llvm { 30 class TargetMachine; 31 } // namespace llvm 32 33 namespace Fortran { 34 namespace common { 35 class IntrinsicTypeDefaultKinds; 36 } // namespace common 37 namespace evaluate { 38 class IntrinsicProcTable; 39 class TargetCharacteristics; 40 } // namespace evaluate 41 namespace parser { 42 class AllCookedSources; 43 struct Program; 44 } // namespace parser 45 namespace semantics { 46 class SemanticsContext; 47 } // namespace semantics 48 49 namespace lower { 50 51 //===----------------------------------------------------------------------===// 52 // Lowering bridge 53 //===----------------------------------------------------------------------===// 54 55 /// The lowering bridge converts the front-end parse trees and semantics 56 /// checking residual to MLIR (FIR dialect) code. 57 class LoweringBridge { 58 public: 59 /// Create a lowering bridge instance. 60 static LoweringBridge 61 create(mlir::MLIRContext &ctx, 62 Fortran::semantics::SemanticsContext &semanticsContext, 63 const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds, 64 const Fortran::evaluate::IntrinsicProcTable &intrinsics, 65 const Fortran::evaluate::TargetCharacteristics &targetCharacteristics, 66 const Fortran::parser::AllCookedSources &allCooked, 67 llvm::StringRef triple, fir::KindMapping &kindMap, 68 const Fortran::lower::LoweringOptions &loweringOptions, 69 const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults, 70 const Fortran::common::LanguageFeatureControl &languageFeatures, 71 const llvm::TargetMachine &targetMachine, 72 const Fortran::frontend::TargetOptions &targetOptions, 73 const Fortran::frontend::CodeGenOptions &codeGenOptions) { 74 return LoweringBridge(ctx, semanticsContext, defaultKinds, intrinsics, 75 targetCharacteristics, allCooked, triple, kindMap, 76 loweringOptions, envDefaults, languageFeatures, 77 targetMachine, targetOptions, codeGenOptions); 78 } 79 80 //===--------------------------------------------------------------------===// 81 // Getters 82 //===--------------------------------------------------------------------===// 83 84 mlir::MLIRContext &getMLIRContext() { return context; } 85 86 /// Get the ModuleOp. It can never be null, which is asserted in the ctor. 87 mlir::ModuleOp getModule() { return *module; } 88 mlir::ModuleOp getModuleAndRelease() { return module.release(); } 89 90 const Fortran::common::IntrinsicTypeDefaultKinds &getDefaultKinds() const { 91 return defaultKinds; 92 } 93 const Fortran::evaluate::IntrinsicProcTable &getIntrinsicTable() const { 94 return intrinsics; 95 } 96 const Fortran::evaluate::TargetCharacteristics & 97 getTargetCharacteristics() const { 98 return targetCharacteristics; 99 } 100 const Fortran::parser::AllCookedSources *getCookedSource() const { 101 return cooked; 102 } 103 104 /// Get the kind map. 105 const fir::KindMapping &getKindMap() const { return kindMap; } 106 107 const Fortran::lower::LoweringOptions &getLoweringOptions() const { 108 return loweringOptions; 109 } 110 111 const std::vector<Fortran::lower::EnvironmentDefault> & 112 getEnvironmentDefaults() const { 113 return envDefaults; 114 } 115 116 const Fortran::common::LanguageFeatureControl &getLanguageFeatures() const { 117 return languageFeatures; 118 } 119 120 /// Create a folding context. Careful: this is very expensive. 121 Fortran::evaluate::FoldingContext createFoldingContext(); 122 123 Fortran::semantics::SemanticsContext &getSemanticsContext() const { 124 return semanticsContext; 125 } 126 127 Fortran::lower::StatementContext &fctCtx() { return functionContext; } 128 129 Fortran::lower::StatementContext &openAccCtx() { return openAccContext; } 130 131 bool validModule() { return getModule(); } 132 133 //===--------------------------------------------------------------------===// 134 // Perform the creation of an mlir::ModuleOp 135 //===--------------------------------------------------------------------===// 136 137 /// Read in an MLIR input file rather than lowering Fortran sources. 138 /// This is intended to be used for testing. 139 void parseSourceFile(llvm::SourceMgr &); 140 141 /// Cross the bridge from the Fortran parse-tree, etc. to MLIR dialects 142 void lower(const Fortran::parser::Program &program, 143 const Fortran::semantics::SemanticsContext &semanticsContext); 144 145 private: 146 explicit LoweringBridge( 147 mlir::MLIRContext &ctx, 148 Fortran::semantics::SemanticsContext &semanticsContext, 149 const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds, 150 const Fortran::evaluate::IntrinsicProcTable &intrinsics, 151 const Fortran::evaluate::TargetCharacteristics &targetCharacteristics, 152 const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple, 153 fir::KindMapping &kindMap, 154 const Fortran::lower::LoweringOptions &loweringOptions, 155 const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults, 156 const Fortran::common::LanguageFeatureControl &languageFeatures, 157 const llvm::TargetMachine &targetMachine, 158 const Fortran::frontend::TargetOptions &targetOptions, 159 const Fortran::frontend::CodeGenOptions &codeGenOptions); 160 LoweringBridge() = delete; 161 LoweringBridge(const LoweringBridge &) = delete; 162 163 Fortran::semantics::SemanticsContext &semanticsContext; 164 Fortran::lower::StatementContext functionContext; 165 Fortran::lower::StatementContext openAccContext; 166 const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds; 167 const Fortran::evaluate::IntrinsicProcTable &intrinsics; 168 const Fortran::evaluate::TargetCharacteristics &targetCharacteristics; 169 const Fortran::parser::AllCookedSources *cooked; 170 mlir::MLIRContext &context; 171 mlir::OwningOpRef<mlir::ModuleOp> module; 172 fir::KindMapping &kindMap; 173 const Fortran::lower::LoweringOptions &loweringOptions; 174 const std::vector<Fortran::lower::EnvironmentDefault> &envDefaults; 175 const Fortran::common::LanguageFeatureControl &languageFeatures; 176 std::set<std::string> tempNames; 177 }; 178 179 } // namespace lower 180 } // namespace Fortran 181 182 #endif // FORTRAN_LOWER_BRIDGE_H 183