1//===- SPIRVSerializationGen.cpp - SPIR-V serialization utility 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// SPIRVSerializationGen generates common utility functions for SPIR-V
10// serialization.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/TableGen/Attribute.h"
15#include "mlir/TableGen/CodeGenHelpers.h"
16#include "mlir/TableGen/EnumInfo.h"
17#include "mlir/TableGen/Format.h"
18#include "mlir/TableGen/GenInfo.h"
19#include "mlir/TableGen/Operator.h"
20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/Sequence.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringExtras.h"
24#include "llvm/ADT/StringMap.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/StringSet.h"
27#include "llvm/Support/FormatVariadic.h"
28#include "llvm/Support/raw_ostream.h"
29#include "llvm/TableGen/Error.h"
30#include "llvm/TableGen/Record.h"
31#include "llvm/TableGen/TableGenBackend.h"
32
33#include <list>
34#include <optional>
35
36using llvm::ArrayRef;
37using llvm::cast;
38using llvm::formatv;
39using llvm::isa;
40using llvm::raw_ostream;
41using llvm::raw_string_ostream;
42using llvm::Record;
43using llvm::RecordKeeper;
44using llvm::SmallVector;
45using llvm::SMLoc;
46using llvm::StringMap;
47using llvm::StringRef;
48using mlir::tblgen::Attribute;
49using mlir::tblgen::EnumCase;
50using mlir::tblgen::EnumInfo;
51using mlir::tblgen::NamedAttribute;
52using mlir::tblgen::NamedTypeConstraint;
53using mlir::tblgen::Operator;
54
55//===----------------------------------------------------------------------===//
56// Availability Wrapper Class
57//===----------------------------------------------------------------------===//
58
59namespace {
60// Wrapper class with helper methods for accessing availability defined in
61// TableGen.
62class Availability {
63public:
64 explicit Availability(const Record *def);
65
66 // Returns the name of the direct TableGen class for this availability
67 // instance.
68 StringRef getClass() const;
69
70 // Returns the generated C++ interface's class namespace.
71 StringRef getInterfaceClassNamespace() const;
72
73 // Returns the generated C++ interface's class name.
74 StringRef getInterfaceClassName() const;
75
76 // Returns the generated C++ interface's description.
77 StringRef getInterfaceDescription() const;
78
79 // Returns the name of the query function insided the generated C++ interface.
80 StringRef getQueryFnName() const;
81
82 // Returns the return type of the query function insided the generated C++
83 // interface.
84 StringRef getQueryFnRetType() const;
85
86 // Returns the code for merging availability requirements.
87 StringRef getMergeActionCode() const;
88
89 // Returns the initializer expression for initializing the final availability
90 // requirements.
91 StringRef getMergeInitializer() const;
92
93 // Returns the C++ type for an availability instance.
94 StringRef getMergeInstanceType() const;
95
96 // Returns the C++ statements for preparing availability instance.
97 StringRef getMergeInstancePreparation() const;
98
99 // Returns the concrete availability instance carried in this case.
100 StringRef getMergeInstance() const;
101
102 // Returns the underlying LLVM TableGen Record.
103 const Record *getDef() const { return def; }
104
105private:
106 // The TableGen definition of this availability.
107 const Record *def;
108};
109} // namespace
110
111Availability::Availability(const Record *def) : def(def) {
112 assert(def->isSubClassOf("Availability") &&
113 "must be subclass of TableGen 'Availability' class");
114}
115
116StringRef Availability::getClass() const {
117 if (def->getDirectSuperClasses().size() != 1) {
118 PrintFatalError(ErrorLoc: def->getLoc(),
119 Msg: "expected to only have one direct superclass");
120 }
121 const Record *parentClass = def->getDirectSuperClasses().front().first;
122 return parentClass->getName();
123}
124
125StringRef Availability::getInterfaceClassNamespace() const {
126 return def->getValueAsString(FieldName: "cppNamespace");
127}
128
129StringRef Availability::getInterfaceClassName() const {
130 return def->getValueAsString(FieldName: "interfaceName");
131}
132
133StringRef Availability::getInterfaceDescription() const {
134 return def->getValueAsString(FieldName: "interfaceDescription");
135}
136
137StringRef Availability::getQueryFnRetType() const {
138 return def->getValueAsString(FieldName: "queryFnRetType");
139}
140
141StringRef Availability::getQueryFnName() const {
142 return def->getValueAsString(FieldName: "queryFnName");
143}
144
145StringRef Availability::getMergeActionCode() const {
146 return def->getValueAsString(FieldName: "mergeAction");
147}
148
149StringRef Availability::getMergeInitializer() const {
150 return def->getValueAsString(FieldName: "initializer");
151}
152
153StringRef Availability::getMergeInstanceType() const {
154 return def->getValueAsString(FieldName: "instanceType");
155}
156
157StringRef Availability::getMergeInstancePreparation() const {
158 return def->getValueAsString(FieldName: "instancePreparation");
159}
160
161StringRef Availability::getMergeInstance() const {
162 return def->getValueAsString(FieldName: "instance");
163}
164
165// Returns the availability spec of the given `def`.
166std::vector<Availability> getAvailabilities(const Record &def) {
167 std::vector<Availability> availabilities;
168
169 if (def.getValue(Name: "availability")) {
170 std::vector<const Record *> availDefs =
171 def.getValueAsListOfDefs(FieldName: "availability");
172 availabilities.reserve(n: availDefs.size());
173 for (const Record *avail : availDefs)
174 availabilities.emplace_back(args&: avail);
175 }
176
177 return availabilities;
178}
179
180//===----------------------------------------------------------------------===//
181// Availability Interface Definitions AutoGen
182//===----------------------------------------------------------------------===//
183
184static void emitInterfaceDef(const Availability &availability,
185 raw_ostream &os) {
186
187 os << availability.getQueryFnRetType() << " ";
188
189 StringRef cppNamespace = availability.getInterfaceClassNamespace();
190 cppNamespace.consume_front(Prefix: "::");
191 if (!cppNamespace.empty())
192 os << cppNamespace << "::";
193
194 StringRef methodName = availability.getQueryFnName();
195 os << availability.getInterfaceClassName() << "::" << methodName << "() {\n"
196 << " return getImpl()->" << methodName << "(getImpl(), getOperation());\n"
197 << "}\n";
198}
199
200static bool emitInterfaceDefs(const RecordKeeper &records, raw_ostream &os) {
201 llvm::emitSourceFileHeader(Desc: "Availability Interface Definitions", OS&: os, Record: records);
202
203 auto defs = records.getAllDerivedDefinitions(ClassName: "Availability");
204 SmallVector<const Record *, 1> handledClasses;
205 for (const Record *def : defs) {
206 if (def->getDirectSuperClasses().size() != 1) {
207 PrintFatalError(ErrorLoc: def->getLoc(),
208 Msg: "expected to only have one direct superclass");
209 }
210 const Record *parent = def->getDirectSuperClasses().front().first;
211 if (llvm::is_contained(Range&: handledClasses, Element: parent))
212 continue;
213
214 Availability availability(def);
215 emitInterfaceDef(availability, os);
216 handledClasses.push_back(Elt: parent);
217 }
218 return false;
219}
220
221//===----------------------------------------------------------------------===//
222// Availability Interface Declarations AutoGen
223//===----------------------------------------------------------------------===//
224
225static void emitConceptDecl(const Availability &availability, raw_ostream &os) {
226 os << " class Concept {\n"
227 << " public:\n"
228 << " virtual ~Concept() = default;\n"
229 << " virtual " << availability.getQueryFnRetType() << " "
230 << availability.getQueryFnName()
231 << "(const Concept *impl, Operation *tblgen_opaque_op) const = 0;\n"
232 << " };\n";
233}
234
235static void emitModelDecl(const Availability &availability, raw_ostream &os) {
236 for (const char *modelClass : {"Model", "FallbackModel"}) {
237 os << " template<typename ConcreteOp>\n";
238 os << " class " << modelClass << " : public Concept {\n"
239 << " public:\n"
240 << " using Interface = " << availability.getInterfaceClassName()
241 << ";\n"
242 << " " << availability.getQueryFnRetType() << " "
243 << availability.getQueryFnName()
244 << "(const Concept *impl, Operation *tblgen_opaque_op) const final {\n"
245 << " auto op = llvm::cast<ConcreteOp>(tblgen_opaque_op);\n"
246 << " (void)op;\n"
247 // Forward to the method on the concrete operation type.
248 << " return op." << availability.getQueryFnName() << "();\n"
249 << " }\n"
250 << " };\n";
251 }
252 os << " template<typename ConcreteModel, typename ConcreteOp>\n";
253 os << " class ExternalModel : public FallbackModel<ConcreteOp> {};\n";
254}
255
256static void emitInterfaceDecl(const Availability &availability,
257 raw_ostream &os) {
258 StringRef interfaceName = availability.getInterfaceClassName();
259 std::string interfaceTraitsName =
260 std::string(formatv(Fmt: "{0}Traits", Vals&: interfaceName));
261
262 llvm::NamespaceEmitter nsEmitter(os,
263 availability.getInterfaceClassNamespace());
264 os << "class " << interfaceName << ";\n\n";
265
266 // Emit the traits struct containing the concept and model declarations.
267 os << "namespace detail {\n"
268 << "struct " << interfaceTraitsName << " {\n";
269 emitConceptDecl(availability, os);
270 os << '\n';
271 emitModelDecl(availability, os);
272 os << "};\n} // namespace detail\n\n";
273
274 // Emit the main interface class declaration.
275 os << "/*\n" << availability.getInterfaceDescription().trim() << "\n*/\n";
276 os << llvm::formatv(Fmt: "class {0} : public OpInterface<{1}, detail::{2}> {\n"
277 "public:\n"
278 " using OpInterface<{1}, detail::{2}>::OpInterface;\n",
279 Vals&: interfaceName, Vals&: interfaceName, Vals&: interfaceTraitsName);
280
281 // Emit query function declaration.
282 os << " " << availability.getQueryFnRetType() << " "
283 << availability.getQueryFnName() << "();\n";
284 os << "};\n\n";
285}
286
287static bool emitInterfaceDecls(const RecordKeeper &records, raw_ostream &os) {
288 llvm::emitSourceFileHeader(Desc: "Availability Interface Declarations", OS&: os,
289 Record: records);
290
291 auto defs = records.getAllDerivedDefinitions(ClassName: "Availability");
292 SmallVector<const Record *, 4> handledClasses;
293 for (const Record *def : defs) {
294 if (def->getDirectSuperClasses().size() != 1) {
295 PrintFatalError(ErrorLoc: def->getLoc(),
296 Msg: "expected to only have one direct superclass");
297 }
298 const Record *parent = def->getDirectSuperClasses().front().first;
299 if (llvm::is_contained(Range&: handledClasses, Element: parent))
300 continue;
301
302 Availability avail(def);
303 emitInterfaceDecl(availability: avail, os);
304 handledClasses.push_back(Elt: parent);
305 }
306 return false;
307}
308
309//===----------------------------------------------------------------------===//
310// Availability Interface Hook Registration
311//===----------------------------------------------------------------------===//
312
313// Registers the operation interface generator to mlir-tblgen.
314static mlir::GenRegistration
315 genInterfaceDecls("gen-avail-interface-decls",
316 "Generate availability interface declarations",
317 [](const RecordKeeper &records, raw_ostream &os) {
318 return emitInterfaceDecls(records, os);
319 });
320
321// Registers the operation interface generator to mlir-tblgen.
322static mlir::GenRegistration
323 genInterfaceDefs("gen-avail-interface-defs",
324 "Generate op interface definitions",
325 [](const RecordKeeper &records, raw_ostream &os) {
326 return emitInterfaceDefs(records, os);
327 });
328
329//===----------------------------------------------------------------------===//
330// Enum Availability Query AutoGen
331//===----------------------------------------------------------------------===//
332
333static void emitAvailabilityQueryForIntEnum(const Record &enumDef,
334 raw_ostream &os) {
335 EnumInfo enumInfo(enumDef);
336 StringRef enumName = enumInfo.getEnumClassName();
337 std::vector<EnumCase> enumerants = enumInfo.getAllCases();
338
339 // Mapping from availability class name to (enumerant, availability
340 // specification) pairs.
341 llvm::StringMap<llvm::SmallVector<std::pair<EnumCase, Availability>, 1>>
342 classCaseMap;
343
344 // Place all availability specifications to their corresponding
345 // availability classes.
346 for (const EnumCase &enumerant : enumerants)
347 for (const Availability &avail : getAvailabilities(def: enumerant.getDef()))
348 classCaseMap[avail.getClass()].push_back(Elt: {enumerant, avail});
349
350 for (const auto &classCasePair : classCaseMap) {
351 Availability avail = classCasePair.getValue().front().second;
352
353 os << formatv(Fmt: "std::optional<{0}> {1}({2} value) {{\n",
354 Vals: avail.getMergeInstanceType(), Vals: avail.getQueryFnName(),
355 Vals&: enumName);
356
357 os << " switch (value) {\n";
358 for (const auto &caseSpecPair : classCasePair.getValue()) {
359 EnumCase enumerant = caseSpecPair.first;
360 Availability avail = caseSpecPair.second;
361 os << formatv(Fmt: " case {0}::{1}: { {2} return {3}({4}); }\n", Vals&: enumName,
362 Vals: enumerant.getSymbol(), Vals: avail.getMergeInstancePreparation(),
363 Vals: avail.getMergeInstanceType(), Vals: avail.getMergeInstance());
364 }
365 // Only emit default if uncovered cases.
366 if (classCasePair.getValue().size() < enumInfo.getAllCases().size())
367 os << " default: break;\n";
368 os << " }\n"
369 << " return std::nullopt;\n"
370 << "}\n";
371 }
372}
373
374static void emitAvailabilityQueryForBitEnum(const Record &enumDef,
375 raw_ostream &os) {
376 EnumInfo enumInfo(enumDef);
377 StringRef enumName = enumInfo.getEnumClassName();
378 std::string underlyingType = std::string(enumInfo.getUnderlyingType());
379 std::vector<EnumCase> enumerants = enumInfo.getAllCases();
380
381 // Mapping from availability class name to (enumerant, availability
382 // specification) pairs.
383 llvm::StringMap<llvm::SmallVector<std::pair<EnumCase, Availability>, 1>>
384 classCaseMap;
385
386 // Place all availability specifications to their corresponding
387 // availability classes.
388 for (const EnumCase &enumerant : enumerants)
389 for (const Availability &avail : getAvailabilities(def: enumerant.getDef()))
390 classCaseMap[avail.getClass()].push_back(Elt: {enumerant, avail});
391
392 for (const auto &classCasePair : classCaseMap) {
393 Availability avail = classCasePair.getValue().front().second;
394
395 os << formatv(Fmt: "std::optional<{0}> {1}({2} value) {{\n",
396 Vals: avail.getMergeInstanceType(), Vals: avail.getQueryFnName(),
397 Vals&: enumName);
398
399 os << formatv(Fmt: " assert(::llvm::popcount(static_cast<{0}>(value)) <= 1"
400 " && \"cannot have more than one bit set\");\n",
401 Vals&: underlyingType);
402
403 os << " switch (value) {\n";
404 for (const auto &caseSpecPair : classCasePair.getValue()) {
405 EnumCase enumerant = caseSpecPair.first;
406 Availability avail = caseSpecPair.second;
407 os << formatv(Fmt: " case {0}::{1}: { {2} return {3}({4}); }\n", Vals&: enumName,
408 Vals: enumerant.getSymbol(), Vals: avail.getMergeInstancePreparation(),
409 Vals: avail.getMergeInstanceType(), Vals: avail.getMergeInstance());
410 }
411 os << " default: break;\n";
412 os << " }\n"
413 << " return std::nullopt;\n"
414 << "}\n";
415 }
416}
417
418static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
419 EnumInfo enumInfo(enumDef);
420 StringRef enumName = enumInfo.getEnumClassName();
421 auto enumerants = enumInfo.getAllCases();
422
423 llvm::NamespaceEmitter ns(os, enumInfo.getCppNamespace());
424 llvm::StringSet<> handledClasses;
425
426 // Place all availability specifications to their corresponding
427 // availability classes.
428 for (const EnumCase &enumerant : enumerants)
429 for (const Availability &avail : getAvailabilities(def: enumerant.getDef())) {
430 StringRef className = avail.getClass();
431 if (handledClasses.count(Key: className))
432 continue;
433 os << formatv(Fmt: "std::optional<{0}> {1}({2} value);\n",
434 Vals: avail.getMergeInstanceType(), Vals: avail.getQueryFnName(),
435 Vals&: enumName);
436 handledClasses.insert(key: className);
437 }
438}
439
440static bool emitEnumDecls(const RecordKeeper &records, raw_ostream &os) {
441 llvm::emitSourceFileHeader(Desc: "SPIR-V Enum Availability Declarations", OS&: os,
442 Record: records);
443
444 auto defs = records.getAllDerivedDefinitions(ClassName: "EnumInfo");
445 for (const auto *def : defs)
446 emitEnumDecl(enumDef: *def, os);
447
448 return false;
449}
450
451static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
452 EnumInfo enumInfo(enumDef);
453 llvm::NamespaceEmitter ns(os, enumInfo.getCppNamespace());
454
455 if (enumInfo.isBitEnum())
456 emitAvailabilityQueryForBitEnum(enumDef, os);
457 else
458 emitAvailabilityQueryForIntEnum(enumDef, os);
459}
460
461static bool emitEnumDefs(const RecordKeeper &records, raw_ostream &os) {
462 llvm::emitSourceFileHeader(Desc: "SPIR-V Enum Availability Definitions", OS&: os,
463 Record: records);
464
465 for (const Record *def : records.getAllDerivedDefinitions(ClassName: "EnumInfo"))
466 emitEnumDef(enumDef: *def, os);
467
468 return false;
469}
470
471//===----------------------------------------------------------------------===//
472// Enum Availability Query Hook Registration
473//===----------------------------------------------------------------------===//
474
475// Registers the enum utility generator to mlir-tblgen.
476static mlir::GenRegistration
477 genEnumDecls("gen-spirv-enum-avail-decls",
478 "Generate SPIR-V enum availability declarations",
479 [](const RecordKeeper &records, raw_ostream &os) {
480 return emitEnumDecls(records, os);
481 });
482
483// Registers the enum utility generator to mlir-tblgen.
484static mlir::GenRegistration
485 genEnumDefs("gen-spirv-enum-avail-defs",
486 "Generate SPIR-V enum availability definitions",
487 [](const RecordKeeper &records, raw_ostream &os) {
488 return emitEnumDefs(records, os);
489 });
490
491//===----------------------------------------------------------------------===//
492// Serialization AutoGen
493//===----------------------------------------------------------------------===//
494
495// These enums are encoded as <id> to constant values in SPIR-V blob, but we
496// directly use the constant value as attribute in SPIR-V dialect. So need
497// to handle them separately from normal enum attributes.
498constexpr llvm::StringLiteral constantIdEnumAttrs[] = {
499 "SPIRV_ScopeAttr",
500 "SPIRV_KHR_CooperativeMatrixUseAttr",
501 "SPIRV_KHR_CooperativeMatrixLayoutAttr",
502 "SPIRV_MemorySemanticsAttr",
503 "SPIRV_MatrixLayoutAttr",
504 "SPIRV_TosaExtAccTypeAttr",
505 "SPIRV_TosaExtResizeModeAttr",
506 "SPIRV_TosaExtNaNPropagationModeAttr",
507 "SPIRV_TosaExtRoundingModeAttr",
508 "SPIRV_QuadSwapDirectionAttr",
509};
510
511/// Generates code to serialize attributes of a SPIRV_Op `op` into `os`. The
512/// generates code extracts the attribute with name `attrName` from
513/// `operandList` of `op`.
514static void emitAttributeSerialization(const Attribute &attr,
515 ArrayRef<SMLoc> loc, StringRef tabs,
516 StringRef opVar, StringRef operandList,
517 StringRef getterName, raw_ostream &os) {
518 os << tabs
519 << formatv(Fmt: "if (auto attr = {0}.{1}Attr()) {{\n", Vals&: opVar, Vals&: getterName);
520 if (llvm::is_contained(Range: constantIdEnumAttrs, Element: attr.getAttrDefName())) {
521 EnumInfo baseEnum(attr.getDef().getValueAsDef(FieldName: "enum"));
522 os << tabs
523 << formatv(Fmt: " {0}.push_back(prepareConstantInt({1}.getLoc(), "
524 "Builder({1}).getI32IntegerAttr(static_cast<uint32_t>("
525 "::llvm::cast<{2}::{3}Attr>(attr).getValue()))));\n",
526 Vals&: operandList, Vals&: opVar, Vals: baseEnum.getCppNamespace(),
527 Vals: baseEnum.getEnumClassName());
528 } else if (attr.isSubClassOf(className: "SPIRV_BitEnumAttr") ||
529 attr.isSubClassOf(className: "SPIRV_I32EnumAttr")) {
530 EnumInfo baseEnum(attr.getDef().getValueAsDef(FieldName: "enum"));
531 os << tabs
532 << formatv(Fmt: " {0}.push_back(static_cast<uint32_t>("
533 "::llvm::cast<{1}::{2}Attr>(attr).getValue()));\n",
534 Vals&: operandList, Vals: baseEnum.getCppNamespace(),
535 Vals: baseEnum.getEnumClassName());
536 } else if (attr.getAttrDefName() == "I32ArrayAttr") {
537 // Serialize all the elements of the array
538 os << tabs << " for (auto attrElem : llvm::cast<ArrayAttr>(attr)) {\n";
539 os << tabs
540 << formatv(Fmt: " {0}.push_back(static_cast<uint32_t>("
541 "llvm::cast<IntegerAttr>(attrElem).getValue().getZExtValue())"
542 ");\n",
543 Vals&: operandList);
544 os << tabs << " }\n";
545 } else if (attr.getAttrDefName() == "I32Attr") {
546 os << tabs
547 << formatv(
548 Fmt: " {0}.push_back(static_cast<uint32_t>("
549 "llvm::cast<IntegerAttr>(attr).getValue().getZExtValue()));\n",
550 Vals&: operandList);
551 } else if (attr.isEnumAttr() || attr.isTypeAttr()) {
552 // It may be the first time this type appears in the IR, so we need to
553 // process it.
554 StringRef attrTypeID = "attrTypeID";
555 os << tabs << formatv(Fmt: " uint32_t {0} = 0;\n", Vals&: attrTypeID);
556 os << tabs
557 << formatv(Fmt: " if (failed(processType({0}.getLoc(), "
558 "llvm::cast<TypeAttr>(attr).getValue(), {1}))) {{\n",
559 Vals&: opVar, Vals&: attrTypeID);
560 os << tabs << " return failure();\n";
561 os << tabs << " }\n";
562 os << tabs << formatv(Fmt: " {0}.push_back(attrTypeID);\n", Vals&: operandList);
563 } else if (llvm::is_contained(Set: {"SPIRV_BoolConstAttr",
564 "SPIRV_TensorArmAxisAttr",
565 "SPIRV_I8OrI16OrF16OrF32OrBF16ConstAttr"},
566 Element: attr.getAttrDefName())) {
567 os << tabs
568 << formatv(
569 Fmt: " {0}.push_back(prepareConstantScalar({1}.getLoc(), attr));\n",
570 Vals&: operandList, Vals&: opVar);
571 } else if (attr.getAttrDefName().contains(Other: "TensorArm")) {
572 os << tabs
573 << formatv(Fmt: " {0}.push_back(prepareConstant({1}.getLoc(), "
574 "llvm::cast<DenseElementsAttr>(attr).getType(), attr));\n",
575 Vals&: operandList, Vals&: opVar);
576 } else {
577 PrintFatalError(
578 ErrorLoc: loc,
579 Msg: llvm::Twine(
580 "unhandled attribute type in SPIR-V serialization generation : '") +
581 attr.getAttrDefName() + llvm::Twine("'"));
582 }
583 os << tabs << "}\n";
584}
585
586/// Generates code to serialize the operands of a SPIRV_Op `op` into `os`. The
587/// generated queries the SSA-ID if operand is a SSA-Value, or serializes the
588/// attributes. The `operands` vector is updated appropriately. `elidedAttrs`
589/// updated as well to include the serialized attributes.
590static void emitArgumentSerialization(const Operator &op, ArrayRef<SMLoc> loc,
591 StringRef tabs, StringRef opVar,
592 StringRef operands, StringRef elidedAttrs,
593 raw_ostream &os) {
594 using mlir::tblgen::Argument;
595
596 // SPIR-V ops can mix operands and attributes in the definition. These
597 // operands and attributes are serialized in the exact order of the definition
598 // to match SPIR-V binary format requirements. It can cause excessive
599 // generated code bloat because we are emitting code to handle each
600 // operand/attribute separately. So here we probe first to check whether all
601 // the operands are ahead of attributes. Then we can serialize all operands
602 // together.
603
604 // Whether all operands are ahead of all attributes in the op's spec.
605 bool areOperandsAheadOfAttrs = true;
606 // Find the first attribute.
607 const Argument *it = llvm::find_if(Range: op.getArgs(), P: [](const Argument &arg) {
608 return isa<NamedAttribute *>(Val: arg);
609 });
610 // Check whether all following arguments are attributes.
611 for (const Argument *ie = op.arg_end(); it != ie; ++it) {
612 if (!isa<NamedAttribute *>(Val: *it)) {
613 areOperandsAheadOfAttrs = false;
614 break;
615 }
616 }
617
618 // Serialize all operands together.
619 if (areOperandsAheadOfAttrs) {
620 if (op.getNumOperands() != 0) {
621 os << tabs
622 << formatv(Fmt: "for (Value operand : {0}->getOperands()) {{\n", Vals&: opVar);
623 os << tabs << " auto id = getValueID(operand);\n";
624 os << tabs << " assert(id && \"use before def!\");\n";
625 os << tabs << formatv(Fmt: " {0}.push_back(id);\n", Vals&: operands);
626 os << tabs << "}\n";
627 }
628 for (const NamedAttribute &attr : op.getAttributes()) {
629 emitAttributeSerialization(
630 attr: (attr.attr.isOptional() ? attr.attr.getBaseAttr() : attr.attr), loc,
631 tabs, opVar, operandList: operands, getterName: op.getGetterName(name: attr.name), os);
632 os << tabs
633 << formatv(Fmt: "{0}.push_back(\"{1}\");\n", Vals&: elidedAttrs, Vals: attr.name);
634 }
635 return;
636 }
637
638 // Serialize operands separately.
639 auto operandNum = 0;
640 for (unsigned i = 0, e = op.getNumArgs(); i < e; ++i) {
641 auto argument = op.getArg(index: i);
642 os << tabs << "{\n";
643 if (isa<NamedTypeConstraint *>(Val: argument)) {
644 os << tabs
645 << formatv(Fmt: " for (auto arg : {0}.getODSOperands({1})) {{\n", Vals&: opVar,
646 Vals&: operandNum);
647 os << tabs << " auto argID = getValueID(arg);\n";
648 os << tabs << " if (!argID) {\n";
649 os << tabs
650 << formatv(Fmt: " return emitError({0}.getLoc(), "
651 "\"operand #{1} has a use before def\");\n",
652 Vals&: opVar, Vals&: operandNum);
653 os << tabs << " }\n";
654 os << tabs << formatv(Fmt: " {0}.push_back(argID);\n", Vals&: operands);
655 os << " }\n";
656 operandNum++;
657 } else {
658 NamedAttribute *attr = cast<NamedAttribute *>(Val&: argument);
659 auto newtabs = tabs.str() + " ";
660 emitAttributeSerialization(
661 attr: (attr->attr.isOptional() ? attr->attr.getBaseAttr() : attr->attr),
662 loc, tabs: newtabs, opVar, operandList: operands, getterName: op.getGetterName(name: attr->name), os);
663 os << newtabs
664 << formatv(Fmt: "{0}.push_back(\"{1}\");\n", Vals&: elidedAttrs, Vals&: attr->name);
665 }
666 os << tabs << "}\n";
667 }
668}
669
670/// Generates code to serializes the result of SPIRV_Op `op` into `os`. The
671/// generated gets the ID for the type of the result (if any), the SSA-ID of
672/// the result and updates `resultID` with the SSA-ID.
673static void emitResultSerialization(const Operator &op, ArrayRef<SMLoc> loc,
674 StringRef tabs, StringRef opVar,
675 StringRef operands, StringRef resultID,
676 raw_ostream &os) {
677 if (op.getNumResults() == 1) {
678 StringRef resultTypeID("resultTypeID");
679 os << tabs << formatv(Fmt: "uint32_t {0} = 0;\n", Vals&: resultTypeID);
680 os << tabs
681 << formatv(
682 Fmt: "if (failed(processType({0}.getLoc(), {0}.getType(), {1}))) {{\n",
683 Vals&: opVar, Vals&: resultTypeID);
684 os << tabs << " return failure();\n";
685 os << tabs << "}\n";
686 os << tabs << formatv(Fmt: "{0}.push_back({1});\n", Vals&: operands, Vals&: resultTypeID);
687 // Create an SSA result <id> for the op
688 os << tabs << formatv(Fmt: "{0} = getNextID();\n", Vals&: resultID);
689 os << tabs
690 << formatv(Fmt: "valueIDMap[{0}.getResult()] = {1};\n", Vals&: opVar, Vals&: resultID);
691 os << tabs << formatv(Fmt: "{0}.push_back({1});\n", Vals&: operands, Vals&: resultID);
692 } else if (op.getNumResults() != 0) {
693 PrintFatalError(ErrorLoc: loc, Msg: "SPIR-V ops can only have zero or one result");
694 }
695}
696
697/// Generates code to serialize attributes of SPIRV_Op `op` that become
698/// decorations on the `resultID` of the serialized operation `opVar` in the
699/// SPIR-V binary.
700static void emitDecorationSerialization(const Operator &op, StringRef tabs,
701 StringRef opVar, StringRef elidedAttrs,
702 StringRef resultID, raw_ostream &os) {
703 if (op.getNumResults() == 1) {
704 // All non-argument attributes translated into OpDecorate instruction
705 os << tabs
706 << formatv(Fmt: "for (auto attr : "
707 "{0}->getDiscardableAttrDictionary().getValue()) {{\n",
708 Vals&: opVar);
709 os << tabs
710 << formatv(Fmt: " if (llvm::is_contained({0}, attr.getName())) {{",
711 Vals&: elidedAttrs);
712 os << tabs << " continue;\n";
713 os << tabs << " }\n";
714 os << tabs
715 << formatv(
716 Fmt: " if (failed(processDecoration({0}.getLoc(), {1}, attr))) {{\n",
717 Vals&: opVar, Vals&: resultID);
718 os << tabs << " return failure();\n";
719 os << tabs << " }\n";
720 os << tabs << "}\n";
721 }
722}
723
724/// Generates code to serialize an SPIRV_Op `op` into `os`.
725static void emitSerializationFunction(const Record *attrClass,
726 const Record *record, const Operator &op,
727 raw_ostream &os) {
728 // If the record has 'autogenSerialization' set to 0, nothing to do
729 if (!record->getValueAsBit(FieldName: "autogenSerialization"))
730 return;
731
732 StringRef opVar("op"), operands("operands"), elidedAttrs("elidedAttrs"),
733 resultID("resultID");
734
735 os << formatv(
736 Fmt: "template <> LogicalResult\nSerializer::processOp<{0}>({0} {1}) {{\n",
737 Vals: op.getQualCppClassName(), Vals&: opVar);
738
739 // Special case for ops without attributes in TableGen definitions
740 if (op.getNumAttributes() == 0 && op.getNumVariableLengthOperands() == 0) {
741 std::string extInstSet;
742 std::string opcode;
743 if (record->isSubClassOf(Name: "SPIRV_ExtInstOp")) {
744 extInstSet =
745 formatv(Fmt: "\"{0}\"", Vals: record->getValueAsString(FieldName: "extendedInstSetName"));
746 opcode = std::to_string(val: record->getValueAsInt(FieldName: "extendedInstOpcode"));
747 } else {
748 extInstSet = "\"\"";
749 opcode = formatv(Fmt: "static_cast<uint32_t>(spirv::Opcode::{0})",
750 Vals: record->getValueAsString(FieldName: "spirvOpName"));
751 }
752
753 os << formatv(Fmt: " return processOpWithoutGrammarAttr({0}, {1}, {2});\n}\n\n",
754 Vals&: opVar, Vals&: extInstSet, Vals&: opcode);
755 return;
756 }
757
758 os << formatv(Fmt: " SmallVector<uint32_t, 4> {0};\n", Vals&: operands);
759 os << formatv(Fmt: " SmallVector<StringRef, 2> {0};\n", Vals&: elidedAttrs);
760
761 // Serialize result information.
762 if (op.getNumResults() == 1) {
763 os << formatv(Fmt: " uint32_t {0} = 0;\n", Vals&: resultID);
764 emitResultSerialization(op, loc: record->getLoc(), tabs: " ", opVar, operands,
765 resultID, os);
766 }
767
768 // Process arguments.
769 emitArgumentSerialization(op, loc: record->getLoc(), tabs: " ", opVar, operands,
770 elidedAttrs, os);
771
772 if (record->isSubClassOf(Name: "SPIRV_ExtInstOp")) {
773 os << formatv(
774 Fmt: " (void)encodeExtensionInstruction({0}, \"{1}\", {2}, {3});\n", Vals&: opVar,
775 Vals: record->getValueAsString(FieldName: "extendedInstSetName"),
776 Vals: record->getValueAsInt(FieldName: "extendedInstOpcode"), Vals&: operands);
777 } else {
778 // Emit debug info.
779 os << formatv(Fmt: " (void)emitDebugLine(functionBody, {0}.getLoc());\n",
780 Vals&: opVar);
781 os << formatv(Fmt: " (void)encodeInstructionInto("
782 "functionBody, spirv::Opcode::{0}, {1});\n",
783 Vals: record->getValueAsString(FieldName: "spirvOpName"), Vals&: operands);
784 }
785
786 // Process decorations.
787 emitDecorationSerialization(op, tabs: " ", opVar, elidedAttrs, resultID, os);
788
789 os << " return success();\n";
790 os << "}\n\n";
791}
792
793/// Generates the prologue for the function that dispatches the serialization of
794/// the operation `opVar` based on its opcode.
795static void initDispatchSerializationFn(StringRef opVar, raw_ostream &os) {
796 os << formatv(
797 Fmt: "LogicalResult Serializer::dispatchToAutogenSerialization(Operation "
798 "*{0}) {{\n",
799 Vals&: opVar);
800 os << " using DispatchFn = LogicalResult (*)(Serializer &, Operation *);\n";
801 os << " static const llvm::DenseMap<TypeID, DispatchFn> dispatchTable = {\n";
802}
803
804/// Generates the dispatch table entry that maps the TypeID of `op` to a call
805/// of the serialization function generated for it.
806static void emitSerializationDispatch(const Operator &op, StringRef tabs,
807 StringRef opVar, raw_ostream &os) {
808 os << tabs
809 << formatv(Fmt: "{{TypeID::get<{0}>(), [](Serializer &s, Operation *{1}) {{\n",
810 Vals: op.getQualCppClassName(), Vals&: opVar);
811 os << tabs
812 << formatv(Fmt: " return s.processOp(cast<{0}>({1}));\n",
813 Vals: op.getQualCppClassName(), Vals&: opVar);
814 os << tabs << " }},\n";
815}
816
817/// Generates the epilogue for the function that dispatches the serialization of
818/// the operation.
819static void finalizeDispatchSerializationFn(StringRef opVar, raw_ostream &os) {
820 os << " };\n";
821 os << formatv(Fmt: " if (DispatchFn fn = "
822 "dispatchTable.lookup({0}->getName().getTypeID()))\n",
823 Vals&: opVar);
824 os << formatv(Fmt: " return fn(*this, {0});\n", Vals&: opVar);
825 os << formatv(
826 Fmt: " return {0}->emitError(\"unhandled operation serialization\");\n",
827 Vals&: opVar);
828 os << "}\n\n";
829}
830
831/// Generates code to deserialize the attribute of a SPIRV_Op into `os`. The
832/// generated code reads the `words` of the serialized instruction at
833/// position `wordIndex` and adds the deserialized attribute into `attrList`.
834static void emitAttributeDeserialization(const Attribute &attr,
835 ArrayRef<SMLoc> loc, StringRef tabs,
836 StringRef attrList, StringRef attrName,
837 StringRef words, StringRef wordIndex,
838 raw_ostream &os) {
839 if (llvm::is_contained(Range: constantIdEnumAttrs, Element: attr.getAttrDefName())) {
840 EnumInfo baseEnum(attr.getDef().getValueAsDef(FieldName: "enum"));
841 os << tabs
842 << formatv(Fmt: "{0}.push_back(opBuilder.getNamedAttr(\"{1}\", "
843 "opBuilder.getAttr<{2}::{3}Attr>(static_cast<{2}::{3}>("
844 "getConstantInt({4}[{5}++]).getValue().getZExtValue()))));\n",
845 Vals&: attrList, Vals&: attrName, Vals: baseEnum.getCppNamespace(),
846 Vals: baseEnum.getEnumClassName(), Vals&: words, Vals&: wordIndex);
847 } else if (attr.isSubClassOf(className: "SPIRV_BitEnumAttr") ||
848 attr.isSubClassOf(className: "SPIRV_I32EnumAttr")) {
849 EnumInfo baseEnum(attr.getDef().getValueAsDef(FieldName: "enum"));
850 os << tabs
851 << formatv(Fmt: " {0}.push_back(opBuilder.getNamedAttr(\"{1}\", "
852 "opBuilder.getAttr<{2}::{3}Attr>("
853 "static_cast<{2}::{3}>({4}[{5}++]))));\n",
854 Vals&: attrList, Vals&: attrName, Vals: baseEnum.getCppNamespace(),
855 Vals: baseEnum.getEnumClassName(), Vals&: words, Vals&: wordIndex);
856 } else if (attr.getAttrDefName() == "I32ArrayAttr") {
857 os << tabs << "SmallVector<Attribute, 4> attrListElems;\n";
858 os << tabs << formatv(Fmt: "while ({0} < {1}.size()) {{\n", Vals&: wordIndex, Vals&: words);
859 os << tabs
860 << formatv(
861 Fmt: " "
862 "attrListElems.push_back(opBuilder.getI32IntegerAttr({0}[{1}++]))"
863 ";\n",
864 Vals&: words, Vals&: wordIndex);
865 os << tabs << "}\n";
866 os << tabs
867 << formatv(Fmt: "{0}.push_back(opBuilder.getNamedAttr(\"{1}\", "
868 "opBuilder.getArrayAttr(attrListElems)));\n",
869 Vals&: attrList, Vals&: attrName);
870 } else if (attr.getAttrDefName() == "I32Attr") {
871 os << tabs
872 << formatv(Fmt: "{0}.push_back(opBuilder.getNamedAttr(\"{1}\", "
873 "opBuilder.getI32IntegerAttr({2}[{3}++])));\n",
874 Vals&: attrList, Vals&: attrName, Vals&: words, Vals&: wordIndex);
875 } else if (attr.isEnumAttr() || attr.isTypeAttr()) {
876 os << tabs
877 << formatv(Fmt: "{0}.push_back(opBuilder.getNamedAttr(\"{1}\", "
878 "TypeAttr::get(getType({2}[{3}++]))));\n",
879 Vals&: attrList, Vals&: attrName, Vals&: words, Vals&: wordIndex);
880 } else if (llvm::is_contained(Set: {"SPIRV_BoolConstAttr",
881 "SPIRV_I8OrI16OrF16OrF32OrBF16ConstAttr"},
882 Element: attr.getAttrDefName()) ||
883 attr.getAttrDefName().contains(Other: "TensorArm")) {
884 os << tabs
885 << formatv(Fmt: "std::optional<std::pair<Attribute, Type>> c = "
886 "getConstant({0}[{1}++]);\n",
887 Vals&: words, Vals&: wordIndex);
888 os << tabs << "if (!c.has_value()) {\n";
889 os << tabs
890 << formatv(Fmt: " "
891 "return emitError(unknownLoc, \"could not fetch "
892 "constant attribute for {0}\") << "
893 "{1} << \" of \" << {2}.size() << \" processed\";\n",
894 Vals&: attrName, Vals&: wordIndex, Vals&: words);
895 os << tabs << "}\n";
896 os << tabs
897 << formatv(Fmt: "{0}.push_back(opBuilder.getNamedAttr(\"{1}\", "
898 "c.value().first));\n",
899 Vals&: attrList, Vals&: attrName);
900 } else {
901 PrintFatalError(
902 ErrorLoc: loc, Msg: llvm::Twine(
903 "unhandled attribute type in deserialization generation : '") +
904 attrName + llvm::Twine("'"));
905 }
906}
907
908/// Generates the code to deserialize the result of an SPIRV_Op `op` into
909/// `os`. The generated code gets the type of the result specified at
910/// `words`[`wordIndex`], the SSA ID for the result at position `wordIndex` + 1
911/// and updates the `resultType` and `valueID` with the parsed type and SSA ID,
912/// respectively.
913static void emitResultDeserialization(const Operator &op, ArrayRef<SMLoc> loc,
914 StringRef tabs, StringRef words,
915 StringRef wordIndex,
916 StringRef resultTypes, StringRef valueID,
917 raw_ostream &os) {
918 // Deserialize result information if it exists
919 if (op.getNumResults() == 1) {
920 os << tabs << "{\n";
921 os << tabs << formatv(Fmt: " if ({0} >= {1}.size()) {{\n", Vals&: wordIndex, Vals&: words);
922 os << tabs
923 << formatv(
924 Fmt: " return emitError(unknownLoc, \"expected result type <id> "
925 "while deserializing {0}\");\n",
926 Vals: op.getQualCppClassName());
927 os << tabs << " }\n";
928 os << tabs << formatv(Fmt: " auto ty = getType({0}[{1}]);\n", Vals&: words, Vals&: wordIndex);
929 os << tabs << " if (!ty) {\n";
930 os << tabs
931 << formatv(
932 Fmt: " return emitError(unknownLoc, \"unknown type result <id> : "
933 "\") << {0}[{1}];\n",
934 Vals&: words, Vals&: wordIndex);
935 os << tabs << " }\n";
936 os << tabs << formatv(Fmt: " {0}.push_back(ty);\n", Vals&: resultTypes);
937 os << tabs << formatv(Fmt: " {0}++;\n", Vals&: wordIndex);
938 os << tabs << formatv(Fmt: " if ({0} >= {1}.size()) {{\n", Vals&: wordIndex, Vals&: words);
939 os << tabs
940 << formatv(
941 Fmt: " return emitError(unknownLoc, \"expected result <id> while "
942 "deserializing {0}\");\n",
943 Vals: op.getQualCppClassName());
944 os << tabs << " }\n";
945 os << tabs << "}\n";
946 os << tabs << formatv(Fmt: "{0} = {1}[{2}++];\n", Vals&: valueID, Vals&: words, Vals&: wordIndex);
947 } else if (op.getNumResults() != 0) {
948 PrintFatalError(ErrorLoc: loc, Msg: "SPIR-V ops can have only zero or one result");
949 }
950}
951
952/// Generates the code to deserialize the operands of an SPIRV_Op `op` into
953/// `os`. The generated code reads the `words` of the binary instruction, from
954/// position `wordIndex` to the end, and either gets the Value corresponding to
955/// the ID encoded, or deserializes the attributes encoded. The parsed
956/// operand(attribute) is added to the `operands` list or `attributes` list.
957static void emitOperandDeserialization(const Operator &op, ArrayRef<SMLoc> loc,
958 StringRef tabs, StringRef words,
959 StringRef wordIndex, StringRef operands,
960 StringRef attributes, raw_ostream &os) {
961 // Process operands/attributes
962 for (unsigned i = 0, e = op.getNumArgs(); i < e; ++i) {
963 auto argument = op.getArg(index: i);
964 if (auto *valueArg =
965 llvm::dyn_cast_if_present<NamedTypeConstraint *>(Val&: argument)) {
966 if (valueArg->isVariableLength()) {
967 if (i != e - 1) {
968 PrintFatalError(
969 ErrorLoc: loc, Msg: "SPIR-V ops can have Variadic<..> or "
970 "Optional<...> arguments only if it's the last argument");
971 }
972 os << tabs
973 << formatv(Fmt: "for (; {0} < {1}.size(); ++{0})", Vals&: wordIndex, Vals&: words);
974 } else {
975 os << tabs << formatv(Fmt: "if ({0} < {1}.size())", Vals&: wordIndex, Vals&: words);
976 }
977 os << " {\n";
978 os << tabs
979 << formatv(Fmt: " auto arg = getValue({0}[{1}]);\n", Vals&: words, Vals&: wordIndex);
980 os << tabs << " if (!arg) {\n";
981 os << tabs
982 << formatv(
983 Fmt: " return emitError(unknownLoc, \"unknown result <id> : \") "
984 "<< {0}[{1}];\n",
985 Vals&: words, Vals&: wordIndex);
986 os << tabs << " }\n";
987 os << tabs << formatv(Fmt: " {0}.push_back(arg);\n", Vals&: operands);
988 if (!valueArg->isVariableLength()) {
989 os << tabs << formatv(Fmt: " {0}++;\n", Vals&: wordIndex);
990 }
991 os << tabs << "}\n";
992 } else {
993 os << tabs << formatv(Fmt: "if ({0} < {1}.size()) {{\n", Vals&: wordIndex, Vals&: words);
994 auto *attr = cast<NamedAttribute *>(Val&: argument);
995 auto newtabs = tabs.str() + " ";
996 emitAttributeDeserialization(
997 attr: (attr->attr.isOptional() ? attr->attr.getBaseAttr() : attr->attr),
998 loc, tabs: newtabs, attrList: attributes, attrName: attr->name, words, wordIndex, os);
999 os << " }\n";
1000 }
1001 }
1002
1003 os << tabs << formatv(Fmt: "if ({0} != {1}.size()) {{\n", Vals&: wordIndex, Vals&: words);
1004 os << tabs
1005 << formatv(
1006 Fmt: " return emitError(unknownLoc, \"found more operands than "
1007 "expected when deserializing {0}, only \") << {1} << \" of \" << "
1008 "{2}.size() << \" processed\";\n",
1009 Vals: op.getQualCppClassName(), Vals&: wordIndex, Vals&: words);
1010 os << tabs << "}\n\n";
1011}
1012
1013/// Generates code to update the `attributes` vector with the attributes
1014/// obtained from parsing the decorations in the SPIR-V binary associated with
1015/// an <id> `valueID`
1016static void emitDecorationDeserialization(const Operator &op, StringRef tabs,
1017 StringRef valueID,
1018 StringRef attributes,
1019 raw_ostream &os) {
1020 // Import decorations parsed
1021 if (op.getNumResults() == 1) {
1022 os << tabs << formatv(Fmt: "if (decorations.count({0})) {{\n", Vals&: valueID);
1023 os << tabs
1024 << formatv(Fmt: " auto attrs = decorations[{0}].getAttrs();\n", Vals&: valueID);
1025 os << tabs
1026 << formatv(Fmt: " {0}.append(attrs.begin(), attrs.end());\n", Vals&: attributes);
1027 os << tabs << "}\n";
1028 }
1029}
1030
1031/// Generates code to deserialize an SPIRV_Op `op` into `os`.
1032static void emitDeserializationFunction(const Record *attrClass,
1033 const Record *record,
1034 const Operator &op, raw_ostream &os) {
1035 // If the record has 'autogenSerialization' set to 0, nothing to do
1036 if (!record->getValueAsBit(FieldName: "autogenSerialization"))
1037 return;
1038
1039 StringRef resultTypes("resultTypes"), valueID("valueID"), words("words"),
1040 wordIndex("wordIndex"), opVar("op"), operands("operands"),
1041 attributes("attributes");
1042
1043 // Method declaration
1044 os << formatv(Fmt: "template <> "
1045 "LogicalResult\nDeserializer::processOp<{0}>(ArrayRef<"
1046 "uint32_t> {1}) {{\n",
1047 Vals: op.getQualCppClassName(), Vals&: words);
1048
1049 // Special case for ops without attributes in TableGen definitions
1050 if (op.getNumAttributes() == 0 && op.getNumVariableLengthOperands() == 0) {
1051 os << formatv(Fmt: " return processOpWithoutGrammarAttr("
1052 "{0}, \"{1}\", {2}, {3});\n}\n\n",
1053 Vals&: words, Vals: op.getOperationName(),
1054 Vals: op.getNumResults() ? "true" : "false", Vals: op.getNumOperands());
1055 return;
1056 }
1057
1058 os << formatv(Fmt: " SmallVector<Type, 1> {0};\n", Vals&: resultTypes);
1059 os << formatv(Fmt: " size_t {0} = 0; (void){0};\n", Vals&: wordIndex);
1060 os << formatv(Fmt: " uint32_t {0} = 0; (void){0};\n", Vals&: valueID);
1061
1062 // Deserialize result information
1063 emitResultDeserialization(op, loc: record->getLoc(), tabs: " ", words, wordIndex,
1064 resultTypes, valueID, os);
1065
1066 os << formatv(Fmt: " SmallVector<Value, 4> {0};\n", Vals&: operands);
1067 os << formatv(Fmt: " SmallVector<NamedAttribute, 4> {0};\n", Vals&: attributes);
1068 // Operand deserialization
1069 emitOperandDeserialization(op, loc: record->getLoc(), tabs: " ", words, wordIndex,
1070 operands, attributes, os);
1071
1072 // Decorations
1073 emitDecorationDeserialization(op, tabs: " ", valueID, attributes, os);
1074
1075 os << formatv(Fmt: " Location loc = createFileLineColLoc(opBuilder);\n");
1076 if (!op.hasNonEmptyProperties()) {
1077 os << formatv(Fmt: " auto {1} = {0}::create(opBuilder, loc, {2}, {3}, {4}); "
1078 "(void){1};\n",
1079 Vals: op.getQualCppClassName(), Vals&: opVar, Vals&: resultTypes, Vals&: operands,
1080 Vals&: attributes);
1081 } else {
1082 os << formatv(Fmt: " {0}::Properties properties{{};\n",
1083 Vals: op.getQualCppClassName());
1084 os << formatv(Fmt: " {0}::populateDefaultProperties(\n"
1085 " OperationName({0}::getOperationName(), "
1086 "opBuilder.getContext()), properties);\n",
1087 Vals: op.getQualCppClassName());
1088 os << formatv(Fmt: " if (failed({0}::setPropertiesFromAttr(\n"
1089 " properties, opBuilder.getDictionaryAttr({1}),\n"
1090 " [&]() {{ return emitError(loc); })))\n"
1091 " return failure();\n",
1092 Vals: op.getQualCppClassName(), Vals&: attributes);
1093 os << " SmallVector<NamedAttribute> discardableAttributes;\n";
1094 os << formatv(Fmt: " for (NamedAttribute attr : {0}) {{\n", Vals&: attributes);
1095 os << " StringRef name = attr.getName().getValue();\n";
1096 os << " (void)name;\n";
1097 os << " if (";
1098 bool emittedName = false;
1099 for (StringRef name : op.getInherentAttrNames()) {
1100 if (emittedName)
1101 os << " && ";
1102 os << formatv(Fmt: "name != \"{0}\"", Vals&: name);
1103 emittedName = true;
1104 }
1105 os << ")\n";
1106 os << " discardableAttributes.push_back(attr);\n";
1107 os << " }\n";
1108 os << formatv(Fmt: " auto {1} = {0}::create(opBuilder, loc, {2}, {3}, "
1109 "properties, discardableAttributes); (void){1};\n",
1110 Vals: op.getQualCppClassName(), Vals&: opVar, Vals&: resultTypes, Vals&: operands);
1111 }
1112 if (op.getNumResults() == 1) {
1113 os << formatv(Fmt: " valueMap[{0}] = {1}.getResult();\n\n", Vals&: valueID, Vals&: opVar);
1114 }
1115
1116 // According to SPIR-V spec:
1117 // This location information applies to the instructions physically following
1118 // this instruction, up to the first occurrence of any of the following: the
1119 // next end of block.
1120 os << formatv(Fmt: " if ({0}.hasTrait<OpTrait::IsTerminator>())\n", Vals&: opVar);
1121 os << formatv(Fmt: " (void)clearDebugLine();\n");
1122 os << " return success();\n";
1123 os << "}\n\n";
1124}
1125
1126/// Generates the prologue for the function that dispatches the deserialization
1127/// based on the `opcode`.
1128static void initDispatchDeserializationFn(StringRef opcode, StringRef words,
1129 raw_ostream &os) {
1130 os << formatv(Fmt: "LogicalResult spirv::Deserializer::"
1131 "dispatchToAutogenDeserialization(spirv::Opcode {0},"
1132 " ArrayRef<uint32_t> {1}) {{\n",
1133 Vals&: opcode, Vals&: words);
1134 os << formatv(Fmt: " switch ({0}) {{\n", Vals&: opcode);
1135}
1136
1137/// Generates the body of the dispatch function, by generating the case label
1138/// for an opcode and the call to the method to perform the deserialization.
1139static void emitDeserializationDispatch(const Operator &op, const Record *def,
1140 StringRef tabs, StringRef words,
1141 raw_ostream &os) {
1142 os << tabs
1143 << formatv(Fmt: "case spirv::Opcode::{0}:\n",
1144 Vals: def->getValueAsString(FieldName: "spirvOpName"));
1145 os << tabs
1146 << formatv(Fmt: " return processOp<{0}>({1});\n", Vals: op.getQualCppClassName(),
1147 Vals&: words);
1148}
1149
1150/// Generates the epilogue for the function that dispatches the deserialization
1151/// of the operation.
1152static void finalizeDispatchDeserializationFn(StringRef opcode,
1153 raw_ostream &os) {
1154 os << " default:\n";
1155 os << " ;\n";
1156 os << " }\n";
1157 StringRef opcodeVar("opcodeString");
1158 os << formatv(Fmt: " auto {0} = spirv::stringifyOpcode({1});\n", Vals&: opcodeVar,
1159 Vals&: opcode);
1160 os << formatv(Fmt: " if (!{0}.empty()) {{\n", Vals&: opcodeVar);
1161 os << formatv(Fmt: " return emitError(unknownLoc, \"unhandled deserialization "
1162 "of \") << {0};\n",
1163 Vals&: opcodeVar);
1164 os << " } else {\n";
1165 os << formatv(Fmt: " return emitError(unknownLoc, \"unhandled opcode \") << "
1166 "static_cast<uint32_t>({0});\n",
1167 Vals&: opcode);
1168 os << " }\n";
1169 os << "}\n";
1170}
1171
1172static void initExtendedSetDeserializationDispatch(StringRef extensionSetName,
1173 StringRef instructionID,
1174 StringRef words,
1175 raw_ostream &os) {
1176 os << formatv(Fmt: "LogicalResult spirv::Deserializer::"
1177 "dispatchToExtensionSetAutogenDeserialization("
1178 "StringRef {0}, uint32_t {1}, ArrayRef<uint32_t> {2}) {{\n",
1179 Vals&: extensionSetName, Vals&: instructionID, Vals&: words);
1180}
1181
1182static void emitExtendedSetDeserializationDispatch(const RecordKeeper &records,
1183 raw_ostream &os) {
1184 StringRef extensionSetName("extensionSetName"),
1185 instructionID("instructionID"), words("words");
1186
1187 // First iterate over all ops derived from SPIRV_ExtensionSetOps to get all
1188 // extensionSets.
1189
1190 // For each of the extensions a separate raw_string_ostream is used to
1191 // generate code into. These are then concatenated at the end. Since
1192 // raw_string_ostream needs a string&, use a vector to store all the string
1193 // that are captured by reference within raw_string_ostream.
1194 StringMap<raw_string_ostream> extensionSets;
1195 std::list<std::string> extensionSetNames;
1196
1197 initExtendedSetDeserializationDispatch(extensionSetName, instructionID, words,
1198 os);
1199 auto defs = records.getAllDerivedDefinitions(ClassName: "SPIRV_ExtInstOp");
1200 for (const auto *def : defs) {
1201 if (!def->getValueAsBit(FieldName: "autogenSerialization")) {
1202 continue;
1203 }
1204 Operator op(def);
1205 auto setName = def->getValueAsString(FieldName: "extendedInstSetName");
1206 if (!extensionSets.count(Key: setName)) {
1207 extensionSetNames.emplace_back(args: "");
1208 extensionSets.try_emplace(Key: setName, Args&: extensionSetNames.back());
1209 auto &setos = extensionSets.find(Key: setName)->second;
1210 setos << formatv(Fmt: " if ({0} == \"{1}\") {{\n", Vals&: extensionSetName, Vals&: setName);
1211 setos << formatv(Fmt: " switch ({0}) {{\n", Vals&: instructionID);
1212 }
1213 auto &setos = extensionSets.find(Key: setName)->second;
1214 setos << formatv(Fmt: " case {0}:\n",
1215 Vals: def->getValueAsInt(FieldName: "extendedInstOpcode"));
1216 setos << formatv(Fmt: " return processOp<{0}>({1});\n",
1217 Vals: op.getQualCppClassName(), Vals&: words);
1218 }
1219
1220 // Append the dispatch code for all the extended sets.
1221 for (auto &extensionSet : extensionSets) {
1222 os << extensionSet.second.str();
1223 os << " default:\n";
1224 os << formatv(
1225 Fmt: " return emitError(unknownLoc, \"unhandled deserializations of "
1226 "\") << {0} << \" from extension set \" << {1};\n",
1227 Vals&: instructionID, Vals&: extensionSetName);
1228 os << " }\n";
1229 os << " }\n";
1230 }
1231
1232 os << formatv(Fmt: " return emitError(unknownLoc, \"unhandled deserialization of "
1233 "extended instruction set {0}\");\n",
1234 Vals&: extensionSetName);
1235 os << "}\n";
1236}
1237
1238/// Emits all the autogenerated serialization/deserializations functions for the
1239/// SPIRV_Ops.
1240static bool emitSerializationFns(const RecordKeeper &records, raw_ostream &os) {
1241 llvm::emitSourceFileHeader(Desc: "SPIR-V Serialization Utilities/Functions", OS&: os,
1242 Record: records);
1243
1244 std::string dSerFnString, dDesFnString, serFnString, deserFnString;
1245 raw_string_ostream dSerFn(dSerFnString), dDesFn(dDesFnString),
1246 serFn(serFnString), deserFn(deserFnString);
1247 const Record *attrClass = records.getClass(Name: "Attr");
1248
1249 // Emit the serialization and deserialization functions simultaneously.
1250 StringRef opVar("op");
1251 StringRef opcode("opcode"), words("words");
1252
1253 // Handle the SPIR-V ops.
1254 initDispatchSerializationFn(opVar, os&: dSerFn);
1255 initDispatchDeserializationFn(opcode, words, os&: dDesFn);
1256 auto defs = records.getAllDerivedDefinitions(ClassName: "SPIRV_Op");
1257 for (const auto *def : defs) {
1258 Operator op(def);
1259 emitSerializationFunction(attrClass, record: def, op, os&: serFn);
1260 emitDeserializationFunction(attrClass, record: def, op, os&: deserFn);
1261 if (def->getValueAsBit(FieldName: "hasOpcode") ||
1262 def->isSubClassOf(Name: "SPIRV_ExtInstOp")) {
1263 emitSerializationDispatch(op, tabs: " ", opVar, os&: dSerFn);
1264 }
1265 if (def->getValueAsBit(FieldName: "hasOpcode")) {
1266 emitDeserializationDispatch(op, def, tabs: " ", words, os&: dDesFn);
1267 }
1268 }
1269 finalizeDispatchSerializationFn(opVar, os&: dSerFn);
1270 finalizeDispatchDeserializationFn(opcode, os&: dDesFn);
1271
1272 emitExtendedSetDeserializationDispatch(records, os&: dDesFn);
1273
1274 os << "#ifdef GET_SERIALIZATION_FNS\n\n";
1275 os << serFn.str();
1276 os << dSerFn.str();
1277 os << "#endif // GET_SERIALIZATION_FNS\n\n";
1278
1279 os << "#ifdef GET_DESERIALIZATION_FNS\n\n";
1280 os << deserFn.str();
1281 os << dDesFn.str();
1282 os << "#endif // GET_DESERIALIZATION_FNS\n\n";
1283
1284 return false;
1285}
1286
1287//===----------------------------------------------------------------------===//
1288// Serialization Hook Registration
1289//===----------------------------------------------------------------------===//
1290
1291static mlir::GenRegistration genSerialization(
1292 "gen-spirv-serialization",
1293 "Generate SPIR-V (de)serialization utilities and functions",
1294 [](const RecordKeeper &records, raw_ostream &os) {
1295 return emitSerializationFns(records, os);
1296 });
1297
1298//===----------------------------------------------------------------------===//
1299// Op Utils AutoGen
1300//===----------------------------------------------------------------------===//
1301
1302static void emitEnumGetAttrNameFnDecl(raw_ostream &os) {
1303 os << formatv(Fmt: "template <typename EnumClass> inline constexpr StringRef "
1304 "attributeName();\n");
1305}
1306
1307static void emitEnumGetAttrNameFnDefn(const EnumInfo &enumInfo,
1308 raw_ostream &os) {
1309 auto enumName = enumInfo.getEnumClassName();
1310 os << formatv(Fmt: "template <> inline StringRef attributeName<{0}>() {{\n",
1311 Vals&: enumName);
1312 os << " "
1313 << formatv(Fmt: "static constexpr const char attrName[] = \"{0}\";\n",
1314 Vals: llvm::convertToSnakeFromCamelCase(input: enumName));
1315 os << " return attrName;\n";
1316 os << "}\n";
1317}
1318
1319static bool emitAttrUtils(const RecordKeeper &records, raw_ostream &os) {
1320 llvm::emitSourceFileHeader(Desc: "SPIR-V Attribute Utilities", OS&: os, Record: records);
1321
1322 auto defs = records.getAllDerivedDefinitions(ClassName: "EnumInfo");
1323 os << "#ifndef MLIR_DIALECT_SPIRV_IR_ATTR_UTILS_H_\n";
1324 os << "#define MLIR_DIALECT_SPIRV_IR_ATTR_UTILS_H_\n";
1325 emitEnumGetAttrNameFnDecl(os);
1326 for (const auto *def : defs) {
1327 EnumInfo enumInfo(*def);
1328 emitEnumGetAttrNameFnDefn(enumInfo, os);
1329 }
1330 os << "#endif // MLIR_DIALECT_SPIRV_IR_ATTR_UTILS_H\n";
1331 return false;
1332}
1333
1334//===----------------------------------------------------------------------===//
1335// Op Utils Hook Registration
1336//===----------------------------------------------------------------------===//
1337
1338static mlir::GenRegistration
1339 genOpUtils("gen-spirv-attr-utils",
1340 "Generate SPIR-V attribute utility definitions",
1341 [](const RecordKeeper &records, raw_ostream &os) {
1342 return emitAttrUtils(records, os);
1343 });
1344
1345//===----------------------------------------------------------------------===//
1346// SPIR-V Availability Impl AutoGen
1347//===----------------------------------------------------------------------===//
1348
1349static void emitAvailabilityImpl(const Operator &srcOp, raw_ostream &os) {
1350 mlir::tblgen::FmtContext fctx;
1351 fctx.addSubst(placeholder: "overall", subst: "tblgen_overall");
1352
1353 std::vector<Availability> opAvailabilities =
1354 getAvailabilities(def: srcOp.getDef());
1355
1356 // First collect all availability classes this op should implement.
1357 // All availability instances keep information for the generated interface and
1358 // the instance's specific requirement. Here we remember a random instance so
1359 // we can get the information regarding the generated interface.
1360 llvm::StringMap<Availability> availClasses;
1361 for (const Availability &avail : opAvailabilities)
1362 availClasses.try_emplace(Key: avail.getClass(), Args: avail);
1363 for (const NamedAttribute &namedAttr : srcOp.getAttributes()) {
1364 Attribute attr = namedAttr.attr.isOptional() ? namedAttr.attr.getBaseAttr()
1365 : namedAttr.attr;
1366 if (!attr.isSubClassOf(className: "SPIRV_BitEnumAttr") &&
1367 !attr.isSubClassOf(className: "SPIRV_I32EnumAttr"))
1368 continue;
1369 EnumInfo enumInfo(attr.getDef().getValueAsDef(FieldName: "enum"));
1370
1371 for (const EnumCase &enumerant : enumInfo.getAllCases())
1372 for (const Availability &caseAvail :
1373 getAvailabilities(def: enumerant.getDef()))
1374 availClasses.try_emplace(Key: caseAvail.getClass(), Args: caseAvail);
1375 }
1376
1377 // Then generate implementation for each availability class.
1378 for (const auto &availClass : availClasses) {
1379 StringRef availClassName = availClass.getKey();
1380 Availability avail = availClass.getValue();
1381
1382 // Generate the implementation method signature.
1383 os << formatv(Fmt: "{0} {1}::{2}() {{\n", Vals: avail.getQueryFnRetType(),
1384 Vals: srcOp.getCppClassName(), Vals: avail.getQueryFnName());
1385
1386 // Create the variable for the final requirement and initialize it.
1387 os << formatv(Fmt: " {0} tblgen_overall = {1};\n", Vals: avail.getQueryFnRetType(),
1388 Vals: avail.getMergeInitializer());
1389
1390 // Update with the op's specific availability spec.
1391 for (const Availability &avail : opAvailabilities)
1392 if (avail.getClass() == availClassName &&
1393 (!avail.getMergeInstancePreparation().empty() ||
1394 !avail.getMergeActionCode().empty())) {
1395 os << " {\n "
1396 // Prepare this instance.
1397 << avail.getMergeInstancePreparation()
1398 << "\n "
1399 // Merge this instance.
1400 << std::string(
1401 tgfmt(fmt: avail.getMergeActionCode(),
1402 ctx: &fctx.addSubst(placeholder: "instance", subst: avail.getMergeInstance())))
1403 << ";\n }\n";
1404 }
1405
1406 // Update with enum attributes' specific availability spec.
1407 for (const NamedAttribute &namedAttr : srcOp.getAttributes()) {
1408 bool isOptional = namedAttr.attr.isOptional();
1409 Attribute attr =
1410 isOptional ? namedAttr.attr.getBaseAttr() : namedAttr.attr;
1411 if (!attr.isSubClassOf(className: "SPIRV_BitEnumAttr") &&
1412 !attr.isSubClassOf(className: "SPIRV_I32EnumAttr"))
1413 continue;
1414 EnumInfo enumInfo(attr.getDef().getValueAsDef(FieldName: "enum"));
1415
1416 // (enumerant, availability specification) pairs for this availability
1417 // class.
1418 SmallVector<std::pair<EnumCase, Availability>, 1> caseSpecs;
1419
1420 // Collect all cases' availability specs.
1421 for (const EnumCase &enumerant : enumInfo.getAllCases())
1422 for (const Availability &caseAvail :
1423 getAvailabilities(def: enumerant.getDef()))
1424 if (availClassName == caseAvail.getClass())
1425 caseSpecs.push_back(Elt: {enumerant, caseAvail});
1426
1427 // If this attribute kind does not have any availability spec from any of
1428 // its cases, no more work to do.
1429 if (caseSpecs.empty())
1430 continue;
1431
1432 std::string getter =
1433 formatv(Fmt: "this->{0}()", Vals: srcOp.getGetterName(name: namedAttr.name)).str();
1434 std::string attrVal = isOptional ? "*" + getter : getter;
1435 if (isOptional)
1436 os << formatv(Fmt: " if ({0})\n", Vals&: getter);
1437
1438 if (enumInfo.isBitEnum()) {
1439 // For BitEnumAttr, we need to iterate over each bit to query its
1440 // availability spec.
1441 os << formatv(Fmt: " for (unsigned i = 0; "
1442 "i < std::numeric_limits<{0}>::digits; ++i) {{\n",
1443 Vals: enumInfo.getUnderlyingType());
1444 os << formatv(Fmt: " {0}::{1} tblgen_attrVal = {2} & "
1445 "static_cast<{0}::{1}>(1 << i);\n",
1446 Vals: enumInfo.getCppNamespace(), Vals: enumInfo.getEnumClassName(),
1447 Vals&: attrVal);
1448 os << formatv(
1449 Fmt: " if (static_cast<{0}>(tblgen_attrVal) == 0) continue;\n",
1450 Vals: enumInfo.getUnderlyingType());
1451 } else {
1452 // For IntEnumAttr, we just need to query the value as a whole.
1453 os << " {\n";
1454 os << formatv(Fmt: " auto tblgen_attrVal = {0};\n", Vals&: attrVal);
1455 }
1456 os << formatv(Fmt: " auto tblgen_instance = {0}::{1}(tblgen_attrVal);\n",
1457 Vals: enumInfo.getCppNamespace(), Vals: avail.getQueryFnName());
1458 os << " if (tblgen_instance) "
1459 // TODO` here once ODS supports
1460 // dialect-specific contents so that we can use not implementing the
1461 // availability interface as indication of no requirements.
1462 << std::string(tgfmt(fmt: caseSpecs.front().second.getMergeActionCode(),
1463 ctx: &fctx.addSubst(placeholder: "instance", subst: "*tblgen_instance")))
1464 << ";\n";
1465 os << " }\n";
1466 }
1467
1468 os << " return tblgen_overall;\n";
1469 os << "}\n";
1470 }
1471}
1472
1473static bool emitAvailabilityImpl(const RecordKeeper &records, raw_ostream &os) {
1474 llvm::emitSourceFileHeader(Desc: "SPIR-V Op Availability Implementations", OS&: os,
1475 Record: records);
1476
1477 auto defs = records.getAllDerivedDefinitions(ClassName: "SPIRV_Op");
1478 for (const auto *def : defs) {
1479 Operator op(def);
1480 if (def->getValueAsBit(FieldName: "autogenAvailability"))
1481 emitAvailabilityImpl(srcOp: op, os);
1482 }
1483 return false;
1484}
1485
1486//===----------------------------------------------------------------------===//
1487// Op Availability Implementation Hook Registration
1488//===----------------------------------------------------------------------===//
1489
1490static mlir::GenRegistration
1491 genOpAvailabilityImpl("gen-spirv-avail-impls",
1492 "Generate SPIR-V operation utility definitions",
1493 [](const RecordKeeper &records, raw_ostream &os) {
1494 return emitAvailabilityImpl(records, os);
1495 });
1496
1497//===----------------------------------------------------------------------===//
1498// SPIR-V Capability Implication AutoGen
1499//===----------------------------------------------------------------------===//
1500
1501static bool emitCapabilityImplication(const RecordKeeper &records,
1502 raw_ostream &os) {
1503 llvm::emitSourceFileHeader(Desc: "SPIR-V Capability Implication", OS&: os, Record: records);
1504
1505 EnumInfo enumInfo(
1506 records.getDef(Name: "SPIRV_CapabilityAttr")->getValueAsDef(FieldName: "enum"));
1507
1508 os << "ArrayRef<spirv::Capability> "
1509 "spirv::getDirectImpliedCapabilities(spirv::Capability cap) {\n"
1510 << " switch (cap) {\n"
1511 << " default: return {};\n";
1512 for (const EnumCase &enumerant : enumInfo.getAllCases()) {
1513 const Record &def = enumerant.getDef();
1514 if (!def.getValue(Name: "implies"))
1515 continue;
1516
1517 std::vector<const Record *> impliedCapsDefs =
1518 def.getValueAsListOfDefs(FieldName: "implies");
1519 os << " case spirv::Capability::" << enumerant.getSymbol()
1520 << ": {static const spirv::Capability implies[" << impliedCapsDefs.size()
1521 << "] = {";
1522 llvm::interleaveComma(c: impliedCapsDefs, os, each_fn: [&](const Record *capDef) {
1523 os << "spirv::Capability::" << EnumCase(capDef).getSymbol();
1524 });
1525 os << "}; return ArrayRef<spirv::Capability>(implies, "
1526 << impliedCapsDefs.size() << "); }\n";
1527 }
1528 os << " }\n";
1529 os << "}\n";
1530
1531 return false;
1532}
1533
1534//===----------------------------------------------------------------------===//
1535// SPIR-V Capability Implication Hook Registration
1536//===----------------------------------------------------------------------===//
1537
1538static mlir::GenRegistration
1539 genCapabilityImplication("gen-spirv-capability-implication",
1540 "Generate utility function to return implied "
1541 "capabilities for a given capability",
1542 [](const RecordKeeper &records, raw_ostream &os) {
1543 return emitCapabilityImplication(records, os);
1544 });
1545