1//===- DialectGen.cpp - MLIR dialect definitions generator ----------------===//
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// DialectGen uses the description of dialects to generate C++ definitions.
10//
11//===----------------------------------------------------------------------===//
12
13#include "CppGenUtilities.h"
14#include "DialectGenUtilities.h"
15#include "mlir/TableGen/Class.h"
16#include "mlir/TableGen/CodeGenHelpers.h"
17#include "mlir/TableGen/Format.h"
18#include "mlir/TableGen/GenInfo.h"
19#include "mlir/TableGen/Interfaces.h"
20#include "mlir/TableGen/Operator.h"
21#include "mlir/TableGen/Trait.h"
22#include "llvm/ADT/Sequence.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/Support/CommandLine.h"
25#include "llvm/Support/Signals.h"
26#include "llvm/TableGen/Error.h"
27#include "llvm/TableGen/Record.h"
28#include "llvm/TableGen/TableGenBackend.h"
29
30#define DEBUG_TYPE "mlir-tblgen-opdefgen"
31
32using namespace mlir;
33using namespace mlir::tblgen;
34using llvm::RecordKeeper;
35
36static llvm::cl::OptionCategory dialectGenCat("Options for -gen-dialect-*");
37static llvm::cl::opt<std::string>
38 selectedDialect("dialect", llvm::cl::desc("The dialect to gen for"),
39 llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated);
40
41static void populateDiscardableAttributes(
42 Dialect &dialect, const llvm::DagInit *discardableAttrDag,
43 SmallVector<std::pair<std::string, std::string>> &discardableAttributes) {
44 for (int i : llvm::seq<int>(Begin: 0, End: discardableAttrDag->getNumArgs())) {
45 const llvm::Init *arg = discardableAttrDag->getArg(Num: i);
46
47 StringRef givenName = discardableAttrDag->getArgNameStr(Num: i);
48 if (givenName.empty())
49 PrintFatalError(ErrorLoc: dialect.getDef()->getLoc(),
50 Msg: "discardable attributes must be named");
51 discardableAttributes.push_back(
52 Elt: {givenName.str(), arg->getAsUnquotedString()});
53 }
54}
55
56std::optional<Dialect>
57tblgen::findDialectToGenerate(ArrayRef<Dialect> dialects) {
58 if (dialects.empty()) {
59 llvm::errs() << "no dialect was found\n";
60 return std::nullopt;
61 }
62
63 // Select the dialect to gen for.
64 if (dialects.size() == 1 && selectedDialect.getNumOccurrences() == 0)
65 return dialects.front();
66
67 if (selectedDialect.getNumOccurrences() == 0) {
68 llvm::errs() << "when more than 1 dialect is present, one must be selected "
69 "via '-dialect'\n";
70 return std::nullopt;
71 }
72
73 const auto *dialectIt = llvm::find_if(Range&: dialects, P: [](const Dialect &dialect) {
74 return dialect.getName() == selectedDialect;
75 });
76 if (dialectIt == dialects.end()) {
77 llvm::errs() << "selected dialect with '-dialect' does not exist\n";
78 return std::nullopt;
79 }
80 return *dialectIt;
81}
82
83//===----------------------------------------------------------------------===//
84// GEN: Dialect declarations
85//===----------------------------------------------------------------------===//
86
87/// The code block for the start of a dialect class declaration.
88///
89/// {0}: The name of the dialect class.
90/// {1}: The dialect namespace.
91/// {2}: The dialect parent class.
92static const char *const dialectDeclBeginStr = R"(
93class {0} : public ::mlir::{2} {
94 explicit {0}(::mlir::MLIRContext *context);
95
96 void initialize();
97 friend class ::mlir::MLIRContext;
98public:
99 ~{0}() override;
100 static constexpr ::llvm::StringLiteral getDialectNamespace() {
101 return ::llvm::StringLiteral("{1}");
102 }
103)";
104
105/// Registration for a single dependent dialect: to be inserted in the ctor
106/// above for each dependent dialect.
107const char *const dialectRegistrationTemplate =
108 "getContext()->loadDialect<{0}>();";
109
110/// The code block for the attribute parser/printer hooks.
111static const char *const attrParserDecl = R"(
112 /// Parse an attribute registered to this dialect.
113 ::mlir::Attribute parseAttribute(::mlir::DialectAsmParser &parser,
114 ::mlir::Type type) const override;
115
116 /// Print an attribute registered to this dialect.
117 void printAttribute(::mlir::Attribute attr,
118 ::mlir::DialectAsmPrinter &os) const override;
119)";
120
121/// The code block for the type parser/printer hooks.
122static const char *const typeParserDecl = R"(
123 /// Parse a type registered to this dialect.
124 ::mlir::Type parseType(::mlir::DialectAsmParser &parser) const override;
125
126 /// Print a type registered to this dialect.
127 void printType(::mlir::Type type,
128 ::mlir::DialectAsmPrinter &os) const override;
129)";
130
131/// The code block for the canonicalization pattern registration hook.
132static const char *const canonicalizerDecl = R"(
133 /// Register canonicalization patterns.
134 void getCanonicalizationPatterns(
135 ::mlir::RewritePatternSet &results) const override;
136)";
137
138/// The code block for the constant materializer hook.
139static const char *const constantMaterializerDecl = R"(
140 /// Materialize a single constant operation from a given attribute value with
141 /// the desired resultant type.
142 ::mlir::Operation *materializeConstant(::mlir::OpBuilder &builder,
143 ::mlir::Attribute value,
144 ::mlir::Type type,
145 ::mlir::Location loc) override;
146)";
147
148/// The code block for the operation attribute verifier hook.
149static const char *const opAttrVerifierDecl = R"(
150 /// Provides a hook for verifying dialect attributes attached to the given
151 /// op.
152 ::llvm::LogicalResult verifyOperationAttribute(
153 ::mlir::Operation *op, ::mlir::NamedAttribute attribute) override;
154)";
155
156/// The code block for the region argument attribute verifier hook.
157static const char *const regionArgAttrVerifierDecl = R"(
158 /// Provides a hook for verifying dialect attributes attached to the given
159 /// op's region argument.
160 ::llvm::LogicalResult verifyRegionArgAttribute(
161 ::mlir::Operation *op, unsigned regionIndex, unsigned argIndex,
162 ::mlir::NamedAttribute attribute) override;
163)";
164
165/// The code block for the region result attribute verifier hook.
166static const char *const regionResultAttrVerifierDecl = R"(
167 /// Provides a hook for verifying dialect attributes attached to the given
168 /// op's region result.
169 ::llvm::LogicalResult verifyRegionResultAttribute(
170 ::mlir::Operation *op, unsigned regionIndex, unsigned resultIndex,
171 ::mlir::NamedAttribute attribute) override;
172)";
173
174/// The code block for the op interface fallback hook.
175static const char *const operationInterfaceFallbackDecl = R"(
176 /// Provides a hook for op interface.
177 void *getRegisteredInterfaceForOp(mlir::TypeID interfaceID,
178 mlir::OperationName opName) override;
179)";
180
181/// The code block for the discardable attribute helper.
182static const char *const discardableAttrHelperDecl = R"(
183 /// Helper to manage the discardable attribute `{1}`.
184 class {0}AttrHelper {{
185 ::mlir::StringAttr name;
186 public:
187 static constexpr ::llvm::StringLiteral getNameStr() {{
188 return "{4}.{1}";
189 }
190 constexpr ::mlir::StringAttr getName() const {{
191 return name;
192 }
193
194 explicit {0}AttrHelper(::mlir::MLIRContext *ctx)
195 : name(::mlir::StringAttr::get(ctx, getNameStr())) {{}
196
197 {2} getAttr(::mlir::Operation *op) const {{
198 return op->getDiscardableAttrOfType<{2}>(name);
199 }
200 void setAttr(::mlir::Operation *op, {2} val) const {{
201 op->setDiscardableAttr(name, val);
202 }
203 bool isAttrPresent(::mlir::Operation *op) const {{
204 return op->hasDiscardableAttrOfType<{2}>(name);
205 }
206 void removeAttr(::mlir::Operation *op) const {{
207 assert(op->hasDiscardableAttrOfType<{2}>(name));
208 op->removeDiscardableAttr(name);
209 }
210 };
211 {0}AttrHelper get{0}AttrHelper() {
212 return {3}AttrName;
213 }
214 private:
215 {0}AttrHelper {3}AttrName;
216 public:
217)";
218
219/// Generate the declaration for the given dialect class.
220static void emitDialectDecl(Dialect &dialect, raw_ostream &os) {
221 // Emit all nested namespaces.
222 {
223 DialectNamespaceEmitter nsEmitter(os, dialect);
224
225 // Emit the start of the decl.
226 std::string cppName = dialect.getCppClassName();
227 StringRef superClassName =
228 dialect.isExtensible() ? "ExtensibleDialect" : "Dialect";
229
230 tblgen::emitSummaryAndDescComments(os, summary: dialect.getSummary(),
231 description: dialect.getDescription(),
232 /*terminateCmment=*/terminateComment: false);
233 os << llvm::formatv(Fmt: dialectDeclBeginStr, Vals&: cppName, Vals: dialect.getName(),
234 Vals&: superClassName);
235
236 // If the dialect requested the default attribute printer and parser, emit
237 // the declarations for the hooks.
238 if (dialect.useDefaultAttributePrinterParser())
239 os << attrParserDecl;
240 // If the dialect requested the default type printer and parser, emit the
241 // delcarations for the hooks.
242 if (dialect.useDefaultTypePrinterParser())
243 os << typeParserDecl;
244
245 // Add the decls for the various features of the dialect.
246 if (dialect.hasCanonicalizer())
247 os << canonicalizerDecl;
248 if (dialect.hasConstantMaterializer())
249 os << constantMaterializerDecl;
250 if (dialect.hasOperationAttrVerify())
251 os << opAttrVerifierDecl;
252 if (dialect.hasRegionArgAttrVerify())
253 os << regionArgAttrVerifierDecl;
254 if (dialect.hasRegionResultAttrVerify())
255 os << regionResultAttrVerifierDecl;
256 if (dialect.hasOperationInterfaceFallback())
257 os << operationInterfaceFallbackDecl;
258
259 const llvm::DagInit *discardableAttrDag =
260 dialect.getDiscardableAttributes();
261 SmallVector<std::pair<std::string, std::string>> discardableAttributes;
262 populateDiscardableAttributes(dialect, discardableAttrDag,
263 discardableAttributes);
264
265 for (const auto &attrPair : discardableAttributes) {
266 std::string camelNameUpper = llvm::convertToCamelFromSnakeCase(
267 input: attrPair.first, /*capitalizeFirst=*/true);
268 std::string camelName = llvm::convertToCamelFromSnakeCase(
269 input: attrPair.first, /*capitalizeFirst=*/false);
270 os << llvm::formatv(Fmt: discardableAttrHelperDecl, Vals&: camelNameUpper,
271 Vals: attrPair.first, Vals: attrPair.second, Vals&: camelName,
272 Vals: dialect.getName());
273 }
274
275 if (std::optional<StringRef> extraDecl = dialect.getExtraClassDeclaration())
276 os << *extraDecl;
277
278 // End the dialect decl.
279 os << "};\n";
280 }
281 if (!dialect.getCppNamespace().empty())
282 os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace()
283 << "::" << dialect.getCppClassName() << ")\n";
284}
285
286/// Emit the `#include` directives for the headers declaring the C++ constructs
287/// used by the declaration of `dialect`.
288static void emitDialectDeclIncludes(const Dialect &dialect, raw_ostream &os) {
289 SmallVector<StringRef> includes;
290 // `::mlir::Dialect` or `::mlir::ExtensibleDialect`.
291 includes.push_back(Elt: dialect.isExtensible() ? "mlir/IR/ExtensibleDialect.h"
292 : "mlir/IR/Dialect.h");
293 if (dialect.getDiscardableAttributes()->getNumArgs() != 0) {
294 // `::mlir::StringAttr` and `::mlir::Operation`, used by the discardable
295 // attribute helpers.
296 includes.push_back(Elt: "mlir/IR/BuiltinAttributes.h");
297 includes.push_back(Elt: "mlir/IR/Operation.h");
298 }
299 tblgen::emitIncludes(os, headers: includes);
300}
301
302static bool emitDialectDecls(const RecordKeeper &records, raw_ostream &os) {
303 emitSourceFileHeader(Desc: "Dialect Declarations", OS&: os, Record: records);
304
305 auto dialectDefs = records.getAllDerivedDefinitions(ClassName: "Dialect");
306 if (dialectDefs.empty())
307 return false;
308
309 SmallVector<Dialect> dialects(dialectDefs.begin(), dialectDefs.end());
310 std::optional<Dialect> dialect = findDialectToGenerate(dialects);
311 if (!dialect)
312 return true;
313 emitDialectDeclIncludes(dialect: *dialect, os);
314 emitDialectDecl(dialect&: *dialect, os);
315 return false;
316}
317
318//===----------------------------------------------------------------------===//
319// GEN: Dialect definitions
320//===----------------------------------------------------------------------===//
321
322/// The code block to generate a dialect constructor definition.
323///
324/// {0}: The name of the dialect class.
325/// {1}: Initialization code that is emitted in the ctor body before calling
326/// initialize(), such as dependent dialect registration.
327/// {2}: The dialect parent class.
328/// {3}: Extra members to initialize
329static const char *const dialectConstructorStr = R"(
330{0}::{0}(::mlir::MLIRContext *context)
331 : ::mlir::{2}(getDialectNamespace(), context, ::mlir::TypeID::get<{0}>())
332 {3}
333 {{
334 {1}
335 initialize();
336}
337)";
338
339/// The code block to generate a default destructor definition.
340///
341/// {0}: The name of the dialect class.
342static const char *const dialectDestructorStr = R"(
343{0}::~{0}() = default;
344
345)";
346
347static void emitDialectDef(Dialect &dialect, const RecordKeeper &records,
348 raw_ostream &os) {
349 std::string cppClassName = dialect.getCppClassName();
350
351 // Emit the TypeID explicit specializations to have a single symbol def.
352 if (!dialect.getCppNamespace().empty())
353 os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace()
354 << "::" << cppClassName << ")\n";
355
356 // Emit all nested namespaces.
357 DialectNamespaceEmitter nsEmitter(os, dialect);
358
359 /// Build the list of dependent dialects.
360 std::string dependentDialectRegistrations;
361 {
362 llvm::raw_string_ostream dialectsOs(dependentDialectRegistrations);
363 llvm::interleave(
364 c: dialect.getDependentDialects(), os&: dialectsOs,
365 each_fn: [&](StringRef dependentDialect) {
366 dialectsOs << llvm::formatv(Fmt: dialectRegistrationTemplate,
367 Vals&: dependentDialect);
368 },
369 separator: "\n ");
370 }
371
372 // Emit the constructor and destructor.
373 StringRef superClassName =
374 dialect.isExtensible() ? "ExtensibleDialect" : "Dialect";
375
376 const llvm::DagInit *discardableAttrDag = dialect.getDiscardableAttributes();
377 SmallVector<std::pair<std::string, std::string>> discardableAttributes;
378 populateDiscardableAttributes(dialect, discardableAttrDag,
379 discardableAttributes);
380 std::string discardableAttributesInit;
381 for (const auto &attrPair : discardableAttributes) {
382 std::string camelName = llvm::convertToCamelFromSnakeCase(
383 input: attrPair.first, /*capitalizeFirst=*/false);
384 llvm::raw_string_ostream os(discardableAttributesInit);
385 os << ", " << camelName << "AttrName(context)";
386 }
387
388 os << llvm::formatv(Fmt: dialectConstructorStr, Vals&: cppClassName,
389 Vals&: dependentDialectRegistrations, Vals&: superClassName,
390 Vals&: discardableAttributesInit);
391 if (!dialect.hasNonDefaultDestructor())
392 os << llvm::formatv(Fmt: dialectDestructorStr, Vals&: cppClassName);
393}
394
395static bool emitDialectDefs(const RecordKeeper &records, raw_ostream &os) {
396 emitSourceFileHeader(Desc: "Dialect Definitions", OS&: os, Record: records);
397
398 auto dialectDefs = records.getAllDerivedDefinitions(ClassName: "Dialect");
399 if (dialectDefs.empty())
400 return false;
401
402 SmallVector<Dialect> dialects(dialectDefs.begin(), dialectDefs.end());
403 std::optional<Dialect> dialect = findDialectToGenerate(dialects);
404 if (!dialect)
405 return true;
406 // `::mlir::MLIRContext::loadDialect`, used to load the dependent dialects.
407 if (!dialect->getDependentDialects().empty())
408 tblgen::emitIncludes(os, headers: StringRef("mlir/IR/MLIRContext.h"));
409 emitDialectDef(dialect&: *dialect, records, os);
410 return false;
411}
412
413//===----------------------------------------------------------------------===//
414// GEN: Dialect registration hooks
415//===----------------------------------------------------------------------===//
416
417static mlir::GenRegistration
418 genDialectDecls("gen-dialect-decls", "Generate dialect declarations",
419 [](const RecordKeeper &records, raw_ostream &os) {
420 return emitDialectDecls(records, os);
421 });
422
423static mlir::GenRegistration
424 genDialectDefs("gen-dialect-defs", "Generate dialect definitions",
425 [](const RecordKeeper &records, raw_ostream &os) {
426 return emitDialectDefs(records, os);
427 });
428