PEP 744: JIT Compilation

Great question. Honestly, the JIT may be a bit too transparent right now, so there aren’t really any great APIs for this yet.

If you want to know if a configure-based build enabled the JIT, you can use "_Py_JIT" in sysconfig.get_config_var("PY_CORE_CFLAGS").

If you want to know if a specific function contains jitted code, this is currently what we do in internal tests:

import _opcode
import types

def is_jitted(f: types.FunctionType) -> bool:
    for i in range(0, len(f.__code__.co_code), 2):
        try:
            _opcode.get_executor(f.__code__, i)
        except RuntimeError:
            # This isn't a JIT build:
            return False
        except ValueError:
            # No executor found:
            continue
        return True
    return False

Here’s an example:

>>> def fibonacci(n):
...     a, b = 0, 1
...     for _ in range(n):
...         a, b = b, a + b
...     return a
...     
>>> is_jitted(fibonacci)
False
>>> fibonacci(100)
354224848179261915075
>>> is_jitted(fibonacci)
True

This should help if you’re just curious, but if you’d like more robust/efficient APIs in sys or dis, then please open an issue letting us know what sort of functionality would be most useful to you!

7 Likes