[PATCH] A new HeapToStack allocation promotion pass

Marc Glisse marc.glisse at inria.fr
Sat Oct 19 12:29:17 PDT 2013


Hello,

I hope the idea of the patch wasn't abandoned, so here is another 
question: how do you handle stack deallocation? I notice that you mark the 
end of life of the alloca'd memory, but I wonder if llvm knows enough 
magic to handle that.

For VLA, we save the stack pointer, allocate on the stack, and at the end 
of the scope we restore the stack pointer. This is possible in a large 
part because scopes in C cannot overlap unless one is included in the 
other. With malloc+free, we have no such guarantee:

p=malloc(m);
q=malloc(n);
free(p);
free(q);

if at each free we tried to restore the frame pointer from the 
corresponding malloc, bad things would happen. Somehow we'd need to 
postpone that deallocation to some place that's pretty hard to define 
(that's where llvm may already have magic). The easiest seems to be not to 
deallocate, which also means removing the deallocations from true VLA that 
may already exist in the function. And it can easily overflow the stack if 
doing a stack allocation inside a loop.

-- 
Marc Glisse



More information about the llvm-commits mailing list