op_info_impl.h 3.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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 <algorithm>
#include <initializer_list>
19
#include <string>
20 21
#include <utility>

22 23 24
#include "paddle/ir/core/builtin_attribute.h"
#include "paddle/ir/core/op_base.h"
#include "paddle/ir/core/type.h"
25 26 27

namespace ir {
class Dialect;
28 29 30 31
typedef void (*VerifyPtr)(const std::vector<OpResult> &inputs,
                          const std::vector<Type> &outputs,
                          const AttributeMap &attributes);

32 33 34 35 36 37 38 39 40
///
/// \brief OpInfoImpl class.
///
class OpInfoImpl {
 public:
  ///
  /// \brief Construct and Deconstruct OpInfoImpl. The memory layout of
  /// OpInfoImpl is: std::pair<TypeId, void *>... | TypeId... | OpInfoImpl
  ///
41 42 43 44 45 46
  static OpInfoImpl *create(Dialect *dialect,
                            TypeId op_id,
                            const char *op_name,
                            std::vector<InterfaceValue> &&interface_map,
                            const std::vector<TypeId> &trait_set,
                            size_t attributes_num,
47 48
                            const char *attributes_name[],
                            VerifyPtr verify);
49

50
  void destroy();
51

52
  ir::IrContext *ir_context() const;
53 54

  /// \brief Search methods for Trait or Interface.
55
  bool HasTrait(TypeId trait_id) const;
56

57
  bool HasInterface(TypeId interface_id) const;
58 59 60

  ir::TypeId id() const { return op_id_; }

61 62
  void *interface_impl(TypeId interface_id) const;

63 64 65 66
  const char *name() const { return op_name_; }

  ir::Dialect *dialect() const { return dialect_; }

67 68 69 70 71 72
  uint32_t AttributeNum() const { return num_attributes_; }

  const char *GetAttributeByIndex(size_t idx) const {
    return idx < num_attributes_ ? p_attributes_[idx] : nullptr;
  }

73 74
  VerifyPtr verify() const { return verify_; }

75
 private:
76
  OpInfoImpl(ir::Dialect *dialect,
77 78
             TypeId op_id,
             const char *op_name,
79 80 81
             uint32_t num_interfaces,
             uint32_t num_traits,
             uint32_t num_attributes,
82 83
             const char **p_attributes,
             VerifyPtr verify)
84
      : dialect_(dialect),
85 86
        op_id_(op_id),
        op_name_(op_name),
87 88 89
        num_interfaces_(num_interfaces),
        num_traits_(num_traits),
        num_attributes_(num_attributes),
90 91
        p_attributes_(p_attributes),
        verify_(verify) {}
92

93 94 95 96 97 98 99 100
  /// The dialect of this Op belong to.
  ir::Dialect *dialect_;

  /// The TypeId of this Op.
  TypeId op_id_;

  /// The name of this Op.
  const char *op_name_;
101 102 103 104 105 106 107 108 109 110

  /// Interface will be recorded by std::pair<TypeId, void*>.
  uint32_t num_interfaces_ = 0;

  /// Trait will be recorded by TypeId.
  uint32_t num_traits_ = 0;

  /// The number of attributes for this Op.
  uint32_t num_attributes_ = 0;

111 112
  /// Attributes array address.
  const char **p_attributes_{nullptr};
113 114

  VerifyPtr verify_{nullptr};
115 116 117
};

}  // namespace ir