thuml/depyf

decompile error for `while`

sumory opened this issue · 1 comments

sumory commented

the test case from test_advanced.py#L18 is decompiled a wrong result. is it an expected behavior or a known bug?

def f(a):
    while a < 5:
        a += 1
        if a > 3:
            continue
        else:
            break
    return 2 * a

''' error: decompile a wrong result
def f(a):
    if a < 5:
        a += 1
        if a > 3:
            return 2 * a
        else:
            return 2 * a
    return 2 * a
'''
df = decompile(f)

Hi, thanks for your interest.

The most difficult part of decompilation is control flow, especially complicated control flows like while. In fact, I doubt if it is even possible to perfectly decompile any Python bytecode with arbitary control flow.

That said, I build in some heuristics for control flow decompilation, especially tailored to deep learning code. Unfortunately, that does not work for while loops.

So, in short, it is a known bug (hey, every Python bytecode decompiler has known bugs anyway :( ). But it does not affect the main usage of depyf in deep learning compilers.