@@ -42,9 +42,6 @@ namespace {
4242
4343template <typename T>
4444using BaseOpConversion = BufferAssignmentOpConversionPattern<T>;
45- using StdReturnOpConverter =
46- detail::BufferAssignmentReturnOpConverter<mlir::ReturnOp, mlir::ReturnOp,
47- lmhlo::CopyOp, true >;
4845
4946Value 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+
303300class 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.
316312class 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);
0 commit comments