|
15 | 15 | #include <unordered_set> |
16 | 16 | #include <vector> |
17 | 17 |
|
18 | | -#include "NanobindUtils.h" |
| 18 | +#include "mlir-c/Debug.h" |
19 | 19 | #include "mlir-c/IR.h" |
20 | 20 | #include "mlir-c/Support.h" |
| 21 | +#include "mlir/Bindings/Python/NanobindUtils.h" |
21 | 22 | #include "mlir/CAPI/Support.h" |
| 23 | + |
22 | 24 | #include "llvm/ADT/DenseMap.h" |
23 | 25 | #include "llvm/ADT/StringExtras.h" |
24 | 26 | #include "llvm/ADT/StringRef.h" |
@@ -200,6 +202,50 @@ class PyGlobals { |
200 | 202 | TypeIDAllocator typeIDAllocator; |
201 | 203 | }; |
202 | 204 |
|
| 205 | +/// Wrapper for the global LLVM debugging flag. |
| 206 | +struct PyGlobalDebugFlag { |
| 207 | + static void set(nanobind::object &o, bool enable) { |
| 208 | + nanobind::ft_lock_guard lock(mutex); |
| 209 | + mlirEnableGlobalDebug(enable); |
| 210 | + } |
| 211 | + |
| 212 | + static bool get(const nanobind::object &) { |
| 213 | + nanobind::ft_lock_guard lock(mutex); |
| 214 | + return mlirIsGlobalDebugEnabled(); |
| 215 | + } |
| 216 | + |
| 217 | + static void bind(nanobind::module_ &m) { |
| 218 | + // Debug flags. |
| 219 | + nanobind::class_<PyGlobalDebugFlag>(m, "_GlobalDebug") |
| 220 | + .def_prop_rw_static("flag", &PyGlobalDebugFlag::get, |
| 221 | + &PyGlobalDebugFlag::set, "LLVM-wide debug flag.") |
| 222 | + .def_static( |
| 223 | + "set_types", |
| 224 | + [](const std::string &type) { |
| 225 | + nanobind::ft_lock_guard lock(mutex); |
| 226 | + mlirSetGlobalDebugType(type.c_str()); |
| 227 | + }, |
| 228 | + nanobind::arg("types"), |
| 229 | + "Sets specific debug types to be produced by LLVM.") |
| 230 | + .def_static( |
| 231 | + "set_types", |
| 232 | + [](const std::vector<std::string> &types) { |
| 233 | + std::vector<const char *> pointers; |
| 234 | + pointers.reserve(types.size()); |
| 235 | + for (const std::string &str : types) |
| 236 | + pointers.push_back(str.c_str()); |
| 237 | + nanobind::ft_lock_guard lock(mutex); |
| 238 | + mlirSetGlobalDebugTypes(pointers.data(), pointers.size()); |
| 239 | + }, |
| 240 | + nanobind::arg("types"), |
| 241 | + "Sets multiple specific debug types to be produced by LLVM."); |
| 242 | + } |
| 243 | + |
| 244 | +private: |
| 245 | + static nanobind::ft_mutex mutex; |
| 246 | +}; |
| 247 | + |
| 248 | + |
203 | 249 | } // namespace python |
204 | 250 | } // namespace mlir |
205 | 251 |
|
|
0 commit comments