Skip to content

Commit

Permalink
Fix for issue p4lang#425
Browse files Browse the repository at this point in the history
  • Loading branch information
mbudiu-vmw committed Apr 12, 2017
1 parent 464cb9b commit 1713113
Show file tree
Hide file tree
Showing 94 changed files with 394 additions and 438 deletions.
6 changes: 5 additions & 1 deletion ir/base.def
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,11 @@ class Annotation {
Annotation(ID n, intmax_t v) : name(n) {
expr.push_back(new IR::Constant(v)); }
/// Predefined annotations used by the compiler
static const cstring nameAnnotation;
static const cstring nameAnnotation; /// indicates the control-plane name
static const cstring tableOnlyAnnotation; /// action cannot be a default_action
static const cstring defaultOnlyAnnotation; /// action can only be a default_action
static const cstring atomicAnnotation; /// code should be executed atomically
static const cstring hiddenAnnotation; /// object should not be exposed to the control-plane
toString{ return cstring("@") + name; }
validate{ BUG_CHECK(!name.name.isNullOrEmpty(), "empty annotation name"); }
}
Expand Down
4 changes: 4 additions & 0 deletions ir/type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ const IR::ID IR::Type_Table::hit = ID("hit");
const IR::ID IR::Type_Table::action_run = ID("action_run");

const cstring IR::Annotation::nameAnnotation = "name";
const cstring IR::Annotation::tableOnlyAnnotation = "tableonly";
const cstring IR::Annotation::defaultOnlyAnnotation = "defaultonly";
const cstring IR::Annotation::atomicAnnotation = "atomic";
const cstring IR::Annotation::hiddenAnnotation = "hidden";

std::map<int, const IR::Type_Bits*> *Type_Bits::signedTypes = nullptr;
std::map<int, const IR::Type_Bits*> *Type_Bits::unsignedTypes = nullptr;
Expand Down
12 changes: 10 additions & 2 deletions midend/actionSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ const IR::Node* DoMoveActionsToTables::postorder(IR::MethodCallStatement* statem
auto props = new IR::TableProperties(nm);
// Synthesize a new table
cstring tblName = IR::ID(refMap->newName(cstring("tbl_") + ac->action->name.name), nullptr);
auto tbl = new IR::P4Table(tblName, props);

auto annos = new IR::Annotations();
annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation,
IR::Vector<IR::Expression>()));
auto tbl = new IR::P4Table(tblName, annos, props);
tables.push_back(tbl);

// Table invocation statement
Expand Down Expand Up @@ -197,7 +201,11 @@ const IR::Statement* DoSynthesizeActions::createAction(const IR::Statement* toAd
b->push_back(toAdd);
body = new IR::BlockStatement(toAdd->srcInfo, b);
}
auto action = new IR::P4Action(name, new IR::ParameterList(), body);

auto annos = new IR::Annotations();
annos->add(new IR::Annotation(IR::Annotation::hiddenAnnotation,
IR::Vector<IR::Expression>()));
auto action = new IR::P4Action(name, annos, new IR::ParameterList(), body);
actions.push_back(action);
auto actpath = new IR::PathExpression(name);
auto repl = new IR::MethodCallExpression(actpath);
Expand Down
76 changes: 46 additions & 30 deletions midend/actionSynthesis.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,41 @@ limitations under the License.

namespace P4 {

// Policy which selects the control blocks where action
// synthesis is applied.
/**
Policy which selects the control blocks where action
synthesis is applied.
*/
class ActionSynthesisPolicy {
public:
virtual ~ActionSynthesisPolicy() {}
// If the policy returns true the control block is processed,
// otherwise it is left unchanged.
/**
If the policy returns true the control block is processed,
otherwise it is left unchanged.
*/
virtual bool convert(const IR::P4Control* control) const = 0;
};

// Convert direct action calls to table invocations.
// control c() {
// action x(in bit b) { ... }
// apply { x(e); }
// }
// turns into
// control c() {
// action x(in bit b) { ... }
// table _tmp() {
// actions = { x(e); }
// const default_action = x();
// }
// apply { _tmp.apply(); }
// }
// For this to work all variable declarations must have been moved to the beginning.
/**
Convert direct action calls to table invocations.
control c() {
action x(in bit b) { ... }
apply { x(e); }
}
turns into
control c() {
action x(in bit b) { ... }
table _tmp() {
actions = { x(e); }
const default_action = x();
}
apply { _tmp.apply(); }
}
For this to work all variable declarations must have been moved to the beginning.
*/
class DoMoveActionsToTables : public Transform {
ReferenceMap* refMap;
TypeMap* typeMap;
Expand All @@ -66,17 +76,23 @@ class DoMoveActionsToTables : public Transform {
const IR::Node* postorder(IR::MethodCallStatement* statement) override;
};

// Convert some statements into action invocations by synthesizing new actions.
// E.g.
// control c(inout bit x) {
// apply { x = 1; }
// }
// is converted to:
// control c(inout bit x) {
// action act() { x = 1; }
// apply { act(); }
// }
// For this to work all variable declarations must have been moved to the beginning.
/**
Convert some statements into action invocations by synthesizing new actions.
E.g.
control c(inout bit x) {
apply { x = 1; }
}
is converted to:
control c(inout bit x) {
action act() { x = 1; }
apply { act(); }
}
For this to work all variable declarations must have been moved to the beginning.
*/
class DoSynthesizeActions : public Transform {
ReferenceMap* refMap;
TypeMap* typeMap;
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_14_samples_outputs/exact_match_mask1-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
}
default_action = NoAction_0();
}
action act() {
@hidden action act() {
key_0 = hdr.data.f1 & 32w0xff00ff;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_14_samples_outputs/hit-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
}
default_action = NoAction_5();
}
action act() {
@hidden action act() {
tmp_0 = true;
}
action act_0() {
@hidden action act_0() {
tmp_0 = false;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
table tbl_act_0 {
@hidden table tbl_act_0 {
actions = {
act_0();
}
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_14_samples_outputs/hitmiss-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
}
default_action = NoAction_5();
}
action act() {
@hidden action act() {
tmp_0 = true;
}
action act_0() {
@hidden action act_0() {
tmp_0 = false;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
table tbl_act_0 {
@hidden table tbl_act_0 {
actions = {
act_0();
}
Expand Down
8 changes: 4 additions & 4 deletions testdata/p4_14_samples_outputs/miss-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,19 @@ control ingress(inout headers hdr, inout metadata meta, inout standard_metadata_
}
default_action = NoAction_5();
}
action act() {
@hidden action act() {
tmp_0 = true;
}
action act_0() {
@hidden action act_0() {
tmp_0 = false;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
table tbl_act_0 {
@hidden table tbl_act_0 {
actions = {
act_0();
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/action-synth-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ parser ParserI(packet_in pk, out H hdr, inout M meta, inout standard_metadata_t
control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
@name("aux.a") action aux_a() {
}
table tbl_aux_a {
@hidden table tbl_aux_a {
actions = {
aux_a();
}
Expand Down
6 changes: 3 additions & 3 deletions testdata/p4_16_samples_outputs/action_call_ebpf-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ control pipe(inout Headers_t headers, out bool pass) {
@name("Reject") action Reject_0() {
pass = x;
}
action act() {
@hidden action act() {
x = true;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
table tbl_Reject {
@hidden table tbl_Reject {
actions = {
Reject_0();
}
Expand Down
2 changes: 1 addition & 1 deletion testdata/p4_16_samples_outputs/action_param1-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ control c(inout bit<32> x) {
@name("a") action a_0() {
x = 32w15;
}
table tbl_a {
@hidden table tbl_a {
actions = {
a_0();
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/arith-inline-bmv2-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
}
const default_action = c_add();
}
action act() {
@hidden action act() {
sm.egress_spec = 9w0;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
Expand Down
16 changes: 8 additions & 8 deletions testdata/p4_16_samples_outputs/arith2-inline-bmv2-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,37 @@ control deparser(packet_out b, in Headers h) {
}

control ingress(inout Headers h, inout Meta m, inout standard_metadata_t sm) {
action act() {
@hidden action act() {
h.h.c = 8w0;
}
action act_0() {
@hidden action act_0() {
h.h.c = 8w1;
}
action act_1() {
@hidden action act_1() {
sm.egress_spec = 9w0;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
const default_action = act();
}
table tbl_act_0 {
@hidden table tbl_act_0 {
actions = {
act_0();
}
const default_action = act_0();
}
table tbl_act_1 {
@hidden table tbl_act_1 {
actions = {
act_1();
}
const default_action = act_1();
}
apply {
if (h.h.a < h.h.b)
if (h.h.a < h.h.b)
tbl_act.apply();
else
else
tbl_act_0.apply();
tbl_act_1.apply();
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/array_field-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ control c(out H[2] h);
package top(c _c);
control my(out H[2] s) {
bit<1> tmp_1;
action act() {
@hidden action act() {
s[32w0].z = 1w1;
s[32w1].z = 1w0;
tmp_1 = f(s[32w0].z, 1w0);
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/clone-bmv2-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ parser ParserI(packet_in pk, out H hdr, inout M meta, inout standard_metadata_t
}

control IngressI(inout H hdr, inout M meta, inout standard_metadata_t smeta) {
action act() {
@hidden action act() {
clone(CloneType.I2E, smeta.clone_spec);
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/complex-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ extern bit<32> f(in bit<32> x);
control c(inout bit<32> r) {
bit<32> tmp_2;
bit<32> tmp_4;
action act() {
@hidden action act() {
tmp_2 = f(32w5);
tmp_4 = f(tmp_2);
r = tmp_4;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
Expand Down
4 changes: 2 additions & 2 deletions testdata/p4_16_samples_outputs/complex1-midend.p4
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ control c(inout bit<32> r) {
bit<32> tmp_8;
bit<32> tmp_10;
bit<32> tmp_12;
action act() {
@hidden action act() {
tmp_6 = f(32w5, 32w2);
tmp_8 = f(32w2, 32w3);
tmp_10 = f(32w6, tmp_8);
tmp_12 = f(tmp_6, tmp_10);
r = tmp_12;
}
table tbl_act {
@hidden table tbl_act {
actions = {
act();
}
Expand Down
Loading

0 comments on commit 1713113

Please sign in to comment.