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

feat: Lower unpacking assignment of iterables to Hugr #689

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

mark-koch
Copy link
Collaborator

Adds the Hugr lowering for #649. See #688 for the base PR that adds the checking logic

@mark-koch mark-koch requested a review from a team as a code owner December 4, 2024 15:53
@mark-koch mark-koch requested review from doug-q and removed request for a team December 4, 2024 15:53
popped_arr_ty = array_type(elem_ty, ht.BoundedNatArg(length - 1))
op = "pop_left" if from_left else "pop_right"
return _instantiate_array_op(
op, elem_ty, length_arg, [arr_ty], [ht.Option(elem_ty, popped_arr_ty)]
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Side note: Maybe we should add an infallibe array_pop op where we statically check that the length isn't 0?

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. It's not clear to me why we can't do this always, since for pop we always know the length of the array.

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 98.48485% with 1 line in your changes missing coverage. Please review.

Project coverage is 92.93%. Comparing base (31717af) to head (5730408).

Files with missing lines Patch % Lines
guppylang/compiler/stmt_compiler.py 98.21% 1 Missing ⚠️
Additional details and impacted files
@@                Coverage Diff                 @@
##           feat/unpacking     #689      +/-   ##
==================================================
+ Coverage           92.88%   92.93%   +0.04%     
==================================================
  Files                  68       68              
  Lines                7848     7902      +54     
==================================================
+ Hits                 7290     7344      +54     
  Misses                558      558              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@doug-q doug-q left a comment

Choose a reason for hiding this comment

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

Looks good. Unless there is a reason not to, I do think you should change to use _instantiate_array_op for discard_empty.

Is this the first time we are generating pop ops? Can we have a test that trys to pop from an array of generic length? If so we should.

popped_arr_ty = array_type(elem_ty, ht.BoundedNatArg(length - 1))
op = "pop_left" if from_left else "pop_right"
return _instantiate_array_op(
op, elem_ty, length_arg, [arr_ty], [ht.Option(elem_ty, popped_arr_ty)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Agree. It's not clear to me why we can't do this always, since for pop we always know the length of the array.

def array_discard_empty(elem_ty: ht.Type) -> ops.ExtOp:
"""Returns an operation that discards an array of length zero."""
arr_ty = array_type(elem_ty, ht.BoundedNatArg(0))
return hugr.std.PRELUDE.get_op("discard_empty").instantiate(
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use _instantiate_array_op here? the extension has changed since this was written.

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 don't think so since discard_empty doesn't take a length arg

@mark-koch
Copy link
Collaborator Author

Is this the first time we are generating pop ops? Can we have a test that trys to pop from an array of generic length? If so we should.

Yes it's the first time they are emitted in Guppy. But Guppy doesn't allow you to pop if the length is generic

@mark-koch mark-koch merged commit 3262031 into feat/unpacking Dec 17, 2024
2 checks passed
@mark-koch mark-koch deleted the unpacking/compile branch December 17, 2024 17:00
github-merge-queue bot pushed a commit that referenced this pull request Dec 17, 2024
Closes #649.

Sorry for the big PR, but there was no good way to split the checking
logic up. I recommend reviewing commit by commit, most of the actual
logic is in 616c0c5. Hugr lowering follows in a separate PR (#689).
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.

3 participants