dialect.h 5.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

17
#include <functional>
18 19
#include <ostream>

20
#include "paddle/ir/core/attribute.h"
21 22
#include "paddle/ir/core/attribute_base.h"
#include "paddle/ir/core/dialect_interface.h"
23
#include "paddle/ir/core/enforce.h"
24 25 26
#include "paddle/ir/core/ir_context.h"
#include "paddle/ir/core/op_base.h"
#include "paddle/ir/core/type_base.h"
27 28

namespace ir {
29 30 31 32

class Operation;
class IrPrinter;

33
class DialectInterface;
34 35
///
/// \brief Dialect can basically be understood as a namespace. In Dialect, we
Z
zhangbo9674 已提交
36 37 38 39
/// can define a series of types, attributes, operations, etc. An instance of
/// the dialect object will be loaded into the global IrContext. Specific
/// compilers only need to combine existing dialects and add their own
/// extensions or customizations.
40
///
41
class IR_API Dialect {
42
 public:
43
  Dialect(std::string name, IrContext *context, TypeId id);
44

45 46
  virtual ~Dialect();

47 48
  const std::string &name() const { return name_; }

49
  IrContext *ir_context() const { return context_; }
50

51
  TypeId id() const { return id_; }
52 53 54 55 56 57 58 59 60 61 62 63

  ///
  /// \brief Register all types contained in the template parameter Args.
  /// To register only one Type, you can use the RegisterType template function.
  ///
  template <typename... Args>
  void RegisterTypes() {
    (void)std::initializer_list<int>{0, (RegisterType<Args>(), 0)...};
  }

  template <typename T>
  void RegisterType() {
64 65 66
    ir_context()->RegisterAbstractType(TypeId::get<T>(),
                                       AbstractType::get<T>(*this));
    TypeManager::RegisterType<T>(ir_context());
67 68
  }

Z
zhangbo9674 已提交
69 70 71 72 73 74 75 76 77 78 79 80
  ///
  /// \brief Register all attributes contained in the template parameter Args.
  /// To register only one Attribute, you can use the RegisterAttribute template
  /// function.
  ///
  template <typename... Args>
  void RegisterAttributes() {
    (void)std::initializer_list<int>{0, (RegisterAttribute<Args>(), 0)...};
  }

  template <typename T>
  void RegisterAttribute() {
81 82 83
    ir_context()->RegisterAbstractAttribute(TypeId::get<T>(),
                                            AbstractAttribute::get<T>(*this));
    AttributeManager::RegisterAttribute<T>(ir_context());
Z
zhangbo9674 已提交
84 85 86
  }

  ///
87
  /// \brief Register Ops.
Z
zhangbo9674 已提交
88
  ///
89 90 91 92 93
  template <typename... Args>
  void RegisterOps() {
    (void)std::initializer_list<int>{0, (RegisterOp<Args>(), 0)...};
  }

94
  template <typename ConcreteOp>
95
  void RegisterOp() {
96 97 98 99 100 101
    ir_context()->RegisterOpInfo(this,
                                 TypeId::get<ConcreteOp>(),
                                 ConcreteOp::name(),
                                 ConcreteOp::GetInterfaceMap(),
                                 ConcreteOp::GetTraitSet(),
                                 ConcreteOp::attributes_num,
102
                                 ConcreteOp::attributes_name,
103
                                 ConcreteOp::VerifyInvariants);
104 105 106
  }

  void RegisterOp(const std::string &name, OpInfoImpl *op_info);
Z
zhangbo9674 已提交
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  ///
  /// \brief Register interface methods.
  ///

  DialectInterface *GetRegisteredInterface(TypeId id) {
    auto it = registered_interfaces_.find(id);
    return it != registered_interfaces_.end() ? it->second.get() : nullptr;
  }

  template <typename InterfaceT>
  InterfaceT *GetRegisteredInterface() {
    return static_cast<InterfaceT *>(
        GetRegisteredInterface(TypeId::get<InterfaceT>()));
  }

  /// Register a dialect interface with this dialect instance.
  void RegisterInterface(std::unique_ptr<DialectInterface> interface);

  /// Register a set of dialect interfaces with this dialect instance.
  template <typename... Args>
  void RegisterInterfaces() {
    (void)std::initializer_list<int>{
        0, (RegisterInterface(std::make_unique<Args>(this)), 0)...};
  }

  template <typename InterfaceT, typename... Args>
  InterfaceT &RegisterInterface(Args &&...args) {
    InterfaceT *interface = new InterfaceT(this, std::forward<Args>(args)...);
    RegisterInterface(std::unique_ptr<DialectInterface>(interface));
    return *interface;
  }

140 141 142 143
  virtual void PrintType(Type type, std::ostream &os) const {
    IR_THROW("dialect has no registered type printing hook");
  }

144
  virtual void PrintAttribute(Attribute attr, std::ostream &os) const {
145
    IR_THROW("dialect has no registered attribute printing hook");
146 147
  }

148 149 150
  virtual void PrintOperation(Operation *op,
                              IrPrinter &printer) const;  // NOLINT

151
 private:
152 153 154 155
  Dialect(const Dialect &) = delete;

  Dialect &operator=(Dialect &) = delete;

156 157
  std::string name_;

158
  IrContext *context_;  // not owned
159

160
  TypeId id_;
161 162 163

  std::unordered_map<TypeId, std::unique_ptr<DialectInterface>>
      registered_interfaces_;
164 165
};
}  // namespace ir