Visitar URL original
W27d #3: emitMatchClass → C body + delegation stub · SonicField/cpython@0fd0057 · GitHub
Skip to content

Commit 0fd0057

Browse files
committed
W27d python#3: emitMatchClass → C body + delegation stub
MATCH_CLASS (mainline) opcode handler. Per W27d Step A scope. C body: hir_builder_emit_match_class_c (~70 lines). Pops names/type/subject. Loads nargs as TCUInt64 const. Emits MatchClass → attrs_tuple. Refines to TOptTupleExact = TTupleExact|TNullptr (constructed via hir_type_union per theologian L2544 fallback — HIR_TYPE_OPTTUPLEEXACT not defined). Cond-branch: true_block refines to TTupleExact + (pre-3.12) loads Py_True; false_block emits CheckErrOccurred + (pre-3.12 Py_False+assign-subject; 3.12+ Py_None +assign-none). Converges at done. ZERO new bridges. C primitives: hir_c_create_load_const + hir_c_create_match_class_reg2 + hir_c_create_refine_type_reg + hir_c_create_cond_branch + hir_c_create_check_err_occurred_reg + hir_c_create_assign_reg + hir_c_create_branch_cpp + hir_type_from_cuint + hir_type_union + hir_type_from_object + hir_func_alloc_register + hir_cfg_alloc_block + hir_builder_temps_alloc_stack (all existing). C++ HIRBuilder::emitMatchClass → 4-line delegation stub. Verification: cmake --build target jit/phoenix_jit clean (BUILD_EXIT=0). Sole-path: MATCH_CLASS is mainline-tested per theologian L2544 coverage observation.
1 parent f9cbca1 commit 0fd0057

2 files changed

Lines changed: 80 additions & 47 deletions

File tree

‎Python/jit/hir/builder.cpp‎

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4810,56 +4810,15 @@ void HIRBuilder::emitMatchMappingSequence(
48104810
phx_ptr_arr_push(&tc.frame.stack, result);
48114811
}
48124812

4813+
extern "C" void hir_builder_emit_match_class_c(
4814+
void *tc, void *func, void *builder, int oparg);
4815+
48134816
void HIRBuilder::emitMatchClass(
4814-
CFG& cfg,
4817+
CFG& /*cfg*/,
48154818
TranslationContext& tc,
48164819
const jit::BytecodeInstruction& bc_instr) {
4817-
PhxPtrArray& stack = tc.frame.stack;
4818-
Register* names = static_cast<Register*>(phx_ptr_arr_pop(&stack));
4819-
Register* type = static_cast<Register*>(phx_ptr_arr_pop(&stack));
4820-
Register* subject = static_cast<Register*>(phx_ptr_arr_pop(&stack));
4821-
auto oparg = bc_instr.oparg();
4822-
4823-
auto nargs = temps_.AllocateStack();
4824-
tc.emitLoadConst(nargs, Type::fromCUInt(oparg, TCUInt64));
4825-
4826-
auto attrs_tuple = temps_.AllocateStack();
4827-
tc.emitMatchClass(attrs_tuple, subject, type, nargs, names);
4828-
tc.emitRefineType(attrs_tuple, TOptTupleExact, attrs_tuple);
4829-
4830-
Register* tuple_or_none = temps_.AllocateStack();
4831-
phx_ptr_arr_push(&stack, tuple_or_none);
4832-
Register* if_success = nullptr;
4833-
if constexpr (PY_VERSION_HEX < 0x030C0000) {
4834-
if_success = temps_.AllocateStack();
4835-
phx_ptr_arr_push(&stack, if_success);
4836-
}
4837-
4838-
auto true_block = cfg.AllocateBlock();
4839-
auto false_block = cfg.AllocateBlock();
4840-
auto done = cfg.AllocateBlock();
4841-
4842-
tc.emitCondBranch(attrs_tuple, true_block, false_block);
4843-
tc.block = true_block;
4844-
tc.emitRefineType(tuple_or_none, TTupleExact, attrs_tuple);
4845-
if constexpr (PY_VERSION_HEX < 0x030C0000) {
4846-
tc.emitLoadConst(if_success, Type::fromObject(Py_True));
4847-
}
4848-
tc.emitBranch(done);
4849-
4850-
tc.block = false_block;
4851-
tc.emitCheckErrOccurred(tc.frame);
4852-
if constexpr (PY_VERSION_HEX < 0x030C0000) {
4853-
tc.emitLoadConst(if_success, Type::fromObject(Py_False));
4854-
tc.emitAssign(tuple_or_none, subject);
4855-
} else {
4856-
Register* none = temps_.AllocateNonStack();
4857-
tc.emitLoadConst(none, Type::fromObject(Py_None));
4858-
tc.emitAssign(tuple_or_none, none);
4859-
}
4860-
tc.emitBranch(done);
4861-
4862-
tc.block = done;
4820+
hir_builder_emit_match_class_c(
4821+
&tc, current_func_, this, bc_instr.oparg());
48634822
}
48644823

48654824
extern "C" void hir_builder_emit_match_keys_c(void *tc, void *func);

‎Python/jit/hir/builder_emit_c.c‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4393,4 +4393,78 @@ void hir_builder_emit_get_yield_from_iter_c(
43934393
phx_ptr_arr_push(&tc->frame.stack, iter_out);
43944394
}
43954395

4396+
/* W27d #3: emitMatchClass (MATCH_CLASS, mainline). Mirrors C++
4397+
* HIRBuilder::emitMatchClass @ builder.cpp:4858.
4398+
*
4399+
* Pops names/type/subject. Loads nargs as TCUInt64 const. Emits MatchClass
4400+
* → attrs_tuple. Refines attrs_tuple to TOptTupleExact (= TTupleExact |
4401+
* TNullptr). Cond-branch on attrs_tuple → true_block (refine to TTupleExact,
4402+
* pre-3.12 also load Py_True into if_success) / false_block (CheckErrOccurred,
4403+
* pre-3.12 load Py_False+assign subject; 3.12+ load Py_None and assign).
4404+
* Converges at done. Pushes tuple_or_none + (pre-3.12) if_success. */
4405+
void hir_builder_emit_match_class_c(
4406+
PhxTranslationContext *tc,
4407+
void *func,
4408+
void *builder,
4409+
int oparg) {
4410+
void *names = phx_ptr_arr_pop(&tc->frame.stack);
4411+
void *type = phx_ptr_arr_pop(&tc->frame.stack);
4412+
void *subject = phx_ptr_arr_pop(&tc->frame.stack);
4413+
4414+
HirType t_cuint64 = HIR_TYPE_CUINT64;
4415+
void *nargs = hir_builder_temps_alloc_stack(builder);
4416+
HirType nargs_type = hir_type_from_cuint((uint64_t)oparg, t_cuint64);
4417+
phx_tc_emit(tc, hir_c_create_load_const(nargs, nargs_type));
4418+
4419+
void *attrs_tuple = hir_builder_temps_alloc_stack(builder);
4420+
phx_tc_emit(tc, hir_c_create_match_class_reg2(
4421+
attrs_tuple, subject, type, nargs, names));
4422+
/* TOptTupleExact = TTupleExact | TNullptr (per theologian L2544 fallback
4423+
* — HIR_TYPE_OPTTUPLEEXACT not in hir_type_c.h, construct via union). */
4424+
HirType t_tupleexact = HIR_TYPE_TUPLEEXACT;
4425+
HirType t_nullptr = HIR_TYPE_NULLPTR;
4426+
HirType t_opt_tupleexact = hir_type_union(t_tupleexact, t_nullptr);
4427+
phx_tc_emit(tc, hir_c_create_refine_type_reg(
4428+
attrs_tuple, t_opt_tupleexact, attrs_tuple));
4429+
4430+
void *tuple_or_none = hir_builder_temps_alloc_stack(builder);
4431+
phx_ptr_arr_push(&tc->frame.stack, tuple_or_none);
4432+
void *if_success = NULL;
4433+
#if PY_VERSION_HEX < 0x030C0000
4434+
if_success = hir_builder_temps_alloc_stack(builder);
4435+
phx_ptr_arr_push(&tc->frame.stack, if_success);
4436+
#endif
4437+
4438+
void *true_block = hir_cfg_alloc_block(func);
4439+
void *false_block = hir_cfg_alloc_block(func);
4440+
void *done = hir_cfg_alloc_block(func);
4441+
4442+
phx_tc_emit(tc, hir_c_create_cond_branch(attrs_tuple, true_block, false_block));
4443+
4444+
tc->block = true_block;
4445+
phx_tc_emit(tc, hir_c_create_refine_type_reg(
4446+
tuple_or_none, t_tupleexact, attrs_tuple));
4447+
#if PY_VERSION_HEX < 0x030C0000
4448+
HirType t_true = hir_type_from_object(Py_True);
4449+
phx_tc_emit(tc, hir_c_create_load_const(if_success, t_true));
4450+
#endif
4451+
phx_tc_emit(tc, hir_c_create_branch_cpp(done));
4452+
4453+
tc->block = false_block;
4454+
phx_tc_emit(tc, hir_c_create_check_err_occurred_reg(&tc->frame));
4455+
#if PY_VERSION_HEX < 0x030C0000
4456+
HirType t_false = hir_type_from_object(Py_False);
4457+
phx_tc_emit(tc, hir_c_create_load_const(if_success, t_false));
4458+
phx_tc_emit(tc, hir_c_create_assign_reg(tuple_or_none, subject));
4459+
#else
4460+
void *none_reg = hir_func_alloc_register(func);
4461+
HirType t_none = hir_type_from_object(Py_None);
4462+
phx_tc_emit(tc, hir_c_create_load_const(none_reg, t_none));
4463+
phx_tc_emit(tc, hir_c_create_assign_reg(tuple_or_none, none_reg));
4464+
#endif
4465+
phx_tc_emit(tc, hir_c_create_branch_cpp(done));
4466+
4467+
tc->block = done;
4468+
}
4469+
43964470

0 commit comments

Comments
 (0)