Skip to content

Commit e7fdb04

Browse files
committed
simplify
1 parent 3ba0c85 commit e7fdb04

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

Python/optimizer_bytecodes.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -279,33 +279,34 @@ dummy_func(void) {
279279
bool rhs_int = sym_matches_type(rhs, &PyLong_Type);
280280
bool lhs_float = sym_matches_type(lhs, &PyFloat_Type);
281281
bool rhs_float = sym_matches_type(rhs, &PyFloat_Type);
282-
// Speculatively promote probable floats to known floats. The
283-
// _RECORD_TOS / _RECORD_NOS uops preceding _BINARY_OP record the
284-
// observed operands during tracing; if the probable type is float
285-
// but no static type is known, emit a guard and narrow the symbol.
286-
// For NB_TRUE_DIVIDE this enables the specialized float path
287-
// below; for NB_REMAINDER it lets the result type propagate as
288-
// float so downstream ops can specialize. NB_POWER is intentionally
289-
// excluded: speculative guards there broke
290-
// test_power_type_depends_on_input_values (see GH-127844), likely
291-
// due to subtle interactions with the case A-G dispatch.
292-
if (oparg == NB_TRUE_DIVIDE || oparg == NB_INPLACE_TRUE_DIVIDE
293-
|| oparg == NB_REMAINDER || oparg == NB_INPLACE_REMAINDER) {
294-
if (!rhs_float && !sym_has_type(rhs)
282+
bool is_truediv = (oparg == NB_TRUE_DIVIDE
283+
|| oparg == NB_INPLACE_TRUE_DIVIDE);
284+
bool is_remainder = (oparg == NB_REMAINDER
285+
|| oparg == NB_INPLACE_REMAINDER);
286+
// Promote probable-float operands to known floats via speculative
287+
// guards. _RECORD_TOS / _RECORD_NOS in the BINARY_OP macro record
288+
// the observed operand during tracing, which sym_get_probable_type
289+
// reads here. Applied only to ops where narrowing unlocks a
290+
// meaningful downstream win:
291+
// - NB_TRUE_DIVIDE: enables the specialized float path below.
292+
// - NB_REMAINDER: lets the float result type propagate.
293+
// NB_POWER is excluded — speculative guards there regressed
294+
// test_power_type_depends_on_input_values (GH-127844).
295+
if (is_truediv || is_remainder) {
296+
if (!sym_has_type(rhs)
295297
&& sym_get_probable_type(rhs) == &PyFloat_Type) {
296298
ADD_OP(_GUARD_TOS_FLOAT, 0, 0);
297299
sym_set_type(rhs, &PyFloat_Type);
298300
rhs_float = true;
299301
}
300-
if (!lhs_float && !sym_has_type(lhs)
302+
if (!sym_has_type(lhs)
301303
&& sym_get_probable_type(lhs) == &PyFloat_Type) {
302304
ADD_OP(_GUARD_NOS_FLOAT, 0, 0);
303305
sym_set_type(lhs, &PyFloat_Type);
304306
lhs_float = true;
305307
}
306308
}
307-
if ((oparg == NB_TRUE_DIVIDE || oparg == NB_INPLACE_TRUE_DIVIDE)
308-
&& lhs_float && rhs_float) {
309+
if (is_truediv && lhs_float && rhs_float) {
309310
if (PyJitRef_IsUnique(lhs)) {
310311
ADD_OP(_BINARY_OP_TRUEDIV_FLOAT_INPLACE, 0, 0);
311312
l = sym_new_null(ctx);
@@ -323,7 +324,7 @@ dummy_func(void) {
323324
}
324325
res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type));
325326
}
326-
else if ((oparg == NB_TRUE_DIVIDE || oparg == NB_INPLACE_TRUE_DIVIDE)
327+
else if (is_truediv
327328
&& (lhs_int || lhs_float) && (rhs_int || rhs_float)) {
328329
res = PyJitRef_MakeUnique(sym_new_type(ctx, &PyFloat_Type));
329330
}

Python/optimizer_cases.c.h

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

0 commit comments

Comments
 (0)