I am running this code in JIT on x86 (32 bit). It crashes when 'alloca %object' instruction is within the body of the cycle, and it finishes successfully when this instruction is in the beginning of main (outside the cycle). Crash occurs in the middle of the cycle after few hundreds of thousands iterations. What is wrong? Does alloca inside the cycle forces it to allocate it on stack for every iteration instead of only once? If this is the case why verify doesn't complain? Yuri --- code --- %object = type { i8, i8, i8} @str2 = private constant [1 x i8] zeroinitializer define void @main() { for.init: %i = alloca i32 store i32 0, i32* %i br label %for.end for.body: %obj = alloca %object %str = getelementptr inbounds [1 x i8]* @str2, i32 0, i32 0 call void @obj_constr(%object* noalias sret %obj, i8* %str) call void @obj_destr(%object* %obj) br label %for.incr for.incr: %incr = load i32* %i %incr3 = add i32 %incr, store i32 %incr3, i32* %i br label %for.end for.end: %iload = load i32* %i %xcmp = icmp sge i32 %iload, 100000000 br i1 %xcmp, label %xend, label %for.body xend: ret void }
Reid Kleckner
2010-Jun-19 01:28 UTC
[LLVMdev] Is alloca instruction allowed within the cycle?
Because that is the meaning of the alloca instruction, allocate more stack space. It would do the same if you called alloca() in C in a loop. The expected way for frontends to implement local variables is to keep inserting alloca instructions into the entry block as you encounter new variables. I don't think mem2reg will even work on allocas not in the entry block. Reid On Fri, Jun 18, 2010 at 6:20 PM, Yuri <yuri at rawbw.com> wrote:> I am running this code in JIT on x86 (32 bit). > It crashes when 'alloca %object' instruction is within the body of the > cycle, and it finishes successfully when this instruction is in the > beginning of main (outside the cycle). Crash occurs in the middle of the > cycle after few hundreds of thousands iterations. > > What is wrong? Does alloca inside the cycle forces it to allocate it on > stack for every iteration instead of only once? > If this is the case why verify doesn't complain? > > Yuri > > > --- code --- > > %object = type { i8, i8, i8} > @str2 = private constant [1 x i8] zeroinitializer > > define void @main() { > for.init: > %i = alloca i32 > store i32 0, i32* %i > br label %for.end > > for.body: > %obj = alloca %object > %str = getelementptr inbounds [1 x i8]* @str2, i32 0, i32 0 > call void @obj_constr(%object* noalias sret %obj, i8* %str) > call void @obj_destr(%object* %obj) > br label %for.incr > > for.incr: > %incr = load i32* %i > %incr3 = add i32 %incr, > store i32 %incr3, i32* %i > br label %for.end > > for.end: > %iload = load i32* %i > %xcmp = icmp sge i32 %iload, 100000000 > br i1 %xcmp, label %xend, label %for.body > > xend: > ret void > } > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
Maybe Matching Threads
- [LLVMdev] Folding vector instructions
- [LLVMdev] Proposal: Loads/stores with deterministic trap/unwind behavior
- [LLVMdev] Why clang inlines with -O3 flag and opt doesn't?
- [LLVMdev] Proposal: Loads/stores with deterministic trap/unwind behavior
- [LLVMdev] Possible miscompilation?