Skip to content

Commit 33a4c1a

Browse files
Stephan Herhuttensorflower-gardener
authored andcommitted
Fix mhlo to lmhlo conversion for ReduceOp.
The existing conversion no longer worked and was not save to undo. Furthermore, the pattern for mhlo.return had been removed. Also adds some tests to ensure this does not degrade again. PiperOrigin-RevId: 321542071 Change-Id: I304db0278a4b7bead2543d7ae4308be9a789f3e4
1 parent c2c96f6 commit 33a4c1a

3 files changed

Lines changed: 63 additions & 21 deletions

File tree

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ namespace {
4242

4343
template <typename T>
4444
using BaseOpConversion = BufferAssignmentOpConversionPattern<T>;
45-
using StdReturnOpConverter =
46-
detail::BufferAssignmentReturnOpConverter<mlir::ReturnOp, mlir::ReturnOp,
47-
lmhlo::CopyOp, true>;
4845

4946
Value InsertDynamicAllocAndDealloc(Location loc, Value result,
5047
Value shape_operand,
@@ -272,34 +269,34 @@ struct HloToLhloReduceOpConverter : public BaseOpConversion<mhlo::ReduceOp> {
272269
// Copy over the operations inside the region.
273270
rewriter.inlineRegionBefore(op.body(), new_op.body(), new_op.body().end());
274271

275-
// Create new block arguments with correct type.
272+
// Convert the region signature to memref and add extra result.
276273
auto& entry_block = new_op.body().front();
277-
int original_arg_count = entry_block.getNumArguments();
278-
for (int i = 0; i < original_arg_count; ++i) {
279-
auto old_arg = entry_block.getArgument(i);
280-
auto old_type = old_arg.getType().cast<TensorType>();
274+
TypeConverter::SignatureConversion sig_conversion(
275+
entry_block.getNumArguments() + 1);
276+
for (auto arg : entry_block.getArguments()) {
277+
auto old_type = arg.getType().cast<TensorType>();
281278
auto new_type =
282279
MemRefType::get(old_type.getShape(), old_type.getElementType());
283-
auto new_arg = entry_block.addArgument(new_type);
284-
rewriter.replaceUsesOfBlockArgument(old_arg, new_arg);
280+
sig_conversion.addInputs(arg.getArgNumber(), new_type);
285281
}
286-
// Add an argument for the result.
287-
entry_block.addArgument(
288-
entry_block.getArgument(original_arg_count).getType());
289-
// Remove the old arguments.
290-
for (int i = original_arg_count - 1; i >= 0; --i) {
291-
entry_block.eraseArgument(i);
292-
}
293-
// Insert terminator at the end.
294-
rewriter.setInsertionPointToEnd(&entry_block);
295-
rewriter.create<lmhlo::TerminatorOp>(loc);
282+
auto return_op = cast<mhlo::ReturnOp>(entry_block.getTerminator());
283+
auto result_type = return_op.results().front().getType().cast<TensorType>();
284+
sig_conversion.addInputs({MemRefType::get(result_type.getShape(),
285+
result_type.getElementType())});
286+
rewriter.applySignatureConversion(&new_op.body(), sig_conversion);
296287

297288
rewriter.replaceOp(op, ArrayRef<Value>(buffer_args).slice(operands.size()));
298289

299290
return success();
300291
}
301292
};
302293

294+
// Legalize mhlo.return to a lmhlo.copy and lmhlo.terminator. This functionality
295+
// is provided by mlir buffer assignment, so use the pattern from there.
296+
// TODO(DFKI): Move this out of detail.
297+
using HloToLhloReturnOpConverter = detail::BufferAssignmentReturnOpConverter<
298+
mhlo::ReturnOp, lmhlo::TerminatorOp, lmhlo::CopyOp, false>;
299+
303300
class HloToLhloTensorLoadOpConverter
304301
: public BaseOpConversion<mlir::TensorLoadOp> {
305302
public:
@@ -312,7 +309,6 @@ class HloToLhloTensorLoadOpConverter
312309
}
313310
};
314311

315-
// TODO(b/137624192): Rewrite into a copy and elide copy if possible.
316312
class HloToLhloTensorStoreOpConverter
317313
: public BaseOpConversion<mlir::TensorStoreOp> {
318314
public:
@@ -506,6 +502,7 @@ void populateHLOToLHLOConversionPattern(
506502
HloToLhloOpConverter<mhlo::SubOp>,
507503
HloToLhloOpConverter<mhlo::TanhOp>,
508504
HloToLhloReduceOpConverter,
505+
HloToLhloReturnOpConverter,
509506
HloToLhloTensorLoadOpConverter,
510507
HloToLhloTensorStoreOpConverter
511508
>(context, bufferAssignment, converter);

‎tensorflow/compiler/mlir/hlo/tests/hlo-legalize-to-lhlo.mlir‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,3 +487,26 @@ func @conv(%input: tensor<3x5x5x3xf32>, %filter : tensor<2x2x3x4xf32>) -> tensor
487487
} : (tensor<2x2x3x4xf32>, tensor<3x5x5x3xf32>) -> tensor<3x5x5x4xf32>
488488
return %out : tensor<3x5x5x4xf32>
489489
}
490+
491+
// -----
492+
493+
// BOTH-LABEL: func @reduce
494+
func @reduce(%arg0: tensor<1x8xf32>, %arg1: tensor<f32>) -> tensor<1xf32> {
495+
// BOTH: %[[OUT:.*]] = alloc() : memref<1xf32>
496+
// BOTH: "lmhlo.reduce"(%{{.+}}, %{{.+}}, %[[OUT]]) ( {
497+
// BOTH: ^bb0(%[[ARG1:.*]]: memref<f32>, %[[ARG2:.*]]: memref<f32>,
498+
// BOTH-SAME: %[[ARG3:.*]]: memref<f32>):
499+
// BOTH: %[[TMP:.*]] = alloc() : memref<f32>
500+
// BOTH: "lmhlo.add"(%[[ARG1]], %[[ARG2]], %[[TMP]])
501+
// BOTH: "lmhlo.copy"(%[[TMP]], %[[ARG3]])
502+
// BOTH: "lmhlo.terminator"() : () -> ()
503+
// BOTH: }) {dimensions = dense<1> : tensor<1xi64>}
504+
// BOTH-SAME: : (memref<1x8xf32>, memref<f32>, memref<1xf32>) -> ()
505+
%0 = "mhlo.reduce"(%arg0, %arg1) ( {
506+
^bb0(%arg2: tensor<f32>, %arg3: tensor<f32>): // no predecessors
507+
%1 = mhlo.add %arg2, %arg3 : tensor<f32>
508+
"mhlo.return"(%1) : (tensor<f32>) -> ()
509+
}) {dimensions = dense<1> : tensor<1xi64>}
510+
: (tensor<1x8xf32>, tensor<f32>) -> tensor<1xf32>
511+
return %0 : tensor<1xf32>
512+
}

‎tensorflow/compiler/mlir/hlo/tests/lhlo-copy-removal.mlir‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,25 @@ func @must_be_removed_second(%arg0: memref<2x2xf32>,
9191
dealloc %0 : memref<2x2xf32>
9292
"lmhlo.terminator"() : () -> ()
9393
}
94+
95+
// -----
96+
97+
// CHECK-LABEL: func @reduce
98+
func @reduce(%arg0: memref<1x8xf32>, %arg1: memref<f32>, %arg2: memref<1xf32>) {
99+
%0 = alloc() : memref<1xf32>
100+
"lmhlo.reduce"(%arg0, %arg1, %0) ( {
101+
// CHECK: ^bb0(%[[ARG0:.*]]: memref<f32>, %[[ARG1:.*]]: memref<f32>,
102+
// CHECK-SAME: %[[ARG2:.*]]: memref<f32>)
103+
^bb0(%arg3: memref<f32>, %arg4: memref<f32>, %arg5: memref<f32>):
104+
%1 = alloc() : memref<f32>
105+
// CHECK: "lmhlo.add"(%[[ARG0]], %[[ARG1]], %[[ARG2]])
106+
"lmhlo.add"(%arg3, %arg4, %1)
107+
: (memref<f32>, memref<f32>, memref<f32>) -> ()
108+
// CHECK-NOT; lmhlo.copy
109+
"lmhlo.copy"(%1, %arg5) : (memref<f32>, memref<f32>) -> ()
110+
"lmhlo.terminator"() : () -> ()
111+
}) {dimensions = dense<1> : tensor<1xi64>}
112+
: (memref<1x8xf32>, memref<f32>, memref<1xf32>) -> ()
113+
"lmhlo.copy"(%0, %arg2) : (memref<1xf32>, memref<1xf32>) -> ()
114+
return
115+
}

0 commit comments

Comments
 (0)