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

fix: Use places in BB signatures for Hugr generation #342

Merged
merged 2 commits into from
Jul 29, 2024

Conversation

mark-koch
Copy link
Collaborator

Fixes #337.

The fix for the linearity checker is easy: When checking assignments, only complain if a local unused linear variable is shadowed.

However, correctly lowering the program in #337 to Hugr requires some additional changes to Hugr codegen. Namely, we need to consider if a BB uses a whole struct s or only some of its fields. In the latter case, we should only feed those used values into the BB. This requires specifying the signature of BBs in the form of Places rather than Variables.

Concretely, this PR does the following:

  • Make Signature generic over the representation of program variables so it can capture both Variable and Place
  • Similarly, make CheckedBB and CheckedCFG generic
  • During checking, we first construct a CheckedCFG[Variable] which then gets turned into a CheckedCFG[Place] during linearity checking
  • Add a test case

@mark-koch mark-koch requested a review from aborgna-q July 29, 2024 14:28
@mark-koch mark-koch requested a review from a team as a code owner July 29, 2024 14:28
@codecov-commenter
Copy link

codecov-commenter commented Jul 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.60%. Comparing base (6698b75) to head (fed423b).
Report is 7 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #342      +/-   ##
==========================================
- Coverage   92.67%   92.60%   -0.07%     
==========================================
  Files          45       45              
  Lines        5105     5153      +48     
==========================================
+ Hits         4731     4772      +41     
- Misses        374      381       +7     

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

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.

Nice solution!

Comment on lines +164 to +167
x, y = str(p1), str(p2)
if is_return_var(x) and is_return_var(y):
return -1 if x < y else 1
return -1 if (p1.ty.linear, x) < (p2.ty.linear, y) else 1
Copy link
Collaborator

Choose a reason for hiding this comment

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

The order doesn't seem consistent?
Consider some vars named

x = "%ret_0_linear"
y = "%ret_1_nonlinear"
z = "%zzz_nonlinear"

then x < y, x > z, y < z is a cycle

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Indeed, this %ret business is quite hacky. This currently works since return vars are only compared with each other, hower this changes with @inout.

This special handling of return vars is removed in #340, so I think it's not really worth putting effort into a nice solution here though

@@ -278,7 +277,7 @@ def leaf_places(place: Place) -> Iterator[Place]:
yield place


def check_cfg_linearity(cfg: "CheckedCFG") -> None:
def check_cfg_linearity(cfg: "CheckedCFG[Variable]") -> "CheckedCFG[Place]":
"""Checks whether a CFG satisfies the linearity requirements.

Raises a user-error if linearity violations are found.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Docstring for the new return value?

Comment on lines 60 to 65
def check_cfg(
cfg: CFG, inputs: VarRow, return_ty: Type, globals: Globals
) -> CheckedCFG:
cfg: CFG, inputs: Row[Variable], return_ty: Type, globals: Globals
) -> CheckedCFG[Place]:
"""Type checks a control-flow graph.

Annotates the basic blocks with input and output type signatures and removes
Copy link
Collaborator

Choose a reason for hiding this comment

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

And also mention the places instead of variables in the return type here.

@@ -117,18 +120,20 @@ def check_cfg(
}

# Finally, run the linearity check
check_cfg_linearity(checked_cfg)
from guppylang.checker.linearity_checker import check_cfg_linearity
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why the local import here, instead of having it at the top?

Copy link
Collaborator Author

@mark-koch mark-koch Jul 29, 2024

Choose a reason for hiding this comment

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

Circularity, since linearity_checker.py already depends on cfg_checker.py.

I could move Signature, CheckedBB, and CheckedCFG to checker/core.py to break the cycle if you prefer?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh. I guess it's ok.

@mark-koch mark-koch requested a review from aborgna-q July 29, 2024 15:05
@mark-koch mark-koch added this pull request to the merge queue Jul 29, 2024
Merged via the queue into main with commit 48b0e35 Jul 29, 2024
3 checks passed
@mark-koch mark-koch deleted the fix/place-reassign branch July 29, 2024 15:21
github-merge-queue bot pushed a commit that referenced this pull request Jul 31, 2024
🤖 I have created a release *beep* *boop*
---


## [0.8.0](v0.7.0...v0.8.0)
(2024-07-30)


### Features

* Parse docstrings for functions
([#334](#334))
([a7cc97a](a7cc97a))


### Bug Fixes

* Use places in BB signatures for Hugr generation
([#342](#342))
([48b0e35](48b0e35))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
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.

Struct linearity checking bug
3 participants