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] support out of tree dialects and passes for python interface #234

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

qzylalala
Copy link

@qzylalala qzylalala commented Nov 8, 2023

Add support for out of tree dialects and passes python interface.
Note: Pybind is needed.

Please follow these commands

cmake -G Ninja .. \                                                                           
    -DMLIR_DIR=$PWD/../llvm/build/lib/cmake/mlir \
    -DLLVM_DIR=$PWD/../llvm/build/lib/cmake/llvm \
    -DLLVM_ENABLE_ASSERTIONS=ON \
    -DCMAKE_BUILD_TYPE=RELEASE \
    -DBUDDY_MLIR_ENABLE_PYTHON_PACKAGES=ON \
    -DPython3_EXECUTABLE=$(which python3)

cmake --build .

export PYTHONPATH=XXX/buddy-mlir/build/midend/python_packages/mlir_core

Then you can import our dialects and passes using python, an example is tests/Python/test_python.py

Copy link
Member

@zhanghb97 zhanghb97 left a comment

Choose a reason for hiding this comment

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

Some quick feedback about the format.
Still reviewing the details.

midend/CMakeLists.txt Outdated Show resolved Hide resolved
midend/include/buddy-mlir-c/Dialects.h Outdated Show resolved Hide resolved
midend/include/buddy-mlir-c/InitAll.h Outdated Show resolved Hide resolved
midend/include/buddy-mlir-c/Registration.h Outdated Show resolved Hide resolved
midend/lib/CAPI/CMakeLists.txt Outdated Show resolved Hide resolved
midend/python/buddy_mlir/dialects/gemmini.py Outdated Show resolved Hide resolved
midend/python/buddy_mlir/dialects/rvv.py Outdated Show resolved Hide resolved
midend/python/buddy_mlir/dialects/sche.py Outdated Show resolved Hide resolved
midend/python/buddy_mlir/dialects/vector_exp.py Outdated Show resolved Hide resolved
@@ -0,0 +1,35 @@
from buddy_mlir.ir import *
Copy link
Member

Choose a reason for hiding this comment

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

Please use Lit and FileCheck to test the code.

Copy link
Author

Choose a reason for hiding this comment

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

When I run ninja check-buddy ,I got an error like this.

check:37'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            4: ModuleNotFoundError: No module named 'buddy_mlir' 

I think this is because we don't export the right python path. The CMakeLists.txt tells us the BUDDY_MLIR_PYTHON_PACKAGES_DIR is ${BUDDY_BUILD_DIR}/python_packages

set(BUDDY_MLIR_PYTHON_PACKAGES_DIR ${BUDDY_BUILD_DIR}/python_packages)

And config.buddy_python_packages_dir equals to BUDDY_MLIR_PYTHON_PACKAGES_DIR.
config.buddy_mlir_enable_python_packages = "@BUDDY_MLIR_ENABLE_PYTHON_PACKAGES@"
config.buddy_python_packages_dir = "@BUDDY_MLIR_PYTHON_PACKAGES_DIR@"
config.mlir_runner_utils_dir = "@LLVM_LIBS_DIR@"

However, I set generated python packages to ${BUDDY_BUILD_DIR}/midend/python_packages/mlir_core.

And when I add this path to config.buddy_mlir_enable_python_packages, it works. So we need only one path finally.

if config.buddy_mlir_enable_python_packages:
llvm_config.with_environment(
"PYTHONPATH",
[
os.path.join(
config.llvm_build_dir,
"tools",
"mlir",
"python_packages",
"mlir_core",
),
config.buddy_python_packages_dir,
],
append_path=True,
)

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.

2 participants