Example #1
0
    def get_cfa_and_namedblocks(self, fn):
        fid = FunctionIdentity.from_function(fn)
        bc = ByteCode(func_id=fid)
        cfa = self.cfa(bc)
        namedblocks = self._scan_namedblocks(bc, cfa)

        #### To debug, uncomment below
        # print(bc.dump())
        # print("IDOMS")
        # for k, v in sorted(cfa.graph.immediate_dominators().items()):
        #     print('{} -> {}'.format(k, v))
        # print("DOMFRONT")
        # for k, vs in sorted(cfa.graph.dominance_frontier().items()):
        #     print('{} -> {}'.format(k, vs))
        # print(namedblocks)
        # cfa.graph.render_dot().view()

        return cfa, namedblocks
Example #2
0
def get_func_ir(func):
    func_id = FunctionIdentity.from_function(func)
    bc = ByteCode(func_id=func_id)
    interp = Interpreter(func_id)
    func_ir = interp.interpret(bc)
    return func_ir
axpy[blocks, threads](r, a, x, y)

# Sanity check
assert (np.all(r == a * x + y))

# Python bytecode

from dis import dis  # noqa
dis(axpy.py_func)

# Bytecode interpretation

from numba.core.bytecode import ByteCode, FunctionIdentity  # noqa
from numba.core.interpreter import Interpreter  # noqa

fi = FunctionIdentity.from_function(axpy.py_func)
interp = Interpreter(fi)
bc = ByteCode(fi)
ir = interp.interpret(bc)

# Control flow analysis

interp.cfa.dump()
dg = interp.cfa.graph.render_dot()
dg.render()  # or just dg in notebook

# Data flow analysis

interp.dfa.infos

# Numba IR