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 | |
Hongbin Zheng | 8a84661 | 2012-04-25 13:18:28 +0000 | [diff] [blame] | 14 | #include "polly/CodeGen/LoopGenerators.h" |
Tobias Grosser | 5624d3c | 2015-12-21 12:38:56 +0000 | [diff] [blame] | 15 | #include "polly/ScopDetection.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" |
Johannes Doerfert | 990cd4c | 2014-10-03 19:11:10 +0000 | [diff] [blame] | 20 | #include "llvm/Support/CommandLine.h" |
Tobias Grosser | ba0d092 | 2015-05-09 09:13:42 +0000 | [diff] [blame] | 21 | #include "llvm/Transforms/Utils/BasicBlockUtils.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 | |
Tobias Grosser | 154d946 | 2014-11-25 17:09:21 +0000 | [diff] [blame] | 81 | if (OuterLoop) { |
| 82 | if (GuardBB) |
Chandler Carruth | 6adcf56 | 2015-01-18 01:47:30 +0000 | [diff] [blame] | 83 | OuterLoop->addBasicBlockToLoop(GuardBB, LI); |
| 84 | OuterLoop->addBasicBlockToLoop(PreHeaderBB, LI); |
Tobias Grosser | 154d946 | 2014-11-25 17:09:21 +0000 | [diff] [blame] | 85 | } |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 86 | |
Chandler Carruth | 6adcf56 | 2015-01-18 01:47:30 +0000 | [diff] [blame] | 87 | NewLoop->addBasicBlockToLoop(HeaderBB, LI); |
Tobias Grosser | 3081b0f | 2013-05-16 06:40:24 +0000 | [diff] [blame] | 88 | |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 89 | // Notify the annotator (if present) that we have a new loop, but only |
| 90 | // after the header block is set. |
| 91 | if (Annotator) |
| 92 | Annotator->pushLoop(NewLoop, Parallel); |
| 93 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 94 | // ExitBB |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 95 | ExitBB = SplitBlock(BeforeBB, &*Builder.GetInsertPoint(), &DT, &LI); |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 96 | ExitBB->setName("polly.loop_exit"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 97 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 98 | // BeforeBB |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 99 | if (GuardBB) { |
| 100 | BeforeBB->getTerminator()->setSuccessor(0, GuardBB); |
| 101 | DT.addNewBlock(GuardBB, BeforeBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 102 | |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 103 | // GuardBB |
| 104 | Builder.SetInsertPoint(GuardBB); |
| 105 | Value *LoopGuard; |
| 106 | LoopGuard = Builder.CreateICmp(Predicate, LB, UB); |
| 107 | LoopGuard->setName("polly.loop_guard"); |
| 108 | Builder.CreateCondBr(LoopGuard, PreHeaderBB, ExitBB); |
| 109 | DT.addNewBlock(PreHeaderBB, GuardBB); |
| 110 | } else { |
| 111 | BeforeBB->getTerminator()->setSuccessor(0, PreHeaderBB); |
| 112 | DT.addNewBlock(PreHeaderBB, BeforeBB); |
| 113 | } |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 114 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 115 | // PreHeaderBB |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 116 | Builder.SetInsertPoint(PreHeaderBB); |
Hongbin Zheng | 4ac4e15 | 2012-04-23 13:03:56 +0000 | [diff] [blame] | 117 | Builder.CreateBr(HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 118 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 119 | // HeaderBB |
| 120 | DT.addNewBlock(HeaderBB, PreHeaderBB); |
| 121 | Builder.SetInsertPoint(HeaderBB); |
| 122 | PHINode *IV = Builder.CreatePHI(LoopIVType, 2, "polly.indvar"); |
| 123 | IV->addIncoming(LB, PreHeaderBB); |
Tobias Grosser | 3717aa5 | 2016-06-11 19:17:15 +0000 | [diff] [blame] | 124 | Stride = Builder.CreateZExtOrBitCast(Stride, LoopIVType); |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 125 | Value *IncrementedIV = Builder.CreateNSWAdd(IV, Stride, "polly.indvar_next"); |
| 126 | Value *LoopCondition; |
| 127 | UB = Builder.CreateSub(UB, Stride, "polly.adjust_ub"); |
| 128 | LoopCondition = Builder.CreateICmp(Predicate, IV, UB); |
| 129 | LoopCondition->setName("polly.loop_cond"); |
Johannes Doerfert | c7b719f | 2014-10-01 20:10:44 +0000 | [diff] [blame] | 130 | |
| 131 | // Create the loop latch and annotate it as such. |
| 132 | BranchInst *B = Builder.CreateCondBr(LoopCondition, HeaderBB, ExitBB); |
| 133 | if (Annotator) |
| 134 | Annotator->annotateLoopLatch(B, NewLoop, Parallel); |
| 135 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 136 | IV->addIncoming(IncrementedIV, HeaderBB); |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 137 | if (GuardBB) |
| 138 | DT.changeImmediateDominator(ExitBB, GuardBB); |
| 139 | else |
Tobias Grosser | f8a678d | 2014-09-28 22:40:36 +0000 | [diff] [blame] | 140 | DT.changeImmediateDominator(ExitBB, HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 141 | |
Tobias Grosser | 5db6ffd | 2013-05-16 06:40:06 +0000 | [diff] [blame] | 142 | // The loop body should be added here. |
| 143 | Builder.SetInsertPoint(HeaderBB->getFirstNonPHI()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 144 | return IV; |
| 145 | } |
| 146 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 147 | Value *ParallelLoopGenerator::createParallelLoop( |
| 148 | Value *LB, Value *UB, Value *Stride, SetVector<Value *> &UsedValues, |
Johannes Doerfert | 521dd58 | 2015-10-07 20:15:56 +0000 | [diff] [blame] | 149 | ValueMapT &Map, BasicBlock::iterator *LoopBody) { |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 150 | Function *SubFn; |
| 151 | |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 152 | AllocaInst *Struct = storeValuesIntoStruct(UsedValues); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 153 | BasicBlock::iterator BeforeLoop = Builder.GetInsertPoint(); |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 154 | Value *IV = createSubFn(Stride, Struct, UsedValues, Map, &SubFn); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 155 | *LoopBody = Builder.GetInsertPoint(); |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 156 | Builder.SetInsertPoint(&*BeforeLoop); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 157 | |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 158 | Value *SubFnParam = Builder.CreateBitCast(Struct, Builder.getInt8PtrTy(), |
| 159 | "polly.par.userContext"); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 160 | |
| 161 | // Add one as the upper bound provided by openmp is a < comparison |
| 162 | // whereas the codegenForSequential function creates a <= comparison. |
| 163 | UB = Builder.CreateAdd(UB, ConstantInt::get(LongType, 1)); |
| 164 | |
| 165 | // Tell the runtime we start a parallel loop |
| 166 | createCallSpawnThreads(SubFn, SubFnParam, LB, UB, Stride); |
| 167 | Builder.CreateCall(SubFn, SubFnParam); |
| 168 | createCallJoinThreads(); |
| 169 | |
Johannes Doerfert | 1356ac7 | 2014-10-03 19:12:05 +0000 | [diff] [blame] | 170 | // Mark the end of the lifetime for the parameter struct. |
| 171 | Type *Ty = Struct->getType(); |
| 172 | ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty)); |
| 173 | Builder.CreateLifetimeEnd(Struct, SizeOf); |
| 174 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 175 | return IV; |
| 176 | } |
| 177 | |
| 178 | void ParallelLoopGenerator::createCallSpawnThreads(Value *SubFn, |
| 179 | Value *SubFnParam, Value *LB, |
| 180 | Value *UB, Value *Stride) { |
| 181 | const std::string Name = "GOMP_parallel_loop_runtime_start"; |
| 182 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 183 | Function *F = M->getFunction(Name); |
| 184 | |
| 185 | // If F is not available, declare it. |
| 186 | if (!F) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 187 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 188 | |
Tobias Grosser | 45bac0d | 2014-03-02 17:05:21 +0000 | [diff] [blame] | 189 | Type *Params[] = {PointerType::getUnqual(FunctionType::get( |
| 190 | Builder.getVoidTy(), Builder.getInt8PtrTy(), false)), |
Johannes Doerfert | a441783 | 2014-10-03 20:40:24 +0000 | [diff] [blame] | 191 | Builder.getInt8PtrTy(), Builder.getInt32Ty(), LongType, |
| 192 | LongType, LongType}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 193 | |
| 194 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Params, false); |
| 195 | F = Function::Create(Ty, Linkage, Name, M); |
| 196 | } |
| 197 | |
Johannes Doerfert | a441783 | 2014-10-03 20:40:24 +0000 | [diff] [blame] | 198 | Value *NumberOfThreads = Builder.getInt32(PollyNumThreads); |
Tobias Grosser | d4ea2f4 | 2015-05-11 13:43:04 +0000 | [diff] [blame] | 199 | Value *Args[] = {SubFn, SubFnParam, NumberOfThreads, LB, UB, Stride}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 200 | |
| 201 | Builder.CreateCall(F, Args); |
| 202 | } |
| 203 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 204 | Value *ParallelLoopGenerator::createCallGetWorkItem(Value *LBPtr, |
| 205 | Value *UBPtr) { |
| 206 | const std::string Name = "GOMP_loop_runtime_next"; |
| 207 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 208 | Function *F = M->getFunction(Name); |
| 209 | |
| 210 | // If F is not available, declare it. |
| 211 | if (!F) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 212 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 213 | Type *Params[] = {LongType->getPointerTo(), LongType->getPointerTo()}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 214 | FunctionType *Ty = FunctionType::get(Builder.getInt8Ty(), Params, false); |
| 215 | F = Function::Create(Ty, Linkage, Name, M); |
| 216 | } |
| 217 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 218 | Value *Args[] = {LBPtr, UBPtr}; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 219 | Value *Return = Builder.CreateCall(F, Args); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 220 | Return = Builder.CreateICmpNE( |
| 221 | Return, Builder.CreateZExt(Builder.getFalse(), Return->getType())); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 222 | return Return; |
| 223 | } |
| 224 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 225 | void ParallelLoopGenerator::createCallJoinThreads() { |
| 226 | const std::string Name = "GOMP_parallel_end"; |
| 227 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 228 | Function *F = M->getFunction(Name); |
| 229 | |
| 230 | // If F is not available, declare it. |
| 231 | if (!F) { |
| 232 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 233 | |
| 234 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 235 | F = Function::Create(Ty, Linkage, Name, M); |
| 236 | } |
| 237 | |
Tobias Grosser | 1128b36 | 2015-05-19 06:25:02 +0000 | [diff] [blame] | 238 | Builder.CreateCall(F, {}); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 241 | void ParallelLoopGenerator::createCallCleanupThread() { |
| 242 | const std::string Name = "GOMP_loop_end_nowait"; |
| 243 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 244 | Function *F = M->getFunction(Name); |
| 245 | |
| 246 | // If F is not available, declare it. |
| 247 | if (!F) { |
| 248 | GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage; |
| 249 | |
| 250 | FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), false); |
| 251 | F = Function::Create(Ty, Linkage, Name, M); |
| 252 | } |
| 253 | |
Tobias Grosser | 1128b36 | 2015-05-19 06:25:02 +0000 | [diff] [blame] | 254 | Builder.CreateCall(F, {}); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 257 | Function *ParallelLoopGenerator::createSubFnDefinition() { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 258 | Function *F = Builder.GetInsertBlock()->getParent(); |
Tobias Grosser | c14582f | 2013-02-05 18:01:29 +0000 | [diff] [blame] | 259 | std::vector<Type *> Arguments(1, Builder.getInt8PtrTy()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 260 | FunctionType *FT = FunctionType::get(Builder.getVoidTy(), Arguments, false); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 261 | Function *SubFn = Function::Create(FT, Function::InternalLinkage, |
Tobias Grosser | a89dc57 | 2015-09-08 06:22:17 +0000 | [diff] [blame] | 262 | F->getName() + "_polly_subfn", M); |
| 263 | |
| 264 | // Certain backends (e.g., NVPTX) do not support '.'s in function names. |
| 265 | // Hence, we ensure that all '.'s are replaced by '_'s. |
| 266 | std::string FunctionName = SubFn->getName(); |
| 267 | std::replace(FunctionName.begin(), FunctionName.end(), '.', '_'); |
| 268 | SubFn->setName(FunctionName); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 269 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 270 | // Do not run any polly pass on the new function. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 271 | SubFn->addFnAttr(PollySkipFnAttr); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 272 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 273 | Function::arg_iterator AI = SubFn->arg_begin(); |
| 274 | AI->setName("polly.par.userContext"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 275 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 276 | return SubFn; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 277 | } |
| 278 | |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 279 | AllocaInst * |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 280 | ParallelLoopGenerator::storeValuesIntoStruct(SetVector<Value *> &Values) { |
| 281 | SmallVector<Type *, 8> Members; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 282 | |
Tobias Grosser | 91f5b26 | 2014-06-04 08:06:40 +0000 | [diff] [blame] | 283 | for (Value *V : Values) |
| 284 | Members.push_back(V->getType()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 285 | |
Johannes Doerfert | 1356ac7 | 2014-10-03 19:12:05 +0000 | [diff] [blame] | 286 | // We do not want to allocate the alloca inside any loop, thus we allocate it |
| 287 | // in the entry block of the function and use annotations to denote the actual |
| 288 | // live span (similar to clang). |
| 289 | BasicBlock &EntryBB = Builder.GetInsertBlock()->getParent()->getEntryBlock(); |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 290 | Instruction *IP = &*EntryBB.getFirstInsertionPt(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 291 | StructType *Ty = StructType::get(Builder.getContext(), Members); |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 292 | AllocaInst *Struct = new AllocaInst(Ty, nullptr, "polly.par.userContext", IP); |
Johannes Doerfert | 1356ac7 | 2014-10-03 19:12:05 +0000 | [diff] [blame] | 293 | |
| 294 | // Mark the start of the lifetime for the parameter struct. |
| 295 | ConstantInt *SizeOf = Builder.getInt64(DL.getTypeAllocSize(Ty)); |
| 296 | Builder.CreateLifetimeStart(Struct, SizeOf); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 297 | |
| 298 | for (unsigned i = 0; i < Values.size(); i++) { |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 299 | Value *Address = Builder.CreateStructGEP(Ty, Struct, i); |
Tobias Grosser | 95e59aa | 2015-09-28 16:46:38 +0000 | [diff] [blame] | 300 | Address->setName("polly.subfn.storeaddr." + Values[i]->getName()); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 301 | Builder.CreateStore(Values[i], Address); |
| 302 | } |
| 303 | |
| 304 | return Struct; |
| 305 | } |
| 306 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 307 | void ParallelLoopGenerator::extractValuesFromStruct( |
Johannes Doerfert | 521dd58 | 2015-10-07 20:15:56 +0000 | [diff] [blame] | 308 | SetVector<Value *> OldValues, Type *Ty, Value *Struct, ValueMapT &Map) { |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 309 | for (unsigned i = 0; i < OldValues.size(); i++) { |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 310 | Value *Address = Builder.CreateStructGEP(Ty, Struct, i); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 311 | Value *NewValue = Builder.CreateLoad(Address); |
Tobias Grosser | 72b8067 | 2015-09-05 10:41:19 +0000 | [diff] [blame] | 312 | NewValue->setName("polly.subfunc.arg." + OldValues[i]->getName()); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 313 | Map[OldValues[i]] = NewValue; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 317 | Value *ParallelLoopGenerator::createSubFn(Value *Stride, AllocaInst *StructData, |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 318 | SetVector<Value *> Data, |
Johannes Doerfert | 521dd58 | 2015-10-07 20:15:56 +0000 | [diff] [blame] | 319 | ValueMapT &Map, Function **SubFnPtr) { |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 320 | BasicBlock *PrevBB, *HeaderBB, *ExitBB, *CheckNextBB, *PreHeaderBB, *AfterBB; |
| 321 | Value *LBPtr, *UBPtr, *UserContext, *Ret1, *HasNextSchedule, *LB, *UB, *IV; |
| 322 | Function *SubFn = createSubFnDefinition(); |
| 323 | LLVMContext &Context = SubFn->getContext(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 324 | |
| 325 | // Store the previous basic block. |
| 326 | PrevBB = Builder.GetInsertBlock(); |
| 327 | |
| 328 | // Create basic blocks. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 329 | HeaderBB = BasicBlock::Create(Context, "polly.par.setup", SubFn); |
| 330 | ExitBB = BasicBlock::Create(Context, "polly.par.exit", SubFn); |
| 331 | CheckNextBB = BasicBlock::Create(Context, "polly.par.checkNext", SubFn); |
| 332 | PreHeaderBB = BasicBlock::Create(Context, "polly.par.loadIVBounds", SubFn); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 333 | |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 334 | DT.addNewBlock(HeaderBB, PrevBB); |
| 335 | DT.addNewBlock(ExitBB, HeaderBB); |
| 336 | DT.addNewBlock(CheckNextBB, HeaderBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 337 | DT.addNewBlock(PreHeaderBB, HeaderBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 338 | |
| 339 | // Fill up basic block HeaderBB. |
| 340 | Builder.SetInsertPoint(HeaderBB); |
Tobias Grosser | 8dd653d | 2016-06-22 16:22:00 +0000 | [diff] [blame] | 341 | LBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.LBPtr"); |
| 342 | UBPtr = Builder.CreateAlloca(LongType, nullptr, "polly.par.UBPtr"); |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 343 | UserContext = Builder.CreateBitCast( |
| 344 | &*SubFn->arg_begin(), StructData->getType(), "polly.par.userContext"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 345 | |
David Blaikie | f0e3d50 | 2015-04-05 22:51:12 +0000 | [diff] [blame] | 346 | extractValuesFromStruct(Data, StructData->getAllocatedType(), UserContext, |
| 347 | Map); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 348 | Builder.CreateBr(CheckNextBB); |
| 349 | |
| 350 | // Add code to check if another set of iterations will be executed. |
| 351 | Builder.SetInsertPoint(CheckNextBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 352 | Ret1 = createCallGetWorkItem(LBPtr, UBPtr); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 353 | HasNextSchedule = Builder.CreateTrunc(Ret1, Builder.getInt1Ty(), |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 354 | "polly.par.hasNextScheduleBlock"); |
| 355 | Builder.CreateCondBr(HasNextSchedule, PreHeaderBB, ExitBB); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 356 | |
Tobias Grosser | d8e3c8c | 2015-08-19 14:22:48 +0000 | [diff] [blame] | 357 | // Add code to load the iv bounds for this set of iterations. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 358 | Builder.SetInsertPoint(PreHeaderBB); |
| 359 | LB = Builder.CreateLoad(LBPtr, "polly.par.LB"); |
| 360 | UB = Builder.CreateLoad(UBPtr, "polly.par.UB"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 361 | |
| 362 | // Subtract one as the upper bound provided by openmp is a < comparison |
| 363 | // whereas the codegenForSequential function creates a <= comparison. |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 364 | UB = Builder.CreateSub(UB, ConstantInt::get(LongType, 1), |
| 365 | "polly.par.UBAdjusted"); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 366 | |
| 367 | Builder.CreateBr(CheckNextBB); |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 368 | Builder.SetInsertPoint(&*--Builder.GetInsertPoint()); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 369 | IV = createLoop(LB, UB, Stride, Builder, P, LI, DT, AfterBB, |
Johannes Doerfert | dd5c144 | 2014-09-10 17:33:32 +0000 | [diff] [blame] | 370 | ICmpInst::ICMP_SLE, nullptr, true, /* UseGuard */ false); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 371 | |
| 372 | BasicBlock::iterator LoopBody = Builder.GetInsertPoint(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 373 | |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 374 | // Add code to terminate this subfunction. |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 375 | Builder.SetInsertPoint(ExitBB); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 376 | createCallCleanupThread(); |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 377 | Builder.CreateRetVoid(); |
| 378 | |
Duncan P. N. Exon Smith | b8f58b5 | 2015-11-06 22:56:54 +0000 | [diff] [blame] | 379 | Builder.SetInsertPoint(&*LoopBody); |
Johannes Doerfert | 12b355a | 2014-10-03 19:10:13 +0000 | [diff] [blame] | 380 | *SubFnPtr = SubFn; |
Tobias Grosser | f74a4cd | 2012-03-23 10:35:18 +0000 | [diff] [blame] | 381 | |
| 382 | return IV; |
| 383 | } |