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

Hocky Yudhiono llvmlistbot at llvm.org
Thu Mar 26 19:13:32 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:

<img width="756" height="315" alt="image" src="https://github.com/user-attachments/assets/23ae2744-cab0-46d7-bb9c-1f2a996151fa" />

<img width="776" height="350" alt="image" src="https://github.com/user-attachments/assets/33f00cf1-aee0-4f3b-912c-c3654a8447c6" />

It seems like the current flow only has one call site for the `UnaryFolder`, which is from `unaryShapeFold`. Of which another 3 call sites are from some ops which output is tosa shape.

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


More information about the Mlir-commits mailing list