1 //===- SPIRVAttributes.h - SPIR-V attribute declarations -------*- C++ -*-===// 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 // This file declares SPIR-V dialect specific attributes. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef MLIR_DIALECT_SPIRV_SPIRVATTRIBUTES_H 14 #define MLIR_DIALECT_SPIRV_SPIRVATTRIBUTES_H 15 16 #include "mlir/Dialect/SPIRV/SPIRVTypes.h" 17 #include "mlir/IR/BuiltinAttributes.h" 18 #include "mlir/Support/LLVM.h" 19 20 // Pull in SPIR-V attribute definitions for target and ABI. 21 #include "mlir/Dialect/SPIRV/TargetAndABI.h.inc" 22 23 namespace mlir { 24 namespace spirv { 25 enum class Capability : uint32_t; 26 enum class DeviceType; 27 enum class Extension; 28 enum class Vendor; 29 enum class Version : uint32_t; 30 31 namespace detail { 32 struct InterfaceVarABIAttributeStorage; 33 struct TargetEnvAttributeStorage; 34 struct VerCapExtAttributeStorage; 35 } // namespace detail 36 37 /// An attribute that specifies the information regarding the interface 38 /// variable: descriptor set, binding, storage class. 39 class InterfaceVarABIAttr 40 : public Attribute::AttrBase<InterfaceVarABIAttr, Attribute, 41 detail::InterfaceVarABIAttributeStorage> { 42 public: 43 using Base::Base; 44 45 /// Gets a InterfaceVarABIAttr. 46 static InterfaceVarABIAttr get(uint32_t descriptorSet, uint32_t binding, 47 Optional<StorageClass> storageClass, 48 MLIRContext *context); 49 static InterfaceVarABIAttr get(IntegerAttr descriptorSet, IntegerAttr binding, 50 IntegerAttr storageClass); 51 52 /// Returns the attribute kind's name (without the 'spv.' prefix). 53 static StringRef getKindName(); 54 55 /// Returns descriptor set. 56 uint32_t getDescriptorSet(); 57 58 /// Returns binding. 59 uint32_t getBinding(); 60 61 /// Returns `spirv::StorageClass`. 62 Optional<StorageClass> getStorageClass(); 63 64 static LogicalResult verifyConstructionInvariants(Location loc, 65 IntegerAttr descriptorSet, 66 IntegerAttr binding, 67 IntegerAttr storageClass); 68 }; 69 70 /// An attribute that specifies the SPIR-V (version, capabilities, extensions) 71 /// triple. 72 class VerCapExtAttr 73 : public Attribute::AttrBase<VerCapExtAttr, Attribute, 74 detail::VerCapExtAttributeStorage> { 75 public: 76 using Base::Base; 77 78 /// Gets a VerCapExtAttr instance. 79 static VerCapExtAttr get(Version version, ArrayRef<Capability> capabilities, 80 ArrayRef<Extension> extensions, 81 MLIRContext *context); 82 static VerCapExtAttr get(IntegerAttr version, ArrayAttr capabilities, 83 ArrayAttr extensions); 84 85 /// Returns the attribute kind's name (without the 'spv.' prefix). 86 static StringRef getKindName(); 87 88 /// Returns the version. 89 Version getVersion(); 90 91 struct ext_iterator final 92 : public llvm::mapped_iterator<ArrayAttr::iterator, 93 Extension (*)(Attribute)> { 94 explicit ext_iterator(ArrayAttr::iterator it); 95 }; 96 using ext_range = llvm::iterator_range<ext_iterator>; 97 98 /// Returns the extensions. 99 ext_range getExtensions(); 100 /// Returns the extensions as a string array attribute. 101 ArrayAttr getExtensionsAttr(); 102 103 struct cap_iterator final 104 : public llvm::mapped_iterator<ArrayAttr::iterator, 105 Capability (*)(Attribute)> { 106 explicit cap_iterator(ArrayAttr::iterator it); 107 }; 108 using cap_range = llvm::iterator_range<cap_iterator>; 109 110 /// Returns the capabilities. 111 cap_range getCapabilities(); 112 /// Returns the capabilities as an integer array attribute. 113 ArrayAttr getCapabilitiesAttr(); 114 115 static LogicalResult verifyConstructionInvariants(Location loc, 116 IntegerAttr version, 117 ArrayAttr capabilities, 118 ArrayAttr extensions); 119 }; 120 121 /// An attribute that specifies the target version, allowed extensions and 122 /// capabilities, and resource limits. These information describes a SPIR-V 123 /// target environment. 124 class TargetEnvAttr 125 : public Attribute::AttrBase<TargetEnvAttr, Attribute, 126 detail::TargetEnvAttributeStorage> { 127 public: 128 /// ID for unknown devices. 129 static constexpr uint32_t kUnknownDeviceID = 0x7FFFFFFF; 130 131 using Base::Base; 132 133 /// Gets a TargetEnvAttr instance. 134 static TargetEnvAttr get(VerCapExtAttr triple, Vendor vendorID, 135 DeviceType deviceType, uint32_t deviceId, 136 DictionaryAttr limits); 137 138 /// Returns the attribute kind's name (without the 'spv.' prefix). 139 static StringRef getKindName(); 140 141 /// Returns the (version, capabilities, extensions) triple attribute. 142 VerCapExtAttr getTripleAttr() const; 143 144 /// Returns the target version. 145 Version getVersion() const; 146 147 /// Returns the target extensions. 148 VerCapExtAttr::ext_range getExtensions(); 149 /// Returns the target extensions as a string array attribute. 150 ArrayAttr getExtensionsAttr(); 151 152 /// Returns the target capabilities. 153 VerCapExtAttr::cap_range getCapabilities(); 154 /// Returns the target capabilities as an integer array attribute. 155 ArrayAttr getCapabilitiesAttr(); 156 157 /// Returns the vendor ID. 158 Vendor getVendorID() const; 159 160 /// Returns the device type. 161 DeviceType getDeviceType() const; 162 163 /// Returns the device ID. 164 uint32_t getDeviceID() const; 165 166 /// Returns the target resource limits. 167 ResourceLimitsAttr getResourceLimits() const; 168 169 static LogicalResult 170 verifyConstructionInvariants(Location loc, VerCapExtAttr triple, 171 Vendor vendorID, DeviceType deviceType, 172 uint32_t deviceID, DictionaryAttr limits); 173 }; 174 } // namespace spirv 175 } // namespace mlir 176 177 #endif // MLIR_DIALECT_SPIRV_SPIRVATTRIBUTES_H 178