[Mlir-commits] [mlir] 79ec0ce - [MLIR][LLVMIR] Add support for importing ConstantInt/FP vector splats. (#180946)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Feb 12 03:28:42 PST 2026


Author: Paul Walker
Date: 2026-02-12T11:28:37Z
New Revision: 79ec0ce5d8b72d26bb25c48083b693b15b53da55

URL: https://github.com/llvm/llvm-project/commit/79ec0ce5d8b72d26bb25c48083b693b15b53da55
DIFF: https://github.com/llvm/llvm-project/commit/79ec0ce5d8b72d26bb25c48083b693b15b53da55.diff

LOG: [MLIR][LLVMIR] Add support for importing ConstantInt/FP vector splats. (#180946)

Updates LLVM IR importing to remove the assumption that
ConstantInt/ConstantFP are always scalar.

Added: 
    

Modified: 
    mlir/lib/Target/LLVMIR/ModuleImport.cpp
    mlir/test/Target/LLVMIR/Import/constant.ll

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 8f3c2759d6f64..2fbf0ce38f6cc 100644
--- a/mlir/lib/Target/LLVMIR/ModuleImport.cpp
+++ b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
@@ -1221,6 +1221,9 @@ static TypedAttr getScalarConstantAsAttr(OpBuilder &builder,
                                          llvm::Constant *constScalar) {
   MLIRContext *context = builder.getContext();
 
+  if (constScalar->getType()->isVectorTy())
+    return {};
+
   // Convert scalar integers.
   if (auto *constInt = dyn_cast<llvm::ConstantInt>(constScalar)) {
     return builder.getIntegerAttr(
@@ -1270,6 +1273,17 @@ Attribute ModuleImport::getConstantAsAttr(llvm::Constant *constant) {
         getBuiltinTypeForAttr(convertType(type)));
   };
 
+  // Convert constant vector splat values.
+  if (isa<llvm::ConstantInt, llvm::ConstantFP>(constant)) {
+    assert(constant->getType()->isVectorTy() && "expected a vector splat");
+    auto shape = getConstantShape(constant->getType());
+    if (!shape)
+      return {};
+    Attribute splatAttr =
+        getScalarConstantAsAttr(builder, constant->getSplatValue());
+    return SplatElementsAttr::get(shape, splatAttr);
+  }
+
   // Convert one-dimensional constant arrays or vectors that store 1/2/4/8-byte
   // integer or half/bfloat/float/double values.
   if (auto *constArray = dyn_cast<llvm::ConstantDataSequential>(constant)) {

diff  --git a/mlir/test/Target/LLVMIR/Import/constant.ll b/mlir/test/Target/LLVMIR/Import/constant.ll
index ddf29c6d2f380..042792d7d5c8a 100644
--- a/mlir/test/Target/LLVMIR/Import/constant.ll
+++ b/mlir/test/Target/LLVMIR/Import/constant.ll
@@ -1,4 +1,5 @@
 ; RUN: mlir-translate -import-llvm -split-input-file %s | FileCheck %s
+; RUN: mlir-translate -import-llvm -split-input-file --use-constant-int-for-fixed-length-splat --use-constant-fp-for-fixed-length-splat %s | FileCheck %s
 
 ; CHECK-LABEL: @int_constants
 define void @int_constants(i16 %arg0, i32 %arg1, i1 %arg2) {
@@ -308,3 +309,11 @@ define [0 x ptr] @load_zero_array() {
 @global_array_with_elements = global [3 x i32] zeroinitializer
 
 ; CHECK: llvm.mlir.global external @global_array_with_elements({{.*}}) {addr_space = 0 : i32} : !llvm.array<3 x i32>
+
+; Test that vector splats work correctly.
+
+ at vector_splat_int = global <2 x i64> splat (i64 7)
+ at vector_splat_float = global <2 x float> splat (float 7.0)
+
+; CHECK: llvm.mlir.global external @vector_splat_int(dense<7> : vector<2xi64>) {addr_space = 0 : i32} : vector<2xi64>
+; CHECK: llvm.mlir.global external @vector_splat_float(dense<7.000000e+00> : vector<2xf32>) {addr_space = 0 : i32} : vector<2xf32>


        


More information about the Mlir-commits mailing list