-
Notifications
You must be signed in to change notification settings - Fork 510
Allow constant folding of serializable enums of different types #5246
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
Open
ChrisDodd
wants to merge
1
commit into
p4lang:main
Choose a base branch
from
ChrisDodd:cdodd-enumcmp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| control proto<P>(inout P pkt); | ||
| package top<P>(proto<P> p); | ||
|
|
||
| enum bit<8> E1 { A = 1, B = 2, C = 10 } | ||
| enum bit<8> E2 { X = 2, Y = 1, Z = 3 } | ||
|
|
||
| header data_t { | ||
| E1 e1; | ||
| E2 e2; | ||
| bit<8> a; | ||
| bit<8> b; | ||
| bit<8> c; | ||
| } | ||
|
|
||
| struct headers { | ||
| data_t data; | ||
| } | ||
|
|
||
|
|
||
| control c(inout headers hdr) { | ||
| apply { | ||
| if (hdr.data.e1 == hdr.data.e2) { hdr.data.a[0+:1] = 1; } | ||
| if (hdr.data.e1 == E1.A) { hdr.data.a[1+:1] = 1; } | ||
| if (hdr.data.e1 == E2.X) { hdr.data.a[2+:1] = 1; } | ||
| if (hdr.data.e1 == hdr.data.c) { hdr.data.a[3+:1] = 1; } | ||
| if (hdr.data.c == E1.B) { hdr.data.a[4+:1] = 1; } | ||
| if (hdr.data.c == E2.Y) { hdr.data.a[5+:1] = 1; } | ||
| if (E1.A == E2.X) { hdr.data.b[0+:1] = 1; } | ||
| if (E1.A == E2.Y) { hdr.data.b[1+:1] = 1; } | ||
| } | ||
| } | ||
|
|
||
| top(c()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| control proto<P>(inout P pkt); | ||
| package top<P>(proto<P> p); | ||
| enum bit<8> E1 { | ||
| A = 8w1, | ||
| B = 8w2, | ||
| C = 8w10 | ||
| } | ||
|
|
||
| enum bit<8> E2 { | ||
| X = 8w2, | ||
| Y = 8w1, | ||
| Z = 8w3 | ||
| } | ||
|
|
||
| header data_t { | ||
| E1 e1; | ||
| E2 e2; | ||
| bit<8> a; | ||
| bit<8> b; | ||
| bit<8> c; | ||
| } | ||
|
|
||
| struct headers { | ||
| data_t data; | ||
| } | ||
|
|
||
| control c(inout headers hdr) { | ||
| apply { | ||
| if (hdr.data.e1 == hdr.data.e2) { | ||
| hdr.data.a[0:0] = 1w1; | ||
| } | ||
| if (hdr.data.e1 == E1.A) { | ||
| hdr.data.a[1:1] = 1w1; | ||
| } | ||
| if (hdr.data.e1 == E2.X) { | ||
| hdr.data.a[2:2] = 1w1; | ||
| } | ||
| if (hdr.data.e1 == hdr.data.c) { | ||
| hdr.data.a[3:3] = 1w1; | ||
| } | ||
| if (hdr.data.c == E1.B) { | ||
| hdr.data.a[4:4] = 1w1; | ||
| } | ||
| if (hdr.data.c == E2.Y) { | ||
| hdr.data.a[5:5] = 1w1; | ||
| } | ||
| if (E1.A == E2.X) { | ||
| hdr.data.b[0:0] = 1w1; | ||
| } | ||
| if (E1.A == E2.Y) { | ||
| hdr.data.b[1:1] = 1w1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| top<headers>(c()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| control proto<P>(inout P pkt); | ||
| package top<P>(proto<P> p); | ||
| enum bit<8> E1 { | ||
| A = 8w1, | ||
| B = 8w2, | ||
| C = 8w10 | ||
| } | ||
|
|
||
| enum bit<8> E2 { | ||
| X = 8w2, | ||
| Y = 8w1, | ||
| Z = 8w3 | ||
| } | ||
|
|
||
| header data_t { | ||
| E1 e1; | ||
| E2 e2; | ||
| bit<8> a; | ||
| bit<8> b; | ||
| bit<8> c; | ||
| } | ||
|
|
||
| struct headers { | ||
| data_t data; | ||
| } | ||
|
|
||
| control c(inout headers hdr) { | ||
| apply { | ||
| if (hdr.data.e1 == hdr.data.e2) { | ||
| hdr.data.a[0:0] = 1w1; | ||
| } | ||
| if (hdr.data.e1 == E1.A) { | ||
| hdr.data.a[1:1] = 1w1; | ||
| } | ||
| if (hdr.data.e1 == E2.X) { | ||
| hdr.data.a[2:2] = 1w1; | ||
| } | ||
| if (hdr.data.e1 == hdr.data.c) { | ||
| hdr.data.a[3:3] = 1w1; | ||
| } | ||
| if (hdr.data.c == E1.B) { | ||
| hdr.data.a[4:4] = 1w1; | ||
| } | ||
| if (hdr.data.c == E2.Y) { | ||
| hdr.data.a[5:5] = 1w1; | ||
| } | ||
| if (E1.A == E2.X) { | ||
| hdr.data.b[0:0] = 1w1; | ||
| } | ||
| if (E1.A == E2.Y) { | ||
| hdr.data.b[1:1] = 1w1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| top<headers>(c()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| control proto<P>(inout P pkt); | ||
| package top<P>(proto<P> p); | ||
| header data_t { | ||
| bit<8> e1; | ||
| bit<8> e2; | ||
| bit<8> a; | ||
| bit<8> b; | ||
| bit<8> c; | ||
| } | ||
|
|
||
| struct headers { | ||
| data_t data; | ||
| } | ||
|
|
||
| control c(inout headers hdr) { | ||
| @hidden action enumCmp22() { | ||
| hdr.data.a[0:0] = 1w1; | ||
| } | ||
| @hidden action enumCmp23() { | ||
| hdr.data.a[1:1] = 1w1; | ||
| } | ||
| @hidden action enumCmp24() { | ||
| hdr.data.a[2:2] = 1w1; | ||
| } | ||
| @hidden action enumCmp25() { | ||
| hdr.data.a[3:3] = 1w1; | ||
| } | ||
| @hidden action enumCmp26() { | ||
| hdr.data.a[4:4] = 1w1; | ||
| } | ||
| @hidden action enumCmp27() { | ||
| hdr.data.a[5:5] = 1w1; | ||
| } | ||
| @hidden action enumCmp29() { | ||
| hdr.data.b[1:1] = 1w1; | ||
| } | ||
| @hidden table tbl_enumCmp22 { | ||
| actions = { | ||
| enumCmp22(); | ||
| } | ||
| const default_action = enumCmp22(); | ||
| } | ||
| @hidden table tbl_enumCmp23 { | ||
| actions = { | ||
| enumCmp23(); | ||
| } | ||
| const default_action = enumCmp23(); | ||
| } | ||
| @hidden table tbl_enumCmp24 { | ||
| actions = { | ||
| enumCmp24(); | ||
| } | ||
| const default_action = enumCmp24(); | ||
| } | ||
| @hidden table tbl_enumCmp25 { | ||
| actions = { | ||
| enumCmp25(); | ||
| } | ||
| const default_action = enumCmp25(); | ||
| } | ||
| @hidden table tbl_enumCmp26 { | ||
| actions = { | ||
| enumCmp26(); | ||
| } | ||
| const default_action = enumCmp26(); | ||
| } | ||
| @hidden table tbl_enumCmp27 { | ||
| actions = { | ||
| enumCmp27(); | ||
| } | ||
| const default_action = enumCmp27(); | ||
| } | ||
| @hidden table tbl_enumCmp29 { | ||
| actions = { | ||
| enumCmp29(); | ||
| } | ||
| const default_action = enumCmp29(); | ||
| } | ||
| apply { | ||
| if (hdr.data.e1 == hdr.data.e2) { | ||
| tbl_enumCmp22.apply(); | ||
| } | ||
| if (hdr.data.e1 == 8w1) { | ||
| tbl_enumCmp23.apply(); | ||
| } | ||
| if (hdr.data.e1 == 8w2) { | ||
| tbl_enumCmp24.apply(); | ||
| } | ||
| if (hdr.data.e1 == hdr.data.c) { | ||
| tbl_enumCmp25.apply(); | ||
| } | ||
| if (hdr.data.c == 8w2) { | ||
| tbl_enumCmp26.apply(); | ||
| } | ||
| if (hdr.data.c == 8w1) { | ||
| tbl_enumCmp27.apply(); | ||
| } | ||
| tbl_enumCmp29.apply(); | ||
| } | ||
| } | ||
|
|
||
| top<headers>(c()) main; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| control proto<P>(inout P pkt); | ||
| package top<P>(proto<P> p); | ||
| enum bit<8> E1 { | ||
| A = 1, | ||
| B = 2, | ||
| C = 10 | ||
| } | ||
|
|
||
| enum bit<8> E2 { | ||
| X = 2, | ||
| Y = 1, | ||
| Z = 3 | ||
| } | ||
|
|
||
| header data_t { | ||
| E1 e1; | ||
| E2 e2; | ||
| bit<8> a; | ||
| bit<8> b; | ||
| bit<8> c; | ||
| } | ||
|
|
||
| struct headers { | ||
| data_t data; | ||
| } | ||
|
|
||
| control c(inout headers hdr) { | ||
| apply { | ||
| if (hdr.data.e1 == hdr.data.e2) { | ||
| hdr.data.a[0+:1] = 1; | ||
| } | ||
| if (hdr.data.e1 == E1.A) { | ||
| hdr.data.a[1+:1] = 1; | ||
| } | ||
| if (hdr.data.e1 == E2.X) { | ||
| hdr.data.a[2+:1] = 1; | ||
| } | ||
| if (hdr.data.e1 == hdr.data.c) { | ||
| hdr.data.a[3+:1] = 1; | ||
| } | ||
| if (hdr.data.c == E1.B) { | ||
| hdr.data.a[4+:1] = 1; | ||
| } | ||
| if (hdr.data.c == E2.Y) { | ||
| hdr.data.a[5+:1] = 1; | ||
| } | ||
| if (E1.A == E2.X) { | ||
| hdr.data.b[0+:1] = 1; | ||
| } | ||
| if (E1.A == E2.Y) { | ||
| hdr.data.b[1+:1] = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| top(c()) main; |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the type guaranteed to have the same identity (pointer value) here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not -- that's what the check is for. If it is NOT the same type, we need to compare the values of the enum instances (which is what will happen if we fall through and don't do this special-case handling of enums of the same type.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, my question was a bit too abridged. I mean to say: is the
typepointer guaranteed to have the same identity if it represents same type (i.e.bit<8>on both side)? I think it is possible to create two differentIR::Type_Bitsinstances with the same "value" that are nevertheless different pointers.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not be possible to to that -- all creation of Type_Bits instances should happen though IR::Type_Bits::get which ensures that only a single instance of each type gets created.