forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make don't care args action-local when used in actions (p4lang#4817)
* Make don't care args action-local when used in actions Signed-off-by: Kyle Cripps <[email protected]> * Add gtest for RemoveDontcareArgs pass Signed-off-by: kfcripps <[email protected]> * Remove unused refMap and typeMap from strength reduction gtest Signed-off-by: kfcripps <[email protected]> * Appease cpplint Signed-off-by: Kyle Cripps <[email protected]> --------- Signed-off-by: Kyle Cripps <[email protected]> Signed-off-by: kfcripps <[email protected]>
- Loading branch information
Showing
10 changed files
with
243 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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 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,115 @@ | ||
#include <gtest/gtest.h> | ||
|
||
#include "frontends/common/parseInput.h" | ||
#include "frontends/p4/dontcareArgs.h" | ||
#include "frontends/p4/frontend.h" | ||
#include "helpers.h" | ||
#include "ir/ir.h" | ||
|
||
using namespace P4; | ||
|
||
namespace Test { | ||
|
||
struct RemoveDontcareArgsTest : P4CTest { | ||
const IR::Node *parseAndProcess(std::string program) { | ||
const auto *pgm = P4::parseP4String(program, CompilerOptions::FrontendVersion::P4_16); | ||
EXPECT_TRUE(pgm); | ||
EXPECT_EQ(::errorCount(), 0); | ||
if (!pgm) { | ||
return nullptr; | ||
} | ||
|
||
P4::TypeMap typeMap; | ||
RemoveDontcareArgs rdca(&typeMap); | ||
|
||
return pgm->apply(rdca); | ||
} | ||
}; | ||
|
||
class CollectActionAndControlLocals : public Inspector { | ||
public: | ||
unsigned actionDecls = 0; | ||
unsigned controlDecls = 0; | ||
|
||
bool preorder(const IR::P4Action *action) override { | ||
for (const auto *c : action->body->components) { | ||
if (c->is<IR::Declaration_Variable>()) ++actionDecls; | ||
} | ||
return true; | ||
} | ||
|
||
bool preorder(const IR::P4Control *control) override { | ||
for (const auto *c : control->controlLocals) { | ||
if (c->is<IR::Declaration_Variable>()) ++controlDecls; | ||
} | ||
return true; | ||
} | ||
}; | ||
|
||
TEST_F(RemoveDontcareArgsTest, Default) { | ||
std::string program_source = P4_SOURCE(R"( | ||
struct S { | ||
bit<64> f; | ||
} | ||
control C(inout S s) { | ||
@name("d") action d_0(@name("b") out bit<64> b_0) { | ||
b_0 = 64w4; | ||
} | ||
@name("foo") action foo_0() { | ||
d_0(_); | ||
} | ||
@name("t") table t_0 { | ||
actions = { | ||
foo_0(); | ||
} | ||
default_action = foo_0(); | ||
} | ||
apply { | ||
t_0.apply(); | ||
} | ||
} | ||
control proto(inout S s); | ||
package top(proto p); | ||
top(C()) main; | ||
)"); | ||
|
||
const auto *program = parseAndProcess(program_source); | ||
ASSERT_TRUE(program); | ||
ASSERT_EQ(::errorCount(), 0); | ||
|
||
CollectActionAndControlLocals collect; | ||
program->apply(collect); | ||
|
||
// Just make sure that the control block has no decls and the action has a single decl. | ||
// The expected output should look something like this: | ||
// struct S { | ||
// bit<64> f; | ||
// } | ||
// control C(inout S s) { | ||
// @name("d") action d_0(@name("b") out bit<64> b_0) { | ||
// b_0 = 64w4; | ||
// } | ||
// @name("foo") action foo_0() { | ||
// bit<64> arg; | ||
// d_0(arg); | ||
// } | ||
// @name("t") table t_0 { | ||
// actions = { | ||
// foo_0(); | ||
// } | ||
// default_action = foo_0(); | ||
// } | ||
// apply { | ||
// t_0.apply(); | ||
// } | ||
// } | ||
// control proto(inout S s); | ||
// package top(proto p); | ||
// top(C()) main; | ||
ASSERT_EQ(collect.actionDecls, 1); | ||
ASSERT_EQ(collect.controlDecls, 0); | ||
} | ||
|
||
} // namespace Test |
This file contains 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 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,27 @@ | ||
struct S { | ||
bit<64> f; | ||
} | ||
|
||
control C(inout S s) { | ||
action d(out bit<64> b) { | ||
b = 4; | ||
} | ||
|
||
action foo() { | ||
d(_); | ||
} | ||
|
||
table t { | ||
actions = { foo; } | ||
default_action = foo; | ||
} | ||
|
||
apply { | ||
t.apply(); | ||
} | ||
} | ||
|
||
control proto(inout S s); | ||
package top(proto p); | ||
|
||
top(C()) main; |
25 changes: 25 additions & 0 deletions
25
testdata/p4_16_samples_outputs/localize_action_dont_care_args-first.p4
This file contains 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,25 @@ | ||
struct S { | ||
bit<64> f; | ||
} | ||
|
||
control C(inout S s) { | ||
action d(out bit<64> b) { | ||
b = 64w4; | ||
} | ||
action foo() { | ||
d(_); | ||
} | ||
table t { | ||
actions = { | ||
foo(); | ||
} | ||
default_action = foo(); | ||
} | ||
apply { | ||
t.apply(); | ||
} | ||
} | ||
|
||
control proto(inout S s); | ||
package top(proto p); | ||
top(C()) main; |
21 changes: 21 additions & 0 deletions
21
testdata/p4_16_samples_outputs/localize_action_dont_care_args-frontend.p4
This file contains 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,21 @@ | ||
struct S { | ||
bit<64> f; | ||
} | ||
|
||
control C(inout S s) { | ||
@name("C.foo") action foo() { | ||
} | ||
@name("C.t") table t_0 { | ||
actions = { | ||
foo(); | ||
} | ||
default_action = foo(); | ||
} | ||
apply { | ||
t_0.apply(); | ||
} | ||
} | ||
|
||
control proto(inout S s); | ||
package top(proto p); | ||
top(C()) main; |
21 changes: 21 additions & 0 deletions
21
testdata/p4_16_samples_outputs/localize_action_dont_care_args-midend.p4
This file contains 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,21 @@ | ||
struct S { | ||
bit<64> f; | ||
} | ||
|
||
control C(inout S s) { | ||
@name("C.foo") action foo() { | ||
} | ||
@name("C.t") table t_0 { | ||
actions = { | ||
foo(); | ||
} | ||
default_action = foo(); | ||
} | ||
apply { | ||
t_0.apply(); | ||
} | ||
} | ||
|
||
control proto(inout S s); | ||
package top(proto p); | ||
top(C()) main; |
25 changes: 25 additions & 0 deletions
25
testdata/p4_16_samples_outputs/localize_action_dont_care_args.p4
This file contains 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,25 @@ | ||
struct S { | ||
bit<64> f; | ||
} | ||
|
||
control C(inout S s) { | ||
action d(out bit<64> b) { | ||
b = 4; | ||
} | ||
action foo() { | ||
d(_); | ||
} | ||
table t { | ||
actions = { | ||
foo; | ||
} | ||
default_action = foo; | ||
} | ||
apply { | ||
t.apply(); | ||
} | ||
} | ||
|
||
control proto(inout S s); | ||
package top(proto p); | ||
top(C()) main; |
Empty file.