MLIR  17.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 
30  /// The number of bits to store a boolean value.
31  unsigned boolNumBits{8};
32 
33  /// Whether to emulate narrower scalar types with 32-bit scalar types if not
34  /// supported by the target.
35  ///
36  /// Non-32-bit scalar types require special hardware support that may not
37  /// exist on all GPUs. This is reflected in SPIR-V as that non-32-bit scalar
38  /// types require special capabilities or extensions. This option controls
39  /// whether to use 32-bit types to emulate < 32-bits-wide scalars, if a scalar
40  /// type of a certain bitwidth is not supported in the target environment.
41  /// This requires the runtime to also feed in data with a matched bitwidth and
42  /// layout for interface types. The runtime can do that by inspecting the
43  /// SPIR-V module.
44  ///
45  /// If the original scalar type has less than 32-bit, a multiple of its
46  /// values will be packed into one 32-bit value to be memory efficient.
48 
49  /// Use 64-bit integers to convert index types.
50  bool use64bitIndex{false};
51 
52  /// Whether to enable fast math mode during conversion. If true, various
53  /// patterns would assume no NaN/infinity numbers as inputs, and thus there
54  /// will be no special guards emitted to check and handle such cases.
55  bool enableFastMathMode{false};
56 };
57 
58 /// Type conversion from builtin types to SPIR-V types for shader interface.
59 ///
60 /// For memref types, this converter additionally performs type wrapping to
61 /// satisfy shader interface requirements: shader interface types must be
62 /// pointers to structs.
64 public:
65  explicit SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr,
66  const SPIRVConversionOptions &options = {});
67 
68  /// Gets the SPIR-V correspondence for the standard index type.
69  Type getIndexType() const;
70 
71  const spirv::TargetEnv &getTargetEnv() const { return targetEnv; }
72 
73  /// Returns the options controlling the SPIR-V type converter.
74  const SPIRVConversionOptions &getOptions() const { return options; }
75 
76  /// Checks if the SPIR-V capability inquired is supported.
77  bool allows(spirv::Capability capability);
78 
79 private:
80  spirv::TargetEnv targetEnv;
81  SPIRVConversionOptions options;
82 
83  MLIRContext *getContext() const;
84 };
85 
86 //===----------------------------------------------------------------------===//
87 // Conversion Target
88 //===----------------------------------------------------------------------===//
89 
90 // The default SPIR-V conversion target.
91 //
92 // It takes a SPIR-V target environment and controls operation legality based on
93 // the their availability in the target environment.
95 public:
96  /// Creates a SPIR-V conversion target for the given target environment.
97  static std::unique_ptr<SPIRVConversionTarget>
98  get(spirv::TargetEnvAttr targetAttr);
99 
100 private:
101  explicit SPIRVConversionTarget(spirv::TargetEnvAttr targetAttr);
102 
103  // Be explicit that instance of this class cannot be copied or moved: there
104  // are lambdas capturing fields of the instance.
107  SPIRVConversionTarget &operator=(const SPIRVConversionTarget &) = delete;
108  SPIRVConversionTarget &operator=(SPIRVConversionTarget &&) = delete;
109 
110  /// Returns true if the given `op` is legal to use under the current target
111  /// environment.
112  bool isLegalOp(Operation *op);
113 
114  spirv::TargetEnv targetEnv;
115 };
116 
117 //===----------------------------------------------------------------------===//
118 // Patterns and Utility Functions
119 //===----------------------------------------------------------------------===//
120 
121 /// Appends to a pattern list additional patterns for translating the builtin
122 /// `func` op to the SPIR-V dialect. These patterns do not handle shader
123 /// interface/ABI; they convert function parameters to be of SPIR-V allowed
124 /// types.
126  RewritePatternSet &patterns);
127 
128 namespace spirv {
129 class AccessChainOp;
130 
131 /// Returns the value for the given `builtin` variable. This function gets or
132 /// inserts the global variable associated for the builtin within the nearest
133 /// symbol table enclosing `op`. Returns null Value on error.
134 Value getBuiltinVariableValue(Operation *op, BuiltIn builtin, Type integerType,
135  OpBuilder &builder);
136 
137 /// Gets the value at the given `offset` of the push constant storage with a
138 /// total of `elementCount` `integerType` integers. A global variable will be
139 /// created in the nearest symbol table enclosing `op` for the push constant
140 /// storage if not existing. Load ops will be created via the given `builder` to
141 /// load values from the push constant. Returns null Value on error.
142 Value getPushConstantValue(Operation *op, unsigned elementCount,
143  unsigned offset, Type integerType,
144  OpBuilder &builder);
145 
146 /// Generates IR to perform index linearization with the given `indices` and
147 /// their corresponding `strides`, adding an initial `offset`.
149  int64_t offset, Type integerType, Location loc,
150  OpBuilder &builder);
151 
152 /// Performs the index computation to get to the element at `indices` of the
153 /// memory pointed to by `basePtr`, using the layout map of `baseType`.
154 /// Returns null if index computation cannot be performed.
155 
156 // TODO: This method assumes that the `baseType` is a MemRefType with AffineMap
157 // that has static strides. Extend to handle dynamic strides.
158 Value getElementPtr(SPIRVTypeConverter &typeConverter, MemRefType baseType,
159  Value basePtr, ValueRange indices, Location loc,
160  OpBuilder &builder);
161 
162 // GetElementPtr implementation for Kernel/OpenCL flavored SPIR-V.
164  MemRefType baseType, Value basePtr,
165  ValueRange indices, Location loc, OpBuilder &builder);
166 
167 // GetElementPtr implementation for Vulkan/Shader flavored SPIR-V.
169  MemRefType baseType, Value basePtr,
170  ValueRange indices, Location loc, OpBuilder &builder);
171 
172 } // namespace spirv
173 } // namespace mlir
174 
175 #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:202
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:75
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.
SPIRVTypeConverter(spirv::TargetEnvAttr targetAttr, const SPIRVConversionOptions &options={})
bool allows(spirv::Capability capability)
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:370
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
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)
Returns the value for the given builtin variable.
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 getElementPtr(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(SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
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(SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
Include the generated interface declarations.
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 to convert 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.
bool enableFastMathMode
Whether to enable fast math mode during conversion.