My Project
Function.h
Go to the documentation of this file.
1 //===- Function.h - MLIR Function Class -------------------------*- C++ -*-===//
2 //
3 // Part of the MLIR Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Functions are the basic unit of composition in MLIR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_IR_FUNCTION_H
14 #define MLIR_IR_FUNCTION_H
15 
17 #include "mlir/IR/Block.h"
19 #include "mlir/IR/OpDefinition.h"
20 #include "mlir/IR/SymbolTable.h"
21 
22 namespace mlir {
23 //===--------------------------------------------------------------------===//
24 // Function Operation.
25 //===--------------------------------------------------------------------===//
26 
32 class FuncOp : public Op<FuncOp, OpTrait::ZeroOperands, OpTrait::ZeroResult,
33  OpTrait::IsIsolatedFromAbove, OpTrait::Symbol,
34  OpTrait::FunctionLike, CallableOpInterface::Trait> {
35 public:
36  using Op::Op;
37  using Op::print;
38 
39  static StringRef getOperationName() { return "func"; }
40 
41  static FuncOp create(Location location, StringRef name, FunctionType type,
42  ArrayRef<NamedAttribute> attrs = {});
43  static FuncOp create(Location location, StringRef name, FunctionType type,
45  static FuncOp create(Location location, StringRef name, FunctionType type,
48 
49  static void build(Builder *builder, OperationState &result, StringRef name,
51  static void build(Builder *builder, OperationState &result, StringRef name,
54 
56  static ParseResult parse(OpAsmParser &parser, OperationState &result);
57  void print(OpAsmPrinter &p);
59 
61  void eraseArgument(unsigned argIndex) { eraseArguments({argIndex}); }
64  void eraseArguments(ArrayRef<unsigned> argIndices);
65 
68  return getAttrOfType<TypeAttr>(getTypeAttrName())
69  .getValue()
70  .cast<FunctionType>();
71  }
72 
80  void setType(FunctionType newType) {
81  SmallVector<char, 16> nameBuf;
82  auto oldType = getType();
83  for (int i = newType.getNumInputs(), e = oldType.getNumInputs(); i < e;
84  i++) {
85  removeAttr(getArgAttrName(i, nameBuf));
86  }
87  for (int i = newType.getNumResults(), e = oldType.getNumResults(); i < e;
88  i++) {
89  removeAttr(getResultAttrName(i, nameBuf));
90  }
92  }
93 
101  FuncOp clone();
102 
106  void cloneInto(FuncOp dest, BlockAndValueMapping &mapper);
107 
108  //===--------------------------------------------------------------------===//
109  // Body Handling
110  //===--------------------------------------------------------------------===//
111 
115  Block *addEntryBlock();
116 
119  Block *addBlock();
120 
121  //===--------------------------------------------------------------------===//
122  // CallableOpInterface
123  //===--------------------------------------------------------------------===//
124 
129  assert(callable.get<SymbolRefAttr>().getLeafReference() == getName());
130  return isExternal() ? nullptr : &getBody();
131  }
132 
135  if (!isExternal())
136  callables.push_back(&getBody());
137  }
138 
142  assert(!isExternal() && region == &getBody() && "invalid callable");
143  return getType().getResults();
144  }
145 
146 private:
147  // This trait needs access to the hooks defined below.
149 
151  unsigned getNumFuncArguments() { return getType().getInputs().size(); }
152 
154  unsigned getNumFuncResults() { return getType().getResults().size(); }
155 
159  LogicalResult verifyType() {
160  auto type = getTypeAttr().getValue();
161  if (!type.isa<FunctionType>())
162  return emitOpError("requires '" + getTypeAttrName() +
163  "' attribute of function type");
164  return success();
165  }
166 };
167 } // end namespace mlir
168 
169 namespace llvm {
170 
171 // Functions hash just like pointers.
172 template <> struct DenseMapInfo<mlir::FuncOp> {
175  return mlir::FuncOp::getFromOpaquePointer(pointer);
176  }
179  return mlir::FuncOp::getFromOpaquePointer(pointer);
180  }
181  static unsigned getHashValue(mlir::FuncOp val) {
182  return hash_value(val.getAsOpaquePointer());
183  }
184  static bool isEqual(mlir::FuncOp LHS, mlir::FuncOp RHS) { return LHS == RHS; }
185 };
186 
188 template <> struct PointerLikeTypeTraits<mlir::FuncOp> {
189 public:
190  static inline void *getAsVoidPointer(mlir::FuncOp I) {
191  return const_cast<void *>(I.getAsOpaquePointer());
192  }
193  static inline mlir::FuncOp getFromVoidPointer(void *P) {
195  }
196  enum { NumLowBitsAvailable = 3 };
197 };
198 
199 } // namespace llvm
200 
201 #endif // MLIR_IR_FUNCTION_H
Definition: Attributes.h:456
Definition: InferTypeOpInterface.cpp:20
StringRef getResultAttrName(unsigned arg, SmallVectorImpl< char > &out)
Return the name of the attribute used for function results.
Definition: FunctionSupport.h:34
Definition: Region.h:23
static void * getAsVoidPointer(mlir::FuncOp I)
Definition: Function.h:190
void cloneInto(FuncOp dest, BlockAndValueMapping &mapper)
Definition: Function.cpp:162
Definition: PassRegistry.cpp:413
Block represents an ordered list of Operations.
Definition: Block.h:21
ArrayRef< Type > getResults() const
Definition: Types.cpp:47
void getCallableRegions(SmallVectorImpl< Region *> &callables)
Returns all of the callable regions of this operation.
Definition: Function.h:134
Definition: LLVM.h:45
void print(raw_ostream &os, OpPrintingFlags flags=llvm::None)
Print the operation to the given stream.
Definition: OpDefinition.h:122
static mlir::FuncOp getTombstoneKey()
Definition: Function.h:177
Region * getCallableRegion(CallInterfaceCallable callable)
Definition: Function.h:128
Function types map from a list of inputs to a list of results.
Definition: Types.h:190
static void build(Builder *builder, OperationState &result, StringRef name, FunctionType type, ArrayRef< NamedAttribute > attrs)
Definition: Function.cpp:49
Definition: OpImplementation.h:214
Definition: LLVM.h:34
Definition: Location.h:52
FuncOp clone()
Definition: Function.cpp:210
static TypeAttr get(Type value)
Definition: Attributes.cpp:349
LogicalResult success(bool isSuccess=true)
Definition: LogicalResult.h:25
Definition: LogicalResult.h:18
void setAttr(Identifier name, Attribute value)
Definition: OpDefinition.h:157
Definition: LLVM.h:37
Definition: Function.h:32
static mlir::FuncOp getFromVoidPointer(void *P)
Definition: Function.h:193
static bool isEqual(mlir::FuncOp LHS, mlir::FuncOp RHS)
Definition: Function.h:184
Definition: CallInterfaces.h:24
unsigned getNumResults() const
Definition: Types.cpp:45
static mlir::FuncOp getEmptyKey()
Definition: Function.h:173
static FuncOp create(Location location, StringRef name, FunctionType type, ArrayRef< NamedAttribute > attrs={})
Definition: Function.cpp:29
Definition: OpImplementation.h:32
NamedAttributeList::RemoveResult removeAttr(Identifier name)
Definition: OpDefinition.h:177
Definition: OperationSupport.h:261
static unsigned getHashValue(mlir::FuncOp val)
Definition: Function.h:181
ArrayRef< Type > getCallableResults(Region *region)
Definition: Function.h:141
unsigned getNumInputs() const
Definition: Types.h:199
Block * addBlock()
Definition: Function.cpp:154
static ParseResult parse(OpAsmParser &parser, OperationState &result)
Operation hooks.
Definition: Function.cpp:71
void setType(FunctionType newType)
Definition: Function.h:80
Op()
This is a public constructor. Any op can be initialized to null.
Definition: OpDefinition.h:1029
StringRef getLeafReference() const
Definition: Attributes.cpp:254
void eraseArguments(ArrayRef< unsigned > argIndices)
Definition: Function.cpp:108
Definition: BlockAndValueMapping.h:26
inline ::llvm::hash_code hash_value(AffineExpr arg)
Make AffineExpr hashable.
Definition: AffineExpr.h:201
Definition: LLVM.h:35
StringRef getTypeAttrName()
Return the name of the attribute used for function types.
Definition: FunctionSupport.h:25
static StringRef getOperationName()
Definition: Function.h:39
Definition: Builders.h:47
Definition: LLVM.h:50
FunctionType getType()
Returns the type of this function.
Definition: Function.h:67
StringRef getArgAttrName(unsigned arg, SmallVectorImpl< char > &out)
Return the name of the attribute used for function arguments.
Definition: FunctionSupport.h:28
InFlightDiagnostic emitOpError(const Twine &message={})
Definition: Operation.cpp:727
Definition: OpDefinition.h:949
LogicalResult verify()
Definition: Function.cpp:88
ArrayRef< Type > getInputs() const
Definition: Types.cpp:41
void print(OpAsmPrinter &p)
Definition: Function.cpp:82
Definition: OpDefinition.h:36
Definition: FunctionSupport.h:100
Block * addEntryBlock()
Definition: Function.cpp:144
void eraseArgument(unsigned argIndex)
Erase a single argument at argIndex.
Definition: Function.h:61
const void * getAsOpaquePointer() const
Methods for supporting PointerLikeTypeTraits.
Definition: OpDefinition.h:1037