[llvm-dev] segv inside loop on x86_64

mats petersson via llvm-dev llvm-dev at lists.llvm.org
Tue Oct 27 02:18:39 PDT 2015


On 27 October 2015 at 08:41, David Chisnall via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> On 27 Oct 2015, at 05:13, Matt Arsenault via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
> >
> >> On Oct 27, 2015, at 12:46 AM, Peter McKinna via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
> >>
> >> label_1:                                          ; preds = %label_2,
> %entry
> >>   %v.87 = load i64, i64* %i, align 8
> >>   %abs_icmp = icmp slt i64 %v.87, 0
> >>   %itetmp = alloca i64
> >>   br i1 %abs_icmp, label %abs_then, label %abs_else
> >
> > You are supposed to have all allocas in the entry block, although I’m
> not sure if this is not expected to work.
>
> This will work, but without a stacksave / stackrestore, each loop
> iteration will allocate 8 bytes of space on the stack.  The default stack
> size on x86-64 is likely to be about 8MB, so if your loop runs for a
> million iterations, it’s pretty much guaranteed to run out of stack space
> and segfault.  If it runs for less and is not one of the first calls in the
> stack, the same applies.
>

And because this is rounded up to 16 bytes, it runs out of half as many
iterations - in other words, around 500k iterations.

Not sure why the compiler generates such awkward code here:

movq %rsp, %rax
addq $-16, %rax
movq %rax, %rsp

but you can see that it subtracts 16 from the stack pointer each iteration,
and will eventually run out of stack-space.

Move the alloca out to the beginning of the function.

--
Mats

>
> David
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20151027/10ccba22/attachment.html>


More information about the llvm-dev mailing list