Hi
I am trying to build a custom pass but i am facing a discrepancy in the GimplePass and IpaPass,
My code in both cases is:
if isinstance(stmt, gcc.GimpleAssign): if isinstance(stmt.lhs,gcc.VarDecl): kill1.update([str(stmt.lhs)]) if isinstance(stmt.rhs[0],gcc.VarDecl): gen1.update([str(stmt.rhs[0])]) if len(stmt.rhs) != 1 : if isinstance(stmt.rhs[1],gcc.VarDecl): gen1.update([str(stmt.rhs[1])])
This works fine in case of intraprocedural pass, but in case of interprocedural pass the len(stmt.rhs) throws errors like,
if len(stmt.rhs) != 1 : AttributeError: 'gcc.GimpleReturn' object has no attribute 'rhs'
or
if len(stmt.rhs) != 1 : TypeError: object of type 'gcc.SsaName' has no len()
Please help me out. I can share the full code too if required.
Thanks
On Thu, 2014-04-17 at 21:12 +0530, LUCKY AGARWAL wrote:
Hi
I am trying to build a custom pass but i am facing a discrepancy in the GimplePass and IpaPass,
My code in both cases is:
if isinstance(stmt, gcc.GimpleAssign): if isinstance(stmt.lhs,gcc.VarDecl): kill1.update([str(stmt.lhs)]) if isinstance(stmt.rhs[0],gcc.VarDecl): gen1.update([str(stmt.rhs[0])]) if len(stmt.rhs) != 1 : if isinstance(stmt.rhs[1],gcc.VarDecl): gen1.update([str(stmt.rhs[1])])
This works fine in case of intraprocedural pass, but in case of interprocedural pass the len(stmt.rhs) throws errors like,
if len(stmt.rhs) != 1 : AttributeError: 'gcc.GimpleReturn' object has no attribute 'rhs'
or
if len(stmt.rhs) != 1 : TypeError: object of type 'gcc.SsaName' has no len()
Please help me out. I can share the full code too if required.
The naming and types of attributes can be a bit haphazard, sorry. Some of the gimple statement subclasses have an "rhs" that's a *list* of gcc.Tree instances (GimpleAssign and GimpleCall), whereas GimpleCond has an "rhs" that's just a gcc.Tree.
The right-hand-side of a gcc.GimpleReturn is within its "retval" attribute: https://gcc-python-plugin.readthedocs.org/en/latest/gimple.html#gcc.GimpleRe...
A gcc.SsaName has attributes "var", "def_stmt", and "version": https://gcc-python-plugin.readthedocs.org/en/latest/tree.html#gcc.SsaName
If this doesn't help, perhaps you're seeing a bug? Do you somehow have a gcc.GimpleAssign with an rhs that's a gcc.SsaName? [That ought not to be possible; internally that attribute is implemented via PyGccGimple_get_rhs within gcc-python-gimple.c, and that always returns a *list* (or raises an exception).]
Hope this is helpful Dave
Thanks for the reply Dave.
I tried to print the stmt which was generating error as:
if isinstance(stmt, gcc.GimpleAssign): if isinstance(stmt.lhs,gcc.VarDecl): kill1.update([str(stmt.lhs)]) if isinstance(stmt.rhs[0],gcc.VarDecl): gen1.update([str(stmt.rhs[0])]) if isinstance(stmt,gcc.SsaName): print 'SSANAME***************************',stmt elif isinstance(stmt,gcc.GimpleReturn): print 'GimpleReturn*****************',stmt
The output I get is
GimpleReturn***************** return;
It looks like a return statement was encountered, but why was a return statement accept by isinstance(stmt, gcc.GimpleAssign).
On Fri, 2014-04-18 at 02:00 +0530, LUCKY AGARWAL wrote:
Thanks for the reply Dave.
I tried to print the stmt which was generating error as:
if isinstance(stmt, gcc.GimpleAssign): if isinstance(stmt.lhs,gcc.VarDecl): kill1.update([str(stmt.lhs)]) if isinstance(stmt.rhs[0],gcc.VarDecl): gen1.update([str(stmt.rhs[0])]) if isinstance(stmt,gcc.SsaName): print 'SSANAME***************************',stmt elif isinstance(stmt,gcc.GimpleReturn): print 'GimpleReturn*****************',stmt
The output I get is
GimpleReturn***************** return;
It looks like a return statement was encountered, but why was a return statement accept by isinstance(stmt, gcc.GimpleAssign).
Interesting. This seems like a bug in the plugin (unless the script is somehow clobbering "stmt" in that Python frame somehow, I guess).
Can you try to reduce this to a minimal reproducer: the smallest possible python script that exhibits this behavior, with the smallest possible C/C++ code?
Thanks Dave
Hi Dave,
PFA the scripts. The output for these is:
Entering Return block***************** main 2 return; GimpleReturn***************** main 2 return;
which essentially states, for function main, basic block 2, the return statement was accepted by both the conditions gcc.GimpleReturn as well as gcc.GimplAssign.
Regards Lucky
On Fri, 2014-04-18 at 20:17 +0530, LUCKY AGARWAL wrote:
Hi Dave,
PFA the scripts. The output for these is:
Entering Return block***************** main 2 return; GimpleReturn***************** main 2 return;
which essentially states, for function main, basic block 2, the return statement was accepted by both the conditions gcc.GimpleReturn as well as gcc.GimplAssign.
Thanks for providing the scripts.
I get the same output as above.
That said I don't see the reason for thinking that the return statement is accepted by both conditions: there's a single gcc.GimpleReturn for the implicit return from the function, and it matches isinstance(stmt, gcc.GimpleReturn).
If I edit the "elif" conditionals to plain "if", I can see that the isinstance(stmt, gcc.GimpleAssign) clause is *not* being triggered by the stmt, for me at least (with a build of the plugin from the latest git, using gcc-4.8.2-7.fc20.x86_64).
Am I misunderstanding things? Otherwise I'm thinking perhaps you have a bug in your script.
Hope this is helpful Dave
Hi Dave,
Solved it.
Apparently python treats spaces and tab characters differently. A tab is an indentation whereas spaces are not. The code looked properly indented but the rhs[1] code lines had spaces instead of tab.
Sorry for wasting your time, and thanks for the prompt support.
Regards Lucky Agarwal
On Sat, 2014-04-19 at 02:51 +0530, LUCKY AGARWAL wrote:
Hi Dave,
Solved it.
Apparently python treats spaces and tab characters differently. A tab is an indentation whereas spaces are not. The code looked properly indented but the rhs[1] code lines had spaces instead of tab.
Aha! Never mix spaces and tabs within Python source code. In fact, it's best to stick to all-spaces. See PEP8: http://legacy.python.org/dev/peps/pep-0008/#tabs-or-spaces
(and there are tools for checking this).
Spaces *are* treated as indentation; but in a mixed spaces-and-tabs file it's likely to give something unintuitive (I believe the parser does the equivalent of treating tabs as indenting to the next 8-space tabstop, which may or may not look how your text editor displays them).
In any case, never mix them.
Sorry for wasting your time, and thanks for the prompt support.
No problem; I'm glad we got it figured out.
Good luck with your script.
BTW if you build something you want to share (or even just to brag about), can you post info about it to this list?
It's nice to be able to list (or link to) things on the "Success Stories" page here: https://gcc-python-plugin.readthedocs.org/en/latest/success.html
Dave
I will keep the space-tab indentation in mind while coding in python next time.
This project was a simple implementation of Classical Funtional approach using Summary Flow functions for Interprocedural Live Variables Analysis.
It was a course project to understand the feasibility and limitations of gcc-python-plugin for research purposes.
Thanks a ton again.
Regards Lucky
gcc-python-plugin@lists.stg.fedorahosted.org