Skip to content

Low effort, precise specialization in tier 2 using lookup tables, replace and replicate. #660

@markshannon

Description

@markshannon

The primary motivation for this is BINARY_OP, but it can apply to BINARY_SUBSCR, COMPARE_OP, and CONTAINS_OP.

The idea is that we add a single tier1 specialization that contains a table lookup. The table is const table of function pointers.
That is:

replaced op(_BINARY_OP_TABLE_LOOKUP, (index/1, left, right -- res)) {
    res = BinaryOpTable[index](left, right);
}
macro (BINARY_OP_TABLE) = _BINARY_TYPE_CHECK + _BINARY_OP_TABLE_LOOKUP;

We replace _BINARY_OP_TABLE_LOOKUP in tier 2 with

replicate(N) op(_BINARY_OP_INLINE, (left, right -- res)) {
    res = BinaryOpTable[oparg](left, right);
}

Note how _BINARY_OP_INLINE replaces index with oparg.
As oparg is a const, the C compiler will (at least it should) eliminate the table lookup.
If we declare the relevant function as inline, it will be inlined and we end up with an optimal tier 2 uop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions