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

Implict cast fix #4399

Merged
merged 1 commit into from
Mar 25, 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
11 changes: 10 additions & 1 deletion frontends/p4/typeChecking/typeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,7 @@ const IR::Expression *TypeInference::actionCall(bool inActionList,
if (paramType == nullptr || argType == nullptr)
// type checking failed before
return actionCall;
constraints.addEqualityConstraint(actionCall, paramType, argType);
constraints.addImplicitCastConstraint(actionCall, paramType, argType);
if (param->direction == IR::Direction::None) {
if (inActionList) {
typeError("%1%: parameter %2% cannot be bound: it is set by the control plane", arg,
Expand All @@ -3509,6 +3509,15 @@ const IR::Expression *TypeInference::actionCall(bool inActionList,
typeError("%1%: action argument must be a compile-time constant",
arg->expression);
}
// This is like an assignment; may make additional conversions.
newExpr = assignment(arg, param->type, arg->expression);
if (readOnly) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you elaborate on that bug? Maybe we can fix the DPDK back end.

Copy link
Contributor Author

@ChrisDodd ChrisDodd Feb 13, 2024

Choose a reason for hiding this comment

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

Basically, the DPDK backend creates new code that is not type-correct, so a following TypeChecking pass tries to introduce new implicit casts, but fails as the readOnly flag is set. I'm not sure why it only fails after this change, as it seems like it should fail regardless. Specifically the incorrect added code is an action call where the actual argument type does not exactly match the formal parameter type (but can be converted implicitly). It seems that typeChecking is not correctly checking this case, and this change fixes that (as part of getting the serializable enum casts working)

I tried adding a additional TypeInferencing pass in the DPDK backend to introduce the implicit casts correctly, but that caused some other problems. This feels like a bit of a hack, leaving the code as semi-type-incorrect (not making the implicit cast explicit) when the readOnly flag is set.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Which pass is this? Maybe we can fix the generation of this action call in the first place?

I think this is something the DPDK back end needs to fix before we add more workarounds to the type checker. The class is already complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is in DPDK::PrependPDotToActionArgs (line ~1318) where it is creating a ListExpression rather than a StructExpression to initialize a struct. The typechecker wants this to be a StructExpression, so will introduce a "cast" (actually rewrite the ListExpression as a StructExpression), which then causes a problem as it is running with readOnly = true (backend.cpp line 107). If I change that call to have readOnly = false, then it doesn't assert and just makes the change. However, it then has problems later in the backend with code that doesn't know what to do with a StructExpression.

Without my change, the typeChecker does not correctly type check action args, so it doesn't try to insert the conversion.

The easiest fix is probably just to add a call to P4::TypeInference with readOnly = false somewhere, and fix the backend code to understand a StructExpression.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@usha1830 Could you take a look at this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure. I will check this out.

// FIXME -- if we're in readonly mode, we should not have introduced any mods
// here, but there's a bug in the DPDK backend where it generates a ListExpression
// that would be converted to a StructExpression, and other problems where it
// can't deal with that StructExpressions, so we hack to avoid breaking those tests
newExpr = arg->expression;
}
} else if (param->direction == IR::Direction::Out ||
param->direction == IR::Direction::InOut) {
if (!isLeftValue(arg->expression))
Expand Down
6 changes: 6 additions & 0 deletions frontends/p4/typeChecking/typeConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ void TypeConstraints::addEqualityConstraint(const IR::Node *source, const IR::Ty
add(c);
}

void TypeConstraints::addImplicitCastConstraint(const IR::Node *source, const IR::Type *left,
const IR::Type *right) {
auto c = new CanBeImplicitlyCastConstraint(left, right, source);
add(c);
}

TypeVariableSubstitution *TypeConstraints::solve() {
LOG3("Solving constraints:\n" << *this);
currentSubstitution = new TypeVariableSubstitution();
Expand Down
2 changes: 2 additions & 0 deletions frontends/p4/typeChecking/typeConstraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ class TypeConstraints final : public IHasDbPrint {
constraints.push_back(constraint);
}
void addEqualityConstraint(const IR::Node *source, const IR::Type *left, const IR::Type *right);
void addImplicitCastConstraint(const IR::Node *source, const IR::Type *left,
const IR::Type *right);
/*
* Solve the specified constraint.
* @param subst Variable substitution which is updated with new constraints.
Expand Down
22 changes: 22 additions & 0 deletions testdata/p4_16_errors/serEnumImplCast.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <core.p4>

enum bit<2> foo_t { A = 0, B = 1, C = 2, D = 3 }

struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
action set_x(foo_t v) { m.x = v; }

table t {
key = { m.y : exact; }
actions = { set_x; }
default_action = set_x(2w0); // not allowed to implicitly cast to serenum
}

apply {
t.apply();
}
}
32 changes: 32 additions & 0 deletions testdata/p4_16_errors_outputs/serEnumImplCast.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <core.p4>

enum bit<2> foo_t {
A = 0,
B = 1,
C = 2,
D = 3
}

struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
action set_x(foo_t v) {
m.x = v;
}
table t {
key = {
m.y: exact;
}
actions = {
set_x;
}
default_action = set_x(2w0);
}
apply {
t.apply();
}
}

6 changes: 6 additions & 0 deletions testdata/p4_16_errors_outputs/serEnumImplCast.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
serEnumImplCast.p4(16): [--Werror=type-error] error: '2w0': values of type 'bit<2>' cannot be implicitly cast to type 'foo_t'
default_action = set_x(2w0); // not allowed to implicitly cast to serenum
^^^
serEnumImplCast.p4(3)
enum bit<2> foo_t { A = 0, B = 1, C = 2, D = 3 }
^^^^^
26 changes: 26 additions & 0 deletions testdata/p4_16_samples/serEnumImplCast.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <core.p4>
control generic<M>(inout M m);
package top<M>(generic<M> c);

enum bit<2> foo_t { A = 0, B = 1, C = 2, D = 3 }

struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
action set_x(bit<2> v) { m.x = v; }

table t {
key = { m.y : exact; }
actions = { set_x; }
default_action = set_x(foo_t.A);
}

apply {
t.apply();
}
}

top(c()) main;
35 changes: 35 additions & 0 deletions testdata/p4_16_samples_outputs/serEnumImplCast-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <core.p4>

control generic<M>(inout M m);
package top<M>(generic<M> c);
enum bit<2> foo_t {
A = 2w0,
B = 2w1,
C = 2w2,
D = 2w3
}

struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
action set_x(bit<2> v) {
m.x = v;
}
table t {
key = {
m.y: exact @name("m.y");
}
actions = {
set_x();
}
default_action = set_x(foo_t.A);
}
apply {
t.apply();
}
}

top<meta_t>(c()) main;
35 changes: 35 additions & 0 deletions testdata/p4_16_samples_outputs/serEnumImplCast-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <core.p4>

control generic<M>(inout M m);
package top<M>(generic<M> c);
enum bit<2> foo_t {
A = 2w0,
B = 2w1,
C = 2w2,
D = 2w3
}

struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
@name("c.set_x") action set_x(@name("v") bit<2> v) {
m.x = v;
}
@name("c.t") table t_0 {
key = {
m.y: exact @name("m.y");
}
actions = {
set_x();
}
default_action = set_x(foo_t.A);
}
apply {
t_0.apply();
}
}

top<meta_t>(c()) main;
28 changes: 28 additions & 0 deletions testdata/p4_16_samples_outputs/serEnumImplCast-midend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <core.p4>

control generic<M>(inout M m);
package top<M>(generic<M> c);
struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
@name("c.set_x") action set_x(@name("v") bit<2> v) {
m.x = v;
}
@name("c.t") table t_0 {
key = {
m.y: exact @name("m.y");
}
actions = {
set_x();
}
default_action = set_x(2w0);
}
apply {
t_0.apply();
}
}

top<meta_t>(c()) main;
35 changes: 35 additions & 0 deletions testdata/p4_16_samples_outputs/serEnumImplCast.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <core.p4>

control generic<M>(inout M m);
package top<M>(generic<M> c);
enum bit<2> foo_t {
A = 0,
B = 1,
C = 2,
D = 3
}

struct meta_t {
bit<2> x;
bit<6> y;
}

control c(inout meta_t m) {
action set_x(bit<2> v) {
m.x = v;
}
table t {
key = {
m.y: exact;
}
actions = {
set_x;
}
default_action = set_x(foo_t.A);
}
apply {
t.apply();
}
}

top(c()) main;
Empty file.
Loading