blob: 7e70c37d294bea8c98e79441b4ae7dbe6dbca7ae [file] [log] [blame]
Chris Lattnere2259872018-06-21 15:22:42 -07001//===- Function.h - MLIR Function Class -------------------------*- C++ -*-===//
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -07002//
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 Lattnere2259872018-06-21 15:22:42 -070018// 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 Lattnerc0c5e0f2018-06-21 09:49:33 -070021//
22//===----------------------------------------------------------------------===//
23
24#ifndef MLIR_IR_FUNCTION_H
25#define MLIR_IR_FUNCTION_H
26
Chris Lattnere2259872018-06-21 15:22:42 -070027#include "mlir/Support/LLVM.h"
Chris Lattnera8e47672018-07-25 14:08:16 -070028#include "llvm/ADT/ilist.h"
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070029
Chris Lattnere2259872018-06-21 15:22:42 -070030namespace mlir {
Chris Lattnerff0d5902018-07-05 09:12:11 -070031class FunctionType;
32class MLIRContext;
Chris Lattnera8e47672018-07-25 14:08:16 -070033class Module;
Chris Lattnerf7e22732018-06-22 22:03:48 -070034
Chris Lattner4c95a502018-06-23 16:03:42 -070035/// This is the base class for all of the MLIR function types.
Chris Lattnera8e47672018-07-25 14:08:16 -070036class Function : public llvm::ilist_node_with_parent<Function, Module> {
Chris Lattner4c95a502018-06-23 16:03:42 -070037public:
Chris Lattnerff0d5902018-07-05 09:12:11 -070038 enum class Kind { ExtFunc, CFGFunc, MLFunc };
Chris Lattner4c95a502018-06-23 16:03:42 -070039
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 Lattnerff0d5902018-07-05 09:12:11 -070046 FunctionType *getType() const { return type; }
47
48 MLIRContext *getContext() const;
Chris Lattnera8e47672018-07-25 14:08:16 -070049 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 Lattner4c95a502018-06-23 16:03:42 -070057
Chris Lattner21e67f62018-07-06 10:46:19 -070058 /// Perform (potentially expensive) checks of invariants, used to detect
Chris Lattner40746442018-07-21 14:32:09 -070059 /// 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 Lattner21e67f62018-07-06 10:46:19 -070062
Chris Lattner4c95a502018-06-23 16:03:42 -070063 void print(raw_ostream &os) const;
64 void dump() const;
65
66protected:
67 Function(StringRef name, FunctionType *type, Kind kind);
68 ~Function() {}
Chris Lattnerff0d5902018-07-05 09:12:11 -070069
Chris Lattner4c95a502018-06-23 16:03:42 -070070private:
71 Kind kind;
Chris Lattnera8e47672018-07-25 14:08:16 -070072 Module *module = nullptr;
Chris Lattner4c95a502018-06-23 16:03:42 -070073 std::string name;
74 FunctionType *const type;
75
Chris Lattnerff0d5902018-07-05 09:12:11 -070076 void operator=(const Function &) = delete;
Chris Lattnera8e47672018-07-25 14:08:16 -070077 friend struct llvm::ilist_traits<Function>;
Chris Lattner4c95a502018-06-23 16:03:42 -070078};
79
80/// An extfunc declaration is a declaration of a function signature that is
81/// defined in some other module.
82class ExtFunction : public Function {
83public:
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 Lattner4c95a502018-06-23 16:03:42 -070090};
91
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -070092} // end namespace mlir
93
Chris Lattnera8e47672018-07-25 14:08:16 -070094//===----------------------------------------------------------------------===//
95// ilist_traits for Function
96//===----------------------------------------------------------------------===//
97
98namespace llvm {
99
100template <>
101struct 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
113private:
114 mlir::Module *getContainingModule();
115};
116} // end namespace llvm
117
Chris Lattnerc0c5e0f2018-06-21 09:49:33 -0700118#endif // MLIR_IR_FUNCTION_H