-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir][python] fix scf.for_
convenience builder
#72170
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
Conversation
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
@llvm/pr-subscribers-mlir Author: Maksim Levental (makslevental) ChangesFull diff: https://github.com/llvm/llvm-project/pull/72170.diff 2 Files Affected:
diff --git a/mlir/python/mlir/dialects/scf.py b/mlir/python/mlir/dialects/scf.py
index 71c80cab76dfb86..20bbed9bc93df67 100644
--- a/mlir/python/mlir/dialects/scf.py
+++ b/mlir/python/mlir/dialects/scf.py
@@ -120,11 +120,13 @@ def for_(
params = [start, stop, step]
for i, p in enumerate(params):
if isinstance(p, int):
- p = constant(p)
+ p = constant(IntegerAttr.get(IndexType.get(), p))
elif isinstance(p, float):
raise ValueError(f"{p=} must be int.")
params[i] = p
+ start, stop, step = params
+
for_op = ForOp(start, stop, step, iter_args, loc=loc, ip=ip)
iv = for_op.induction_variable
iter_args = tuple(for_op.inner_iter_args)
diff --git a/mlir/test/python/dialects/scf.py b/mlir/test/python/dialects/scf.py
index 414307d8191513b..8875c5a23d1847c 100644
--- a/mlir/test/python/dialects/scf.py
+++ b/mlir/test/python/dialects/scf.py
@@ -3,7 +3,9 @@
from mlir.ir import *
from mlir.dialects import arith
from mlir.dialects import func
+from mlir.dialects import memref
from mlir.dialects import scf
+from mlir.passmanager import PassManager
def constructAndPrintInModule(f):
@@ -54,25 +56,96 @@ def induction_var(lb, ub, step):
# CHECK-LABEL: TEST: testForSugar
-@constructAndPrintInModule
def testForSugar():
- index_type = IndexType.get()
- range = scf.for_
+ print("TEST: testForSugar")
+ with Context(), Location.unknown():
+ index_type = IndexType.get()
+ memref_t = MemRefType.get([10], index_type)
+ range = scf.for_
+ module = Module.create()
+ with InsertionPoint(module.body):
- @func.FuncOp.from_py_func(index_type, index_type, index_type)
- def range_loop(lb, ub, step):
- for i in range(lb, ub, step):
- add = arith.addi(i, i)
- scf.yield_([])
- return
+ @func.FuncOp.from_py_func(index_type, index_type, index_type, memref_t)
+ def range_loop(lb, ub, step, memref_v):
+ for i in range(lb, ub, step):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+
+ scf.yield_([])
+
+ for i in range(lb, 10, 1):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+ scf.yield_([])
+
+ for i in range(0, ub, 1):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+ scf.yield_([])
+
+ for i in range(0, 10, step):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+ scf.yield_([])
+
+ for i in range(0, 10, 1):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+ scf.yield_([])
+
+ for i in range(0, 10):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+ scf.yield_([])
+
+ for i in range(10):
+ add = arith.addi(i, i)
+ memref.store(add, memref_v, [i])
+ scf.yield_([])
+
+ return
+
+ pm = PassManager("builtin.module")
+ pm.add("canonicalize")
+ pm.run(module.operation)
+ print(module)
-# CHECK: func.func @range_loop(%[[ARG0:.*]]: index, %[[ARG1:.*]]: index, %[[ARG2:.*]]: index) {
-# CHECK: scf.for %[[IV:.*]] = %[[ARG0]] to %[[ARG1]] step %[[ARG2]]
-# CHECK: %0 = arith.addi %[[IV]], %[[IV]] : index
+# CHECK: func.func @range_loop(%arg0: index, %arg1: index, %arg2: index, %arg3: memref<10xindex>) {
+# CHECK: %c0 = arith.constant 0 : index
+# CHECK: %c1 = arith.constant 1 : index
+# CHECK: %c10 = arith.constant 10 : index
+# CHECK: scf.for %arg4 = %arg0 to %arg1 step %arg2 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: scf.for %arg4 = %arg0 to %c10 step %c1 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: scf.for %arg4 = %c0 to %arg1 step %c1 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: scf.for %arg4 = %c0 to %c10 step %arg2 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: scf.for %arg4 = %c0 to %c10 step %c1 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: scf.for %arg4 = %c0 to %c10 step %c1 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: scf.for %arg4 = %c0 to %c10 step %c1 {
+# CHECK: %0 = arith.addi %arg4, %arg4 : index
+# CHECK: memref.store %0, %arg3[%arg4] : memref<10xindex>
+# CHECK: }
+# CHECK: return
# CHECK: }
-# CHECK: return
-# CHECK: }
+testForSugar()
@constructAndPrintInModule
|
scf.for_
convenience builder
rkayaith
approved these changes
Nov 13, 2023
ee7cd7f
to
07cfcfb
Compare
makslevental
added a commit
to Xilinx/mlir-aie
that referenced
this pull request
Nov 14, 2023
makslevental
added a commit
to Xilinx/mlir-aie
that referenced
this pull request
Nov 14, 2023
zahiraam
pushed a commit
to zahiraam/llvm-project
that referenced
this pull request
Nov 20, 2023
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.
I believe this used to work but at some point
ConstantOp
adoptedTypedAttrInterface
for the value and afterwards the registered attribute builders in ir.py stopped getting called.