No stack allocation whole program compilation?

If you write an app that is:

  • Single threaded
  • Has no cycles in call graph
  • Doesn't use alloca or VLAs
  • Can modern whole program optimizing compilers optimize away all stack allocation (eg GCC, MSVC, ICC)? It seems like in those circumstances it should be able to allocate all possible stack space statically. By 'whole program' I mean the compiler has access to /all/ source code (no possiblity of dlopen'ing things at runtime, etc.).


    If you can guarantee the conditions you stated, then yes: it would be possible to effectively have the stack be completely statically allocated. Each function would have a block of stack memory.

    However, will actual compilers do it? No.

    It gains absolutely nothing to do so. Indeed, it may gain less than nothing. Often times, much of the working stack is in the cache, so modifications to it are pretty cheap. If the stack were in static memory, then the only time any particular function's "stack" memory would be cached would be if you had recently called that function. Using a real stack, you're more likely to be working in the cache.

    Furthermore, giving each function a block of stack memory can easily make your program's static memory usage much larger than it needs to be. The stack is a fixed-size construct; no matter how many functions you have, the stack takes up a certain size. If you have 100,000 functions, and each function takes up 64 bytes of space, then your static "stack" must take up ~6.4MB of space.

    Why? You're never going to be using most of that memory at any one time. The program would run just fine in a 1MB or even 512KB stack; why take up 6x that memory for nothing?

    So it is both not a performance optimization and can bloats your program's memory.


    This is a comment that's too long to be a comment:

    Note that while all stack allocations may be theoretically optimized away, more may be allocated than necessary. This is not what the OP was asking, but it could be interesting to consider. Finding the minimum-sized allocation required would be equivalent to solving the halting problem. Imagine a program structured as:

    <do 'something'>
    <call last thing which happens to require more
     stack space than everything else in 'something'>
    

    You only need the additional stack space if <do 'something'> "halts".

    You can also imagine other variations where optimizing becomes arbitrarily hard. For example, your program could simply evaluate a 3SAT expression with user input and do something depending on that -- but that 3SAT expression may or may not have any value that results in true.

    Perhaps there is a more trivial case: The user may simply never enter input that requires more stack space for processing.


    It's possible for a compiler to do this, but it would be such a specific optimization that it probably wouldn't.

    If you had a program that was completely inlined, you would take care of the overhead of setting up stack frames for function calls.

    However, if you wanted to also get rid of stack allocations for local variables, the compiler has to transform those local variables into global variables. No compiler I know of does that, and on some platforms it takes extra instructions to reference a global variable compared to a local variable (as the address must be loaded with two instructions rather than one). Plus, since referencing a stack variable is such a common operation, it's usually encoded into a smaller instruction.

    链接地址: http://www.djcxy.com/p/58852.html

    上一篇: 地图错误:在切换视角时,标记会在整个地方移动。 固定?

    下一篇: 没有堆栈分配整个程序编译?