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.
Remove unused parser declarations and unused parser type declarations…
… that result from `RemoveRedundantParsers` pass. (p4lang#4368) * Remove unused parser declarations and unused parser type declarations that result from `RemoveRedundantParsers` pass. * Add unit test * Still call RemoveAllUnusedDeclarations before RemoveRedundantParsers and add additional RemoveAllUnusedDeclarations pass to RemoveRedundantParsers.
- Loading branch information
Showing
8 changed files
with
252 additions
and
1 deletion.
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
77 changes: 77 additions & 0 deletions
77
testdata/p4_16_samples/redundant_parsers_dangling_unused_parser_decl.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,77 @@ | ||
// The RemoveRedundantParsers pass removes applications of parsers which | ||
// unconditionally transition to the accept state from the start state. | ||
// For example: | ||
// parser callee() { | ||
// state start { | ||
// transition accept; | ||
// } | ||
// } | ||
// | ||
// parser caller() { | ||
// callee() subparser; | ||
// state start { | ||
// subparser.apply(); // <--- removed by RemoveRedundantParsers pass | ||
// transition accept; | ||
// } | ||
// } | ||
// | ||
// After the application of 'subparser' is removed by RemoveRedundantParsers, | ||
// subparser's unused declaration as well as its type's declaration remains. | ||
// This test validates that these declarations get removed after the RemoveRedundantParsers | ||
// pass. If they aren't removed, a Compiler Bug will occur in one of the inlining passes | ||
// in some cases, and other unexpected behavior could occur elsewhere as well. | ||
|
||
#include <core.p4> | ||
|
||
header h_t { | ||
bit<16> f; | ||
} | ||
|
||
struct headers { | ||
h_t h1; | ||
h_t h2; | ||
} | ||
|
||
// This unused parser type declaration remains after RemoveRedundantParsers. | ||
parser RedundantParser(inout headers hdr) { | ||
state start { | ||
transition accept; | ||
} | ||
} | ||
|
||
parser IParser2(packet_in packet, inout headers hdr) { | ||
state start { | ||
packet.extract(hdr.h2); | ||
transition select(hdr.h2.f) { | ||
default: accept; | ||
} | ||
} | ||
} | ||
|
||
parser IParser(packet_in packet, inout headers hdr) { | ||
// This unused local parser declaration remains after RemoveRedundantParsers. | ||
RedundantParser() redundant_parser; | ||
|
||
state start { | ||
packet.extract(hdr.h1); | ||
transition select(hdr.h1.f) { | ||
1 : s1; | ||
default : s2; | ||
} | ||
} | ||
|
||
state s1 { | ||
// The below application of redundant_parser gets optimized out by RemoveRedundantParsers. | ||
redundant_parser.apply(hdr); | ||
transition accept; | ||
} | ||
|
||
state s2 { | ||
IParser2.apply(packet, hdr); | ||
transition accept; | ||
} | ||
} | ||
|
||
parser Parser(packet_in p, inout headers hdr); | ||
package top(Parser p); | ||
top(IParser()) main; |
49 changes: 49 additions & 0 deletions
49
testdata/p4_16_samples_outputs/redundant_parsers_dangling_unused_parser_decl-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,49 @@ | ||
#include <core.p4> | ||
|
||
header h_t { | ||
bit<16> f; | ||
} | ||
|
||
struct headers { | ||
h_t h1; | ||
h_t h2; | ||
} | ||
|
||
parser RedundantParser(inout headers hdr) { | ||
state start { | ||
transition accept; | ||
} | ||
} | ||
|
||
parser IParser2(packet_in packet, inout headers hdr) { | ||
state start { | ||
packet.extract<h_t>(hdr.h2); | ||
transition select(hdr.h2.f) { | ||
default: accept; | ||
} | ||
} | ||
} | ||
|
||
parser IParser(packet_in packet, inout headers hdr) { | ||
RedundantParser() redundant_parser; | ||
@name("IParser2") IParser2() IParser2_inst; | ||
state start { | ||
packet.extract<h_t>(hdr.h1); | ||
transition select(hdr.h1.f) { | ||
16w1: s1; | ||
default: s2; | ||
} | ||
} | ||
state s1 { | ||
redundant_parser.apply(hdr); | ||
transition accept; | ||
} | ||
state s2 { | ||
IParser2_inst.apply(packet, hdr); | ||
transition accept; | ||
} | ||
} | ||
|
||
parser Parser(packet_in p, inout headers hdr); | ||
package top(Parser p); | ||
top(IParser()) main; |
39 changes: 39 additions & 0 deletions
39
testdata/p4_16_samples_outputs/redundant_parsers_dangling_unused_parser_decl-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,39 @@ | ||
#include <core.p4> | ||
|
||
header h_t { | ||
bit<16> f; | ||
} | ||
|
||
struct headers { | ||
h_t h1; | ||
h_t h2; | ||
} | ||
|
||
parser IParser(packet_in packet, inout headers hdr) { | ||
state start { | ||
packet.extract<h_t>(hdr.h1); | ||
transition select(hdr.h1.f) { | ||
16w1: s1; | ||
default: s2; | ||
} | ||
} | ||
state s1 { | ||
transition accept; | ||
} | ||
state s2 { | ||
transition IParser2_start; | ||
} | ||
state IParser2_start { | ||
packet.extract<h_t>(hdr.h2); | ||
transition select(hdr.h2.f) { | ||
default: s2_0; | ||
} | ||
} | ||
state s2_0 { | ||
transition accept; | ||
} | ||
} | ||
|
||
parser Parser(packet_in p, inout headers hdr); | ||
package top(Parser p); | ||
top(IParser()) main; |
31 changes: 31 additions & 0 deletions
31
testdata/p4_16_samples_outputs/redundant_parsers_dangling_unused_parser_decl-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,31 @@ | ||
#include <core.p4> | ||
|
||
header h_t { | ||
bit<16> f; | ||
} | ||
|
||
struct headers { | ||
h_t h1; | ||
h_t h2; | ||
} | ||
|
||
parser IParser(packet_in packet, inout headers hdr) { | ||
state start { | ||
packet.extract<h_t>(hdr.h1); | ||
transition select(hdr.h1.f) { | ||
16w1: s1; | ||
default: s2; | ||
} | ||
} | ||
state s1 { | ||
transition accept; | ||
} | ||
state s2 { | ||
packet.extract<h_t>(hdr.h2); | ||
transition accept; | ||
} | ||
} | ||
|
||
parser Parser(packet_in p, inout headers hdr); | ||
package top(Parser p); | ||
top(IParser()) main; |
48 changes: 48 additions & 0 deletions
48
testdata/p4_16_samples_outputs/redundant_parsers_dangling_unused_parser_decl.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,48 @@ | ||
#include <core.p4> | ||
|
||
header h_t { | ||
bit<16> f; | ||
} | ||
|
||
struct headers { | ||
h_t h1; | ||
h_t h2; | ||
} | ||
|
||
parser RedundantParser(inout headers hdr) { | ||
state start { | ||
transition accept; | ||
} | ||
} | ||
|
||
parser IParser2(packet_in packet, inout headers hdr) { | ||
state start { | ||
packet.extract(hdr.h2); | ||
transition select(hdr.h2.f) { | ||
default: accept; | ||
} | ||
} | ||
} | ||
|
||
parser IParser(packet_in packet, inout headers hdr) { | ||
RedundantParser() redundant_parser; | ||
state start { | ||
packet.extract(hdr.h1); | ||
transition select(hdr.h1.f) { | ||
1: s1; | ||
default: s2; | ||
} | ||
} | ||
state s1 { | ||
redundant_parser.apply(hdr); | ||
transition accept; | ||
} | ||
state s2 { | ||
IParser2.apply(packet, hdr); | ||
transition accept; | ||
} | ||
} | ||
|
||
parser Parser(packet_in p, inout headers hdr); | ||
package top(Parser p); | ||
top(IParser()) main; |
Empty file.