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