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

Unstable if/whiles are errored #1467

Merged
merged 8 commits into from
May 12, 2023
Merged

Unstable if/whiles are errored #1467

merged 8 commits into from
May 12, 2023

Conversation

calebmkim
Copy link
Contributor

@calebmkim calebmkim commented May 11, 2023

We decided on the following requirement for while loops/ if statements:

Either: a) the condition port for if/while loops is stable or b) they have a comb group (in which case the Calyx compiler will compile it such that the loop will be stable).

One test case that I wasn't quite sure how to change: tests/backends/mlir/with-guards.futil, found here. I see why there's an error, but wasn't quite sure how to change it, since I'm not completely sure what the test is doing ?

Some of the test cases were relying on ThisComponent's input ports. I changed these by making a 1 bit register that holds the value of ThisComponent's input port and writing to that register at the very beginning. It adds an extra cycle.
However, for our method of compiling static while loops with static bodies in #1431, I think it's important that the condition is not ThisComponent's port.

@rachitnigam
Copy link
Contributor

The backend tests aren’t great but they just check to see if we can generate backend code

Copy link
Contributor

@rachitnigam rachitnigam left a comment

Choose a reason for hiding this comment

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

LGTM!

@calebmkim
Copy link
Contributor Author

Ok, merging! (@paili0628 I think this can help unblock #1431).

@calebmkim calebmkim merged commit 41df59a into master May 12, 2023
@calebmkim calebmkim deleted the well-formed-while branch May 12, 2023 14:02
@sampsyo
Copy link
Contributor

sampsyo commented May 13, 2023

Looks good overall! I do also want to point out that this doesn't entirely address the semantics question from #1438 and there is still room to hammer out a rigorous definition there. Namely, as @calebmkim notes, the policy is:

Either: a) the condition port for if/while loops is stable or b) they have a comb group (in which case the Calyx compiler will compile it such that the loop will be stable).

This leads to two weird cases:

  • while cond is illegal, but while cond with cg is legal, even though cond doesn't come from anything defined in cg and in fact has nothing to do at all with cg. This is a little too tied to the current way remove-comb-groups works. It seems like it should be legal, for example, for an optimization pass to prune dead comb groups, which would transform a legal loop into an illegal one under this policy.
  • while cond with cg is classified as legal but cond is actually affected by other assignments, outside of cg, that render it unreliable as a loop condition.

So the "unfinished business" from #1438 remains: clearly defining what conds are legal, in English. This well-formedness check is necessary but insufficient; the rule we are gravitating toward requires dynamic reasoning.

@calebmkim
Copy link
Contributor Author

calebmkim commented May 13, 2023

Yeah, I see what your'e saying.

As for the two cases:

while cond is illegal, but while cond with cg is legal, even though cond doesn't come from anything defined in cg and in fact has nothing to do at all with cg. This is a little too tied to the current way remove-comb-groups works. It seems like it should be legal, for example, for an optimization pass to prune dead comb groups, which would transform a legal loop into an illegal one under this policy.

Interesting point. Maybe we should make the condition for a valid while loop even more restrictive. We could change criterion b) to be something like "has a comb group, and that comb group writes to the component that cond reads from"? I don't think this would be too difficult to check, and it (at least to me) sort of makes sense why we would want to enforce this.

while cond with cg is classified as legal but cond is actually affected by other assignments, outside of cg, that render it unreliable as a loop condition.

I think I'd have to think about a specific case of how this would happen. I'll def keep it in mind tho.

Edit: this is also just my quick thoughts. I think it may require careful thinking to lay out exactly what we want to require from while ports.

@sampsyo
Copy link
Contributor

sampsyo commented May 14, 2023

I do think this is on the right track! I honestly don't think a sound static check is the most urgent thing here, but it's something to think about eventually.

It seems like there is room to generalize the concept of @stable to include:

  • combinational logic driven continuously and stably from @stable signals
  • stuff that is locally stable by the above definition when a certain comb group is active

Basically, it seems like the desired policy involves some kind of "propagation" of stability, but exactly the rules for that propagation remain unclear.

@rachitnigam
Copy link
Contributor

Ah, that's a good point @sampsyo @calebmkim! It better be the case that the cond in while cond with cg is defined by the combinational group. It would be definitely weird if we allowed anything else!

@rachitnigam
Copy link
Contributor

@calebmkim can we open an issue about this?

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