[DispatchCreation] Preserve dynamic reshape types when folding unit dimensions (#25179)

`DropUnitDimsFromCollapseOfExpand` derived the new `expand_shape` result
type
from `getConstantIntValue` on the `output_shape` operands. When a
dimension was
dynamic in the type but supplied by an `arith.constant`, the new type
became
static while the value was still passed as a dynamic operand, producing
invalid
IR. Take the dims from the original result type instead, which keeps
type and operands consistent.

Co-authored-by: Ege Beysel <beyselege@gmail.com>

Co-authored-by: Ege Beysel <beyselege@gmail.com>
diff --git a/compiler/src/iree/compiler/DispatchCreation/FoldUnitExtentDims.cpp b/compiler/src/iree/compiler/DispatchCreation/FoldUnitExtentDims.cpp
index 2136f7c..db8a2c3 100644
--- a/compiler/src/iree/compiler/DispatchCreation/FoldUnitExtentDims.cpp
+++ b/compiler/src/iree/compiler/DispatchCreation/FoldUnitExtentDims.cpp
@@ -127,8 +127,7 @@
         expandOp.getStaticOutputShape(), expandOp.getOutputShape(), rewriter);
     for (auto [idx, ofr] : llvm::enumerate(origInterSizes)) {
       if (!toDrop.contains(idx)) {
-        std::optional<int64_t> staticDim = getConstantIntValue(ofr);
-        newInterShape.push_back(staticDim.value_or(ShapedType::kDynamic));
+        newInterShape.push_back(interShape[idx]);
         newInterSizes.push_back(ofr);
       }
     }
diff --git a/compiler/src/iree/compiler/DispatchCreation/test/fold_unit_dims.mlir b/compiler/src/iree/compiler/DispatchCreation/test/fold_unit_dims.mlir
index a93d4a1..af4876b 100644
--- a/compiler/src/iree/compiler/DispatchCreation/test/fold_unit_dims.mlir
+++ b/compiler/src/iree/compiler/DispatchCreation/test/fold_unit_dims.mlir
@@ -802,3 +802,34 @@
 // between.
 //       CHECK:   linalg.generic
 //  CHECK-SAME:       ins(%[[DQ]],
+
+// -----
+
+// A constant value may supply a dimension whose tensor type is still dynamic.
+// Folding unit dimensions must preserve that type while shortening the reshape.
+util.func public @fold_constant_dynamic_size(%arg0: tensor<?xf32>) -> tensor<?x784xf32> {
+  %c1 = arith.constant 1 : index
+  %expanded = tensor.expand_shape %arg0 [[0, 1, 2, 3]] output_shape [%c1, 1, 1, 784] : tensor<?xf32> into tensor<?x1x1x784xf32>
+  %collapsed = tensor.collapse_shape %expanded [[0, 1, 2], [3]] : tensor<?x1x1x784xf32> into tensor<?x784xf32>
+  util.return %collapsed : tensor<?x784xf32>
+}
+// CHECK-LABEL: util.func public @fold_constant_dynamic_size
+//  CHECK-SAME:     %[[ARG0:[a-zA-Z0-9]+]]: tensor<?xf32>
+//       CHECK:   %[[C1:.+]] = arith.constant 1 : index
+//       CHECK:   %[[EXPANDED:.+]] = tensor.expand_shape %[[ARG0]] {{\[}}[0, 1]] output_shape [%[[C1]], 784] : tensor<?xf32> into tensor<?x784xf32>
+//   CHECK-NOT:   tensor.collapse_shape
+//       CHECK:   util.return %[[EXPANDED]] : tensor<?x784xf32>
+
+// -----
+
+// non constant size stays dynamic
+util.func public @preserve_unknown_dynamic_size(%arg0: tensor<?xf32>, %size: index) -> tensor<?x784xf32> {
+  %expanded = tensor.expand_shape %arg0 [[0, 1, 2, 3]] output_shape [%size, 1, 1, 784] : tensor<?xf32> into tensor<?x1x1x784xf32>
+  %collapsed = tensor.collapse_shape %expanded [[0, 1, 2], [3]] : tensor<?x1x1x784xf32> into tensor<?x784xf32>
+  util.return %collapsed : tensor<?x784xf32>
+}
+// CHECK-LABEL: util.func public @preserve_unknown_dynamic_size
+//  CHECK-SAME:     %[[ARG0:[a-zA-Z0-9]+]]: tensor<?xf32>, %[[SIZE:[a-zA-Z0-9]+]]: index
+//       CHECK:   %[[EXPANDED:.+]] = tensor.expand_shape %[[ARG0]] {{\[}}[0, 1]] output_shape [%[[SIZE]], 784] : tensor<?xf32> into tensor<?x784xf32>
+//   CHECK-NOT:   tensor.collapse_shape
+//       CHECK:   util.return %[[EXPANDED]] : tensor<?x784xf32>