Skip to content

Commit c6f4a6a

Browse files
committed
Refactor invert in predicate symbol to kind
1 parent 142eff4 commit c6f4a6a

File tree

5 files changed

+35
-30
lines changed

5 files changed

+35
-30
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ extern JitOptRef _Py_uop_sym_new_truthiness(JitOptContext *ctx, JitOptRef value,
205205
extern bool _Py_uop_sym_is_compact_int(JitOptRef sym);
206206
extern JitOptRef _Py_uop_sym_new_compact_int(JitOptContext *ctx);
207207
extern void _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef sym);
208-
extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind, bool invert);
208+
extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind);
209209
extern void _Py_uop_sym_apply_predicate_narrowing(JitOptContext *ctx, JitOptRef sym, bool branch_is_true);
210210

211211
extern void _Py_uop_abstractcontext_init(JitOptContext *ctx);

Include/internal/pycore_optimizer_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ typedef struct {
7575

7676
typedef enum {
7777
JIT_PRED_IS,
78+
JIT_PRED_IS_NOT,
7879
} JitOptPredicateKind;
7980

8081
typedef struct {
8182
uint8_t tag;
8283
uint8_t kind;
83-
bool invert;
8484
uint16_t lhs;
8585
uint16_t rhs;
8686
} JitOptPredicate;

Python/optimizer_bytecodes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ dummy_func(void) {
535535
}
536536

537537
op(_IS_OP, (left, right -- b, l, r)) {
538-
b = sym_new_predicate(ctx, left, right, JIT_PRED_IS, oparg != 0);
538+
b = sym_new_predicate(ctx, left, right, (oparg ? JIT_PRED_IS_NOT : JIT_PRED_IS));
539539
l = left;
540540
r = right;
541541
}

Python/optimizer_cases.c.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/optimizer_symbols.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef ref)
836836
}
837837

838838
JitOptRef
839-
_Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind, bool invert)
839+
_Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind)
840840
{
841841
JitOptSymbol *lhs = PyJitRef_Unwrap(lhs_ref);
842842
JitOptSymbol *rhs = PyJitRef_Unwrap(rhs_ref);
@@ -847,7 +847,6 @@ _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_r
847847
}
848848

849849
res->tag = JIT_SYM_PREDICATE_TAG;
850-
res->predicate.invert = invert;
851850
res->predicate.kind = kind;
852851
res->predicate.lhs = (uint16_t)(lhs - allocation_base(ctx));
853852
res->predicate.rhs = (uint16_t)(rhs - allocation_base(ctx));
@@ -864,34 +863,40 @@ _Py_uop_sym_apply_predicate_narrowing(JitOptContext *ctx, JitOptRef ref, bool br
864863
}
865864

866865
JitOptPredicate pred = sym->predicate;
867-
bool narrow = (branch_is_true && !pred.invert) || (!branch_is_true && pred.invert);
868-
if (!narrow) {
869-
return;
870-
}
871866

872867
JitOptRef lhs_ref = PyJitRef_Wrap(allocation_base(ctx) + pred.lhs);
873868
JitOptRef rhs_ref = PyJitRef_Wrap(allocation_base(ctx) + pred.rhs);
874869

875870
bool lhs_is_const = _Py_uop_sym_is_const(ctx, lhs_ref);
876871
bool rhs_is_const = _Py_uop_sym_is_const(ctx, rhs_ref);
872+
if (!lhs_is_const && !rhs_is_const) {
873+
return;;
874+
}
877875

876+
bool narrow = false;
878877
switch(pred.kind) {
879-
case JIT_PRED_IS: {
880-
if (!lhs_is_const && !rhs_is_const) {
881-
break;
882-
}
883-
JitOptRef subject_ref = lhs_is_const ? rhs_ref : lhs_ref;
884-
JitOptRef const_ref = lhs_is_const ? lhs_ref : rhs_ref;
885-
886-
PyObject *const_val = _Py_uop_sym_get_const(ctx, const_ref);
887-
if (const_val == NULL) {
888-
break;
889-
}
890-
_Py_uop_sym_set_const(ctx, subject_ref, const_val);
891-
assert(_Py_uop_sym_is_const(ctx, subject_ref));
878+
case JIT_PRED_IS:
879+
narrow = branch_is_true;
892880
break;
893-
}
881+
case JIT_PRED_IS_NOT:
882+
narrow = !branch_is_true;
883+
break;
884+
default:
885+
break;
886+
}
887+
if (!narrow) {
888+
return;
889+
}
890+
891+
JitOptRef subject_ref = lhs_is_const ? rhs_ref : lhs_ref;
892+
JitOptRef const_ref = lhs_is_const ? lhs_ref : rhs_ref;
893+
894+
PyObject *const_val = _Py_uop_sym_get_const(ctx, const_ref);
895+
if (const_val == NULL) {
896+
return;
894897
}
898+
_Py_uop_sym_set_const(ctx, subject_ref, const_val);
899+
assert(_Py_uop_sym_is_const(ctx, subject_ref));
895900
}
896901

897902
JitOptRef
@@ -1236,7 +1241,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
12361241
if (PyJitRef_IsNull(subject) || PyJitRef_IsNull(const_true)) {
12371242
goto fail;
12381243
}
1239-
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, false);
1244+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS);
12401245
if (PyJitRef_IsNull(ref)) {
12411246
goto fail;
12421247
}
@@ -1249,7 +1254,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
12491254
if (PyJitRef_IsNull(subject)) {
12501255
goto fail;
12511256
}
1252-
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, false);
1257+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS);
12531258
if (PyJitRef_IsNull(ref)) {
12541259
goto fail;
12551260
}
@@ -1261,7 +1266,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
12611266
if (PyJitRef_IsNull(subject)) {
12621267
goto fail;
12631268
}
1264-
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, true);
1269+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS_NOT);
12651270
if (PyJitRef_IsNull(ref)) {
12661271
goto fail;
12671272
}
@@ -1274,7 +1279,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
12741279
if (PyJitRef_IsNull(subject)) {
12751280
goto fail;
12761281
}
1277-
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, true);
1282+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS_NOT);
12781283
if (PyJitRef_IsNull(ref)) {
12791284
goto fail;
12801285
}
@@ -1287,7 +1292,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
12871292
if (PyJitRef_IsNull(subject) || PyJitRef_IsNull(const_none)) {
12881293
goto fail;
12891294
}
1290-
ref = _Py_uop_sym_new_predicate(ctx, subject, const_none, JIT_PRED_IS, false);
1295+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_none, JIT_PRED_IS);
12911296
if (PyJitRef_IsNull(ref)) {
12921297
goto fail;
12931298
}
@@ -1302,7 +1307,7 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
13021307
if (PyJitRef_IsNull(subject) || PyJitRef_IsNull(const_one)) {
13031308
goto fail;
13041309
}
1305-
ref = _Py_uop_sym_new_predicate(ctx, subject, const_one, JIT_PRED_IS, false);
1310+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_one, JIT_PRED_IS);
13061311
if (PyJitRef_IsNull(ref)) {
13071312
goto fail;
13081313
}

0 commit comments

Comments
 (0)