Skip to content

Commit

Permalink
dialects: (stream) remove abstract stream read and write operations (#…
Browse files Browse the repository at this point in the history
…3486)

Another incremental change towards getting rid of the stream dialect,
now that the definitions are small enough it feels like the right move
is to duplicate the definitions, as there's no particular reason for
them to be coupled.
  • Loading branch information
superlopuh authored Nov 20, 2024
1 parent f7088a0 commit 5603904
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
35 changes: 33 additions & 2 deletions xdsl/dialects/memref_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
opt_prop_def,
prop_def,
region_def,
result_def,
traits_def,
var_operand_def,
)
Expand Down Expand Up @@ -187,14 +188,44 @@ def offsets(self) -> tuple[tuple[int, ...], ...]:


@irdl_op_definition
class ReadOp(stream.ReadOperation):
class ReadOp(IRDLOperation):
name = "memref_stream.read"

T: ClassVar = VarConstraint("T", AnyAttr())

stream = operand_def(stream.ReadableStreamType.constr(T))
res = result_def(T)

assembly_format = "`from` $stream attr-dict `:` type($res)"

def __init__(self, stream_val: SSAValue, result_type: Attribute | None = None):
if result_type is None:
assert isinstance(stream_type := stream_val.type, stream.ReadableStreamType)
stream_type = cast(stream.ReadableStreamType[Attribute], stream_type)
result_type = stream_type.element_type
super().__init__(operands=[stream_val], result_types=[result_type])

def assembly_line(self) -> str | None:
return None


@irdl_op_definition
class WriteOp(stream.WriteOperation):
class WriteOp(IRDLOperation):
name = "memref_stream.write"

T: ClassVar = VarConstraint("T", AnyAttr())

value = operand_def(T)
stream = operand_def(stream.WritableStreamType.constr(T))

assembly_format = "$value `to` $stream attr-dict `:` type($value)"

def __init__(self, value: SSAValue, stream: SSAValue):
super().__init__(operands=[value, stream])

def assembly_line(self) -> str | None:
return None


@irdl_op_definition
class StreamingRegionOp(IRDLOperation):
Expand Down
29 changes: 27 additions & 2 deletions xdsl/dialects/riscv_snitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
)
from xdsl.ir import Attribute, Block, Dialect, Operation, Region, SSAValue
from xdsl.irdl import (
AnyAttr,
VarConstraint,
attr_def,
base,
Expand Down Expand Up @@ -155,17 +156,41 @@ def assembly_line(self) -> str | None:


@irdl_op_definition
class ReadOp(stream.ReadOperation, RISCVAsmOperation):
class ReadOp(RISCVAsmOperation):
name = "riscv_snitch.read"

T: ClassVar = VarConstraint("T", AnyAttr())

stream = operand_def(stream.ReadableStreamType.constr(T))
res = result_def(T)

assembly_format = "`from` $stream attr-dict `:` type($res)"

def __init__(self, stream_val: SSAValue, result_type: Attribute | None = None):
if result_type is None:
assert isinstance(stream_type := stream_val.type, stream.ReadableStreamType)
stream_type = cast(stream.ReadableStreamType[Attribute], stream_type)
result_type = stream_type.element_type
super().__init__(operands=[stream_val], result_types=[result_type])

def assembly_line(self) -> str | None:
return None


@irdl_op_definition
class WriteOp(stream.WriteOperation, RISCVAsmOperation):
class WriteOp(RISCVAsmOperation):
name = "riscv_snitch.write"

T: ClassVar = VarConstraint("T", AnyAttr())

value = operand_def(T)
stream = operand_def(stream.WritableStreamType.constr(T))

assembly_format = "$value `to` $stream attr-dict `:` type($value)"

def __init__(self, value: SSAValue, stream: SSAValue):
super().__init__(operands=[value, stream])

def assembly_line(self) -> str | None:
return None

Expand Down
45 changes: 1 addition & 44 deletions xdsl/dialects/stream.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
from __future__ import annotations

import abc
from typing import ClassVar, Generic, TypeVar, cast
from typing import Generic, TypeVar

from xdsl.dialects.builtin import ContainerType
from xdsl.ir import (
Attribute,
Dialect,
ParametrizedAttribute,
SSAValue,
TypeAttribute,
)
from xdsl.irdl import (
AnyAttr,
BaseAttr,
GenericAttrConstraint,
IRDLOperation,
ParamAttrConstraint,
ParameterDef,
VarConstraint,
irdl_attr_definition,
operand_def,
result_def,
)

_StreamTypeElement = TypeVar("_StreamTypeElement", bound=Attribute, covariant=True)
Expand Down Expand Up @@ -87,42 +80,6 @@ def constr(
)


class ReadOperation(IRDLOperation, abc.ABC):
"""
Abstract base class for operations that read from a stream.
"""

T: ClassVar = VarConstraint("T", AnyAttr())

stream = operand_def(ReadableStreamType.constr(T))
res = result_def(T)

assembly_format = "`from` $stream attr-dict `:` type($res)"

def __init__(self, stream: SSAValue, result_type: Attribute | None = None):
if result_type is None:
assert isinstance(stream_type := stream.type, ReadableStreamType)
stream_type = cast(ReadableStreamType[Attribute], stream_type)
result_type = stream_type.element_type
super().__init__(operands=[stream], result_types=[result_type])


class WriteOperation(IRDLOperation, abc.ABC):
"""
Abstract base class for operations that write to a stream.
"""

T: ClassVar = VarConstraint("T", AnyAttr())

value = operand_def(T)
stream = operand_def(WritableStreamType.constr(T))

assembly_format = "$value `to` $stream attr-dict `:` type($value)"

def __init__(self, value: SSAValue, stream: SSAValue):
super().__init__(operands=[value, stream])


Stream = Dialect(
"stream",
[],
Expand Down

0 comments on commit 5603904

Please sign in to comment.