operation.h 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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

#include "paddle/ir/builtin_attribute.h"
18
#include "paddle/ir/op_info.h"
19
#include "paddle/ir/operation_utils.h"
20 21 22 23
#include "paddle/ir/type.h"
#include "paddle/ir/value_impl.h"

namespace ir {
24 25 26 27
template <class ConcreteTrait>
class OpTraitBase;
template <typename ConcreteInterface>
class OpInterfaceBase;
28 29
class Program;

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

44 45 46
  ///
  /// \brief Destroy the operation objects and free memeory by create().
  ///
47 48
  void destroy();

49 50
  IrContext *ir_context() const;

51 52
  ir::OpResult GetResultByIndex(uint32_t index);

53 54
  ir::OpOperand GetOperandByIndex(uint32_t index);

55 56
  std::string print();

57
  const AttributeMap &attribute() const { return attribute_; }
58

59
  ir::OpInfo op_info() const { return op_info_; }
60

61 62 63 64
  uint32_t num_results() const { return num_results_; }

  uint32_t num_operands() const { return num_operands_; }

65 66
  std::string op_name() const;

67 68 69 70 71 72 73 74 75 76 77 78 79 80
  template <typename T>
  T dyn_cast() const {
    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>();
  }
81

82 83 84 85 86 87
  Program *parent_program() const { return parent_program_; }

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

88 89 90
 private:
  Operation(uint32_t num_results,
            uint32_t num_operands,
91
            const AttributeMap &attribute,
92 93 94 95 96 97 98 99 100 101 102 103
            ir::OpInfo op_info);

  template <typename T, typename Enabler = void>
  struct CastUtil {
    static T call(const Operation *op) {
      throw("Can't dyn_cast to T, T should be a Trait or Interface");
    }
  };
  template <typename T>
  struct CastUtil<T,
                  typename std::enable_if<
                      std::is_base_of<OpTraitBase<T>, T>::value>::type> {
104 105 106
    static T call(const Operation *op) {
      return T(op->HasTrait<T>() ? op : nullptr);
    }
107 108 109 110 111 112
  };
  template <typename T>
  struct CastUtil<T,
                  typename std::enable_if<
                      std::is_base_of<OpInterfaceBase<T>, T>::value>::type> {
    static T call(const Operation *op) {
113 114
      typename T::Concept *interface_impl = op->op_info().GetInterfaceImpl<T>();
      return interface_impl ? T(op, interface_impl) : T(nullptr, nullptr);
115 116
    }
  };
117

118
  AttributeMap attribute_;
119

120 121
  ir::OpInfo op_info_;

122 123 124
  uint32_t num_results_ = 0;

  uint32_t num_operands_ = 0;
125 126

  ir::Program *parent_program_{nullptr};
127 128 129
};

}  // namespace ir