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

dialects: (csl) adding constructors for CSL ops #2742

Merged
merged 3 commits into from
Jun 17, 2024
Merged
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
76 changes: 76 additions & 0 deletions xdsl/dialects/csl.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,13 @@ class ImportModuleConstOp(IRDLOperation):

result = result_def(ImportedModuleType)

def __init__(self, name: str, params: SSAValue | Operation | None = None):
super().__init__(
operands=[params],
result_types=[ImportedModuleType()],
properties={"module": StringAttr(name)},
)


@irdl_op_definition
class ConstStructOp(IRDLOperation):
Expand All @@ -398,6 +405,18 @@ def verify_(self) -> None:
"Number of ssa_fields has to match the number of arguments"
)

def __init__(self, *args: tuple[str, Operation]):
operands: list[Operation] = []
fields: list[StringAttr] = []
for fname, op in args:
fields.append(StringAttr(fname))
operands.append(op)
super().__init__(
operands=[operands],
result_types=[ComptimeStructType()],
properties={"ssa_fields": ArrayAttr(fields)},
)


@irdl_op_definition
class GetColorOp(IRDLOperation):
Expand All @@ -406,6 +425,9 @@ class GetColorOp(IRDLOperation):
id = operand_def(IntegerType)
res = result_def(ColorType)

def __init__(self, op: Operation):
super().__init__(operands=[op], result_types=[ColorType()])


@irdl_op_definition
class MemberAccessOp(IRDLOperation):
Expand Down Expand Up @@ -438,6 +460,21 @@ class MemberCallOp(IRDLOperation):

result = opt_result_def(Attribute)

def __init__(
self,
fname: str,
result_type: Attribute,
struct: Operation,
*params: SSAValue | Operation,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I'd like this to not be a vararg, and instead be a Sequence[SSAValue | Operation].

Having a vararg after a bunch of other arguments would make the code less readable at the call site imo.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As long as it's limited to function name+type, it should hopefully be fine enough to avoid me doing cross-PR changes (:

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm strongly in favour of switching to Sequence here, as it's otherwise out of line with the other call operations...

):
super().__init__(
operands=[struct, params],
result_types=[result_type],
properties={
"field": StringAttr(fname),
},
)


@irdl_op_definition
class FuncOp(_FuncBase):
Expand Down Expand Up @@ -703,6 +740,16 @@ class SetTileCodeOp(IRDLOperation):
y_coord = operand_def(IntegerType)
params = opt_operand_def(ComptimeStructType)

def __init__(
self,
fname: str | StringAttr,
x_coord: SSAValue | Operation,
y_coord: SSAValue | Operation,
params: SSAValue | Operation | None = None,
):
name = StringAttr(fname) if isinstance(fname, str) else fname
super().__init__(operands=[x_coord, y_coord, params], properties={"file": name})


class _GetDsdOp(IRDLOperation, ABC):
"""
Expand Down Expand Up @@ -1486,6 +1533,13 @@ class ParamOp(IRDLOperation):

res = result_def(T)

def __init__(self, name: str, result_type: T):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure if this is allowed in python... @PapyChacal do you have any input on this? I think you know more on this than I do...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I thought that's what make TypeVars so neat
https://mypy.readthedocs.io/en/stable/generics.html

Copy link
Collaborator

Choose a reason for hiding this comment

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

But T is not a TypeVar, it's an IRDL constraint! These two are sadly quite different...

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think it's necessarily wrong, but it's not expressing what you may think it is.

Copy link
Collaborator Author

@n-io n-io Jun 17, 2024

Choose a reason for hiding this comment

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

You're right, I misread, it's not a TypeVar. I think it's not an IRDL constraint either, though, it should just be a union type with some additional meta data.

super().__init__(
operands=[[]],
result_types=[result_type],
properties={"param_name": StringAttr(name)},
)


@irdl_op_definition
class SignednessCastOp(IRDLOperation):
Expand All @@ -1511,6 +1565,22 @@ def verify_(self) -> None:
"Input and output type must be of different signedness"
)

def __init__(
self, op: SSAValue | Operation, result_type: IntegerType | None = None
):
if result_type is None:
typ = op.results[0].type if isinstance(op, Operation) else op.type
assert isinstance(typ, IntegerType)
result_type = IntegerType(
typ.width,
(
Signedness.SIGNLESS
if typ.signedness.data == Signedness.UNSIGNED
else Signedness.UNSIGNED
),
)
super().__init__(operands=[op], result_types=[result_type])


@irdl_op_definition
class ConcatStructOp(IRDLOperation):
Expand All @@ -1534,6 +1604,12 @@ class ConcatStructOp(IRDLOperation):

result = result_def(ComptimeStructType)

def __init__(self, op1: Operation, op2: Operation):
super().__init__(
operands=[op1, op2],
result_types=[ComptimeStructType()],
)


CSL = Dialect(
"csl",
Expand Down
Loading