Skip to content

Commit 61bca8b

Browse files
[MLIR][MHLO] Generalize extent tensor cast elimination in bcast moving
PiperOrigin-RevId: 370112887 Change-Id: Ice514670af35f2fd72c849cab8a8d0a9e48d4355
1 parent 1e922cc commit 61bca8b

2 files changed

Lines changed: 11 additions & 26 deletions

File tree

‎tensorflow/compiler/mlir/hlo/lib/Dialect/mhlo/transforms/move_up_dynamic_broadcasts_for_fusion.cc‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,28 +305,27 @@ struct MergeAssumingOpsPattern : public OpRewritePattern<shape::AssumingOp> {
305305
}
306306
};
307307

308-
// Eliminate extent tensor casts. Instead, produce the concrete extent tensor
308+
// Eliminate casted extent tensors. Instead, produce the concrete extent tensor
309309
// type where possible.
310-
template <typename OpTy>
311-
struct CanonicalizeCastedExtentTensorOpPattern
310+
struct CanonicalizeCastedShapeOfOpPattern
312311
: public OpRewritePattern<tensor::CastOp> {
313312
using OpRewritePattern<tensor::CastOp>::OpRewritePattern;
314313

315314
LogicalResult matchAndRewrite(tensor::CastOp op,
316315
PatternRewriter &rewriter) const override {
317-
// Only merge tensor cast into a producer op if we know it supports it.
318-
auto producer_op = op.source().getDefiningOp<OpTy>();
319-
if (!producer_op) return failure();
316+
// Only merge tensor cast into `shape_of` ops.
317+
auto shape_of_op = op.source().getDefiningOp<shape::ShapeOfOp>();
318+
if (!shape_of_op) return failure();
320319

321320
// Desired type must be an extent tensor type.
322321
auto result_ty = op.getType().dyn_cast<RankedTensorType>();
323322
if (!result_ty || result_ty.getRank() != 1 ||
324323
!result_ty.getElementType().isIndex())
325324
return failure();
326325

327-
rewriter.replaceOpWithNewOp<OpTy>(op, result_ty, producer_op->getOperands(),
328-
producer_op->getAttrs());
329-
if (producer_op->getUses().empty()) rewriter.eraseOp(producer_op);
326+
rewriter.replaceOpWithNewOp<shape::ShapeOfOp>(op, result_ty,
327+
shape_of_op.arg());
328+
if (shape_of_op->getUses().empty()) rewriter.eraseOp(shape_of_op);
330329
return success();
331330
}
332331
};
@@ -402,8 +401,7 @@ void PopulateMoveUpDynamicBroadcastsForFusionPatterns(
402401
MLIRContext *context, OwningRewritePatternList *patterns) {
403402
// clang-format off
404403
patterns->insert<
405-
CanonicalizeCastedExtentTensorOpPattern<shape::ShapeOfOp>,
406-
CanonicalizeCastedExtentTensorOpPattern<shape::BroadcastOp>,
404+
CanonicalizeCastedShapeOfOpPattern,
407405
InlineBroadcastedShapeOperandsPattern<shape::CstrBroadcastableOp>,
408406
MergeAssumingOpsPattern,
409407
MoveIntoAssumingOpPattern<shape::ShapeOfOp>,

‎tensorflow/compiler/mlir/hlo/tests/move_up_dynamic_broadcasts_for_fusion.mlir‎

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,9 @@ func @do_not_merge_assuming_ops() {
311311

312312
// -----
313313

314-
// CHECK: @merge_extent_tensor_cast_into_shape_of
314+
// CHECK: @eliminate_extent_tensor_cast
315315
// CHECK-SAME: (%[[ARG:.*]]: tensor<2x?x4xf32>)
316-
func @merge_extent_tensor_cast_into_shape_of(%arg : tensor<2x?x4xf32>) {
316+
func @eliminate_extent_tensor_cast(%arg : tensor<2x?x4xf32>) {
317317
// CHECK-NOT: shape_of
318318
// CHECK: %[[RESULT:.*]] = shape.shape_of %[[ARG]] : tensor<2x?x4xf32> -> tensor<3xindex>
319319
// CHECK-NEXT: "use"(%[[RESULT]]) : (tensor<3xindex>) -> ()
@@ -325,19 +325,6 @@ func @merge_extent_tensor_cast_into_shape_of(%arg : tensor<2x?x4xf32>) {
325325

326326
// -----
327327

328-
// CHECK: @merge_extent_tensor_cast_into_broadcast
329-
// CHECK-SAME: (%[[ARG0:.*]]: tensor<3xindex>, %[[ARG1:.*]]: tensor<3xindex>)
330-
func @merge_extent_tensor_cast_into_broadcast(%arg0 : tensor<3xindex>, %arg1 : tensor<3xindex>) {
331-
// CHECK: %[[RESULT:.*]] = shape.broadcast %[[ARG0]], %[[ARG1]] : tensor<3xindex>, tensor<3xindex> -> tensor<3xindex>
332-
// CHECK: "use"(%[[RESULT]]) : (tensor<3xindex>) -> ()
333-
%0 = shape.broadcast %arg0, %arg1 : tensor<3xindex>, tensor<3xindex> -> tensor<?xindex>
334-
%1 = tensor.cast %0 : tensor<?xindex> to tensor<3xindex>
335-
"use"(%1) : (tensor<3xindex>) -> ()
336-
return
337-
}
338-
339-
// -----
340-
341328
// Exemplary IR as it appears in the lowering of two subsequent `tf.Sub` ops.
342329
// CHECK-LABEL: @sub_sub
343330
// CHECK-SAME: (%[[ARG0:.*]]: tensor<?x32xf16>, %[[ARG1:.*]]: tensor<?x32xf16>, %[[ARG2:.*]]: tensor<?x?x32xf16>)

0 commit comments

Comments
 (0)