Sei-Lisa / LSL-PyOptimizer

Optimizes a LSL2 script, folding constants, removing unused code and more. Adds new syntax features too.
GNU General Public License v3.0
35 stars 11 forks source link

AttributeError: 'optimizer' object has no attribute 'PythonType2LSL' #32

Closed KrsityKu closed 5 months ago

KrsityKu commented 5 months ago

Optimizer produces the error: AttributeError: 'optimizer' object has no attribute 'PythonType2LSL' When optimizing the following code with Constant Folding and Dead Code Removal

default
{
    link_message(integer u1, integer u2, string u3, key u4)
    {
        vector vec = <1, 1, 1>;
        float val = 2.0;
        float x = vec.x * val;

        llOwnerSay((string)x);
    }
}

This is the smallest reproducible I managed to get that throws the error, my code uses a lot of macros and constant folding that eventually results in the same error.

Also, I've noticed that with only Dead Code Removal enabled. (Constant folding off) it produces this snippet:

default
{
    link_message(integer u1, integer u2, string u3, key u4)
    {
        vector vec = <1, 1, 1>;
        2.;
        float x = vec.x * 2.;
        llOwnerSay((string)x);
    }
}

That 2.; is pointless.

Sei-Lisa commented 5 months ago

Congratulations, you've found a decade-old bug 😁

This is broken since 36759cacba2675c5a69ff32f0c21f076a90c0735 in which the constant was moved from the class to the module's top level. Will fix, thanks for the report.

As for the 2.;, I still have to look deeper into this one.

Sei-Lisa commented 5 months ago

Not necessary in this case because it was easy to reproduce for me, but in general, could you please use flag -y for getting a full backtrace when reporting errors? It is usually disabled because Python backtraces can be a bit daunting to users, but for bug reporting they are most useful to the developers.

The constant folding optimizer does much more than folding constants, despite its name. It's the one that removes side-effect-free (SEF) statements, for one, so the presence of the 2.; is expected. There's a bit of a mix of responsibilities in the constant folding and the dead code removal modules, in the interest of speed, to avoid making multiple passes. If you disable constant folding, you won't get SEF statements removed, even if that sounds like it might be the task of DCR. Actually, that code is not "dead", because in the optimizer, "dead" code is defined as code that is never reached, and execution certainly reaches the statement 2.;.

In this case, there's an additional problem where, after fixing that issue, it needs another pass to fully optimize the resulting script.

KrsityKu commented 5 months ago

could you please use flag -y for getting a full backtrace

I'll keep that in mind, though I usually use the online version to narrow down a small reproducible.