Skip to content

Commit d5b2208

Browse files
Apply review suggestions
1 parent 2541683 commit d5b2208

File tree

12 files changed

+336
-291
lines changed

12 files changed

+336
-291
lines changed

Include/internal/pycore_opcode_metadata.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_optimizer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern "C" {
1010

1111
#include "pycore_typedefs.h" // _PyInterpreterFrame
1212
#include "pycore_uop_ids.h"
13+
#include "pycore_stackref.h" // _PyStackRef
1314
#include <stdbool.h>
1415

1516

@@ -257,6 +258,7 @@ extern bool _Py_uop_sym_is_not_null(JitOptSymbol *sym);
257258
extern bool _Py_uop_sym_is_const(JitOptContext *ctx, JitOptSymbol *sym);
258259
extern PyObject *_Py_uop_sym_get_const(JitOptContext *ctx, JitOptSymbol *sym);
259260
extern JitOptSymbol *_Py_uop_sym_new_const_steal(JitOptContext *ctx, PyObject *const_val);
261+
extern _PyStackRef _Py_uop_sym_get_const_as_stackref(JitOptContext *ctx, JitOptSymbol *sym);
260262
extern JitOptSymbol *_Py_uop_sym_new_unknown(JitOptContext *ctx);
261263
extern JitOptSymbol *_Py_uop_sym_new_not_null(JitOptContext *ctx);
262264
extern JitOptSymbol *_Py_uop_sym_new_type(

Include/internal/pycore_stackref.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,15 @@ _PyStackRef_FromPyObjectImmortal(PyObject *obj, const char *filename, int linenu
132132
}
133133
#define PyStackRef_FromPyObjectImmortal(obj) _PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj), __FILE__, __LINE__)
134134

135+
static inline _PyStackRef
136+
_PyStackRef_FromPyObjectImmortalUnchecked(PyObject *obj, const char *filename, int linenumber)
137+
{
138+
return _Py_stackref_create(obj, filename, linenumber);
139+
}
140+
#define PyStackRef_FromPyObjectImmortalUnchecked(obj) _PyStackRef_FromPyObjectImmortalUnchecked(_PyObject_CAST(obj), __FILE__, __LINE__)
141+
142+
143+
135144
static inline void
136145
_PyStackRef_CLOSE(_PyStackRef ref, const char *filename, int linenumber)
137146
{
@@ -324,6 +333,17 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
324333
}
325334
#define PyStackRef_FromPyObjectImmortal(obj) PyStackRef_FromPyObjectImmortal(_PyObject_CAST(obj))
326335

336+
static inline _PyStackRef
337+
PyStackRef_FromPyObjectImmortalUnchecked(PyObject *obj)
338+
{
339+
// Make sure we don't take an already tagged value.
340+
assert(((uintptr_t)obj & Py_TAG_BITS) == 0);
341+
assert(obj != NULL);
342+
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_DEFERRED };
343+
}
344+
#define PyStackRef_FromPyObjectImmortalUnchecked(obj) PyStackRef_FromPyObjectImmortalUnchecked(_PyObject_CAST(obj))
345+
346+
327347
#define PyStackRef_CLOSE(REF) \
328348
do { \
329349
_PyStackRef _close_tmp = (REF); \
@@ -535,6 +555,12 @@ PyStackRef_FromPyObjectImmortal(PyObject *obj)
535555
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_REFCNT};
536556
}
537557

558+
static inline _PyStackRef
559+
PyStackRef_FromPyObjectImmortalUnchecked(PyObject *obj)
560+
{
561+
return (_PyStackRef){ .bits = (uintptr_t)obj | Py_TAG_REFCNT};
562+
}
563+
538564
/* WARNING: This macro evaluates its argument more than once */
539565
#ifdef _WIN32
540566
#define PyStackRef_DUP(REF) \

Include/internal/pycore_uop_metadata.h

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/bytecodes.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ dummy_func(
443443
PyStackRef_CLOSE(receiver);
444444
}
445445

446-
pure inst(UNARY_NEGATIVE, (value -- res)) {
446+
inst(UNARY_NEGATIVE, (value -- res)) {
447447
PyObject *res_o = PyNumber_Negative(PyStackRef_AsPyObjectBorrow(value));
448448
PyStackRef_CLOSE(value);
449449
ERROR_IF(res_o == NULL, error);
@@ -573,7 +573,7 @@ dummy_func(
573573
_GUARD_TYPE_VERSION +
574574
_REPLACE_WITH_TRUE;
575575

576-
pure inst(UNARY_INVERT, (value -- res)) {
576+
inst(UNARY_INVERT, (value -- res)) {
577577
PyObject *res_o = PyNumber_Invert(PyStackRef_AsPyObjectBorrow(value));
578578
PyStackRef_CLOSE(value);
579579
ERROR_IF(res_o == NULL, error);
@@ -669,7 +669,7 @@ dummy_func(
669669
EXIT_IF(!PyFloat_CheckExact(value_o));
670670
}
671671

672-
op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
672+
pure op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
673673
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
674674
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
675675
assert(PyFloat_CheckExact(left_o));
@@ -684,7 +684,7 @@ dummy_func(
684684
ERROR_IF(PyStackRef_IsNull(res), error);
685685
}
686686

687-
op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
687+
pure op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
688688
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
689689
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
690690
assert(PyFloat_CheckExact(left_o));
@@ -699,7 +699,7 @@ dummy_func(
699699
ERROR_IF(PyStackRef_IsNull(res), error);
700700
}
701701

702-
op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
702+
pure op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
703703
PyObject *left_o = PyStackRef_AsPyObjectBorrow(left);
704704
PyObject *right_o = PyStackRef_AsPyObjectBorrow(right);
705705
assert(PyFloat_CheckExact(left_o));

Python/optimizer_analysis.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "pycore_uop_ids.h"
3131
#include "pycore_range.h"
3232
#include "pycore_unicodeobject.h"
33+
#include "pycore_ceval.h"
3334

3435
#include <stdarg.h>
3536
#include <stdbool.h>
@@ -323,6 +324,7 @@ remove_globals(_PyInterpreterFrame *frame, _PyUOpInstruction *buffer,
323324
#define sym_is_const _Py_uop_sym_is_const
324325
#define sym_get_const _Py_uop_sym_get_const
325326
#define sym_new_const_steal _Py_uop_sym_new_const_steal
327+
#define sym_get_const_as_stackref _Py_uop_sym_get_const_as_stackref
326328
#define sym_new_unknown _Py_uop_sym_new_unknown
327329
#define sym_new_not_null _Py_uop_sym_new_not_null
328330
#define sym_new_type _Py_uop_sym_new_type

Python/optimizer_bytecodes.c

Lines changed: 15 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -234,63 +234,27 @@ dummy_func(void) {
234234
}
235235

236236
op(_BINARY_OP_ADD_FLOAT, (left, right -- res)) {
237-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
238-
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
239-
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
240-
PyObject *temp = PyFloat_FromDouble(
241-
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) +
242-
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
243-
if (temp == NULL) {
244-
goto error;
245-
}
246-
res = sym_new_const(ctx, temp);
247-
Py_DECREF(temp);
248-
// TODO gh-115506:
249-
// replace opcode with constant propagated one and update tests!
250-
}
251-
else {
252-
res = sym_new_type(ctx, &PyFloat_Type);
253-
}
237+
// We need to tell the cases generator that it's being used by the constant generator.
238+
// We should fix this in the cases generator.
239+
(void)(left);
240+
(void)(right);
241+
res = sym_new_type(ctx, &PyFloat_Type);
254242
}
255243

256244
op(_BINARY_OP_SUBTRACT_FLOAT, (left, right -- res)) {
257-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
258-
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
259-
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
260-
PyObject *temp = PyFloat_FromDouble(
261-
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) -
262-
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
263-
if (temp == NULL) {
264-
goto error;
265-
}
266-
res = sym_new_const(ctx, temp);
267-
Py_DECREF(temp);
268-
// TODO gh-115506:
269-
// replace opcode with constant propagated one and update tests!
270-
}
271-
else {
272-
res = sym_new_type(ctx, &PyFloat_Type);
273-
}
245+
// We need to tell the cases generator that it's being used by the constant generator.
246+
// We should fix this in the cases generator.
247+
(void)(left);
248+
(void)(right);
249+
res = sym_new_type(ctx, &PyFloat_Type);
274250
}
275251

276252
op(_BINARY_OP_MULTIPLY_FLOAT, (left, right -- res)) {
277-
if (sym_is_const(ctx, left) && sym_is_const(ctx, right)) {
278-
assert(PyFloat_CheckExact(sym_get_const(ctx, left)));
279-
assert(PyFloat_CheckExact(sym_get_const(ctx, right)));
280-
PyObject *temp = PyFloat_FromDouble(
281-
PyFloat_AS_DOUBLE(sym_get_const(ctx, left)) *
282-
PyFloat_AS_DOUBLE(sym_get_const(ctx, right)));
283-
if (temp == NULL) {
284-
goto error;
285-
}
286-
res = sym_new_const(ctx, temp);
287-
Py_DECREF(temp);
288-
// TODO gh-115506:
289-
// replace opcode with constant propagated one and update tests!
290-
}
291-
else {
292-
res = sym_new_type(ctx, &PyFloat_Type);
293-
}
253+
// We need to tell the cases generator that it's being used by the constant generator.
254+
// We should fix this in the cases generator.
255+
(void)(left);
256+
(void)(right);
257+
res = sym_new_type(ctx, &PyFloat_Type);
294258
}
295259

296260
op(_BINARY_OP_ADD_UNICODE, (left, right -- res)) {

0 commit comments

Comments
 (0)