MLIR 23.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 return lhs == rhs->getDialect();
91 }
92 };
93
94 /// A set of registered dialect interface instances.
96 using InterfaceVectorT = std::vector<const DialectInterface *>;
97
98public:
100 StringRef interfaceName);
102
103protected:
104 /// Get the interface for the dialect of given operation, or null if one
105 /// is not registered.
107
108 /// Get the interface for the given dialect.
109 const DialectInterface *getInterfaceFor(Dialect *dialect) const {
110 auto it = interfaces.find_as(dialect);
111 return it == interfaces.end() ? nullptr : *it;
112 }
113
114 /// An iterator class that iterates the held interface objects of the given
115 /// derived interface type.
116 template <typename InterfaceT>
117 struct iterator
118 : public llvm::mapped_iterator_base<iterator<InterfaceT>,
119 InterfaceVectorT::const_iterator,
120 const InterfaceT &> {
121 using llvm::mapped_iterator_base<iterator<InterfaceT>,
122 InterfaceVectorT::const_iterator,
123 const InterfaceT &>::mapped_iterator_base;
124
125 /// Map the element to the iterator result type.
126 const InterfaceT &mapElement(const DialectInterface *interface) const {
127 return *static_cast<const InterfaceT *>(interface);
128 }
129 };
130
131 /// Iterator access to the held interfaces.
132 template <typename InterfaceT>
134 return iterator<InterfaceT>(orderedInterfaces.begin());
135 }
136 template <typename InterfaceT>
138 return iterator<InterfaceT>(orderedInterfaces.end());
139 }
140
141private:
142 /// A set of registered dialect interface instances.
143 InterfaceSetT interfaces;
144 /// An ordered list of the registered interface instances, necessary for
145 /// deterministic iteration.
146 // NOTE: SetVector does not provide find access, so it can't be used here.
147 InterfaceVectorT orderedInterfaces;
148};
149} // namespace detail
150
151/// A collection of dialect interfaces within a context, for a given concrete
152/// interface type.
153template <typename InterfaceType>
156public:
158
159 /// Collect the registered dialect interfaces within the provided context.
162 ctx, InterfaceType::getInterfaceID(),
163 llvm::getTypeName<InterfaceType>()) {}
164
165 /// Get the interface for a given object, or null if one is not registered.
166 /// The object may be a dialect or an operation instance.
167 template <typename Object>
168 const InterfaceType *getInterfaceFor(Object *obj) const {
169 return static_cast<const InterfaceType *>(
171 }
172
173 /// Iterator access to the held interfaces.
174 using iterator =
178
179private:
182};
183
184} // namespace mlir
185
186#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:87
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:227
AttrTypeReplacer.
Include the generated interface declarations.
llvm::DenseMapInfo< T, Enable > DenseMapInfo
Definition LLVM.h:116
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:122
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.