From fdb653788c28d3ec88c5d3fb7c18fc1043b743b2 Mon Sep 17 00:00:00 2001 From: Hanhan Wang Date: Fri, 9 Apr 2021 07:07:41 -0700 Subject: [PATCH] Add support for lowering and/or within mhlo.reduce op body. PiperOrigin-RevId: 367627034 --- .../mhlo/transforms/legalize_to_linalg.cc | 2 + tests/hlo-legalize-to-linalg.mlir | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lib/Dialect/mhlo/transforms/legalize_to_linalg.cc b/lib/Dialect/mhlo/transforms/legalize_to_linalg.cc index 36a7346..850ef00 100644 --- a/lib/Dialect/mhlo/transforms/legalize_to_linalg.cc +++ b/lib/Dialect/mhlo/transforms/legalize_to_linalg.cc @@ -2107,6 +2107,8 @@ void populateHLOToLinalgConversionPattern(MLIRContext* context, patterns->insert, ReduceRegionXLAOpConversion, ReduceRegionXLAOpConversion, + ReduceRegionXLAOpConversion, + ReduceRegionXLAOpConversion, ReduceRegionReturnOpConversion>(context); } diff --git a/tests/hlo-legalize-to-linalg.mlir b/tests/hlo-legalize-to-linalg.mlir index 457147d..a3eb16b 100644 --- a/tests/hlo-legalize-to-linalg.mlir +++ b/tests/hlo-legalize-to-linalg.mlir @@ -1352,6 +1352,56 @@ func @reduce_maximum(%arg0: tensor<5x4xi32>, %arg1: tensor) -> tensor<5xi32 // ----- +func @reduce_and(%arg0: tensor<5x4xi1>, %arg1: tensor) -> tensor<5xi1> { + %0 = "mhlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg3: tensor, %arg4 : tensor): + %1 = mhlo.and %arg3, %arg4 : tensor + "mhlo.return"(%1) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<5x4xi1>, tensor) -> tensor<5xi1> + return %0 : tensor<5xi1> +} +// CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0, d1) -> (d0, d1)> +// CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0, d1) -> (d0)> +// CHECK-LABEL: @reduce_and +// CHECK-DAG: %[[INIT:.*]] = tensor.extract %{{.*}} : tensor +// CHECK-DAG: %[[INIT_TENSOR:.*]] = linalg.init_tensor [5] +// CHECK-DAG: %[[FILL_TENSOR:.*]] = linalg.fill(%[[INIT_TENSOR]], %[[INIT]]) +// CHECK: linalg.generic +// CHECK-SAME: indexing_maps = [#[[MAP0]], #[[MAP1]]] +// CHECK-SAME: iterator_types = ["parallel", "reduction"] +// CHECK-SAME: ins(%{{.*}}tensor<5x4xi1>) +// CHECK-SAME: outs(%[[FILL_TENSOR]] : tensor<5xi1>) +// CHECK-NEXT: ^bb0(%[[LHS_IN:.*]]: i1, %[[RHS_IN:.*]]: i1): +// CHECK-NEXT: %[[RESULT:.*]] = and %[[LHS_IN]], %[[RHS_IN]] : i1 +// CHECK-NEXT: linalg.yield %[[RESULT]] : i1 + +// ----- + +func @reduce_or(%arg0: tensor<5x4xi1>, %arg1: tensor) -> tensor<5xi1> { + %0 = "mhlo.reduce"(%arg0, %arg1) ({ + ^bb0(%arg3: tensor, %arg4 : tensor): + %1 = mhlo.or %arg3, %arg4 : tensor + "mhlo.return"(%1) : (tensor) -> () + }) {dimensions = dense<1> : tensor<1xi64>} : (tensor<5x4xi1>, tensor) -> tensor<5xi1> + return %0 : tensor<5xi1> +} +// CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0, d1) -> (d0, d1)> +// CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0, d1) -> (d0)> +// CHECK-LABEL: @reduce_or +// CHECK-DAG: %[[INIT:.*]] = tensor.extract %{{.*}} : tensor +// CHECK-DAG: %[[INIT_TENSOR:.*]] = linalg.init_tensor [5] +// CHECK-DAG: %[[FILL_TENSOR:.*]] = linalg.fill(%[[INIT_TENSOR]], %[[INIT]]) +// CHECK: linalg.generic +// CHECK-SAME: indexing_maps = [#[[MAP0]], #[[MAP1]]] +// CHECK-SAME: iterator_types = ["parallel", "reduction"] +// CHECK-SAME: ins(%{{.*}}tensor<5x4xi1>) +// CHECK-SAME: outs(%[[FILL_TENSOR]] : tensor<5xi1>) +// CHECK-NEXT: ^bb0(%[[LHS_IN:.*]]: i1, %[[RHS_IN:.*]]: i1): +// CHECK-NEXT: %[[RESULT:.*]] = or %[[LHS_IN]], %[[RHS_IN]] : i1 +// CHECK-NEXT: linalg.yield %[[RESULT]] : i1 + +// ----- + func @reduce_dim0(%arg0: tensor<5x4xi32>, %arg1: tensor) -> tensor<4xi32> { %0 = "mhlo.reduce"(%arg0, %arg1) ({ ^bb0(%arg3: tensor, %arg4 : tensor):