#include "SPIRVParsingUtils.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVAttributes.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVEnums.h"
#include "mlir/Dialect/SPIRV/IR/SPIRVOps.h"
#include "llvm/ADT/STLExtras.h"
#include <cstdint>
using namespace mlir::spirv::AttrNames;
namespace mlir::spirv {
static LogicalResult
verifyCoopMatrixAccess(Operation *op, Type pointer, Type coopMatrix,
spirv::MemoryAccessAttr memoryOperand) {
auto pointerType = cast<PointerType>(pointer);
Type pointeeType = pointerType.getPointeeType();
if (!isa<ScalarType, VectorType>(pointeeType)) {
return op->emitOpError(
"Pointer must point to a scalar or vector type but provided ")
<< pointeeType;
}
if (memoryOperand) {
spirv::MemoryAccess operandSet = memoryOperand.getValue();
if (isa<spirv::KHRCooperativeMatrixLoadOp>(op) &&
spirv::bitEnumContainsAll(operandSet,
spirv::MemoryAccess::MakePointerAvailable)) {
return op->emitOpError(
"not compatible with memory operand 'MakePointerAvailable'");
}
if (isa<spirv::KHRCooperativeMatrixStoreOp>(op) &&
spirv::bitEnumContainsAll(operandSet,
spirv::MemoryAccess::MakePointerVisible)) {
return op->emitOpError(
"not compatible with memory operand 'MakePointerVisible'");
}
if (spirv::bitEnumContainsAll(memoryOperand.getValue(),
spirv::MemoryAccess::Aligned)) {
return op->emitOpError("has unhandled memory operand 'Aligned'");
}
}
return success();
}
LogicalResult KHRCooperativeMatrixLoadOp::verify() {
return verifyCoopMatrixAccess(*this, getPointer().getType(),
getResult().getType(), getMemoryOperandAttr());
}
LogicalResult KHRCooperativeMatrixStoreOp::verify() {
return verifyCoopMatrixAccess(*this, getPointer().getType(),
getObject().getType(), getMemoryOperandAttr());
}
LogicalResult KHRCooperativeMatrixMulAddOp::verify() {
auto typeA = cast<spirv::CooperativeMatrixType>(getA().getType());
auto typeB = cast<spirv::CooperativeMatrixType>(getB().getType());
auto typeC = cast<spirv::CooperativeMatrixType>(getC().getType());
if (typeA.getUse() != CooperativeMatrixUseKHR::MatrixA)
return emitOpError("operand #0 must be of use 'MatrixA'");
if (typeB.getUse() != CooperativeMatrixUseKHR::MatrixB)
return emitOpError("operand #1 must be of use 'MatrixB'");
if (typeC.getUse() != CooperativeMatrixUseKHR::MatrixAcc)
return emitOpError("operand #2 must be of use 'MatrixAcc'");
if (!llvm::all_equal({typeA.getScope(), typeB.getScope(), typeC.getScope()}))
return emitOpError("matrix scope mismatch");
if (typeA.getRows() != typeC.getRows())
return emitOpError("matrix size mismatch on dimension 'M'");
if (typeB.getColumns() != typeC.getColumns())
return emitOpError("matrix size mismatch on dimension 'N'");
if (typeA.getColumns() != typeB.getRows())
return emitOpError("matrix size mismatch on dimension 'K'");
if (getMatrixOperands()) {
Type elementTypes[] = {typeA.getElementType(), typeB.getElementType(),
typeC.getElementType()};
if (!llvm::all_of(elementTypes, llvm::IsaPred<IntegerType>)) {
return emitOpError("Matrix Operands require all matrix element types to "
"be Integer Types");
}
}
return success();
}
}