[Mlir-commits] [mlir] 24e95ba - [mlir][Linalg] Preserve discardable/user-defined attributes during generalization (#178599)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jan 29 05:13:09 PST 2026


Author: Abhishek Varma
Date: 2026-01-29T13:13:03Z
New Revision: 24e95bae3655c2740d1525a3095652cfa98189da

URL: https://github.com/llvm/llvm-project/commit/24e95bae3655c2740d1525a3095652cfa98189da
DIFF: https://github.com/llvm/llvm-project/commit/24e95bae3655c2740d1525a3095652cfa98189da.diff

LOG: [mlir][Linalg] Preserve discardable/user-defined attributes during generalization (#178599)

-- As observed in a [downstream
project](https://github.com/iree-org/iree/pull/23294#discussion_r2734982998)
: the named to generize linalg op conversion wasn't preserving
discardable attributes.
-- This commit aims to fix the same.
-- Only a single test case is added as the change applies to any named
linalg op's generalization.

Signed-off-by: Abhishek Varma <abhvarma at amd.com>

Added: 
    

Modified: 
    mlir/lib/Dialect/Linalg/Transforms/Generalization.cpp
    mlir/test/Dialect/Linalg/generalize-named-ops.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Linalg/Transforms/Generalization.cpp b/mlir/lib/Dialect/Linalg/Transforms/Generalization.cpp
index 75bb1757a55f5..35abdc027f194 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/Generalization.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/Generalization.cpp
@@ -64,6 +64,14 @@ FailureOr<GenericOp> mlir::linalg::generalizeNamedOp(RewriterBase &rewriter,
                         outputs, indexingMaps, iterators);
   rewriter.inlineRegionBefore(linalgOp->getRegion(0), genericOp.getRegion(),
                               genericOp.getRegion().begin());
+
+  // Discardable attributes carry user-defined metadata (e.g., annotations for
+  // downstream passes). Generalization is a semantics-preserving
+  // transformation, so dropping this metadata would be unexpected. This is safe
+  // because discardable attributes are by definition independent of op
+  // semantics.
+  genericOp->setDiscardableAttrs(linalgOp->getDiscardableAttrDictionary());
+
   rewriter.replaceOp(linalgOp, genericOp->getResults());
   return genericOp;
 }

diff  --git a/mlir/test/Dialect/Linalg/generalize-named-ops.mlir b/mlir/test/Dialect/Linalg/generalize-named-ops.mlir
index dcdd6c8db4b21..e346bee901f1d 100644
--- a/mlir/test/Dialect/Linalg/generalize-named-ops.mlir
+++ b/mlir/test/Dialect/Linalg/generalize-named-ops.mlir
@@ -1261,3 +1261,21 @@ func.func @contract_matmul_bcast_a_b(
       outs(%C: memref<3x7xf32>)
   return
 }
+
+// -----
+
+// Test that discardable (user-defined) attributes are preserved during
+// generalization.
+
+func.func @preserve_discardable_attrs(%A : tensor<16x8xf32>,
+                                            %B : tensor<8x32xf32>,
+                                            %C : tensor<16x32xf32>) -> tensor<16x32xf32> {
+  %0 = linalg.matmul {my_custom_attr = "preserved", another_attr = 42 : i64}
+                     ins(%A, %B: tensor<16x8xf32>, tensor<8x32xf32>)
+                     outs(%C: tensor<16x32xf32>) -> tensor<16x32xf32>
+  return %0: tensor<16x32xf32>
+}
+
+// CHECK-LABEL: func @preserve_discardable_attrs
+// CHECK:         linalg.generic
+// CHECK-SAME:        attrs = {another_attr = 42 : i64, my_custom_attr = "preserved"}


        


More information about the Mlir-commits mailing list