[Mlir-commits] [mlir] 4ddf664 - [mlir][ABI] Fix scalable vector mapping in ABITypeMapper (#206617)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Jul 1 07:57:17 PDT 2026


Author: Fedor Nikolaev
Date: 2026-07-01T15:57:12+01:00
New Revision: 4ddf664c6c3d6487c7fca1fd2149c0f2356db321

URL: https://github.com/llvm/llvm-project/commit/4ddf664c6c3d6487c7fca1fd2149c0f2356db321
DIFF: https://github.com/llvm/llvm-project/commit/4ddf664c6c3d6487c7fca1fd2149c0f2356db321.diff

LOG: [mlir][ABI] Fix scalable vector mapping in ABITypeMapper (#206617)

mapVectorType in ABITypeMapper incorrectly used ElementCount::getFixed
for all vector types, including scalable ones (e.g. vector<[4]xf32>).
This results in scalable vectors being mapped as fixed-size vectors,
which produces incorrect ABI lowering for SVE and RISC-V V targets.

Added: 
    

Modified: 
    mlir/lib/ABI/ABITypeMapper.cpp
    mlir/unittests/ABI/ABITypeMapperTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/ABI/ABITypeMapper.cpp b/mlir/lib/ABI/ABITypeMapper.cpp
index 83dc6990ec5bb..fe63df659302a 100644
--- a/mlir/lib/ABI/ABITypeMapper.cpp
+++ b/mlir/lib/ABI/ABITypeMapper.cpp
@@ -70,12 +70,19 @@ const llvm::abi::Type *ABITypeMapper::mapVectorType(mlir::VectorType type) {
   if (!elementTy)
     return nullptr;
 
+  // ElementCount supports at most one scalable dimension, hence
+  // vectors with more than one scalable dimension (e.g. vector<[2]x[4]xf32>)
+  // cannot be represented and mapVectorType must return nullptr in this case.
+  if (llvm::count(type.getScalableDims(), true) > 1)
+    return nullptr;
+
   auto shape = type.getShape();
   uint64_t totalElements = 1;
   for (int64_t dim : shape)
     totalElements *= dim;
 
-  llvm::ElementCount ec = llvm::ElementCount::getFixed(totalElements);
+  llvm::ElementCount ec =
+      llvm::ElementCount::get(totalElements, type.isScalable());
   uint64_t abiAlign = dl.getTypeABIAlignment(type);
   return builder.getVectorType(elementTy, ec, llvm::Align(abiAlign));
 }

diff  --git a/mlir/unittests/ABI/ABITypeMapperTest.cpp b/mlir/unittests/ABI/ABITypeMapperTest.cpp
index 4a7989298a149..c702cf387b58a 100644
--- a/mlir/unittests/ABI/ABITypeMapperTest.cpp
+++ b/mlir/unittests/ABI/ABITypeMapperTest.cpp
@@ -170,4 +170,34 @@ TEST_F(ABITypeMapperTest, MapUnsignedI32) {
   EXPECT_FALSE(intTy->isSigned());
 }
 
+TEST_F(ABITypeMapperTest, MapScalableVectorOf4xF32) {
+  DataLayout dl(module);
+  ABITypeMapper mapper(dl);
+
+  auto f32 = Float32Type::get(&ctx);
+  auto vec = VectorType::get({4}, f32, /* scalableDims=*/true);
+  const llvm::abi::Type *result = mapper.map(vec);
+
+  ASSERT_NE(result, nullptr);
+  EXPECT_TRUE(result->isVector());
+
+  auto *vecTy = llvm::cast<llvm::abi::VectorType>(result);
+  EXPECT_TRUE(vecTy->getNumElements().isScalable());
+  EXPECT_EQ(vecTy->getNumElements().getKnownMinValue(), 4u);
+  EXPECT_TRUE(vecTy->getElementType()->isFloat());
+}
+
+TEST_F(ABITypeMapperTest, MapWithTwoScalableDimsReturnsNull) {
+  DataLayout dl(module);
+  ABITypeMapper mapper(dl);
+
+  auto f32 = Float32Type::get(&ctx);
+  auto vec = VectorType::get({2, 4}, f32, /*scalableDims=*/{true, true});
+  const llvm::abi::Type *result = mapper.map(vec);
+
+  // LLVM supports at most 1 scalable dimension, hence this case should
+  // return nullptr.
+  EXPECT_EQ(result, nullptr);
+}
+
 } // namespace


        


More information about the Mlir-commits mailing list