MLIR  18.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  /// Whether to enable fast math mode during conversion. If true, various
59  /// patterns would assume no NaN/infinity numbers as inputs, and thus there
60  /// will be no special guards emitted to check and handle such cases.
61  bool enableFastMathMode{false};
62 
63  /// Use 64-bit integers when converting index types.
64  bool use64bitIndex{false};
65 };
66 
67 /// Type conversion from builtin types to SPIR-V types for shader interface.
68 ///
69 /// For memref types, this converter additionally performs type wrapping to
70 /// satisfy shader interface requirements: shader interface types must be
71 /// pointers to structs.
73 public:
74  explicit SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr,
75  const SPIRVConversionOptions &options = {});
76 
77  /// Gets the SPIR-V correspondence for the standard index type.
78  Type getIndexType() const;
79 
80  /// Gets the bitwidth of the index type when converted to SPIR-V.
81  unsigned getIndexTypeBitwidth() const {
82  return options.use64bitIndex ? 64 : 32;
83  }
84 
85  const spirv::TargetEnv &getTargetEnv() const { return targetEnv; }
86 
87  /// Returns the options controlling the SPIR-V type converter.
88  const SPIRVConversionOptions &getOptions() const { return options; }
89 
90  /// Checks if the SPIR-V capability inquired is supported.
91  bool allows(spirv::Capability capability) const;
92 
93 private:
94  spirv::TargetEnv targetEnv;
95  SPIRVConversionOptions options;
96 
97  MLIRContext *getContext() const;
98 };
99 
100 //===----------------------------------------------------------------------===//
101 // Conversion Target
102 //===----------------------------------------------------------------------===//
103 
104 // The default SPIR-V conversion target.
105 //
106 // It takes a SPIR-V target environment and controls operation legality based on
107 // the their availability in the target environment.
109 public:
110  /// Creates a SPIR-V conversion target for the given target environment.
111  static std::unique_ptr<SPIRVConversionTarget>
112  get(spirv::TargetEnvAttr targetAttr);
113 
114 private:
115  explicit SPIRVConversionTarget(spirv::TargetEnvAttr targetAttr);
116 
117  // Be explicit that instance of this class cannot be copied or moved: there
118  // are lambdas capturing fields of the instance.
121  SPIRVConversionTarget &operator=(const SPIRVConversionTarget &) = delete;
122  SPIRVConversionTarget &operator=(SPIRVConversionTarget &&) = delete;
123 
124  /// Returns true if the given `op` is legal to use under the current target
125  /// environment.
126  bool isLegalOp(Operation *op);
127 
128  spirv::TargetEnv targetEnv;
129 };
130 
131 //===----------------------------------------------------------------------===//
132 // Patterns and Utility Functions
133 //===----------------------------------------------------------------------===//
134 
135 /// Appends to a pattern list additional patterns for translating the builtin
136 /// `func` op to the SPIR-V dialect. These patterns do not handle shader
137 /// interface/ABI; they convert function parameters to be of SPIR-V allowed
138 /// types.
140  RewritePatternSet &patterns);
141 
142 namespace spirv {
143 class AccessChainOp;
144 
145 /// Returns the value for the given `builtin` variable. This function gets or
146 /// inserts the global variable associated for the builtin within the nearest
147 /// symbol table enclosing `op`. Returns null Value on error.
148 ///
149 /// The global name being generated will be mangled using `preffix` and
150 /// `suffix`.
151 Value getBuiltinVariableValue(Operation *op, BuiltIn builtin, Type integerType,
152  OpBuilder &builder,
153  StringRef prefix = "__builtin__",
154  StringRef suffix = "__");
155 
156 /// Gets the value at the given `offset` of the push constant storage with a
157 /// total of `elementCount` `integerType` integers. A global variable will be
158 /// created in the nearest symbol table enclosing `op` for the push constant
159 /// storage if not existing. Load ops will be created via the given `builder` to
160 /// load values from the push constant. Returns null Value on error.
161 Value getPushConstantValue(Operation *op, unsigned elementCount,
162  unsigned offset, Type integerType,
163  OpBuilder &builder);
164 
165 /// Generates IR to perform index linearization with the given `indices` and
166 /// their corresponding `strides`, adding an initial `offset`.
168  int64_t offset, Type integerType, Location loc,
169  OpBuilder &builder);
170 
171 /// Performs the index computation to get to the element at `indices` of the
172 /// memory pointed to by `basePtr`, using the layout map of `baseType`.
173 /// Returns null if index computation cannot be performed.
174 
175 // TODO: This method assumes that the `baseType` is a MemRefType with AffineMap
176 // that has static strides. Extend to handle dynamic strides.
177 Value getElementPtr(const SPIRVTypeConverter &typeConverter,
178  MemRefType baseType, Value basePtr, ValueRange indices,
179  Location loc, OpBuilder &builder);
180 
181 // GetElementPtr implementation for Kernel/OpenCL flavored SPIR-V.
182 Value getOpenCLElementPtr(const SPIRVTypeConverter &typeConverter,
183  MemRefType baseType, Value basePtr,
184  ValueRange indices, Location loc, OpBuilder &builder);
185 
186 // GetElementPtr implementation for Vulkan/Shader flavored SPIR-V.
187 Value getVulkanElementPtr(const SPIRVTypeConverter &typeConverter,
188  MemRefType baseType, Value basePtr,
189  ValueRange indices, Location loc, OpBuilder &builder);
190 
191 } // namespace spirv
192 } // namespace mlir
193 
194 #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:206
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:378
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.
bool enableFastMathMode
Whether to enable fast math mode during conversion.