Skip to content

Commit c181912

Browse files
committed
simplify: port RefineType + GuardType identity to C (chunk 3)
Two new C handlers: - simplify_refine_type_c: fully replaces C++ simplifyRefineType - simplify_guard_type_identity_c: handles GuardType identity path (type already matches), falls through to C++ for TNoneType case
1 parent 4bd8c72 commit c181912

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

Python/jit/hir/simplify.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,10 +2736,13 @@ Register* simplifyInstr(Env& env, const Instr* instr) {
27362736
return static_cast<Register*>(simplify_check_c(instr));
27372737
case Opcode::kCheckSequenceBounds:
27382738
return simplifyCheckSequenceBounds(env, instr);
2739-
case Opcode::kGuardType:
2739+
case Opcode::kGuardType: {
2740+
auto *r = static_cast<Register*>(simplify_guard_type_identity_c(instr));
2741+
if (r) return r;
27402742
return simplifyGuardType(env, static_cast<const GuardType*>(instr));
2743+
}
27412744
case Opcode::kRefineType:
2742-
return simplifyRefineType(static_cast<const RefineType*>(instr));
2745+
return static_cast<Register*>(simplify_refine_type_c(instr));
27432746
case Opcode::kCast:
27442747
return simplifyCast(static_cast<const Cast*>(instr));
27452748

Python/jit/hir/simplify_c.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,30 @@ void *simplify_check_c(const void *instr) {
4646
return NULL;
4747
}
4848

49+
/* ---- simplifyRefineType ----
50+
* If input already has the target type, RefineType is redundant. */
51+
void *simplify_refine_type_c(const void *instr) {
52+
void *input = hir_c_get_operand(instr, 0);
53+
HirType input_type = hir_register_type(input);
54+
HirType target = ((const HirRefineType *)instr)->type;
55+
if (hir_type_is_subtype(input_type, target)) {
56+
return input;
57+
}
58+
return NULL;
59+
}
60+
61+
/* ---- simplifyGuardType (partial — type-already-matches path) ----
62+
* If input already has the guarded type, GuardType is redundant. */
63+
void *simplify_guard_type_identity_c(const void *instr) {
64+
void *input = hir_c_get_operand(instr, 0);
65+
HirType input_type = hir_register_type(input);
66+
HirType target = hir_c_guard_type_target(instr);
67+
if (hir_type_is_subtype(input_type, target)) {
68+
return input;
69+
}
70+
return NULL;
71+
}
72+
4973
/* ---- simplifyIsTruthy (partial — CBool constant folding) ----
5074
* If input is a known CBool constant, replace with the constant directly. */
5175
void *simplify_is_truthy_cbool_c(SimplifyEnv *env, const void *instr) {

Python/jit/hir/simplify_c.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ void *simplify_env_emit_use_type(SimplifyEnv *env, void *val, HirType type);
2727

2828
/* Env-free handlers (return existing register or NULL) */
2929
void *simplify_check_c(const void *instr);
30+
void *simplify_refine_type_c(const void *instr);
31+
void *simplify_guard_type_identity_c(const void *instr);
3032

3133
/* Env-using handlers */
3234
void *simplify_is_truthy_cbool_c(SimplifyEnv *env, const void *instr);

0 commit comments

Comments
 (0)