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: Make True and False branches unconditional #740

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mark-koch
Copy link
Collaborator

@mark-koch mark-koch commented Dec 20, 2024

For example, when building a CFG for if True: ..., build an unconditional branch. This improves linearity checking for functions involving while True loops, since dataflow analysis now understands that such loops can only exit on an explicit break or return.

It also means that Guppy is now better at detecting unreachable code. For now, we error out in this case, but in the future it would be nice to only emit a warning (see #739).

One complication introduced by this is that Guppy can now generate CFGs where the exit block is unreachable. This lead to some problems with borrowed variables. This edge case now needs to be specially handled.

I recommend reviewing each of the three commits separately

@mark-koch mark-koch requested a review from a team as a code owner December 20, 2024 10:57
@mark-koch mark-koch requested review from qartik and aborgna-q and removed request for qartik December 20, 2024 10:57
@codecov-commenter
Copy link

Codecov Report

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

Project coverage is 92.84%. Comparing base (647ad3d) to head (8a30da1).

Files with missing lines Patch % Lines
guppylang/cfg/builder.py 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #740      +/-   ##
==========================================
+ Coverage   92.81%   92.84%   +0.02%     
==========================================
  Files          71       71              
  Lines        8159     8200      +41     
==========================================
+ Hits         7573     7613      +40     
- Misses        586      587       +1     

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

Comment on lines +1 to +6
Error: Linearity violation (at $FILE:13:8)
|
11 |
12 | @guppy(module)
13 | def foo(q: qubit @owned) -> None:
| ^^^^^^^^^^^^^^^ Variable `q` with linear type `qubit` is leaked
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Arguably, this shouldn't be an error since non-terminating functions technically cannot leak. However, it would be difficult to generate valid Hugr for cases like this, e.g.

while True:
    qubit()

Therefore, I think it's ok to still complain about leaks, even if the function provably doesn't terminate

Copy link
Collaborator

@aborgna-q aborgna-q left a comment

Choose a reason for hiding this comment

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

Uhm, I don't think erroring out on unreachable code is a good option.
Is there no alternative asides from implementing warnings? Even completely ignoring the unreachable code may be better.

Adding if False and ..., mid-point returns, and unused branches is something to be expected while coding and testing things. These errors would make development a lot more annoying.

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