<div dir="ltr"><div>Here is a feature I would like to introduce to my frontend:</div><div><br></div><div>A builtin function to calculate the upper bound of the stack size of any given function.</div><div><br></div><div>The idea here is that, if we do not have any:</div><div> * external library calls</div><div> * alloca</div><div> * indirect function calls</div><div> * non-tail-recursion</div><div> * inline assembly that modifies the stack pointer</div><div><br></div><div>Then, we can look at a given function and determine precisely how much stack space is needed to spawn a thread to run that function. This provides two benefits:</div><div> * Statically guarantee that stack overflow will not occur</div><div> * Threads can have smaller stack sizes, lowering the cost of creating a thread</div><div><br></div><div>This feature requires tight coupling with LLVM, because:</div><div> * We need to know the stack size post-optimizations</div><div> * Frontend needs to make a compile error if non-tail-recursion occurs, but LLVM might introduce (or remove?) tail recursion, and so we would need to capture this error from LLVM and report it back in the frontend.</div><div><br></div><div><br></div><div>What do you think? Is this feature reasonable to achieve with LLVM and in scope of the LLVM project?</div><div><br></div><div>I would propose an LLVM builtin function to perform this calculation, and I can work on a proof-of-concept if llvm-dev thinks this idea is worth pursuing.</div><div><br></div><div><br></div>Upstream issue: <a href="https://github.com/zig-lang/zig/issues/157">https://github.com/zig-lang/zig/issues/157</a><br></div>