Skip to content

Commit fddfa84

Browse files
committed
works
1 parent 1ffcd72 commit fddfa84

File tree

13 files changed

+63
-87
lines changed

13 files changed

+63
-87
lines changed

mlir/cmake/modules/AddMLIRPython.cmake

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,6 @@ function(add_mlir_python_common_capi_library name)
609609
# Generate the aggregate .so that everything depends on.
610610
add_mlir_aggregate(${name}
611611
SHARED
612-
DISABLE_INSTALL
613612
EMBED_LIBS ${_embed_libs}
614613
)
615614

mlir/include/mlir/Bindings/Python/Globals.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,6 @@ struct PyGlobalDebugFlag {
245245
static nanobind::ft_mutex mutex;
246246
};
247247

248-
249248
} // namespace python
250249
} // namespace mlir
251250

mlir/include/mlir/Bindings/Python/IRCore.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,25 @@ struct MLIRError {
13251325
std::vector<PyDiagnostic::DiagnosticInfo> errorDiagnostics;
13261326
};
13271327

1328+
inline void registerMLIRError() {
1329+
nanobind::register_exception_translator(
1330+
[](const std::exception_ptr &p, void *payload) {
1331+
// We can't define exceptions with custom fields through pybind, so
1332+
// instead the exception class is defined in python and imported here.
1333+
try {
1334+
if (p)
1335+
std::rethrow_exception(p);
1336+
} catch (const MLIRError &e) {
1337+
nanobind::object obj =
1338+
nanobind::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
1339+
.attr("MLIRError")(e.message, e.errorDiagnostics);
1340+
PyErr_SetObject(PyExc_Exception, obj.ptr());
1341+
}
1342+
});
1343+
}
1344+
1345+
void registerMLIRErrorInCore();
1346+
13281347
//------------------------------------------------------------------------------
13291348
// Utilities.
13301349
//------------------------------------------------------------------------------

mlir/lib/Bindings/Python/IRAttributes.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,18 +1852,6 @@ void populateIRAttributes(nb::module_ &m) {
18521852
PyUnitAttribute::bind(m);
18531853

18541854
PyStridedLayoutAttribute::bind(m);
1855-
nb::register_exception_translator([](const std::exception_ptr &p,
1856-
void *payload) {
1857-
// We can't define exceptions with custom fields through pybind, so
1858-
// instead the exception class is defined in python and imported here.
1859-
try {
1860-
if (p)
1861-
std::rethrow_exception(p);
1862-
} catch (const MLIRError &e) {
1863-
nb::object obj = nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
1864-
.attr("MLIRError")(e.message, e.errorDiagnostics);
1865-
PyErr_SetObject(PyExc_Exception, obj.ptr());
1866-
}
1867-
});
1855+
registerMLIRError();
18681856
}
18691857
} // namespace mlir::python

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,6 @@ nb::object PyOperation::create(std::string_view name,
920920
PyMlirContext::ErrorCapture errors(location.getContext());
921921
MlirOperation operation = mlirOperationCreate(&state);
922922
if (!operation.ptr) {
923-
for (auto take : errors.take()) {
924-
std::cout << take.message << "\n";
925-
}
926923
throw MLIRError("Operation creation failed", errors.take());
927924
}
928925
PyOperationRef created =
@@ -1672,7 +1669,7 @@ void PySymbolTable::walkSymbolTables(PyOperationBase &from,
16721669
}
16731670
}
16741671

1675-
void registerMLIRErrorInIRCore() {
1672+
void registerMLIRErrorInCore() {
16761673
nb::register_exception_translator([](const std::exception_ptr &p,
16771674
void *payload) {
16781675
// We can't define exceptions with custom fields through pybind, so

mlir/lib/Bindings/Python/IRInterfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
#include <utility>
1313
#include <vector>
1414

15-
#include "mlir/Bindings/Python/IRCore.h"
1615
#include "mlir-c/BuiltinAttributes.h"
1716
#include "mlir-c/IR.h"
1817
#include "mlir-c/Interfaces.h"
1918
#include "mlir-c/Support.h"
19+
#include "mlir/Bindings/Python/IRCore.h"
2020
#include "mlir/Bindings/Python/Nanobind.h"
2121
#include "llvm/ADT/STLExtras.h"
2222
#include "llvm/ADT/SmallVector.h"

mlir/lib/Bindings/Python/IRTypes.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
#include <optional>
1515

16-
#include "mlir/Bindings/Python/NanobindUtils.h"
1716
#include "mlir-c/BuiltinAttributes.h"
1817
#include "mlir-c/BuiltinTypes.h"
1918
#include "mlir-c/Support.h"
19+
#include "mlir/Bindings/Python/NanobindUtils.h"
2020

2121
namespace nb = nanobind;
2222
using namespace mlir;
@@ -1175,18 +1175,6 @@ void populateIRTypes(nb::module_ &m) {
11751175
PyTupleType::bind(m);
11761176
PyFunctionType::bind(m);
11771177
PyOpaqueType::bind(m);
1178-
nb::register_exception_translator([](const std::exception_ptr &p,
1179-
void *payload) {
1180-
// We can't define exceptions with custom fields through pybind, so
1181-
// instead the exception class is defined in python and imported here.
1182-
try {
1183-
if (p)
1184-
std::rethrow_exception(p);
1185-
} catch (const MLIRError &e) {
1186-
nb::object obj = nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
1187-
.attr("MLIRError")(e.message, e.errorDiagnostics);
1188-
PyErr_SetObject(PyExc_Exception, obj.ptr());
1189-
}
1190-
});
1191-
}
1178+
registerMLIRError();
11921179
}
1180+
} // namespace mlir::python

mlir/lib/Bindings/Python/MainModule.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,29 +2250,13 @@ static void populateIRCore(nb::module_ &m) {
22502250

22512251
// Attribute builder getter.
22522252
PyAttrBuilderMap::bind(m);
2253-
2254-
// nb::register_exception_translator([](const std::exception_ptr &p,
2255-
// void *payload) {
2256-
// // We can't define exceptions with custom fields through pybind, so
2257-
// instead
2258-
// // the exception class is defined in python and imported here.
2259-
// try {
2260-
// if (p)
2261-
// std::rethrow_exception(p);
2262-
// } catch (const MLIRError &e) {
2263-
// nb::object obj = nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
2264-
// .attr("MLIRError")(e.message, e.errorDiagnostics);
2265-
// PyErr_SetObject(PyExc_Exception, obj.ptr());
2266-
// }
2267-
// });
22682253
}
22692254

22702255
namespace mlir::python {
22712256
void populateIRAffine(nb::module_ &m);
22722257
void populateIRAttributes(nb::module_ &m);
22732258
void populateIRInterfaces(nb::module_ &m);
22742259
void populateIRTypes(nb::module_ &m);
2275-
void registerMLIRErrorInIRCore();
22762260
} // namespace mlir::python
22772261

22782262
// -----------------------------------------------------------------------------
@@ -2415,18 +2399,6 @@ NB_MODULE(_mlir, m) {
24152399
auto passManagerModule =
24162400
m.def_submodule("passmanager", "MLIR Pass Management Bindings");
24172401
populatePassManagerSubmodule(passManagerModule);
2418-
registerMLIRErrorInIRCore();
2419-
nb::register_exception_translator([](const std::exception_ptr &p,
2420-
void *payload) {
2421-
// We can't define exceptions with custom fields through pybind, so
2422-
// instead the exception class is defined in python and imported here.
2423-
try {
2424-
if (p)
2425-
std::rethrow_exception(p);
2426-
} catch (const MLIRError &e) {
2427-
nb::object obj = nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
2428-
.attr("MLIRError")(e.message, e.errorDiagnostics);
2429-
PyErr_SetObject(PyExc_Exception, obj.ptr());
2430-
}
2431-
});
2402+
registerMLIRError();
2403+
registerMLIRErrorInCore();
24322404
}

mlir/lib/Bindings/Python/Pass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
#include "Pass.h"
1010

11+
#include "mlir-c/Pass.h"
1112
#include "mlir/Bindings/Python/Globals.h"
1213
#include "mlir/Bindings/Python/IRCore.h"
13-
#include "mlir-c/Pass.h"
1414
// clang-format off
1515
#include "mlir/Bindings/Python/Nanobind.h"
1616
#include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.
@@ -254,4 +254,5 @@ void mlir::python::populatePassManagerSubmodule(nb::module_ &m) {
254254
},
255255
"Print the textual representation for this PassManager, suitable to "
256256
"be passed to `parse` for round-tripping.");
257+
registerMLIRError();
257258
}

mlir/lib/Bindings/Python/Rewrite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#include "Rewrite.h"
1010

11-
#include "mlir/Bindings/Python/IRCore.h"
1211
#include "mlir-c/IR.h"
1312
#include "mlir-c/Rewrite.h"
1413
#include "mlir-c/Support.h"
14+
#include "mlir/Bindings/Python/IRCore.h"
1515
// clang-format off
1616
#include "mlir/Bindings/Python/Nanobind.h"
1717
#include "mlir-c/Bindings/Python/Interop.h" // This is expected after nanobind.

0 commit comments

Comments
 (0)