MLIR  19.0.0git
SPIRVConversion.h
Go to the documentation of this file.
1 //===- SPIRVConversion.h - SPIR-V Conversion 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 // Defines utilities to use while converting to the SPIR-V dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef MLIR_DIALECT_SPIRV_TRANSFORMS_SPIRVCONVERSION_H
14 #define MLIR_DIALECT_SPIRV_TRANSFORMS_SPIRVCONVERSION_H
15 
21 #include "llvm/ADT/SmallSet.h"
22 
23 namespace mlir {
24 
25 //===----------------------------------------------------------------------===//
26 // Type Converter
27 //===----------------------------------------------------------------------===//
28 
29 /// How sub-byte values are storaged in memory.
31  /// Sub-byte values are tightly packed without any padding, e.g., 4xi2 -> i8.
32  Packed,
33 };
34 
36  /// The number of bits to store a boolean value.
37  unsigned boolNumBits{8};
38 
39  /// How sub-byte values are storaged in memory.
41 
42  /// Whether to emulate narrower scalar types with 32-bit scalar types if not
43  /// supported by the target.
44  ///
45  /// Non-32-bit scalar types require special hardware support that may not
46  /// exist on all GPUs. This is reflected in SPIR-V as that non-32-bit scalar
47  /// types require special capabilities or extensions. This option controls
48  /// whether to use 32-bit types to emulate < 32-bits-wide scalars, if a scalar
49  /// type of a certain bitwidth is not supported in the target environment.
50  /// This requires the runtime to also feed in data with a matched bitwidth and
51  /// layout for interface types. The runtime can do that by inspecting the
52  /// SPIR-V module.
53  ///
54  /// If the original scalar type has less than 32-bit, a multiple of its
55  /// values will be packed into one 32-bit value to be memory efficient.
57 
58  /// Use 64-bit integers when converting index types.
59  bool use64bitIndex{false};
60 };
61 
62 /// Type conversion from builtin types to SPIR-V types for shader interface.
63 ///
64 /// For memref types, this converter additionally performs type wrapping to
65 /// satisfy shader interface requirements: shader interface types must be
66 /// pointers to structs.
68 public:
69  explicit SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr,
70  const SPIRVConversionOptions &options = {});
71 
72  /// Gets the SPIR-V correspondence for the standard index type.
73  Type getIndexType() const;
74 
75  /// Gets the bitwidth of the index type when converted to SPIR-V.
76  unsigned getIndexTypeBitwidth() const {
77  return options.use64bitIndex ? 64 : 32;
78  }
79 
80  const spirv::TargetEnv &getTargetEnv() const { return targetEnv; }
81 
82  /// Returns the options controlling the SPIR-V type converter.
83  const SPIRVConversionOptions &getOptions() const { return options; }
84 
85  /// Checks if the SPIR-V capability inquired is supported.
86  bool allows(spirv::Capability capability) const;
87 
88 private:
89  spirv::TargetEnv targetEnv;
90  SPIRVConversionOptions options;
91 
92  MLIRContext *getContext() const;
93 };
94 
95 //===----------------------------------------------------------------------===//
96 // Conversion Target
97 //===----------------------------------------------------------------------===//
98 
99 // The default SPIR-V conversion target.
100 //
101 // It takes a SPIR-V target environment and controls operation legality based on
102 // the their availability in the target environment.
104 public:
105  /// Creates a SPIR-V conversion target for the given target environment.
106  static std::unique_ptr<SPIRVConversionTarget>
107  get(spirv::TargetEnvAttr targetAttr);
108 
109 private:
110  explicit SPIRVConversionTarget(spirv::TargetEnvAttr targetAttr);
111 
112  // Be explicit that instance of this class cannot be copied or moved: there
113  // are lambdas capturing fields of the instance.
116  SPIRVConversionTarget &operator=(const SPIRVConversionTarget &) = delete;
117  SPIRVConversionTarget &operator=(SPIRVConversionTarget &&) = delete;
118 
119  /// Returns true if the given `op` is legal to use under the current target
120  /// environment.
121  bool isLegalOp(Operation *op);
122 
123  spirv::TargetEnv targetEnv;
124 };
125 
126 //===----------------------------------------------------------------------===//
127 // Patterns and Utility Functions
128 //===----------------------------------------------------------------------===//
129 
130 /// Appends to a pattern list additional patterns for translating the builtin
131 /// `func` op to the SPIR-V dialect. These patterns do not handle shader
132 /// interface/ABI; they convert function parameters to be of SPIR-V allowed
133 /// types.
135  RewritePatternSet &patterns);
136 
137 namespace spirv {
138 class AccessChainOp;
139 
140 /// Returns the value for the given `builtin` variable. This function gets or
141 /// inserts the global variable associated for the builtin within the nearest
142 /// symbol table enclosing `op`. Returns null Value on error.
143 ///
144 /// The global name being generated will be mangled using `preffix` and
145 /// `suffix`.
146 Value getBuiltinVariableValue(Operation *op, BuiltIn builtin, Type integerType,
147  OpBuilder &builder,
148  StringRef prefix = "__builtin__",
149  StringRef suffix = "__");
150 
151 /// Gets the value at the given `offset` of the push constant storage with a
152 /// total of `elementCount` `integerType` integers. A global variable will be
153 /// created in the nearest symbol table enclosing `op` for the push constant
154 /// storage if not existing. Load ops will be created via the given `builder` to
155 /// load values from the push constant. Returns null Value on error.
156 Value getPushConstantValue(Operation *op, unsigned elementCount,
157  unsigned offset, Type integerType,
158  OpBuilder &builder);
159 
160 /// Generates IR to perform index linearization with the given `indices` and
161 /// their corresponding `strides`, adding an initial `offset`.
163  int64_t offset, Type integerType, Location loc,
164  OpBuilder &builder);
165 
166 /// Performs the index computation to get to the element at `indices` of the
167 /// memory pointed to by `basePtr`, using the layout map of `baseType`.
168 /// Returns null if index computation cannot be performed.
169 
170 // TODO: This method assumes that the `baseType` is a MemRefType with AffineMap
171 // that has static strides. Extend to handle dynamic strides.
172 Value getElementPtr(const SPIRVTypeConverter &typeConverter,
173  MemRefType baseType, Value basePtr, ValueRange indices,
174  Location loc, OpBuilder &builder);
175 
176 // GetElementPtr implementation for Kernel/OpenCL flavored SPIR-V.
177 Value getOpenCLElementPtr(const SPIRVTypeConverter &typeConverter,
178  MemRefType baseType, Value basePtr,
179  ValueRange indices, Location loc, OpBuilder &builder);
180 
181 // GetElementPtr implementation for Vulkan/Shader flavored SPIR-V.
182 Value getVulkanElementPtr(const SPIRVTypeConverter &typeConverter,
183  MemRefType baseType, Value basePtr,
184  ValueRange indices, Location loc, OpBuilder &builder);
185 
186 } // namespace spirv
187 } // namespace mlir
188 
189 #endif // MLIR_DIALECT_SPIRV_TRANSFORMS_SPIRVCONVERSION_H
This class describes a specific conversion target.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
This class helps build Operations.
Definition: Builders.h:209
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
static std::unique_ptr< SPIRVConversionTarget > get(spirv::TargetEnvAttr targetAttr)
Creates a SPIR-V conversion target for the given target environment.
Type conversion from builtin types to SPIR-V types for shader interface.
const SPIRVConversionOptions & getOptions() const
Returns the options controlling the SPIR-V type converter.
const spirv::TargetEnv & getTargetEnv() const
Type getIndexType() const
Gets the SPIR-V correspondence for the standard index type.
unsigned getIndexTypeBitwidth() const
Gets the bitwidth of the index type when converted to SPIR-V.
SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr, const SPIRVConversionOptions &options={})
bool allows(spirv::Capability capability) const
Checks if the SPIR-V capability inquired is supported.
Type conversion class.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:381
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
An attribute that specifies the target version, allowed extensions and capabilities,...
A wrapper class around a spirv::TargetEnvAttr to provide query methods for allowed version/capabiliti...
Definition: TargetAndABI.h:29
Value getBuiltinVariableValue(Operation *op, BuiltIn builtin, Type integerType, OpBuilder &builder, StringRef prefix="__builtin__", StringRef suffix="__")
Returns the value for the given builtin variable.
Value getElementPtr(const SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
Performs the index computation to get to the element at indices of the memory pointed to by basePtr,...
Value getOpenCLElementPtr(const SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
Value getPushConstantValue(Operation *op, unsigned elementCount, unsigned offset, Type integerType, OpBuilder &builder)
Gets the value at the given offset of the push constant storage with a total of elementCount integerT...
Value linearizeIndex(ValueRange indices, ArrayRef< int64_t > strides, int64_t offset, Type integerType, Location loc, OpBuilder &builder)
Generates IR to perform index linearization with the given indices and their corresponding strides,...
Value getVulkanElementPtr(const SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
Include the generated interface declarations.
SPIRVSubByteTypeStorage
How sub-byte values are storaged in memory.
@ Packed
Sub-byte values are tightly packed without any padding, e.g., 4xi2 -> i8.
void populateBuiltinFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating the builtin func op to the SPIR-V diale...
bool use64bitIndex
Use 64-bit integers when converting index types.
unsigned boolNumBits
The number of bits to store a boolean value.
bool emulateLT32BitScalarTypes
Whether to emulate narrower scalar types with 32-bit scalar types if not supported by the target.
SPIRVSubByteTypeStorage subByteTypeStorage
How sub-byte values are storaged in memory.