operation.h 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// 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.

#pragma once

17
#include <ostream>
18
#include <vector>
19
#include "paddle/ir/core/block.h"
20
#include "paddle/ir/core/enforce.h"
21
#include "paddle/ir/core/macros.h"
22 23 24
#include "paddle/ir/core/op_info.h"
#include "paddle/ir/core/operation_utils.h"
#include "paddle/ir/core/type.h"
25 26

namespace ir {
27
class OpBase;
28
class Program;
29 30
class OpOperand;
class OpResult;
31

32
class IR_API alignas(8) Operation final {
33 34 35 36
 public:
  ///
  /// \brief Malloc memory and construct objects in the following order:
  /// OpResultImpls|Operation|OpOperandImpls.
37 38
  /// NOTE: Similar to new and delete, the destroy() and the create() need to be
  /// used in conjunction.
39
  ///
40
  static Operation *Create(const std::vector<ir::OpResult> &inputs,
41
                           const AttributeMap &attributes,
42 43 44
                           const std::vector<ir::Type> &output_types,
                           ir::OpInfo op_info,
                           size_t num_regions = 0);
45
  static Operation *Create(OperationArgument &&op_argument);
46

47
  ///
C
co63oc 已提交
48
  /// \brief Destroy the operation objects and free memory by create().
49
  ///
50
  void Destroy();
51

52
  IrContext *ir_context() const;
53

54
  Dialect *dialect() const;
55

56 57
  OpResult result(uint32_t index) const;

58
  OpOperand operand(uint32_t index) const;
59

60
  Value operand_source(uint32_t index) const;
61

62 63
  /// Returns the region held by this operation at position 'index'.
  Region &region(unsigned index);
64
  const Region &region(unsigned index) const;
65

66
  void Print(std::ostream &os) const;
67

68 69
  const AttributeMap &attributes() const { return attributes_; }

70 71
  template <typename T>
  T attribute(const std::string &name) {
72 73 74
    Attribute attr = attribute(name);
    IR_ENFORCE(attr.isa<T>(), "Attribute (%s) type is not right.", name);
    return attr.dyn_cast<T>();
75 76
  }

77
  void set_attribute(const std::string &key, Attribute value) {
78 79
    attributes_[key] = value;
  }
80

81 82 83 84 85 86
  Attribute attribute(const std::string &key) const;

  bool HasAttribute(const std::string &key) const {
    return attributes_.find(key) != attributes_.end();
  }

87
  ir::OpInfo info() const { return info_; }
88

89 90 91 92
  uint32_t num_results() const { return num_results_; }

  uint32_t num_operands() const { return num_operands_; }

93 94
  uint32_t num_regions() const { return num_regions_; }

95
  std::string name() const;
96

97
  template <typename T>
Z
zhangbo9674 已提交
98
  T dyn_cast() {
99 100 101 102 103
    return CastUtil<T>::call(this);
  }

  template <typename Trait>
  bool HasTrait() const {
104
    return info_.HasTrait<Trait>();
105 106 107 108
  }

  template <typename Interface>
  bool HasInterface() const {
109
    return info_.HasInterface<Interface>();
110
  }
111

112
  const Block *GetParent() const { return parent_; }
113

114 115 116 117 118 119
  Block *GetParent() {
    return const_cast<Block *>(
        const_cast<const Operation *>(this)->GetParent());
  }

  Region *GetParentRegion();
120 121 122

  Operation *GetParentOp() const;

123 124 125 126 127 128
  const Program *GetParentProgram() const;

  Program *GetParentProgram() {
    return const_cast<Program *>(
        const_cast<const Operation *>(this)->GetParentProgram());
  }
129

130 131
  operator Block::iterator() { return position_; }

132 133
  operator Block::const_iterator() const { return position_; }

134 135 136
  /// Replace all uses of results of this operation with the provided 'values'.
  void ReplaceAllUsesWith(const std::vector<Value> &values);

137 138
  void ReplaceAllUsesWith(const std::vector<OpResult> &op_results);

139 140 141 142
  inline void ReplaceAllUsesWith(Value value) {
    ReplaceAllUsesWith(std::vector<Value>{value});
  }

143 144
  void Verify();

145 146 147 148
  std::vector<OpOperand> operands() const;

  std::vector<OpResult> results() const;

149
 private:
150
  DISABLE_COPY_AND_ASSIGN(Operation);
151 152 153
  Operation(const AttributeMap &attribute,
            ir::OpInfo op_info,
            uint32_t num_results,
154
            uint32_t num_operands,
155
            uint32_t num_regions);
156 157 158

  template <typename T, typename Enabler = void>
  struct CastUtil {
Z
zhangbo9674 已提交
159
    static T call(Operation *op) {
160
      throw("Can't dyn_cast to T, T should be a Op or Trait or Interface");
161
    }
162
  };
163

164
  // Allow access to 'SetParent'.
165
  friend class Block;
166
  void SetParent(Block *parent, const Block::iterator &position);
167

168
  template <typename T>
169 170 171
  struct CastUtil<
      T,
      typename std::enable_if<std::is_base_of<OpBase, T>::value>::type> {
Z
zhangbo9674 已提交
172
    static T call(Operation *op) { return T::dyn_cast(op); }
173
  };
174

175
  AttributeMap attributes_;
176

177
  OpInfo info_;
178

179 180 181
  const uint32_t num_results_ = 0;
  const uint32_t num_operands_ = 0;
  const uint32_t num_regions_ = 0;
182

183 184
  Region *regions_{nullptr};
  Block *parent_{nullptr};
185
  Block::iterator position_;
186 187 188
};

}  // namespace ir