Skip to content

Commit 5e85a73

Browse files
author
git apple-llvm automerger
committed
Merge commit '06d6cbbb2e06' from apple/main into swift/next
2 parents 9a55002 + 06d6cbb commit 5e85a73

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

mlir/lib/Dialect/Linalg/Transforms/DropUnitDims.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@ getReassociationMapForFoldingUnitDims(ArrayRef<OpFoldResult> mixedSizes) {
459459
reassociation.emplace_back(ReassociationIndices{});
460460
std::swap(reassociation.back(), curr);
461461
}
462-
if (!curr.empty())
462+
// When the reassociations are not empty, then fold the remaining
463+
// unit-dimensions into the last dimension. If the reassociations so far is
464+
// empty, then leave it emtpy. This will fold everything to a rank-0 tensor.
465+
if (!curr.empty() && !reassociation.empty())
463466
reassociation.back().append(curr.begin(), curr.end());
464467
return reassociation;
465468
}

mlir/test/Dialect/Linalg/drop-unit-extent-dims.mlir

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,3 +496,27 @@ func @unit_dim_for_reduction_inner(%arg0: tensor<?x1x?x1xf32>) -> tensor<?x1xf32
496496
// CHECK-SAME: outs(%[[FILL]] : tensor<?xf32>)
497497
// CHECK: %[[RESULT_RESHAPE:.+]] = linalg.tensor_reshape %[[RESULT]] {{\[}}[0, 1]]
498498
// CHECK: return %[[RESULT_RESHAPE]]
499+
500+
// -----
501+
502+
func @subtensor_unit_dims(%arg0: tensor<1x3xf32>) -> tensor<1x1xf32> {
503+
%0 = subtensor %arg0[0, 2] [1, 1] [1, 1] : tensor<1x3xf32> to tensor<1x1xf32>
504+
return %0 : tensor<1x1xf32>
505+
}
506+
// CHECK-LABEL: func @subtensor_unit_dims
507+
// CHECK: %[[SUBTENSOR:.+]] = subtensor
508+
// CHECK-SAME: tensor<1x3xf32> to tensor<f32>
509+
// CHECK: %[[RESULT:.+]] = linalg.tensor_reshape %[[SUBTENSOR]] []
510+
// CHECK: return %[[RESULT]]
511+
512+
// -----
513+
514+
func @subtensor_insert_unit_dims(%arg0: tensor<1x3xf32>, %arg1: tensor<1x1xf32>) -> tensor<1x3xf32> {
515+
%0 = subtensor_insert %arg1 into %arg0[0, 2] [1, 1] [1, 1] : tensor<1x1xf32> into tensor<1x3xf32>
516+
return %0 : tensor<1x3xf32>
517+
}
518+
// CHECK-LABEL: func @subtensor_insert_unit_dims
519+
// CHECK: %[[RESHAPE:.+]] = linalg.tensor_reshape %{{.+}} []
520+
// CHECK: %[[RESULT:.+]] = subtensor_insert %[[RESHAPE]]
521+
// CHECK-SAME: tensor<f32> into tensor<1x3xf32>
522+
// CHECK: return %[[RESULT]]

0 commit comments

Comments
 (0)