[Mlir-commits] [mlir] [mlir][Vector] Add Broadcast -> CastOp reordering to SinkVectorBroadcasting patterns. (PR #68257)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Oct 4 19:56:09 PDT 2023


https://github.com/MaheshRavishankar updated https://github.com/llvm/llvm-project/pull/68257

>From a1e599a5f912b1623cd7c5cab507862eeab593b9 Mon Sep 17 00:00:00 2001
From: MaheshRavishankar <mahesh at nod-labs.com>
Date: Tue, 3 Oct 2023 20:04:41 -0700
Subject: [PATCH] [mlir][Vector] Add Broadcast -> CastOp reordering to
 SinkVectorBroadcasting patterns.

Also fix an issue with sink broadcast across elementwise where
`arith.cmpf` is elementwise, but result type is different. The result
type is not same as the operand type, creating illegal IR.
---
 .../Vector/Transforms/VectorTransforms.cpp    | 13 ++++++++++--
 .../Dialect/Vector/sink-vector-broadcast.mlir | 20 +++++++++++++++++++
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
index 77076090f354e6b..16f9d7e4d57eef7 100644
--- a/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
+++ b/mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp
@@ -912,6 +912,15 @@ struct ReorderElementwiseOpsOnBroadcast final
       return failure();
     if (!OpTrait::hasElementwiseMappableTraits(op))
       return failure();
+    if (op->getNumOperands() == 0 ||
+        op->getResults()[0].getType() != op->getOperand(0).getType()) {
+      return failure();
+    }
+    // Avoid operations that only accept vector types, since broadcast
+    // source might be scalar types.
+    if (isa<vector::FMAOp>(op)) {
+      return failure();
+    }
 
     // Get the type of the lhs operand
     auto *lhsBcastOrSplat = op->getOperand(0).getDefiningOp();
@@ -1447,8 +1456,8 @@ void mlir::vector::
 
 void mlir::vector::populateSinkVectorBroadcastPatterns(
     RewritePatternSet &patterns, PatternBenefit benefit) {
-  patterns.add<ReorderElementwiseOpsOnBroadcast>(patterns.getContext(),
-                                                 benefit);
+  patterns.add<ReorderCastOpsOnBroadcast, ReorderElementwiseOpsOnBroadcast>(
+      patterns.getContext(), benefit);
 }
 
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Vector/sink-vector-broadcast.mlir b/mlir/test/Dialect/Vector/sink-vector-broadcast.mlir
index d9d2f44e6f16c1f..e7863a9e8b7b780 100644
--- a/mlir/test/Dialect/Vector/sink-vector-broadcast.mlir
+++ b/mlir/test/Dialect/Vector/sink-vector-broadcast.mlir
@@ -105,3 +105,23 @@ func.func @broadcast_not_elementwise() -> vector<2x2xf32> {
 
   return %mm1 : vector<2x2xf32>
 }
+
+// CHECK-LABEL: func.func @dont_sink_cmp(
+//       CHECK:   %[[BROADCAST:.+]] = vector.broadcast
+//       CHECK:   %[[RETURN:.+]] = arith.cmpf uno, %[[BROADCAST]], %[[BROADCAST]]
+//       CHECK:   return %[[RETURN]]
+func.func @dont_sink_cmp(%arg0 : f32, %arg1 : vector<1xf32>) -> vector<1xi1> {
+  %0 = vector.broadcast %arg0 : f32 to vector<1xf32>
+  %1 = arith.cmpf uno, %0, %0 : vector<1xf32>
+  return %1 : vector<1xi1>
+}
+
+// CHECK-LABEL: func.func @dont_sink_fma(
+  //     CHECK:   %[[BROADCAST:.+]] = vector.broadcast
+  //     CHECK:   %[[RESULT:.+]] = vector.fma %[[BROADCAST]]
+  //     CHECK:   return %[[RESULT]]
+func.func @dont_sink_fma(%arg0 : f32) -> vector<1xf32> {
+  %0 = vector.broadcast %arg0 : f32 to vector<1xf32>
+  %1 = vector.fma %0, %0, %0 : vector<1xf32>
+  return %1 : vector<1xf32>
+}



More information about the Mlir-commits mailing list