@@ -1473,8 +1473,8 @@ struct ReduceRegionXLAOpConversion : public OpConversionPattern<OpTy> {
14731473 })) {
14741474 return failure ();
14751475 }
1476- Value result = lmhlo::HloOpToStdScalarOp::map<OpTy>(op, args[ 0 ]. getType (),
1477- args, &rewriter);
1476+ Value result = lmhlo::HloOpToStdScalarOp::map<OpTy>(
1477+ op, getElementTypeOrSelf (op. getType ()), args, &rewriter);
14781478 rewriter.replaceOp (op, result);
14791479 return success ();
14801480 }
@@ -1518,58 +1518,65 @@ class ReduceOnTensorsConversion : public OpConversionPattern<mhlo::ReduceOp> {
15181518 ConversionPatternRewriter& rewriter) const final {
15191519 Location loc = op.getLoc ();
15201520 mhlo::ReduceOp::Adaptor adaptor (args);
1521- if (op.getNumOperands () != 2 ) {
1522- return op.emitError (" expects exactly two operands" );
1523- }
1524- Value src = adaptor.inputs ()[0 ];
1525- auto src_type = src.getType ().cast <ShapedType>();
1521+
1522+ int num_inputs = static_cast <int >(adaptor.inputs ().size ());
1523+ auto src_type = adaptor.inputs ()[0 ].getType ().cast <ShapedType>();
15261524 int src_rank = src_type.getRank ();
15271525 if (!src_rank) {
15281526 return rewriter.notifyMatchFailure (op, " expects known-rank args" );
15291527 }
15301528
1531- // Check if init_value is constant. If so, inline the value into the region.
1532- Value init_value = adaptor.init_values ()[0 ];
1533- Attribute init_const_val = GetInitValueAsConst (init_value);
1534- if (init_const_val) {
1535- init_value = rewriter.create <ConstantOp>(
1536- init_value.getDefiningOp ()->getLoc (), init_const_val);
1537- } else {
1538- init_value = rewriter.create <tensor::ExtractOp>(loc, init_value);
1529+ SmallVector<int64_t , 4 > reduction_dims = Extract1DVector (op.dimensions ());
1530+
1531+ SmallVector<Value> inputs, outputs;
1532+ SmallVector<AffineMap, 3 > indexing_maps;
1533+ for (int i = 0 ; i < num_inputs; ++i) {
1534+ Value src = adaptor.inputs ()[i];
1535+ if (src.getType () != src_type) return failure ();
1536+
1537+ // Check if init_value is constant. If so, inline the value into the
1538+ // region.
1539+ Value init_value = adaptor.init_values ()[i];
1540+ Attribute init_const_val = GetInitValueAsConst (init_value);
1541+ if (init_const_val) {
1542+ init_value = rewriter.create <ConstantOp>(
1543+ init_value.getDefiningOp ()->getLoc (), init_const_val);
1544+ } else {
1545+ init_value = rewriter.create <tensor::ExtractOp>(loc, init_value);
1546+ }
1547+
1548+ inputs.push_back (src);
1549+ auto result_type = op.getResult (i).getType ().cast <ShapedType>();
1550+ SmallVector<Value, 8 > dyn_shape = GetReduceOpInitTensorDynSizes (
1551+ rewriter, loc, src, result_type, reduction_dims);
1552+ auto init_tensor = GetInitTensor (rewriter, loc, result_type, dyn_shape);
1553+ Value filled_tensor =
1554+ rewriter.create <linalg::FillOp>(loc, init_tensor, init_value)
1555+ .result ();
1556+ outputs.push_back (filled_tensor);
15391557 }
15401558
1541- // Prepare indexing maps for linalg generic op. The elements are for src and
1542- // dst. Transpose `src` to make the reduction loops be the innermost,
1559+ // Prepare indexing maps for linalg generic op. The elements are for src
1560+ // and dst. Transpose `src` to make the reduction loops be the innermost,
15431561 // because it's easier to fully utilize processors.
1544- SmallVector<AffineMap, 3 > indexing_maps;
1545- SmallVector<int64_t , 4 > reduction_dims = Extract1DVector (op.dimensions ());
1546- indexing_maps.emplace_back (GetTransposeMapForReduction (
1547- rewriter.getContext (), src_rank, reduction_dims));
1562+ indexing_maps.append (
1563+ num_inputs, GetTransposeMapForReduction (rewriter.getContext (), src_rank,
1564+ reduction_dims));
15481565
15491566 // The indexing map of `dst` should drop the reduction loops. Since the
15501567 // reduction loops now are all in the innermost, drops
1551- // `reduction_dims.size()` dimensions. We don't need an inverse permutation
1552- // here because they are the same.
1568+ // `reduction_dims.size()` dimensions. We don't need an inverse
1569+ // permutation here because they are the same.
15531570 SmallVector<AffineExpr, 4 > exprs;
15541571 for (int i = 0 , e = src_rank - reduction_dims.size (); i < e; ++i)
15551572 exprs.push_back (rewriter.getAffineDimExpr (i));
1556- indexing_maps.emplace_back (AffineMap::get (src_rank, /* symbolCount=*/ 0 ,
1557- exprs, rewriter.getContext ()));
1558-
1559- SmallVector<Value, 2 > inputs = {adaptor.inputs ()[0 ]};
1560- Type result_type = op.getResult (0 ).getType ();
1561- auto shaped_type = result_type.cast <ShapedType>();
1562- SmallVector<Value, 8 > dyn_shape = GetReduceOpInitTensorDynSizes (
1563- rewriter, loc, adaptor.inputs ()[0 ], result_type.cast <ShapedType>(),
1564- reduction_dims);
1565- auto init_tensor = GetInitTensor (rewriter, loc, shaped_type, dyn_shape);
1566- Value filled_tensor =
1567- rewriter.create <linalg::FillOp>(loc, init_tensor, init_value)
1568- .getResult (0 );
1573+ indexing_maps.append (num_inputs,
1574+ AffineMap::get (src_rank, /* symbolCount=*/ 0 , exprs,
1575+ rewriter.getContext ()));
15691576
15701577 auto linalg_op = rewriter.create <linalg::GenericOp>(
15711578 loc, /* resultTensorTypes=*/ op.getResultTypes (), inputs,
1572- /* outputBuffers=*/ ValueRange{filled_tensor }, indexing_maps,
1579+ /* outputBuffers=*/ ValueRange{outputs }, indexing_maps,
15731580 GetParallelAndReductionIterators (src_rank, reduction_dims.size ()));
15741581
15751582 // Convert the signature of the body. The reduce op region apply function
@@ -1579,10 +1586,10 @@ class ReduceOnTensorsConversion : public OpConversionPattern<mhlo::ReduceOp> {
15791586 // be converted to "(f32, f32, f32)".
15801587 Region& region = linalg_op.region ();
15811588 rewriter.inlineRegionBefore (op.body (), region, region.end ());
1582- TypeConverter::SignatureConversion signatureConverter ( 2 );
1583- signatureConverter. addInputs ( 0 , src_type. getElementType ());
1584- signatureConverter .addInputs (1 , src_type.getElementType ());
1585- rewriter.applySignatureConversion (®ion, signatureConverter );
1589+ TypeConverter::SignatureConversion signature_converter (num_inputs * 2 );
1590+ for ( int i = 0 ; i < num_inputs * 2 ; ++i)
1591+ signature_converter .addInputs (i , src_type.getElementType ());
1592+ rewriter.applySignatureConversion (®ion, signature_converter );
15861593 rewriter.replaceOp (op, linalg_op.getResults ());
15871594 return success ();
15881595 }
@@ -2272,6 +2279,8 @@ void populateHLOToLinalgConversionPattern(MLIRContext* context,
22722279 ReduceRegionXLAOpConversion<mhlo::MaxOp>,
22732280 ReduceRegionXLAOpConversion<mhlo::AndOp>,
22742281 ReduceRegionXLAOpConversion<mhlo::OrOp>,
2282+ ReduceRegionXLAOpConversion<mhlo::SelectOp>,
2283+ ReduceRegionXLAOpConversion<mhlo::CompareOp>,
22752284 ReduceRegionReturnOpConversion>(context);
22762285}
22772286
0 commit comments