numba / numba-rvsdg

Numba compatible RVSDG (Regionalized Value State Dependence Graph) utilities.
https://numba-rvsdg.readthedocs.io/
BSD 2-Clause "Simplified" License
18 stars 7 forks source link

Unifying RETURN_VALUE bytecode #90

Open sklam opened 1 year ago

sklam commented 1 year ago

The RETURN_VALUE bytecode is not unified by when SCFG join returns. This leads to SCFG that semantically terminates in the middle of switches.

For example:

import dis
from numba_rvsdg.core.datastructures.scfg import SCFG
from numba_rvsdg.core.datastructures.byte_flow import ByteFlow

def example(x):
    if x:
        return 1
    else:
        return 2

dis.dis(example)

bf = ByteFlow.from_bytecode(example).restructure()
bf.scfg.view()

The bytecode is:

  5           0 RESUME                   0

  6           2 LOAD_FAST                0 (x)
              4 POP_JUMP_FORWARD_IF_FALSE     2 (to 10)

  7           6 LOAD_CONST               1 (1)
              8 RETURN_VALUE

  9     >>   10 LOAD_CONST               2 (2)
             12 RETURN_VALUE

The SCFG Screenshot 2023-07-31 at 6 28 27 PM

Notice that both "branch" regions contain a RETURN_VALUE.

I have two workarounds at https://github.com/sklam/numba/commit/c9d363449c0a01a5745dde4604d3bf0ebff98d42:

Update - Aug 4

I have a new workaround that solely addresses the problem in RVSDG->NumbaIR (bcinterp.py) at https://github.com/numba/numba/commit/a117aae0d8a4012662f531ab8e965cf97b90938d. It relies on interpreting RETURN_VALUE as non terminating.