Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===// |
| 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 | // |
| 10 | // This contains code to emit OpenMP nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "CGOpenMPRuntime.h" |
| 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 17 | #include "TargetInfo.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 18 | #include "clang/AST/Stmt.h" |
| 19 | #include "clang/AST/StmtOpenMP.h" |
| 20 | using namespace clang; |
| 21 | using namespace CodeGen; |
| 22 | |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // OpenMP Directive Emission |
| 25 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 26 | void CodeGenFunction::EmitOMPAggregateAssign( |
| 27 | llvm::Value *DestAddr, llvm::Value *SrcAddr, QualType OriginalType, |
| 28 | const llvm::function_ref<void(llvm::Value *, llvm::Value *)> &CopyGen) { |
| 29 | // Perform element-by-element initialization. |
| 30 | QualType ElementTy; |
| 31 | auto SrcBegin = SrcAddr; |
| 32 | auto DestBegin = DestAddr; |
| 33 | auto ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
| 34 | auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestBegin); |
| 35 | // Cast from pointer to array type to pointer to single element. |
| 36 | SrcBegin = Builder.CreatePointerBitCastOrAddrSpaceCast(SrcBegin, |
| 37 | DestBegin->getType()); |
| 38 | auto DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
| 39 | // The basic structure here is a while-do loop. |
| 40 | auto BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 41 | auto DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 42 | auto IsEmpty = |
| 43 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 44 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 45 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 46 | // Enter the loop body, making that address the current address. |
| 47 | auto EntryBB = Builder.GetInsertBlock(); |
| 48 | EmitBlock(BodyBB); |
| 49 | auto SrcElementCurrent = |
| 50 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 51 | SrcElementCurrent->addIncoming(SrcBegin, EntryBB); |
| 52 | auto DestElementCurrent = Builder.CreatePHI(DestBegin->getType(), 2, |
| 53 | "omp.arraycpy.destElementPast"); |
| 54 | DestElementCurrent->addIncoming(DestBegin, EntryBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 55 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 56 | // Emit copy. |
| 57 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 58 | |
| 59 | // Shift the address forward by one element. |
| 60 | auto DestElementNext = Builder.CreateConstGEP1_32( |
| 61 | DestElementCurrent, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
| 62 | auto SrcElementNext = Builder.CreateConstGEP1_32( |
| 63 | SrcElementCurrent, /*Idx0=*/1, "omp.arraycpy.src.element"); |
| 64 | // Check whether we've reached the end. |
| 65 | auto Done = |
| 66 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 67 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
| 68 | DestElementCurrent->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 69 | SrcElementCurrent->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
| 70 | |
| 71 | // Done. |
| 72 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 73 | } |
| 74 | |
| 75 | void CodeGenFunction::EmitOMPCopy(CodeGenFunction &CGF, |
| 76 | QualType OriginalType, llvm::Value *DestAddr, |
| 77 | llvm::Value *SrcAddr, const VarDecl *DestVD, |
| 78 | const VarDecl *SrcVD, const Expr *Copy) { |
| 79 | if (OriginalType->isArrayType()) { |
| 80 | auto *BO = dyn_cast<BinaryOperator>(Copy); |
| 81 | if (BO && BO->getOpcode() == BO_Assign) { |
| 82 | // Perform simple memcpy for simple copying. |
| 83 | CGF.EmitAggregateAssign(DestAddr, SrcAddr, OriginalType); |
| 84 | } else { |
| 85 | // For arrays with complex element types perform element by element |
| 86 | // copying. |
| 87 | CGF.EmitOMPAggregateAssign( |
| 88 | DestAddr, SrcAddr, OriginalType, |
| 89 | [&CGF, Copy, SrcVD, DestVD](llvm::Value *DestElement, |
| 90 | llvm::Value *SrcElement) { |
| 91 | // Working with the single array element, so have to remap |
| 92 | // destination and source variables to corresponding array |
| 93 | // elements. |
| 94 | CodeGenFunction::OMPPrivateScope Remap(CGF); |
| 95 | Remap.addPrivate(DestVD, [DestElement]() -> llvm::Value *{ |
| 96 | return DestElement; |
| 97 | }); |
| 98 | Remap.addPrivate( |
| 99 | SrcVD, [SrcElement]() -> llvm::Value *{ return SrcElement; }); |
| 100 | (void)Remap.Privatize(); |
| 101 | CGF.EmitIgnoredExpr(Copy); |
| 102 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 103 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 104 | } else { |
| 105 | // Remap pseudo source variable to private copy. |
| 106 | CodeGenFunction::OMPPrivateScope Remap(CGF); |
| 107 | Remap.addPrivate(SrcVD, [SrcAddr]() -> llvm::Value *{ return SrcAddr; }); |
| 108 | Remap.addPrivate(DestVD, [DestAddr]() -> llvm::Value *{ return DestAddr; }); |
| 109 | (void)Remap.Privatize(); |
| 110 | // Emit copying of the whole variable. |
| 111 | CGF.EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 112 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 115 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 116 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 117 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 118 | for (auto &&I = D.getClausesOfKind(OMPC_firstprivate); I; ++I) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 119 | auto *C = cast<OMPFirstprivateClause>(*I); |
| 120 | auto IRef = C->varlist_begin(); |
| 121 | auto InitsRef = C->inits().begin(); |
| 122 | for (auto IInit : C->private_copies()) { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 123 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 124 | if (EmittedAsFirstprivate.count(OrigVD) == 0) { |
| 125 | EmittedAsFirstprivate.insert(OrigVD); |
| 126 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 127 | auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
| 128 | bool IsRegistered; |
| 129 | DeclRefExpr DRE( |
| 130 | const_cast<VarDecl *>(OrigVD), |
| 131 | /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( |
| 132 | OrigVD) != nullptr, |
| 133 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
| 134 | auto *OriginalAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 135 | QualType Type = OrigVD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 136 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 137 | // Emit VarDecl with copy init for arrays. |
| 138 | // Get the address of the original variable captured in current |
| 139 | // captured region. |
| 140 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{ |
| 141 | auto Emission = EmitAutoVarAlloca(*VD); |
| 142 | auto *Init = VD->getInit(); |
| 143 | if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) { |
| 144 | // Perform simple memcpy. |
| 145 | EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr, |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 146 | Type); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 147 | } else { |
| 148 | EmitOMPAggregateAssign( |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 149 | Emission.getAllocatedAddress(), OriginalAddr, Type, |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 150 | [this, VDInit, Init](llvm::Value *DestElement, |
| 151 | llvm::Value *SrcElement) { |
| 152 | // Clean up any temporaries needed by the initialization. |
| 153 | RunCleanupsScope InitScope(*this); |
| 154 | // Emit initialization for single element. |
| 155 | LocalDeclMap[VDInit] = SrcElement; |
| 156 | EmitAnyExprToMem(Init, DestElement, |
| 157 | Init->getType().getQualifiers(), |
| 158 | /*IsInitializer*/ false); |
| 159 | LocalDeclMap.erase(VDInit); |
| 160 | }); |
| 161 | } |
| 162 | EmitAutoVarCleanups(Emission); |
| 163 | return Emission.getAllocatedAddress(); |
| 164 | }); |
| 165 | } else { |
| 166 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{ |
| 167 | // Emit private VarDecl with copy init. |
| 168 | // Remap temp VDInit variable to the address of the original |
| 169 | // variable |
| 170 | // (for proper handling of captured global variables). |
| 171 | LocalDeclMap[VDInit] = OriginalAddr; |
| 172 | EmitDecl(*VD); |
| 173 | LocalDeclMap.erase(VDInit); |
| 174 | return GetAddrOfLocalVar(VD); |
| 175 | }); |
| 176 | } |
| 177 | assert(IsRegistered && |
| 178 | "firstprivate var already registered as private"); |
| 179 | // Silence the warning about unused variable. |
| 180 | (void)IsRegistered; |
| 181 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 182 | ++IRef, ++InitsRef; |
| 183 | } |
| 184 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 185 | return !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 188 | void CodeGenFunction::EmitOMPPrivateClause( |
| 189 | const OMPExecutableDirective &D, |
| 190 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 191 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 192 | for (auto &&I = D.getClausesOfKind(OMPC_private); I; ++I) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 193 | auto *C = cast<OMPPrivateClause>(*I); |
| 194 | auto IRef = C->varlist_begin(); |
| 195 | for (auto IInit : C->private_copies()) { |
| 196 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 197 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 198 | auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 199 | bool IsRegistered = |
| 200 | PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{ |
| 201 | // Emit private VarDecl with copy init. |
| 202 | EmitDecl(*VD); |
| 203 | return GetAddrOfLocalVar(VD); |
| 204 | }); |
| 205 | assert(IsRegistered && "private var already registered as private"); |
| 206 | // Silence the warning about unused variable. |
| 207 | (void)IsRegistered; |
| 208 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 209 | ++IRef; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 214 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
| 215 | // threadprivate_var1 = master_threadprivate_var1; |
| 216 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 217 | // ... |
| 218 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 219 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 220 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 221 | for (auto &&I = D.getClausesOfKind(OMPC_copyin); I; ++I) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 222 | auto *C = cast<OMPCopyinClause>(*I); |
| 223 | auto IRef = C->varlist_begin(); |
| 224 | auto ISrcRef = C->source_exprs().begin(); |
| 225 | auto IDestRef = C->destination_exprs().begin(); |
| 226 | for (auto *AssignOp : C->assignment_ops()) { |
| 227 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 228 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 229 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 230 | |
| 231 | // Get the address of the master variable. If we are emitting code with |
| 232 | // TLS support, the address is passed from the master as field in the |
| 233 | // captured declaration. |
| 234 | llvm::Value *MasterAddr; |
| 235 | if (getLangOpts().OpenMPUseTLS && |
| 236 | getContext().getTargetInfo().isTLSSupported()) { |
| 237 | assert(CapturedStmtInfo->lookup(VD) && |
| 238 | "Copyin threadprivates should have been captured!"); |
| 239 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(), |
| 240 | VK_LValue, (*IRef)->getExprLoc()); |
| 241 | MasterAddr = EmitLValue(&DRE).getAddress(); |
| 242 | } else { |
| 243 | MasterAddr = VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 244 | : CGM.GetAddrOfGlobal(VD); |
| 245 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 246 | // Get the address of the threadprivate variable. |
| 247 | auto *PrivateAddr = EmitLValue(*IRef).getAddress(); |
| 248 | if (CopiedVars.size() == 1) { |
| 249 | // At first check if current thread is a master thread. If it is, no |
| 250 | // need to copy data. |
| 251 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 252 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 253 | Builder.CreateCondBr( |
| 254 | Builder.CreateICmpNE( |
| 255 | Builder.CreatePtrToInt(MasterAddr, CGM.IntPtrTy), |
| 256 | Builder.CreatePtrToInt(PrivateAddr, CGM.IntPtrTy)), |
| 257 | CopyBegin, CopyEnd); |
| 258 | EmitBlock(CopyBegin); |
| 259 | } |
| 260 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 261 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 262 | EmitOMPCopy(*this, Type, PrivateAddr, MasterAddr, DestVD, SrcVD, |
| 263 | AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 264 | } |
| 265 | ++IRef; |
| 266 | ++ISrcRef; |
| 267 | ++IDestRef; |
| 268 | } |
| 269 | } |
| 270 | if (CopyEnd) { |
| 271 | // Exit out of copying procedure for non-master thread. |
| 272 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 273 | return true; |
| 274 | } |
| 275 | return false; |
| 276 | } |
| 277 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 278 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 279 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 280 | bool HasAtLeastOneLastprivate = false; |
| 281 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 282 | for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 283 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 284 | auto *C = cast<OMPLastprivateClause>(*I); |
| 285 | auto IRef = C->varlist_begin(); |
| 286 | auto IDestRef = C->destination_exprs().begin(); |
| 287 | for (auto *IInit : C->private_copies()) { |
| 288 | // Keep the address of the original variable for future update at the end |
| 289 | // of the loop. |
| 290 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 291 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
| 292 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 293 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> llvm::Value *{ |
| 294 | DeclRefExpr DRE( |
| 295 | const_cast<VarDecl *>(OrigVD), |
| 296 | /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( |
| 297 | OrigVD) != nullptr, |
| 298 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
| 299 | return EmitLValue(&DRE).getAddress(); |
| 300 | }); |
| 301 | // Check if the variable is also a firstprivate: in this case IInit is |
| 302 | // not generated. Initialization of this variable will happen in codegen |
| 303 | // for 'firstprivate' clause. |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 304 | if (IInit) { |
| 305 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 306 | bool IsRegistered = |
| 307 | PrivateScope.addPrivate(OrigVD, [&]() -> llvm::Value *{ |
| 308 | // Emit private VarDecl with copy init. |
| 309 | EmitDecl(*VD); |
| 310 | return GetAddrOfLocalVar(VD); |
| 311 | }); |
| 312 | assert(IsRegistered && |
| 313 | "lastprivate var already registered as private"); |
| 314 | (void)IsRegistered; |
| 315 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 316 | } |
| 317 | ++IRef, ++IDestRef; |
| 318 | } |
| 319 | } |
| 320 | return HasAtLeastOneLastprivate; |
| 321 | } |
| 322 | |
| 323 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
| 324 | const OMPExecutableDirective &D, llvm::Value *IsLastIterCond) { |
| 325 | // Emit following code: |
| 326 | // if (<IsLastIterCond>) { |
| 327 | // orig_var1 = private_orig_var1; |
| 328 | // ... |
| 329 | // orig_varn = private_orig_varn; |
| 330 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 331 | llvm::BasicBlock *ThenBB = nullptr; |
| 332 | llvm::BasicBlock *DoneBB = nullptr; |
| 333 | if (IsLastIterCond) { |
| 334 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 335 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 336 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 337 | EmitBlock(ThenBB); |
| 338 | } |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 339 | llvm::DenseMap<const Decl *, const Expr *> LoopCountersAndUpdates; |
| 340 | const Expr *LastIterVal = nullptr; |
| 341 | const Expr *IVExpr = nullptr; |
| 342 | const Expr *IncExpr = nullptr; |
| 343 | if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 344 | if (isOpenMPWorksharingDirective(D.getDirectiveKind())) { |
| 345 | LastIterVal = cast<VarDecl>(cast<DeclRefExpr>( |
| 346 | LoopDirective->getUpperBoundVariable()) |
| 347 | ->getDecl()) |
| 348 | ->getAnyInitializer(); |
| 349 | IVExpr = LoopDirective->getIterationVariable(); |
| 350 | IncExpr = LoopDirective->getInc(); |
| 351 | auto IUpdate = LoopDirective->updates().begin(); |
| 352 | for (auto *E : LoopDirective->counters()) { |
| 353 | auto *D = cast<DeclRefExpr>(E)->getDecl()->getCanonicalDecl(); |
| 354 | LoopCountersAndUpdates[D] = *IUpdate; |
| 355 | ++IUpdate; |
| 356 | } |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 357 | } |
| 358 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 359 | { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 360 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 361 | bool FirstLCV = true; |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 362 | for (auto &&I = D.getClausesOfKind(OMPC_lastprivate); I; ++I) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 363 | auto *C = cast<OMPLastprivateClause>(*I); |
| 364 | auto IRef = C->varlist_begin(); |
| 365 | auto ISrcRef = C->source_exprs().begin(); |
| 366 | auto IDestRef = C->destination_exprs().begin(); |
| 367 | for (auto *AssignOp : C->assignment_ops()) { |
| 368 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 369 | QualType Type = PrivateVD->getType(); |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 370 | auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
| 371 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 372 | // If lastprivate variable is a loop control variable for loop-based |
| 373 | // directive, update its value before copyin back to original |
| 374 | // variable. |
| 375 | if (auto *UpExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) { |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 376 | if (FirstLCV && LastIterVal) { |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 377 | EmitAnyExprToMem(LastIterVal, EmitLValue(IVExpr).getAddress(), |
| 378 | IVExpr->getType().getQualifiers(), |
| 379 | /*IsInitializer=*/false); |
| 380 | EmitIgnoredExpr(IncExpr); |
| 381 | FirstLCV = false; |
| 382 | } |
| 383 | EmitIgnoredExpr(UpExpr); |
| 384 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 385 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 386 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 387 | // Get the address of the original variable. |
| 388 | auto *OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 389 | // Get the address of the private variable. |
| 390 | auto *PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 391 | EmitOMPCopy(*this, Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, |
| 392 | AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 393 | } |
| 394 | ++IRef; |
| 395 | ++ISrcRef; |
| 396 | ++IDestRef; |
| 397 | } |
| 398 | } |
| 399 | } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 400 | if (IsLastIterCond) { |
| 401 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 402 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 405 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 406 | const OMPExecutableDirective &D, |
| 407 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 408 | for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 409 | auto *C = cast<OMPReductionClause>(*I); |
| 410 | auto ILHS = C->lhs_exprs().begin(); |
| 411 | auto IRHS = C->rhs_exprs().begin(); |
| 412 | for (auto IRef : C->varlists()) { |
| 413 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); |
| 414 | auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 415 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
| 416 | // Store the address of the original variable associated with the LHS |
| 417 | // implicit variable. |
| 418 | PrivateScope.addPrivate(LHSVD, [this, OrigVD, IRef]() -> llvm::Value *{ |
| 419 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 420 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 421 | IRef->getType(), VK_LValue, IRef->getExprLoc()); |
| 422 | return EmitLValue(&DRE).getAddress(); |
| 423 | }); |
| 424 | // Emit reduction copy. |
| 425 | bool IsRegistered = |
| 426 | PrivateScope.addPrivate(OrigVD, [this, PrivateVD]() -> llvm::Value *{ |
| 427 | // Emit private VarDecl with reduction init. |
| 428 | EmitDecl(*PrivateVD); |
| 429 | return GetAddrOfLocalVar(PrivateVD); |
| 430 | }); |
| 431 | assert(IsRegistered && "private var already registered as private"); |
| 432 | // Silence the warning about unused variable. |
| 433 | (void)IsRegistered; |
| 434 | ++ILHS, ++IRHS; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
| 440 | const OMPExecutableDirective &D) { |
| 441 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 442 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 443 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 444 | bool HasAtLeastOneReduction = false; |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 445 | for (auto &&I = D.getClausesOfKind(OMPC_reduction); I; ++I) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 446 | HasAtLeastOneReduction = true; |
| 447 | auto *C = cast<OMPReductionClause>(*I); |
| 448 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 449 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 450 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 451 | } |
| 452 | if (HasAtLeastOneReduction) { |
| 453 | // Emit nowait reduction if nowait clause is present or directive is a |
| 454 | // parallel directive (it always has implicit barrier). |
| 455 | CGM.getOpenMPRuntime().emitReduction( |
| 456 | *this, D.getLocEnd(), LHSExprs, RHSExprs, ReductionOps, |
| 457 | D.getSingleClause(OMPC_nowait) || |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 458 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
| 459 | D.getDirectiveKind() == OMPD_simd, |
| 460 | D.getDirectiveKind() == OMPD_simd); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 464 | static void emitCommonOMPParallelDirective(CodeGenFunction &CGF, |
| 465 | const OMPExecutableDirective &S, |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 466 | OpenMPDirectiveKind InnermostKind, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 467 | const RegionCodeGenTy &CodeGen) { |
Alexey Bataev | 1809571 | 2014-10-10 12:19:54 +0000 | [diff] [blame] | 468 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 469 | auto CapturedStruct = CGF.GenerateCapturedStmtArgument(*CS); |
| 470 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 471 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 472 | if (auto C = S.getSingleClause(OMPC_num_threads)) { |
| 473 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 474 | auto NumThreadsClause = cast<OMPNumThreadsClause>(C); |
| 475 | auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 476 | /*IgnoreResultAssign*/ true); |
| 477 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
| 478 | CGF, NumThreads, NumThreadsClause->getLocStart()); |
| 479 | } |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 480 | if (auto *C = S.getSingleClause(OMPC_proc_bind)) { |
| 481 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
| 482 | auto *ProcBindClause = cast<OMPProcBindClause>(C); |
| 483 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
| 484 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); |
| 485 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 486 | const Expr *IfCond = nullptr; |
| 487 | if (auto C = S.getSingleClause(OMPC_if)) { |
| 488 | IfCond = cast<OMPIfClause>(C)->getCondition(); |
| 489 | } |
| 490 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, |
| 491 | CapturedStruct, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
| 495 | LexicalScope Scope(*this, S.getSourceRange()); |
| 496 | // Emit parallel region as a standalone region. |
| 497 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 498 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 499 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
| 500 | bool Firstprivates = CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 501 | if (Copyins || Firstprivates) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 502 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 503 | // initialization of firstprivate variables or propagation master's thread |
| 504 | // values of threadprivate variables to local instances of that variables |
| 505 | // of all other implicit threads. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 506 | CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(), |
| 507 | OMPD_unknown); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 508 | } |
| 509 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 510 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 511 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 512 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 513 | CGF.EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 514 | // Emit implicit barrier at the end of the 'parallel' directive. |
| 515 | CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(), |
| 516 | OMPD_unknown); |
| 517 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 518 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 519 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 520 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 521 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 522 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 523 | RunCleanupsScope BodyScope(*this); |
| 524 | // Update counters values on current iteration. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 525 | for (auto I : D.updates()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 526 | EmitIgnoredExpr(I); |
| 527 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 528 | // Update the linear variables. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 529 | for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) { |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 530 | auto *C = cast<OMPLinearClause>(*I); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 531 | for (auto U : C->updates()) { |
| 532 | EmitIgnoredExpr(U); |
| 533 | } |
| 534 | } |
| 535 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 536 | // On a continue in the body, jump to the end. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 537 | auto Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 538 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 539 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 540 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 541 | // The end (updates/cleanups). |
| 542 | EmitBlock(Continue.getBlock()); |
| 543 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 544 | // TODO: Update lastprivates if the SeparateIter flag is true. |
| 545 | // This will be implemented in a follow-up OMPLastprivateClause patch, but |
| 546 | // result should be still correct without it, as we do not make these |
| 547 | // variables private yet. |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 550 | void CodeGenFunction::EmitOMPInnerLoop( |
| 551 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 552 | const Expr *IncExpr, |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 553 | const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, |
| 554 | const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 555 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 556 | |
| 557 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 558 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 559 | EmitBlock(CondBlock); |
| 560 | LoopStack.push(CondBlock); |
| 561 | |
| 562 | // If there are any cleanups between here and the loop-exit scope, |
| 563 | // create a block to stage a loop exit along. |
| 564 | auto ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 565 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 566 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 567 | |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 568 | auto LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 569 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 570 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 571 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 572 | if (ExitBlock != LoopExit.getBlock()) { |
| 573 | EmitBlock(ExitBlock); |
| 574 | EmitBranchThroughCleanup(LoopExit); |
| 575 | } |
| 576 | |
| 577 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 578 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 579 | |
| 580 | // Create a block for the increment. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 581 | auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 582 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 583 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 584 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 585 | |
| 586 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 587 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 588 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 589 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 590 | BreakContinueStack.pop_back(); |
| 591 | EmitBranch(CondBlock); |
| 592 | LoopStack.pop(); |
| 593 | // Emit the fall-through block. |
| 594 | EmitBlock(LoopExit.getBlock()); |
| 595 | } |
| 596 | |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 597 | void CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 598 | // Emit inits for the linear variables. |
| 599 | for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) { |
| 600 | auto *C = cast<OMPLinearClause>(*I); |
| 601 | for (auto Init : C->inits()) { |
| 602 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 603 | auto *OrigVD = cast<VarDecl>( |
| 604 | cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())->getDecl()); |
| 605 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 606 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 607 | VD->getInit()->getType(), VK_LValue, |
| 608 | VD->getInit()->getExprLoc()); |
| 609 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 610 | EmitExprAsInit(&DRE, VD, |
| 611 | MakeAddrLValue(Emission.getAllocatedAddress(), |
| 612 | VD->getType(), Emission.Alignment), |
| 613 | /*capturedByInit=*/false); |
| 614 | EmitAutoVarCleanups(Emission); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 615 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 616 | // Emit the linear steps for the linear clauses. |
| 617 | // If a step is not constant, it is pre-calculated before the loop. |
| 618 | if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 619 | if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 620 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 621 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 622 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 623 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 624 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void emitLinearClauseFinal(CodeGenFunction &CGF, |
| 628 | const OMPLoopDirective &D) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 629 | // Emit the final values of the linear variables. |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 630 | for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) { |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 631 | auto *C = cast<OMPLinearClause>(*I); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 632 | auto IC = C->varlist_begin(); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 633 | for (auto F : C->finals()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 634 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
| 635 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 636 | CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 637 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 638 | auto *OrigAddr = CGF.EmitLValue(&DRE).getAddress(); |
| 639 | CodeGenFunction::OMPPrivateScope VarScope(CGF); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 640 | VarScope.addPrivate(OrigVD, |
| 641 | [OrigAddr]() -> llvm::Value *{ return OrigAddr; }); |
| 642 | (void)VarScope.Privatize(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 643 | CGF.EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 644 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 645 | } |
| 646 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 649 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 650 | const OMPExecutableDirective &D) { |
| 651 | for (auto &&I = D.getClausesOfKind(OMPC_aligned); I; ++I) { |
| 652 | auto *Clause = cast<OMPAlignedClause>(*I); |
| 653 | unsigned ClauseAlignment = 0; |
| 654 | if (auto AlignmentExpr = Clause->getAlignment()) { |
| 655 | auto AlignmentCI = |
| 656 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 657 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 658 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 659 | for (auto E : Clause->varlists()) { |
| 660 | unsigned Alignment = ClauseAlignment; |
| 661 | if (Alignment == 0) { |
| 662 | // OpenMP [2.8.1, Description] |
| 663 | // If no optional parameter is specified, implementation-defined default |
| 664 | // alignments for SIMD instructions on the target platforms are assumed. |
| 665 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 666 | CGF.getContext() |
| 667 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 668 | E->getType()->getPointeeType())) |
| 669 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 670 | } |
| 671 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 672 | "alignment is not power of 2"); |
| 673 | if (Alignment != 0) { |
| 674 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
| 675 | CGF.EmitAlignmentAssumption(PtrValue, Alignment); |
| 676 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 677 | } |
| 678 | } |
| 679 | } |
| 680 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 681 | static void emitPrivateLoopCounters(CodeGenFunction &CGF, |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 682 | CodeGenFunction::OMPPrivateScope &LoopScope, |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 683 | ArrayRef<Expr *> Counters, |
| 684 | ArrayRef<Expr *> PrivateCounters) { |
| 685 | auto I = PrivateCounters.begin(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 686 | for (auto *E : Counters) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 687 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 688 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
| 689 | llvm::Value *Addr; |
| 690 | (void)LoopScope.addPrivate(PrivateVD, [&]() -> llvm::Value * { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 691 | // Emit var without initialization. |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 692 | auto VarEmission = CGF.EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 693 | CGF.EmitAutoVarCleanups(VarEmission); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 694 | Addr = VarEmission.getAllocatedAddress(); |
| 695 | return Addr; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 696 | }); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 697 | (void)LoopScope.addPrivate(VD, [&]() -> llvm::Value * { return Addr; }); |
| 698 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 699 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 702 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 703 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 704 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 705 | { |
| 706 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 707 | emitPrivateLoopCounters(CGF, PreCondScope, S.counters(), |
| 708 | S.private_counters()); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 709 | const VarDecl *IVDecl = |
| 710 | cast<VarDecl>(cast<DeclRefExpr>(S.getIterationVariable())->getDecl()); |
| 711 | bool IsRegistered = PreCondScope.addPrivate(IVDecl, [&]() -> llvm::Value *{ |
| 712 | // Emit var without initialization. |
| 713 | auto VarEmission = CGF.EmitAutoVarAlloca(*IVDecl); |
| 714 | CGF.EmitAutoVarCleanups(VarEmission); |
| 715 | return VarEmission.getAllocatedAddress(); |
| 716 | }); |
| 717 | assert(IsRegistered && "counter already registered as private"); |
| 718 | // Silence the warning about unused variable. |
| 719 | (void)IsRegistered; |
| 720 | (void)PreCondScope.Privatize(); |
| 721 | // Initialize internal counter to 0 to calculate initial values of real |
| 722 | // counters. |
| 723 | LValue IV = CGF.EmitLValue(S.getIterationVariable()); |
| 724 | CGF.EmitStoreOfScalar( |
| 725 | llvm::ConstantInt::getNullValue( |
| 726 | IV.getAddress()->getType()->getPointerElementType()), |
| 727 | CGF.EmitLValue(S.getIterationVariable()), /*isInit=*/true); |
| 728 | // Get initial values of real counters. |
| 729 | for (auto I : S.updates()) { |
| 730 | CGF.EmitIgnoredExpr(I); |
| 731 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 732 | } |
| 733 | // Check that loop is executed at least one time. |
| 734 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 735 | } |
| 736 | |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 737 | static void |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 738 | emitPrivateLinearVars(CodeGenFunction &CGF, const OMPExecutableDirective &D, |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 739 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 740 | for (auto &&I = D.getClausesOfKind(OMPC_linear); I; ++I) { |
| 741 | auto *C = cast<OMPLinearClause>(*I); |
| 742 | for (auto *E : C->varlists()) { |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 743 | auto VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 744 | bool IsRegistered = PrivateScope.addPrivate(VD, [&]()->llvm::Value * { |
| 745 | // Emit var without initialization. |
| 746 | auto VarEmission = CGF.EmitAutoVarAlloca(*VD); |
| 747 | CGF.EmitAutoVarCleanups(VarEmission); |
| 748 | return VarEmission.getAllocatedAddress(); |
| 749 | }); |
| 750 | assert(IsRegistered && "linear var already registered as private"); |
| 751 | // Silence the warning about unused variable. |
| 752 | (void)IsRegistered; |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 757 | static void emitSafelenClause(CodeGenFunction &CGF, |
| 758 | const OMPExecutableDirective &D) { |
| 759 | if (auto *C = |
| 760 | cast_or_null<OMPSafelenClause>(D.getSingleClause(OMPC_safelen))) { |
| 761 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 762 | /*ignoreResult=*/true); |
| 763 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 764 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 765 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 766 | // the memory instructions parallel, because loop-carried |
| 767 | // dependences of 'safelen' iterations are possible. |
| 768 | CGF.LoopStack.setParallel(false); |
| 769 | } |
| 770 | } |
| 771 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 772 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D) { |
| 773 | // Walk clauses and process safelen/lastprivate. |
| 774 | LoopStack.setParallel(); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 775 | LoopStack.setVectorizeEnable(true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 776 | emitSafelenClause(*this, D); |
| 777 | } |
| 778 | |
| 779 | void CodeGenFunction::EmitOMPSimdFinal(const OMPLoopDirective &D) { |
| 780 | auto IC = D.counters().begin(); |
| 781 | for (auto F : D.finals()) { |
| 782 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 783 | if (LocalDeclMap.lookup(OrigVD) || CapturedStmtInfo->lookup(OrigVD)) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 784 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 785 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 786 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
| 787 | auto *OrigAddr = EmitLValue(&DRE).getAddress(); |
| 788 | OMPPrivateScope VarScope(*this); |
| 789 | VarScope.addPrivate(OrigVD, |
| 790 | [OrigAddr]() -> llvm::Value *{ return OrigAddr; }); |
| 791 | (void)VarScope.Privatize(); |
| 792 | EmitIgnoredExpr(F); |
| 793 | } |
| 794 | ++IC; |
| 795 | } |
| 796 | emitLinearClauseFinal(*this, D); |
| 797 | } |
| 798 | |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 799 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 800 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 801 | // if (PreCond) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 802 | // for (IV in 0..LastIteration) BODY; |
| 803 | // <Final counter/linear vars updates>; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 804 | // } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 805 | // |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 806 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 807 | // Emit: if (PreCond) - begin. |
| 808 | // If the condition constant folds and can be elided, avoid emitting the |
| 809 | // whole loop. |
| 810 | bool CondConstant; |
| 811 | llvm::BasicBlock *ContBlock = nullptr; |
| 812 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 813 | if (!CondConstant) |
| 814 | return; |
| 815 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 816 | auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
| 817 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 818 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 819 | CGF.getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 820 | CGF.EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 821 | CGF.incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 822 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 823 | |
| 824 | // Emit the loop iteration variable. |
| 825 | const Expr *IVExpr = S.getIterationVariable(); |
| 826 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 827 | CGF.EmitVarDecl(*IVDecl); |
| 828 | CGF.EmitIgnoredExpr(S.getInit()); |
| 829 | |
| 830 | // Emit the iterations count variable. |
| 831 | // If it is not a variable, Sema decided to calculate iterations count on |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 832 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 833 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 834 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 835 | // Emit calculation of the iterations count. |
| 836 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 837 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 838 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 839 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 840 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 841 | emitAlignedClause(CGF, S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 842 | CGF.EmitOMPLinearClauseInit(S); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 843 | bool HasLastprivateClause; |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 844 | { |
| 845 | OMPPrivateScope LoopScope(CGF); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 846 | emitPrivateLoopCounters(CGF, LoopScope, S.counters(), |
| 847 | S.private_counters()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 848 | emitPrivateLinearVars(CGF, S, LoopScope); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 849 | CGF.EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 850 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 851 | HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 852 | (void)LoopScope.Privatize(); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 853 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 854 | S.getInc(), |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 855 | [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 856 | CGF.EmitOMPLoopBody(S, JumpDest()); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 857 | CGF.EmitStopPoint(&S); |
| 858 | }, |
| 859 | [](CodeGenFunction &) {}); |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 860 | // Emit final copy of the lastprivate variables at the end of loops. |
| 861 | if (HasLastprivateClause) { |
| 862 | CGF.EmitOMPLastprivateClauseFinal(S); |
| 863 | } |
Alexey Bataev | 89e7e8e | 2015-06-17 06:21:39 +0000 | [diff] [blame] | 864 | CGF.EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 865 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 866 | CGF.EmitOMPSimdFinal(S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 867 | // Emit: if (PreCond) - end. |
| 868 | if (ContBlock) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 869 | CGF.EmitBranch(ContBlock); |
| 870 | CGF.EmitBlock(ContBlock, true); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 871 | } |
| 872 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 873 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 876 | void CodeGenFunction::EmitOMPForOuterLoop(OpenMPScheduleClauseKind ScheduleKind, |
| 877 | const OMPLoopDirective &S, |
| 878 | OMPPrivateScope &LoopScope, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 879 | bool Ordered, llvm::Value *LB, |
| 880 | llvm::Value *UB, llvm::Value *ST, |
| 881 | llvm::Value *IL, llvm::Value *Chunk) { |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 882 | auto &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 883 | |
| 884 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 885 | const bool DynamicOrOrdered = Ordered || RT.isDynamic(ScheduleKind); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 886 | |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 887 | assert((Ordered || |
| 888 | !RT.isStaticNonchunked(ScheduleKind, /*Chunked=*/Chunk != nullptr)) && |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 889 | "static non-chunked schedule does not need outer loop"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 890 | |
| 891 | // Emit outer loop. |
| 892 | // |
| 893 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 894 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 895 | // distributed to threads in the team in chunks as the threads request them. |
| 896 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 897 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 898 | // iterations, except for the last chunk to be distributed, which may have |
| 899 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 900 | // |
| 901 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 902 | // to threads in the team in chunks as the executing threads request them. |
| 903 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 904 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 905 | // each chunk is proportional to the number of unassigned iterations divided |
| 906 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 907 | // with value k (greater than 1), the size of each chunk is determined in the |
| 908 | // same way, with the restriction that the chunks do not contain fewer than k |
| 909 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 910 | // than k iterations). |
| 911 | // |
| 912 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 913 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 914 | // implementation the freedom to choose any possible mapping of iterations to |
| 915 | // threads in the team. |
| 916 | // |
| 917 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 918 | // deferred until run time, and the schedule and chunk size are taken from the |
| 919 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 920 | // implementation defined |
| 921 | // |
| 922 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 923 | // idx = LB; |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 924 | // while (idx <= UB) { BODY; ++idx; |
| 925 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 926 | // } // inner loop |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 927 | // } |
| 928 | // |
| 929 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 930 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 931 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 932 | // the team in a round-robin fashion in the order of the thread number. |
| 933 | // |
| 934 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 935 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 936 | // LB = LB + ST; |
| 937 | // UB = UB + ST; |
| 938 | // } |
| 939 | // |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 940 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 941 | const Expr *IVExpr = S.getIterationVariable(); |
| 942 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 943 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 944 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 945 | RT.emitForInit( |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 946 | *this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, Ordered, IL, LB, |
| 947 | (DynamicOrOrdered ? EmitAnyExpr(S.getLastIteration()).getScalarVal() |
| 948 | : UB), |
| 949 | ST, Chunk); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 950 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 951 | auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
| 952 | |
| 953 | // Start the loop with a block that tests the condition. |
| 954 | auto CondBlock = createBasicBlock("omp.dispatch.cond"); |
| 955 | EmitBlock(CondBlock); |
| 956 | LoopStack.push(CondBlock); |
| 957 | |
| 958 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 959 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 960 | // UB = min(UB, GlobalUB) |
| 961 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 962 | // IV = LB |
| 963 | EmitIgnoredExpr(S.getInit()); |
| 964 | // IV < UB |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 965 | BoolCondVal = EvaluateExprAsBool(S.getCond()); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 966 | } else { |
| 967 | BoolCondVal = RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, |
| 968 | IL, LB, UB, ST); |
| 969 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 970 | |
| 971 | // If there are any cleanups between here and the loop-exit scope, |
| 972 | // create a block to stage a loop exit along. |
| 973 | auto ExitBlock = LoopExit.getBlock(); |
| 974 | if (LoopScope.requiresCleanups()) |
| 975 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 976 | |
| 977 | auto LoopBody = createBasicBlock("omp.dispatch.body"); |
| 978 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 979 | if (ExitBlock != LoopExit.getBlock()) { |
| 980 | EmitBlock(ExitBlock); |
| 981 | EmitBranchThroughCleanup(LoopExit); |
| 982 | } |
| 983 | EmitBlock(LoopBody); |
| 984 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 985 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 986 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 987 | if (DynamicOrOrdered) |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 988 | EmitIgnoredExpr(S.getInit()); |
| 989 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 990 | // Create a block for the increment. |
| 991 | auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
| 992 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 993 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 994 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 995 | // with dynamic/guided scheduling and without ordered clause. |
| 996 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 997 | LoopStack.setParallel((ScheduleKind == OMPC_SCHEDULE_dynamic || |
| 998 | ScheduleKind == OMPC_SCHEDULE_guided) && |
| 999 | !Ordered); |
| 1000 | } else { |
| 1001 | EmitOMPSimdInit(S); |
| 1002 | } |
| 1003 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1004 | SourceLocation Loc = S.getLocStart(); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1005 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), |
| 1006 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 1007 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1008 | CGF.EmitStopPoint(&S); |
| 1009 | }, |
| 1010 | [Ordered, IVSize, IVSigned, Loc](CodeGenFunction &CGF) { |
| 1011 | if (Ordered) { |
| 1012 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd( |
| 1013 | CGF, Loc, IVSize, IVSigned); |
| 1014 | } |
| 1015 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1016 | |
| 1017 | EmitBlock(Continue.getBlock()); |
| 1018 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1019 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1020 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
| 1021 | EmitIgnoredExpr(S.getNextLowerBound()); |
| 1022 | EmitIgnoredExpr(S.getNextUpperBound()); |
| 1023 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1024 | |
| 1025 | EmitBranch(CondBlock); |
| 1026 | LoopStack.pop(); |
| 1027 | // Emit the fall-through block. |
| 1028 | EmitBlock(LoopExit.getBlock()); |
| 1029 | |
| 1030 | // Tell the runtime we are done. |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1031 | if (!DynamicOrOrdered) |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1032 | RT.emitForStaticFinish(*this, S.getLocEnd()); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1035 | /// \brief Emit a helper variable and return corresponding lvalue. |
| 1036 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1037 | const DeclRefExpr *Helper) { |
| 1038 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1039 | CGF.EmitVarDecl(*VDecl); |
| 1040 | return CGF.EmitLValue(Helper); |
| 1041 | } |
| 1042 | |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1043 | static std::pair<llvm::Value * /*Chunk*/, OpenMPScheduleClauseKind> |
| 1044 | emitScheduleClause(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1045 | bool OuterRegion) { |
| 1046 | // Detect the loop schedule kind and chunk. |
| 1047 | auto ScheduleKind = OMPC_SCHEDULE_unknown; |
| 1048 | llvm::Value *Chunk = nullptr; |
| 1049 | if (auto *C = |
| 1050 | cast_or_null<OMPScheduleClause>(S.getSingleClause(OMPC_schedule))) { |
| 1051 | ScheduleKind = C->getScheduleKind(); |
| 1052 | if (const auto *Ch = C->getChunkSize()) { |
| 1053 | if (auto *ImpRef = cast_or_null<DeclRefExpr>(C->getHelperChunkSize())) { |
| 1054 | if (OuterRegion) { |
| 1055 | const VarDecl *ImpVar = cast<VarDecl>(ImpRef->getDecl()); |
| 1056 | CGF.EmitVarDecl(*ImpVar); |
| 1057 | CGF.EmitStoreThroughLValue( |
| 1058 | CGF.EmitAnyExpr(Ch), |
| 1059 | CGF.MakeNaturalAlignAddrLValue(CGF.GetAddrOfLocalVar(ImpVar), |
| 1060 | ImpVar->getType())); |
| 1061 | } else { |
| 1062 | Ch = ImpRef; |
| 1063 | } |
| 1064 | } |
| 1065 | if (!C->getHelperChunkSize() || !OuterRegion) { |
| 1066 | Chunk = CGF.EmitScalarExpr(Ch); |
| 1067 | Chunk = CGF.EmitScalarConversion(Chunk, Ch->getType(), |
| 1068 | S.getIterationVariable()->getType()); |
| 1069 | } |
| 1070 | } |
| 1071 | } |
| 1072 | return std::make_pair(Chunk, ScheduleKind); |
| 1073 | } |
| 1074 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1075 | bool CodeGenFunction::EmitOMPWorksharingLoop(const OMPLoopDirective &S) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1076 | // Emit the loop iteration variable. |
| 1077 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 1078 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 1079 | EmitVarDecl(*IVDecl); |
| 1080 | |
| 1081 | // Emit the iterations count variable. |
| 1082 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 1083 | // iteration (e.g., it is foldable into a constant). |
| 1084 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 1085 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1086 | // Emit calculation of the iterations count. |
| 1087 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1088 | } |
| 1089 | |
| 1090 | auto &RT = CGM.getOpenMPRuntime(); |
| 1091 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1092 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1093 | // Check pre-condition. |
| 1094 | { |
| 1095 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1096 | // If the condition constant folds and can be elided, avoid emitting the |
| 1097 | // whole loop. |
| 1098 | bool CondConstant; |
| 1099 | llvm::BasicBlock *ContBlock = nullptr; |
| 1100 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1101 | if (!CondConstant) |
| 1102 | return false; |
| 1103 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1104 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 1105 | ContBlock = createBasicBlock("omp.precond.end"); |
| 1106 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1107 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1108 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1109 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1110 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1111 | |
| 1112 | emitAlignedClause(*this, S); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1113 | EmitOMPLinearClauseInit(S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1114 | // Emit 'then' code. |
| 1115 | { |
| 1116 | // Emit helper vars inits. |
| 1117 | LValue LB = |
| 1118 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1119 | LValue UB = |
| 1120 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1121 | LValue ST = |
| 1122 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 1123 | LValue IL = |
| 1124 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 1125 | |
| 1126 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1127 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
| 1128 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1129 | // initialization of firstprivate variables. |
| 1130 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 1131 | OMPD_unknown); |
| 1132 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 1133 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1134 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 1135 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame^] | 1136 | emitPrivateLoopCounters(*this, LoopScope, S.counters(), |
| 1137 | S.private_counters()); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1138 | emitPrivateLinearVars(*this, S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 1139 | (void)LoopScope.Privatize(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1140 | |
| 1141 | // Detect the loop schedule kind and chunk. |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1142 | llvm::Value *Chunk; |
| 1143 | OpenMPScheduleClauseKind ScheduleKind; |
| 1144 | auto ScheduleInfo = |
| 1145 | emitScheduleClause(*this, S, /*OuterRegion=*/false); |
| 1146 | Chunk = ScheduleInfo.first; |
| 1147 | ScheduleKind = ScheduleInfo.second; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1148 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1149 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1150 | const bool Ordered = S.getSingleClause(OMPC_ordered) != nullptr; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1151 | if (RT.isStaticNonchunked(ScheduleKind, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1152 | /* Chunked */ Chunk != nullptr) && |
| 1153 | !Ordered) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1154 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 1155 | EmitOMPSimdInit(S); |
| 1156 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1157 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1158 | // When no chunk_size is specified, the iteration space is divided into |
| 1159 | // chunks that are approximately equal in size, and at most one chunk is |
| 1160 | // distributed to each thread. Note that the size of the chunks is |
| 1161 | // unspecified in this case. |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1162 | RT.emitForInit(*this, S.getLocStart(), ScheduleKind, IVSize, IVSigned, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1163 | Ordered, IL.getAddress(), LB.getAddress(), |
| 1164 | UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1165 | auto LoopExit = getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1166 | // UB = min(UB, GlobalUB); |
| 1167 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 1168 | // IV = LB; |
| 1169 | EmitIgnoredExpr(S.getInit()); |
| 1170 | // while (idx <= UB) { BODY; ++idx; } |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 1171 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1172 | S.getInc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1173 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 1174 | CGF.EmitOMPLoopBody(S, LoopExit); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1175 | CGF.EmitStopPoint(&S); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1176 | }, |
| 1177 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1178 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1179 | // Tell the runtime we are done. |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1180 | RT.emitForStaticFinish(*this, S.getLocStart()); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1181 | } else { |
| 1182 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 1183 | // runtime and runs the inner loop to process it. |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1184 | EmitOMPForOuterLoop(ScheduleKind, S, LoopScope, Ordered, |
| 1185 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 1186 | IL.getAddress(), Chunk); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1187 | } |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 1188 | EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1189 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 1190 | if (HasLastprivateClause) |
| 1191 | EmitOMPLastprivateClauseFinal( |
| 1192 | S, Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1193 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1194 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 1195 | EmitOMPSimdFinal(S); |
| 1196 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1197 | // We're now done with the loop, so jump to the continuation block. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1198 | if (ContBlock) { |
| 1199 | EmitBranch(ContBlock); |
| 1200 | EmitBlock(ContBlock, true); |
| 1201 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1202 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1203 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1204 | } |
| 1205 | |
| 1206 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1207 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1208 | bool HasLastprivates = false; |
| 1209 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) { |
| 1210 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S); |
| 1211 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1212 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 1213 | |
| 1214 | // Emit an implicit barrier at the end. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1215 | if (!S.getSingleClause(OMPC_nowait) || HasLastprivates) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1216 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 1217 | } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 1218 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1219 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1220 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
| 1221 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1222 | bool HasLastprivates = false; |
| 1223 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF) { |
| 1224 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S); |
| 1225 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1226 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1227 | |
| 1228 | // Emit an implicit barrier at the end. |
| 1229 | if (!S.getSingleClause(OMPC_nowait) || HasLastprivates) { |
| 1230 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 1231 | } |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1234 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 1235 | const Twine &Name, |
| 1236 | llvm::Value *Init = nullptr) { |
| 1237 | auto LVal = CGF.MakeNaturalAlignAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
| 1238 | if (Init) |
| 1239 | CGF.EmitScalarInit(Init, LVal); |
| 1240 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1243 | OpenMPDirectiveKind |
| 1244 | CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1245 | auto *Stmt = cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt(); |
| 1246 | auto *CS = dyn_cast<CompoundStmt>(Stmt); |
| 1247 | if (CS && CS->size() > 1) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1248 | bool HasLastprivates = false; |
| 1249 | auto &&CodeGen = [&S, CS, &HasLastprivates](CodeGenFunction &CGF) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1250 | auto &C = CGF.CGM.getContext(); |
| 1251 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 1252 | // Emit helper vars inits. |
| 1253 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 1254 | CGF.Builder.getInt32(0)); |
| 1255 | auto *GlobalUBVal = CGF.Builder.getInt32(CS->size() - 1); |
| 1256 | LValue UB = |
| 1257 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 1258 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 1259 | CGF.Builder.getInt32(1)); |
| 1260 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 1261 | CGF.Builder.getInt32(0)); |
| 1262 | // Loop counter. |
| 1263 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
| 1264 | OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1265 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1266 | OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1267 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1268 | // Generate condition for loop. |
| 1269 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
| 1270 | OK_Ordinary, S.getLocStart(), |
| 1271 | /*fpContractable=*/false); |
| 1272 | // Increment for loop counter. |
| 1273 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, |
| 1274 | OK_Ordinary, S.getLocStart()); |
| 1275 | auto BodyGen = [CS, &S, &IV](CodeGenFunction &CGF) { |
| 1276 | // Iterate through all sections and emit a switch construct: |
| 1277 | // switch (IV) { |
| 1278 | // case 0: |
| 1279 | // <SectionStmt[0]>; |
| 1280 | // break; |
| 1281 | // ... |
| 1282 | // case <NumSection> - 1: |
| 1283 | // <SectionStmt[<NumSection> - 1]>; |
| 1284 | // break; |
| 1285 | // } |
| 1286 | // .omp.sections.exit: |
| 1287 | auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 1288 | auto *SwitchStmt = CGF.Builder.CreateSwitch( |
| 1289 | CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, |
| 1290 | CS->size()); |
| 1291 | unsigned CaseNumber = 0; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1292 | for (auto *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1293 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 1294 | CGF.EmitBlock(CaseBB); |
| 1295 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1296 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1297 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 1298 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1299 | } |
| 1300 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
| 1301 | }; |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1302 | |
| 1303 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1304 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
| 1305 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1306 | // initialization of firstprivate variables. |
| 1307 | CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(), |
| 1308 | OMPD_unknown); |
| 1309 | } |
Alexey Bataev | 7387083 | 2015-04-27 04:12:12 +0000 | [diff] [blame] | 1310 | CGF.EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1311 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | a89adf2 | 2015-04-27 05:04:13 +0000 | [diff] [blame] | 1312 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1313 | (void)LoopScope.Privatize(); |
| 1314 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1315 | // Emit static non-chunked loop. |
| 1316 | CGF.CGM.getOpenMPRuntime().emitForInit( |
| 1317 | CGF, S.getLocStart(), OMPC_SCHEDULE_static, /*IVSize=*/32, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1318 | /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), |
| 1319 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1320 | // UB = min(UB, GlobalUB); |
| 1321 | auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); |
| 1322 | auto *MinUBGlobalUB = CGF.Builder.CreateSelect( |
| 1323 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 1324 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 1325 | // IV = LB; |
| 1326 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); |
| 1327 | // while (idx <= UB) { BODY; ++idx; } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1328 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 1329 | [](CodeGenFunction &) {}); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1330 | // Tell the runtime we are done. |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1331 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocStart()); |
Alexey Bataev | a89adf2 | 2015-04-27 05:04:13 +0000 | [diff] [blame] | 1332 | CGF.EmitOMPReductionClauseFinal(S); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1333 | |
| 1334 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 1335 | if (HasLastprivates) |
| 1336 | CGF.EmitOMPLastprivateClauseFinal( |
| 1337 | S, CGF.Builder.CreateIsNotNull( |
| 1338 | CGF.EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1339 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1340 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1341 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1342 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 1343 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 1344 | // directive. |
| 1345 | if (HasLastprivates && S.getSingleClause(OMPC_nowait)) { |
| 1346 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1347 | // initialization of firstprivate variables. |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1348 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 1349 | OMPD_unknown); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1350 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1351 | return OMPD_sections; |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1352 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1353 | // If only one section is found - no need to generate loop, emit as a single |
| 1354 | // region. |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1355 | bool HasFirstprivates; |
Alexey Bataev | a89adf2 | 2015-04-27 05:04:13 +0000 | [diff] [blame] | 1356 | // No need to generate reductions for sections with single section region, we |
| 1357 | // can use original shared variables for all operations. |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1358 | bool HasReductions = !S.getClausesOfKind(OMPC_reduction).empty(); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 1359 | // No need to generate lastprivates for sections with single section region, |
| 1360 | // we can use original shared variable for all calculations with barrier at |
| 1361 | // the end of the sections. |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1362 | bool HasLastprivates = !S.getClausesOfKind(OMPC_lastprivate).empty(); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1363 | auto &&CodeGen = [Stmt, &S, &HasFirstprivates](CodeGenFunction &CGF) { |
| 1364 | CodeGenFunction::OMPPrivateScope SingleScope(CGF); |
| 1365 | HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope); |
Alexey Bataev | 7387083 | 2015-04-27 04:12:12 +0000 | [diff] [blame] | 1366 | CGF.EmitOMPPrivateClause(S, SingleScope); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1367 | (void)SingleScope.Privatize(); |
| 1368 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1369 | CGF.EmitStmt(Stmt); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1370 | }; |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1371 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), |
| 1372 | llvm::None, llvm::None, llvm::None, |
| 1373 | llvm::None); |
Alexey Bataev | a89adf2 | 2015-04-27 05:04:13 +0000 | [diff] [blame] | 1374 | // Emit barrier for firstprivates, lastprivates or reductions only if |
| 1375 | // 'sections' directive has 'nowait' clause. Otherwise the barrier will be |
| 1376 | // generated by the codegen for the directive. |
| 1377 | if ((HasFirstprivates || HasLastprivates || HasReductions) && |
| 1378 | S.getSingleClause(OMPC_nowait)) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1379 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 1380 | // initialization of firstprivate variables. |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1381 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 1382 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1383 | return OMPD_single; |
| 1384 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1385 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1386 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
| 1387 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1388 | OpenMPDirectiveKind EmittedAs = EmitSections(S); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1389 | // Emit an implicit barrier at the end. |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1390 | if (!S.getSingleClause(OMPC_nowait)) { |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1391 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), EmittedAs); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1392 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1396 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1397 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1398 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1399 | CGF.EnsureInsertPoint(); |
| 1400 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1401 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 1404 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1405 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1406 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1407 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1408 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1409 | // Check if there are any 'copyprivate' clauses associated with this |
| 1410 | // 'single' |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1411 | // construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1412 | // Build a list of copyprivate variables along with helper expressions |
| 1413 | // (<source>, <destination>, <destination>=<source> expressions) |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1414 | for (auto &&I = S.getClausesOfKind(OMPC_copyprivate); I; ++I) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1415 | auto *C = cast<OMPCopyprivateClause>(*I); |
| 1416 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1417 | DestExprs.append(C->destination_exprs().begin(), |
| 1418 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1419 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1420 | AssignmentOps.append(C->assignment_ops().begin(), |
| 1421 | C->assignment_ops().end()); |
| 1422 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1423 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 1424 | // Emit code for 'single' region along with 'copyprivate' clauses |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1425 | bool HasFirstprivates; |
| 1426 | auto &&CodeGen = [&S, &HasFirstprivates](CodeGenFunction &CGF) { |
| 1427 | CodeGenFunction::OMPPrivateScope SingleScope(CGF); |
| 1428 | HasFirstprivates = CGF.EmitOMPFirstprivateClause(S, SingleScope); |
Alexey Bataev | 59c654a | 2015-04-27 03:48:52 +0000 | [diff] [blame] | 1429 | CGF.EmitOMPPrivateClause(S, SingleScope); |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1430 | (void)SingleScope.Privatize(); |
| 1431 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1432 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1433 | CGF.EnsureInsertPoint(); |
| 1434 | }; |
| 1435 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 1436 | CopyprivateVars, DestExprs, SrcExprs, |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1437 | AssignmentOps); |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 1438 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 1439 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
| 1440 | if ((!S.getSingleClause(OMPC_nowait) || HasFirstprivates) && |
| 1441 | CopyprivateVars.empty()) { |
| 1442 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 1443 | *this, S.getLocStart(), |
| 1444 | S.getSingleClause(OMPC_nowait) ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1445 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 1448 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1449 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1450 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1451 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1452 | CGF.EnsureInsertPoint(); |
| 1453 | }; |
| 1454 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 1457 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1458 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1459 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1460 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1461 | CGF.EnsureInsertPoint(); |
| 1462 | }; |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1463 | CGM.getOpenMPRuntime().emitCriticalRegion( |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1464 | *this, S.getDirectiveName().getAsString(), CodeGen, S.getLocStart()); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 1467 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 1468 | const OMPParallelForDirective &S) { |
| 1469 | // Emit directive as a combined directive that consists of two implicit |
| 1470 | // directives: 'parallel' with 'for' directive. |
| 1471 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 040d540 | 2015-05-12 08:35:28 +0000 | [diff] [blame] | 1472 | (void)emitScheduleClause(*this, S, /*OuterRegion=*/true); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 1473 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1474 | CGF.EmitOMPWorksharingLoop(S); |
| 1475 | // Emit implicit barrier at the end of parallel region, but this barrier |
| 1476 | // is at the end of 'for' directive, so emit it as the implicit barrier for |
| 1477 | // this 'for' directive. |
| 1478 | CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(), |
| 1479 | OMPD_parallel); |
| 1480 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1481 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1484 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1485 | const OMPParallelForSimdDirective &S) { |
| 1486 | // Emit directive as a combined directive that consists of two implicit |
| 1487 | // directives: 'parallel' with 'for' directive. |
| 1488 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1489 | (void)emitScheduleClause(*this, S, /*OuterRegion=*/true); |
| 1490 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1491 | CGF.EmitOMPWorksharingLoop(S); |
| 1492 | // Emit implicit barrier at the end of parallel region, but this barrier |
| 1493 | // is at the end of 'for' directive, so emit it as the implicit barrier for |
| 1494 | // this 'for' directive. |
| 1495 | CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(), |
| 1496 | OMPD_parallel); |
| 1497 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1498 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1501 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1502 | const OMPParallelSectionsDirective &S) { |
| 1503 | // Emit directive as a combined directive that consists of two implicit |
| 1504 | // directives: 'parallel' with 'sections' directive. |
| 1505 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1506 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1507 | (void)CGF.EmitSections(S); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 1508 | // Emit implicit barrier at the end of parallel region. |
| 1509 | CGF.CGM.getOpenMPRuntime().emitBarrierCall(CGF, S.getLocStart(), |
| 1510 | OMPD_parallel); |
| 1511 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1512 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1515 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 1516 | // Emit outlined function for task construct. |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1517 | LexicalScope Scope(*this, S.getSourceRange()); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1518 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 1519 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 1520 | auto *I = CS->getCapturedDecl()->param_begin(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1521 | auto *PartId = std::next(I); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1522 | // The first function argument for tasks is a thread id, the second one is a |
| 1523 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1524 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 1525 | // Get list of private variables. |
| 1526 | llvm::SmallVector<const Expr *, 8> PrivateVars; |
| 1527 | llvm::SmallVector<const Expr *, 8> PrivateCopies; |
| 1528 | for (auto &&I = S.getClausesOfKind(OMPC_private); I; ++I) { |
| 1529 | auto *C = cast<OMPPrivateClause>(*I); |
| 1530 | auto IRef = C->varlist_begin(); |
| 1531 | for (auto *IInit : C->private_copies()) { |
| 1532 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 1533 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 1534 | PrivateVars.push_back(*IRef); |
| 1535 | PrivateCopies.push_back(IInit); |
| 1536 | } |
| 1537 | ++IRef; |
| 1538 | } |
| 1539 | } |
| 1540 | EmittedAsPrivate.clear(); |
| 1541 | // Get list of firstprivate variables. |
| 1542 | llvm::SmallVector<const Expr *, 8> FirstprivateVars; |
| 1543 | llvm::SmallVector<const Expr *, 8> FirstprivateCopies; |
| 1544 | llvm::SmallVector<const Expr *, 8> FirstprivateInits; |
| 1545 | for (auto &&I = S.getClausesOfKind(OMPC_firstprivate); I; ++I) { |
| 1546 | auto *C = cast<OMPFirstprivateClause>(*I); |
| 1547 | auto IRef = C->varlist_begin(); |
| 1548 | auto IElemInitRef = C->inits().begin(); |
| 1549 | for (auto *IInit : C->private_copies()) { |
| 1550 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 1551 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 1552 | FirstprivateVars.push_back(*IRef); |
| 1553 | FirstprivateCopies.push_back(IInit); |
| 1554 | FirstprivateInits.push_back(*IElemInitRef); |
| 1555 | } |
| 1556 | ++IRef, ++IElemInitRef; |
| 1557 | } |
| 1558 | } |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 1559 | // Build list of dependences. |
| 1560 | llvm::SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 8> |
| 1561 | Dependences; |
| 1562 | for (auto &&I = S.getClausesOfKind(OMPC_depend); I; ++I) { |
| 1563 | auto *C = cast<OMPDependClause>(*I); |
| 1564 | for (auto *IRef : C->varlists()) { |
| 1565 | Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
| 1566 | } |
| 1567 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1568 | auto &&CodeGen = [PartId, &S, &PrivateVars, &FirstprivateVars]( |
| 1569 | CodeGenFunction &CGF) { |
| 1570 | // Set proper addresses for generated private copies. |
| 1571 | auto *CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 1572 | OMPPrivateScope Scope(CGF); |
| 1573 | if (!PrivateVars.empty() || !FirstprivateVars.empty()) { |
| 1574 | auto *CopyFn = CGF.Builder.CreateAlignedLoad( |
| 1575 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3)), |
| 1576 | CGF.PointerAlignInBytes); |
| 1577 | auto *PrivatesPtr = CGF.Builder.CreateAlignedLoad( |
| 1578 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2)), |
| 1579 | CGF.PointerAlignInBytes); |
| 1580 | // Map privates. |
| 1581 | llvm::SmallVector<std::pair<const VarDecl *, llvm::Value *>, 16> |
| 1582 | PrivatePtrs; |
| 1583 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 1584 | CallArgs.push_back(PrivatesPtr); |
| 1585 | for (auto *E : PrivateVars) { |
| 1586 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1587 | auto *PrivatePtr = |
| 1588 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType())); |
| 1589 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 1590 | CallArgs.push_back(PrivatePtr); |
| 1591 | } |
| 1592 | for (auto *E : FirstprivateVars) { |
| 1593 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1594 | auto *PrivatePtr = |
| 1595 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType())); |
| 1596 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 1597 | CallArgs.push_back(PrivatePtr); |
| 1598 | } |
| 1599 | CGF.EmitRuntimeCall(CopyFn, CallArgs); |
| 1600 | for (auto &&Pair : PrivatePtrs) { |
| 1601 | auto *Replacement = |
| 1602 | CGF.Builder.CreateAlignedLoad(Pair.second, CGF.PointerAlignInBytes); |
| 1603 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 1604 | } |
| 1605 | } |
| 1606 | (void)Scope.Privatize(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1607 | if (*PartId) { |
| 1608 | // TODO: emit code for untied tasks. |
| 1609 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1610 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1611 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1612 | auto OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 1613 | S, *I, OMPD_task, CodeGen); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 1614 | // Check if we should emit tied or untied task. |
| 1615 | bool Tied = !S.getSingleClause(OMPC_untied); |
| 1616 | // Check if the task is final |
| 1617 | llvm::PointerIntPair<llvm::Value *, 1, bool> Final; |
| 1618 | if (auto *Clause = S.getSingleClause(OMPC_final)) { |
| 1619 | // If the condition constant folds and can be elided, try to avoid emitting |
| 1620 | // the condition and the dead arm of the if/else. |
| 1621 | auto *Cond = cast<OMPFinalClause>(Clause)->getCondition(); |
| 1622 | bool CondConstant; |
| 1623 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 1624 | Final.setInt(CondConstant); |
| 1625 | else |
| 1626 | Final.setPointer(EvaluateExprAsBool(Cond)); |
| 1627 | } else { |
| 1628 | // By default the task is not final. |
| 1629 | Final.setInt(/*IntVal=*/false); |
| 1630 | } |
| 1631 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1632 | const Expr *IfCond = nullptr; |
| 1633 | if (auto C = S.getSingleClause(OMPC_if)) { |
| 1634 | IfCond = cast<OMPIfClause>(C)->getCondition(); |
| 1635 | } |
Alexey Bataev | 9e03404 | 2015-05-05 04:05:12 +0000 | [diff] [blame] | 1636 | CGM.getOpenMPRuntime().emitTaskCall( |
| 1637 | *this, S.getLocStart(), S, Tied, Final, OutlinedFn, SharedsTy, |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 1638 | CapturedStruct, IfCond, PrivateVars, PrivateCopies, FirstprivateVars, |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 1639 | FirstprivateCopies, FirstprivateInits, Dependences); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 1642 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 1643 | const OMPTaskyieldDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1644 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 1645 | } |
| 1646 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 1647 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 1648 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 1651 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
| 1652 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 1655 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 1656 | const OMPTaskgroupDirective &S) { |
| 1657 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1658 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1659 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1660 | CGF.EnsureInsertPoint(); |
| 1661 | }; |
| 1662 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); |
| 1663 | } |
| 1664 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 1665 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1666 | CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { |
| 1667 | if (auto C = S.getSingleClause(/*K*/ OMPC_flush)) { |
| 1668 | auto FlushClause = cast<OMPFlushClause>(C); |
| 1669 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 1670 | FlushClause->varlist_end()); |
| 1671 | } |
| 1672 | return llvm::None; |
| 1673 | }(), S.getLocStart()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1676 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
| 1677 | LexicalScope Scope(*this, S.getSourceRange()); |
| 1678 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
| 1679 | CGF.EmitStmt(cast<CapturedStmt>(S.getAssociatedStmt())->getCapturedStmt()); |
| 1680 | CGF.EnsureInsertPoint(); |
| 1681 | }; |
| 1682 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart()); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 1685 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
| 1686 | QualType SrcType, QualType DestType) { |
| 1687 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 1688 | "DestType must have scalar evaluation kind."); |
| 1689 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
| 1690 | return Val.isScalar() |
| 1691 | ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType) |
| 1692 | : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, |
| 1693 | DestType); |
| 1694 | } |
| 1695 | |
| 1696 | static CodeGenFunction::ComplexPairTy |
| 1697 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
| 1698 | QualType DestType) { |
| 1699 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 1700 | "DestType must have complex evaluation kind."); |
| 1701 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 1702 | if (Val.isScalar()) { |
| 1703 | // Convert the input element to the element type of the complex. |
| 1704 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
| 1705 | auto ScalarVal = |
| 1706 | CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestElementType); |
| 1707 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 1708 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 1709 | } else { |
| 1710 | assert(Val.isComplex() && "Must be a scalar or complex."); |
| 1711 | auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 1712 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
| 1713 | ComplexVal.first = CGF.EmitScalarConversion( |
| 1714 | Val.getComplexVal().first, SrcElementType, DestElementType); |
| 1715 | ComplexVal.second = CGF.EmitScalarConversion( |
| 1716 | Val.getComplexVal().second, SrcElementType, DestElementType); |
| 1717 | } |
| 1718 | return ComplexVal; |
| 1719 | } |
| 1720 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1721 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 1722 | LValue LVal, RValue RVal) { |
| 1723 | if (LVal.isGlobalReg()) { |
| 1724 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 1725 | } else { |
| 1726 | CGF.EmitAtomicStore(RVal, LVal, IsSeqCst ? llvm::SequentiallyConsistent |
| 1727 | : llvm::Monotonic, |
| 1728 | LVal.isVolatile(), /*IsInit=*/false); |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | static void emitSimpleStore(CodeGenFunction &CGF, LValue LVal, RValue RVal, |
| 1733 | QualType RValTy) { |
| 1734 | switch (CGF.getEvaluationKind(LVal.getType())) { |
| 1735 | case TEK_Scalar: |
| 1736 | CGF.EmitStoreThroughLValue( |
| 1737 | RValue::get(convertToScalarValue(CGF, RVal, RValTy, LVal.getType())), |
| 1738 | LVal); |
| 1739 | break; |
| 1740 | case TEK_Complex: |
| 1741 | CGF.EmitStoreOfComplex( |
| 1742 | convertToComplexValue(CGF, RVal, RValTy, LVal.getType()), LVal, |
| 1743 | /*isInit=*/false); |
| 1744 | break; |
| 1745 | case TEK_Aggregate: |
| 1746 | llvm_unreachable("Must be a scalar or complex."); |
| 1747 | } |
| 1748 | } |
| 1749 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 1750 | static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 1751 | const Expr *X, const Expr *V, |
| 1752 | SourceLocation Loc) { |
| 1753 | // v = x; |
| 1754 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 1755 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 1756 | LValue XLValue = CGF.EmitLValue(X); |
| 1757 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 1758 | RValue Res = XLValue.isGlobalReg() |
| 1759 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
| 1760 | : CGF.EmitAtomicLoad(XLValue, Loc, |
| 1761 | IsSeqCst ? llvm::SequentiallyConsistent |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 1762 | : llvm::Monotonic, |
| 1763 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 1764 | // OpenMP, 2.12.6, atomic Construct |
| 1765 | // Any atomic construct with a seq_cst clause forces the atomically |
| 1766 | // performed operation to include an implicit flush operation without a |
| 1767 | // list. |
| 1768 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 1769 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1770 | emitSimpleStore(CGF,VLValue, Res, X->getType().getNonReferenceType()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 1771 | } |
| 1772 | |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 1773 | static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 1774 | const Expr *X, const Expr *E, |
| 1775 | SourceLocation Loc) { |
| 1776 | // x = expr; |
| 1777 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1778 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 1779 | // OpenMP, 2.12.6, atomic Construct |
| 1780 | // Any atomic construct with a seq_cst clause forces the atomically |
| 1781 | // performed operation to include an implicit flush operation without a |
| 1782 | // list. |
| 1783 | if (IsSeqCst) |
| 1784 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 1785 | } |
| 1786 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 1787 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 1788 | RValue Update, |
| 1789 | BinaryOperatorKind BO, |
| 1790 | llvm::AtomicOrdering AO, |
| 1791 | bool IsXLHSInRHSPart) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1792 | auto &Context = CGF.CGM.getContext(); |
| 1793 | // Allow atomicrmw only if 'x' and 'update' are integer values, lvalue for 'x' |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 1794 | // expression is simple and atomic is allowed for the given type for the |
| 1795 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1796 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 1797 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 1798 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 1799 | (Update.getScalarVal()->getType() != |
| 1800 | X.getAddress()->getType()->getPointerElementType())) || |
| 1801 | !X.getAddress()->getType()->getPointerElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1802 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 1803 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1804 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1805 | |
| 1806 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 1807 | switch (BO) { |
| 1808 | case BO_Add: |
| 1809 | RMWOp = llvm::AtomicRMWInst::Add; |
| 1810 | break; |
| 1811 | case BO_Sub: |
| 1812 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1813 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1814 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 1815 | break; |
| 1816 | case BO_And: |
| 1817 | RMWOp = llvm::AtomicRMWInst::And; |
| 1818 | break; |
| 1819 | case BO_Or: |
| 1820 | RMWOp = llvm::AtomicRMWInst::Or; |
| 1821 | break; |
| 1822 | case BO_Xor: |
| 1823 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 1824 | break; |
| 1825 | case BO_LT: |
| 1826 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 1827 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 1828 | : llvm::AtomicRMWInst::Max) |
| 1829 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 1830 | : llvm::AtomicRMWInst::UMax); |
| 1831 | break; |
| 1832 | case BO_GT: |
| 1833 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 1834 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 1835 | : llvm::AtomicRMWInst::Min) |
| 1836 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 1837 | : llvm::AtomicRMWInst::UMin); |
| 1838 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1839 | case BO_Assign: |
| 1840 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 1841 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1842 | case BO_Mul: |
| 1843 | case BO_Div: |
| 1844 | case BO_Rem: |
| 1845 | case BO_Shl: |
| 1846 | case BO_Shr: |
| 1847 | case BO_LAnd: |
| 1848 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1849 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1850 | case BO_PtrMemD: |
| 1851 | case BO_PtrMemI: |
| 1852 | case BO_LE: |
| 1853 | case BO_GE: |
| 1854 | case BO_EQ: |
| 1855 | case BO_NE: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1856 | case BO_AddAssign: |
| 1857 | case BO_SubAssign: |
| 1858 | case BO_AndAssign: |
| 1859 | case BO_OrAssign: |
| 1860 | case BO_XorAssign: |
| 1861 | case BO_MulAssign: |
| 1862 | case BO_DivAssign: |
| 1863 | case BO_RemAssign: |
| 1864 | case BO_ShlAssign: |
| 1865 | case BO_ShrAssign: |
| 1866 | case BO_Comma: |
| 1867 | llvm_unreachable("Unsupported atomic update operation"); |
| 1868 | } |
| 1869 | auto *UpdateVal = Update.getScalarVal(); |
| 1870 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 1871 | UpdateVal = CGF.Builder.CreateIntCast( |
| 1872 | IC, X.getAddress()->getType()->getPointerElementType(), |
| 1873 | X.getType()->hasSignedIntegerRepresentation()); |
| 1874 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1875 | auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getAddress(), UpdateVal, AO); |
| 1876 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1879 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1880 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 1881 | llvm::AtomicOrdering AO, SourceLocation Loc, |
| 1882 | const llvm::function_ref<RValue(RValue)> &CommonGen) { |
| 1883 | // Update expressions are allowed to have the following forms: |
| 1884 | // x binop= expr; -> xrval + expr; |
| 1885 | // x++, ++x -> xrval + 1; |
| 1886 | // x--, --x -> xrval - 1; |
| 1887 | // x = x binop expr; -> xrval binop expr |
| 1888 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1889 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 1890 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1891 | if (X.isGlobalReg()) { |
| 1892 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 1893 | // 'xrval'. |
| 1894 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 1895 | } else { |
| 1896 | // Perform compare-and-swap procedure. |
| 1897 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 1898 | } |
| 1899 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1900 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 1904 | const Expr *X, const Expr *E, |
| 1905 | const Expr *UE, bool IsXLHSInRHSPart, |
| 1906 | SourceLocation Loc) { |
| 1907 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 1908 | "Update expr in 'atomic update' must be a binary operator."); |
| 1909 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 1910 | // Update expressions are allowed to have the following forms: |
| 1911 | // x binop= expr; -> xrval + expr; |
| 1912 | // x++, ++x -> xrval + 1; |
| 1913 | // x--, --x -> xrval - 1; |
| 1914 | // x = x binop expr; -> xrval binop expr |
| 1915 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1916 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 1917 | LValue XLValue = CGF.EmitLValue(X); |
| 1918 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 1919 | auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1920 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 1921 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 1922 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 1923 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 1924 | auto Gen = |
| 1925 | [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { |
| 1926 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 1927 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 1928 | return CGF.EmitAnyExpr(UE); |
| 1929 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 1930 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 1931 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 1932 | // OpenMP, 2.12.6, atomic Construct |
| 1933 | // Any atomic construct with a seq_cst clause forces the atomically |
| 1934 | // performed operation to include an implicit flush operation without a |
| 1935 | // list. |
| 1936 | if (IsSeqCst) |
| 1937 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 1938 | } |
| 1939 | |
| 1940 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
| 1941 | QualType SourceType, QualType ResType) { |
| 1942 | switch (CGF.getEvaluationKind(ResType)) { |
| 1943 | case TEK_Scalar: |
| 1944 | return RValue::get(convertToScalarValue(CGF, Value, SourceType, ResType)); |
| 1945 | case TEK_Complex: { |
| 1946 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType); |
| 1947 | return RValue::getComplex(Res.first, Res.second); |
| 1948 | } |
| 1949 | case TEK_Aggregate: |
| 1950 | break; |
| 1951 | } |
| 1952 | llvm_unreachable("Must be a scalar or complex."); |
| 1953 | } |
| 1954 | |
| 1955 | static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 1956 | bool IsPostfixUpdate, const Expr *V, |
| 1957 | const Expr *X, const Expr *E, |
| 1958 | const Expr *UE, bool IsXLHSInRHSPart, |
| 1959 | SourceLocation Loc) { |
| 1960 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 1961 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 1962 | RValue NewVVal; |
| 1963 | LValue VLValue = CGF.EmitLValue(V); |
| 1964 | LValue XLValue = CGF.EmitLValue(X); |
| 1965 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
| 1966 | auto AO = IsSeqCst ? llvm::SequentiallyConsistent : llvm::Monotonic; |
| 1967 | QualType NewVValType; |
| 1968 | if (UE) { |
| 1969 | // 'x' is updated with some additional value. |
| 1970 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 1971 | "Update expr in 'atomic capture' must be a binary operator."); |
| 1972 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 1973 | // Update expressions are allowed to have the following forms: |
| 1974 | // x binop= expr; -> xrval + expr; |
| 1975 | // x++, ++x -> xrval + 1; |
| 1976 | // x--, --x -> xrval - 1; |
| 1977 | // x = x binop expr; -> xrval binop expr |
| 1978 | // x = expr Op x; - > expr binop xrval; |
| 1979 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 1980 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 1981 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 1982 | NewVValType = XRValExpr->getType(); |
| 1983 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 1984 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
| 1985 | IsSeqCst, IsPostfixUpdate](RValue XRValue) -> RValue { |
| 1986 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 1987 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 1988 | RValue Res = CGF.EmitAnyExpr(UE); |
| 1989 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 1990 | return Res; |
| 1991 | }; |
| 1992 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 1993 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 1994 | if (Res.first) { |
| 1995 | // 'atomicrmw' instruction was generated. |
| 1996 | if (IsPostfixUpdate) { |
| 1997 | // Use old value from 'atomicrmw'. |
| 1998 | NewVVal = Res.second; |
| 1999 | } else { |
| 2000 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 2001 | // value of 'x'. |
| 2002 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 2003 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 2004 | NewVVal = CGF.EmitAnyExpr(UE); |
| 2005 | } |
| 2006 | } |
| 2007 | } else { |
| 2008 | // 'x' is simply rewritten with some 'expr'. |
| 2009 | NewVValType = X->getType().getNonReferenceType(); |
| 2010 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
| 2011 | X->getType().getNonReferenceType()); |
| 2012 | auto &&Gen = [&CGF, &NewVVal, ExprRValue](RValue XRValue) -> RValue { |
| 2013 | NewVVal = XRValue; |
| 2014 | return ExprRValue; |
| 2015 | }; |
| 2016 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 2017 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 2018 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 2019 | Loc, Gen); |
| 2020 | if (Res.first) { |
| 2021 | // 'atomicrmw' instruction was generated. |
| 2022 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 2023 | } |
| 2024 | } |
| 2025 | // Emit post-update store to 'v' of old/new 'x' value. |
| 2026 | emitSimpleStore(CGF, VLValue, NewVVal, NewVValType); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2027 | // OpenMP, 2.12.6, atomic Construct |
| 2028 | // Any atomic construct with a seq_cst clause forces the atomically |
| 2029 | // performed operation to include an implicit flush operation without a |
| 2030 | // list. |
| 2031 | if (IsSeqCst) |
| 2032 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 2033 | } |
| 2034 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2035 | static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2036 | bool IsSeqCst, bool IsPostfixUpdate, |
| 2037 | const Expr *X, const Expr *V, const Expr *E, |
| 2038 | const Expr *UE, bool IsXLHSInRHSPart, |
| 2039 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2040 | switch (Kind) { |
| 2041 | case OMPC_read: |
| 2042 | EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
| 2043 | break; |
| 2044 | case OMPC_write: |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 2045 | EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
| 2046 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2047 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2048 | case OMPC_update: |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 2049 | EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
| 2050 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2051 | case OMPC_capture: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2052 | EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
| 2053 | IsXLHSInRHSPart, Loc); |
| 2054 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2055 | case OMPC_if: |
| 2056 | case OMPC_final: |
| 2057 | case OMPC_num_threads: |
| 2058 | case OMPC_private: |
| 2059 | case OMPC_firstprivate: |
| 2060 | case OMPC_lastprivate: |
| 2061 | case OMPC_reduction: |
| 2062 | case OMPC_safelen: |
| 2063 | case OMPC_collapse: |
| 2064 | case OMPC_default: |
| 2065 | case OMPC_seq_cst: |
| 2066 | case OMPC_shared: |
| 2067 | case OMPC_linear: |
| 2068 | case OMPC_aligned: |
| 2069 | case OMPC_copyin: |
| 2070 | case OMPC_copyprivate: |
| 2071 | case OMPC_flush: |
| 2072 | case OMPC_proc_bind: |
| 2073 | case OMPC_schedule: |
| 2074 | case OMPC_ordered: |
| 2075 | case OMPC_nowait: |
| 2076 | case OMPC_untied: |
| 2077 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 2078 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2079 | case OMPC_mergeable: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 2080 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 2081 | } |
| 2082 | } |
| 2083 | |
| 2084 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
| 2085 | bool IsSeqCst = S.getSingleClause(/*K=*/OMPC_seq_cst); |
| 2086 | OpenMPClauseKind Kind = OMPC_unknown; |
| 2087 | for (auto *C : S.clauses()) { |
| 2088 | // Find first clause (skip seq_cst clause, if it is first). |
| 2089 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 2090 | Kind = C->getClauseKind(); |
| 2091 | break; |
| 2092 | } |
| 2093 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 2094 | |
| 2095 | const auto *CS = |
| 2096 | S.getAssociatedStmt()->IgnoreContainers(/*IgnoreCaptured=*/true); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2097 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 2098 | enterFullExpression(EWC); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2099 | } |
| 2100 | // Processing for statements under 'atomic capture'. |
| 2101 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
| 2102 | for (const auto *C : Compound->body()) { |
| 2103 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { |
| 2104 | enterFullExpression(EWC); |
| 2105 | } |
| 2106 | } |
| 2107 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 2108 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2109 | LexicalScope Scope(*this, S.getSourceRange()); |
| 2110 | auto &&CodeGen = [&S, Kind, IsSeqCst](CodeGenFunction &CGF) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 2111 | EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
| 2112 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
| 2113 | S.isXLHSInRHSPart(), S.getLocStart()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2114 | }; |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2115 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 2116 | } |
| 2117 | |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 2118 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &) { |
| 2119 | llvm_unreachable("CodeGen for 'omp target' is not supported yet."); |
| 2120 | } |
| 2121 | |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 2122 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &) { |
| 2123 | llvm_unreachable("CodeGen for 'omp teams' is not supported yet."); |
| 2124 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2125 | |
| 2126 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 2127 | const OMPCancellationPointDirective &S) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2128 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), |
| 2129 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2132 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 2133 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), |
| 2134 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 2137 | CodeGenFunction::JumpDest |
| 2138 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
| 2139 | if (Kind == OMPD_parallel || Kind == OMPD_task) |
| 2140 | return ReturnBlock; |
| 2141 | else if (Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections) |
| 2142 | return BreakContinueStack.empty() ? JumpDest() |
| 2143 | : BreakContinueStack.back().BreakBlock; |
| 2144 | return JumpDest(); |
| 2145 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 2146 | |
| 2147 | // Generate the instructions for '#pragma omp target data' directive. |
| 2148 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 2149 | const OMPTargetDataDirective &S) { |
| 2150 | |
| 2151 | // emit the code inside the construct for now |
| 2152 | auto CS = cast<CapturedStmt>(S.getAssociatedStmt()); |
| 2153 | EmitStmt(CS->getCapturedStmt()); |
| 2154 | } |