[Mlir-commits] [mlir] [MLIR][Transform][Python] expose transform.debug extension in Python (PR #145550)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Tue Jun 24 09:55:02 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Rolf Morel (rolfmorel)
<details>
<summary>Changes</summary>
---
Full diff: https://github.com/llvm/llvm-project/pull/145550.diff
4 Files Affected:
- (modified) mlir/python/CMakeLists.txt (+9)
- (added) mlir/python/mlir/dialects/TransformDebugExtensionOps.td (+19)
- (added) mlir/python/mlir/dialects/transform/debug.py (+86)
- (added) mlir/test/python/dialects/transform_debug_ext.py (+47)
``````````diff
diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt
index ee07081246fc7..b2daabb2a5957 100644
--- a/mlir/python/CMakeLists.txt
+++ b/mlir/python/CMakeLists.txt
@@ -171,6 +171,15 @@ ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
DIALECT_NAME transform
EXTENSION_NAME transform_pdl_extension)
+declare_mlir_dialect_extension_python_bindings(
+ADD_TO_PARENT MLIRPythonSources.Dialects
+ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
+ TD_FILE dialects/TransformDebugExtensionOps.td
+ SOURCES
+ dialects/transform/debug.py
+ DIALECT_NAME transform
+ EXTENSION_NAME transform_debug_extension)
+
declare_mlir_dialect_python_bindings(
ADD_TO_PARENT MLIRPythonSources.Dialects
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
diff --git a/mlir/python/mlir/dialects/TransformDebugExtensionOps.td b/mlir/python/mlir/dialects/TransformDebugExtensionOps.td
new file mode 100644
index 0000000000000..22a85d2366994
--- /dev/null
+++ b/mlir/python/mlir/dialects/TransformDebugExtensionOps.td
@@ -0,0 +1,19 @@
+//===-- TransformDebugExtensionOps.td - Binding entry point *- tablegen -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Entry point of the generated Python bindings for the Debug extension of the
+// Transform dialect.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef PYTHON_BINDINGS_TRANSFORM_DEBUG_EXTENSION_OPS
+#define PYTHON_BINDINGS_TRANSFORM_DEBUG_EXTENSION_OPS
+
+include "mlir/Dialect/Transform/DebugExtension/DebugExtensionOps.td"
+
+#endif // PYTHON_BINDINGS_TRANSFORM_DEBUG_EXTENSION_OPS
diff --git a/mlir/python/mlir/dialects/transform/debug.py b/mlir/python/mlir/dialects/transform/debug.py
new file mode 100644
index 0000000000000..738c556b1d362
--- /dev/null
+++ b/mlir/python/mlir/dialects/transform/debug.py
@@ -0,0 +1,86 @@
+# 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
+
+from typing import Optional
+
+from ...ir import Attribute, Operation, Value, StringAttr
+from .._transform_debug_extension_ops_gen import *
+from .._transform_pdl_extension_ops_gen import _Dialect
+
+try:
+ from .._ods_common import _cext as _ods_cext
+except ImportError as e:
+ raise RuntimeError("Error loading imports from extension module") from e
+
+from typing import Union
+
+
+ at _ods_cext.register_operation(_Dialect, replace=True)
+class DebugEmitParamAsRemarkOp(DebugEmitParamAsRemarkOp):
+ def __init__(
+ self,
+ param: Attribute,
+ *,
+ anchor: Optional[Operation] = None,
+ message: Optional[Union[StringAttr, str]] = None,
+ loc=None,
+ ip=None,
+ ):
+ if isinstance(message, str):
+ message = StringAttr.get(message)
+
+ super().__init__(
+ param,
+ anchor=anchor,
+ message=message,
+ loc=loc,
+ ip=ip,
+ )
+
+
+def emit_param_as_remark(
+ param: Attribute,
+ *,
+ anchor: Optional[Operation] = None,
+ message: Optional[Union[StringAttr, str]] = None,
+ loc=None,
+ ip=None,
+):
+ return DebugEmitParamAsRemarkOp(
+ param, anchor=anchor, message=message, loc=loc, ip=ip
+ )
+
+del debug_emit_param_as_remark
+
+ at _ods_cext.register_operation(_Dialect, replace=True)
+class DebugEmitRemarkAtOp(DebugEmitRemarkAtOp):
+ def __init__(
+ self,
+ at: Union[Operation, Value],
+ message: Optional[Union[StringAttr, str]] = None,
+ *,
+ loc=None,
+ ip=None,
+ ):
+ if isinstance(message, str):
+ message = StringAttr.get(message)
+
+ super().__init__(
+ at,
+ message,
+ loc=loc,
+ ip=ip,
+ )
+
+
+def emit_remark_at(
+ at: Union[Operation, Value],
+ message: Optional[Union[StringAttr, str]] = None,
+ *,
+ loc=None,
+ ip=None,
+):
+ return DebugEmitRemarkAtOp(at, message, loc=loc, ip=ip)
+
+del debug_emit_remark_at
diff --git a/mlir/test/python/dialects/transform_debug_ext.py b/mlir/test/python/dialects/transform_debug_ext.py
new file mode 100644
index 0000000000000..c96e7e66e03d1
--- /dev/null
+++ b/mlir/test/python/dialects/transform_debug_ext.py
@@ -0,0 +1,47 @@
+# RUN: %PYTHON %s | FileCheck %s
+
+from mlir.ir import *
+from mlir.dialects import transform
+from mlir.dialects.transform import debug
+
+
+def run(f):
+ print("\nTEST:", f.__name__)
+ with Context(), Location.unknown():
+ module = Module.create()
+ with InsertionPoint(module.body):
+ sequence = transform.SequenceOp(
+ transform.FailurePropagationMode.Propagate,
+ [],
+ transform.AnyOpType.get(),
+ )
+ with InsertionPoint(sequence.body):
+ f(sequence.bodyTarget)
+ transform.YieldOp()
+ print(module)
+ return f
+
+
+ at run
+def testDebugEmitParamAsRemark(target):
+ i0 = IntegerAttr.get(IntegerType.get_signless(32), 0)
+ i0_param = transform.ParamConstantOp(transform.AnyParamType.get(), i0)
+ debug.emit_param_as_remark(i0_param)
+ debug.emit_param_as_remark(i0_param, anchor=target, message="some text")
+ # CHECK-LABEL: TEST: testDebugEmitParamAsRemark
+ # CHECK: ^{{.*}}(%[[ARG0:.+]]: !transform.any_op):
+ # CHECK: %[[PARAM:.*]] = transform.param.constant
+ # CHECK: transform.debug.emit_param_as_remark %[[PARAM]]
+ # CHECK: transform.debug.emit_param_as_remark %[[PARAM]]
+ # CHECK-SAME: "some text"
+ # CHECK-SAME: at %[[ARG0]]
+
+
+ at run
+def testDebugEmitRemarkAtOp(target):
+ i0 = IntegerAttr.get(IntegerType.get_signless(32), 0)
+ i0_param = transform.ParamConstantOp(transform.AnyParamType.get(), i0)
+ debug.emit_remark_at(target, "some text")
+ # CHECK-LABEL: TEST: testDebugEmitRemarkAtOp
+ # CHECK: ^{{.*}}(%[[ARG0:.+]]: !transform.any_op):
+ # CHECK: transform.debug.emit_remark_at %[[ARG0]], "some text"
``````````
</details>
https://github.com/llvm/llvm-project/pull/145550
More information about the Mlir-commits
mailing list