MLIR 23.0.0git
Interfaces.cpp
Go to the documentation of this file.
1//===- Interfaces.cpp - Interface classes ---------------------------------===//
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
10#include "llvm/ADT/FunctionExtras.h"
11#include "llvm/ADT/StringSet.h"
12#include "llvm/TableGen/Error.h"
13#include "llvm/TableGen/Record.h"
14#include <utility>
15
16using namespace mlir;
17using namespace mlir::tblgen;
18using llvm::DagInit;
19using llvm::DefInit;
20using llvm::Init;
21using llvm::ListInit;
22using llvm::Record;
23using llvm::StringInit;
24
25//===----------------------------------------------------------------------===//
26// InterfaceMethod
27//===----------------------------------------------------------------------===//
28
29InterfaceMethod::InterfaceMethod(const Record *def, std::string uniqueName)
30 : def(def), uniqueName(uniqueName) {
31 const DagInit *args = def->getValueAsDag("arguments");
32 for (unsigned i = 0, e = args->getNumArgs(); i != e; ++i) {
33 arguments.push_back({cast<StringInit>(args->getArg(i))->getValue(),
34 args->getArgNameStr(i)});
35 }
36}
37
39 return def->getValueAsString("returnType");
40}
41
42// Return the name of this method.
43StringRef InterfaceMethod::getName() const {
44 return def->getValueAsString("name");
45}
46
47// Return the name of this method.
48StringRef InterfaceMethod::getUniqueName() const { return uniqueName; }
49
50// Return if this method is static.
52 return def->isSubClassOf("StaticInterfaceMethod");
53}
54
55// Return if the method is only a declaration.
57 return def->isSubClassOf("InterfaceMethodDeclaration");
58}
59
60// Return the body for this method if it has one.
61std::optional<StringRef> InterfaceMethod::getBody() const {
62 // Trim leading and trailing spaces from the default implementation.
63 auto value = def->getValueAsString("body").trim();
64 return value.empty() ? std::optional<StringRef>() : value;
65}
66
67// Return the default implementation for this method if it has one.
68std::optional<StringRef> InterfaceMethod::getDefaultImplementation() const {
69 // Trim leading and trailing spaces from the default implementation.
70 auto value = def->getValueAsString("defaultBody").trim();
71 return value.empty() ? std::optional<StringRef>() : value;
72}
73
74// Return the description of this method if it has one.
75std::optional<StringRef> InterfaceMethod::getDescription() const {
76 auto value = def->getValueAsString("description");
77 return value.empty() ? std::optional<StringRef>() : value;
78}
79
83
84bool InterfaceMethod::arg_empty() const { return arguments.empty(); }
85
86//===----------------------------------------------------------------------===//
87// Interface
88//===----------------------------------------------------------------------===//
89
90Interface::Interface(const Record *def) : def(def) {
91 assert(def->isSubClassOf("Interface") &&
92 "must be subclass of TableGen 'Interface' class");
93
94 // Initialize the interface methods.
95 auto *listInit = dyn_cast<ListInit>(def->getValueInit("methods"));
96 // In case of overloaded methods, we need to find a unique name for each for
97 // the internal function pointer in the "vtable" we generate. This is an
98 // internal name, we could use a randomly generated name as long as there are
99 // no collisions.
100 StringSet<> uniqueNames;
101 for (const Init *init : listInit->getElements()) {
102 std::string name =
103 cast<DefInit>(init)->getDef()->getValueAsString("name").str();
104 while (!uniqueNames.insert(name).second) {
105 name = name + "_" + std::to_string(uniqueNames.size());
106 }
107 methods.emplace_back(cast<DefInit>(init)->getDef(), name);
108 }
109
110 // Initialize the interface base classes.
111 auto *basesInit = dyn_cast<ListInit>(def->getValueInit("baseInterfaces"));
112 // Chained inheritance will produce duplicates in the base interface set.
113 StringSet<> basesAdded;
114 llvm::unique_function<void(Interface)> addBaseInterfaceFn =
115 [&](const Interface &baseInterface) {
116 // Inherit any base interfaces.
117 for (const auto &baseBaseInterface : baseInterface.getBaseInterfaces())
118 addBaseInterfaceFn(baseBaseInterface);
119
120 // Add the base interface.
121 if (basesAdded.contains(baseInterface.getName()))
122 return;
123 baseInterfaces.push_back(std::make_unique<Interface>(baseInterface));
124 basesAdded.insert(baseInterface.getName());
125 };
126 for (const Init *init : basesInit->getElements())
127 addBaseInterfaceFn(Interface(cast<DefInit>(init)->getDef()));
128}
129
130// Return the name of this interface.
131StringRef Interface::getName() const {
132 return def->getValueAsString("cppInterfaceName");
133}
134
135// Returns this interface's name prefixed with namespaces.
137 StringRef cppNamespace = getCppNamespace();
138 StringRef name = getName();
139 if (cppNamespace.empty())
140 return name.str();
141 return (cppNamespace + "::" + name).str();
142}
143
144// Return the C++ namespace of this interface.
145StringRef Interface::getCppNamespace() const {
146 return def->getValueAsString("cppNamespace");
147}
148
149// Return the methods of this interface.
151
152// Return the description of this method if it has one.
153std::optional<StringRef> Interface::getDescription() const {
154 auto value = def->getValueAsString("description");
155 return value.empty() ? std::optional<StringRef>() : value;
156}
157
158// Return the interfaces extra class declaration code.
159std::optional<StringRef> Interface::getExtraClassDeclaration() const {
160 auto value = def->getValueAsString("extraClassDeclaration");
161 return value.empty() ? std::optional<StringRef>() : value;
162}
163
164// Return the traits extra class declaration code.
165std::optional<StringRef> Interface::getExtraTraitClassDeclaration() const {
166 auto value = def->getValueAsString("extraTraitClassDeclaration");
167 return value.empty() ? std::optional<StringRef>() : value;
168}
169
170// Return the shared extra class declaration code.
171std::optional<StringRef> Interface::getExtraSharedClassDeclaration() const {
172 auto value = def->getValueAsString("extraSharedClassDeclaration");
173 return value.empty() ? std::optional<StringRef>() : value;
174}
175
176std::optional<StringRef> Interface::getExtraClassOf() const {
177 auto value = def->getValueAsString("extraClassOf");
178 return value.empty() ? std::optional<StringRef>() : value;
179}
180
181// Return the body for this method if it has one.
182std::optional<StringRef> Interface::getVerify() const {
183 // Only OpInterface supports the verify method.
184 if (!isa<OpInterface>(this))
185 return std::nullopt;
186 auto value = def->getValueAsString("verify");
187 return value.empty() ? std::optional<StringRef>() : value;
188}
189
191 return def->getValueAsBit("verifyWithRegions");
192}
193
194//===----------------------------------------------------------------------===//
195// AttrInterface
196//===----------------------------------------------------------------------===//
197
198bool AttrInterface::classof(const Interface *interface) {
199 return interface->getDef().isSubClassOf("AttrInterface");
200}
201
202//===----------------------------------------------------------------------===//
203// OpInterface
204//===----------------------------------------------------------------------===//
205
206bool OpInterface::classof(const Interface *interface) {
207 return interface->getDef().isSubClassOf("OpInterface");
208}
209
210//===----------------------------------------------------------------------===//
211// TypeInterface
212//===----------------------------------------------------------------------===//
213
214bool TypeInterface::classof(const Interface *interface) {
215 return interface->getDef().isSubClassOf("TypeInterface");
216}
217
218//===----------------------------------------------------------------------===//
219// DialectInterface
220//===----------------------------------------------------------------------===//
221
222bool DialectInterface::classof(const Interface *interface) {
223 return interface->getDef().isSubClassOf("DialectInterface");
224}
StringRef getReturnType() const
std::optional< StringRef > getDefaultImplementation() const
ArrayRef< Argument > getArguments() const
InterfaceMethod(const llvm::Record *def, std::string uniqueName)
std::optional< StringRef > getBody() const
StringRef getUniqueName() const
std::optional< StringRef > getDescription() const
Interface(const llvm::Record *def)
std::optional< StringRef > getExtraClassOf() const
std::optional< StringRef > getDescription() const
std::optional< StringRef > getExtraClassDeclaration() const
std::optional< StringRef > getExtraSharedClassDeclaration() const
ArrayRef< InterfaceMethod > getMethods() const
std::optional< StringRef > getExtraTraitClassDeclaration() const
bool verifyWithRegions() const
std::optional< StringRef > getVerify() const
const llvm::Record & getDef() const
Definition Interfaces.h:133
std::string getFullyQualifiedName() const
StringRef getCppNamespace() const
StringRef getName() const
Include the generated interface declarations.
llvm::StringSet< AllocatorTy > StringSet
Definition LLVM.h:125
Interface(const llvm::Record *def)
static bool classof(const Interface *interface)
Interface(const llvm::Record *def)
static bool classof(const Interface *interface)
Interface(const llvm::Record *def)
static bool classof(const Interface *interface)
Interface(const llvm::Record *def)
static bool classof(const Interface *interface)