1//===- BytecodeDialectGen.cpp - Dialect bytecode read/writer gen ---------===//
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#include "CppGenUtilities.h"
10#include "mlir/Support/IndentedOstream.h"
11#include "mlir/TableGen/GenInfo.h"
12#include "llvm/ADT/MapVector.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/SmallVectorExtras.h"
15#include "llvm/Support/CommandLine.h"
16#include "llvm/Support/FormatVariadic.h"
17#include "llvm/TableGen/Error.h"
18#include "llvm/TableGen/Record.h"
19#include <regex>
20
21using namespace llvm;
22
23static cl::OptionCategory dialectGenCat("Options for -gen-bytecode");
24static cl::opt<std::string>
25 selectedBcDialect("bytecode-dialect", cl::desc("The dialect to gen for"),
26 cl::cat(dialectGenCat), cl::CommaSeparated);
27
28namespace {
29/// The kind of entity, attribute or type, that readers and writers are
30/// generated for.
31struct AttrOrTypeKind {
32 /// Lowercase name, e.g. "attribute". Capitalized, it names the generated
33 /// entry points, e.g. `readAttribute` and `writeAttribute`.
34 StringRef name;
35 /// Fully qualified C++ class, e.g. "::mlir::Attribute".
36 StringRef cppClass;
37};
38
39/// Helper class to generate C++ bytecode parser helpers.
40class Generator {
41public:
42 Generator(raw_ostream &output) : output(output) {}
43
44 /// Returns whether successfully emitted attribute/type parsers.
45 void emitParse(const AttrOrTypeKind &kind, const Record &x);
46
47 /// Returns whether successfully emitted attribute/type printers.
48 void emitPrint(const AttrOrTypeKind &kind, StringRef type,
49 ArrayRef<std::pair<int64_t, const Record *>> vec);
50
51 /// Emits parse dispatch table.
52 void emitParseDispatch(const AttrOrTypeKind &kind,
53 ArrayRef<const Record *> vec);
54
55 /// Emits print dispatch table.
56 void emitPrintDispatch(const AttrOrTypeKind &kind, ArrayRef<std::string> vec);
57
58private:
59 /// Emits parse calls to construct given kind.
60 void emitParseHelper(StringRef returnType, StringRef builder,
61 ArrayRef<const Init *> args,
62 ArrayRef<std::string> argNames, StringRef failure,
63 mlir::raw_indented_ostream &ios);
64
65 /// Emits print instructions.
66 void emitPrintHelper(const Record *memberRec, const AttrOrTypeKind &kind,
67 StringRef parent, StringRef name,
68 mlir::raw_indented_ostream &ios);
69
70 raw_ostream &output;
71};
72} // namespace
73
74/// Helper to replace set of from strings to target in `s`.
75/// Assumed: non-overlapping replacements.
76static std::string format(StringRef templ,
77 std::map<std::string, std::string> &&map) {
78 std::string s = templ.str();
79 for (const auto &[from, to] : map)
80 // All replacements start with $, don't treat as anchor.
81 s = std::regex_replace(s: s, e: std::regex("\\" + from), fmt: to);
82 return s;
83}
84
85/// Return string with first character capitalized.
86static std::string capitalize(StringRef str) {
87 return ((Twine)toUpper(x: str[0]) + str.drop_front()).str();
88}
89
90/// Return the C++ type for the given record.
91static std::string getCType(const Record *def) {
92 std::string format = "{0}";
93 if (def->isSubClassOf(Name: "Array")) {
94 def = def->getValueAsDef(FieldName: "elemT");
95 format = "::llvm::SmallVector<{0}>";
96 }
97
98 StringRef cType = def->getValueAsString(FieldName: "cType");
99 if (cType.empty()) {
100 if (def->isAnonymous())
101 PrintFatalError(ErrorLoc: def->getLoc(), Msg: "Unable to determine cType");
102
103 return formatv(Fmt: format.c_str(), Vals: def->getName().str());
104 }
105 return formatv(Fmt: format.c_str(), Vals: cType.str());
106}
107
108void Generator::emitParseDispatch(const AttrOrTypeKind &kind,
109 ArrayRef<const Record *> vec) {
110 mlir::raw_indented_ostream os(output);
111 char const *head = R"(static {0} read{1}(::mlir::MLIRContext* context, )"
112 R"(::mlir::DialectBytecodeReader &reader))";
113 os << formatv(Fmt: head, Vals: kind.cppClass, Vals: capitalize(str: kind.name));
114 auto funScope = os.scope(open: " {\n", close: "}\n\n");
115
116 if (vec.empty()) {
117 os << "return reader.emitError() << \"unknown " << kind.name << "\", "
118 << kind.cppClass << "();\n";
119 return;
120 }
121
122 os << "uint64_t kind;\n";
123 os << "if (::mlir::failed(reader.readVarInt(kind)))\n"
124 << " return " << kind.cppClass << "();\n";
125 os << "switch (kind) ";
126 {
127 auto switchScope = os.scope(open: "{\n", close: "}\n");
128 for (const auto &it : llvm::enumerate(First&: vec)) {
129 if (it.value()->getName() == "ReservedOrDead")
130 continue;
131
132 os << formatv(Fmt: "case {1}:\n return read{0}(context, reader);\n",
133 Vals: it.value()->getName(), Vals: it.index());
134 }
135 os << "default:\n"
136 << " reader.emitError() << \"unknown " << kind.name
137 << " code: \" << kind;\n"
138 << " return " << kind.cppClass << "();\n";
139 }
140 os << "return " << kind.cppClass << "();\n";
141}
142
143void Generator::emitParse(const AttrOrTypeKind &kind, const Record &x) {
144 if (x.getNameInitAsString() == "ReservedOrDead")
145 return;
146
147 char const *head = R"(static {0} read{1}(::mlir::MLIRContext* context, )"
148 R"(::mlir::DialectBytecodeReader &reader) )";
149 mlir::raw_indented_ostream os(output);
150 std::string returnType = getCType(def: &x);
151 os << formatv(Fmt: head, Vals: kind.cppClass, Vals: x.getName());
152 const DagInit *members = x.getValueAsDag(FieldName: "members");
153 SmallVector<std::string> argNames =
154 llvm::map_to_vector(C: members->getArgNames(), F: [](const StringInit *init) {
155 return init->getAsUnquotedString();
156 });
157 StringRef builder = x.getValueAsString(FieldName: "cBuilder").trim();
158 emitParseHelper(returnType, builder, args: members->getArgs(), argNames,
159 failure: returnType + "()", ios&: os);
160 os << "\n\n";
161}
162
163static void printParseConditional(mlir::raw_indented_ostream &ios,
164 ArrayRef<const Init *> args,
165 ArrayRef<std::string> argNames) {
166 ios << "if ";
167 auto parenScope = ios.scope(open: "(", close: ") {");
168 ios.indent();
169
170 auto listHelperName = [](StringRef name) {
171 return formatv(Fmt: "read{0}", Vals: capitalize(str: name));
172 };
173
174 auto parsedArgs = llvm::filter_to_vector(C&: args, Pred: [](const Init *const attr) {
175 const Record *def = cast<DefInit>(Val: attr)->getDef();
176 if (def->isSubClassOf(Name: "Array"))
177 return true;
178 return !def->getValueAsString(FieldName: "cParser").empty();
179 });
180
181 interleave(
182 c: zip(t&: parsedArgs, u&: argNames),
183 each_fn: [&](std::tuple<const Init *&, const std::string &> it) {
184 const Record *attr = cast<DefInit>(Val: std::get<0>(t&: it))->getDef();
185 std::string parser;
186 if (auto optParser = attr->getValueAsOptionalString(FieldName: "cParser")) {
187 parser = *optParser;
188 } else if (attr->isSubClassOf(Name: "Array")) {
189 const Record *def = attr->getValueAsDef(FieldName: "elemT");
190 bool composite = def->isSubClassOf(Name: "CompositeBytecode");
191 if (!composite && def->isSubClassOf(Name: "AttributeKind"))
192 parser = "::mlir::succeeded($_reader.readAttributes($_var))";
193 else if (!composite && def->isSubClassOf(Name: "TypeKind"))
194 parser = "::mlir::succeeded($_reader.readTypes($_var))";
195 else if (attr->isSubClassOf(Name: "ArrayWithKnownSize")) {
196 std::string sizeRef = attr->getValueAsString(FieldName: "knownSizeRef").str();
197 parser =
198 ("::mlir::succeeded($_reader.readListWithKnownSize($_var, " +
199 sizeRef + ", " + listHelperName(std::get<1>(t&: it)) + "))")
200 .str();
201 } else
202 parser = ("::mlir::succeeded($_reader.readList($_var, " +
203 listHelperName(std::get<1>(t&: it)) + "))")
204 .str();
205 } else {
206 PrintFatalError(ErrorLoc: attr->getLoc(), Msg: "No parser specified");
207 }
208 std::string type = getCType(def: attr);
209 ios << format(templ: parser, map: {{"$_reader", "reader"},
210 {"$_resultType", type},
211 {"$_var", std::get<1>(t&: it)}});
212 },
213 between_fn: [&]() { ios << " &&\n"; });
214}
215
216void Generator::emitParseHelper(StringRef returnType, StringRef builder,
217 ArrayRef<const Init *> args,
218 ArrayRef<std::string> argNames,
219 StringRef failure,
220 mlir::raw_indented_ostream &ios) {
221 auto funScope = ios.scope(open: "{\n", close: "}");
222
223 if (args.empty()) {
224 ios << formatv(
225 Fmt: "return ::mlir::getChecked<{0}>([&]() {{ return reader.emitError(); "
226 "}, context);\n",
227 Vals&: returnType);
228 return;
229 }
230
231 // Print decls.
232 std::string lastCType = "";
233 for (auto [arg, name] : zip(t&: args, u&: argNames)) {
234 const DefInit *first = dyn_cast<DefInit>(Val: arg);
235 if (!first)
236 PrintFatalError(Msg: "Unexpected type for " + name);
237 const Record *def = first->getDef();
238
239 // Create variable decls, if there are a block of same type then create
240 // comma separated list of them.
241 std::string cType = getCType(def);
242 if (lastCType == cType) {
243 ios << ", ";
244 } else {
245 if (!lastCType.empty())
246 ios << ";\n";
247 ios << cType << " ";
248 }
249 ios << name;
250 lastCType = cType;
251 }
252 ios << ";\n";
253
254 // Returns the name of the helper used in list parsing. E.g., the name of the
255 // lambda passed to array parsing.
256 auto listHelperName = [](StringRef name) {
257 return formatv(Fmt: "read{0}", Vals: capitalize(str: name));
258 };
259
260 // Emit list helper functions.
261 for (auto [arg, name] : zip(t&: args, u&: argNames)) {
262 const Record *attr = cast<DefInit>(Val: arg)->getDef();
263 if (!attr->isSubClassOf(Name: "Array"))
264 continue;
265
266 // TODO: Dedupe readers.
267 const Record *def = attr->getValueAsDef(FieldName: "elemT");
268 if (!def->isSubClassOf(Name: "CompositeBytecode") &&
269 (def->isSubClassOf(Name: "AttributeKind") || def->isSubClassOf(Name: "TypeKind")))
270 continue;
271
272 std::string returnType = getCType(def);
273 ios << "auto " << listHelperName(name) << " = [&]() -> ::mlir::FailureOr<"
274 << returnType << "> ";
275 SmallVector<const Init *> args;
276 SmallVector<std::string> argNames;
277 if (def->isSubClassOf(Name: "CompositeBytecode")) {
278 const DagInit *members = def->getValueAsDag(FieldName: "members");
279 args = llvm::to_vector(Range: members->getArgs());
280 argNames = llvm::map_to_vector(
281 C: members->getArgNames(),
282 F: [](const StringInit *init) { return init->getAsUnquotedString(); });
283 } else {
284 args = {def->getDefInit()};
285 argNames = {"temp"};
286 }
287 StringRef builder = def->getValueAsString(FieldName: "cBuilder");
288 emitParseHelper(returnType, builder, args, argNames, failure: "::mlir::failure()",
289 ios);
290 ios << ";\n";
291 }
292
293 // Print parse conditional.
294 printParseConditional(ios, args, argNames);
295
296 // Compute args to pass to create method.
297 auto passedArgs = llvm::filter_to_vector(
298 C&: argNames, Pred: [](StringRef str) { return !str.starts_with(Prefix: "_"); });
299 std::string argStr;
300 raw_string_ostream argStream(argStr);
301 interleaveComma(c: passedArgs, os&: argStream,
302 each_fn: [&](const std::string &str) { argStream << str; });
303 // Return the invoked constructor.
304 ios << "\nreturn "
305 << format(templ: builder, map: {{"$_resultType", returnType.str()},
306 {"$_args", argStream.str()}})
307 << ";\n";
308 ios.unindent();
309
310 // TODO: Emit error in debug.
311 // This assumes the result types in error case can always be empty
312 // constructed.
313 ios << "}\nreturn " << failure << ";\n";
314}
315
316void Generator::emitPrint(const AttrOrTypeKind &kind, StringRef type,
317 ArrayRef<std::pair<int64_t, const Record *>> vec) {
318 if (type == "ReservedOrDead")
319 return;
320
321 char const *head = R"(static ::llvm::LogicalResult write({0} {1}, )"
322 R"(::mlir::DialectBytecodeWriter &writer) )";
323 mlir::raw_indented_ostream os(output);
324 os << formatv(Fmt: head, Vals&: type, Vals: kind.name);
325 auto funScope = os.scope(open: "{\n", close: "}\n\n");
326
327 // Check that predicates specified if multiple bytecode instances.
328 for (const Record *rec : make_second_range(c&: vec)) {
329 StringRef pred = rec->getValueAsString(FieldName: "printerPredicate");
330 if (vec.size() > 1 && pred.empty()) {
331 for (auto [index, rec] : vec) {
332 (void)index;
333 StringRef pred = rec->getValueAsString(FieldName: "printerPredicate");
334 if (vec.size() > 1 && pred.empty())
335 PrintError(ErrorLoc: rec->getLoc(),
336 Msg: "Requires printing predicate given common cType");
337 }
338 PrintFatalError(Msg: "Unspecified for shared cType " + type);
339 }
340 }
341
342 for (auto [index, rec] : vec) {
343 StringRef pred = rec->getValueAsString(FieldName: "printerPredicate");
344 if (!pred.empty()) {
345 os << "if (" << format(templ: pred, map: {{"$_val", kind.name.str()}}) << ") {\n";
346 os.indent();
347 }
348
349 os << "writer.writeVarInt(/* " << rec->getName() << " */ " << index
350 << ");\n";
351
352 auto *members = rec->getValueAsDag(FieldName: "members");
353 for (auto [arg, name] :
354 llvm::zip(t: members->getArgs(), u: members->getArgNames())) {
355 const DefInit *def = dyn_cast<DefInit>(Val: arg);
356 assert(def);
357 const Record *memberRec = def->getDef();
358 emitPrintHelper(memberRec, kind, parent: kind.name, name: name->getAsUnquotedString(),
359 ios&: os);
360 }
361
362 if (!pred.empty()) {
363 os << "return ::mlir::success();\n";
364 os.unindent();
365 os << "}\n";
366 } else {
367 os << "return ::mlir::success();\n";
368 }
369 }
370 os << "return ::mlir::failure();\n";
371}
372
373void Generator::emitPrintHelper(const Record *memberRec,
374 const AttrOrTypeKind &kind, StringRef parent,
375 StringRef name,
376 mlir::raw_indented_ostream &ios) {
377 std::string getter;
378 if (auto cGetter = memberRec->getValueAsOptionalString(FieldName: "cGetter");
379 cGetter && !cGetter->empty()) {
380 getter = format(
381 templ: *cGetter,
382 map: {{"$_attrType", parent.str()},
383 {"$_member", name.str()},
384 {"$_getMember", "get" + convertToCamelFromSnakeCase(input: name, capitalizeFirst: true)}});
385 } else {
386 getter =
387 formatv(Fmt: "{0}.get{1}()", Vals&: parent, Vals: convertToCamelFromSnakeCase(input: name, capitalizeFirst: true))
388 .str();
389 }
390
391 if (memberRec->isSubClassOf(Name: "Array")) {
392 const Record *def = memberRec->getValueAsDef(FieldName: "elemT");
393 if (!def->isSubClassOf(Name: "CompositeBytecode")) {
394 if (def->isSubClassOf(Name: "AttributeKind")) {
395 ios << "writer.writeAttributes(" << getter << ");\n";
396 return;
397 }
398 if (def->isSubClassOf(Name: "TypeKind")) {
399 ios << "writer.writeTypes(" << getter << ");\n";
400 return;
401 }
402 }
403 std::string returnType = getCType(def);
404 std::string nestedName = kind.name.str();
405 StringRef writeMethod = memberRec->isSubClassOf(Name: "ArrayWithKnownSize")
406 ? "writer.writeListWithKnownSize("
407 : "writer.writeList(";
408 ios << writeMethod << getter << ", [&](" << returnType << " " << nestedName
409 << ") ";
410 auto lambdaScope = ios.scope(open: "{\n", close: "});\n");
411 return emitPrintHelper(memberRec: def, kind, parent: nestedName, name: nestedName, ios);
412 }
413 if (memberRec->isSubClassOf(Name: "CompositeBytecode")) {
414 auto *members = memberRec->getValueAsDag(FieldName: "members");
415 for (auto [arg, argName] :
416 zip(t: members->getArgs(), u: members->getArgNames())) {
417 const DefInit *def = dyn_cast<DefInit>(Val: arg);
418 assert(def);
419 emitPrintHelper(memberRec: def->getDef(), kind, parent,
420 name: argName->getAsUnquotedString(), ios);
421 }
422 }
423
424 if (std::string printer = memberRec->getValueAsString(FieldName: "cPrinter").str();
425 !printer.empty())
426 ios << format(templ: printer, map: {{"$_writer", "writer"},
427 {"$_name", kind.name.str()},
428 {"$_getter", getter}})
429 << ";\n";
430}
431
432void Generator::emitPrintDispatch(const AttrOrTypeKind &kind,
433 ArrayRef<std::string> vec) {
434 mlir::raw_indented_ostream os(output);
435 char const *head = R"(static ::llvm::LogicalResult write{0}({1} {2},
436 ::mlir::DialectBytecodeWriter &writer))";
437 os << formatv(Fmt: head, Vals: capitalize(str: kind.name), Vals: kind.cppClass, Vals: kind.name);
438 auto funScope = os.scope(open: " {\n", close: "}\n\n");
439
440 os << "return ::llvm::TypeSwitch<" << kind.cppClass
441 << ", ::llvm::LogicalResult>(" << kind.name << ")";
442 auto switchScope = os.scope(open: "", close: "");
443 for (StringRef type : vec) {
444 if (type == "ReservedOrDead")
445 continue;
446
447 os << "\n.Case([&](" << type << " t)";
448 auto caseScope = os.scope(open: " {\n", close: "})");
449 os << "return write(t, writer);\n";
450 }
451 os << "\n.Default([&](" << kind.cppClass
452 << ") { return ::mlir::failure(); });\n";
453}
454
455namespace {
456/// Container of Attribute or Type for Dialect.
457struct AttrOrType {
458 std::vector<const Record *> attr, type;
459};
460} // namespace
461
462/// Headers declaring the C++ constructs used by the generated readers and
463/// writers.
464static constexpr StringRef bytecodeIncludes[] = {
465 // `::llvm::TypeSwitch`.
466 "llvm/ADT/TypeSwitch.h",
467 // `::mlir::DialectBytecodeReader`, `::mlir::DialectBytecodeWriter`,
468 // `::mlir::getChecked`, `::mlir::readResourceHandle`.
469 "mlir/Bytecode/BytecodeImplementation.h",
470};
471
472static bool emitBCRW(const RecordKeeper &records, raw_ostream &os) {
473 MapVector<StringRef, AttrOrType> dialectAttrOrType;
474 for (const Record *it :
475 records.getAllDerivedDefinitions(ClassName: "DialectAttributes")) {
476 if (!selectedBcDialect.empty() &&
477 it->getValueAsString(FieldName: "dialect") != selectedBcDialect)
478 continue;
479 dialectAttrOrType[it->getValueAsString(FieldName: "dialect")].attr =
480 it->getValueAsListOfDefs(FieldName: "elems");
481 }
482 for (const Record *it : records.getAllDerivedDefinitions(ClassName: "DialectTypes")) {
483 if (!selectedBcDialect.empty() &&
484 it->getValueAsString(FieldName: "dialect") != selectedBcDialect)
485 continue;
486 dialectAttrOrType[it->getValueAsString(FieldName: "dialect")].type =
487 it->getValueAsListOfDefs(FieldName: "elems");
488 }
489
490 if (dialectAttrOrType.size() != 1)
491 PrintFatalError(Msg: "Single dialect per invocation required (either only "
492 "one in input file or specified via dialect option)");
493
494 auto it = dialectAttrOrType.front();
495 mlir::tblgen::emitIncludes(os, headers: bytecodeIncludes);
496 Generator gen(os);
497
498 const std::vector<const Record *> *vecs[] = {&it.second.attr,
499 &it.second.type};
500 const AttrOrTypeKind kinds[] = {{.name: "attribute", .cppClass: "::mlir::Attribute"},
501 {.name: "type", .cppClass: "::mlir::Type"}};
502 for (auto [vec, kind] : zip(t&: vecs, u: kinds)) {
503 // Handle Attribute/Type emission.
504 std::map<std::string, std::vector<std::pair<int64_t, const Record *>>>
505 perType;
506 for (auto kt : llvm::enumerate(First: *vec))
507 perType[getCType(def: kt.value())].emplace_back(args: kt.index(), args: kt.value());
508 for (const auto &jt : perType) {
509 for (auto kt : jt.second)
510 gen.emitParse(kind, x: *std::get<1>(in&: kt));
511 gen.emitPrint(kind, type: jt.first, vec: jt.second);
512 }
513 gen.emitParseDispatch(kind, vec: *vec);
514
515 SmallVector<std::string> types;
516 for (const auto &it : perType)
517 types.push_back(Elt: it.first);
518 gen.emitPrintDispatch(kind, vec: types);
519 }
520
521 return false;
522}
523
524static mlir::GenRegistration
525 genBCRW("gen-bytecode", "Generate dialect bytecode readers/writers",
526 [](const RecordKeeper &records, raw_ostream &os) {
527 return emitBCRW(records, os);
528 });
529