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