Skip to content

Commit 0622572

Browse files
committed
simplify: port CIntToCBool to C via SimplifyEnv (chunk 5)
Second Category 2 handler. simplify_cint_to_cbool_c folds known int constants to CBool. Adds make_cbool_type helper for constructing HirType with CBool bits + int specialization.
1 parent dcb0432 commit 0622572

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

Python/jit/hir/simplify.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2826,8 +2826,12 @@ Register* simplifyInstr(Env& env, const Instr* instr) {
28262826
case Opcode::kStoreSubscr:
28272827
return simplifyStoreSubscr(env, instr);
28282828

2829-
case Opcode::kCIntToCBool:
2830-
return simplifyCIntToCBool(env, instr);
2829+
case Opcode::kCIntToCBool: {
2830+
SimplifyEnv cenv = make_c_env();
2831+
auto *r = static_cast<Register*>(simplify_cint_to_cbool_c(&cenv, instr));
2832+
sync_c_env(cenv);
2833+
return r;
2834+
}
28312835

28322836
case Opcode::kGetIter: {
28332837
// C->C inlining: narrow iterator type for known input types

Python/jit/hir/simplify_c.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ void *simplify_guard_type_identity_c(const void *instr) {
7070
return NULL;
7171
}
7272

73+
/* Helper: create HirType for CBool(val) with int specialization */
74+
static HirType make_cbool_type(intptr_t val) {
75+
HirType t = HIR_TYPE_CBOOL;
76+
t.bits_and_flags |= ((uint64_t)HIR_SPEC_INT << HIR_TYPE_SPEC_SHIFT);
77+
t.int_val = val != 0 ? 1 : 0;
78+
return t;
79+
}
80+
81+
/* ---- simplifyCIntToCBool ----
82+
* If input is a known int constant, fold to CBool(val != 0). */
83+
void *simplify_cint_to_cbool_c(SimplifyEnv *env, const void *instr) {
84+
void *input = hir_c_get_operand(instr, 0);
85+
HirType input_type = hir_register_type(input);
86+
if (hir_type_has_int_spec(&input_type)) {
87+
return simplify_env_emit_load_const(env, make_cbool_type(hir_type_int_spec(&input_type)));
88+
}
89+
return NULL;
90+
}
91+
7392
/* ---- simplifyPrimitiveBoxBool ----
7493
* If input is a known int constant, replace with Py_True/Py_False. */
7594
void *simplify_primitive_box_bool_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

3333
/* Env-using handlers (Category 2) */
3434
void *simplify_primitive_box_bool_c(SimplifyEnv *env, const void *instr);
35+
void *simplify_cint_to_cbool_c(SimplifyEnv *env, const void *instr);
3536

3637
#ifdef __cplusplus
3738
}

0 commit comments

Comments
 (0)