This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author BTaskaya
Recipients BTaskaya, zhangchaospecial
Date 2021-06-24.17:00:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1624554004.17.0.560139560541.issue44505@roundup.psfhosted.org>
In-reply-to
Content
There are multiple issues with doing 'static' loop invariant code motion in python, but the most obvious one is how would you detect if anything in the body is related to the loop or not? The trivial way that the compiled languages do this optimization is generating some sort of data dependency graph, and seeing whether anything in an expression refers to the loop target though in Python you can't know for sure unless you know every possible type in the loop body and the type of the loop target which is something nearly impossible to infer (due to the dynamic nature of python). You could theoretically do this only if the whole loop only uses simple values:

for x in [1, 2, 3, 4, 5]:
    a = 5
    if a > 3:
        a += 2
    continue

and in that case, it is better to just optimize the whole loop away but since this kind of code doesn't exist at all in the public, there is no need to make the compiler more complicated.

Another option is generating multiple code objects with guards, which would require to do first some sort of partial type inference and generate the code for the possible types. It would require a substantial amount of changes on both the compiler and the interpreter which is something I'd say can be deferred for later if we implement a tracing JIT. With traces, it should be way easier to do this sort of trivial compiler optimization (loop invariants, constant propagation, GVN, etc.) since we would know the types, and possible code paths.
History
Date User Action Args
2021-06-24 17:00:04BTaskayasetrecipients: + BTaskaya, zhangchaospecial
2021-06-24 17:00:04BTaskayasetmessageid: <1624554004.17.0.560139560541.issue44505@roundup.psfhosted.org>
2021-06-24 17:00:04BTaskayalinkissue44505 messages
2021-06-24 17:00:03BTaskayacreate