MLIR 24.0.0git
TosaValidation.cpp
Go to the documentation of this file.
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
17
18#include <string>
19#include <type_traits>
20
24#include "mlir/IR/Builders.h"
25#include "mlir/IR/BuiltinOps.h"
26#include "mlir/IR/Matchers.h"
28#include "mlir/Pass/Pass.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
35namespace mlir {
36namespace tosa {
37#define GEN_PASS_DEF_TOSAVALIDATION
38#include "mlir/Dialect/Tosa/Transforms/Passes.h.inc"
39} // namespace tosa
40} // namespace mlir
41
42using namespace mlir;
43using namespace mlir::tosa;
44
45namespace {
46
47static LogicalResult
48checkConstantOperands(Operation *op, ArrayRef<unsigned int> operandIndices) {
49 for (const auto index : operandIndices) {
50 Attribute attr;
51 if (!matchPattern(op->getOperand(index), m_Constant(&attr))) {
52 return op->emitOpError("expected compile time resolvable constant, but "
53 "got variable value for operand #")
54 << index;
55 }
56 }
57 return success();
58}
59
60static LogicalResult checkConstantOperandMul(Operation *op,
61 const TargetEnv &env) {
62 if (!env.allows(Extension::dynamic) && isa<tosa::MulOp>(op)) {
63 // Check 'shift'
64 return checkConstantOperands(op, {2});
65 }
66 return success();
67}
68
69static LogicalResult checkConstantOperandTable(Operation *op,
70 const TargetEnv &env) {
71 if (!env.allows(Extension::dynamic) && isa<tosa::TableOp>(op)) {
72 // Check 'table'
73 return checkConstantOperands(op, {1});
74 }
75 return success();
76}
77
78static LogicalResult checkConstantOperandPad(Operation *op,
79 const TargetEnv &env) {
80 if (auto padOp = dyn_cast<tosa::PadOp>(op)) {
81 // Assume this op is zero-padding if padConst is not presented
82 if (!env.allows(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, {2});
86 }
87 return success();
88}
89
90static LogicalResult checkConstantOperandRescale(Operation *op,
91 const TargetEnv &env) {
92 if (!env.allows(Extension::dynamic) && isa<tosa::RescaleOp>(op)) {
93 // Check 'multiplier', 'shift', 'input_zp' and 'output_zp'
94 return checkConstantOperands(op, {1, 2, 3, 4});
95 }
96 return success();
97}
98
99template <typename T>
100static LogicalResult checkConstantOperandConvOps(Operation *op,
101 const TargetEnv &env) {
102 if (!env.allows(Extension::dynamic) && isa<T>(op)) {
103 // Check 'input_zp' and 'weight_zp'
104 return checkConstantOperands(op, {3, 4});
105 }
106 return success();
107}
108
109static LogicalResult checkConstantOperandMatMul(Operation *op,
110 const TargetEnv &env) {
111 if (!env.allows(Extension::dynamic) &&
112 isa<tosa::MatMulOp, tosa::MatMulTOp>(op)) {
113 // Check 'A_zp' and 'B_zp'
114 return checkConstantOperands(op, {2, 3});
115 }
116 return success();
117}
118
119static LogicalResult
120checkConstantOperandRowGatherBlockScaled(Operation *op, const TargetEnv &env) {
121 if (!env.allows(Extension::dynamic) &&
122 isa<tosa::RowGatherBlockScaledOp>(op)) {
123 auto rowGatherOp = cast<tosa::RowGatherBlockScaledOp>(op);
124 const unsigned rowCountIndex = rowGatherOp.getValues().size() + 1;
125 return checkConstantOperands(op, {rowCountIndex});
126 }
127 return success();
128}
129
130static LogicalResult checkConstantOperandRowGather(Operation *op,
131 const TargetEnv &env) {
132 if (!env.allows(Extension::dynamic) && isa<tosa::RowGatherOp>(op)) {
133 // Check 'row_count'
134 return checkConstantOperands(op, {2});
135 }
136 return success();
137}
138
139static LogicalResult checkConstantOperandAvgPool2d(Operation *op,
140 const TargetEnv &env) {
141 if (!env.allows(Extension::dynamic) && isa<tosa::AvgPool2dOp>(op)) {
142 // Check 'input_zp' and 'output_zp'
143 return checkConstantOperands(op, {1, 2});
144 }
145 return success();
146}
147
148static LogicalResult
149checkConstantOperandAvgPool2dAdaptive(Operation *op, const TargetEnv &env) {
150 if (!env.allows(Extension::dynamic) && isa<tosa::AvgPool2dAdaptiveOp>(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, {1, 2});
155 }
156 return success();
157}
158
159static LogicalResult checkConstantOperandNegate(Operation *op,
160 const TargetEnv &env) {
161 if (!env.allows(Extension::dynamic) && isa<tosa::NegateOp>(op)) {
162 // Check 'input1_zp' and 'output_zp'
163 return checkConstantOperands(op, {1, 2});
164 }
165 return success();
166}
167
168static LogicalResult checkConstantOperandSilceShape(Operation *op,
169 const TargetEnv &env) {
170 if (!env.allows(Extension::dynamic) && isa<tosa::SliceShapeOp>(op)) {
171 // Check 'start' and 'size'
172 return checkConstantOperands(op, {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.
179static LogicalResult checkSpecificationVersionConstraint(Operation *op,
180 const TargetEnv &env) {
181 auto matmul = dyn_cast<tosa::MatMulOp>(op);
182 if (!matmul ||
184 TosaSpecificationVersion(SpecificationVersion::V_1_1_DRAFT)))
185 return success();
186
187 auto aType = dyn_cast<RankedTensorType>(matmul.getA().getType());
188 auto bType = dyn_cast<RankedTensorType>(matmul.getB().getType());
189 auto outputType = dyn_cast<RankedTensorType>(matmul.getOutput().getType());
190 auto getBatchDimOrDynamic = [](RankedTensorType type) {
191 return type ? type.getDimSize(0) : ShapedType::kDynamic;
192 };
193 if ((!aType || aType.getRank() == 3) && (!bType || bType.getRank() == 3) &&
194 (!outputType || outputType.getRank() == 3) &&
195 succeeded(verifyCompatibleDims({getBatchDimOrDynamic(aType),
196 getBatchDimOrDynamic(bType),
197 getBatchDimOrDynamic(outputType)})))
198 return success();
199
200 return op->emitOpError(
201 "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
209struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
210public:
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(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
240private:
241 void populateConstantOperandChecks() {
242 constCheckers.emplace_back(checkConstantOperandMul);
243 constCheckers.emplace_back(checkConstantOperandTable);
244 constCheckers.emplace_back(checkConstantOperandPad);
245 constCheckers.emplace_back(checkConstantOperandRescale);
246 constCheckers.emplace_back(checkConstantOperandConvOps<tosa::Conv2DOp>);
247 constCheckers.emplace_back(checkConstantOperandConvOps<tosa::Conv3DOp>);
248 constCheckers.emplace_back(
249 checkConstantOperandConvOps<tosa::DepthwiseConv2DOp>);
250 constCheckers.emplace_back(
251 checkConstantOperandConvOps<tosa::TransposeConv2DOp>);
252 constCheckers.emplace_back(checkConstantOperandMatMul);
253 constCheckers.emplace_back(checkConstantOperandRowGather);
254 constCheckers.emplace_back(checkConstantOperandRowGatherBlockScaled);
255 constCheckers.emplace_back(checkConstantOperandAvgPool2d);
256 constCheckers.emplace_back(checkConstantOperandAvgPool2dAdaptive);
257 constCheckers.emplace_back(checkConstantOperandNegate);
258 constCheckers.emplace_back(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, v, targetEnv.getLevel().MAX_KERNEL, inputName,
274 "MAX_KERNEL");
275 }
276
277 LogicalResult levelCheckStride(Operation *op, int32_t v,
278 const StringRef inputName) {
279 return levelCheck(op, v, targetEnv.getLevel().MAX_STRIDE, inputName,
280 "MAX_STRIDE");
281 }
282
283 LogicalResult levelCheckScale(Operation *op, int32_t v,
284 const StringRef inputName) {
285 return levelCheck(op, v, targetEnv.getLevel().MAX_SCALE, inputName,
286 "MAX_SCALE");
287 }
288
289 LogicalResult levelCheckListSize(Operation *op, int32_t v,
290 const StringRef inputName) {
291 const std::string inputDesc =
292 llvm::formatv("length(tensor_list_shape({0}))", inputName);
293 return levelCheck(op, v, targetEnv.getLevel().MAX_TENSOR_LIST_SIZE,
294 inputDesc, "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>(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, 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, 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>(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(tosaOp, v.getType(), "operand")))
378 return failure();
379 }
380 for (const auto &v : tosaOp->getResults()) {
381 if (failed(levelCheckShapeLength(tosaOp, v.getType(), "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, k, "kernel"))) {
397 return failure();
398 }
399 }
400 for (auto s : poolOp.getStride()) {
401 if (failed(levelCheckStride(op, s, "stride"))) {
402 return failure();
403 }
404 }
405 for (auto p : poolOp.getPad()) {
406 if (failed(levelCheckKernel(op, p, "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(poolOp.getKernel().getDefiningOp(),
428 kernelValues)) {
429 for (const auto k : kernelValues)
430 if (failed(levelCheckKernel(op, k, "kernel")))
431 return failure();
432 }
433
434 SmallVector<int64_t> strideValues;
435 if (tosa::getConstShapeValues(poolOp.getStride().getDefiningOp(),
436 strideValues)) {
437 for (const auto s : strideValues)
438 if (failed(levelCheckStride(op, s, "stride")))
439 return failure();
440 }
441
442 SmallVector<int64_t> padValues;
443 if (tosa::getConstShapeValues(poolOp.getPad().getDefiningOp(), padValues)) {
444 for (const auto p : padValues)
445 if (failed(levelCheckKernel(op, p, "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, k, "dilation"))) {
459 return failure();
460 }
461 }
462 for (auto p : convOp.getPad()) {
463 if (failed(levelCheckKernel(op, p, "pad"))) {
464 return failure();
465 }
466 }
467 for (auto s : convOp.getStride()) {
468 if (failed(levelCheckStride(op, s, "stride"))) {
469 return failure();
470 }
471 }
472 auto dilation = convOp.getDilation();
473 if (ShapedType weightType =
474 dyn_cast<ShapedType>(op->getOperand(1).getType())) {
475 auto shape = weightType.getShape();
476 if (isa<tosa::Conv2DOp>(op)) {
477 assert(shape.size() == 4);
478 assert(dilation.size() == 2);
479 if (failed(levelCheckKernel(op, dilation[0] * shape[1],
480 "dilation_y * KH")) ||
481 failed(levelCheckKernel(op, dilation[1] * shape[2],
482 "dilation_x * KW")))
483 return failure();
484 } else if (isa<tosa::Conv3DOp>(op)) {
485 assert(shape.size() == 5);
486 assert(dilation.size() == 3);
487 if (failed(levelCheckKernel(op, dilation[0] * shape[1],
488 "dilation_d * KD")) ||
489 failed(levelCheckKernel(op, dilation[1] * shape[2],
490 "dilation_y * KH")) ||
491 failed(levelCheckKernel(op, dilation[2] * shape[3],
492 "dilation_x * KW")))
493 return failure();
494 } else if (isa<tosa::DepthwiseConv2DOp>(op)) {
495 assert(shape.size() == 4);
496 assert(dilation.size() == 2);
497 if (failed(levelCheckKernel(op, dilation[0] * shape[0],
498 "dilation_y * KH")) ||
499 failed(levelCheckKernel(op, dilation[1] * shape[1],
500 "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>(op);
510 if (!convOp)
511 return success();
512
513 SmallVector<int64_t> padValues;
514 if (tosa::getConstShapeValues(convOp.getPad().getDefiningOp(), padValues)) {
515 for (const auto p : padValues)
516 if (failed(levelCheckKernel(op, p, "pad <= MAX_KERNEL")))
517 return failure();
518 }
519
520 SmallVector<int64_t> strideValues;
521 if (tosa::getConstShapeValues(convOp.getStride().getDefiningOp(),
522 strideValues)) {
523 for (const auto s : strideValues)
524 if (failed(levelCheckKernel(op, s, "stride <= MAX_KERNEL")))
525 return failure();
526 }
527
528 SmallVector<int64_t> dilationValues;
529 if (tosa::getConstShapeValues(convOp.getDilation().getDefiningOp(),
530 dilationValues)) {
531 int64_t KH = ShapedType::kDynamic;
532 int64_t KW = ShapedType::kDynamic;
533 const ShapeAdaptor weightDataShape(convOp.getWeightData().getType());
534 KH = weightDataShape.getDimSize(1);
535 KW = weightDataShape.getDimSize(2);
536 const ShapeAdaptor weightScaleShape(convOp.getWeightScale().getType());
537 KH = ShapedType::isDynamic(KH) ? weightScaleShape.getDimSize(1) : KH;
538 KW = ShapedType::isDynamic(KW) ? weightScaleShape.getDimSize(2) : KW;
539
540 if (!ShapedType::isDynamic(KH) &&
541 failed(levelCheckKernel(op, dilationValues[0] * KH,
542 "dilation_y * KH <= MAX_KERNEL)")))
543 return failure();
544
545 if (!ShapedType::isDynamic(KW) &&
546 failed(levelCheckKernel(op, dilationValues[1] * KW,
547 "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>(v.getType())) {
560 auto shape = type.getShape();
561 assert(shape.size() == 3);
562 if (failed(levelCheckKernel(op, shape[1], "H")) ||
563 failed(levelCheckKernel(op, shape[2], "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>(op)) {
575 if (ShapedType filterType =
576 dyn_cast<ShapedType>(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(levelCheckKernel(op, shape[1], "KH")) ||
581 failed(levelCheckKernel(op, shape[2], "KW"))) {
582 return failure();
583 }
584 }
585 for (auto p : transpose.getOutPad()) {
586 if (failed(levelCheckKernel(op, p, "pad"))) {
587 return failure();
588 }
589 }
590 for (auto s : transpose.getStride()) {
591 if (failed(levelCheckStride(op, s, "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>(op)) {
602 SmallVector<int64_t> scale;
603 if (!tosa::getConstShapeValues(resize.getScale().getDefiningOp(),
604 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(scale, [](int64_t s) { return s <= 0; }))
617 return success();
618
619 if (failed(
620 levelCheckScale(op, scaleYN / scaleYD, "scale_y_n/scale_y_d")) ||
621 failed(
622 levelCheckScale(op, scaleXN / scaleXD, "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>(op) || isa<ModuleOp>(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, 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>(op)) {
659 return levelCheckListSize(op, concat.getInput1().size(), "input1");
660 }
661 if (auto custom = dyn_cast<tosa::CustomOp>(op)) {
662 if (failed(levelCheckListSize(op, custom.getInputList().size(),
663 "input_list")) ||
664 failed(levelCheckListSize(op, custom.getOutputList().size(),
665 "output_list"))) {
666 return failure();
667 }
668 }
669 if (auto condIf = dyn_cast<tosa::IfOp>(op)) {
670 if (failed(
671 levelCheckListSize(op, condIf.getInputList().size(), "inputs")) ||
672 failed(levelCheckListSize(op, condIf.getOutputList().size(),
673 "outputs"))) {
674 return failure();
675 }
676 }
677 if (auto w = dyn_cast<tosa::WhileOp>(op)) {
678 if (failed(levelCheckListSize(op, w.getInputList().size(), "inputs")) ||
679 failed(levelCheckListSize(op, w.getOutputList().size(), "outputs"))) {
680 return failure();
681 }
682 }
683 if (auto concat_shape = dyn_cast<tosa::ConcatShapeOp>(op))
684 return levelCheckListSize(op, concat_shape.getInput().size(), "input");
685 return success();
686 }
687
688 LogicalResult attributeCheckRescale(Operation *op) {
689 if (auto rescale = dyn_cast<tosa::RescaleOp>(op)) {
690 if (rescale.getRoundingMode() == RoundingMode::DOUBLE_ROUND &&
691 !targetEnv.allows(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(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>(op)) {
710 const TosaSpecificationVersion targetVersion = targetEnv.getSpecVersion();
711 const TosaSpecificationVersion minRequiredVersion(1, 1, true);
712 if (cast.getInputUnsigned() &&
713 !(targetVersion.isBackwardsCompatibleWith(minRequiredVersion)))
714 return op->emitOpError()
715 << "failed attribute check: CAST attribute input_unsigned "
716 << "requires version 1.1.draft"
717 << " (got " << stringifyVersion(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;
735 TosaProfileCompliance profileComp;
736 tosa::TargetEnv targetEnv;
737};
738
739template <>
740LogicalResult TosaValidation::levelCheckRanks(tosa::ArgMaxOp tosaOp) {
741 auto *op = tosaOp.getOperation();
742 if (failed(levelCheckRank(op, tosaOp.getInput(), "operand",
743 targetEnv.getLevel().MAX_RANK)))
744 return failure();
745
746 // rank(output) = rank(input) - 1
747 if (failed(levelCheckRank(op, tosaOp.getOutput(), "result",
748 targetEnv.getLevel().MAX_RANK - 1)))
749 return failure();
750
751 return success();
752}
753
754template <>
755LogicalResult TosaValidation::levelCheckRanks(tosa::ArgMinOp tosaOp) {
756 auto *op = tosaOp.getOperation();
757 if (failed(levelCheckRank(op, tosaOp.getInput(), "operand",
758 targetEnv.getLevel().MAX_RANK)))
759 return failure();
760
761 // rank(output) = rank(input) - 1
762 if (failed(levelCheckRank(op, tosaOp.getOutput(), "result",
763 targetEnv.getLevel().MAX_RANK - 1)))
764 return failure();
765
766 return success();
767}
768
769template <>
770LogicalResult TosaValidation::levelCheckRanks(tosa::IfOp tosaOp) {
771 auto *op = tosaOp.getOperation();
772
773 // Only the condition input has rank limitation.
774 if (failed(levelCheckRank(op, tosaOp.getCondition(), "operand",
775 targetEnv.getLevel().MAX_RANK)))
776 return failure();
777
778 return success();
779}
780
781template <>
782LogicalResult TosaValidation::levelCheckRanks(tosa::VariableOp tosaOp) {
783 auto *op = tosaOp.getOperation();
784 auto variableType = getVariableType(tosaOp);
785 if (failed(levelCheckRank(op, variableType, "variable type",
786 targetEnv.getLevel().MAX_RANK)))
787 return failure();
788
789 return success();
790}
791
792template <>
793LogicalResult TosaValidation::levelCheckSizes(tosa::VariableOp tosaOp) {
794 auto *op = tosaOp.getOperation();
795 auto variableType = getVariableType(tosaOp);
796 if (failed(levelCheckSize(op, variableType, "variable type")))
797 return failure();
798
799 return success();
800}
801
802LogicalResult 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
829 CHECK_RANKS_AND_SIZES(Sigmoid);
831 // Elementwise Binary Operators
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);
849 // Elementwise Unary Operators
851 CHECK_RANKS_AND_SIZES(BitwiseNot);
858 CHECK_RANKS_AND_SIZES(LogicalNot);
859 CHECK_RANKS_AND_SIZES(Negate);
860 CHECK_RANKS_AND_SIZES(Reciprocal);
863 // Elementwise Ternary Operators
864 CHECK_RANKS_AND_SIZES(Select);
865 // Comparison Operators
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);
879 CHECK_RANKS_AND_SIZES(Reshape);
880 CHECK_RANKS_AND_SIZES(ReshapeBlockScaled);
881 CHECK_RANKS_AND_SIZES(Reverse);
884 CHECK_RANKS_AND_SIZES(Transpose);
885 // Type Conversion
887 CHECK_RANKS_AND_SIZES(CastFromBlockScaled);
888 CHECK_RANKS_AND_SIZES(CastToBlockScaled);
889 CHECK_RANKS_AND_SIZES(Rescale);
890 // Data Nodes
892 CHECK_RANKS_AND_SIZES(Identity);
893 // Control Flow Operators
895 // Variable Operators
896 CHECK_RANKS_AND_SIZES(Variable);
897 CHECK_RANKS_AND_SIZES(VariableWrite);
898 CHECK_RANKS_AND_SIZES(VariableRead);
899 // Shape Operators
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
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.
959LogicalResult TosaValidation::levelCheckSize(Operation *op,
960 const Type &typeToCheck,
961 const StringRef operandOrResult) {
962 if (ShapedType type = dyn_cast<ShapedType>(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(dim);
968 const TosaSpecificationVersion targetVersion = targetEnv.getSpecVersion();
969 const TosaSpecificationVersion minRequiredVersion(1, 1, true);
970 if (targetVersion.isBackwardsCompatibleWith(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(getElementTypeOrSelf(type));
986 int64_t elementBytes = std::max(INT64_C(1), 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
1004LogicalResult 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(levelCheckRanksAndSizes(op)))
1012 return failure();
1013
1014 if (failed(levelCheckPool<tosa::AvgPool2dOp>(op)) ||
1015 failed(levelCheckAdaptivePool<tosa::AvgPool2dAdaptiveOp>(op)) ||
1016 failed(levelCheckConv<tosa::Conv2DOp>(op)) ||
1017 failed(levelCheckConv<tosa::Conv3DOp>(op)) ||
1018 failed(levelCheckConv<tosa::DepthwiseConv2DOp>(op)) ||
1019 failed(levelCheckFFT<tosa::FFT2dOp>(op)) ||
1020 failed(levelCheckPool<tosa::MaxPool2dOp>(op)) ||
1021 failed(levelCheckAdaptivePool<tosa::MaxPool2dAdaptiveOp>(op)) ||
1022 failed(levelCheckFFT<tosa::RFFT2dOp>(op)) ||
1023 failed(levelCheckTransposeConv2d(op)) || failed(levelCheckResize(op)) ||
1024 failed(levelCheckConv2DBlockScaled(op))) {
1025 return failure();
1026 }
1027
1028 // level check MAX_TENSOR_LIST_SIZE
1029 if (failed(levelCheckListSize(op))) {
1030 return failure();
1031 }
1032
1033 if (isa<tosa::IfOp>(op) || isa<tosa::WhileOp>(op)) {
1034 if (failed(levelCheckMaxNesting(op))) {
1035 return failure();
1036 }
1037 }
1038
1039 return success();
1040}
1041
1042LogicalResult TosaValidation::applyAttributeCheck(Operation *op) {
1043 if (failed(attributeCheckRescale(op)))
1044 return failure();
1045 if (failed(attributeCheckCast(op)))
1046 return failure();
1047 return success();
1048}
1049
1050inline 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
1056LogicalResult TosaValidation::CheckVariable(Operation *op) {
1057 if (auto variableOp = dyn_cast<mlir::tosa::VariableOp>(op)) {
1058 mlir::StringAttr nameAttr = variableOp.getNameAttr();
1059
1060 if (variablesMap.count(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(varShapeAttr.getValues<int64_t>());
1066 RankedTensorType variableType =
1067 RankedTensorType::get(ArrayRef<int64_t>(shape), elementType);
1068
1069 variablesMap[nameAttr] = variableType;
1070 }
1071
1072 return success();
1073}
1074
1075LogicalResult TosaValidation::CheckVariableReadOrWrite(Operation *op) {
1076 if (isa<mlir::tosa::VariableReadOp>(op) ||
1077 isa<mlir::tosa::VariableWriteOp>(op)) {
1078 mlir::StringAttr nameAttr =
1080 .Case<mlir::tosa::VariableReadOp, mlir::tosa::VariableWriteOp>(
1081 [](auto variableOp) { return variableOp.getNameAttr(); });
1082 if (!variablesMap.count(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, 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, varType))
1096 return op->emitOpError() << "result type does not equal variable type";
1097 }
1098 }
1099
1100 return success();
1101}
1102
1103LogicalResult TosaValidation::applyVariableCheck(Operation *op) {
1104 if (failed(CheckVariable(op)) || failed(CheckVariableReadOrWrite(op)))
1105 return failure();
1106 return success();
1107}
1108
1109LogicalResult checkErrorIfResize(Operation *op,
1110 bool checkStrictSpecConformance) {
1111 auto resize = dyn_cast<tosa::ResizeOp>(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>(input.getType());
1119 const RankedTensorType outputType =
1120 llvm::dyn_cast<RankedTensorType>(output.getType());
1121
1122 if (!inputType || !outputType)
1123 return op->emitOpError("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(1), outputType.getDimSize(2),
1131 inputType.getDimSize(1), inputType.getDimSize(2)};
1132 const int64_t *maxDim = llvm::max_element(sizes);
1133 if (maxDim != sizes.end() && *maxDim >= 16384)
1134 return op->emitOpError(
1135 "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(resize.getScale().getDefiningOp(), scale))
1141 return failure();
1142
1143 // ERROR_IF(scale <= 0)
1144 if (llvm::any_of(scale, [](int64_t s) { return s <= 0; }))
1145 return op->emitOpError("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 "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("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(resize.getOffset().getDefiningOp(), offset) ||
1166 !tosa::getConstShapeValues(resize.getBorder().getDefiningOp(), 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("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("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("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("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(1);
1206 const int64_t ow = outputType.getDimSize(2);
1207 const int64_t ih = inputType.getDimSize(1);
1208 const int64_t iw = inputType.getDimSize(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 "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 "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 "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("calculated output width did not match expected: ")
1240 << "calculated=" << calculatedOutWidth << ", expected=" << ow;
1241 }
1242
1243 return success();
1244}
1245
1246LogicalResult checkErrorIfMul(Operation *op) {
1247 auto mul = dyn_cast<tosa::MulOp>(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(mul.getShift(), m_Constant(&shift_elem)))
1255 return success();
1256 int32_t shift = shift_elem.getValues<IntegerAttr>()[0].getInt();
1257 auto inputElemType = getElementTypeOrSelf(mul.getInput1());
1258 if (inputElemType.isInteger(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
1275LogicalResult checkErrorIfTable(Operation *op) {
1276 auto table = dyn_cast<tosa::TableOp>(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(table.getInput1().getType());
1282 const int tableSize = inputElemType.isInteger(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
1295LogicalResult checkErrorIfRescale(Operation *op) {
1296 auto rescale = dyn_cast<tosa::RescaleOp>(op);
1297 if (!rescale)
1298 return success();
1299
1300 auto inputType = llvm::dyn_cast<ShapedType>(rescale.getInput().getType());
1301 auto outputType = llvm::dyn_cast<ShapedType>(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(maybeIZp) && rescale.verifyInputZeroPoint(*maybeIZp).failed())
1361 return failure();
1362
1363 if (FailureOr<int64_t> maybeOZp = rescale.getOutputZeroPoint();
1364 succeeded(maybeOZp) && rescale.verifyOutputZeroPoint(*maybeOZp).failed())
1365 return failure();
1366
1367 return success();
1368}
1369
1370LogicalResult checkErrorIfPad(Operation *op) {
1371 auto pad = dyn_cast<tosa::PadOp>(op);
1372 if (!pad)
1373 return success();
1374
1375 DenseIntElementsAttr paddingAttr;
1376 if (!matchPattern(pad.getPadding(), m_Constant(&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
1389LogicalResult checkErrorIfReshape(Operation *op) {
1390 auto reshapeOp = dyn_cast<tosa::ReshapeOp>(op);
1391 if (!reshapeOp)
1392 return success();
1393
1394 SmallVector<int64_t> shapeValues;
1395 if (!tosa::getConstShapeValues(reshapeOp.getShape().getDefiningOp(),
1396 shapeValues))
1397 return success();
1398
1399 if (llvm::is_contained(shapeValues, kInferableDimSize))
1400 return op->emitOpError("shape input contains inferable dimension (")
1402 << ") "
1403 "which does not conform to the TOSA specification";
1404
1405 return success();
1406}
1407
1408LogicalResult checkErrorIfSlice(Operation *op) {
1409 auto sliceOp = dyn_cast<tosa::SliceOp>(op);
1410 if (!sliceOp)
1411 return success();
1412
1413 SmallVector<int64_t> startValues;
1414 SmallVector<int64_t> sizeValues;
1415 const bool hasStartValues = tosa::getConstShapeValues(
1416 sliceOp.getStart().getDefiningOp(), startValues);
1417 const bool hasSizeValues =
1418 tosa::getConstShapeValues(sliceOp.getSize().getDefiningOp(), sizeValues);
1419
1420 if (hasStartValues && llvm::is_contained(startValues, kInferableDimSize))
1421 return op->emitOpError("start input contains inferable dimension (")
1423 << ") which does not conform to the TOSA specification";
1424 if (hasSizeValues && llvm::is_contained(sizeValues, kInferableDimSize))
1425 return op->emitOpError("size input contains inferable dimension (")
1427 << ") which "
1428 "does not conform to the TOSA specification";
1430 // ERROR_IF(any(start < 0))
1431 if (hasStartValues &&
1432 llvm::any_of(startValues, [](int64_t v) { return v < 0; }))
1433 return op->emitOpError("start values must be non-negative, got [")
1434 << startValues << "]";
1435
1436 // ERROR_IF(any(size <= 0))
1437 if (hasSizeValues &&
1438 llvm::any_of(sizeValues, [](int64_t v) { return v <= 0; }))
1439 return op->emitOpError("size values must be > 0, got [")
1440 << sizeValues << "]";
1442 // MLIR-specific: output shape must match the size values.
1443 if (hasSizeValues) {
1444 const auto outputType = sliceOp.getOutput().getType();
1445 if (const auto rankedOutput = dyn_cast<RankedTensorType>(outputType)) {
1446 if (failed(verifyCompatibleShape(rankedOutput.getShape(), sizeValues)))
1447 return op->emitOpError(
1448 "expected output shape to match size values, got ")
1449 << outputType << " vs [" << sizeValues << "]";
1450 }
1451 }
1453 // ERROR_IF(any(start + size > shape1))
1454 if (hasStartValues && hasSizeValues) {
1455 if (const auto inputType =
1456 dyn_cast<RankedTensorType>(sliceOp.getInput1().getType())) {
1457 for (const auto &[index, vals] : llvm::enumerate(llvm::zip_equal(
1458 startValues, sizeValues, inputType.getShape()))) {
1459 const auto &[start, size, inputDim] = vals;
1460 if (ShapedType::isDynamic(inputDim))
1461 continue;
1462 // Earlier checks already ensured that start and slice are non-negative.
1463 // Avoid overflowing start + size.
1464 if (start > inputDim || size > inputDim - start)
1465 return op->emitOpError(
1466 "start + size must be less than or equal to input "
1467 "dimension size, got start=")
1468 << start << ", size=" << size
1469 << " vs input dim size=" << inputDim << " at dimension "
1470 << index;
1471 }
1475 // ERROR_IF(is_block_scale<in_out_t>() && start[rank(shape1) - 1] %
1476 // get_innermost_block_size<in_out_t>() != 0)
1477 if (hasStartValues && !startValues.empty()) {
1478 auto elemType = getElementTypeOrSelf(sliceOp.getInput1().getType());
1479 if (auto blockScaledType = dyn_cast<BlockScaledType>(elemType)) {
1480 const int64_t startBlock = startValues.back();
1481 const int64_t scaleBlock =
1482 BlockShapeAttr::getBlockShapeValue(blockScaledType.getBlockShape());
1483 if (startBlock % scaleBlock != 0)
1484 return op->emitOpError(
1485 "expected start innermost block size to match data type "
1486 "for block scaled input, got start block=")
1487 << startBlock << ", scale block=" << scaleBlock;
1488 }
1489 }
1490
1491 return success();
1492}
1493
1494static bool isOpIsolatedWithinRegion(Operation *op, Region *region) {
1495 return llvm::all_of(op->getOperands(), [&](auto operand) {
1496 Region *operandRegion = operand.getParentRegion();
1497 return operandRegion && region->isAncestor(operandRegion);
1498 });
1499}
1500
1501static LogicalResult isRegionIsolatedFromAbove(Region &regionToCheck) {
1502 bool noLiveInValue = true;
1503 regionToCheck.walk([&noLiveInValue, &regionToCheck](Operation *op) {
1504 if (!isOpIsolatedWithinRegion(op, &regionToCheck)) {
1505 noLiveInValue = false;
1506 return WalkResult::interrupt();
1507 }
1508 return WalkResult::advance();
1509 });
1510 return noLiveInValue ? success() : failure();
1511}
1512
1513LogicalResult checkIsolatedRegion(Operation *op, Region &regionToCheck,
1514 StringRef regionName) {
1515 if (succeeded(isRegionIsolatedFromAbove(regionToCheck)))
1516 return success();
1517 return op->emitOpError()
1518 << "is not conformant to the TOSA specification. It requires the '"
1519 << regionName << "' region is isolated from above.\n";
1520}
1521
1522LogicalResult checkErrorIfCondIf(Operation *op) {
1523 auto ifOp = dyn_cast<tosa::IfOp>(op);
1524 if (!ifOp)
1525 return success();
1526
1527 // Currently the dialect supports declaring cond_if operations that
1528 // have then/else regions that reference values from outside these
1529 // regions. According to the specification, all values used by the
1530 // then/else regions must be explicitly declared within the regions.
1531 // Therefore we must check that the then/else regions are
1532 // "isolated from above", in order to be conformant to the
1533 // specification.
1534 //
1535 // Note: the dialect currently supports two styles of syntax for
1536 // declaring "cond_if" operations. We'll refer to these as follows:
1537 //
1538 // Generic:
1539 // %0 = "tosa.cond_if"(%arg0, %arg1, %arg2) ({
1540 // ^bb0(%arg3, %arg4):
1541 // tosa.yield %arg3
1542 // }, {
1543 // ^bb0(%arg3, %arg4):
1544 // tosa.yield %arg4
1545 // })
1546 //
1547 // Simplified:
1548 // %0 = tosa.cond_if %arg2 (%arg3 = %arg0, %arg4 = %arg1) {
1549 // ^bb0(%arg3, %arg4):
1550 // tosa.yield %arg3
1551 // } else {
1552 // ^bb0(%arg3, %arg4):
1553 // tosa.yield %arg4
1554 // }
1555
1556 if (failed(checkIsolatedRegion(op, ifOp.getThenGraph(), "then")) ||
1557 failed(checkIsolatedRegion(op, ifOp.getElseGraph(), "else")))
1558 return failure();
1559 return success();
1560}
1561
1562LogicalResult checkErrorIfWhileLoop(Operation *op) {
1563 auto whileOp = dyn_cast<tosa::WhileOp>(op);
1564 if (!whileOp)
1565 return success();
1566
1567 if (failed(checkIsolatedRegion(op, whileOp.getCondGraph(), "cond")) ||
1568 failed(checkIsolatedRegion(op, whileOp.getBodyGraph(), "body")))
1569 return failure();
1570 return success();
1571}
1572
1573LogicalResult checkErrorIfScatter(Operation *op) {
1574 auto scatterOp = dyn_cast<tosa::ScatterOp>(op);
1575 if (!scatterOp)
1576 return success();
1577
1578 // for constant indices, check that there are no duplicate values
1579 DenseIntElementsAttr indicesAttr;
1580 if (!matchPattern(scatterOp.getIndices(), m_Constant(&indicesAttr)))
1581 return success();
1582
1583 auto const indicesType =
1584 dyn_cast<ShapedType>(scatterOp.getIndices().getType());
1585 if (!indicesType || !indicesType.hasRank()) {
1586 op->emitOpError("expect ranked indices tensor");
1587 return failure();
1588 }
1589
1590 if (!hasUniqueConstantScatterIndices(indicesType, indicesAttr)) {
1591 op->emitOpError("indices values contain duplicates");
1592 return failure();
1593 }
1594
1595 return success();
1596}
1597
1598LogicalResult TosaValidation::applyErrorIfCheck(Operation *op) {
1599 if (failed(checkErrorIfResize(op, strictOpSpecAlignment)) ||
1600 failed(checkErrorIfMul(op)) || failed(checkErrorIfTable(op)) ||
1601 failed(checkErrorIfRescale(op)) || failed(checkErrorIfPad(op)) ||
1602 failed(checkErrorIfReshape(op)) || failed(checkErrorIfSlice(op)) ||
1603 failed(checkErrorIfCondIf(op)) || failed(checkErrorIfWhileLoop(op)) ||
1604 failed(checkErrorIfScatter(op)))
1605 return failure();
1606 return success();
1607}
1608
1609LogicalResult TosaValidation::applyFunctionSignatureCheck(func::FuncOp op) {
1610 // Require tensor type parameters and results
1611 const auto isTensorType = [](Type type) { return isa<TensorType>(type); };
1612 if (!llvm::all_of(op.getArgumentTypes(), isTensorType))
1613 return op.emitOpError()
1614 << "Function argument types must be a tensor type to be TOSA "
1615 "compliant, got !tosa.shape type";
1616 if (!llvm::all_of(op.getResultTypes(), isTensorType))
1617 return op.emitOpError()
1618 << "Function return types must be a tensor type to be TOSA "
1619 "compliant, got !tosa.shape type";
1620
1621 // Validate element types
1622 if (failed(validateOperationElementTypes(op, !strictOpSpecAlignment)))
1623 return failure();
1624
1625 // Level check
1626 const TosaLevel tosaLevel = targetEnv.getLevel();
1627 for (const auto &[idx, argType] : llvm::enumerate(op.getArgumentTypes())) {
1628 const std::string inputDesc = llvm::formatv("input argument {0}", idx);
1629 if (failed(levelCheckRank(op, argType, inputDesc, tosaLevel.MAX_RANK)))
1630 return failure();
1631 if (failed(levelCheckSize(op, argType, inputDesc)))
1632 return failure();
1633 }
1634 for (const auto &[idx, resultType] : llvm::enumerate(op.getResultTypes())) {
1635 const std::string resultDesc = llvm::formatv("return value {0}", idx);
1636 if (failed(levelCheckRank(op, resultType, resultDesc, tosaLevel.MAX_RANK)))
1637 return failure();
1638 if (failed(levelCheckSize(op, resultType, resultDesc)))
1639 return failure();
1640 }
1641
1642 // Explicitly check for no zero dimensions
1643 // Note: This check is not required for TOSA operations since it is mandated
1644 // on construction
1645 for (const Type &argType :
1646 llvm::concat<const Type>(op.getArgumentTypes(), op.getResultTypes())) {
1647 if (auto shapedType = dyn_cast<ShapedType>(argType)) {
1648 if (llvm::any_of(shapedType.getShape(),
1649 [](int64_t dim) { return dim == 0; }))
1650 return op.emitOpError() << "Function argument or return types must not "
1651 "have zero dimensions";
1652 }
1653 }
1654
1655 return success();
1656}
1657
1658LogicalResult TosaValidation::validateValidElementType(Operation *op, Type type,
1659 bool allowUnsigned) {
1660 if (isa<FloatType>(type)) {
1661 if (isa<Float32Type, Float16Type, BFloat16Type, Float8E4M3FNType,
1662 Float8E5M2Type, Float4E2M1FNType, Float6E2M3FNType,
1663 Float6E3M2FNType, Float8E8M0FNUType>(type))
1664 return success();
1665 } else if (auto intTy = dyn_cast<IntegerType>(type)) {
1666 if (intTy.isSignless()) {
1667 switch (intTy.getWidth()) {
1668 case 1:
1669 case 4:
1670 case 8:
1671 case 16:
1672 case 32:
1673 case 48:
1674 case 64:
1675 return success();
1676 }
1677 } else if (allowUnsigned && intTy.isUnsigned()) {
1678 switch (intTy.getWidth()) {
1679 case 8:
1680 case 16:
1681 case 32:
1682 return success();
1683 }
1684 }
1685 } else if (isa<tosa::shapeType>(type))
1686 return success();
1687 else if (isa<MXI8Type, tosa::mxint8Type, tosa::BlockScaledType>(type))
1688 return success();
1689
1690 return op->emitOpError() << "is not profile-aligned: element type " << type
1691 << " is not legal";
1692}
1693
1694LogicalResult
1695TosaValidation::validateOperationElementTypes(TosaOp op, bool allowUnsigned) {
1696 for (Value operand : op->getOperands()) {
1697 Type elementTy = getElementTypeOrSelf(operand);
1698 if (failed(validateValidElementType(op, elementTy, allowUnsigned)))
1699 return failure();
1700 }
1701
1702 for (Type resultTy : op->getResultTypes()) {
1703 Type elementTy = getElementTypeOrSelf(resultTy);
1704 if (failed(validateValidElementType(op, elementTy, allowUnsigned)))
1705 return failure();
1706 }
1707
1708 if (auto variableOp = dyn_cast<tosa::VariableOp>(*op)) {
1709 if (failed(
1710 validateValidElementType(op, variableOp.getType(), allowUnsigned)))
1711 return failure();
1712 }
1713 return success();
1714}
1715
1716LogicalResult
1717TosaValidation::validateOperationElementTypes(func::FuncOp op,
1718 bool allowUnsigned) {
1719 for (const Type &argType :
1720 llvm::concat<const Type>(op.getArgumentTypes(), op.getResultTypes())) {
1721 const Type elementTy = getElementTypeOrSelf(argType);
1722 if (failed(validateValidElementType(op, elementTy, allowUnsigned)))
1723 return failure();
1724 }
1725
1726 return success();
1727}
1728
1729void TosaValidation::runOnOperation() {
1730 ModuleOp modOp = getOperation();
1731 TosaDialect *tosaDialect = getContext().getLoadedDialect<TosaDialect>();
1732 if (!tosaDialect)
1733 return;
1734
1735 const TargetEnvAttr targetEnvAttr = lookupTargetEnvOrDefault(modOp);
1736 const auto maybeTargetEnv =
1737 tosa::TargetEnv::createTargetEnvFromAttr(targetEnvAttr, modOp.getLoc());
1738 if (failed(maybeTargetEnv))
1739 return signalPassFailure();
1740 targetEnv = *maybeTargetEnv;
1741
1742 const auto functions = modOp.getOps<func::FuncOp>();
1743 if (validateFunctionSignature &&
1744 llvm::any_of(functions, [&](func::FuncOp func) {
1745 return failed(applyFunctionSignatureCheck(func));
1746 }))
1747 return signalPassFailure();
1748
1749 modOp.walk([&](TosaOp op) {
1750 // validate operator element types:
1751 // - rescale operator is allowed to have ui8/ui16/ui32
1752 // operands/results when strictOpSpecAlignment is false
1753 // - perform valid element type check at the beginning to
1754 // protect rest of code against quantized element types
1755 const bool allowUnsigned =
1756 !strictOpSpecAlignment && isa<tosa::RescaleOp>(op);
1757 if (failed(validateOperationElementTypes(op, allowUnsigned)))
1758 return signalPassFailure();
1759
1760 if (strictOpSpecAlignment &&
1761 failed(profileComp.checkProfile(op, targetEnv)))
1762 return signalPassFailure();
1763
1764 if (strictOpSpecAlignment &&
1765 failed(profileComp.checkExtension(op, targetEnv)))
1766 return signalPassFailure();
1767
1768 if (strictOpSpecAlignment &&
1769 failed(checkSpecificationVersionConstraint(op, targetEnv)))
1770 return signalPassFailure();
1771
1772 if (!allowInvalidOpDatatypeCombinations &&
1773 failed(profileComp.checkInvalid(op)))
1774 return signalPassFailure();
1775
1776 // Some uses of TOSA rely on the constant operands of particular
1777 // operations.
1778 if (failed(applyConstantOperandCheck(op)))
1779 return signalPassFailure();
1780
1781 // do level checks
1782 if (failed(applyLevelCheck(op)))
1783 return signalPassFailure();
1784
1785 // check additional attribute restrictions
1786 if (failed(applyAttributeCheck(op)))
1787 return signalPassFailure();
1788
1789 // do variable type checks
1790 if (failed(applyVariableCheck(op)))
1791 return signalPassFailure();
1792
1793 // do error if checks
1794 if (failed(applyErrorIfCheck(op)))
1795 return signalPassFailure();
1796 });
1797}
1798} // namespace
return success()
lhs
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
static std::optional< int64_t > idivCheck(const int64_t lhs, const int64_t rhs)
Definition TosaOps.cpp:275
#define CHECK_RANKS_AND_SIZES(tosaOp)
#define CHECK_SIZES(tosaOp)
#define CHECK_SHAPE_LEN(tosaOp)
@ Gather
#define mul(a, b)
Attributes are known-constant values of operations.
Definition Attributes.h:25
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
Value getOperand(unsigned idx)
Definition Operation.h:377
Operation * getParentOp()
Returns the closest surrounding operation that contains this operation or nullptr if this is a top-le...
Definition Operation.h:251
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:405
result_range getResults()
Definition Operation.h:442
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
RetT walk(FnT &&callback)
Walk all nested operations, blocks or regions (including this region), depending on the type of callb...
Definition Region.h:297
Type getType() const
Return the type of this value.
Definition Value.h:105
static WalkResult advance()
Definition WalkResult.h:47
static WalkResult interrupt()
Definition WalkResult.h:46
This class represents the capability enabled in the target implementation such as profile,...
Definition TargetEnv.h:119
TosaLevel getLevel() const
Definition TargetEnv.h:136
static FailureOr< TargetEnv > createTargetEnvFromAttr(TargetEnvAttr targetAttr, Location targetEnvAttrLoc)
bool allows(Profile prof) const
Definition TargetEnv.h:139
TosaSpecificationVersion getSpecVersion() const
Definition TargetEnv.h:132
LogicalResult checkExtension(Operation *op, const tosa::TargetEnv &targetEnv)
LogicalResult checkInvalid(Operation *op)
LogicalResult checkProfile(Operation *op, const tosa::TargetEnv &targetEnv)
A thin wrapper around the SpecificationVersion enum to represent and provide utilities around the TOS...
Definition TargetEnv.h:60
bool isBackwardsCompatibleWith(TosaSpecificationVersion baseVersion) const
Definition TargetEnv.h:69
SmallVector< AffineExpr, 4 > concat(ArrayRef< AffineExpr > a, ArrayRef< AffineExpr > b)
Return the vector that is the concatenation of a and b.
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:853
llvm::SmallString< 4 > stringifyVersion(TosaSpecificationVersion version)
Definition TargetEnv.cpp:25
RankedTensorType getVariableType(VariableOp variableOp)
static constexpr TosaLevel TOSA_LEVEL_NONE
Definition TargetEnv.h:45
bool hasUniqueConstantScatterIndices(ShapedType indicesType, DenseIntElementsAttr indicesAttr)
constexpr int64_t kInferableDimSize
Represents a dimension in the shape of a tensor that can be inferred based on the other provided dime...
Definition TosaOps.h:97
unsigned getBitWidth(Type type)
Definition TosaOps.cpp:327
TargetEnvAttr lookupTargetEnvOrDefault(Operation *op)
Queries the target environment recursively from enclosing symbol table ops containing the given op or...
bool getConstShapeValues(Operation *op, llvm::SmallVector< int64_t > &result_shape)
Include the generated interface declarations.
bool matchPattern(Value value, const Pattern &pattern)
Entry point for matching a pattern over a Value.
Definition Matchers.h:490
@ Mul
RHS of mul is always a constant or a symbolic expression.
Definition AffineExpr.h:43
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
LogicalResult verifyCompatibleDims(ArrayRef< int64_t > dims)
Dimensions are compatible if all non-dynamic dims are equal.
llvm::TypeSwitch< T, ResultT > TypeSwitch
Definition LLVM.h:147
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:123
LogicalResult verifyCompatibleShape(ArrayRef< int64_t > shape1, ArrayRef< int64_t > shape2)
Returns success if the given two shapes are compatible.
detail::constant_op_matcher m_Constant()
Matches a constant foldable operation.
Definition Matchers.h:369