Accessing a type from a custom dialect in Python

I have an out-of-tree project, which introduce a custom dialect with custom types. I have followed instructions here to generate a python package myproject.dialects.mydialect (I’m also wondering if I should name this mlir.dialects.mydialect) with my custom ops, and I can now generate some MLIR representation with it. However, it doesn’t seem I have access to my custom types. For examples, I can access F32Type from mlir.ir, but how would I be able to access the custom types? I also saw this instruction from here mlir._cext.append_dialect_search_prefix("myproject.mlir.dialects") for extending the search path for dialects, but the mlir module doesn’t seem to have a _cext submodule or something.

At the moment, the facilities to generate Python bindings only work with the Operations defined in your dialect’s tablegen files. There is no functionality to automatically generate Python bindings for your dialect’s Types. To create Python classes for your types, you can use some helpers defined in mlir/Bindings/Python/PybindAdaptors.h. I guess this isn’t documented yet, but here’s an example of how we use them in the CIRCT project: circt/HWModule.cpp at 218617cf1f6b1a5058bee178076ee9197429132e · llvm/circt · GitHub. You’ll also need some helper functions to create an object of your type, query if a given object is your type, etc. The recommended approach there is to create a C API for that, so the bindings always call into the C API.

1 Like