fix(0109): hoist all per-instruction allocas to the function entry block

An alloca built at its use site re-executes on every pass through that
block, and LLVM reclaims allocas only at ret — so loop-body locals,
nested-loop index slots, and emitter spill temps (ig.tmp, sret slots, ABI
coercion temps, byval materialization) grew the stack per iteration and
long loops segfaulted on stack exhaustion.

New LLVMEmitter.buildEntryAlloca inserts after existing entry-block
allocas and restores the builder position; every LLVMBuildAlloca site
reachable during instruction emission now routes through it.
Initialization stores stay at the use site (per-iteration re-init is
unchanged), and entry slots become mem2reg-promotable. The 35 .ir
snapshot diffs are pure alloca position moves (type multisets verified
identical per file).

Regression: examples/0047-basic-loop-local-stack-reuse.sx (segfaulted
pre-fix on both the 1M-iteration body-local loop and the 3M-iteration
nested loop).
This commit is contained in:
agra
2026-06-10 17:27:11 +03:00
parent e81780e32e
commit 878c4226a6
43 changed files with 1661 additions and 1468 deletions

View File

@@ -1386,6 +1386,33 @@ pub const LLVMEmitter = struct {
self.debugInfo().endFunctionDebug();
}
/// Build an alloca in the current function's ENTRY block, not at the
/// builder's position. An alloca executed inside a loop body allocates
/// fresh stack on every iteration (LLVM only reclaims at `ret`), so any
/// alloca reachable per-instruction must be hoisted here; only entry-block
/// allocas are static frame slots (and mem2reg-promotable). Insertion goes
/// after existing entry allocas; the builder position is restored.
pub fn buildEntryAlloca(self: *LLVMEmitter, ty: c.LLVMTypeRef, name: [*:0]const u8) c.LLVMValueRef {
const cur_bb = c.LLVMGetInsertBlock(self.builder);
const func = c.LLVMGetBasicBlockParent(cur_bb);
const entry_bb = c.LLVMGetEntryBasicBlock(func);
if (entry_bb == cur_bb) {
return c.LLVMBuildAlloca(self.builder, ty, name);
}
var insert_before = c.LLVMGetFirstInstruction(entry_bb);
while (insert_before != null) : (insert_before = c.LLVMGetNextInstruction(insert_before)) {
if (c.LLVMGetInstructionOpcode(insert_before) != c.LLVMAlloca) break;
}
if (insert_before != null) {
c.LLVMPositionBuilderBefore(self.builder, insert_before);
} else {
c.LLVMPositionBuilderAtEnd(self.builder, entry_bb);
}
const result = c.LLVMBuildAlloca(self.builder, ty, name);
c.LLVMPositionBuilderAtEnd(self.builder, cur_bb);
return result;
}
/// After emitting all blocks, fill in PHI incoming values from branch args.
fn fixupPhiNodes(self: *LLVMEmitter, func: *const Function, func_idx: u32) void {
if (self.pending_phis.items.len == 0) return;
@@ -2101,7 +2128,7 @@ pub const LLVMEmitter = struct {
}
// Struct/Array/Vector types: store to alloca, ptrtoint for the pointer
if (kind == c.LLVMStructTypeKind or kind == c.LLVMArrayTypeKind or kind == c.LLVMVectorTypeKind or kind == c.LLVMScalableVectorTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, ty, "ba.tmp");
const tmp = self.buildEntryAlloca(ty, "ba.tmp");
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildPtrToInt(self.builder, tmp, self.cached_i64, "ba.p2i");
}
@@ -2279,14 +2306,14 @@ pub const LLVMEmitter = struct {
}
// Struct → Integer (C ABI coercion: store struct to memory, load as integer)
if (val_kind == c.LLVMStructTypeKind and param_kind == c.LLVMIntegerTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, param_ty, "abi.tmp");
const tmp = self.buildEntryAlloca(param_ty, "abi.tmp");
_ = c.LLVMBuildStore(self.builder, c.LLVMConstNull(param_ty), tmp);
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.coerce");
}
// Integer → Struct (C ABI return coercion: store integer to memory, load as struct)
if (val_kind == c.LLVMIntegerTypeKind and param_kind == c.LLVMStructTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, val_ty, "abi.ret.tmp");
const tmp = self.buildEntryAlloca(val_ty, "abi.ret.tmp");
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.ret.coerce");
}
@@ -2295,19 +2322,19 @@ pub const LLVMEmitter = struct {
// memory-bitcast pattern as the integer case; the array type carries
// 16 bytes of storage so we alloca with param_ty to guarantee size.
if (val_kind == c.LLVMStructTypeKind and param_kind == c.LLVMArrayTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, param_ty, "abi.struct2arr");
const tmp = self.buildEntryAlloca(param_ty, "abi.struct2arr");
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.coerce.arr");
}
// Array → Struct (return-side counterpart for 9..16-byte structs)
if (val_kind == c.LLVMArrayTypeKind and param_kind == c.LLVMStructTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, val_ty, "abi.arr2struct");
const tmp = self.buildEntryAlloca(val_ty, "abi.arr2struct");
_ = c.LLVMBuildStore(self.builder, val, tmp);
return c.LLVMBuildLoad2(self.builder, param_ty, tmp, "abi.ret.coerce.arr");
}
// Array → Ptr (array decay: alloca + GEP to first element)
if (val_kind == c.LLVMArrayTypeKind and param_kind == c.LLVMPointerTypeKind) {
const tmp = c.LLVMBuildAlloca(self.builder, val_ty, "ca.arr");
const tmp = self.buildEntryAlloca(val_ty, "ca.arr");
_ = c.LLVMBuildStore(self.builder, val, tmp);
const zero = c.LLVMConstInt(self.cached_i64, 0, 0);
var indices = [_]c.LLVMValueRef{ zero, zero };
@@ -2720,7 +2747,7 @@ pub const LLVMEmitter = struct {
field_val = c.LLVMConstInt(self.cached_i64, 0, 0);
} else {
const base_ty = c.LLVMTypeOf(base_val);
const tmp = c.LLVMBuildAlloca(self.builder, base_ty, "fv.utmp");
const tmp = self.buildEntryAlloca(base_ty, "fv.utmp");
_ = c.LLVMBuildStore(self.builder, base_val, tmp);
const payload_ptr = c.LLVMBuildStructGEP2(self.builder, base_ty, tmp, 1, "fv.pp");
const field_llvm_ty = self.toLLVMType(field.ty);