Skip to content

Commit 00d52d3

Browse files
committed
Eliminate actions from tables.
1 parent d815807 commit 00d52d3

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

core/specialization/passes/elim_dead_code.cpp

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "backends/p4tools/common/lib/table_utils.h"
77
#include "backends/p4tools/modules/flay/core/lib/return_macros.h"
88
#include "backends/p4tools/modules/flay/options.h"
9+
#include "ir/indexed_vector.h"
910
#include "ir/node.h"
1011
#include "ir/vector.h"
1112
#include "lib/error.h"
@@ -83,7 +84,7 @@ const IR::Node *ElimDeadCode::preorder(IR::SwitchStatement *switchStmt) {
8384
}
8485
continue;
8586
}
86-
printInfo("---DEAD_CODE--- %1% can be deleted.", switchCase->label);
87+
printInfo("---DEAD_CODE--- SwitchCase %1% can be deleted.", switchCase->label);
8788
_eliminatedNodes.emplace_back(switchCase, nullptr);
8889
// We are removing a statement that had previous fall-through labels.
8990
if (previousFallThrough && !filteredSwitchCases.empty() &&
@@ -150,6 +151,54 @@ const IR::Node *ElimDeadCode::preorder(IR::Member *member) {
150151
return result;
151152
}
152153

154+
const IR::Node *ElimDeadCode::preorder(IR::P4Table *table) {
155+
IR::IndexedVector<IR::ActionListElement> actionListVector;
156+
157+
ASSIGN_OR_RETURN_WITH_MESSAGE(const auto &defaultAction, table->getDefaultAction(), table,
158+
::P4::error("Table %1% does not have a default action.", table));
159+
ASSIGN_OR_RETURN_WITH_MESSAGE(
160+
const auto &defaultActionCall, defaultAction.to<IR::MethodCallExpression>(), table,
161+
::P4::error("%1% is not a method call expression.", defaultAction));
162+
163+
const auto *actionList = table->getActionList();
164+
for (const auto *action : actionList->actionList) {
165+
// Do not remove actions which have a default only annotation.
166+
// Do not remove the default action.
167+
if (action->getAnnotation(IR::Annotation::defaultOnlyAnnotation) != nullptr ||
168+
defaultActionCall.method->toString() ==
169+
action->expression->checkedTo<IR::MethodCallExpression>()->method->toString()) {
170+
actionListVector.push_back(action);
171+
continue;
172+
}
173+
auto reachabilityOpt = _reachabilityMap.get().isNodeReachable(action);
174+
if (!reachabilityOpt.has_value()) {
175+
actionListVector.push_back(action);
176+
continue;
177+
}
178+
if (reachabilityOpt.value()) {
179+
printInfo("---DEAD_CODE--- ActionListElement %1% will always be executed.", action);
180+
actionListVector.clear();
181+
actionListVector.push_back(action);
182+
break;
183+
}
184+
_eliminatedNodes.emplace_back(action, nullptr);
185+
printInfo("---DEAD_CODE--- ActionListElement %1% can be deleted.", action);
186+
}
187+
auto *newActionList = new IR::ActionList(actionListVector);
188+
IR::TableProperties properties;
189+
for (const auto *property : table->properties->properties) {
190+
if (property->name == IR::TableProperties::actionsPropertyName) {
191+
auto *newProp = property->clone();
192+
newProp->value = newActionList;
193+
properties.push_back(newProp);
194+
} else {
195+
properties.push_back(property);
196+
}
197+
}
198+
table->properties = new IR::TableProperties(properties);
199+
return table;
200+
}
201+
153202
const IR::Node *ElimDeadCode::preorder(IR::MethodCallStatement *stmt) {
154203
const auto *call = stmt->methodCall->method->to<IR::Member>();
155204
RETURN_IF_FALSE(call != nullptr && call->member == IR::IApply::applyMethodName, stmt);
@@ -169,7 +218,7 @@ const IR::Node *ElimDeadCode::preorder(IR::MethodCallStatement *stmt) {
169218
::P4::error("Table %1% does not have a default action.", tableDecl));
170219
ASSIGN_OR_RETURN_WITH_MESSAGE(
171220
const auto &defaultActionCall, defaultAction.to<IR::MethodCallExpression>(), stmt,
172-
::P4::error("%1% is not a method call expression.", table.getDefaultAction()));
221+
::P4::error("%1% is not a method call expression.", defaultAction));
173222
for (const auto *action : tableActionList) {
174223
// Do not remove the default action.
175224
if (defaultActionCall.method->toString() ==

core/specialization/passes/elim_dead_code.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ElimDeadCode : public Transform {
2727
std::vector<EliminatedReplacedPair> _eliminatedNodes;
2828

2929
const IR::Node *preorder(IR::P4Parser *parser) override;
30+
const IR::Node *preorder(IR::P4Table *p4Table) override;
3031
const IR::Node *preorder(IR::IfStatement *stmt) override;
3132
const IR::Node *preorder(IR::SwitchStatement *switchStmt) override;
3233
const IR::Node *preorder(IR::MethodCallStatement *stmt) override;

0 commit comments

Comments
 (0)