19#include "llvm/ADT/TypeSwitch.h"
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 ||
31 baseVector.getNumElements() != resultVector.getNumElements()))
33 "base and result must have the same number of components");
37 if (baseElement.getIntOrFloatBitWidth() <=
38 resultElement.getIntOrFloatBitWidth())
40 "result components must be narrower than base components");
44static FailureOr<std::pair<Type, Type>>
51 using TypePair = std::pair<Type, Type>;
52 auto [operandElemTy, resultElemTy] =
55 [resultType](
auto concreteOperandTy) -> TypePair {
56 if (
auto concreteResultTy =
57 dyn_cast<
decltype(concreteOperandTy)>(resultType)) {
58 return {concreteOperandTy.getElementType(),
59 concreteResultTy.getElementType()};
63 .Default([resultType](
Type operandType) -> TypePair {
64 return {operandType, resultType};
67 if (!operandElemTy || !resultElemTy) {
68 op->
emitOpError(
"incompatible operand and result types");
72 return TypePair{operandElemTy, resultElemTy};
76 bool requireSameBitWidth =
true,
77 bool skipBitWidthCheck =
false) {
79 if (skipBitWidthCheck)
82 FailureOr<std::pair<Type, Type>> elemTypes =
84 if (failed(elemTypes))
86 auto [operandElemTy, resultElemTy] = *elemTypes;
88 unsigned operandTypeBitWidth = operandElemTy.getIntOrFloatBitWidth();
89 unsigned resultTypeBitWidth = resultElemTy.getIntOrFloatBitWidth();
90 bool isSameBitWidth = operandTypeBitWidth == resultTypeBitWidth;
92 if (requireSameBitWidth) {
93 if (!isSameBitWidth) {
95 "expected the same bit widths for operand type and result "
96 "type, but provided ")
97 << operandElemTy <<
" and " << resultElemTy;
102 if (isSameBitWidth) {
104 "expected the different bit widths for operand type and result "
105 "type, but provided ")
106 << operandElemTy <<
" and " << resultElemTy;
115LogicalResult BitcastOp::verify() {
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");
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");
133 if (operandCoopMatrixType.getRows() != resultCoopMatrixType.getRows() ||
134 operandCoopMatrixType.getColumns() != resultCoopMatrixType.getColumns())
135 return emitError(
"cooperative matrix dimensions must match");
137 if (operandCoopMatrixType.getScope() != resultCoopMatrixType.getScope())
138 return emitError(
"cooperative matrix scope must match");
140 if (operandCoopMatrixType.getUse() != resultCoopMatrixType.getUse())
141 return emitError(
"cooperative matrix use must match");
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");
153 if (isa<spirv::PointerType>(operandType) &&
154 !isa<spirv::PointerType>(resultType)) {
156 "unhandled bit cast conversion from pointer type to non-pointer type");
158 if (!isa<spirv::PointerType>(operandType) &&
159 isa<spirv::PointerType>(resultType)) {
161 "unhandled bit cast conversion from non-pointer type to pointer type");
165 if (operandBitWidth != resultBitWidth) {
166 return emitOpError(
"mismatch in result type bitwidth ")
167 << resultBitWidth <<
" and operand type bitwidth "
177LogicalResult ConvertPtrToUOp::verify() {
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>();
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");
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>();
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");
219LogicalResult PtrCastToGenericOp::verify() {
221 auto resultType = cast<spirv::PointerType>(getResult().
getType());
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");
230 spirv::StorageClass resultStorage = resultType.getStorageClass();
231 if (resultStorage != spirv::StorageClass::Generic)
232 return emitError(
"result type must be of storage class Generic");
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;
247LogicalResult GenericCastToPtrOp::verify() {
249 auto resultType = cast<spirv::PointerType>(getResult().
getType());
251 spirv::StorageClass operandStorage = operandType.getStorageClass();
252 if (operandStorage != spirv::StorageClass::Generic)
253 return emitError(
"pointer type must be of storage class Generic");
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");
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;
275LogicalResult GenericCastToPtrExplicitOp::verify() {
277 auto resultType = cast<spirv::PointerType>(getResult().
getType());
279 spirv::StorageClass operandStorage = operandType.getStorageClass();
280 if (operandStorage != spirv::StorageClass::Generic)
281 return emitError(
"pointer type must be of storage class Generic");
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");
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;
303LogicalResult ConvertFToSOp::verify() {
312LogicalResult ConvertFToUOp::verify() {
321LogicalResult ConvertSToFOp::verify() {
330LogicalResult ConvertUToFOp::verify() {
339LogicalResult spirv::FConvertOp::verify() {
344 FailureOr<std::pair<Type, Type>> elemTypes =
348 auto [operandElemTy, resultElemTy] = *elemTypes;
350 if (operandElemTy == resultElemTy) {
351 return emitOpError(
"expected different component types for operand type "
352 "and result type, but provided ")
353 << operandElemTy <<
" and " << resultElemTy;
362LogicalResult spirv::SConvertOp::verify() {
370LogicalResult spirv::UConvertOp::verify() {
static Value getPointer(Location loc, Value value, ConversionPatternRewriter &rewriter)
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.
Value getOperand(unsigned idx)
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
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...
Type getType() const
Return the type of this value.
static FailureOr< std::pair< Type, Type > > getCastOpOperandAndResultElementType(Operation *op)
static LogicalResult verifyCastOp(Operation *op, bool requireSameBitWidth=true, bool skipBitWidthCheck=false)
unsigned getBitWidth(Type type)
Returns the bit width of the type.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
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