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

Introduce local compile-time known values #1213

Merged
merged 23 commits into from
Jun 3, 2024

Conversation

jnfoster
Copy link
Collaborator

@jnfoster jnfoster commented Jan 9, 2023

Fixes #1001 and #932.

@mihaibudiu
Copy link
Contributor

will add this to the agenda for today

p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
Copy link
Contributor

@apinski-cavium apinski-cavium left a comment

Choose a reason for hiding this comment

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

This is just to simplify the definition such that it becomes obvious that all "compile-time known values" are also "local compile-time known values".
The definition should be not changed from the original definition.

p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
@jnfoster
Copy link
Collaborator Author

jnfoster commented Jan 9, 2023

@apinski-cavium, actually, I had inverted the definitions -- local compile-time known is more restrictive. See my latest commit which fixes this and adopts your streamlining suggestions.

@apinski-cavium
Copy link
Contributor

@apinski-cavium, actually, I had inverted the definitions -- local compile-time known is more restrictive. See my latest commit which fixes this and adopts your streamlining suggestions.

Yes I didn't even noticed that until now and yes this is now +1 from me.

@mihaibudiu
Copy link
Contributor

Would be nice for Petra to validate this.

@jnfoster
Copy link
Collaborator Author

jnfoster commented Mar 3, 2023

Anyone else have comments or suggestions on this one?

@jnfoster
Copy link
Collaborator Author

Any further comments on this or should we merge it?

@mihaibudiu
Copy link
Contributor

I do have some open questions about generics and literals.

@jnfoster
Copy link
Collaborator Author

Sure. Can you provide a pointer to those questions? I might be missing something but I don't see them in the conversation on this issue.

p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
@mihaibudiu
Copy link
Contributor

Looks like I forgot to submit the comments. Somehow github has preserved them for a long time...

@jonathan-dilorenzo
Copy link
Collaborator

@jnfoster, let me know when this is ready for me to do a review.

jnfoster and others added 5 commits May 13, 2024 15:18
Co-authored-by: Vladimír Štill <git@vstill.eu>
Co-authored-by: Vladimír Štill <git@vstill.eu>
Co-authored-by: Vladimír Štill <git@vstill.eu>
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
p4-16/spec/P4-16-spec.mdk Outdated Show resolved Hide resolved
@vlstill
Copy link
Contributor

vlstill commented May 14, 2024

By a very quick test, it seems that the compiler does reject constructor parameters as well as generic-parameter-derived values in type declarations:

int<4> fn<T>(inout bit<4> data) {
    int<(T.minSizeInBits())> v = 42;
    v = v + data;
    return v;
}

control C(inout bit<4> data)(int cnt) {
    int<(cnt)> var;

    apply {
    }
}
# ./p4test test.p4
test.p4(2): [--Werror=expected] error: T.minSizeInBits(): expected a constant
    int<(T.minSizeInBits())> v = 42;
         ^^^^^^^^^^^^^^^^^
test.p4(8): [--Werror=expected] error: cnt: expected a constant
    int<(cnt)> var;
         ^^^

So at least this one case should be safe and we would not be restricting what the compiler already compiles.

@vlstill
Copy link
Contributor

vlstill commented May 14, 2024

However, minSizeInBits is currently not considered a constant also where it should be according to this spec:

typedef int<4> T;

int<4> fn(inout bit<4> data) {
    int<(T.minSizeInBits())> v = 42;
    v = v + data;
    return v;
}

const int cnt = 4;

control C(inout bit<4> data) {
    int<(cnt)> var;

    apply {
    }
}
$ ./p4test test.p4
test.p4(4): [--Werror=expected] error: T.minSizeInBits(): expected a constant
    int<(T.minSizeInBits())> v = 42;
         ^^^^^^^^^^^^^^^^^

@jnfoster
Copy link
Collaborator Author

jnfoster commented Jun 3, 2024

Here is a small example showing what p4c does.

control C(inout bit<8> x);
package P(C c);

const int f = 42;

int g(int x) { return x; }

control MyB(inout bit<8> x, inout bit<8> y)(int n) {
  bit<(1 + 1)> a = 0;
  bit<(f)> b = 1;
  bit<(f / 2)> c = 2;
  // These are all currently errors, and they should be
  // bit<(g(3))> d = 3;
  // bit<(n)> e = 3;
  // bit<(x)> f = 4;
  apply {
    x = y;
  }
}

control MyC(inout bit<8> x)(int o) {
  MyB(o) b;
  bit<8> y = 0;
  apply {
    b.apply(x, y);
  }
}

P(MyC(9)) main;

@jnfoster
Copy link
Collaborator Author

jnfoster commented Jun 3, 2024

@jonathan-dilorenzo this is ready for you to take a look.

Copy link
Collaborator

@jonathan-dilorenzo jonathan-dilorenzo left a comment

Choose a reason for hiding this comment

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

Looks great! Merging!

@jonathan-dilorenzo
Copy link
Collaborator

Actually @jnfoster, can you say why you also wanted to remove the experimenting with bison file? Doesn't look obviously related to this...

For greater clarity, this commit moves the comment about the calling
convention for directionless parameters of extern object type out of
an itemized list. Instead, it is a standalone sentence below the list.
@jnfoster jnfoster force-pushed the local-compile-time-known branch 2 times, most recently from 55cd6ea to 630a54c Compare June 3, 2024 22:01
@jnfoster
Copy link
Collaborator Author

jnfoster commented Jun 3, 2024

Good catch. I've fixed that.

(Sorry for the force-pushes. I was trying to rebase and squash these to tidy it up, which is perhaps ill-advised on a public PR.)


- A simple integer constant has type `int`.
- An integer has type `int`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is specifically referring to integer literals with no width prefix -- those with width prefixes are covered by the subsequent bullet points. Maybe this point should be last and should say "an integer with no width prefix has type int"

- an unsigned integer, i.e. `bit<W>` for some compile-time known `W`.
- a signed integer, i.e. `int<W>` for some compile-time known `W`.
- an unsigned integer, i.e. `bit<W>` for some compile-time known value `W`.
- a signed integer, i.e. `int<W>` for some compile-time known value `W`.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should these be "some locally compile-time known value"?

@jonathan-dilorenzo
Copy link
Collaborator

Np! Looks good to me. Merging.

@jonathan-dilorenzo jonathan-dilorenzo merged commit 5c58113 into p4lang:main Jun 3, 2024
1 check passed
@jafingerhut
Copy link
Collaborator

@jnfoster @ChrisDodd If Nate agrees that some of Chris Dodd's latest comments/questions justify changes, it would be good to create a separate issue to track those.

@jnfoster
Copy link
Collaborator Author

jnfoster commented Jun 5, 2024

@jnfoster @ChrisDodd If Nate agrees that some of Chris Dodd's latest comments/questions justify changes, it would be good to create a separate issue to track those.

The suggestions are good, but creating an issue for something that can be implemented in a few minutes seems like overkill. I created #1286. If people agree, I think it would be safe without discussing this at the LDWG -- all of the changes only increase clarity, but don't change meaning.

Of course, we can also discuss it, so we feel good about the process and about PRs being applied :-)

@jonathan-dilorenzo
Copy link
Collaborator

Made an executive decision to merge. Obvious and simple improvements aren't worth the discussion imo, but feel free to push back if you feel otherwise.

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.

Constructor parameters are not explicitly listed as compile-time-known values
7 participants