Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 1 | //===- Module.h - MLIR Module Class -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Copyright 2019 The MLIR Authors. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | // ============================================================================= |
| 17 | // |
| 18 | // Module is the top-level container for code in an MLIR program. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #ifndef MLIR_IR_MODULE_H |
| 23 | #define MLIR_IR_MODULE_H |
| 24 | |
| 25 | #include "mlir/IR/Function.h" |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 26 | #include "llvm/ADT/DenseMap.h" |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 27 | #include "llvm/ADT/ilist.h" |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 28 | #include <vector> |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 29 | |
| 30 | namespace mlir { |
Uday Bondhugula | faf37dd | 2018-06-29 18:09:29 -0700 | [diff] [blame] | 31 | |
| 32 | class AffineMap; |
| 33 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 34 | class Module { |
| 35 | public: |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 36 | explicit Module(MLIRContext *context); |
| 37 | |
| 38 | MLIRContext *getContext() const { return context; } |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 39 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 40 | /// This is the list of functions in the module. |
| 41 | typedef llvm::iplist<Function> FunctionListType; |
| 42 | FunctionListType &getFunctions() { return functions; } |
| 43 | const FunctionListType &getFunctions() const { return functions; } |
| 44 | |
| 45 | // Iteration over the functions in the module. |
| 46 | using iterator = FunctionListType::iterator; |
| 47 | using const_iterator = FunctionListType::const_iterator; |
| 48 | using reverse_iterator = FunctionListType::reverse_iterator; |
| 49 | using const_reverse_iterator = FunctionListType::const_reverse_iterator; |
| 50 | |
| 51 | iterator begin() { return functions.begin(); } |
| 52 | iterator end() { return functions.end(); } |
| 53 | const_iterator begin() const { return functions.begin(); } |
| 54 | const_iterator end() const { return functions.end(); } |
| 55 | reverse_iterator rbegin() { return functions.rbegin(); } |
| 56 | reverse_iterator rend() { return functions.rend(); } |
| 57 | const_reverse_iterator rbegin() const { return functions.rbegin(); } |
| 58 | const_reverse_iterator rend() const { return functions.rend(); } |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 59 | |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 60 | // Interfaces for working with the symbol table. |
| 61 | |
| 62 | /// Look up a function with the specified name, returning null if no such |
| 63 | /// name exists. |
| 64 | Function *getNamedFunction(StringRef name); |
| 65 | const Function *getNamedFunction(StringRef name) const { |
| 66 | return const_cast<Module *>(this)->getNamedFunction(name); |
| 67 | } |
| 68 | |
| 69 | /// Look up a function with the specified name, returning null if no such |
| 70 | /// name exists. |
| 71 | Function *getNamedFunction(Identifier name); |
| 72 | |
| 73 | const Function *getNamedFunction(Identifier name) const { |
| 74 | return const_cast<Module *>(this)->getNamedFunction(name); |
| 75 | } |
| 76 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 77 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 78 | /// compiler bugs. On error, this fills in the string and return true, |
| 79 | /// or aborts if the string was not provided. |
| 80 | bool verify(std::string *errorResult = nullptr) const; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 81 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 82 | void print(raw_ostream &os) const; |
| 83 | void dump() const; |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 84 | |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 85 | private: |
| 86 | friend struct llvm::ilist_traits<Function>; |
| 87 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 88 | /// getSublistAccess() - Returns pointer to member of function list |
| 89 | static FunctionListType Module::*getSublistAccess(Function *) { |
| 90 | return &Module::functions; |
| 91 | } |
| 92 | |
Chris Lattner | 158e0a3e | 2018-07-08 20:51:38 -0700 | [diff] [blame] | 93 | MLIRContext *context; |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 94 | |
| 95 | /// This is a mapping from a name to the function with that name. |
| 96 | llvm::DenseMap<Identifier, Function *> symbolTable; |
| 97 | |
| 98 | /// This is used when name conflicts are detected. |
| 99 | unsigned uniquingCounter = 0; |
| 100 | |
| 101 | /// This is the actual list of functions the module contains. |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 102 | FunctionListType functions; |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 103 | }; |
| 104 | } // end namespace mlir |
| 105 | |
| 106 | #endif // MLIR_IR_FUNCTION_H |