Skip to content

Commit e2ca9cc

Browse files
Fixing bug in HSIndexSimplifier path (#3382)
* fixing bug #issue3374
1 parent f7c41b4 commit e2ca9cc

11 files changed

+898
-22
lines changed

midend/hsIndexSimplify.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const IR::Node* HSIndexTransform::postorder(IR::ArrayIndex* curArrayIndex) {
5151
return curArrayIndex;
5252
}
5353

54-
IR::Node* HSIndexSimplifier::eliminateArrayIndexes(HSIndexFinder& aiFinder,
54+
IR::Node* HSIndexContretizer::eliminateArrayIndexes(HSIndexFinder& aiFinder,
5555
IR::Statement* statement,
5656
const IR::Expression* expr) {
5757
if (aiFinder.arrayIndex == nullptr) {
@@ -94,7 +94,7 @@ IR::Node* HSIndexSimplifier::eliminateArrayIndexes(HSIndexFinder& aiFinder,
9494
}
9595
if (expr != nullptr && locals != nullptr) {
9696
// Add case for write out of bound.
97-
cstring typeString = expr->type->node_type_name();
97+
cstring typeString = expr->type->toString();
9898
const IR::PathExpression* pathExpr = nullptr;
9999
if (generatedVariables->count(typeString) == 0) {
100100
// Add assigment of undefined header.
@@ -120,7 +120,7 @@ IR::Node* HSIndexSimplifier::eliminateArrayIndexes(HSIndexFinder& aiFinder,
120120
return new IR::BlockStatement(newComponents);
121121
}
122122

123-
IR::Node* HSIndexSimplifier::preorder(IR::AssignmentStatement* assignmentStatement) {
123+
IR::Node* HSIndexContretizer::preorder(IR::AssignmentStatement* assignmentStatement) {
124124
HSIndexFinder aiFinder(locals, refMap, typeMap, generatedVariables);
125125
assignmentStatement->left->apply(aiFinder);
126126
if (aiFinder.arrayIndex == nullptr) {
@@ -157,12 +157,13 @@ class IsNonConstantArrayIndex : public KeyIsSimple, public Inspector {
157157
}
158158
};
159159

160-
IR::Node* HSIndexSimplifier::preorder(IR::P4Control* control) {
160+
IR::Node* HSIndexContretizer::preorder(IR::P4Control* control) {
161161
DoSimplifyKey keySimplifier(refMap, typeMap, new IsNonConstantArrayIndex());
162162
const auto* controlKeySimplified = control->apply(keySimplifier)->to<IR::P4Control>();
163163
auto* newControl = controlKeySimplified->clone();
164164
IR::IndexedVector<IR::Declaration> newControlLocals;
165-
HSIndexSimplifier hsSimplifier(refMap, typeMap, &newControlLocals, generatedVariables);
165+
GeneratedVariablesMap blockGeneratedVariables;
166+
HSIndexContretizer hsSimplifier(refMap, typeMap, &newControlLocals, &blockGeneratedVariables);
166167
newControl->body = newControl->body->apply(hsSimplifier)->to<IR::BlockStatement>();
167168
for (auto* declaration : controlKeySimplified->controlLocals) {
168169
if (declaration->is<IR::P4Action>()) {
@@ -175,19 +176,18 @@ IR::Node* HSIndexSimplifier::preorder(IR::P4Control* control) {
175176
return newControl;
176177
}
177178

178-
IR::Node* HSIndexSimplifier::preorder(IR::P4Parser* parser) {
179+
IR::Node* HSIndexContretizer::preorder(IR::P4Parser* parser) {
179180
prune();
180181
return parser;
181182
}
182183

183-
IR::Node* HSIndexSimplifier::preorder(IR::BlockStatement* blockStatement) {
184-
GeneratedVariablesMap blockGeneratedVariables;
185-
HSIndexFinder aiFinder(locals, refMap, typeMap, &blockGeneratedVariables);
184+
IR::Node* HSIndexContretizer::preorder(IR::BlockStatement* blockStatement) {
185+
HSIndexFinder aiFinder(locals, refMap, typeMap, generatedVariables);
186186
blockStatement->apply(aiFinder);
187187
if (aiFinder.arrayIndex == nullptr) {
188188
return blockStatement;
189189
}
190-
HSIndexSimplifier hsSimplifier(refMap, typeMap, locals, &blockGeneratedVariables);
190+
HSIndexContretizer hsSimplifier(refMap, typeMap, locals, generatedVariables);
191191
auto* newBlock = blockStatement->clone();
192192
IR::IndexedVector<IR::StatOrDecl> newComponents;
193193
for (auto& component : blockStatement->components) {
@@ -204,21 +204,21 @@ IR::Node* HSIndexSimplifier::preorder(IR::BlockStatement* blockStatement) {
204204
return newBlock;
205205
}
206206

207-
IR::Node* HSIndexSimplifier::preorder(IR::IfStatement* ifStatement) {
207+
IR::Node* HSIndexContretizer::preorder(IR::IfStatement* ifStatement) {
208208
HSIndexFinder aiFinder(locals, refMap, typeMap, generatedVariables);
209209
ifStatement->condition->apply(aiFinder);
210210
return eliminateArrayIndexes(aiFinder, ifStatement, nullptr);
211211
}
212212

213-
IR::Node* HSIndexSimplifier::preorder(IR::MethodCallStatement* methodCallStatement) {
213+
IR::Node* HSIndexContretizer::preorder(IR::MethodCallStatement* methodCallStatement) {
214214
HSIndexFinder aiFinder(locals, refMap, typeMap, generatedVariables);
215215
methodCallStatement->apply(aiFinder);
216216
// Here we mean that in/out parameter will be replaced by correspondent assignments.
217217
// In this case no need to consider assignment to undefined value.
218218
return eliminateArrayIndexes(aiFinder, methodCallStatement, nullptr);
219219
}
220220

221-
IR::Node* HSIndexSimplifier::preorder(IR::SwitchStatement* switchStatement) {
221+
IR::Node* HSIndexContretizer::preorder(IR::SwitchStatement* switchStatement) {
222222
HSIndexFinder aiFinder(locals, refMap, typeMap, generatedVariables);
223223
switchStatement->expression->apply(aiFinder);
224224
return eliminateArrayIndexes(aiFinder, switchStatement, nullptr);

midend/hsIndexSimplify.h

+15-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef std::map<cstring, const IR::PathExpression*> GeneratedVariablesMap;
1515
/// adds it to the corresponded local definitions.
1616
class HSIndexFinder : public Inspector {
1717
friend class HSIndexTransform;
18-
friend class HSIndexSimplifier;
18+
friend class HSIndexContretizer;
1919
IR::IndexedVector<IR::Declaration>* locals;
2020
ReferenceMap* refMap;
2121
TypeMap* typeMap;
@@ -42,7 +42,7 @@ class HSIndexFinder : public Inspector {
4242

4343
/// This class substitutes index of a header stack in all occurence of found header stack.
4444
class HSIndexTransform : public Transform {
45-
friend class HSIndexSimplifier;
45+
friend class HSIndexContretizer;
4646
int index;
4747
HSIndexFinder& hsIndexFinder;
4848

@@ -66,14 +66,14 @@ class HSIndexTransform : public Transform {
6666
/// hdivr0 = hdr.i;
6767
/// if (hdivr0 == 0) { hdr.h[0] = 1;}
6868
/// else if (hdivr0 == 1){hdr.h[1] = 1;}
69-
class HSIndexSimplifier : public Transform {
69+
class HSIndexContretizer : public Transform {
7070
ReferenceMap* refMap;
7171
TypeMap* typeMap;
7272
IR::IndexedVector<IR::Declaration>* locals;
7373
GeneratedVariablesMap* generatedVariables;
7474

7575
public:
76-
HSIndexSimplifier(ReferenceMap* refMap, TypeMap* typeMap,
76+
HSIndexContretizer(ReferenceMap* refMap, TypeMap* typeMap,
7777
IR::IndexedVector<IR::Declaration>* locals = nullptr,
7878
GeneratedVariablesMap* generatedVariables = nullptr)
7979
: refMap(refMap), typeMap(typeMap), locals(locals), generatedVariables(generatedVariables) {
@@ -92,6 +92,17 @@ class HSIndexSimplifier : public Transform {
9292
const IR::Expression* expr);
9393
};
9494

95+
class HSIndexSimplifier : public PassManager {
96+
public:
97+
HSIndexSimplifier(ReferenceMap* refMap, TypeMap* typeMap) {
98+
// remove block statements
99+
passes.push_back(new TypeChecking(refMap, typeMap, true));
100+
passes.push_back(new HSIndexContretizer(refMap, typeMap));
101+
setName("HSIndexSimplifier");
102+
}
103+
};
104+
105+
95106
} // namespace P4
96107

97108
#endif /* _MIDEND_HSINDEXSIMPLIFY_H_ */

testdata/p4_16_samples/issue3374.p4

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
/*
2+
Copyright 2022 Intel Corporation
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
#include <core.p4>
18+
#include <v1model.p4>
19+
20+
#define MAX_LAYERS 2
21+
typedef bit<48> EthernetAddress;
22+
23+
enum bit<16> ether_type_t {
24+
TPID = 0x8100,
25+
IPV4 = 0x0800,
26+
IPV6 = 0x86DD
27+
}
28+
29+
header ethernet_t {
30+
EthernetAddress dstAddr;
31+
EthernetAddress srcAddr;
32+
bit<16> etherType;
33+
}
34+
35+
header vlan_tag_h {
36+
bit<3> pcp;
37+
bit<1> cfi;
38+
bit<12> vid;
39+
ether_type_t ether_type;
40+
}
41+
42+
struct headers_t {
43+
ethernet_t ethernet;
44+
vlan_tag_h[MAX_LAYERS] vlan_tag;
45+
}
46+
47+
struct main_metadata_t {
48+
bit<2> depth;
49+
bit<16> ethType;
50+
}
51+
52+
parser parserImpl(
53+
packet_in pkt,
54+
out headers_t hdrs,
55+
inout main_metadata_t meta,
56+
inout standard_metadata_t stdmeta)
57+
{
58+
state start {
59+
meta.depth = MAX_LAYERS - 1;
60+
pkt.extract(hdrs.ethernet);
61+
transition select(hdrs.ethernet.etherType) {
62+
ether_type_t.TPID : parse_vlan_tag;
63+
default: accept;
64+
}
65+
}
66+
state parse_vlan_tag {
67+
pkt.extract(hdrs.vlan_tag.next);
68+
meta.depth = meta.depth - 1;
69+
transition select(hdrs.vlan_tag.last.ether_type) {
70+
ether_type_t.TPID : parse_vlan_tag;
71+
default: accept;
72+
}
73+
}
74+
}
75+
76+
control verifyChecksum(
77+
inout headers_t hdr,
78+
inout main_metadata_t meta)
79+
{
80+
apply { }
81+
}
82+
83+
control ingressImpl(
84+
inout headers_t hdrs,
85+
inout main_metadata_t meta,
86+
inout standard_metadata_t stdmeta)
87+
{
88+
action drop_packet() {
89+
mark_to_drop(stdmeta);
90+
}
91+
action execute() {
92+
meta.ethType = hdrs.vlan_tag[meta.depth - 1].ether_type;
93+
hdrs.vlan_tag[meta.depth - 1].ether_type = (ether_type_t)16w2;
94+
hdrs.vlan_tag[meta.depth].vid = (bit<12>)hdrs.vlan_tag[meta.depth].cfi;
95+
hdrs.vlan_tag[meta.depth].vid = hdrs.vlan_tag[meta.depth-1].vid;
96+
// hdrs.vlan_tag[meta.depth].vid = hdrs.vlan_tag[1].vid;
97+
}
98+
action execute_1() {
99+
drop_packet();
100+
}
101+
102+
table stub {
103+
key = {
104+
hdrs.vlan_tag[meta.depth].vid : exact;
105+
}
106+
107+
actions = {
108+
execute;
109+
}
110+
const default_action = execute;
111+
size=1000000;
112+
}
113+
114+
table stub1 {
115+
key = {
116+
hdrs.ethernet.etherType : exact;
117+
}
118+
119+
actions = {
120+
execute_1;
121+
}
122+
const default_action = execute_1;
123+
size=1000000;
124+
}
125+
apply {
126+
switch (hdrs.vlan_tag[meta.depth].vid) {
127+
// switch (hdrs.vlan_tag[0].vid) {
128+
12w1: { stub.apply();}
129+
12w2: {
130+
if (hdrs.vlan_tag[meta.depth].ether_type == hdrs.ethernet.etherType)
131+
stub1.apply();
132+
}
133+
}
134+
}
135+
}
136+
137+
control egressImpl(
138+
inout headers_t hdr,
139+
inout main_metadata_t meta,
140+
inout standard_metadata_t stdmeta)
141+
{
142+
apply { }
143+
}
144+
145+
control updateChecksum(
146+
inout headers_t hdr,
147+
inout main_metadata_t meta)
148+
{
149+
apply { }
150+
}
151+
152+
control deparserImpl(
153+
packet_out pkt,
154+
in headers_t hdr)
155+
{
156+
apply {
157+
pkt.emit(hdr.ethernet);
158+
}
159+
}
160+
161+
V1Switch(parserImpl(),
162+
verifyChecksum(),
163+
ingressImpl(),
164+
egressImpl(),
165+
updateChecksum(),
166+
deparserImpl()) main;

testdata/p4_16_samples_outputs/control-hs-index-test6-midend.p4

+4-5
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ parser p(packet_in pkt, out headers hdr, inout Meta m, inout standard_metadata_t
3939

4040
control ingress(inout headers h, inout Meta m, inout standard_metadata_t sm) {
4141
bit<32> hsiVar;
42-
bit<32> hsiVar_0;
4342
bit<32> hsVar;
4443
@noWarn("unused") @name(".NoAction") action NoAction_1() {
4544
}
@@ -66,7 +65,7 @@ control ingress(inout headers h, inout Meta m, inout standard_metadata_t sm) {
6665
key_0 = hsVar;
6766
}
6867
@hidden action controlhsindextest6l48_2() {
69-
hsiVar_0 = h.i.index;
68+
hsiVar = h.i.index;
7069
}
7170
@hidden table tbl_controlhsindextest6l48 {
7271
actions = {
@@ -94,11 +93,11 @@ control ingress(inout headers h, inout Meta m, inout standard_metadata_t sm) {
9493
}
9594
apply {
9695
tbl_controlhsindextest6l48.apply();
97-
if (hsiVar_0 == 32w0) {
96+
if (hsiVar == 32w0) {
9897
tbl_controlhsindextest6l48_0.apply();
99-
} else if (hsiVar_0 == 32w1) {
98+
} else if (hsiVar == 32w1) {
10099
tbl_controlhsindextest6l48_1.apply();
101-
} else if (hsiVar_0 >= 32w1) {
100+
} else if (hsiVar >= 32w1) {
102101
tbl_controlhsindextest6l48_2.apply();
103102
}
104103
t_0.apply();

0 commit comments

Comments
 (0)