[Mlir-commits] [mlir] [mlir][tensor] Loosen restrictions on folding dynamic reshapes (PR #137963)

Ian Wood llvmlistbot at llvm.org
Wed Apr 30 11:05:14 PDT 2025


================
@@ -31,59 +31,70 @@ mlir::getReassociationIndicesForReshape(ShapedType sourceType,
 std::optional<SmallVector<ReassociationIndices>>
 mlir::getReassociationIndicesForCollapse(ArrayRef<int64_t> sourceShape,
                                          ArrayRef<int64_t> targetShape) {
-  if (sourceShape.size() <= targetShape.size())
+  unsigned numSourceDims = sourceShape.size(),
+           numTargetDims = targetShape.size();
+  if (numSourceDims <= numTargetDims)
     return std::nullopt;
-  unsigned sourceDim = 0;
-  SmallVector<ReassociationIndices> reassociationMap;
-  reassociationMap.reserve(targetShape.size());
-
-  ReassociationIndices currIndices;
-  int64_t prodOfCollapsedDims = 1;
-  while (sourceDim < sourceShape.size()) {
-    unsigned targetDim = reassociationMap.size();
-    // If we have mapped all the target dimensions stop and handle the remaining
-    // tail of size-1 dimensions explicitly.
-    if (targetDim == targetShape.size())
-      break;
+  SmallVector<ReassociationIndices, 4> reassociationMap;
+  reassociationMap.reserve(numTargetDims);
 
+  unsigned sourceDim = 0, targetDim = 0;
+  for (; targetDim < numTargetDims; ++targetDim) {
     int64_t currTargetShape = targetShape[targetDim];
-    while (sourceDim < (sourceShape.size() - 1) &&
-           sourceShape[sourceDim] != ShapedType::kDynamic &&
-           prodOfCollapsedDims * sourceShape[sourceDim] < currTargetShape) {
+    ReassociationIndices currIndices;
+    // 1. Target dimension is dynamic. Source shape should contain at least
+    // one dynamic dimension.
+    if (currTargetShape == ShapedType::kDynamic) {
----------------
IanWood1 wrote:

There may be a pre-existing issue here: we should bail out when collapsing shapes that contain two or more adjacent dynamic dimensions, since the reassociation becomes indeterminate (as mentioned in the code comment below).

For example, the reshape ?x?x? -> ?x? could validly map to either:
- [[0], [1, 2]], or
- [[0, 1], [2]]

Both are valid so the reassoc cannot be determined. Here's [a gist with mlir testcases](https://gist.github.com/IanWood1/6671b9f105f6c6993b998f0a14fa770b).

As far as I can tell, if the output contains no adjacent dynamic dims the reassociation should be uniquely inferable. In that case, it might be worth considering a backtracking algorithm to fully generalize `getReassociationIndicesForCollapse`.

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


More information about the Mlir-commits mailing list