Core: Correct the pool-duplication TODO: the dead allocas do cost stack

The previous note claimed LLVM deletes the unreferenced duplicate temps.
Measured: removing only the dead alloca from the IR moves the BPF frame
from r10-32 to r10-24, so llc keeps it through frame layout and each
duplicated i64 temp costs 8 bytes per nesting level. opt -O2 would
remove it; the pipeline is llc only.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Pragyansh Chaturvedi
2026-09-19 15:31:27 +05:30
parent 055a81b395
commit 5e5c14e210

View File

@ -149,11 +149,12 @@ def allocate_mem(compilation_context, builder, body, func, ret_type, local_sym_t
# TODO: allocate_mem is re-entered for every if body (handle_if_allocation),
# and each level allocates a pool of its own even though the top-level count
# already covers nested statements. The duplicates are unreferenced and LLVM
# deletes them (verified: the BPF frame is identical with and without the
# nesting), so this costs nothing at runtime; it is IR noise and a trap,
# since only the last-allocated pool stays in local_sym_tab. Fix: count
# recursively, allocate once at the top level.
# already covers nested statements. Only the last-allocated pool stays in
# local_sym_tab; the others are unreferenced, but llc does not delete
# unreferenced allocas before frame layout (opt -O2 would, and the pipeline
# is llc only), so each duplicated temp slot costs its size in BPF stack:
# measured 8 bytes per i64 temp per nesting level. Fix: count recursively,
# allocate once at the top level.
allocate_temp_pool(builder, max_temps_needed, local_sym_tab)
return local_sym_tab