[Mlir-commits] [mlir] [mlir][tosa] Disallow inferable dim in reshape/slice validation (PR #182472)

Luke Hutton llvmlistbot at llvm.org
Mon Mar 2 02:20:53 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 "
----------------
lhutton1 wrote:

See comment below

https://github.com/llvm/llvm-project/pull/182472


More information about the Mlir-commits mailing list