14#include "llvm/ADT/TypeSwitch.h"
15#include "llvm/Support/InterleavedRange.h"
26#include "mlir/Dialect/SPIRV/IR/SPIRVAttrUtils.inc"
37 using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
62 using KeyTy = std::tuple<Attribute, Attribute, Attribute>;
87 std::tuple<Attribute, ClientAPI, Vendor, DeviceType, uint32_t, Attribute>;
104 std::get<2>(key), std::get<3>(key),
105 std::get<4>(key), std::get<5>(key));
126 std::optional<int> subgroupSize,
127 std::optional<int> targetWidth) {
130 if (workgroupSize.size() != 3)
131 return emitError() <<
"expected 3 workgroup size values, but got "
132 << workgroupSize.size();
134 [](int32_t v) { return v <= 0; }))
135 return emitError() <<
"expected positive workgroup size values";
145 std::optional<spirv::StorageClass> storageClass,
148 auto descriptorSetAttr =
b.getI32IntegerAttr(descriptorSet);
149 auto bindingAttr =
b.getI32IntegerAttr(binding);
150 auto storageClassAttr =
151 storageClass ?
b.getI32IntegerAttr(
static_cast<uint32_t
>(*storageClass))
153 return get(descriptorSetAttr, bindingAttr, storageClassAttr);
156spirv::InterfaceVarABIAttr
158 IntegerAttr storageClass) {
159 assert(descriptorSet && binding);
161 return Base::get(context, descriptorSet, binding, storageClass);
165 return "interface_var_abi";
169 return cast<IntegerAttr>(
getImpl()->binding).getInt();
173 return cast<IntegerAttr>(
getImpl()->descriptorSet).getInt();
176std::optional<spirv::StorageClass>
179 return static_cast<spirv::StorageClass
>(
180 cast<IntegerAttr>(
getImpl()->storageClass).getValue().getZExtValue());
186 IntegerAttr binding, IntegerAttr storageClass) {
187 if (!descriptorSet.getType().isSignlessInteger(32))
188 return emitError() <<
"expected 32-bit integer for descriptor set";
190 if (!binding.getType().isSignlessInteger(32))
191 return emitError() <<
"expected 32-bit integer for binding";
194 if (
auto storageClassAttr = cast<IntegerAttr>(storageClass)) {
195 auto storageClassValue =
196 spirv::symbolizeStorageClass(storageClassAttr.getInt());
197 if (!storageClassValue)
198 return emitError() <<
"unknown storage class";
200 return emitError() <<
"expected valid storage class";
216 auto versionAttr =
b.getI32IntegerAttr(
static_cast<uint32_t
>(version));
219 capAttrs.reserve(capabilities.size());
220 for (spirv::Capability cap : capabilities)
221 capAttrs.push_back(
b.getI32IntegerAttr(
static_cast<uint32_t
>(cap)));
224 extAttrs.reserve(extensions.size());
225 for (spirv::Extension ext : extensions)
226 extAttrs.push_back(
b.getStringAttr(spirv::stringifyExtension(ext)));
228 return get(versionAttr,
b.getArrayAttr(capAttrs),
b.getArrayAttr(extAttrs));
232 ArrayAttr capabilities,
233 ArrayAttr extensions) {
234 assert(version && capabilities && extensions);
236 return Base::get(context, version, capabilities, extensions);
242 return static_cast<spirv::Version
>(
243 cast<IntegerAttr>(
getImpl()->version).getValue().getZExtValue());
247 :
llvm::mapped_iterator<ArrayAttr::iterator,
250 return *symbolizeExtension(cast<StringAttr>(attr).getValue());
259 return cast<ArrayAttr>(
getImpl()->extensions);
263 :
llvm::mapped_iterator<ArrayAttr::iterator,
266 return *symbolizeCapability(
267 cast<IntegerAttr>(attr).getValue().getZExtValue());
276 return cast<ArrayAttr>(
getImpl()->capabilities);
281 ArrayAttr capabilities, ArrayAttr extensions) {
282 if (!version.getType().isSignlessInteger(32))
283 return emitError() <<
"expected 32-bit integer for version";
285 if (!llvm::all_of(capabilities.getValue(), [](
Attribute attr) {
286 if (auto intAttr = dyn_cast<IntegerAttr>(attr))
287 if (spirv::symbolizeCapability(intAttr.getValue().getZExtValue()))
291 return emitError() <<
"unknown capability in capability list";
293 if (!llvm::all_of(extensions.getValue(), [](
Attribute attr) {
294 if (auto strAttr = dyn_cast<StringAttr>(attr))
295 if (spirv::symbolizeExtension(strAttr.getValue()))
299 return emitError() <<
"unknown extension in extension list";
310 Vendor vendorID, DeviceType deviceType, uint32_t deviceID) {
311 assert(triple && limits &&
"expected valid triple and limits");
313 return Base::get(context, triple, clientAPI, vendorID, deviceType, deviceID,
320 return cast<spirv::VerCapExtAttr>(
getImpl()->triple);
360 return cast<spirv::ResourceLimitsAttr>(
getImpl()->limits);
367#define GET_ATTRDEF_CLASSES
368#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.cpp.inc"
378 function_ref<LogicalResult(SMLoc, StringRef)> processKeyword) {
389 auto loc = parser.getCurrentLocation();
391 if (parser.parseKeyword(&keyword) ||
392 failed(processKeyword(loc, keyword)))
410 IntegerAttr descriptorSetAttr;
413 uint32_t descriptorSet = 0;
416 if (!descriptorSetParseResult.has_value() ||
417 failed(*descriptorSetParseResult)) {
418 parser.
emitError(loc,
"missing descriptor set");
427 IntegerAttr bindingAttr;
430 uint32_t binding = 0;
433 if (!bindingParseResult.has_value() || failed(*bindingParseResult)) {
434 parser.
emitError(loc,
"missing binding");
443 IntegerAttr storageClassAttr;
447 StringRef storageClass;
451 if (
auto storageClassSymbol =
452 spirv::symbolizeStorageClass(storageClass)) {
454 static_cast<uint32_t
>(*storageClassSymbol));
456 parser.
emitError(loc,
"unknown storage class: ") << storageClass;
475 IntegerAttr versionAttr;
482 if (
auto versionSymbol = spirv::symbolizeVersion(version)) {
486 parser.
emitError(loc,
"unknown version: ") << version;
495 StringRef errorKeyword;
497 auto processCapability = [&](SMLoc loc, StringRef capability) {
498 if (
auto capSymbol = spirv::symbolizeCapability(capability)) {
499 capabilities.push_back(
503 return errorloc = loc, errorKeyword = capability, failure();
506 if (!errorKeyword.empty())
507 parser.
emitError(errorloc,
"unknown capability: ") << errorKeyword;
518 StringRef errorKeyword;
520 auto processExtension = [&](SMLoc loc, StringRef extension) {
521 if (spirv::symbolizeExtension(extension)) {
525 return errorloc = loc, errorKeyword = extension, failure();
528 if (!errorKeyword.empty())
529 parser.
emitError(errorloc,
"unknown extension: ") << errorKeyword;
552 auto clientAPI = spirv::ClientAPI::Unknown;
560 if (
auto apiSymbol = spirv::symbolizeClientAPI(apiStr))
561 clientAPI = *apiSymbol;
563 parser.
emitError(loc,
"unknown client API: ") << apiStr;
569 Vendor vendorID = Vendor::Unknown;
570 DeviceType deviceType = DeviceType::Unknown;
576 if (
auto vendorSymbol = spirv::symbolizeVendor(vendorStr))
577 vendorID = *vendorSymbol;
579 parser.
emitError(loc,
"unknown vendor: ") << vendorStr;
583 StringRef deviceTypeStr;
586 if (
auto deviceTypeSymbol = spirv::symbolizeDeviceType(deviceTypeStr))
587 deviceType = *deviceTypeSymbol;
589 parser.
emitError(loc,
"unknown device type: ") << deviceTypeStr;
602 ResourceLimitsAttr limitsAttr;
607 deviceType, deviceID);
610Attribute SPIRVDialect::parseAttribute(DialectAsmParser &parser,
615 OptionalParseResult
result =
618 if (type && attr && !isa<TypedAttr>(attr)) {
649 << spirv::stringifyVersion(triple.
getVersion()) <<
", "
650 << llvm::interleaved_array(llvm::map_range(
653 << llvm::interleaved_array(
662 if (clientAPI != spirv::ClientAPI::Unknown)
663 printer <<
", api=" << clientAPI;
667 if (vendorID != spirv::Vendor::Unknown) {
668 printer <<
", " << spirv::stringifyVendor(vendorID);
669 if (deviceType != spirv::DeviceType::Unknown) {
670 printer <<
":" << spirv::stringifyDeviceType(deviceType);
672 printer <<
":" << deviceID;
685 printer <<
", " << spirv::stringifyStorageClass(*storageClass);
689void SPIRVDialect::printAttribute(Attribute attr,
690 DialectAsmPrinter &printer)
const {
694 if (
auto targetEnv = dyn_cast<TargetEnvAttr>(attr))
695 print(targetEnv, printer);
696 else if (
auto vceAttr = dyn_cast<VerCapExtAttr>(attr))
697 print(vceAttr, printer);
698 else if (
auto interfaceVarABIAttr = dyn_cast<InterfaceVarABIAttr>(attr))
699 print(interfaceVarABIAttr, printer);
701 llvm_unreachable(
"unhandled SPIR-V attribute kind");
708void spirv::SPIRVDialect::registerAttributes() {
709 addAttributes<InterfaceVarABIAttr, TargetEnvAttr, VerCapExtAttr>();
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 Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
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 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.
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.
T * allocate()
Allocate an instance of the provided type.
ArrayRef< T > asArrayRef() const
ImplType * getImpl() const
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).
uint32_t getDescriptorSet()
Returns descriptor set.
static InterfaceVarABIAttr get(uint32_t descriptorSet, uint32_t binding, std::optional< StorageClass > storageClass, MLIRContext *context)
Gets a InterfaceVarABIAttr.
static LogicalResult verifyInvariants(function_ref< InFlightDiagnostic()> emitError, IntegerAttr descriptorSet, IntegerAttr binding, IntegerAttr storageClass)
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 verifyInvariants(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.
llvm::iterator_range< ext_iterator > ext_range
ext_range getExtensions()
Returns the extensions.
ArrayAttr getExtensionsAttr()
Returns the extensions as a string array attribute.
llvm::iterator_range< cap_iterator > cap_range
The OpAsmOpInterface, see OpAsmInterface.td for more details.
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
StorageUniquer::StorageAllocator AttributeStorageAllocator
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
llvm::function_ref< Fn > function_ref
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)
std::tuple< Attribute, ClientAPI, Vendor, DeviceType, uint32_t, Attribute > KeyTy
TargetEnvAttributeStorage(Attribute triple, ClientAPI clientAPI, Vendor vendorID, DeviceType deviceType, uint32_t deviceID, Attribute limits)
bool operator==(const KeyTy &key) const
static TargetEnvAttributeStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
std::tuple< Attribute, Attribute, Attribute > KeyTy
static VerCapExtAttributeStorage * construct(AttributeStorageAllocator &allocator, const KeyTy &key)
VerCapExtAttributeStorage(Attribute version, Attribute capabilities, Attribute extensions)
bool operator==(const KeyTy &key) const