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

Allow constant-folding of arbitrary-precision integer casts #4325

Merged
merged 3 commits into from
Jan 11, 2024
Merged
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
10 changes: 10 additions & 0 deletions frontends/common/constantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ const IR::Node *DoConstantFolding::postorder(IR::Cast *e) {
} else {
return e;
}
} else if (etype->is<IR::Type_InfInt>()) {
if (const auto *constant = expr->to<IR::Constant>()) {
const auto *ctype = constant->type;
if (!ctype->is<IR::Type_Bits>() && !ctype->is<IR::Type_InfInt>()) {
::error(ErrorType::ERR_INVALID, "%1%: Cannot cast %1% to arbitrary presion integer",
ctype);
return e;
}
return new IR::Constant(e->srcInfo, etype, constant->value, constant->base);
}
} else if (etype->is<IR::Type_Boolean>()) {
if (expr->is<IR::BoolLiteral>()) return expr;
if (expr->is<IR::Constant>()) {
Expand Down
9 changes: 9 additions & 0 deletions testdata/p4_16_samples/constant-fold-infint.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const bit<4> a = 0b0101;
const int b = (int)a; // 5
const bit<7> c = (bit<7>)b; // 5

const int<4> d = -1;
const int e = (int)d; // -1
const bit<7> f = (bit<7>)e; // 0b1111111 = 127
const int<7> g = (int<7>)e; // 0b1111111 = -1
const int h = (int)g; // -1
8 changes: 8 additions & 0 deletions testdata/p4_16_samples_outputs/constant-fold-infint-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const bit<4> a = 4w0b101;
const int b = 0b101;
const bit<7> c = 7w0b101;
const int<4> d = -4s1;
const int e = -1;
const bit<7> f = 7w127;
const int<7> g = -7s1;
const int h = -1;
Empty file.
8 changes: 8 additions & 0 deletions testdata/p4_16_samples_outputs/constant-fold-infint.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const bit<4> a = 0b101;
const int b = (int)a;
const bit<7> c = (bit<7>)b;
const int<4> d = -1;
const int e = (int)d;
const bit<7> f = (bit<7>)e;
const int<7> g = (int<7>)e;
const int h = (int)g;
4 changes: 4 additions & 0 deletions testdata/p4_16_samples_outputs/constant-fold-infint.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
constant-fold-infint.p4(6): [--Wwarn=mismatch] warning: -7w1: negative value with unsigned type
const int e = (int)d; // -1
^^^^^^
[--Wwarn=missing] warning: Program does not contain a `main' module
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/issue2444-first.p4
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const int a = 1;
const bool b = true;
const int z = (int)2w1;
const bool w = (bool)(int)2w1;
const int z = 1;
const bool w = true;
const int z1 = 1;
const bool w1 = true;
const int z2 = 3;
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/issue3219-first.p4
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
bit<4> func1() {
bit<4> t = (bit<4>)(int)1;
bit<4> t = 4w1;
return t;
}
bit<4> func2() {
bit<4> t = (bit<4>)(int)4w1;
bit<4> t = 4w1;
return t;
}
control c(out bit<4> result) {
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue3219-frontend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ control c(out bit<4> result) {
@name("c.retval") bit<4> retval;
@name("c.t") bit<4> t_0;
apply {
t_0 = (bit<4>)(int)1;
t_0 = 4w1;
retval = t_0;
result = retval;
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/issue3219-midend.p4
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
control c(out bit<4> result) {
@hidden action issue3219l13() {
result = (bit<4>)(int)1;
result = 4w1;
}
@hidden table tbl_issue3219l13 {
actions = {
Expand Down
Loading