[PATCH] D102782: Add support for Warning Flag "-Wstack-usage="

Ryan Santhirarajan via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 9 15:17:18 PDT 2021


rsanthir.quic added inline comments.


================
Comment at: clang/docs/ReleaseNotes.rst:79-80
 
+- ``-Wstack-usage=<byte-size>`` warn if stack usage of user functions might
+  exceed <byte-size>.
+
----------------
Quuxplusone wrote:
> Does this mean:
> - Warn if the size of any single function's stack frame (including temporaries and local variables, but not parameters or return addresses) exceeds <byte-size>.
> - Warn if the size of any single function's stack frame (including parameters and return addresses) exceeds <byte-size>.
> - Warn if the total stack usage of the longest visible call chain in this translation unit might exceed <byte-size>.
> ?
> 
Looking at how the value for this flag is obtained, which is an alias of "-Wframe-larger-than", I see that it is all objects placed on the stack by each function plus any space reserved for arguments of callsites in the function. 

There is no precise definition of what is included as it depends on the target as well as what is stored on the stack or not.

I guess in simplest terms, for each function this will report how much stack space is used by that function.



================
Comment at: clang/test/Frontend/backend-stack-usage-diagnostic.c:19-23
+// WARN: warning: stack frame size of {{[0-9]+}} bytes in function 'test_square'
+// IGNORE-NOT:  stack frame size of {{[0-9]+}} bytes in function 'test_square'
+int test_square(int num) {
+  return num * num;
+}
----------------
Quuxplusone wrote:
> This function has no "stack frame" in the usual sense of the word, because it's just
> ```
>         movl    %edi, %eax
>         imull   %edi, %eax
>         ret
> ```
> So am I correct to infer that the `-Wstack-usage=` option includes the size of the return address in its computations? So the stack usage of this function would be computed as "8 bytes" because the `callq` instruction pushes 8 bytes on the stack?
> This seems eminently reasonable to me, but non-obvious, and should get some user-facing documentation.
When a function stores the return address on the stack, that will be included in that functions stack usage.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D102782/new/

https://reviews.llvm.org/D102782



More information about the cfe-commits mailing list