edA-qa mort-ora-y via llvm-dev
2018-Mar-18 09:46 UTC
[llvm-dev] `free` counterpart to `alloca`, or way to lift to function home
I'm unintentionally allocating too much space on the stack by using `alloca` inside a loop. To fix this I will do my `alloca` outside of the loop itself. I'm wondering if there is a way for this to be automatically done: given alloca a function scope, rather than loop scope. I'm curious also, since this actually allocates each time in the loop, is there a way to say the stack allocations are no longer required and return to an early stack position? My `alloca` will always be in order, in a tree, thus it'd be safe to return to an early allocation point. -- edA-qa mort-ora-y http://mortoray.com/ Creator of the Leaf language http://leaflang.org/ Streaming algorithms, AI, and design on Twitch https://www.twitch.tv/mortoray Twitter edaqa
Tim Northover via llvm-dev
2018-Mar-18 10:49 UTC
[llvm-dev] `free` counterpart to `alloca`, or way to lift to function home
On 18 March 2018 at 09:46, edA-qa mort-ora-y via llvm-dev <llvm-dev at lists.llvm.org> wrote:> I'm curious also, since this actually allocates each time in the loop, > is there a way to say the stack allocations are no longer required and > return to an early stack position? My `alloca` will always be in order, > in a tree, thus it'd be safe to return to an early allocation point.As usual, Clang knows the answer: $ cat tmp.c void foo(int *); void bar(int n) { for (int i = 0; i < n; ++i) { int arr[i]; foo(arr); } } $ clang tmp.c -S -o- -emit-llvm -Os [...] define void @bar(i32) local_unnamed_addr #0 { [...] %8 = call i8* @llvm.stacksave() %9 = alloca i32, i64 %7, align 16 call void @foo(i32* nonnull %9) #3 call void @llvm.stackrestore(i8* %8) [...] } Cheers. Tim.
Possibly Parallel Threads
- A struct {i8, i64} has size == 12, clang says size 16
- A struct {i8, i64} has size == 12, clang says size 16
- Migration from 3.8 to 6.0 questions (segfault most concerning)
- A struct {i8, i64} has size == 12, clang says size 16
- How to set Target/Triple of ExecutionEngine