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

Enable const identifiers in bit slice expressions, and anywhere else local compile-time known values are allowed #4920

Open
jafingerhut opened this issue Sep 19, 2024 · 3 comments
Labels
p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).

Comments

@jafingerhut
Copy link
Contributor

jafingerhut commented Sep 19, 2024

This change was made to the language spec recently: p4lang/p4-spec#1307

Thus according to the spec, the following code is legal:

    const bit<8> lo = 2;
    const bit<8> hi = lo + 3;
    bit<4> x;
    bit<8> y;
    x = y[hi:lo];

but I believe this is not yet supported by p4c. This issue is to track when/if this is enhanced in p4c.

Note: Technically the language spec enhancement also allows const values to be used in any place where a local compile-time known value is required, but a bit slice hi or lo expression are the only ones I know of off the top of my head.

@fruffy fruffy added the p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). label Sep 19, 2024
@jafingerhut
Copy link
Contributor Author

Hmmm. I may be imagining that I tried something like this in the past month or two and it failed, but it seems to be working for a test P4 program (attached) that has something like the above code snippet at its core, so perhaps I am wrong, and p4c already supports this.
issue4920-bmv2.p4.txt

@ChrisDodd
Copy link
Contributor

It does require the explicit const -- that allows the folding to happen directly without any dataflow analysis to prove that there are no intervening assignments to the variable.

@jafingerhut
Copy link
Contributor Author

AI for Andy: Add a test program, probably the one attached to an issue above, as a new p4c test program, that can be used to verify that future compiler changes do not break this already-working behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

No branches or pull requests

3 participants