Skip to content

[mlir][python] move transform extras #76102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mlir/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ declare_mlir_python_sources(
ROOT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/mlir"
GEN_ENUM_BINDINGS
SOURCES
extras/dialects/transform/__init__.py)
dialects/transform/extras/__init__.py)

declare_mlir_dialect_extension_python_bindings(
ADD_TO_PARENT MLIRPythonSources.Dialects
Expand Down
1 change: 1 addition & 0 deletions mlir/python/mlir/dialects/transform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from .._transform_ops_gen import *
from .._transform_ops_gen import _Dialect
from ..._mlir_libs._mlirDialectsTransform import *
from ..._mlir_libs._mlirDialectsTransform import AnyOpType, OperationType

try:
from ...ir import *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

from __future__ import annotations
from typing import Callable, Optional, Sequence
from typing import Callable, Optional, Sequence, Union

from .... import ir
from ....dialects import transform
from ....dialects.transform import structured
from .. import AnyOpType, OperationType, NamedSequenceOp, YieldOp
from .. import structured


class Handle(ir.Value):
Expand All @@ -25,16 +24,16 @@ def __init__(
self,
v: ir.Value,
*,
parent: Optional[Handle] = None,
children: Optional[Sequence[Handle]] = None,
parent: Optional["Handle"] = None,
children: Optional[Sequence["Handle"]] = None,
):
super().__init__(v)
self.parent = parent
self.children = children if children is not None else []


@ir.register_value_caster(transform.AnyOpType.get_static_typeid())
@ir.register_value_caster(transform.OperationType.get_static_typeid())
@ir.register_value_caster(AnyOpType.get_static_typeid())
@ir.register_value_caster(OperationType.get_static_typeid())
class OpHandle(Handle):
"""
Wrapper around a transform operation handle with methods to chain further
Expand All @@ -52,11 +51,13 @@ def __init__(

def match_ops(
self,
ops: str
| ir.OpView
| structured.MatchInterfaceEnum
| Sequence[str | ir.OpView],
) -> OpHandle:
ops: Union[
str,
ir.OpView,
structured.MatchInterfaceEnum,
Sequence[Union[str, ir.OpView]],
],
) -> "OpHandle":
"""
Emits a `transform.structured.MatchOp`.
Returns a handle to payload ops that match the given names, types, or
Expand All @@ -70,23 +71,23 @@ def match_ops(
if isinstance(ops, str):
ops = structured.MatchInterfaceEnum[ops]
match_op = structured.MatchOp(
transform.AnyOpType.get(),
AnyOpType.get(),
self,
interface=ops,
)

# Handle op name(s), either given directly as string or given as op.
else:
if isinstance(ops, str):
op_type = transform.OperationType.get(ops)
op_type = OperationType.get(ops)
op_names = [ops]
elif isinstance(ops, Sequence):
op_type = transform.AnyOpType.get()
op_type = AnyOpType.get()
op_names = [
op if isinstance(op, str) else op.OPERATION_NAME for op in ops
]
else:
op_type = transform.OperationType.get(ops.OPERATION_NAME)
op_type = OperationType.get(ops.OPERATION_NAME)
op_names = [ops.OPERATION_NAME]
match_op = structured.MatchOp.match_op_names(
op_type,
Expand All @@ -100,7 +101,7 @@ def match_ops(


def insert_transform_script(
block_or_insertion_point: ir.Block | ir.InsertionPoint,
block_or_insertion_point: Union[ir.Block, ir.InsertionPoint],
script: Callable[[OpHandle], None],
dump_script: bool = False,
) -> None:
Expand Down Expand Up @@ -137,12 +138,12 @@ def test_match_ops_single(module: OpHandle):

with context, ir.Location.unknown(context):
with insertion_point:
named_sequence_op = transform.NamedSequenceOp(
"__transform_main", [transform.AnyOpType.get()], []
named_sequence_op = NamedSequenceOp(
"__transform_main", [AnyOpType.get()], []
)
with ir.InsertionPoint(named_sequence_op.body):
script(named_sequence_op.bodyTarget)
transform.YieldOp([])
YieldOp([])

if dump_script:
print(named_sequence_op)
2 changes: 1 addition & 1 deletion mlir/test/python/dialects/transform_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mlir import ir
from mlir.dialects import scf
from mlir.dialects.transform import structured
from mlir.extras.dialects.transform import OpHandle, insert_transform_script
from mlir.dialects.transform.extras import OpHandle, insert_transform_script


def build_transform_script(script: Callable[[OpHandle], None]):
Expand Down