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 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 14 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "CGOpenMPRuntime.h" |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "TargetInfo.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 19 | #include "clang/AST/Stmt.h" |
| 20 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | /// Lexical scope for OpenMP executable constructs, that handles correct codegen |
| 28 | /// for captured expressions. |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 29 | class OMPLexicalScope : public CodeGenFunction::LexicalScope { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 30 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 31 | for (const auto *C : S.clauses()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 32 | if (const auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 33 | if (const auto *PreInit = |
| 34 | cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 35 | for (const auto *I : PreInit->decls()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 36 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 37 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 38 | } else { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 39 | CodeGenFunction::AutoVarEmission Emission = |
| 40 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 41 | CGF.EmitAutoVarCleanups(Emission); |
| 42 | } |
| 43 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 48 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 49 | |
| 50 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 51 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 52 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 53 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl)); |
| 54 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 55 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 56 | public: |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 57 | OMPLexicalScope( |
| 58 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 59 | const llvm::Optional<OpenMPDirectiveKind> CapturedRegion = llvm::None, |
| 60 | const bool EmitPreInitStmt = true) |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 61 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 62 | InlinedShareds(CGF) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 63 | if (EmitPreInitStmt) |
| 64 | emitPreInitStmt(CGF, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 65 | if (!CapturedRegion.hasValue()) |
| 66 | return; |
| 67 | assert(S.hasAssociatedStmt() && |
| 68 | "Expected associated statement for inlined directive."); |
| 69 | const CapturedStmt *CS = S.getCapturedStmt(*CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 70 | for (const auto &C : CS->captures()) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 71 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 72 | auto *VD = C.getCapturedVar(); |
| 73 | assert(VD == VD->getCanonicalDecl() && |
| 74 | "Canonical decl must be captured."); |
| 75 | DeclRefExpr DRE( |
| 76 | const_cast<VarDecl *>(VD), |
| 77 | isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo && |
| 78 | InlinedShareds.isGlobalVarCaptured(VD)), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 79 | VD->getType().getNonReferenceType(), VK_LValue, C.getLocation()); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 80 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 81 | return CGF.EmitLValue(&DRE).getAddress(); |
| 82 | }); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 85 | (void)InlinedShareds.Privatize(); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 86 | } |
| 87 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 88 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 89 | /// Lexical scope for OpenMP parallel construct, that handles correct codegen |
| 90 | /// for captured expressions. |
| 91 | class OMPParallelScope final : public OMPLexicalScope { |
| 92 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 93 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 94 | return !(isOpenMPTargetExecutionDirective(Kind) || |
| 95 | isOpenMPLoopBoundSharingDirective(Kind)) && |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 96 | isOpenMPParallelDirective(Kind); |
| 97 | } |
| 98 | |
| 99 | public: |
| 100 | OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 101 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 102 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 105 | /// Lexical scope for OpenMP teams construct, that handles correct codegen |
| 106 | /// for captured expressions. |
| 107 | class OMPTeamsScope final : public OMPLexicalScope { |
| 108 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 109 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
| 110 | return !isOpenMPTargetExecutionDirective(Kind) && |
| 111 | isOpenMPTeamsDirective(Kind); |
| 112 | } |
| 113 | |
| 114 | public: |
| 115 | OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 116 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 117 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 120 | /// Private scope for OpenMP loop-based directives, that supports capturing |
| 121 | /// of used expression from loop statement. |
| 122 | class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { |
| 123 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 124 | CodeGenFunction::OMPMapVars PreCondVars; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 125 | for (const auto *E : S.counters()) { |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 126 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 127 | (void)PreCondVars.setVarAddr( |
| 128 | CGF, VD, CGF.CreateMemTemp(VD->getType().getNonReferenceType())); |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 129 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 130 | (void)PreCondVars.apply(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 131 | if (const auto *PreInits = cast_or_null<DeclStmt>(S.getPreInits())) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 132 | for (const auto *I : PreInits->decls()) |
| 133 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 134 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 135 | PreCondVars.restore(CGF); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | public: |
| 139 | OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) |
| 140 | : CodeGenFunction::RunCleanupsScope(CGF) { |
| 141 | emitPreInitStmt(CGF, S); |
| 142 | } |
| 143 | }; |
| 144 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 145 | class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope { |
| 146 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 147 | |
| 148 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 149 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 150 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 151 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && |
| 152 | cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); |
| 153 | } |
| 154 | |
| 155 | public: |
| 156 | OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 157 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 158 | InlinedShareds(CGF) { |
| 159 | for (const auto *C : S.clauses()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 160 | if (const auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 161 | if (const auto *PreInit = |
| 162 | cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 163 | for (const auto *I : PreInit->decls()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 164 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 165 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 166 | } else { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 167 | CodeGenFunction::AutoVarEmission Emission = |
| 168 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 169 | CGF.EmitAutoVarCleanups(Emission); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) { |
| 174 | for (const Expr *E : UDP->varlists()) { |
| 175 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 176 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D)) |
| 177 | CGF.EmitVarDecl(*OED); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 182 | CGF.EmitOMPPrivateClause(S, InlinedShareds); |
| 183 | if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) { |
| 184 | if (const Expr *E = TG->getReductionRef()) |
| 185 | CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())); |
| 186 | } |
| 187 | const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt()); |
| 188 | while (CS) { |
| 189 | for (auto &C : CS->captures()) { |
| 190 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 191 | auto *VD = C.getCapturedVar(); |
| 192 | assert(VD == VD->getCanonicalDecl() && |
| 193 | "Canonical decl must be captured."); |
| 194 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 195 | isCapturedVar(CGF, VD) || |
| 196 | (CGF.CapturedStmtInfo && |
| 197 | InlinedShareds.isGlobalVarCaptured(VD)), |
| 198 | VD->getType().getNonReferenceType(), VK_LValue, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 199 | C.getLocation()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 200 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 201 | return CGF.EmitLValue(&DRE).getAddress(); |
| 202 | }); |
| 203 | } |
| 204 | } |
| 205 | CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt()); |
| 206 | } |
| 207 | (void)InlinedShareds.Privatize(); |
| 208 | } |
| 209 | }; |
| 210 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 211 | } // namespace |
| 212 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 213 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 214 | const OMPExecutableDirective &S, |
| 215 | const RegionCodeGenTy &CodeGen); |
| 216 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 217 | LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 218 | if (const auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) { |
| 219 | if (const auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 220 | OrigVD = OrigVD->getCanonicalDecl(); |
| 221 | bool IsCaptured = |
| 222 | LambdaCaptureFields.lookup(OrigVD) || |
| 223 | (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) || |
| 224 | (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)); |
| 225 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), IsCaptured, |
| 226 | OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc()); |
| 227 | return EmitLValue(&DRE); |
| 228 | } |
| 229 | } |
| 230 | return EmitLValue(E); |
| 231 | } |
| 232 | |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 233 | llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 234 | ASTContext &C = getContext(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 235 | llvm::Value *Size = nullptr; |
| 236 | auto SizeInChars = C.getTypeSizeInChars(Ty); |
| 237 | if (SizeInChars.isZero()) { |
| 238 | // getTypeSizeInChars() returns 0 for a VLA. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 239 | while (const VariableArrayType *VAT = C.getAsVariableArrayType(Ty)) { |
| 240 | VlaSizePair VlaSize = getVLASize(VAT); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 241 | Ty = VlaSize.Type; |
| 242 | Size = Size ? Builder.CreateNUWMul(Size, VlaSize.NumElts) |
| 243 | : VlaSize.NumElts; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 244 | } |
| 245 | SizeInChars = C.getTypeSizeInChars(Ty); |
| 246 | if (SizeInChars.isZero()) |
| 247 | return llvm::ConstantInt::get(SizeTy, /*V=*/0); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 248 | return Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); |
| 249 | } |
| 250 | return CGM.getSize(SizeInChars); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 253 | void CodeGenFunction::GenerateOpenMPCapturedVars( |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 254 | const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 255 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 256 | auto CurField = RD->field_begin(); |
| 257 | auto CurCap = S.captures().begin(); |
| 258 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 259 | E = S.capture_init_end(); |
| 260 | I != E; ++I, ++CurField, ++CurCap) { |
| 261 | if (CurField->hasCapturedVLAType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 262 | const VariableArrayType *VAT = CurField->getCapturedVLAType(); |
| 263 | llvm::Value *Val = VLASizeMap[VAT->getSizeExpr()]; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 264 | CapturedVars.push_back(Val); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 265 | } else if (CurCap->capturesThis()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 266 | CapturedVars.push_back(CXXThisValue); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 267 | } else if (CurCap->capturesVariableByCopy()) { |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 268 | llvm::Value *CV = EmitLoadOfScalar(EmitLValue(*I), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 269 | |
| 270 | // If the field is not a pointer, we need to save the actual value |
| 271 | // and load it as a void pointer. |
| 272 | if (!CurField->getType()->isAnyPointerType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 273 | ASTContext &Ctx = getContext(); |
| 274 | Address DstAddr = CreateMemTemp( |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 275 | Ctx.getUIntPtrType(), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 276 | Twine(CurCap->getCapturedVar()->getName(), ".casted")); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 277 | LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 278 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 279 | llvm::Value *SrcAddrVal = EmitScalarConversion( |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 280 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 281 | Ctx.getPointerType(CurField->getType()), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 282 | LValue SrcLV = |
| 283 | MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); |
| 284 | |
| 285 | // Store the value using the source type pointer. |
| 286 | EmitStoreThroughLValue(RValue::get(CV), SrcLV); |
| 287 | |
| 288 | // Load the value using the destination type pointer. |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 289 | CV = EmitLoadOfScalar(DstLV, CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 290 | } |
| 291 | CapturedVars.push_back(CV); |
| 292 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 293 | assert(CurCap->capturesVariable() && "Expected capture by reference."); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 294 | CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 295 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 299 | static Address castValueFromUintptr(CodeGenFunction &CGF, SourceLocation Loc, |
| 300 | QualType DstType, StringRef Name, |
| 301 | LValue AddrLV, |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 302 | bool isReferenceType = false) { |
| 303 | ASTContext &Ctx = CGF.getContext(); |
| 304 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 305 | llvm::Value *CastedPtr = CGF.EmitScalarConversion( |
| 306 | AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(), |
| 307 | Ctx.getPointerType(DstType), Loc); |
| 308 | Address TmpAddr = |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 309 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
| 310 | .getAddress(); |
| 311 | |
| 312 | // If we are dealing with references we need to return the address of the |
| 313 | // reference instead of the reference of the value. |
| 314 | if (isReferenceType) { |
| 315 | QualType RefType = Ctx.getLValueReferenceType(DstType); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 316 | llvm::Value *RefVal = TmpAddr.getPointer(); |
| 317 | TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name, ".ref")); |
| 318 | LValue TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType); |
| 319 | CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit=*/true); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return TmpAddr; |
| 323 | } |
| 324 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 325 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 326 | if (T->isLValueReferenceType()) |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 327 | return C.getLValueReferenceType( |
| 328 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 329 | /*SpelledAsLValue=*/false); |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 330 | if (T->isPointerType()) |
| 331 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 332 | if (const ArrayType *A = T->getAsArrayTypeUnsafe()) { |
| 333 | if (const auto *VLA = dyn_cast<VariableArrayType>(A)) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 334 | return getCanonicalParamType(C, VLA->getElementType()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 335 | if (!A->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 336 | return C.getCanonicalType(T); |
| 337 | } |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 338 | return C.getCanonicalParamType(T); |
| 339 | } |
| 340 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 341 | namespace { |
| 342 | /// Contains required data for proper outlined function codegen. |
| 343 | struct FunctionOptions { |
| 344 | /// Captured statement for which the function is generated. |
| 345 | const CapturedStmt *S = nullptr; |
| 346 | /// true if cast to/from UIntPtr is required for variables captured by |
| 347 | /// value. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 348 | const bool UIntPtrCastRequired = true; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 349 | /// true if only casted arguments must be registered as local args or VLA |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 350 | /// sizes. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 351 | const bool RegisterCastedArgsOnly = false; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 352 | /// Name of the generated function. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 353 | const StringRef FunctionName; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 354 | explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, |
| 355 | bool RegisterCastedArgsOnly, |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 356 | StringRef FunctionName) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 357 | : S(S), UIntPtrCastRequired(UIntPtrCastRequired), |
| 358 | RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 359 | FunctionName(FunctionName) {} |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 360 | }; |
| 361 | } |
| 362 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 363 | static llvm::Function *emitOutlinedFunctionPrologue( |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 364 | CodeGenFunction &CGF, FunctionArgList &Args, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 365 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 366 | &LocalAddrs, |
| 367 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> |
| 368 | &VLASizes, |
| 369 | llvm::Value *&CXXThisValue, const FunctionOptions &FO) { |
| 370 | const CapturedDecl *CD = FO.S->getCapturedDecl(); |
| 371 | const RecordDecl *RD = FO.S->getCapturedRecordDecl(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 372 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 373 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 374 | CXXThisValue = nullptr; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 375 | // Build the argument list. |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 376 | CodeGenModule &CGM = CGF.CGM; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 377 | ASTContext &Ctx = CGM.getContext(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 378 | FunctionArgList TargetArgs; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 379 | Args.append(CD->param_begin(), |
| 380 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 381 | TargetArgs.append( |
| 382 | CD->param_begin(), |
| 383 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 384 | auto I = FO.S->captures().begin(); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 385 | FunctionDecl *DebugFunctionDecl = nullptr; |
| 386 | if (!FO.UIntPtrCastRequired) { |
| 387 | FunctionProtoType::ExtProtoInfo EPI; |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 388 | QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 389 | DebugFunctionDecl = FunctionDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 390 | Ctx, Ctx.getTranslationUnitDecl(), FO.S->getBeginLoc(), |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 391 | SourceLocation(), DeclarationName(), FunctionTy, |
| 392 | Ctx.getTrivialTypeSourceInfo(FunctionTy), SC_Static, |
| 393 | /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 394 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 395 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 396 | QualType ArgType = FD->getType(); |
| 397 | IdentifierInfo *II = nullptr; |
| 398 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 399 | |
| 400 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 401 | // function argument type should be uintptr and the value properly casted to |
| 402 | // uintptr. This is necessary given that the runtime library is only able to |
| 403 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 404 | // outlined function. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 405 | if (FO.UIntPtrCastRequired && |
| 406 | ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
| 407 | I->capturesVariableArrayType())) |
| 408 | ArgType = Ctx.getUIntPtrType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 409 | |
| 410 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 411 | CapVar = I->getCapturedVar(); |
| 412 | II = CapVar->getIdentifier(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 413 | } else if (I->capturesThis()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 414 | II = &Ctx.Idents.get("this"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 415 | } else { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 416 | assert(I->capturesVariableArrayType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 417 | II = &Ctx.Idents.get("vla"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 418 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 419 | if (ArgType->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 420 | ArgType = getCanonicalParamType(Ctx, ArgType); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 421 | VarDecl *Arg; |
| 422 | if (DebugFunctionDecl && (CapVar || I->capturesThis())) { |
| 423 | Arg = ParmVarDecl::Create( |
| 424 | Ctx, DebugFunctionDecl, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 425 | CapVar ? CapVar->getBeginLoc() : FD->getBeginLoc(), |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 426 | CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, |
| 427 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
| 428 | } else { |
| 429 | Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), |
| 430 | II, ArgType, ImplicitParamDecl::Other); |
| 431 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 432 | Args.emplace_back(Arg); |
| 433 | // Do not cast arguments if we emit function with non-original types. |
| 434 | TargetArgs.emplace_back( |
| 435 | FO.UIntPtrCastRequired |
| 436 | ? Arg |
| 437 | : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 438 | ++I; |
| 439 | } |
| 440 | Args.append( |
| 441 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 442 | CD->param_end()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 443 | TargetArgs.append( |
| 444 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 445 | CD->param_end()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 446 | |
| 447 | // Create the function declaration. |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 448 | const CGFunctionInfo &FuncInfo = |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 449 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 450 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 451 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 452 | auto *F = |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 453 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 454 | FO.FunctionName, &CGM.getModule()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 455 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 456 | if (CD->isNothrow()) |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 457 | F->setDoesNotThrow(); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 458 | F->setDoesNotRecurse(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 459 | |
| 460 | // Generate the function. |
Alexey Bataev | 6e01dc1 | 2017-08-14 16:03:47 +0000 | [diff] [blame] | 461 | CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 462 | FO.S->getBeginLoc(), CD->getBody()->getBeginLoc()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 463 | unsigned Cnt = CD->getContextParamPosition(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 464 | I = FO.S->captures().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 465 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 466 | // Do not map arguments if we emit function with non-original types. |
| 467 | Address LocalAddr(Address::invalid()); |
| 468 | if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) { |
| 469 | LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt], |
| 470 | TargetArgs[Cnt]); |
| 471 | } else { |
| 472 | LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]); |
| 473 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 474 | // If we are capturing a pointer by copy we don't need to do anything, just |
| 475 | // use the value that we get from the arguments. |
| 476 | if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 477 | const VarDecl *CurVD = I->getCapturedVar(); |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 478 | // If the variable is a reference we need to materialize it here. |
| 479 | if (CurVD->getType()->isReferenceType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 480 | Address RefAddr = CGF.CreateMemTemp( |
| 481 | CurVD->getType(), CGM.getPointerAlign(), ".materialized_ref"); |
| 482 | CGF.EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr, |
| 483 | /*Volatile=*/false, CurVD->getType()); |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 484 | LocalAddr = RefAddr; |
| 485 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 486 | if (!FO.RegisterCastedArgsOnly) |
| 487 | LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}}); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 488 | ++Cnt; |
| 489 | ++I; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 490 | continue; |
| 491 | } |
| 492 | |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 493 | LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), |
| 494 | AlignmentSource::Decl); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 495 | if (FD->hasCapturedVLAType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 496 | if (FO.UIntPtrCastRequired) { |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 497 | ArgLVal = CGF.MakeAddrLValue( |
| 498 | castValueFromUintptr(CGF, I->getLocation(), FD->getType(), |
| 499 | Args[Cnt]->getName(), ArgLVal), |
| 500 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 501 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 502 | llvm::Value *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
| 503 | const VariableArrayType *VAT = FD->getCapturedVLAType(); |
| 504 | VLASizes.try_emplace(Args[Cnt], VAT->getSizeExpr(), ExprArg); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 505 | } else if (I->capturesVariable()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 506 | const VarDecl *Var = I->getCapturedVar(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 507 | QualType VarTy = Var->getType(); |
| 508 | Address ArgAddr = ArgLVal.getAddress(); |
| 509 | if (!VarTy->isReferenceType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 510 | if (ArgLVal.getType()->isLValueReferenceType()) { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 511 | ArgAddr = CGF.EmitLoadOfReference(ArgLVal); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 512 | } else if (!VarTy->isVariablyModifiedType() || |
| 513 | !VarTy->isPointerType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 514 | assert(ArgLVal.getType()->isPointerType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 515 | ArgAddr = CGF.EmitLoadOfPointer( |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 516 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
| 517 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 518 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 519 | if (!FO.RegisterCastedArgsOnly) { |
| 520 | LocalAddrs.insert( |
| 521 | {Args[Cnt], |
| 522 | {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); |
| 523 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 524 | } else if (I->capturesVariableByCopy()) { |
| 525 | assert(!FD->getType()->isAnyPointerType() && |
| 526 | "Not expecting a captured pointer."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 527 | const VarDecl *Var = I->getCapturedVar(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 528 | QualType VarTy = Var->getType(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 529 | LocalAddrs.insert( |
| 530 | {Args[Cnt], |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 531 | {Var, FO.UIntPtrCastRequired |
| 532 | ? castValueFromUintptr(CGF, I->getLocation(), |
| 533 | FD->getType(), Args[Cnt]->getName(), |
| 534 | ArgLVal, VarTy->isReferenceType()) |
| 535 | : ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 536 | } else { |
| 537 | // If 'this' is captured, load it into CXXThisValue. |
| 538 | assert(I->capturesThis()); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 539 | CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 540 | LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 541 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 542 | ++Cnt; |
| 543 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 546 | return F; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | llvm::Function * |
| 550 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { |
| 551 | assert( |
| 552 | CapturedStmtInfo && |
| 553 | "CapturedStmtInfo should be set when generating the captured function"); |
| 554 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 555 | // Build the argument list. |
| 556 | bool NeedWrapperFunction = |
| 557 | getDebugInfo() && |
| 558 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo; |
| 559 | FunctionArgList Args; |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 560 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 561 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 562 | SmallString<256> Buffer; |
| 563 | llvm::raw_svector_ostream Out(Buffer); |
| 564 | Out << CapturedStmtInfo->getHelperName(); |
| 565 | if (NeedWrapperFunction) |
| 566 | Out << "_debug__"; |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 567 | FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 568 | Out.str()); |
| 569 | llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, |
| 570 | VLASizes, CXXThisValue, FO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 571 | for (const auto &LocalAddrPair : LocalAddrs) { |
| 572 | if (LocalAddrPair.second.first) { |
| 573 | setAddrOfLocalVar(LocalAddrPair.second.first, |
| 574 | LocalAddrPair.second.second); |
| 575 | } |
| 576 | } |
| 577 | for (const auto &VLASizePair : VLASizes) |
| 578 | VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 579 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 580 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 581 | FinishFunction(CD->getBodyRBrace()); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 582 | if (!NeedWrapperFunction) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 583 | return F; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 584 | |
Alexey Bataev | efd884d | 2017-08-04 21:26:25 +0000 | [diff] [blame] | 585 | FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 586 | /*RegisterCastedArgsOnly=*/true, |
| 587 | CapturedStmtInfo->getHelperName()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 588 | CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 589 | WrapperCGF.CapturedStmtInfo = CapturedStmtInfo; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 590 | Args.clear(); |
| 591 | LocalAddrs.clear(); |
| 592 | VLASizes.clear(); |
| 593 | llvm::Function *WrapperF = |
| 594 | emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 595 | WrapperCGF.CXXThisValue, WrapperFO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 596 | llvm::SmallVector<llvm::Value *, 4> CallArgs; |
| 597 | for (const auto *Arg : Args) { |
| 598 | llvm::Value *CallArg; |
| 599 | auto I = LocalAddrs.find(Arg); |
| 600 | if (I != LocalAddrs.end()) { |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 601 | LValue LV = WrapperCGF.MakeAddrLValue( |
| 602 | I->second.second, |
| 603 | I->second.first ? I->second.first->getType() : Arg->getType(), |
| 604 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 605 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 606 | } else { |
| 607 | auto EI = VLASizes.find(Arg); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 608 | if (EI != VLASizes.end()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 609 | CallArg = EI->second.second; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 610 | } else { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 611 | LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 612 | Arg->getType(), |
| 613 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 614 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 617 | CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 618 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 619 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 620 | F, CallArgs); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 621 | WrapperCGF.FinishFunction(); |
| 622 | return WrapperF; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 625 | //===----------------------------------------------------------------------===// |
| 626 | // OpenMP Directive Emission |
| 627 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 628 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 629 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 630 | const llvm::function_ref<void(Address, Address)> CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 631 | // Perform element-by-element initialization. |
| 632 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 633 | |
| 634 | // Drill down to the base element type on both arrays. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 635 | const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
| 636 | llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 637 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 638 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 639 | llvm::Value *SrcBegin = SrcAddr.getPointer(); |
| 640 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 641 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 642 | llvm::Value *DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 643 | // The basic structure here is a while-do loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 644 | llvm::BasicBlock *BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 645 | llvm::BasicBlock *DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 646 | llvm::Value *IsEmpty = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 647 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 648 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 649 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 650 | // Enter the loop body, making that address the current address. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 651 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 652 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 653 | |
| 654 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 655 | |
| 656 | llvm::PHINode *SrcElementPHI = |
| 657 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 658 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 659 | Address SrcElementCurrent = |
| 660 | Address(SrcElementPHI, |
| 661 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 662 | |
| 663 | llvm::PHINode *DestElementPHI = |
| 664 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 665 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 666 | Address DestElementCurrent = |
| 667 | Address(DestElementPHI, |
| 668 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 669 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 670 | // Emit copy. |
| 671 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 672 | |
| 673 | // Shift the address forward by one element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 674 | llvm::Value *DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 675 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 676 | llvm::Value *SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 677 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 678 | // Check whether we've reached the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 679 | llvm::Value *Done = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 680 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 681 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 682 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 683 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 684 | |
| 685 | // Done. |
| 686 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 687 | } |
| 688 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 689 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 690 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 691 | const VarDecl *SrcVD, const Expr *Copy) { |
| 692 | if (OriginalType->isArrayType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 693 | const auto *BO = dyn_cast<BinaryOperator>(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 694 | if (BO && BO->getOpcode() == BO_Assign) { |
| 695 | // Perform simple memcpy for simple copying. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 696 | LValue Dest = MakeAddrLValue(DestAddr, OriginalType); |
| 697 | LValue Src = MakeAddrLValue(SrcAddr, OriginalType); |
| 698 | EmitAggregateAssign(Dest, Src, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 699 | } else { |
| 700 | // For arrays with complex element types perform element by element |
| 701 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 702 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 703 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 704 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 705 | // Working with the single array element, so have to remap |
| 706 | // destination and source variables to corresponding array |
| 707 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 708 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 709 | Remap.addPrivate(DestVD, [DestElement]() { return DestElement; }); |
| 710 | Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 711 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 712 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 713 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 714 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 715 | } else { |
| 716 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 717 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 718 | Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; }); |
| 719 | Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 720 | (void)Remap.Privatize(); |
| 721 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 722 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 723 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 726 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 727 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 728 | if (!HaveInsertPoint()) |
| 729 | return false; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 730 | bool FirstprivateIsLastprivate = false; |
| 731 | llvm::DenseSet<const VarDecl *> Lastprivates; |
| 732 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 733 | for (const auto *D : C->varlists()) |
| 734 | Lastprivates.insert( |
| 735 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 736 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 737 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 738 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 739 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 740 | // Force emission of the firstprivate copy if the directive does not emit |
| 741 | // outlined function, like omp for, omp simd, omp distribute etc. |
| 742 | bool MustEmitFirstprivateCopy = |
| 743 | CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 744 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 745 | auto IRef = C->varlist_begin(); |
| 746 | auto InitsRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 747 | for (const Expr *IInit : C->private_copies()) { |
| 748 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 749 | bool ThisFirstprivateIsLastprivate = |
| 750 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 751 | const FieldDecl *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 752 | if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 753 | !FD->getType()->isReferenceType()) { |
| 754 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 755 | ++IRef; |
| 756 | ++InitsRef; |
| 757 | continue; |
| 758 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 759 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 760 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 761 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 762 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 763 | const auto *VDInit = |
| 764 | cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 765 | bool IsRegistered; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 766 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 767 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 768 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 769 | LValue OriginalLVal = EmitLValue(&DRE); |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 770 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 771 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 772 | // Emit VarDecl with copy init for arrays. |
| 773 | // Get the address of the original variable captured in current |
| 774 | // captured region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 775 | IsRegistered = PrivateScope.addPrivate( |
| 776 | OrigVD, [this, VD, Type, OriginalLVal, VDInit]() { |
| 777 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 778 | const Expr *Init = VD->getInit(); |
| 779 | if (!isa<CXXConstructExpr>(Init) || |
| 780 | isTrivialInitializer(Init)) { |
| 781 | // Perform simple memcpy. |
| 782 | LValue Dest = |
| 783 | MakeAddrLValue(Emission.getAllocatedAddress(), Type); |
| 784 | EmitAggregateAssign(Dest, OriginalLVal, Type); |
| 785 | } else { |
| 786 | EmitOMPAggregateAssign( |
| 787 | Emission.getAllocatedAddress(), OriginalLVal.getAddress(), |
| 788 | Type, |
| 789 | [this, VDInit, Init](Address DestElement, |
| 790 | Address SrcElement) { |
| 791 | // Clean up any temporaries needed by the |
| 792 | // initialization. |
| 793 | RunCleanupsScope InitScope(*this); |
| 794 | // Emit initialization for single element. |
| 795 | setAddrOfLocalVar(VDInit, SrcElement); |
| 796 | EmitAnyExprToMem(Init, DestElement, |
| 797 | Init->getType().getQualifiers(), |
| 798 | /*IsInitializer*/ false); |
| 799 | LocalDeclMap.erase(VDInit); |
| 800 | }); |
| 801 | } |
| 802 | EmitAutoVarCleanups(Emission); |
| 803 | return Emission.getAllocatedAddress(); |
| 804 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 805 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 806 | Address OriginalAddr = OriginalLVal.getAddress(); |
| 807 | IsRegistered = PrivateScope.addPrivate( |
| 808 | OrigVD, [this, VDInit, OriginalAddr, VD]() { |
| 809 | // Emit private VarDecl with copy init. |
| 810 | // Remap temp VDInit variable to the address of the original |
| 811 | // variable (for proper handling of captured global variables). |
| 812 | setAddrOfLocalVar(VDInit, OriginalAddr); |
| 813 | EmitDecl(*VD); |
| 814 | LocalDeclMap.erase(VDInit); |
| 815 | return GetAddrOfLocalVar(VD); |
| 816 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 817 | } |
| 818 | assert(IsRegistered && |
| 819 | "firstprivate var already registered as private"); |
| 820 | // Silence the warning about unused variable. |
| 821 | (void)IsRegistered; |
| 822 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 823 | ++IRef; |
| 824 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 827 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 828 | } |
| 829 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 830 | void CodeGenFunction::EmitOMPPrivateClause( |
| 831 | const OMPExecutableDirective &D, |
| 832 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 833 | if (!HaveInsertPoint()) |
| 834 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 835 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 836 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 837 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 838 | for (const Expr *IInit : C->private_copies()) { |
| 839 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 840 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 841 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 842 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
| 843 | // Emit private VarDecl with copy init. |
| 844 | EmitDecl(*VD); |
| 845 | return GetAddrOfLocalVar(VD); |
| 846 | }); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 847 | assert(IsRegistered && "private var already registered as private"); |
| 848 | // Silence the warning about unused variable. |
| 849 | (void)IsRegistered; |
| 850 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 851 | ++IRef; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 856 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 857 | if (!HaveInsertPoint()) |
| 858 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 859 | // threadprivate_var1 = master_threadprivate_var1; |
| 860 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 861 | // ... |
| 862 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 863 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 864 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 865 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 866 | auto IRef = C->varlist_begin(); |
| 867 | auto ISrcRef = C->source_exprs().begin(); |
| 868 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 869 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 870 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 871 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 872 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 873 | // Get the address of the master variable. If we are emitting code with |
| 874 | // TLS support, the address is passed from the master as field in the |
| 875 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 876 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 877 | if (getLangOpts().OpenMPUseTLS && |
| 878 | getContext().getTargetInfo().isTLSSupported()) { |
| 879 | assert(CapturedStmtInfo->lookup(VD) && |
| 880 | "Copyin threadprivates should have been captured!"); |
| 881 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(), |
| 882 | VK_LValue, (*IRef)->getExprLoc()); |
| 883 | MasterAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 884 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 885 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 886 | MasterAddr = |
| 887 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 888 | : CGM.GetAddrOfGlobal(VD), |
| 889 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 890 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 891 | // Get the address of the threadprivate variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 892 | Address PrivateAddr = EmitLValue(*IRef).getAddress(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 893 | if (CopiedVars.size() == 1) { |
| 894 | // At first check if current thread is a master thread. If it is, no |
| 895 | // need to copy data. |
| 896 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 897 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 898 | Builder.CreateCondBr( |
| 899 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 900 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 901 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), |
| 902 | CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 903 | CopyBegin, CopyEnd); |
| 904 | EmitBlock(CopyBegin); |
| 905 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 906 | const auto *SrcVD = |
| 907 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 908 | const auto *DestVD = |
| 909 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 910 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 911 | } |
| 912 | ++IRef; |
| 913 | ++ISrcRef; |
| 914 | ++IDestRef; |
| 915 | } |
| 916 | } |
| 917 | if (CopyEnd) { |
| 918 | // Exit out of copying procedure for non-master thread. |
| 919 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 920 | return true; |
| 921 | } |
| 922 | return false; |
| 923 | } |
| 924 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 925 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 926 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 927 | if (!HaveInsertPoint()) |
| 928 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 929 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 930 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 931 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 932 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 933 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 934 | SIMDLCVs.insert( |
| 935 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 936 | } |
| 937 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 938 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 939 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 940 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 941 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 942 | !getLangOpts().OpenMPSimd) |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 943 | break; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 944 | auto IRef = C->varlist_begin(); |
| 945 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 946 | for (const Expr *IInit : C->private_copies()) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 947 | // Keep the address of the original variable for future update at the end |
| 948 | // of the loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 949 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 950 | // Taskloops do not require additional initialization, it is done in |
| 951 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 952 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 953 | const auto *DestVD = |
| 954 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 955 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 956 | DeclRefExpr DRE( |
| 957 | const_cast<VarDecl *>(OrigVD), |
| 958 | /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( |
| 959 | OrigVD) != nullptr, |
| 960 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
| 961 | return EmitLValue(&DRE).getAddress(); |
| 962 | }); |
| 963 | // Check if the variable is also a firstprivate: in this case IInit is |
| 964 | // not generated. Initialization of this variable will happen in codegen |
| 965 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 966 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 967 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 968 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 969 | // Emit private VarDecl with copy init. |
| 970 | EmitDecl(*VD); |
| 971 | return GetAddrOfLocalVar(VD); |
| 972 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 973 | assert(IsRegistered && |
| 974 | "lastprivate var already registered as private"); |
| 975 | (void)IsRegistered; |
| 976 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 977 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 978 | ++IRef; |
| 979 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | return HasAtLeastOneLastprivate; |
| 983 | } |
| 984 | |
| 985 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 986 | const OMPExecutableDirective &D, bool NoFinals, |
| 987 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 988 | if (!HaveInsertPoint()) |
| 989 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 990 | // Emit following code: |
| 991 | // if (<IsLastIterCond>) { |
| 992 | // orig_var1 = private_orig_var1; |
| 993 | // ... |
| 994 | // orig_varn = private_orig_varn; |
| 995 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 996 | llvm::BasicBlock *ThenBB = nullptr; |
| 997 | llvm::BasicBlock *DoneBB = nullptr; |
| 998 | if (IsLastIterCond) { |
| 999 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 1000 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 1001 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 1002 | EmitBlock(ThenBB); |
| 1003 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1004 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 1005 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1006 | if (const auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1007 | auto IC = LoopDirective->counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1008 | for (const Expr *F : LoopDirective->finals()) { |
| 1009 | const auto *D = |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1010 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 1011 | if (NoFinals) |
| 1012 | AlreadyEmittedVars.insert(D); |
| 1013 | else |
| 1014 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1015 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1018 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 1019 | auto IRef = C->varlist_begin(); |
| 1020 | auto ISrcRef = C->source_exprs().begin(); |
| 1021 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1022 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 1023 | const auto *PrivateVD = |
| 1024 | cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1025 | QualType Type = PrivateVD->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1026 | const auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1027 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 1028 | // If lastprivate variable is a loop control variable for loop-based |
| 1029 | // directive, update its value before copyin back to original |
| 1030 | // variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1031 | if (const Expr *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1032 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1033 | const auto *SrcVD = |
| 1034 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 1035 | const auto *DestVD = |
| 1036 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1037 | // Get the address of the original variable. |
| 1038 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 1039 | // Get the address of the private variable. |
| 1040 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1041 | if (const auto *RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1042 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1043 | Address(Builder.CreateLoad(PrivateAddr), |
| 1044 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1045 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1046 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1047 | ++IRef; |
| 1048 | ++ISrcRef; |
| 1049 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1050 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1051 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1052 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1053 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1054 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1055 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1056 | } |
| 1057 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1058 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 1059 | const OMPExecutableDirective &D, |
| 1060 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1061 | if (!HaveInsertPoint()) |
| 1062 | return; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1063 | SmallVector<const Expr *, 4> Shareds; |
| 1064 | SmallVector<const Expr *, 4> Privates; |
| 1065 | SmallVector<const Expr *, 4> ReductionOps; |
| 1066 | SmallVector<const Expr *, 4> LHSs; |
| 1067 | SmallVector<const Expr *, 4> RHSs; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1068 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1069 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1070 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1071 | auto ILHS = C->lhs_exprs().begin(); |
| 1072 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1073 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1074 | Shareds.emplace_back(Ref); |
| 1075 | Privates.emplace_back(*IPriv); |
| 1076 | ReductionOps.emplace_back(*IRed); |
| 1077 | LHSs.emplace_back(*ILHS); |
| 1078 | RHSs.emplace_back(*IRHS); |
| 1079 | std::advance(IPriv, 1); |
| 1080 | std::advance(IRed, 1); |
| 1081 | std::advance(ILHS, 1); |
| 1082 | std::advance(IRHS, 1); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1085 | ReductionCodeGen RedCG(Shareds, Privates, ReductionOps); |
| 1086 | unsigned Count = 0; |
| 1087 | auto ILHS = LHSs.begin(); |
| 1088 | auto IRHS = RHSs.begin(); |
| 1089 | auto IPriv = Privates.begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1090 | for (const Expr *IRef : Shareds) { |
| 1091 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1092 | // Emit private VarDecl with reduction init. |
| 1093 | RedCG.emitSharedLValue(*this, Count); |
| 1094 | RedCG.emitAggregateType(*this, Count); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1095 | AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1096 | RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), |
| 1097 | RedCG.getSharedLValue(Count), |
| 1098 | [&Emission](CodeGenFunction &CGF) { |
| 1099 | CGF.EmitAutoVarInit(Emission); |
| 1100 | return true; |
| 1101 | }); |
| 1102 | EmitAutoVarCleanups(Emission); |
| 1103 | Address BaseAddr = RedCG.adjustPrivateAddress( |
| 1104 | *this, Count, Emission.getAllocatedAddress()); |
| 1105 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1106 | RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1107 | assert(IsRegistered && "private var already registered as private"); |
| 1108 | // Silence the warning about unused variable. |
| 1109 | (void)IsRegistered; |
| 1110 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1111 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 1112 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1113 | QualType Type = PrivateVD->getType(); |
| 1114 | bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); |
| 1115 | if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1116 | // Store the address of the original variable associated with the LHS |
| 1117 | // implicit variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1118 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1119 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1120 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1121 | PrivateScope.addPrivate( |
| 1122 | RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); }); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1123 | } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || |
| 1124 | isa<ArraySubscriptExpr>(IRef)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1125 | // Store the address of the original variable associated with the LHS |
| 1126 | // implicit variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1127 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1128 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1129 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1130 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1131 | return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), |
| 1132 | ConvertTypeForMem(RHSVD->getType()), |
| 1133 | "rhs.begin"); |
| 1134 | }); |
| 1135 | } else { |
| 1136 | QualType Type = PrivateVD->getType(); |
| 1137 | bool IsArray = getContext().getAsArrayType(Type) != nullptr; |
| 1138 | Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(); |
| 1139 | // Store the address of the original variable associated with the LHS |
| 1140 | // implicit variable. |
| 1141 | if (IsArray) { |
| 1142 | OriginalAddr = Builder.CreateElementBitCast( |
| 1143 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1144 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1145 | PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1146 | PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1147 | RHSVD, [this, PrivateVD, RHSVD, IsArray]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1148 | return IsArray |
| 1149 | ? Builder.CreateElementBitCast( |
| 1150 | GetAddrOfLocalVar(PrivateVD), |
| 1151 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin") |
| 1152 | : GetAddrOfLocalVar(PrivateVD); |
| 1153 | }); |
| 1154 | } |
| 1155 | ++ILHS; |
| 1156 | ++IRHS; |
| 1157 | ++IPriv; |
| 1158 | ++Count; |
| 1159 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1163 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1164 | if (!HaveInsertPoint()) |
| 1165 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1166 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1167 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1168 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1169 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1170 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1171 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1172 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1173 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1174 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1175 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1176 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1177 | } |
| 1178 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1179 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1180 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1181 | ReductionKind == OMPD_simd; |
| 1182 | bool SimpleReduction = ReductionKind == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1183 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1184 | // parallel directive (it always has implicit barrier). |
| 1185 | CGM.getOpenMPRuntime().emitReduction( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1186 | *this, D.getEndLoc(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1187 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } |
| 1190 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1191 | static void emitPostUpdateForReductionClause( |
| 1192 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1193 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1194 | if (!CGF.HaveInsertPoint()) |
| 1195 | return; |
| 1196 | llvm::BasicBlock *DoneBB = nullptr; |
| 1197 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1198 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1199 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1200 | if (llvm::Value *Cond = CondGen(CGF)) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1201 | // If the first post-update expression is found, emit conditional |
| 1202 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1203 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1204 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1205 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1206 | CGF.EmitBlock(ThenBB); |
| 1207 | } |
| 1208 | } |
| 1209 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1210 | } |
| 1211 | } |
| 1212 | if (DoneBB) |
| 1213 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1214 | } |
| 1215 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1216 | namespace { |
| 1217 | /// Codegen lambda for appending distribute lower and upper bounds to outlined |
| 1218 | /// parallel function. This is necessary for combined constructs such as |
| 1219 | /// 'distribute parallel for' |
| 1220 | typedef llvm::function_ref<void(CodeGenFunction &, |
| 1221 | const OMPExecutableDirective &, |
| 1222 | llvm::SmallVectorImpl<llvm::Value *> &)> |
| 1223 | CodeGenBoundParametersTy; |
| 1224 | } // anonymous namespace |
| 1225 | |
| 1226 | static void emitCommonOMPParallelDirective( |
| 1227 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 1228 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1229 | const CodeGenBoundParametersTy &CodeGenBoundParameters) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1230 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1231 | llvm::Value *OutlinedFn = |
| 1232 | CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1233 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1234 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1235 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1236 | llvm::Value *NumThreads = |
| 1237 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1238 | /*IgnoreResultAssign=*/true); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1239 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1240 | CGF, NumThreads, NumThreadsClause->getBeginLoc()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1241 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1242 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1243 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1244 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1245 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getBeginLoc()); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1246 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1247 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1248 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1249 | if (C->getNameModifier() == OMPD_unknown || |
| 1250 | C->getNameModifier() == OMPD_parallel) { |
| 1251 | IfCond = C->getCondition(); |
| 1252 | break; |
| 1253 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1254 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1255 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1256 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1257 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1258 | // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk |
| 1259 | // lower and upper bounds with the pragma 'for' chunking mechanism. |
| 1260 | // The following lambda takes care of appending the lower and upper bound |
| 1261 | // parameters when necessary |
| 1262 | CodeGenBoundParameters(CGF, S, CapturedVars); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1263 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1264 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getBeginLoc(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1265 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1268 | static void emitEmptyBoundParameters(CodeGenFunction &, |
| 1269 | const OMPExecutableDirective &, |
| 1270 | llvm::SmallVectorImpl<llvm::Value *> &) {} |
| 1271 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1272 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1273 | // Emit parallel region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1274 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 1275 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1276 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1277 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1278 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1279 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1280 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1281 | // propagation master's thread values of threadprivate variables to local |
| 1282 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1283 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1284 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1285 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1286 | } |
| 1287 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1288 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1289 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1290 | CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1291 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1292 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1293 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, |
| 1294 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1295 | emitPostUpdateForReductionClause(*this, S, |
| 1296 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1297 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1298 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1299 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1300 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1301 | RunCleanupsScope BodyScope(*this); |
| 1302 | // Update counters values on current iteration. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1303 | for (const Expr *UE : D.updates()) |
| 1304 | EmitIgnoredExpr(UE); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1305 | // Update the linear variables. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1306 | // In distribute directives only loop counters may be marked as linear, no |
| 1307 | // need to generate the code for them. |
| 1308 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1309 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1310 | for (const Expr *UE : C->updates()) |
| 1311 | EmitIgnoredExpr(UE); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1312 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1313 | } |
| 1314 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1315 | // On a continue in the body, jump to the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1316 | JumpDest Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1317 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1318 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1319 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1320 | // The end (updates/cleanups). |
| 1321 | EmitBlock(Continue.getBlock()); |
| 1322 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1325 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1326 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1327 | const Expr *IncExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1328 | const llvm::function_ref<void(CodeGenFunction &)> BodyGen, |
| 1329 | const llvm::function_ref<void(CodeGenFunction &)> PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1330 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1331 | |
| 1332 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1333 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1334 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1335 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1336 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1337 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1338 | |
| 1339 | // If there are any cleanups between here and the loop-exit scope, |
| 1340 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1341 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1342 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1343 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1344 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1345 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1346 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1347 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1348 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1349 | if (ExitBlock != LoopExit.getBlock()) { |
| 1350 | EmitBlock(ExitBlock); |
| 1351 | EmitBranchThroughCleanup(LoopExit); |
| 1352 | } |
| 1353 | |
| 1354 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1355 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1356 | |
| 1357 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1358 | JumpDest Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1359 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1360 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1361 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1362 | |
| 1363 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1364 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1365 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1366 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1367 | BreakContinueStack.pop_back(); |
| 1368 | EmitBranch(CondBlock); |
| 1369 | LoopStack.pop(); |
| 1370 | // Emit the fall-through block. |
| 1371 | EmitBlock(LoopExit.getBlock()); |
| 1372 | } |
| 1373 | |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1374 | bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1375 | if (!HaveInsertPoint()) |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1376 | return false; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1377 | // Emit inits for the linear variables. |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1378 | bool HasLinears = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1379 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1380 | for (const Expr *Init : C->inits()) { |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1381 | HasLinears = true; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1382 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
| 1383 | if (const auto *Ref = |
| 1384 | dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1385 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1386 | const auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1387 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1388 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1389 | VD->getInit()->getType(), VK_LValue, |
| 1390 | VD->getInit()->getExprLoc()); |
| 1391 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1392 | VD->getType()), |
| 1393 | /*capturedByInit=*/false); |
| 1394 | EmitAutoVarCleanups(Emission); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1395 | } else { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1396 | EmitVarDecl(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1397 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1398 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1399 | // Emit the linear steps for the linear clauses. |
| 1400 | // If a step is not constant, it is pre-calculated before the loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1401 | if (const auto *CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1402 | if (const auto *SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1403 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1404 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1405 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1406 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1407 | } |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1408 | return HasLinears; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1411 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1412 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1413 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1414 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1415 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1416 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1417 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1418 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1419 | auto IC = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1420 | for (const Expr *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1421 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1422 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1423 | // If the first post-update expression is found, emit conditional |
| 1424 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1425 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.linear.pu"); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1426 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1427 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1428 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1429 | } |
| 1430 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1431 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1432 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1433 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1434 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1435 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1436 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1437 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1438 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1439 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1440 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1441 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1442 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1443 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1444 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1445 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1446 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1449 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1450 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1451 | if (!CGF.HaveInsertPoint()) |
| 1452 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1453 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1454 | unsigned ClauseAlignment = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1455 | if (const Expr *AlignmentExpr = Clause->getAlignment()) { |
| 1456 | auto *AlignmentCI = |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1457 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1458 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1459 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1460 | for (const Expr *E : Clause->varlists()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1461 | unsigned Alignment = ClauseAlignment; |
| 1462 | if (Alignment == 0) { |
| 1463 | // OpenMP [2.8.1, Description] |
| 1464 | // If no optional parameter is specified, implementation-defined default |
| 1465 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1466 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1467 | CGF.getContext() |
| 1468 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1469 | E->getType()->getPointeeType())) |
| 1470 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1471 | } |
| 1472 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1473 | "alignment is not power of 2"); |
| 1474 | if (Alignment != 0) { |
| 1475 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
| 1476 | CGF.EmitAlignmentAssumption(PtrValue, Alignment); |
| 1477 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1478 | } |
| 1479 | } |
| 1480 | } |
| 1481 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1482 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1483 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1484 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1485 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1486 | auto I = S.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1487 | for (const Expr *E : S.counters()) { |
| 1488 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1489 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1490 | // Emit var without initialization. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1491 | AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1492 | EmitAutoVarCleanups(VarEmission); |
| 1493 | LocalDeclMap.erase(PrivateVD); |
| 1494 | (void)LoopScope.addPrivate(VD, [&VarEmission]() { |
| 1495 | return VarEmission.getAllocatedAddress(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1496 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1497 | if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || |
| 1498 | VD->hasGlobalStorage()) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1499 | (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1500 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 1501 | LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), |
| 1502 | E->getType(), VK_LValue, E->getExprLoc()); |
| 1503 | return EmitLValue(&DRE).getAddress(); |
| 1504 | }); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1505 | } else { |
| 1506 | (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() { |
| 1507 | return VarEmission.getAllocatedAddress(); |
| 1508 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1509 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1510 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1511 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1512 | // Privatize extra loop counters used in loops for ordered(n) clauses. |
| 1513 | for (const auto *C : S.getClausesOfKind<OMPOrderedClause>()) { |
| 1514 | if (!C->getNumForLoops()) |
| 1515 | continue; |
| 1516 | for (unsigned I = S.getCollapsedNumber(), |
| 1517 | E = C->getLoopNumIterations().size(); |
| 1518 | I < E; ++I) { |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 1519 | const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I)); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1520 | const auto *VD = cast<VarDecl>(DRE->getDecl()); |
| 1521 | // Override only those variables that are really emitted already. |
| 1522 | if (LocalDeclMap.count(VD)) { |
| 1523 | (void)LoopScope.addPrivate(VD, [this, DRE, VD]() { |
| 1524 | return CreateMemTemp(DRE->getType(), VD->getName()); |
| 1525 | }); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1531 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1532 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1533 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1534 | if (!CGF.HaveInsertPoint()) |
| 1535 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1536 | { |
| 1537 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1538 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1539 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1540 | // Get initial values of real counters. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1541 | for (const Expr *I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1542 | CGF.EmitIgnoredExpr(I); |
| 1543 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1544 | } |
| 1545 | // Check that loop is executed at least one time. |
| 1546 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 1547 | } |
| 1548 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1549 | void CodeGenFunction::EmitOMPLinearClause( |
| 1550 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1551 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1552 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1553 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1554 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1555 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1556 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1557 | SIMDLCVs.insert( |
| 1558 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1559 | } |
| 1560 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1561 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1562 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1563 | for (const Expr *E : C->varlists()) { |
| 1564 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1565 | const auto *PrivateVD = |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1566 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1567 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1568 | bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1569 | // Emit private VarDecl with copy init. |
| 1570 | EmitVarDecl(*PrivateVD); |
| 1571 | return GetAddrOfLocalVar(PrivateVD); |
| 1572 | }); |
| 1573 | assert(IsRegistered && "linear var already registered as private"); |
| 1574 | // Silence the warning about unused variable. |
| 1575 | (void)IsRegistered; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1576 | } else { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1577 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1578 | } |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1579 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1580 | } |
| 1581 | } |
| 1582 | } |
| 1583 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1584 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1585 | const OMPExecutableDirective &D, |
| 1586 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1587 | if (!CGF.HaveInsertPoint()) |
| 1588 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1589 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1590 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1591 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1592 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1593 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1594 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1595 | // the memory instructions parallel, because loop-carried |
| 1596 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1597 | if (!IsMonotonic) |
| 1598 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1599 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1600 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1601 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1602 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1603 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1604 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1605 | // the memory instructions parallel, because loop-carried |
| 1606 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1607 | CGF.LoopStack.setParallel(/*Enable=*/false); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1608 | } |
| 1609 | } |
| 1610 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1611 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1612 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1613 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1614 | LoopStack.setParallel(!IsMonotonic); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1615 | LoopStack.setVectorizeEnable(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1616 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1619 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1620 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1621 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1622 | if (!HaveInsertPoint()) |
| 1623 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1624 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1625 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1626 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1627 | for (const Expr *F : D.finals()) { |
| 1628 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
| 1629 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1630 | const auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1631 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1632 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1633 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1634 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1635 | // If the first post-update expression is found, emit conditional |
| 1636 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1637 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.final.then"); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1638 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1639 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1640 | EmitBlock(ThenBB); |
| 1641 | } |
| 1642 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1643 | Address OrigAddr = Address::invalid(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1644 | if (CED) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1645 | OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1646 | } else { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1647 | DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), |
| 1648 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1649 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
| 1650 | OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1651 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1652 | OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1653 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1654 | (void)VarScope.Privatize(); |
| 1655 | EmitIgnoredExpr(F); |
| 1656 | } |
| 1657 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1658 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1659 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1660 | if (DoneBB) |
| 1661 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1664 | static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, |
| 1665 | const OMPLoopDirective &S, |
| 1666 | CodeGenFunction::JumpDest LoopExit) { |
| 1667 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1668 | CGF.EmitStopPoint(&S); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 1669 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1670 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1671 | /// Emit a helper variable and return corresponding lvalue. |
| 1672 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1673 | const DeclRefExpr *Helper) { |
| 1674 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1675 | CGF.EmitVarDecl(*VDecl); |
| 1676 | return CGF.EmitLValue(Helper); |
| 1677 | } |
| 1678 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1679 | static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1680 | PrePostActionTy &Action) { |
| 1681 | Action.Enter(CGF); |
| 1682 | assert(isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 1683 | "Expected simd directive"); |
| 1684 | OMPLoopScope PreInitScope(CGF, S); |
| 1685 | // if (PreCond) { |
| 1686 | // for (IV in 0..LastIteration) BODY; |
| 1687 | // <Final counter/linear vars updates>; |
| 1688 | // } |
| 1689 | // |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1690 | if (isOpenMPDistributeDirective(S.getDirectiveKind()) || |
| 1691 | isOpenMPWorksharingDirective(S.getDirectiveKind()) || |
| 1692 | isOpenMPTaskLoopDirective(S.getDirectiveKind())) { |
| 1693 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1694 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1695 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1696 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1697 | // Emit: if (PreCond) - begin. |
| 1698 | // If the condition constant folds and can be elided, avoid emitting the |
| 1699 | // whole loop. |
| 1700 | bool CondConstant; |
| 1701 | llvm::BasicBlock *ContBlock = nullptr; |
| 1702 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1703 | if (!CondConstant) |
| 1704 | return; |
| 1705 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1706 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1707 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
| 1708 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1709 | CGF.getProfileCount(&S)); |
| 1710 | CGF.EmitBlock(ThenBlock); |
| 1711 | CGF.incrementProfileCounter(&S); |
| 1712 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1713 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1714 | // Emit the loop iteration variable. |
| 1715 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1716 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1717 | CGF.EmitVarDecl(*IVDecl); |
| 1718 | CGF.EmitIgnoredExpr(S.getInit()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1719 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1720 | // Emit the iterations count variable. |
| 1721 | // If it is not a variable, Sema decided to calculate iterations count on |
| 1722 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1723 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1724 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1725 | // Emit calculation of the iterations count. |
| 1726 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1727 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1728 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1729 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1730 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1731 | emitAlignedClause(CGF, S); |
| 1732 | (void)CGF.EmitOMPLinearClauseInit(S); |
| 1733 | { |
| 1734 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1735 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 1736 | CGF.EmitOMPLinearClause(S, LoopScope); |
| 1737 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 1738 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 1739 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 1740 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 1741 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 1742 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1743 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1744 | S.getInc(), |
| 1745 | [&S](CodeGenFunction &CGF) { |
| 1746 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 1747 | CGF.EmitStopPoint(&S); |
| 1748 | }, |
| 1749 | [](CodeGenFunction &) {}); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1750 | CGF.EmitOMPSimdFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1751 | // Emit final copy of the lastprivate variables at the end of loops. |
| 1752 | if (HasLastprivateClause) |
| 1753 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
| 1754 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1755 | emitPostUpdateForReductionClause(CGF, S, |
| 1756 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1757 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1758 | CGF.EmitOMPLinearClauseFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1759 | // Emit: if (PreCond) - end. |
| 1760 | if (ContBlock) { |
| 1761 | CGF.EmitBranch(ContBlock); |
| 1762 | CGF.EmitBlock(ContBlock, true); |
| 1763 | } |
| 1764 | } |
| 1765 | |
| 1766 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
| 1767 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 1768 | emitOMPSimdRegion(CGF, S, Action); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1769 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1770 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1771 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1772 | } |
| 1773 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1774 | void CodeGenFunction::EmitOMPOuterLoop( |
| 1775 | bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, |
| 1776 | CodeGenFunction::OMPPrivateScope &LoopScope, |
| 1777 | const CodeGenFunction::OMPLoopArguments &LoopArgs, |
| 1778 | const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, |
| 1779 | const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1780 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1781 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1782 | const Expr *IVExpr = S.getIterationVariable(); |
| 1783 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1784 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1785 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1786 | JumpDest LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1787 | |
| 1788 | // Start the loop with a block that tests the condition. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1789 | llvm::BasicBlock *CondBlock = createBasicBlock("omp.dispatch.cond"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1790 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1791 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1792 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1793 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1794 | |
| 1795 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1796 | if (!DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1797 | // UB = min(UB, GlobalUB) or |
| 1798 | // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. |
| 1799 | // 'distribute parallel for') |
| 1800 | EmitIgnoredExpr(LoopArgs.EUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1801 | // IV = LB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1802 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1803 | // IV < UB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1804 | BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1805 | } else { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1806 | BoolCondVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1807 | RT.emitForNext(*this, S.getBeginLoc(), IVSize, IVSigned, LoopArgs.IL, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1808 | LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1809 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1810 | |
| 1811 | // If there are any cleanups between here and the loop-exit scope, |
| 1812 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1813 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1814 | if (LoopScope.requiresCleanups()) |
| 1815 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1816 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1817 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.dispatch.body"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1818 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1819 | if (ExitBlock != LoopExit.getBlock()) { |
| 1820 | EmitBlock(ExitBlock); |
| 1821 | EmitBranchThroughCleanup(LoopExit); |
| 1822 | } |
| 1823 | EmitBlock(LoopBody); |
| 1824 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1825 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1826 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1827 | if (DynamicOrOrdered) |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1828 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1829 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1830 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1831 | JumpDest Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1832 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1833 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1834 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1835 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1836 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1837 | LoopStack.setParallel(!IsMonotonic); |
| 1838 | else |
| 1839 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1840 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1841 | SourceLocation Loc = S.getBeginLoc(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1842 | |
| 1843 | // when 'distribute' is not combined with a 'for': |
| 1844 | // while (idx <= UB) { BODY; ++idx; } |
| 1845 | // when 'distribute' is combined with a 'for' |
| 1846 | // (e.g. 'distribute parallel for') |
| 1847 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 1848 | EmitOMPInnerLoop( |
| 1849 | S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, |
| 1850 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 1851 | CodeGenLoop(CGF, S, LoopExit); |
| 1852 | }, |
| 1853 | [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { |
| 1854 | CodeGenOrdered(CGF, Loc, IVSize, IVSigned); |
| 1855 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1856 | |
| 1857 | EmitBlock(Continue.getBlock()); |
| 1858 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1859 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1860 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1861 | EmitIgnoredExpr(LoopArgs.NextLB); |
| 1862 | EmitIgnoredExpr(LoopArgs.NextUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1863 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1864 | |
| 1865 | EmitBranch(CondBlock); |
| 1866 | LoopStack.pop(); |
| 1867 | // Emit the fall-through block. |
| 1868 | EmitBlock(LoopExit.getBlock()); |
| 1869 | |
| 1870 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1871 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 1872 | if (!DynamicOrOrdered) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1873 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 1874 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1875 | }; |
| 1876 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
| 1879 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1880 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1881 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1882 | const OMPLoopArguments &LoopArgs, |
| 1883 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1884 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1885 | |
| 1886 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1887 | const bool DynamicOrOrdered = |
| 1888 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1889 | |
| 1890 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1891 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1892 | LoopArgs.Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1893 | "static non-chunked schedule does not need outer loop"); |
| 1894 | |
| 1895 | // Emit outer loop. |
| 1896 | // |
| 1897 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1898 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1899 | // distributed to threads in the team in chunks as the threads request them. |
| 1900 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1901 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1902 | // iterations, except for the last chunk to be distributed, which may have |
| 1903 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1904 | // |
| 1905 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1906 | // to threads in the team in chunks as the executing threads request them. |
| 1907 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1908 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1909 | // each chunk is proportional to the number of unassigned iterations divided |
| 1910 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1911 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1912 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1913 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1914 | // than k iterations). |
| 1915 | // |
| 1916 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1917 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1918 | // implementation the freedom to choose any possible mapping of iterations to |
| 1919 | // threads in the team. |
| 1920 | // |
| 1921 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1922 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1923 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1924 | // implementation defined |
| 1925 | // |
| 1926 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1927 | // idx = LB; |
| 1928 | // while (idx <= UB) { BODY; ++idx; |
| 1929 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1930 | // } // inner loop |
| 1931 | // } |
| 1932 | // |
| 1933 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1934 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1935 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1936 | // the team in a round-robin fashion in the order of the thread number. |
| 1937 | // |
| 1938 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1939 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1940 | // LB = LB + ST; |
| 1941 | // UB = UB + ST; |
| 1942 | // } |
| 1943 | // |
| 1944 | |
| 1945 | const Expr *IVExpr = S.getIterationVariable(); |
| 1946 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1947 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1948 | |
| 1949 | if (DynamicOrOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1950 | const std::pair<llvm::Value *, llvm::Value *> DispatchBounds = |
| 1951 | CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1952 | llvm::Value *LBVal = DispatchBounds.first; |
| 1953 | llvm::Value *UBVal = DispatchBounds.second; |
| 1954 | CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, |
| 1955 | LoopArgs.Chunk}; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1956 | RT.emitForDispatchInit(*this, S.getBeginLoc(), ScheduleKind, IVSize, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1957 | IVSigned, Ordered, DipatchRTInputValues); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1958 | } else { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1959 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 1960 | IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, |
| 1961 | LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1962 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1963 | ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1966 | auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, |
| 1967 | const unsigned IVSize, |
| 1968 | const bool IVSigned) { |
| 1969 | if (Ordered) { |
| 1970 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, |
| 1971 | IVSigned); |
| 1972 | } |
| 1973 | }; |
| 1974 | |
| 1975 | OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, |
| 1976 | LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); |
| 1977 | OuterLoopArgs.IncExpr = S.getInc(); |
| 1978 | OuterLoopArgs.Init = S.getInit(); |
| 1979 | OuterLoopArgs.Cond = S.getCond(); |
| 1980 | OuterLoopArgs.NextLB = S.getNextLowerBound(); |
| 1981 | OuterLoopArgs.NextUB = S.getNextUpperBound(); |
| 1982 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, |
| 1983 | emitOMPLoopBodyWithStopPoint, CodeGenOrdered); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1986 | static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, |
| 1987 | const unsigned IVSize, const bool IVSigned) {} |
| 1988 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1989 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1990 | OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, |
| 1991 | OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, |
| 1992 | const CodeGenLoopTy &CodeGenLoopContent) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1993 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1994 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1995 | |
| 1996 | // Emit outer loop. |
| 1997 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 1998 | // dynamic |
| 1999 | // |
| 2000 | |
| 2001 | const Expr *IVExpr = S.getIterationVariable(); |
| 2002 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2003 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2004 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2005 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2006 | IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, |
| 2007 | LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2008 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2009 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2010 | // for combined 'distribute' and 'for' the increment expression of distribute |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2011 | // is stored in DistInc. For 'distribute' alone, it is in Inc. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2012 | Expr *IncExpr; |
| 2013 | if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) |
| 2014 | IncExpr = S.getDistInc(); |
| 2015 | else |
| 2016 | IncExpr = S.getInc(); |
| 2017 | |
| 2018 | // this routine is shared by 'omp distribute parallel for' and |
| 2019 | // 'omp distribute': select the right EUB expression depending on the |
| 2020 | // directive |
| 2021 | OMPLoopArguments OuterLoopArgs; |
| 2022 | OuterLoopArgs.LB = LoopArgs.LB; |
| 2023 | OuterLoopArgs.UB = LoopArgs.UB; |
| 2024 | OuterLoopArgs.ST = LoopArgs.ST; |
| 2025 | OuterLoopArgs.IL = LoopArgs.IL; |
| 2026 | OuterLoopArgs.Chunk = LoopArgs.Chunk; |
| 2027 | OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2028 | ? S.getCombinedEnsureUpperBound() |
| 2029 | : S.getEnsureUpperBound(); |
| 2030 | OuterLoopArgs.IncExpr = IncExpr; |
| 2031 | OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2032 | ? S.getCombinedInit() |
| 2033 | : S.getInit(); |
| 2034 | OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2035 | ? S.getCombinedCond() |
| 2036 | : S.getCond(); |
| 2037 | OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2038 | ? S.getCombinedNextLowerBound() |
| 2039 | : S.getNextLowerBound(); |
| 2040 | OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2041 | ? S.getCombinedNextUpperBound() |
| 2042 | : S.getNextUpperBound(); |
| 2043 | |
| 2044 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, |
| 2045 | LoopScope, OuterLoopArgs, CodeGenLoopContent, |
| 2046 | emitEmptyOrdered); |
| 2047 | } |
| 2048 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2049 | static std::pair<LValue, LValue> |
| 2050 | emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, |
| 2051 | const OMPExecutableDirective &S) { |
| 2052 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2053 | LValue LB = |
| 2054 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2055 | LValue UB = |
| 2056 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2057 | |
| 2058 | // When composing 'distribute' with 'for' (e.g. as in 'distribute |
| 2059 | // parallel for') we need to use the 'distribute' |
| 2060 | // chunk lower and upper bounds rather than the whole loop iteration |
| 2061 | // space. These are parameters to the outlined function for 'parallel' |
| 2062 | // and we copy the bounds of the previous schedule into the |
| 2063 | // the current ones. |
| 2064 | LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); |
| 2065 | LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2066 | llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar( |
| 2067 | PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2068 | PrevLBVal = CGF.EmitScalarConversion( |
| 2069 | PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2070 | LS.getIterationVariable()->getType(), |
| 2071 | LS.getPrevLowerBoundVariable()->getExprLoc()); |
| 2072 | llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar( |
| 2073 | PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2074 | PrevUBVal = CGF.EmitScalarConversion( |
| 2075 | PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2076 | LS.getIterationVariable()->getType(), |
| 2077 | LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2078 | |
| 2079 | CGF.EmitStoreOfScalar(PrevLBVal, LB); |
| 2080 | CGF.EmitStoreOfScalar(PrevUBVal, UB); |
| 2081 | |
| 2082 | return {LB, UB}; |
| 2083 | } |
| 2084 | |
| 2085 | /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then |
| 2086 | /// we need to use the LB and UB expressions generated by the worksharing |
| 2087 | /// code generation support, whereas in non combined situations we would |
| 2088 | /// just emit 0 and the LastIteration expression |
| 2089 | /// This function is necessary due to the difference of the LB and UB |
| 2090 | /// types for the RT emission routines for 'for_static_init' and |
| 2091 | /// 'for_dispatch_init' |
| 2092 | static std::pair<llvm::Value *, llvm::Value *> |
| 2093 | emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, |
| 2094 | const OMPExecutableDirective &S, |
| 2095 | Address LB, Address UB) { |
| 2096 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2097 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2098 | // when implementing a dynamic schedule for a 'for' combined with a |
| 2099 | // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop |
| 2100 | // is not normalized as each team only executes its own assigned |
| 2101 | // distribute chunk |
| 2102 | QualType IteratorTy = IVExpr->getType(); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2103 | llvm::Value *LBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2104 | CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2105 | llvm::Value *UBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2106 | CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2107 | return {LBVal, UBVal}; |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2108 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2109 | |
| 2110 | static void emitDistributeParallelForDistributeInnerBoundParams( |
| 2111 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2112 | llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { |
| 2113 | const auto &Dir = cast<OMPLoopDirective>(S); |
| 2114 | LValue LB = |
| 2115 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2116 | llvm::Value *LBCast = CGF.Builder.CreateIntCast( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2117 | CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2118 | CapturedVars.push_back(LBCast); |
| 2119 | LValue UB = |
| 2120 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); |
| 2121 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2122 | llvm::Value *UBCast = CGF.Builder.CreateIntCast( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2123 | CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2124 | CapturedVars.push_back(UBCast); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2125 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2126 | |
| 2127 | static void |
| 2128 | emitInnerParallelForWhenCombined(CodeGenFunction &CGF, |
| 2129 | const OMPLoopDirective &S, |
| 2130 | CodeGenFunction::JumpDest LoopExit) { |
| 2131 | auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2132 | PrePostActionTy &Action) { |
| 2133 | Action.Enter(CGF); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2134 | bool HasCancel = false; |
| 2135 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2136 | if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) |
| 2137 | HasCancel = D->hasCancel(); |
| 2138 | else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) |
| 2139 | HasCancel = D->hasCancel(); |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 2140 | else if (const auto *D = |
| 2141 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) |
| 2142 | HasCancel = D->hasCancel(); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2143 | } |
| 2144 | CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), |
| 2145 | HasCancel); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2146 | CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), |
| 2147 | emitDistributeParallelForInnerBounds, |
| 2148 | emitDistributeParallelForDispatchBounds); |
| 2149 | }; |
| 2150 | |
| 2151 | emitCommonOMPParallelDirective( |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2152 | CGF, S, |
| 2153 | isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, |
| 2154 | CGInlinedWorksharingLoop, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2155 | emitDistributeParallelForDistributeInnerBoundParams); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2156 | } |
| 2157 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2158 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 2159 | const OMPDistributeParallelForDirective &S) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2160 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2161 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2162 | S.getDistInc()); |
| 2163 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2164 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 2165 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2168 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 2169 | const OMPDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2170 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2171 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2172 | S.getDistInc()); |
| 2173 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2174 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2175 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2176 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2177 | |
| 2178 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 2179 | const OMPDistributeSimdDirective &S) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2180 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2181 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 2182 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2183 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2184 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2187 | void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| 2188 | CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { |
| 2189 | // Emit SPMD target parallel for region as a standalone region. |
| 2190 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2191 | emitOMPSimdRegion(CGF, S, Action); |
| 2192 | }; |
| 2193 | llvm::Function *Fn; |
| 2194 | llvm::Constant *Addr; |
| 2195 | // Emit target region as a standalone region. |
| 2196 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 2197 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 2198 | assert(Fn && Addr && "Target device function emission failed."); |
| 2199 | } |
| 2200 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2201 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 2202 | const OMPTargetSimdDirective &S) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2203 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2204 | emitOMPSimdRegion(CGF, S, Action); |
| 2205 | }; |
| 2206 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2209 | namespace { |
| 2210 | struct ScheduleKindModifiersTy { |
| 2211 | OpenMPScheduleClauseKind Kind; |
| 2212 | OpenMPScheduleClauseModifier M1; |
| 2213 | OpenMPScheduleClauseModifier M2; |
| 2214 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2215 | OpenMPScheduleClauseModifier M1, |
| 2216 | OpenMPScheduleClauseModifier M2) |
| 2217 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2218 | }; |
| 2219 | } // namespace |
| 2220 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2221 | bool CodeGenFunction::EmitOMPWorksharingLoop( |
| 2222 | const OMPLoopDirective &S, Expr *EUB, |
| 2223 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
| 2224 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2225 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2226 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2227 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2228 | EmitVarDecl(*IVDecl); |
| 2229 | |
| 2230 | // Emit the iterations count variable. |
| 2231 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2232 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2233 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2234 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2235 | // Emit calculation of the iterations count. |
| 2236 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2237 | } |
| 2238 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2239 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2240 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2241 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2242 | // Check pre-condition. |
| 2243 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2244 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2245 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2246 | // If the condition constant folds and can be elided, avoid emitting the |
| 2247 | // whole loop. |
| 2248 | bool CondConstant; |
| 2249 | llvm::BasicBlock *ContBlock = nullptr; |
| 2250 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2251 | if (!CondConstant) |
| 2252 | return false; |
| 2253 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2254 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2255 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2256 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2257 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2258 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2259 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2260 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2261 | |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2262 | RunCleanupsScope DoacrossCleanupScope(*this); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2263 | bool Ordered = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2264 | if (const auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2265 | if (OrderedClause->getNumForLoops()) |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 2266 | RT.emitDoacrossInit(*this, S, OrderedClause->getLoopNumIterations()); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2267 | else |
| 2268 | Ordered = true; |
| 2269 | } |
| 2270 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2271 | llvm::DenseSet<const Expr *> EmittedFinals; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2272 | emitAlignedClause(*this, S); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2273 | bool HasLinears = EmitOMPLinearClauseInit(S); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2274 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2275 | |
| 2276 | std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S); |
| 2277 | LValue LB = Bounds.first; |
| 2278 | LValue UB = Bounds.second; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2279 | LValue ST = |
| 2280 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2281 | LValue IL = |
| 2282 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2283 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2284 | // Emit 'then' code. |
| 2285 | { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2286 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2287 | if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2288 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2289 | // initialization of firstprivate variables and post-update of |
| 2290 | // lastprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2291 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2292 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2293 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2294 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 2295 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2296 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 2297 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2298 | EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2299 | EmitOMPLinearClause(S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 2300 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2301 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2302 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2303 | |
| 2304 | // Detect the loop schedule kind and chunk. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2305 | const Expr *ChunkExpr = nullptr; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2306 | OpenMPScheduleTy ScheduleKind; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2307 | if (const auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2308 | ScheduleKind.Schedule = C->getScheduleKind(); |
| 2309 | ScheduleKind.M1 = C->getFirstScheduleModifier(); |
| 2310 | ScheduleKind.M2 = C->getSecondScheduleModifier(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2311 | ChunkExpr = C->getChunkSize(); |
Gheorghe-Teodor Bercea | 8233af9 | 2018-09-27 20:29:00 +0000 | [diff] [blame] | 2312 | } else { |
| 2313 | // Default behaviour for schedule clause. |
| 2314 | CGM.getOpenMPRuntime().getDefaultScheduleAndChunk( |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2315 | *this, S, ScheduleKind.Schedule, ChunkExpr); |
| 2316 | } |
| 2317 | bool HasChunkSizeOne = false; |
| 2318 | llvm::Value *Chunk = nullptr; |
| 2319 | if (ChunkExpr) { |
| 2320 | Chunk = EmitScalarExpr(ChunkExpr); |
| 2321 | Chunk = EmitScalarConversion(Chunk, ChunkExpr->getType(), |
| 2322 | S.getIterationVariable()->getType(), |
| 2323 | S.getBeginLoc()); |
Nico Weber | 9f0246d | 2018-11-21 12:47:43 +0000 | [diff] [blame] | 2324 | llvm::APSInt EvaluatedChunk; |
| 2325 | if (ChunkExpr->EvaluateAsInt(EvaluatedChunk, getContext())) |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2326 | HasChunkSizeOne = (EvaluatedChunk.getLimitedValue() == 1); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2327 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2328 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2329 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2330 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 2331 | // If the static schedule kind is specified or if the ordered clause is |
| 2332 | // specified, and if no monotonic modifier is specified, the effect will |
| 2333 | // be as if the monotonic modifier was specified. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2334 | bool StaticChunkedOne = RT.isStaticChunked(ScheduleKind.Schedule, |
| 2335 | /* Chunked */ Chunk != nullptr) && HasChunkSizeOne && |
| 2336 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
| 2337 | if ((RT.isStaticNonchunked(ScheduleKind.Schedule, |
| 2338 | /* Chunked */ Chunk != nullptr) || |
| 2339 | StaticChunkedOne) && |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2340 | !Ordered) { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2341 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 2342 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2343 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2344 | // When no chunk_size is specified, the iteration space is divided into |
| 2345 | // chunks that are approximately equal in size, and at most one chunk is |
| 2346 | // distributed to each thread. Note that the size of the chunks is |
| 2347 | // unspecified in this case. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2348 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2349 | IVSize, IVSigned, Ordered, IL.getAddress(), LB.getAddress(), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2350 | UB.getAddress(), ST.getAddress(), |
| 2351 | StaticChunkedOne ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2352 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2353 | ScheduleKind, StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2354 | JumpDest LoopExit = |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2355 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2356 | // UB = min(UB, GlobalUB); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2357 | if (!StaticChunkedOne) |
| 2358 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2359 | // IV = LB; |
| 2360 | EmitIgnoredExpr(S.getInit()); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2361 | // For unchunked static schedule generate: |
| 2362 | // |
| 2363 | // while (idx <= UB) { |
| 2364 | // BODY; |
| 2365 | // ++idx; |
| 2366 | // } |
| 2367 | // |
| 2368 | // For static schedule with chunk one: |
| 2369 | // |
| 2370 | // while (IV <= PrevUB) { |
| 2371 | // BODY; |
| 2372 | // IV += ST; |
| 2373 | // } |
| 2374 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), |
| 2375 | StaticChunkedOne ? S.getCombinedParForInDistCond() : S.getCond(), |
| 2376 | StaticChunkedOne ? S.getDistInc() : S.getInc(), |
| 2377 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2378 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 2379 | CGF.EmitStopPoint(&S); |
| 2380 | }, |
| 2381 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2382 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2383 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2384 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2385 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2386 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2387 | }; |
| 2388 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2389 | } else { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2390 | const bool IsMonotonic = |
| 2391 | Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || |
| 2392 | ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || |
| 2393 | ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 2394 | ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2395 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2396 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2397 | const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(), |
| 2398 | ST.getAddress(), IL.getAddress(), |
| 2399 | Chunk, EUB); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2400 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2401 | LoopArguments, CGDispatchBounds); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2402 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2403 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2404 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
| 2405 | return CGF.Builder.CreateIsNotNull( |
| 2406 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
| 2407 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2408 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2409 | EmitOMPReductionClauseFinal( |
| 2410 | S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) |
| 2411 | ? /*Parallel and Simd*/ OMPD_parallel_for_simd |
| 2412 | : /*Parallel only*/ OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2413 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2414 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2415 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2416 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2417 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2418 | }); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2419 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2420 | if (HasLastprivateClause) |
| 2421 | EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2422 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2423 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2424 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2425 | EmitOMPLinearClauseFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2426 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2427 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2428 | }); |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2429 | DoacrossCleanupScope.ForceCleanup(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2430 | // We're now done with the loop, so jump to the continuation block. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2431 | if (ContBlock) { |
| 2432 | EmitBranch(ContBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2433 | EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2434 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2435 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2436 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2437 | } |
| 2438 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2439 | /// The following two functions generate expressions for the loop lower |
| 2440 | /// and upper bounds in case of static and dynamic (dispatch) schedule |
| 2441 | /// of the associated 'for' or 'distribute' loop. |
| 2442 | static std::pair<LValue, LValue> |
| 2443 | emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2444 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2445 | LValue LB = |
| 2446 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2447 | LValue UB = |
| 2448 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2449 | return {LB, UB}; |
| 2450 | } |
| 2451 | |
| 2452 | /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not |
| 2453 | /// consider the lower and upper bound expressions generated by the |
| 2454 | /// worksharing loop support, but we use 0 and the iteration space size as |
| 2455 | /// constants |
| 2456 | static std::pair<llvm::Value *, llvm::Value *> |
| 2457 | emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2458 | Address LB, Address UB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2459 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2460 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2461 | const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); |
| 2462 | llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); |
| 2463 | llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); |
| 2464 | return {LBVal, UBVal}; |
| 2465 | } |
| 2466 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2467 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2468 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2469 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2470 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2471 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2472 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2473 | emitForLoopBounds, |
| 2474 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2475 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2476 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2477 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2478 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2479 | S.hasCancel()); |
| 2480 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2481 | |
| 2482 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2483 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2484 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2485 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2486 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2487 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2488 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2489 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2490 | PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2491 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2492 | emitForLoopBounds, |
| 2493 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2494 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2495 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2496 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2497 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2498 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2499 | |
| 2500 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2501 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2502 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2505 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2506 | const Twine &Name, |
| 2507 | llvm::Value *Init = nullptr) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2508 | LValue LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2509 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2510 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2511 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2514 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2515 | const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt(); |
| 2516 | const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2517 | bool HasLastprivates = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2518 | auto &&CodeGen = [&S, CapturedStmt, CS, |
| 2519 | &HasLastprivates](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2520 | ASTContext &C = CGF.getContext(); |
| 2521 | QualType KmpInt32Ty = |
| 2522 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2523 | // Emit helper vars inits. |
| 2524 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2525 | CGF.Builder.getInt32(0)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2526 | llvm::ConstantInt *GlobalUBVal = CS != nullptr |
| 2527 | ? CGF.Builder.getInt32(CS->size() - 1) |
| 2528 | : CGF.Builder.getInt32(0); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2529 | LValue UB = |
| 2530 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2531 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2532 | CGF.Builder.getInt32(1)); |
| 2533 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2534 | CGF.Builder.getInt32(0)); |
| 2535 | // Loop counter. |
| 2536 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2537 | OpaqueValueExpr IVRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2538 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2539 | OpaqueValueExpr UBRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2540 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2541 | // Generate condition for loop. |
| 2542 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2543 | OK_Ordinary, S.getBeginLoc(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2544 | // Increment for loop counter. |
| 2545 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2546 | S.getBeginLoc(), true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2547 | auto &&BodyGen = [CapturedStmt, CS, &S, &IV](CodeGenFunction &CGF) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2548 | // Iterate through all sections and emit a switch construct: |
| 2549 | // switch (IV) { |
| 2550 | // case 0: |
| 2551 | // <SectionStmt[0]>; |
| 2552 | // break; |
| 2553 | // ... |
| 2554 | // case <NumSection> - 1: |
| 2555 | // <SectionStmt[<NumSection> - 1]>; |
| 2556 | // break; |
| 2557 | // } |
| 2558 | // .omp.sections.exit: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2559 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 2560 | llvm::SwitchInst *SwitchStmt = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2561 | CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getBeginLoc()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2562 | ExitBB, CS == nullptr ? 1 : CS->size()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2563 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2564 | unsigned CaseNumber = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2565 | for (const Stmt *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2566 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2567 | CGF.EmitBlock(CaseBB); |
| 2568 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2569 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2570 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2571 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2572 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2573 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2574 | llvm::BasicBlock *CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2575 | CGF.EmitBlock(CaseBB); |
| 2576 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2577 | CGF.EmitStmt(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2578 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2579 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2580 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2581 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2582 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2583 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2584 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2585 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2586 | // initialization of firstprivate variables and post-update of lastprivate |
| 2587 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2588 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2589 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2590 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2591 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2592 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2593 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2594 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2595 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2596 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2597 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2598 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2599 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2600 | OpenMPScheduleTy ScheduleKind; |
| 2601 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2602 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2603 | /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), |
| 2604 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2605 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2606 | CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, StaticInit); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2607 | // UB = min(UB, GlobalUB); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2608 | llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, S.getBeginLoc()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2609 | llvm::Value *MinUBGlobalUB = CGF.Builder.CreateSelect( |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2610 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 2611 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 2612 | // IV = LB; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2613 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getBeginLoc()), IV); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2614 | // while (idx <= UB) { BODY; ++idx; } |
| 2615 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 2616 | [](CodeGenFunction &) {}); |
| 2617 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2618 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2619 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2620 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2621 | }; |
| 2622 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2623 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2624 | // Emit post-update of the reduction variables if IsLastIter != 0. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2625 | emitPostUpdateForReductionClause(CGF, S, [IL, &S](CodeGenFunction &CGF) { |
| 2626 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2627 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2628 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2629 | |
| 2630 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2631 | if (HasLastprivates) |
| 2632 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2633 | S, /*NoFinals=*/false, |
| 2634 | CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2635 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2636 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2637 | |
| 2638 | bool HasCancel = false; |
| 2639 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 2640 | HasCancel = OSD->hasCancel(); |
| 2641 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 2642 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2643 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2644 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 2645 | HasCancel); |
| 2646 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 2647 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 2648 | // directive. |
| 2649 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2650 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2651 | // initialization of firstprivate variables. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2652 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2653 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2654 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2655 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2656 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2657 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2658 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2659 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2660 | EmitSections(S); |
| 2661 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2662 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2663 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2664 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2665 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2666 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
| 2669 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2670 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2671 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2672 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2673 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2674 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 2675 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2676 | } |
| 2677 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2678 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2679 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2680 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2681 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2682 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2683 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2684 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2685 | // Build a list of copyprivate variables along with helper expressions |
| 2686 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2687 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2688 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2689 | DestExprs.append(C->destination_exprs().begin(), |
| 2690 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2691 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2692 | AssignmentOps.append(C->assignment_ops().begin(), |
| 2693 | C->assignment_ops().end()); |
| 2694 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2695 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 2696 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2697 | Action.Enter(CGF); |
| 2698 | OMPPrivateScope SingleScope(CGF); |
| 2699 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 2700 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 2701 | (void)SingleScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2702 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2703 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2704 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2705 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2706 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2707 | CopyprivateVars, DestExprs, |
| 2708 | SrcExprs, AssignmentOps); |
| 2709 | } |
| 2710 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 2711 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2712 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 2713 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2714 | *this, S.getBeginLoc(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2715 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2716 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2719 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2720 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2721 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2722 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2723 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2724 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2725 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2726 | } |
| 2727 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2728 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2729 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2730 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2731 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2732 | }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2733 | const Expr *Hint = nullptr; |
| 2734 | if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2735 | Hint = HintClause->getHint(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2736 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2737 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 2738 | S.getDirectiveName().getAsString(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2739 | CodeGen, S.getBeginLoc(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2742 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 2743 | const OMPParallelForDirective &S) { |
| 2744 | // Emit directive as a combined directive that consists of two implicit |
| 2745 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2746 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2747 | Action.Enter(CGF); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2748 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2749 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2750 | emitDispatchForLoopBounds); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2751 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2752 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, |
| 2753 | emitEmptyBoundParameters); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2756 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2757 | const OMPParallelForSimdDirective &S) { |
| 2758 | // Emit directive as a combined directive that consists of two implicit |
| 2759 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2760 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2761 | Action.Enter(CGF); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2762 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2763 | emitDispatchForLoopBounds); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2764 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2765 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, |
| 2766 | emitEmptyBoundParameters); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2769 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2770 | const OMPParallelSectionsDirective &S) { |
| 2771 | // Emit directive as a combined directive that consists of two implicit |
| 2772 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2773 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2774 | Action.Enter(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2775 | CGF.EmitSections(S); |
| 2776 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2777 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, |
| 2778 | emitEmptyBoundParameters); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2781 | void CodeGenFunction::EmitOMPTaskBasedDirective( |
| 2782 | const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, |
| 2783 | const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, |
| 2784 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2785 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2786 | const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2787 | auto I = CS->getCapturedDecl()->param_begin(); |
| 2788 | auto PartId = std::next(I); |
| 2789 | auto TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2790 | // Check if the task is final |
| 2791 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 2792 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2793 | // the condition and the dead arm of the if/else. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2794 | const Expr *Cond = Clause->getCondition(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2795 | bool CondConstant; |
| 2796 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2797 | Data.Final.setInt(CondConstant); |
| 2798 | else |
| 2799 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2800 | } else { |
| 2801 | // By default the task is not final. |
| 2802 | Data.Final.setInt(/*IntVal=*/false); |
| 2803 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2804 | // Check if the task has 'priority' clause. |
| 2805 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2806 | const Expr *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 2807 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 2808 | Data.Priority.setPointer(EmitScalarConversion( |
| 2809 | EmitScalarExpr(Prio), Prio->getType(), |
| 2810 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2811 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2812 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2813 | // The first function argument for tasks is a thread id, the second one is a |
| 2814 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2815 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 2816 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2817 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2818 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2819 | for (const Expr *IInit : C->private_copies()) { |
| 2820 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2821 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2822 | Data.PrivateVars.push_back(*IRef); |
| 2823 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2824 | } |
| 2825 | ++IRef; |
| 2826 | } |
| 2827 | } |
| 2828 | EmittedAsPrivate.clear(); |
| 2829 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2830 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2831 | auto IRef = C->varlist_begin(); |
| 2832 | auto IElemInitRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2833 | for (const Expr *IInit : C->private_copies()) { |
| 2834 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2835 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2836 | Data.FirstprivateVars.push_back(*IRef); |
| 2837 | Data.FirstprivateCopies.push_back(IInit); |
| 2838 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2839 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 2840 | ++IRef; |
| 2841 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2842 | } |
| 2843 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2844 | // Get list of lastprivate variables (for taskloops). |
| 2845 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 2846 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 2847 | auto IRef = C->varlist_begin(); |
| 2848 | auto ID = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2849 | for (const Expr *IInit : C->private_copies()) { |
| 2850 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2851 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2852 | Data.LastprivateVars.push_back(*IRef); |
| 2853 | Data.LastprivateCopies.push_back(IInit); |
| 2854 | } |
| 2855 | LastprivateDstsOrigs.insert( |
| 2856 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 2857 | cast<DeclRefExpr>(*IRef)}); |
| 2858 | ++IRef; |
| 2859 | ++ID; |
| 2860 | } |
| 2861 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2862 | SmallVector<const Expr *, 4> LHSs; |
| 2863 | SmallVector<const Expr *, 4> RHSs; |
| 2864 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 2865 | auto IPriv = C->privates().begin(); |
| 2866 | auto IRed = C->reduction_ops().begin(); |
| 2867 | auto ILHS = C->lhs_exprs().begin(); |
| 2868 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2869 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2870 | Data.ReductionVars.emplace_back(Ref); |
| 2871 | Data.ReductionCopies.emplace_back(*IPriv); |
| 2872 | Data.ReductionOps.emplace_back(*IRed); |
| 2873 | LHSs.emplace_back(*ILHS); |
| 2874 | RHSs.emplace_back(*IRHS); |
| 2875 | std::advance(IPriv, 1); |
| 2876 | std::advance(IRed, 1); |
| 2877 | std::advance(ILHS, 1); |
| 2878 | std::advance(IRHS, 1); |
| 2879 | } |
| 2880 | } |
| 2881 | Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2882 | *this, S.getBeginLoc(), LHSs, RHSs, Data); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2883 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2884 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2885 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2886 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2887 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, |
| 2888 | CapturedRegion](CodeGenFunction &CGF, |
| 2889 | PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2890 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2891 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2892 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 2893 | !Data.LastprivateVars.empty()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2894 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2895 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 2896 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 2897 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 2898 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2899 | // Map privates. |
| 2900 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 2901 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2902 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2903 | for (const Expr *E : Data.PrivateVars) { |
| 2904 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2905 | Address PrivatePtr = CGF.CreateMemTemp( |
| 2906 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2907 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2908 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2909 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2910 | for (const Expr *E : Data.FirstprivateVars) { |
| 2911 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2912 | Address PrivatePtr = |
| 2913 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2914 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2915 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2916 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2917 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2918 | for (const Expr *E : Data.LastprivateVars) { |
| 2919 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2920 | Address PrivatePtr = |
| 2921 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2922 | ".lastpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2923 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2924 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 2925 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2926 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2927 | CopyFn, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2928 | for (const auto &Pair : LastprivateDstsOrigs) { |
| 2929 | const auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2930 | DeclRefExpr DRE( |
| 2931 | const_cast<VarDecl *>(OrigVD), |
| 2932 | /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup( |
| 2933 | OrigVD) != nullptr, |
| 2934 | Pair.second->getType(), VK_LValue, Pair.second->getExprLoc()); |
| 2935 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
| 2936 | return CGF.EmitLValue(&DRE).getAddress(); |
| 2937 | }); |
| 2938 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2939 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2940 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2941 | CGF.getContext().getDeclAlign(Pair.first)); |
| 2942 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2943 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2944 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2945 | if (Data.Reductions) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2946 | OMPLexicalScope LexScope(CGF, S, CapturedRegion); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2947 | ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies, |
| 2948 | Data.ReductionOps); |
| 2949 | llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( |
| 2950 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); |
| 2951 | for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { |
| 2952 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2953 | RedCG.emitAggregateType(CGF, Cnt); |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2954 | // FIXME: This must removed once the runtime library is fixed. |
| 2955 | // Emit required threadprivate variables for |
| 2956 | // initilizer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2957 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2958 | RedCG, Cnt); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2959 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2960 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2961 | Replacement = |
| 2962 | Address(CGF.EmitScalarConversion( |
| 2963 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 2964 | CGF.getContext().getPointerType( |
| 2965 | Data.ReductionCopies[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2966 | Data.ReductionCopies[Cnt]->getExprLoc()), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2967 | Replacement.getAlignment()); |
| 2968 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 2969 | Scope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 2970 | [Replacement]() { return Replacement; }); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2971 | } |
| 2972 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2973 | // Privatize all private variables except for in_reduction items. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2974 | (void)Scope.Privatize(); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2975 | SmallVector<const Expr *, 4> InRedVars; |
| 2976 | SmallVector<const Expr *, 4> InRedPrivs; |
| 2977 | SmallVector<const Expr *, 4> InRedOps; |
| 2978 | SmallVector<const Expr *, 4> TaskgroupDescriptors; |
| 2979 | for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { |
| 2980 | auto IPriv = C->privates().begin(); |
| 2981 | auto IRed = C->reduction_ops().begin(); |
| 2982 | auto ITD = C->taskgroup_descriptors().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2983 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2984 | InRedVars.emplace_back(Ref); |
| 2985 | InRedPrivs.emplace_back(*IPriv); |
| 2986 | InRedOps.emplace_back(*IRed); |
| 2987 | TaskgroupDescriptors.emplace_back(*ITD); |
| 2988 | std::advance(IPriv, 1); |
| 2989 | std::advance(IRed, 1); |
| 2990 | std::advance(ITD, 1); |
| 2991 | } |
| 2992 | } |
| 2993 | // Privatize in_reduction items here, because taskgroup descriptors must be |
| 2994 | // privatized earlier. |
| 2995 | OMPPrivateScope InRedScope(CGF); |
| 2996 | if (!InRedVars.empty()) { |
| 2997 | ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps); |
| 2998 | for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { |
| 2999 | RedCG.emitSharedLValue(CGF, Cnt); |
| 3000 | RedCG.emitAggregateType(CGF, Cnt); |
| 3001 | // The taskgroup descriptor variable is always implicit firstprivate and |
| 3002 | // privatized already during procoessing of the firstprivates. |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3003 | // FIXME: This must removed once the runtime library is fixed. |
| 3004 | // Emit required threadprivate variables for |
| 3005 | // initilizer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3006 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3007 | RedCG, Cnt); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3008 | llvm::Value *ReductionsPtr = |
| 3009 | CGF.EmitLoadOfScalar(CGF.EmitLValue(TaskgroupDescriptors[Cnt]), |
| 3010 | TaskgroupDescriptors[Cnt]->getExprLoc()); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3011 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3012 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3013 | Replacement = Address( |
| 3014 | CGF.EmitScalarConversion( |
| 3015 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 3016 | CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3017 | InRedPrivs[Cnt]->getExprLoc()), |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3018 | Replacement.getAlignment()); |
| 3019 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 3020 | InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 3021 | [Replacement]() { return Replacement; }); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3022 | } |
| 3023 | } |
| 3024 | (void)InRedScope.Privatize(); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3025 | |
| 3026 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3027 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3028 | }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3029 | llvm::Value *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3030 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 3031 | Data.NumberOfParts); |
| 3032 | OMPLexicalScope Scope(*this, S); |
| 3033 | TaskGen(*this, OutlinedFn, Data); |
| 3034 | } |
| 3035 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3036 | static ImplicitParamDecl * |
| 3037 | createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3038 | QualType Ty, CapturedDecl *CD, |
| 3039 | SourceLocation Loc) { |
| 3040 | auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3041 | ImplicitParamDecl::Other); |
| 3042 | auto *OrigRef = DeclRefExpr::Create( |
| 3043 | C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, |
| 3044 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
| 3045 | auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3046 | ImplicitParamDecl::Other); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3047 | auto *PrivateRef = DeclRefExpr::Create( |
| 3048 | C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3049 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3050 | QualType ElemType = C.getBaseElementType(Ty); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3051 | auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType, |
| 3052 | ImplicitParamDecl::Other); |
| 3053 | auto *InitRef = DeclRefExpr::Create( |
| 3054 | C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, |
| 3055 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3056 | PrivateVD->setInitStyle(VarDecl::CInit); |
| 3057 | PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, |
| 3058 | InitRef, /*BasePath=*/nullptr, |
| 3059 | VK_RValue)); |
| 3060 | Data.FirstprivateVars.emplace_back(OrigRef); |
| 3061 | Data.FirstprivateCopies.emplace_back(PrivateRef); |
| 3062 | Data.FirstprivateInits.emplace_back(InitRef); |
| 3063 | return OrigVD; |
| 3064 | } |
| 3065 | |
| 3066 | void CodeGenFunction::EmitOMPTargetTaskBasedDirective( |
| 3067 | const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, |
| 3068 | OMPTargetDataInfo &InputInfo) { |
| 3069 | // Emit outlined function for task construct. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3070 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
| 3071 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3072 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3073 | auto I = CS->getCapturedDecl()->param_begin(); |
| 3074 | auto PartId = std::next(I); |
| 3075 | auto TaskT = std::next(I, 4); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3076 | OMPTaskDataTy Data; |
| 3077 | // The task is not final. |
| 3078 | Data.Final.setInt(/*IntVal=*/false); |
| 3079 | // Get list of firstprivate variables. |
| 3080 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 3081 | auto IRef = C->varlist_begin(); |
| 3082 | auto IElemInitRef = C->inits().begin(); |
| 3083 | for (auto *IInit : C->private_copies()) { |
| 3084 | Data.FirstprivateVars.push_back(*IRef); |
| 3085 | Data.FirstprivateCopies.push_back(IInit); |
| 3086 | Data.FirstprivateInits.push_back(*IElemInitRef); |
| 3087 | ++IRef; |
| 3088 | ++IElemInitRef; |
| 3089 | } |
| 3090 | } |
| 3091 | OMPPrivateScope TargetScope(*this); |
| 3092 | VarDecl *BPVD = nullptr; |
| 3093 | VarDecl *PVD = nullptr; |
| 3094 | VarDecl *SVD = nullptr; |
| 3095 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3096 | auto *CD = CapturedDecl::Create( |
| 3097 | getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); |
| 3098 | llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); |
| 3099 | QualType BaseAndPointersType = getContext().getConstantArrayType( |
| 3100 | getContext().VoidPtrTy, ArrSize, ArrayType::Normal, |
| 3101 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3102 | BPVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3103 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3104 | PVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3105 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3106 | QualType SizesType = getContext().getConstantArrayType( |
| 3107 | getContext().getSizeType(), ArrSize, ArrayType::Normal, |
| 3108 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3109 | SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3110 | S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3111 | TargetScope.addPrivate( |
| 3112 | BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); |
| 3113 | TargetScope.addPrivate(PVD, |
| 3114 | [&InputInfo]() { return InputInfo.PointersArray; }); |
| 3115 | TargetScope.addPrivate(SVD, |
| 3116 | [&InputInfo]() { return InputInfo.SizesArray; }); |
| 3117 | } |
| 3118 | (void)TargetScope.Privatize(); |
| 3119 | // Build list of dependences. |
| 3120 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3121 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3122 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3123 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, |
| 3124 | &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3125 | // Set proper addresses for generated private copies. |
| 3126 | OMPPrivateScope Scope(CGF); |
| 3127 | if (!Data.FirstprivateVars.empty()) { |
| 3128 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3129 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 3130 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 3131 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 3132 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3133 | // Map privates. |
| 3134 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3135 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3136 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3137 | for (const Expr *E : Data.FirstprivateVars) { |
| 3138 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3139 | Address PrivatePtr = |
| 3140 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3141 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3142 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3143 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3144 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3145 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3146 | CopyFn, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3147 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3148 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3149 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3150 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3151 | } |
| 3152 | } |
| 3153 | // Privatize all private variables except for in_reduction items. |
| 3154 | (void)Scope.Privatize(); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3155 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3156 | InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( |
| 3157 | CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0, CGF.getPointerSize()); |
| 3158 | InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( |
| 3159 | CGF.GetAddrOfLocalVar(PVD), /*Index=*/0, CGF.getPointerSize()); |
| 3160 | InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( |
| 3161 | CGF.GetAddrOfLocalVar(SVD), /*Index=*/0, CGF.getSizeSize()); |
| 3162 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3163 | |
| 3164 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3165 | OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3166 | BodyGen(CGF); |
| 3167 | }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3168 | llvm::Value *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3169 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, |
| 3170 | Data.NumberOfParts); |
| 3171 | llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); |
| 3172 | IntegerLiteral IfCond(getContext(), TrueOrFalse, |
| 3173 | getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3174 | SourceLocation()); |
| 3175 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3176 | CGM.getOpenMPRuntime().emitTaskCall(*this, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3177 | SharedsTy, CapturedStruct, &IfCond, Data); |
| 3178 | } |
| 3179 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3180 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 3181 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3182 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3183 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3184 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3185 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 3186 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3187 | if (C->getNameModifier() == OMPD_unknown || |
| 3188 | C->getNameModifier() == OMPD_task) { |
| 3189 | IfCond = C->getCondition(); |
| 3190 | break; |
| 3191 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3192 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3193 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3194 | OMPTaskDataTy Data; |
| 3195 | // Check if we should emit tied or untied task. |
| 3196 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3197 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3198 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3199 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3200 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3201 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3202 | const OMPTaskDataTy &Data) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3203 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3204 | SharedsTy, CapturedStruct, IfCond, |
| 3205 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3206 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3207 | EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3210 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 3211 | const OMPTaskyieldDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3212 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getBeginLoc()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3215 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3216 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3217 | } |
| 3218 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3219 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3220 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getBeginLoc()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3223 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 3224 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3225 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3226 | Action.Enter(CGF); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3227 | if (const Expr *E = S.getReductionRef()) { |
| 3228 | SmallVector<const Expr *, 4> LHSs; |
| 3229 | SmallVector<const Expr *, 4> RHSs; |
| 3230 | OMPTaskDataTy Data; |
| 3231 | for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { |
| 3232 | auto IPriv = C->privates().begin(); |
| 3233 | auto IRed = C->reduction_ops().begin(); |
| 3234 | auto ILHS = C->lhs_exprs().begin(); |
| 3235 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3236 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3237 | Data.ReductionVars.emplace_back(Ref); |
| 3238 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3239 | Data.ReductionOps.emplace_back(*IRed); |
| 3240 | LHSs.emplace_back(*ILHS); |
| 3241 | RHSs.emplace_back(*IRHS); |
| 3242 | std::advance(IPriv, 1); |
| 3243 | std::advance(IRed, 1); |
| 3244 | std::advance(ILHS, 1); |
| 3245 | std::advance(IRHS, 1); |
| 3246 | } |
| 3247 | } |
| 3248 | llvm::Value *ReductionDesc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3249 | CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getBeginLoc(), |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3250 | LHSs, RHSs, Data); |
| 3251 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3252 | CGF.EmitVarDecl(*VD); |
| 3253 | CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), |
| 3254 | /*Volatile=*/false, E->getType()); |
| 3255 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3256 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3257 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3258 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3259 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3260 | } |
| 3261 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3262 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3263 | CGM.getOpenMPRuntime().emitFlush( |
| 3264 | *this, |
| 3265 | [&S]() -> ArrayRef<const Expr *> { |
| 3266 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) |
| 3267 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 3268 | FlushClause->varlist_end()); |
| 3269 | return llvm::None; |
| 3270 | }(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3271 | S.getBeginLoc()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3272 | } |
| 3273 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3274 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, |
| 3275 | const CodeGenLoopTy &CodeGenLoop, |
| 3276 | Expr *IncExpr) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3277 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3278 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 3279 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3280 | EmitVarDecl(*IVDecl); |
| 3281 | |
| 3282 | // Emit the iterations count variable. |
| 3283 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 3284 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3285 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3286 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3287 | // Emit calculation of the iterations count. |
| 3288 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3289 | } |
| 3290 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3291 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3292 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3293 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3294 | // Check pre-condition. |
| 3295 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3296 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3297 | // Skip the entire loop if we don't meet the precondition. |
| 3298 | // If the condition constant folds and can be elided, avoid emitting the |
| 3299 | // whole loop. |
| 3300 | bool CondConstant; |
| 3301 | llvm::BasicBlock *ContBlock = nullptr; |
| 3302 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3303 | if (!CondConstant) |
| 3304 | return; |
| 3305 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3306 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3307 | ContBlock = createBasicBlock("omp.precond.end"); |
| 3308 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3309 | getProfileCount(&S)); |
| 3310 | EmitBlock(ThenBlock); |
| 3311 | incrementProfileCounter(&S); |
| 3312 | } |
| 3313 | |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3314 | emitAlignedClause(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3315 | // Emit 'then' code. |
| 3316 | { |
| 3317 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3318 | |
| 3319 | LValue LB = EmitOMPHelperVar( |
| 3320 | *this, cast<DeclRefExpr>( |
| 3321 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3322 | ? S.getCombinedLowerBoundVariable() |
| 3323 | : S.getLowerBoundVariable()))); |
| 3324 | LValue UB = EmitOMPHelperVar( |
| 3325 | *this, cast<DeclRefExpr>( |
| 3326 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3327 | ? S.getCombinedUpperBoundVariable() |
| 3328 | : S.getUpperBoundVariable()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3329 | LValue ST = |
| 3330 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 3331 | LValue IL = |
| 3332 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 3333 | |
| 3334 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3335 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3336 | // Emit implicit barrier to synchronize threads and avoid data races |
| 3337 | // on initialization of firstprivate variables and post-update of |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3338 | // lastprivate variables. |
| 3339 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3340 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3341 | /*ForceSimpleCall=*/true); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3342 | } |
| 3343 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3344 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 3345 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3346 | !isOpenMPTeamsDirective(S.getDirectiveKind())) |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3347 | EmitOMPReductionClauseInit(S, LoopScope); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3348 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3349 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3350 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 3351 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 3352 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3353 | |
| 3354 | // Detect the distribute schedule kind and chunk. |
| 3355 | llvm::Value *Chunk = nullptr; |
| 3356 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3357 | if (const auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3358 | ScheduleKind = C->getDistScheduleKind(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3359 | if (const Expr *Ch = C->getChunkSize()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3360 | Chunk = EmitScalarExpr(Ch); |
| 3361 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3362 | S.getIterationVariable()->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3363 | S.getBeginLoc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3364 | } |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 3365 | } else { |
| 3366 | // Default behaviour for dist_schedule clause. |
| 3367 | CGM.getOpenMPRuntime().getDefaultDistScheduleAndChunk( |
| 3368 | *this, S, ScheduleKind, Chunk); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3369 | } |
| 3370 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 3371 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 3372 | |
| 3373 | // OpenMP [2.10.8, distribute Construct, Description] |
| 3374 | // If dist_schedule is specified, kind must be static. If specified, |
| 3375 | // iterations are divided into chunks of size chunk_size, chunks are |
| 3376 | // assigned to the teams of the league in a round-robin fashion in the |
| 3377 | // order of the team number. When no chunk_size is specified, the |
| 3378 | // iteration space is divided into chunks that are approximately equal |
| 3379 | // in size, and at most one chunk is distributed to each team of the |
| 3380 | // league. The size of the chunks is unspecified in this case. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3381 | bool StaticChunked = RT.isStaticChunked( |
| 3382 | ScheduleKind, /* Chunked */ Chunk != nullptr) && |
| 3383 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3384 | if (RT.isStaticNonchunked(ScheduleKind, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3385 | /* Chunked */ Chunk != nullptr) || |
| 3386 | StaticChunked) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3387 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 3388 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3389 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 3390 | IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3391 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 3392 | StaticChunked ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3393 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3394 | StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3395 | JumpDest LoopExit = |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3396 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 3397 | // UB = min(UB, GlobalUB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3398 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3399 | ? S.getCombinedEnsureUpperBound() |
| 3400 | : S.getEnsureUpperBound()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3401 | // IV = LB; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3402 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3403 | ? S.getCombinedInit() |
| 3404 | : S.getInit()); |
| 3405 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3406 | const Expr *Cond = |
| 3407 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3408 | ? S.getCombinedCond() |
| 3409 | : S.getCond(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3410 | |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3411 | if (StaticChunked) |
| 3412 | Cond = S.getCombinedDistCond(); |
| 3413 | |
| 3414 | // For static unchunked schedules generate: |
| 3415 | // |
| 3416 | // 1. For distribute alone, codegen |
| 3417 | // while (idx <= UB) { |
| 3418 | // BODY; |
| 3419 | // ++idx; |
| 3420 | // } |
| 3421 | // |
| 3422 | // 2. When combined with 'for' (e.g. as in 'distribute parallel for') |
| 3423 | // while (idx <= UB) { |
| 3424 | // <CodeGen rest of pragma>(LB, UB); |
| 3425 | // idx += ST; |
| 3426 | // } |
| 3427 | // |
| 3428 | // For static chunk one schedule generate: |
| 3429 | // |
| 3430 | // while (IV <= GlobalUB) { |
| 3431 | // <CodeGen rest of pragma>(LB, UB); |
| 3432 | // LB += ST; |
| 3433 | // UB += ST; |
| 3434 | // UB = min(UB, GlobalUB); |
| 3435 | // IV = LB; |
| 3436 | // } |
| 3437 | // |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3438 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr, |
| 3439 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 3440 | CodeGenLoop(CGF, S, LoopExit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3441 | }, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3442 | [&S, StaticChunked](CodeGenFunction &CGF) { |
| 3443 | if (StaticChunked) { |
| 3444 | CGF.EmitIgnoredExpr(S.getCombinedNextLowerBound()); |
| 3445 | CGF.EmitIgnoredExpr(S.getCombinedNextUpperBound()); |
| 3446 | CGF.EmitIgnoredExpr(S.getCombinedEnsureUpperBound()); |
| 3447 | CGF.EmitIgnoredExpr(S.getCombinedInit()); |
| 3448 | } |
| 3449 | }); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3450 | EmitBlock(LoopExit.getBlock()); |
| 3451 | // Tell the runtime we are done. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3452 | RT.emitForStaticFinish(*this, S.getBeginLoc(), S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3453 | } else { |
| 3454 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 3455 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3456 | const OMPLoopArguments LoopArguments = { |
| 3457 | LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(), |
| 3458 | Chunk}; |
| 3459 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, |
| 3460 | CodeGenLoop); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3461 | } |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3462 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3463 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3464 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3465 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3466 | }); |
| 3467 | } |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3468 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 3469 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3470 | !isOpenMPTeamsDirective(S.getDirectiveKind())) { |
Jonas Hahnfeld | 5aaaece | 2018-10-02 19:12:47 +0000 | [diff] [blame] | 3471 | EmitOMPReductionClauseFinal(S, OMPD_simd); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3472 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 3473 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3474 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3475 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3476 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3477 | }); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3478 | } |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3479 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3480 | if (HasLastprivateClause) { |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3481 | EmitOMPLastprivateClauseFinal( |
| 3482 | S, /*NoFinals=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3483 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3484 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3485 | } |
| 3486 | |
| 3487 | // We're now done with the loop, so jump to the continuation block. |
| 3488 | if (ContBlock) { |
| 3489 | EmitBranch(ContBlock); |
| 3490 | EmitBlock(ContBlock, true); |
| 3491 | } |
| 3492 | } |
| 3493 | } |
| 3494 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3495 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 3496 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3497 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3498 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3499 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3500 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 3501 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3504 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 3505 | const CapturedStmt *S) { |
| 3506 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 3507 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 3508 | CGF.CapturedStmtInfo = &CapStmtInfo; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3509 | llvm::Function *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3510 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3511 | return Fn; |
| 3512 | } |
| 3513 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3514 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3515 | if (S.hasClausesOfKind<OMPDependClause>()) { |
| 3516 | assert(!S.getAssociatedStmt() && |
| 3517 | "No associated statement must be in ordered depend construct."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3518 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 3519 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3520 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3521 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3522 | const auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3523 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 3524 | PrePostActionTy &Action) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3525 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3526 | if (C) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3527 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3528 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3529 | llvm::Function *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3530 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3531 | OutlinedFn, CapturedVars); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3532 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3533 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3534 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3535 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3536 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3537 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3538 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getBeginLoc(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 3539 | } |
| 3540 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3541 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3542 | QualType SrcType, QualType DestType, |
| 3543 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3544 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 3545 | "DestType must have scalar evaluation kind."); |
| 3546 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3547 | return Val.isScalar() ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 3548 | DestType, Loc) |
| 3549 | : CGF.EmitComplexToScalarConversion( |
| 3550 | Val.getComplexVal(), SrcType, DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3551 | } |
| 3552 | |
| 3553 | static CodeGenFunction::ComplexPairTy |
| 3554 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3555 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3556 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 3557 | "DestType must have complex evaluation kind."); |
| 3558 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 3559 | if (Val.isScalar()) { |
| 3560 | // Convert the input element to the element type of the complex. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3561 | QualType DestElementType = |
| 3562 | DestType->castAs<ComplexType>()->getElementType(); |
| 3563 | llvm::Value *ScalarVal = CGF.EmitScalarConversion( |
| 3564 | Val.getScalarVal(), SrcType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3565 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 3566 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 3567 | } else { |
| 3568 | assert(Val.isComplex() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3569 | QualType SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 3570 | QualType DestElementType = |
| 3571 | DestType->castAs<ComplexType>()->getElementType(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3572 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3573 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3574 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3575 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3576 | } |
| 3577 | return ComplexVal; |
| 3578 | } |
| 3579 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3580 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 3581 | LValue LVal, RValue RVal) { |
| 3582 | if (LVal.isGlobalReg()) { |
| 3583 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 3584 | } else { |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3585 | CGF.EmitAtomicStore(RVal, LVal, |
| 3586 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3587 | : llvm::AtomicOrdering::Monotonic, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3588 | LVal.isVolatile(), /*IsInit=*/false); |
| 3589 | } |
| 3590 | } |
| 3591 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3592 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 3593 | QualType RValTy, SourceLocation Loc) { |
| 3594 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3595 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3596 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 3597 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 3598 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3599 | break; |
| 3600 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3601 | EmitStoreOfComplex( |
| 3602 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3603 | /*isInit=*/false); |
| 3604 | break; |
| 3605 | case TEK_Aggregate: |
| 3606 | llvm_unreachable("Must be a scalar or complex."); |
| 3607 | } |
| 3608 | } |
| 3609 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3610 | static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3611 | const Expr *X, const Expr *V, |
| 3612 | SourceLocation Loc) { |
| 3613 | // v = x; |
| 3614 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 3615 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 3616 | LValue XLValue = CGF.EmitLValue(X); |
| 3617 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 3618 | RValue Res = XLValue.isGlobalReg() |
| 3619 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3620 | : CGF.EmitAtomicLoad( |
| 3621 | XLValue, Loc, |
| 3622 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3623 | : llvm::AtomicOrdering::Monotonic, |
| 3624 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3625 | // OpenMP, 2.12.6, atomic Construct |
| 3626 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3627 | // performed operation to include an implicit flush operation without a |
| 3628 | // list. |
| 3629 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3630 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3631 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3634 | static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3635 | const Expr *X, const Expr *E, |
| 3636 | SourceLocation Loc) { |
| 3637 | // x = expr; |
| 3638 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3639 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3640 | // OpenMP, 2.12.6, atomic Construct |
| 3641 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3642 | // performed operation to include an implicit flush operation without a |
| 3643 | // list. |
| 3644 | if (IsSeqCst) |
| 3645 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3646 | } |
| 3647 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3648 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 3649 | RValue Update, |
| 3650 | BinaryOperatorKind BO, |
| 3651 | llvm::AtomicOrdering AO, |
| 3652 | bool IsXLHSInRHSPart) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3653 | ASTContext &Context = CGF.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3654 | // 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] | 3655 | // expression is simple and atomic is allowed for the given type for the |
| 3656 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3657 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 3658 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 3659 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 3660 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3661 | X.getAddress().getElementType())) || |
| 3662 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3663 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 3664 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3665 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3666 | |
| 3667 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 3668 | switch (BO) { |
| 3669 | case BO_Add: |
| 3670 | RMWOp = llvm::AtomicRMWInst::Add; |
| 3671 | break; |
| 3672 | case BO_Sub: |
| 3673 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3674 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3675 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 3676 | break; |
| 3677 | case BO_And: |
| 3678 | RMWOp = llvm::AtomicRMWInst::And; |
| 3679 | break; |
| 3680 | case BO_Or: |
| 3681 | RMWOp = llvm::AtomicRMWInst::Or; |
| 3682 | break; |
| 3683 | case BO_Xor: |
| 3684 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 3685 | break; |
| 3686 | case BO_LT: |
| 3687 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3688 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 3689 | : llvm::AtomicRMWInst::Max) |
| 3690 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 3691 | : llvm::AtomicRMWInst::UMax); |
| 3692 | break; |
| 3693 | case BO_GT: |
| 3694 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3695 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 3696 | : llvm::AtomicRMWInst::Min) |
| 3697 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 3698 | : llvm::AtomicRMWInst::UMin); |
| 3699 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3700 | case BO_Assign: |
| 3701 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 3702 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3703 | case BO_Mul: |
| 3704 | case BO_Div: |
| 3705 | case BO_Rem: |
| 3706 | case BO_Shl: |
| 3707 | case BO_Shr: |
| 3708 | case BO_LAnd: |
| 3709 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3710 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3711 | case BO_PtrMemD: |
| 3712 | case BO_PtrMemI: |
| 3713 | case BO_LE: |
| 3714 | case BO_GE: |
| 3715 | case BO_EQ: |
| 3716 | case BO_NE: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 3717 | case BO_Cmp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3718 | case BO_AddAssign: |
| 3719 | case BO_SubAssign: |
| 3720 | case BO_AndAssign: |
| 3721 | case BO_OrAssign: |
| 3722 | case BO_XorAssign: |
| 3723 | case BO_MulAssign: |
| 3724 | case BO_DivAssign: |
| 3725 | case BO_RemAssign: |
| 3726 | case BO_ShlAssign: |
| 3727 | case BO_ShrAssign: |
| 3728 | case BO_Comma: |
| 3729 | llvm_unreachable("Unsupported atomic update operation"); |
| 3730 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3731 | llvm::Value *UpdateVal = Update.getScalarVal(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3732 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 3733 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3734 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3735 | X.getType()->hasSignedIntegerRepresentation()); |
| 3736 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3737 | llvm::Value *Res = |
| 3738 | CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3739 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3740 | } |
| 3741 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3742 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3743 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 3744 | llvm::AtomicOrdering AO, SourceLocation Loc, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3745 | const llvm::function_ref<RValue(RValue)> CommonGen) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3746 | // Update expressions are allowed to have the following forms: |
| 3747 | // x binop= expr; -> xrval + expr; |
| 3748 | // x++, ++x -> xrval + 1; |
| 3749 | // x--, --x -> xrval - 1; |
| 3750 | // x = x binop expr; -> xrval binop expr |
| 3751 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3752 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 3753 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3754 | if (X.isGlobalReg()) { |
| 3755 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 3756 | // 'xrval'. |
| 3757 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 3758 | } else { |
| 3759 | // Perform compare-and-swap procedure. |
| 3760 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3761 | } |
| 3762 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3763 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3766 | static void emitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3767 | const Expr *X, const Expr *E, |
| 3768 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3769 | SourceLocation Loc) { |
| 3770 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3771 | "Update expr in 'atomic update' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3772 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3773 | // Update expressions are allowed to have the following forms: |
| 3774 | // x binop= expr; -> xrval + expr; |
| 3775 | // x++, ++x -> xrval + 1; |
| 3776 | // x--, --x -> xrval - 1; |
| 3777 | // x = x binop expr; -> xrval binop expr |
| 3778 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3779 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3780 | LValue XLValue = CGF.EmitLValue(X); |
| 3781 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3782 | llvm::AtomicOrdering AO = IsSeqCst |
| 3783 | ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3784 | : llvm::AtomicOrdering::Monotonic; |
| 3785 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3786 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3787 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3788 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3789 | auto &&Gen = [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) { |
| 3790 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3791 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3792 | return CGF.EmitAnyExpr(UE); |
| 3793 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3794 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3795 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3796 | // OpenMP, 2.12.6, atomic Construct |
| 3797 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3798 | // performed operation to include an implicit flush operation without a |
| 3799 | // list. |
| 3800 | if (IsSeqCst) |
| 3801 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3802 | } |
| 3803 | |
| 3804 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3805 | QualType SourceType, QualType ResType, |
| 3806 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3807 | switch (CGF.getEvaluationKind(ResType)) { |
| 3808 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3809 | return RValue::get( |
| 3810 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3811 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3812 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3813 | return RValue::getComplex(Res.first, Res.second); |
| 3814 | } |
| 3815 | case TEK_Aggregate: |
| 3816 | break; |
| 3817 | } |
| 3818 | llvm_unreachable("Must be a scalar or complex."); |
| 3819 | } |
| 3820 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3821 | static void emitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3822 | bool IsPostfixUpdate, const Expr *V, |
| 3823 | const Expr *X, const Expr *E, |
| 3824 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3825 | SourceLocation Loc) { |
| 3826 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 3827 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 3828 | RValue NewVVal; |
| 3829 | LValue VLValue = CGF.EmitLValue(V); |
| 3830 | LValue XLValue = CGF.EmitLValue(X); |
| 3831 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3832 | llvm::AtomicOrdering AO = IsSeqCst |
| 3833 | ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3834 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3835 | QualType NewVValType; |
| 3836 | if (UE) { |
| 3837 | // 'x' is updated with some additional value. |
| 3838 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3839 | "Update expr in 'atomic capture' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3840 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3841 | // Update expressions are allowed to have the following forms: |
| 3842 | // x binop= expr; -> xrval + expr; |
| 3843 | // x++, ++x -> xrval + 1; |
| 3844 | // x--, --x -> xrval - 1; |
| 3845 | // x = x binop expr; -> xrval binop expr |
| 3846 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3847 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3848 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3849 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3850 | NewVValType = XRValExpr->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3851 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3852 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3853 | IsPostfixUpdate](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3854 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3855 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3856 | RValue Res = CGF.EmitAnyExpr(UE); |
| 3857 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 3858 | return Res; |
| 3859 | }; |
| 3860 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3861 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3862 | if (Res.first) { |
| 3863 | // 'atomicrmw' instruction was generated. |
| 3864 | if (IsPostfixUpdate) { |
| 3865 | // Use old value from 'atomicrmw'. |
| 3866 | NewVVal = Res.second; |
| 3867 | } else { |
| 3868 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 3869 | // value of 'x'. |
| 3870 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3871 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 3872 | NewVVal = CGF.EmitAnyExpr(UE); |
| 3873 | } |
| 3874 | } |
| 3875 | } else { |
| 3876 | // 'x' is simply rewritten with some 'expr'. |
| 3877 | NewVValType = X->getType().getNonReferenceType(); |
| 3878 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3879 | X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3880 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3881 | NewVVal = XRValue; |
| 3882 | return ExprRValue; |
| 3883 | }; |
| 3884 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 3885 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3886 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 3887 | Loc, Gen); |
| 3888 | if (Res.first) { |
| 3889 | // 'atomicrmw' instruction was generated. |
| 3890 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 3891 | } |
| 3892 | } |
| 3893 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3894 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3895 | // OpenMP, 2.12.6, atomic Construct |
| 3896 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3897 | // performed operation to include an implicit flush operation without a |
| 3898 | // list. |
| 3899 | if (IsSeqCst) |
| 3900 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3901 | } |
| 3902 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3903 | static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3904 | bool IsSeqCst, bool IsPostfixUpdate, |
| 3905 | const Expr *X, const Expr *V, const Expr *E, |
| 3906 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3907 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3908 | switch (Kind) { |
| 3909 | case OMPC_read: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3910 | emitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3911 | break; |
| 3912 | case OMPC_write: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3913 | emitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3914 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3915 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3916 | case OMPC_update: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3917 | emitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3918 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3919 | case OMPC_capture: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3920 | emitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3921 | IsXLHSInRHSPart, Loc); |
| 3922 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3923 | case OMPC_if: |
| 3924 | case OMPC_final: |
| 3925 | case OMPC_num_threads: |
| 3926 | case OMPC_private: |
| 3927 | case OMPC_firstprivate: |
| 3928 | case OMPC_lastprivate: |
| 3929 | case OMPC_reduction: |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 3930 | case OMPC_task_reduction: |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 3931 | case OMPC_in_reduction: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3932 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 3933 | case OMPC_simdlen: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3934 | case OMPC_collapse: |
| 3935 | case OMPC_default: |
| 3936 | case OMPC_seq_cst: |
| 3937 | case OMPC_shared: |
| 3938 | case OMPC_linear: |
| 3939 | case OMPC_aligned: |
| 3940 | case OMPC_copyin: |
| 3941 | case OMPC_copyprivate: |
| 3942 | case OMPC_flush: |
| 3943 | case OMPC_proc_bind: |
| 3944 | case OMPC_schedule: |
| 3945 | case OMPC_ordered: |
| 3946 | case OMPC_nowait: |
| 3947 | case OMPC_untied: |
| 3948 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 3949 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3950 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 3951 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 3952 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 3953 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 3954 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 3955 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 3956 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 3957 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 3958 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 3959 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 3960 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 3961 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 3962 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 3963 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 3964 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 3965 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 3966 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 3967 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 3968 | case OMPC_is_device_ptr: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 3969 | case OMPC_unified_address: |
Alexey Bataev | 94c5064 | 2018-10-01 14:26:31 +0000 | [diff] [blame] | 3970 | case OMPC_unified_shared_memory: |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 3971 | case OMPC_reverse_offload: |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 3972 | case OMPC_dynamic_allocators: |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 3973 | case OMPC_atomic_default_mem_order: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3974 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 3975 | } |
| 3976 | } |
| 3977 | |
| 3978 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3979 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3980 | OpenMPClauseKind Kind = OMPC_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3981 | for (const OMPClause *C : S.clauses()) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3982 | // Find first clause (skip seq_cst clause, if it is first). |
| 3983 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 3984 | Kind = C->getClauseKind(); |
| 3985 | break; |
| 3986 | } |
| 3987 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3988 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3989 | const Stmt *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 3990 | if (const auto *FE = dyn_cast<FullExpr>(CS)) |
| 3991 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3992 | // Processing for statements under 'atomic capture'. |
| 3993 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3994 | for (const Stmt *C : Compound->body()) { |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 3995 | if (const auto *FE = dyn_cast<FullExpr>(C)) |
| 3996 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3997 | } |
| 3998 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3999 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4000 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, |
| 4001 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 4002 | CGF.EmitStopPoint(CS); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4003 | emitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4004 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4005 | S.isXLHSInRHSPart(), S.getBeginLoc()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 4006 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4007 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4008 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4009 | } |
| 4010 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4011 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 4012 | const OMPExecutableDirective &S, |
| 4013 | const RegionCodeGenTy &CodeGen) { |
| 4014 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 4015 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4016 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4017 | // On device emit this construct as inlined code. |
| 4018 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 4019 | OMPLexicalScope Scope(CGF, S, OMPD_target); |
| 4020 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4021 | CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 4ac68a2 | 2018-05-16 15:08:32 +0000 | [diff] [blame] | 4022 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4023 | }); |
| 4024 | return; |
| 4025 | } |
| 4026 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4027 | llvm::Function *Fn = nullptr; |
| 4028 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4029 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4030 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 4031 | // Check for the at most one if clause associated with the target region. |
| 4032 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4033 | if (C->getNameModifier() == OMPD_unknown || |
| 4034 | C->getNameModifier() == OMPD_target) { |
| 4035 | IfCond = C->getCondition(); |
| 4036 | break; |
| 4037 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4038 | } |
| 4039 | |
| 4040 | // Check if we have any device clause associated with the directive. |
| 4041 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4042 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4043 | Device = C->getDevice(); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4044 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4045 | // Check if we have an if clause whose conditional always evaluates to false |
| 4046 | // or if we do not have any targets specified. If so the target region is not |
| 4047 | // an offload entry point. |
| 4048 | bool IsOffloadEntry = true; |
| 4049 | if (IfCond) { |
| 4050 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4051 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4052 | IsOffloadEntry = false; |
| 4053 | } |
| 4054 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4055 | IsOffloadEntry = false; |
| 4056 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4057 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4058 | StringRef ParentName; |
| 4059 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 4060 | // the mangling of the device outlined kernel. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4061 | if (const auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4062 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4063 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4064 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 4065 | else |
| 4066 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4067 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4068 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4069 | // Emit target region as a standalone region. |
| 4070 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 4071 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4072 | OMPLexicalScope Scope(CGF, S, OMPD_task); |
| 4073 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4076 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 4077 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4078 | Action.Enter(CGF); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4079 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4080 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4081 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4082 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4083 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4084 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4085 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4086 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4087 | } |
| 4088 | |
| 4089 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 4090 | StringRef ParentName, |
| 4091 | const OMPTargetDirective &S) { |
| 4092 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4093 | emitTargetRegion(CGF, S, Action); |
| 4094 | }; |
| 4095 | llvm::Function *Fn; |
| 4096 | llvm::Constant *Addr; |
| 4097 | // Emit target region as a standalone region. |
| 4098 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4099 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4100 | assert(Fn && Addr && "Target device function emission failed."); |
| 4101 | } |
| 4102 | |
| 4103 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 4104 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4105 | emitTargetRegion(CGF, S, Action); |
| 4106 | }; |
| 4107 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4108 | } |
| 4109 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4110 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 4111 | const OMPExecutableDirective &S, |
| 4112 | OpenMPDirectiveKind InnermostKind, |
| 4113 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4114 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4115 | llvm::Value *OutlinedFn = |
| 4116 | CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 4117 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 4118 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4119 | const auto *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 4120 | const auto *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4121 | if (NT || TL) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4122 | const Expr *NumTeams = NT ? NT->getNumTeams() : nullptr; |
| 4123 | const Expr *ThreadLimit = TL ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4124 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4125 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4126 | S.getBeginLoc()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4129 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4130 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4131 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4132 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getBeginLoc(), OutlinedFn, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4133 | CapturedVars); |
| 4134 | } |
| 4135 | |
| 4136 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 4137 | // Emit teams region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4138 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4139 | Action.Enter(CGF); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4140 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 4141 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4142 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4143 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4144 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4145 | CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4146 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4147 | }; |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4148 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4149 | emitPostUpdateForReductionClause(*this, S, |
| 4150 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4151 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4152 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4153 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4154 | const OMPTargetTeamsDirective &S) { |
| 4155 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 4156 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4157 | // Emit teams region as a standalone region. |
| 4158 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4159 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4160 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4161 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4162 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4163 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4164 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4165 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4166 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4167 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4168 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4169 | }; |
| 4170 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4171 | emitPostUpdateForReductionClause(CGF, S, |
| 4172 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 4176 | CodeGenModule &CGM, StringRef ParentName, |
| 4177 | const OMPTargetTeamsDirective &S) { |
| 4178 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4179 | emitTargetTeamsRegion(CGF, Action, S); |
| 4180 | }; |
| 4181 | llvm::Function *Fn; |
| 4182 | llvm::Constant *Addr; |
| 4183 | // Emit target region as a standalone region. |
| 4184 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4185 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4186 | assert(Fn && Addr && "Target device function emission failed."); |
| 4187 | } |
| 4188 | |
| 4189 | void CodeGenFunction::EmitOMPTargetTeamsDirective( |
| 4190 | const OMPTargetTeamsDirective &S) { |
| 4191 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4192 | emitTargetTeamsRegion(CGF, Action, S); |
| 4193 | }; |
| 4194 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4195 | } |
| 4196 | |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4197 | static void |
| 4198 | emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4199 | const OMPTargetTeamsDistributeDirective &S) { |
| 4200 | Action.Enter(CGF); |
| 4201 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4202 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4203 | }; |
| 4204 | |
| 4205 | // Emit teams region as a standalone region. |
| 4206 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4207 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4208 | Action.Enter(CGF); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4209 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4210 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4211 | (void)PrivateScope.Privatize(); |
| 4212 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4213 | CodeGenDistribute); |
| 4214 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4215 | }; |
| 4216 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); |
| 4217 | emitPostUpdateForReductionClause(CGF, S, |
| 4218 | [](CodeGenFunction &) { return nullptr; }); |
| 4219 | } |
| 4220 | |
| 4221 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| 4222 | CodeGenModule &CGM, StringRef ParentName, |
| 4223 | const OMPTargetTeamsDistributeDirective &S) { |
| 4224 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4225 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4226 | }; |
| 4227 | llvm::Function *Fn; |
| 4228 | llvm::Constant *Addr; |
| 4229 | // Emit target region as a standalone region. |
| 4230 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4231 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4232 | assert(Fn && Addr && "Target device function emission failed."); |
| 4233 | } |
| 4234 | |
| 4235 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 4236 | const OMPTargetTeamsDistributeDirective &S) { |
| 4237 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4238 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4239 | }; |
| 4240 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4241 | } |
| 4242 | |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4243 | static void emitTargetTeamsDistributeSimdRegion( |
| 4244 | CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4245 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4246 | Action.Enter(CGF); |
| 4247 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4248 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4249 | }; |
| 4250 | |
| 4251 | // Emit teams region as a standalone region. |
| 4252 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4253 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4254 | Action.Enter(CGF); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4255 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4256 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4257 | (void)PrivateScope.Privatize(); |
| 4258 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4259 | CodeGenDistribute); |
| 4260 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4261 | }; |
| 4262 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); |
| 4263 | emitPostUpdateForReductionClause(CGF, S, |
| 4264 | [](CodeGenFunction &) { return nullptr; }); |
| 4265 | } |
| 4266 | |
| 4267 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| 4268 | CodeGenModule &CGM, StringRef ParentName, |
| 4269 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4270 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4271 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4272 | }; |
| 4273 | llvm::Function *Fn; |
| 4274 | llvm::Constant *Addr; |
| 4275 | // Emit target region as a standalone region. |
| 4276 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4277 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4278 | assert(Fn && Addr && "Target device function emission failed."); |
| 4279 | } |
| 4280 | |
| 4281 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 4282 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4283 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4284 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4285 | }; |
| 4286 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4287 | } |
| 4288 | |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4289 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 4290 | const OMPTeamsDistributeDirective &S) { |
| 4291 | |
| 4292 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4293 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4294 | }; |
| 4295 | |
| 4296 | // Emit teams region as a standalone region. |
| 4297 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4298 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4299 | Action.Enter(CGF); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4300 | OMPPrivateScope PrivateScope(CGF); |
| 4301 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4302 | (void)PrivateScope.Privatize(); |
| 4303 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4304 | CodeGenDistribute); |
| 4305 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4306 | }; |
Alexey Bataev | 95c6dd4 | 2017-11-29 15:14:16 +0000 | [diff] [blame] | 4307 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4308 | emitPostUpdateForReductionClause(*this, S, |
| 4309 | [](CodeGenFunction &) { return nullptr; }); |
| 4310 | } |
| 4311 | |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4312 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 4313 | const OMPTeamsDistributeSimdDirective &S) { |
| 4314 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4315 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4316 | }; |
| 4317 | |
| 4318 | // Emit teams region as a standalone region. |
| 4319 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4320 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4321 | Action.Enter(CGF); |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4322 | OMPPrivateScope PrivateScope(CGF); |
| 4323 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4324 | (void)PrivateScope.Privatize(); |
| 4325 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, |
| 4326 | CodeGenDistribute); |
| 4327 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4328 | }; |
| 4329 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); |
| 4330 | emitPostUpdateForReductionClause(*this, S, |
| 4331 | [](CodeGenFunction &) { return nullptr; }); |
| 4332 | } |
| 4333 | |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4334 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 4335 | const OMPTeamsDistributeParallelForDirective &S) { |
| 4336 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4337 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4338 | S.getDistInc()); |
| 4339 | }; |
| 4340 | |
| 4341 | // Emit teams region as a standalone region. |
| 4342 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4343 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4344 | Action.Enter(CGF); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4345 | OMPPrivateScope PrivateScope(CGF); |
| 4346 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4347 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 4348 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4349 | CodeGenDistribute); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4350 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4351 | }; |
| 4352 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4353 | emitPostUpdateForReductionClause(*this, S, |
| 4354 | [](CodeGenFunction &) { return nullptr; }); |
| 4355 | } |
| 4356 | |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4357 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 4358 | const OMPTeamsDistributeParallelForSimdDirective &S) { |
| 4359 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4360 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4361 | S.getDistInc()); |
| 4362 | }; |
| 4363 | |
| 4364 | // Emit teams region as a standalone region. |
| 4365 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4366 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4367 | Action.Enter(CGF); |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4368 | OMPPrivateScope PrivateScope(CGF); |
| 4369 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4370 | (void)PrivateScope.Privatize(); |
| 4371 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4372 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4373 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4374 | }; |
| 4375 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4376 | emitPostUpdateForReductionClause(*this, S, |
| 4377 | [](CodeGenFunction &) { return nullptr; }); |
| 4378 | } |
| 4379 | |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4380 | static void emitTargetTeamsDistributeParallelForRegion( |
| 4381 | CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, |
| 4382 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4383 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4384 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4385 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4386 | S.getDistInc()); |
| 4387 | }; |
| 4388 | |
| 4389 | // Emit teams region as a standalone region. |
| 4390 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4391 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4392 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4393 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4394 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4395 | (void)PrivateScope.Privatize(); |
| 4396 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4397 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4398 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4399 | }; |
| 4400 | |
| 4401 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, |
| 4402 | CodeGenTeams); |
| 4403 | emitPostUpdateForReductionClause(CGF, S, |
| 4404 | [](CodeGenFunction &) { return nullptr; }); |
| 4405 | } |
| 4406 | |
| 4407 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 4408 | CodeGenModule &CGM, StringRef ParentName, |
| 4409 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4410 | // Emit SPMD target teams distribute parallel for region as a standalone |
| 4411 | // region. |
| 4412 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4413 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4414 | }; |
| 4415 | llvm::Function *Fn; |
| 4416 | llvm::Constant *Addr; |
| 4417 | // Emit target region as a standalone region. |
| 4418 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4419 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4420 | assert(Fn && Addr && "Target device function emission failed."); |
| 4421 | } |
| 4422 | |
| 4423 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 4424 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4425 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4426 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4427 | }; |
| 4428 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4429 | } |
| 4430 | |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4431 | static void emitTargetTeamsDistributeParallelForSimdRegion( |
| 4432 | CodeGenFunction &CGF, |
| 4433 | const OMPTargetTeamsDistributeParallelForSimdDirective &S, |
| 4434 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4435 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4436 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4437 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4438 | S.getDistInc()); |
| 4439 | }; |
| 4440 | |
| 4441 | // Emit teams region as a standalone region. |
| 4442 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4443 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4444 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4445 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4446 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4447 | (void)PrivateScope.Privatize(); |
| 4448 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4449 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4450 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4451 | }; |
| 4452 | |
| 4453 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd, |
| 4454 | CodeGenTeams); |
| 4455 | emitPostUpdateForReductionClause(CGF, S, |
| 4456 | [](CodeGenFunction &) { return nullptr; }); |
| 4457 | } |
| 4458 | |
| 4459 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 4460 | CodeGenModule &CGM, StringRef ParentName, |
| 4461 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4462 | // Emit SPMD target teams distribute parallel for simd region as a standalone |
| 4463 | // region. |
| 4464 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4465 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4466 | }; |
| 4467 | llvm::Function *Fn; |
| 4468 | llvm::Constant *Addr; |
| 4469 | // Emit target region as a standalone region. |
| 4470 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4471 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4472 | assert(Fn && Addr && "Target device function emission failed."); |
| 4473 | } |
| 4474 | |
| 4475 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 4476 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4477 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4478 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4479 | }; |
| 4480 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4481 | } |
| 4482 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4483 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 4484 | const OMPCancellationPointDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4485 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getBeginLoc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 4486 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4487 | } |
| 4488 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4489 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 4490 | const Expr *IfCond = nullptr; |
| 4491 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4492 | if (C->getNameModifier() == OMPD_unknown || |
| 4493 | C->getNameModifier() == OMPD_cancel) { |
| 4494 | IfCond = C->getCondition(); |
| 4495 | break; |
| 4496 | } |
| 4497 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4498 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 4499 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4500 | } |
| 4501 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4502 | CodeGenFunction::JumpDest |
| 4503 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4504 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
| 4505 | Kind == OMPD_target_parallel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4506 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4507 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4508 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 4509 | Kind == OMPD_distribute_parallel_for || |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 4510 | Kind == OMPD_target_parallel_for || |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 4511 | Kind == OMPD_teams_distribute_parallel_for || |
| 4512 | Kind == OMPD_target_teams_distribute_parallel_for); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4513 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4514 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4515 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4516 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 4517 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 4518 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 4519 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 4520 | auto OrigVarIt = C.varlist_begin(); |
| 4521 | auto InitIt = C.inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4522 | for (const Expr *PvtVarIt : C.private_copies()) { |
| 4523 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 4524 | const auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 4525 | const auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4526 | |
| 4527 | // In order to identify the right initializer we need to match the |
| 4528 | // declaration used by the mapping logic. In some cases we may get |
| 4529 | // OMPCapturedExprDecl that refers to the original declaration. |
| 4530 | const ValueDecl *MatchingVD = OrigVD; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4531 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4532 | // OMPCapturedExprDecl are used to privative fields of the current |
| 4533 | // structure. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4534 | const auto *ME = cast<MemberExpr>(OED->getInit()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4535 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 4536 | "Base should be the current struct!"); |
| 4537 | MatchingVD = ME->getMemberDecl(); |
| 4538 | } |
| 4539 | |
| 4540 | // If we don't have information about the current list item, move on to |
| 4541 | // the next one. |
| 4542 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 4543 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 4544 | continue; |
| 4545 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4546 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, OrigVD, |
| 4547 | InitAddrIt, InitVD, |
| 4548 | PvtVD]() { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4549 | // Initialize the temporary initialization variable with the address we |
| 4550 | // get from the runtime library. We have to cast the source address |
| 4551 | // because it is always a void *. References are materialized in the |
| 4552 | // privatization scope, so the initialization here disregards the fact |
| 4553 | // the original variable is a reference. |
| 4554 | QualType AddrQTy = |
| 4555 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 4556 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 4557 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 4558 | setAddrOfLocalVar(InitVD, InitAddr); |
| 4559 | |
| 4560 | // Emit private declaration, it will be initialized by the value we |
| 4561 | // declaration we just added to the local declarations map. |
| 4562 | EmitDecl(*PvtVD); |
| 4563 | |
| 4564 | // The initialization variables reached its purpose in the emission |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4565 | // of the previous declaration, so we don't need it anymore. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4566 | LocalDeclMap.erase(InitVD); |
| 4567 | |
| 4568 | // Return the address of the private variable. |
| 4569 | return GetAddrOfLocalVar(PvtVD); |
| 4570 | }); |
| 4571 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 4572 | // Silence the warning about unused variable. |
| 4573 | (void)IsRegistered; |
| 4574 | |
| 4575 | ++OrigVarIt; |
| 4576 | ++InitIt; |
| 4577 | } |
| 4578 | } |
| 4579 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4580 | // Generate the instructions for '#pragma omp target data' directive. |
| 4581 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 4582 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4583 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 4584 | |
| 4585 | // Create a pre/post action to signal the privatization of the device pointer. |
| 4586 | // This action can be replaced by the OpenMP runtime code generation to |
| 4587 | // deactivate privatization. |
| 4588 | bool PrivatizeDevicePointers = false; |
| 4589 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 4590 | bool &PrivatizeDevicePointers; |
| 4591 | |
| 4592 | public: |
| 4593 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 4594 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 4595 | void Enter(CodeGenFunction &CGF) override { |
| 4596 | PrivatizeDevicePointers = true; |
| 4597 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4598 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4599 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 4600 | |
| 4601 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4602 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4603 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4604 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4605 | }; |
| 4606 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4607 | // Codegen that selects whether to generate the privatization code or not. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4608 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 4609 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 4610 | PrePostActionTy &Action) { |
| 4611 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 4612 | PrivatizeDevicePointers = false; |
| 4613 | |
| 4614 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 4615 | // needed. |
| 4616 | Action.Enter(CGF); |
| 4617 | |
| 4618 | if (PrivatizeDevicePointers) { |
| 4619 | OMPPrivateScope PrivateScope(CGF); |
| 4620 | // Emit all instances of the use_device_ptr clause. |
| 4621 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 4622 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 4623 | Info.CaptureDeviceAddrMap); |
| 4624 | (void)PrivateScope.Privatize(); |
| 4625 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4626 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4627 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4628 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4629 | }; |
| 4630 | |
| 4631 | // Forward the provided action to the privatization codegen. |
| 4632 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 4633 | PrivRCG.setAction(Action); |
| 4634 | |
| 4635 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 4636 | // we don't use an inline scope as changes in the references inside the |
| 4637 | // region are expected to be visible outside, so we do not privative them. |
| 4638 | OMPLexicalScope Scope(CGF, S); |
| 4639 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 4640 | PrivRCG); |
| 4641 | }; |
| 4642 | |
| 4643 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4644 | |
| 4645 | // If we don't have target devices, don't bother emitting the data mapping |
| 4646 | // code. |
| 4647 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4648 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4649 | return; |
| 4650 | } |
| 4651 | |
| 4652 | // Check if we have any if clause associated with the directive. |
| 4653 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4654 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4655 | IfCond = C->getCondition(); |
| 4656 | |
| 4657 | // Check if we have any device clause associated with the directive. |
| 4658 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4659 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4660 | Device = C->getDevice(); |
| 4661 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4662 | // Set the action to signal privatization of device pointers. |
| 4663 | RCG.setAction(PrivAction); |
| 4664 | |
| 4665 | // Emit region code. |
| 4666 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 4667 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4668 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4669 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4670 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 4671 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4672 | // If we don't have target devices, don't bother emitting the data mapping |
| 4673 | // code. |
| 4674 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4675 | return; |
| 4676 | |
| 4677 | // Check if we have any if clause associated with the directive. |
| 4678 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4679 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4680 | IfCond = C->getCondition(); |
| 4681 | |
| 4682 | // Check if we have any device clause associated with the directive. |
| 4683 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4684 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4685 | Device = C->getDevice(); |
| 4686 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4687 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4688 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4691 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 4692 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4693 | // If we don't have target devices, don't bother emitting the data mapping |
| 4694 | // code. |
| 4695 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4696 | return; |
| 4697 | |
| 4698 | // Check if we have any if clause associated with the directive. |
| 4699 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4700 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4701 | IfCond = C->getCondition(); |
| 4702 | |
| 4703 | // Check if we have any device clause associated with the directive. |
| 4704 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4705 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4706 | Device = C->getDevice(); |
| 4707 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4708 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4709 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4712 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 4713 | const OMPTargetParallelDirective &S, |
| 4714 | PrePostActionTy &Action) { |
| 4715 | // Get the captured statement associated with the 'parallel' region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4716 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4717 | Action.Enter(CGF); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4718 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4719 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4720 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4721 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4722 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4723 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4724 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4725 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4726 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4727 | // TODO: Add support for clauses. |
| 4728 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4729 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4730 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4731 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, |
| 4732 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4733 | emitPostUpdateForReductionClause(CGF, S, |
| 4734 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4735 | } |
| 4736 | |
| 4737 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 4738 | CodeGenModule &CGM, StringRef ParentName, |
| 4739 | const OMPTargetParallelDirective &S) { |
| 4740 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4741 | emitTargetParallelRegion(CGF, S, Action); |
| 4742 | }; |
| 4743 | llvm::Function *Fn; |
| 4744 | llvm::Constant *Addr; |
| 4745 | // Emit target region as a standalone region. |
| 4746 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4747 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4748 | assert(Fn && Addr && "Target device function emission failed."); |
| 4749 | } |
| 4750 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4751 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 4752 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4753 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4754 | emitTargetParallelRegion(CGF, S, Action); |
| 4755 | }; |
| 4756 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4759 | static void emitTargetParallelForRegion(CodeGenFunction &CGF, |
| 4760 | const OMPTargetParallelForDirective &S, |
| 4761 | PrePostActionTy &Action) { |
| 4762 | Action.Enter(CGF); |
| 4763 | // Emit directive as a combined directive that consists of two implicit |
| 4764 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4765 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4766 | Action.Enter(CGF); |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4767 | CodeGenFunction::OMPCancelStackRAII CancelRegion( |
| 4768 | CGF, OMPD_target_parallel_for, S.hasCancel()); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4769 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4770 | emitDispatchForLoopBounds); |
| 4771 | }; |
| 4772 | emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, |
| 4773 | emitEmptyBoundParameters); |
| 4774 | } |
| 4775 | |
| 4776 | void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| 4777 | CodeGenModule &CGM, StringRef ParentName, |
| 4778 | const OMPTargetParallelForDirective &S) { |
| 4779 | // Emit SPMD target parallel for region as a standalone region. |
| 4780 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4781 | emitTargetParallelForRegion(CGF, S, Action); |
| 4782 | }; |
| 4783 | llvm::Function *Fn; |
| 4784 | llvm::Constant *Addr; |
| 4785 | // Emit target region as a standalone region. |
| 4786 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4787 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4788 | assert(Fn && Addr && "Target device function emission failed."); |
| 4789 | } |
| 4790 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4791 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 4792 | const OMPTargetParallelForDirective &S) { |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4793 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4794 | emitTargetParallelForRegion(CGF, S, Action); |
| 4795 | }; |
| 4796 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4799 | static void |
| 4800 | emitTargetParallelForSimdRegion(CodeGenFunction &CGF, |
| 4801 | const OMPTargetParallelForSimdDirective &S, |
| 4802 | PrePostActionTy &Action) { |
| 4803 | Action.Enter(CGF); |
| 4804 | // Emit directive as a combined directive that consists of two implicit |
| 4805 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4806 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4807 | Action.Enter(CGF); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4808 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4809 | emitDispatchForLoopBounds); |
| 4810 | }; |
| 4811 | emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, |
| 4812 | emitEmptyBoundParameters); |
| 4813 | } |
| 4814 | |
| 4815 | void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| 4816 | CodeGenModule &CGM, StringRef ParentName, |
| 4817 | const OMPTargetParallelForSimdDirective &S) { |
| 4818 | // Emit SPMD target parallel for region as a standalone region. |
| 4819 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4820 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4821 | }; |
| 4822 | llvm::Function *Fn; |
| 4823 | llvm::Constant *Addr; |
| 4824 | // Emit target region as a standalone region. |
| 4825 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4826 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4827 | assert(Fn && Addr && "Target device function emission failed."); |
| 4828 | } |
| 4829 | |
| 4830 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 4831 | const OMPTargetParallelForSimdDirective &S) { |
| 4832 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4833 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4834 | }; |
| 4835 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4836 | } |
| 4837 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4838 | /// Emit a helper variable and return corresponding lvalue. |
| 4839 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 4840 | const ImplicitParamDecl *PVD, |
| 4841 | CodeGenFunction::OMPPrivateScope &Privates) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4842 | const auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 4843 | Privates.addPrivate(VDecl, |
| 4844 | [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); }); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4845 | } |
| 4846 | |
| 4847 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 4848 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 4849 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4850 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4851 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 4852 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4853 | const Expr *IfCond = nullptr; |
| 4854 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4855 | if (C->getNameModifier() == OMPD_unknown || |
| 4856 | C->getNameModifier() == OMPD_taskloop) { |
| 4857 | IfCond = C->getCondition(); |
| 4858 | break; |
| 4859 | } |
| 4860 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4861 | |
| 4862 | OMPTaskDataTy Data; |
| 4863 | // Check if taskloop must be emitted without taskgroup. |
| 4864 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4865 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4866 | Data.Tied = true; |
| 4867 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4868 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 4869 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4870 | Data.Schedule.setInt(/*IntVal=*/false); |
| 4871 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4872 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 4873 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4874 | Data.Schedule.setInt(/*IntVal=*/true); |
| 4875 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4876 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4877 | |
| 4878 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4879 | // if (PreCond) { |
| 4880 | // for (IV in 0..LastIteration) BODY; |
| 4881 | // <Final counter/linear vars updates>; |
| 4882 | // } |
| 4883 | // |
| 4884 | |
| 4885 | // Emit: if (PreCond) - begin. |
| 4886 | // If the condition constant folds and can be elided, avoid emitting the |
| 4887 | // whole loop. |
| 4888 | bool CondConstant; |
| 4889 | llvm::BasicBlock *ContBlock = nullptr; |
| 4890 | OMPLoopScope PreInitScope(CGF, S); |
| 4891 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 4892 | if (!CondConstant) |
| 4893 | return; |
| 4894 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4895 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4896 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 4897 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 4898 | CGF.getProfileCount(&S)); |
| 4899 | CGF.EmitBlock(ThenBlock); |
| 4900 | CGF.incrementProfileCounter(&S); |
| 4901 | } |
| 4902 | |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4903 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 4904 | CGF.EmitOMPSimdInit(S); |
| 4905 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4906 | OMPPrivateScope LoopScope(CGF); |
| 4907 | // Emit helper vars inits. |
| 4908 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 4909 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 4910 | auto *LBP = std::next(I, LowerBound); |
| 4911 | auto *UBP = std::next(I, UpperBound); |
| 4912 | auto *STP = std::next(I, Stride); |
| 4913 | auto *LIP = std::next(I, LastIter); |
| 4914 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 4915 | LoopScope); |
| 4916 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 4917 | LoopScope); |
| 4918 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 4919 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 4920 | LoopScope); |
| 4921 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4922 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4923 | (void)LoopScope.Privatize(); |
| 4924 | // Emit the loop iteration variable. |
| 4925 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4926 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4927 | CGF.EmitVarDecl(*IVDecl); |
| 4928 | CGF.EmitIgnoredExpr(S.getInit()); |
| 4929 | |
| 4930 | // Emit the iterations count variable. |
| 4931 | // If it is not a variable, Sema decided to calculate iterations count on |
| 4932 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4933 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4934 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 4935 | // Emit calculation of the iterations count. |
| 4936 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 4937 | } |
| 4938 | |
| 4939 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 4940 | S.getInc(), |
| 4941 | [&S](CodeGenFunction &CGF) { |
| 4942 | CGF.EmitOMPLoopBody(S, JumpDest()); |
| 4943 | CGF.EmitStopPoint(&S); |
| 4944 | }, |
| 4945 | [](CodeGenFunction &) {}); |
| 4946 | // Emit: if (PreCond) - end. |
| 4947 | if (ContBlock) { |
| 4948 | CGF.EmitBranch(ContBlock); |
| 4949 | CGF.EmitBlock(ContBlock, true); |
| 4950 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4951 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 4952 | if (HasLastprivateClause) { |
| 4953 | CGF.EmitOMPLastprivateClauseFinal( |
| 4954 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 4955 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 4956 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4957 | (*LIP)->getType(), S.getBeginLoc()))); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4958 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4959 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4960 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
| 4961 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
| 4962 | const OMPTaskDataTy &Data) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4963 | auto &&CodeGen = [&S, OutlinedFn, SharedsTy, CapturedStruct, IfCond, |
| 4964 | &Data](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4965 | OMPLoopScope PreInitScope(CGF, S); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4966 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getBeginLoc(), S, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4967 | OutlinedFn, SharedsTy, |
| 4968 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4969 | }; |
| 4970 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 4971 | CodeGen); |
| 4972 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4973 | if (Data.Nogroup) { |
| 4974 | EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); |
| 4975 | } else { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4976 | CGM.getOpenMPRuntime().emitTaskgroupRegion( |
| 4977 | *this, |
| 4978 | [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, |
| 4979 | PrePostActionTy &Action) { |
| 4980 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4981 | CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, |
| 4982 | Data); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4983 | }, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4984 | S.getBeginLoc()); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4985 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4988 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4989 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4990 | } |
| 4991 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4992 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 4993 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4994 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4995 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4996 | |
| 4997 | // Generate the instructions for '#pragma omp target update' directive. |
| 4998 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 4999 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5000 | // If we don't have target devices, don't bother emitting the data mapping |
| 5001 | // code. |
| 5002 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 5003 | return; |
| 5004 | |
| 5005 | // Check if we have any if clause associated with the directive. |
| 5006 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5007 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5008 | IfCond = C->getCondition(); |
| 5009 | |
| 5010 | // Check if we have any device clause associated with the directive. |
| 5011 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5012 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5013 | Device = C->getDevice(); |
| 5014 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5015 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5016 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5017 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5018 | |
| 5019 | void CodeGenFunction::EmitSimpleOMPExecutableDirective( |
| 5020 | const OMPExecutableDirective &D) { |
| 5021 | if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) |
| 5022 | return; |
| 5023 | auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5024 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 5025 | emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); |
| 5026 | } else { |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5027 | OMPPrivateScope LoopGlobals(CGF); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5028 | if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5029 | for (const Expr *E : LD->counters()) { |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5030 | const auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 5031 | if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) { |
| 5032 | LValue GlobLVal = CGF.EmitLValue(E); |
| 5033 | LoopGlobals.addPrivate( |
| 5034 | VD, [&GlobLVal]() { return GlobLVal.getAddress(); }); |
| 5035 | } |
Bjorn Pettersson | 6c2d83b | 2018-10-30 08:49:26 +0000 | [diff] [blame] | 5036 | if (isa<OMPCapturedExprDecl>(VD)) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5037 | // Emit only those that were not explicitly referenced in clauses. |
| 5038 | if (!CGF.LocalDeclMap.count(VD)) |
| 5039 | CGF.EmitVarDecl(*VD); |
| 5040 | } |
| 5041 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5042 | for (const auto *C : D.getClausesOfKind<OMPOrderedClause>()) { |
| 5043 | if (!C->getNumForLoops()) |
| 5044 | continue; |
| 5045 | for (unsigned I = LD->getCollapsedNumber(), |
| 5046 | E = C->getLoopNumIterations().size(); |
| 5047 | I < E; ++I) { |
| 5048 | if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 5049 | cast<DeclRefExpr>(C->getLoopCounter(I))->getDecl())) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5050 | // Emit only those that were not explicitly referenced in clauses. |
| 5051 | if (!CGF.LocalDeclMap.count(VD)) |
| 5052 | CGF.EmitVarDecl(*VD); |
| 5053 | } |
| 5054 | } |
| 5055 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5056 | } |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5057 | LoopGlobals.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5058 | CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5059 | } |
| 5060 | }; |
| 5061 | OMPSimdLexicalScope Scope(*this, D); |
| 5062 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5063 | *this, |
| 5064 | isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd |
| 5065 | : D.getDirectiveKind(), |
| 5066 | CodeGen); |
| 5067 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5068 | |