[Mlir-commits] [mlir] [mlir] Canonicalization pattern for 'shape.shape_of' (PR #98531)

Spenser Bauman llvmlistbot at llvm.org
Mon Jul 15 06:50:28 PDT 2024


================
@@ -1585,13 +1585,25 @@ OpFoldResult ReshapeOp::fold(FoldAdaptor adaptor) {
           getResult().getType()))
     return reshapedSource;
 
+  // If the producer of operand 'source' is another 'tensor.reshape' op, use the
+  // producer's input instead as the original tensor to reshape. This could
+  // render such producer dead code.
+  if (auto reshapeOpProducer = getSource().getDefiningOp<ReshapeOp>()) {
+    setOperand(0, reshapeOpProducer.getSource());
+    return getResult();
+  }
+
   auto source = getSource();
   auto sourceTy = dyn_cast<RankedTensorType>(source.getType());
   auto resultTy = dyn_cast<RankedTensorType>(getType());
-
   if (!sourceTy || !resultTy || sourceTy != resultTy)
     return {};
 
+  // If the source and result are both 1D tensors and have the same type, the
+  // reshape has no effect, even if the tensor if dynamically shaped.
----------------
sabauma wrote:

Spelling: even if the tensor **is** dynamically shaped.

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


More information about the Mlir-commits mailing list