Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wants to merge 5 commits into from

Conversation

asl
Copy link
Contributor

@asl asl commented Mar 25, 2024

For #4120 and p4lang/p4-spec#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
Copy link
Contributor Author

asl commented Mar 25, 2024

Tagging @jafingerhut and @ChrisDodd

@asl asl marked this pull request as draft March 25, 2024 09:43
@fruffy fruffy added core Topics concerning the core segments of the compiler (frontend, midend, parser) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/). labels Mar 25, 2024
ir/write_context.cpp Outdated Show resolved Hide resolved
@@ -231,6 +231,7 @@ inline std::ostream& operator<<(std::ostream& out, const P4::Token& t) {
ELSE ENTRIES ENUM ERROR EXIT EXTERN HEADER HEADER_UNION IF IN INOUT
INT KEY LIST SELECT MATCH_KIND TYPE OUT PACKAGE PARSER PRAGMA PRIORITY RETURN
STATE STRING STRUCT SWITCH TABLE TRANSITION TUPLE TYPEDEF VARBIT VALUESET VOID
%token<Token> FOREACH BREAK CONTINUE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is structured in a way to simplify review. It could be merged into the token list above

@asl
Copy link
Contributor Author

asl commented Mar 26, 2024

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

@ChrisDodd ChrisDodd mentioned this pull request Mar 26, 2024
@fruffy
Copy link
Collaborator

fruffy commented Jul 11, 2024

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

@ChrisDodd
Copy link
Contributor

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.

@asl asl closed this Jul 11, 2024
@asl asl deleted the foreach-poc branch July 11, 2024 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Topics concerning the core segments of the compiler (frontend, midend, parser) p4-spec Topics related to the P4 specification (https://github.com/p4lang/p4-spec/).
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants