14 #include "llvm/ADT/TypeSwitch.h"
25 #include "mlir/Dialect/SPIRV/IR/SPIRVAttrUtils.inc"
36 using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
40 : descriptorSet(descriptorSet), binding(binding),
41 storageClass(storageClass) {}
44 return std::get<0>(key) == descriptorSet && std::get<1>(key) == binding &&
45 std::get<2>(key) == storageClass;
61 using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
65 : version(version), capabilities(capabilities), extensions(extensions) {}
68 return std::get<0>(key) == version && std::get<1>(key) == capabilities &&
69 std::get<2>(key) == extensions;
86 std::tuple<Attribute, ClientAPI, Vendor, DeviceType, uint32_t, Attribute>;
89 Vendor vendorID, DeviceType deviceType,
91 : triple(triple), limits(limits), clientAPI(clientAPI),
92 vendorID(vendorID), deviceType(deviceType), deviceID(deviceID) {}
95 return key == std::make_tuple(triple, clientAPI, vendorID, deviceType,
103 std::get<2>(key), std::get<3>(key),
104 std::get<4>(key), std::get<5>(key));
124 std::optional<spirv::StorageClass> storageClass,
129 auto storageClassAttr =
132 return get(descriptorSetAttr, bindingAttr, storageClassAttr);
137 IntegerAttr storageClass) {
138 assert(descriptorSet && binding);
140 return Base::get(context, descriptorSet, binding, storageClass);
144 return "interface_var_abi";
148 return getImpl()->binding.cast<IntegerAttr>().getInt();
152 return getImpl()->descriptorSet.cast<IntegerAttr>().getInt();
155 std::optional<spirv::StorageClass>
157 if (getImpl()->storageClass)
158 return static_cast<spirv::StorageClass
>(
159 getImpl()->storageClass.cast<IntegerAttr>().getValue().getZExtValue());
165 IntegerAttr binding, IntegerAttr storageClass) {
166 if (!descriptorSet.getType().isSignlessInteger(32))
167 return emitError() <<
"expected 32-bit integer for descriptor set";
169 if (!binding.getType().isSignlessInteger(32))
170 return emitError() <<
"expected 32-bit integer for binding";
173 if (
auto storageClassAttr = storageClass.cast<IntegerAttr>()) {
174 auto storageClassValue =
175 spirv::symbolizeStorageClass(storageClassAttr.getInt());
176 if (!storageClassValue)
177 return emitError() <<
"unknown storage class";
179 return emitError() <<
"expected valid storage class";
195 auto versionAttr = b.getI32IntegerAttr(
static_cast<uint32_t
>(version));
198 capAttrs.reserve(capabilities.size());
199 for (spirv::Capability cap : capabilities)
200 capAttrs.push_back(b.getI32IntegerAttr(
static_cast<uint32_t
>(cap)));
203 extAttrs.reserve(extensions.size());
204 for (spirv::Extension ext : extensions)
205 extAttrs.push_back(b.getStringAttr(spirv::stringifyExtension(ext)));
207 return get(versionAttr, b.getArrayAttr(capAttrs), b.getArrayAttr(extAttrs));
211 ArrayAttr capabilities,
212 ArrayAttr extensions) {
213 assert(version && capabilities && extensions);
215 return Base::get(context, version, capabilities, extensions);
221 return static_cast<spirv::Version
>(
222 getImpl()->version.cast<IntegerAttr>().getValue().getZExtValue());
226 :
llvm::mapped_iterator<ArrayAttr::iterator,
229 return *symbolizeExtension(attr.cast<StringAttr>().getValue());
238 return getImpl()->extensions.cast<ArrayAttr>();
242 :
llvm::mapped_iterator<ArrayAttr::iterator,
245 return *symbolizeCapability(
246 attr.cast<IntegerAttr>().getValue().getZExtValue());
255 return getImpl()->capabilities.cast<ArrayAttr>();
260 IntegerAttr version, ArrayAttr capabilities,
261 ArrayAttr extensions) {
262 if (!version.getType().isSignlessInteger(32))
263 return emitError() <<
"expected 32-bit integer for version";
265 if (!llvm::all_of(capabilities.getValue(), [](
Attribute attr) {
266 if (auto intAttr = attr.dyn_cast<IntegerAttr>())
267 if (spirv::symbolizeCapability(intAttr.getValue().getZExtValue()))
271 return emitError() <<
"unknown capability in capability list";
273 if (!llvm::all_of(extensions.getValue(), [](
Attribute attr) {
274 if (auto strAttr = attr.dyn_cast<StringAttr>())
275 if (spirv::symbolizeExtension(strAttr.getValue()))
279 return emitError() <<
"unknown extension in extension list";
290 Vendor vendorID, DeviceType deviceType, uint32_t deviceID) {
291 assert(triple && limits &&
"expected valid triple and limits");
293 return Base::get(context, triple, clientAPI, vendorID, deviceType, deviceID,
304 return getTripleAttr().getVersion();
308 return getTripleAttr().getExtensions();
312 return getTripleAttr().getExtensionsAttr();
316 return getTripleAttr().getCapabilities();
320 return getTripleAttr().getCapabilitiesAttr();
340 return getImpl()->limits.cast<spirv::ResourceLimitsAttr>();
347 #define GET_ATTRDEF_CLASSES
348 #include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.cpp.inc"
369 auto loc = parser.getCurrentLocation();
371 if (parser.parseKeyword(&keyword) ||
372 failed(processKeyword(loc, keyword)))
390 IntegerAttr descriptorSetAttr;
393 uint32_t descriptorSet = 0;
396 if (!descriptorSetParseResult.has_value() ||
397 failed(*descriptorSetParseResult)) {
398 parser.
emitError(loc,
"missing descriptor set");
407 IntegerAttr bindingAttr;
410 uint32_t binding = 0;
413 if (!bindingParseResult.has_value() ||
failed(*bindingParseResult)) {
414 parser.
emitError(loc,
"missing binding");
423 IntegerAttr storageClassAttr;
427 StringRef storageClass;
431 if (
auto storageClassSymbol =
432 spirv::symbolizeStorageClass(storageClass)) {
434 static_cast<uint32_t
>(*storageClassSymbol));
436 parser.
emitError(loc,
"unknown storage class: ") << storageClass;
455 IntegerAttr versionAttr;
462 if (
auto versionSymbol = spirv::symbolizeVersion(version)) {
466 parser.
emitError(loc,
"unknown version: ") << version;
471 ArrayAttr capabilitiesAttr;
475 StringRef errorKeyword;
477 auto processCapability = [&](SMLoc loc, StringRef capability) {
478 if (
auto capSymbol = spirv::symbolizeCapability(capability)) {
479 capabilities.push_back(
483 return errorloc = loc, errorKeyword = capability,
failure();
486 if (!errorKeyword.empty())
487 parser.
emitError(errorloc,
"unknown capability: ") << errorKeyword;
494 ArrayAttr extensionsAttr;
498 StringRef errorKeyword;
500 auto processExtension = [&](SMLoc loc, StringRef extension) {
501 if (spirv::symbolizeExtension(extension)) {
505 return errorloc = loc, errorKeyword = extension,
failure();
508 if (!errorKeyword.empty())
509 parser.
emitError(errorloc,
"unknown extension: ") << errorKeyword;
532 auto clientAPI = spirv::ClientAPI::Unknown;
540 if (
auto apiSymbol = spirv::symbolizeClientAPI(apiStr))
541 clientAPI = *apiSymbol;
543 parser.
emitError(loc,
"unknown client API: ") << apiStr;
549 Vendor vendorID = Vendor::Unknown;
550 DeviceType deviceType = DeviceType::Unknown;
556 if (
auto vendorSymbol = spirv::symbolizeVendor(vendorStr))
557 vendorID = *vendorSymbol;
559 parser.
emitError(loc,
"unknown vendor: ") << vendorStr;
563 StringRef deviceTypeStr;
566 if (
auto deviceTypeSymbol = spirv::symbolizeDeviceType(deviceTypeStr))
567 deviceType = *deviceTypeSymbol;
569 parser.
emitError(loc,
"unknown device type: ") << deviceTypeStr;
582 ResourceLimitsAttr limitsAttr;
587 deviceType, deviceID);
602 generatedAttributeParser(parser, &attrKind, type, attr);
625 << spirv::stringifyVersion(triple.
getVersion()) <<
", [";
626 llvm::interleaveComma(
628 [&](spirv::Capability cap) { os << spirv::stringifyCapability(cap); });
631 os << attr.cast<StringAttr>().getValue();
640 if (clientAPI != spirv::ClientAPI::Unknown)
641 printer <<
", api=" << clientAPI;
645 if (vendorID != spirv::Vendor::Unknown) {
646 printer <<
", " << spirv::stringifyVendor(vendorID);
647 if (deviceType != spirv::DeviceType::Unknown) {
648 printer <<
":" << spirv::stringifyDeviceType(deviceType);
650 printer <<
":" << deviceID;
663 printer <<
", " << spirv::stringifyStorageClass(*storageClass);
667 void SPIRVDialect::printAttribute(
Attribute attr,
669 if (
succeeded(generatedAttributePrinter(attr, printer)))
673 print(targetEnv, printer);
675 print(vceAttr, printer);
677 print(interfaceVarABIAttr, printer);
679 llvm_unreachable(
"unhandled SPIR-V attribute kind");
686 void spirv::SPIRVDialect::registerAttributes() {
687 addAttributes<InterfaceVarABIAttr, TargetEnvAttr, VerCapExtAttr>();
689 #define GET_ATTRDEF_LIST
690 #include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.cpp.inc"
static Attribute parseTargetEnvAttr(DialectAsmParser &parser)
Parses a spirv::TargetEnvAttr.
static ParseResult parseKeywordList(DialectAsmParser &parser, function_ref< LogicalResult(SMLoc, StringRef)> processKeyword)
Parses a comma-separated list of keywords, invokes processKeyword on each of the parsed keyword,...
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
static Attribute parseVerCapExtAttr(DialectAsmParser &parser)
static Attribute parseInterfaceVarABIAttr(DialectAsmParser &parser)
Parses a spirv::InterfaceVarABIAttr.
virtual OptionalParseResult parseOptionalInteger(APInt &result)=0
Parse an optional integer value from the stream.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
virtual ParseResult parseRParen()=0
Parse a ) token.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseOptionalColon()=0
Parse a : token if present.
virtual ParseResult parseLSquare()=0
Parse a [ token.
virtual ParseResult parseRSquare()=0
Parse a ] token.
ParseResult parseInteger(IntT &result)
Parse an integer value from the stream.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseEqual()=0
Parse a = token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual ParseResult parseOptionalComma()=0
Parse a , token if present.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseOptionalRSquare()=0
Parse a ] token if present.
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseLParen()=0
Parse a ( token.
virtual ParseResult parseComma()=0
Parse a , token.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
virtual raw_ostream & getStream() const
Return the raw output stream used by this printer.
Base storage class appearing in an attribute.
Attributes are known-constant values of operations.
This class is a general helper class for creating context-global objects like types,...
IntegerAttr getI32IntegerAttr(int32_t value)
StringAttr getStringAttr(const Twine &bytes)
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
The DialectAsmParser has methods for interacting with the asm parser when parsing attributes and type...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
This class represents a diagnostic that is inflight and set to be reported.
MLIRContext is the top-level object for a collection of MLIR operations.
This class implements Optional functionality for ParseResult.
bool has_value() const
Returns true if we contain a valid ParseResult value.
This class represents success/failure for parsing-like operations that find it important to chain tog...
This is a utility allocator used to allocate memory for instances of derived types.
T * allocate()
Allocate an instance of the provided type.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
ImplType * getImpl() const
Utility for easy access to the storage instance.
An attribute that specifies the information regarding the interface variable: descriptor set,...
uint32_t getBinding()
Returns binding.
static StringRef getKindName()
Returns the attribute kind's name (without the 'spirv.' prefix).
static LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, IntegerAttr descriptorSet, IntegerAttr binding, IntegerAttr storageClass)
uint32_t getDescriptorSet()
Returns descriptor set.
static InterfaceVarABIAttr get(uint32_t descriptorSet, uint32_t binding, std::optional< StorageClass > storageClass, MLIRContext *context)
Gets a InterfaceVarABIAttr.
std::optional< StorageClass > getStorageClass()
Returns spirv::StorageClass.
An attribute that specifies the target version, allowed extensions and capabilities,...
Version getVersion() const
Returns the target version.
VerCapExtAttr::cap_range getCapabilities()
Returns the target capabilities.
ResourceLimitsAttr getResourceLimits() const
Returns the target resource limits.
static StringRef getKindName()
Returns the attribute kind's name (without the 'spirv.' prefix).
VerCapExtAttr getTripleAttr() const
Returns the (version, capabilities, extensions) triple attribute.
ArrayAttr getCapabilitiesAttr()
Returns the target capabilities as an integer array attribute.
VerCapExtAttr::ext_range getExtensions()
Returns the target extensions.
Vendor getVendorID() const
Returns the vendor ID.
DeviceType getDeviceType() const
Returns the device type.
ClientAPI getClientAPI() const
Returns the client API.
ArrayAttr getExtensionsAttr()
Returns the target extensions as a string array attribute.
uint32_t getDeviceID() const
Returns the device ID.
static constexpr uint32_t kUnknownDeviceID
ID for unknown devices.
static TargetEnvAttr get(VerCapExtAttr triple, ResourceLimitsAttr limits, ClientAPI clientAPI=ClientAPI::Unknown, Vendor vendorID=Vendor::Unknown, DeviceType deviceType=DeviceType::Unknown, uint32_t deviceId=kUnknownDeviceID)
Gets a TargetEnvAttr instance.
An attribute that specifies the SPIR-V (version, capabilities, extensions) triple.
cap_range getCapabilities()
Returns the capabilities.
static LogicalResult verify(function_ref< InFlightDiagnostic()> emitError, IntegerAttr version, ArrayAttr capabilities, ArrayAttr extensions)
Version getVersion()
Returns the version.
static StringRef getKindName()
Returns the attribute kind's name (without the 'spirv.' prefix).
static VerCapExtAttr get(Version version, ArrayRef< Capability > capabilities, ArrayRef< Extension > extensions, MLIRContext *context)
Gets a VerCapExtAttr instance.
ArrayAttr getCapabilitiesAttr()
Returns the capabilities as an integer array attribute.
ext_range getExtensions()
Returns the extensions.
ArrayAttr getExtensionsAttr()
Returns the extensions as a string array attribute.
Include the generated interface declarations.
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Attribute parseAttribute(llvm::StringRef attrStr, MLIRContext *context)
This parses a single MLIR attribute to an MLIR context if it was valid.
This class represents an efficient way to signal success or failure.
cap_iterator(ArrayAttr::iterator it)
ext_iterator(ArrayAttr::iterator it)
InterfaceVarABIAttributeStorage(Attribute descriptorSet, Attribute binding, Attribute storageClass)
std::tuple< Attribute, Attribute, Attribute > KeyTy
bool operator==(const KeyTy &key) const
static InterfaceVarABIAttributeStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
static TargetEnvAttributeStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
TargetEnvAttributeStorage(Attribute triple, ClientAPI clientAPI, Vendor vendorID, DeviceType deviceType, uint32_t deviceID, Attribute limits)
bool operator==(const KeyTy &key) const
std::tuple< Attribute, ClientAPI, Vendor, DeviceType, uint32_t, Attribute > KeyTy
static VerCapExtAttributeStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
std::tuple< Attribute, Attribute, Attribute > KeyTy
VerCapExtAttributeStorage(Attribute version, Attribute capabilities, Attribute extensions)
bool operator==(const KeyTy &key) const