p4c
p4c copied to clipboard
[WIP] Add example implementation for bounded foreach-style loops
For #4120 and https://github.com/p4lang/p4-spec/issues/1261
This is an example implementation for foreach-style bounded loops. It contains no tests (as they are written for dowstream backend) and some sema checks are certainly lacking, but still shows the intention. One thing that is currently missed is the use-def changes, I have not ported them to upstream, but will re-add shortly. Certainly changes to other passes might be required (e.g. copyprop).
The PR has some tricks how to handle loop index variable in order to play nicely with the existing code.
Examples of supported code:
action a0(bit<8> x, bit<8> y) {
// Reduce
bit<64> idx = -1;
foreach (bit<8> i in 0 .. x) {
foreach (bit<8> j in i .. y) {
idx = foo(j);
if (idx == -1)
break;
}
}
}
action a1(bit<8> x, bit<8> y) {
// Reduce
bit<64> idx = -1;
foreach (bit<8> i in 0 .. x) {
foreach (bit<8> j in 0 .. y) {
idx = foo(j);
if (idx == -1) {
continue;
}
idx = foo(i);
}
}
}
Tagging @jafingerhut and @ChrisDodd
Another part missed here from downstream: type inference check that types of loop index variable is compatible with the range.
Are the changes here merged into #4562? Can this be closed?
Are the changes here merged into #4562? Can this be closed?
There were minor syntax changes (for
instead of a new foreach
keyword), but I believe this can be closed.