@@ -2897,6 +2897,14 @@ maybeGetTracebackLocation(const std::optional<PyLocation> &location) {
28972897// Populates the core exports of the 'ir' submodule.
28982898// ------------------------------------------------------------------------------
28992899
2900+ MlirDialectHandle createMlirDialectHandleFromCapsule (nb::object capsule) {
2901+ MlirDialectHandle rawRegistry =
2902+ mlirPythonCapsuleToDialectHandle (capsule.ptr ());
2903+ if (mlirDialectHandleIsNull (rawRegistry))
2904+ throw nb::python_error ();
2905+ return rawRegistry;
2906+ }
2907+
29002908void mlir::python::populateIRCore (nb::module_ &m) {
29012909 // disable leak warnings which tend to be false positives.
29022910 nb::set_leak_warnings (false );
@@ -3126,14 +3134,38 @@ void mlir::python::populateIRCore(nb::module_ &m) {
31263134 },
31273135 nb::sig (" def __repr__(self) -> str" ));
31283136
3137+ // ----------------------------------------------------------------------------
3138+ // Mapping of MlirDialectHandle
3139+ // ----------------------------------------------------------------------------
3140+
3141+ nb::class_<MlirDialectHandle>(m, " DialectHandle" )
3142+ .def_static (MLIR_PYTHON_CAPI_FACTORY_ATTR,
3143+ &createMlirDialectHandleFromCapsule);
3144+
31293145 // ----------------------------------------------------------------------------
31303146 // Mapping of PyDialectRegistry
31313147 // ----------------------------------------------------------------------------
31323148 nb::class_<PyDialectRegistry>(m, " DialectRegistry" )
31333149 .def_prop_ro (MLIR_PYTHON_CAPI_PTR_ATTR, &PyDialectRegistry::getCapsule)
31343150 .def_static (MLIR_PYTHON_CAPI_FACTORY_ATTR,
31353151 &PyDialectRegistry::createFromCapsule)
3136- .def (nb::init<>());
3152+ .def (nb::init<>())
3153+ .def (" insert_dialect" ,
3154+ [](PyDialectRegistry &self, MlirDialectHandle handle) {
3155+ mlirDialectHandleInsertDialect (handle, self.get ());
3156+ })
3157+ .def (" insert_dialect" ,
3158+ [](PyDialectRegistry &self, intptr_t ptr) {
3159+ mlirDialectHandleInsertDialect (
3160+ {reinterpret_cast <const void *>(ptr)}, self.get ());
3161+ })
3162+ .def_prop_ro (" dialect_names" , [](PyDialectRegistry &self) {
3163+ int64_t numDialectNames =
3164+ mlirDialectRegistryGetNumDialectNames (self.get ());
3165+ std::vector<MlirStringRef> dialectNames (numDialectNames);
3166+ mlirDialectRegistryGetDialectNames (self.get (), dialectNames.data ());
3167+ return dialectNames;
3168+ });
31373169
31383170 // ----------------------------------------------------------------------------
31393171 // Mapping of Location
0 commit comments