<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<p><br>
</p>
<div class="moz-cite-prefix">On 10/21/2017 12:43 PM, Andrew Kelley
via llvm-dev wrote:<br>
</div>
<blockquote
cite="mid:CACXN+=TKN7RwdGOYDLq_zC8gkpYAbtpFxxoHL9HPkLjJFnhoVQ@mail.gmail.com"
type="cite">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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>
</blockquote>
<br>
Would a builtin function be the best way to expose this information?
Maybe generating a side table would be best? As I recall, we already
produce some stack-size information as part of our StackMap table
(see <a class="moz-txt-link-freetext" href="https://llvm.org/docs/StackMaps.html">https://llvm.org/docs/StackMaps.html</a>). Another option could be
some special form of prefix/prologue data
(<a class="moz-txt-link-freetext" href="http://llvm.org/docs/LangRef.html#prefix-data">http://llvm.org/docs/LangRef.html#prefix-data</a> /
<a class="moz-txt-link-freetext" href="http://llvm.org/docs/LangRef.html#prologue-data">http://llvm.org/docs/LangRef.html#prologue-data</a>).<br>
<br>
The "@stack_size(function)" you've discussed on the zig issue may
not be implementable in general (because we'd need some way of
making sure that we always generate code for "@foo" before any other
function containing a call to "@stack_size(@foo)", and that's
probably not possible in general).<br>
<br>
-Hal<br>
<br>
<blockquote
cite="mid:CACXN+=TKN7RwdGOYDLq_zC8gkpYAbtpFxxoHL9HPkLjJFnhoVQ@mail.gmail.com"
type="cite">
<div dir="ltr">
<div><br>
</div>
<div><br>
</div>
Upstream issue: <a moz-do-not-send="true"
href="https://github.com/zig-lang/zig/issues/157">https://github.com/zig-lang/zig/issues/157</a><br>
</div>
<br>
<fieldset class="mimeAttachmentHeader"></fieldset>
<br>
<pre wrap="">_______________________________________________
LLVM Developers mailing list
<a class="moz-txt-link-abbreviated" href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>
<a class="moz-txt-link-freetext" href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a>
</pre>
</blockquote>
<br>
<pre class="moz-signature" cols="72">--
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</body>
</html>