-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[MLIR][Python] Support Python-defined passes in MLIR #156000
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 all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8386c87
[MLIR][Python] Support Python-defined passes in MLIR
PragmaTwice cb82621
add ctor with args for Pass
PragmaTwice 7556ca2
fix lifetime issue
PragmaTwice d5055a7
add test case
PragmaTwice 6d2f472
fix header
PragmaTwice 1a98ae8
format
PragmaTwice 751fe84
format
PragmaTwice 7966ddd
format
PragmaTwice 6a9ec66
remove useless comment
PragmaTwice 2965a9e
improve test
PragmaTwice 1dde449
Merge branch 'main' into mlir-python-pass
PragmaTwice ca80408
rename mlirApplyPatternsAndFoldGreedilyForOp with mlirApplyPatternsAn…
c8c2fae
add clang-format annotation for header ordering
PragmaTwice 01e68c5
rename mlir.passmanager.Pass to mlir.passes.Pass
PragmaTwice e565ffb
Merge changes from #157369
PragmaTwice 9f526c7
fix rewrite.cpp
PragmaTwice b6080ea
fix header order
PragmaTwice f72c83c
Merge branch 'main' of https://github.com/llvm/llvm-project into mlir…
PragmaTwice 7a491af
fix apply_patterns_and_fold_greedily call
PragmaTwice 46b833d
overload add()
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import gc, sys | ||
from mlir.ir import * | ||
from mlir.passmanager import * | ||
from mlir.dialects.builtin import ModuleOp | ||
from mlir.dialects import pdl | ||
from mlir.rewrite import * | ||
|
||
|
||
def log(*args): | ||
print(*args, file=sys.stderr) | ||
sys.stderr.flush() | ||
|
||
|
||
def run(f): | ||
log("\nTEST:", f.__name__) | ||
f() | ||
gc.collect() | ||
assert Context._get_live_count() == 0 | ||
|
||
|
||
def make_pdl_module(): | ||
with Location.unknown(): | ||
pdl_module = Module.create() | ||
with InsertionPoint(pdl_module.body): | ||
# Change all arith.addi with index types to arith.muli. | ||
@pdl.pattern(benefit=1, sym_name="addi_to_mul") | ||
def pat(): | ||
# Match arith.addi with index types. | ||
i64_type = pdl.TypeOp(IntegerType.get_signless(64)) | ||
operand0 = pdl.OperandOp(i64_type) | ||
operand1 = pdl.OperandOp(i64_type) | ||
op0 = pdl.OperationOp( | ||
name="arith.addi", args=[operand0, operand1], types=[i64_type] | ||
) | ||
|
||
# Replace the matched op with arith.muli. | ||
@pdl.rewrite() | ||
def rew(): | ||
newOp = pdl.OperationOp( | ||
name="arith.muli", args=[operand0, operand1], types=[i64_type] | ||
) | ||
pdl.ReplaceOp(op0, with_op=newOp) | ||
|
||
return pdl_module | ||
|
||
|
||
# CHECK-LABEL: TEST: testCustomPass | ||
@run | ||
def testCustomPass(): | ||
with Context(): | ||
pdl_module = make_pdl_module() | ||
frozen = PDLModule(pdl_module).freeze() | ||
|
||
module = ModuleOp.parse( | ||
r""" | ||
module { | ||
func.func @add(%a: i64, %b: i64) -> i64 { | ||
%sum = arith.addi %a, %b : i64 | ||
return %sum : i64 | ||
} | ||
} | ||
""" | ||
) | ||
|
||
def custom_pass_1(op): | ||
print("hello from pass 1!!!", file=sys.stderr) | ||
|
||
class CustomPass2: | ||
def __call__(self, m): | ||
apply_patterns_and_fold_greedily(m, frozen) | ||
|
||
custom_pass_2 = CustomPass2() | ||
|
||
pm = PassManager("any") | ||
pm.enable_ir_printing() | ||
|
||
# CHECK: hello from pass 1!!! | ||
# CHECK-LABEL: Dump After custom_pass_1 | ||
pm.add(custom_pass_1) | ||
# CHECK-LABEL: Dump After CustomPass2 | ||
# CHECK: arith.muli | ||
pm.add(custom_pass_2, "CustomPass2") | ||
# CHECK-LABEL: Dump After ArithToLLVMConversionPass | ||
# CHECK: llvm.mul | ||
pm.add("convert-arith-to-llvm") | ||
pm.run(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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: what happens on lambdas?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not the best thing ever but it doesn't blow up (and also I don't see anyone using a lambda here...).