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