p4-spec icon indicating copy to clipboard operation
p4-spec copied to clipboard

Clarify whether P4-16 allows `switch` statements in actions

Open kfcripps opened this issue 3 weeks ago • 9 comments

Current behavior of the p4c compiler is:

  1. Report the following error message for backends that do run the EliminateSwitch midend pass:
error: SwitchStatement: switch statements not supported in actions on this target
  1. Supports switch statements in actions for backends that do not run the EliminateSwitch midend pass.
    • Note that this is not tested in the p4c repository because the p4test backend does run the EliminateSwitch midend pass.

However, the P4-16 spec seems to indicate that switch statements are not supported in actions at all.

For example, returns are not supported in parsers, and switch statements are only supported in control blocks.

The switch statement can only be used within control blocks.

Should the P4-16 spec be modified to allow switch statements in actions? Or should the above error message be moved to a frontend pass that is by default run for all backends?

Below is an example program, which backends that do not run EliminateSwitch do accept, and backends that do run EliminateSwitch do not accept.

extern void __e(in bit<28> arg);

struct meta_t {
    bit<8> f1;
    bit<8> f2;
}

action foo(inout meta_t meta) {
    switch (meta.f1 + meta.f2) {
        0 :       { __e(0); }
        2 :       { }
        3 :
        5 :       { __e(5); }
        7 :
        default : { __e(99); }
    }
}

control C(inout meta_t meta) {
    table t {
        actions = { foo(meta); }
        default_action = foo(meta);
    }

    apply {
        t.apply();
    }
}

control proto(inout meta_t meta);
package top(proto p);

top(C()) main;

kfcripps avatar Feb 04 '25 00:02 kfcripps