blob: 38c4ef8936f84f742d923a48ad9daef92ac55b7e [file] [log] [blame]
Chris Lattnere2259872018-06-21 15:22:42 -07001//===- 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 Lattner974a8762018-08-17 16:49:42 -070026#include "llvm/ADT/DenseMap.h"
Chris Lattnera8e47672018-07-25 14:08:16 -070027#include "llvm/ADT/ilist.h"
Chris Lattner21e67f62018-07-06 10:46:19 -070028#include <vector>
Chris Lattnere2259872018-06-21 15:22:42 -070029
30namespace mlir {
Uday Bondhugulafaf37dd2018-06-29 18:09:29 -070031
32class AffineMap;
33
Chris Lattnere2259872018-06-21 15:22:42 -070034class Module {
35public:
Chris Lattner158e0a3e2018-07-08 20:51:38 -070036 explicit Module(MLIRContext *context);
37
38 MLIRContext *getContext() const { return context; }
Chris Lattnere2259872018-06-21 15:22:42 -070039
Chris Lattnera8e47672018-07-25 14:08:16 -070040 /// 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 Lattnere2259872018-06-21 15:22:42 -070059
Chris Lattner974a8762018-08-17 16:49:42 -070060 // Interfaces for working with the symbol table.
61
62 /// Look up a function with the specified name, returning null if no such
Chris Lattner4613d9e2018-08-19 21:17:22 -070063 /// name exists. Function names never include the @ on them.
Chris Lattner974a8762018-08-17 16:49:42 -070064 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
Chris Lattner4613d9e2018-08-19 21:17:22 -070070 /// name exists. Function names never include the @ on them.
Chris Lattner974a8762018-08-17 16:49:42 -070071 Function *getNamedFunction(Identifier name);
72
73 const Function *getNamedFunction(Identifier name) const {
74 return const_cast<Module *>(this)->getNamedFunction(name);
75 }
76
Chris Lattner21e67f62018-07-06 10:46:19 -070077 /// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -070078 /// 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 Lattner21e67f62018-07-06 10:46:19 -070081
Chris Lattner4c95a502018-06-23 16:03:42 -070082 void print(raw_ostream &os) const;
83 void dump() const;
Chris Lattner158e0a3e2018-07-08 20:51:38 -070084
Chris Lattner974a8762018-08-17 16:49:42 -070085private:
86 friend struct llvm::ilist_traits<Function>;
87
Chris Lattnera8e47672018-07-25 14:08:16 -070088 /// getSublistAccess() - Returns pointer to member of function list
89 static FunctionListType Module::*getSublistAccess(Function *) {
90 return &Module::functions;
91 }
92
Chris Lattner158e0a3e2018-07-08 20:51:38 -070093 MLIRContext *context;
Chris Lattner974a8762018-08-17 16:49:42 -070094
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 Lattnera8e47672018-07-25 14:08:16 -0700102 FunctionListType functions;
Chris Lattnere2259872018-06-21 15:22:42 -0700103};
104} // end namespace mlir
105
106#endif // MLIR_IR_FUNCTION_H