[PATCH] A new HeapToStack allocation promotion pass

Hal Finkel hfinkel at anl.gov
Tue Sep 24 19:21:42 PDT 2013


----- Original Message -----
> On Sep 23, 2013, at 5:00 PM, hfinkel at anl.gov wrote:
> 
> > This adds a new optimization pass that can 'promote' malloc'd
> > memory to stack-allocated memory when the lifetime of the
> > allocation can be determined to be bounded by the execution of the
> > function.
> 
> Is this transformation safe in general?

This is a good point; I'd not thought about the malloc introspection functions. I think that there are multiple possible viewpoints:

 1. Clang/LLVM implement particular standards (C/C++), and according to those standards malloc/free are reserved as part of the implementation. As a result, the compiler is free to implement these functions in any way it sees fit, so long as that mechanism complies with the requirements contained the language specification. The introspection functions are not part of the language specifications, and you cannot expect them to work unless you disable the compiler's understanding of malloc/free: -fno-builtin=malloc (or similar). FWIW, these introspection functions also fail under any setup where an alternate allocator has hooked malloc/free.

 2. Clang/LLVM serve as a compiler that is part of a (conforming) programming environment for the target system. This environment includes the system libraries, specifically including all documented public functions in those libraries. In this view, the 'as if' requirement extends to all side effects of the system-libc-provided function implementations.

I'm open to both views; what do others think?

Thanks again,
Hal

> Various libc implementations
> have functions for getting information about the allocated chunk.
> For example, malloc_size(3) on OS X, malloc_usable_size(3) in glibc.
> This patch seems to break valid code:
> 
> [secdev:~/m] s$ uname -a
> Linux secdev 3.8.0-30-generic #44-Ubuntu SMP Thu Aug 22 20:52:24 UTC
> 2013 x86_64 x86_64 x86_64 GNU/Linux
> [secdev:~/m] s$ cat a.c
> #include <malloc.h>
> #include <stdlib.h>
> #include <stdio.h>
> 
> int main()
> {
>   void *p = malloc(10);
>   size_t s = malloc_usable_size(p);
>   printf("%zu\n", s);
>   free(p);
> }
> [secdev:~/m] s$ ../build-master/bin/clang -Wall a.c
> [secdev:~/m] s$ ./a.out
> 24
> [secdev:~/m] s$ ../build-master/bin/clang -Wall a.c -O1
> [secdev:~/m] s$ ./a.out
> Segmentation fault (core dumped)
> 
> 
> --
> Stephen Checkoway
> 
> 
> 
> 
> 
> 

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



More information about the llvm-commits mailing list