Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 1 | //===- Function.cpp - MLIR Function Classes -------------------------------===// |
| 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 | // ============================================================================= |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 17 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 18 | #include "mlir/IR/CFGFunction.h" |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 19 | #include "mlir/IR/MLFunction.h" |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 20 | #include "mlir/IR/Module.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 21 | #include "mlir/IR/Types.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 22 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 23 | using namespace mlir; |
| 24 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 25 | Function::Function(StringRef name, FunctionType *type, Kind kind) |
| 26 | : kind(kind), name(name.str()), type(type) { |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 29 | MLIRContext *Function::getContext() const { return getType()->getContext(); } |
| 30 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 31 | /// Delete this object. |
| 32 | void Function::destroy() { |
| 33 | switch (getKind()) { |
| 34 | case Kind::ExtFunc: |
| 35 | delete cast<ExtFunction>(this); |
| 36 | break; |
| 37 | case Kind::MLFunc: |
| 38 | delete cast<MLFunction>(this); |
| 39 | break; |
| 40 | case Kind::CFGFunc: |
| 41 | delete cast<CFGFunction>(this); |
| 42 | break; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | Module *llvm::ilist_traits<Function>::getContainingModule() { |
| 47 | size_t Offset( |
| 48 | size_t(&((Module *)nullptr->*Module::getSublistAccess(nullptr)))); |
| 49 | iplist<Function> *Anchor(static_cast<iplist<Function> *>(this)); |
| 50 | return reinterpret_cast<Module *>(reinterpret_cast<char *>(Anchor) - Offset); |
| 51 | } |
| 52 | |
| 53 | /// This is a trait method invoked when a Function is added to a Module. We |
| 54 | /// keep the module pointer up to date. |
| 55 | void llvm::ilist_traits<Function>::addNodeToList(Function *function) { |
| 56 | assert(!function->getModule() && "already in a module!"); |
| 57 | function->module = getContainingModule(); |
| 58 | } |
| 59 | |
| 60 | /// This is a trait method invoked when a Function is removed from a Module. |
| 61 | /// We keep the module pointer up to date. |
| 62 | void llvm::ilist_traits<Function>::removeNodeFromList(Function *function) { |
| 63 | assert(function->module && "not already in a module!"); |
| 64 | function->module = nullptr; |
| 65 | } |
| 66 | |
| 67 | /// This is a trait method invoked when an instruction is moved from one block |
| 68 | /// to another. We keep the block pointer up to date. |
| 69 | void llvm::ilist_traits<Function>::transferNodesFromList( |
| 70 | ilist_traits<Function> &otherList, function_iterator first, |
| 71 | function_iterator last) { |
| 72 | // If we are transferring functions within the same module, the Module |
| 73 | // pointer doesn't need to be updated. |
| 74 | Module *curParent = getContainingModule(); |
| 75 | if (curParent == otherList.getContainingModule()) |
| 76 | return; |
| 77 | |
| 78 | // Update the 'module' member of each function. |
| 79 | for (; first != last; ++first) |
| 80 | first->module = curParent; |
| 81 | } |
| 82 | |
| 83 | /// Unlink this function from its Module and delete it. |
| 84 | void Function::eraseFromModule() { |
| 85 | assert(getModule() && "Function has no parent"); |
| 86 | getModule()->getFunctions().erase(this); |
| 87 | } |
| 88 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 89 | //===----------------------------------------------------------------------===// |
| 90 | // ExtFunction implementation. |
| 91 | //===----------------------------------------------------------------------===// |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 92 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 93 | ExtFunction::ExtFunction(StringRef name, FunctionType *type) |
| 94 | : Function(name, type, Kind::ExtFunc) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 97 | //===----------------------------------------------------------------------===// |
| 98 | // CFGFunction implementation. |
| 99 | //===----------------------------------------------------------------------===// |
| 100 | |
| 101 | CFGFunction::CFGFunction(StringRef name, FunctionType *type) |
| 102 | : Function(name, type, Kind::CFGFunc) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 103 | } |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 104 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 105 | CFGFunction::~CFGFunction() { |
| 106 | // Instructions may have cyclic references, which need to be dropped before we |
| 107 | // can start deleting them. |
| 108 | for (auto &bb : *this) { |
| 109 | for (auto &inst : bb) |
| 110 | inst.dropAllReferences(); |
| 111 | } |
| 112 | } |
| 113 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 114 | //===----------------------------------------------------------------------===// |
| 115 | // MLFunction implementation. |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | MLFunction::MLFunction(StringRef name, FunctionType *type) |
Tatiana Shpeisman | c2d88e9 | 2018-07-14 16:44:22 -0700 | [diff] [blame] | 119 | : Function(name, type, Kind::MLFunc), StmtBlock(StmtBlockKind::MLFunc) {} |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 120 | |
| 121 | MLFunction::~MLFunction() { |
| 122 | // TODO: When move SSA stuff is supported. |
| 123 | // dropAllReferences(); |
| 124 | } |