[MLIR][Python] Expose PassManager::enableStatistics to CAPI and Python (#162591)

`PassManager::enableStatistics` seems currently missing in both C API
and Python bindings. So here we added them in this PR, which includes
the `PassDisplayMode` enum type and the `EnableStatistics` method.
This commit is contained in:
Twice 2025-10-09 20:11:19 +08:00 committed by GitHub
parent 70b19a8ad3
commit d12b0e2539
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 0 deletions

View file

@ -92,6 +92,18 @@ mlirPassManagerEnableVerifier(MlirPassManager passManager, bool enable);
MLIR_CAPI_EXPORTED void
mlirPassManagerEnableTiming(MlirPassManager passManager);
/// Enumerated type of pass display modes.
/// Mainly used in mlirPassManagerEnableStatistics.
typedef enum {
MLIR_PASS_DISPLAY_MODE_LIST,
MLIR_PASS_DISPLAY_MODE_PIPELINE,
} MlirPassDisplayMode;
/// Enable pass statistics.
MLIR_CAPI_EXPORTED void
mlirPassManagerEnableStatistics(MlirPassManager passManager,
MlirPassDisplayMode displayMode);
/// Nest an OpPassManager under the top-level PassManager, the nested
/// passmanager will only run on operations matching the provided name.
/// The returned OpPassManager will be destroyed when the parent is destroyed.

View file

@ -57,6 +57,13 @@ private:
/// Create the `mlir.passmanager` here.
void mlir::python::populatePassManagerSubmodule(nb::module_ &m) {
//----------------------------------------------------------------------------
// Mapping of enumerated types
//----------------------------------------------------------------------------
nb::enum_<MlirPassDisplayMode>(m, "PassDisplayMode")
.value("LIST", MLIR_PASS_DISPLAY_MODE_LIST)
.value("PIPELINE", MLIR_PASS_DISPLAY_MODE_PIPELINE);
//----------------------------------------------------------------------------
// Mapping of MlirExternalPass
//----------------------------------------------------------------------------
@ -139,6 +146,14 @@ void mlir::python::populatePassManagerSubmodule(nb::module_ &m) {
mlirPassManagerEnableTiming(passManager.get());
},
"Enable pass timing.")
.def(
"enable_statistics",
[](PyPassManager &passManager, MlirPassDisplayMode displayMode) {
mlirPassManagerEnableStatistics(passManager.get(), displayMode);
},
"displayMode"_a =
MlirPassDisplayMode::MLIR_PASS_DISPLAY_MODE_PIPELINE,
"Enable pass statistics.")
.def_static(
"parse",
[](const std::string &pipeline, DefaultingPyMlirContext context) {

View file

@ -13,6 +13,7 @@
#include "mlir/CAPI/Support.h"
#include "mlir/CAPI/Utils.h"
#include "mlir/Pass/PassManager.h"
#include "llvm/Support/ErrorHandling.h"
#include <optional>
using namespace mlir;
@ -79,6 +80,20 @@ void mlirPassManagerEnableTiming(MlirPassManager passManager) {
unwrap(passManager)->enableTiming();
}
void mlirPassManagerEnableStatistics(MlirPassManager passManager,
MlirPassDisplayMode displayMode) {
PassDisplayMode mode;
switch (displayMode) {
case MLIR_PASS_DISPLAY_MODE_LIST:
mode = PassDisplayMode::List;
break;
case MLIR_PASS_DISPLAY_MODE_PIPELINE:
mode = PassDisplayMode::Pipeline;
break;
}
unwrap(passManager)->enableStatistics(mode);
}
MlirOpPassManager mlirPassManagerGetNestedUnder(MlirPassManager passManager,
MlirStringRef operationName) {
return wrap(&unwrap(passManager)->nest(unwrap(operationName)));

View file

@ -435,3 +435,23 @@ def testPrintIrTree():
print_file_tree(temp_dir)
log("// Tree printing end")
# CHECK-LABEL: TEST: testEnableStatistics
@run
def testEnableStatistics():
with Context() as ctx:
module = ModuleOp.parse(
"""
module {
func.func @main() {
%0 = arith.constant 10
return
}
}
"""
)
pm = PassManager.parse("builtin.module(canonicalize)")
pm.enable_statistics()
# CHECK: Pass statistics report
pm.run(module)