Wave icon indicating copy to clipboard operation
Wave copied to clipboard

Avoid accumulating aggregate temporary stack allocations across loop iterations

Open LunaStev opened this issue 3 weeks ago • 0 comments

Problem

Aggregate temporaries use alloca at the current insertion point. In a loop at O0, these allocations accumulate until function return even when the temporary does not escape.

Reproduction and evidence

Audited canonical master 69a67c6e0fb700f37ee6114f5a66d37ee13771a5 (0.0.1-alpha-dev). Matching checkout-built compiler/std were used for compiler probes on Linux amd64 with Rust 1.89.0 and LLVM 21.1.8. Artifact inspection, mocked/projection checks and foreign-target object emission are identified below and are not native execution on those operating systems.

struct S{x:i64;y:i64;z:i64;w:i64;}fun main()->i32{var i:i32=0;var sum:i64=0;while(i<2000000){var s:S=S{x:1,y:2,z:3,w:4};sum+=s.x;i+=1;}if(sum!=2000000){return 1;}return 0;}
target/debug/wavec --std-root "$PWD/std" build repro.wave -O0 --run --out-dir out-O0
target/debug/wavec --std-root "$PWD/std" build repro.wave -O2 --run --out-dir out-O2

With the ordinary host stack configuration unchanged: O0 terminates with SIGSEGV; O2 exits 0. The equivalent scalar loop exits 0 at O0/O2. A separate 200,000-iteration/1 MiB controlled-stack fixture reproduced the same difference.

Scope

Give nonescaping temporary storage an appropriate lifetime/reuse strategy while preserving address identity for genuinely escaping/live temporaries. Inspect aggregate, array and C ABI temporary paths together.

Starting points: llvm/src/expression/rvalue/structs.rs:38, llvm/src/expression/rvalue/arrays.rs:46.

Completion criteria

  • [ ] The nonescaping aggregate loop runs at O0 without stack growth proportional to iterations.
  • [ ] Retain correct aggregate values, aliasing/evaluation order and address lifetimes.
  • [ ] Cover representative temporary paths and O0/O2 controls; do not raise the OS stack limit as the fix.

Related: #409. Related broad trackers or completed implementations are context, not claims that their entire scope is unfinished.

LunaStev avatar Nov 12 '26 12:11 LunaStev