Skip to content

Commit f28f09d

Browse files
[mlir][Vector] Add Broadcast -> CastOp reordering to SinkVectorBroadcasting patterns. (llvm#68257)
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. Similar issue with `vector.fma` which only accepts vector operand types, while broadcasts can have scalar sources. Sinking broadcast across would result in an illegal `vector.fma` (with scalar operands).
1 parent 1b3fc40 commit f28f09d

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,15 @@ struct ReorderElementwiseOpsOnBroadcast final
912912
return failure();
913913
if (!OpTrait::hasElementwiseMappableTraits(op))
914914
return failure();
915+
if (op->getNumOperands() == 0 ||
916+
op->getResults()[0].getType() != op->getOperand(0).getType()) {
917+
return failure();
918+
}
919+
// Avoid operations that only accept vector types, since broadcast
920+
// source might be scalar types.
921+
if (isa<vector::FMAOp>(op)) {
922+
return failure();
923+
}
915924

916925
// Get the type of the lhs operand
917926
auto *lhsBcastOrSplat = op->getOperand(0).getDefiningOp();
@@ -1447,8 +1456,8 @@ void mlir::vector::
14471456

14481457
void mlir::vector::populateSinkVectorBroadcastPatterns(
14491458
RewritePatternSet &patterns, PatternBenefit benefit) {
1450-
patterns.add<ReorderElementwiseOpsOnBroadcast>(patterns.getContext(),
1451-
benefit);
1459+
patterns.add<ReorderCastOpsOnBroadcast, ReorderElementwiseOpsOnBroadcast>(
1460+
patterns.getContext(), benefit);
14521461
}
14531462

14541463
//===----------------------------------------------------------------------===//

mlir/test/Dialect/Vector/sink-vector-broadcast.mlir

+20
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,23 @@ func.func @broadcast_not_elementwise() -> vector<2x2xf32> {
105105

106106
return %mm1 : vector<2x2xf32>
107107
}
108+
109+
// CHECK-LABEL: func.func @dont_sink_cmp(
110+
// CHECK: %[[BROADCAST:.+]] = vector.broadcast
111+
// CHECK: %[[RETURN:.+]] = arith.cmpf uno, %[[BROADCAST]], %[[BROADCAST]]
112+
// CHECK: return %[[RETURN]]
113+
func.func @dont_sink_cmp(%arg0 : f32, %arg1 : vector<1xf32>) -> vector<1xi1> {
114+
%0 = vector.broadcast %arg0 : f32 to vector<1xf32>
115+
%1 = arith.cmpf uno, %0, %0 : vector<1xf32>
116+
return %1 : vector<1xi1>
117+
}
118+
119+
// CHECK-LABEL: func.func @dont_sink_fma(
120+
// CHECK: %[[BROADCAST:.+]] = vector.broadcast
121+
// CHECK: %[[RESULT:.+]] = vector.fma %[[BROADCAST]]
122+
// CHECK: return %[[RESULT]]
123+
func.func @dont_sink_fma(%arg0 : f32) -> vector<1xf32> {
124+
%0 = vector.broadcast %arg0 : f32 to vector<1xf32>
125+
%1 = vector.fma %0, %0, %0 : vector<1xf32>
126+
return %1 : vector<1xf32>
127+
}

0 commit comments

Comments
 (0)