MLIR 24.0.0git
CastOps.cpp
Go to the documentation of this file.
1//===- CastOps.cpp - MLIR SPIR-V Cast Ops --------------------------------===//
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 the cast and conversion operations in the SPIR-V dialect.
10//
11//===----------------------------------------------------------------------===//
12
14
15#include "SPIRVOpUtils.h"
16#include "SPIRVParsingUtils.h"
17
19#include "llvm/ADT/TypeSwitch.h"
20
21using namespace mlir::spirv::AttrNames;
22
23namespace mlir::spirv {
24LogicalResult BitcastExtractEXTOp::verify() {
25 Type baseType = getBase().getType();
26 Type resultType = getResult().getType();
27 auto baseVector = dyn_cast<VectorType>(baseType);
28 auto resultVector = dyn_cast<VectorType>(resultType);
29 if (!baseVector != !resultVector ||
30 (baseVector &&
31 baseVector.getNumElements() != resultVector.getNumElements()))
32 return emitOpError(
33 "base and result must have the same number of components");
34
35 Type baseElement = getElementTypeOrSelf(baseType);
36 Type resultElement = getElementTypeOrSelf(resultType);
37 if (baseElement.getIntOrFloatBitWidth() <=
38 resultElement.getIntOrFloatBitWidth())
39 return emitOpError(
40 "result components must be narrower than base components");
41 return success();
42}
43
44static FailureOr<std::pair<Type, Type>>
46 Type operandType = op->getOperand(0).getType();
47 Type resultType = op->getResult(0).getType();
48
49 // ODS checks that result type and operand type have the same shape. Check
50 // that composite types match and extract the element types, if any.
51 using TypePair = std::pair<Type, Type>;
52 auto [operandElemTy, resultElemTy] =
54 .Case<VectorType, spirv::CooperativeMatrixType>(
55 [resultType](auto concreteOperandTy) -> TypePair {
56 if (auto concreteResultTy =
57 dyn_cast<decltype(concreteOperandTy)>(resultType)) {
58 return {concreteOperandTy.getElementType(),
59 concreteResultTy.getElementType()};
60 }
61 return {};
62 })
63 .Default([resultType](Type operandType) -> TypePair {
64 return {operandType, resultType};
65 });
66
67 if (!operandElemTy || !resultElemTy) {
68 op->emitOpError("incompatible operand and result types");
69 return failure();
70 }
71
72 return TypePair{operandElemTy, resultElemTy};
73}
74
75static LogicalResult verifyCastOp(Operation *op,
76 bool requireSameBitWidth = true,
77 bool skipBitWidthCheck = false) {
78 // Some CastOps have no limit on bit widths for result and operand type.
79 if (skipBitWidthCheck)
80 return success();
81
82 FailureOr<std::pair<Type, Type>> elemTypes =
84 if (failed(elemTypes))
85 return failure();
86 auto [operandElemTy, resultElemTy] = *elemTypes;
87
88 unsigned operandTypeBitWidth = operandElemTy.getIntOrFloatBitWidth();
89 unsigned resultTypeBitWidth = resultElemTy.getIntOrFloatBitWidth();
90 bool isSameBitWidth = operandTypeBitWidth == resultTypeBitWidth;
91
92 if (requireSameBitWidth) {
93 if (!isSameBitWidth) {
94 return op->emitOpError(
95 "expected the same bit widths for operand type and result "
96 "type, but provided ")
97 << operandElemTy << " and " << resultElemTy;
98 }
99 return success();
100 }
101
102 if (isSameBitWidth) {
103 return op->emitOpError(
104 "expected the different bit widths for operand type and result "
105 "type, but provided ")
106 << operandElemTy << " and " << resultElemTy;
107 }
108 return success();
109}
110
111//===----------------------------------------------------------------------===//
112// spirv.BitcastOp
113//===----------------------------------------------------------------------===//
114
115LogicalResult BitcastOp::verify() {
116 // TODO: The SPIR-V spec validation rules are different for different
117 // versions.
118 auto operandType = getOperand().getType();
119 auto resultType = getResult().getType();
120 if (operandType == resultType) {
121 return emitError("result type must be different from operand type");
122 }
123
124 auto operandCoopMatrixType =
125 dyn_cast<spirv::CooperativeMatrixType>(operandType);
126 auto resultCoopMatrixType =
127 dyn_cast<spirv::CooperativeMatrixType>(resultType);
128 if (operandCoopMatrixType || resultCoopMatrixType) {
129 if (!operandCoopMatrixType || !resultCoopMatrixType)
130 return emitError("unhandled bit cast conversion from cooperative matrix "
131 "type to non-cooperative matrix type");
132
133 if (operandCoopMatrixType.getRows() != resultCoopMatrixType.getRows() ||
134 operandCoopMatrixType.getColumns() != resultCoopMatrixType.getColumns())
135 return emitError("cooperative matrix dimensions must match");
136
137 if (operandCoopMatrixType.getScope() != resultCoopMatrixType.getScope())
138 return emitError("cooperative matrix scope must match");
139
140 if (operandCoopMatrixType.getUse() != resultCoopMatrixType.getUse())
141 return emitError("cooperative matrix use must match");
142
143 unsigned operandBitWidth =
144 getBitWidth(operandCoopMatrixType.getElementType());
145 unsigned resultBitWidth =
146 getBitWidth(resultCoopMatrixType.getElementType());
147 if (operandBitWidth != resultBitWidth)
148 return emitOpError("mismatch in result and operand type bitwidth");
149
150 return success();
151 }
152
153 if (isa<spirv::PointerType>(operandType) &&
154 !isa<spirv::PointerType>(resultType)) {
155 return emitError(
156 "unhandled bit cast conversion from pointer type to non-pointer type");
157 }
158 if (!isa<spirv::PointerType>(operandType) &&
159 isa<spirv::PointerType>(resultType)) {
160 return emitError(
161 "unhandled bit cast conversion from non-pointer type to pointer type");
162 }
163 auto operandBitWidth = getBitWidth(operandType);
164 auto resultBitWidth = getBitWidth(resultType);
165 if (operandBitWidth != resultBitWidth) {
166 return emitOpError("mismatch in result type bitwidth ")
167 << resultBitWidth << " and operand type bitwidth "
168 << operandBitWidth;
169 }
170 return success();
171}
172
173//===----------------------------------------------------------------------===//
174// spirv.ConvertPtrToUOp
175//===----------------------------------------------------------------------===//
176
177LogicalResult ConvertPtrToUOp::verify() {
178 auto operandType = cast<spirv::PointerType>(getPointer().getType());
179 auto resultType = cast<spirv::ScalarType>(getResult().getType());
180 if (!resultType || !resultType.isSignlessInteger())
181 return emitError("result must be a scalar type of unsigned integer");
182 auto spirvModule = (*this)->getParentOfType<spirv::ModuleOp>();
183 if (!spirvModule)
184 return success();
185 auto addressingModel = spirvModule.getAddressingModel();
186 if ((addressingModel == spirv::AddressingModel::Logical) ||
187 (addressingModel == spirv::AddressingModel::PhysicalStorageBuffer64 &&
188 operandType.getStorageClass() !=
189 spirv::StorageClass::PhysicalStorageBuffer))
190 return emitError("operand must be a physical pointer");
191 return success();
192}
193
194//===----------------------------------------------------------------------===//
195// spirv.ConvertUToPtrOp
196//===----------------------------------------------------------------------===//
197
198LogicalResult ConvertUToPtrOp::verify() {
199 auto operandType = cast<spirv::ScalarType>(getOperand().getType());
200 auto resultType = cast<spirv::PointerType>(getResult().getType());
201 if (!operandType || !operandType.isSignlessInteger())
202 return emitError("operand must be a scalar type of unsigned integer");
203 auto spirvModule = (*this)->getParentOfType<spirv::ModuleOp>();
204 if (!spirvModule)
205 return success();
206 auto addressingModel = spirvModule.getAddressingModel();
207 if ((addressingModel == spirv::AddressingModel::Logical) ||
208 (addressingModel == spirv::AddressingModel::PhysicalStorageBuffer64 &&
209 resultType.getStorageClass() !=
210 spirv::StorageClass::PhysicalStorageBuffer))
211 return emitError("result must be a physical pointer");
212 return success();
213}
214
215//===----------------------------------------------------------------------===//
216// spirv.PtrCastToGenericOp
217//===----------------------------------------------------------------------===//
218
219LogicalResult PtrCastToGenericOp::verify() {
220 auto operandType = cast<spirv::PointerType>(getPointer().getType());
221 auto resultType = cast<spirv::PointerType>(getResult().getType());
222
223 spirv::StorageClass operandStorage = operandType.getStorageClass();
224 if (operandStorage != spirv::StorageClass::Workgroup &&
225 operandStorage != spirv::StorageClass::CrossWorkgroup &&
226 operandStorage != spirv::StorageClass::Function)
227 return emitError("pointer must point to the Workgroup, CrossWorkgroup"
228 ", or Function Storage Class");
229
230 spirv::StorageClass resultStorage = resultType.getStorageClass();
231 if (resultStorage != spirv::StorageClass::Generic)
232 return emitError("result type must be of storage class Generic");
233
234 Type operandPointeeType = operandType.getPointeeType();
235 Type resultPointeeType = resultType.getPointeeType();
236 if (operandPointeeType != resultPointeeType)
237 return emitOpError("pointer operand's pointee type must have the same "
238 "as the op result type, but found ")
239 << operandPointeeType << " vs " << resultPointeeType;
240 return success();
241}
242
243//===----------------------------------------------------------------------===//
244// spirv.GenericCastToPtrOp
245//===----------------------------------------------------------------------===//
246
247LogicalResult GenericCastToPtrOp::verify() {
248 auto operandType = cast<spirv::PointerType>(getPointer().getType());
249 auto resultType = cast<spirv::PointerType>(getResult().getType());
250
251 spirv::StorageClass operandStorage = operandType.getStorageClass();
252 if (operandStorage != spirv::StorageClass::Generic)
253 return emitError("pointer type must be of storage class Generic");
254
255 spirv::StorageClass resultStorage = resultType.getStorageClass();
256 if (resultStorage != spirv::StorageClass::Workgroup &&
257 resultStorage != spirv::StorageClass::CrossWorkgroup &&
258 resultStorage != spirv::StorageClass::Function)
259 return emitError("result must point to the Workgroup, CrossWorkgroup, "
260 "or Function Storage Class");
261
262 Type operandPointeeType = operandType.getPointeeType();
263 Type resultPointeeType = resultType.getPointeeType();
264 if (operandPointeeType != resultPointeeType)
265 return emitOpError("pointer operand's pointee type must have the same "
266 "as the op result type, but found ")
267 << operandPointeeType << " vs " << resultPointeeType;
268 return success();
269}
270
271//===----------------------------------------------------------------------===//
272// spirv.GenericCastToPtrExplicitOp
273//===----------------------------------------------------------------------===//
274
275LogicalResult GenericCastToPtrExplicitOp::verify() {
276 auto operandType = cast<spirv::PointerType>(getPointer().getType());
277 auto resultType = cast<spirv::PointerType>(getResult().getType());
278
279 spirv::StorageClass operandStorage = operandType.getStorageClass();
280 if (operandStorage != spirv::StorageClass::Generic)
281 return emitError("pointer type must be of storage class Generic");
282
283 spirv::StorageClass resultStorage = resultType.getStorageClass();
284 if (resultStorage != spirv::StorageClass::Workgroup &&
285 resultStorage != spirv::StorageClass::CrossWorkgroup &&
286 resultStorage != spirv::StorageClass::Function)
287 return emitError("result must point to the Workgroup, CrossWorkgroup, "
288 "or Function Storage Class");
289
290 Type operandPointeeType = operandType.getPointeeType();
291 Type resultPointeeType = resultType.getPointeeType();
292 if (operandPointeeType != resultPointeeType)
293 return emitOpError("pointer operand's pointee type must have the same "
294 "as the op result type, but found ")
295 << operandPointeeType << " vs " << resultPointeeType;
296 return success();
297}
298
299//===----------------------------------------------------------------------===//
300// spirv.ConvertFToSOp
301//===----------------------------------------------------------------------===//
302
303LogicalResult ConvertFToSOp::verify() {
304 return verifyCastOp(*this, /*requireSameBitWidth=*/false,
305 /*skipBitWidthCheck=*/true);
306}
307
308//===----------------------------------------------------------------------===//
309// spirv.ConvertFToUOp
310//===----------------------------------------------------------------------===//
311
312LogicalResult ConvertFToUOp::verify() {
313 return verifyCastOp(*this, /*requireSameBitWidth=*/false,
314 /*skipBitWidthCheck=*/true);
315}
316
317//===----------------------------------------------------------------------===//
318// spirv.ConvertSToFOp
319//===----------------------------------------------------------------------===//
320
321LogicalResult ConvertSToFOp::verify() {
322 return verifyCastOp(*this, /*requireSameBitWidth=*/false,
323 /*skipBitWidthCheck=*/true);
324}
325
326//===----------------------------------------------------------------------===//
327// spirv.ConvertUToFOp
328//===----------------------------------------------------------------------===//
329
330LogicalResult ConvertUToFOp::verify() {
331 return verifyCastOp(*this, /*requireSameBitWidth=*/false,
332 /*skipBitWidthCheck=*/true);
333}
334
335//===----------------------------------------------------------------------===//
336// spirv.FConvertOp
337//===----------------------------------------------------------------------===//
338
339LogicalResult spirv::FConvertOp::verify() {
340 // The SPIR-V spec requires the component type to differ, not the bit
341 // width: "The component type must not equal the component type in Result
342 // Type." (OpFConvert, section 3.42.11). This allows converting between
343 // same-width encodings such as f16 and bf16.
344 FailureOr<std::pair<Type, Type>> elemTypes =
346 if (failed(elemTypes))
347 return failure();
348 auto [operandElemTy, resultElemTy] = *elemTypes;
349
350 if (operandElemTy == resultElemTy) {
351 return emitOpError("expected different component types for operand type "
352 "and result type, but provided ")
353 << operandElemTy << " and " << resultElemTy;
354 }
355 return success();
356}
357
358//===----------------------------------------------------------------------===//
359// spirv.SConvertOp
360//===----------------------------------------------------------------------===//
361
362LogicalResult spirv::SConvertOp::verify() {
363 return verifyCastOp(*this, /*requireSameBitWidth=*/false);
364}
365
366//===----------------------------------------------------------------------===//
367// spirv.UConvertOp
368//===----------------------------------------------------------------------===//
369
370LogicalResult spirv::UConvertOp::verify() {
371 return verifyCastOp(*this, /*requireSameBitWidth=*/false);
372}
373
374} // namespace mlir::spirv
static Value getPointer(Location loc, Value value, ConversionPatternRewriter &rewriter)
return success()
static Value getBase(Value v)
Looks through known "view-like" ops to find the base memref.
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Value getOperand(unsigned idx)
Definition Operation.h:377
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:434
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Type getType() const
Return the type of this value.
Definition Value.h:105
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:886
static FailureOr< std::pair< Type, Type > > getCastOpOperandAndResultElementType(Operation *op)
Definition CastOps.cpp:45
static LogicalResult verifyCastOp(Operation *op, bool requireSameBitWidth=true, bool skipBitWidthCheck=false)
Definition CastOps.cpp:75
unsigned getBitWidth(Type type)
Returns the bit width of the type.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:311
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:147