Skip to content

Commit 8f21909

Browse files
authored
[mlir] expose -debug-only equivalent to C and Python (#93175)
These are useful for finer-grain debugging and complement the already exposed global debug flag.
1 parent 720cade commit 8f21909

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

mlir/include/mlir-c/Debug.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ MLIR_CAPI_EXPORTED void mlirEnableGlobalDebug(bool enable);
2121
/// Retuns `true` if the global debugging flag is set, false otherwise.
2222
MLIR_CAPI_EXPORTED bool mlirIsGlobalDebugEnabled();
2323

24+
/// Sets the current debug type, similarly to `-debug-only=type` in the
25+
/// command-line tools. Note that global debug should be enabled for any output
26+
/// to be produced.
27+
MLIR_CAPI_EXPORTED void mlirSetGlobalDebugType(const char *type);
28+
29+
/// Sets multiple current debug types, similarly to `-debug-only=type1,type2" in
30+
/// the command-line tools. Note that global debug should be enabled for any
31+
/// output to be produced.
32+
MLIR_CAPI_EXPORTED void mlirSetGlobalDebugTypes(const char **types, intptr_t n);
33+
34+
/// Checks if `type` is set as the current debug type.
35+
MLIR_CAPI_EXPORTED bool mlirIsCurrentDebugType(const char *type);
36+
2437
#ifdef __cplusplus
2538
}
2639
#endif

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,20 @@ struct PyGlobalDebugFlag {
240240
// Debug flags.
241241
py::class_<PyGlobalDebugFlag>(m, "_GlobalDebug", py::module_local())
242242
.def_property_static("flag", &PyGlobalDebugFlag::get,
243-
&PyGlobalDebugFlag::set, "LLVM-wide debug flag");
243+
&PyGlobalDebugFlag::set, "LLVM-wide debug flag")
244+
.def_static(
245+
"set_types",
246+
[](const std::string &type) {
247+
mlirSetGlobalDebugType(type.c_str());
248+
},
249+
"types"_a, "Sets specific debug types to be produced by LLVM")
250+
.def_static("set_types", [](const std::vector<std::string> &types) {
251+
std::vector<const char *> pointers;
252+
pointers.reserve(types.size());
253+
for (const std::string &str : types)
254+
pointers.push_back(str.c_str());
255+
mlirSetGlobalDebugTypes(pointers.data(), pointers.size());
256+
});
244257
}
245258
};
246259

mlir/lib/CAPI/Debug/Debug.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,21 @@
1616
void mlirEnableGlobalDebug(bool enable) { llvm::DebugFlag = enable; }
1717

1818
bool mlirIsGlobalDebugEnabled() { return llvm::DebugFlag; }
19+
20+
void mlirSetGlobalDebugType(const char *type) {
21+
// Depending on the NDEBUG flag, this name can be either a function or a macro
22+
// that expands to something that isn't a funciton call, so we cannot
23+
// explicitly prefix it with `llvm::` or declare `using` it.
24+
using namespace llvm;
25+
setCurrentDebugType(type);
26+
}
27+
28+
void mlirSetGlobalDebugTypes(const char **types, intptr_t n) {
29+
using namespace llvm;
30+
setCurrentDebugTypes(types, n);
31+
}
32+
33+
bool mlirIsCurrentDebugType(const char *type) {
34+
using namespace llvm;
35+
return isCurrentDebugType(type);
36+
}

0 commit comments

Comments
 (0)