1//===- SPIRVAttributes.cpp - SPIR-V attribute definitions -----------------===//
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 "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
10#include "mlir/Dialect/SPIRV/IR/SPIRVDialect.h"
11#include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h"
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/DialectImplementation.h"
14#include "llvm/ADT/TypeSwitch.h"
15#include "llvm/Support/InterleavedRange.h"
16
17using namespace mlir;
18using namespace mlir::spirv;
19
20//===----------------------------------------------------------------------===//
21// TableGen'erated attribute utility functions
22//===----------------------------------------------------------------------===//
23
24namespace mlir {
25namespace spirv {
26#include "mlir/Dialect/SPIRV/IR/SPIRVAttrUtils.inc"
27} // namespace spirv
28
29//===----------------------------------------------------------------------===//
30// Attribute storage classes
31//===----------------------------------------------------------------------===//
32
33namespace spirv {
34namespace detail {
35
36struct InterfaceVarABIAttributeStorage : public AttributeStorage {
37 using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
38
39 InterfaceVarABIAttributeStorage(Attribute descriptorSet, Attribute binding,
40 Attribute storageClass)
41 : descriptorSet(descriptorSet), binding(binding),
42 storageClass(storageClass) {}
43
44 bool operator==(const KeyTy &key) const {
45 return std::get<0>(t: key) == descriptorSet && std::get<1>(t: key) == binding &&
46 std::get<2>(t: key) == storageClass;
47 }
48
49 static InterfaceVarABIAttributeStorage *
50 construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
51 return new (allocator.allocate<InterfaceVarABIAttributeStorage>())
52 InterfaceVarABIAttributeStorage(std::get<0>(t: key), std::get<1>(t: key),
53 std::get<2>(t: key));
54 }
55
56 Attribute descriptorSet;
57 Attribute binding;
58 Attribute storageClass;
59};
60
61struct VerCapExtAttributeStorage : public AttributeStorage {
62 using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
63
64 VerCapExtAttributeStorage(Attribute version, Attribute capabilities,
65 Attribute extensions)
66 : version(version), capabilities(capabilities), extensions(extensions) {}
67
68 bool operator==(const KeyTy &key) const {
69 return std::get<0>(t: key) == version && std::get<1>(t: key) == capabilities &&
70 std::get<2>(t: key) == extensions;
71 }
72
73 static VerCapExtAttributeStorage *
74 construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
75 return new (allocator.allocate<VerCapExtAttributeStorage>())
76 VerCapExtAttributeStorage(std::get<0>(t: key), std::get<1>(t: key),
77 std::get<2>(t: key));
78 }
79
80 Attribute version;
81 Attribute capabilities;
82 Attribute extensions;
83};
84
85struct TargetEnvAttributeStorage : public AttributeStorage {
86 using KeyTy =
87 std::tuple<Attribute, ClientAPI, Vendor, DeviceType, uint32_t, Attribute>;
88
89 TargetEnvAttributeStorage(Attribute triple, ClientAPI clientAPI,
90 Vendor vendorID, DeviceType deviceType,
91 uint32_t deviceID, Attribute limits)
92 : triple(triple), limits(limits), clientAPI(clientAPI),
93 vendorID(vendorID), deviceType(deviceType), deviceID(deviceID) {}
94
95 bool operator==(const KeyTy &key) const {
96 return key == std::make_tuple(args: triple, args: clientAPI, args: vendorID, args: deviceType,
97 args: deviceID, args: limits);
98 }
99
100 static TargetEnvAttributeStorage *
101 construct(AttributeStorageAllocator &allocator, const KeyTy &key) {
102 return new (allocator.allocate<TargetEnvAttributeStorage>())
103 TargetEnvAttributeStorage(std::get<0>(t: key), std::get<1>(t: key),
104 std::get<2>(t: key), std::get<3>(t: key),
105 std::get<4>(t: key), std::get<5>(t: key));
106 }
107
108 Attribute triple;
109 Attribute limits;
110 ClientAPI clientAPI;
111 Vendor vendorID;
112 DeviceType deviceType;
113 uint32_t deviceID;
114};
115} // namespace detail
116} // namespace spirv
117} // namespace mlir
118
119//===----------------------------------------------------------------------===//
120// InterfaceVarABIAttr
121//===----------------------------------------------------------------------===//
122
123spirv::InterfaceVarABIAttr
124spirv::InterfaceVarABIAttr::get(uint32_t descriptorSet, uint32_t binding,
125 std::optional<spirv::StorageClass> storageClass,
126 MLIRContext *context) {
127 Builder b(context);
128 auto descriptorSetAttr = b.getI32IntegerAttr(value: descriptorSet);
129 auto bindingAttr = b.getI32IntegerAttr(value: binding);
130 auto storageClassAttr =
131 storageClass ? b.getI32IntegerAttr(value: static_cast<uint32_t>(*storageClass))
132 : IntegerAttr();
133 return get(descriptorSet: descriptorSetAttr, binding: bindingAttr, storageClass: storageClassAttr);
134}
135
136spirv::InterfaceVarABIAttr
137spirv::InterfaceVarABIAttr::get(IntegerAttr descriptorSet, IntegerAttr binding,
138 IntegerAttr storageClass) {
139 assert(descriptorSet && binding);
140 MLIRContext *context = descriptorSet.getContext();
141 return Base::get(ctx: context, args&: descriptorSet, args&: binding, args&: storageClass);
142}
143
144StringRef spirv::InterfaceVarABIAttr::getKindName() {
145 return "interface_var_abi";
146}
147
148uint32_t spirv::InterfaceVarABIAttr::getBinding() {
149 return cast<IntegerAttr>(Val&: getImpl()->binding).getInt();
150}
151
152uint32_t spirv::InterfaceVarABIAttr::getDescriptorSet() {
153 return cast<IntegerAttr>(Val&: getImpl()->descriptorSet).getInt();
154}
155
156std::optional<spirv::StorageClass>
157spirv::InterfaceVarABIAttr::getStorageClass() {
158 if (getImpl()->storageClass)
159 return static_cast<spirv::StorageClass>(
160 cast<IntegerAttr>(Val&: getImpl()->storageClass).getValue().getZExtValue());
161 return std::nullopt;
162}
163
164LogicalResult spirv::InterfaceVarABIAttr::verifyInvariants(
165 function_ref<InFlightDiagnostic()> emitError, IntegerAttr descriptorSet,
166 IntegerAttr binding, IntegerAttr storageClass) {
167 if (!descriptorSet.getType().isSignlessInteger(width: 32))
168 return emitError() << "expected 32-bit integer for descriptor set";
169
170 if (!binding.getType().isSignlessInteger(width: 32))
171 return emitError() << "expected 32-bit integer for binding";
172
173 if (storageClass) {
174 if (auto storageClassAttr = cast<IntegerAttr>(Val&: storageClass)) {
175 auto storageClassValue =
176 spirv::symbolizeStorageClass(storageClassAttr.getInt());
177 if (!storageClassValue)
178 return emitError() << "unknown storage class";
179 } else {
180 return emitError() << "expected valid storage class";
181 }
182 }
183
184 return success();
185}
186
187//===----------------------------------------------------------------------===//
188// VerCapExtAttr
189//===----------------------------------------------------------------------===//
190
191spirv::VerCapExtAttr spirv::VerCapExtAttr::get(
192 spirv::Version version, ArrayRef<spirv::Capability> capabilities,
193 ArrayRef<spirv::Extension> extensions, MLIRContext *context) {
194 Builder b(context);
195
196 auto versionAttr = b.getI32IntegerAttr(value: static_cast<uint32_t>(version));
197
198 SmallVector<Attribute, 4> capAttrs;
199 capAttrs.reserve(N: capabilities.size());
200 for (spirv::Capability cap : capabilities)
201 capAttrs.push_back(Elt: b.getI32IntegerAttr(value: static_cast<uint32_t>(cap)));
202
203 SmallVector<Attribute, 4> extAttrs;
204 extAttrs.reserve(N: extensions.size());
205 for (spirv::Extension ext : extensions)
206 extAttrs.push_back(Elt: b.getStringAttr(bytes: spirv::stringifyExtension(ext)));
207
208 return get(version: versionAttr, capabilities: b.getArrayAttr(value: capAttrs), extensions: b.getArrayAttr(value: extAttrs));
209}
210
211spirv::VerCapExtAttr spirv::VerCapExtAttr::get(IntegerAttr version,
212 ArrayAttr capabilities,
213 ArrayAttr extensions) {
214 assert(version && capabilities && extensions);
215 MLIRContext *context = version.getContext();
216 return Base::get(ctx: context, args&: version, args&: capabilities, args&: extensions);
217}
218
219StringRef spirv::VerCapExtAttr::getKindName() { return "vce"; }
220
221spirv::Version spirv::VerCapExtAttr::getVersion() {
222 return static_cast<spirv::Version>(
223 cast<IntegerAttr>(Val&: getImpl()->version).getValue().getZExtValue());
224}
225
226spirv::VerCapExtAttr::ext_iterator::ext_iterator(ArrayAttr::iterator it)
227 : llvm::mapped_iterator<ArrayAttr::iterator,
228 spirv::Extension (*)(Attribute)>(
229 it, [](Attribute attr) {
230 return *symbolizeExtension(cast<StringAttr>(Val&: attr).getValue());
231 }) {}
232
233spirv::VerCapExtAttr::ext_range spirv::VerCapExtAttr::getExtensions() {
234 auto range = getExtensionsAttr().getValue();
235 return {ext_iterator(range.begin()), ext_iterator(range.end())};
236}
237
238ArrayAttr spirv::VerCapExtAttr::getExtensionsAttr() {
239 return cast<ArrayAttr>(Val&: getImpl()->extensions);
240}
241
242spirv::VerCapExtAttr::cap_iterator::cap_iterator(ArrayAttr::iterator it)
243 : llvm::mapped_iterator<ArrayAttr::iterator,
244 spirv::Capability (*)(Attribute)>(
245 it, [](Attribute attr) {
246 return *symbolizeCapability(
247 cast<IntegerAttr>(Val&: attr).getValue().getZExtValue());
248 }) {}
249
250spirv::VerCapExtAttr::cap_range spirv::VerCapExtAttr::getCapabilities() {
251 auto range = getCapabilitiesAttr().getValue();
252 return {cap_iterator(range.begin()), cap_iterator(range.end())};
253}
254
255ArrayAttr spirv::VerCapExtAttr::getCapabilitiesAttr() {
256 return cast<ArrayAttr>(Val&: getImpl()->capabilities);
257}
258
259LogicalResult spirv::VerCapExtAttr::verifyInvariants(
260 function_ref<InFlightDiagnostic()> emitError, IntegerAttr version,
261 ArrayAttr capabilities, ArrayAttr extensions) {
262 if (!version.getType().isSignlessInteger(width: 32))
263 return emitError() << "expected 32-bit integer for version";
264
265 if (!llvm::all_of(Range: capabilities.getValue(), P: [](Attribute attr) {
266 if (auto intAttr = dyn_cast<IntegerAttr>(Val&: attr))
267 if (spirv::symbolizeCapability(intAttr.getValue().getZExtValue()))
268 return true;
269 return false;
270 }))
271 return emitError() << "unknown capability in capability list";
272
273 if (!llvm::all_of(Range: extensions.getValue(), P: [](Attribute attr) {
274 if (auto strAttr = dyn_cast<StringAttr>(Val&: attr))
275 if (spirv::symbolizeExtension(strAttr.getValue()))
276 return true;
277 return false;
278 }))
279 return emitError() << "unknown extension in extension list";
280
281 return success();
282}
283
284//===----------------------------------------------------------------------===//
285// TargetEnvAttr
286//===----------------------------------------------------------------------===//
287
288spirv::TargetEnvAttr spirv::TargetEnvAttr::get(
289 spirv::VerCapExtAttr triple, ResourceLimitsAttr limits, ClientAPI clientAPI,
290 Vendor vendorID, DeviceType deviceType, uint32_t deviceID) {
291 assert(triple && limits && "expected valid triple and limits");
292 MLIRContext *context = triple.getContext();
293 return Base::get(ctx: context, args&: triple, args&: clientAPI, args&: vendorID, args&: deviceType, args&: deviceID,
294 args&: limits);
295}
296
297StringRef spirv::TargetEnvAttr::getKindName() { return "target_env"; }
298
299spirv::VerCapExtAttr spirv::TargetEnvAttr::getTripleAttr() const {
300 return cast<spirv::VerCapExtAttr>(Val&: getImpl()->triple);
301}
302
303spirv::Version spirv::TargetEnvAttr::getVersion() const {
304 return getTripleAttr().getVersion();
305}
306
307spirv::VerCapExtAttr::ext_range spirv::TargetEnvAttr::getExtensions() {
308 return getTripleAttr().getExtensions();
309}
310
311ArrayAttr spirv::TargetEnvAttr::getExtensionsAttr() {
312 return getTripleAttr().getExtensionsAttr();
313}
314
315spirv::VerCapExtAttr::cap_range spirv::TargetEnvAttr::getCapabilities() {
316 return getTripleAttr().getCapabilities();
317}
318
319ArrayAttr spirv::TargetEnvAttr::getCapabilitiesAttr() {
320 return getTripleAttr().getCapabilitiesAttr();
321}
322
323spirv::ClientAPI spirv::TargetEnvAttr::getClientAPI() const {
324 return getImpl()->clientAPI;
325}
326
327spirv::Vendor spirv::TargetEnvAttr::getVendorID() const {
328 return getImpl()->vendorID;
329}
330
331spirv::DeviceType spirv::TargetEnvAttr::getDeviceType() const {
332 return getImpl()->deviceType;
333}
334
335uint32_t spirv::TargetEnvAttr::getDeviceID() const {
336 return getImpl()->deviceID;
337}
338
339spirv::ResourceLimitsAttr spirv::TargetEnvAttr::getResourceLimits() const {
340 return cast<spirv::ResourceLimitsAttr>(Val&: getImpl()->limits);
341}
342
343//===----------------------------------------------------------------------===//
344// ODS Generated Attributes
345//===----------------------------------------------------------------------===//
346
347#define GET_ATTRDEF_CLASSES
348#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.cpp.inc"
349
350//===----------------------------------------------------------------------===//
351// Attribute Parsing
352//===----------------------------------------------------------------------===//
353
354/// Parses a comma-separated list of keywords, invokes `processKeyword` on each
355/// of the parsed keyword, and returns failure if any error occurs.
356static ParseResult
357parseKeywordList(DialectAsmParser &parser,
358 function_ref<LogicalResult(SMLoc, StringRef)> processKeyword) {
359 if (parser.parseLSquare())
360 return failure();
361
362 // Special case for empty list.
363 if (succeeded(Result: parser.parseOptionalRSquare()))
364 return success();
365
366 // Keep parsing the keyword and an optional comma following it. If the comma
367 // is successfully parsed, then we have more keywords to parse.
368 if (failed(Result: parser.parseCommaSeparatedList(parseElementFn: [&]() {
369 auto loc = parser.getCurrentLocation();
370 StringRef keyword;
371 if (parser.parseKeyword(keyword: &keyword) ||
372 failed(Result: processKeyword(loc, keyword)))
373 return failure();
374 return success();
375 })))
376 return failure();
377 return parser.parseRSquare();
378}
379
380/// Parses a spirv::InterfaceVarABIAttr.
381static Attribute parseInterfaceVarABIAttr(DialectAsmParser &parser) {
382 if (parser.parseLess())
383 return {};
384
385 Builder &builder = parser.getBuilder();
386
387 if (parser.parseLParen())
388 return {};
389
390 IntegerAttr descriptorSetAttr;
391 {
392 auto loc = parser.getCurrentLocation();
393 uint32_t descriptorSet = 0;
394 auto descriptorSetParseResult = parser.parseOptionalInteger(result&: descriptorSet);
395
396 if (!descriptorSetParseResult.has_value() ||
397 failed(Result: *descriptorSetParseResult)) {
398 parser.emitError(loc, message: "missing descriptor set");
399 return {};
400 }
401 descriptorSetAttr = builder.getI32IntegerAttr(value: descriptorSet);
402 }
403
404 if (parser.parseComma())
405 return {};
406
407 IntegerAttr bindingAttr;
408 {
409 auto loc = parser.getCurrentLocation();
410 uint32_t binding = 0;
411 auto bindingParseResult = parser.parseOptionalInteger(result&: binding);
412
413 if (!bindingParseResult.has_value() || failed(Result: *bindingParseResult)) {
414 parser.emitError(loc, message: "missing binding");
415 return {};
416 }
417 bindingAttr = builder.getI32IntegerAttr(value: binding);
418 }
419
420 if (parser.parseRParen())
421 return {};
422
423 IntegerAttr storageClassAttr;
424 {
425 if (succeeded(Result: parser.parseOptionalComma())) {
426 auto loc = parser.getCurrentLocation();
427 StringRef storageClass;
428 if (parser.parseKeyword(keyword: &storageClass))
429 return {};
430
431 if (auto storageClassSymbol =
432 spirv::symbolizeStorageClass(storageClass)) {
433 storageClassAttr = builder.getI32IntegerAttr(
434 value: static_cast<uint32_t>(*storageClassSymbol));
435 } else {
436 parser.emitError(loc, message: "unknown storage class: ") << storageClass;
437 return {};
438 }
439 }
440 }
441
442 if (parser.parseGreater())
443 return {};
444
445 return spirv::InterfaceVarABIAttr::get(descriptorSet: descriptorSetAttr, binding: bindingAttr,
446 storageClass: storageClassAttr);
447}
448
449static Attribute parseVerCapExtAttr(DialectAsmParser &parser) {
450 if (parser.parseLess())
451 return {};
452
453 Builder &builder = parser.getBuilder();
454
455 IntegerAttr versionAttr;
456 {
457 auto loc = parser.getCurrentLocation();
458 StringRef version;
459 if (parser.parseKeyword(keyword: &version) || parser.parseComma())
460 return {};
461
462 if (auto versionSymbol = spirv::symbolizeVersion(version)) {
463 versionAttr =
464 builder.getI32IntegerAttr(value: static_cast<uint32_t>(*versionSymbol));
465 } else {
466 parser.emitError(loc, message: "unknown version: ") << version;
467 return {};
468 }
469 }
470
471 ArrayAttr capabilitiesAttr;
472 {
473 SmallVector<Attribute, 4> capabilities;
474 SMLoc errorloc;
475 StringRef errorKeyword;
476
477 auto processCapability = [&](SMLoc loc, StringRef capability) {
478 if (auto capSymbol = spirv::symbolizeCapability(capability)) {
479 capabilities.push_back(
480 Elt: builder.getI32IntegerAttr(value: static_cast<uint32_t>(*capSymbol)));
481 return success();
482 }
483 return errorloc = loc, errorKeyword = capability, failure();
484 };
485 if (parseKeywordList(parser, processKeyword: processCapability) || parser.parseComma()) {
486 if (!errorKeyword.empty())
487 parser.emitError(loc: errorloc, message: "unknown capability: ") << errorKeyword;
488 return {};
489 }
490
491 capabilitiesAttr = builder.getArrayAttr(value: capabilities);
492 }
493
494 ArrayAttr extensionsAttr;
495 {
496 SmallVector<Attribute, 1> extensions;
497 SMLoc errorloc;
498 StringRef errorKeyword;
499
500 auto processExtension = [&](SMLoc loc, StringRef extension) {
501 if (spirv::symbolizeExtension(extension)) {
502 extensions.push_back(Elt: builder.getStringAttr(bytes: extension));
503 return success();
504 }
505 return errorloc = loc, errorKeyword = extension, failure();
506 };
507 if (parseKeywordList(parser, processKeyword: processExtension)) {
508 if (!errorKeyword.empty())
509 parser.emitError(loc: errorloc, message: "unknown extension: ") << errorKeyword;
510 return {};
511 }
512
513 extensionsAttr = builder.getArrayAttr(value: extensions);
514 }
515
516 if (parser.parseGreater())
517 return {};
518
519 return spirv::VerCapExtAttr::get(version: versionAttr, capabilities: capabilitiesAttr,
520 extensions: extensionsAttr);
521}
522
523/// Parses a spirv::TargetEnvAttr.
524static Attribute parseTargetEnvAttr(DialectAsmParser &parser) {
525 if (parser.parseLess())
526 return {};
527
528 spirv::VerCapExtAttr tripleAttr;
529 if (parser.parseAttribute(result&: tripleAttr) || parser.parseComma())
530 return {};
531
532 auto clientAPI = spirv::ClientAPI::Unknown;
533 if (succeeded(Result: parser.parseOptionalKeyword(keyword: "api"))) {
534 if (parser.parseEqual())
535 return {};
536 auto loc = parser.getCurrentLocation();
537 StringRef apiStr;
538 if (parser.parseKeyword(keyword: &apiStr))
539 return {};
540 if (auto apiSymbol = spirv::symbolizeClientAPI(apiStr))
541 clientAPI = *apiSymbol;
542 else
543 parser.emitError(loc, message: "unknown client API: ") << apiStr;
544 if (parser.parseComma())
545 return {};
546 }
547
548 // Parse [vendor[:device-type[:device-id]]]
549 Vendor vendorID = Vendor::Unknown;
550 DeviceType deviceType = DeviceType::Unknown;
551 uint32_t deviceID = spirv::TargetEnvAttr::kUnknownDeviceID;
552 {
553 auto loc = parser.getCurrentLocation();
554 StringRef vendorStr;
555 if (succeeded(Result: parser.parseOptionalKeyword(keyword: &vendorStr))) {
556 if (auto vendorSymbol = spirv::symbolizeVendor(vendorStr))
557 vendorID = *vendorSymbol;
558 else
559 parser.emitError(loc, message: "unknown vendor: ") << vendorStr;
560
561 if (succeeded(Result: parser.parseOptionalColon())) {
562 loc = parser.getCurrentLocation();
563 StringRef deviceTypeStr;
564 if (parser.parseKeyword(keyword: &deviceTypeStr))
565 return {};
566 if (auto deviceTypeSymbol = spirv::symbolizeDeviceType(deviceTypeStr))
567 deviceType = *deviceTypeSymbol;
568 else
569 parser.emitError(loc, message: "unknown device type: ") << deviceTypeStr;
570
571 if (succeeded(Result: parser.parseOptionalColon())) {
572 loc = parser.getCurrentLocation();
573 if (parser.parseInteger(result&: deviceID))
574 return {};
575 }
576 }
577 if (parser.parseComma())
578 return {};
579 }
580 }
581
582 ResourceLimitsAttr limitsAttr;
583 if (parser.parseAttribute(result&: limitsAttr) || parser.parseGreater())
584 return {};
585
586 return spirv::TargetEnvAttr::get(triple: tripleAttr, limits: limitsAttr, clientAPI, vendorID,
587 deviceType, deviceID);
588}
589
590Attribute SPIRVDialect::parseAttribute(DialectAsmParser &parser,
591 Type type) const {
592 // Parse the kind keyword first.
593 StringRef attrKind;
594 Attribute attr;
595 OptionalParseResult result =
596 generatedAttributeParser(parser, mnemonic: &attrKind, type, value&: attr);
597 if (result.has_value()) {
598 if (type && attr && !isa<TypedAttr>(Val: attr)) {
599 parser.emitError(loc: parser.getNameLoc(), message: "unexpected type");
600 return {};
601 }
602 return attr;
603 }
604
605 // The remaining SPIR-V attributes are dictionaries so they do not have type.
606 if (type) {
607 parser.emitError(loc: parser.getNameLoc(), message: "unexpected type");
608 return {};
609 }
610
611 if (attrKind == spirv::TargetEnvAttr::getKindName())
612 return parseTargetEnvAttr(parser);
613 if (attrKind == spirv::VerCapExtAttr::getKindName())
614 return parseVerCapExtAttr(parser);
615 if (attrKind == spirv::InterfaceVarABIAttr::getKindName())
616 return parseInterfaceVarABIAttr(parser);
617
618 parser.emitError(loc: parser.getNameLoc(), message: "unknown SPIR-V attribute kind: ")
619 << attrKind;
620 return {};
621}
622
623//===----------------------------------------------------------------------===//
624// Attribute Printing
625//===----------------------------------------------------------------------===//
626
627static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer) {
628 printer << spirv::VerCapExtAttr::getKindName() << "<"
629 << spirv::stringifyVersion(triple.getVersion()) << ", "
630 << llvm::interleaved_array(R: llvm::map_range(
631 C: triple.getCapabilities(), F: spirv::stringifyCapability))
632 << ", "
633 << llvm::interleaved_array(
634 R: triple.getExtensionsAttr().getAsValueRange<StringAttr>())
635 << ">";
636}
637
638static void print(spirv::TargetEnvAttr targetEnv, DialectAsmPrinter &printer) {
639 printer << spirv::TargetEnvAttr::getKindName() << "<#spirv.";
640 print(triple: targetEnv.getTripleAttr(), printer);
641 auto clientAPI = targetEnv.getClientAPI();
642 if (clientAPI != spirv::ClientAPI::Unknown)
643 printer << ", api=" << clientAPI;
644 spirv::Vendor vendorID = targetEnv.getVendorID();
645 spirv::DeviceType deviceType = targetEnv.getDeviceType();
646 uint32_t deviceID = targetEnv.getDeviceID();
647 if (vendorID != spirv::Vendor::Unknown) {
648 printer << ", " << spirv::stringifyVendor(vendorID);
649 if (deviceType != spirv::DeviceType::Unknown) {
650 printer << ":" << spirv::stringifyDeviceType(deviceType);
651 if (deviceID != spirv::TargetEnvAttr::kUnknownDeviceID)
652 printer << ":" << deviceID;
653 }
654 }
655 printer << ", " << targetEnv.getResourceLimits() << ">";
656}
657
658static void print(spirv::InterfaceVarABIAttr interfaceVarABIAttr,
659 DialectAsmPrinter &printer) {
660 printer << spirv::InterfaceVarABIAttr::getKindName() << "<("
661 << interfaceVarABIAttr.getDescriptorSet() << ", "
662 << interfaceVarABIAttr.getBinding() << ")";
663 auto storageClass = interfaceVarABIAttr.getStorageClass();
664 if (storageClass)
665 printer << ", " << spirv::stringifyStorageClass(*storageClass);
666 printer << ">";
667}
668
669void SPIRVDialect::printAttribute(Attribute attr,
670 DialectAsmPrinter &printer) const {
671 if (succeeded(Result: generatedAttributePrinter(def: attr, printer)))
672 return;
673
674 if (auto targetEnv = dyn_cast<TargetEnvAttr>(Val&: attr))
675 print(targetEnv, printer);
676 else if (auto vceAttr = dyn_cast<VerCapExtAttr>(Val&: attr))
677 print(triple: vceAttr, printer);
678 else if (auto interfaceVarABIAttr = dyn_cast<InterfaceVarABIAttr>(Val&: attr))
679 print(interfaceVarABIAttr, printer);
680 else
681 llvm_unreachable("unhandled SPIR-V attribute kind");
682}
683
684//===----------------------------------------------------------------------===//
685// SPIR-V Dialect
686//===----------------------------------------------------------------------===//
687
688void spirv::SPIRVDialect::registerAttributes() {
689 addAttributes<InterfaceVarABIAttr, TargetEnvAttr, VerCapExtAttr>();
690 addAttributes<
691#define GET_ATTRDEF_LIST
692#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.cpp.inc"
693 >();
694}
695