Skip to content

Commit

Permalink
[#4883] Remove unused actions whose @name name starts with "__" (#4900
Browse files Browse the repository at this point in the history
)

* Remove unused actions whose external name starts with '__'

Signed-off-by: Kyle Cripps <kyle@pensando.io>

* Add additional test case

Signed-off-by: Kyle Cripps <kyle@pensando.io>

---------

Signed-off-by: Kyle Cripps <kyle@pensando.io>
  • Loading branch information
kfcripps authored Sep 17, 2024
1 parent 64271f0 commit b0890a8
Show file tree
Hide file tree
Showing 14 changed files with 375 additions and 5 deletions.
2 changes: 1 addition & 1 deletion frontends/p4/unusedDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const IR::Node *RemoveUnusedDeclarations::process(const IR::IDeclaration *decl)
LOG3("Visiting " << decl);
if (decl->getName().name == IR::ParserState::verify && getParent<IR::P4Program>())
return decl->getNode();
if (decl->getName().name.startsWith("__"))
if (decl->externalName(decl->getName().name).startsWith("__"))
// Internal identifiers, e.g., __v1model_version
return decl->getNode();
if (refMap->isUsed(getOriginal<IR::IDeclaration>())) return decl->getNode();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
structure-valued-expr-errs-1.p4(100): [--Werror=type-error] error: 'hdr.h1 = { }'
structure-valued-expr-errs-1.p4(100): [--Werror=type-error] error: 'hdr.h1 = {}'
hdr.h1 = {};
^
---- Actual error:
Expand All @@ -9,7 +9,7 @@ structure-valued-expr-errs-1.p4(31)
header h1_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(100): Source expression '{ }' produces a result of type 'tuple<>' which cannot be assigned to a left-value with type 'header h1_t'
structure-valued-expr-errs-1.p4(100): Source expression '{}' produces a result of type 'tuple<>' which cannot be assigned to a left-value with type 'header h1_t'
hdr.h1 = {};
^^
structure-valued-expr-errs-1.p4(100)
Expand All @@ -18,7 +18,7 @@ structure-valued-expr-errs-1.p4(100)
structure-valued-expr-errs-1.p4(31)
header h1_t {
^^^^
structure-valued-expr-errs-1.p4(101): [--Werror=type-error] error: 'hdr.h2 = { }'
structure-valued-expr-errs-1.p4(101): [--Werror=type-error] error: 'hdr.h2 = {}'
hdr.h2 = {};
^
---- Actual error:
Expand All @@ -29,7 +29,7 @@ structure-valued-expr-errs-1.p4(35)
header h2_t {
^^^^
---- Originating from:
structure-valued-expr-errs-1.p4(101): Source expression '{ }' produces a result of type 'tuple<>' which cannot be assigned to a left-value with type 'header h2_t'
structure-valued-expr-errs-1.p4(101): Source expression '{}' produces a result of type 'tuple<>' which cannot be assigned to a left-value with type 'header h2_t'
hdr.h2 = {};
^^
structure-valued-expr-errs-1.p4(100)
Expand Down
41 changes: 41 additions & 0 deletions testdata/p4_16_samples/issue4883_dup_has_returned_decl.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);

control C(inout S s) {
action a1() {
if (foo(s.f == 0, false)) {
return;
}
}

@name("._xyz")
action a2() {
a1();
a1();
}

table t {
actions = { a2; }
}

apply {
t.apply();
}
}

control C2() {
apply {
S s;
C.apply(s);
}
}

control proto();
package top(proto p);

top(C2()) main;
41 changes: 41 additions & 0 deletions testdata/p4_16_samples/issue4883_dup_has_returned_decl2.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);

control C(inout S s) {
action a1() {
if (foo(s.f == 0, false)) {
return;
}
}

@name("a2")
action __xyz() {
a1();
a1();
}

table t {
actions = { __xyz; }
}

apply {
t.apply();
}
}

control C2() {
apply {
S s;
C.apply(s);
}
}

control proto();
package top(proto p);

top(C2()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C(inout S s) {
action a1() {
if (foo(s.f == 16w0, false)) {
return;
}
}
@name("._xyz") action a2() {
a1();
a1();
}
table t {
actions = {
a2();
@defaultonly NoAction();
}
default_action = NoAction();
}
apply {
t.apply();
}
}

control C2() {
@name("C") C() C_inst;
apply {
S s;
C_inst.apply(s);
}
}

control proto();
package top(proto p);
top(C2()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C2() {
@name("C2.s") S s_0;
@name("C2.C.tmp") bool C_tmp;
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("._xyz") action __xyz_0() {
C_tmp = foo(s_0.f == 16w0, false);
C_tmp = foo(s_0.f == 16w0, false);
}
@name("C2.C.t") table C_t {
actions = {
__xyz_0();
@defaultonly NoAction_1();
}
default_action = NoAction_1();
}
apply {
C_t.apply();
}
}

control proto();
package top(proto p);
top(C2()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C2() {
@name("C2.s") S s_0;
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("._xyz") action __xyz_0() {
foo(s_0.f == 16w0, false);
foo(s_0.f == 16w0, false);
}
@name("C2.C.t") table C_t {
actions = {
__xyz_0();
@defaultonly NoAction_1();
}
default_action = NoAction_1();
}
apply {
C_t.apply();
}
}

control proto();
package top(proto p);
top(C2()) main;
37 changes: 37 additions & 0 deletions testdata/p4_16_samples_outputs/issue4883_dup_has_returned_decl.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C(inout S s) {
action a1() {
if (foo(s.f == 0, false)) {
return;
}
}
@name("._xyz") action a2() {
a1();
a1();
}
table t {
actions = {
a2;
}
}
apply {
t.apply();
}
}

control C2() {
apply {
S s;
C.apply(s);
}
}

control proto();
package top(proto p);
top(C2()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issue4883_dup_has_returned_decl.p4(34): [--Wwarn=uninitialized_use] warning: s.f may be uninitialized
C.apply(s);
^
issue4883_dup_has_returned_decl.p4(11): [--Wwarn=uninitialized_use] warning: s.f may be uninitialized
if (foo(s.f == 0, false)) {
^^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C(inout S s) {
action a1() {
if (foo(s.f == 16w0, false)) {
return;
}
}
@name("a2") action __xyz() {
a1();
a1();
}
table t {
actions = {
__xyz();
@defaultonly NoAction();
}
default_action = NoAction();
}
apply {
t.apply();
}
}

control C2() {
@name("C") C() C_inst;
apply {
S s;
C_inst.apply(s);
}
}

control proto();
package top(proto p);
top(C2()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C2() {
@name("C2.s") S s_0;
@name("C2.C.tmp") bool C_tmp;
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("C2.C.a2") action C_a2_0() {
C_tmp = foo(s_0.f == 16w0, false);
C_tmp = foo(s_0.f == 16w0, false);
}
@name("C2.C.t") table C_t {
actions = {
C_a2_0();
@defaultonly NoAction_1();
}
default_action = NoAction_1();
}
apply {
C_t.apply();
}
}

control proto();
package top(proto p);
top(C2()) main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <core.p4>

struct S {
bit<16> f;
}

extern bool foo(in bool x, in bool y);
control C2() {
@name("C2.s") S s_0;
@noWarn("unused") @name(".NoAction") action NoAction_1() {
}
@name("C2.C.a2") action C_a2_0() {
foo(s_0.f == 16w0, false);
foo(s_0.f == 16w0, false);
}
@name("C2.C.t") table C_t {
actions = {
C_a2_0();
@defaultonly NoAction_1();
}
default_action = NoAction_1();
}
apply {
C_t.apply();
}
}

control proto();
package top(proto p);
top(C2()) main;
Loading

0 comments on commit b0890a8

Please sign in to comment.