Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 1 | //===------ LoopGenerators.cpp - IR helper to create loops ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 10 | // This file contains functions to create scalar and parallel loops as LLVM-IR. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 14 | #include "polly/ScopDetection.h" |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 15 | #include "polly/CodeGen/LoopGenerators.h" |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 535d52c | 2013-01-02 11:47:44 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | e87c6a8 | 2014-01-13 09:56:11 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Dominators.h" |
Tobias Grosser | 8362818 | 2013-05-07 08:11:54 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Johannes Doerfert | 990cd4c | 2014-10-03 19:11:10 +0000 | [diff] [blame] | 21 | #include "llvm/Support/CommandLine.h" |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 24 | using namespace polly; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 25 | |
Johannes Doerfert | 990cd4c | 2014-10-03 19:11:10 +0000 | [diff] [blame] | 26 | static cl::opt<int> |
| 27 | PollyNumThreads("polly-num-threads", |
| 28 | cl::desc("Number of threads to use (0 = auto)"), cl::Hidden, |
| 29 | cl::init(0)); |
| 30 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 31 | // We generate a loop of either of the following structures: |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 32 | // |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 33 | // BeforeBB BeforeBB |
| 34 | // | | |
| 35 | // v v |
| 36 | // GuardBB PreHeaderBB |
| 37 | // / | | _____ |
| 38 | // __ PreHeaderBB | v \/ | |
| 39 | // / \ / | HeaderBB latch |
| 40 | // latch HeaderBB | |\ | |
| 41 | // \ / \ / | \------/ |
| 42 | // < \ / | |
| 43 | // \ / v |
| 44 | // ExitBB ExitBB |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 45 | // |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 46 | // depending on whether or not we know that it is executed at least once. If |
| 47 | // not, GuardBB checks if the loop is executed at least once. If this is the |
| 48 | // case we branch to PreHeaderBB and subsequently to the HeaderBB, which |
| 49 | // contains the loop iv 'polly.indvar', the incremented loop iv |
| 50 | // 'polly.indvar_next' as well as the condition to check if we execute another |
| 51 | // iteration of the loop. After the loop has finished, we branch to ExitBB. |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 52 | Value *polly::createLoop(Value *LB, Value *UB, Value *Stride, |
Johannes Doerfert | 2ef3f4f | 2014-08-07 17:14:54 +0000 | [diff] [blame] | 53 | PollyIRBuilder &Builder, Pass *P, LoopInfo &LI, |
| 54 | DominatorTree &DT, BasicBlock *&ExitBB, |
Tobias Grosser | 37c9b8e | 2014-03-04 14:59:00 +0000 | [diff] [blame] | 55 | ICmpInst::Predicate Predicate, |
Johannes Doerfert | 51d1c74 | 2014-10-02 15:32:17 +0000 | [diff] [blame] | 56 | ScopAnnotator *Annotator, bool Parallel, |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 57 | bool UseGuard) { |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 58 | Function *F = Builder.GetInsertBlock()->getParent(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 59 | LLVMContext &Context = F->getContext(); |
| 60 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 61 | assert(LB->getType() == UB->getType() && "Types of loop bounds do not match"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 62 | IntegerType *LoopIVType = dyn_cast<IntegerType>(UB->getType()); |
| 63 | assert(LoopIVType && "UB is not integer?"); |
| 64 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 65 | BasicBlock *BeforeBB = Builder.GetInsertBlock(); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 66 | BasicBlock *GuardBB = |
| 67 | UseGuard ? BasicBlock::Create(Context, "polly.loop_if", F) : nullptr; |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 68 | BasicBlock *HeaderBB = BasicBlock::Create(Context, "polly.loop_header", F); |
| 69 | BasicBlock *PreHeaderBB = |
| 70 | BasicBlock::Create(Context, "polly.loop_preheader", F); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 71 | |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 72 | // Update LoopInfo |
| 73 | Loop *OuterLoop = LI.getLoopFor(BeforeBB); |
| 74 | Loop *NewLoop = new Loop(); |
| 75 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 76 | if (OuterLoop) |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 77 | OuterLoop->addChildLoop(NewLoop); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 78 | else |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 79 | LI.addTopLevelLoop(NewLoop); |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 80 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 81 | if (OuterLoop && GuardBB) |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 82 | OuterLoop->addBasicBlockToLoop(GuardBB, LI.getBase()); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 83 | else if (OuterLoop) |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 84 | OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI.getBase()); |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 85 | |
| 86 | NewLoop->addBasicBlockToLoop(HeaderBB, LI.getBase()); |
| 87 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 88 | // Notify the annotator (if present) that we have a new loop, but only |
| 89 | // after the header block is set. |
| 90 | if (Annotator) |
| 91 | Annotator->pushLoop(NewLoop, Parallel); |
| 92 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 93 | // ExitBB |
| 94 | ExitBB = SplitBlock(BeforeBB, Builder.GetInsertPoint()++, P); |
| 95 | ExitBB->setName("polly.loop_exit"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 96 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 97 | // BeforeBB |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 98 | if (GuardBB) { |
| 99 | BeforeBB->getTerminator()->setSuccessor(0, GuardBB); |
| 100 | DT.addNewBlock(GuardBB, BeforeBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 101 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 102 | // GuardBB |
| 103 | Builder.SetInsertPoint(GuardBB); |
| 104 | Value *LoopGuard; |
| 105 | LoopGuard = Builder.CreateICmp(Predicate, LB, UB); |
| 106 | LoopGuard->setName("polly.loop_guard"); |
| 107 | Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB); |
| 108 | DT.addNewBlock(PreHeaderBB, GuardBB); |
| 109 | } else { |
| 110 | BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB); |
| 111 | DT.addNewBlock(PreHeaderBB, BeforeBB); |
| 112 | } |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 113 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 114 | // PreHeaderBB |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 115 | Builder.SetInsertPoint(PreHeaderBB); |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 116 | Builder.CreateBr(HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 117 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 118 | // HeaderBB |
| 119 | DT.addNewBlock(HeaderBB, PreHeaderBB); |
| 120 | Builder.SetInsertPoint(HeaderBB); |
| 121 | PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar"); |
| 122 | IV->addIncoming(LB, PreHeaderBB); |
| 123 | Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); |
| 124 | Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next"); |
| 125 | Value *LoopCondition; |
| 126 | UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub"); |
| 127 | LoopCondition = Builder.CreateICmp(Predicate, IV, UB); |
| 128 | LoopCondition->setName("polly.loop_cond"); |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 129 | |
| 130 | // Create the loop latch and annotate it as such. |
| 131 | BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB); |
| 132 | if (Annotator) |
| 133 | Annotator->annotateLoopLatch(B, NewLoop, Parallel); |
| 134 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 135 | IV->addIncoming(IncrementedIV, HeaderBB); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 136 | if (GuardBB) |
| 137 | DT.changeImmediateDominator(ExitBB, GuardBB); |
| 138 | else |
Tobias Grosser | f8a678d | 2014-09-28 22:40:36 +0000 | [diff] [blame] | 139 | DT.changeImmediateDominator(ExitBB, HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 140 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 141 | // The loop body should be added here. |
| 142 | Builder.SetInsertPoint(HeaderBB->getFirstNonPHI()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 143 | return IV; |
| 144 | } |
| 145 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 146 | Value *ParallelLoopGenerator::createParallelLoop( |
| 147 | Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues, |
| 148 | ValueToValueMapTy &Map, BasicBlock::iterator *LoopBody) { |
| 149 | Value *Struct, *IV, *SubFnParam; |
| 150 | Function *SubFn; |
| 151 | |
| 152 | Struct = storeValuesIntoStruct(UsedValues); |
| 153 | |
| 154 | BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint(); |
| 155 | IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn); |
| 156 | *LoopBody = Builder.GetInsertPoint(); |
| 157 | Builder.SetInsertPoint(BeforeLoop); |
| 158 | |
| 159 | SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), |
| 160 | "polly.par.userContext"); |
| 161 | |
| 162 | // Add one as the upper bound provided by openmp is a < comparison |
| 163 | // whereas the codegenForSequential function creates a <= comparison. |
| 164 | UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1)); |
| 165 | |
| 166 | // Tell the runtime we start a parallel loop |
| 167 | createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride); |
| 168 | Builder.CreateCall(SubFn, SubFnParam); |
| 169 | createCallJoinThreads(); |
| 170 | |
Johannes Doerfert | 1356ac7 | 2014-10-03 19:12:05 +0000 | [diff] [blame^] | 171 | // Mark the end of the lifetime for the parameter struct. |
| 172 | Type *Ty = Struct->getType(); |
| 173 | ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty)); |
| 174 | Builder.CreateLifetimeEnd(Struct, SizeOf); |
| 175 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 176 | return IV; |
| 177 | } |
| 178 | |
| 179 | void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn, |
| 180 | Value *SubFnParam, Value *LB, |
| 181 | Value *UB, Value *Stride) { |
| 182 | const std::string Name = "GOMP_parallel_loop_runtime_start"; |
| 183 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 184 | Function *F = M->getFunction(Name); |
| 185 | |
| 186 | // If F is not available, declare it. |
| 187 | if (!F) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 188 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 189 | |
Tobias Grosser | 45bac0d | 2014-03-02 17:05:21 +0000 | [diff] [blame] | 190 | Type *Params[] = {PointerType::getUnqual(FunctionType::get( |
| 191 | Builder.getVoidTy(), Builder.getInt8PtrTy(), false)), |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 192 | Builder.getInt8PtrTy(), LongType, LongType, LongType, |
| 193 | LongType}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 194 | |
| 195 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); |
| 196 | F = Function::Create(Ty, Linkage, Name, M); |
| 197 | } |
| 198 | |
Johannes Doerfert | 990cd4c | 2014-10-03 19:11:10 +0000 | [diff] [blame] | 199 | Value *NumberOfThreads = ConstantInt::get(LongType, PollyNumThreads); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 200 | Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 201 | |
| 202 | Builder.CreateCall(F, Args); |
| 203 | } |
| 204 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 205 | Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr, |
| 206 | Value *UBPtr) { |
| 207 | const std::string Name = "GOMP_loop_runtime_next"; |
| 208 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 209 | Function *F = M->getFunction(Name); |
| 210 | |
| 211 | // If F is not available, declare it. |
| 212 | if (!F) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 213 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 214 | Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 215 | FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); |
| 216 | F = Function::Create(Ty, Linkage, Name, M); |
| 217 | } |
| 218 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 219 | Value *Args[] = {LBPtr, UBPtr}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 220 | Value *Return = Builder.CreateCall(F, Args); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 221 | Return = Builder.CreateICmpNE( |
| 222 | Return, Builder.CreateZExt(Builder.getFalse(), Return->getType())); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 223 | return Return; |
| 224 | } |
| 225 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 226 | void ParallelLoopGenerator::createCallJoinThreads() { |
| 227 | const std::string Name = "GOMP_parallel_end"; |
| 228 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 229 | Function *F = M->getFunction(Name); |
| 230 | |
| 231 | // If F is not available, declare it. |
| 232 | if (!F) { |
| 233 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 234 | |
| 235 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 236 | F = Function::Create(Ty, Linkage, Name, M); |
| 237 | } |
| 238 | |
| 239 | Builder.CreateCall(F); |
| 240 | } |
| 241 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 242 | void ParallelLoopGenerator::createCallCleanupThread() { |
| 243 | const std::string Name = "GOMP_loop_end_nowait"; |
| 244 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 245 | Function *F = M->getFunction(Name); |
| 246 | |
| 247 | // If F is not available, declare it. |
| 248 | if (!F) { |
| 249 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 250 | |
| 251 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 252 | F = Function::Create(Ty, Linkage, Name, M); |
| 253 | } |
| 254 | |
| 255 | Builder.CreateCall(F); |
| 256 | } |
| 257 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 258 | Function *ParallelLoopGenerator::createSubFnDefinition() { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 259 | Function *F = Builder.GetInsertBlock()->getParent(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 260 | std::vector<Type *> Arguments(1, Builder.getInt8PtrTy()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 261 | FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 262 | Function *SubFn = Function::Create(FT, Function::InternalLinkage, |
| 263 | F->getName() + ".polly.subfn", M); |
| 264 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 265 | // Do not run any polly pass on the new function. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 266 | SubFn->addFnAttr(PollySkipFnAttr); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 267 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 268 | Function::arg_iterator AI = SubFn->arg_begin(); |
| 269 | AI->setName("polly.par.userContext"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 270 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 271 | return SubFn; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 274 | Value * |
| 275 | ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) { |
| 276 | SmallVector<Type *, 8> Members; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 277 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 278 | for (Value *V : Values) |
| 279 | Members.push_back(V->getType()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 280 | |
Johannes Doerfert | 1356ac7 | 2014-10-03 19:12:05 +0000 | [diff] [blame^] | 281 | // We do not want to allocate the alloca inside any loop, thus we allocate it |
| 282 | // in the entry block of the function and use annotations to denote the actual |
| 283 | // live span (similar to clang). |
| 284 | BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock(); |
| 285 | Instruction *IP = EntryBB.getFirstInsertionPt(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 286 | StructType *Ty = StructType::get(Builder.getContext(), Members); |
Johannes Doerfert | 1356ac7 | 2014-10-03 19:12:05 +0000 | [diff] [blame^] | 287 | Value *Struct = new AllocaInst(Ty, 0, "polly.par.userContext", IP); |
| 288 | |
| 289 | // Mark the start of the lifetime for the parameter struct. |
| 290 | ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty)); |
| 291 | Builder.CreateLifetimeStart(Struct, SizeOf); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 292 | |
| 293 | for (unsigned i = 0; i < Values.size(); i++) { |
| 294 | Value *Address = Builder.CreateStructGEP(Struct, i); |
| 295 | Builder.CreateStore(Values[i], Address); |
| 296 | } |
| 297 | |
| 298 | return Struct; |
| 299 | } |
| 300 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 301 | void ParallelLoopGenerator::extractValuesFromStruct( |
| 302 | SetVector<Value *> OldValues, Value *Struct, ValueToValueMapTy &Map) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 303 | for (unsigned i = 0; i < OldValues.size(); i++) { |
| 304 | Value *Address = Builder.CreateStructGEP(Struct, i); |
| 305 | Value *NewValue = Builder.CreateLoad(Address); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 306 | Map[OldValues[i]] = NewValue; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 310 | Value *ParallelLoopGenerator::createSubFn(Value *Stride, Value *StructData, |
| 311 | SetVector<Value *> Data, |
| 312 | ValueToValueMapTy &Map, |
| 313 | Function **SubFnPtr) { |
| 314 | BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB; |
| 315 | Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV; |
| 316 | Function *SubFn = createSubFnDefinition(); |
| 317 | LLVMContext &Context = SubFn->getContext(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 318 | |
| 319 | // Store the previous basic block. |
| 320 | PrevBB = Builder.GetInsertBlock(); |
| 321 | |
| 322 | // Create basic blocks. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 323 | HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn); |
| 324 | ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn); |
| 325 | CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn); |
| 326 | PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 327 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 328 | DT.addNewBlock(HeaderBB, PrevBB); |
| 329 | DT.addNewBlock(ExitBB, HeaderBB); |
| 330 | DT.addNewBlock(CheckNextBB, HeaderBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 331 | DT.addNewBlock(PreHeaderBB, HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 332 | |
| 333 | // Fill up basic block HeaderBB. |
| 334 | Builder.SetInsertPoint(HeaderBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 335 | LBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.LBPtr"); |
| 336 | UBPtr = Builder.CreateAlloca(LongType, 0, "polly.par.UBPtr"); |
| 337 | UserContext = Builder.CreateBitCast(SubFn->arg_begin(), StructData->getType(), |
| 338 | "polly.par.userContext"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 339 | |
| 340 | extractValuesFromStruct(Data, UserContext, Map); |
| 341 | Builder.CreateBr(CheckNextBB); |
| 342 | |
| 343 | // Add code to check if another set of iterations will be executed. |
| 344 | Builder.SetInsertPoint(CheckNextBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 345 | Ret1 = createCallGetWorkItem(LBPtr, UBPtr); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 346 | HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 347 | "polly.par.hasNextScheduleBlock"); |
| 348 | Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 349 | |
| 350 | // Add code to to load the iv bounds for this set of iterations. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 351 | Builder.SetInsertPoint(PreHeaderBB); |
| 352 | LB = Builder.CreateLoad(LBPtr, "polly.par.LB"); |
| 353 | UB = Builder.CreateLoad(UBPtr, "polly.par.UB"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 354 | |
| 355 | // Subtract one as the upper bound provided by openmp is a < comparison |
| 356 | // whereas the codegenForSequential function creates a <= comparison. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 357 | UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1), |
| 358 | "polly.par.UBAdjusted"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 359 | |
| 360 | Builder.CreateBr(CheckNextBB); |
| 361 | Builder.SetInsertPoint(--Builder.GetInsertPoint()); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 362 | IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB, |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 363 | ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 364 | |
| 365 | BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 366 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 367 | // Add code to terminate this subfunction. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 368 | Builder.SetInsertPoint(ExitBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 369 | createCallCleanupThread(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 370 | Builder.CreateRetVoid(); |
| 371 | |
| 372 | Builder.SetInsertPoint(LoopBody); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 373 | *SubFnPtr = SubFn; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 374 | |
| 375 | return IV; |
| 376 | } |