On Oct 31, 2008, at 3:58 AM, Dag Sverre Seljebotn wrote: > Stefan Behnel wrote: >> Jason Evans wrote: >>> Using the latest cython-dev, the following program causes a C >>> compiler >>> warning: >>> >>> -------------------------------------------------------------------- >>> - >>> cdef foo(): >>> cdef x = 42 >>> >>> if x == 42 and x == 43: >>> print "foo" >>> >>> foo() >>> -------------------------------------------------------------------- >>> - >>> foo.c: In function ‘__pyx_f_3foo_foo’: >>> foo.c:214: warning: unused variable ‘__pyx_3’ >> >> I've seen things like this happen when the switch transform >> optimisation >> hits. It can leave already allocated temp variables unused. > > However x isn't C-typed so the switch transform shouldn't kick in > here?
I believe this is because it never needs to store the Python result of the and--the expressions are turned into c bints and the and is done in pure C. This is an optimization I did a while back. > >> Does anyone know if there is an easy way to move the temp >> allocation to a >> later time in cases like this? It would be best to move it after >> the tree >> optimisation transformations. > > First of all, temp allocation is a bit fragile, and I strongly > recommend > waiting until after the release before fixing this one. > > There's plans to move all temp allocation to code generation. There's > two things one can use, depending on the case: > > 1) Manually allocated temps can be allocated through the > code.funcscope > object instead of the env object > > 2) I made a transition class "NewTempExprNode" (near top of > ExprNodes.py) which does temp allocation during code generation, after > all tranforms have been run. > > To start having the temporary result of an expression node be > allocated > later in the pipeline, the class should simply inherit from > NewTempsExprNode rather than ExprNode. > > This does however not work with some expression nodes which makes > assumptions about earlier availability of the temp expression, which > have to be refactored and/or rewritten first. > > Once all ExprNodes works using this node as ancestor, its alternative > temp implementation should be folded into ExprNode and the class > removed. It's going to be a big task, but things will be *much* nicer once temps are allocated at this later stage in code generation. Thanks Dag for all your work in this direction. Until then I wouldn't worry too much about unused variable declarations as long as they're just warnings and the code produced is still correct. - Robert _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
