Skip to content

Conversation

hsemenenko
Copy link
Contributor

Closes #797

@hsemenenko hsemenenko requested a review from a team as a code owner July 14, 2025 10:53
@hsemenenko hsemenenko requested a review from acl-cqc July 14, 2025 10:53
@hsemenenko
Copy link
Contributor Author

Blocked by CQCL/hugr#2520.

Copy link
Contributor

@acl-cqc acl-cqc left a comment

Choose a reason for hiding this comment

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

Thanks @hsemenenko - sorry for the slow response - looks good and I am 90% of the way to hitting Approve ;). A couple of thoughts....

and the big issue may be, do we know that setting a custom-lowering-to-Hugr actually achieves anything? (That is, we may also need work on the Hugr/TKET2 side, separately from this PR.)

],
)

hugr_ext.add_op_def(op_def)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm a bit nervous here - if we have multiple uses of the same (decorated) function, do we call add_op_def multiple times? Or does this happen only once, when python encounters the @lowered_op? If the latter, what happens if I have two different but same-named @lowered_ops? (We would want that to be an error, methinks)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good observation. In the case where the function was called multiple times the second usage would override the first. I have moved the op definition outside this function so that it is only called a single time.

code. The only information we need to access is that it's a function type and
that there are no unsolved existential vars.
"""
from guppylang_internals.definition.function import parse_py_func
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmmm, ok. So the differences against the RawCustomFunctionDef.parse that we're overriding here are:

  • we suppress the error if there is a body
  • we don't look at self.signature before we call self._get_signature(func_ast, globals)....is that right?
  • we don't assign the local variable docstring that AFAICS in the original/overridden method is defined but not used(?!)

There is definitely an opportunity here for both commoning-up, and possibly cleaning up....do you feel like taking it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, these are the difference between the two methods. As suppressing the error if there is no body is required for the lowering case, it was suggested that these implementations were kept separate. This does come with a small side affect that self.signature is not used.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, agreed that you need suppress the warning, and since you are now using self.signature, looks good to me. You could possibly add a private helper that does the sig = .....; ty = .....; return CustomFunctionDef(self...., self....) etc. (taking just self and func_ast as parameters) but fine as it stands.

@hsemenenko
Copy link
Contributor Author

Thanks @acl-cqc I've addressed your comments. As far as I am aware, lower_funcs are not used at any point in the compilation. This PR creates the functionality from the Guppy side but, as you say, further work at the hugr level is needed for the lower_funcs to actually be used.

@hsemenenko hsemenenko requested a review from acl-cqc August 18, 2025 14:54
Copy link
Contributor

@acl-cqc acl-cqc left a comment

Choose a reason for hiding this comment

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

Thanks @hsemenenko, sorry I missed the request for rereview! Looks good to me....the only thing I can see that might be worth adding would be a test that if you define two functions with the same name, both with @lowerable_op(the_same_ext), that the second either errors, or properly replaces the first (without inheriting the lower-function, say), as appropriate.

code. The only information we need to access is that it's a function type and
that there are no unsolved existential vars.
"""
from guppylang_internals.definition.function import parse_py_func
Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, agreed that you need suppress the warning, and since you are now using self.signature, looks good to me. You could possibly add a private helper that does the sig = .....; ty = .....; return CustomFunctionDef(self...., self....) etc. (taking just self and func_ast as parameters) but fine as it stands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Auto defining extensions with attached lowerings
2 participants