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

Luke Hutton llvmlistbot at llvm.org
Thu Mar 26 06:45:33 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);
----------------
lhutton1 wrote:

Apologies, I thought I'd left this comment in the previous review, but it seems to have gone missing.. 

Could we convert this to? IMO it seems more readable. I've also removed the `operandTy.getRank() == 0` check since the tablegen definition enforces the input is rank >= 1 if it is ranked, so it's not needed here.

```suggestion
 // If the dim-length is 1, or reversing axis is unit-dim, also a no-op.
  const bool noOpReverse =
      llvm::isa_and_nonnull<SplatElementsAttr>(adaptor.getInput1());
  if (!noOpReverse || !operandTy.hasRank() || !operandTy.getDimSize(axis) == 0)
       return {};
  return foldToInputIfTypeMatches(*this, operand);     
```

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


More information about the Mlir-commits mailing list