p4c: Reference tables from another control block

We want to model table dependencies (using annotations), and we want to directly reference the path expression, like so:

control c() {
    table t { ... }
    @depend(t) table t2 { ... }
...
}

but the issue is that some table may depend on another which is not in the same control block:

control c1() {
    table t { ... }
...
}
control c2() {
    @depend(c1.t) table t2 { ... }  // c1.t does not compile
...
}

Is this possible? If not, is this a feasible request?

Currently we have to use the table name as a string, which we would like to avoid if possible.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

I can see several possibilities:

  • hack the compiler adding the capabilities you need (singleton pattern, visibility modifiers, renaming passes that consistently change names, etc). If you do that you can contribute your changes back to the community and see whether other people could benefit from them, and get them potentially integrated in the compiler. After all, this is one of the benefits of the code being available.
  • use string annotations, as you are already doing. The problem with renaming still remains, but perhaps the @name annotations that the compiler inserts are enough for you to recover the original names
  • use a higher level language and tool that generates P4 and feeds it to the standard compiler after you analyze the program and extract the information you need. You could perhaps build this on top of the existing codebase by adapting the existing parser.

The best solution will depend on what kind of information you really need to extract from your program. Writing a compiler analysis to build the control/and data dependence graph between the tables is not particularly difficult, but I assume you have a different notion of dependency in mind and you want to feed to the tools information that is not apparent in the source program.

Unfortunately the language spec dictates that declarations within a parser are control are private: https://p4.org/p4-spec/docs/P4-16-v1.2.1.html#sec-name-visibility

This is a possible feature that could be added to the language - visibility modifiers (e.g., public) if there is a use case for them, but no one has proposed this yet.

Even assuming that you do make the declarations visible, you will be faced with the problem that the compiler will inline the controls and rename variables; the compiler has to understand your annotations to preserve the names when doing that.