Skip to content

Commit a7f7aaf

Browse files
committed
simplify: port Unbox unbox(box(x)) identity path to C
Env-free handler: if IndexUnbox/PrimitiveUnbox input is a PrimitiveBox with matching type, return the inner operand directly. Uses HIR_OP_PrimitiveBox opcode check + hir_type_equal for type comparison. Falls through to C++ for constant-folding paths.
1 parent ade9151 commit a7f7aaf

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Python/jit/hir/simplify.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2830,8 +2830,11 @@ Register* simplifyInstr(Env& env, const Instr* instr) {
28302830
return r;
28312831
}
28322832
case Opcode::kIndexUnbox:
2833-
case Opcode::kPrimitiveUnbox:
2833+
case Opcode::kPrimitiveUnbox: {
2834+
auto *r = static_cast<Register*>(simplify_unbox_box_c(instr));
2835+
if (r) return r;
28342836
return simplifyUnbox(env, instr);
2837+
}
28352838

28362839
case Opcode::kIsNegativeAndErrOccurred: {
28372840
SimplifyEnv cenv = make_c_env();

Python/jit/hir/simplify_c.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,25 @@ void *simplify_primitive_compare_box_true_c(const void *instr) {
212212
return hir_c_get_operand(left_def, 0);
213213
}
214214

215+
/* ---- simplifyUnbox (partial — unbox(box(x)) → x) ---- */
216+
void *simplify_unbox_box_c(const void *instr) {
217+
void *input = hir_c_get_operand(instr, 0);
218+
void *output_reg = hir_c_output(instr);
219+
if (output_reg == NULL) return NULL;
220+
HirType output_type = hir_register_type(output_reg);
221+
222+
extern void *hir_reg_instr(void *reg);
223+
void *box_instr = hir_reg_instr(input);
224+
if (box_instr == NULL || hir_c_opcode(box_instr) != HIR_OP_PrimitiveBox)
225+
return NULL;
226+
227+
HirType box_type = ((const HirPrimitiveBox *)box_instr)->type;
228+
if (hir_type_equal(&box_type, &output_type)) {
229+
return hir_c_get_operand(box_instr, 0);
230+
}
231+
return NULL;
232+
}
233+
215234
/* ---- simplifyIntConvert ----
216235
* If input already has the target type, IntConvert is redundant. */
217236
void *simplify_int_convert_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
@@ -30,6 +30,7 @@ void *simplify_check_c(const void *instr);
3030
void *simplify_refine_type_c(const void *instr);
3131
void *simplify_guard_type_identity_c(const void *instr);
3232
void *simplify_primitive_compare_box_true_c(const void *instr);
33+
void *simplify_unbox_box_c(const void *instr);
3334
void *simplify_int_convert_c(SimplifyEnv *env, const void *instr);
3435

3536
/* Env-using handlers (Category 2) */

0 commit comments

Comments
 (0)