xref: /llvm-project/mlir/include/mlir/Dialect/SPIRV/IR/TargetAndABI.h (revision 9dbb6e1978d3d2d61ef65c2dac1fd8add5a4c7a2)
1 //===- TargetAndABI.h - SPIR-V target and ABI utilities  --------*- 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 utilities for SPIR-V target and shader interface ABI.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
14 #define MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
15 
16 #include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
17 #include "mlir/Support/LLVM.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include <optional>
20 
21 namespace mlir {
22 class Operation;
23 
24 namespace spirv {
25 enum class StorageClass : uint32_t;
26 
27 /// A wrapper class around a spirv::TargetEnvAttr to provide query methods for
28 /// allowed version/capabilities/extensions.
29 class TargetEnv {
30 public:
31   explicit TargetEnv(TargetEnvAttr targetAttr);
32 
33   Version getVersion() const;
34 
35   /// Returns true if the given capability is allowed.
36   bool allows(Capability) const;
37   /// Returns the first allowed one if any of the given capabilities is allowed.
38   /// Returns std::nullopt otherwise.
39   std::optional<Capability> allows(ArrayRef<Capability>) const;
40 
41   /// Returns true if the given extension is allowed.
42   bool allows(Extension) const;
43   /// Returns the first allowed one if any of the given extensions is allowed.
44   /// Returns std::nullopt otherwise.
45   std::optional<Extension> allows(ArrayRef<Extension>) const;
46 
47   /// Returns the vendor ID.
48   Vendor getVendorID() const;
49 
50   /// Returns the device type.
51   DeviceType getDeviceType() const;
52 
53   /// Returns the device ID.
54   uint32_t getDeviceID() const;
55 
56   /// Returns the MLIRContext.
57   MLIRContext *getContext() const;
58 
59   /// Returns the target resource limits.
60   ResourceLimitsAttr getResourceLimits() const;
61 
getAttr()62   TargetEnvAttr getAttr() const { return targetAttr; }
63 
64   /// Allows implicity converting to the underlying spirv::TargetEnvAttr.
TargetEnvAttr()65   operator TargetEnvAttr() const { return targetAttr; }
66 
67 private:
68   TargetEnvAttr targetAttr;
69   llvm::SmallSet<Extension, 4> givenExtensions;    /// Allowed extensions
70   llvm::SmallSet<Capability, 8> givenCapabilities; /// Allowed capabilities
71 };
72 
73 /// Returns the attribute name for specifying argument ABI information.
74 StringRef getInterfaceVarABIAttrName();
75 
76 /// Gets the InterfaceVarABIAttr given its fields.
77 InterfaceVarABIAttr
78 getInterfaceVarABIAttr(unsigned descriptorSet, unsigned binding,
79                        std::optional<StorageClass> storageClass,
80                        MLIRContext *context);
81 
82 /// Returns whether the given SPIR-V target (described by TargetEnvAttr) needs
83 /// ABI attributes for interface variables (spirv.interface_var_abi).
84 bool needsInterfaceVarABIAttrs(TargetEnvAttr targetAttr);
85 
86 /// Returns the attribute name for specifying entry point information.
87 StringRef getEntryPointABIAttrName();
88 
89 /// Gets the EntryPointABIAttr given its fields.
90 /// targetWidth is used by several execution modes. It is the element width
91 /// of floating-point operations.
92 /// Refer to Execution Mode in SPIR-V specification.
93 /// https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#_execution_mode
94 EntryPointABIAttr getEntryPointABIAttr(MLIRContext *context,
95                                        ArrayRef<int32_t> workgroupSize = {},
96                                        std::optional<int> subgroupSize = {},
97                                        std::optional<int> targetWidth = {});
98 
99 /// Queries the entry point ABI on the nearest function-like op containing the
100 /// given `op`. Returns null attribute if not found.
101 EntryPointABIAttr lookupEntryPointABI(Operation *op);
102 
103 /// Queries the local workgroup size from entry point ABI on the nearest
104 /// function-like op containing the given `op`. Returns null attribute if not
105 /// found.
106 DenseI32ArrayAttr lookupLocalWorkGroupSize(Operation *op);
107 
108 /// Returns a default resource limits attribute that uses numbers from
109 /// "Table 46. Required Limits" of the Vulkan spec.
110 ResourceLimitsAttr getDefaultResourceLimits(MLIRContext *context);
111 
112 /// Returns the attribute name for specifying SPIR-V target environment.
113 StringRef getTargetEnvAttrName();
114 
115 /// Returns the default target environment: SPIR-V 1.0 with Shader capability
116 /// and no extra extensions.
117 TargetEnvAttr getDefaultTargetEnv(MLIRContext *context);
118 
119 /// Queries the target environment recursively from enclosing symbol table ops
120 /// containing the given `op`.
121 TargetEnvAttr lookupTargetEnv(Operation *op);
122 
123 /// Queries the target environment recursively from enclosing symbol table ops
124 /// containing the given `op` or returns the default target environment as
125 /// returned by getDefaultTargetEnv() if not provided.
126 TargetEnvAttr lookupTargetEnvOrDefault(Operation *op);
127 
128 /// Returns addressing model selected based on target environment.
129 AddressingModel getAddressingModel(TargetEnvAttr targetAttr,
130                                    bool use64bitAddress);
131 
132 /// Returns execution model selected based on target environment.
133 /// Returns failure if it cannot be selected.
134 FailureOr<ExecutionModel> getExecutionModel(TargetEnvAttr targetAttr);
135 
136 /// Returns memory model selected based on target environment.
137 /// Returns failure if it cannot be selected.
138 FailureOr<MemoryModel> getMemoryModel(TargetEnvAttr targetAttr);
139 
140 } // namespace spirv
141 } // namespace mlir
142 
143 #endif // MLIR_DIALECT_SPIRV_IR_TARGETANDABI_H
144