Index: lib/CodeGen/LoopGenerators.cpp =================================================================== --- lib/CodeGen/LoopGenerators.cpp +++ lib/CodeGen/LoopGenerators.cpp @@ -172,6 +172,11 @@ Builder.CreateCall(SubFn, SubFnParam); createCallJoinThreads(); + // Mark the end of the lifetime for the parameter struct + Type *Ty = Struct->getType(); + ConstantInt *SizeOf = dyn_cast(ConstantExpr::getSizeOf(Ty)); + Builder.CreateLifetimeEnd(Struct, SizeOf); + return IV; } @@ -280,9 +285,22 @@ for (Value *V : Values) Members.push_back(V->getType()); + // We do not want to allocate the alloca inside any loop, thus we allocate it + // in the entry block of the function and use annotations to denote the actual + // live span (similar to clang). + // + // XXX: It might be necessary to allocate the struct explicitly in front of + // the outermost loop surrounding the current insertion block in order + // to prevent us from allocating stack space without actually using it. + // For now we assume the annotations suffice to sink the allocas to the + // most recent dominating basic block which is not part of a loop. + BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock(); + Instruction *IP = EntryBB.getFirstInsertionPt(); StructType *Ty = StructType::get(Builder.getContext(), Members); - Value *Struct = - new AllocaInst(Ty, 0, "polly.par.userContext", Builder.GetInsertPoint()); + Value *Struct = new AllocaInst(Ty, 0, "polly.par.userContext", IP); + + ConstantInt *SizeOf = dyn_cast(ConstantExpr::getSizeOf(Ty)); + Builder.CreateLifetimeStart(Struct, SizeOf); for (unsigned i = 0; i < Values.size(); i++) { Value *Address = Builder.CreateStructGEP(Struct, i);