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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Feb 11 06:10:45 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-llvm

Author: Paul Walker (paulwalker-arm)

<details>
<summary>Changes</summary>

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

NOTE: The first commit shows the erroneous result when the --use-constant-int-for-fixed-length-splat option is enabled. The goal is to make this and related options the default.

---
Full diff: https://github.com/llvm/llvm-project/pull/180946.diff


2 Files Affected:

- (modified) mlir/lib/Target/LLVMIR/ModuleImport.cpp (+14) 
- (modified) mlir/test/Target/LLVMIR/Import/constant.ll (+9) 


``````````diff
diff --git a/mlir/lib/Target/LLVMIR/ModuleImport.cpp b/mlir/lib/Target/LLVMIR/ModuleImport.cpp
index 8f3c2759d6f64..f126acbaaa774 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>

``````````

</details>


https://github.com/llvm/llvm-project/pull/180946


More information about the Mlir-commits mailing list