Skip to content

Commit 4d49566

Browse files
committed
Separate compact int and int64 optimizer facts
1 parent f35d39b commit 4d49566

5 files changed

Lines changed: 35 additions & 5 deletions

File tree

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ extern JitOptRef _Py_uop_sym_tuple_getitem(JitOptContext *ctx, JitOptRef sym, Py
462462
extern Py_ssize_t _Py_uop_sym_tuple_length(JitOptRef sym);
463463
extern JitOptRef _Py_uop_sym_new_truthiness(JitOptContext *ctx, JitOptRef value, bool truthy);
464464
extern bool _Py_uop_sym_is_compact_int(JitOptRef sym);
465+
extern bool _Py_uop_sym_fits_int64(JitOptRef sym);
465466
extern JitOptRef _Py_uop_sym_new_compact_int(JitOptContext *ctx);
466467
extern void _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef sym);
467468
extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind);

Python/optimizer_analysis.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ add_op(JitOptContext *ctx, _PyUOpInstruction *this_instr,
299299
#define sym_tuple_length _Py_uop_sym_tuple_length
300300
#define sym_is_immortal _Py_uop_symbol_is_immortal
301301
#define sym_is_compact_int _Py_uop_sym_is_compact_int
302+
#define sym_fits_int64 _Py_uop_sym_fits_int64
302303
#define sym_new_compact_int _Py_uop_sym_new_compact_int
303304
#define sym_new_truthiness _Py_uop_sym_new_truthiness
304305
#define sym_new_predicate _Py_uop_sym_new_predicate

Python/optimizer_bytecodes.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
4343
#define sym_is_immortal _Py_uop_symbol_is_immortal
4444
#define sym_new_compact_int _Py_uop_sym_new_compact_int
4545
#define sym_is_compact_int _Py_uop_sym_is_compact_int
46+
#define sym_fits_int64 _Py_uop_sym_fits_int64
4647
#define sym_new_truthiness _Py_uop_sym_new_truthiness
4748
#define sym_new_predicate _Py_uop_sym_new_predicate
4849
#define sym_apply_predicate_narrowing _Py_uop_sym_apply_predicate_narrowing
@@ -236,7 +237,7 @@ dummy_func(void) {
236237
* type is already known to be PyLong_Type the cheaper _OVERFLOWED guard
237238
* (which skips the type check) can be used instead. */
238239
op(_GUARD_TOS_INT_WIDE, (value -- value)) {
239-
if (sym_is_compact_int(value)) {
240+
if (sym_fits_int64(value)) {
240241
ADD_OP(_NOP, 0, 0);
241242
}
242243
else {
@@ -248,7 +249,7 @@ dummy_func(void) {
248249
}
249250

250251
op(_GUARD_NOS_INT_WIDE, (left, unused -- left, unused)) {
251-
if (sym_is_compact_int(left)) {
252+
if (sym_fits_int64(left)) {
252253
ADD_OP(_NOP, 0, 0);
253254
}
254255
else {

Python/optimizer_cases.c.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.

Python/optimizer_symbols.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,16 @@ _Py_uop_symbol_is_immortal(JitOptSymbol *sym)
967967

968968
bool
969969
_Py_uop_sym_is_compact_int(JitOptRef ref)
970+
{
971+
JitOptSymbol *sym = PyJitRef_Unwrap(ref);
972+
if (sym->tag == JIT_SYM_KNOWN_VALUE_TAG) {
973+
return (bool)_PyLong_CheckExactAndCompact(sym->value.value);
974+
}
975+
return sym->tag == JIT_SYM_COMPACT_INT;
976+
}
977+
978+
bool
979+
_Py_uop_sym_fits_int64(JitOptRef ref)
970980
{
971981
JitOptSymbol *sym = PyJitRef_Unwrap(ref);
972982
if (sym->tag == JIT_SYM_KNOWN_VALUE_TAG) {
@@ -1008,7 +1018,7 @@ _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef ref)
10081018
}
10091019
return;
10101020
case JIT_SYM_KNOWN_VALUE_TAG:
1011-
if (!_PyLong_CheckExactAndFitsInt64(sym->value.value)) {
1021+
if (!_PyLong_CheckExactAndCompact(sym->value.value)) {
10121022
Py_CLEAR(sym->value.value);
10131023
sym_set_bottom(ctx, sym);
10141024
}
@@ -1567,6 +1577,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
15671577
_Py_uop_abstractcontext_init(ctx, NULL);
15681578
PyObject *val_42 = NULL;
15691579
PyObject *val_43 = NULL;
1580+
PyObject *val_noncompact_int64 = NULL;
15701581
PyObject *val_big = NULL;
15711582
PyObject *tuple = NULL;
15721583
PyFunctionObject *func = NULL;
@@ -1619,6 +1630,11 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
16191630
assert(val_43 != NULL);
16201631
assert(_Py_IsImmortal(val_43));
16211632

1633+
val_noncompact_int64 = PyNumber_Lshift(_PyLong_GetOne(), PyLong_FromLong(40));
1634+
if (val_noncompact_int64 == NULL) {
1635+
goto fail;
1636+
}
1637+
16221638
ref = _Py_uop_sym_new_type(ctx, &PyLong_Type);
16231639
if (PyJitRef_IsNull(ref)) {
16241640
goto fail;
@@ -1960,10 +1976,19 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
19601976
}
19611977

19621978
JitOptRef ref_42 = _Py_uop_sym_new_const(ctx, val_42);
1979+
JitOptRef ref_noncompact_int64 = _Py_uop_sym_new_const(ctx, val_noncompact_int64);
19631980
JitOptRef ref_big = _Py_uop_sym_new_const(ctx, val_big);
19641981
JitOptRef ref_int = _Py_uop_sym_new_compact_int(ctx);
19651982
TEST_PREDICATE(_Py_uop_sym_is_compact_int(ref_42), "42 is not a compact int");
1983+
TEST_PREDICATE(_Py_uop_sym_fits_int64(ref_42), "42 does not fit int64");
1984+
TEST_PREDICATE(
1985+
!_Py_uop_sym_is_compact_int(ref_noncompact_int64),
1986+
"(1 << 40) is a compact int");
1987+
TEST_PREDICATE(
1988+
_Py_uop_sym_fits_int64(ref_noncompact_int64),
1989+
"(1 << 40) does not fit int64");
19661990
TEST_PREDICATE(!_Py_uop_sym_is_compact_int(ref_big), "(1 << 200) is a compact int");
1991+
TEST_PREDICATE(!_Py_uop_sym_fits_int64(ref_big), "(1 << 200) fits int64");
19671992
TEST_PREDICATE(_Py_uop_sym_is_compact_int(ref_int), "compact int is not a compact int");
19681993
TEST_PREDICATE(_Py_uop_sym_matches_type(ref_int, &PyLong_Type), "compact int is not an int");
19691994

@@ -2092,6 +2117,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
20922117
_Py_uop_abstractcontext_fini(ctx);
20932118
Py_DECREF(val_42);
20942119
Py_DECREF(val_43);
2120+
Py_DECREF(val_noncompact_int64);
20952121
Py_DECREF(val_big);
20962122
Py_DECREF(tuple);
20972123
Py_DECREF(func);
@@ -2101,6 +2127,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
21012127
_Py_uop_abstractcontext_fini(ctx);
21022128
Py_XDECREF(val_42);
21032129
Py_XDECREF(val_43);
2130+
Py_XDECREF(val_noncompact_int64);
21042131
Py_XDECREF(val_big);
21052132
Py_XDECREF(tuple);
21062133
Py_XDECREF(func);

0 commit comments

Comments
 (0)