Skip to content

Commit 9d54dfb

Browse files
authored
extend ActionSynthesisPolicy (#1689)
- add a query to specify when statements should be combined into a single action, or made into separate actions. - add Context argument to query functions.
1 parent 61bb527 commit 9d54dfb

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

backends/bmv2/common/backend.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class SkipControls : public P4::ActionSynthesisPolicy {
8282

8383
public:
8484
explicit SkipControls(const std::set<cstring> *skip) : skip(skip) { CHECK_NULL(skip); }
85-
bool convert(const IR::P4Control* control) const {
85+
bool convert(const Visitor::Context *, const IR::P4Control* control) override {
8686
if (skip->find(control->name) != skip->end())
8787
return false;
8888
return true;

backends/p4test/midend.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class SkipControls : public P4::ActionSynthesisPolicy {
5858

5959
public:
6060
explicit SkipControls(const std::set<cstring> *skip) : skip(skip) { CHECK_NULL(skip); }
61-
bool convert(const IR::P4Control* control) const override {
61+
bool convert(const Visitor::Context *, const IR::P4Control* control) override {
6262
if (skip->find(control->name) != skip->end())
6363
return false;
6464
return true;

midend/actionSynthesis.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ bool DoSynthesizeActions::mustMove(const IR::AssignmentStatement *assign) {
113113
const IR::Node* DoSynthesizeActions::preorder(IR::P4Control* control) {
114114
actions.clear();
115115
changes = false;
116-
if (policy != nullptr && !policy->convert(control))
116+
if (policy != nullptr && !policy->convert(getContext(), control))
117117
prune(); // skip this one
118118
return control;
119119
}
@@ -132,10 +132,20 @@ const IR::Node* DoSynthesizeActions::preorder(IR::BlockStatement* statement) {
132132
for (auto c : statement->components) {
133133
if (c->is<IR::AssignmentStatement>()) {
134134
if (mustMove(c->to<IR::AssignmentStatement>())) {
135+
if (policy && !actbody->components.empty() &&
136+
!policy->can_combine(getContext(), actbody, c)) {
137+
auto action = createAction(actbody);
138+
left->push_back(action);
139+
actbody = new IR::BlockStatement; }
135140
actbody->push_back(c);
136141
continue; }
137142
} else if (c->is<IR::MethodCallStatement>()) {
138143
if (mustMove(c->to<IR::MethodCallStatement>())) {
144+
if (policy && !actbody->components.empty() &&
145+
!policy->can_combine(getContext(), actbody, c)) {
146+
auto action = createAction(actbody);
147+
left->push_back(action);
148+
actbody = new IR::BlockStatement; }
139149
actbody->push_back(c);
140150
continue;
141151
}

midend/actionSynthesis.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,21 @@ class ActionSynthesisPolicy {
3434
If the policy returns true the control block is processed,
3535
otherwise it is left unchanged.
3636
*/
37-
virtual bool convert(const IR::P4Control* control) const = 0;
37+
virtual bool convert(const Visitor::Context *ctxt, const IR::P4Control* control) = 0;
38+
39+
/**
40+
Called for each statement that may be put into an action when there are preceeding
41+
statements already put into an action --
42+
@param ctxt context of the code being processsed (control and parents)
43+
@param blk previous statement(s) being put into an action
44+
@param stmt statement to be added to the block for a single action
45+
@returns
46+
true statement should be added to the same action
47+
false statement should start a new action
48+
*/
49+
virtual bool can_combine(const Visitor::Context *, const IR::BlockStatement *,
50+
const IR::StatOrDecl *) {
51+
return true; }
3852
};
3953

4054
/**

0 commit comments

Comments
 (0)