# P4c: switch statement fall-through case broken?

**URL:** <https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450>\
**Category:** P4 Dev\
**Created:** [August 12, 2022, 6:34pm UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450 "2022-08-12T18:34:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![muke56](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@muke56](https://forum.p4.org/u/muke56)\
**Post date:** [August 12, 2022, 6:34pm UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/1 "2022-08-12T18:34:45Z")

</div>

Hello folks … I am hitting what seems like a basic issue with p4c compiler.  
I have a switch statement with a fall-through case, like so

```auto
100 control aaa {
101 apply {  
102 ... 
103 meta.xxx = hdr.yyy;
104 switch (table1.apply().action_run) {
105 actionA: // fall-through
106 actionB: {
107 table2.apply();
108 }
109 }
110 table3.apply();
111 }
112 }

```

the json generated skips table 1, table 2 and directly jumps to table3

```auto
        {
          "name" : "tbl_aaa103",
          "source_info" : {
            "filename" : "aaa.p4",
            "line" : 103,
            "column" : 10,
            "source_fragment" : "= hdr.yyy; ..."
          },
          ... 
          "base_default_next" : "table3",
          "next_tables" : {
            "aaa103" : "table3"
          },

```

whereas if I change the code to this

```auto
100 control aaa {
101 apply {  
102 ... 
103 meta.xxx = hdr.yyy;
104 switch (table1.apply().action_run) {
106 actionB: {
107 table2.apply();
108 }
109 }
110 table3.apply();
111 }
112 }

```

then the json generated has this

```auto
        {
          "name" : "tbl_aaa103",
          "source_info" : {
            "filename" : "aaa.p4",
            "line" : 103,
            "column" : 10,
            "source_fragment" : "= hdr.yyy; ..."
          },
          ... 
          "base_default_next" : "table1",
          "next_tables" : {
            "aaa103" : "table1"
          },

```

I am invoking p4c from the p4lang/p4c:latest docker image.  
Am I doing something wrong ?

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [August 13, 2022, 3:39am UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/2 "2022-08-13T03:39:42Z")

</div>

I would recommend publishing complete P4 source files for the cases that you believe may be generating incorrect code, e.g. in a public code repository like Github or Gitlab, and provide a link to it here.

Could it perhaps be that your table1 and table2’s actions do not do anything, and the compiler might be eliminating them as an optimization that preserves the same packet processing behavior?

---

<div class="post-metadata">

**Author:** ![muke56](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@muke56](https://forum.p4.org/u/muke56)\
**Post date:** [August 15, 2022, 11:13pm UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/3 "2022-08-15T23:13:17Z")

</div>

Thanks for getting back Andy. No I don’t see how the compiler could have optimized this out … especially since just the 1 change of removing the fallback action A fixes it.

The source code is from the public Sonic Dash repo. This is the line that causes the issue

> <https://github.com/Azure/DASH/blob/main/dash-pipeline/bmv2/dash_outbound.p4#L121>

From the source code route\_vnet is the actionA (fall-through)  
and route\_vnet\_direct is actionB.

---

<div class="post-metadata">

**Author:** ![andyfingerhut](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/andyfingerhut/32/17_2.png) [@andyfingerhut](https://forum.p4.org/u/andyfingerhut)\
**Post date:** [August 16, 2022, 5:10am UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/4 "2022-08-16T05:10:55Z")

</div>

By any chance have you found a smaller P4 program that exhibits a similar looks-wrong behavior in its output? If so, it would be great if you could publish that, as it can help more quickly isolate the cause of the problem.

---

<div class="post-metadata">

**Author:** ![mbudiu-vmw](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.p4.org/mbudiu-vmw/32/40_2.png) [@mbudiu-vmw](https://forum.p4.org/u/mbudiu-vmw)\
**Post date:** [August 16, 2022, 3:21pm UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/5 "2022-08-16T15:21:55Z")

</div>

Any chance you could file a bug with the open-source compiler here:

> **[Issues · p4lang/p4c](https://github.com/p4lang/p4c/issues)**
>
> P4\_16 reference compiler. Contribute to p4lang/p4c development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![muke56](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@muke56](https://forum.p4.org/u/muke56)\
**Post date:** [August 17, 2022, 5:23pm UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/6 "2022-08-17T17:23:14Z")

</div>

Yes I hit the issue with this simple p4 test program.

> <https://github.com/mukeshmv/p4_test/blob/master/test_pipeline.p4>

---

<div class="post-metadata">

**Author:** ![muke56](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@muke56](https://forum.p4.org/u/muke56)\
**Post date:** [August 17, 2022, 5:29pm UTC](https://forum.p4.org/t/p4c-switch-statement-fall-through-case-broken/450/7 "2022-08-17T17:29:32Z")

</div>

Sure thanks. I filed the issue.

> <https://github.com/p4lang/p4c/issues/3488>
>
> I am hitting what seems like a basic issue with a fall-through action in a P4 sw…itch statement.
> I have a switch statement with a fall-through case, like so with the fall-through action at line number 105.
> 
> \`\`\`
> 100 control aaa {
> 101 apply {  
> 102 ... 
> 103 meta.xxx = hdr.yyy;
> 104 switch (table1.apply().action\_run) {
> 105 actionA: // fall-through
> 106 actionB: {
> 107 table2.apply();
> 108 }
> 109 }
> 110 table3.apply();
> 111 }
> 112 }
> \`\`\`
> 
> the json generated skips table 1, table 2 and directly jumps to table3
> 
> \`\`\`
> {
> "name" : "tbl\_aaa103",
> "source\_info" : {
> "filename" : "aaa.p4",
> "line" : 103,
> "column" : 10,
> "source\_fragment" : "= hdr.yyy; ..."
> },
> ... 
> "base\_default\_next" : "table3",
> "next\_tables" : {
> "aaa103" : "table3"
> },
> 
> \`\`\`
> 
> whereas if I change the code to remove just the fall-through action without any other changes
> 
> \`\`\`
> 100 control aaa {
> 101 apply {  
> 102 ... 
> 103 meta.xxx = hdr.yyy;
> 104 switch (table1.apply().action\_run) {
> 106 actionB: {
> 107 table2.apply();
> 108 }
> 109 }
> 110 table3.apply();
> 111 }
> 112 }
> \`\`\`
> 
> now the generated json properly points to table1 as the next stage.  
> 
> \`\`\`
> {
> "name" : "tbl\_aaa103",
> "source\_info" : {
> "filename" : "aaa.p4",
> "line" : 103,
> "column" : 10,
> "source\_fragment" : "= hdr.yyy; ..."
> },
> ... 
> "base\_default\_next" : "table1",
> "next\_tables" : {
> "aaa103" : "table1"
> },
> \`\`\`
> 
> I am invoking p4c from the p4lang/p4c:latest docker image.
