[MLIR][Python] Add region_op wrappers for linalg#167616
Merged
ashermancinelli merged 3 commits intollvm:mainfrom Nov 12, 2025
Merged
[MLIR][Python] Add region_op wrappers for linalg#167616ashermancinelli merged 3 commits intollvm:mainfrom
ashermancinelli merged 3 commits intollvm:mainfrom
Conversation
Makes linalg.reduce and linalg.map region_ops so they can be constructed from functions and be called as decorators.
Member
|
@llvm/pr-subscribers-mlir-linalg @llvm/pr-subscribers-mlir Author: Asher Mancinelli (ashermancinelli) ChangesMakes linalg.reduce and linalg.map region_ops so they can be constructed from functions and be called as decorators. Full diff: https://github.com/llvm/llvm-project/pull/167616.diff 2 Files Affected:
diff --git a/mlir/python/mlir/dialects/linalg/__init__.py b/mlir/python/mlir/dialects/linalg/__init__.py
index d387c12deeed9..2f463cadfdba9 100644
--- a/mlir/python/mlir/dialects/linalg/__init__.py
+++ b/mlir/python/mlir/dialects/linalg/__init__.py
@@ -352,3 +352,6 @@ def unpack(
ip=ip,
)
)
+
+reduce = region_op(ReduceOp, terminator=YieldOp)
+map = region_op(MapOp, terminator=YieldOp)
diff --git a/mlir/test/python/dialects/linalg/ops.py b/mlir/test/python/dialects/linalg/ops.py
index 709a1d2424f35..b99e437e34f57 100644
--- a/mlir/test/python/dialects/linalg/ops.py
+++ b/mlir/test/python/dialects/linalg/ops.py
@@ -1,7 +1,8 @@
# RUN: %PYTHON %s | FileCheck %s
-from mlir.dialects import arith, func, linalg, tensor, memref
+from mlir.dialects import arith, func, linalg, tensor, memref, builtin
from mlir.dialects.linalg.opdsl.lang import *
+from mlir.extras import types as T
from mlir.ir import *
@@ -857,3 +858,69 @@ def elementwise_op(
)
print(module)
+
+@run
+def testReduceOp():
+ with Context(), Location.unknown():
+ f32 = T.f32()
+ tensor_type = T.tensor(10, f32)
+ @builtin.module
+ def module():
+ @func.func(tensor_type)
+ def reduce_op(input):
+ c1 = arith.constant(f32, 1.0)
+ single_result = ir.RankedTensorType.get((), f32)
+ dims = ir.DenseI64ArrayAttr.get([0])
+ init = tensor.splat(single_result, c1, [])
+
+ @linalg.reduce(
+ result=[single_result],
+ inputs=[input],
+ inits=[init],
+ dimensions=dims,
+ )
+ def reduced(element: f32, acc: f32):
+ return arith.mulf(acc, element)
+
+ return tensor.extract(reduced, [])
+
+ print(module)
+
+# CHECK-LABEL: func.func @reduce_op(
+# CHECK-SAME: %[[ARG0:.*]]: tensor<10xf32>) -> f32 {
+# CHECK: %[[CONSTANT_0:.*]] = arith.constant 1.000000e+00 : f32
+# CHECK: %[[SPLAT_0:.*]] = tensor.splat %[[CONSTANT_0]] : tensor<f32>
+# CHECK: %[[REDUCE_0:.*]] = linalg.reduce { arith.mulf } ins(%[[ARG0]] : tensor<10xf32>) outs(%[[SPLAT_0]] : tensor<f32>) dimensions = [0]
+# CHECK: %[[EXTRACT_0:.*]] = tensor.extract %[[REDUCE_0]][] : tensor<f32>
+# CHECK: return %[[EXTRACT_0]] : f32
+# CHECK: }
+
+@run
+def testMapOp():
+ with Context(), Location.unknown():
+ f32 = T.f32()
+ tensor_type = T.tensor(10, f32)
+ @builtin.module
+ def module():
+ @func.func(tensor_type)
+ def map_op(input):
+ empty = tensor.empty(tensor_type.shape, f32)
+ @linalg.map(
+ result=[tensor_type],
+ inputs=[input, input],
+ init=empty,
+ )
+ def add(element: f32, acc: f32, init: f32):
+ return arith.addf(element, acc)
+
+ return add
+
+ module.verify()
+ print(module)
+
+# CHECK-LABEL: func.func @map_op(
+# CHECK-SAME: %[[ARG0:.*]]: tensor<10xf32>) -> tensor<10xf32> {
+# CHECK: %[[EMPTY_0:.*]] = tensor.empty() : tensor<10xf32>
+# CHECK: %[[MAP_0:.*]] = linalg.map { arith.addf } ins(%[[ARG0]], %[[ARG0]] : tensor<10xf32>, tensor<10xf32>) outs(%[[EMPTY_0]] : tensor<10xf32>)
+# CHECK: return %[[MAP_0]] : tensor<10xf32>
+# CHECK: }
|
|
✅ With the latest revision this PR passed the Python code formatter. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes linalg.reduce and linalg.map region_ops so they can be constructed from functions and be called as decorators.