[PATCH] A new HeapToStack allocation promotion pass

Hal Finkel hfinkel at anl.gov
Sat Oct 5 15:29:26 PDT 2013


----- Original Message -----
> 
> On Oct 5, 2013, at 4:30 PM, Hal Finkel <hfinkel at anl.gov> wrote:
> 
> > 
> > ----- Stephen Checkoway <s at pahtak.org> wrote:
> >> 
> >> In addition to not working with standard library functions that
> >> except things in the heap (malloc_usable_size, mallinfo) as I
> >> mentioned previously, it also doesn't work if the allocated
> >> memory escapes via exceptions (I think someone else mentioned
> >> that previously).
> > 
> > Yep, Nick also pointed this out; thanks for confirming!
> 
> No problem. Here's one with longjmp().

It seems in general that we have two situations to deal with:

 1. If the pointer (or some alias) is captured, and we (or some function we call) has some indefinite loop (including the use operating-system-assisted synchronization primitives), then some other thread might free the memory. Maybe I could call safe functions in this regard 'non-blocking'?

 2. A function that we call may throw, longjmp, etc. making the code after it potentially unreachable. This affects the 'free on all exit paths' condition. So we need to way to recognize safe functions in this regard (maybe 'always_returns'?). 

 2a. This called function may free the pointer (before throwing, etc.) if either the pointer (or some alias) is provided as a parameter or the pointer is captured.

 -Hal

> 
> #include <setjmp.h>
> #include <stdlib.h>
> 
> static void *q;
> static jmp_buf env;
> 
> static __attribute__((noinline)) void bar(int a)
> {
>   if (a > 1)
>     longjmp(env, 1);
> }
> static __attribute__((noinline)) void foo(int a)
> {
>   void *p = malloc(10);
>   q = p;
>   bar(a);
>   free(p);
> }
> 
> int main(int argc, char *argv[])
> {
>   if (!setjmp(env))
>     foo(argc);
>   if (argc > 1)
>     free(q);
> }
> 
> > I'll work up a more conservative version and post it soon.
> 
> 
> Great! This seems like a useful optimization.
> 
> --
> Stephen Checkoway
> 
> 
> 
> 
> 
> 

-- 
Hal Finkel
Assistant Computational Scientist
Leadership Computing Facility
Argonne National Laboratory



More information about the llvm-commits mailing list