MLIR  16.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 non-32-bit scalar types with 32-bit scalar types if
34  /// no native support.
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, if a scalar type of a certain
40  /// bitwidth is not supported in the target environment. This requires the
41  /// runtime to also feed in data with a matched bitwidth and layout for
42  /// interface types. The runtime can do that by inspecting the SPIR-V
43  /// 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;
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
Include the generated interface declarations.
unsigned boolNumBits
The number of bits to store a boolean value.
Value getVulkanElementPtr(SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
bool enableFastMathMode
Whether to enable fast math mode during conversion.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
bool emulateNon32BitScalarTypes
Whether to emulate non-32-bit scalar types with 32-bit scalar types if no native support.
Value getBuiltinVariableValue(Operation *op, BuiltIn builtin, Type integerType, OpBuilder &builder)
Returns the value for the given builtin variable.
Value getOpenCLElementPtr(SPIRVTypeConverter &typeConverter, MemRefType baseType, Value basePtr, ValueRange indices, Location loc, OpBuilder &builder)
void populateBuiltinFuncToSPIRVPatterns(SPIRVTypeConverter &typeConverter, RewritePatternSet &patterns)
Appends to a pattern list additional patterns for translating the builtin func op to the SPIR-V diale...
const spirv::TargetEnv & getTargetEnv() const
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...
A wrapper class around a spirv::TargetEnvAttr to provide query methods for allowed version/capabiliti...
Definition: TargetAndABI.h:28
static bool isLegalOp(Operation *op)
Returns true if the given op is considered as legal for SPIR-V conversion.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
static llvm::ManagedStatic< PassManagerOptions > options
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...
Type conversion class.
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...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:56
This class describes a specific conversion target.
This class helps build Operations.
Definition: Builders.h:196
This class provides an abstraction over the different types of ranges over Values.
Definition: ValueRange.h:345
bool use64bitIndex
Use 64-bit integers to convert index types.
An attribute that specifies the target version, allowed extensions and capabilities, and resource limits.
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.