[Mlir-commits] [mlir] [mlir][tosa] Disallow inferable dim in reshape/slice validation (PR #182472)
Sayan Saha
llvmlistbot at llvm.org
Sat Feb 28 16:45:55 PST 2026
================
@@ -1214,6 +1215,47 @@ LogicalResult checkErrorIfPad(Operation *op) {
return success();
}
+LogicalResult checkErrorIfReshape(Operation *op) {
+ auto reshapeOp = dyn_cast<tosa::ReshapeOp>(op);
+ if (!reshapeOp)
+ return success();
+
+ constexpr int64_t kInferableDim = -1;
+ SmallVector<int64_t> shapeValues;
+ if (!tosa::getConstShapeValues(reshapeOp.getShape().getDefiningOp(),
+ shapeValues))
+ return success();
+
+ if (llvm::is_contained(shapeValues, kInferableDim))
+ return op->emitOpError("shape input contains inferable dimension (-1) "
+ "which does not conform to the TOSA specification");
+
+ return success();
+}
+
+LogicalResult checkErrorIfSlice(Operation *op) {
+ auto sliceOp = dyn_cast<tosa::SliceOp>(op);
+ if (!sliceOp)
+ return success();
+
+ constexpr int64_t kInferableDim = -1;
+ SmallVector<int64_t> startValues;
+ SmallVector<int64_t> sizeValues;
+ const bool hasStartValues = tosa::getConstShapeValues(
+ sliceOp.getStart().getDefiningOp(), startValues);
+ const bool hasSizeValues =
+ tosa::getConstShapeValues(sliceOp.getSize().getDefiningOp(), sizeValues);
+
+ if (hasStartValues && llvm::is_contained(startValues, kInferableDim))
+ return op->emitOpError("start input contains inferable dimension (-1) "
+ "which does not conform to the TOSA specification");
+ if (hasSizeValues && llvm::is_contained(sizeValues, kInferableDim))
+ return op->emitOpError("size input contains inferable dimension (-1) which "
----------------
sahas3 wrote:
This is a breaking change for us too.
Source IR:
```
func.func @slice_nofold(%arg0: tensor<?x4xf32>) -> tensor<?x2xf32> {
%0 = tosa.const_shape {values = dense<[0, 0]> : tensor<2xindex>} : () -> !tosa.shape<2>
%1 = tosa.const_shape {values = dense<[-1, 2]> : tensor<2xindex>} : () -> !tosa.shape<2>
%3 = tosa.slice %arg0, %0, %1 : (tensor<?x4xf32>, !tosa.shape<2>, !tosa.shape<2>) -> tensor<?x2xf32>
return %3 : tensor<?x2xf32>
}
```
when lowered to `tensor` produces:
```
mlir-opt --tosa-to-tensor
func.func @slice_nofold(%arg0: tensor<?x4xf32>) -> tensor<?x2xf32> {
%c0 = arith.constant 0 : index
%dim = tensor.dim %arg0, %c0 : tensor<?x4xf32>
%c0_0 = arith.constant 0 : index
%0 = arith.subi %dim, %c0_0 : index
%extracted_slice = tensor.extract_slice %arg0[0, 0] [%0, 2] [1, 1] : tensor<?x4xf32> to tensor<?x2xf32>
return %extracted_slice : tensor<?x2xf32>
}
```
https://github.com/llvm/llvm-project/pull/182472
More information about the Mlir-commits
mailing list