Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InlineFunctions pass sometime seems to generate invalid code. #2126

Closed
fruffy opened this issue Jan 4, 2020 · 9 comments · Fixed by #2133
Closed

InlineFunctions pass sometime seems to generate invalid code. #2126

fruffy opened this issue Jan 4, 2020 · 9 comments · Fixed by #2133
Assignees
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.

Comments

@fruffy
Copy link
Collaborator

fruffy commented Jan 4, 2020

The intermediate output after the InlineFunctions pass occasionally leads to invalid P4 code.

If we compile issue1538.p4 with
./p4c/build/p4c --top4 FrontEnd,MidEnd --dump dmp p4c/testdata/p4_16_samples/issue1538.p4 and then try to run
./p4c/build/p4c dmp/issue1538-FrontEnd_44_InlineFunctions.p4i
on the generated file we get the following output:

dmp/issue1538-FrontEnd_44_InlineFunctions.p4i(37): error: Duplicate declaration of hasReturned; previous at 
            bool hasReturned = false;
                 ^^^^^^^^^^^
dmp/issue1538-FrontEnd_44_InlineFunctions.p4i(26)
            bool hasReturned = false;
                 ^^^^^^^^^^^
dmp/issue1538-FrontEnd_44_InlineFunctions.p4i(38): error: Duplicate declaration of retval; previous at 
            bit<16> retval;
                    ^^^^^^
dmp/issue1538-FrontEnd_44_InlineFunctions.p4i(27)
            bit<16> retval;
                    ^^^^^^
error: 2 errors encountered, aborting compilation
@jafingerhut
Copy link
Contributor

I tried reproducing this, but with latest p4c source code built, I get errors about the --top4 and --dump command line options not being recognized. I know I have used these before. Is anyone else seeing such messages for those options, or know why I might be?

@jafingerhut
Copy link
Contributor

Never mind. I figured it out. By default, if you run a copy of p4c that you have installed using sudo make install, the Python command line parsing code is in 'INSTALLED' mode, which does not recognize a few options like --top4. It does if you run the executable in the p4c/build directory.

@hesingh
Copy link
Contributor

hesingh commented Jan 4, 2020

Right. I just used --top4 and --dump yesterday with latest p4c and see no problems. I always use p4c/build/p4c

@jafingerhut
Copy link
Contributor

I don't see the error on the file you mentioned, but I do on the file issue1538-FrontEnd_44_InlineFunctions.p4 and a few others.

It appears the errors should not be issued, though, since the variables are scoped locally within a block { in braces} inside a parser state, so perhaps there is a bug in scoping of local variables?

@hesingh
Copy link
Contributor

hesingh commented Jan 4, 2020

I don't see the error on the file you mentioned, but I do on the file issue1538-FrontEnd_44_InlineFunctions.p4 and a few others.

It appears the errors should not be issued, though, since the variables are scoped locally within a block { in braces} inside a parser state, so perhaps there is a bug in scoping of local variables?

It looks like an issue only in parser code. The same p4i file has bool hasReturned used in ingress control with no issues.

@jafingerhut
Copy link
Contributor

The attached program is close to minimal one that exhibits the bug. When I attempt to compile it with the 2020-Jan-04 master version of p4c, I get this error, which appears to me like no such error should occur:

$ p4c --target bmv2 --arch v1model issue-2126.p4
issue-2126.p4(58): error: Duplicate declaration of retval; previous at 
            bit<16> retval;
                    ^^^^^^
issue-2126.p4(42)
            bit<16> retval;
                    ^^^^^^
error: 1 errors encountered, aborting compilation

issue-2126.p4.txt

@hesingh
Copy link
Contributor

hesingh commented Jan 4, 2020

The Duplicate declaration error is from the symbols table used by the lang parser (yacc code) in p4c. This symbols table has no knowledge of code blocks such as code within { }. The damage is already done when FrontEnd_xx_InlineFunctions.p4i is created. p4c's frontend passes have to be looked at to see which pass declares variables such as hasReturned inside code blocks of parser instead of declaring the variable outside of any parser state. This is one way to fix the error.

@fruffy
Copy link
Collaborator Author

fruffy commented Jan 5, 2020

I accidentally provided the first pass where the issue is fixed again after InlineFunctions. I edited the post.
Normally brackets should be interpreted as parserBlockStatement, maybe there is something amiss here?

@mihaibudiu mihaibudiu self-assigned this Jan 9, 2020
@mihaibudiu mihaibudiu added the bug This behavior is unintended and should be fixed. label Jan 9, 2020
@mihaibudiu
Copy link
Contributor

There are several problems, but one of them is certainly a bug - the one highlighted by Andy.

@mihaibudiu mihaibudiu added the fixed This topic is considered to be fixed. label Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This behavior is unintended and should be fixed. fixed This topic is considered to be fixed.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants