Skip to content
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

dialect: (riscv) Custom print attributes #1363

Merged
merged 5 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions tests/filecheck/dialects/riscv/riscv_ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@
riscv.j {"immediate" = #riscv.label<"label">} : () -> ()
// CHECK-NEXT: riscv.j {"immediate" = #riscv.label<"label">} : () -> ()

riscv.jalr %0 {"immediate" = 1 : i32}: (!riscv.reg<>) -> ()
// CHECK-NEXT: riscv.jalr %0 {"immediate" = 1 : i32} : (!riscv.reg<>) -> ()
riscv.jalr %0 {"immediate" = 1 : i32, "rd" = !riscv.reg<>} : (!riscv.reg<>) -> ()
// CHECK-NEXT: riscv.jalr %0 {"immediate" = 1 : i32, "rd" = !riscv.reg<>} : (!riscv.reg<>) -> ()
riscv.jalr %0 {"immediate" = #riscv.label<"label">} : (!riscv.reg<>) -> ()
// CHECK-NEXT: riscv.jalr %0 {"immediate" = #riscv.label<"label">} : (!riscv.reg<>) -> ()
riscv.jalr %0, 1: (!riscv.reg<>) -> ()
// CHECK-NEXT: riscv.jalr %0, 1 : (!riscv.reg<>) -> ()
riscv.jalr %0, 1, !riscv.reg<> : (!riscv.reg<>) -> ()
// CHECK-NEXT: riscv.jalr %0, 1, !riscv.reg<> : (!riscv.reg<>) -> ()
riscv.jalr %0, "label" : (!riscv.reg<>) -> ()
// CHECK-NEXT: riscv.jalr %0, "label" : (!riscv.reg<>) -> ()

riscv.ret : () -> ()
// CHECK-NEXT: riscv.ret : () -> ()
Expand Down
72 changes: 63 additions & 9 deletions xdsl/dialects/riscv.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from collections.abc import Mapping, Set
from io import StringIO
from typing import IO, ClassVar, Sequence, TypeAlias

Expand Down Expand Up @@ -42,7 +43,7 @@
var_operand_def,
var_result_def,
)
from xdsl.parser import AttrParser, Parser
from xdsl.parser import AttrParser, Parser, UnresolvedOperand
from xdsl.printer import Printer
from xdsl.traits import IsTerminator, NoTerminator
from xdsl.utils.exceptions import VerifyException
Expand Down Expand Up @@ -333,13 +334,11 @@ def assembly_line(self) -> str | None:

@classmethod
def parse(cls, parser: Parser) -> Self:
args = parser.parse_optional_undelimited_comma_separated_list(
parser.parse_optional_unresolved_operand,
parser.parse_unresolved_operand,
)
if args is None:
args = []
attributes = parser.parse_optional_attr_dict()
args = cls.parse_unresolved_operand(parser)
custom_attributes = cls.parse_attributes(parser)
remaining_attributes = parser.parse_optional_attr_dict()
# TODO ensure distinct keys for attributes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please file an issue and reference it in this comment? Feels like it should be common infrastructure

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed an issue #1369

attributes = custom_attributes | remaining_attributes
regions = parser.parse_region_list()
parser.parse_punctuation(":")
func_type = parser.parse_function_type()
Expand All @@ -351,15 +350,45 @@ def parse(cls, parser: Parser) -> Self:
regions=regions,
)

@classmethod
def parse_unresolved_operand(cls, parser: Parser) -> list[UnresolvedOperand]:
"""
Parse a list of comma separated unresolved operands.

Notice that this method will consume trailing comma.
"""
if operand := parser.parse_optional_unresolved_operand():
operands = [operand]
while parser.parse_optional_punctuation(",") and (
operand := parser.parse_optional_unresolved_operand()
):
operands.append(operand)
return operands
return []

@classmethod
def parse_attributes(cls, parser: Parser) -> Mapping[str, Attribute]:
kingiler marked this conversation as resolved.
Show resolved Hide resolved
"""
Parse custom printed attributes. Subclass may overwrite this method.
kingiler marked this conversation as resolved.
Show resolved Hide resolved
"""
return parser.parse_optional_attr_dict()

def print(self, printer: Printer) -> None:
if self.operands:
printer.print(" ")
printer.print_list(self.operands, printer.print_operand)
printer.print_op_attributes(self.attributes)
self.print_attributes(printer)
printer.print_regions(self.regions)
printer.print(" : ")
printer.print_operation_type(self)

def print_attributes(self, printer: Printer) -> Set[str]:
kingiler marked this conversation as resolved.
Show resolved Hide resolved
"""
Custom print specific attributes and return the set of custom printed attributes. Subclass may overwrite this method.
kingiler marked this conversation as resolved.
Show resolved Hide resolved
"""
printer.print_op_attributes(self.attributes)
return self.attributes.keys()


AssemblyInstructionArg: TypeAlias = (
AnyIntegerAttr | LabelAttr | SSAValue | IntRegisterType | str
Expand Down Expand Up @@ -721,6 +750,31 @@ def __init__(
def assembly_line_args(self) -> tuple[AssemblyInstructionArg | None, ...]:
return self.rd, self.rs1, self.immediate

@classmethod
def parse_attributes(cls, parser: Parser) -> Mapping[str, Attribute]:
attributes = dict[str, Attribute]()
if immediate := parser.parse_optional_integer(allow_boolean=False):
attributes["immediate"] = IntegerAttr(
immediate, IntegerType(12, Signedness.SIGNED)
)
elif immediate := parser.parse_optional_str_literal():
attributes["immediate"] = LabelAttr(immediate)
if parser.parse_optional_punctuation(","):
attributes["rd"] = parser.parse_attribute()
return attributes

def print_attributes(self, printer: Printer) -> Set[str]:
printer.print(", ")
match self.immediate:
case IntegerAttr():
printer.print(self.immediate.value.data)
case LabelAttr():
printer.print_string_literal(self.immediate.data)
if self.rd is not None:
printer.print(", ")
printer.print_attribute(self.rd)
return {"immediate", "rd"}


class RdRsIntegerOperation(IRDLOperation, RISCVInstruction, ABC):
"""
Expand Down