Skip to content

Commit 3b23771

Browse files
committed
simplify: port IsTruthy Bool + LongExact paths to C
Bool path: compare input with Py_True via PrimitiveCompare(kEqual). LongExact path: compare with _PyLong_GetZero() via PrimitiveCompare(kNotEqual). Falls through to C++ for known-object, collection-length, and other paths.
1 parent 1c15f4d commit 3b23771

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Python/jit/hir/simplify.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2782,8 +2782,13 @@ Register* simplifyInstr(Env& env, const Instr* instr) {
27822782
return r;
27832783
}
27842784

2785-
case Opcode::kIsTruthy:
2785+
case Opcode::kIsTruthy: {
2786+
SimplifyEnv cenv = make_c_env();
2787+
auto *r = static_cast<Register*>(simplify_is_truthy_c(&cenv, instr));
2788+
sync_c_env(cenv);
2789+
if (r) return r;
27862790
return simplifyIsTruthy(env, instr);
2791+
}
27872792

27882793
// TODO(T255262756) - Enable this again. See P2169675076 and P2184559031 (same
27892794
// pattern but applied to simplifyLoadAttrTypeReceiver).

Python/jit/hir/simplify_c.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "cinderx/Jit/hir/hir_instr_c.h"
88
#include "cinderx/Jit/hir/hir_type_c.h"
99
#include "Python.h"
10+
#include "pycore_long.h"
1011

1112
/* Forward declarations (avoid hir_c_api.h typedef conflicts) */
1213
extern HirType hir_register_type(void *reg);
@@ -212,6 +213,30 @@ void *simplify_primitive_compare_box_true_c(const void *instr) {
212213
return hir_c_get_operand(left_def, 0);
213214
}
214215

216+
/* ---- simplifyIsTruthy (partial — Bool + LongExact paths) ---- */
217+
void *simplify_is_truthy_c(SimplifyEnv *env, const void *instr) {
218+
void *input = hir_c_get_operand(instr, 0);
219+
HirType input_type = hir_register_type(input);
220+
221+
/* TBool: compare with Py_True */
222+
HirType t_bool = HIR_TYPE_BOOL;
223+
if (hir_type_is_subtype(input_type, t_bool)) {
224+
simplify_env_emit_use_type(env, input, t_bool);
225+
void *right = simplify_env_emit_load_const(env, hir_type_from_object(Py_True));
226+
return simplify_env_emit_primitive_compare(env, HIR_PCMP_Equal, input, right);
227+
}
228+
229+
/* TLongExact: compare with _PyLong_GetZero() */
230+
HirType t_long_exact = HIR_TYPE_LONGEXACT;
231+
if (hir_type_is_subtype(input_type, t_long_exact)) {
232+
simplify_env_emit_use_type(env, input, input_type);
233+
void *right = simplify_env_emit_load_const(env, hir_type_from_object(_PyLong_GetZero()));
234+
return simplify_env_emit_primitive_compare(env, HIR_PCMP_NotEqual, input, right);
235+
}
236+
237+
return NULL;
238+
}
239+
215240
/* ---- simplifyCheckSequenceBounds ----
216241
* If sequence is MakeTuple with known length + idx is known int, fold bounds check. */
217242
void *simplify_check_sequence_bounds_c(SimplifyEnv *env, const void *instr) {

Python/jit/hir/simplify_c.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void *simplify_guard_type_identity_c(const void *instr);
3232
void *simplify_primitive_compare_box_true_c(const void *instr);
3333
void *simplify_unbox_box_c(const void *instr);
3434
void *simplify_cond_branch_check_type_c(SimplifyEnv *env, const void *instr);
35+
void *simplify_is_truthy_c(SimplifyEnv *env, const void *instr);
3536
void *simplify_check_sequence_bounds_c(SimplifyEnv *env, const void *instr);
3637
void *simplify_load_array_item_tuple_c(SimplifyEnv *env, const void *instr);
3738
void *simplify_load_tuple_item_c(SimplifyEnv *env, const void *instr);

0 commit comments

Comments
 (0)