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