[Mlir-commits] [mlir] [mlir][linalg] Fix vectorization precondition for tensor.pad (PR #175869)

Ian Wood llvmlistbot at llvm.org
Wed Jan 14 10:48:42 PST 2026


https://github.com/IanWood1 updated https://github.com/llvm/llvm-project/pull/175869

>From 1ea4e7aaa8f44717431ca3da63c719e76ad22068 Mon Sep 17 00:00:00 2001
From: Ian Wood <ianwood at u.northwestern.edu>
Date: Tue, 13 Jan 2026 15:49:56 -0800
Subject: [PATCH 1/3] [mlir][linalg] Fix vectorization precondition check for
 tensor.pad

Signed-off-by: Ian Wood <ianwood at u.northwestern.edu>
---
 .../Linalg/Transforms/Vectorization.cpp       |  4 +--
 .../Linalg/vectorization/unsupported.mlir     | 25 +++++++++++++++++++
 2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
index 52d651b59bbd0..5ddefb726080e 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
@@ -2511,8 +2511,8 @@ vectorizePadOpPrecondition(tensor::PadOp padOp,
   // which is zero here. Hence we will load the pad value which is what we want
   // in this case. If the low pad is dynamically zero then the lowering is
   // correct as well as no shifts are necessary.
-  if (llvm::any_of(llvm::enumerate(padOp.getLow()), [&](const auto &en) {
-        Value padValue = en.value();
+  if (llvm::any_of(llvm::enumerate(padOp.getMixedLowPad()), [&](const auto &en) {
+        OpFoldResult padValue = en.value();
         unsigned pos = en.index();
         std::optional<int64_t> pad = getConstantIntValue(padValue);
         return (!pad.has_value() || pad.value() != 0) &&
diff --git a/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir b/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
index f653a4852b074..eb7cbfd4ed6d7 100644
--- a/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
+++ b/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
@@ -332,6 +332,31 @@ module attributes {transform.with_named_sequence} {
 
 // -----
 
+// This test verifies that vectorization correctly handles mixed static/dynamic
+// low padding.
+func.func @tensor_pad_non_zero_low_pad_mixed_dynamic_static(
+  %0 : tensor<1x?xf32>, %low : index, %high : index)
+    -> tensor<1x3xf32> {
+  // expected-error @+3 {{Attempted to vectorize, but failed}}
+  %cst = arith.constant 42.43 : f32
+  %1 = tensor.pad %0 low[0, %low] high[0, %high]  {
+    ^bb0(%i: index, %j: index):
+      tensor.yield %cst : f32
+    } : tensor<1x?xf32> to tensor<1x3xf32>
+  return %1: tensor<1x3xf32>
+}
+
+module attributes {transform.with_named_sequence} {
+  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
+    %0 = transform.structured.match ops{["tensor.pad"]} in %arg1
+      : (!transform.any_op) -> !transform.any_op
+    transform.structured.vectorize %0 vector_sizes [2, 4] : !transform.any_op
+    transform.yield
+  }
+}
+
+// -----
+
 // With dynamically shaped source, the vectorizer infers the vector size for
 // xfer Ops from the destination tensor and, conservatively, assumes
 // out-of-bounds accesses. Out-of-bounds accesses require a pad value, but

>From ce46130506da654591cb8e0fb34791a57b52f31e Mon Sep 17 00:00:00 2001
From: Ian Wood <ianwood at u.northwestern.edu>
Date: Tue, 13 Jan 2026 16:03:21 -0800
Subject: [PATCH 2/3] Fix C++ formatting

Signed-off-by: Ian Wood <ianwood at u.northwestern.edu>
---
 .../Dialect/Linalg/Transforms/Vectorization.cpp   | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
index 5ddefb726080e..c7d5dff74c5a9 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
@@ -2511,13 +2511,14 @@ vectorizePadOpPrecondition(tensor::PadOp padOp,
   // which is zero here. Hence we will load the pad value which is what we want
   // in this case. If the low pad is dynamically zero then the lowering is
   // correct as well as no shifts are necessary.
-  if (llvm::any_of(llvm::enumerate(padOp.getMixedLowPad()), [&](const auto &en) {
-        OpFoldResult padValue = en.value();
-        unsigned pos = en.index();
-        std::optional<int64_t> pad = getConstantIntValue(padValue);
-        return (!pad.has_value() || pad.value() != 0) &&
-               resultTensorShape[pos] != 1;
-      })) {
+  if (llvm::any_of(llvm::enumerate(padOp.getMixedLowPad()),
+                   [&](const auto &en) {
+                     OpFoldResult padValue = en.value();
+                     unsigned pos = en.index();
+                     std::optional<int64_t> pad = getConstantIntValue(padValue);
+                     return (!pad.has_value() || pad.value() != 0) &&
+                            resultTensorShape[pos] != 1;
+                   })) {
     LDBG() << "low pad must all be zero for all non unit dims: " << padOp;
     return failure();
   }

>From e6875f0f9a670a1e9e5a4d431b34274de19d6813 Mon Sep 17 00:00:00 2001
From: Ian Wood <ianwood at u.northwestern.edu>
Date: Tue, 13 Jan 2026 16:19:50 -0800
Subject: [PATCH 3/3] Fix error comment

Signed-off-by: Ian Wood <ianwood at u.northwestern.edu>
---
 mlir/test/Dialect/Linalg/vectorization/unsupported.mlir | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir b/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
index eb7cbfd4ed6d7..271d6169609e9 100644
--- a/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
+++ b/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
@@ -337,7 +337,7 @@ module attributes {transform.with_named_sequence} {
 func.func @tensor_pad_non_zero_low_pad_mixed_dynamic_static(
   %0 : tensor<1x?xf32>, %low : index, %high : index)
     -> tensor<1x3xf32> {
-  // expected-error @+3 {{Attempted to vectorize, but failed}}
+  // expected-error @+2 {{Attempted to vectorize, but failed}}
   %cst = arith.constant 42.43 : f32
   %1 = tensor.pad %0 low[0, %low] high[0, %high]  {
     ^bb0(%i: index, %j: index):



More information about the Mlir-commits mailing list