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 47 48 49
  static OpInfo 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,
                       const char *attributes_name[],
                       VerifyPtr verify);
  static void Destroy(OpInfo info);
50

51
  TypeId id() const { return op_id_; }
52

53 54 55 56 57
  Dialect *dialect() const { return dialect_; }

  VerifyPtr verify() const { return verify_; }

  IrContext *ir_context() const;
58 59

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

62
  bool HasInterface(TypeId interface_id) const;
63

64
  void *GetInterfaceImpl(TypeId interface_id) const;
65

66 67
  const char *name() const { return op_name_; }

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

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

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

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

  /// 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