Skip to content

Commit 0515449

Browse files
authored
[mlir][tensor][memref] Enhance collapse(expand(src)) canonicalization pattern. (#145995)
1 parent 30e519e commit 0515449

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

mlir/include/mlir/Dialect/Utils/ReshapeOpsUtils.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef MLIR_DIALECT_UTILS_RESHAPEOPSUTILS_H
1515
#define MLIR_DIALECT_UTILS_RESHAPEOPSUTILS_H
1616

17+
#include "mlir/Dialect/Arith/IR/Arith.h"
1718
#include "mlir/Dialect/Utils/StaticValueUtils.h"
1819
#include "mlir/IR/OpImplementation.h"
1920
#include "mlir/IR/PatternMatch.h"
@@ -305,8 +306,43 @@ struct ComposeCollapseOfExpandOp : public OpRewritePattern<CollapseOpTy> {
305306
rewriter.replaceOpWithNewOp<CollapseOpTy>(
306307
collapseOp, resultType, expandOp.getSrc(), composedReassociation);
307308
} else if (srcRank < resultRank) {
309+
// Compute the dynamic output shape for the new expand_shape op.
310+
Location loc = collapseOp.getLoc();
311+
SmallVector<OpFoldResult> origOutputShape =
312+
expandOp.getMixedOutputShape();
313+
SmallVector<OpFoldResult> newOutputShape;
314+
for (const ReassociationIndices &indices :
315+
collapseOp.getReassociationIndices()) {
316+
int64_t numStaticElems = 1;
317+
SmallVector<Value> dynamicSizes;
318+
for (int64_t idx : indices) {
319+
OpFoldResult size = origOutputShape[idx];
320+
if (std::optional<int64_t> maybeCst = getConstantIntValue(size)) {
321+
numStaticElems *= maybeCst.value();
322+
continue;
323+
}
324+
dynamicSizes.push_back(cast<Value>(size));
325+
}
326+
if (dynamicSizes.empty()) {
327+
newOutputShape.push_back(rewriter.getIndexAttr(numStaticElems));
328+
continue;
329+
}
330+
331+
// There is at least one dynamic size, so we can initialize `result` to
332+
// the first dynamic size.
333+
Value result = dynamicSizes[0];
334+
for (Value v : llvm::drop_begin(dynamicSizes))
335+
result = rewriter.create<arith::MulIOp>(loc, result, v);
336+
if (numStaticElems != 1) {
337+
result = rewriter.create<arith::MulIOp>(
338+
loc, result,
339+
rewriter.create<arith::ConstantIndexOp>(loc, numStaticElems));
340+
}
341+
newOutputShape.push_back(result);
342+
}
308343
rewriter.replaceOpWithNewOp<ExpandOpTy>(
309-
collapseOp, resultType, expandOp.getSrc(), composedReassociation);
344+
collapseOp, resultType, expandOp.getSrc(), composedReassociation,
345+
newOutputShape);
310346
} else {
311347
// Collapses/expansions that do not change the rank are not allowed. Use
312348
// a cast instead.

mlir/test/Dialect/MemRef/canonicalize.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,24 @@ func.func @compose_collapse_of_collapse(%arg0 : memref<?x?x?x?x?xf32>)
466466

467467
// -----
468468

469+
func.func @compose_collapse_of_expand_partially_dynamic(%arg0: memref<?xf16>, %arg1: index, %arg2: index) -> memref<8x?x?xf16> {
470+
%expanded = memref.expand_shape %arg0 [[0, 1, 2, 3, 4]] output_shape [4, 2, %arg1, %arg2, 32] : memref<?xf16> into memref<4x2x?x?x32xf16>
471+
%collapsed = memref.collapse_shape %expanded [[0, 1], [2], [3, 4]] : memref<4x2x?x?x32xf16> into memref<8x?x?xf16>
472+
return %collapsed : memref<8x?x?xf16>
473+
}
474+
// CHECK: func @compose_collapse_of_expand_partially_dynamic
475+
// CHECK-SAME: %[[SRC:.[a-zA-Z0-9]+]]
476+
// CHECK-SAME: %[[ORIG_D2:.[a-zA-Z0-9]+]]
477+
// CHECK-SAME: %[[ORIG_D3:.[a-zA-Z0-9]+]]
478+
// CHECK-DAG: %[[C32:.+]] = arith.constant 32
479+
// CHECK: %[[COLLAPSED_D2:.+]] = arith.muli %[[ORIG_D3]], %[[C32]]
480+
// CHECK: %[[RESULT:.+]] = memref.expand_shape %[[SRC]]
481+
// CHECK-SAME: [0, 1, 2]
482+
// CHECK-SAME: output_shape [8, %[[ORIG_D2]], %[[COLLAPSED_D2]]]
483+
// CHECK: return %[[RESULT]]
484+
485+
// -----
486+
469487
func.func @do_not_compose_collapse_of_expand_non_identity_layout(
470488
%arg0: memref<?x?xf32, strided<[?, 1], offset: 0>>, %sz0: index, %sz1: index)
471489
-> memref<?xf32, strided<[?], offset: 0>> {

mlir/test/Dialect/Tensor/canonicalize.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,24 @@ func.func @compose_collapse_of_expand_1D(%arg0 : tensor<2048xf32>)
12431243

12441244
// -----
12451245

1246+
func.func @compose_collapse_of_expand_partially_dynamic(%arg0: tensor<?xf16>, %arg1: index, %arg2: index) -> tensor<8x?x?xf16> {
1247+
%expanded = tensor.expand_shape %arg0 [[0, 1, 2, 3, 4]] output_shape [4, 2, %arg1, %arg2, 32] : tensor<?xf16> into tensor<4x2x?x?x32xf16>
1248+
%collapsed = tensor.collapse_shape %expanded [[0, 1], [2], [3, 4]] : tensor<4x2x?x?x32xf16> into tensor<8x?x?xf16>
1249+
return %collapsed : tensor<8x?x?xf16>
1250+
}
1251+
// CHECK: func @compose_collapse_of_expand_partially_dynamic
1252+
// CHECK-SAME: %[[SRC:.[a-zA-Z0-9]+]]
1253+
// CHECK-SAME: %[[ORIG_D2:.[a-zA-Z0-9]+]]
1254+
// CHECK-SAME: %[[ORIG_D3:.[a-zA-Z0-9]+]]
1255+
// CHECK-DAG: %[[C32:.+]] = arith.constant 32
1256+
// CHECK: %[[COLLAPSED_D2:.+]] = arith.muli %[[ORIG_D3]], %[[C32]]
1257+
// CHECK: %[[RESULT:.+]] = tensor.expand_shape %[[SRC]]
1258+
// CHECK-SAME: [0, 1, 2]
1259+
// CHECK-SAME: output_shape [8, %[[ORIG_D2]], %[[COLLAPSED_D2]]]
1260+
// CHECK: return %[[RESULT]]
1261+
1262+
// -----
1263+
12461264
func.func @compose_expand_of_collapse_0_rank_to_expand(%arg0 : tensor<1x1x1xf32>)
12471265
-> tensor<1x1x1x1xf32> {
12481266
%0 = tensor.collapse_shape %arg0 []

0 commit comments

Comments
 (0)