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

Add statement splitter utility #5158

Merged
merged 11 commits into from
Mar 6, 2025
Merged

Conversation

vlstill
Copy link
Contributor

@vlstill vlstill commented Mar 3, 2025

Add functionality for splitting statements. Split given statement so that on all control-flow paths all the statements up to the first one matching predicate are in a before statement, while the rest (starting from the matching statemet) is in an after statement.

The visiting order is a bit tricky, but it should work for any code without loops and without non-spec branching constructs. Not currently used in P4C, but I think this is worth having as it can be useful for some targets.

E.g.

a = a + 4
if (b > 5) {
    bit<3> v = c + 2;
    t1.apply();
    d = 8 + v;
} else {
    if (e == 4) {
        d = 1;
        t2.apply();
        c = 2
    }
}

====>

splitting on table calls:
before:

a = a + 4
cond_1 = b > 5;
if (cond_1) {
    v = c + 2;
} else {
    cond_2 = e == 4;
    if (cond_2) {
        d = 1;
    }
}

after:

if (cond_1) {
    t1.apply();
    d = 8 + v;
} else {
    if (cond_2) {
        t2.apply();
        c = 2
    }
}

and finally hoisted declarations:

bool cond_1;
bool cond_2;
bit<3> v;

Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
Signed-off-by: Vladimír Štill <[email protected]>
@vlstill vlstill added the core Topics concerning the core segments of the compiler (frontend, midend, parser) label Mar 3, 2025
@vlstill vlstill self-assigned this Mar 3, 2025
@fruffy fruffy added the run-validation Use this tag to trigger a Validation CI run. label Mar 3, 2025
@fruffy
Copy link
Collaborator

fruffy commented Mar 3, 2025

Would try to enable this with p4test just to see whether there are any failures. Excluding ref file mismatches ofc.

Edit: Oh I see, this is a very targeted pass. Hard to test broadly. In your example, the input would be the particular table.apply() method call statements?

@vlstill
Copy link
Contributor Author

vlstill commented Mar 3, 2025

Would try to enable this with p4test just to see whether there are any failures. Excluding ref file mismatches ofc.

Edit: Oh I see, this is a very targeted pass. Hard to test broadly. In your example, the input would be the particular table.apply() method call statements?

Yes, it is not even a pass that is exposed, just the function. Intended to be used in other passes.

In the example, condition would be something like

[](const auto *stmt, const auto *) {
  if (const auto *mcs = stmt->to<IR::MethodCallStatement>()) {
    if (const auto *apply = P4::MethodCall::resolve(mcs, refMap)->to<P4::ApplyMethod>()) {
      return apply->apllyObject->is<IR::P4Table>();
    }
  }
  return false;
}

return false;
}

for (size_t i = 0, sz = bs->components.size(); i < sz; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We generally prefer C++ for statements like

for (auto &el : bc->components) {

here the & in only necessary in a Transform/Modifier where you're changing the block statement. In an Inspector you can leave it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, but I do need the index in the body of the loop, and in absence of a good range adapter like Python's enumerate, I prefer to fall back to C style loop in this cases, compared to e.g. using pointer distance to recover the index.

@vlstill vlstill requested review from ChrisDodd and asl March 5, 2025 14:48
Signed-off-by: Vladimír Štill <[email protected]>
@vlstill vlstill enabled auto-merge March 6, 2025 13:13
Signed-off-by: Vladimír Štill <[email protected]>
@vlstill vlstill added this pull request to the merge queue Mar 6, 2025
Merged via the queue into p4lang:main with commit ec3f4c9 Mar 6, 2025
21 checks passed
@vlstill vlstill deleted the statement-splitter branch March 6, 2025 15:46
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) run-validation Use this tag to trigger a Validation CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants