Skip to content

Commit eb27753

Browse files
committed
SSAify: wire C implementation as sole path, fix LoadNull type + pool realloc
Three fixes: - LoadNull type: {{0}, 0} (TBottom) → HIR_TYPE_NULLPTR (0x80000000000). Wrong type caused reflowTypes to assign incorrect types, leading to SIGSEGV in codegen when dereferencing null register. - Pool realloc: contiguous pool array invalidated all PhxSSABlock pointers on growth. Switch to individual PyMem_RawMalloc per block. - Wire hir_ssaify_run_c as sole path in SSAify::Run (1-arg version). 2-arg version (builder.cpp inlining) remains C++.
1 parent 92fe7e8 commit eb27753

File tree

2 files changed

+18
-20
lines changed

2 files changed

+18
-20
lines changed

Python/jit/hir/ssa.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "cinderx/Jit/hir/ssa.h"
44

55
#include "cinderx/Jit/hir/hir_instr_c.h"
6+
#include "cinderx/Jit/hir/ssaify_c.h"
67
#include "cinderx/Common/log.h"
78
#include "cinderx/Jit/hir/hir.h"
89
#include "cinderx/Jit/hir/phi_elimination.h"
@@ -17,8 +18,7 @@ namespace jit::hir {
1718

1819

1920
void SSAify::Run(Function& irfunc) {
20-
Run(irfunc, irfunc.cfg.entry_block);
21-
PhiElimination{}.Run(irfunc);
21+
hir_ssaify_run_c(static_cast<void*>(&irfunc));
2222
}
2323

2424
// This implements the algorithm outlined in "Simple and Efficient Construction

Python/jit/hir/ssaify_c.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,11 @@ typedef struct {
140140
void *func; /* HirFunction */
141141
void *null_reg; /* singleton null register */
142142
PhxMap block_map; /* BasicBlock* → PhxSSABlock* */
143-
PhxSSABlock *pool; /* arena-allocated SSABasicBlocks */
144-
size_t pool_count;
145-
size_t pool_cap;
143+
PhxPtrArr allocs; /* individually allocated PhxSSABlock* for cleanup */
146144
} PhxSSAState;
147145

148146
static PhxSSABlock *ssa_alloc_block(PhxSSAState *st, void *bb) {
149-
if (st->pool_count >= st->pool_cap) {
150-
st->pool_cap = st->pool_cap ? st->pool_cap * 2 : 32;
151-
st->pool = (PhxSSABlock *)PyMem_RawRealloc(st->pool, st->pool_cap * sizeof(PhxSSABlock));
152-
}
153-
PhxSSABlock *s = &st->pool[st->pool_count++];
147+
PhxSSABlock *s = (PhxSSABlock *)PyMem_RawMalloc(sizeof(PhxSSABlock));
154148
memset(s, 0, sizeof(*s));
155149
s->block = bb;
156150
phx_arr_init(&s->preds);
@@ -159,6 +153,7 @@ static PhxSSABlock *ssa_alloc_block(PhxSSAState *st, void *bb) {
159153
phx_map_init(&s->phi_nodes);
160154
phx_arr_init(&s->inc_phi_regs);
161155
phx_arr_init(&s->inc_phi_outs);
156+
phx_arr_push(&st->allocs, s);
162157
return s;
163158
}
164159

@@ -244,7 +239,7 @@ static void *ssa_get_define(PhxSSAState *st, PhxSSABlock *ssablock, void *reg) {
244239
it = hir_bb_next_instr(bb, it);
245240
}
246241
st->null_reg = hir_func_alloc_register(st->func);
247-
HirType nullptr_type = {{0}, 0};
242+
HirType nullptr_type = HIR_TYPE_NULLPTR;
248243
void *loadnull = hir_c_create_load_const(st->null_reg, nullptr_type);
249244
if (it) {
250245
hir_c_copy_bytecode_offset(loadnull, it);
@@ -294,7 +289,7 @@ typedef struct {
294289

295290
static int ssa_visit_use_cb(void **reg_slot, void *ctx_raw) {
296291
SSAVisitCtx *vc = (SSAVisitCtx *)ctx_raw;
297-
if (*reg_slot == NULL) return 1;
292+
JIT_CHECK_C(*reg_slot != NULL, "Instructions should not have nullptr operands");
298293
*reg_slot = ssa_get_define(vc->st, vc->ssablock, *reg_slot);
299294
return 1;
300295
}
@@ -315,6 +310,7 @@ void hir_ssaify_run_c(HirFunction func) {
315310
memset(&st, 0, sizeof(st));
316311
st.func = func;
317312
phx_map_init(&st.block_map);
313+
phx_arr_init(&st.allocs);
318314

319315
HirCFG *cfg = (HirCFG *)hir_func_cfg_ptr(func);
320316

@@ -405,15 +401,17 @@ void hir_ssaify_run_c(HirFunction func) {
405401
}
406402

407403
/* Cleanup */
408-
for (size_t i = 0; i < st.pool_count; i++) {
409-
phx_arr_destroy(&st.pool[i].preds);
410-
phx_arr_destroy(&st.pool[i].succs);
411-
phx_map_destroy(&st.pool[i].local_defs);
412-
phx_map_destroy(&st.pool[i].phi_nodes);
413-
phx_arr_destroy(&st.pool[i].inc_phi_regs);
414-
phx_arr_destroy(&st.pool[i].inc_phi_outs);
404+
for (size_t i = 0; i < st.allocs.count; i++) {
405+
PhxSSABlock *s = (PhxSSABlock *)st.allocs.data[i];
406+
phx_arr_destroy(&s->preds);
407+
phx_arr_destroy(&s->succs);
408+
phx_map_destroy(&s->local_defs);
409+
phx_map_destroy(&s->phi_nodes);
410+
phx_arr_destroy(&s->inc_phi_regs);
411+
phx_arr_destroy(&s->inc_phi_outs);
412+
PyMem_RawFree(s);
415413
}
416-
PyMem_RawFree(st.pool);
414+
phx_arr_destroy(&st.allocs);
417415
phx_map_destroy(&st.block_map);
418416
PyMem_RawFree(rpo);
419417

0 commit comments

Comments
 (0)