Skip to content

Commit 33b8920

Browse files
committed
Add predicate symbol tests
1 parent d0a3ef7 commit 33b8920

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Python/optimizer_symbols.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,70 @@ _Py_uop_symbols_test(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(ignored))
12241224
TEST_PREDICATE(_Py_uop_sym_is_const(ctx, value) == true, "value is not constant");
12251225
TEST_PREDICATE(_Py_uop_sym_get_const(ctx, value) == Py_True, "value is not True");
12261226

1227+
// Resolving predicate result to True should narrow subject to True
1228+
JitOptRef subject = _Py_uop_sym_new_unknown(ctx);
1229+
JitOptRef const_true = _Py_uop_sym_new_const(ctx, Py_True);
1230+
if (PyJitRef_IsNull(subject) || PyJitRef_IsNull(const_true)) {
1231+
goto fail;
1232+
}
1233+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, false);
1234+
if (PyJitRef_IsNull(ref)) {
1235+
goto fail;
1236+
}
1237+
_Py_uop_sym_apply_predicate_narrowing(ctx, ref, true);
1238+
TEST_PREDICATE(_Py_uop_sym_is_const(ctx, subject), "predicate narrowing did not const-narrow subject");
1239+
TEST_PREDICATE(_Py_uop_sym_get_const(ctx, subject) == Py_True, "predicate narrowing did not narrow subject to True");
1240+
1241+
// Resolving predicate result to False should not narrow subject
1242+
subject = _Py_uop_sym_new_unknown(ctx);
1243+
if (PyJitRef_IsNull(subject)) {
1244+
goto fail;
1245+
}
1246+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, false);
1247+
if (PyJitRef_IsNull(ref)) {
1248+
goto fail;
1249+
}
1250+
_Py_uop_sym_apply_predicate_narrowing(ctx, ref, false);
1251+
TEST_PREDICATE(!_Py_uop_sym_is_const(ctx, subject), "predicate narrowing incorrectly narrowed subject");
1252+
1253+
// Resolving inverted predicate to False should narrow subject to True
1254+
subject = _Py_uop_sym_new_unknown(ctx);
1255+
if (PyJitRef_IsNull(subject)) {
1256+
goto fail;
1257+
}
1258+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, true);
1259+
if (PyJitRef_IsNull(ref)) {
1260+
goto fail;
1261+
}
1262+
_Py_uop_sym_apply_predicate_narrowing(ctx, ref, false);
1263+
TEST_PREDICATE(_Py_uop_sym_is_const(ctx, subject), "predicate narrowing (inverted) did not const-narrow subject");
1264+
TEST_PREDICATE(_Py_uop_sym_get_const(ctx, subject) == Py_True, "predicate narrowing (inverted) did not narrow subject to True");
1265+
1266+
// Resolving inverted predicate to True should not narrow subject
1267+
subject = _Py_uop_sym_new_unknown(ctx);
1268+
if (PyJitRef_IsNull(subject)) {
1269+
goto fail;
1270+
}
1271+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_true, JIT_PRED_IS, true);
1272+
if (PyJitRef_IsNull(ref)) {
1273+
goto fail;
1274+
}
1275+
_Py_uop_sym_apply_predicate_narrowing(ctx, ref, true);
1276+
TEST_PREDICATE(!_Py_uop_sym_is_const(ctx, subject), "predicate narrowing incorrectly narrowed subject (inverted/true)");
1277+
1278+
// Test narrowing subject to None
1279+
subject = _Py_uop_sym_new_unknown(ctx);
1280+
JitOptRef const_none = _Py_uop_sym_new_const(ctx, Py_None);
1281+
if (PyJitRef_IsNull(subject) || PyJitRef_IsNull(const_none)) {
1282+
goto fail;
1283+
}
1284+
ref = _Py_uop_sym_new_predicate(ctx, subject, const_none, JIT_PRED_IS, false);
1285+
if (PyJitRef_IsNull(ref)) {
1286+
goto fail;
1287+
}
1288+
_Py_uop_sym_apply_predicate_narrowing(ctx, ref, true);
1289+
TEST_PREDICATE(_Py_uop_sym_is_const(ctx, subject), "predicate narrowing did not const-narrow subject (None)");
1290+
TEST_PREDICATE(_Py_uop_sym_get_const(ctx, subject) == Py_None, "predicate narrowing did not narrow subject to None");
12271291

12281292
val_big = PyNumber_Lshift(_PyLong_GetOne(), PyLong_FromLong(66));
12291293
if (val_big == NULL) {

0 commit comments

Comments
 (0)