Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,7 @@ const IR::Node *DoConstantFolding::compare(const IR::Operation_Binary *e) {
if (typesKnown) {
auto le = EnumInstance::resolve(eleft, typeMap);
auto re = EnumInstance::resolve(eright, typeMap);
if (le != nullptr && re != nullptr) {
BUG_CHECK(le->type == re->type, "%1%: different enum types in comparison", e);
if (le != nullptr && re != nullptr && le->type == re->type) {
Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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.)

Copy link
Copy Markdown
Member

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 type pointer 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 different IR::Type_Bits instances with the same "value" that are nevertheless different pointers.

Copy link
Copy Markdown
Contributor Author

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.

bool bresult = (le->name == re->name) == eqTest;
return new IR::BoolLiteral(e->srcInfo, IR::Type_Boolean::get(), bresult);
}
Expand Down
33 changes: 33 additions & 0 deletions testdata/p4_16_samples/enumCmp.p4
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;
56 changes: 56 additions & 0 deletions testdata/p4_16_samples_outputs/enumCmp-first.p4
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;
56 changes: 56 additions & 0 deletions testdata/p4_16_samples_outputs/enumCmp-frontend.p4
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;
102 changes: 102 additions & 0 deletions testdata/p4_16_samples_outputs/enumCmp-midend.p4
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;
56 changes: 56 additions & 0 deletions testdata/p4_16_samples_outputs/enumCmp.p4
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.
Loading