p4lang / p4c

P4_16 reference compiler
https://p4.org/
Apache License 2.0
642 stars 430 forks source link

for loop incorrectly transformed by p4c #4739

Open jafingerhut opened 2 weeks ago

jafingerhut commented 2 weeks ago

I was writing some simple test cases of for loops to try out some aspects of this feature, and believe I have found a case where the compiler front end in some pass has an incorrect transformation, because the output of the front end, and thus also the mid end, appear functionally to be infinite loops, whereas the input program was definitely not.

I tested with two versions of p4c, the most recent of which was:

The command I used was:

mkdir -p tmp
p4test --dump tmp --top4 FrontEndLast,FrontEndDump,MidEndLast loop-var-modifiable-in-body1.p4

The attached zip file contains the input P4 program, and the contents of the tmp directory created by the command above on my system. p4c-issue-4739.zip

jafingerhut commented 2 weeks ago

@ChrisDodd Pinging you on this one, in case you have time to look at it.

ChrisDodd commented 2 weeks ago

The problem seems to happen in DoSimplifyDefUse -- it seems that it thinks the assignments to i in the loop are dead (have no uses), so they get elimiinated. Not sure why that is happening.