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: (func) add init to func.FuncOp #1037

Merged
merged 2 commits into from
May 31, 2023
Merged
Changes from 1 commit
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
59 changes: 30 additions & 29 deletions xdsl/dialects/func.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ class FuncOp(IRDLOperation):
function_type: OpAttr[FunctionType]
sym_visibility: OptOpAttr[StringAttr]

def __init__(
self,
name: str,
function_type: FunctionType | tuple[Sequence[Attribute], Sequence[Attribute]],
region: Region,
visibility: StringAttr | str | None = None,
):
if isinstance(visibility, str):
visibility = StringAttr(visibility)
if isinstance(function_type, tuple):
inputs, outputs = function_type
function_type = FunctionType.from_lists(inputs, outputs)
attributes: dict[str, Attribute | None] = {
"sym_name": StringAttr(name),
"function_type": function_type,
"sym_visibility": visibility,
}
super().__init__(attributes=attributes, regions=[region])

def verify_(self) -> None:
# If this is an empty region (external function), then return
if len(self.body.blocks) == 0:
Expand Down Expand Up @@ -161,30 +180,19 @@ def from_callable(
return_types: Sequence[Attribute],
func: Block.BlockCallback,
) -> FuncOp:
type_attr = FunctionType.from_lists(input_types, return_types)
attributes: dict[str, Attribute] = {
"sym_name": StringAttr(name),
"function_type": type_attr,
"sym_visibility": StringAttr("private"),
}
op = FuncOp.build(
attributes=attributes,
regions=[Region(Block.from_callable(input_types, func))],
)
return op
region = Region(Block.from_callable(input_types, func))
return FuncOp(name, (input_types, return_types), region, "private")

@staticmethod
def external(
name: str, input_types: Sequence[Attribute], return_types: Sequence[Attribute]
) -> FuncOp:
type_attr = FunctionType.from_lists(input_types, return_types)
attributes: dict[str, Attribute] = {
"sym_name": StringAttr(name),
"function_type": type_attr,
"sym_visibility": StringAttr("private"),
}
op = FuncOp.build(attributes=attributes, regions=[Region()])
return op
return FuncOp(
name=name,
function_type=(input_types, return_types),
region=Region(),
visibility="private",
)

@staticmethod
def from_region(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe deprecate/remove this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea, although I think that can be an issue for the time being: #1038

Expand All @@ -194,16 +202,9 @@ def from_region(
region: Region,
visibility: StringAttr | str | None = None,
) -> FuncOp:
if isinstance(visibility, str):
visibility = StringAttr(visibility)
type_attr = FunctionType.from_lists(input_types, return_types)
attributes: dict[str, Attribute | None] = {
"sym_name": StringAttr(name),
"function_type": type_attr,
"sym_visibility": visibility,
}
op = FuncOp.build(attributes=attributes, regions=[region])
return op
return FuncOp(
name=name, function_type=(input_types, return_types), region=region
)

def replace_argument_type(self, arg: int | BlockArgument, new_type: Attribute):
"""
Expand Down