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 /// \file 10 /// Implements lowering. Convert Fortran source to 11 /// [MLIR](https://github.com/tensorflow/mlir). 12 /// 13 /// [Coding style](https://llvm.org/docs/CodingStandards.html) 14 /// 15 //===----------------------------------------------------------------------===// 16 17 #ifndef FORTRAN_LOWER_BRIDGE_H 18 #define FORTRAN_LOWER_BRIDGE_H 19 20 #include "flang/Common/Fortran.h" 21 #include "flang/Lower/AbstractConverter.h" 22 #include "flang/Optimizer/Support/KindMapping.h" 23 #include "mlir/IR/BuiltinOps.h" 24 25 namespace fir { 26 struct NameUniquer; 27 } 28 29 namespace Fortran { 30 namespace common { 31 class IntrinsicTypeDefaultKinds; 32 } // namespace common 33 namespace evaluate { 34 class IntrinsicProcTable; 35 } // namespace evaluate 36 namespace parser { 37 class AllCookedSources; 38 struct Program; 39 } // namespace parser 40 namespace semantics { 41 class SemanticsContext; 42 } // namespace semantics 43 44 namespace lower { 45 46 //===----------------------------------------------------------------------===// 47 // Lowering bridge 48 //===----------------------------------------------------------------------===// 49 50 /// The lowering bridge converts the front-end parse trees and semantics 51 /// checking residual to MLIR (FIR dialect) code. 52 class LoweringBridge { 53 public: 54 /// Create a lowering bridge instance. 55 static LoweringBridge create(const Fortran::common::IntrinsicTypeDefaultKinds & defaultKinds,const Fortran::evaluate::IntrinsicProcTable & intrinsics,const Fortran::parser::AllCookedSources & allCooked)56 create(const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds, 57 const Fortran::evaluate::IntrinsicProcTable &intrinsics, 58 const Fortran::parser::AllCookedSources &allCooked) { 59 return LoweringBridge{defaultKinds, intrinsics, allCooked}; 60 } 61 62 //===--------------------------------------------------------------------===// 63 // Getters 64 //===--------------------------------------------------------------------===// 65 getMLIRContext()66 mlir::MLIRContext &getMLIRContext() { return *context.get(); } getModule()67 mlir::ModuleOp &getModule() { return *module.get(); } getDefaultKinds()68 const Fortran::common::IntrinsicTypeDefaultKinds &getDefaultKinds() const { 69 return defaultKinds; 70 } getIntrinsicTable()71 const Fortran::evaluate::IntrinsicProcTable &getIntrinsicTable() const { 72 return intrinsics; 73 } getCookedSource()74 const Fortran::parser::AllCookedSources *getCookedSource() const { 75 return cooked; 76 } 77 78 /// Get the kind map. getKindMap()79 const fir::KindMapping &getKindMap() const { return kindMap; } 80 81 /// Create a folding context. Careful: this is very expensive. 82 Fortran::evaluate::FoldingContext createFoldingContext() const; 83 validModule()84 bool validModule() { return getModule(); } 85 86 //===--------------------------------------------------------------------===// 87 // Perform the creation of an mlir::ModuleOp 88 //===--------------------------------------------------------------------===// 89 90 /// Read in an MLIR input file rather than lowering Fortran sources. 91 /// This is intended to be used for testing. 92 void parseSourceFile(llvm::SourceMgr &); 93 94 /// Cross the bridge from the Fortran parse-tree, etc. to MLIR dialects 95 void lower(const Fortran::parser::Program &program, fir::NameUniquer &uniquer, 96 const Fortran::semantics::SemanticsContext &semanticsContext); 97 98 private: 99 explicit LoweringBridge( 100 const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds, 101 const Fortran::evaluate::IntrinsicProcTable &intrinsics, 102 const Fortran::parser::AllCookedSources &); 103 LoweringBridge() = delete; 104 LoweringBridge(const LoweringBridge &) = delete; 105 106 const Fortran::common::IntrinsicTypeDefaultKinds &defaultKinds; 107 const Fortran::evaluate::IntrinsicProcTable &intrinsics; 108 const Fortran::parser::AllCookedSources *cooked; 109 std::unique_ptr<mlir::MLIRContext> context; 110 std::unique_ptr<mlir::ModuleOp> module; 111 fir::KindMapping kindMap; 112 }; 113 114 } // namespace lower 115 } // namespace Fortran 116 117 #endif // FORTRAN_LOWER_BRIDGE_H 118