Skip to content

Commit d828f8c

Browse files
committed
Refactor predicate optimizer symbol
- Delay analysis until predicate resolves - Generalize predicate to lhs, rhs - Expand to accept all safe constants (not just singletons)
1 parent 8834e12 commit d828f8c

File tree

6 files changed

+21
-47
lines changed

6 files changed

+21
-47
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ extern JitOptRef _Py_uop_sym_new_truthiness(JitOptContext *ctx, JitOptRef value,
191191
extern bool _Py_uop_sym_is_compact_int(JitOptRef sym);
192192
extern JitOptRef _Py_uop_sym_new_compact_int(JitOptContext *ctx);
193193
extern void _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef sym);
194-
extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef subject_ref, JitOptRef constant_ref, JitOptPredicateKind kind, bool invert);
195-
extern bool _Py_uop_sym_is_known_singleton(JitOptContext *ctx, JitOptRef sym);
194+
extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind, bool invert);
196195
extern void _Py_uop_sym_apply_predicate_narrowing(JitOptContext *ctx, JitOptRef sym, bool branch_is_true);
197196

198197
extern void _Py_uop_abstractcontext_init(JitOptContext *ctx);

Include/internal/pycore_optimizer_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ typedef struct {
8080
uint8_t tag;
8181
uint8_t kind;
8282
bool invert;
83-
uint16_t subject;
84-
uint16_t constant;
83+
uint16_t lhs;
84+
uint16_t rhs;
8585
} JitOptPredicate;
8686

8787
typedef struct {

Python/optimizer_analysis.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,6 @@ incorrect_keys(PyObject *obj, uint32_t version)
187187
#define sym_new_compact_int _Py_uop_sym_new_compact_int
188188
#define sym_new_truthiness _Py_uop_sym_new_truthiness
189189
#define sym_new_predicate _Py_uop_sym_new_predicate
190-
#define sym_is_known_singleton _Py_uop_sym_is_known_singleton
191190
#define sym_apply_predicate_narrowing _Py_uop_sym_apply_predicate_narrowing
192191

193192
#define JUMP_TO_LABEL(label) goto label;

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ typedef struct _Py_UOpsAbstractFrame _Py_UOpsAbstractFrame;
3939
#define sym_is_compact_int _Py_uop_sym_is_compact_int
4040
#define sym_new_truthiness _Py_uop_sym_new_truthiness
4141
#define sym_new_predicate _Py_uop_sym_new_predicate
42-
#define sym_is_known_singleton _Py_uop_sym_is_known_singleton
4342
#define sym_apply_predicate_narrowing _Py_uop_sym_apply_predicate_narrowing
4443

4544
extern int
@@ -536,16 +535,7 @@ dummy_func(void) {
536535
}
537536

538537
op(_IS_OP, (left, right -- b, l, r)) {
539-
bool invert = (oparg != 0);
540-
if (sym_is_known_singleton(ctx, left)) {
541-
b = sym_new_predicate(ctx, right, left, JIT_PRED_IS ,invert);
542-
}
543-
else if (sym_is_known_singleton(ctx, right)) {
544-
b = sym_new_predicate(ctx, left, right, JIT_PRED_IS, invert);
545-
}
546-
else {
547-
b = sym_new_type(ctx, &PyBool_Type);
548-
}
538+
b = sym_new_predicate(ctx, left, right, JIT_PRED_IS, oparg != 0);
549539
l = left;
550540
r = right;
551541
}

Python/optimizer_cases.c.h

Lines changed: 1 addition & 10 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: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -788,12 +788,10 @@ _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef ref)
788788
}
789789

790790
JitOptRef
791-
_Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef subject_ref, JitOptRef constant_ref, JitOptPredicateKind kind, bool invert)
791+
_Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind, bool invert)
792792
{
793-
assert(_Py_uop_sym_is_const(ctx, constant_ref));
794-
795-
JitOptSymbol *subject = PyJitRef_Unwrap(subject_ref);
796-
JitOptSymbol *constant = PyJitRef_Unwrap(constant_ref);
793+
JitOptSymbol *lhs = PyJitRef_Unwrap(lhs_ref);
794+
JitOptSymbol *rhs = PyJitRef_Unwrap(rhs_ref);
797795

798796
JitOptSymbol *res = sym_new(ctx);
799797
if (res == NULL) {
@@ -803,22 +801,12 @@ _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef subject_ref, JitOptRef c
803801
res->tag = JIT_SYM_PREDICATE_TAG;
804802
res->predicate.invert = invert;
805803
res->predicate.kind = kind;
806-
res->predicate.subject = (uint16_t)(subject - allocation_base(ctx));
807-
res->predicate.constant = (uint16_t)(constant - allocation_base(ctx));
804+
res->predicate.lhs = (uint16_t)(lhs - allocation_base(ctx));
805+
res->predicate.rhs = (uint16_t)(rhs - allocation_base(ctx));
808806

809807
return PyJitRef_Wrap(res);
810808
}
811809

812-
bool
813-
_Py_uop_sym_is_known_singleton(JitOptContext *ctx, JitOptRef ref)
814-
{
815-
if (_Py_uop_sym_is_safe_const(ctx, ref)) {
816-
PyObject *value = _Py_uop_sym_get_const(ctx, ref);
817-
return value == Py_None || value == Py_True || value == Py_False;
818-
}
819-
return false;
820-
}
821-
822810
void
823811
_Py_uop_sym_apply_predicate_narrowing(JitOptContext *ctx, JitOptRef ref, bool branch_is_true)
824812
{
@@ -833,10 +821,17 @@ _Py_uop_sym_apply_predicate_narrowing(JitOptContext *ctx, JitOptRef ref, bool br
833821
return;
834822
}
835823

836-
if (pred.kind == JIT_PRED_IS) {
837-
JitOptRef subject_ref = PyJitRef_Wrap(allocation_base(ctx) + pred.subject);
838-
JitOptRef constant_ref = PyJitRef_Wrap(allocation_base(ctx) + pred.constant);
839-
PyObject *const_val = _Py_uop_sym_get_const(ctx, constant_ref);
824+
JitOptRef lhs_ref = PyJitRef_Wrap(allocation_base(ctx) + pred.lhs);
825+
JitOptRef rhs_ref = PyJitRef_Wrap(allocation_base(ctx) + pred.rhs);
826+
827+
bool lhs_is_const = _Py_uop_sym_is_safe_const(ctx, lhs_ref);
828+
bool rhs_is_const = _Py_uop_sym_is_safe_const(ctx, rhs_ref);
829+
830+
if (pred.kind == JIT_PRED_IS && (lhs_is_const || rhs_is_const)) {
831+
JitOptRef subject_ref = lhs_is_const ? rhs_ref : lhs_ref;
832+
JitOptRef const_ref = lhs_is_const ? lhs_ref : rhs_ref;
833+
834+
PyObject *const_val = _Py_uop_sym_get_const(ctx, const_ref);
840835
if (const_val == NULL) {
841836
return;
842837
}

0 commit comments

Comments
 (0)