Select expression semantics for duplicate case expressions

Skip to first unread message

Pitchumani Sivanupandi

Dec 6, 2023, 7:24:41 AM12/6/23
to p4-dev
Hi All,

Is there any frontend passes that checks select case keyset expressions for duplicate?
select (p.tcp.port) {
  0x1234: port_2;
  0x1234: port_3;

Note: Expressions such as variable and value_set can also be used in case expression.

Is it left to target to implement semantics for duplicate checking?
Or Is there any pass that target backend can use?

The spec says keyset expressions may overlap:


Gibb, Glen

Dec 6, 2023, 10:32:06 AM12/6/23
to Sivanupandi, Pitchumani, p4-dev

Hi Pitchumani,


I don’t believe that there’s currently a frontend pass that does this.






You received this message because you are subscribed to the Google Groups "p4-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit

Vladimir Gurevich

Dec 6, 2023, 2:01:24 PM12/6/23
to Gibb, Glen, Sivanupandi, Pitchumani, p4-dev
Hello Pitchumani,

Please note, that the semantics is defined very clearly -- the code is executed in the program order. In your case that means that the transition will always happen to the state port_2 and never to port_3. Thus the code is completely legal, although in this particular case some low-level warning might be sometimes helpful, and sometimes not -- this construct has some practical uses.

I am not exactly sure what you want to achieve, but at the very minimum you should not be adding passes to reject such code.

Happy hacking,

Mihai Budiu

Dec 6, 2023, 2:20:54 PM12/6/23
to Vladimir Gurevich, Gibb, Glen, Sivanupandi, Pitchumani, p4-dev
We have several passes that rewrite select and switch statements, but not the one you may want.

There's a pass called simplifySelectList in the midend which expands labels to all have the same kind. This is needed because a default (_) can match both a tuple and a scalar.

There is another pass called replaceSelectRange, which replaces range expressions with cubes using masks. This may produce an exponential number of labels in the size of the original label size (number of fields).

Constant folding can deal with select and switch too, but for that the select expression must be constant itself.

There is also a singleArgumentSelect midend pass which converts a select of a tuple to a select from the concatenation of the tuple elements.

In general labels of select can express "cubes" (because of range expressions, default expressions, and mask expressions), so this problem reduces to some form of Boolean function minimization, which is NP-hard in general. That's why we have never attempted a general solution. A label is unreachable if it describes a set of values that is covered by all previous labels. There has been discussion about it, though. One could do the easier cases, like the one given as an example.


From: <> on behalf of Vladimir Gurevich <>
Sent: Wednesday, December 6, 2023 11:01 AM
To: Gibb, Glen <>
Cc: Sivanupandi, Pitchumani <>; p4-dev <>
Subject: Re: [P4-dev] Select expression semantics for duplicate case expressions

Pitchumani Sivanupandi

Dec 6, 2023, 11:21:52 PM12/6/23
to p4-dev, Vladimir Gurevich, Sivanupandi, Pitchumani, p4-dev, Gibb, Glen
Thanks Vladimir.

I'm implementing a compiler translation for select case with specific expression, So, I would like to confirm all possible use cases that can reach backend.


Pitchumani Sivanupandi

Dec 7, 2023, 12:14:21 AM12/7/23
to p4-dev, Mihai Budiu, Sivanupandi, Pitchumani, p4-dev, Vladimir Gurevich, Gibb, Glen
Hi Mihai,

Thanks for the detailed info.
I have checked few passes and didn't see the duplicate check. So, I wanted to confirm with dev community.


Reply all
Reply to author
0 new messages