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

python pass registration #710

Merged
merged 6 commits into from
Nov 18, 2023
Merged

python pass registration #710

merged 6 commits into from
Nov 18, 2023

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Nov 2, 2023

This PR demonstrates linking C++ directly to the python bindings (without going through the C API).

In fact it demonstrates something more exciting: tunneling python callbacks all the way into MLIR passes:

def print_ops(op):
    print(op.name)

aie.dialects.aie.register_python_pass_demo_pass(print_ops)

module = """
module {
  %12 = AIE.tile(1, 2)
  %buf = AIE.buffer(%12) : memref<256xi32>
  %4 = AIE.core(%12)  {
    %0 = arith.constant 0 : i32
    %1 = arith.constant 0 : index
    memref.store %0, %buf[%1] : memref<256xi32>
    AIE.end
  }
}
"""

mlir_module = Module.parse(module)
PassManager.parse("builtin.module(python-pass-demo)").run(
        mlir_module.operation
 )

This will print

AIE.tile
AIE.buffer
arith.constant
arith.constant
memref.store
AIE.end
AIE.core
builtin.module

by calling print_ops from inside the pass (where there's a this->getOperation()->walk([this](Operation *op) { func(wrap(op)); });).

The only thing that needs to be changed for this to work is to build MLIR with RTTI. This sounds scary (maybe?) but it doesn't seem to matter at all (my wheels are built and tested with RTTI now for months...).

In the future (very soon) this pass will do more than just demonstrate the capability (at which point it will be renamed appropriately).

Note, this is stacked on top of #711 so be sure to exclude the first commit from the diff view.

@makslevental makslevental force-pushed the python_pass branch 7 times, most recently from 3051584 to c31854a Compare November 2, 2023 14:28
@makslevental makslevental changed the title WIP: python pass registration python pass registration Nov 2, 2023
@makslevental makslevental force-pushed the python_pass branch 2 times, most recently from 678214a to 17fea38 Compare November 2, 2023 18:56
@makslevental makslevental marked this pull request as ready for review November 2, 2023 19:49
@makslevental makslevental force-pushed the python_pass branch 4 times, most recently from c736e9c to 342b598 Compare November 2, 2023 23:43
@stephenneuendorffer
Copy link
Collaborator

The only thing that needs to be changed for this to work is to build MLIR with RTTI. This sounds scary (maybe?) but it doesn't seem to matter at all (my wheels are built and tested with RTTI now for months...).

I don't think there's a real problem compiling with RTTI: It just adds overhead that LLVM is mostly designed to avoid. IIRC there might also be some other c++ python interop problems, I seem to remember discussing with Stella at one point.

@makslevental
Copy link
Contributor Author

I don't think there's a real problem compiling with RTTI: It just adds overhead that LLVM is mostly designed to avoid. IIRC there might also be some other c++ python interop problems, I seem to remember discussing with Stella at one point.

I was under the same impression but after chatting with @stellaraccident offline about this, I think the consensus is it's best to avoid it if not needed. Well good news! Turns out you can indeed avoid it - the current version puts the pass behind a "blood-brain" barrier (just a separate TU with a pure Python C API interface) and compiles the object file with -fno-rtti. Locally it works without compiling upstream with RTTI enabled but let's see how the test here shakes out.

@makslevental makslevental force-pushed the python_pass branch 2 times, most recently from d91855d to 232cbe9 Compare November 3, 2023 05:24
@stellaraccident
Copy link

Just don't cross the streams. Commingling c++ code compiled with and without it for the same types is a recipe for bad things.

I doubt you'll get support for running rtti builds upstream to test. Ditto in iree.

@makslevental makslevental force-pushed the python_pass branch 3 times, most recently from 0d06871 to d4cfee8 Compare November 7, 2023 19:04
@makslevental makslevental force-pushed the python_pass branch 4 times, most recently from 614d9f5 to 11b98d7 Compare November 11, 2023 23:04
@makslevental
Copy link
Contributor Author

@AndraBisca @fifield I would like to merge this.

Can you please take a look because you both are working on the bindings and tell me if there's anything you don't understand. Additionally, I have tested this locally (on Mac and Linux, the latter with with chess) but not Windows nor with Peano. If either of you could test locally to double check that would be helpful.

@makslevental makslevental force-pushed the python_pass branch 3 times, most recently from 200b0dc to 8d4f183 Compare November 11, 2023 23:42
Copy link
Collaborator

@AndraBisca AndraBisca left a comment

Choose a reason for hiding this comment

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

This looks really good! Thank you for all the helpful comments you added in the files as well!

@makslevental makslevental force-pushed the python_pass branch 3 times, most recently from 289ee49 to f59cfc5 Compare November 18, 2023 06:47
@makslevental makslevental enabled auto-merge (squash) November 18, 2023 06:51
@makslevental makslevental enabled auto-merge (squash) November 18, 2023 06:58
@makslevental makslevental merged commit 6f8dafc into main Nov 18, 2023
6 checks passed
@makslevental makslevental deleted the python_pass branch November 18, 2023 18:38
Comment on lines +68 to +70
"../../lib/libmlir_runner_utils.so",
"../../lib/libmlir_c_runner_utils.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 guess this makes some assumptions about directory layout that I don't follow since I get a failure here. Is it possible to get cmake or something else to fill in the path?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that directory is CMAKE_BIN/lib but but you need to build LLVM/MLIR with the execution engine enabled for them to be copied to that location

${CMAKE_BINARY_DIR}/lib

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can gate this test by availability of the execution engine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also it's possible if you built without DCMAKE_PLATFORM_NO_VERSIONED_SONAME=ON then the .so got version and the script doesn't pick it up (fixed in #771).

makslevental added a commit that referenced this pull request Dec 8, 2023
This PR implements routing/path-finding using ILP (using Gurobi/or-tools) through the python pass registration mechanism landed in #710.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants