/
OpDocGen.cpp
461 lines (404 loc) · 15.8 KB
/
OpDocGen.cpp
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//===- OpDocGen.cpp - MLIR operation documentation 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
//
//===----------------------------------------------------------------------===//
//
// OpDocGen uses the description of operations to generate documentation for the
// operations.
//
//===----------------------------------------------------------------------===//
#include "DialectGenUtilities.h"
#include "DocGenUtilities.h"
#include "OpGenHelpers.h"
#include "mlir/Support/IndentedOstream.h"
#include "mlir/TableGen/AttrOrTypeDef.h"
#include "mlir/TableGen/GenInfo.h"
#include "mlir/TableGen/Operator.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/Signals.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"
#include <set>
using namespace llvm;
using namespace mlir;
using namespace mlir::tblgen;
using mlir::tblgen::Operator;
// Emit the description by aligning the text to the left per line (e.g.,
// removing the minimum indentation across the block).
//
// This expects that the description in the tablegen file is already formatted
// in a way the user wanted but has some additional indenting due to being
// nested in the op definition.
void mlir::tblgen::emitDescription(StringRef description, raw_ostream &os) {
raw_indented_ostream ros(os);
ros.printReindented(description.rtrim(" \t"));
}
void mlir::tblgen::emitDescriptionComment(StringRef description,
raw_ostream &os, StringRef prefix) {
if (description.empty())
return;
raw_indented_ostream ros(os);
StringRef trimmed = description.rtrim(" \t");
ros.printReindented(trimmed, (Twine(prefix) + "/// ").str());
if (!trimmed.endswith("\n"))
ros << "\n";
}
// Emits `str` with trailing newline if not empty.
static void emitIfNotEmpty(StringRef str, raw_ostream &os) {
if (!str.empty()) {
emitDescription(str, os);
os << "\n";
}
}
/// Emit the given named constraint.
template <typename T>
static void emitNamedConstraint(const T &it, raw_ostream &os) {
if (!it.name.empty())
os << "| `" << it.name << "`";
else
os << "«unnamed»";
os << " | " << it.constraint.getSummary() << "\n";
}
//===----------------------------------------------------------------------===//
// Operation Documentation
//===----------------------------------------------------------------------===//
/// Emit the assembly format of an operation.
static void emitAssemblyFormat(StringRef opName, StringRef format,
raw_ostream &os) {
os << "\nSyntax:\n\n```\noperation ::= `" << opName << "` ";
// Print the assembly format aligned.
unsigned indent = strlen("operation ::= ");
std::pair<StringRef, StringRef> split = format.split('\n');
os << split.first.trim() << "\n";
do {
split = split.second.split('\n');
StringRef formatChunk = split.first.trim();
if (!formatChunk.empty())
os.indent(indent) << formatChunk << "\n";
} while (!split.second.empty());
os << "```\n\n";
}
static void emitOpTraitsDoc(const Operator &op, raw_ostream &os) {
// TODO: We should link to the trait/documentation of it. That also means we
// should add descriptions to traits that can be queried.
// Collect using set to sort effects, interfaces & traits.
std::set<std::string> effects, interfaces, traits;
for (auto &trait : op.getTraits()) {
if (isa<PredTrait>(&trait))
continue;
std::string name = trait.getDef().getName().str();
StringRef ref = name;
StringRef traitName = trait.getDef().getValueAsString("trait");
traitName.consume_back("::Trait");
traitName.consume_back("::Impl");
if (ref.startswith("anonymous_"))
name = traitName.str();
if (isa<InterfaceTrait>(&trait)) {
if (trait.getDef().isSubClassOf("SideEffectsTraitBase")) {
auto effectName = trait.getDef().getValueAsString("baseEffectName");
effectName.consume_front("::");
effectName.consume_front("mlir::");
std::string effectStr;
llvm::raw_string_ostream os(effectStr);
os << effectName << "{";
auto list = trait.getDef().getValueAsListOfDefs("effects");
llvm::interleaveComma(list, os, [&](Record *rec) {
StringRef effect = rec->getValueAsString("effect");
effect.consume_front("::");
effect.consume_front("mlir::");
os << effect << " on " << rec->getValueAsString("resource");
});
os << "}";
effects.insert(os.str());
name.append(llvm::formatv(" ({0})", traitName).str());
}
interfaces.insert(name);
continue;
}
traits.insert(name);
}
if (!traits.empty()) {
llvm::interleaveComma(traits, os << "\nTraits: ");
os << "\n";
}
if (!interfaces.empty()) {
llvm::interleaveComma(interfaces, os << "\nInterfaces: ");
os << "\n";
}
if (!effects.empty()) {
llvm::interleaveComma(effects, os << "\nEffects: ");
os << "\n";
}
}
static void emitOpDoc(const Operator &op, raw_ostream &os) {
os << llvm::formatv("### `{0}` ({1})\n", op.getOperationName(),
op.getQualCppClassName());
// Emit the summary, syntax, and description if present.
if (op.hasSummary())
os << "\n" << op.getSummary() << "\n\n";
if (op.hasAssemblyFormat())
emitAssemblyFormat(op.getOperationName(), op.getAssemblyFormat().trim(),
os);
if (op.hasDescription())
mlir::tblgen::emitDescription(op.getDescription(), os);
emitOpTraitsDoc(op, os);
// Emit attributes.
if (op.getNumAttributes() != 0) {
// TODO: Attributes are only documented by TableGen name, with no further
// info. This should be improved.
os << "\n#### Attributes:\n\n";
os << "| Attribute | MLIR Type | Description |\n"
<< "| :-------: | :-------: | ----------- |\n";
for (const auto &it : op.getAttributes()) {
StringRef storageType = it.attr.getStorageType();
os << "| `" << it.name << "` | " << storageType << " | "
<< it.attr.getSummary() << "\n";
}
}
// Emit each of the operands.
if (op.getNumOperands() != 0) {
os << "\n#### Operands:\n\n";
os << "| Operand | Description |\n"
<< "| :-----: | ----------- |\n";
for (const auto &it : op.getOperands())
emitNamedConstraint(it, os);
}
// Emit results.
if (op.getNumResults() != 0) {
os << "\n#### Results:\n\n";
os << "| Result | Description |\n"
<< "| :----: | ----------- |\n";
for (const auto &it : op.getResults())
emitNamedConstraint(it, os);
}
// Emit successors.
if (op.getNumSuccessors() != 0) {
os << "\n#### Successors:\n\n";
os << "| Successor | Description |\n"
<< "| :-------: | ----------- |\n";
for (const auto &it : op.getSuccessors())
emitNamedConstraint(it, os);
}
os << "\n";
}
static void emitOpDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
auto opDefs = getRequestedOpDefinitions(recordKeeper);
os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
for (const llvm::Record *opDef : opDefs)
emitOpDoc(Operator(opDef), os);
}
//===----------------------------------------------------------------------===//
// Attribute Documentation
//===----------------------------------------------------------------------===//
static void emitAttrDoc(const Attribute &attr, raw_ostream &os) {
os << "### " << attr.getSummary() << "\n\n";
emitDescription(attr.getDescription(), os);
os << "\n\n";
}
//===----------------------------------------------------------------------===//
// Type Documentation
//===----------------------------------------------------------------------===//
static void emitTypeDoc(const Type &type, raw_ostream &os) {
os << "### " << type.getSummary() << "\n\n";
emitDescription(type.getDescription(), os);
os << "\n\n";
}
//===----------------------------------------------------------------------===//
// TypeDef Documentation
//===----------------------------------------------------------------------===//
static void emitAttrOrTypeDefAssemblyFormat(const AttrOrTypeDef &def,
raw_ostream &os) {
ArrayRef<AttrOrTypeParameter> parameters = def.getParameters();
if (parameters.empty()) {
os << "\nSyntax: `!" << def.getDialect().getName() << "."
<< def.getMnemonic() << "`\n";
return;
}
os << "\nSyntax:\n\n```\n!" << def.getDialect().getName() << "."
<< def.getMnemonic() << "<\n";
for (const auto &it : llvm::enumerate(parameters)) {
const AttrOrTypeParameter ¶m = it.value();
os << " " << param.getSyntax();
if (it.index() < (parameters.size() - 1))
os << ",";
os << " # " << param.getName() << "\n";
}
os << ">\n```\n";
}
static void emitAttrOrTypeDefDoc(const AttrOrTypeDef &def, raw_ostream &os) {
os << llvm::formatv("### {0}\n", def.getCppClassName());
// Emit the summary if present.
if (def.hasSummary())
os << "\n" << def.getSummary() << "\n";
// Emit the syntax if present.
if (def.getMnemonic() && !def.hasCustomAssemblyFormat())
emitAttrOrTypeDefAssemblyFormat(def, os);
// Emit the description if present.
if (def.hasDescription()) {
os << "\n";
mlir::tblgen::emitDescription(def.getDescription(), os);
}
// Emit parameter documentation.
ArrayRef<AttrOrTypeParameter> parameters = def.getParameters();
if (!parameters.empty()) {
os << "\n#### Parameters:\n\n";
os << "| Parameter | C++ type | Description |\n"
<< "| :-------: | :-------: | ----------- |\n";
for (const auto &it : parameters) {
auto desc = it.getSummary();
os << "| " << it.getName() << " | `" << it.getCppType() << "` | "
<< (desc ? *desc : "") << " |\n";
}
}
os << "\n";
}
static void emitAttrOrTypeDefDoc(const RecordKeeper &recordKeeper,
raw_ostream &os, StringRef recordTypeName) {
std::vector<llvm::Record *> defs =
recordKeeper.getAllDerivedDefinitions(recordTypeName);
os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
for (const llvm::Record *def : defs)
emitAttrOrTypeDefDoc(AttrOrTypeDef(def), os);
}
//===----------------------------------------------------------------------===//
// Dialect Documentation
//===----------------------------------------------------------------------===//
static void emitDialectDoc(const Dialect &dialect,
ArrayRef<Attribute> attributes,
ArrayRef<AttrDef> attrDefs, ArrayRef<Operator> ops,
ArrayRef<Type> types, ArrayRef<TypeDef> typeDefs,
raw_ostream &os) {
os << "# '" << dialect.getName() << "' Dialect\n\n";
emitIfNotEmpty(dialect.getSummary(), os);
emitIfNotEmpty(dialect.getDescription(), os);
// Generate a TOC marker except if description already contains one.
llvm::Regex r("^[[:space:]]*\\[TOC\\]$", llvm::Regex::RegexFlags::Newline);
if (!r.match(dialect.getDescription()))
os << "[TOC]\n\n";
if (!ops.empty()) {
os << "## Operation definition\n\n";
for (const Operator &op : ops)
emitOpDoc(op, os);
}
if (!attributes.empty()) {
os << "## Attribute constraint definition\n\n";
for (const Attribute &attr : attributes)
emitAttrDoc(attr, os);
}
if (!attrDefs.empty()) {
os << "## Attribute definition\n\n";
for (const AttrDef &def : attrDefs)
emitAttrOrTypeDefDoc(def, os);
}
// TODO: Add link between use and def for types
if (!types.empty()) {
os << "## Type constraint definition\n\n";
for (const Type &type : types)
emitTypeDoc(type, os);
}
if (!typeDefs.empty()) {
os << "## Type definition\n\n";
for (const TypeDef &def : typeDefs)
emitAttrOrTypeDefDoc(def, os);
}
}
static bool emitDialectDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
std::vector<Record *> opDefs = getRequestedOpDefinitions(recordKeeper);
std::vector<Record *> attrDefs =
recordKeeper.getAllDerivedDefinitionsIfDefined("DialectAttr");
std::vector<Record *> typeDefs =
recordKeeper.getAllDerivedDefinitionsIfDefined("DialectType");
std::vector<Record *> typeDefDefs =
recordKeeper.getAllDerivedDefinitionsIfDefined("TypeDef");
std::vector<Record *> attrDefDefs =
recordKeeper.getAllDerivedDefinitionsIfDefined("AttrDef");
llvm::SetVector<Dialect, SmallVector<Dialect>, std::set<Dialect>>
dialectsWithDocs;
llvm::StringMap<std::vector<Attribute>> dialectAttrs;
llvm::StringMap<std::vector<AttrDef>> dialectAttrDefs;
llvm::StringMap<std::vector<Operator>> dialectOps;
llvm::StringMap<std::vector<Type>> dialectTypes;
llvm::StringMap<std::vector<TypeDef>> dialectTypeDefs;
llvm::SmallDenseSet<Record *> seen;
for (Record *attrDef : attrDefDefs) {
AttrDef attr(attrDef);
dialectAttrDefs[attr.getDialect().getName()].push_back(attr);
dialectsWithDocs.insert(attr.getDialect());
seen.insert(attrDef);
}
for (Record *attrDef : attrDefs) {
if (seen.count(attrDef))
continue;
Attribute attr(attrDef);
if (const Dialect &dialect = attr.getDialect()) {
dialectAttrs[dialect.getName()].push_back(attr);
dialectsWithDocs.insert(dialect);
}
}
for (Record *opDef : opDefs) {
Operator op(opDef);
dialectOps[op.getDialect().getName()].push_back(op);
dialectsWithDocs.insert(op.getDialect());
}
for (Record *typeDef : typeDefDefs) {
TypeDef type(typeDef);
dialectTypeDefs[type.getDialect().getName()].push_back(type);
dialectsWithDocs.insert(type.getDialect());
seen.insert(typeDef);
}
for (Record *typeDef : typeDefs) {
if (seen.count(typeDef))
continue;
Type type(typeDef);
if (const Dialect &dialect = type.getDialect()) {
dialectTypes[dialect.getName()].push_back(type);
dialectsWithDocs.insert(dialect);
}
}
Optional<Dialect> dialect =
findDialectToGenerate(dialectsWithDocs.getArrayRef());
if (!dialect)
return true;
os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
StringRef dialectName = dialect->getName();
emitDialectDoc(*dialect, dialectAttrs[dialectName],
dialectAttrDefs[dialectName], dialectOps[dialectName],
dialectTypes[dialectName], dialectTypeDefs[dialectName], os);
return false;
}
//===----------------------------------------------------------------------===//
// Gen Registration
//===----------------------------------------------------------------------===//
static mlir::GenRegistration
genAttrRegister("gen-attrdef-doc",
"Generate dialect attribute documentation",
[](const RecordKeeper &records, raw_ostream &os) {
emitAttrOrTypeDefDoc(records, os, "AttrDef");
return false;
});
static mlir::GenRegistration
genOpRegister("gen-op-doc", "Generate dialect documentation",
[](const RecordKeeper &records, raw_ostream &os) {
emitOpDoc(records, os);
return false;
});
static mlir::GenRegistration
genTypeRegister("gen-typedef-doc", "Generate dialect type documentation",
[](const RecordKeeper &records, raw_ostream &os) {
emitAttrOrTypeDefDoc(records, os, "TypeDef");
return false;
});
static mlir::GenRegistration
genRegister("gen-dialect-doc", "Generate dialect documentation",
[](const RecordKeeper &records, raw_ostream &os) {
return emitDialectDoc(records, os);
});