p4c icon indicating copy to clipboard operation
p4c copied to clipboard

[WIP] Add example implementation for bounded foreach-style loops

Open asl opened this issue 11 months ago • 2 comments

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);
            }
        }
    }

asl avatar Mar 25 '24 09:03 asl

Tagging @jafingerhut and @ChrisDodd

asl avatar Mar 25 '24 09:03 asl

Another part missed here from downstream: type inference check that types of loop index variable is compatible with the range.

asl avatar Mar 26 '24 08:03 asl

Are the changes here merged into #4562? Can this be closed?

fruffy avatar Jul 11 '24 00:07 fruffy

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.

ChrisDodd avatar Jul 11 '24 00:07 ChrisDodd