Skip to content

Commit f4def10

Browse files
committed
simplify: port IsNegativeAndErrOccurred + CInt type helper (chunk 10)
simplify_is_neg_and_err_c: if input is LoadConst, no exception possible, emit 0 constant. Adds make_cint_type/make_int_spec_type helpers for constructing int-specialized HirTypes in C.
1 parent d42a0a3 commit f4def10

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

Python/jit/hir/simplify.cpp

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

2828-
case Opcode::kIsNegativeAndErrOccurred:
2829-
return simplifyIsNegativeAndErrOccurred(env, instr);
2828+
case Opcode::kIsNegativeAndErrOccurred: {
2829+
SimplifyEnv cenv = make_c_env();
2830+
auto *r = static_cast<Register*>(simplify_is_neg_and_err_c(&cenv, instr));
2831+
sync_c_env(cenv);
2832+
return r;
2833+
}
28302834

28312835
case Opcode::kStoreAttr:
28322836
return simplifyStoreAttr(env, instr);

Python/jit/hir/simplify_c.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,21 @@ void *simplify_env_emit_primitive_box_bool(SimplifyEnv *env, void *src) {
107107
return simplify_env_emit(env, instr);
108108
}
109109

110-
/* Helper: create HirType for CBool(val) with int specialization */
110+
/* Helper: create HirType with int specialization */
111+
static HirType make_int_spec_type(HirType base, intptr_t val) {
112+
base.bits_and_flags |= ((uint64_t)HIR_SPEC_INT << HIR_TYPE_SPEC_SHIFT);
113+
base.int_val = val;
114+
return base;
115+
}
116+
111117
static HirType make_cbool_type(intptr_t val) {
112-
HirType t = HIR_TYPE_CBOOL;
113-
t.bits_and_flags |= ((uint64_t)HIR_SPEC_INT << HIR_TYPE_SPEC_SHIFT);
114-
t.int_val = val != 0 ? 1 : 0;
115-
return t;
118+
return make_int_spec_type(((HirType)HIR_TYPE_CBOOL), val != 0 ? 1 : 0);
119+
}
120+
121+
static HirType make_cint_type(HirType target_type, intptr_t val) {
122+
uint64_t bits = target_type.bits_and_flags & HIR_TYPE_BITS_MASK;
123+
HirType t = HIR_TYPE_SIMPLE(bits, HIR_TYPE_LIFETIME_BOTTOM);
124+
return make_int_spec_type(t, val);
116125
}
117126

118127
/* ---- simplifyCIntToCBool ----
@@ -194,6 +203,19 @@ void *simplify_int_convert_c(SimplifyEnv *env, const void *instr) {
194203
return NULL;
195204
}
196205

206+
/* ---- simplifyIsNegativeAndErrOccurred ----
207+
* If input is a LoadConst, we know no exception is active → result is 0. */
208+
void *simplify_is_neg_and_err_c(SimplifyEnv *env, const void *instr) {
209+
void *input = hir_c_get_operand(instr, 0);
210+
extern void *hir_reg_instr(void *reg);
211+
void *def = hir_reg_instr(input);
212+
if (def == NULL || hir_c_opcode(def) != HIR_OP_LoadConst) {
213+
return NULL;
214+
}
215+
HirType output_type = hir_register_type(hir_c_output(instr));
216+
return simplify_env_emit_load_const(env, make_cint_type(output_type, 0));
217+
}
218+
197219
/* ---- simplifyCondBranch (partial — constant condition folding) ----
198220
* If condition is a known int constant, fold to unconditional Branch. */
199221
void *simplify_cond_branch_const_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
@@ -36,6 +36,7 @@ void *simplify_primitive_box_bool_c(SimplifyEnv *env, const void *instr);
3636
void *simplify_cint_to_cbool_c(SimplifyEnv *env, const void *instr);
3737
void *simplify_cond_branch_const_c(SimplifyEnv *env, const void *instr);
3838
void *simplify_compare_c(SimplifyEnv *env, const void *instr);
39+
void *simplify_is_neg_and_err_c(SimplifyEnv *env, const void *instr);
3940

4041
#ifdef __cplusplus
4142
}

0 commit comments

Comments
 (0)