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