blob: 60beeca612ee5669a34ea6101c46a24de02cadee [file] [log] [blame]
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -07001//===- 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 Lattnerc0c5e0f2018-06-21 09:49:33 -070017
Chris Lattner4c95a502018-06-23 16:03:42 -070018#include "mlir/IR/CFGFunction.h"
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -070019#include "mlir/IR/MLFunction.h"
Chris Lattnera8e47672018-07-25 14:08:16 -070020#include "mlir/IR/Module.h"
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -070021#include "mlir/IR/StmtVisitor.h"
Chris Lattnerff0d5902018-07-05 09:12:11 -070022#include "mlir/IR/Types.h"
Chris Lattner4c95a502018-06-23 16:03:42 -070023#include "llvm/ADT/StringRef.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070024using namespace mlir;
25
Chris Lattner4c95a502018-06-23 16:03:42 -070026Function::Function(StringRef name, FunctionType *type, Kind kind)
27 : kind(kind), name(name.str()), type(type) {
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070028}
29
Chris Lattnerff0d5902018-07-05 09:12:11 -070030MLIRContext *Function::getContext() const { return getType()->getContext(); }
31
Chris Lattnera8e47672018-07-25 14:08:16 -070032/// Delete this object.
33void Function::destroy() {
34 switch (getKind()) {
35 case Kind::ExtFunc:
36 delete cast<ExtFunction>(this);
37 break;
38 case Kind::MLFunc:
39 delete cast<MLFunction>(this);
40 break;
41 case Kind::CFGFunc:
42 delete cast<CFGFunction>(this);
43 break;
44 }
45}
46
47Module *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
55/// keep the module pointer up to date.
56void llvm::ilist_traits<Function>::addNodeToList(Function *function) {
57 assert(!function->getModule() && "already in a module!");
58 function->module = getContainingModule();
59}
60
61/// This is a trait method invoked when a Function is removed from a Module.
62/// We keep the module pointer up to date.
63void llvm::ilist_traits<Function>::removeNodeFromList(Function *function) {
64 assert(function->module && "not already in a module!");
65 function->module = nullptr;
66}
67
68/// This is a trait method invoked when an instruction is moved from one block
69/// to another. We keep the block pointer up to date.
70void llvm::ilist_traits<Function>::transferNodesFromList(
71 ilist_traits<Function> &otherList, function_iterator first,
72 function_iterator last) {
73 // If we are transferring functions within the same module, the Module
74 // pointer doesn't need to be updated.
75 Module *curParent = getContainingModule();
76 if (curParent == otherList.getContainingModule())
77 return;
78
79 // Update the 'module' member of each function.
80 for (; first != last; ++first)
81 first->module = curParent;
82}
83
84/// Unlink this function from its Module and delete it.
85void Function::eraseFromModule() {
86 assert(getModule() && "Function has no parent");
87 getModule()->getFunctions().erase(this);
88}
89
Chris Lattner4c95a502018-06-23 16:03:42 -070090//===----------------------------------------------------------------------===//
91// ExtFunction implementation.
92//===----------------------------------------------------------------------===//
Chris Lattnerf7e22732018-06-22 22:03:48 -070093
Chris Lattner4c95a502018-06-23 16:03:42 -070094ExtFunction::ExtFunction(StringRef name, FunctionType *type)
95 : Function(name, type, Kind::ExtFunc) {
Chris Lattnere2259872018-06-21 15:22:42 -070096}
97
Chris Lattner4c95a502018-06-23 16:03:42 -070098//===----------------------------------------------------------------------===//
99// CFGFunction implementation.
100//===----------------------------------------------------------------------===//
101
102CFGFunction::CFGFunction(StringRef name, FunctionType *type)
103 : Function(name, type, Kind::CFGFunc) {
Chris Lattnere2259872018-06-21 15:22:42 -0700104}
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700105
Chris Lattnera8e47672018-07-25 14:08:16 -0700106CFGFunction::~CFGFunction() {
107 // Instructions may have cyclic references, which need to be dropped before we
108 // can start deleting them.
109 for (auto &bb : *this) {
110 for (auto &inst : bb)
111 inst.dropAllReferences();
Chris Lattner1e9fd7f2018-07-26 08:56:26 -0700112 bb.getTerminator()->dropAllReferences();
Chris Lattnera8e47672018-07-25 14:08:16 -0700113 }
114}
115
Tatiana Shpeismanc96b5872018-06-28 17:02:32 -0700116//===----------------------------------------------------------------------===//
117// MLFunction implementation.
118//===----------------------------------------------------------------------===//
119
120MLFunction::MLFunction(StringRef name, FunctionType *type)
Tatiana Shpeismanc2d88e92018-07-14 16:44:22 -0700121 : Function(name, type, Kind::MLFunc), StmtBlock(StmtBlockKind::MLFunc) {}
Chris Lattnera8e47672018-07-25 14:08:16 -0700122
123MLFunction::~MLFunction() {
Uday Bondhugula081d9e72018-07-27 10:58:14 -0700124 struct DropReferencesPass : public StmtWalker<DropReferencesPass> {
Tatiana Shpeisman60bf7be2018-07-26 18:09:20 -0700125 void visitOperationStmt(OperationStmt *stmt) { stmt->dropAllReferences(); }
126 };
127 DropReferencesPass pass;
Uday Bondhugula081d9e72018-07-27 10:58:14 -0700128 pass.walk(const_cast<MLFunction *>(this));
Chris Lattnera8e47672018-07-25 14:08:16 -0700129}