[Mlir-commits] [mlir] [mlir][tosa] Harden folds/canonicalizations for unranked and dynamic shapes (PR #188188)

Luke Hutton llvmlistbot at llvm.org
Thu Mar 26 03:32:23 PDT 2026


================
@@ -1735,29 +1754,22 @@ OpFoldResult ResizeOp::fold(FoldAdaptor adaptor) {
     return {};
   }
 
-  auto input = getInput();
-  auto inputTy = llvm::cast<RankedTensorType>(input.getType());
-  auto resultTy = llvm::cast<RankedTensorType>(getType());
-  if (inputTy != resultTy)
-    return {};
-
-  return input;
+  return foldToInputIfTypeMatches(*this, getInput());
 }
 
 OpFoldResult ReverseOp::fold(FoldAdaptor adaptor) {
   auto operand = getInput1();
   auto operandTy = llvm::cast<ShapedType>(operand.getType());
   auto axis = getAxis();
-  auto operandAttr =
-      llvm::dyn_cast_if_present<SplatElementsAttr>(adaptor.getInput1());
-  if (operandAttr)
-    return operandAttr;
-
-  // If the dim-length is 1, tosa.reverse is a no-op.
-  if (operandTy.hasRank() &&
-      (operandTy.getRank() == 0 || operandTy.getDimSize(axis) == 1))
-    return operand;
-
+  // Check if the reverse is a no-op
+  // If the operand is a splat, the reverse is a no-op.
+  bool noOpReverse =
+      llvm::isa_and_nonnull<SplatElementsAttr>(adaptor.getInput1());
+  // If the dim-length is 1, or reversing axis is unit-dim, also a no-op.
+  noOpReverse |= (operandTy.hasRank() && (operandTy.getRank() == 0 ||
+                                          operandTy.getDimSize(axis) == 1));
+  if (noOpReverse)
+    return foldToInputIfTypeMatches(*this, operand);
----------------
lhutton1 wrote:

```suggestion
  const bool noOpReverse =
      llvm::isa_and_nonnull<SplatElementsAttr>(adaptor.getInput1());

  // If the dim-length is 1, or reversing axis is unit-dim, also a no-op.
  if (!noOpReverse || !operandTy.hasRank() || operandTy.getDimSize(axis) == 1)
      return {};
 
  return foldToInputIfTypeMatches(*this, operand);
```

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


More information about the Mlir-commits mailing list