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