MLIR 22.0.0git
DialectInterface.h
Go to the documentation of this file.
1//===- DialectInterface.h - IR Dialect Interfaces ---------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_IR_DIALECTINTERFACE_H
10#define MLIR_IR_DIALECTINTERFACE_H
11
12#include "mlir/Support/TypeID.h"
13#include "llvm/ADT/DenseSet.h"
14#include "llvm/ADT/STLExtras.h"
15#include <vector>
16
17namespace mlir {
18class Dialect;
19class MLIRContext;
20class Operation;
21
22//===----------------------------------------------------------------------===//
23// DialectInterface
24//===----------------------------------------------------------------------===//
25namespace detail {
26/// The base class used for all derived interface types. This class provides
27/// utilities necessary for registration.
28template <typename ConcreteType, typename BaseT>
29class DialectInterfaceBase : public BaseT {
30public:
32
33 /// Get a unique id for the derived interface type.
35
36protected:
37 DialectInterfaceBase(Dialect *dialect) : BaseT(dialect, getInterfaceID()) {}
38};
39} // namespace detail
40
41/// This class represents an interface overridden for a single dialect.
43public:
45
46 /// The base class used for all derived interface types. This class provides
47 /// utilities necessary for registration.
48 template <typename ConcreteType>
50
51 /// Return the dialect that this interface represents.
52 Dialect *getDialect() const { return dialect; }
53
54 /// Return the context that holds the parent dialect of this interface.
55 MLIRContext *getContext() const;
56
57 /// Return the derived interface id.
58 TypeID getID() const { return interfaceID; }
59
60protected:
62 : dialect(dialect), interfaceID(id) {}
63
64private:
65 /// The dialect that represents this interface.
66 Dialect *dialect;
67
68 /// The unique identifier for the derived interface type.
69 TypeID interfaceID;
70};
71
72//===----------------------------------------------------------------------===//
73// DialectInterfaceCollection
74//===----------------------------------------------------------------------===//
75
76namespace detail {
77/// This class is the base class for a collection of instances for a specific
78/// interface kind.
80 /// DenseMap info for dialect interfaces that allows lookup by the dialect.
81 struct InterfaceKeyInfo : public DenseMapInfo<const DialectInterface *> {
82 using DenseMapInfo<const DialectInterface *>::isEqual;
83
84 static unsigned getHashValue(Dialect *key) { return llvm::hash_value(key); }
85 static unsigned getHashValue(const DialectInterface *key) {
86 return getHashValue(key->getDialect());
87 }
88
89 static bool isEqual(Dialect *lhs, const DialectInterface *rhs) {
90 if (rhs == getEmptyKey() || rhs == getTombstoneKey())
91 return false;
92 return lhs == rhs->getDialect();
93 }
94 };
95
96 /// A set of registered dialect interface instances.
98 using InterfaceVectorT = std::vector<const DialectInterface *>;
99
100public:
102 StringRef interfaceName);
104
105protected:
106 /// Get the interface for the dialect of given operation, or null if one
107 /// is not registered.
109
110 /// Get the interface for the given dialect.
111 const DialectInterface *getInterfaceFor(Dialect *dialect) const {
112 auto it = interfaces.find_as(dialect);
113 return it == interfaces.end() ? nullptr : *it;
114 }
115
116 /// An iterator class that iterates the held interface objects of the given
117 /// derived interface type.
118 template <typename InterfaceT>
119 struct iterator
120 : public llvm::mapped_iterator_base<iterator<InterfaceT>,
121 InterfaceVectorT::const_iterator,
122 const InterfaceT &> {
123 using llvm::mapped_iterator_base<iterator<InterfaceT>,
124 InterfaceVectorT::const_iterator,
125 const InterfaceT &>::mapped_iterator_base;
126
127 /// Map the element to the iterator result type.
128 const InterfaceT &mapElement(const DialectInterface *interface) const {
129 return *static_cast<const InterfaceT *>(interface);
130 }
131 };
132
133 /// Iterator access to the held interfaces.
134 template <typename InterfaceT>
136 return iterator<InterfaceT>(orderedInterfaces.begin());
137 }
138 template <typename InterfaceT>
140 return iterator<InterfaceT>(orderedInterfaces.end());
141 }
142
143private:
144 /// A set of registered dialect interface instances.
145 InterfaceSetT interfaces;
146 /// An ordered list of the registered interface instances, necessary for
147 /// deterministic iteration.
148 // NOTE: SetVector does not provide find access, so it can't be used here.
149 InterfaceVectorT orderedInterfaces;
150};
151} // namespace detail
152
153/// A collection of dialect interfaces within a context, for a given concrete
154/// interface type.
155template <typename InterfaceType>
158public:
160
161 /// Collect the registered dialect interfaces within the provided context.
164 ctx, InterfaceType::getInterfaceID(),
165 llvm::getTypeName<InterfaceType>()) {}
166
167 /// Get the interface for a given object, or null if one is not registered.
168 /// The object may be a dialect or an operation instance.
169 template <typename Object>
170 const InterfaceType *getInterfaceFor(Object *obj) const {
171 return static_cast<const InterfaceType *>(
173 }
174
175 /// Iterator access to the held interfaces.
176 using iterator =
180
181private:
184};
185
186} // namespace mlir
187
188#endif
lhs
DialectInterfaceCollection< InterfaceType > Base
detail::DialectInterfaceCollectionBase::iterator< InterfaceType > iterator
Iterator access to the held interfaces.
DialectInterfaceCollection(MLIRContext *ctx)
Collect the registered dialect interfaces within the provided context.
const InterfaceType * getInterfaceFor(Object *obj) const
Get the interface for a given object, or null if one is not registered.
This class represents an interface overridden for a single dialect.
MLIRContext * getContext() const
Return the context that holds the parent dialect of this interface.
Definition Dialect.cpp:117
Dialect * getDialect() const
Return the dialect that this interface represents.
DialectInterface(Dialect *dialect, TypeID id)
detail::DialectInterfaceBase< ConcreteType, DialectInterface > Base
The base class used for all derived interface types.
TypeID getID() const
Return the derived interface id.
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition Dialect.h:38
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
This class provides an efficient unique identifier for a specific C++ type.
Definition TypeID.h:107
static TypeID get()
Construct a type info object for the given type T.
Definition TypeID.h:245
The base class used for all derived interface types.
DialectInterfaceBase< ConcreteType, BaseT > Base
static TypeID getInterfaceID()
Get a unique id for the derived interface type.
This class is the base class for a collection of instances for a specific interface kind.
const DialectInterface * getInterfaceFor(Operation *op) const
Get the interface for the dialect of given operation, or null if one is not registered.
Definition Dialect.cpp:140
iterator< InterfaceT > interface_end() const
const DialectInterface * getInterfaceFor(Dialect *dialect) const
Get the interface for the given dialect.
iterator< InterfaceT > interface_begin() const
Iterator access to the held interfaces.
DialectInterfaceCollectionBase(MLIRContext *ctx, TypeID interfaceKind, StringRef interfaceName)
Definition Dialect.cpp:121
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Definition CallGraph.h:229
AttrTypeReplacer.
Include the generated interface declarations.
llvm::DenseMapInfo< T, Enable > DenseMapInfo
Definition LLVM.h:122
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:128
An iterator class that iterates the held interface objects of the given derived interface type.
const InterfaceT & mapElement(const DialectInterface *interface) const
Map the element to the iterator result type.