Skip to content

Commit d7e02b9

Browse files
committed
Fix #5042: Allow non-constant args for directionless action params
Signed-off-by: devalgupta404 <devalgupta4@gmail.com>
1 parent 190c314 commit d7e02b9

File tree

2 files changed

+87
-9
lines changed

2 files changed

+87
-9
lines changed

frontends/p4/typeChecking/typeCheckExpr.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,15 +1893,8 @@ const IR::Expression *TypeInferenceBase::actionCall(bool inActionList,
18931893
typeError("%1%: parameter %2% cannot be bound: it is set by the control plane", arg,
18941894
param);
18951895
} else if (inTable) {
1896-
// For actions None parameters are treated as IN
1897-
// parameters when the action is called directly. We
1898-
// don't require them to be bound to a compile-time
1899-
// constant. But if the action is instantiated in a
1900-
// table (as default_action or entries), then the
1901-
// arguments do have to be compile-time constants.
1902-
if (!isCompileTimeConstant(arg->expression))
1903-
typeError("%1%: action argument must be a compile-time constant",
1904-
arg->expression);
1896+
// Directionless action parameters behave like 'in'
1897+
// parameters even when used in tables (P4 Spec Section 6.8).
19051898
}
19061899
// This is like an assignment; may make additional conversions.
19071900
newExpr = assignment(arg, param->type, arg->expression);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright 2026 Deval Gupta
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+
/*
18+
* Test for issue #5042: Directionless action parameters should accept
19+
* non-compile-time-known values when provided by the P4 program.
20+
* Per P4 Spec Section 6.8, such parameters behave as 'in' direction.
21+
*/
22+
23+
#include <core.p4>
24+
#include <v1model.p4>
25+
26+
header h_t {
27+
bit<8> f;
28+
}
29+
30+
struct metadata_t {
31+
bit<8> x;
32+
}
33+
34+
struct headers_t {
35+
h_t h;
36+
}
37+
38+
parser p(packet_in pkt, out headers_t hdr, inout metadata_t meta,
39+
inout standard_metadata_t std_meta) {
40+
state start {
41+
pkt.extract(hdr.h);
42+
meta.x = hdr.h.f;
43+
transition accept;
44+
}
45+
}
46+
47+
control ingress(inout headers_t hdr, inout metadata_t meta,
48+
inout standard_metadata_t std_meta) {
49+
// Action with directionless parameter 'data'
50+
action a(bit<8> data) {
51+
hdr.h.f = data;
52+
}
53+
54+
table t {
55+
key = { hdr.h.f : exact; }
56+
actions = { a; }
57+
// meta.x is NOT compile-time known, but per P4 Spec 6.8
58+
// directionless params behave as 'in' when provided by program
59+
default_action = a(meta.x);
60+
}
61+
62+
apply {
63+
t.apply();
64+
}
65+
}
66+
67+
control egress(inout headers_t hdr, inout metadata_t meta,
68+
inout standard_metadata_t std_meta) {
69+
apply { }
70+
}
71+
72+
control deparser(packet_out pkt, in headers_t hdr) {
73+
apply { pkt.emit(hdr.h); }
74+
}
75+
76+
control verifyChecksum(inout headers_t hdr, inout metadata_t meta) {
77+
apply { }
78+
}
79+
80+
control computeChecksum(inout headers_t hdr, inout metadata_t meta) {
81+
apply { }
82+
}
83+
84+
V1Switch(p(), verifyChecksum(), ingress(), egress(),
85+
computeChecksum(), deparser()) main;

0 commit comments

Comments
 (0)