Skip to content

[mlir][TD] Support padding with poison #152003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2025
Merged

[mlir][TD] Support padding with poison #152003

merged 3 commits into from
Aug 8, 2025

Conversation

newling
Copy link
Contributor

@newling newling commented Aug 4, 2025

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Aug 4, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-linalg

Author: James Newling (newling)

Changes

Testing

In this file pad_operand is called by rewriteAsPaddedOp is called by transform::PadTilingInterfaceOp::apply. The only tests that reach pad_operand in MLIR are for example this test. It's not clear to me how to pass a poison attribute in there though. I've tried passing a string "poison" attribute and it hits logic here. And without a string I can't get poison attribute to parse either. I don't see any examples in the test directory with poison attribute. Is this not a path we can use? i.e. I want an array attribute like padding_values= [poison, 0.0 : f32].

In IREE, I can see this works via a less targeted test using applyPaddingLevel in this file.


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

2 Files Affected:

  • (modified) mlir/lib/Dialect/Linalg/Transforms/PadTilingInterface.cpp (+12-6)
  • (modified) mlir/test/Dialect/Linalg/transform-op-pad-tiling-interface.mlir (+3-1)
diff --git a/mlir/lib/Dialect/Linalg/Transforms/PadTilingInterface.cpp b/mlir/lib/Dialect/Linalg/Transforms/PadTilingInterface.cpp
index 2e6252336dfeb..3d12bc397813b 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/PadTilingInterface.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/PadTilingInterface.cpp
@@ -11,6 +11,7 @@
 #include "mlir/Dialect/Affine/IR/AffineOps.h"
 #include "mlir/Dialect/Complex/IR/Complex.h"
 #include "mlir/Dialect/Tensor/IR/Tensor.h"
+#include "mlir/Dialect/UB/IR/UBOps.h"
 #include "mlir/Dialect/Utils/StaticValueUtils.h"
 #include "mlir/IR/AffineExpr.h"
 #include "mlir/IR/BuiltinAttributes.h"
@@ -230,13 +231,18 @@ static Value padOperand(RewriterBase &rewriter, TilingInterface opToPad,
   Value paddingValue;
   if (auto complexTy =
           dyn_cast<ComplexType>(getElementTypeOrSelf(v.getType()))) {
-    auto complexAttr = cast<ArrayAttr>(paddingValueAttr);
-    paddingValue = complex::ConstantOp::create(rewriter, opToPad.getLoc(),
-                                               complexTy, complexAttr);
-  } else {
-    paddingValue = arith::ConstantOp::create(rewriter, opToPad.getLoc(),
-                                             cast<TypedAttr>(paddingValueAttr));
+    if (auto complexAttr = dyn_cast<ArrayAttr>(paddingValueAttr)) {
+      paddingValue = complex::ConstantOp::create(rewriter, opToPad.getLoc(),
+                                                 complexTy, complexAttr);
+    }
+  } else if (isa<ub::PoisonAttr>(paddingValueAttr)) {
+    paddingValue = ub::PoisonOp::create(rewriter, opToPad.getLoc(),
+                                        getElementTypeOrSelf(v.getType()));
+  } else if (auto typedAttr = dyn_cast<TypedAttr>(paddingValueAttr)) {
+    paddingValue =
+        arith::ConstantOp::create(rewriter, opToPad.getLoc(), typedAttr);
   }
+  assert(paddingValue && "failed to create value from padding attribute");
 
   // Pad the operand to the bounding box defined by `paddedShape`.
   SmallVector<int64_t> tensorShape;
diff --git a/mlir/test/Dialect/Linalg/transform-op-pad-tiling-interface.mlir b/mlir/test/Dialect/Linalg/transform-op-pad-tiling-interface.mlir
index f7418769f79ca..2857b53103779 100644
--- a/mlir/test/Dialect/Linalg/transform-op-pad-tiling-interface.mlir
+++ b/mlir/test/Dialect/Linalg/transform-op-pad-tiling-interface.mlir
@@ -4,6 +4,7 @@
 //           CHECK:   linalg.fill ins(%{{.*}} : f32) outs(%{{.*}} : tensor<8x25xf32>) -> tensor<8x25xf32>
 func.func @pad_fill(%value: f32, %output: tensor<24x25xf32>) -> tensor<24x25xf32>
 {
+  // %goo = ub.poison : f32
   %0 = linalg.fill ins(%value : f32) outs(%output : tensor<24x25xf32>) -> tensor<24x25xf32>
   func.return %0 : tensor<24x25xf32>
 }
@@ -18,7 +19,8 @@ module attributes {transform.with_named_sequence} {
       : (!transform.any_op) -> (!transform.any_op, !transform.any_op)
 
     %fill_padded, %_ = transform.structured.pad_tiling_interface %fill_l1 to padding_sizes [8] {
-      padding_values=[0.0 : f32, 0.0 : f32]
+      // padding_values= [poison, 0.0 : f32]
+      padding_values= [0.0 : f32, 0.0 : f32]
     } : (!transform.any_op) -> (!transform.any_op, !transform.any_op)
 
     transform.yield

@newling
Copy link
Contributor Author

newling commented Aug 5, 2025

@Hardcode84 a poison related question you might have an idea about -- is an array attribute like padding_values= [poison : f32, 0.0 : f32] something we could feasibly have?

@Hardcode84
Copy link
Contributor

You can try padding_values= [#ub.poison, 0.0 : f32]

@newling
Copy link
Contributor Author

newling commented Aug 6, 2025

Thanks you so much @Hardcode84 !

Copy link
Member

@Groverkss Groverkss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@banach-space
Copy link
Contributor

[mlir][tensor] Support padding with poison

[nit] Isn't this PR updating TD rather than Tensor?

@newling
Copy link
Contributor Author

newling commented Aug 7, 2025

[mlir][tensor] Support padding with poison

[nit] Isn't this PR updating TD rather than Tensor?

Maybe Linalg? At least, all the files touched are in a directory /Dialect/Linalg/. But definitely not Tensor, you're right.

@newling newling changed the title [mlir][tensor] Support padding with poison [mlir][TD] Support padding with poison Aug 7, 2025
@newling newling merged commit b574bcf into llvm:main Aug 8, 2025
9 checks passed
rupprecht added a commit to rupprecht/llvm-project that referenced this pull request Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants