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

Hocky Yudhiono llvmlistbot at llvm.org
Thu Mar 26 08:36:13 PDT 2026


================
@@ -1784,28 +1752,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;
+  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 the dim-length is 1, tosa.reverse is a no-op.
-  if (operandTy.hasRank() &&
-      (operandTy.getRank() == 0 || operandTy.getDimSize(axis) == 1))
-    return operand;
+  if (noOpReverse)
+    return foldToInputIfTypeMatches(*this, operand);
----------------
hockyy wrote:

```c++
OpFoldResult ReverseOp::fold(FoldAdaptor adaptor) {
  auto operand = getInput1();
  auto operandTy = llvm::cast<ShapedType>(operand.getType());
  auto axis = getAxis();
  // If the dim-length is 1, or reversing axis is unit-dim, also a no-op.
  const bool isSplatInput =
      llvm::isa_and_nonnull<SplatElementsAttr>(adaptor.getInput1());
  if (!operandTy.hasRank() ||
      (!isSplatInput && operandTy.hasRank() && operandTy.getDimSize(axis) != 1))
    return {};
  return foldToInputIfTypeMatches(*this, operand);
}
```
@lhutton1 I can come up with this.. So
if its unranked (`*xf32`) or (its not DenseSplat and the reversing axis size is not 1) then dont fold 

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


More information about the Mlir-commits mailing list