Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reduce duplication in new opcodes
  • Loading branch information
eendebakpt committed Mar 24, 2026
commit 7d633168e98e8c5d6b18f67122c387754424d963
56 changes: 6 additions & 50 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -793,46 +793,23 @@ dummy_func(
// assignment (+=) on ob_fval, which generates problematic JIT
// stencils on i686-pc-windows-msvc.
tier2 op(_BINARY_OP_ADD_FLOAT_INPLACE, (left, right -- res, l, r)) {

@markshannon markshannon Mar 23, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This op and its variants all share a lot of common code.
Could you factor out the code into a macro to perform the inplace operation?
Like:

tier2 op(_BINARY_OP_ADD_FLOAT_INPLACE, (left, right -- res, l, r)) {
    res = FLOAT_INPLACE_OP(left, +, right);
    l = PyStackRef_NULL;
    r = right;
    INPUTS_DEAD();
}

tier2 op(_BINARY_OP_MULTIPLY_FLOAT_INPLACE_RIGHT, (left, right -- res, l, r)) {
    res = FLOAT_INPLACE_OP(right, *, left);
    l = left
    r = PyStackRef_NULL;
    INPUTS_DEAD();
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normal C macros are not allowed in the bytecodes.c opcodes (they are not expanded). I have not yet found a way to refactor this nicely.

We could add a new macro to the DSL (like INPUTS_DEAD) for this, but it feels a bit odd to add something to the DSL for this particular case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normal C macros are not allowed in the bytecodes.c opcodes

They are. You just need to define them in ceval_macros.h. Would you like me to do this, or would you like to do it?

PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));
assert(_PyObject_IsUniquelyReferenced(left_o));
STAT_INC(BINARY_OP, hit);
double dres = ((PyFloatObject *)left_o)->ob_fval + ((PyFloatObject *)right_o)->ob_fval;
((PyFloatObject *)left_o)->ob_fval = dres;
// Transfer ownership of left to res.
// Original left is now dead.
FLOAT_INPLACE_OP(left, right, left, +);
res = left;
l = PyStackRef_NULL;
r = right;
Comment thread
eendebakpt marked this conversation as resolved.
INPUTS_DEAD();
}

tier2 op(_BINARY_OP_SUBTRACT_FLOAT_INPLACE, (left, right -- res, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));
assert(_PyObject_IsUniquelyReferenced(left_o));
STAT_INC(BINARY_OP, hit);
double dres = ((PyFloatObject *)left_o)->ob_fval - ((PyFloatObject *)right_o)->ob_fval;
((PyFloatObject *)left_o)->ob_fval = dres;
FLOAT_INPLACE_OP(left, right, left, -);
res = left;
l = PyStackRef_NULL;
r = right;
INPUTS_DEAD();
}

tier2 op(_BINARY_OP_MULTIPLY_FLOAT_INPLACE, (left, right -- res, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));
assert(_PyObject_IsUniquelyReferenced(left_o));
STAT_INC(BINARY_OP, hit);
double dres = ((PyFloatObject *)left_o)->ob_fval * ((PyFloatObject *)right_o)->ob_fval;
((PyFloatObject *)left_o)->ob_fval = dres;
FLOAT_INPLACE_OP(left, right, left, *);
res = left;
l = PyStackRef_NULL;
r = right;
Expand All @@ -841,44 +818,23 @@ dummy_func(

// Inplace RIGHT variants: mutate the uniquely-referenced right operand.
tier2 op(_BINARY_OP_ADD_FLOAT_INPLACE_RIGHT, (left, right -- res, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));
assert(_PyObject_IsUniquelyReferenced(right_o));
STAT_INC(BINARY_OP, hit);
double dres = ((PyFloatObject *)left_o)->ob_fval + ((PyFloatObject *)right_o)->ob_fval;
((PyFloatObject *)right_o)->ob_fval = dres;
FLOAT_INPLACE_OP(left, right, right, +);
res = right;
l = left;
r = PyStackRef_NULL;
INPUTS_DEAD();
}

tier2 op(_BINARY_OP_MULTIPLY_FLOAT_INPLACE_RIGHT, (left, right -- res, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));
assert(_PyObject_IsUniquelyReferenced(right_o));
STAT_INC(BINARY_OP, hit);
double dres = ((PyFloatObject *)left_o)->ob_fval * ((PyFloatObject *)right_o)->ob_fval;
((PyFloatObject *)right_o)->ob_fval = dres;
FLOAT_INPLACE_OP(left, right, right, *);
res = right;
l = left;
r = PyStackRef_NULL;
INPUTS_DEAD();
}

tier2 op(_BINARY_OP_SUBTRACT_FLOAT_INPLACE_RIGHT, (left, right -- res, l, r)) {
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
assert(PyFloat_CheckExact(left_o));
assert(PyFloat_CheckExact(right_o));
assert(_PyObject_IsUniquelyReferenced(right_o));
STAT_INC(BINARY_OP, hit);
double dres = ((PyFloatObject *)left_o)->ob_fval - ((PyFloatObject *)right_o)->ob_fval;
((PyFloatObject *)right_o)->ob_fval = dres;
FLOAT_INPLACE_OP(left, right, right, -);
res = right;
l = left;
r = PyStackRef_NULL;
Expand Down
19 changes: 19 additions & 0 deletions Python/ceval_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,22 @@ gen_try_set_executing(PyGenObject *gen)
}
return false;
}

// Macro for inplace float binary ops (tier 2 only).
// Mutates the uniquely-referenced TARGET operand in place.
// TARGET must be either left or right.
#define FLOAT_INPLACE_OP(left, right, TARGET, OP) \
do { \
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left); \
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right); \
assert(PyFloat_CheckExact(left_o)); \
assert(PyFloat_CheckExact(right_o)); \
assert(_PyObject_IsUniquelyReferenced( \
PyStackRef_AsPyObjectBorrow(TARGET))); \
STAT_INC(BINARY_OP, hit); \
double _dres = \
((PyFloatObject *)left_o)->ob_fval \
OP ((PyFloatObject *)right_o)->ob_fval; \
((PyFloatObject *)PyStackRef_AsPyObjectBorrow(TARGET)) \
->ob_fval = _dres; \
} while (0)
Loading
Loading