-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[MLIR][Python] Expose the insertion point of pattern rewriter #161001
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
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
28d65b8
[MLIR][Python] Expose the insertion point of pattern rewriter
PragmaTwice beab53d
format
PragmaTwice f5c348e
Merge branch 'main' of https://github.com/llvm/llvm-project into mlir…
PragmaTwice 68fbb0f
add comment for c api
PragmaTwice 02f1782
fix doc
PragmaTwice 32f16be
Merge branch 'main' of https://github.com/llvm/llvm-project into mlir…
PragmaTwice 68264af
refactor to a class
PragmaTwice a87e193
make ip a property
PragmaTwice 8694c34
remove useless field
PragmaTwice 4b5fd88
Update mlir/lib/CAPI/Transforms/Rewrite.cpp
PragmaTwice 4d406f6
fix type of iterator
PragmaTwice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ def construct_and_print_in_module(f): | |
print(module) | ||
return f | ||
|
||
|
||
def get_pdl_patterns(): | ||
# Create a rewrite from add to mul. This will match | ||
# - operation name is arith.addi | ||
|
@@ -121,8 +122,10 @@ def load_myint_dialect(): | |
|
||
|
||
# This PDL pattern is to fold constant additions, | ||
# i.e. add(constant0, constant1) -> constant2 | ||
# where constant2 = constant0 + constant1. | ||
# including two patterns: | ||
# 1. add(constant0, constant1) -> constant2 | ||
# where constant2 = constant0 + constant1; | ||
# 2. add(x, 0) or add(0, x) -> x. | ||
def get_pdl_pattern_fold(): | ||
m = Module.create() | ||
i32 = IntegerType.get_signless(32) | ||
|
@@ -237,3 +240,87 @@ def test_pdl_register_function_constraint(module_): | |
apply_patterns_and_fold_greedily(module_, frozen) | ||
|
||
return module_ | ||
|
||
|
||
# This pattern is to expand constant to additions | ||
# unless the constant is no more than 1, | ||
# e.g. 3 -> 1 + 2 -> 1 + (1 + 1). | ||
def get_pdl_pattern_expand(): | ||
m = Module.create() | ||
i32 = IntegerType.get_signless(32) | ||
with InsertionPoint(m.body): | ||
|
||
@pdl.pattern(benefit=1, sym_name="myint_constant_expand") | ||
def pat(): | ||
t = pdl.TypeOp(i32) | ||
cst = pdl.AttributeOp() | ||
pdl.apply_native_constraint([], "is_one", [cst]) | ||
op0 = pdl.OperationOp( | ||
name="myint.constant", attributes={"value": cst}, types=[t] | ||
) | ||
|
||
@pdl.rewrite() | ||
def rew(): | ||
expanded = pdl.apply_native_rewrite( | ||
[pdl.OperationType.get()], "expand", [cst] | ||
) | ||
pdl.ReplaceOp(op0, with_op=expanded) | ||
|
||
def is_one(rewriter, results, values): | ||
cst = values[0].value | ||
return cst <= 1 | ||
|
||
def expand(rewriter, results, values): | ||
cst = values[0].value | ||
c1 = cst // 2 | ||
c2 = cst - c1 | ||
with rewriter.ip(): | ||
op1 = Operation.create( | ||
"myint.constant", | ||
results=[i32], | ||
attributes={"value": IntegerAttr.get(i32, c1)}, | ||
) | ||
op2 = Operation.create( | ||
"myint.constant", | ||
results=[i32], | ||
attributes={"value": IntegerAttr.get(i32, c2)}, | ||
) | ||
res = Operation.create( | ||
"myint.add", results=[i32], operands=[op1.result, op2.result] | ||
) | ||
results.append(res) | ||
Comment on lines
273
to
291
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function as an example of retrieving and using the insertion point of the rewriter. |
||
|
||
pdl_module = PDLModule(m) | ||
pdl_module.register_constraint_function("is_one", is_one) | ||
pdl_module.register_rewrite_function("expand", expand) | ||
return pdl_module.freeze() | ||
|
||
|
||
# CHECK-LABEL: TEST: test_pdl_register_function_expand | ||
# CHECK: %0 = "myint.constant"() {value = 1 : i32} : () -> i32 | ||
# CHECK: %1 = "myint.constant"() {value = 1 : i32} : () -> i32 | ||
# CHECK: %2 = "myint.add"(%0, %1) : (i32, i32) -> i32 | ||
# CHECK: %3 = "myint.constant"() {value = 1 : i32} : () -> i32 | ||
# CHECK: %4 = "myint.constant"() {value = 1 : i32} : () -> i32 | ||
# CHECK: %5 = "myint.constant"() {value = 1 : i32} : () -> i32 | ||
# CHECK: %6 = "myint.add"(%4, %5) : (i32, i32) -> i32 | ||
# CHECK: %7 = "myint.add"(%3, %6) : (i32, i32) -> i32 | ||
# CHECK: %8 = "myint.add"(%2, %7) : (i32, i32) -> i32 | ||
# CHECK: return %8 : i32 | ||
@construct_and_print_in_module | ||
def test_pdl_register_function_expand(module_): | ||
load_myint_dialect() | ||
|
||
module_ = Module.parse( | ||
""" | ||
func.func @f() -> i32 { | ||
%0 = "myint.constant"() { value = 5 }: () -> (i32) | ||
return %0 : i32 | ||
} | ||
""" | ||
) | ||
|
||
frozen = get_pdl_pattern_expand() | ||
apply_patterns_and_fold_greedily(module_, frozen) | ||
|
||
return module_ |
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.
Uh oh!
There was an error while loading. Please reload this page.