1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
//===- CastOps.cpp - MLIR SPIR-V Cast Ops --------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Defines the cast and conversion operations in the SPIR-V dialect.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "SPIRVOpUtils.h"
#include "SPIRVParsingUtils.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir::spirv::AttrNames;
namespace mlir::spirv {
static LogicalResult verifyCastOp(Operation *op,
bool requireSameBitWidth = true,
bool skipBitWidthCheck = false) {
// Some CastOps have no limit on bit widths for result and operand type.
if (skipBitWidthCheck)
return success();
Type operandType = op->getOperand(0).getType();
Type resultType = op->getResult(0).getType();
// ODS checks that result type and operand type have the same shape. Check
// that composite types match and extract the element types, if any.
using TypePair = std::pair<Type, Type>;
auto [operandElemTy, resultElemTy] =
TypeSwitch<Type, TypePair>(operandType)
.Case<VectorType, spirv::CooperativeMatrixType,
spirv::CooperativeMatrixNVType, spirv::JointMatrixINTELType>(
[resultType](auto concreteOperandTy) -> TypePair {
if (auto concreteResultTy =
dyn_cast<decltype(concreteOperandTy)>(resultType)) {
return {concreteOperandTy.getElementType(),
concreteResultTy.getElementType()};
}
return {};
})
.Default([resultType](Type operandType) -> TypePair {
return {operandType, resultType};
});
if (!operandElemTy || !resultElemTy)
return op->emitOpError("incompatible operand and result types");
unsigned operandTypeBitWidth = operandElemTy.getIntOrFloatBitWidth();
unsigned resultTypeBitWidth = resultElemTy.getIntOrFloatBitWidth();
bool isSameBitWidth = operandTypeBitWidth == resultTypeBitWidth;
if (requireSameBitWidth) {
if (!isSameBitWidth) {
return op->emitOpError(
"expected the same bit widths for operand type and result "
"type, but provided ")
<< operandElemTy << " and " << resultElemTy;
}
return success();
}
if (isSameBitWidth) {
return op->emitOpError(
"expected the different bit widths for operand type and result "
"type, but provided ")
<< operandElemTy << " and " << resultElemTy;
}
return success();
}
//===----------------------------------------------------------------------===//
// spirv.BitcastOp
//===----------------------------------------------------------------------===//
LogicalResult BitcastOp::verify() {
// TODO: The SPIR-V spec validation rules are different for different
// versions.
auto operandType = getOperand().getType();
auto resultType = getResult().getType();
if (operandType == resultType) {
return emitError("result type must be different from operand type");
}
if (llvm::isa<spirv::PointerType>(operandType) &&
!llvm::isa<spirv::PointerType>(resultType)) {
return emitError(
"unhandled bit cast conversion from pointer type to non-pointer type");
}
if (!llvm::isa<spirv::PointerType>(operandType) &&
llvm::isa<spirv::PointerType>(resultType)) {
return emitError(
"unhandled bit cast conversion from non-pointer type to pointer type");
}
auto operandBitWidth = getBitWidth(operandType);
auto resultBitWidth = getBitWidth(resultType);
if (operandBitWidth != resultBitWidth) {
return emitOpError("mismatch in result type bitwidth ")
<< resultBitWidth << " and operand type bitwidth "
<< operandBitWidth;
}
return success();
}
//===----------------------------------------------------------------------===//
// spirv.ConvertPtrToUOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertPtrToUOp::verify() {
auto operandType = llvm::cast<spirv::PointerType>(getPointer().getType());
auto resultType = llvm::cast<spirv::ScalarType>(getResult().getType());
if (!resultType || !resultType.isSignlessInteger())
return emitError("result must be a scalar type of unsigned integer");
auto spirvModule = (*this)->getParentOfType<spirv::ModuleOp>();
if (!spirvModule)
return success();
auto addressingModel = spirvModule.getAddressingModel();
if ((addressingModel == spirv::AddressingModel::Logical) ||
(addressingModel == spirv::AddressingModel::PhysicalStorageBuffer64 &&
operandType.getStorageClass() !=
spirv::StorageClass::PhysicalStorageBuffer))
return emitError("operand must be a physical pointer");
return success();
}
//===----------------------------------------------------------------------===//
// spirv.ConvertUToPtrOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertUToPtrOp::verify() {
auto operandType = llvm::cast<spirv::ScalarType>(getOperand().getType());
auto resultType = llvm::cast<spirv::PointerType>(getResult().getType());
if (!operandType || !operandType.isSignlessInteger())
return emitError("result must be a scalar type of unsigned integer");
auto spirvModule = (*this)->getParentOfType<spirv::ModuleOp>();
if (!spirvModule)
return success();
auto addressingModel = spirvModule.getAddressingModel();
if ((addressingModel == spirv::AddressingModel::Logical) ||
(addressingModel == spirv::AddressingModel::PhysicalStorageBuffer64 &&
resultType.getStorageClass() !=
spirv::StorageClass::PhysicalStorageBuffer))
return emitError("result must be a physical pointer");
return success();
}
//===----------------------------------------------------------------------===//
// spirv.PtrCastToGenericOp
//===----------------------------------------------------------------------===//
LogicalResult PtrCastToGenericOp::verify() {
auto operandType = llvm::cast<spirv::PointerType>(getPointer().getType());
auto resultType = llvm::cast<spirv::PointerType>(getResult().getType());
spirv::StorageClass operandStorage = operandType.getStorageClass();
if (operandStorage != spirv::StorageClass::Workgroup &&
operandStorage != spirv::StorageClass::CrossWorkgroup &&
operandStorage != spirv::StorageClass::Function)
return emitError("pointer must point to the Workgroup, CrossWorkgroup"
", or Function Storage Class");
spirv::StorageClass resultStorage = resultType.getStorageClass();
if (resultStorage != spirv::StorageClass::Generic)
return emitError("result type must be of storage class Generic");
Type operandPointeeType = operandType.getPointeeType();
Type resultPointeeType = resultType.getPointeeType();
if (operandPointeeType != resultPointeeType)
return emitOpError("pointer operand's pointee type must have the same "
"as the op result type, but found ")
<< operandPointeeType << " vs " << resultPointeeType;
return success();
}
//===----------------------------------------------------------------------===//
// spirv.GenericCastToPtrOp
//===----------------------------------------------------------------------===//
LogicalResult GenericCastToPtrOp::verify() {
auto operandType = llvm::cast<spirv::PointerType>(getPointer().getType());
auto resultType = llvm::cast<spirv::PointerType>(getResult().getType());
spirv::StorageClass operandStorage = operandType.getStorageClass();
if (operandStorage != spirv::StorageClass::Generic)
return emitError("pointer type must be of storage class Generic");
spirv::StorageClass resultStorage = resultType.getStorageClass();
if (resultStorage != spirv::StorageClass::Workgroup &&
resultStorage != spirv::StorageClass::CrossWorkgroup &&
resultStorage != spirv::StorageClass::Function)
return emitError("result must point to the Workgroup, CrossWorkgroup, "
"or Function Storage Class");
Type operandPointeeType = operandType.getPointeeType();
Type resultPointeeType = resultType.getPointeeType();
if (operandPointeeType != resultPointeeType)
return emitOpError("pointer operand's pointee type must have the same "
"as the op result type, but found ")
<< operandPointeeType << " vs " << resultPointeeType;
return success();
}
//===----------------------------------------------------------------------===//
// spirv.GenericCastToPtrExplicitOp
//===----------------------------------------------------------------------===//
LogicalResult GenericCastToPtrExplicitOp::verify() {
auto operandType = llvm::cast<spirv::PointerType>(getPointer().getType());
auto resultType = llvm::cast<spirv::PointerType>(getResult().getType());
spirv::StorageClass operandStorage = operandType.getStorageClass();
if (operandStorage != spirv::StorageClass::Generic)
return emitError("pointer type must be of storage class Generic");
spirv::StorageClass resultStorage = resultType.getStorageClass();
if (resultStorage != spirv::StorageClass::Workgroup &&
resultStorage != spirv::StorageClass::CrossWorkgroup &&
resultStorage != spirv::StorageClass::Function)
return emitError("result must point to the Workgroup, CrossWorkgroup, "
"or Function Storage Class");
Type operandPointeeType = operandType.getPointeeType();
Type resultPointeeType = resultType.getPointeeType();
if (operandPointeeType != resultPointeeType)
return emitOpError("pointer operand's pointee type must have the same "
"as the op result type, but found ")
<< operandPointeeType << " vs " << resultPointeeType;
return success();
}
//===----------------------------------------------------------------------===//
// spirv.ConvertFToSOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertFToSOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false,
/*skipBitWidthCheck=*/true);
}
//===----------------------------------------------------------------------===//
// spirv.ConvertFToUOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertFToUOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false,
/*skipBitWidthCheck=*/true);
}
//===----------------------------------------------------------------------===//
// spirv.ConvertSToFOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertSToFOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false,
/*skipBitWidthCheck=*/true);
}
//===----------------------------------------------------------------------===//
// spirv.ConvertUToFOp
//===----------------------------------------------------------------------===//
LogicalResult ConvertUToFOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false,
/*skipBitWidthCheck=*/true);
}
//===----------------------------------------------------------------------===//
// spirv.INTELConvertBF16ToFOp
//===----------------------------------------------------------------------===//
LogicalResult INTELConvertBF16ToFOp::verify() {
auto operandType = getOperand().getType();
auto resultType = getResult().getType();
// ODS checks that vector result type and vector operand type have the same
// shape.
if (auto vectorType = llvm::dyn_cast<VectorType>(operandType)) {
unsigned operandNumElements = vectorType.getNumElements();
unsigned resultNumElements =
llvm::cast<VectorType>(resultType).getNumElements();
if (operandNumElements != resultNumElements) {
return emitOpError(
"operand and result must have same number of elements");
}
}
return success();
}
//===----------------------------------------------------------------------===//
// spirv.INTELConvertFToBF16Op
//===----------------------------------------------------------------------===//
LogicalResult INTELConvertFToBF16Op::verify() {
auto operandType = getOperand().getType();
auto resultType = getResult().getType();
// ODS checks that vector result type and vector operand type have the same
// shape.
if (auto vectorType = llvm::dyn_cast<VectorType>(operandType)) {
unsigned operandNumElements = vectorType.getNumElements();
unsigned resultNumElements =
llvm::cast<VectorType>(resultType).getNumElements();
if (operandNumElements != resultNumElements) {
return emitOpError(
"operand and result must have same number of elements");
}
}
return success();
}
//===----------------------------------------------------------------------===//
// spirv.FConvertOp
//===----------------------------------------------------------------------===//
LogicalResult spirv::FConvertOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false);
}
//===----------------------------------------------------------------------===//
// spirv.SConvertOp
//===----------------------------------------------------------------------===//
LogicalResult spirv::SConvertOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false);
}
//===----------------------------------------------------------------------===//
// spirv.UConvertOp
//===----------------------------------------------------------------------===//
LogicalResult spirv::UConvertOp::verify() {
return verifyCastOp(*this, /*requireSameBitWidth=*/false);
}
} // namespace mlir::spirv
|