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