--- /dev/null
+//===-- mlir-c/Debug.h - C API for MLIR/LLVM debugging functions --*- C -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
+// Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir-c/Support.h"
+
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/// Sets the global debugging flag.
+MLIR_CAPI_EXPORTED void mlirEnableGlobalDebug(bool enable);
+
+/// Retuns `true` if the global debugging flag is set, false otherwise.
+MLIR_CAPI_EXPORTED bool mlirIsGlobalDebugEnabled();
+
+#ifdef __cplusplus
+}
+#endif
+
+#ifndef MLIR_C_DEBUG_H
+#define MLIR_C_DEBUG_H
+#endif // MLIR_C_DEBUG_H
typedef struct MlirNamedAttribute MlirNamedAttribute;
//===----------------------------------------------------------------------===//
-// Global API.
-//===----------------------------------------------------------------------===//
-
-/// Set the global debugging flag.
-MLIR_CAPI_EXPORTED void mlirEnableGlobalDebug(bool enable);
-
-//===----------------------------------------------------------------------===//
// Context API.
//===----------------------------------------------------------------------===//
#include "mlir-c/Bindings/Python/Interop.h"
#include "mlir-c/BuiltinAttributes.h"
#include "mlir-c/BuiltinTypes.h"
+#include "mlir-c/Debug.h"
#include "mlir-c/Registration.h"
#include "llvm/ADT/SmallVector.h"
#include <pybind11/stl.h>
// Utilities.
//------------------------------------------------------------------------------
-// Helper for creating an @classmethod.
+/// Helper for creating an @classmethod.
template <class Func, typename... Args>
py::object classmethod(Func f, Args... args) {
py::object cf = py::cpp_function(f, args...);
return mlirStringRefCreate(s.data(), s.size());
}
+/// Wrapper for the global LLVM debugging flag.
+struct PyGlobalDebugFlag {
+ static void set(py::object &o, bool enable) { mlirEnableGlobalDebug(enable); }
+
+ static bool get(py::object) { return mlirIsGlobalDebugEnabled(); }
+
+ static void bind(py::module &m) {
+ // Debug flags.
+ py::class_<PyGlobalDebugFlag>(m, "_GlobalDebug")
+ .def_property_static("flag", &PyGlobalDebugFlag::get,
+ &PyGlobalDebugFlag::set, "LLVM-wide debug flag");
+ }
+};
+
//------------------------------------------------------------------------------
// Collections.
//------------------------------------------------------------------------------
void mlir::python::populateIRCore(py::module &m) {
//----------------------------------------------------------------------------
- // Mapping of Global functions
- //----------------------------------------------------------------------------
- m.def("_enable_debug", [](bool enable) { mlirEnableGlobalDebug(enable); });
-
- //----------------------------------------------------------------------------
- // Mapping of MlirContext
+ // Mapping of MlirContext.
//----------------------------------------------------------------------------
py::class_<PyMlirContext>(m, "Context")
.def(py::init<>(&PyMlirContext::createNewContextForInit))
PyOpResultList::bind(m);
PyRegionIterator::bind(m);
PyRegionList::bind(m);
+
+ // Debug bindings.
+ PyGlobalDebugFlag::bind(m);
}
_reexport_cext("ir", __name__)
del _reexport_cext
-# Extra functions that are not visible to _reexport_cext.
-# TODO: is this really necessary?
-from _mlir.ir import _enable_debug
-_enable_debug = _enable_debug
\ No newline at end of file
+add_subdirectory(Debug)
add_subdirectory(Dialect)
add_subdirectory(Conversion)
add_subdirectory(ExecutionEngine)
--- /dev/null
+add_mlir_public_c_api_library(MLIRCAPIDebug
+ Debug.cpp
+
+ LINK_LIBS PUBLIC
+ MLIRSupport
+)
--- /dev/null
+//===- Debug.cpp - C Interface for MLIR/LLVM Debugging Functions ----------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir-c/Debug.h"
+#include "mlir-c/Support.h"
+
+#include "mlir/CAPI/Support.h"
+
+#include "llvm/Support/Debug.h"
+
+void mlirEnableGlobalDebug(bool enable) { llvm::DebugFlag = enable; }
+
+bool mlirIsGlobalDebugEnabled() { return llvm::DebugFlag; }
using namespace mlir;
//===----------------------------------------------------------------------===//
-// Global API.
-//===----------------------------------------------------------------------===//
-
-void mlirEnableGlobalDebug(bool enable) { ::llvm::DebugFlag = true; }
-
-//===----------------------------------------------------------------------===//
// Context API.
//===----------------------------------------------------------------------===//
assert Context._get_live_count() == 0
-# CHECK-LABEL: TEST: testExports
-def testExports():
- from mlir.ir import _enable_debug
-
-run(testExports)
-
-
# CHECK-LABEL: TEST: testContextEnterExit
def testContextEnterExit():
with Context() as ctx:
--- /dev/null
+# RUN: %PYTHON %s | FileCheck %s
+
+from mlir.ir import *
+
+def run(f):
+ print("\nTEST:", f.__name__)
+ f()
+
+
+# CHECK-LABEL: TEST: testNameIsPrivate
+def testNameIsPrivate():
+ # `import *` ignores private names starting with an understore, so the debug
+ # flag shouldn't be visible unless explicitly imported.
+ try:
+ _GlobalDebug.flag = True
+ except NameError:
+ pass
+ else:
+ assert False, "_GlobalDebug must not be available by default"
+
+run(testNameIsPrivate)
+
+
+# CHECK-LABEL: TEST: testDebugDlag
+def testDebugDlag():
+ # Private names must be imported expilcitly.
+ from mlir.ir import _GlobalDebug
+
+ # CHECK: False
+ print(_GlobalDebug.flag)
+ _GlobalDebug.flag = True
+ # CHECK: True
+ print(_GlobalDebug.flag)
+ _GlobalDebug.flag = False
+ # CHECK: False
+ print(_GlobalDebug.flag)
+
+run(testDebugDlag)
+