operation.h 3.7 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 "paddle/ir/core/block.h"
19 20 21
#include "paddle/ir/core/op_info.h"
#include "paddle/ir/core/operation_utils.h"
#include "paddle/ir/core/type.h"
22 23

namespace ir {
24
class OpBase;
25
class Program;
26 27
class OpOperand;
class OpResult;
28

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

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

49
  IrContext *ir_context() const;
50
  Dialect *dialect() const;
51
  OpResult GetResultByIndex(uint32_t index) const;
52

53
  OpOperand GetOperandByIndex(uint32_t index) const;
54

55
  void Print(std::ostream &os);
56

57 58 59 60 61
  const AttributeMap &attributes() const { return attributes_; }

  void SetAttribute(const std::string &key, Attribute value) {
    attributes_[key] = value;
  }
62

63
  ir::OpInfo info() const { return info_; }
64

65 66 67 68
  uint32_t num_results() const { return num_results_; }

  uint32_t num_operands() const { return num_operands_; }

69 70
  uint32_t num_regions() const { return num_regions_; }

71
  std::string name() const;
72

73
  template <typename T>
Z
zhangbo9674 已提交
74
  T dyn_cast() {
75 76 77 78 79
    return CastUtil<T>::call(this);
  }

  template <typename Trait>
  bool HasTrait() const {
80
    return info_.HasTrait<Trait>();
81 82 83 84
  }

  template <typename Interface>
  bool HasInterface() const {
85
    return info_.HasInterface<Interface>();
86
  }
87

88
  Block *GetParent() const { return parent_; }
89

90 91 92 93 94
  Region *GetParentRegion() const;

  Operation *GetParentOp() const;

  Program *GetParentProgram();
95

96 97 98
  /// Returns the region held by this operation at position 'index'.
  Region &GetRegion(unsigned index);

99 100
  operator Block::iterator() { return position_; }

101
 private:
102 103 104
  Operation(const AttributeMap &attribute,
            ir::OpInfo op_info,
            uint32_t num_results,
105
            uint32_t num_operands,
106
            uint32_t num_regions);
107 108 109

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

115
  // Allow access to 'SetParent'.
116
  friend class Block;
117
  void SetParent(Block *parent, const Block::iterator &position);
118

119
  template <typename T>
120 121 122
  struct CastUtil<
      T,
      typename std::enable_if<std::is_base_of<OpBase, T>::value>::type> {
Z
zhangbo9674 已提交
123
    static T call(Operation *op) { return T::dyn_cast(op); }
124
  };
125

126
  AttributeMap attributes_;
127

128
  OpInfo info_;
129

130 131 132
  const uint32_t num_results_ = 0;
  const uint32_t num_operands_ = 0;
  const uint32_t num_regions_ = 0;
133

134 135
  Region *regions_{nullptr};
  Block *parent_{nullptr};
136
  Block::iterator position_;
137 138 139
};

}  // namespace ir