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_IR_SPIRVATTRIBUTES_H 14 #define MLIR_DIALECT_SPIRV_IR_SPIRVATTRIBUTES_H 15 16 #include "mlir/Dialect/SPIRV/IR/SPIRVTypes.h" 17 #include "mlir/IR/BuiltinAttributes.h" 18 #include "mlir/Support/LLVM.h" 19 20 namespace mlir { 21 namespace spirv { 22 class VerCapExtAttr; 23 } 24 } // namespace mlir 25 26 // Pull in TableGen'erated SPIR-V attribute definitions for target and ABI. 27 #define GET_ATTRDEF_CLASSES 28 #include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h.inc" 29 30 namespace mlir { 31 namespace spirv { 32 enum class Capability : uint32_t; 33 enum class DeviceType : uint32_t; 34 enum class Extension : uint32_t; 35 enum class Vendor : uint32_t; 36 enum class Version : uint32_t; 37 38 namespace detail { 39 struct InterfaceVarABIAttributeStorage; 40 struct TargetEnvAttributeStorage; 41 struct VerCapExtAttributeStorage; 42 } // namespace detail 43 44 // TableGen'erated helper functions. 45 // 46 // Get the name used in the Op to refer to an enum value of the given 47 // `EnumClass`. 48 // template <typename EnumClass> StringRef attributeName(); 49 // 50 #include "mlir/Dialect/SPIRV/IR/SPIRVAttrUtils.inc" 51 52 /// An attribute that specifies the information regarding the interface 53 /// variable: descriptor set, binding, storage class. 54 class InterfaceVarABIAttr 55 : public Attribute::AttrBase<InterfaceVarABIAttr, Attribute, 56 detail::InterfaceVarABIAttributeStorage> { 57 public: 58 using Base::Base; 59 60 /// Gets a InterfaceVarABIAttr. 61 static InterfaceVarABIAttr get(uint32_t descriptorSet, uint32_t binding, 62 std::optional<StorageClass> storageClass, 63 MLIRContext *context); 64 static InterfaceVarABIAttr get(IntegerAttr descriptorSet, IntegerAttr binding, 65 IntegerAttr storageClass); 66 67 /// Returns the attribute kind's name (without the 'spirv.' prefix). 68 static StringRef getKindName(); 69 70 /// Returns descriptor set. 71 uint32_t getDescriptorSet(); 72 73 /// Returns binding. 74 uint32_t getBinding(); 75 76 /// Returns `spirv::StorageClass`. 77 std::optional<StorageClass> getStorageClass(); 78 79 static LogicalResult 80 verifyInvariants(function_ref<InFlightDiagnostic()> emitError, 81 IntegerAttr descriptorSet, IntegerAttr binding, 82 IntegerAttr storageClass); 83 84 static constexpr StringLiteral name = "spirv.interface_var_abi"; 85 }; 86 87 /// An attribute that specifies the SPIR-V (version, capabilities, extensions) 88 /// triple. 89 class VerCapExtAttr 90 : public Attribute::AttrBase<VerCapExtAttr, Attribute, 91 detail::VerCapExtAttributeStorage> { 92 public: 93 using Base::Base; 94 95 /// Gets a VerCapExtAttr instance. 96 static VerCapExtAttr get(Version version, ArrayRef<Capability> capabilities, 97 ArrayRef<Extension> extensions, 98 MLIRContext *context); 99 static VerCapExtAttr get(IntegerAttr version, ArrayAttr capabilities, 100 ArrayAttr extensions); 101 102 /// Returns the attribute kind's name (without the 'spirv.' prefix). 103 static StringRef getKindName(); 104 105 /// Returns the version. 106 Version getVersion(); 107 108 struct ext_iterator final 109 : public llvm::mapped_iterator<ArrayAttr::iterator, 110 Extension (*)(Attribute)> { 111 explicit ext_iterator(ArrayAttr::iterator it); 112 }; 113 using ext_range = llvm::iterator_range<ext_iterator>; 114 115 /// Returns the extensions. 116 ext_range getExtensions(); 117 /// Returns the extensions as a string array attribute. 118 ArrayAttr getExtensionsAttr(); 119 120 struct cap_iterator final 121 : public llvm::mapped_iterator<ArrayAttr::iterator, 122 Capability (*)(Attribute)> { 123 explicit cap_iterator(ArrayAttr::iterator it); 124 }; 125 using cap_range = llvm::iterator_range<cap_iterator>; 126 127 /// Returns the capabilities. 128 cap_range getCapabilities(); 129 /// Returns the capabilities as an integer array attribute. 130 ArrayAttr getCapabilitiesAttr(); 131 132 static LogicalResult 133 verifyInvariants(function_ref<InFlightDiagnostic()> emitError, 134 IntegerAttr version, ArrayAttr capabilities, 135 ArrayAttr extensions); 136 137 static constexpr StringLiteral name = "spirv.ver_cap_ext"; 138 }; 139 140 /// An attribute that specifies the target version, allowed extensions and 141 /// capabilities, and resource limits. These information describes a SPIR-V 142 /// target environment. 143 class TargetEnvAttr 144 : public Attribute::AttrBase<TargetEnvAttr, Attribute, 145 detail::TargetEnvAttributeStorage> { 146 public: 147 /// ID for unknown devices. 148 static constexpr uint32_t kUnknownDeviceID = 0x7FFFFFFF; 149 150 using Base::Base; 151 152 /// Gets a TargetEnvAttr instance. 153 static TargetEnvAttr get(VerCapExtAttr triple, ResourceLimitsAttr limits, 154 ClientAPI clientAPI = ClientAPI::Unknown, 155 Vendor vendorID = Vendor::Unknown, 156 DeviceType deviceType = DeviceType::Unknown, 157 uint32_t deviceId = kUnknownDeviceID); 158 159 /// Returns the attribute kind's name (without the 'spirv.' prefix). 160 static StringRef getKindName(); 161 162 /// Returns the (version, capabilities, extensions) triple attribute. 163 VerCapExtAttr getTripleAttr() const; 164 165 /// Returns the target version. 166 Version getVersion() const; 167 168 /// Returns the target extensions. 169 VerCapExtAttr::ext_range getExtensions(); 170 /// Returns the target extensions as a string array attribute. 171 ArrayAttr getExtensionsAttr(); 172 173 /// Returns the target capabilities. 174 VerCapExtAttr::cap_range getCapabilities(); 175 /// Returns the target capabilities as an integer array attribute. 176 ArrayAttr getCapabilitiesAttr(); 177 178 /// Returns the client API. 179 ClientAPI getClientAPI() const; 180 181 /// Returns the vendor ID. 182 Vendor getVendorID() const; 183 184 /// Returns the device type. 185 DeviceType getDeviceType() const; 186 187 /// Returns the device ID. 188 uint32_t getDeviceID() const; 189 190 /// Returns the target resource limits. 191 ResourceLimitsAttr getResourceLimits() const; 192 193 static constexpr StringLiteral name = "spirv.target_env"; 194 }; 195 } // namespace spirv 196 } // namespace mlir 197 198 #endif // MLIR_DIALECT_SPIRV_IR_SPIRVATTRIBUTES_H 199