[Mlir-commits] [mlir] 72b7daf - [mlir][linalg] Fix vectorization precondition for tensor.pad (#175869)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Jan 14 11:07:03 PST 2026
Author: Ian Wood
Date: 2026-01-14T19:06:58Z
New Revision: 72b7daf82b1af90f087515ac7e79e557f603f958
URL: https://github.com/llvm/llvm-project/commit/72b7daf82b1af90f087515ac7e79e557f603f958
DIFF: https://github.com/llvm/llvm-project/commit/72b7daf82b1af90f087515ac7e79e557f603f958.diff
LOG: [mlir][linalg] Fix vectorization precondition for tensor.pad (#175869)
This changes `padOp.getLow()` to `padOp.getMixedLowPad()` in
`vectorizePadOpPrecondition()`. The `getMixedLowPad()` function
correctly returns both static and dynamic padding values, ensuring that
the indexing is correct when checking for non-zero low padding on
non-unit result dimensions. Using the added test as an example,
`getLow()` would only return `%low`, which means `en.index()` would be 0
instead of the correct dimension index 1.
---------
Signed-off-by: Ian Wood <ianwood at u.northwestern.edu>
Added:
Modified:
mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
Removed:
################################################################################
diff --git a/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp b/mlir/lib/Dialect/Linalg/Transforms/Vectorization.cpp
index 52d651b59bbd0..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.getLow()), [&](const auto &en) {
- Value 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();
}
diff --git a/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir b/mlir/test/Dialect/Linalg/vectorization/unsupported.mlir
index f653a4852b074..271d6169609e9 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 @+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):
+ 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
More information about the Mlir-commits
mailing list