Coverage Report

Created: 2020-06-26 05:44

/home/arjun/llvm-project/mlir/include/mlir/IR/Function.h
Line
Count
Source (jump to first uncovered line)
1
//===- Function.h - MLIR Function Class -------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM 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
16
#include "mlir/IR/Block.h"
17
#include "mlir/IR/FunctionSupport.h"
18
#include "mlir/IR/OpDefinition.h"
19
#include "mlir/IR/SymbolTable.h"
20
#include "mlir/Interfaces/CallInterfaces.h"
21
#include "llvm/Support/PointerLikeTypeTraits.h"
22
23
namespace mlir {
24
//===--------------------------------------------------------------------===//
25
// Function Operation.
26
//===--------------------------------------------------------------------===//
27
28
/// FuncOp represents a function, or an operation containing one region that
29
/// forms a CFG(Control Flow Graph). The region of a function is not allowed to
30
/// implicitly capture global values, and all external references must use
31
/// Function arguments or attributes that establish a symbolic connection(e.g.
32
/// symbols referenced by name via a string attribute).
33
class FuncOp
34
    : public Op<FuncOp, OpTrait::ZeroOperands, OpTrait::ZeroResult,
35
                OpTrait::OneRegion, OpTrait::IsIsolatedFromAbove,
36
                OpTrait::FunctionLike, OpTrait::AutomaticAllocationScope,
37
                OpTrait::AffineScope, CallableOpInterface::Trait,
38
                SymbolOpInterface::Trait> {
39
public:
40
  using Op::Op;
41
  using Op::print;
42
43
0
  static StringRef getOperationName() { return "func"; }
44
45
  static FuncOp create(Location location, StringRef name, FunctionType type,
46
                       ArrayRef<NamedAttribute> attrs = {});
47
  static FuncOp create(Location location, StringRef name, FunctionType type,
48
                       iterator_range<dialect_attr_iterator> attrs);
49
  static FuncOp create(Location location, StringRef name, FunctionType type,
50
                       ArrayRef<NamedAttribute> attrs,
51
                       ArrayRef<MutableDictionaryAttr> argAttrs);
52
53
  static void build(OpBuilder &builder, OperationState &result, StringRef name,
54
                    FunctionType type, ArrayRef<NamedAttribute> attrs);
55
  static void build(OpBuilder &builder, OperationState &result, StringRef name,
56
                    FunctionType type, ArrayRef<NamedAttribute> attrs,
57
                    ArrayRef<MutableDictionaryAttr> argAttrs);
58
59
  /// Operation hooks.
60
  static ParseResult parse(OpAsmParser &parser, OperationState &result);
61
  void print(OpAsmPrinter &p);
62
  LogicalResult verify();
63
64
  /// Erase a single argument at `argIndex`.
65
0
  void eraseArgument(unsigned argIndex) { eraseArguments({argIndex}); }
66
  /// Erases the arguments listed in `argIndices`.
67
  /// `argIndices` is allowed to have duplicates and can be in any order.
68
  void eraseArguments(ArrayRef<unsigned> argIndices);
69
70
  /// Create a deep copy of this function and all of its blocks, remapping
71
  /// any operands that use values outside of the function using the map that is
72
  /// provided (leaving them alone if no entry is present). If the mapper
73
  /// contains entries for function arguments, these arguments are not included
74
  /// in the new function. Replaces references to cloned sub-values with the
75
  /// corresponding value that is copied, and adds those mappings to the mapper.
76
  FuncOp clone(BlockAndValueMapping &mapper);
77
  FuncOp clone();
78
79
  /// Clone the internal blocks and attributes from this function into dest. Any
80
  /// cloned blocks are appended to the back of dest. This function asserts that
81
  /// the attributes of the current function and dest are compatible.
82
  void cloneInto(FuncOp dest, BlockAndValueMapping &mapper);
83
84
  //===--------------------------------------------------------------------===//
85
  // CallableOpInterface
86
  //===--------------------------------------------------------------------===//
87
88
  /// Returns the region on the current operation that is callable. This may
89
  /// return null in the case of an external callable object, e.g. an external
90
  /// function.
91
0
  Region *getCallableRegion() { return isExternal() ? nullptr : &getBody(); }
92
93
  /// Returns the results types that the callable region produces when executed.
94
0
  ArrayRef<Type> getCallableResults() { return getType().getResults(); }
95
96
private:
97
  // This trait needs access to the hooks defined below.
98
  friend class OpTrait::FunctionLike<FuncOp>;
99
100
  /// Returns the number of arguments. This is a hook for OpTrait::FunctionLike.
101
0
  unsigned getNumFuncArguments() { return getType().getInputs().size(); }
102
103
  /// Returns the number of results. This is a hook for OpTrait::FunctionLike.
104
0
  unsigned getNumFuncResults() { return getType().getResults().size(); }
105
106
  /// Hook for OpTrait::FunctionLike, called after verifying that the 'type'
107
  /// attribute is present and checks if it holds a function type.  Ensures
108
  /// getType, getNumFuncArguments, and getNumFuncResults can be called safely.
109
0
  LogicalResult verifyType() {
110
0
    auto type = getTypeAttr().getValue();
111
0
    if (!type.isa<FunctionType>())
112
0
      return emitOpError("requires '" + getTypeAttrName() +
113
0
                         "' attribute of function type");
114
0
    return success();
115
0
  }
116
};
117
} // end namespace mlir
118
119
namespace llvm {
120
121
// Functions hash just like pointers.
122
template <> struct DenseMapInfo<mlir::FuncOp> {
123
0
  static mlir::FuncOp getEmptyKey() {
124
0
    auto pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
125
0
    return mlir::FuncOp::getFromOpaquePointer(pointer);
126
0
  }
127
0
  static mlir::FuncOp getTombstoneKey() {
128
0
    auto pointer = llvm::DenseMapInfo<void *>::getTombstoneKey();
129
0
    return mlir::FuncOp::getFromOpaquePointer(pointer);
130
0
  }
131
0
  static unsigned getHashValue(mlir::FuncOp val) {
132
0
    return hash_value(val.getAsOpaquePointer());
133
0
  }
134
0
  static bool isEqual(mlir::FuncOp LHS, mlir::FuncOp RHS) { return LHS == RHS; }
135
};
136
137
/// Allow stealing the low bits of FuncOp.
138
template <> struct PointerLikeTypeTraits<mlir::FuncOp> {
139
public:
140
0
  static inline void *getAsVoidPointer(mlir::FuncOp I) {
141
0
    return const_cast<void *>(I.getAsOpaquePointer());
142
0
  }
143
0
  static inline mlir::FuncOp getFromVoidPointer(void *P) {
144
0
    return mlir::FuncOp::getFromOpaquePointer(P);
145
0
  }
146
  static constexpr int NumLowBitsAvailable = 3;
147
};
148
149
} // namespace llvm
150
151
#endif // MLIR_IR_FUNCTION_H