2018-06-22 00:49:33 +08:00
|
|
|
//===- Function.cpp - MLIR Function Classes -------------------------------===//
|
|
|
|
//
|
|
|
|
// Copyright 2019 The MLIR Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
// =============================================================================
|
|
|
|
|
2018-12-28 03:07:34 +08:00
|
|
|
#include "mlir/IR/Function.h"
|
2018-09-19 07:36:26 +08:00
|
|
|
#include "AttributeListStorage.h"
|
2018-08-20 12:17:22 +08:00
|
|
|
#include "mlir/IR/Attributes.h"
|
2018-09-08 00:08:13 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2018-07-26 05:08:16 +08:00
|
|
|
#include "mlir/IR/Module.h"
|
2018-07-27 09:09:20 +08:00
|
|
|
#include "mlir/IR/StmtVisitor.h"
|
2018-07-06 00:12:11 +08:00
|
|
|
#include "mlir/IR/Types.h"
|
2018-08-18 07:49:42 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2018-06-24 07:03:42 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-06-22 00:49:33 +08:00
|
|
|
using namespace mlir;
|
|
|
|
|
2018-11-09 04:28:35 +08:00
|
|
|
Function::Function(Kind kind, Location location, StringRef name,
|
2018-10-31 05:59:22 +08:00
|
|
|
FunctionType type, ArrayRef<NamedAttribute> attrs)
|
|
|
|
: nameAndKind(Identifier::get(name, type.getContext()), kind),
|
2018-12-28 03:07:34 +08:00
|
|
|
location(location), type(type), blocks(this) {
|
2018-09-19 07:36:26 +08:00
|
|
|
this->attrs = AttributeListStorage::get(attrs, getContext());
|
2018-12-28 03:07:34 +08:00
|
|
|
|
|
|
|
// Creating of an MLFunction automatically populates the entry block and
|
|
|
|
// arguments.
|
|
|
|
// TODO(clattner): Unify this behavior.
|
|
|
|
if (kind == Kind::MLFunc) {
|
|
|
|
// The body of an MLFunction always has one block.
|
|
|
|
auto *entry = new StmtBlock();
|
|
|
|
blocks.push_back(entry);
|
|
|
|
|
|
|
|
// Initialize the arguments.
|
|
|
|
entry->addArguments(type.getInputs());
|
|
|
|
}
|
2018-09-19 07:36:26 +08:00
|
|
|
}
|
2018-06-22 00:49:33 +08:00
|
|
|
|
2018-08-20 12:17:22 +08:00
|
|
|
Function::~Function() {
|
2018-12-28 03:07:34 +08:00
|
|
|
// Instructions may have cyclic references, which need to be dropped before we
|
|
|
|
// can start deleting them.
|
|
|
|
for (auto &bb : *this)
|
|
|
|
for (auto &inst : bb)
|
|
|
|
inst.dropAllReferences();
|
|
|
|
|
2018-08-20 12:17:22 +08:00
|
|
|
// Clean up function attributes referring to this function.
|
|
|
|
FunctionAttr::dropFunctionReference(this);
|
|
|
|
}
|
|
|
|
|
2018-09-19 07:36:26 +08:00
|
|
|
ArrayRef<NamedAttribute> Function::getAttrs() const {
|
|
|
|
if (attrs)
|
|
|
|
return attrs->getElements();
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2018-10-31 05:59:22 +08:00
|
|
|
MLIRContext *Function::getContext() const { return getType().getContext(); }
|
2018-07-06 00:12:11 +08:00
|
|
|
|
2018-07-26 05:08:16 +08:00
|
|
|
Module *llvm::ilist_traits<Function>::getContainingModule() {
|
|
|
|
size_t Offset(
|
|
|
|
size_t(&((Module *)nullptr->*Module::getSublistAccess(nullptr))));
|
|
|
|
iplist<Function> *Anchor(static_cast<iplist<Function> *>(this));
|
|
|
|
return reinterpret_cast<Module *>(reinterpret_cast<char *>(Anchor) - Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when a Function is added to a Module. We
|
2018-08-18 07:49:42 +08:00
|
|
|
/// keep the module pointer and module symbol table up to date.
|
2018-07-26 05:08:16 +08:00
|
|
|
void llvm::ilist_traits<Function>::addNodeToList(Function *function) {
|
|
|
|
assert(!function->getModule() && "already in a module!");
|
2018-08-18 07:49:42 +08:00
|
|
|
auto *module = getContainingModule();
|
|
|
|
function->module = module;
|
|
|
|
|
|
|
|
// Add this function to the symbol table of the module, uniquing the name if
|
|
|
|
// a conflict is detected.
|
2018-09-08 00:08:13 +08:00
|
|
|
if (!module->symbolTable.insert({function->getName(), function}).second) {
|
2018-08-18 07:49:42 +08:00
|
|
|
// If a conflict was detected, then the function will not have been added to
|
|
|
|
// the symbol table. Try suffixes until we get to a unique name that works.
|
|
|
|
SmallString<128> nameBuffer(function->getName().begin(),
|
|
|
|
function->getName().end());
|
|
|
|
unsigned originalLength = nameBuffer.size();
|
|
|
|
|
|
|
|
// Iteratively try suffixes until we find one that isn't used. We use a
|
|
|
|
// module level uniquing counter to avoid N^2 behavior.
|
|
|
|
do {
|
|
|
|
nameBuffer.resize(originalLength);
|
|
|
|
nameBuffer += '_';
|
|
|
|
nameBuffer += std::to_string(module->uniquingCounter++);
|
2018-09-08 00:08:13 +08:00
|
|
|
function->nameAndKind.setPointer(
|
|
|
|
Identifier::get(nameBuffer, module->getContext()));
|
|
|
|
} while (
|
|
|
|
!module->symbolTable.insert({function->getName(), function}).second);
|
2018-08-18 07:49:42 +08:00
|
|
|
}
|
2018-07-26 05:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when a Function is removed from a Module.
|
|
|
|
/// We keep the module pointer up to date.
|
|
|
|
void llvm::ilist_traits<Function>::removeNodeFromList(Function *function) {
|
|
|
|
assert(function->module && "not already in a module!");
|
2018-08-18 07:49:42 +08:00
|
|
|
|
|
|
|
// Remove the symbol table entry.
|
|
|
|
function->module->symbolTable.erase(function->getName());
|
2018-07-26 05:08:16 +08:00
|
|
|
function->module = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This is a trait method invoked when an instruction is moved from one block
|
|
|
|
/// to another. We keep the block pointer up to date.
|
|
|
|
void llvm::ilist_traits<Function>::transferNodesFromList(
|
|
|
|
ilist_traits<Function> &otherList, function_iterator first,
|
|
|
|
function_iterator last) {
|
|
|
|
// If we are transferring functions within the same module, the Module
|
|
|
|
// pointer doesn't need to be updated.
|
|
|
|
Module *curParent = getContainingModule();
|
|
|
|
if (curParent == otherList.getContainingModule())
|
|
|
|
return;
|
|
|
|
|
2018-08-18 07:49:42 +08:00
|
|
|
// Update the 'module' member and symbol table records for each function.
|
|
|
|
for (; first != last; ++first) {
|
|
|
|
removeNodeFromList(&*first);
|
|
|
|
addNodeToList(&*first);
|
|
|
|
}
|
2018-07-26 05:08:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Unlink this function from its Module and delete it.
|
2018-12-28 03:07:34 +08:00
|
|
|
void Function::erase() {
|
2018-07-26 05:08:16 +08:00
|
|
|
assert(getModule() && "Function has no parent");
|
|
|
|
getModule()->getFunctions().erase(this);
|
|
|
|
}
|
|
|
|
|
2018-09-08 00:08:13 +08:00
|
|
|
/// Emit a note about this instruction, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
|
|
|
void Function::emitNote(const Twine &message) const {
|
|
|
|
getContext()->emitDiagnostic(getLoc(), message,
|
|
|
|
MLIRContext::DiagnosticKind::Note);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Emit a warning about this operation, reporting up to any diagnostic
|
|
|
|
/// handlers that may be listening.
|
|
|
|
void Function::emitWarning(const Twine &message) const {
|
|
|
|
getContext()->emitDiagnostic(getLoc(), message,
|
|
|
|
MLIRContext::DiagnosticKind::Warning);
|
|
|
|
}
|
|
|
|
|
2018-12-08 01:30:25 +08:00
|
|
|
/// Emit an error about fatal conditions with this operation, reporting up to
|
|
|
|
/// any diagnostic handlers that may be listening. This function always
|
|
|
|
/// returns true. NOTE: This may terminate the containing application, only use
|
|
|
|
/// when the IR is in an inconsistent state.
|
|
|
|
bool Function::emitError(const Twine &message) const {
|
|
|
|
return getContext()->emitError(getLoc(), message);
|
2018-09-08 00:08:13 +08:00
|
|
|
}
|
2018-07-26 05:08:16 +08:00
|
|
|
|
2018-06-29 08:02:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MLFunction implementation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-12-28 13:21:41 +08:00
|
|
|
const OperationInst *MLFunction::getReturnStmt() const {
|
|
|
|
return cast<OperationInst>(&getBody()->back());
|
2018-08-14 02:30:36 +08:00
|
|
|
}
|
|
|
|
|
2018-12-28 13:21:41 +08:00
|
|
|
OperationInst *MLFunction::getReturnStmt() {
|
|
|
|
return cast<OperationInst>(&getBody()->back());
|
2018-08-14 02:30:36 +08:00
|
|
|
}
|
2018-10-12 04:52:57 +08:00
|
|
|
|
2018-12-28 13:21:41 +08:00
|
|
|
void MLFunction::walk(std::function<void(OperationInst *)> callback) {
|
2018-10-12 04:52:57 +08:00
|
|
|
struct Walker : public StmtWalker<Walker> {
|
2018-12-28 13:21:41 +08:00
|
|
|
std::function<void(OperationInst *)> const &callback;
|
|
|
|
Walker(std::function<void(OperationInst *)> const &callback)
|
2018-10-12 04:52:57 +08:00
|
|
|
: callback(callback) {}
|
|
|
|
|
2018-12-28 13:21:41 +08:00
|
|
|
void visitOperationInst(OperationInst *opStmt) { callback(opStmt); }
|
2018-10-12 04:52:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Walker v(callback);
|
|
|
|
v.walk(this);
|
|
|
|
}
|
|
|
|
|
2018-12-28 13:21:41 +08:00
|
|
|
void MLFunction::walkPostOrder(std::function<void(OperationInst *)> callback) {
|
2018-10-12 04:52:57 +08:00
|
|
|
struct Walker : public StmtWalker<Walker> {
|
2018-12-28 13:21:41 +08:00
|
|
|
std::function<void(OperationInst *)> const &callback;
|
|
|
|
Walker(std::function<void(OperationInst *)> const &callback)
|
2018-10-12 04:52:57 +08:00
|
|
|
: callback(callback) {}
|
|
|
|
|
2018-12-28 13:21:41 +08:00
|
|
|
void visitOperationInst(OperationInst *opStmt) { callback(opStmt); }
|
2018-10-12 04:52:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
Walker v(callback);
|
|
|
|
v.walkPostOrder(this);
|
|
|
|
}
|