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

Check if 'match_kind.exact' is defined when expanding switch statements #3654

Merged
merged 1 commit into from
Nov 3, 2022
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
16 changes: 16 additions & 0 deletions midend/eliminateSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ limitations under the License.

namespace P4 {

const IR::Node* DoEliminateSwitch::postorder(IR::P4Program* program) {
if (!exactNeeded)
return program;
for (auto *obj : program->objects) {
if (auto *match_kind = obj->to<IR::Declaration_MatchKind>()) {
if (match_kind->getDeclByName(P4CoreLibrary::instance.exactMatch.Id()))
return program;
}
}
::error(ErrorType::ERR_NOT_FOUND,
"Could not find declaration for 'match_kind.exact', which is needed to implement "
"switch statements; did you include core.p4?");
return program;
}

const IR::Node* DoEliminateSwitch::postorder(IR::P4Control* control) {
for (auto a : toInsert)
control->controlLocals.push_back(a);
Expand Down Expand Up @@ -54,6 +69,7 @@ const IR::Node* DoEliminateSwitch::postorder(IR::SwitchStatement* statement) {
auto tableKeyEl = new IR::KeyElement(
src, new IR::PathExpression(key),
new IR::PathExpression(P4CoreLibrary::instance.exactMatch.Id()));
exactNeeded = true;
IR::IndexedVector<IR::ActionListElement> actionsList;
IR::IndexedVector<IR::Property> properties;
IR::Vector<IR::SwitchCase> cases;
Expand Down
5 changes: 4 additions & 1 deletion midend/eliminateSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ class DoEliminateSwitch final : public Transform {
const TypeMap* typeMap;
std::vector<const IR::Declaration*> toInsert;
public:
explicit DoEliminateSwitch(ReferenceMap* refMap, const TypeMap* typeMap):
bool exactNeeded = false;

DoEliminateSwitch(ReferenceMap* refMap, const TypeMap* typeMap):
refMap(refMap), typeMap(typeMap)
{ setName("DoEliminateSwitch"); CHECK_NULL(refMap); CHECK_NULL(typeMap); }
const IR::Node* postorder(IR::SwitchStatement* statement) override;
const IR::Node* postorder(IR::P4Control* control) override;
const IR::Node* postorder(IR::P4Program* program) override;
};

class EliminateSwitch final : public PassManager {
Expand Down
11 changes: 11 additions & 0 deletions testdata/p4_16_errors/issue3613.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
control c(in bit<4> a)
{
apply {
switch (a) {
4w0: {}
}
}
}
control ct(in bit<4> a);
package pt(ct t);
pt(c()) main;
12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue3613-first.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
control c(in bit<4> a) {
apply {
switch (a) {
4w0: {
}
}
}
}

control ct(in bit<4> a);
package pt(ct t);
pt(c()) main;
12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue3613-frontend.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
control c(in bit<4> a) {
apply {
switch (a) {
4w0: {
}
}
}
}

control ct(in bit<4> a);
package pt(ct t);
pt(c()) main;
12 changes: 12 additions & 0 deletions testdata/p4_16_errors_outputs/issue3613.p4
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
control c(in bit<4> a) {
apply {
switch (a) {
4w0: {
}
}
}
}

control ct(in bit<4> a);
package pt(ct t);
pt(c()) main;
1 change: 1 addition & 0 deletions testdata/p4_16_errors_outputs/issue3613.p4-stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[--Werror=not-found] error: Could not find declaration for 'match_kind.exact', which is needed to implement switch statements; did you include core.p4?