Skip to content

Commit c44404b

Browse files
committed
[MLIR][Conversion] Vector to LLVM: Remove unneeded vectorshuffle
if vector.broadcast source is a scalar and target is a single element 1D vector.
1 parent 27e2d5c commit c44404b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1723,12 +1723,16 @@ struct VectorBroadcastScalarToLowRankLowering
17231723
return success();
17241724
}
17251725

1726-
// For 1-d vector, we additionally do a `vectorshuffle`.
1726+
// For 1-d vector, we additionally do a `vectorshuffle` if vector width > 1.
17271727
auto v =
17281728
LLVM::InsertElementOp::create(rewriter, broadcast.getLoc(), vectorType,
17291729
poison, adaptor.getSource(), zero);
17301730

17311731
int64_t width = cast<VectorType>(broadcast.getType()).getDimSize(0);
1732+
if (width == 1) {
1733+
rewriter.replaceOp(broadcast, v);
1734+
return success();
1735+
}
17321736
SmallVector<int32_t> zeroValues(width, 0);
17331737

17341738
// Shuffle the value across the desired number of elements.

mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ func.func @broadcast_vec1d_from_f32(%arg0: f32) -> vector<2xf32> {
7676

7777
// -----
7878

79+
func.func @broadcast_single_elem_vec1d_from_f32(%arg0: f32) -> vector<1xf32> {
80+
%0 = vector.broadcast %arg0 : f32 to vector<1xf32>
81+
return %0 : vector<1xf32>
82+
}
83+
// CHECK-LABEL: @broadcast_single_elem_vec1d_from_f32
84+
// CHECK-SAME: %[[A:.*]]: f32)
85+
// CHECK: %[[T0:.*]] = llvm.insertelement %[[A]]
86+
// CHECK: return %[[T0]] : vector<1xf32>
87+
88+
// -----
89+
7990
func.func @broadcast_vec1d_from_f32_scalable(%arg0: f32) -> vector<[2]xf32> {
8091
%0 = vector.broadcast %arg0 : f32 to vector<[2]xf32>
8192
return %0 : vector<[2]xf32>

0 commit comments

Comments
 (0)