MLIR 23.0.0git
MainModule.cpp
Go to the documentation of this file.
1//===- MainModule.cpp - Main pybind module --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "Pass.h"
10#include "Rewrite.h"
16
17namespace nb = nanobind;
19
20namespace mlir {
21namespace python {
23void populateIRAffine(nb::module_ &m);
24void populateIRAttributes(nb::module_ &m);
25void populateIRInterfaces(nb::module_ &m);
26void populateIRTypes(nb::module_ &m);
27void populateIRCore(nb::module_ &m);
28void populateRoot(nb::module_ &m);
29} // namespace MLIR_BINDINGS_PYTHON_DOMAIN
30} // namespace python
31} // namespace mlir
32
33// -----------------------------------------------------------------------------
34// Module initialization.
35// -----------------------------------------------------------------------------
36NB_MODULE(_mlir, m) {
37 // disable leak warnings which tend to be false positives.
38 nb::set_leak_warnings(false);
39
40 m.doc() = "MLIR Python Native Extension";
41 populateRoot(m);
42 // Define and populate IR submodule.
43 auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
44 populateIRCore(irModule);
45 populateIRAffine(irModule);
46 populateIRAttributes(irModule);
47 populateIRInterfaces(irModule);
48 populateIRTypes(irModule);
49
50 auto rewriteModule = m.def_submodule("rewrite", "MLIR Rewrite Bindings");
51 populateRewriteSubmodule(rewriteModule);
52
53 // Define and populate PassManager submodule.
54 auto passManagerModule =
55 m.def_submodule("passmanager", "MLIR Pass Management Bindings");
56 populatePassManagerSubmodule(passManagerModule);
57 nanobind::register_exception_translator(
58 [](const std::exception_ptr &p, void *payload) {
59 // We can't define exceptions with custom fields through pybind, so
60 // instead the exception class is defined in python and imported here.
61 try {
62 if (p)
63 std::rethrow_exception(p);
64 } catch (const MLIRError &e) {
65 nanobind::object obj =
66 nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
67 .attr("MLIRError")(e.message, e.errorDiagnostics);
68 PyErr_SetObject(PyExc_Exception, obj.ptr());
69 }
70 });
71}
#define MAKE_MLIR_PYTHON_QUALNAME(local)
Definition Interop.h:57
NB_MODULE(_mlir, m)
MLIR_PYTHON_API_EXPORTED void populateRoot(nanobind::module_ &m)
void populateRewriteSubmodule(nb::module_ &m)
Create the mlir.rewrite here.
Definition Rewrite.cpp:328
MLIR_PYTHON_API_EXPORTED void populateIRTypes(nanobind::module_ &m)
void populatePassManagerSubmodule(nb::module_ &m)
Create the mlir.passmanager here.
Definition Pass.cpp:66
MLIR_PYTHON_API_EXPORTED void populateIRCore(nanobind::module_ &m)
MLIR_PYTHON_API_EXPORTED void populateIRAttributes(nanobind::module_ &m)
Include the generated interface declarations.
Custom exception that allows access to error diagnostic information.
Definition IRCore.h:1331
std::vector< PyDiagnostic::DiagnosticInfo > errorDiagnostics
Definition IRCore.h:1336