operation.h 3.1 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

Z
zhangbo9674 已提交
17
#include <iostream>
18 19 20 21 22
#include <vector>
#include "paddle/ir/core/op_info.h"
#include "paddle/ir/core/operation_utils.h"
#include "paddle/ir/core/type.h"
#include "paddle/ir/core/value_impl.h"
23 24

namespace ir {
25
class OpBase;
26 27
class Program;

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

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

47 48
  IrContext *ir_context() const;

49
  ir::OpResult GetResultByIndex(uint32_t index) const;
50

51
  ir::OpOperand GetOperandByIndex(uint32_t index) const;
52

53 54
  std::string print();

55
  const AttributeMap &attribute() const { return attribute_; }
56

57
  ir::OpInfo op_info() const { return op_info_; }
58

59 60 61 62
  uint32_t num_results() const { return num_results_; }

  uint32_t num_operands() const { return num_operands_; }

63 64
  std::string op_name() const;

65
  template <typename T>
Z
zhangbo9674 已提交
66
  T dyn_cast() {
67 68 69 70 71 72 73 74 75 76 77 78
    return CastUtil<T>::call(this);
  }

  template <typename Trait>
  bool HasTrait() const {
    return op_info_.HasTrait<Trait>();
  }

  template <typename Interface>
  bool HasInterface() const {
    return op_info_.HasInterface<Interface>();
  }
79

80 81 82 83 84 85
  Program *parent_program() const { return parent_program_; }

  void set_parent_program(Program *parent_program) {
    parent_program_ = parent_program;
  }

86 87 88
 private:
  Operation(uint32_t num_results,
            uint32_t num_operands,
89
            const AttributeMap &attribute,
90 91 92 93
            ir::OpInfo op_info);

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

99
  template <typename T>
100 101 102
  struct CastUtil<
      T,
      typename std::enable_if<std::is_base_of<OpBase, T>::value>::type> {
Z
zhangbo9674 已提交
103
    static T call(Operation *op) { return T::dyn_cast(op); }
104
  };
105

106
  AttributeMap attribute_;
107

108
  OpInfo op_info_;
109

110 111 112
  uint32_t num_results_ = 0;

  uint32_t num_operands_ = 0;
113

114
  Program *parent_program_{nullptr};
115 116 117
};

}  // namespace ir