Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 1 | //===- Function.h - MLIR Function Class -------------------------*- C++ -*-===// |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 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 | // |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 18 | // Functions are the basic unit of composition in MLIR. There are three |
| 19 | // different kinds of functions: external functions, CFG functions, and ML |
| 20 | // functions. |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 21 | // |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | #ifndef MLIR_IR_FUNCTION_H |
| 25 | #define MLIR_IR_FUNCTION_H |
| 26 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 27 | #include "mlir/Support/LLVM.h" |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 28 | #include "llvm/ADT/ilist.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 29 | |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 30 | namespace mlir { |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 31 | class FunctionType; |
| 32 | class MLIRContext; |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 33 | class Module; |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 34 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 35 | /// This is the base class for all of the MLIR function types. |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 36 | class Function : public llvm::ilist_node_with_parent<Function, Module> { |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 37 | public: |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 38 | enum class Kind { ExtFunc, CFGFunc, MLFunc }; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 39 | |
| 40 | Kind getKind() const { return kind; } |
| 41 | |
| 42 | /// Return the name of this function, without the @. |
| 43 | const std::string &getName() const { return name; } |
| 44 | |
| 45 | /// Return the type of this function. |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 46 | FunctionType *getType() const { return type; } |
| 47 | |
| 48 | MLIRContext *getContext() const; |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 49 | Module *getModule() { return module; } |
| 50 | const Module *getModule() const { return module; } |
| 51 | |
| 52 | /// Unlink this instruction from its module and delete it. |
| 53 | void eraseFromModule(); |
| 54 | |
| 55 | /// Delete this object. |
| 56 | void destroy(); |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 57 | |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 58 | /// Perform (potentially expensive) checks of invariants, used to detect |
Chris Lattner | 4074644 | 2018-07-21 14:32:09 -0700 | [diff] [blame] | 59 | /// compiler bugs. On error, this fills in the string and return true, |
| 60 | /// or aborts if the string was not provided. |
| 61 | bool verify(std::string *errorResult = nullptr) const; |
Chris Lattner | 21e67f6 | 2018-07-06 10:46:19 -0700 | [diff] [blame] | 62 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 63 | void print(raw_ostream &os) const; |
| 64 | void dump() const; |
| 65 | |
| 66 | protected: |
| 67 | Function(StringRef name, FunctionType *type, Kind kind); |
| 68 | ~Function() {} |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 69 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 70 | private: |
| 71 | Kind kind; |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 72 | Module *module = nullptr; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 73 | std::string name; |
| 74 | FunctionType *const type; |
| 75 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 76 | void operator=(const Function &) = delete; |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 77 | friend struct llvm::ilist_traits<Function>; |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /// An extfunc declaration is a declaration of a function signature that is |
| 81 | /// defined in some other module. |
| 82 | class ExtFunction : public Function { |
| 83 | public: |
| 84 | ExtFunction(StringRef name, FunctionType *type); |
| 85 | |
| 86 | /// Methods for support type inquiry through isa, cast, and dyn_cast. |
| 87 | static bool classof(const Function *func) { |
| 88 | return func->getKind() == Kind::ExtFunc; |
| 89 | } |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 92 | } // end namespace mlir |
| 93 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame^] | 94 | //===----------------------------------------------------------------------===// |
| 95 | // ilist_traits for Function |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
| 98 | namespace llvm { |
| 99 | |
| 100 | template <> |
| 101 | struct ilist_traits<::mlir::Function> |
| 102 | : public ilist_alloc_traits<::mlir::Function> { |
| 103 | using Function = ::mlir::Function; |
| 104 | using function_iterator = simple_ilist<Function>::iterator; |
| 105 | |
| 106 | static void deleteNode(Function *inst) { inst->destroy(); } |
| 107 | |
| 108 | void addNodeToList(Function *function); |
| 109 | void removeNodeFromList(Function *function); |
| 110 | void transferNodesFromList(ilist_traits<Function> &otherList, |
| 111 | function_iterator first, function_iterator last); |
| 112 | |
| 113 | private: |
| 114 | mlir::Module *getContainingModule(); |
| 115 | }; |
| 116 | } // end namespace llvm |
| 117 | |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 118 | #endif // MLIR_IR_FUNCTION_H |