Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This contains code to emit OpenMP nodes as LLVM code. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 13 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 14 | #include "CGOpenMPRuntime.h" |
| 15 | #include "CodeGenFunction.h" |
| 16 | #include "CodeGenModule.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 17 | #include "TargetInfo.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 18 | #include "clang/AST/Stmt.h" |
| 19 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | using namespace CodeGen; |
| 23 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | /// Lexical scope for OpenMP executable constructs, that handles correct codegen |
| 26 | /// for captured expressions. |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 27 | class OMPLexicalScope : public CodeGenFunction::LexicalScope { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 28 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 29 | for (const auto *C : S.clauses()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 30 | if (const auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 31 | if (const auto *PreInit = |
| 32 | cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 33 | for (const auto *I : PreInit->decls()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 34 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 35 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 36 | } else { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 37 | CodeGenFunction::AutoVarEmission Emission = |
| 38 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 39 | CGF.EmitAutoVarCleanups(Emission); |
| 40 | } |
| 41 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 46 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 47 | |
| 48 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 49 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 50 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 51 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl)); |
| 52 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 53 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 54 | public: |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 55 | OMPLexicalScope( |
| 56 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 57 | const llvm::Optional<OpenMPDirectiveKind> CapturedRegion = llvm::None, |
| 58 | const bool EmitPreInitStmt = true) |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 59 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 60 | InlinedShareds(CGF) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 61 | if (EmitPreInitStmt) |
| 62 | emitPreInitStmt(CGF, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 63 | if (!CapturedRegion.hasValue()) |
| 64 | return; |
| 65 | assert(S.hasAssociatedStmt() && |
| 66 | "Expected associated statement for inlined directive."); |
| 67 | const CapturedStmt *CS = S.getCapturedStmt(*CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 68 | for (const auto &C : CS->captures()) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 69 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 70 | auto *VD = C.getCapturedVar(); |
| 71 | assert(VD == VD->getCanonicalDecl() && |
| 72 | "Canonical decl must be captured."); |
| 73 | DeclRefExpr DRE( |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 74 | CGF.getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 75 | isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo && |
| 76 | InlinedShareds.isGlobalVarCaptured(VD)), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 77 | VD->getType().getNonReferenceType(), VK_LValue, C.getLocation()); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 78 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 79 | return CGF.EmitLValue(&DRE).getAddress(); |
| 80 | }); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 83 | (void)InlinedShareds.Privatize(); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 84 | } |
| 85 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 86 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 87 | /// Lexical scope for OpenMP parallel construct, that handles correct codegen |
| 88 | /// for captured expressions. |
| 89 | class OMPParallelScope final : public OMPLexicalScope { |
| 90 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 91 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 92 | return !(isOpenMPTargetExecutionDirective(Kind) || |
| 93 | isOpenMPLoopBoundSharingDirective(Kind)) && |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 94 | isOpenMPParallelDirective(Kind); |
| 95 | } |
| 96 | |
| 97 | public: |
| 98 | OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 99 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 100 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 103 | /// Lexical scope for OpenMP teams construct, that handles correct codegen |
| 104 | /// for captured expressions. |
| 105 | class OMPTeamsScope final : public OMPLexicalScope { |
| 106 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 107 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
| 108 | return !isOpenMPTargetExecutionDirective(Kind) && |
| 109 | isOpenMPTeamsDirective(Kind); |
| 110 | } |
| 111 | |
| 112 | public: |
| 113 | OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 114 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 115 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 118 | /// Private scope for OpenMP loop-based directives, that supports capturing |
| 119 | /// of used expression from loop statement. |
| 120 | class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { |
| 121 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 122 | CodeGenFunction::OMPMapVars PreCondVars; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 123 | for (const auto *E : S.counters()) { |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 124 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 125 | (void)PreCondVars.setVarAddr( |
| 126 | CGF, VD, CGF.CreateMemTemp(VD->getType().getNonReferenceType())); |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 127 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 128 | (void)PreCondVars.apply(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 129 | if (const auto *PreInits = cast_or_null<DeclStmt>(S.getPreInits())) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 130 | for (const auto *I : PreInits->decls()) |
| 131 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 132 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 133 | PreCondVars.restore(CGF); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | public: |
| 137 | OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) |
| 138 | : CodeGenFunction::RunCleanupsScope(CGF) { |
| 139 | emitPreInitStmt(CGF, S); |
| 140 | } |
| 141 | }; |
| 142 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 143 | class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope { |
| 144 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 145 | |
| 146 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 147 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 148 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 149 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && |
| 150 | cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); |
| 151 | } |
| 152 | |
| 153 | public: |
| 154 | OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 155 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 156 | InlinedShareds(CGF) { |
| 157 | for (const auto *C : S.clauses()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 158 | if (const auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 159 | if (const auto *PreInit = |
| 160 | cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 161 | for (const auto *I : PreInit->decls()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 162 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 163 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 164 | } else { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 165 | CodeGenFunction::AutoVarEmission Emission = |
| 166 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 167 | CGF.EmitAutoVarCleanups(Emission); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) { |
| 172 | for (const Expr *E : UDP->varlists()) { |
| 173 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 174 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D)) |
| 175 | CGF.EmitVarDecl(*OED); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 180 | CGF.EmitOMPPrivateClause(S, InlinedShareds); |
| 181 | if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) { |
| 182 | if (const Expr *E = TG->getReductionRef()) |
| 183 | CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())); |
| 184 | } |
| 185 | const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt()); |
| 186 | while (CS) { |
| 187 | for (auto &C : CS->captures()) { |
| 188 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 189 | auto *VD = C.getCapturedVar(); |
| 190 | assert(VD == VD->getCanonicalDecl() && |
| 191 | "Canonical decl must be captured."); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 192 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 193 | isCapturedVar(CGF, VD) || |
| 194 | (CGF.CapturedStmtInfo && |
| 195 | InlinedShareds.isGlobalVarCaptured(VD)), |
| 196 | VD->getType().getNonReferenceType(), VK_LValue, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 197 | C.getLocation()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 198 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 199 | return CGF.EmitLValue(&DRE).getAddress(); |
| 200 | }); |
| 201 | } |
| 202 | } |
| 203 | CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt()); |
| 204 | } |
| 205 | (void)InlinedShareds.Privatize(); |
| 206 | } |
| 207 | }; |
| 208 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 209 | } // namespace |
| 210 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 211 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 212 | const OMPExecutableDirective &S, |
| 213 | const RegionCodeGenTy &CodeGen); |
| 214 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 215 | LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 216 | if (const auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) { |
| 217 | if (const auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 218 | OrigVD = OrigVD->getCanonicalDecl(); |
| 219 | bool IsCaptured = |
| 220 | LambdaCaptureFields.lookup(OrigVD) || |
| 221 | (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) || |
| 222 | (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 223 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), IsCaptured, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 224 | OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc()); |
| 225 | return EmitLValue(&DRE); |
| 226 | } |
| 227 | } |
| 228 | return EmitLValue(E); |
| 229 | } |
| 230 | |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 231 | llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 232 | ASTContext &C = getContext(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 233 | llvm::Value *Size = nullptr; |
| 234 | auto SizeInChars = C.getTypeSizeInChars(Ty); |
| 235 | if (SizeInChars.isZero()) { |
| 236 | // getTypeSizeInChars() returns 0 for a VLA. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 237 | while (const VariableArrayType *VAT = C.getAsVariableArrayType(Ty)) { |
| 238 | VlaSizePair VlaSize = getVLASize(VAT); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 239 | Ty = VlaSize.Type; |
| 240 | Size = Size ? Builder.CreateNUWMul(Size, VlaSize.NumElts) |
| 241 | : VlaSize.NumElts; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 242 | } |
| 243 | SizeInChars = C.getTypeSizeInChars(Ty); |
| 244 | if (SizeInChars.isZero()) |
| 245 | return llvm::ConstantInt::get(SizeTy, /*V=*/0); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 246 | return Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); |
| 247 | } |
| 248 | return CGM.getSize(SizeInChars); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 251 | void CodeGenFunction::GenerateOpenMPCapturedVars( |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 252 | const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 253 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 254 | auto CurField = RD->field_begin(); |
| 255 | auto CurCap = S.captures().begin(); |
| 256 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 257 | E = S.capture_init_end(); |
| 258 | I != E; ++I, ++CurField, ++CurCap) { |
| 259 | if (CurField->hasCapturedVLAType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 260 | const VariableArrayType *VAT = CurField->getCapturedVLAType(); |
| 261 | llvm::Value *Val = VLASizeMap[VAT->getSizeExpr()]; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 262 | CapturedVars.push_back(Val); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 263 | } else if (CurCap->capturesThis()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 264 | CapturedVars.push_back(CXXThisValue); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 265 | } else if (CurCap->capturesVariableByCopy()) { |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 266 | llvm::Value *CV = EmitLoadOfScalar(EmitLValue(*I), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 267 | |
| 268 | // If the field is not a pointer, we need to save the actual value |
| 269 | // and load it as a void pointer. |
| 270 | if (!CurField->getType()->isAnyPointerType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 271 | ASTContext &Ctx = getContext(); |
| 272 | Address DstAddr = CreateMemTemp( |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 273 | Ctx.getUIntPtrType(), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 274 | Twine(CurCap->getCapturedVar()->getName(), ".casted")); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 275 | LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 276 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 277 | llvm::Value *SrcAddrVal = EmitScalarConversion( |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 278 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 279 | Ctx.getPointerType(CurField->getType()), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 280 | LValue SrcLV = |
| 281 | MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); |
| 282 | |
| 283 | // Store the value using the source type pointer. |
| 284 | EmitStoreThroughLValue(RValue::get(CV), SrcLV); |
| 285 | |
| 286 | // Load the value using the destination type pointer. |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 287 | CV = EmitLoadOfScalar(DstLV, CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 288 | } |
| 289 | CapturedVars.push_back(CV); |
| 290 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 291 | assert(CurCap->capturesVariable() && "Expected capture by reference."); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 292 | CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 293 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 297 | static Address castValueFromUintptr(CodeGenFunction &CGF, SourceLocation Loc, |
| 298 | QualType DstType, StringRef Name, |
| 299 | LValue AddrLV, |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 300 | bool isReferenceType = false) { |
| 301 | ASTContext &Ctx = CGF.getContext(); |
| 302 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 303 | llvm::Value *CastedPtr = CGF.EmitScalarConversion( |
| 304 | AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(), |
| 305 | Ctx.getPointerType(DstType), Loc); |
| 306 | Address TmpAddr = |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 307 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
| 308 | .getAddress(); |
| 309 | |
| 310 | // If we are dealing with references we need to return the address of the |
| 311 | // reference instead of the reference of the value. |
| 312 | if (isReferenceType) { |
| 313 | QualType RefType = Ctx.getLValueReferenceType(DstType); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 314 | llvm::Value *RefVal = TmpAddr.getPointer(); |
| 315 | TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name, ".ref")); |
| 316 | LValue TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType); |
| 317 | CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit=*/true); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | return TmpAddr; |
| 321 | } |
| 322 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 323 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 324 | if (T->isLValueReferenceType()) |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 325 | return C.getLValueReferenceType( |
| 326 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 327 | /*SpelledAsLValue=*/false); |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 328 | if (T->isPointerType()) |
| 329 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 330 | if (const ArrayType *A = T->getAsArrayTypeUnsafe()) { |
| 331 | if (const auto *VLA = dyn_cast<VariableArrayType>(A)) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 332 | return getCanonicalParamType(C, VLA->getElementType()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 333 | if (!A->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 334 | return C.getCanonicalType(T); |
| 335 | } |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 336 | return C.getCanonicalParamType(T); |
| 337 | } |
| 338 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 339 | namespace { |
| 340 | /// Contains required data for proper outlined function codegen. |
| 341 | struct FunctionOptions { |
| 342 | /// Captured statement for which the function is generated. |
| 343 | const CapturedStmt *S = nullptr; |
| 344 | /// true if cast to/from UIntPtr is required for variables captured by |
| 345 | /// value. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 346 | const bool UIntPtrCastRequired = true; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 347 | /// 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] | 348 | /// sizes. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 349 | const bool RegisterCastedArgsOnly = false; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 350 | /// Name of the generated function. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 351 | const StringRef FunctionName; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 352 | explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, |
| 353 | bool RegisterCastedArgsOnly, |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 354 | StringRef FunctionName) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 355 | : S(S), UIntPtrCastRequired(UIntPtrCastRequired), |
| 356 | RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 357 | FunctionName(FunctionName) {} |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 358 | }; |
| 359 | } |
| 360 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 361 | static llvm::Function *emitOutlinedFunctionPrologue( |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 362 | CodeGenFunction &CGF, FunctionArgList &Args, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 363 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 364 | &LocalAddrs, |
| 365 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> |
| 366 | &VLASizes, |
| 367 | llvm::Value *&CXXThisValue, const FunctionOptions &FO) { |
| 368 | const CapturedDecl *CD = FO.S->getCapturedDecl(); |
| 369 | const RecordDecl *RD = FO.S->getCapturedRecordDecl(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 370 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 371 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 372 | CXXThisValue = nullptr; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 373 | // Build the argument list. |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 374 | CodeGenModule &CGM = CGF.CGM; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 375 | ASTContext &Ctx = CGM.getContext(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 376 | FunctionArgList TargetArgs; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 377 | Args.append(CD->param_begin(), |
| 378 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 379 | TargetArgs.append( |
| 380 | CD->param_begin(), |
| 381 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 382 | auto I = FO.S->captures().begin(); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 383 | FunctionDecl *DebugFunctionDecl = nullptr; |
| 384 | if (!FO.UIntPtrCastRequired) { |
| 385 | FunctionProtoType::ExtProtoInfo EPI; |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 386 | QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 387 | DebugFunctionDecl = FunctionDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 388 | Ctx, Ctx.getTranslationUnitDecl(), FO.S->getBeginLoc(), |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 389 | SourceLocation(), DeclarationName(), FunctionTy, |
| 390 | Ctx.getTrivialTypeSourceInfo(FunctionTy), SC_Static, |
| 391 | /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 392 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 393 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 394 | QualType ArgType = FD->getType(); |
| 395 | IdentifierInfo *II = nullptr; |
| 396 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 397 | |
| 398 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 399 | // function argument type should be uintptr and the value properly casted to |
| 400 | // uintptr. This is necessary given that the runtime library is only able to |
| 401 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 402 | // outlined function. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 403 | if (FO.UIntPtrCastRequired && |
| 404 | ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
| 405 | I->capturesVariableArrayType())) |
| 406 | ArgType = Ctx.getUIntPtrType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 407 | |
| 408 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 409 | CapVar = I->getCapturedVar(); |
| 410 | II = CapVar->getIdentifier(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 411 | } else if (I->capturesThis()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 412 | II = &Ctx.Idents.get("this"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 413 | } else { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 414 | assert(I->capturesVariableArrayType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 415 | II = &Ctx.Idents.get("vla"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 416 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 417 | if (ArgType->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 418 | ArgType = getCanonicalParamType(Ctx, ArgType); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 419 | VarDecl *Arg; |
| 420 | if (DebugFunctionDecl && (CapVar || I->capturesThis())) { |
| 421 | Arg = ParmVarDecl::Create( |
| 422 | Ctx, DebugFunctionDecl, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 423 | CapVar ? CapVar->getBeginLoc() : FD->getBeginLoc(), |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 424 | CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, |
| 425 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
| 426 | } else { |
| 427 | Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), |
| 428 | II, ArgType, ImplicitParamDecl::Other); |
| 429 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 430 | Args.emplace_back(Arg); |
| 431 | // Do not cast arguments if we emit function with non-original types. |
| 432 | TargetArgs.emplace_back( |
| 433 | FO.UIntPtrCastRequired |
| 434 | ? Arg |
| 435 | : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 436 | ++I; |
| 437 | } |
| 438 | Args.append( |
| 439 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 440 | CD->param_end()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 441 | TargetArgs.append( |
| 442 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 443 | CD->param_end()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 444 | |
| 445 | // Create the function declaration. |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 446 | const CGFunctionInfo &FuncInfo = |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 447 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 448 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 449 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 450 | auto *F = |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 451 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 452 | FO.FunctionName, &CGM.getModule()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 453 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 454 | if (CD->isNothrow()) |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 455 | F->setDoesNotThrow(); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 456 | F->setDoesNotRecurse(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 457 | |
| 458 | // Generate the function. |
Alexey Bataev | 6e01dc1 | 2017-08-14 16:03:47 +0000 | [diff] [blame] | 459 | CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 460 | FO.S->getBeginLoc(), CD->getBody()->getBeginLoc()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 461 | unsigned Cnt = CD->getContextParamPosition(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 462 | I = FO.S->captures().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 463 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 464 | // Do not map arguments if we emit function with non-original types. |
| 465 | Address LocalAddr(Address::invalid()); |
| 466 | if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) { |
| 467 | LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt], |
| 468 | TargetArgs[Cnt]); |
| 469 | } else { |
| 470 | LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]); |
| 471 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 472 | // If we are capturing a pointer by copy we don't need to do anything, just |
| 473 | // use the value that we get from the arguments. |
| 474 | if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 475 | const VarDecl *CurVD = I->getCapturedVar(); |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 476 | // If the variable is a reference we need to materialize it here. |
| 477 | if (CurVD->getType()->isReferenceType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 478 | Address RefAddr = CGF.CreateMemTemp( |
| 479 | CurVD->getType(), CGM.getPointerAlign(), ".materialized_ref"); |
| 480 | CGF.EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr, |
| 481 | /*Volatile=*/false, CurVD->getType()); |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 482 | LocalAddr = RefAddr; |
| 483 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 484 | if (!FO.RegisterCastedArgsOnly) |
| 485 | LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}}); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 486 | ++Cnt; |
| 487 | ++I; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 488 | continue; |
| 489 | } |
| 490 | |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 491 | LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), |
| 492 | AlignmentSource::Decl); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 493 | if (FD->hasCapturedVLAType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 494 | if (FO.UIntPtrCastRequired) { |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 495 | ArgLVal = CGF.MakeAddrLValue( |
| 496 | castValueFromUintptr(CGF, I->getLocation(), FD->getType(), |
| 497 | Args[Cnt]->getName(), ArgLVal), |
| 498 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 499 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 500 | llvm::Value *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
| 501 | const VariableArrayType *VAT = FD->getCapturedVLAType(); |
| 502 | VLASizes.try_emplace(Args[Cnt], VAT->getSizeExpr(), ExprArg); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 503 | } else if (I->capturesVariable()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 504 | const VarDecl *Var = I->getCapturedVar(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 505 | QualType VarTy = Var->getType(); |
| 506 | Address ArgAddr = ArgLVal.getAddress(); |
| 507 | if (!VarTy->isReferenceType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 508 | if (ArgLVal.getType()->isLValueReferenceType()) { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 509 | ArgAddr = CGF.EmitLoadOfReference(ArgLVal); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 510 | } else if (!VarTy->isVariablyModifiedType() || |
| 511 | !VarTy->isPointerType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 512 | assert(ArgLVal.getType()->isPointerType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 513 | ArgAddr = CGF.EmitLoadOfPointer( |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 514 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
| 515 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 516 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 517 | if (!FO.RegisterCastedArgsOnly) { |
| 518 | LocalAddrs.insert( |
| 519 | {Args[Cnt], |
| 520 | {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); |
| 521 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 522 | } else if (I->capturesVariableByCopy()) { |
| 523 | assert(!FD->getType()->isAnyPointerType() && |
| 524 | "Not expecting a captured pointer."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 525 | const VarDecl *Var = I->getCapturedVar(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 526 | QualType VarTy = Var->getType(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 527 | LocalAddrs.insert( |
| 528 | {Args[Cnt], |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 529 | {Var, FO.UIntPtrCastRequired |
| 530 | ? castValueFromUintptr(CGF, I->getLocation(), |
| 531 | FD->getType(), Args[Cnt]->getName(), |
| 532 | ArgLVal, VarTy->isReferenceType()) |
| 533 | : ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 534 | } else { |
| 535 | // If 'this' is captured, load it into CXXThisValue. |
| 536 | assert(I->capturesThis()); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 537 | CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 538 | LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 539 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 540 | ++Cnt; |
| 541 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 544 | return F; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | llvm::Function * |
| 548 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { |
| 549 | assert( |
| 550 | CapturedStmtInfo && |
| 551 | "CapturedStmtInfo should be set when generating the captured function"); |
| 552 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 553 | // Build the argument list. |
| 554 | bool NeedWrapperFunction = |
| 555 | getDebugInfo() && |
| 556 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo; |
| 557 | FunctionArgList Args; |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 558 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 559 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 560 | SmallString<256> Buffer; |
| 561 | llvm::raw_svector_ostream Out(Buffer); |
| 562 | Out << CapturedStmtInfo->getHelperName(); |
| 563 | if (NeedWrapperFunction) |
| 564 | Out << "_debug__"; |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 565 | FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 566 | Out.str()); |
| 567 | llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, |
| 568 | VLASizes, CXXThisValue, FO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 569 | for (const auto &LocalAddrPair : LocalAddrs) { |
| 570 | if (LocalAddrPair.second.first) { |
| 571 | setAddrOfLocalVar(LocalAddrPair.second.first, |
| 572 | LocalAddrPair.second.second); |
| 573 | } |
| 574 | } |
| 575 | for (const auto &VLASizePair : VLASizes) |
| 576 | VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 577 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 578 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 579 | FinishFunction(CD->getBodyRBrace()); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 580 | if (!NeedWrapperFunction) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 581 | return F; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 582 | |
Alexey Bataev | efd884d | 2017-08-04 21:26:25 +0000 | [diff] [blame] | 583 | FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 584 | /*RegisterCastedArgsOnly=*/true, |
| 585 | CapturedStmtInfo->getHelperName()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 586 | CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 587 | WrapperCGF.CapturedStmtInfo = CapturedStmtInfo; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 588 | Args.clear(); |
| 589 | LocalAddrs.clear(); |
| 590 | VLASizes.clear(); |
| 591 | llvm::Function *WrapperF = |
| 592 | emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 593 | WrapperCGF.CXXThisValue, WrapperFO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 594 | llvm::SmallVector<llvm::Value *, 4> CallArgs; |
| 595 | for (const auto *Arg : Args) { |
| 596 | llvm::Value *CallArg; |
| 597 | auto I = LocalAddrs.find(Arg); |
| 598 | if (I != LocalAddrs.end()) { |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 599 | LValue LV = WrapperCGF.MakeAddrLValue( |
| 600 | I->second.second, |
| 601 | I->second.first ? I->second.first->getType() : Arg->getType(), |
| 602 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 603 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 604 | } else { |
| 605 | auto EI = VLASizes.find(Arg); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 606 | if (EI != VLASizes.end()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 607 | CallArg = EI->second.second; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 608 | } else { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 609 | LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 610 | Arg->getType(), |
| 611 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 612 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 615 | CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 616 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 617 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 618 | F, CallArgs); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 619 | WrapperCGF.FinishFunction(); |
| 620 | return WrapperF; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 623 | //===----------------------------------------------------------------------===// |
| 624 | // OpenMP Directive Emission |
| 625 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 626 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 627 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 628 | const llvm::function_ref<void(Address, Address)> CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 629 | // Perform element-by-element initialization. |
| 630 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 631 | |
| 632 | // Drill down to the base element type on both arrays. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 633 | const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
| 634 | llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 635 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 636 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 637 | llvm::Value *SrcBegin = SrcAddr.getPointer(); |
| 638 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 639 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 640 | llvm::Value *DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 641 | // The basic structure here is a while-do loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 642 | llvm::BasicBlock *BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 643 | llvm::BasicBlock *DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 644 | llvm::Value *IsEmpty = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 645 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 646 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 647 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 648 | // Enter the loop body, making that address the current address. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 649 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 650 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 651 | |
| 652 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 653 | |
| 654 | llvm::PHINode *SrcElementPHI = |
| 655 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 656 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 657 | Address SrcElementCurrent = |
| 658 | Address(SrcElementPHI, |
| 659 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 660 | |
| 661 | llvm::PHINode *DestElementPHI = |
| 662 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 663 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 664 | Address DestElementCurrent = |
| 665 | Address(DestElementPHI, |
| 666 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 667 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 668 | // Emit copy. |
| 669 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 670 | |
| 671 | // Shift the address forward by one element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 672 | llvm::Value *DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 673 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 674 | llvm::Value *SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 675 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 676 | // Check whether we've reached the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 677 | llvm::Value *Done = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 678 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 679 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 680 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 681 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 682 | |
| 683 | // Done. |
| 684 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 685 | } |
| 686 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 687 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 688 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 689 | const VarDecl *SrcVD, const Expr *Copy) { |
| 690 | if (OriginalType->isArrayType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 691 | const auto *BO = dyn_cast<BinaryOperator>(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 692 | if (BO && BO->getOpcode() == BO_Assign) { |
| 693 | // Perform simple memcpy for simple copying. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 694 | LValue Dest = MakeAddrLValue(DestAddr, OriginalType); |
| 695 | LValue Src = MakeAddrLValue(SrcAddr, OriginalType); |
| 696 | EmitAggregateAssign(Dest, Src, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 697 | } else { |
| 698 | // For arrays with complex element types perform element by element |
| 699 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 700 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 701 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 702 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 703 | // Working with the single array element, so have to remap |
| 704 | // destination and source variables to corresponding array |
| 705 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 706 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 707 | Remap.addPrivate(DestVD, [DestElement]() { return DestElement; }); |
| 708 | Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 709 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 710 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 711 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 712 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 713 | } else { |
| 714 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 715 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 716 | Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; }); |
| 717 | Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 718 | (void)Remap.Privatize(); |
| 719 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 720 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 721 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 724 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 725 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 726 | if (!HaveInsertPoint()) |
| 727 | return false; |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 728 | bool DeviceConstTarget = |
| 729 | getLangOpts().OpenMPIsDevice && |
| 730 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 731 | bool FirstprivateIsLastprivate = false; |
| 732 | llvm::DenseSet<const VarDecl *> Lastprivates; |
| 733 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 734 | for (const auto *D : C->varlists()) |
| 735 | Lastprivates.insert( |
| 736 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 737 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 738 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 739 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 740 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 741 | // Force emission of the firstprivate copy if the directive does not emit |
| 742 | // outlined function, like omp for, omp simd, omp distribute etc. |
| 743 | bool MustEmitFirstprivateCopy = |
| 744 | CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 745 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 746 | auto IRef = C->varlist_begin(); |
| 747 | auto InitsRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 748 | for (const Expr *IInit : C->private_copies()) { |
| 749 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 750 | bool ThisFirstprivateIsLastprivate = |
| 751 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 752 | const FieldDecl *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 753 | if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 754 | !FD->getType()->isReferenceType()) { |
| 755 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 756 | ++IRef; |
| 757 | ++InitsRef; |
| 758 | continue; |
| 759 | } |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 760 | // Do not emit copy for firstprivate constant variables in target regions, |
| 761 | // captured by reference. |
| 762 | if (DeviceConstTarget && OrigVD->getType().isConstant(getContext()) && |
| 763 | FD && FD->getType()->isReferenceType()) { |
| 764 | (void)CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(*this, |
| 765 | OrigVD); |
| 766 | ++IRef; |
| 767 | ++InitsRef; |
| 768 | continue; |
| 769 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 770 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 771 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 772 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 773 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 774 | const auto *VDInit = |
| 775 | cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 776 | bool IsRegistered; |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 777 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 778 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 779 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 780 | LValue OriginalLVal = EmitLValue(&DRE); |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 781 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 782 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 783 | // Emit VarDecl with copy init for arrays. |
| 784 | // Get the address of the original variable captured in current |
| 785 | // captured region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 786 | IsRegistered = PrivateScope.addPrivate( |
| 787 | OrigVD, [this, VD, Type, OriginalLVal, VDInit]() { |
| 788 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 789 | const Expr *Init = VD->getInit(); |
| 790 | if (!isa<CXXConstructExpr>(Init) || |
| 791 | isTrivialInitializer(Init)) { |
| 792 | // Perform simple memcpy. |
| 793 | LValue Dest = |
| 794 | MakeAddrLValue(Emission.getAllocatedAddress(), Type); |
| 795 | EmitAggregateAssign(Dest, OriginalLVal, Type); |
| 796 | } else { |
| 797 | EmitOMPAggregateAssign( |
| 798 | Emission.getAllocatedAddress(), OriginalLVal.getAddress(), |
| 799 | Type, |
| 800 | [this, VDInit, Init](Address DestElement, |
| 801 | Address SrcElement) { |
| 802 | // Clean up any temporaries needed by the |
| 803 | // initialization. |
| 804 | RunCleanupsScope InitScope(*this); |
| 805 | // Emit initialization for single element. |
| 806 | setAddrOfLocalVar(VDInit, SrcElement); |
| 807 | EmitAnyExprToMem(Init, DestElement, |
| 808 | Init->getType().getQualifiers(), |
| 809 | /*IsInitializer*/ false); |
| 810 | LocalDeclMap.erase(VDInit); |
| 811 | }); |
| 812 | } |
| 813 | EmitAutoVarCleanups(Emission); |
| 814 | return Emission.getAllocatedAddress(); |
| 815 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 816 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 817 | Address OriginalAddr = OriginalLVal.getAddress(); |
| 818 | IsRegistered = PrivateScope.addPrivate( |
| 819 | OrigVD, [this, VDInit, OriginalAddr, VD]() { |
| 820 | // Emit private VarDecl with copy init. |
| 821 | // Remap temp VDInit variable to the address of the original |
| 822 | // variable (for proper handling of captured global variables). |
| 823 | setAddrOfLocalVar(VDInit, OriginalAddr); |
| 824 | EmitDecl(*VD); |
| 825 | LocalDeclMap.erase(VDInit); |
| 826 | return GetAddrOfLocalVar(VD); |
| 827 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 828 | } |
| 829 | assert(IsRegistered && |
| 830 | "firstprivate var already registered as private"); |
| 831 | // Silence the warning about unused variable. |
| 832 | (void)IsRegistered; |
| 833 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 834 | ++IRef; |
| 835 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 838 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 841 | void CodeGenFunction::EmitOMPPrivateClause( |
| 842 | const OMPExecutableDirective &D, |
| 843 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 844 | if (!HaveInsertPoint()) |
| 845 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 846 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 847 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 848 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 849 | for (const Expr *IInit : C->private_copies()) { |
| 850 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 851 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 852 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 853 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
| 854 | // Emit private VarDecl with copy init. |
| 855 | EmitDecl(*VD); |
| 856 | return GetAddrOfLocalVar(VD); |
| 857 | }); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 858 | assert(IsRegistered && "private var already registered as private"); |
| 859 | // Silence the warning about unused variable. |
| 860 | (void)IsRegistered; |
| 861 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 862 | ++IRef; |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 867 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 868 | if (!HaveInsertPoint()) |
| 869 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 870 | // threadprivate_var1 = master_threadprivate_var1; |
| 871 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 872 | // ... |
| 873 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 874 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 875 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 876 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 877 | auto IRef = C->varlist_begin(); |
| 878 | auto ISrcRef = C->source_exprs().begin(); |
| 879 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 880 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 881 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 882 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 883 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 884 | // Get the address of the master variable. If we are emitting code with |
| 885 | // TLS support, the address is passed from the master as field in the |
| 886 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 887 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 888 | if (getLangOpts().OpenMPUseTLS && |
| 889 | getContext().getTargetInfo().isTLSSupported()) { |
| 890 | assert(CapturedStmtInfo->lookup(VD) && |
| 891 | "Copyin threadprivates should have been captured!"); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 892 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), true, |
| 893 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 894 | MasterAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 895 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 896 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 897 | MasterAddr = |
| 898 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 899 | : CGM.GetAddrOfGlobal(VD), |
| 900 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 901 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 902 | // Get the address of the threadprivate variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 903 | Address PrivateAddr = EmitLValue(*IRef).getAddress(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 904 | if (CopiedVars.size() == 1) { |
| 905 | // At first check if current thread is a master thread. If it is, no |
| 906 | // need to copy data. |
| 907 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 908 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 909 | Builder.CreateCondBr( |
| 910 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 911 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 912 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), |
| 913 | CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 914 | CopyBegin, CopyEnd); |
| 915 | EmitBlock(CopyBegin); |
| 916 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 917 | const auto *SrcVD = |
| 918 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 919 | const auto *DestVD = |
| 920 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 921 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 922 | } |
| 923 | ++IRef; |
| 924 | ++ISrcRef; |
| 925 | ++IDestRef; |
| 926 | } |
| 927 | } |
| 928 | if (CopyEnd) { |
| 929 | // Exit out of copying procedure for non-master thread. |
| 930 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 931 | return true; |
| 932 | } |
| 933 | return false; |
| 934 | } |
| 935 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 936 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 937 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 938 | if (!HaveInsertPoint()) |
| 939 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 940 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 941 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 942 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 943 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 944 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 945 | SIMDLCVs.insert( |
| 946 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 947 | } |
| 948 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 949 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 950 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 951 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 952 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 953 | !getLangOpts().OpenMPSimd) |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 954 | break; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 955 | auto IRef = C->varlist_begin(); |
| 956 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 957 | for (const Expr *IInit : C->private_copies()) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 958 | // Keep the address of the original variable for future update at the end |
| 959 | // of the loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 960 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 961 | // Taskloops do not require additional initialization, it is done in |
| 962 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 963 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 964 | const auto *DestVD = |
| 965 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 966 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 967 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
| 968 | /*RefersToEnclosingVariableOrCapture=*/ |
| 969 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 970 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 971 | return EmitLValue(&DRE).getAddress(); |
| 972 | }); |
| 973 | // Check if the variable is also a firstprivate: in this case IInit is |
| 974 | // not generated. Initialization of this variable will happen in codegen |
| 975 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 976 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 977 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 978 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 979 | // Emit private VarDecl with copy init. |
| 980 | EmitDecl(*VD); |
| 981 | return GetAddrOfLocalVar(VD); |
| 982 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 983 | assert(IsRegistered && |
| 984 | "lastprivate var already registered as private"); |
| 985 | (void)IsRegistered; |
| 986 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 987 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 988 | ++IRef; |
| 989 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | return HasAtLeastOneLastprivate; |
| 993 | } |
| 994 | |
| 995 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 996 | const OMPExecutableDirective &D, bool NoFinals, |
| 997 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 998 | if (!HaveInsertPoint()) |
| 999 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1000 | // Emit following code: |
| 1001 | // if (<IsLastIterCond>) { |
| 1002 | // orig_var1 = private_orig_var1; |
| 1003 | // ... |
| 1004 | // orig_varn = private_orig_varn; |
| 1005 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1006 | llvm::BasicBlock *ThenBB = nullptr; |
| 1007 | llvm::BasicBlock *DoneBB = nullptr; |
| 1008 | if (IsLastIterCond) { |
| 1009 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 1010 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 1011 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 1012 | EmitBlock(ThenBB); |
| 1013 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1014 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 1015 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1016 | if (const auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1017 | auto IC = LoopDirective->counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1018 | for (const Expr *F : LoopDirective->finals()) { |
| 1019 | const auto *D = |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1020 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 1021 | if (NoFinals) |
| 1022 | AlreadyEmittedVars.insert(D); |
| 1023 | else |
| 1024 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1025 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1028 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 1029 | auto IRef = C->varlist_begin(); |
| 1030 | auto ISrcRef = C->source_exprs().begin(); |
| 1031 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1032 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 1033 | const auto *PrivateVD = |
| 1034 | cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1035 | QualType Type = PrivateVD->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1036 | const auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1037 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 1038 | // If lastprivate variable is a loop control variable for loop-based |
| 1039 | // directive, update its value before copyin back to original |
| 1040 | // variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1041 | if (const Expr *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1042 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1043 | const auto *SrcVD = |
| 1044 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 1045 | const auto *DestVD = |
| 1046 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1047 | // Get the address of the original variable. |
| 1048 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 1049 | // Get the address of the private variable. |
| 1050 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1051 | if (const auto *RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1052 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1053 | Address(Builder.CreateLoad(PrivateAddr), |
| 1054 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1055 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1056 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1057 | ++IRef; |
| 1058 | ++ISrcRef; |
| 1059 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1060 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1061 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1062 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1063 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1064 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1065 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1068 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 1069 | const OMPExecutableDirective &D, |
| 1070 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1071 | if (!HaveInsertPoint()) |
| 1072 | return; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1073 | SmallVector<const Expr *, 4> Shareds; |
| 1074 | SmallVector<const Expr *, 4> Privates; |
| 1075 | SmallVector<const Expr *, 4> ReductionOps; |
| 1076 | SmallVector<const Expr *, 4> LHSs; |
| 1077 | SmallVector<const Expr *, 4> RHSs; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1078 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1079 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1080 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1081 | auto ILHS = C->lhs_exprs().begin(); |
| 1082 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1083 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1084 | Shareds.emplace_back(Ref); |
| 1085 | Privates.emplace_back(*IPriv); |
| 1086 | ReductionOps.emplace_back(*IRed); |
| 1087 | LHSs.emplace_back(*ILHS); |
| 1088 | RHSs.emplace_back(*IRHS); |
| 1089 | std::advance(IPriv, 1); |
| 1090 | std::advance(IRed, 1); |
| 1091 | std::advance(ILHS, 1); |
| 1092 | std::advance(IRHS, 1); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1093 | } |
| 1094 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1095 | ReductionCodeGen RedCG(Shareds, Privates, ReductionOps); |
| 1096 | unsigned Count = 0; |
| 1097 | auto ILHS = LHSs.begin(); |
| 1098 | auto IRHS = RHSs.begin(); |
| 1099 | auto IPriv = Privates.begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1100 | for (const Expr *IRef : Shareds) { |
| 1101 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1102 | // Emit private VarDecl with reduction init. |
| 1103 | RedCG.emitSharedLValue(*this, Count); |
| 1104 | RedCG.emitAggregateType(*this, Count); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1105 | AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1106 | RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), |
| 1107 | RedCG.getSharedLValue(Count), |
| 1108 | [&Emission](CodeGenFunction &CGF) { |
| 1109 | CGF.EmitAutoVarInit(Emission); |
| 1110 | return true; |
| 1111 | }); |
| 1112 | EmitAutoVarCleanups(Emission); |
| 1113 | Address BaseAddr = RedCG.adjustPrivateAddress( |
| 1114 | *this, Count, Emission.getAllocatedAddress()); |
| 1115 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1116 | RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1117 | assert(IsRegistered && "private var already registered as private"); |
| 1118 | // Silence the warning about unused variable. |
| 1119 | (void)IsRegistered; |
| 1120 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1121 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 1122 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1123 | QualType Type = PrivateVD->getType(); |
| 1124 | bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); |
| 1125 | if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1126 | // Store the address of the original variable associated with the LHS |
| 1127 | // implicit variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1128 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1129 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1130 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1131 | PrivateScope.addPrivate( |
| 1132 | RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); }); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1133 | } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || |
| 1134 | isa<ArraySubscriptExpr>(IRef)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1135 | // Store the address of the original variable associated with the LHS |
| 1136 | // implicit variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1137 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1138 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1139 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1140 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1141 | return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), |
| 1142 | ConvertTypeForMem(RHSVD->getType()), |
| 1143 | "rhs.begin"); |
| 1144 | }); |
| 1145 | } else { |
| 1146 | QualType Type = PrivateVD->getType(); |
| 1147 | bool IsArray = getContext().getAsArrayType(Type) != nullptr; |
| 1148 | Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(); |
| 1149 | // Store the address of the original variable associated with the LHS |
| 1150 | // implicit variable. |
| 1151 | if (IsArray) { |
| 1152 | OriginalAddr = Builder.CreateElementBitCast( |
| 1153 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1154 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1155 | PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1156 | PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1157 | RHSVD, [this, PrivateVD, RHSVD, IsArray]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1158 | return IsArray |
| 1159 | ? Builder.CreateElementBitCast( |
| 1160 | GetAddrOfLocalVar(PrivateVD), |
| 1161 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin") |
| 1162 | : GetAddrOfLocalVar(PrivateVD); |
| 1163 | }); |
| 1164 | } |
| 1165 | ++ILHS; |
| 1166 | ++IRHS; |
| 1167 | ++IPriv; |
| 1168 | ++Count; |
| 1169 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1173 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1174 | if (!HaveInsertPoint()) |
| 1175 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1176 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1177 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1178 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1179 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1180 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1181 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1182 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1183 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1184 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1185 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1186 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1187 | } |
| 1188 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1189 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1190 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1191 | ReductionKind == OMPD_simd; |
| 1192 | bool SimpleReduction = ReductionKind == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1193 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1194 | // parallel directive (it always has implicit barrier). |
| 1195 | CGM.getOpenMPRuntime().emitReduction( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1196 | *this, D.getEndLoc(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1197 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1198 | } |
| 1199 | } |
| 1200 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1201 | static void emitPostUpdateForReductionClause( |
| 1202 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1203 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1204 | if (!CGF.HaveInsertPoint()) |
| 1205 | return; |
| 1206 | llvm::BasicBlock *DoneBB = nullptr; |
| 1207 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1208 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1209 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1210 | if (llvm::Value *Cond = CondGen(CGF)) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1211 | // If the first post-update expression is found, emit conditional |
| 1212 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1213 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1214 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1215 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1216 | CGF.EmitBlock(ThenBB); |
| 1217 | } |
| 1218 | } |
| 1219 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1220 | } |
| 1221 | } |
| 1222 | if (DoneBB) |
| 1223 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1224 | } |
| 1225 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1226 | namespace { |
| 1227 | /// Codegen lambda for appending distribute lower and upper bounds to outlined |
| 1228 | /// parallel function. This is necessary for combined constructs such as |
| 1229 | /// 'distribute parallel for' |
| 1230 | typedef llvm::function_ref<void(CodeGenFunction &, |
| 1231 | const OMPExecutableDirective &, |
| 1232 | llvm::SmallVectorImpl<llvm::Value *> &)> |
| 1233 | CodeGenBoundParametersTy; |
| 1234 | } // anonymous namespace |
| 1235 | |
| 1236 | static void emitCommonOMPParallelDirective( |
| 1237 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 1238 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1239 | const CodeGenBoundParametersTy &CodeGenBoundParameters) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1240 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1241 | llvm::Function *OutlinedFn = |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1242 | CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1243 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1244 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1245 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1246 | llvm::Value *NumThreads = |
| 1247 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1248 | /*IgnoreResultAssign=*/true); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1249 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1250 | CGF, NumThreads, NumThreadsClause->getBeginLoc()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1251 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1252 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1253 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1254 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1255 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getBeginLoc()); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1256 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1257 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1258 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1259 | if (C->getNameModifier() == OMPD_unknown || |
| 1260 | C->getNameModifier() == OMPD_parallel) { |
| 1261 | IfCond = C->getCondition(); |
| 1262 | break; |
| 1263 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1264 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1265 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1266 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1267 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1268 | // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk |
| 1269 | // lower and upper bounds with the pragma 'for' chunking mechanism. |
| 1270 | // The following lambda takes care of appending the lower and upper bound |
| 1271 | // parameters when necessary |
| 1272 | CodeGenBoundParameters(CGF, S, CapturedVars); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1273 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1274 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getBeginLoc(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1275 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1278 | static void emitEmptyBoundParameters(CodeGenFunction &, |
| 1279 | const OMPExecutableDirective &, |
| 1280 | llvm::SmallVectorImpl<llvm::Value *> &) {} |
| 1281 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1282 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1283 | // Emit parallel region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1284 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 1285 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1286 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1287 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1288 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1289 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1290 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1291 | // propagation master's thread values of threadprivate variables to local |
| 1292 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1293 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1294 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1295 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1296 | } |
| 1297 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1298 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1299 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1300 | CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1301 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1302 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1303 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, |
| 1304 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1305 | emitPostUpdateForReductionClause(*this, S, |
| 1306 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1307 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1308 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1309 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1310 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1311 | RunCleanupsScope BodyScope(*this); |
| 1312 | // Update counters values on current iteration. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1313 | for (const Expr *UE : D.updates()) |
| 1314 | EmitIgnoredExpr(UE); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1315 | // Update the linear variables. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1316 | // In distribute directives only loop counters may be marked as linear, no |
| 1317 | // need to generate the code for them. |
| 1318 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1319 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1320 | for (const Expr *UE : C->updates()) |
| 1321 | EmitIgnoredExpr(UE); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1322 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1325 | // On a continue in the body, jump to the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1326 | JumpDest Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1327 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1328 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1329 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1330 | // The end (updates/cleanups). |
| 1331 | EmitBlock(Continue.getBlock()); |
| 1332 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1335 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1336 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1337 | const Expr *IncExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1338 | const llvm::function_ref<void(CodeGenFunction &)> BodyGen, |
| 1339 | const llvm::function_ref<void(CodeGenFunction &)> PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1340 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1341 | |
| 1342 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1343 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1344 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1345 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1346 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1347 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1348 | |
| 1349 | // If there are any cleanups between here and the loop-exit scope, |
| 1350 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1351 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1352 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1353 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1354 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1355 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1356 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1357 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1358 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1359 | if (ExitBlock != LoopExit.getBlock()) { |
| 1360 | EmitBlock(ExitBlock); |
| 1361 | EmitBranchThroughCleanup(LoopExit); |
| 1362 | } |
| 1363 | |
| 1364 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1365 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1366 | |
| 1367 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1368 | JumpDest Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1369 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1370 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1371 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1372 | |
| 1373 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1374 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1375 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1376 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1377 | BreakContinueStack.pop_back(); |
| 1378 | EmitBranch(CondBlock); |
| 1379 | LoopStack.pop(); |
| 1380 | // Emit the fall-through block. |
| 1381 | EmitBlock(LoopExit.getBlock()); |
| 1382 | } |
| 1383 | |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1384 | bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1385 | if (!HaveInsertPoint()) |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1386 | return false; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1387 | // Emit inits for the linear variables. |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1388 | bool HasLinears = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1389 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1390 | for (const Expr *Init : C->inits()) { |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1391 | HasLinears = true; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1392 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
| 1393 | if (const auto *Ref = |
| 1394 | dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1395 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1396 | const auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1397 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1398 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1399 | VD->getInit()->getType(), VK_LValue, |
| 1400 | VD->getInit()->getExprLoc()); |
| 1401 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1402 | VD->getType()), |
| 1403 | /*capturedByInit=*/false); |
| 1404 | EmitAutoVarCleanups(Emission); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1405 | } else { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1406 | EmitVarDecl(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1407 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1408 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1409 | // Emit the linear steps for the linear clauses. |
| 1410 | // 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] | 1411 | if (const auto *CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1412 | if (const auto *SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1413 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1414 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1415 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1416 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1417 | } |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1418 | return HasLinears; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1421 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1422 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1423 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1424 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1425 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1426 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1427 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1428 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1429 | auto IC = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1430 | for (const Expr *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1431 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1432 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1433 | // If the first post-update expression is found, emit conditional |
| 1434 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1435 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.linear.pu"); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1436 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1437 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1438 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1439 | } |
| 1440 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1441 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1442 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1443 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1444 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1445 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1446 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1447 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1448 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1449 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1450 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1451 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1452 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1453 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1454 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1455 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1456 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1459 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1460 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1461 | if (!CGF.HaveInsertPoint()) |
| 1462 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1463 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1464 | unsigned ClauseAlignment = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1465 | if (const Expr *AlignmentExpr = Clause->getAlignment()) { |
| 1466 | auto *AlignmentCI = |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1467 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1468 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1469 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1470 | for (const Expr *E : Clause->varlists()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1471 | unsigned Alignment = ClauseAlignment; |
| 1472 | if (Alignment == 0) { |
| 1473 | // OpenMP [2.8.1, Description] |
| 1474 | // If no optional parameter is specified, implementation-defined default |
| 1475 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1476 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1477 | CGF.getContext() |
| 1478 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1479 | E->getType()->getPointeeType())) |
| 1480 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1481 | } |
| 1482 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1483 | "alignment is not power of 2"); |
| 1484 | if (Alignment != 0) { |
| 1485 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
Roman Lebedev | bd1c087 | 2019-01-15 09:44:25 +0000 | [diff] [blame] | 1486 | CGF.EmitAlignmentAssumption( |
| 1487 | PtrValue, E, /*No second loc needed*/ SourceLocation(), Alignment); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1488 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1493 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1494 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1495 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1496 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1497 | auto I = S.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1498 | for (const Expr *E : S.counters()) { |
| 1499 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1500 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1501 | // Emit var without initialization. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1502 | AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1503 | EmitAutoVarCleanups(VarEmission); |
| 1504 | LocalDeclMap.erase(PrivateVD); |
| 1505 | (void)LoopScope.addPrivate(VD, [&VarEmission]() { |
| 1506 | return VarEmission.getAllocatedAddress(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1507 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1508 | if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || |
| 1509 | VD->hasGlobalStorage()) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1510 | (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1511 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1512 | LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), |
| 1513 | E->getType(), VK_LValue, E->getExprLoc()); |
| 1514 | return EmitLValue(&DRE).getAddress(); |
| 1515 | }); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1516 | } else { |
| 1517 | (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() { |
| 1518 | return VarEmission.getAllocatedAddress(); |
| 1519 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1520 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1521 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1522 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1523 | // Privatize extra loop counters used in loops for ordered(n) clauses. |
| 1524 | for (const auto *C : S.getClausesOfKind<OMPOrderedClause>()) { |
| 1525 | if (!C->getNumForLoops()) |
| 1526 | continue; |
| 1527 | for (unsigned I = S.getCollapsedNumber(), |
| 1528 | E = C->getLoopNumIterations().size(); |
| 1529 | I < E; ++I) { |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 1530 | const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I)); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1531 | const auto *VD = cast<VarDecl>(DRE->getDecl()); |
Alexey Bataev | 0d8fcdf | 2019-03-14 20:36:00 +0000 | [diff] [blame] | 1532 | // Override only those variables that can be captured to avoid re-emission |
| 1533 | // of the variables declared within the loops. |
| 1534 | if (DRE->refersToEnclosingVariableOrCapture()) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1535 | (void)LoopScope.addPrivate(VD, [this, DRE, VD]() { |
| 1536 | return CreateMemTemp(DRE->getType(), VD->getName()); |
| 1537 | }); |
| 1538 | } |
| 1539 | } |
| 1540 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1543 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1544 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1545 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1546 | if (!CGF.HaveInsertPoint()) |
| 1547 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1548 | { |
| 1549 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1550 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1551 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1552 | // Get initial values of real counters. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1553 | for (const Expr *I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1554 | CGF.EmitIgnoredExpr(I); |
| 1555 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1556 | } |
| 1557 | // Check that loop is executed at least one time. |
| 1558 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 1559 | } |
| 1560 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1561 | void CodeGenFunction::EmitOMPLinearClause( |
| 1562 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1563 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1564 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1565 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1566 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1567 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1568 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1569 | SIMDLCVs.insert( |
| 1570 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1571 | } |
| 1572 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1573 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1574 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1575 | for (const Expr *E : C->varlists()) { |
| 1576 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1577 | const auto *PrivateVD = |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1578 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1579 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1580 | bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1581 | // Emit private VarDecl with copy init. |
| 1582 | EmitVarDecl(*PrivateVD); |
| 1583 | return GetAddrOfLocalVar(PrivateVD); |
| 1584 | }); |
| 1585 | assert(IsRegistered && "linear var already registered as private"); |
| 1586 | // Silence the warning about unused variable. |
| 1587 | (void)IsRegistered; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1588 | } else { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1589 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1590 | } |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1591 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1596 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1597 | const OMPExecutableDirective &D, |
| 1598 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1599 | if (!CGF.HaveInsertPoint()) |
| 1600 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1601 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1602 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1603 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1604 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1605 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1606 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1607 | // the memory instructions parallel, because loop-carried |
| 1608 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1609 | if (!IsMonotonic) |
| 1610 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1611 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1612 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1613 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1614 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1615 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1616 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1617 | // the memory instructions parallel, because loop-carried |
| 1618 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1619 | CGF.LoopStack.setParallel(/*Enable=*/false); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1623 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1624 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1625 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1626 | LoopStack.setParallel(!IsMonotonic); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1627 | LoopStack.setVectorizeEnable(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1628 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1631 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1632 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1633 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1634 | if (!HaveInsertPoint()) |
| 1635 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1636 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1637 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1638 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1639 | for (const Expr *F : D.finals()) { |
| 1640 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
| 1641 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1642 | const auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1643 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1644 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1645 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1646 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1647 | // If the first post-update expression is found, emit conditional |
| 1648 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1649 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.final.then"); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1650 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1651 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1652 | EmitBlock(ThenBB); |
| 1653 | } |
| 1654 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1655 | Address OrigAddr = Address::invalid(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1656 | if (CED) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1657 | OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1658 | } else { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1659 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(PrivateVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1660 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1661 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
| 1662 | OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1663 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1664 | OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1665 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1666 | (void)VarScope.Privatize(); |
| 1667 | EmitIgnoredExpr(F); |
| 1668 | } |
| 1669 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1670 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1671 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1672 | if (DoneBB) |
| 1673 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1676 | static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, |
| 1677 | const OMPLoopDirective &S, |
| 1678 | CodeGenFunction::JumpDest LoopExit) { |
| 1679 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1680 | CGF.EmitStopPoint(&S); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 1681 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1682 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1683 | /// Emit a helper variable and return corresponding lvalue. |
| 1684 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1685 | const DeclRefExpr *Helper) { |
| 1686 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1687 | CGF.EmitVarDecl(*VDecl); |
| 1688 | return CGF.EmitLValue(Helper); |
| 1689 | } |
| 1690 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1691 | static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1692 | PrePostActionTy &Action) { |
| 1693 | Action.Enter(CGF); |
| 1694 | assert(isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 1695 | "Expected simd directive"); |
| 1696 | OMPLoopScope PreInitScope(CGF, S); |
| 1697 | // if (PreCond) { |
| 1698 | // for (IV in 0..LastIteration) BODY; |
| 1699 | // <Final counter/linear vars updates>; |
| 1700 | // } |
| 1701 | // |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1702 | if (isOpenMPDistributeDirective(S.getDirectiveKind()) || |
| 1703 | isOpenMPWorksharingDirective(S.getDirectiveKind()) || |
| 1704 | isOpenMPTaskLoopDirective(S.getDirectiveKind())) { |
| 1705 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1706 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1707 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1708 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1709 | // Emit: if (PreCond) - begin. |
| 1710 | // If the condition constant folds and can be elided, avoid emitting the |
| 1711 | // whole loop. |
| 1712 | bool CondConstant; |
| 1713 | llvm::BasicBlock *ContBlock = nullptr; |
| 1714 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1715 | if (!CondConstant) |
| 1716 | return; |
| 1717 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1718 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1719 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
| 1720 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1721 | CGF.getProfileCount(&S)); |
| 1722 | CGF.EmitBlock(ThenBlock); |
| 1723 | CGF.incrementProfileCounter(&S); |
| 1724 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1725 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1726 | // Emit the loop iteration variable. |
| 1727 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1728 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1729 | CGF.EmitVarDecl(*IVDecl); |
| 1730 | CGF.EmitIgnoredExpr(S.getInit()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1731 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1732 | // Emit the iterations count variable. |
| 1733 | // If it is not a variable, Sema decided to calculate iterations count on |
| 1734 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1735 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1736 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1737 | // Emit calculation of the iterations count. |
| 1738 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1739 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1740 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1741 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1742 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1743 | emitAlignedClause(CGF, S); |
| 1744 | (void)CGF.EmitOMPLinearClauseInit(S); |
| 1745 | { |
| 1746 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1747 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 1748 | CGF.EmitOMPLinearClause(S, LoopScope); |
| 1749 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 1750 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 1751 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 1752 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 1753 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 1754 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1755 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1756 | S.getInc(), |
| 1757 | [&S](CodeGenFunction &CGF) { |
| 1758 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 1759 | CGF.EmitStopPoint(&S); |
| 1760 | }, |
| 1761 | [](CodeGenFunction &) {}); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1762 | CGF.EmitOMPSimdFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1763 | // Emit final copy of the lastprivate variables at the end of loops. |
| 1764 | if (HasLastprivateClause) |
| 1765 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
| 1766 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1767 | emitPostUpdateForReductionClause(CGF, S, |
| 1768 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1769 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1770 | CGF.EmitOMPLinearClauseFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1771 | // Emit: if (PreCond) - end. |
| 1772 | if (ContBlock) { |
| 1773 | CGF.EmitBranch(ContBlock); |
| 1774 | CGF.EmitBlock(ContBlock, true); |
| 1775 | } |
| 1776 | } |
| 1777 | |
| 1778 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
| 1779 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 1780 | emitOMPSimdRegion(CGF, S, Action); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1781 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1782 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1783 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1786 | void CodeGenFunction::EmitOMPOuterLoop( |
| 1787 | bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, |
| 1788 | CodeGenFunction::OMPPrivateScope &LoopScope, |
| 1789 | const CodeGenFunction::OMPLoopArguments &LoopArgs, |
| 1790 | const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, |
| 1791 | const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1792 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1793 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1794 | const Expr *IVExpr = S.getIterationVariable(); |
| 1795 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1796 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1797 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1798 | JumpDest LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1799 | |
| 1800 | // Start the loop with a block that tests the condition. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1801 | llvm::BasicBlock *CondBlock = createBasicBlock("omp.dispatch.cond"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1802 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1803 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1804 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1805 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1806 | |
| 1807 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1808 | if (!DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1809 | // UB = min(UB, GlobalUB) or |
| 1810 | // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. |
| 1811 | // 'distribute parallel for') |
| 1812 | EmitIgnoredExpr(LoopArgs.EUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1813 | // IV = LB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1814 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1815 | // IV < UB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1816 | BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1817 | } else { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1818 | BoolCondVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1819 | RT.emitForNext(*this, S.getBeginLoc(), IVSize, IVSigned, LoopArgs.IL, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1820 | LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1821 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1822 | |
| 1823 | // If there are any cleanups between here and the loop-exit scope, |
| 1824 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1825 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1826 | if (LoopScope.requiresCleanups()) |
| 1827 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1828 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1829 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.dispatch.body"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1830 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1831 | if (ExitBlock != LoopExit.getBlock()) { |
| 1832 | EmitBlock(ExitBlock); |
| 1833 | EmitBranchThroughCleanup(LoopExit); |
| 1834 | } |
| 1835 | EmitBlock(LoopBody); |
| 1836 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1837 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1838 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1839 | if (DynamicOrOrdered) |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1840 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1841 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1842 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1843 | JumpDest Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1844 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1845 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1846 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1847 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1848 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1849 | LoopStack.setParallel(!IsMonotonic); |
| 1850 | else |
| 1851 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1852 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1853 | SourceLocation Loc = S.getBeginLoc(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1854 | |
| 1855 | // when 'distribute' is not combined with a 'for': |
| 1856 | // while (idx <= UB) { BODY; ++idx; } |
| 1857 | // when 'distribute' is combined with a 'for' |
| 1858 | // (e.g. 'distribute parallel for') |
| 1859 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 1860 | EmitOMPInnerLoop( |
| 1861 | S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, |
| 1862 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 1863 | CodeGenLoop(CGF, S, LoopExit); |
| 1864 | }, |
| 1865 | [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { |
| 1866 | CodeGenOrdered(CGF, Loc, IVSize, IVSigned); |
| 1867 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1868 | |
| 1869 | EmitBlock(Continue.getBlock()); |
| 1870 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1871 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1872 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1873 | EmitIgnoredExpr(LoopArgs.NextLB); |
| 1874 | EmitIgnoredExpr(LoopArgs.NextUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1875 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1876 | |
| 1877 | EmitBranch(CondBlock); |
| 1878 | LoopStack.pop(); |
| 1879 | // Emit the fall-through block. |
| 1880 | EmitBlock(LoopExit.getBlock()); |
| 1881 | |
| 1882 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1883 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 1884 | if (!DynamicOrOrdered) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1885 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 1886 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1887 | }; |
| 1888 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1892 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1893 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1894 | const OMPLoopArguments &LoopArgs, |
| 1895 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1896 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1897 | |
| 1898 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1899 | const bool DynamicOrOrdered = |
| 1900 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1901 | |
| 1902 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1903 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1904 | LoopArgs.Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1905 | "static non-chunked schedule does not need outer loop"); |
| 1906 | |
| 1907 | // Emit outer loop. |
| 1908 | // |
| 1909 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1910 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1911 | // distributed to threads in the team in chunks as the threads request them. |
| 1912 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1913 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1914 | // iterations, except for the last chunk to be distributed, which may have |
| 1915 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1916 | // |
| 1917 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1918 | // to threads in the team in chunks as the executing threads request them. |
| 1919 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1920 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1921 | // each chunk is proportional to the number of unassigned iterations divided |
| 1922 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1923 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1924 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1925 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1926 | // than k iterations). |
| 1927 | // |
| 1928 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1929 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1930 | // implementation the freedom to choose any possible mapping of iterations to |
| 1931 | // threads in the team. |
| 1932 | // |
| 1933 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1934 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1935 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1936 | // implementation defined |
| 1937 | // |
| 1938 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1939 | // idx = LB; |
| 1940 | // while (idx <= UB) { BODY; ++idx; |
| 1941 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1942 | // } // inner loop |
| 1943 | // } |
| 1944 | // |
| 1945 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1946 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1947 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1948 | // the team in a round-robin fashion in the order of the thread number. |
| 1949 | // |
| 1950 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1951 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1952 | // LB = LB + ST; |
| 1953 | // UB = UB + ST; |
| 1954 | // } |
| 1955 | // |
| 1956 | |
| 1957 | const Expr *IVExpr = S.getIterationVariable(); |
| 1958 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1959 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1960 | |
| 1961 | if (DynamicOrOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1962 | const std::pair<llvm::Value *, llvm::Value *> DispatchBounds = |
| 1963 | CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1964 | llvm::Value *LBVal = DispatchBounds.first; |
| 1965 | llvm::Value *UBVal = DispatchBounds.second; |
| 1966 | CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, |
| 1967 | LoopArgs.Chunk}; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1968 | RT.emitForDispatchInit(*this, S.getBeginLoc(), ScheduleKind, IVSize, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1969 | IVSigned, Ordered, DipatchRTInputValues); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1970 | } else { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1971 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 1972 | IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, |
| 1973 | LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1974 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1975 | ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1976 | } |
| 1977 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1978 | auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, |
| 1979 | const unsigned IVSize, |
| 1980 | const bool IVSigned) { |
| 1981 | if (Ordered) { |
| 1982 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, |
| 1983 | IVSigned); |
| 1984 | } |
| 1985 | }; |
| 1986 | |
| 1987 | OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, |
| 1988 | LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); |
| 1989 | OuterLoopArgs.IncExpr = S.getInc(); |
| 1990 | OuterLoopArgs.Init = S.getInit(); |
| 1991 | OuterLoopArgs.Cond = S.getCond(); |
| 1992 | OuterLoopArgs.NextLB = S.getNextLowerBound(); |
| 1993 | OuterLoopArgs.NextUB = S.getNextUpperBound(); |
| 1994 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, |
| 1995 | emitOMPLoopBodyWithStopPoint, CodeGenOrdered); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1996 | } |
| 1997 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1998 | static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, |
| 1999 | const unsigned IVSize, const bool IVSigned) {} |
| 2000 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2001 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2002 | OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, |
| 2003 | OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, |
| 2004 | const CodeGenLoopTy &CodeGenLoopContent) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2005 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2006 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2007 | |
| 2008 | // Emit outer loop. |
| 2009 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 2010 | // dynamic |
| 2011 | // |
| 2012 | |
| 2013 | const Expr *IVExpr = S.getIterationVariable(); |
| 2014 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2015 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2016 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2017 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2018 | IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, |
| 2019 | LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2020 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2021 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2022 | // for combined 'distribute' and 'for' the increment expression of distribute |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2023 | // is stored in DistInc. For 'distribute' alone, it is in Inc. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2024 | Expr *IncExpr; |
| 2025 | if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) |
| 2026 | IncExpr = S.getDistInc(); |
| 2027 | else |
| 2028 | IncExpr = S.getInc(); |
| 2029 | |
| 2030 | // this routine is shared by 'omp distribute parallel for' and |
| 2031 | // 'omp distribute': select the right EUB expression depending on the |
| 2032 | // directive |
| 2033 | OMPLoopArguments OuterLoopArgs; |
| 2034 | OuterLoopArgs.LB = LoopArgs.LB; |
| 2035 | OuterLoopArgs.UB = LoopArgs.UB; |
| 2036 | OuterLoopArgs.ST = LoopArgs.ST; |
| 2037 | OuterLoopArgs.IL = LoopArgs.IL; |
| 2038 | OuterLoopArgs.Chunk = LoopArgs.Chunk; |
| 2039 | OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2040 | ? S.getCombinedEnsureUpperBound() |
| 2041 | : S.getEnsureUpperBound(); |
| 2042 | OuterLoopArgs.IncExpr = IncExpr; |
| 2043 | OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2044 | ? S.getCombinedInit() |
| 2045 | : S.getInit(); |
| 2046 | OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2047 | ? S.getCombinedCond() |
| 2048 | : S.getCond(); |
| 2049 | OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2050 | ? S.getCombinedNextLowerBound() |
| 2051 | : S.getNextLowerBound(); |
| 2052 | OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2053 | ? S.getCombinedNextUpperBound() |
| 2054 | : S.getNextUpperBound(); |
| 2055 | |
| 2056 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, |
| 2057 | LoopScope, OuterLoopArgs, CodeGenLoopContent, |
| 2058 | emitEmptyOrdered); |
| 2059 | } |
| 2060 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2061 | static std::pair<LValue, LValue> |
| 2062 | emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, |
| 2063 | const OMPExecutableDirective &S) { |
| 2064 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2065 | LValue LB = |
| 2066 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2067 | LValue UB = |
| 2068 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2069 | |
| 2070 | // When composing 'distribute' with 'for' (e.g. as in 'distribute |
| 2071 | // parallel for') we need to use the 'distribute' |
| 2072 | // chunk lower and upper bounds rather than the whole loop iteration |
| 2073 | // space. These are parameters to the outlined function for 'parallel' |
| 2074 | // and we copy the bounds of the previous schedule into the |
| 2075 | // the current ones. |
| 2076 | LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); |
| 2077 | LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2078 | llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar( |
| 2079 | PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2080 | PrevLBVal = CGF.EmitScalarConversion( |
| 2081 | PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2082 | LS.getIterationVariable()->getType(), |
| 2083 | LS.getPrevLowerBoundVariable()->getExprLoc()); |
| 2084 | llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar( |
| 2085 | PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2086 | PrevUBVal = CGF.EmitScalarConversion( |
| 2087 | PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2088 | LS.getIterationVariable()->getType(), |
| 2089 | LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2090 | |
| 2091 | CGF.EmitStoreOfScalar(PrevLBVal, LB); |
| 2092 | CGF.EmitStoreOfScalar(PrevUBVal, UB); |
| 2093 | |
| 2094 | return {LB, UB}; |
| 2095 | } |
| 2096 | |
| 2097 | /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then |
| 2098 | /// we need to use the LB and UB expressions generated by the worksharing |
| 2099 | /// code generation support, whereas in non combined situations we would |
| 2100 | /// just emit 0 and the LastIteration expression |
| 2101 | /// This function is necessary due to the difference of the LB and UB |
| 2102 | /// types for the RT emission routines for 'for_static_init' and |
| 2103 | /// 'for_dispatch_init' |
| 2104 | static std::pair<llvm::Value *, llvm::Value *> |
| 2105 | emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, |
| 2106 | const OMPExecutableDirective &S, |
| 2107 | Address LB, Address UB) { |
| 2108 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2109 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2110 | // when implementing a dynamic schedule for a 'for' combined with a |
| 2111 | // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop |
| 2112 | // is not normalized as each team only executes its own assigned |
| 2113 | // distribute chunk |
| 2114 | QualType IteratorTy = IVExpr->getType(); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2115 | llvm::Value *LBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2116 | CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2117 | llvm::Value *UBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2118 | CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2119 | return {LBVal, UBVal}; |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2120 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2121 | |
| 2122 | static void emitDistributeParallelForDistributeInnerBoundParams( |
| 2123 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2124 | llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { |
| 2125 | const auto &Dir = cast<OMPLoopDirective>(S); |
| 2126 | LValue LB = |
| 2127 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2128 | llvm::Value *LBCast = CGF.Builder.CreateIntCast( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2129 | CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2130 | CapturedVars.push_back(LBCast); |
| 2131 | LValue UB = |
| 2132 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); |
| 2133 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2134 | llvm::Value *UBCast = CGF.Builder.CreateIntCast( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2135 | CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2136 | CapturedVars.push_back(UBCast); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2137 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2138 | |
| 2139 | static void |
| 2140 | emitInnerParallelForWhenCombined(CodeGenFunction &CGF, |
| 2141 | const OMPLoopDirective &S, |
| 2142 | CodeGenFunction::JumpDest LoopExit) { |
| 2143 | auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2144 | PrePostActionTy &Action) { |
| 2145 | Action.Enter(CGF); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2146 | bool HasCancel = false; |
| 2147 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2148 | if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) |
| 2149 | HasCancel = D->hasCancel(); |
| 2150 | else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) |
| 2151 | HasCancel = D->hasCancel(); |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 2152 | else if (const auto *D = |
| 2153 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) |
| 2154 | HasCancel = D->hasCancel(); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2155 | } |
| 2156 | CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), |
| 2157 | HasCancel); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2158 | CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), |
| 2159 | emitDistributeParallelForInnerBounds, |
| 2160 | emitDistributeParallelForDispatchBounds); |
| 2161 | }; |
| 2162 | |
| 2163 | emitCommonOMPParallelDirective( |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2164 | CGF, S, |
| 2165 | isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, |
| 2166 | CGInlinedWorksharingLoop, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2167 | emitDistributeParallelForDistributeInnerBoundParams); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2168 | } |
| 2169 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2170 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 2171 | const OMPDistributeParallelForDirective &S) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2172 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2173 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2174 | S.getDistInc()); |
| 2175 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2176 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 2177 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2180 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 2181 | const OMPDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2182 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2183 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2184 | S.getDistInc()); |
| 2185 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2186 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2187 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2188 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2189 | |
| 2190 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 2191 | const OMPDistributeSimdDirective &S) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2192 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2193 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 2194 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2195 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2196 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2199 | void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| 2200 | CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { |
| 2201 | // Emit SPMD target parallel for region as a standalone region. |
| 2202 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2203 | emitOMPSimdRegion(CGF, S, Action); |
| 2204 | }; |
| 2205 | llvm::Function *Fn; |
| 2206 | llvm::Constant *Addr; |
| 2207 | // Emit target region as a standalone region. |
| 2208 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 2209 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 2210 | assert(Fn && Addr && "Target device function emission failed."); |
| 2211 | } |
| 2212 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2213 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 2214 | const OMPTargetSimdDirective &S) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2215 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2216 | emitOMPSimdRegion(CGF, S, Action); |
| 2217 | }; |
| 2218 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2219 | } |
| 2220 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2221 | namespace { |
| 2222 | struct ScheduleKindModifiersTy { |
| 2223 | OpenMPScheduleClauseKind Kind; |
| 2224 | OpenMPScheduleClauseModifier M1; |
| 2225 | OpenMPScheduleClauseModifier M2; |
| 2226 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2227 | OpenMPScheduleClauseModifier M1, |
| 2228 | OpenMPScheduleClauseModifier M2) |
| 2229 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2230 | }; |
| 2231 | } // namespace |
| 2232 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2233 | bool CodeGenFunction::EmitOMPWorksharingLoop( |
| 2234 | const OMPLoopDirective &S, Expr *EUB, |
| 2235 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
| 2236 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2237 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2238 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2239 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2240 | EmitVarDecl(*IVDecl); |
| 2241 | |
| 2242 | // Emit the iterations count variable. |
| 2243 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2244 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2245 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2246 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2247 | // Emit calculation of the iterations count. |
| 2248 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2249 | } |
| 2250 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2251 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2252 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2253 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2254 | // Check pre-condition. |
| 2255 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2256 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2257 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2258 | // If the condition constant folds and can be elided, avoid emitting the |
| 2259 | // whole loop. |
| 2260 | bool CondConstant; |
| 2261 | llvm::BasicBlock *ContBlock = nullptr; |
| 2262 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2263 | if (!CondConstant) |
| 2264 | return false; |
| 2265 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2266 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2267 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2268 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2269 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2270 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2271 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2272 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2273 | |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2274 | RunCleanupsScope DoacrossCleanupScope(*this); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2275 | bool Ordered = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2276 | if (const auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2277 | if (OrderedClause->getNumForLoops()) |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 2278 | RT.emitDoacrossInit(*this, S, OrderedClause->getLoopNumIterations()); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2279 | else |
| 2280 | Ordered = true; |
| 2281 | } |
| 2282 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2283 | llvm::DenseSet<const Expr *> EmittedFinals; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2284 | emitAlignedClause(*this, S); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2285 | bool HasLinears = EmitOMPLinearClauseInit(S); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2286 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2287 | |
| 2288 | std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S); |
| 2289 | LValue LB = Bounds.first; |
| 2290 | LValue UB = Bounds.second; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2291 | LValue ST = |
| 2292 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2293 | LValue IL = |
| 2294 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2295 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2296 | // Emit 'then' code. |
| 2297 | { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2298 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2299 | if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2300 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2301 | // initialization of firstprivate variables and post-update of |
| 2302 | // lastprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2303 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2304 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2305 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2306 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 2307 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2308 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 2309 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2310 | EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2311 | EmitOMPLinearClause(S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 2312 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2313 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2314 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2315 | |
| 2316 | // Detect the loop schedule kind and chunk. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2317 | const Expr *ChunkExpr = nullptr; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2318 | OpenMPScheduleTy ScheduleKind; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2319 | if (const auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2320 | ScheduleKind.Schedule = C->getScheduleKind(); |
| 2321 | ScheduleKind.M1 = C->getFirstScheduleModifier(); |
| 2322 | ScheduleKind.M2 = C->getSecondScheduleModifier(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2323 | ChunkExpr = C->getChunkSize(); |
Gheorghe-Teodor Bercea | 8233af9 | 2018-09-27 20:29:00 +0000 | [diff] [blame] | 2324 | } else { |
| 2325 | // Default behaviour for schedule clause. |
| 2326 | CGM.getOpenMPRuntime().getDefaultScheduleAndChunk( |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2327 | *this, S, ScheduleKind.Schedule, ChunkExpr); |
| 2328 | } |
| 2329 | bool HasChunkSizeOne = false; |
| 2330 | llvm::Value *Chunk = nullptr; |
| 2331 | if (ChunkExpr) { |
| 2332 | Chunk = EmitScalarExpr(ChunkExpr); |
| 2333 | Chunk = EmitScalarConversion(Chunk, ChunkExpr->getType(), |
| 2334 | S.getIterationVariable()->getType(), |
| 2335 | S.getBeginLoc()); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 2336 | Expr::EvalResult Result; |
| 2337 | if (ChunkExpr->EvaluateAsInt(Result, getContext())) { |
| 2338 | llvm::APSInt EvaluatedChunk = Result.Val.getInt(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2339 | HasChunkSizeOne = (EvaluatedChunk.getLimitedValue() == 1); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 2340 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2341 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2342 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2343 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2344 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 2345 | // If the static schedule kind is specified or if the ordered clause is |
| 2346 | // specified, and if no monotonic modifier is specified, the effect will |
| 2347 | // be as if the monotonic modifier was specified. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2348 | bool StaticChunkedOne = RT.isStaticChunked(ScheduleKind.Schedule, |
| 2349 | /* Chunked */ Chunk != nullptr) && HasChunkSizeOne && |
| 2350 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
| 2351 | if ((RT.isStaticNonchunked(ScheduleKind.Schedule, |
| 2352 | /* Chunked */ Chunk != nullptr) || |
| 2353 | StaticChunkedOne) && |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2354 | !Ordered) { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2355 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 2356 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2357 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2358 | // When no chunk_size is specified, the iteration space is divided into |
| 2359 | // chunks that are approximately equal in size, and at most one chunk is |
| 2360 | // distributed to each thread. Note that the size of the chunks is |
| 2361 | // unspecified in this case. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2362 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2363 | IVSize, IVSigned, Ordered, IL.getAddress(), LB.getAddress(), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2364 | UB.getAddress(), ST.getAddress(), |
| 2365 | StaticChunkedOne ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2366 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2367 | ScheduleKind, StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2368 | JumpDest LoopExit = |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2369 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2370 | // UB = min(UB, GlobalUB); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2371 | if (!StaticChunkedOne) |
| 2372 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2373 | // IV = LB; |
| 2374 | EmitIgnoredExpr(S.getInit()); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2375 | // For unchunked static schedule generate: |
| 2376 | // |
| 2377 | // while (idx <= UB) { |
| 2378 | // BODY; |
| 2379 | // ++idx; |
| 2380 | // } |
| 2381 | // |
| 2382 | // For static schedule with chunk one: |
| 2383 | // |
| 2384 | // while (IV <= PrevUB) { |
| 2385 | // BODY; |
| 2386 | // IV += ST; |
| 2387 | // } |
| 2388 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), |
| 2389 | StaticChunkedOne ? S.getCombinedParForInDistCond() : S.getCond(), |
| 2390 | StaticChunkedOne ? S.getDistInc() : S.getInc(), |
| 2391 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2392 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 2393 | CGF.EmitStopPoint(&S); |
| 2394 | }, |
| 2395 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2396 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2397 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2398 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2399 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2400 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2401 | }; |
| 2402 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2403 | } else { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2404 | const bool IsMonotonic = |
| 2405 | Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || |
| 2406 | ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || |
| 2407 | ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 2408 | ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2409 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2410 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2411 | const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(), |
| 2412 | ST.getAddress(), IL.getAddress(), |
| 2413 | Chunk, EUB); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2414 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2415 | LoopArguments, CGDispatchBounds); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2416 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2417 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2418 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
| 2419 | return CGF.Builder.CreateIsNotNull( |
| 2420 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
| 2421 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2422 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2423 | EmitOMPReductionClauseFinal( |
| 2424 | S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) |
| 2425 | ? /*Parallel and Simd*/ OMPD_parallel_for_simd |
| 2426 | : /*Parallel only*/ OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2427 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2428 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2429 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2430 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2431 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2432 | }); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2433 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2434 | if (HasLastprivateClause) |
| 2435 | EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2436 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2437 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2438 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2439 | EmitOMPLinearClauseFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2440 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2441 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2442 | }); |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2443 | DoacrossCleanupScope.ForceCleanup(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2444 | // 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] | 2445 | if (ContBlock) { |
| 2446 | EmitBranch(ContBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2447 | EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2448 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2449 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2450 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2453 | /// The following two functions generate expressions for the loop lower |
| 2454 | /// and upper bounds in case of static and dynamic (dispatch) schedule |
| 2455 | /// of the associated 'for' or 'distribute' loop. |
| 2456 | static std::pair<LValue, LValue> |
| 2457 | emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2458 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2459 | LValue LB = |
| 2460 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2461 | LValue UB = |
| 2462 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2463 | return {LB, UB}; |
| 2464 | } |
| 2465 | |
| 2466 | /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not |
| 2467 | /// consider the lower and upper bound expressions generated by the |
| 2468 | /// worksharing loop support, but we use 0 and the iteration space size as |
| 2469 | /// constants |
| 2470 | static std::pair<llvm::Value *, llvm::Value *> |
| 2471 | emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2472 | Address LB, Address UB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2473 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2474 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2475 | const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); |
| 2476 | llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); |
| 2477 | llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); |
| 2478 | return {LBVal, UBVal}; |
| 2479 | } |
| 2480 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2481 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2482 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2483 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2484 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2485 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2486 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2487 | emitForLoopBounds, |
| 2488 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2489 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2490 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2491 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2492 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2493 | S.hasCancel()); |
| 2494 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2495 | |
| 2496 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2497 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2498 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2499 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2500 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2501 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2502 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2503 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2504 | PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2505 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2506 | emitForLoopBounds, |
| 2507 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2508 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2509 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2510 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2511 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2512 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2513 | |
| 2514 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2515 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2516 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2519 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2520 | const Twine &Name, |
| 2521 | llvm::Value *Init = nullptr) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2522 | LValue LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2523 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2524 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2525 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2528 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2529 | const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt(); |
| 2530 | const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2531 | bool HasLastprivates = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2532 | auto &&CodeGen = [&S, CapturedStmt, CS, |
| 2533 | &HasLastprivates](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2534 | ASTContext &C = CGF.getContext(); |
| 2535 | QualType KmpInt32Ty = |
| 2536 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2537 | // Emit helper vars inits. |
| 2538 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2539 | CGF.Builder.getInt32(0)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2540 | llvm::ConstantInt *GlobalUBVal = CS != nullptr |
| 2541 | ? CGF.Builder.getInt32(CS->size() - 1) |
| 2542 | : CGF.Builder.getInt32(0); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2543 | LValue UB = |
| 2544 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2545 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2546 | CGF.Builder.getInt32(1)); |
| 2547 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2548 | CGF.Builder.getInt32(0)); |
| 2549 | // Loop counter. |
| 2550 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2551 | OpaqueValueExpr IVRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2552 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2553 | OpaqueValueExpr UBRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2554 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2555 | // Generate condition for loop. |
| 2556 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2557 | OK_Ordinary, S.getBeginLoc(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2558 | // Increment for loop counter. |
| 2559 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2560 | S.getBeginLoc(), true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2561 | auto &&BodyGen = [CapturedStmt, CS, &S, &IV](CodeGenFunction &CGF) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2562 | // Iterate through all sections and emit a switch construct: |
| 2563 | // switch (IV) { |
| 2564 | // case 0: |
| 2565 | // <SectionStmt[0]>; |
| 2566 | // break; |
| 2567 | // ... |
| 2568 | // case <NumSection> - 1: |
| 2569 | // <SectionStmt[<NumSection> - 1]>; |
| 2570 | // break; |
| 2571 | // } |
| 2572 | // .omp.sections.exit: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2573 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 2574 | llvm::SwitchInst *SwitchStmt = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2575 | CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getBeginLoc()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2576 | ExitBB, CS == nullptr ? 1 : CS->size()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2577 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2578 | unsigned CaseNumber = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2579 | for (const Stmt *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2580 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2581 | CGF.EmitBlock(CaseBB); |
| 2582 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2583 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2584 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2585 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2586 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2587 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2588 | llvm::BasicBlock *CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2589 | CGF.EmitBlock(CaseBB); |
| 2590 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2591 | CGF.EmitStmt(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2592 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2593 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2594 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2595 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2596 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2597 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2598 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2599 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2600 | // initialization of firstprivate variables and post-update of lastprivate |
| 2601 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2602 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2603 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2604 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2605 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2606 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2607 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2608 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2609 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2610 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2611 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2612 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2613 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2614 | OpenMPScheduleTy ScheduleKind; |
| 2615 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2616 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2617 | /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), |
| 2618 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2619 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2620 | CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, StaticInit); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2621 | // UB = min(UB, GlobalUB); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2622 | llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, S.getBeginLoc()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2623 | llvm::Value *MinUBGlobalUB = CGF.Builder.CreateSelect( |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2624 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 2625 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 2626 | // IV = LB; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2627 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getBeginLoc()), IV); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2628 | // while (idx <= UB) { BODY; ++idx; } |
| 2629 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 2630 | [](CodeGenFunction &) {}); |
| 2631 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2632 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2633 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2634 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2635 | }; |
| 2636 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2637 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2638 | // Emit post-update of the reduction variables if IsLastIter != 0. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2639 | emitPostUpdateForReductionClause(CGF, S, [IL, &S](CodeGenFunction &CGF) { |
| 2640 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2641 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2642 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2643 | |
| 2644 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2645 | if (HasLastprivates) |
| 2646 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2647 | S, /*NoFinals=*/false, |
| 2648 | CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2649 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2650 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2651 | |
| 2652 | bool HasCancel = false; |
| 2653 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 2654 | HasCancel = OSD->hasCancel(); |
| 2655 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 2656 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2657 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2658 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 2659 | HasCancel); |
| 2660 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 2661 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 2662 | // directive. |
| 2663 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2664 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2665 | // initialization of firstprivate variables. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2666 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2667 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2668 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2669 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2670 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2671 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +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 | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2674 | EmitSections(S); |
| 2675 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2676 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2677 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2678 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2679 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2680 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2684 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2685 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2686 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2687 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2688 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 2689 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2692 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2693 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2694 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2695 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2696 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2697 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2698 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2699 | // Build a list of copyprivate variables along with helper expressions |
| 2700 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2701 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2702 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2703 | DestExprs.append(C->destination_exprs().begin(), |
| 2704 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2705 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2706 | AssignmentOps.append(C->assignment_ops().begin(), |
| 2707 | C->assignment_ops().end()); |
| 2708 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2709 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 2710 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2711 | Action.Enter(CGF); |
| 2712 | OMPPrivateScope SingleScope(CGF); |
| 2713 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 2714 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 2715 | (void)SingleScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2716 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2717 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2718 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2719 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2720 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2721 | CopyprivateVars, DestExprs, |
| 2722 | SrcExprs, AssignmentOps); |
| 2723 | } |
| 2724 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 2725 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2726 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 2727 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2728 | *this, S.getBeginLoc(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2729 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2730 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2733 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2734 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2735 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2736 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2737 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2738 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2739 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2742 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2743 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2744 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2745 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2746 | }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2747 | const Expr *Hint = nullptr; |
| 2748 | if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2749 | Hint = HintClause->getHint(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2750 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2751 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 2752 | S.getDirectiveName().getAsString(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2753 | CodeGen, S.getBeginLoc(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2754 | } |
| 2755 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2756 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 2757 | const OMPParallelForDirective &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); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2762 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2763 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2764 | emitDispatchForLoopBounds); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2765 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2766 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, |
| 2767 | emitEmptyBoundParameters); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2768 | } |
| 2769 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2770 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2771 | const OMPParallelForSimdDirective &S) { |
| 2772 | // Emit directive as a combined directive that consists of two implicit |
| 2773 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2774 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2775 | Action.Enter(CGF); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2776 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2777 | emitDispatchForLoopBounds); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2778 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2779 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, |
| 2780 | emitEmptyBoundParameters); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2781 | } |
| 2782 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2783 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2784 | const OMPParallelSectionsDirective &S) { |
| 2785 | // Emit directive as a combined directive that consists of two implicit |
| 2786 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2787 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2788 | Action.Enter(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2789 | CGF.EmitSections(S); |
| 2790 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2791 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, |
| 2792 | emitEmptyBoundParameters); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2793 | } |
| 2794 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2795 | void CodeGenFunction::EmitOMPTaskBasedDirective( |
| 2796 | const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, |
| 2797 | const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, |
| 2798 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2799 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2800 | const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2801 | auto I = CS->getCapturedDecl()->param_begin(); |
| 2802 | auto PartId = std::next(I); |
| 2803 | auto TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2804 | // Check if the task is final |
| 2805 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 2806 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2807 | // the condition and the dead arm of the if/else. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2808 | const Expr *Cond = Clause->getCondition(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2809 | bool CondConstant; |
| 2810 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2811 | Data.Final.setInt(CondConstant); |
| 2812 | else |
| 2813 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2814 | } else { |
| 2815 | // By default the task is not final. |
| 2816 | Data.Final.setInt(/*IntVal=*/false); |
| 2817 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2818 | // Check if the task has 'priority' clause. |
| 2819 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2820 | const Expr *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 2821 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 2822 | Data.Priority.setPointer(EmitScalarConversion( |
| 2823 | EmitScalarExpr(Prio), Prio->getType(), |
| 2824 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2825 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2826 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2827 | // The first function argument for tasks is a thread id, the second one is a |
| 2828 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2829 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 2830 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2831 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2832 | auto IRef = C->varlist_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.PrivateVars.push_back(*IRef); |
| 2837 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2838 | } |
| 2839 | ++IRef; |
| 2840 | } |
| 2841 | } |
| 2842 | EmittedAsPrivate.clear(); |
| 2843 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2844 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2845 | auto IRef = C->varlist_begin(); |
| 2846 | auto IElemInitRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2847 | for (const Expr *IInit : C->private_copies()) { |
| 2848 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2849 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2850 | Data.FirstprivateVars.push_back(*IRef); |
| 2851 | Data.FirstprivateCopies.push_back(IInit); |
| 2852 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2853 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 2854 | ++IRef; |
| 2855 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2856 | } |
| 2857 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2858 | // Get list of lastprivate variables (for taskloops). |
| 2859 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 2860 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 2861 | auto IRef = C->varlist_begin(); |
| 2862 | auto ID = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2863 | for (const Expr *IInit : C->private_copies()) { |
| 2864 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2865 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2866 | Data.LastprivateVars.push_back(*IRef); |
| 2867 | Data.LastprivateCopies.push_back(IInit); |
| 2868 | } |
| 2869 | LastprivateDstsOrigs.insert( |
| 2870 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 2871 | cast<DeclRefExpr>(*IRef)}); |
| 2872 | ++IRef; |
| 2873 | ++ID; |
| 2874 | } |
| 2875 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2876 | SmallVector<const Expr *, 4> LHSs; |
| 2877 | SmallVector<const Expr *, 4> RHSs; |
| 2878 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 2879 | auto IPriv = C->privates().begin(); |
| 2880 | auto IRed = C->reduction_ops().begin(); |
| 2881 | auto ILHS = C->lhs_exprs().begin(); |
| 2882 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2883 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2884 | Data.ReductionVars.emplace_back(Ref); |
| 2885 | Data.ReductionCopies.emplace_back(*IPriv); |
| 2886 | Data.ReductionOps.emplace_back(*IRed); |
| 2887 | LHSs.emplace_back(*ILHS); |
| 2888 | RHSs.emplace_back(*IRHS); |
| 2889 | std::advance(IPriv, 1); |
| 2890 | std::advance(IRed, 1); |
| 2891 | std::advance(ILHS, 1); |
| 2892 | std::advance(IRHS, 1); |
| 2893 | } |
| 2894 | } |
| 2895 | Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2896 | *this, S.getBeginLoc(), LHSs, RHSs, Data); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2897 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2898 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2899 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2900 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2901 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, |
| 2902 | CapturedRegion](CodeGenFunction &CGF, |
| 2903 | PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2904 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2905 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2906 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 2907 | !Data.LastprivateVars.empty()) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2908 | llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( |
| 2909 | CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2910 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2911 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 2912 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 2913 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 2914 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2915 | // Map privates. |
| 2916 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 2917 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2918 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2919 | for (const Expr *E : Data.PrivateVars) { |
| 2920 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2921 | Address PrivatePtr = CGF.CreateMemTemp( |
| 2922 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2923 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2924 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2925 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2926 | for (const Expr *E : Data.FirstprivateVars) { |
| 2927 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2928 | Address PrivatePtr = |
| 2929 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2930 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2931 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2932 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2933 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2934 | for (const Expr *E : Data.LastprivateVars) { |
| 2935 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2936 | Address PrivatePtr = |
| 2937 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2938 | ".lastpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2939 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2940 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 2941 | } |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2942 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 2943 | CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2944 | for (const auto &Pair : LastprivateDstsOrigs) { |
| 2945 | const auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 2946 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(OrigVD), |
| 2947 | /*RefersToEnclosingVariableOrCapture=*/ |
| 2948 | CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 2949 | Pair.second->getType(), VK_LValue, |
| 2950 | Pair.second->getExprLoc()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2951 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
| 2952 | return CGF.EmitLValue(&DRE).getAddress(); |
| 2953 | }); |
| 2954 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2955 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2956 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2957 | CGF.getContext().getDeclAlign(Pair.first)); |
| 2958 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2959 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2960 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2961 | if (Data.Reductions) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2962 | OMPLexicalScope LexScope(CGF, S, CapturedRegion); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2963 | ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies, |
| 2964 | Data.ReductionOps); |
| 2965 | llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( |
| 2966 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); |
| 2967 | for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { |
| 2968 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2969 | RedCG.emitAggregateType(CGF, Cnt); |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2970 | // FIXME: This must removed once the runtime library is fixed. |
| 2971 | // Emit required threadprivate variables for |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 2972 | // initializer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2973 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2974 | RedCG, Cnt); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2975 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2976 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2977 | Replacement = |
| 2978 | Address(CGF.EmitScalarConversion( |
| 2979 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 2980 | CGF.getContext().getPointerType( |
| 2981 | Data.ReductionCopies[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2982 | Data.ReductionCopies[Cnt]->getExprLoc()), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2983 | Replacement.getAlignment()); |
| 2984 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 2985 | Scope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 2986 | [Replacement]() { return Replacement; }); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2987 | } |
| 2988 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2989 | // Privatize all private variables except for in_reduction items. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2990 | (void)Scope.Privatize(); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2991 | SmallVector<const Expr *, 4> InRedVars; |
| 2992 | SmallVector<const Expr *, 4> InRedPrivs; |
| 2993 | SmallVector<const Expr *, 4> InRedOps; |
| 2994 | SmallVector<const Expr *, 4> TaskgroupDescriptors; |
| 2995 | for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { |
| 2996 | auto IPriv = C->privates().begin(); |
| 2997 | auto IRed = C->reduction_ops().begin(); |
| 2998 | auto ITD = C->taskgroup_descriptors().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2999 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3000 | InRedVars.emplace_back(Ref); |
| 3001 | InRedPrivs.emplace_back(*IPriv); |
| 3002 | InRedOps.emplace_back(*IRed); |
| 3003 | TaskgroupDescriptors.emplace_back(*ITD); |
| 3004 | std::advance(IPriv, 1); |
| 3005 | std::advance(IRed, 1); |
| 3006 | std::advance(ITD, 1); |
| 3007 | } |
| 3008 | } |
| 3009 | // Privatize in_reduction items here, because taskgroup descriptors must be |
| 3010 | // privatized earlier. |
| 3011 | OMPPrivateScope InRedScope(CGF); |
| 3012 | if (!InRedVars.empty()) { |
| 3013 | ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps); |
| 3014 | for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { |
| 3015 | RedCG.emitSharedLValue(CGF, Cnt); |
| 3016 | RedCG.emitAggregateType(CGF, Cnt); |
| 3017 | // The taskgroup descriptor variable is always implicit firstprivate and |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3018 | // privatized already during processing of the firstprivates. |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3019 | // FIXME: This must removed once the runtime library is fixed. |
| 3020 | // Emit required threadprivate variables for |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3021 | // initializer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3022 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3023 | RedCG, Cnt); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3024 | llvm::Value *ReductionsPtr = |
| 3025 | CGF.EmitLoadOfScalar(CGF.EmitLValue(TaskgroupDescriptors[Cnt]), |
| 3026 | TaskgroupDescriptors[Cnt]->getExprLoc()); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3027 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3028 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3029 | Replacement = Address( |
| 3030 | CGF.EmitScalarConversion( |
| 3031 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 3032 | CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3033 | InRedPrivs[Cnt]->getExprLoc()), |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3034 | Replacement.getAlignment()); |
| 3035 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 3036 | InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 3037 | [Replacement]() { return Replacement; }); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3038 | } |
| 3039 | } |
| 3040 | (void)InRedScope.Privatize(); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3041 | |
| 3042 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3043 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3044 | }; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3045 | llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3046 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 3047 | Data.NumberOfParts); |
| 3048 | OMPLexicalScope Scope(*this, S); |
| 3049 | TaskGen(*this, OutlinedFn, Data); |
| 3050 | } |
| 3051 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3052 | static ImplicitParamDecl * |
| 3053 | createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3054 | QualType Ty, CapturedDecl *CD, |
| 3055 | SourceLocation Loc) { |
| 3056 | auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3057 | ImplicitParamDecl::Other); |
| 3058 | auto *OrigRef = DeclRefExpr::Create( |
| 3059 | C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, |
| 3060 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
| 3061 | auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3062 | ImplicitParamDecl::Other); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3063 | auto *PrivateRef = DeclRefExpr::Create( |
| 3064 | C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3065 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3066 | QualType ElemType = C.getBaseElementType(Ty); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3067 | auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType, |
| 3068 | ImplicitParamDecl::Other); |
| 3069 | auto *InitRef = DeclRefExpr::Create( |
| 3070 | C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, |
| 3071 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3072 | PrivateVD->setInitStyle(VarDecl::CInit); |
| 3073 | PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, |
| 3074 | InitRef, /*BasePath=*/nullptr, |
| 3075 | VK_RValue)); |
| 3076 | Data.FirstprivateVars.emplace_back(OrigRef); |
| 3077 | Data.FirstprivateCopies.emplace_back(PrivateRef); |
| 3078 | Data.FirstprivateInits.emplace_back(InitRef); |
| 3079 | return OrigVD; |
| 3080 | } |
| 3081 | |
| 3082 | void CodeGenFunction::EmitOMPTargetTaskBasedDirective( |
| 3083 | const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, |
| 3084 | OMPTargetDataInfo &InputInfo) { |
| 3085 | // Emit outlined function for task construct. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3086 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
| 3087 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3088 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3089 | auto I = CS->getCapturedDecl()->param_begin(); |
| 3090 | auto PartId = std::next(I); |
| 3091 | auto TaskT = std::next(I, 4); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3092 | OMPTaskDataTy Data; |
| 3093 | // The task is not final. |
| 3094 | Data.Final.setInt(/*IntVal=*/false); |
| 3095 | // Get list of firstprivate variables. |
| 3096 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 3097 | auto IRef = C->varlist_begin(); |
| 3098 | auto IElemInitRef = C->inits().begin(); |
| 3099 | for (auto *IInit : C->private_copies()) { |
| 3100 | Data.FirstprivateVars.push_back(*IRef); |
| 3101 | Data.FirstprivateCopies.push_back(IInit); |
| 3102 | Data.FirstprivateInits.push_back(*IElemInitRef); |
| 3103 | ++IRef; |
| 3104 | ++IElemInitRef; |
| 3105 | } |
| 3106 | } |
| 3107 | OMPPrivateScope TargetScope(*this); |
| 3108 | VarDecl *BPVD = nullptr; |
| 3109 | VarDecl *PVD = nullptr; |
| 3110 | VarDecl *SVD = nullptr; |
| 3111 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3112 | auto *CD = CapturedDecl::Create( |
| 3113 | getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); |
| 3114 | llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); |
| 3115 | QualType BaseAndPointersType = getContext().getConstantArrayType( |
| 3116 | getContext().VoidPtrTy, ArrSize, ArrayType::Normal, |
| 3117 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3118 | BPVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3119 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3120 | PVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3121 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3122 | QualType SizesType = getContext().getConstantArrayType( |
| 3123 | getContext().getSizeType(), ArrSize, ArrayType::Normal, |
| 3124 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3125 | SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3126 | S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3127 | TargetScope.addPrivate( |
| 3128 | BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); |
| 3129 | TargetScope.addPrivate(PVD, |
| 3130 | [&InputInfo]() { return InputInfo.PointersArray; }); |
| 3131 | TargetScope.addPrivate(SVD, |
| 3132 | [&InputInfo]() { return InputInfo.SizesArray; }); |
| 3133 | } |
| 3134 | (void)TargetScope.Privatize(); |
| 3135 | // Build list of dependences. |
| 3136 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3137 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3138 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3139 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, |
| 3140 | &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3141 | // Set proper addresses for generated private copies. |
| 3142 | OMPPrivateScope Scope(CGF); |
| 3143 | if (!Data.FirstprivateVars.empty()) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3144 | llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( |
| 3145 | CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3146 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3147 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 3148 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 3149 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 3150 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3151 | // Map privates. |
| 3152 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3153 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3154 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3155 | for (const Expr *E : Data.FirstprivateVars) { |
| 3156 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3157 | Address PrivatePtr = |
| 3158 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3159 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3160 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3161 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3162 | } |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3163 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 3164 | CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3165 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3166 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3167 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3168 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3169 | } |
| 3170 | } |
| 3171 | // Privatize all private variables except for in_reduction items. |
| 3172 | (void)Scope.Privatize(); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3173 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3174 | InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3175 | CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3176 | InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3177 | CGF.GetAddrOfLocalVar(PVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3178 | InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3179 | CGF.GetAddrOfLocalVar(SVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3180 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3181 | |
| 3182 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3183 | OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3184 | BodyGen(CGF); |
| 3185 | }; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3186 | llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3187 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, |
| 3188 | Data.NumberOfParts); |
| 3189 | llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); |
| 3190 | IntegerLiteral IfCond(getContext(), TrueOrFalse, |
| 3191 | getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3192 | SourceLocation()); |
| 3193 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3194 | CGM.getOpenMPRuntime().emitTaskCall(*this, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3195 | SharedsTy, CapturedStruct, &IfCond, Data); |
| 3196 | } |
| 3197 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3198 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 3199 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3200 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3201 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3202 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3203 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 3204 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3205 | if (C->getNameModifier() == OMPD_unknown || |
| 3206 | C->getNameModifier() == OMPD_task) { |
| 3207 | IfCond = C->getCondition(); |
| 3208 | break; |
| 3209 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3210 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3211 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3212 | OMPTaskDataTy Data; |
| 3213 | // Check if we should emit tied or untied task. |
| 3214 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3215 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3216 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3217 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3218 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3219 | IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3220 | const OMPTaskDataTy &Data) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3221 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3222 | SharedsTy, CapturedStruct, IfCond, |
| 3223 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3224 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3225 | EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3228 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 3229 | const OMPTaskyieldDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3230 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getBeginLoc()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3233 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3234 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3237 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3238 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getBeginLoc()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3239 | } |
| 3240 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3241 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 3242 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3243 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3244 | Action.Enter(CGF); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3245 | if (const Expr *E = S.getReductionRef()) { |
| 3246 | SmallVector<const Expr *, 4> LHSs; |
| 3247 | SmallVector<const Expr *, 4> RHSs; |
| 3248 | OMPTaskDataTy Data; |
| 3249 | for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { |
| 3250 | auto IPriv = C->privates().begin(); |
| 3251 | auto IRed = C->reduction_ops().begin(); |
| 3252 | auto ILHS = C->lhs_exprs().begin(); |
| 3253 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3254 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3255 | Data.ReductionVars.emplace_back(Ref); |
| 3256 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3257 | Data.ReductionOps.emplace_back(*IRed); |
| 3258 | LHSs.emplace_back(*ILHS); |
| 3259 | RHSs.emplace_back(*IRHS); |
| 3260 | std::advance(IPriv, 1); |
| 3261 | std::advance(IRed, 1); |
| 3262 | std::advance(ILHS, 1); |
| 3263 | std::advance(IRHS, 1); |
| 3264 | } |
| 3265 | } |
| 3266 | llvm::Value *ReductionDesc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3267 | CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getBeginLoc(), |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3268 | LHSs, RHSs, Data); |
| 3269 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3270 | CGF.EmitVarDecl(*VD); |
| 3271 | CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), |
| 3272 | /*Volatile=*/false, E->getType()); |
| 3273 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3274 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3275 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3276 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3277 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3280 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3281 | CGM.getOpenMPRuntime().emitFlush( |
| 3282 | *this, |
| 3283 | [&S]() -> ArrayRef<const Expr *> { |
| 3284 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) |
| 3285 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 3286 | FlushClause->varlist_end()); |
| 3287 | return llvm::None; |
| 3288 | }(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3289 | S.getBeginLoc()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3292 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, |
| 3293 | const CodeGenLoopTy &CodeGenLoop, |
| 3294 | Expr *IncExpr) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3295 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3296 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 3297 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3298 | EmitVarDecl(*IVDecl); |
| 3299 | |
| 3300 | // Emit the iterations count variable. |
| 3301 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 3302 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3303 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3304 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3305 | // Emit calculation of the iterations count. |
| 3306 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3307 | } |
| 3308 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3309 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3310 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3311 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3312 | // Check pre-condition. |
| 3313 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3314 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3315 | // Skip the entire loop if we don't meet the precondition. |
| 3316 | // If the condition constant folds and can be elided, avoid emitting the |
| 3317 | // whole loop. |
| 3318 | bool CondConstant; |
| 3319 | llvm::BasicBlock *ContBlock = nullptr; |
| 3320 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3321 | if (!CondConstant) |
| 3322 | return; |
| 3323 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3324 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3325 | ContBlock = createBasicBlock("omp.precond.end"); |
| 3326 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3327 | getProfileCount(&S)); |
| 3328 | EmitBlock(ThenBlock); |
| 3329 | incrementProfileCounter(&S); |
| 3330 | } |
| 3331 | |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3332 | emitAlignedClause(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3333 | // Emit 'then' code. |
| 3334 | { |
| 3335 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3336 | |
| 3337 | LValue LB = EmitOMPHelperVar( |
| 3338 | *this, cast<DeclRefExpr>( |
| 3339 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3340 | ? S.getCombinedLowerBoundVariable() |
| 3341 | : S.getLowerBoundVariable()))); |
| 3342 | LValue UB = EmitOMPHelperVar( |
| 3343 | *this, cast<DeclRefExpr>( |
| 3344 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3345 | ? S.getCombinedUpperBoundVariable() |
| 3346 | : S.getUpperBoundVariable()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3347 | LValue ST = |
| 3348 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 3349 | LValue IL = |
| 3350 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 3351 | |
| 3352 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3353 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3354 | // Emit implicit barrier to synchronize threads and avoid data races |
| 3355 | // on initialization of firstprivate variables and post-update of |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3356 | // lastprivate variables. |
| 3357 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3358 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3359 | /*ForceSimpleCall=*/true); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3360 | } |
| 3361 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3362 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 3363 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3364 | !isOpenMPTeamsDirective(S.getDirectiveKind())) |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3365 | EmitOMPReductionClauseInit(S, LoopScope); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3366 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3367 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3368 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 3369 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 3370 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3371 | |
| 3372 | // Detect the distribute schedule kind and chunk. |
| 3373 | llvm::Value *Chunk = nullptr; |
| 3374 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3375 | if (const auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3376 | ScheduleKind = C->getDistScheduleKind(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3377 | if (const Expr *Ch = C->getChunkSize()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3378 | Chunk = EmitScalarExpr(Ch); |
| 3379 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3380 | S.getIterationVariable()->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3381 | S.getBeginLoc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3382 | } |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 3383 | } else { |
| 3384 | // Default behaviour for dist_schedule clause. |
| 3385 | CGM.getOpenMPRuntime().getDefaultDistScheduleAndChunk( |
| 3386 | *this, S, ScheduleKind, Chunk); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3387 | } |
| 3388 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 3389 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 3390 | |
| 3391 | // OpenMP [2.10.8, distribute Construct, Description] |
| 3392 | // If dist_schedule is specified, kind must be static. If specified, |
| 3393 | // iterations are divided into chunks of size chunk_size, chunks are |
| 3394 | // assigned to the teams of the league in a round-robin fashion in the |
| 3395 | // order of the team number. When no chunk_size is specified, the |
| 3396 | // iteration space is divided into chunks that are approximately equal |
| 3397 | // in size, and at most one chunk is distributed to each team of the |
| 3398 | // league. The size of the chunks is unspecified in this case. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3399 | bool StaticChunked = RT.isStaticChunked( |
| 3400 | ScheduleKind, /* Chunked */ Chunk != nullptr) && |
| 3401 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3402 | if (RT.isStaticNonchunked(ScheduleKind, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3403 | /* Chunked */ Chunk != nullptr) || |
| 3404 | StaticChunked) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3405 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 3406 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3407 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 3408 | IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3409 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 3410 | StaticChunked ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3411 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3412 | StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3413 | JumpDest LoopExit = |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3414 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 3415 | // UB = min(UB, GlobalUB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3416 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3417 | ? S.getCombinedEnsureUpperBound() |
| 3418 | : S.getEnsureUpperBound()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3419 | // IV = LB; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3420 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3421 | ? S.getCombinedInit() |
| 3422 | : S.getInit()); |
| 3423 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3424 | const Expr *Cond = |
| 3425 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3426 | ? S.getCombinedCond() |
| 3427 | : S.getCond(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3428 | |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3429 | if (StaticChunked) |
| 3430 | Cond = S.getCombinedDistCond(); |
| 3431 | |
| 3432 | // For static unchunked schedules generate: |
| 3433 | // |
| 3434 | // 1. For distribute alone, codegen |
| 3435 | // while (idx <= UB) { |
| 3436 | // BODY; |
| 3437 | // ++idx; |
| 3438 | // } |
| 3439 | // |
| 3440 | // 2. When combined with 'for' (e.g. as in 'distribute parallel for') |
| 3441 | // while (idx <= UB) { |
| 3442 | // <CodeGen rest of pragma>(LB, UB); |
| 3443 | // idx += ST; |
| 3444 | // } |
| 3445 | // |
| 3446 | // For static chunk one schedule generate: |
| 3447 | // |
| 3448 | // while (IV <= GlobalUB) { |
| 3449 | // <CodeGen rest of pragma>(LB, UB); |
| 3450 | // LB += ST; |
| 3451 | // UB += ST; |
| 3452 | // UB = min(UB, GlobalUB); |
| 3453 | // IV = LB; |
| 3454 | // } |
| 3455 | // |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3456 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr, |
| 3457 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 3458 | CodeGenLoop(CGF, S, LoopExit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3459 | }, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3460 | [&S, StaticChunked](CodeGenFunction &CGF) { |
| 3461 | if (StaticChunked) { |
| 3462 | CGF.EmitIgnoredExpr(S.getCombinedNextLowerBound()); |
| 3463 | CGF.EmitIgnoredExpr(S.getCombinedNextUpperBound()); |
| 3464 | CGF.EmitIgnoredExpr(S.getCombinedEnsureUpperBound()); |
| 3465 | CGF.EmitIgnoredExpr(S.getCombinedInit()); |
| 3466 | } |
| 3467 | }); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3468 | EmitBlock(LoopExit.getBlock()); |
| 3469 | // Tell the runtime we are done. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3470 | RT.emitForStaticFinish(*this, S.getBeginLoc(), S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3471 | } else { |
| 3472 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 3473 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3474 | const OMPLoopArguments LoopArguments = { |
| 3475 | LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(), |
| 3476 | Chunk}; |
| 3477 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, |
| 3478 | CodeGenLoop); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3479 | } |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3480 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3481 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3482 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3483 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3484 | }); |
| 3485 | } |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3486 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 3487 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3488 | !isOpenMPTeamsDirective(S.getDirectiveKind())) { |
Jonas Hahnfeld | 5aaaece | 2018-10-02 19:12:47 +0000 | [diff] [blame] | 3489 | EmitOMPReductionClauseFinal(S, OMPD_simd); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3490 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 3491 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3492 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3493 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3494 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3495 | }); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3496 | } |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3497 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3498 | if (HasLastprivateClause) { |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3499 | EmitOMPLastprivateClauseFinal( |
| 3500 | S, /*NoFinals=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3501 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3502 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3503 | } |
| 3504 | |
| 3505 | // We're now done with the loop, so jump to the continuation block. |
| 3506 | if (ContBlock) { |
| 3507 | EmitBranch(ContBlock); |
| 3508 | EmitBlock(ContBlock, true); |
| 3509 | } |
| 3510 | } |
| 3511 | } |
| 3512 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3513 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 3514 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3515 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3516 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3517 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3518 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 3519 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3522 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 3523 | const CapturedStmt *S) { |
| 3524 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 3525 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 3526 | CGF.CapturedStmtInfo = &CapStmtInfo; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3527 | llvm::Function *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3528 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3529 | return Fn; |
| 3530 | } |
| 3531 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3532 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3533 | if (S.hasClausesOfKind<OMPDependClause>()) { |
| 3534 | assert(!S.getAssociatedStmt() && |
| 3535 | "No associated statement must be in ordered depend construct."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3536 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 3537 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3538 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3539 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3540 | const auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3541 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 3542 | PrePostActionTy &Action) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3543 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3544 | if (C) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3545 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3546 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3547 | llvm::Function *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3548 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3549 | OutlinedFn, CapturedVars); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3550 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3551 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3552 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3553 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3554 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3555 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3556 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getBeginLoc(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 3557 | } |
| 3558 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3559 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3560 | QualType SrcType, QualType DestType, |
| 3561 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3562 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 3563 | "DestType must have scalar evaluation kind."); |
| 3564 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3565 | return Val.isScalar() ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 3566 | DestType, Loc) |
| 3567 | : CGF.EmitComplexToScalarConversion( |
| 3568 | Val.getComplexVal(), SrcType, DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | static CodeGenFunction::ComplexPairTy |
| 3572 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3573 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3574 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 3575 | "DestType must have complex evaluation kind."); |
| 3576 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 3577 | if (Val.isScalar()) { |
| 3578 | // Convert the input element to the element type of the complex. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3579 | QualType DestElementType = |
| 3580 | DestType->castAs<ComplexType>()->getElementType(); |
| 3581 | llvm::Value *ScalarVal = CGF.EmitScalarConversion( |
| 3582 | Val.getScalarVal(), SrcType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3583 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 3584 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 3585 | } else { |
| 3586 | assert(Val.isComplex() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3587 | QualType SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 3588 | QualType DestElementType = |
| 3589 | DestType->castAs<ComplexType>()->getElementType(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3590 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3591 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3592 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3593 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3594 | } |
| 3595 | return ComplexVal; |
| 3596 | } |
| 3597 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3598 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 3599 | LValue LVal, RValue RVal) { |
| 3600 | if (LVal.isGlobalReg()) { |
| 3601 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 3602 | } else { |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3603 | CGF.EmitAtomicStore(RVal, LVal, |
| 3604 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3605 | : llvm::AtomicOrdering::Monotonic, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3606 | LVal.isVolatile(), /*IsInit=*/false); |
| 3607 | } |
| 3608 | } |
| 3609 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3610 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 3611 | QualType RValTy, SourceLocation Loc) { |
| 3612 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3613 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3614 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 3615 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 3616 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3617 | break; |
| 3618 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3619 | EmitStoreOfComplex( |
| 3620 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3621 | /*isInit=*/false); |
| 3622 | break; |
| 3623 | case TEK_Aggregate: |
| 3624 | llvm_unreachable("Must be a scalar or complex."); |
| 3625 | } |
| 3626 | } |
| 3627 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3628 | static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3629 | const Expr *X, const Expr *V, |
| 3630 | SourceLocation Loc) { |
| 3631 | // v = x; |
| 3632 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 3633 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 3634 | LValue XLValue = CGF.EmitLValue(X); |
| 3635 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 3636 | RValue Res = XLValue.isGlobalReg() |
| 3637 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3638 | : CGF.EmitAtomicLoad( |
| 3639 | XLValue, Loc, |
| 3640 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3641 | : llvm::AtomicOrdering::Monotonic, |
| 3642 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3643 | // OpenMP, 2.12.6, atomic Construct |
| 3644 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3645 | // performed operation to include an implicit flush operation without a |
| 3646 | // list. |
| 3647 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3648 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3649 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3652 | static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3653 | const Expr *X, const Expr *E, |
| 3654 | SourceLocation Loc) { |
| 3655 | // x = expr; |
| 3656 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3657 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3658 | // OpenMP, 2.12.6, atomic Construct |
| 3659 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3660 | // performed operation to include an implicit flush operation without a |
| 3661 | // list. |
| 3662 | if (IsSeqCst) |
| 3663 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3664 | } |
| 3665 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3666 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 3667 | RValue Update, |
| 3668 | BinaryOperatorKind BO, |
| 3669 | llvm::AtomicOrdering AO, |
| 3670 | bool IsXLHSInRHSPart) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3671 | ASTContext &Context = CGF.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3672 | // 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] | 3673 | // expression is simple and atomic is allowed for the given type for the |
| 3674 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3675 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 3676 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 3677 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 3678 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3679 | X.getAddress().getElementType())) || |
| 3680 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3681 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 3682 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3683 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3684 | |
| 3685 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 3686 | switch (BO) { |
| 3687 | case BO_Add: |
| 3688 | RMWOp = llvm::AtomicRMWInst::Add; |
| 3689 | break; |
| 3690 | case BO_Sub: |
| 3691 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3692 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3693 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 3694 | break; |
| 3695 | case BO_And: |
| 3696 | RMWOp = llvm::AtomicRMWInst::And; |
| 3697 | break; |
| 3698 | case BO_Or: |
| 3699 | RMWOp = llvm::AtomicRMWInst::Or; |
| 3700 | break; |
| 3701 | case BO_Xor: |
| 3702 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 3703 | break; |
| 3704 | case BO_LT: |
| 3705 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3706 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 3707 | : llvm::AtomicRMWInst::Max) |
| 3708 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 3709 | : llvm::AtomicRMWInst::UMax); |
| 3710 | break; |
| 3711 | case BO_GT: |
| 3712 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3713 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 3714 | : llvm::AtomicRMWInst::Min) |
| 3715 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 3716 | : llvm::AtomicRMWInst::UMin); |
| 3717 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3718 | case BO_Assign: |
| 3719 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 3720 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3721 | case BO_Mul: |
| 3722 | case BO_Div: |
| 3723 | case BO_Rem: |
| 3724 | case BO_Shl: |
| 3725 | case BO_Shr: |
| 3726 | case BO_LAnd: |
| 3727 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3728 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3729 | case BO_PtrMemD: |
| 3730 | case BO_PtrMemI: |
| 3731 | case BO_LE: |
| 3732 | case BO_GE: |
| 3733 | case BO_EQ: |
| 3734 | case BO_NE: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 3735 | case BO_Cmp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3736 | case BO_AddAssign: |
| 3737 | case BO_SubAssign: |
| 3738 | case BO_AndAssign: |
| 3739 | case BO_OrAssign: |
| 3740 | case BO_XorAssign: |
| 3741 | case BO_MulAssign: |
| 3742 | case BO_DivAssign: |
| 3743 | case BO_RemAssign: |
| 3744 | case BO_ShlAssign: |
| 3745 | case BO_ShrAssign: |
| 3746 | case BO_Comma: |
| 3747 | llvm_unreachable("Unsupported atomic update operation"); |
| 3748 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3749 | llvm::Value *UpdateVal = Update.getScalarVal(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3750 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 3751 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3752 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3753 | X.getType()->hasSignedIntegerRepresentation()); |
| 3754 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3755 | llvm::Value *Res = |
| 3756 | CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3757 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3760 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3761 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 3762 | llvm::AtomicOrdering AO, SourceLocation Loc, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3763 | const llvm::function_ref<RValue(RValue)> CommonGen) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3764 | // Update expressions are allowed to have the following forms: |
| 3765 | // x binop= expr; -> xrval + expr; |
| 3766 | // x++, ++x -> xrval + 1; |
| 3767 | // x--, --x -> xrval - 1; |
| 3768 | // x = x binop expr; -> xrval binop expr |
| 3769 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3770 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 3771 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3772 | if (X.isGlobalReg()) { |
| 3773 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 3774 | // 'xrval'. |
| 3775 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 3776 | } else { |
| 3777 | // Perform compare-and-swap procedure. |
| 3778 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3779 | } |
| 3780 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3781 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3782 | } |
| 3783 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3784 | static void emitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3785 | const Expr *X, const Expr *E, |
| 3786 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3787 | SourceLocation Loc) { |
| 3788 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3789 | "Update expr in 'atomic update' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3790 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3791 | // Update expressions are allowed to have the following forms: |
| 3792 | // x binop= expr; -> xrval + expr; |
| 3793 | // x++, ++x -> xrval + 1; |
| 3794 | // x--, --x -> xrval - 1; |
| 3795 | // x = x binop expr; -> xrval binop expr |
| 3796 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3797 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3798 | LValue XLValue = CGF.EmitLValue(X); |
| 3799 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3800 | llvm::AtomicOrdering AO = IsSeqCst |
| 3801 | ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3802 | : llvm::AtomicOrdering::Monotonic; |
| 3803 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3804 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3805 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3806 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3807 | auto &&Gen = [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) { |
| 3808 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3809 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3810 | return CGF.EmitAnyExpr(UE); |
| 3811 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3812 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3813 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3814 | // OpenMP, 2.12.6, atomic Construct |
| 3815 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3816 | // performed operation to include an implicit flush operation without a |
| 3817 | // list. |
| 3818 | if (IsSeqCst) |
| 3819 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3820 | } |
| 3821 | |
| 3822 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3823 | QualType SourceType, QualType ResType, |
| 3824 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3825 | switch (CGF.getEvaluationKind(ResType)) { |
| 3826 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3827 | return RValue::get( |
| 3828 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3829 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3830 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3831 | return RValue::getComplex(Res.first, Res.second); |
| 3832 | } |
| 3833 | case TEK_Aggregate: |
| 3834 | break; |
| 3835 | } |
| 3836 | llvm_unreachable("Must be a scalar or complex."); |
| 3837 | } |
| 3838 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3839 | static void emitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3840 | bool IsPostfixUpdate, const Expr *V, |
| 3841 | const Expr *X, const Expr *E, |
| 3842 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3843 | SourceLocation Loc) { |
| 3844 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 3845 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 3846 | RValue NewVVal; |
| 3847 | LValue VLValue = CGF.EmitLValue(V); |
| 3848 | LValue XLValue = CGF.EmitLValue(X); |
| 3849 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3850 | llvm::AtomicOrdering AO = IsSeqCst |
| 3851 | ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3852 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3853 | QualType NewVValType; |
| 3854 | if (UE) { |
| 3855 | // 'x' is updated with some additional value. |
| 3856 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3857 | "Update expr in 'atomic capture' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3858 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3859 | // Update expressions are allowed to have the following forms: |
| 3860 | // x binop= expr; -> xrval + expr; |
| 3861 | // x++, ++x -> xrval + 1; |
| 3862 | // x--, --x -> xrval - 1; |
| 3863 | // x = x binop expr; -> xrval binop expr |
| 3864 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3865 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3866 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3867 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3868 | NewVValType = XRValExpr->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3869 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3870 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3871 | IsPostfixUpdate](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3872 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3873 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3874 | RValue Res = CGF.EmitAnyExpr(UE); |
| 3875 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 3876 | return Res; |
| 3877 | }; |
| 3878 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3879 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3880 | if (Res.first) { |
| 3881 | // 'atomicrmw' instruction was generated. |
| 3882 | if (IsPostfixUpdate) { |
| 3883 | // Use old value from 'atomicrmw'. |
| 3884 | NewVVal = Res.second; |
| 3885 | } else { |
| 3886 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 3887 | // value of 'x'. |
| 3888 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3889 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 3890 | NewVVal = CGF.EmitAnyExpr(UE); |
| 3891 | } |
| 3892 | } |
| 3893 | } else { |
| 3894 | // 'x' is simply rewritten with some 'expr'. |
| 3895 | NewVValType = X->getType().getNonReferenceType(); |
| 3896 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3897 | X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3898 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3899 | NewVVal = XRValue; |
| 3900 | return ExprRValue; |
| 3901 | }; |
| 3902 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 3903 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3904 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 3905 | Loc, Gen); |
| 3906 | if (Res.first) { |
| 3907 | // 'atomicrmw' instruction was generated. |
| 3908 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 3909 | } |
| 3910 | } |
| 3911 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3912 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3913 | // OpenMP, 2.12.6, atomic Construct |
| 3914 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3915 | // performed operation to include an implicit flush operation without a |
| 3916 | // list. |
| 3917 | if (IsSeqCst) |
| 3918 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3919 | } |
| 3920 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3921 | static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3922 | bool IsSeqCst, bool IsPostfixUpdate, |
| 3923 | const Expr *X, const Expr *V, const Expr *E, |
| 3924 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3925 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3926 | switch (Kind) { |
| 3927 | case OMPC_read: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3928 | emitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3929 | break; |
| 3930 | case OMPC_write: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3931 | emitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3932 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3933 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3934 | case OMPC_update: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3935 | emitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3936 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3937 | case OMPC_capture: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3938 | emitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3939 | IsXLHSInRHSPart, Loc); |
| 3940 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3941 | case OMPC_if: |
| 3942 | case OMPC_final: |
| 3943 | case OMPC_num_threads: |
| 3944 | case OMPC_private: |
| 3945 | case OMPC_firstprivate: |
| 3946 | case OMPC_lastprivate: |
| 3947 | case OMPC_reduction: |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 3948 | case OMPC_task_reduction: |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 3949 | case OMPC_in_reduction: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3950 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 3951 | case OMPC_simdlen: |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 3952 | case OMPC_allocator: |
Alexey Bataev | e04483e | 2019-03-27 14:14:31 +0000 | [diff] [blame^] | 3953 | case OMPC_allocate: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3954 | case OMPC_collapse: |
| 3955 | case OMPC_default: |
| 3956 | case OMPC_seq_cst: |
| 3957 | case OMPC_shared: |
| 3958 | case OMPC_linear: |
| 3959 | case OMPC_aligned: |
| 3960 | case OMPC_copyin: |
| 3961 | case OMPC_copyprivate: |
| 3962 | case OMPC_flush: |
| 3963 | case OMPC_proc_bind: |
| 3964 | case OMPC_schedule: |
| 3965 | case OMPC_ordered: |
| 3966 | case OMPC_nowait: |
| 3967 | case OMPC_untied: |
| 3968 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 3969 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3970 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 3971 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 3972 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 3973 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 3974 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 3975 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 3976 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 3977 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 3978 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 3979 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 3980 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 3981 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 3982 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 3983 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 3984 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 3985 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 3986 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 3987 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 3988 | case OMPC_is_device_ptr: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 3989 | case OMPC_unified_address: |
Alexey Bataev | 94c5064 | 2018-10-01 14:26:31 +0000 | [diff] [blame] | 3990 | case OMPC_unified_shared_memory: |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 3991 | case OMPC_reverse_offload: |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 3992 | case OMPC_dynamic_allocators: |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 3993 | case OMPC_atomic_default_mem_order: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3994 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3999 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4000 | OpenMPClauseKind Kind = OMPC_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4001 | for (const OMPClause *C : S.clauses()) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4002 | // Find first clause (skip seq_cst clause, if it is first). |
| 4003 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 4004 | Kind = C->getClauseKind(); |
| 4005 | break; |
| 4006 | } |
| 4007 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 4008 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4009 | const Stmt *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 4010 | if (const auto *FE = dyn_cast<FullExpr>(CS)) |
| 4011 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4012 | // Processing for statements under 'atomic capture'. |
| 4013 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4014 | for (const Stmt *C : Compound->body()) { |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 4015 | if (const auto *FE = dyn_cast<FullExpr>(C)) |
| 4016 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4017 | } |
| 4018 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 4019 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4020 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, |
| 4021 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 4022 | CGF.EmitStopPoint(CS); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4023 | emitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4024 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4025 | S.isXLHSInRHSPart(), S.getBeginLoc()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 4026 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4027 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4028 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4029 | } |
| 4030 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4031 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 4032 | const OMPExecutableDirective &S, |
| 4033 | const RegionCodeGenTy &CodeGen) { |
| 4034 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 4035 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4036 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4037 | // On device emit this construct as inlined code. |
| 4038 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 4039 | OMPLexicalScope Scope(CGF, S, OMPD_target); |
| 4040 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4041 | CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 4ac68a2 | 2018-05-16 15:08:32 +0000 | [diff] [blame] | 4042 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4043 | }); |
| 4044 | return; |
| 4045 | } |
| 4046 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4047 | llvm::Function *Fn = nullptr; |
| 4048 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4049 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4050 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 4051 | // Check for the at most one if clause associated with the target region. |
| 4052 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4053 | if (C->getNameModifier() == OMPD_unknown || |
| 4054 | C->getNameModifier() == OMPD_target) { |
| 4055 | IfCond = C->getCondition(); |
| 4056 | break; |
| 4057 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | // Check if we have any device clause associated with the directive. |
| 4061 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4062 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4063 | Device = C->getDevice(); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4064 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4065 | // Check if we have an if clause whose conditional always evaluates to false |
| 4066 | // or if we do not have any targets specified. If so the target region is not |
| 4067 | // an offload entry point. |
| 4068 | bool IsOffloadEntry = true; |
| 4069 | if (IfCond) { |
| 4070 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4071 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4072 | IsOffloadEntry = false; |
| 4073 | } |
| 4074 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4075 | IsOffloadEntry = false; |
| 4076 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4077 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4078 | StringRef ParentName; |
| 4079 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 4080 | // the mangling of the device outlined kernel. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4081 | if (const auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4082 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4083 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4084 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 4085 | else |
| 4086 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4087 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4088 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4089 | // Emit target region as a standalone region. |
| 4090 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 4091 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4092 | OMPLexicalScope Scope(CGF, S, OMPD_task); |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 4093 | auto &&SizeEmitter = [](CodeGenFunction &CGF, const OMPLoopDirective &D) { |
| 4094 | OMPLoopScope(CGF, D); |
| 4095 | // Emit calculation of the iterations count. |
| 4096 | llvm::Value *NumIterations = CGF.EmitScalarExpr(D.getNumIterations()); |
| 4097 | NumIterations = CGF.Builder.CreateIntCast(NumIterations, CGF.Int64Ty, |
| 4098 | /*IsSigned=*/false); |
| 4099 | return NumIterations; |
| 4100 | }; |
Alexey Bataev | 4920e1a | 2019-01-30 20:49:52 +0000 | [diff] [blame] | 4101 | if (IsOffloadEntry) |
| 4102 | CGM.getOpenMPRuntime().emitTargetNumIterationsCall(CGF, S, Device, |
| 4103 | SizeEmitter); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4104 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4105 | } |
| 4106 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4107 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 4108 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4109 | Action.Enter(CGF); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4110 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4111 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4112 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4113 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4114 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4115 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4116 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4117 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 4121 | StringRef ParentName, |
| 4122 | const OMPTargetDirective &S) { |
| 4123 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4124 | emitTargetRegion(CGF, S, Action); |
| 4125 | }; |
| 4126 | llvm::Function *Fn; |
| 4127 | llvm::Constant *Addr; |
| 4128 | // Emit target region as a standalone region. |
| 4129 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4130 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4131 | assert(Fn && Addr && "Target device function emission failed."); |
| 4132 | } |
| 4133 | |
| 4134 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 4135 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4136 | emitTargetRegion(CGF, S, Action); |
| 4137 | }; |
| 4138 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4139 | } |
| 4140 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4141 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 4142 | const OMPExecutableDirective &S, |
| 4143 | OpenMPDirectiveKind InnermostKind, |
| 4144 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4145 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4146 | llvm::Function *OutlinedFn = |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4147 | CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 4148 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 4149 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4150 | const auto *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 4151 | const auto *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4152 | if (NT || TL) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4153 | const Expr *NumTeams = NT ? NT->getNumTeams() : nullptr; |
| 4154 | const Expr *ThreadLimit = TL ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4155 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4156 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4157 | S.getBeginLoc()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4158 | } |
| 4159 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4160 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4161 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4162 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4163 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getBeginLoc(), OutlinedFn, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4164 | CapturedVars); |
| 4165 | } |
| 4166 | |
| 4167 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 4168 | // Emit teams region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4169 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4170 | Action.Enter(CGF); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4171 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 4172 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4173 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4174 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4175 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4176 | CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4177 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4178 | }; |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4179 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4180 | emitPostUpdateForReductionClause(*this, S, |
| 4181 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4182 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4183 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4184 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4185 | const OMPTargetTeamsDirective &S) { |
| 4186 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 4187 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4188 | // Emit teams region as a standalone region. |
| 4189 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4190 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4191 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4192 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4193 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4194 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4195 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4196 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4197 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4198 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4199 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4200 | }; |
| 4201 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4202 | emitPostUpdateForReductionClause(CGF, S, |
| 4203 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4204 | } |
| 4205 | |
| 4206 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 4207 | CodeGenModule &CGM, StringRef ParentName, |
| 4208 | const OMPTargetTeamsDirective &S) { |
| 4209 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4210 | emitTargetTeamsRegion(CGF, Action, S); |
| 4211 | }; |
| 4212 | llvm::Function *Fn; |
| 4213 | llvm::Constant *Addr; |
| 4214 | // Emit target region as a standalone region. |
| 4215 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4216 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4217 | assert(Fn && Addr && "Target device function emission failed."); |
| 4218 | } |
| 4219 | |
| 4220 | void CodeGenFunction::EmitOMPTargetTeamsDirective( |
| 4221 | const OMPTargetTeamsDirective &S) { |
| 4222 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4223 | emitTargetTeamsRegion(CGF, Action, S); |
| 4224 | }; |
| 4225 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4226 | } |
| 4227 | |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4228 | static void |
| 4229 | emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4230 | const OMPTargetTeamsDistributeDirective &S) { |
| 4231 | Action.Enter(CGF); |
| 4232 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4233 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4234 | }; |
| 4235 | |
| 4236 | // Emit teams region as a standalone region. |
| 4237 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4238 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4239 | Action.Enter(CGF); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4240 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4241 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4242 | (void)PrivateScope.Privatize(); |
| 4243 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4244 | CodeGenDistribute); |
| 4245 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4246 | }; |
| 4247 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); |
| 4248 | emitPostUpdateForReductionClause(CGF, S, |
| 4249 | [](CodeGenFunction &) { return nullptr; }); |
| 4250 | } |
| 4251 | |
| 4252 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| 4253 | CodeGenModule &CGM, StringRef ParentName, |
| 4254 | const OMPTargetTeamsDistributeDirective &S) { |
| 4255 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4256 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4257 | }; |
| 4258 | llvm::Function *Fn; |
| 4259 | llvm::Constant *Addr; |
| 4260 | // Emit target region as a standalone region. |
| 4261 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4262 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4263 | assert(Fn && Addr && "Target device function emission failed."); |
| 4264 | } |
| 4265 | |
| 4266 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 4267 | const OMPTargetTeamsDistributeDirective &S) { |
| 4268 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4269 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4270 | }; |
| 4271 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4272 | } |
| 4273 | |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4274 | static void emitTargetTeamsDistributeSimdRegion( |
| 4275 | CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4276 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4277 | Action.Enter(CGF); |
| 4278 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4279 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4280 | }; |
| 4281 | |
| 4282 | // Emit teams region as a standalone region. |
| 4283 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4284 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4285 | Action.Enter(CGF); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4286 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4287 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4288 | (void)PrivateScope.Privatize(); |
| 4289 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4290 | CodeGenDistribute); |
| 4291 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4292 | }; |
| 4293 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); |
| 4294 | emitPostUpdateForReductionClause(CGF, S, |
| 4295 | [](CodeGenFunction &) { return nullptr; }); |
| 4296 | } |
| 4297 | |
| 4298 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| 4299 | CodeGenModule &CGM, StringRef ParentName, |
| 4300 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4301 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4302 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4303 | }; |
| 4304 | llvm::Function *Fn; |
| 4305 | llvm::Constant *Addr; |
| 4306 | // Emit target region as a standalone region. |
| 4307 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4308 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4309 | assert(Fn && Addr && "Target device function emission failed."); |
| 4310 | } |
| 4311 | |
| 4312 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 4313 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4314 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4315 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4316 | }; |
| 4317 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4318 | } |
| 4319 | |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4320 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 4321 | const OMPTeamsDistributeDirective &S) { |
| 4322 | |
| 4323 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4324 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4325 | }; |
| 4326 | |
| 4327 | // Emit teams region as a standalone region. |
| 4328 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4329 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4330 | Action.Enter(CGF); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4331 | OMPPrivateScope PrivateScope(CGF); |
| 4332 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4333 | (void)PrivateScope.Privatize(); |
| 4334 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4335 | CodeGenDistribute); |
| 4336 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4337 | }; |
Alexey Bataev | 95c6dd4 | 2017-11-29 15:14:16 +0000 | [diff] [blame] | 4338 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4339 | emitPostUpdateForReductionClause(*this, S, |
| 4340 | [](CodeGenFunction &) { return nullptr; }); |
| 4341 | } |
| 4342 | |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4343 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 4344 | const OMPTeamsDistributeSimdDirective &S) { |
| 4345 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4346 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4347 | }; |
| 4348 | |
| 4349 | // Emit teams region as a standalone region. |
| 4350 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4351 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4352 | Action.Enter(CGF); |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4353 | OMPPrivateScope PrivateScope(CGF); |
| 4354 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4355 | (void)PrivateScope.Privatize(); |
| 4356 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, |
| 4357 | CodeGenDistribute); |
| 4358 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4359 | }; |
| 4360 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); |
| 4361 | emitPostUpdateForReductionClause(*this, S, |
| 4362 | [](CodeGenFunction &) { return nullptr; }); |
| 4363 | } |
| 4364 | |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4365 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 4366 | const OMPTeamsDistributeParallelForDirective &S) { |
| 4367 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4368 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4369 | S.getDistInc()); |
| 4370 | }; |
| 4371 | |
| 4372 | // Emit teams region as a standalone region. |
| 4373 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4374 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4375 | Action.Enter(CGF); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4376 | OMPPrivateScope PrivateScope(CGF); |
| 4377 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4378 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 4379 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4380 | CodeGenDistribute); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4381 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4382 | }; |
| 4383 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4384 | emitPostUpdateForReductionClause(*this, S, |
| 4385 | [](CodeGenFunction &) { return nullptr; }); |
| 4386 | } |
| 4387 | |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4388 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 4389 | const OMPTeamsDistributeParallelForSimdDirective &S) { |
| 4390 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4391 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4392 | S.getDistInc()); |
| 4393 | }; |
| 4394 | |
| 4395 | // Emit teams region as a standalone region. |
| 4396 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4397 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4398 | Action.Enter(CGF); |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4399 | OMPPrivateScope PrivateScope(CGF); |
| 4400 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4401 | (void)PrivateScope.Privatize(); |
| 4402 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4403 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4404 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4405 | }; |
| 4406 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4407 | emitPostUpdateForReductionClause(*this, S, |
| 4408 | [](CodeGenFunction &) { return nullptr; }); |
| 4409 | } |
| 4410 | |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4411 | static void emitTargetTeamsDistributeParallelForRegion( |
| 4412 | CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, |
| 4413 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4414 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4415 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4416 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4417 | S.getDistInc()); |
| 4418 | }; |
| 4419 | |
| 4420 | // Emit teams region as a standalone region. |
| 4421 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4422 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4423 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4424 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4425 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4426 | (void)PrivateScope.Privatize(); |
| 4427 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4428 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4429 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4430 | }; |
| 4431 | |
| 4432 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, |
| 4433 | CodeGenTeams); |
| 4434 | emitPostUpdateForReductionClause(CGF, S, |
| 4435 | [](CodeGenFunction &) { return nullptr; }); |
| 4436 | } |
| 4437 | |
| 4438 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 4439 | CodeGenModule &CGM, StringRef ParentName, |
| 4440 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4441 | // Emit SPMD target teams distribute parallel for region as a standalone |
| 4442 | // region. |
| 4443 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4444 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4445 | }; |
| 4446 | llvm::Function *Fn; |
| 4447 | llvm::Constant *Addr; |
| 4448 | // Emit target region as a standalone region. |
| 4449 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4450 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4451 | assert(Fn && Addr && "Target device function emission failed."); |
| 4452 | } |
| 4453 | |
| 4454 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 4455 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4456 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4457 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4458 | }; |
| 4459 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4460 | } |
| 4461 | |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4462 | static void emitTargetTeamsDistributeParallelForSimdRegion( |
| 4463 | CodeGenFunction &CGF, |
| 4464 | const OMPTargetTeamsDistributeParallelForSimdDirective &S, |
| 4465 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4466 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4467 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4468 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4469 | S.getDistInc()); |
| 4470 | }; |
| 4471 | |
| 4472 | // Emit teams region as a standalone region. |
| 4473 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4474 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4475 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4476 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4477 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4478 | (void)PrivateScope.Privatize(); |
| 4479 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4480 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4481 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4482 | }; |
| 4483 | |
| 4484 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd, |
| 4485 | CodeGenTeams); |
| 4486 | emitPostUpdateForReductionClause(CGF, S, |
| 4487 | [](CodeGenFunction &) { return nullptr; }); |
| 4488 | } |
| 4489 | |
| 4490 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 4491 | CodeGenModule &CGM, StringRef ParentName, |
| 4492 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4493 | // Emit SPMD target teams distribute parallel for simd region as a standalone |
| 4494 | // region. |
| 4495 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4496 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4497 | }; |
| 4498 | llvm::Function *Fn; |
| 4499 | llvm::Constant *Addr; |
| 4500 | // Emit target region as a standalone region. |
| 4501 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4502 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4503 | assert(Fn && Addr && "Target device function emission failed."); |
| 4504 | } |
| 4505 | |
| 4506 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 4507 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4508 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4509 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4510 | }; |
| 4511 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4512 | } |
| 4513 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4514 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 4515 | const OMPCancellationPointDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4516 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getBeginLoc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 4517 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4520 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 4521 | const Expr *IfCond = nullptr; |
| 4522 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4523 | if (C->getNameModifier() == OMPD_unknown || |
| 4524 | C->getNameModifier() == OMPD_cancel) { |
| 4525 | IfCond = C->getCondition(); |
| 4526 | break; |
| 4527 | } |
| 4528 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4529 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 4530 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4533 | CodeGenFunction::JumpDest |
| 4534 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4535 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
| 4536 | Kind == OMPD_target_parallel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4537 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4538 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4539 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 4540 | Kind == OMPD_distribute_parallel_for || |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 4541 | Kind == OMPD_target_parallel_for || |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 4542 | Kind == OMPD_teams_distribute_parallel_for || |
| 4543 | Kind == OMPD_target_teams_distribute_parallel_for); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4544 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4545 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4546 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4547 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 4548 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 4549 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 4550 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 4551 | auto OrigVarIt = C.varlist_begin(); |
| 4552 | auto InitIt = C.inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4553 | for (const Expr *PvtVarIt : C.private_copies()) { |
| 4554 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 4555 | const auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 4556 | const auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4557 | |
| 4558 | // In order to identify the right initializer we need to match the |
| 4559 | // declaration used by the mapping logic. In some cases we may get |
| 4560 | // OMPCapturedExprDecl that refers to the original declaration. |
| 4561 | const ValueDecl *MatchingVD = OrigVD; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4562 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4563 | // OMPCapturedExprDecl are used to privative fields of the current |
| 4564 | // structure. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4565 | const auto *ME = cast<MemberExpr>(OED->getInit()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4566 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 4567 | "Base should be the current struct!"); |
| 4568 | MatchingVD = ME->getMemberDecl(); |
| 4569 | } |
| 4570 | |
| 4571 | // If we don't have information about the current list item, move on to |
| 4572 | // the next one. |
| 4573 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 4574 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 4575 | continue; |
| 4576 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4577 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, OrigVD, |
| 4578 | InitAddrIt, InitVD, |
| 4579 | PvtVD]() { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4580 | // Initialize the temporary initialization variable with the address we |
| 4581 | // get from the runtime library. We have to cast the source address |
| 4582 | // because it is always a void *. References are materialized in the |
| 4583 | // privatization scope, so the initialization here disregards the fact |
| 4584 | // the original variable is a reference. |
| 4585 | QualType AddrQTy = |
| 4586 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 4587 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 4588 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 4589 | setAddrOfLocalVar(InitVD, InitAddr); |
| 4590 | |
| 4591 | // Emit private declaration, it will be initialized by the value we |
| 4592 | // declaration we just added to the local declarations map. |
| 4593 | EmitDecl(*PvtVD); |
| 4594 | |
| 4595 | // The initialization variables reached its purpose in the emission |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4596 | // of the previous declaration, so we don't need it anymore. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4597 | LocalDeclMap.erase(InitVD); |
| 4598 | |
| 4599 | // Return the address of the private variable. |
| 4600 | return GetAddrOfLocalVar(PvtVD); |
| 4601 | }); |
| 4602 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 4603 | // Silence the warning about unused variable. |
| 4604 | (void)IsRegistered; |
| 4605 | |
| 4606 | ++OrigVarIt; |
| 4607 | ++InitIt; |
| 4608 | } |
| 4609 | } |
| 4610 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4611 | // Generate the instructions for '#pragma omp target data' directive. |
| 4612 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 4613 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4614 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 4615 | |
| 4616 | // Create a pre/post action to signal the privatization of the device pointer. |
| 4617 | // This action can be replaced by the OpenMP runtime code generation to |
| 4618 | // deactivate privatization. |
| 4619 | bool PrivatizeDevicePointers = false; |
| 4620 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 4621 | bool &PrivatizeDevicePointers; |
| 4622 | |
| 4623 | public: |
| 4624 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 4625 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 4626 | void Enter(CodeGenFunction &CGF) override { |
| 4627 | PrivatizeDevicePointers = true; |
| 4628 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4629 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4630 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 4631 | |
| 4632 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4633 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4634 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4635 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4636 | }; |
| 4637 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4638 | // Codegen that selects whether to generate the privatization code or not. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4639 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 4640 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 4641 | PrePostActionTy &Action) { |
| 4642 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 4643 | PrivatizeDevicePointers = false; |
| 4644 | |
| 4645 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 4646 | // needed. |
| 4647 | Action.Enter(CGF); |
| 4648 | |
| 4649 | if (PrivatizeDevicePointers) { |
| 4650 | OMPPrivateScope PrivateScope(CGF); |
| 4651 | // Emit all instances of the use_device_ptr clause. |
| 4652 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 4653 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 4654 | Info.CaptureDeviceAddrMap); |
| 4655 | (void)PrivateScope.Privatize(); |
| 4656 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4657 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4658 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4659 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4660 | }; |
| 4661 | |
| 4662 | // Forward the provided action to the privatization codegen. |
| 4663 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 4664 | PrivRCG.setAction(Action); |
| 4665 | |
| 4666 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 4667 | // we don't use an inline scope as changes in the references inside the |
| 4668 | // region are expected to be visible outside, so we do not privative them. |
| 4669 | OMPLexicalScope Scope(CGF, S); |
| 4670 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 4671 | PrivRCG); |
| 4672 | }; |
| 4673 | |
| 4674 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4675 | |
| 4676 | // If we don't have target devices, don't bother emitting the data mapping |
| 4677 | // code. |
| 4678 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4679 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4680 | return; |
| 4681 | } |
| 4682 | |
| 4683 | // Check if we have any if clause associated with the directive. |
| 4684 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4685 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4686 | IfCond = C->getCondition(); |
| 4687 | |
| 4688 | // Check if we have any device clause associated with the directive. |
| 4689 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4690 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4691 | Device = C->getDevice(); |
| 4692 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4693 | // Set the action to signal privatization of device pointers. |
| 4694 | RCG.setAction(PrivAction); |
| 4695 | |
| 4696 | // Emit region code. |
| 4697 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 4698 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4699 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4700 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4701 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 4702 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4703 | // If we don't have target devices, don't bother emitting the data mapping |
| 4704 | // code. |
| 4705 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4706 | return; |
| 4707 | |
| 4708 | // Check if we have any if clause associated with the directive. |
| 4709 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4710 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4711 | IfCond = C->getCondition(); |
| 4712 | |
| 4713 | // Check if we have any device clause associated with the directive. |
| 4714 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4715 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4716 | Device = C->getDevice(); |
| 4717 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4718 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4719 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4722 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 4723 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4724 | // If we don't have target devices, don't bother emitting the data mapping |
| 4725 | // code. |
| 4726 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4727 | return; |
| 4728 | |
| 4729 | // Check if we have any if clause associated with the directive. |
| 4730 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4731 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4732 | IfCond = C->getCondition(); |
| 4733 | |
| 4734 | // Check if we have any device clause associated with the directive. |
| 4735 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4736 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4737 | Device = C->getDevice(); |
| 4738 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4739 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4740 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4741 | } |
| 4742 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4743 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 4744 | const OMPTargetParallelDirective &S, |
| 4745 | PrePostActionTy &Action) { |
| 4746 | // Get the captured statement associated with the 'parallel' region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4747 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4748 | Action.Enter(CGF); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4749 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4750 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4751 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4752 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4753 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4754 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4755 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4756 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4757 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4758 | // TODO: Add support for clauses. |
| 4759 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4760 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4761 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4762 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, |
| 4763 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4764 | emitPostUpdateForReductionClause(CGF, S, |
| 4765 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4766 | } |
| 4767 | |
| 4768 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 4769 | CodeGenModule &CGM, StringRef ParentName, |
| 4770 | const OMPTargetParallelDirective &S) { |
| 4771 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4772 | emitTargetParallelRegion(CGF, S, Action); |
| 4773 | }; |
| 4774 | llvm::Function *Fn; |
| 4775 | llvm::Constant *Addr; |
| 4776 | // Emit target region as a standalone region. |
| 4777 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4778 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4779 | assert(Fn && Addr && "Target device function emission failed."); |
| 4780 | } |
| 4781 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4782 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 4783 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4784 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4785 | emitTargetParallelRegion(CGF, S, Action); |
| 4786 | }; |
| 4787 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4790 | static void emitTargetParallelForRegion(CodeGenFunction &CGF, |
| 4791 | const OMPTargetParallelForDirective &S, |
| 4792 | PrePostActionTy &Action) { |
| 4793 | Action.Enter(CGF); |
| 4794 | // Emit directive as a combined directive that consists of two implicit |
| 4795 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4796 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4797 | Action.Enter(CGF); |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4798 | CodeGenFunction::OMPCancelStackRAII CancelRegion( |
| 4799 | CGF, OMPD_target_parallel_for, S.hasCancel()); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4800 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4801 | emitDispatchForLoopBounds); |
| 4802 | }; |
| 4803 | emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, |
| 4804 | emitEmptyBoundParameters); |
| 4805 | } |
| 4806 | |
| 4807 | void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| 4808 | CodeGenModule &CGM, StringRef ParentName, |
| 4809 | const OMPTargetParallelForDirective &S) { |
| 4810 | // Emit SPMD target parallel for region as a standalone region. |
| 4811 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4812 | emitTargetParallelForRegion(CGF, S, Action); |
| 4813 | }; |
| 4814 | llvm::Function *Fn; |
| 4815 | llvm::Constant *Addr; |
| 4816 | // Emit target region as a standalone region. |
| 4817 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4818 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4819 | assert(Fn && Addr && "Target device function emission failed."); |
| 4820 | } |
| 4821 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4822 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 4823 | const OMPTargetParallelForDirective &S) { |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4824 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4825 | emitTargetParallelForRegion(CGF, S, Action); |
| 4826 | }; |
| 4827 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4828 | } |
| 4829 | |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4830 | static void |
| 4831 | emitTargetParallelForSimdRegion(CodeGenFunction &CGF, |
| 4832 | const OMPTargetParallelForSimdDirective &S, |
| 4833 | PrePostActionTy &Action) { |
| 4834 | Action.Enter(CGF); |
| 4835 | // Emit directive as a combined directive that consists of two implicit |
| 4836 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4837 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4838 | Action.Enter(CGF); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4839 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4840 | emitDispatchForLoopBounds); |
| 4841 | }; |
| 4842 | emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, |
| 4843 | emitEmptyBoundParameters); |
| 4844 | } |
| 4845 | |
| 4846 | void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| 4847 | CodeGenModule &CGM, StringRef ParentName, |
| 4848 | const OMPTargetParallelForSimdDirective &S) { |
| 4849 | // Emit SPMD target parallel for region as a standalone region. |
| 4850 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4851 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4852 | }; |
| 4853 | llvm::Function *Fn; |
| 4854 | llvm::Constant *Addr; |
| 4855 | // Emit target region as a standalone region. |
| 4856 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4857 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4858 | assert(Fn && Addr && "Target device function emission failed."); |
| 4859 | } |
| 4860 | |
| 4861 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 4862 | const OMPTargetParallelForSimdDirective &S) { |
| 4863 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4864 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4865 | }; |
| 4866 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4867 | } |
| 4868 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4869 | /// Emit a helper variable and return corresponding lvalue. |
| 4870 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 4871 | const ImplicitParamDecl *PVD, |
| 4872 | CodeGenFunction::OMPPrivateScope &Privates) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4873 | const auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 4874 | Privates.addPrivate(VDecl, |
| 4875 | [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); }); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 4879 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 4880 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4881 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4882 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 4883 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4884 | const Expr *IfCond = nullptr; |
| 4885 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4886 | if (C->getNameModifier() == OMPD_unknown || |
| 4887 | C->getNameModifier() == OMPD_taskloop) { |
| 4888 | IfCond = C->getCondition(); |
| 4889 | break; |
| 4890 | } |
| 4891 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4892 | |
| 4893 | OMPTaskDataTy Data; |
| 4894 | // Check if taskloop must be emitted without taskgroup. |
| 4895 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4896 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4897 | Data.Tied = true; |
| 4898 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4899 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 4900 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4901 | Data.Schedule.setInt(/*IntVal=*/false); |
| 4902 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4903 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 4904 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4905 | Data.Schedule.setInt(/*IntVal=*/true); |
| 4906 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4907 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4908 | |
| 4909 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4910 | // if (PreCond) { |
| 4911 | // for (IV in 0..LastIteration) BODY; |
| 4912 | // <Final counter/linear vars updates>; |
| 4913 | // } |
| 4914 | // |
| 4915 | |
| 4916 | // Emit: if (PreCond) - begin. |
| 4917 | // If the condition constant folds and can be elided, avoid emitting the |
| 4918 | // whole loop. |
| 4919 | bool CondConstant; |
| 4920 | llvm::BasicBlock *ContBlock = nullptr; |
| 4921 | OMPLoopScope PreInitScope(CGF, S); |
| 4922 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 4923 | if (!CondConstant) |
| 4924 | return; |
| 4925 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4926 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4927 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 4928 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 4929 | CGF.getProfileCount(&S)); |
| 4930 | CGF.EmitBlock(ThenBlock); |
| 4931 | CGF.incrementProfileCounter(&S); |
| 4932 | } |
| 4933 | |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4934 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 4935 | CGF.EmitOMPSimdInit(S); |
| 4936 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4937 | OMPPrivateScope LoopScope(CGF); |
| 4938 | // Emit helper vars inits. |
| 4939 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 4940 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 4941 | auto *LBP = std::next(I, LowerBound); |
| 4942 | auto *UBP = std::next(I, UpperBound); |
| 4943 | auto *STP = std::next(I, Stride); |
| 4944 | auto *LIP = std::next(I, LastIter); |
| 4945 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 4946 | LoopScope); |
| 4947 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 4948 | LoopScope); |
| 4949 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 4950 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 4951 | LoopScope); |
| 4952 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4953 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4954 | (void)LoopScope.Privatize(); |
| 4955 | // Emit the loop iteration variable. |
| 4956 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4957 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4958 | CGF.EmitVarDecl(*IVDecl); |
| 4959 | CGF.EmitIgnoredExpr(S.getInit()); |
| 4960 | |
| 4961 | // Emit the iterations count variable. |
| 4962 | // If it is not a variable, Sema decided to calculate iterations count on |
| 4963 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4964 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4965 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 4966 | // Emit calculation of the iterations count. |
| 4967 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 4968 | } |
| 4969 | |
| 4970 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 4971 | S.getInc(), |
| 4972 | [&S](CodeGenFunction &CGF) { |
| 4973 | CGF.EmitOMPLoopBody(S, JumpDest()); |
| 4974 | CGF.EmitStopPoint(&S); |
| 4975 | }, |
| 4976 | [](CodeGenFunction &) {}); |
| 4977 | // Emit: if (PreCond) - end. |
| 4978 | if (ContBlock) { |
| 4979 | CGF.EmitBranch(ContBlock); |
| 4980 | CGF.EmitBlock(ContBlock, true); |
| 4981 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4982 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 4983 | if (HasLastprivateClause) { |
| 4984 | CGF.EmitOMPLastprivateClauseFinal( |
| 4985 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 4986 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 4987 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4988 | (*LIP)->getType(), S.getBeginLoc()))); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4989 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4990 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4991 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4992 | IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4993 | const OMPTaskDataTy &Data) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4994 | auto &&CodeGen = [&S, OutlinedFn, SharedsTy, CapturedStruct, IfCond, |
| 4995 | &Data](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4996 | OMPLoopScope PreInitScope(CGF, S); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4997 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getBeginLoc(), S, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4998 | OutlinedFn, SharedsTy, |
| 4999 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5000 | }; |
| 5001 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 5002 | CodeGen); |
| 5003 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5004 | if (Data.Nogroup) { |
| 5005 | EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); |
| 5006 | } else { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5007 | CGM.getOpenMPRuntime().emitTaskgroupRegion( |
| 5008 | *this, |
| 5009 | [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, |
| 5010 | PrePostActionTy &Action) { |
| 5011 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5012 | CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, |
| 5013 | Data); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5014 | }, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5015 | S.getBeginLoc()); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5016 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5017 | } |
| 5018 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5019 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5020 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5021 | } |
| 5022 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5023 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 5024 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 5025 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5026 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5027 | |
| 5028 | // Generate the instructions for '#pragma omp target update' directive. |
| 5029 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 5030 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5031 | // If we don't have target devices, don't bother emitting the data mapping |
| 5032 | // code. |
| 5033 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 5034 | return; |
| 5035 | |
| 5036 | // Check if we have any if clause associated with the directive. |
| 5037 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5038 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5039 | IfCond = C->getCondition(); |
| 5040 | |
| 5041 | // Check if we have any device clause associated with the directive. |
| 5042 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5043 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5044 | Device = C->getDevice(); |
| 5045 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5046 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5047 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5048 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5049 | |
| 5050 | void CodeGenFunction::EmitSimpleOMPExecutableDirective( |
| 5051 | const OMPExecutableDirective &D) { |
| 5052 | if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) |
| 5053 | return; |
| 5054 | auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5055 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 5056 | emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); |
| 5057 | } else { |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5058 | OMPPrivateScope LoopGlobals(CGF); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5059 | if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5060 | for (const Expr *E : LD->counters()) { |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5061 | const auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 5062 | if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) { |
| 5063 | LValue GlobLVal = CGF.EmitLValue(E); |
| 5064 | LoopGlobals.addPrivate( |
| 5065 | VD, [&GlobLVal]() { return GlobLVal.getAddress(); }); |
| 5066 | } |
Bjorn Pettersson | 6c2d83b | 2018-10-30 08:49:26 +0000 | [diff] [blame] | 5067 | if (isa<OMPCapturedExprDecl>(VD)) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5068 | // Emit only those that were not explicitly referenced in clauses. |
| 5069 | if (!CGF.LocalDeclMap.count(VD)) |
| 5070 | CGF.EmitVarDecl(*VD); |
| 5071 | } |
| 5072 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5073 | for (const auto *C : D.getClausesOfKind<OMPOrderedClause>()) { |
| 5074 | if (!C->getNumForLoops()) |
| 5075 | continue; |
| 5076 | for (unsigned I = LD->getCollapsedNumber(), |
| 5077 | E = C->getLoopNumIterations().size(); |
| 5078 | I < E; ++I) { |
| 5079 | if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 5080 | cast<DeclRefExpr>(C->getLoopCounter(I))->getDecl())) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5081 | // Emit only those that were not explicitly referenced in clauses. |
| 5082 | if (!CGF.LocalDeclMap.count(VD)) |
| 5083 | CGF.EmitVarDecl(*VD); |
| 5084 | } |
| 5085 | } |
| 5086 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5087 | } |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5088 | LoopGlobals.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5089 | CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5090 | } |
| 5091 | }; |
| 5092 | OMPSimdLexicalScope Scope(*this, D); |
| 5093 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5094 | *this, |
| 5095 | isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd |
| 5096 | : D.getDirectiveKind(), |
| 5097 | CodeGen); |
| 5098 | } |