[Mlir-commits] [mlir] [MLIR][Conversion] Vector to LLVM: Remove unneeded vector shuffle (PR #162946)
Sang Ik Lee
llvmlistbot at llvm.org
Fri Oct 10 17:13:27 PDT 2025
https://github.com/silee2 created https://github.com/llvm/llvm-project/pull/162946
if vector.broadcast source is a scalar and target is a single element 1D vector.
>From c44404b91ff2e99b56fecc613ce21268f6f43109 Mon Sep 17 00:00:00 2001
From: "Lee, Sang Ik" <sang.ik.lee at intel.com>
Date: Sat, 11 Oct 2025 00:09:38 +0000
Subject: [PATCH] [MLIR][Conversion] Vector to LLVM: Remove unneeded
vectorshuffle if vector.broadcast source is a scalar and target is a single
element 1D vector.
---
.../Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp | 6 +++++-
mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir | 11 +++++++++++
2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
index 5355909b62a7f..2bab94f82723e 100644
--- a/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
+++ b/mlir/lib/Conversion/VectorToLLVM/ConvertVectorToLLVM.cpp
@@ -1723,12 +1723,16 @@ struct VectorBroadcastScalarToLowRankLowering
return success();
}
- // For 1-d vector, we additionally do a `vectorshuffle`.
+ // For 1-d vector, we additionally do a `vectorshuffle` if vector width > 1.
auto v =
LLVM::InsertElementOp::create(rewriter, broadcast.getLoc(), vectorType,
poison, adaptor.getSource(), zero);
int64_t width = cast<VectorType>(broadcast.getType()).getDimSize(0);
+ if (width == 1) {
+ rewriter.replaceOp(broadcast, v);
+ return success();
+ }
SmallVector<int32_t> zeroValues(width, 0);
// Shuffle the value across the desired number of elements.
diff --git a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
index 2d33888854ea7..f704b8dba5eed 100644
--- a/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
+++ b/mlir/test/Conversion/VectorToLLVM/vector-to-llvm.mlir
@@ -76,6 +76,17 @@ func.func @broadcast_vec1d_from_f32(%arg0: f32) -> vector<2xf32> {
// -----
+func.func @broadcast_single_elem_vec1d_from_f32(%arg0: f32) -> vector<1xf32> {
+ %0 = vector.broadcast %arg0 : f32 to vector<1xf32>
+ return %0 : vector<1xf32>
+}
+// CHECK-LABEL: @broadcast_single_elem_vec1d_from_f32
+// CHECK-SAME: %[[A:.*]]: f32)
+// CHECK: %[[T0:.*]] = llvm.insertelement %[[A]]
+// CHECK: return %[[T0]] : vector<1xf32>
+
+// -----
+
func.func @broadcast_vec1d_from_f32_scalable(%arg0: f32) -> vector<[2]xf32> {
%0 = vector.broadcast %arg0 : f32 to vector<[2]xf32>
return %0 : vector<[2]xf32>
More information about the Mlir-commits
mailing list