Skip to content
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

core: add MLIROptPass, calling mlir-opt #1257

Merged
merged 7 commits into from Jul 17, 2023
Merged

Conversation

superlopuh
Copy link
Member

@superlopuh superlopuh commented Jul 9, 2023

This adds a Python class to pipe code through mlir-opt. This has been quite useful to me while writing the pass pipeline in Python, to leverage things like cse and lowering through builtin dialects. I'm planning to take mlir-opt out of the Toy tutorial for the sake of having an end-to-end compiler with no dependencies, but I think it will be useful to future projects using xDSL.

@superlopuh superlopuh added the core xDSL core (ir, textual format, ...) label Jul 9, 2023
@superlopuh superlopuh self-assigned this Jul 9, 2023
@superlopuh superlopuh changed the title [WIP] core: add MLIROptPass, calling mlir-opt core: add MLIROptPass, calling mlir-opt Jul 17, 2023
@codecov
Copy link

codecov bot commented Jul 17, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.06 🎉

Comparison is base (e781c09) 89.34% compared to head (a48c287) 89.41%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1257      +/-   ##
==========================================
+ Coverage   89.34%   89.41%   +0.06%     
==========================================
  Files         181      182       +1     
  Lines       24080    24119      +39     
  Branches     3663     3666       +3     
==========================================
+ Hits        21515    21565      +50     
+ Misses       1991     1980      -11     
  Partials      574      574              
Impacted Files Coverage Δ
xdsl/transforms/mlir_opt.py 100.00% <100.00%> (ø)
xdsl/xdsl_opt_main.py 96.71% <100.00%> (+0.01%) ⬆️

... and 17 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@PapyChacal PapyChacal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely 😍

Copy link
Collaborator

@webmiche webmiche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the general idea of this. However, I wonder about how platform-independent this is. And I feel you should link to the commit hash in the README (or wherever that is nowadays).

Comment on lines +25 to +26
if not shutil.which("mlir-opt"):
raise ValueError("mlir-opt is not available")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work platform-independently?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea :) what platforms should I test this on?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, not even sure we need somethgin else than linux, we aren't weirdos after all.

Maybe documentation is enough? Also, from looking at the docu, shutil should at least work relatively well with several platforms, so....

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should just keep it as it is for now, and then fix it if it later on becomes a problem ? Also, it seems it should work in Python.

@@ -0,0 +1,15 @@
// RUN: xdsl-opt %s -p mlir-opt{arguments='--cse','--mlir-print-op-generic'} --print-op-generic | filecheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how mlir structures it's optimizations, but this feels more like dce than cse. Maybe change the example (or ignore the comment 😉 )

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try dce

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mlir-opt: Unknown command line argument '--dce'.  Try: 'mlir-opt --help'
mlir-opt: Did you mean '--cse'?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cse does dce if I recall, there is no dce passes AFAIK?

Comment on lines +11 to +15
// CHECK: "builtin.module"() ({
// CHECK-NEXT: "func.func"() ({
// CHECK-NEXT: "func.return"() : () -> ()
// CHECK-NEXT: }) {"function_type" = () -> (), "sym_name" = "do_nothing"} : () -> ()
// CHECK-NEXT: }) : () -> ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use CHECK-NOT? Feels like it's intended for exactly this. Plus. this will be more stable to printing changes for all of these ops.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like check-next for the whole thing because then if it prints an error or something unexpected then at least we catch it. I'm not sure how robust check-not would be if mlir-opt prints "error"

xdsl/passes.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@math-fehr math-fehr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Could you just add a test when mlir-opt fails?

@@ -0,0 +1,15 @@
// RUN: xdsl-opt %s -p mlir-opt{arguments='--cse','--mlir-print-op-generic'} --print-op-generic | filecheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cse does dce if I recall, there is no dce passes AFAIK?

xdsl/transforms/mlir_opt.py Outdated Show resolved Hide resolved
Comment on lines +25 to +26
if not shutil.which("mlir-opt"):
raise ValueError("mlir-opt is not available")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should just keep it as it is for now, and then fix it if it later on becomes a problem ? Also, it seems it should work in Python.

xdsl/transforms/mlir_opt.py Outdated Show resolved Hide resolved
@superlopuh
Copy link
Member Author

superlopuh commented Jul 17, 2023

  • add test case for fail

@superlopuh superlopuh merged commit 949b11c into main Jul 17, 2023
10 checks passed
@superlopuh superlopuh deleted the sasha/mlir-opt-pass branch July 17, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core xDSL core (ir, textual format, ...)
Projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants