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" |
Tatiana Shpeisman | 60bf7be | 2018-07-26 18:09:20 -0700 | [diff] [blame] | 21 | #include "mlir/IR/StmtVisitor.h" |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 22 | #include "mlir/IR/Types.h" |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 23 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 24 | #include "llvm/ADT/StringRef.h" |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 25 | using namespace mlir; |
| 26 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 27 | Function::Function(StringRef name, FunctionType *type, Kind kind) |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 28 | : kind(kind), name(Identifier::get(name, type->getContext())), type(type) {} |
Chris Lattner | c0c5e0f | 2018-06-21 09:49:33 -0700 | [diff] [blame] | 29 | |
Chris Lattner | ff0d590 | 2018-07-05 09:12:11 -0700 | [diff] [blame] | 30 | MLIRContext *Function::getContext() const { return getType()->getContext(); } |
| 31 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 32 | /// Delete this object. |
| 33 | void Function::destroy() { |
| 34 | switch (getKind()) { |
| 35 | case Kind::ExtFunc: |
| 36 | delete cast<ExtFunction>(this); |
| 37 | break; |
| 38 | case Kind::MLFunc: |
Tatiana Shpeisman | bc3c749 | 2018-08-06 11:54:39 -0700 | [diff] [blame] | 39 | cast<MLFunction>(this)->destroy(); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 40 | break; |
| 41 | case Kind::CFGFunc: |
| 42 | delete cast<CFGFunction>(this); |
| 43 | break; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | Module *llvm::ilist_traits<Function>::getContainingModule() { |
| 48 | size_t Offset( |
| 49 | size_t(&((Module *)nullptr->*Module::getSublistAccess(nullptr)))); |
| 50 | iplist<Function> *Anchor(static_cast<iplist<Function> *>(this)); |
| 51 | return reinterpret_cast<Module *>(reinterpret_cast<char *>(Anchor) - Offset); |
| 52 | } |
| 53 | |
| 54 | /// This is a trait method invoked when a Function is added to a Module. We |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 55 | /// keep the module pointer and module symbol table up to date. |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 56 | void llvm::ilist_traits<Function>::addNodeToList(Function *function) { |
| 57 | assert(!function->getModule() && "already in a module!"); |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 58 | auto *module = getContainingModule(); |
| 59 | function->module = module; |
| 60 | |
| 61 | // Add this function to the symbol table of the module, uniquing the name if |
| 62 | // a conflict is detected. |
| 63 | if (!module->symbolTable.insert({function->name, function}).second) { |
| 64 | // If a conflict was detected, then the function will not have been added to |
| 65 | // the symbol table. Try suffixes until we get to a unique name that works. |
| 66 | SmallString<128> nameBuffer(function->getName().begin(), |
| 67 | function->getName().end()); |
| 68 | unsigned originalLength = nameBuffer.size(); |
| 69 | |
| 70 | // Iteratively try suffixes until we find one that isn't used. We use a |
| 71 | // module level uniquing counter to avoid N^2 behavior. |
| 72 | do { |
| 73 | nameBuffer.resize(originalLength); |
| 74 | nameBuffer += '_'; |
| 75 | nameBuffer += std::to_string(module->uniquingCounter++); |
| 76 | function->name = Identifier::get(nameBuffer, module->getContext()); |
| 77 | } while (!module->symbolTable.insert({function->name, function}).second); |
| 78 | } |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /// This is a trait method invoked when a Function is removed from a Module. |
| 82 | /// We keep the module pointer up to date. |
| 83 | void llvm::ilist_traits<Function>::removeNodeFromList(Function *function) { |
| 84 | assert(function->module && "not already in a module!"); |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 85 | |
| 86 | // Remove the symbol table entry. |
| 87 | function->module->symbolTable.erase(function->getName()); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 88 | function->module = nullptr; |
| 89 | } |
| 90 | |
| 91 | /// This is a trait method invoked when an instruction is moved from one block |
| 92 | /// to another. We keep the block pointer up to date. |
| 93 | void llvm::ilist_traits<Function>::transferNodesFromList( |
| 94 | ilist_traits<Function> &otherList, function_iterator first, |
| 95 | function_iterator last) { |
| 96 | // If we are transferring functions within the same module, the Module |
| 97 | // pointer doesn't need to be updated. |
| 98 | Module *curParent = getContainingModule(); |
| 99 | if (curParent == otherList.getContainingModule()) |
| 100 | return; |
| 101 | |
Chris Lattner | 974a876 | 2018-08-17 16:49:42 -0700 | [diff] [blame^] | 102 | // Update the 'module' member and symbol table records for each function. |
| 103 | for (; first != last; ++first) { |
| 104 | removeNodeFromList(&*first); |
| 105 | addNodeToList(&*first); |
| 106 | } |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /// Unlink this function from its Module and delete it. |
| 110 | void Function::eraseFromModule() { |
| 111 | assert(getModule() && "Function has no parent"); |
| 112 | getModule()->getFunctions().erase(this); |
| 113 | } |
| 114 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 115 | //===----------------------------------------------------------------------===// |
| 116 | // ExtFunction implementation. |
| 117 | //===----------------------------------------------------------------------===// |
Chris Lattner | f7e2273 | 2018-06-22 22:03:48 -0700 | [diff] [blame] | 118 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 119 | ExtFunction::ExtFunction(StringRef name, FunctionType *type) |
| 120 | : Function(name, type, Kind::ExtFunc) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | 4c95a50 | 2018-06-23 16:03:42 -0700 | [diff] [blame] | 123 | //===----------------------------------------------------------------------===// |
| 124 | // CFGFunction implementation. |
| 125 | //===----------------------------------------------------------------------===// |
| 126 | |
| 127 | CFGFunction::CFGFunction(StringRef name, FunctionType *type) |
| 128 | : Function(name, type, Kind::CFGFunc) { |
Chris Lattner | e225987 | 2018-06-21 15:22:42 -0700 | [diff] [blame] | 129 | } |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 130 | |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 131 | CFGFunction::~CFGFunction() { |
| 132 | // Instructions may have cyclic references, which need to be dropped before we |
| 133 | // can start deleting them. |
| 134 | for (auto &bb : *this) { |
| 135 | for (auto &inst : bb) |
| 136 | inst.dropAllReferences(); |
James Molloy | f8bbadb | 2018-08-01 11:53:50 -0700 | [diff] [blame] | 137 | if (bb.getTerminator()) |
| 138 | bb.getTerminator()->dropAllReferences(); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 142 | //===----------------------------------------------------------------------===// |
| 143 | // MLFunction implementation. |
| 144 | //===----------------------------------------------------------------------===// |
| 145 | |
Tatiana Shpeisman | bc3c749 | 2018-08-06 11:54:39 -0700 | [diff] [blame] | 146 | /// Create a new MLFunction with the specific fields. |
| 147 | MLFunction *MLFunction::create(StringRef name, FunctionType *type) { |
| 148 | const auto &argTypes = type->getInputs(); |
| 149 | auto byteSize = totalSizeToAlloc<MLFuncArgument>(argTypes.size()); |
| 150 | void *rawMem = malloc(byteSize); |
| 151 | |
| 152 | // Initialize the MLFunction part of the function object. |
| 153 | auto function = ::new (rawMem) MLFunction(name, type); |
| 154 | |
| 155 | // Initialize the arguments. |
| 156 | auto arguments = function->getArgumentsInternal(); |
| 157 | for (unsigned i = 0, e = argTypes.size(); i != e; ++i) |
| 158 | new (&arguments[i]) MLFuncArgument(argTypes[i], function); |
| 159 | return function; |
| 160 | } |
| 161 | |
Tatiana Shpeisman | c96b587 | 2018-06-28 17:02:32 -0700 | [diff] [blame] | 162 | MLFunction::MLFunction(StringRef name, FunctionType *type) |
Tatiana Shpeisman | c2d88e9 | 2018-07-14 16:44:22 -0700 | [diff] [blame] | 163 | : Function(name, type, Kind::MLFunc), StmtBlock(StmtBlockKind::MLFunc) {} |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 164 | |
| 165 | MLFunction::~MLFunction() { |
Tatiana Shpeisman | bc3c749 | 2018-08-06 11:54:39 -0700 | [diff] [blame] | 166 | // Explicitly erase statements instead of relying of 'StmtBlock' destructor |
| 167 | // since child statements need to be destroyed before function arguments |
| 168 | // are destroyed. |
| 169 | clear(); |
| 170 | |
| 171 | // Explicitly run the destructors for the function arguments. |
| 172 | for (auto &arg : getArgumentsInternal()) |
| 173 | arg.~MLFuncArgument(); |
| 174 | } |
| 175 | |
| 176 | void MLFunction::destroy() { |
| 177 | this->~MLFunction(); |
| 178 | free(this); |
Chris Lattner | a8e4767 | 2018-07-25 14:08:16 -0700 | [diff] [blame] | 179 | } |
Uday Bondhugula | 2ce3b57 | 2018-08-13 11:30:36 -0700 | [diff] [blame] | 180 | |
| 181 | const OperationStmt *MLFunction::getReturnStmt() const { |
| 182 | return cast<OperationStmt>(&back()); |
| 183 | } |
| 184 | |
| 185 | OperationStmt *MLFunction::getReturnStmt() { |
| 186 | return cast<OperationStmt>(&back()); |
| 187 | } |