Skip to content
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
24 changes: 18 additions & 6 deletions src/pydsl/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from mlir.dialects import transform
from mlir.dialects.transform import loop, structured
from mlir.ir import IndexType, IntegerAttr, OpView, UnitAttr
from mlir.ir import IndexType, IntegerAttr, OpView, UnitAttr, Attribute

from pydsl.macro import CallMacro, Compiled, Evaluated, Uncompiled
from pydsl.protocols import SubtreeOut, ToMLIRBase, lower_single
Expand Down Expand Up @@ -98,17 +98,29 @@ def decorator(mlir):


@CallMacro.generate()
def tag(visitor: ToMLIRBase, attr_name: Evaluated[str]) -> Evaluated[Callable]:
def tag(
visitor: ToMLIRBase,
attr_name: Evaluated[str],
attr_value: Evaluated[str | None] = None,
) -> Evaluated[Callable]:
"""
Tags the `mlir` MLIR operation with a MLIR unit attribute with name
Tags the `mlir` MLIR operation with a MLIR attribute with name
`attr_name`.

Arguments:
- `mlir`: AST. The AST node whose equivalent MLIR Operator is to be tagged
with the unit attribute
- `attr_name`: str. The name of the unit attribute
with the attribute
- `attr_name`: str. The name of the attribute
- `attr_value`: str. An optional argument that will convert the attribute
from a unit attribute to a key/value attribute pair.
"""
return attr_setter(attr_name, UnitAttr.get())
if type(attr_name) is not str:
raise TypeError("Attribute name is not a string")

if attr_value is None:
return attr_setter(attr_name, UnitAttr.get())
else:
return attr_setter(attr_name, Attribute.parse(attr_value))


@CallMacro.generate()
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,22 @@ def f(m: MemRef[UInt32, 4, 4]):
)


def test_tag_with_string_value():
@compile(globals())
@tag("mytag", "0")
def f():
a: F32 = 0.0
b: F32 = 1.0

mlir = f.emit_mlir()

# No runtime check: testing function of tag only
assert r"mytag = 0" in mlir


if __name__ == "__main__":
run(test_multiple_recursively_tag)
run(test_multiple_recursively_int_attr)
run(test_cse_then_coalesce)
run(test_outline_loop)
run(test_tag_with_string_value)