Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the JIT's type-lowering/specialization pass's data structures. #706

Open
markshannon opened this issue Nov 22, 2024 · 0 comments
Open

Comments

@markshannon
Copy link
Member

Currently the symbol structure looks like this:

struct _Py_UopsSymbol {
    int flags;  // 0 bits: Top; 2 or more bits: Bottom
    PyTypeObject *typ;  // Borrowed reference
    PyObject *const_val;  // Owned reference (!)
    unsigned int type_version; // currently stores type version
};

which is both hard to use and inefficient. This awkwardness is effectively blocking 2 PRs: python/cpython#119478 and python/cpython#121002 as I am not willing to risk introducing bugs.

We should change the symbol to a tagged union:

union _Py_UopSymbol {
    uint8_t tag;
    _PyUopSym_Instance instance;
    _PyUopSym_Value value;
    _PyUopSym_Function func;
    _PyUopSym_Tuple tuple;
    ...
}

Each of the subtypes would start with uint8_t tag; and should be no larger than 16 bytes (on a 64bit machine).
E.g.

struct _PyUopSym_Instance {
    uint8_t tag;
    PyTypeObject *typ;  // Borrowed reference
};

All operations would take the form of a switch statement. Using an enum for the tag would allow the C compiler to generate warnings when a case was forgotten.
E.g.

bool
_Py_uop_sym_matches_type(_Py_UopsSymbol *sym, PyTypeObject *typ) 
{
    switch(sym->tag) {
        case INSTANCE_TAG:
             return sym->instance.typ == typ;
        case VALUE_TAG:
            return Py_TYPE(sym->value.value) == typ;
        ...

This is nothing novel, just good engineering practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant