| 1 | //===- TosaValidation.cpp ------------------------------------------------===// |
| 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 | // Validate if TOSA dialect input matches with the specification for given |
| 10 | // requirements. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "mlir/Dialect/Tosa/IR/TargetEnv.h" |
| 15 | #include "mlir/Dialect/Tosa/IR/TosaProfileCompliance.h" |
| 16 | #include "mlir/Dialect/Tosa/Transforms/Passes.h" |
| 17 | |
| 18 | #include <string> |
| 19 | #include <type_traits> |
| 20 | |
| 21 | #include "mlir/Dialect/Func/IR/FuncOps.h" |
| 22 | #include "mlir/Dialect/Tosa/IR/TosaOps.h" |
| 23 | #include "mlir/Dialect/Tosa/Utils/ConversionUtils.h" |
| 24 | #include "mlir/IR/Builders.h" |
| 25 | #include "mlir/IR/BuiltinOps.h" |
| 26 | #include "mlir/IR/Matchers.h" |
| 27 | #include "mlir/IR/TypeUtilities.h" |
| 28 | #include "mlir/Pass/Pass.h" |
| 29 | #include "mlir/Transforms/DialectConversion.h" |
| 30 | #include "llvm/ADT/STLExtras.h" |
| 31 | #include "llvm/ADT/StringExtras.h" |
| 32 | #include "llvm/ADT/TypeSwitch.h" |
| 33 | #include "llvm/Support/FormatVariadic.h" |
| 34 | |
| 35 | namespace mlir { |
| 36 | namespace tosa { |
| 37 | #define GEN_PASS_DEF_TOSAVALIDATION |
| 38 | #include "mlir/Dialect/Tosa/Transforms/Passes.h.inc" |
| 39 | } // namespace tosa |
| 40 | } // namespace mlir |
| 41 | |
| 42 | using namespace mlir; |
| 43 | using namespace mlir::tosa; |
| 44 | |
| 45 | namespace { |
| 46 | |
| 47 | static LogicalResult |
| 48 | checkConstantOperands(Operation *op, ArrayRef<unsigned int> operandIndices) { |
| 49 | for (const auto index : operandIndices) { |
| 50 | Attribute attr; |
| 51 | if (!matchPattern(value: op->getOperand(idx: index), pattern: m_Constant(bind_value: &attr))) { |
| 52 | return op->emitOpError(message: "expected compile time resolvable constant, but " |
| 53 | "got variable value for operand #" ) |
| 54 | << index; |
| 55 | } |
| 56 | } |
| 57 | return success(); |
| 58 | } |
| 59 | |
| 60 | static LogicalResult checkConstantOperandMul(Operation *op, |
| 61 | const TargetEnv &env) { |
| 62 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::MulOp>(Val: op)) { |
| 63 | // Check 'shift' |
| 64 | return checkConstantOperands(op, operandIndices: {2}); |
| 65 | } |
| 66 | return success(); |
| 67 | } |
| 68 | |
| 69 | static LogicalResult checkConstantOperandTable(Operation *op, |
| 70 | const TargetEnv &env) { |
| 71 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::TableOp>(Val: op)) { |
| 72 | // Check 'table' |
| 73 | return checkConstantOperands(op, operandIndices: {1}); |
| 74 | } |
| 75 | return success(); |
| 76 | } |
| 77 | |
| 78 | static LogicalResult checkConstantOperandPad(Operation *op, |
| 79 | const TargetEnv &env) { |
| 80 | if (auto padOp = dyn_cast<tosa::PadOp>(Val: op)) { |
| 81 | // Assume this op is zero-padding if padConst is not presented |
| 82 | if (!env.allows(ext: Extension::dynamic) && padOp.getPadConst()) |
| 83 | // Check 'pad_const' |
| 84 | // Note: 'padding' (operand 1) is not checked as it is a tosa.shape type |
| 85 | return checkConstantOperands(op, operandIndices: {2}); |
| 86 | } |
| 87 | return success(); |
| 88 | } |
| 89 | |
| 90 | static LogicalResult checkConstantOperandRescale(Operation *op, |
| 91 | const TargetEnv &env) { |
| 92 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::RescaleOp>(Val: op)) { |
| 93 | // Check 'multiplier', 'shift', 'input_zp' and 'output_zp' |
| 94 | return checkConstantOperands(op, operandIndices: {1, 2, 3, 4}); |
| 95 | } |
| 96 | return success(); |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | static LogicalResult checkConstantOperandConvOps(Operation *op, |
| 101 | const TargetEnv &env) { |
| 102 | if (!env.allows(ext: Extension::dynamic) && isa<T>(op)) { |
| 103 | // Check 'input_zp' and 'weight_zp' |
| 104 | return checkConstantOperands(op, operandIndices: {3, 4}); |
| 105 | } |
| 106 | return success(); |
| 107 | } |
| 108 | |
| 109 | static LogicalResult checkConstantOperandMatMul(Operation *op, |
| 110 | const TargetEnv &env) { |
| 111 | if (!env.allows(ext: Extension::dynamic) && |
| 112 | isa<tosa::MatMulOp, tosa::MatMulTOp>(Val: op)) { |
| 113 | // Check 'A_zp' and 'B_zp' |
| 114 | return checkConstantOperands(op, operandIndices: {2, 3}); |
| 115 | } |
| 116 | return success(); |
| 117 | } |
| 118 | |
| 119 | static LogicalResult |
| 120 | checkConstantOperandRowGatherBlockScaled(Operation *op, const TargetEnv &env) { |
| 121 | if (!env.allows(ext: Extension::dynamic) && |
| 122 | isa<tosa::RowGatherBlockScaledOp>(Val: op)) { |
| 123 | auto rowGatherOp = cast<tosa::RowGatherBlockScaledOp>(Val: op); |
| 124 | const unsigned rowCountIndex = rowGatherOp.getValues().size() + 1; |
| 125 | return checkConstantOperands(op, operandIndices: {rowCountIndex}); |
| 126 | } |
| 127 | return success(); |
| 128 | } |
| 129 | |
| 130 | static LogicalResult checkConstantOperandRowGather(Operation *op, |
| 131 | const TargetEnv &env) { |
| 132 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::RowGatherOp>(Val: op)) { |
| 133 | // Check 'row_count' |
| 134 | return checkConstantOperands(op, operandIndices: {2}); |
| 135 | } |
| 136 | return success(); |
| 137 | } |
| 138 | |
| 139 | static LogicalResult checkConstantOperandAvgPool2d(Operation *op, |
| 140 | const TargetEnv &env) { |
| 141 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::AvgPool2dOp>(Val: op)) { |
| 142 | // Check 'input_zp' and 'output_zp' |
| 143 | return checkConstantOperands(op, operandIndices: {1, 2}); |
| 144 | } |
| 145 | return success(); |
| 146 | } |
| 147 | |
| 148 | static LogicalResult |
| 149 | checkConstantOperandAvgPool2dAdaptive(Operation *op, const TargetEnv &env) { |
| 150 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::AvgPool2dAdaptiveOp>(Val: op)) { |
| 151 | // Check 'input_zp' and 'output_zp'. |
| 152 | // Note: 'kernel', 'stride', and 'pad' (operands 3, 4, 5) are not checked |
| 153 | // as they are tosa.shape types. |
| 154 | return checkConstantOperands(op, operandIndices: {1, 2}); |
| 155 | } |
| 156 | return success(); |
| 157 | } |
| 158 | |
| 159 | static LogicalResult checkConstantOperandNegate(Operation *op, |
| 160 | const TargetEnv &env) { |
| 161 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::NegateOp>(Val: op)) { |
| 162 | // Check 'input1_zp' and 'output_zp' |
| 163 | return checkConstantOperands(op, operandIndices: {1, 2}); |
| 164 | } |
| 165 | return success(); |
| 166 | } |
| 167 | |
| 168 | static LogicalResult checkConstantOperandSilceShape(Operation *op, |
| 169 | const TargetEnv &env) { |
| 170 | if (!env.allows(ext: Extension::dynamic) && isa<tosa::SliceShapeOp>(Val: op)) { |
| 171 | // Check 'start' and 'size' |
| 172 | return checkConstantOperands(op, operandIndices: {1, 2}); |
| 173 | } |
| 174 | return success(); |
| 175 | } |
| 176 | |
| 177 | // MATMUL's data type availability predates variadic batch support, so the |
| 178 | // generated availability checks cannot distinguish its 1.0 and 1.1 shapes. |
| 179 | static LogicalResult checkSpecificationVersionConstraint(Operation *op, |
| 180 | const TargetEnv &env) { |
| 181 | auto matmul = dyn_cast<tosa::MatMulOp>(Val: op); |
| 182 | if (!matmul || |
| 183 | env.getSpecVersion().isBackwardsCompatibleWith( |
| 184 | baseVersion: TosaSpecificationVersion(SpecificationVersion::V_1_1_DRAFT))) |
| 185 | return success(); |
| 186 | |
| 187 | auto aType = dyn_cast<RankedTensorType>(Val: matmul.getA().getType()); |
| 188 | auto bType = dyn_cast<RankedTensorType>(Val: matmul.getB().getType()); |
| 189 | auto outputType = dyn_cast<RankedTensorType>(Val: matmul.getOutput().getType()); |
| 190 | auto getBatchDimOrDynamic = [](RankedTensorType type) { |
| 191 | return type ? type.getDimSize(idx: 0) : ShapedType::kDynamic; |
| 192 | }; |
| 193 | if ((!aType || aType.getRank() == 3) && (!bType || bType.getRank() == 3) && |
| 194 | (!outputType || outputType.getRank() == 3) && |
| 195 | succeeded(Result: verifyCompatibleDims(dims: {getBatchDimOrDynamic(aType), |
| 196 | getBatchDimOrDynamic(bType), |
| 197 | getBatchDimOrDynamic(outputType)}))) |
| 198 | return success(); |
| 199 | |
| 200 | return op->emitOpError( |
| 201 | message: "MATMUL ranks other than 3 or batch broadcasting require TOSA " |
| 202 | "specification version 1.1.draft" ); |
| 203 | } |
| 204 | |
| 205 | //===----------------------------------------------------------------------===// |
| 206 | // TOSA Validation Pass. |
| 207 | //===----------------------------------------------------------------------===// |
| 208 | |
| 209 | struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> { |
| 210 | public: |
| 211 | explicit TosaValidation() { populateConstantOperandChecks(); } |
| 212 | |
| 213 | explicit TosaValidation(const TosaValidationOptions &options) |
| 214 | : TosaValidation() { |
| 215 | this->strictOpSpecAlignment = options.strictOpSpecAlignment; |
| 216 | this->allowInvalidOpDatatypeCombinations = |
| 217 | options.allowInvalidOpDatatypeCombinations; |
| 218 | this->validateFunctionSignature = options.validateFunctionSignature; |
| 219 | } |
| 220 | void runOnOperation() final; |
| 221 | |
| 222 | LogicalResult applyConstantOperandCheck(Operation *op) { |
| 223 | for (auto &checker : constCheckers) { |
| 224 | if (failed(Result: checker(op, targetEnv))) |
| 225 | return failure(); |
| 226 | } |
| 227 | return success(); |
| 228 | } |
| 229 | |
| 230 | LogicalResult applyFunctionSignatureCheck(func::FuncOp op); |
| 231 | LogicalResult applyLevelCheck(Operation *op); |
| 232 | LogicalResult applyAttributeCheck(Operation *op); |
| 233 | |
| 234 | // check variable read/write data types against variable declarations |
| 235 | LogicalResult applyVariableCheck(Operation *op); |
| 236 | |
| 237 | // check error if conditions |
| 238 | LogicalResult applyErrorIfCheck(Operation *op); |
| 239 | |
| 240 | private: |
| 241 | void populateConstantOperandChecks() { |
| 242 | constCheckers.emplace_back(Args&: checkConstantOperandMul); |
| 243 | constCheckers.emplace_back(Args&: checkConstantOperandTable); |
| 244 | constCheckers.emplace_back(Args&: checkConstantOperandPad); |
| 245 | constCheckers.emplace_back(Args&: checkConstantOperandRescale); |
| 246 | constCheckers.emplace_back(Args&: checkConstantOperandConvOps<tosa::Conv2DOp>); |
| 247 | constCheckers.emplace_back(Args&: checkConstantOperandConvOps<tosa::Conv3DOp>); |
| 248 | constCheckers.emplace_back( |
| 249 | Args&: checkConstantOperandConvOps<tosa::DepthwiseConv2DOp>); |
| 250 | constCheckers.emplace_back( |
| 251 | Args&: checkConstantOperandConvOps<tosa::TransposeConv2DOp>); |
| 252 | constCheckers.emplace_back(Args&: checkConstantOperandMatMul); |
| 253 | constCheckers.emplace_back(Args&: checkConstantOperandRowGather); |
| 254 | constCheckers.emplace_back(Args&: checkConstantOperandRowGatherBlockScaled); |
| 255 | constCheckers.emplace_back(Args&: checkConstantOperandAvgPool2d); |
| 256 | constCheckers.emplace_back(Args&: checkConstantOperandAvgPool2dAdaptive); |
| 257 | constCheckers.emplace_back(Args&: checkConstantOperandNegate); |
| 258 | constCheckers.emplace_back(Args&: checkConstantOperandSilceShape); |
| 259 | } |
| 260 | |
| 261 | LogicalResult levelCheck(Operation *op, const int32_t calculatedValue, |
| 262 | const int32_t maxLevel, const StringRef inputName, |
| 263 | const StringRef levelName) { |
| 264 | if (calculatedValue > maxLevel) |
| 265 | return op->emitOpError() |
| 266 | << "failed level check: " << inputName << " <= " << levelName |
| 267 | << " (" << maxLevel << "), got " << calculatedValue; |
| 268 | return success(); |
| 269 | } |
| 270 | |
| 271 | LogicalResult levelCheckKernel(Operation *op, int32_t v, |
| 272 | const StringRef inputName) { |
| 273 | return levelCheck(op, calculatedValue: v, maxLevel: targetEnv.getLevel().MAX_KERNEL, inputName, |
| 274 | levelName: "MAX_KERNEL" ); |
| 275 | } |
| 276 | |
| 277 | LogicalResult levelCheckStride(Operation *op, int32_t v, |
| 278 | const StringRef inputName) { |
| 279 | return levelCheck(op, calculatedValue: v, maxLevel: targetEnv.getLevel().MAX_STRIDE, inputName, |
| 280 | levelName: "MAX_STRIDE" ); |
| 281 | } |
| 282 | |
| 283 | LogicalResult levelCheckScale(Operation *op, int32_t v, |
| 284 | const StringRef inputName) { |
| 285 | return levelCheck(op, calculatedValue: v, maxLevel: targetEnv.getLevel().MAX_SCALE, inputName, |
| 286 | levelName: "MAX_SCALE" ); |
| 287 | } |
| 288 | |
| 289 | LogicalResult levelCheckListSize(Operation *op, int32_t v, |
| 290 | const StringRef inputName) { |
| 291 | const std::string inputDesc = |
| 292 | llvm::formatv(Fmt: "length(tensor_list_shape({0}))" , Vals: inputName); |
| 293 | return levelCheck(op, calculatedValue: v, maxLevel: targetEnv.getLevel().MAX_TENSOR_LIST_SIZE, |
| 294 | inputName: inputDesc, levelName: "MAX_TENSOR_LIST_SIZE" ); |
| 295 | } |
| 296 | |
| 297 | // Perform the Level Rank check on the tensor type. |
| 298 | LogicalResult levelCheckRank(Operation *op, const Type typeToCheck, |
| 299 | const StringRef operandOrResult, |
| 300 | int32_t highest_rank) { |
| 301 | if (ShapedType type = dyn_cast<ShapedType>(Val: typeToCheck)) { |
| 302 | if (!type.hasRank()) |
| 303 | return op->emitOpError() << "failed level check: unranked tensor" ; |
| 304 | if (type.getRank() > highest_rank) |
| 305 | return op->emitOpError() << "failed level check: " << operandOrResult |
| 306 | << " rank(shape) <= MAX_RANK" ; |
| 307 | } |
| 308 | return success(); |
| 309 | } |
| 310 | |
| 311 | // Perform the Level Rank check on the tensor value. |
| 312 | LogicalResult levelCheckRank(Operation *op, const Value &v, |
| 313 | const StringRef operandOrResult, |
| 314 | int32_t highest_rank) { |
| 315 | return levelCheckRank(op, typeToCheck: v.getType(), operandOrResult, highest_rank); |
| 316 | } |
| 317 | |
| 318 | // Perform the Level tensor size check on the tensor type. |
| 319 | LogicalResult levelCheckSize(Operation *op, const Type &typeToCheck, |
| 320 | const StringRef operandOrResult); |
| 321 | |
| 322 | // Perform the Level tensor size check on the tensor value. |
| 323 | LogicalResult levelCheckSize(Operation *op, const Value &v, |
| 324 | const StringRef operandOrResult) { |
| 325 | return levelCheckSize(op, typeToCheck: v.getType(), operandOrResult); |
| 326 | } |
| 327 | |
| 328 | // Perform the Level shape length check on a value. |
| 329 | LogicalResult levelCheckShapeLength(Operation *op, const Type typeToCheck, |
| 330 | const StringRef operandOrResult) { |
| 331 | if (tosa::shapeType shapeType = dyn_cast<tosa::shapeType>(Val: typeToCheck)) { |
| 332 | if (shapeType.getRank() > targetEnv.getLevel().MAX_SHAPE_LEN) |
| 333 | return op->emitOpError() |
| 334 | << "failed shape type level check: " << typeToCheck |
| 335 | << " exceeds MAX_SHAPE_LEN" ; |
| 336 | } |
| 337 | return success(); |
| 338 | } |
| 339 | |
| 340 | // Level check sizes of all operands and results of the operation. |
| 341 | template <typename T> |
| 342 | LogicalResult levelCheckSizes(T tosaOp) { |
| 343 | auto op = tosaOp.getOperation(); |
| 344 | for (auto v : op->getOperands()) { |
| 345 | if (failed(levelCheckSize(op, v, "operand" ))) |
| 346 | return failure(); |
| 347 | } |
| 348 | |
| 349 | for (auto v : op->getResults()) { |
| 350 | if (failed(levelCheckSize(op, v, "result" ))) |
| 351 | return failure(); |
| 352 | } |
| 353 | return success(); |
| 354 | } |
| 355 | |
| 356 | // Level check ranks of all operands, attribute and results of the operation. |
| 357 | template <typename T> |
| 358 | LogicalResult levelCheckRanks(T tosaOp) { |
| 359 | auto op = tosaOp.getOperation(); |
| 360 | const TosaLevel tosaLevel = targetEnv.getLevel(); |
| 361 | for (auto v : op->getOperands()) { |
| 362 | if (failed(levelCheckRank(op, v, "operand" , tosaLevel.MAX_RANK))) |
| 363 | return failure(); |
| 364 | } |
| 365 | |
| 366 | for (auto v : op->getResults()) { |
| 367 | if (failed(levelCheckRank(op, v, "result" , tosaLevel.MAX_RANK))) |
| 368 | return failure(); |
| 369 | } |
| 370 | return success(); |
| 371 | } |
| 372 | // Level check shape lengths of all operands and results of an operation that |
| 373 | // are tosa.shape type. |
| 374 | template <typename T> |
| 375 | LogicalResult levelCheckShapeLengths(T tosaOp) { |
| 376 | for (const auto &v : tosaOp->getOperands()) { |
| 377 | if (failed(levelCheckShapeLength(op: tosaOp, typeToCheck: v.getType(), operandOrResult: "operand" ))) |
| 378 | return failure(); |
| 379 | } |
| 380 | for (const auto &v : tosaOp->getResults()) { |
| 381 | if (failed(levelCheckShapeLength(op: tosaOp, typeToCheck: v.getType(), operandOrResult: "result" ))) |
| 382 | return failure(); |
| 383 | } |
| 384 | |
| 385 | return success(); |
| 386 | } |
| 387 | |
| 388 | // Level check ranks and sizes. |
| 389 | LogicalResult levelCheckRanksAndSizes(Operation *op); |
| 390 | |
| 391 | // Pool Op: level check kernel/stride/pad values |
| 392 | template <typename T> |
| 393 | LogicalResult levelCheckPool(Operation *op) { |
| 394 | if (auto poolOp = dyn_cast<T>(op)) { |
| 395 | for (auto k : poolOp.getKernel()) { |
| 396 | if (failed(levelCheckKernel(op, v: k, inputName: "kernel" ))) { |
| 397 | return failure(); |
| 398 | } |
| 399 | } |
| 400 | for (auto s : poolOp.getStride()) { |
| 401 | if (failed(levelCheckStride(op, v: s, inputName: "stride" ))) { |
| 402 | return failure(); |
| 403 | } |
| 404 | } |
| 405 | for (auto p : poolOp.getPad()) { |
| 406 | if (failed(levelCheckKernel(op, v: p, inputName: "pad" ))) { |
| 407 | return failure(); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | return success(); |
| 412 | } |
| 413 | |
| 414 | template <typename T> |
| 415 | static constexpr bool IsSupportedAdaptivePoolOp = |
| 416 | std::is_same_v<T, tosa::AvgPool2dAdaptiveOp> || |
| 417 | std::is_same_v<T, tosa::MaxPool2dAdaptiveOp>; |
| 418 | |
| 419 | template <typename T, typename std::enable_if<IsSupportedAdaptivePoolOp<T>, |
| 420 | int>::type = 0> |
| 421 | LogicalResult levelCheckAdaptivePool(Operation *op) { |
| 422 | auto poolOp = dyn_cast<T>(op); |
| 423 | if (!poolOp) |
| 424 | return success(); |
| 425 | |
| 426 | SmallVector<int64_t> kernelValues; |
| 427 | if (tosa::getConstShapeValues(op: poolOp.getKernel().getDefiningOp(), |
| 428 | result_shape&: kernelValues)) { |
| 429 | for (const auto k : kernelValues) |
| 430 | if (failed(Result: levelCheckKernel(op, v: k, inputName: "kernel" ))) |
| 431 | return failure(); |
| 432 | } |
| 433 | |
| 434 | SmallVector<int64_t> strideValues; |
| 435 | if (tosa::getConstShapeValues(op: poolOp.getStride().getDefiningOp(), |
| 436 | result_shape&: strideValues)) { |
| 437 | for (const auto s : strideValues) |
| 438 | if (failed(Result: levelCheckStride(op, v: s, inputName: "stride" ))) |
| 439 | return failure(); |
| 440 | } |
| 441 | |
| 442 | SmallVector<int64_t> padValues; |
| 443 | if (tosa::getConstShapeValues(op: poolOp.getPad().getDefiningOp(), result_shape&: padValues)) { |
| 444 | for (const auto p : padValues) |
| 445 | if (failed(Result: levelCheckKernel(op, v: p, inputName: "pad" ))) |
| 446 | return failure(); |
| 447 | } |
| 448 | |
| 449 | return success(); |
| 450 | } |
| 451 | |
| 452 | // Conv Op: level check dilation/stride/pad values |
| 453 | template <typename T> |
| 454 | LogicalResult levelCheckConv(Operation *op) { |
| 455 | if (auto convOp = dyn_cast<T>(op)) { |
| 456 | |
| 457 | for (auto k : convOp.getDilation()) { |
| 458 | if (failed(levelCheckKernel(op, v: k, inputName: "dilation" ))) { |
| 459 | return failure(); |
| 460 | } |
| 461 | } |
| 462 | for (auto p : convOp.getPad()) { |
| 463 | if (failed(levelCheckKernel(op, v: p, inputName: "pad" ))) { |
| 464 | return failure(); |
| 465 | } |
| 466 | } |
| 467 | for (auto s : convOp.getStride()) { |
| 468 | if (failed(levelCheckStride(op, v: s, inputName: "stride" ))) { |
| 469 | return failure(); |
| 470 | } |
| 471 | } |
| 472 | auto dilation = convOp.getDilation(); |
| 473 | if (ShapedType weightType = |
| 474 | dyn_cast<ShapedType>(Val: op->getOperand(idx: 1).getType())) { |
| 475 | auto shape = weightType.getShape(); |
| 476 | if (isa<tosa::Conv2DOp>(Val: op)) { |
| 477 | assert(shape.size() == 4); |
| 478 | assert(dilation.size() == 2); |
| 479 | if (failed(levelCheckKernel(op, v: dilation[0] * shape[1], |
| 480 | inputName: "dilation_y * KH" )) || |
| 481 | failed(levelCheckKernel(op, v: dilation[1] * shape[2], |
| 482 | inputName: "dilation_x * KW" ))) |
| 483 | return failure(); |
| 484 | } else if (isa<tosa::Conv3DOp>(Val: op)) { |
| 485 | assert(shape.size() == 5); |
| 486 | assert(dilation.size() == 3); |
| 487 | if (failed(levelCheckKernel(op, v: dilation[0] * shape[1], |
| 488 | inputName: "dilation_d * KD" )) || |
| 489 | failed(levelCheckKernel(op, v: dilation[1] * shape[2], |
| 490 | inputName: "dilation_y * KH" )) || |
| 491 | failed(levelCheckKernel(op, v: dilation[2] * shape[3], |
| 492 | inputName: "dilation_x * KW" ))) |
| 493 | return failure(); |
| 494 | } else if (isa<tosa::DepthwiseConv2DOp>(Val: op)) { |
| 495 | assert(shape.size() == 4); |
| 496 | assert(dilation.size() == 2); |
| 497 | if (failed(levelCheckKernel(op, v: dilation[0] * shape[0], |
| 498 | inputName: "dilation_y * KH" )) || |
| 499 | failed(levelCheckKernel(op, v: dilation[1] * shape[1], |
| 500 | inputName: "dilation_x * KW" ))) |
| 501 | return failure(); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | return success(); |
| 506 | } |
| 507 | |
| 508 | LogicalResult levelCheckConv2DBlockScaled(Operation *op) { |
| 509 | auto convOp = dyn_cast<Conv2DBlockScaledOp>(Val: op); |
| 510 | if (!convOp) |
| 511 | return success(); |
| 512 | |
| 513 | SmallVector<int64_t> padValues; |
| 514 | if (tosa::getConstShapeValues(op: convOp.getPad().getDefiningOp(), result_shape&: padValues)) { |
| 515 | for (const auto p : padValues) |
| 516 | if (failed(Result: levelCheckKernel(op, v: p, inputName: "pad <= MAX_KERNEL" ))) |
| 517 | return failure(); |
| 518 | } |
| 519 | |
| 520 | SmallVector<int64_t> strideValues; |
| 521 | if (tosa::getConstShapeValues(op: convOp.getStride().getDefiningOp(), |
| 522 | result_shape&: strideValues)) { |
| 523 | for (const auto s : strideValues) |
| 524 | if (failed(Result: levelCheckKernel(op, v: s, inputName: "stride <= MAX_KERNEL" ))) |
| 525 | return failure(); |
| 526 | } |
| 527 | |
| 528 | SmallVector<int64_t> dilationValues; |
| 529 | if (tosa::getConstShapeValues(op: convOp.getDilation().getDefiningOp(), |
| 530 | result_shape&: dilationValues)) { |
| 531 | int64_t KH = ShapedType::kDynamic; |
| 532 | int64_t KW = ShapedType::kDynamic; |
| 533 | const ShapeAdaptor weightDataShape(convOp.getWeightData().getType()); |
| 534 | KH = weightDataShape.getDimSize(index: 1); |
| 535 | KW = weightDataShape.getDimSize(index: 2); |
| 536 | const ShapeAdaptor weightScaleShape(convOp.getWeightScale().getType()); |
| 537 | KH = ShapedType::isDynamic(dValue: KH) ? weightScaleShape.getDimSize(index: 1) : KH; |
| 538 | KW = ShapedType::isDynamic(dValue: KW) ? weightScaleShape.getDimSize(index: 2) : KW; |
| 539 | |
| 540 | if (!ShapedType::isDynamic(dValue: KH) && |
| 541 | failed(Result: levelCheckKernel(op, v: dilationValues[0] * KH, |
| 542 | inputName: "dilation_y * KH <= MAX_KERNEL)" ))) |
| 543 | return failure(); |
| 544 | |
| 545 | if (!ShapedType::isDynamic(dValue: KW) && |
| 546 | failed(Result: levelCheckKernel(op, v: dilationValues[1] * KW, |
| 547 | inputName: "dilation_x * KW <= MAX_KERNEL)" ))) |
| 548 | return failure(); |
| 549 | } |
| 550 | |
| 551 | return success(); |
| 552 | } |
| 553 | |
| 554 | // FFT op: level check H, W in input shape [N,H,W] |
| 555 | template <typename T> |
| 556 | LogicalResult levelCheckFFT(Operation *op) { |
| 557 | if (isa<T>(op)) { |
| 558 | for (auto v : op->getOperands()) { |
| 559 | if (ShapedType type = dyn_cast<ShapedType>(Val: v.getType())) { |
| 560 | auto shape = type.getShape(); |
| 561 | assert(shape.size() == 3); |
| 562 | if (failed(Result: levelCheckKernel(op, v: shape[1], inputName: "H" )) || |
| 563 | failed(Result: levelCheckKernel(op, v: shape[2], inputName: "W" ))) { |
| 564 | return failure(); |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | return success(); |
| 570 | } |
| 571 | |
| 572 | // TransposeConv2d op: level check kH/kW, outpad, and stride |
| 573 | LogicalResult levelCheckTransposeConv2d(Operation *op) { |
| 574 | if (auto transpose = dyn_cast<tosa::TransposeConv2DOp>(Val: op)) { |
| 575 | if (ShapedType filterType = |
| 576 | dyn_cast<ShapedType>(Val: transpose.getWeight().getType())) { |
| 577 | auto shape = filterType.getShape(); |
| 578 | assert(shape.size() == 4); |
| 579 | // level check kernel sizes for kH and KW |
| 580 | if (failed(Result: levelCheckKernel(op, v: shape[1], inputName: "KH" )) || |
| 581 | failed(Result: levelCheckKernel(op, v: shape[2], inputName: "KW" ))) { |
| 582 | return failure(); |
| 583 | } |
| 584 | } |
| 585 | for (auto p : transpose.getOutPad()) { |
| 586 | if (failed(Result: levelCheckKernel(op, v: p, inputName: "pad" ))) { |
| 587 | return failure(); |
| 588 | } |
| 589 | } |
| 590 | for (auto s : transpose.getStride()) { |
| 591 | if (failed(Result: levelCheckStride(op, v: s, inputName: "stride" ))) { |
| 592 | return failure(); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | return success(); |
| 597 | } |
| 598 | |
| 599 | // Resize op: level check max scales |
| 600 | LogicalResult levelCheckResize(Operation *op) { |
| 601 | if (auto resize = dyn_cast<tosa::ResizeOp>(Val: op)) { |
| 602 | SmallVector<int64_t> scale; |
| 603 | if (!tosa::getConstShapeValues(op: resize.getScale().getDefiningOp(), |
| 604 | result_shape&: scale)) { |
| 605 | return failure(); |
| 606 | } |
| 607 | const int64_t scaleYN = scale[0]; |
| 608 | const int64_t scaleYD = scale[1]; |
| 609 | const int64_t scaleXN = scale[2]; |
| 610 | const int64_t scaleXD = scale[3]; |
| 611 | |
| 612 | // Invalid (non-positive) scale values are reported by the ERROR_IF |
| 613 | // check in applyErrorIfCheck(). Skip the level ratio check here to |
| 614 | // avoid dividing by a non-positive value and reporting a misleading |
| 615 | // level error for an already-invalid scale. |
| 616 | if (llvm::any_of(Range&: scale, P: [](int64_t s) { return s <= 0; })) |
| 617 | return success(); |
| 618 | |
| 619 | if (failed( |
| 620 | Result: levelCheckScale(op, v: scaleYN / scaleYD, inputName: "scale_y_n/scale_y_d" )) || |
| 621 | failed( |
| 622 | Result: levelCheckScale(op, v: scaleXN / scaleXD, inputName: "scale_x_n/scale_x_d" ))) { |
| 623 | return failure(); |
| 624 | } |
| 625 | } |
| 626 | return success(); |
| 627 | } |
| 628 | |
| 629 | // Recursively perform a bottom-up search to determine the maximum nesting |
| 630 | // depth, starting from a specific operation and continuing up to the function |
| 631 | // or module scope. Tosa nesting_depth starts at 0 and increments by one each |
| 632 | // time a new nested `region` is encountered. |
| 633 | static void getMaxNestedDepth(Operation *op, int32_t &depth) { |
| 634 | if (isa<mlir::func::FuncOp>(Val: op) || isa<ModuleOp>(Val: op)) |
| 635 | return; |
| 636 | |
| 637 | op = op->getParentOp(); |
| 638 | if (!op) |
| 639 | return; |
| 640 | |
| 641 | depth++; |
| 642 | getMaxNestedDepth(op, depth); |
| 643 | } |
| 644 | |
| 645 | LogicalResult levelCheckMaxNesting(Operation *op) { |
| 646 | int32_t maxNestedDepth = 0; |
| 647 | getMaxNestedDepth(op, depth&: maxNestedDepth); |
| 648 | |
| 649 | const int32_t maxNestingLevel = targetEnv.getLevel().MAX_NESTING; |
| 650 | if (maxNestedDepth >= maxNestingLevel) |
| 651 | return op->emitOpError() |
| 652 | << "failed level check: tosa_nesting_depth < MAX_NESTING" << " (" |
| 653 | << maxNestingLevel << "), got " << maxNestedDepth; |
| 654 | return success(); |
| 655 | } |
| 656 | |
| 657 | LogicalResult levelCheckListSize(Operation *op) { |
| 658 | if (auto concat = dyn_cast<tosa::ConcatOp>(Val: op)) { |
| 659 | return levelCheckListSize(op, v: concat.getInput1().size(), inputName: "input1" ); |
| 660 | } |
| 661 | if (auto custom = dyn_cast<tosa::CustomOp>(Val: op)) { |
| 662 | if (failed(Result: levelCheckListSize(op, v: custom.getInputList().size(), |
| 663 | inputName: "input_list" )) || |
| 664 | failed(Result: levelCheckListSize(op, v: custom.getOutputList().size(), |
| 665 | inputName: "output_list" ))) { |
| 666 | return failure(); |
| 667 | } |
| 668 | } |
| 669 | if (auto condIf = dyn_cast<tosa::IfOp>(Val: op)) { |
| 670 | if (failed( |
| 671 | Result: levelCheckListSize(op, v: condIf.getInputList().size(), inputName: "inputs" )) || |
| 672 | failed(Result: levelCheckListSize(op, v: condIf.getOutputList().size(), |
| 673 | inputName: "outputs" ))) { |
| 674 | return failure(); |
| 675 | } |
| 676 | } |
| 677 | if (auto w = dyn_cast<tosa::WhileOp>(Val: op)) { |
| 678 | if (failed(Result: levelCheckListSize(op, v: w.getInputList().size(), inputName: "inputs" )) || |
| 679 | failed(Result: levelCheckListSize(op, v: w.getOutputList().size(), inputName: "outputs" ))) { |
| 680 | return failure(); |
| 681 | } |
| 682 | } |
| 683 | if (auto concat_shape = dyn_cast<tosa::ConcatShapeOp>(Val: op)) |
| 684 | return levelCheckListSize(op, v: concat_shape.getInput().size(), inputName: "input" ); |
| 685 | return success(); |
| 686 | } |
| 687 | |
| 688 | LogicalResult attributeCheckRescale(Operation *op) { |
| 689 | if (auto rescale = dyn_cast<tosa::RescaleOp>(Val: op)) { |
| 690 | if (rescale.getRoundingMode() == RoundingMode::DOUBLE_ROUND && |
| 691 | !targetEnv.allows(ext: Extension::doubleround)) { |
| 692 | op->emitOpError() |
| 693 | << "failed attribute check: rounding_mode = DOUBLE_ROUND " |
| 694 | << "requires extension [doubleround]" ; |
| 695 | return failure(); |
| 696 | } |
| 697 | if (rescale.getRoundingMode() == RoundingMode::INEXACT_ROUND && |
| 698 | !targetEnv.allows(ext: Extension::inexactround)) { |
| 699 | op->emitOpError() |
| 700 | << "failed attribute check: rounding_mode = INEXACT_ROUND " |
| 701 | << "requires extension [inexactround]" ; |
| 702 | return failure(); |
| 703 | } |
| 704 | } |
| 705 | return success(); |
| 706 | } |
| 707 | |
| 708 | LogicalResult attributeCheckCast(Operation *op) { |
| 709 | if (auto cast = dyn_cast<tosa::CastOp>(Val: op)) { |
| 710 | const TosaSpecificationVersion targetVersion = targetEnv.getSpecVersion(); |
| 711 | const TosaSpecificationVersion minRequiredVersion(1, 1, true); |
| 712 | if (cast.getInputUnsigned() && |
| 713 | !(targetVersion.isBackwardsCompatibleWith(baseVersion: minRequiredVersion))) |
| 714 | return op->emitOpError() |
| 715 | << "failed attribute check: CAST attribute input_unsigned " |
| 716 | << "requires version 1.1.draft" |
| 717 | << " (got " << stringifyVersion(version: targetVersion) << ") " ; |
| 718 | } |
| 719 | return success(); |
| 720 | } |
| 721 | |
| 722 | LogicalResult CheckVariable(Operation *op); |
| 723 | LogicalResult CheckVariableReadOrWrite(Operation *op); |
| 724 | LogicalResult validateValidElementType(Operation *op, Type type, |
| 725 | bool allowUnsigned = false); |
| 726 | LogicalResult validateOperationElementTypes(TosaOp op, |
| 727 | bool allowUnsigned = false); |
| 728 | LogicalResult validateOperationElementTypes(func::FuncOp op, |
| 729 | bool allowUnsigned = false); |
| 730 | |
| 731 | SmallVector< |
| 732 | std::function<LogicalResult(Operation *, const tosa::TargetEnv &)>> |
| 733 | constCheckers; |
| 734 | DenseMap<StringAttr, mlir::Type> variablesMap; |
| 735 | TosaProfileCompliance profileComp; |
| 736 | tosa::TargetEnv targetEnv; |
| 737 | }; |
| 738 | |
| 739 | template <> |
| 740 | LogicalResult TosaValidation::levelCheckRanks(tosa::ArgMaxOp tosaOp) { |
| 741 | auto *op = tosaOp.getOperation(); |
| 742 | if (failed(Result: levelCheckRank(op, v: tosaOp.getInput(), operandOrResult: "operand" , |
| 743 | highest_rank: targetEnv.getLevel().MAX_RANK))) |
| 744 | return failure(); |
| 745 | |
| 746 | // rank(output) = rank(input) - 1 |
| 747 | if (failed(Result: levelCheckRank(op, v: tosaOp.getOutput(), operandOrResult: "result" , |
| 748 | highest_rank: targetEnv.getLevel().MAX_RANK - 1))) |
| 749 | return failure(); |
| 750 | |
| 751 | return success(); |
| 752 | } |
| 753 | |
| 754 | template <> |
| 755 | LogicalResult TosaValidation::levelCheckRanks(tosa::ArgMinOp tosaOp) { |
| 756 | auto *op = tosaOp.getOperation(); |
| 757 | if (failed(Result: levelCheckRank(op, v: tosaOp.getInput(), operandOrResult: "operand" , |
| 758 | highest_rank: targetEnv.getLevel().MAX_RANK))) |
| 759 | return failure(); |
| 760 | |
| 761 | // rank(output) = rank(input) - 1 |
| 762 | if (failed(Result: levelCheckRank(op, v: tosaOp.getOutput(), operandOrResult: "result" , |
| 763 | highest_rank: targetEnv.getLevel().MAX_RANK - 1))) |
| 764 | return failure(); |
| 765 | |
| 766 | return success(); |
| 767 | } |
| 768 | |
| 769 | template <> |
| 770 | LogicalResult TosaValidation::levelCheckRanks(tosa::IfOp tosaOp) { |
| 771 | auto *op = tosaOp.getOperation(); |
| 772 | |
| 773 | // Only the condition input has rank limitation. |
| 774 | if (failed(Result: levelCheckRank(op, v: tosaOp.getCondition(), operandOrResult: "operand" , |
| 775 | highest_rank: targetEnv.getLevel().MAX_RANK))) |
| 776 | return failure(); |
| 777 | |
| 778 | return success(); |
| 779 | } |
| 780 | |
| 781 | template <> |
| 782 | LogicalResult TosaValidation::levelCheckRanks(tosa::VariableOp tosaOp) { |
| 783 | auto *op = tosaOp.getOperation(); |
| 784 | auto variableType = getVariableType(variableOp: tosaOp); |
| 785 | if (failed(Result: levelCheckRank(op, typeToCheck: variableType, operandOrResult: "variable type" , |
| 786 | highest_rank: targetEnv.getLevel().MAX_RANK))) |
| 787 | return failure(); |
| 788 | |
| 789 | return success(); |
| 790 | } |
| 791 | |
| 792 | template <> |
| 793 | LogicalResult TosaValidation::levelCheckSizes(tosa::VariableOp tosaOp) { |
| 794 | auto *op = tosaOp.getOperation(); |
| 795 | auto variableType = getVariableType(variableOp: tosaOp); |
| 796 | if (failed(Result: levelCheckSize(op, typeToCheck: variableType, operandOrResult: "variable type" ))) |
| 797 | return failure(); |
| 798 | |
| 799 | return success(); |
| 800 | } |
| 801 | |
| 802 | LogicalResult TosaValidation::levelCheckRanksAndSizes(Operation *op) { |
| 803 | #define CHECK_RANKS_AND_SIZES(tosaOp) \ |
| 804 | if (isa<tosa::tosaOp##Op>(op)) { \ |
| 805 | if (failed(levelCheckRanks(cast<tosa::tosaOp##Op>(op)))) \ |
| 806 | return failure(); \ |
| 807 | if (failed(levelCheckSizes(cast<tosa::tosaOp##Op>(op)))) \ |
| 808 | return failure(); \ |
| 809 | } |
| 810 | |
| 811 | #define CHECK_SIZES(tosaOp) \ |
| 812 | if (isa<tosa::tosaOp##Op>(op)) { \ |
| 813 | if (failed(levelCheckSizes(cast<tosa::tosaOp##Op>(op)))) \ |
| 814 | return failure(); \ |
| 815 | } |
| 816 | |
| 817 | #define CHECK_SHAPE_LEN(tosaOp) \ |
| 818 | if (isa<tosa::tosaOp##Op>(op)) { \ |
| 819 | if (failed(levelCheckShapeLengths(cast<tosa::tosaOp##Op>(op)))) \ |
| 820 | return failure(); \ |
| 821 | } |
| 822 | |
| 823 | // Tensor Operators |
| 824 | CHECK_RANKS_AND_SIZES(ArgMax); |
| 825 | CHECK_RANKS_AND_SIZES(ArgMin); |
| 826 | // Activation Functions |
| 827 | CHECK_RANKS_AND_SIZES(Clamp); |
| 828 | CHECK_RANKS_AND_SIZES(Erf); |
| 829 | CHECK_RANKS_AND_SIZES(Sigmoid); |
| 830 | CHECK_RANKS_AND_SIZES(Tanh); |
| 831 | // Elementwise Binary Operators |
| 832 | CHECK_RANKS_AND_SIZES(Add); |
| 833 | CHECK_RANKS_AND_SIZES(ArithmeticRightShift); |
| 834 | CHECK_RANKS_AND_SIZES(BitwiseAnd); |
| 835 | CHECK_RANKS_AND_SIZES(BitwiseOr); |
| 836 | CHECK_RANKS_AND_SIZES(BitwiseXor); |
| 837 | CHECK_RANKS_AND_SIZES(IntDiv); |
| 838 | CHECK_RANKS_AND_SIZES(LogicalAnd); |
| 839 | CHECK_RANKS_AND_SIZES(LogicalLeftShift); |
| 840 | CHECK_RANKS_AND_SIZES(LogicalRightShift); |
| 841 | CHECK_RANKS_AND_SIZES(LogicalOr); |
| 842 | CHECK_RANKS_AND_SIZES(LogicalXor); |
| 843 | CHECK_RANKS_AND_SIZES(Maximum); |
| 844 | CHECK_RANKS_AND_SIZES(Minimum); |
| 845 | CHECK_RANKS_AND_SIZES(Mul); |
| 846 | CHECK_RANKS_AND_SIZES(Pow); |
| 847 | CHECK_RANKS_AND_SIZES(Sub); |
| 848 | CHECK_RANKS_AND_SIZES(Table); |
| 849 | // Elementwise Unary Operators |
| 850 | CHECK_RANKS_AND_SIZES(Abs); |
| 851 | CHECK_RANKS_AND_SIZES(BitwiseNot); |
| 852 | CHECK_RANKS_AND_SIZES(Ceil); |
| 853 | CHECK_RANKS_AND_SIZES(Clz); |
| 854 | CHECK_RANKS_AND_SIZES(Cos); |
| 855 | CHECK_RANKS_AND_SIZES(Exp); |
| 856 | CHECK_RANKS_AND_SIZES(Floor); |
| 857 | CHECK_RANKS_AND_SIZES(Log); |
| 858 | CHECK_RANKS_AND_SIZES(LogicalNot); |
| 859 | CHECK_RANKS_AND_SIZES(Negate); |
| 860 | CHECK_RANKS_AND_SIZES(Reciprocal); |
| 861 | CHECK_RANKS_AND_SIZES(Rsqrt); |
| 862 | CHECK_RANKS_AND_SIZES(Sin); |
| 863 | // Elementwise Ternary Operators |
| 864 | CHECK_RANKS_AND_SIZES(Select); |
| 865 | // Comparison Operators |
| 866 | CHECK_RANKS_AND_SIZES(Equal); |
| 867 | CHECK_RANKS_AND_SIZES(Greater); |
| 868 | CHECK_RANKS_AND_SIZES(GreaterEqual); |
| 869 | // Reduction Operators |
| 870 | CHECK_RANKS_AND_SIZES(ReduceAll); |
| 871 | CHECK_RANKS_AND_SIZES(ReduceAny); |
| 872 | CHECK_RANKS_AND_SIZES(ReduceMax); |
| 873 | CHECK_RANKS_AND_SIZES(ReduceMin); |
| 874 | CHECK_RANKS_AND_SIZES(ReduceProduct); |
| 875 | CHECK_RANKS_AND_SIZES(ReduceSum); |
| 876 | // Data Layout Operators |
| 877 | CHECK_RANKS_AND_SIZES(Concat); |
| 878 | CHECK_RANKS_AND_SIZES(Pad); |
| 879 | CHECK_RANKS_AND_SIZES(Reshape); |
| 880 | CHECK_RANKS_AND_SIZES(ReshapeBlockScaled); |
| 881 | CHECK_RANKS_AND_SIZES(Reverse); |
| 882 | CHECK_RANKS_AND_SIZES(Slice); |
| 883 | CHECK_RANKS_AND_SIZES(Tile); |
| 884 | CHECK_RANKS_AND_SIZES(Transpose); |
| 885 | // Type Conversion |
| 886 | CHECK_RANKS_AND_SIZES(Cast); |
| 887 | CHECK_RANKS_AND_SIZES(CastFromBlockScaled); |
| 888 | CHECK_RANKS_AND_SIZES(CastToBlockScaled); |
| 889 | CHECK_RANKS_AND_SIZES(Rescale); |
| 890 | // Data Nodes |
| 891 | CHECK_RANKS_AND_SIZES(Const); |
| 892 | CHECK_RANKS_AND_SIZES(Identity); |
| 893 | // Control Flow Operators |
| 894 | CHECK_RANKS_AND_SIZES(If); |
| 895 | // Variable Operators |
| 896 | CHECK_RANKS_AND_SIZES(Variable); |
| 897 | CHECK_RANKS_AND_SIZES(VariableWrite); |
| 898 | CHECK_RANKS_AND_SIZES(VariableRead); |
| 899 | // Shape Operators |
| 900 | CHECK_RANKS_AND_SIZES(Dim); |
| 901 | |
| 902 | // For the following operators, check whether the size of each tensor |
| 903 | // operand is valid in a given Level. |
| 904 | |
| 905 | // Tensor Operators |
| 906 | CHECK_SIZES(AvgPool2d); |
| 907 | CHECK_SIZES(AvgPool2dAdaptive); |
| 908 | CHECK_SIZES(Conv2D); |
| 909 | CHECK_SIZES(Conv2DBlockScaled); |
| 910 | CHECK_SIZES(Conv3D); |
| 911 | CHECK_SIZES(DepthwiseConv2D); |
| 912 | CHECK_SIZES(TransposeConv2D); |
| 913 | CHECK_SIZES(FFT2d); |
| 914 | CHECK_RANKS_AND_SIZES(MatMul); |
| 915 | CHECK_RANKS_AND_SIZES(MatMulT); |
| 916 | CHECK_SIZES(MatmulTBlockScaled); |
| 917 | CHECK_SIZES(MaxPool2d); |
| 918 | CHECK_SIZES(MaxPool2dAdaptive); |
| 919 | CHECK_SIZES(RFFT2d); |
| 920 | // Scatter/Gather Operators |
| 921 | CHECK_SIZES(Gather); |
| 922 | CHECK_SIZES(RowGather); |
| 923 | CHECK_SIZES(Scatter); |
| 924 | // Image Operators |
| 925 | CHECK_SIZES(Resize); |
| 926 | // Custom Operators |
| 927 | CHECK_SIZES(Custom); |
| 928 | // Control Flow Operators |
| 929 | CHECK_SIZES(While); |
| 930 | // Shape Operators |
| 931 | CHECK_SIZES(ConstShape); |
| 932 | |
| 933 | // For the following operations, check whether the shape length of each |
| 934 | // operand is valid given a level. |
| 935 | |
| 936 | // Shape Operators |
| 937 | CHECK_SHAPE_LEN(AddShape); |
| 938 | CHECK_SHAPE_LEN(AssertEqualShape); |
| 939 | CHECK_SHAPE_LEN(ConcatShape); |
| 940 | CHECK_SHAPE_LEN(DivCeilShape); |
| 941 | CHECK_SHAPE_LEN(DivFloorShape); |
| 942 | CHECK_SHAPE_LEN(Exp2Shape); |
| 943 | CHECK_SHAPE_LEN(Log2CeilShape); |
| 944 | CHECK_SHAPE_LEN(Log2FloorShape); |
| 945 | CHECK_SHAPE_LEN(MaxShape); |
| 946 | CHECK_SHAPE_LEN(MinShape); |
| 947 | CHECK_SHAPE_LEN(ModShape); |
| 948 | CHECK_SHAPE_LEN(MulShape); |
| 949 | CHECK_SHAPE_LEN(SliceShape); |
| 950 | CHECK_SHAPE_LEN(SubShape); |
| 951 | |
| 952 | #undef CHECK_RANKS_AND_SIZES |
| 953 | #undef CHECK_SIZES |
| 954 | #undef CHECK_SHAPE_LEN |
| 955 | return success(); |
| 956 | } |
| 957 | |
| 958 | // Perform the Level tensor size check on the tensor type. |
| 959 | LogicalResult TosaValidation::levelCheckSize(Operation *op, |
| 960 | const Type &typeToCheck, |
| 961 | const StringRef operandOrResult) { |
| 962 | if (ShapedType type = dyn_cast<ShapedType>(Val: typeToCheck)) { |
| 963 | if (!type.hasRank()) |
| 964 | return op->emitOpError() << "failed level check: unranked tensor" ; |
| 965 | auto shape = type.getShape(); |
| 966 | for (auto dim : shape) { |
| 967 | const bool dimIsDynamic = mlir::ShapedType::isDynamic(dValue: dim); |
| 968 | const TosaSpecificationVersion targetVersion = targetEnv.getSpecVersion(); |
| 969 | const TosaSpecificationVersion minRequiredVersion(1, 1, true); |
| 970 | if (targetVersion.isBackwardsCompatibleWith(baseVersion: minRequiredVersion) && |
| 971 | dimIsDynamic) |
| 972 | // TOSA 1.1 and above supports dynamic dimensions, however, they must be |
| 973 | // resolved at backend compile time. Runtime dynamism is not currently |
| 974 | // supported. Checking this requirement is met is delegated to backends. |
| 975 | return success(); |
| 976 | |
| 977 | // When targeting TOSA 1.0 or below, dynamic dims are not supported |
| 978 | if (dimIsDynamic) |
| 979 | return op->emitOpError() << "failed level check: " << operandOrResult |
| 980 | << " shape dimension cannot be dynamic when" |
| 981 | << " targeting TOSA specification version 1.0" |
| 982 | << " or below" ; |
| 983 | } |
| 984 | |
| 985 | int64_t elementBits = tosa::getBitWidth(type: getElementTypeOrSelf(type)); |
| 986 | int64_t elementBytes = std::max(INT64_C(1), b: elementBits / 8); |
| 987 | int64_t size = elementBytes * type.getNumElements(); |
| 988 | |
| 989 | // According to 1.11. Tensor Definitions of Tosa spec, the value of |
| 990 | // tensor_size_t is 1 << MAX_LOG2_SIZE) - 1 where MAX_LOG2_SIZE is |
| 991 | // defined in 1.7. Levels. |
| 992 | // For each tensor, the number of tensor elements multiplied by the |
| 993 | // element size in bytes must be representable as a tensor_size_t. |
| 994 | const int64_t maxSize = |
| 995 | (INT64_C(1) << targetEnv.getLevel().MAX_LOG2_SIZE) - 1; |
| 996 | if (size > maxSize) |
| 997 | return op->emitOpError() |
| 998 | << "failed level check: " << operandOrResult |
| 999 | << " tensor size (in bytes) <= (1 << MAX_LOG2_SIZE - 1)" ; |
| 1000 | } |
| 1001 | return success(); |
| 1002 | } |
| 1003 | |
| 1004 | LogicalResult TosaValidation::applyLevelCheck(Operation *op) { |
| 1005 | if (targetEnv.getLevel() == TOSA_LEVEL_NONE) { |
| 1006 | // no need to do level checks |
| 1007 | return success(); |
| 1008 | } |
| 1009 | |
| 1010 | // check rank and sizes early so later checks can assume shaped operands |
| 1011 | if (failed(Result: levelCheckRanksAndSizes(op))) |
| 1012 | return failure(); |
| 1013 | |
| 1014 | if (failed(Result: levelCheckPool<tosa::AvgPool2dOp>(op)) || |
| 1015 | failed(Result: levelCheckAdaptivePool<tosa::AvgPool2dAdaptiveOp>(op)) || |
| 1016 | failed(Result: levelCheckConv<tosa::Conv2DOp>(op)) || |
| 1017 | failed(Result: levelCheckConv<tosa::Conv3DOp>(op)) || |
| 1018 | failed(Result: levelCheckConv<tosa::DepthwiseConv2DOp>(op)) || |
| 1019 | failed(Result: levelCheckFFT<tosa::FFT2dOp>(op)) || |
| 1020 | failed(Result: levelCheckPool<tosa::MaxPool2dOp>(op)) || |
| 1021 | failed(Result: levelCheckAdaptivePool<tosa::MaxPool2dAdaptiveOp>(op)) || |
| 1022 | failed(Result: levelCheckFFT<tosa::RFFT2dOp>(op)) || |
| 1023 | failed(Result: levelCheckTransposeConv2d(op)) || failed(Result: levelCheckResize(op)) || |
| 1024 | failed(Result: levelCheckConv2DBlockScaled(op))) { |
| 1025 | return failure(); |
| 1026 | } |
| 1027 | |
| 1028 | // level check MAX_TENSOR_LIST_SIZE |
| 1029 | if (failed(Result: levelCheckListSize(op))) { |
| 1030 | return failure(); |
| 1031 | } |
| 1032 | |
| 1033 | if (isa<tosa::IfOp>(Val: op) || isa<tosa::WhileOp>(Val: op)) { |
| 1034 | if (failed(Result: levelCheckMaxNesting(op))) { |
| 1035 | return failure(); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | return success(); |
| 1040 | } |
| 1041 | |
| 1042 | LogicalResult TosaValidation::applyAttributeCheck(Operation *op) { |
| 1043 | if (failed(Result: attributeCheckRescale(op))) |
| 1044 | return failure(); |
| 1045 | if (failed(Result: attributeCheckCast(op))) |
| 1046 | return failure(); |
| 1047 | return success(); |
| 1048 | } |
| 1049 | |
| 1050 | inline bool CompatibleTypes(const mlir::Type &type, |
| 1051 | const mlir::Type &declaredType) { |
| 1052 | // for now, simply use type equality comparison |
| 1053 | return type == declaredType; |
| 1054 | } |
| 1055 | |
| 1056 | LogicalResult TosaValidation::CheckVariable(Operation *op) { |
| 1057 | if (auto variableOp = dyn_cast<mlir::tosa::VariableOp>(Val: op)) { |
| 1058 | mlir::StringAttr nameAttr = variableOp.getNameAttr(); |
| 1059 | |
| 1060 | if (variablesMap.count(Val: nameAttr)) |
| 1061 | return op->emitOpError() << "name has already been declared" ; |
| 1062 | |
| 1063 | auto elementType = variableOp.getType(); |
| 1064 | DenseIntElementsAttr varShapeAttr = variableOp.getVarShape(); |
| 1065 | SmallVector<int64_t> shape = to_vector(Range: varShapeAttr.getValues<int64_t>()); |
| 1066 | RankedTensorType variableType = |
| 1067 | RankedTensorType::get(shape: ArrayRef<int64_t>(shape), elementType); |
| 1068 | |
| 1069 | variablesMap[nameAttr] = variableType; |
| 1070 | } |
| 1071 | |
| 1072 | return success(); |
| 1073 | } |
| 1074 | |
| 1075 | LogicalResult TosaValidation::CheckVariableReadOrWrite(Operation *op) { |
| 1076 | if (isa<mlir::tosa::VariableReadOp>(Val: op) || |
| 1077 | isa<mlir::tosa::VariableWriteOp>(Val: op)) { |
| 1078 | mlir::StringAttr nameAttr = |
| 1079 | TypeSwitch<Operation *, mlir::StringAttr>(op) |
| 1080 | .Case<mlir::tosa::VariableReadOp, mlir::tosa::VariableWriteOp>( |
| 1081 | caseFn: [](auto variableOp) { return variableOp.getNameAttr(); }); |
| 1082 | if (!variablesMap.count(Val: nameAttr)) |
| 1083 | return op->emitOpError() << "name has not been declared" ; |
| 1084 | |
| 1085 | auto varType = variablesMap[nameAttr]; |
| 1086 | |
| 1087 | for (auto v : op->getOperands()) { |
| 1088 | auto type = v.getType(); |
| 1089 | if (!CompatibleTypes(type, declaredType: varType)) |
| 1090 | return op->emitOpError() << "operand type does not equal variable type" ; |
| 1091 | } |
| 1092 | |
| 1093 | for (auto v : op->getResults()) { |
| 1094 | auto type = v.getType(); |
| 1095 | if (!CompatibleTypes(type, declaredType: varType)) |
| 1096 | return op->emitOpError() << "result type does not equal variable type" ; |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | return success(); |
| 1101 | } |
| 1102 | |
| 1103 | LogicalResult TosaValidation::applyVariableCheck(Operation *op) { |
| 1104 | if (failed(Result: CheckVariable(op)) || failed(Result: CheckVariableReadOrWrite(op))) |
| 1105 | return failure(); |
| 1106 | return success(); |
| 1107 | } |
| 1108 | |
| 1109 | LogicalResult checkErrorIfResize(Operation *op, |
| 1110 | bool checkStrictSpecConformance) { |
| 1111 | auto resize = dyn_cast<tosa::ResizeOp>(Val: op); |
| 1112 | if (!resize) |
| 1113 | return success(); |
| 1114 | |
| 1115 | const Value input = resize.getInput(); |
| 1116 | const Value output = resize.getOutput(); |
| 1117 | const RankedTensorType inputType = |
| 1118 | llvm::dyn_cast<RankedTensorType>(Val: input.getType()); |
| 1119 | const RankedTensorType outputType = |
| 1120 | llvm::dyn_cast<RankedTensorType>(Val: output.getType()); |
| 1121 | |
| 1122 | if (!inputType || !outputType) |
| 1123 | return op->emitOpError(message: "expect ranked input/output tensor" ); |
| 1124 | |
| 1125 | // Ensure the image size is supported by GPU APIs and that for integer |
| 1126 | // implementations, position * stride does not overflow int32_t. |
| 1127 | if (checkStrictSpecConformance && inputType.hasStaticShape() && |
| 1128 | outputType.hasStaticShape()) { |
| 1129 | const SmallVector<int64_t, 4> sizes = { |
| 1130 | outputType.getDimSize(idx: 1), outputType.getDimSize(idx: 2), |
| 1131 | inputType.getDimSize(idx: 1), inputType.getDimSize(idx: 2)}; |
| 1132 | const int64_t *maxDim = llvm::max_element(Range: sizes); |
| 1133 | if (maxDim != sizes.end() && *maxDim >= 16384) |
| 1134 | return op->emitOpError( |
| 1135 | message: "expect input/output height/width dims to be < 16384, " ) |
| 1136 | << "got [OH, OW, IH, IW] = " << sizes; |
| 1137 | } |
| 1138 | |
| 1139 | SmallVector<int64_t> scale; |
| 1140 | if (!tosa::getConstShapeValues(op: resize.getScale().getDefiningOp(), result_shape&: scale)) |
| 1141 | return failure(); |
| 1142 | |
| 1143 | // ERROR_IF(scale <= 0) |
| 1144 | if (llvm::any_of(Range&: scale, P: [](int64_t s) { return s <= 0; })) |
| 1145 | return op->emitOpError(message: "expect all scale values to be > 0, got " ) << scale; |
| 1146 | |
| 1147 | const int64_t scaleYN = scale[0]; |
| 1148 | const int64_t scaleYD = scale[1]; |
| 1149 | const int64_t scaleXN = scale[2]; |
| 1150 | const int64_t scaleXD = scale[3]; |
| 1151 | |
| 1152 | // Ensure scale values don't overflow int32 accumulator |
| 1153 | if (scaleYN > (1 << 11) || scaleXN > (1 << 11)) |
| 1154 | return op->emitOpError( |
| 1155 | message: "expect all scale numerator values to be <= (1 << 11), " |
| 1156 | "got scale_y_n=" ) |
| 1157 | << scaleYN << ", scale_x_n=" << scaleXN; |
| 1158 | |
| 1159 | if (scaleYD >= 16 * scaleYN || scaleXD >= 16 * scaleXN) |
| 1160 | return op->emitOpError(message: "expect a downscale ratio larger than 1/16, got y=" ) |
| 1161 | << scaleYN << "/" << scaleYD << ", x=" << scaleXN << "/" << scaleXD; |
| 1162 | |
| 1163 | SmallVector<int64_t> offset; |
| 1164 | SmallVector<int64_t> border; |
| 1165 | if (!tosa::getConstShapeValues(op: resize.getOffset().getDefiningOp(), result_shape&: offset) || |
| 1166 | !tosa::getConstShapeValues(op: resize.getBorder().getDefiningOp(), result_shape&: border)) |
| 1167 | return failure(); |
| 1168 | |
| 1169 | const int64_t offsetY = offset[0]; |
| 1170 | const int64_t offsetX = offset[1]; |
| 1171 | const int64_t borderY = border[0]; |
| 1172 | const int64_t borderX = border[1]; |
| 1173 | |
| 1174 | // Set a consistent lower limit of 1/16 downscale to simplify |
| 1175 | // implementations |
| 1176 | if (checkStrictSpecConformance) { |
| 1177 | if (offsetY < -scaleYN || offsetY >= 16 * scaleYN) |
| 1178 | return op->emitOpError(message: "expect offsetY / scaleYNumerator to be in range " |
| 1179 | "[-1, 16), got " ) |
| 1180 | << offsetY << "/" << scaleYN; |
| 1181 | if (offsetX < -scaleXN || offsetX >= 16 * scaleXN) |
| 1182 | return op->emitOpError(message: "expect offsetX / scaleXNumerator to be in range " |
| 1183 | "[-1, 16), got " ) |
| 1184 | << offsetX << "/" << scaleXN; |
| 1185 | |
| 1186 | if (borderY < -16 * scaleYN || borderY >= scaleYN) |
| 1187 | return op->emitOpError(message: "expect borderY / scaleYNumerator to be in range " |
| 1188 | "[-16, 1), got " ) |
| 1189 | << borderY << "/" << scaleYN; |
| 1190 | if (borderX < -16 * scaleXN || borderX >= scaleXN) |
| 1191 | return op->emitOpError(message: "expect borderX / scaleXNumerator to be in range " |
| 1192 | "[-16, 1), got " ) |
| 1193 | << borderX << "/" << scaleXN; |
| 1194 | } |
| 1195 | |
| 1196 | // In TOSA specification, we do not support broadcast behavior. Here is a |
| 1197 | // strict check to conform to the specification. |
| 1198 | auto idivCheck = [](const int64_t lhs, |
| 1199 | const int64_t rhs) -> std::optional<int64_t> { |
| 1200 | if (lhs % rhs != 0) |
| 1201 | return std::nullopt; |
| 1202 | return lhs / rhs; |
| 1203 | }; |
| 1204 | |
| 1205 | const int64_t oh = outputType.getDimSize(idx: 1); |
| 1206 | const int64_t ow = outputType.getDimSize(idx: 2); |
| 1207 | const int64_t ih = inputType.getDimSize(idx: 1); |
| 1208 | const int64_t iw = inputType.getDimSize(idx: 2); |
| 1209 | |
| 1210 | if (ih != ShapedType::kDynamic) { |
| 1211 | const std::optional<int64_t> calculatedOutHeightMinusOne = |
| 1212 | idivCheck((ih - 1) * scaleYN - offsetY + borderY, scaleYD); |
| 1213 | if (!calculatedOutHeightMinusOne.has_value()) |
| 1214 | return op->emitOpError( |
| 1215 | message: "expected (input_height - 1) * scale_y_n - offset_y + " |
| 1216 | "border_y " ) |
| 1217 | << "to be wholly divisible by scale_y_d, got ((" << ih |
| 1218 | << " - 1) * " << scaleYN << " - " << offsetY << " + " << borderY |
| 1219 | << ") / " << scaleYD; |
| 1220 | const int64_t calculatedOutHeight = calculatedOutHeightMinusOne.value() + 1; |
| 1221 | if (oh != ShapedType::kDynamic && calculatedOutHeight != oh) |
| 1222 | return op->emitOpError( |
| 1223 | message: "calculated output height did not match expected: " ) |
| 1224 | << "calculated=" << calculatedOutHeight << ", expected=" << oh; |
| 1225 | } |
| 1226 | |
| 1227 | if (iw != ShapedType::kDynamic) { |
| 1228 | const std::optional<int64_t> calculatedOutWidthMinusOne = |
| 1229 | idivCheck((iw - 1) * scaleXN - offsetX + borderX, scaleXD); |
| 1230 | if (!calculatedOutWidthMinusOne.has_value()) |
| 1231 | return op->emitOpError( |
| 1232 | message: "expected (input_width - 1) * scale_x_n - offset_x + " |
| 1233 | "border_x " ) |
| 1234 | << "to be wholly divisible by scale_x_d, got ((" << iw |
| 1235 | << " - 1) * " << scaleXN << " - " << offsetX << " + " << borderX |
| 1236 | << ") / " << scaleXD; |
| 1237 | const int64_t calculatedOutWidth = calculatedOutWidthMinusOne.value() + 1; |
| 1238 | if (ow != ShapedType::kDynamic && calculatedOutWidth != ow) |
| 1239 | return op->emitOpError(message: "calculated output width did not match expected: " ) |
| 1240 | << "calculated=" << calculatedOutWidth << ", expected=" << ow; |
| 1241 | } |
| 1242 | |
| 1243 | return success(); |
| 1244 | } |
| 1245 | |
| 1246 | LogicalResult checkErrorIfMul(Operation *op) { |
| 1247 | auto mul = dyn_cast<tosa::MulOp>(Val: op); |
| 1248 | if (!mul) |
| 1249 | return success(); |
| 1250 | |
| 1251 | // REQUIRE(0 <= shift && shift <= 63); |
| 1252 | // REQUIRE(is_same<in_t,int32_t>() || shift == 0); |
| 1253 | ElementsAttr shift_elem; |
| 1254 | if (!matchPattern(value: mul.getShift(), pattern: m_Constant(bind_value: &shift_elem))) |
| 1255 | return success(); |
| 1256 | int32_t shift = shift_elem.getValues<IntegerAttr>()[0].getInt(); |
| 1257 | auto inputElemType = getElementTypeOrSelf(val: mul.getInput1()); |
| 1258 | if (inputElemType.isInteger(width: 32)) { |
| 1259 | // 0 <= shift <= 63 for int32_t type |
| 1260 | if (shift < 0 || shift > 63) |
| 1261 | return op->emitOpError() |
| 1262 | << "requires 0 <= shift && shift <= 63, but got: " << shift; |
| 1263 | } else { |
| 1264 | // shift must be 0 for all other types |
| 1265 | if (shift != 0) |
| 1266 | return op->emitOpError() |
| 1267 | << "requires shift = 0 for all input data types that " |
| 1268 | "are not int32_t, but got: " |
| 1269 | << shift; |
| 1270 | } |
| 1271 | |
| 1272 | return success(); |
| 1273 | } |
| 1274 | |
| 1275 | LogicalResult checkErrorIfTable(Operation *op) { |
| 1276 | auto table = dyn_cast<tosa::TableOp>(Val: op); |
| 1277 | if (!table) |
| 1278 | return success(); |
| 1279 | |
| 1280 | // REQUIRE(length(table) == TABLE_SIZE) where TABLE_SIZE is 256 or 513 |
| 1281 | const auto inputElemType = getElementTypeOrSelf(type: table.getInput1().getType()); |
| 1282 | const int tableSize = inputElemType.isInteger(width: 8) ? 256 : 513; |
| 1283 | |
| 1284 | const ShapeAdaptor tableShape(table.getTable().getType()); |
| 1285 | if (tableShape.hasStaticShape()) { |
| 1286 | const auto numElements = tableShape.getNumElements(); |
| 1287 | if (numElements != tableSize) |
| 1288 | return op->emitOpError() << "requires table size of " << tableSize |
| 1289 | << ", got " << numElements; |
| 1290 | } |
| 1291 | |
| 1292 | return success(); |
| 1293 | } |
| 1294 | |
| 1295 | LogicalResult checkErrorIfRescale(Operation *op) { |
| 1296 | auto rescale = dyn_cast<tosa::RescaleOp>(Val: op); |
| 1297 | if (!rescale) |
| 1298 | return success(); |
| 1299 | |
| 1300 | auto inputType = llvm::dyn_cast<ShapedType>(Val: rescale.getInput().getType()); |
| 1301 | auto outputType = llvm::dyn_cast<ShapedType>(Val: rescale.getOutput().getType()); |
| 1302 | if (!inputType || !outputType || !inputType.getElementType().isInteger() || |
| 1303 | !outputType.getElementType().isInteger()) |
| 1304 | return success(); |
| 1305 | |
| 1306 | auto inElemType = inputType.getElementType(); |
| 1307 | auto outElemType = outputType.getElementType(); |
| 1308 | auto inWidth = inElemType.getIntOrFloatBitWidth(); |
| 1309 | auto outWidth = outElemType.getIntOrFloatBitWidth(); |
| 1310 | |
| 1311 | bool inputUnsigned = rescale.getInputUnsigned(); |
| 1312 | bool outputUnsigned = rescale.getOutputUnsigned(); |
| 1313 | |
| 1314 | bool scale32 = rescale.getScale32(); |
| 1315 | auto roundingMode = rescale.getRoundingMode(); |
| 1316 | |
| 1317 | // ERROR_IF(scale32 && is_same<in_t,i48_t>()) |
| 1318 | if (scale32 && inWidth == 48) |
| 1319 | return op->emitOpError() << "scale32 is not allowed with 48-bit input." ; |
| 1320 | |
| 1321 | // ERROR_IF(!scale32 && (rounding_mode == DOUBLE_ROUND)) |
| 1322 | if (!scale32 && roundingMode == RoundingMode::DOUBLE_ROUND) |
| 1323 | return op->emitOpError() |
| 1324 | << "DOUBLE_ROUND is only allowed with scale32=true." ; |
| 1325 | |
| 1326 | // ERROR_IF(input_unsigned && output_unsigned) |
| 1327 | if (inputUnsigned && outputUnsigned) |
| 1328 | return op->emitOpError() << "input and output cannot be both unsigned." ; |
| 1329 | |
| 1330 | // ERROR_IF(is_same<out_t,i32_t>() && input_unsigned) |
| 1331 | if (outWidth == 32 && inputUnsigned) |
| 1332 | return op->emitOpError() |
| 1333 | << "i32 output type is not allowed with unsigned input." ; |
| 1334 | |
| 1335 | // ERROR_IF(is_same<in_t,i32_t>() && output_unsigned) |
| 1336 | if (inWidth == 32 && outputUnsigned) |
| 1337 | return op->emitOpError() |
| 1338 | << "i32 input type is not allowed with unsigned output." ; |
| 1339 | |
| 1340 | // ERROR_IF(is_same<in_t,i48_t>() && output_unsigned) |
| 1341 | if (inWidth == 48 && outputUnsigned) |
| 1342 | return op->emitOpError() |
| 1343 | << "i48 input type is not allowed with unsigned output." ; |
| 1344 | |
| 1345 | // ERROR_IF(is_same<in_t, i48_t> && input_unsigned) |
| 1346 | if (inWidth == 48 && inputUnsigned) |
| 1347 | return op->emitOpError() << "i48 input type cannot be unsigned." ; |
| 1348 | |
| 1349 | // ERROR_IF(is_same<in_t, i32_t> && input_unsigned) |
| 1350 | if (inWidth == 32 && inputUnsigned) |
| 1351 | return op->emitOpError() << "i32 input type cannot be unsigned." ; |
| 1352 | |
| 1353 | // ERROR_IF(is_same<out_t, i32_t> && output_unsigned) |
| 1354 | if (outWidth == 32 && outputUnsigned) |
| 1355 | return op->emitOpError() << "i32 output type cannot be unsigned." ; |
| 1356 | |
| 1357 | // ERROR_IF(zp != 0 && !is_same<in_t, i8_t>) |
| 1358 | // ERROR_IF(zp != 0 && zp != 32768 && is_same<in_t, u16_t>) |
| 1359 | if (FailureOr<int64_t> maybeIZp = rescale.getInputZeroPoint(); |
| 1360 | succeeded(Result: maybeIZp) && rescale.verifyInputZeroPoint(zp: *maybeIZp).failed()) |
| 1361 | return failure(); |
| 1362 | |
| 1363 | if (FailureOr<int64_t> maybeOZp = rescale.getOutputZeroPoint(); |
| 1364 | succeeded(Result: maybeOZp) && rescale.verifyOutputZeroPoint(zp: *maybeOZp).failed()) |
| 1365 | return failure(); |
| 1366 | |
| 1367 | return success(); |
| 1368 | } |
| 1369 | |
| 1370 | LogicalResult checkErrorIfPad(Operation *op) { |
| 1371 | auto pad = dyn_cast<tosa::PadOp>(Val: op); |
| 1372 | if (!pad) |
| 1373 | return success(); |
| 1374 | |
| 1375 | DenseIntElementsAttr paddingAttr; |
| 1376 | if (!matchPattern(value: pad.getPadding(), pattern: m_Constant(bind_value: &paddingAttr))) |
| 1377 | // Pad verifier will catch this |
| 1378 | return success(); |
| 1379 | |
| 1380 | for (const APInt &val : paddingAttr.getValues<APInt>()) { |
| 1381 | if (val.getSExtValue() < 0) |
| 1382 | return op->emitOpError() << "padding value must all be non-negative, got " |
| 1383 | << val.getSExtValue(); |
| 1384 | } |
| 1385 | |
| 1386 | return success(); |
| 1387 | } |
| 1388 | |
| 1389 | LogicalResult checkErrorIfTile(Operation *op) { |
| 1390 | auto tileOp = dyn_cast<tosa::TileOp>(Val: op); |
| 1391 | if (!tileOp) |
| 1392 | return success(); |
| 1393 | |
| 1394 | SmallVector<int64_t> multiples; |
| 1395 | if (failed(Result: tileOp.getConstantMultiples(multiples))) |
| 1396 | return success(); |
| 1397 | |
| 1398 | if (llvm::any_of(Range&: multiples, P: [](int64_t v) { return v <= 0; })) |
| 1399 | return op->emitOpError(message: "multiples values must be > 0, got [" ) |
| 1400 | << multiples << "]" ; |
| 1401 | |
| 1402 | return success(); |
| 1403 | } |
| 1404 | |
| 1405 | LogicalResult checkErrorIfReshape(Operation *op) { |
| 1406 | auto reshapeOp = dyn_cast<tosa::ReshapeOp>(Val: op); |
| 1407 | if (!reshapeOp) |
| 1408 | return success(); |
| 1409 | |
| 1410 | SmallVector<int64_t> shapeValues; |
| 1411 | if (!tosa::getConstShapeValues(op: reshapeOp.getShape().getDefiningOp(), |
| 1412 | result_shape&: shapeValues)) |
| 1413 | return success(); |
| 1414 | |
| 1415 | // ERROR_IF(any(shape < 1)): shape dimensions must be positive. This also |
| 1416 | // rejects the legacy inferable dimension (-1) and zero-sized dimensions, and |
| 1417 | // makes the `ShapedType::kDynamic` sentinel (INT64_MIN) invalid. Dynamic |
| 1418 | // dimensions must be expressed with `tosa.dim`, not -1. |
| 1419 | for (int64_t dim : shapeValues) |
| 1420 | if (dim < 1) |
| 1421 | return op->emitOpError() |
| 1422 | << "new shape has invalid tensor dimension size " << dim; |
| 1423 | |
| 1424 | auto outputType = dyn_cast<RankedTensorType>(Val: reshapeOp.getType()); |
| 1425 | |
| 1426 | // ERROR_IF(shape disagrees with the statically known result shape) |
| 1427 | if (outputType && |
| 1428 | outputType.getRank() == static_cast<int64_t>(shapeValues.size())) { |
| 1429 | for (auto [newShapeDim, outputShapeDim] : |
| 1430 | llvm::zip_equal(t&: shapeValues, u: outputType.getShape())) { |
| 1431 | if (ShapedType::isDynamic(dValue: outputShapeDim)) |
| 1432 | continue; |
| 1433 | if (newShapeDim != outputShapeDim) |
| 1434 | return op->emitOpError() |
| 1435 | << "new shape is inconsistent with result shape" ; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | // ERROR_IF(the number of elements is not preserved). Every dimension is now |
| 1440 | // known to be positive, so the product is exact. |
| 1441 | if (auto inputType = |
| 1442 | dyn_cast<RankedTensorType>(Val: reshapeOp.getInput1().getType()); |
| 1443 | inputType && inputType.hasStaticShape()) { |
| 1444 | int64_t inputElementsNum = inputType.getNumElements(); |
| 1445 | if (outputType && outputType.hasStaticShape()) { |
| 1446 | int64_t outputElementsNum = outputType.getNumElements(); |
| 1447 | if (inputElementsNum != outputElementsNum) |
| 1448 | return op->emitOpError() << "cannot reshape " << inputElementsNum |
| 1449 | << " elements into " << outputElementsNum; |
| 1450 | } |
| 1451 | |
| 1452 | int64_t newShapeElementsNum = |
| 1453 | llvm::accumulate(Range&: shapeValues, Init: int64_t(1), |
| 1454 | Op: [](int64_t acc, int64_t dim) { return acc * dim; }); |
| 1455 | if (inputElementsNum != newShapeElementsNum) |
| 1456 | return op->emitOpError() << "cannot reshape " << inputElementsNum |
| 1457 | << " elements into " << newShapeElementsNum; |
| 1458 | } |
| 1459 | |
| 1460 | return success(); |
| 1461 | } |
| 1462 | |
| 1463 | LogicalResult checkErrorIfSlice(Operation *op) { |
| 1464 | auto sliceOp = dyn_cast<tosa::SliceOp>(Val: op); |
| 1465 | if (!sliceOp) |
| 1466 | return success(); |
| 1467 | |
| 1468 | SmallVector<int64_t> startValues; |
| 1469 | SmallVector<int64_t> sizeValues; |
| 1470 | const bool hasStartValues = tosa::getConstShapeValues( |
| 1471 | op: sliceOp.getStart().getDefiningOp(), result_shape&: startValues); |
| 1472 | const bool hasSizeValues = |
| 1473 | tosa::getConstShapeValues(op: sliceOp.getSize().getDefiningOp(), result_shape&: sizeValues); |
| 1474 | |
| 1475 | if (hasStartValues && llvm::is_contained(Range&: startValues, Element: kInferableDimSize)) |
| 1476 | return op->emitOpError(message: "start input contains inferable dimension (" ) |
| 1477 | << kInferableDimSize |
| 1478 | << ") which does not conform to the TOSA specification" ; |
| 1479 | if (hasSizeValues && llvm::is_contained(Range&: sizeValues, Element: kInferableDimSize)) |
| 1480 | return op->emitOpError(message: "size input contains inferable dimension (" ) |
| 1481 | << kInferableDimSize |
| 1482 | << ") which " |
| 1483 | "does not conform to the TOSA specification" ; |
| 1484 | |
| 1485 | // ERROR_IF(any(start < 0)) |
| 1486 | if (hasStartValues && |
| 1487 | llvm::any_of(Range&: startValues, P: [](int64_t v) { return v < 0; })) |
| 1488 | return op->emitOpError(message: "start values must be non-negative, got [" ) |
| 1489 | << startValues << "]" ; |
| 1490 | |
| 1491 | // ERROR_IF(any(size <= 0)) |
| 1492 | if (hasSizeValues && |
| 1493 | llvm::any_of(Range&: sizeValues, P: [](int64_t v) { return v <= 0; })) |
| 1494 | return op->emitOpError(message: "size values must be > 0, got [" ) |
| 1495 | << sizeValues << "]" ; |
| 1496 | |
| 1497 | // MLIR-specific: output shape must match the size values. |
| 1498 | if (hasSizeValues) { |
| 1499 | const auto outputType = sliceOp.getOutput().getType(); |
| 1500 | if (const auto rankedOutput = dyn_cast<RankedTensorType>(Val: outputType)) { |
| 1501 | if (failed(Result: verifyCompatibleShape(shape1: rankedOutput.getShape(), shape2: sizeValues))) |
| 1502 | return op->emitOpError( |
| 1503 | message: "expected output shape to match size values, got " ) |
| 1504 | << outputType << " vs [" << sizeValues << "]" ; |
| 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | // ERROR_IF(any(start + size > shape1)) |
| 1509 | if (hasStartValues && hasSizeValues) { |
| 1510 | if (const auto inputType = |
| 1511 | dyn_cast<RankedTensorType>(Val: sliceOp.getInput1().getType())) { |
| 1512 | for (const auto &[index, vals] : llvm::enumerate(First: llvm::zip_equal( |
| 1513 | t&: startValues, u&: sizeValues, args: inputType.getShape()))) { |
| 1514 | const auto &[start, size, inputDim] = vals; |
| 1515 | if (ShapedType::isDynamic(dValue: inputDim)) |
| 1516 | continue; |
| 1517 | // Earlier checks already ensured that start and slice are non-negative. |
| 1518 | // Avoid overflowing start + size. |
| 1519 | if (start > inputDim || size > inputDim - start) |
| 1520 | return op->emitOpError( |
| 1521 | message: "start + size must be less than or equal to input " |
| 1522 | "dimension size, got start=" ) |
| 1523 | << start << ", size=" << size |
| 1524 | << " vs input dim size=" << inputDim << " at dimension " |
| 1525 | << index; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | // ERROR_IF(is_block_scale<in_out_t>() && start[rank(shape1) - 1] % |
| 1531 | // get_innermost_block_size<in_out_t>() != 0) |
| 1532 | if (hasStartValues && !startValues.empty()) { |
| 1533 | auto elemType = getElementTypeOrSelf(type: sliceOp.getInput1().getType()); |
| 1534 | if (auto blockScaledType = dyn_cast<BlockScaledType>(Val&: elemType)) { |
| 1535 | const int64_t startBlock = startValues.back(); |
| 1536 | const int64_t scaleBlock = |
| 1537 | BlockShapeAttr::getBlockShapeValue(blockShape: blockScaledType.getBlockShape()); |
| 1538 | if (startBlock % scaleBlock != 0) |
| 1539 | return op->emitOpError( |
| 1540 | message: "expected start innermost block size to match data type " |
| 1541 | "for block scaled input, got start block=" ) |
| 1542 | << startBlock << ", scale block=" << scaleBlock; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | return success(); |
| 1547 | } |
| 1548 | |
| 1549 | static bool isOpIsolatedWithinRegion(Operation *op, Region *region) { |
| 1550 | return llvm::all_of(Range: op->getOperands(), P: [&](auto operand) { |
| 1551 | Region *operandRegion = operand.getParentRegion(); |
| 1552 | return operandRegion && region->isAncestor(other: operandRegion); |
| 1553 | }); |
| 1554 | } |
| 1555 | |
| 1556 | static LogicalResult isRegionIsolatedFromAbove(Region ®ionToCheck) { |
| 1557 | bool noLiveInValue = true; |
| 1558 | regionToCheck.walk(callback: [&noLiveInValue, ®ionToCheck](Operation *op) { |
| 1559 | if (!isOpIsolatedWithinRegion(op, region: ®ionToCheck)) { |
| 1560 | noLiveInValue = false; |
| 1561 | return WalkResult::interrupt(); |
| 1562 | } |
| 1563 | return WalkResult::advance(); |
| 1564 | }); |
| 1565 | return noLiveInValue ? success() : failure(); |
| 1566 | } |
| 1567 | |
| 1568 | LogicalResult checkIsolatedRegion(Operation *op, Region ®ionToCheck, |
| 1569 | StringRef regionName) { |
| 1570 | if (succeeded(Result: isRegionIsolatedFromAbove(regionToCheck))) |
| 1571 | return success(); |
| 1572 | return op->emitOpError() |
| 1573 | << "is not conformant to the TOSA specification. It requires the '" |
| 1574 | << regionName << "' region is isolated from above.\n" ; |
| 1575 | } |
| 1576 | |
| 1577 | LogicalResult checkErrorIfCondIf(Operation *op) { |
| 1578 | auto ifOp = dyn_cast<tosa::IfOp>(Val: op); |
| 1579 | if (!ifOp) |
| 1580 | return success(); |
| 1581 | |
| 1582 | // Currently the dialect supports declaring cond_if operations that |
| 1583 | // have then/else regions that reference values from outside these |
| 1584 | // regions. According to the specification, all values used by the |
| 1585 | // then/else regions must be explicitly declared within the regions. |
| 1586 | // Therefore we must check that the then/else regions are |
| 1587 | // "isolated from above", in order to be conformant to the |
| 1588 | // specification. |
| 1589 | // |
| 1590 | // Note: the dialect currently supports two styles of syntax for |
| 1591 | // declaring "cond_if" operations. We'll refer to these as follows: |
| 1592 | // |
| 1593 | // Generic: |
| 1594 | // %0 = "tosa.cond_if"(%arg0, %arg1, %arg2) ({ |
| 1595 | // ^bb0(%arg3, %arg4): |
| 1596 | // tosa.yield %arg3 |
| 1597 | // }, { |
| 1598 | // ^bb0(%arg3, %arg4): |
| 1599 | // tosa.yield %arg4 |
| 1600 | // }) |
| 1601 | // |
| 1602 | // Simplified: |
| 1603 | // %0 = tosa.cond_if %arg2 (%arg3 = %arg0, %arg4 = %arg1) { |
| 1604 | // ^bb0(%arg3, %arg4): |
| 1605 | // tosa.yield %arg3 |
| 1606 | // } else { |
| 1607 | // ^bb0(%arg3, %arg4): |
| 1608 | // tosa.yield %arg4 |
| 1609 | // } |
| 1610 | |
| 1611 | if (failed(Result: checkIsolatedRegion(op, regionToCheck&: ifOp.getThenGraph(), regionName: "then" )) || |
| 1612 | failed(Result: checkIsolatedRegion(op, regionToCheck&: ifOp.getElseGraph(), regionName: "else" ))) |
| 1613 | return failure(); |
| 1614 | return success(); |
| 1615 | } |
| 1616 | |
| 1617 | LogicalResult checkErrorIfWhileLoop(Operation *op) { |
| 1618 | auto whileOp = dyn_cast<tosa::WhileOp>(Val: op); |
| 1619 | if (!whileOp) |
| 1620 | return success(); |
| 1621 | |
| 1622 | if (failed(Result: checkIsolatedRegion(op, regionToCheck&: whileOp.getCondGraph(), regionName: "cond" )) || |
| 1623 | failed(Result: checkIsolatedRegion(op, regionToCheck&: whileOp.getBodyGraph(), regionName: "body" ))) |
| 1624 | return failure(); |
| 1625 | return success(); |
| 1626 | } |
| 1627 | |
| 1628 | LogicalResult checkErrorIfScatter(Operation *op) { |
| 1629 | auto scatterOp = dyn_cast<tosa::ScatterOp>(Val: op); |
| 1630 | if (!scatterOp) |
| 1631 | return success(); |
| 1632 | |
| 1633 | // for constant indices, check that there are no duplicate values |
| 1634 | DenseIntElementsAttr indicesAttr; |
| 1635 | if (!matchPattern(value: scatterOp.getIndices(), pattern: m_Constant(bind_value: &indicesAttr))) |
| 1636 | return success(); |
| 1637 | |
| 1638 | auto const indicesType = |
| 1639 | dyn_cast<ShapedType>(Val: scatterOp.getIndices().getType()); |
| 1640 | if (!indicesType || !indicesType.hasRank()) { |
| 1641 | op->emitOpError(message: "expect ranked indices tensor" ); |
| 1642 | return failure(); |
| 1643 | } |
| 1644 | |
| 1645 | if (!hasUniqueConstantScatterIndices(indicesType, indicesAttr)) { |
| 1646 | op->emitOpError(message: "indices values contain duplicates" ); |
| 1647 | return failure(); |
| 1648 | } |
| 1649 | |
| 1650 | return success(); |
| 1651 | } |
| 1652 | |
| 1653 | LogicalResult TosaValidation::applyErrorIfCheck(Operation *op) { |
| 1654 | if (failed(Result: checkErrorIfResize(op, checkStrictSpecConformance: strictOpSpecAlignment)) || |
| 1655 | failed(Result: checkErrorIfMul(op)) || failed(Result: checkErrorIfTable(op)) || |
| 1656 | failed(Result: checkErrorIfRescale(op)) || failed(Result: checkErrorIfPad(op)) || |
| 1657 | failed(Result: checkErrorIfTile(op)) || failed(Result: checkErrorIfReshape(op)) || |
| 1658 | failed(Result: checkErrorIfSlice(op)) || failed(Result: checkErrorIfCondIf(op)) || |
| 1659 | failed(Result: checkErrorIfWhileLoop(op)) || failed(Result: checkErrorIfScatter(op))) |
| 1660 | return failure(); |
| 1661 | return success(); |
| 1662 | } |
| 1663 | |
| 1664 | LogicalResult TosaValidation::applyFunctionSignatureCheck(func::FuncOp op) { |
| 1665 | // Require tensor type parameters and results |
| 1666 | const auto isTensorType = [](Type type) { return isa<TensorType>(Val: type); }; |
| 1667 | if (!llvm::all_of(Range: op.getArgumentTypes(), P: isTensorType)) |
| 1668 | return op.emitOpError() |
| 1669 | << "Function argument types must be a tensor type to be TOSA " |
| 1670 | "compliant, got !tosa.shape type" ; |
| 1671 | if (!llvm::all_of(Range: op.getResultTypes(), P: isTensorType)) |
| 1672 | return op.emitOpError() |
| 1673 | << "Function return types must be a tensor type to be TOSA " |
| 1674 | "compliant, got !tosa.shape type" ; |
| 1675 | |
| 1676 | // Validate element types |
| 1677 | if (failed(Result: validateOperationElementTypes(op, allowUnsigned: !strictOpSpecAlignment))) |
| 1678 | return failure(); |
| 1679 | |
| 1680 | // Level check |
| 1681 | const TosaLevel tosaLevel = targetEnv.getLevel(); |
| 1682 | for (const auto &[idx, argType] : llvm::enumerate(First: op.getArgumentTypes())) { |
| 1683 | const std::string inputDesc = llvm::formatv(Fmt: "input argument {0}" , Vals&: idx); |
| 1684 | if (failed(Result: levelCheckRank(op, typeToCheck: argType, operandOrResult: inputDesc, highest_rank: tosaLevel.MAX_RANK))) |
| 1685 | return failure(); |
| 1686 | if (failed(Result: levelCheckSize(op, typeToCheck: argType, operandOrResult: inputDesc))) |
| 1687 | return failure(); |
| 1688 | } |
| 1689 | for (const auto &[idx, resultType] : llvm::enumerate(First: op.getResultTypes())) { |
| 1690 | const std::string resultDesc = llvm::formatv(Fmt: "return value {0}" , Vals&: idx); |
| 1691 | if (failed(Result: levelCheckRank(op, typeToCheck: resultType, operandOrResult: resultDesc, highest_rank: tosaLevel.MAX_RANK))) |
| 1692 | return failure(); |
| 1693 | if (failed(Result: levelCheckSize(op, typeToCheck: resultType, operandOrResult: resultDesc))) |
| 1694 | return failure(); |
| 1695 | } |
| 1696 | |
| 1697 | // Explicitly check for no zero dimensions |
| 1698 | // Note: This check is not required for TOSA operations since it is mandated |
| 1699 | // on construction |
| 1700 | for (const Type &argType : |
| 1701 | llvm::concat<const Type>(Ranges: op.getArgumentTypes(), Ranges: op.getResultTypes())) { |
| 1702 | if (auto shapedType = dyn_cast<ShapedType>(Val: argType)) { |
| 1703 | if (llvm::any_of(Range: shapedType.getShape(), |
| 1704 | P: [](int64_t dim) { return dim == 0; })) |
| 1705 | return op.emitOpError() << "Function argument or return types must not " |
| 1706 | "have zero dimensions" ; |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | return success(); |
| 1711 | } |
| 1712 | |
| 1713 | LogicalResult TosaValidation::validateValidElementType(Operation *op, Type type, |
| 1714 | bool allowUnsigned) { |
| 1715 | if (isa<FloatType>(Val: type)) { |
| 1716 | if (isa<Float32Type, Float16Type, BFloat16Type, Float8E4M3FNType, |
| 1717 | Float8E5M2Type, Float4E2M1FNType, Float6E2M3FNType, |
| 1718 | Float6E3M2FNType, Float8E8M0FNUType>(Val: type)) |
| 1719 | return success(); |
| 1720 | } else if (auto intTy = dyn_cast<IntegerType>(Val&: type)) { |
| 1721 | if (intTy.isSignless()) { |
| 1722 | switch (intTy.getWidth()) { |
| 1723 | case 1: |
| 1724 | case 4: |
| 1725 | case 8: |
| 1726 | case 16: |
| 1727 | case 32: |
| 1728 | case 48: |
| 1729 | case 64: |
| 1730 | return success(); |
| 1731 | } |
| 1732 | } else if (allowUnsigned && intTy.isUnsigned()) { |
| 1733 | switch (intTy.getWidth()) { |
| 1734 | case 8: |
| 1735 | case 16: |
| 1736 | case 32: |
| 1737 | return success(); |
| 1738 | } |
| 1739 | } |
| 1740 | } else if (isa<tosa::shapeType>(Val: type)) |
| 1741 | return success(); |
| 1742 | else if (isa<MXI8Type, tosa::mxint8Type, tosa::BlockScaledType>(Val: type)) |
| 1743 | return success(); |
| 1744 | |
| 1745 | return op->emitOpError() << "is not profile-aligned: element type " << type |
| 1746 | << " is not legal" ; |
| 1747 | } |
| 1748 | |
| 1749 | LogicalResult |
| 1750 | TosaValidation::validateOperationElementTypes(TosaOp op, bool allowUnsigned) { |
| 1751 | for (Value operand : op->getOperands()) { |
| 1752 | Type elementTy = getElementTypeOrSelf(val: operand); |
| 1753 | if (failed(Result: validateValidElementType(op, type: elementTy, allowUnsigned))) |
| 1754 | return failure(); |
| 1755 | } |
| 1756 | |
| 1757 | for (Type resultTy : op->getResultTypes()) { |
| 1758 | Type elementTy = getElementTypeOrSelf(type: resultTy); |
| 1759 | if (failed(Result: validateValidElementType(op, type: elementTy, allowUnsigned))) |
| 1760 | return failure(); |
| 1761 | } |
| 1762 | |
| 1763 | if (auto variableOp = dyn_cast<tosa::VariableOp>(Val&: *op)) { |
| 1764 | if (failed( |
| 1765 | Result: validateValidElementType(op, type: variableOp.getType(), allowUnsigned))) |
| 1766 | return failure(); |
| 1767 | } |
| 1768 | return success(); |
| 1769 | } |
| 1770 | |
| 1771 | LogicalResult |
| 1772 | TosaValidation::validateOperationElementTypes(func::FuncOp op, |
| 1773 | bool allowUnsigned) { |
| 1774 | for (const Type &argType : |
| 1775 | llvm::concat<const Type>(Ranges: op.getArgumentTypes(), Ranges: op.getResultTypes())) { |
| 1776 | const Type elementTy = getElementTypeOrSelf(type: argType); |
| 1777 | if (failed(Result: validateValidElementType(op, type: elementTy, allowUnsigned))) |
| 1778 | return failure(); |
| 1779 | } |
| 1780 | |
| 1781 | return success(); |
| 1782 | } |
| 1783 | |
| 1784 | void TosaValidation::runOnOperation() { |
| 1785 | ModuleOp modOp = getOperation(); |
| 1786 | TosaDialect *tosaDialect = getContext().getLoadedDialect<TosaDialect>(); |
| 1787 | if (!tosaDialect) |
| 1788 | return; |
| 1789 | |
| 1790 | const TargetEnvAttr targetEnvAttr = lookupTargetEnvOrDefault(op: modOp); |
| 1791 | const auto maybeTargetEnv = |
| 1792 | tosa::TargetEnv::createTargetEnvFromAttr(targetAttr: targetEnvAttr, targetEnvAttrLoc: modOp.getLoc()); |
| 1793 | if (failed(Result: maybeTargetEnv)) |
| 1794 | return signalPassFailure(); |
| 1795 | targetEnv = *maybeTargetEnv; |
| 1796 | |
| 1797 | const auto functions = modOp.getOps<func::FuncOp>(); |
| 1798 | if (validateFunctionSignature && |
| 1799 | llvm::any_of(Range: functions, P: [&](func::FuncOp func) { |
| 1800 | return failed(Result: applyFunctionSignatureCheck(op: func)); |
| 1801 | })) |
| 1802 | return signalPassFailure(); |
| 1803 | |
| 1804 | modOp.walk(callback: [&](TosaOp op) { |
| 1805 | // validate operator element types: |
| 1806 | // - rescale operator is allowed to have ui8/ui16/ui32 |
| 1807 | // operands/results when strictOpSpecAlignment is false |
| 1808 | // - perform valid element type check at the beginning to |
| 1809 | // protect rest of code against quantized element types |
| 1810 | const bool allowUnsigned = |
| 1811 | !strictOpSpecAlignment && isa<tosa::RescaleOp>(Val: op); |
| 1812 | if (failed(Result: validateOperationElementTypes(op, allowUnsigned))) |
| 1813 | return signalPassFailure(); |
| 1814 | |
| 1815 | if (strictOpSpecAlignment && |
| 1816 | failed(Result: profileComp.checkProfile(op, targetEnv))) |
| 1817 | return signalPassFailure(); |
| 1818 | |
| 1819 | if (strictOpSpecAlignment && |
| 1820 | failed(Result: profileComp.checkExtension(op, targetEnv))) |
| 1821 | return signalPassFailure(); |
| 1822 | |
| 1823 | if (strictOpSpecAlignment && |
| 1824 | failed(Result: checkSpecificationVersionConstraint(op, env: targetEnv))) |
| 1825 | return signalPassFailure(); |
| 1826 | |
| 1827 | if (!allowInvalidOpDatatypeCombinations && |
| 1828 | failed(Result: profileComp.checkInvalid(op))) |
| 1829 | return signalPassFailure(); |
| 1830 | |
| 1831 | // Some uses of TOSA rely on the constant operands of particular |
| 1832 | // operations. |
| 1833 | if (failed(Result: applyConstantOperandCheck(op))) |
| 1834 | return signalPassFailure(); |
| 1835 | |
| 1836 | // do level checks |
| 1837 | if (failed(Result: applyLevelCheck(op))) |
| 1838 | return signalPassFailure(); |
| 1839 | |
| 1840 | // check additional attribute restrictions |
| 1841 | if (failed(Result: applyAttributeCheck(op))) |
| 1842 | return signalPassFailure(); |
| 1843 | |
| 1844 | // do variable type checks |
| 1845 | if (failed(Result: applyVariableCheck(op))) |
| 1846 | return signalPassFailure(); |
| 1847 | |
| 1848 | // do error if checks |
| 1849 | if (failed(Result: applyErrorIfCheck(op))) |
| 1850 | return signalPassFailure(); |
| 1851 | }); |
| 1852 | } |
| 1853 | } // namespace |
| 1854 | |