Index: lib/CodeGen/IslNodeBuilder.cpp =================================================================== --- lib/CodeGen/IslNodeBuilder.cpp +++ lib/CodeGen/IslNodeBuilder.cpp @@ -78,6 +78,11 @@ cl::desc("The size of the first level cache line size specified in bytes."), cl::Hidden, cl::init(64), cl::ZeroOrMore, cl::cat(PollyCategory)); +static cl::opt PollyAllocateArraysHeap( + "polly-arrays-heap", + cl::desc("Allocate arrays on the heap and not on the stack."), cl::Hidden, + cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); + __isl_give isl_ast_expr * IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For, ICmpInst::Predicate &Predicate) { @@ -1384,10 +1389,23 @@ } void IslNodeBuilder::allocateNewArrays() { + for (auto &SAI : S.arrays()) { if (SAI->getBasePtr()) continue; + // errs() << "###################\n"; + // SAI->print(errs()); + // errs() << SAI->getNumberOfDimensions() << "\n"; + // for (int i = SAI->getNumberOfDimensions() - 1; i >= 0; i--) { + // auto *DimSize = SAI->getDimensionSize(i); + // unsigned UnsignedDimSize = static_cast(DimSize) + // ->getAPInt() + // .getLimitedValue(); + // errs() << UnsignedDimSize << "\n"; + // } + // errs() << "###################\n"; + assert(SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) && "The size of the outermost dimension is used to declare newly " "created arrays that require memory allocation."); @@ -1407,10 +1425,62 @@ auto InstIt = Builder.GetInsertBlock()->getParent()->getEntryBlock().getTerminator(); - auto *CreatedArray = new AllocaInst(NewArrayType, DL.getAllocaAddrSpace(), - SAI->getName(), &*InstIt); - CreatedArray->setAlignment(PollyTargetFirstLevelCacheLineSize); - SAI->setBasePtr(CreatedArray); + + if (PollyAllocateArraysHeap) { + + // Instruction * InsertBefore, => Instr before which insert, here + // &*InstIt Type * IntPtrTy, Type * AllocTy, => Type + // allocated, here NewArrayType Value * AllocSize, Value * + // ArraySize = nullptr, => size of the Array Function * MallocF = + // nullptr, const Twine & Name = "" => name, here SAI->getName() + + // 01299 unsigned TypeSize = DL.getTypeAllocSize(FieldTy); + // 01300 if (StructType *ST = dyn_cast(FieldTy)) + // 01301 TypeSize = DL.getStructLayout(ST)->getSizeInBytes(); + // 01302 Type *IntPtrTy = DL.getIntPtrType(CI->getType()); + // 01303 Value *NMI = CallInst::CreateMalloc(CI, IntPtrTy, FieldTy, + // 01304 ConstantInt::get(IntPtrTy, TypeSize), 01305 + // NElems, nullptr, 01306 + // CI->getName() + ".f" + Twine(FieldNo)); + + errs() << "##### DEBUG ###### \n"; + SAI->print(errs()); + NewArrayType->print(errs()); + errs() << "\n"; + errs() << NewArrayType->getTypeID(); + errs() << "\n"; + errs() << "##### FIN ####### \n"; + + // Get the 'int' pointer for the type of the array + auto IntPtrTy = DL.getIntPtrType(NewArrayType); + + // Get the size of this pointer (why so complicated ?) + unsigned TypeSize = DL.getTypeAllocSize(NewArrayType); + if (StructType *ST = dyn_cast(NewArrayType)) + TypeSize = DL.getStructLayout(ST)->getSizeInBytes(); + + // Get the size of the array = TypeSize*size(dim_1)*...*size(dim_n) + unsigned array_size_int = TypeSize; + for (int i = SAI->getNumberOfDimensions() - 1; i >= 0; i--) { + auto *DimSize = SAI->getDimensionSize(i); + unsigned UnsignedDimSize = static_cast(DimSize) + ->getAPInt() + .getLimitedValue(); + array_size_int *= UnsignedDimSize; + } + + auto *CreatedArray = CallInst::CreateMalloc( + &*InstIt, IntPtrTy, NewArrayType, + ConstantInt::get(IntPtrTy, TypeSize), + // IntPtrty is not the right type + ConstantInt::get(IntPtrTy, array_size_int), nullptr, SAI->getName()); + SAI->setBasePtr(CreatedArray); + } else { + auto *CreatedArray = new AllocaInst(NewArrayType, DL.getAllocaAddrSpace(), + SAI->getName(), &*InstIt); + CreatedArray->setAlignment(PollyTargetFirstLevelCacheLineSize); + SAI->setBasePtr(CreatedArray); + } } }