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, |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 299 | LValue AddrLV) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 300 | ASTContext &Ctx = CGF.getContext(); |
| 301 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 302 | llvm::Value *CastedPtr = CGF.EmitScalarConversion( |
| 303 | AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(), |
| 304 | Ctx.getPointerType(DstType), Loc); |
| 305 | Address TmpAddr = |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 306 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
| 307 | .getAddress(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 308 | return TmpAddr; |
| 309 | } |
| 310 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 311 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 312 | if (T->isLValueReferenceType()) |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 313 | return C.getLValueReferenceType( |
| 314 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 315 | /*SpelledAsLValue=*/false); |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 316 | if (T->isPointerType()) |
| 317 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 318 | if (const ArrayType *A = T->getAsArrayTypeUnsafe()) { |
| 319 | if (const auto *VLA = dyn_cast<VariableArrayType>(A)) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 320 | return getCanonicalParamType(C, VLA->getElementType()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 321 | if (!A->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 322 | return C.getCanonicalType(T); |
| 323 | } |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 324 | return C.getCanonicalParamType(T); |
| 325 | } |
| 326 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 327 | namespace { |
| 328 | /// Contains required data for proper outlined function codegen. |
| 329 | struct FunctionOptions { |
| 330 | /// Captured statement for which the function is generated. |
| 331 | const CapturedStmt *S = nullptr; |
| 332 | /// true if cast to/from UIntPtr is required for variables captured by |
| 333 | /// value. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 334 | const bool UIntPtrCastRequired = true; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 335 | /// 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] | 336 | /// sizes. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 337 | const bool RegisterCastedArgsOnly = false; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 338 | /// Name of the generated function. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 339 | const StringRef FunctionName; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 340 | explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, |
| 341 | bool RegisterCastedArgsOnly, |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 342 | StringRef FunctionName) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 343 | : S(S), UIntPtrCastRequired(UIntPtrCastRequired), |
| 344 | RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 345 | FunctionName(FunctionName) {} |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 346 | }; |
| 347 | } |
| 348 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 349 | static llvm::Function *emitOutlinedFunctionPrologue( |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 350 | CodeGenFunction &CGF, FunctionArgList &Args, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 351 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 352 | &LocalAddrs, |
| 353 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> |
| 354 | &VLASizes, |
| 355 | llvm::Value *&CXXThisValue, const FunctionOptions &FO) { |
| 356 | const CapturedDecl *CD = FO.S->getCapturedDecl(); |
| 357 | const RecordDecl *RD = FO.S->getCapturedRecordDecl(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 358 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 359 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 360 | CXXThisValue = nullptr; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 361 | // Build the argument list. |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 362 | CodeGenModule &CGM = CGF.CGM; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 363 | ASTContext &Ctx = CGM.getContext(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 364 | FunctionArgList TargetArgs; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 365 | Args.append(CD->param_begin(), |
| 366 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 367 | TargetArgs.append( |
| 368 | CD->param_begin(), |
| 369 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 370 | auto I = FO.S->captures().begin(); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 371 | FunctionDecl *DebugFunctionDecl = nullptr; |
| 372 | if (!FO.UIntPtrCastRequired) { |
| 373 | FunctionProtoType::ExtProtoInfo EPI; |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 374 | QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 375 | DebugFunctionDecl = FunctionDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 376 | Ctx, Ctx.getTranslationUnitDecl(), FO.S->getBeginLoc(), |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 377 | SourceLocation(), DeclarationName(), FunctionTy, |
| 378 | Ctx.getTrivialTypeSourceInfo(FunctionTy), SC_Static, |
| 379 | /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 380 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 381 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 382 | QualType ArgType = FD->getType(); |
| 383 | IdentifierInfo *II = nullptr; |
| 384 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 385 | |
| 386 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 387 | // function argument type should be uintptr and the value properly casted to |
| 388 | // uintptr. This is necessary given that the runtime library is only able to |
| 389 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 390 | // outlined function. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 391 | if (FO.UIntPtrCastRequired && |
| 392 | ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
| 393 | I->capturesVariableArrayType())) |
| 394 | ArgType = Ctx.getUIntPtrType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 395 | |
| 396 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 397 | CapVar = I->getCapturedVar(); |
| 398 | II = CapVar->getIdentifier(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 399 | } else if (I->capturesThis()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 400 | II = &Ctx.Idents.get("this"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 401 | } else { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 402 | assert(I->capturesVariableArrayType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 403 | II = &Ctx.Idents.get("vla"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 404 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 405 | if (ArgType->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 406 | ArgType = getCanonicalParamType(Ctx, ArgType); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 407 | VarDecl *Arg; |
| 408 | if (DebugFunctionDecl && (CapVar || I->capturesThis())) { |
| 409 | Arg = ParmVarDecl::Create( |
| 410 | Ctx, DebugFunctionDecl, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 411 | CapVar ? CapVar->getBeginLoc() : FD->getBeginLoc(), |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 412 | CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, |
| 413 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
| 414 | } else { |
| 415 | Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), |
| 416 | II, ArgType, ImplicitParamDecl::Other); |
| 417 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 418 | Args.emplace_back(Arg); |
| 419 | // Do not cast arguments if we emit function with non-original types. |
| 420 | TargetArgs.emplace_back( |
| 421 | FO.UIntPtrCastRequired |
| 422 | ? Arg |
| 423 | : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 424 | ++I; |
| 425 | } |
| 426 | Args.append( |
| 427 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 428 | CD->param_end()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 429 | TargetArgs.append( |
| 430 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 431 | CD->param_end()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 432 | |
| 433 | // Create the function declaration. |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 434 | const CGFunctionInfo &FuncInfo = |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 435 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 436 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 437 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 438 | auto *F = |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 439 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 440 | FO.FunctionName, &CGM.getModule()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 441 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 442 | if (CD->isNothrow()) |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 443 | F->setDoesNotThrow(); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 444 | F->setDoesNotRecurse(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 445 | |
| 446 | // Generate the function. |
Alexey Bataev | 6e01dc1 | 2017-08-14 16:03:47 +0000 | [diff] [blame] | 447 | CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 448 | FO.S->getBeginLoc(), CD->getBody()->getBeginLoc()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 449 | unsigned Cnt = CD->getContextParamPosition(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 450 | I = FO.S->captures().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 451 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 452 | // Do not map arguments if we emit function with non-original types. |
| 453 | Address LocalAddr(Address::invalid()); |
| 454 | if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) { |
| 455 | LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt], |
| 456 | TargetArgs[Cnt]); |
| 457 | } else { |
| 458 | LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]); |
| 459 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 460 | // If we are capturing a pointer by copy we don't need to do anything, just |
| 461 | // use the value that we get from the arguments. |
| 462 | if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 463 | const VarDecl *CurVD = I->getCapturedVar(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 464 | if (!FO.RegisterCastedArgsOnly) |
| 465 | LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}}); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 466 | ++Cnt; |
| 467 | ++I; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 468 | continue; |
| 469 | } |
| 470 | |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 471 | LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), |
| 472 | AlignmentSource::Decl); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 473 | if (FD->hasCapturedVLAType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 474 | if (FO.UIntPtrCastRequired) { |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 475 | ArgLVal = CGF.MakeAddrLValue( |
| 476 | castValueFromUintptr(CGF, I->getLocation(), FD->getType(), |
| 477 | Args[Cnt]->getName(), ArgLVal), |
| 478 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 479 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 480 | llvm::Value *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
| 481 | const VariableArrayType *VAT = FD->getCapturedVLAType(); |
| 482 | VLASizes.try_emplace(Args[Cnt], VAT->getSizeExpr(), ExprArg); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 483 | } else if (I->capturesVariable()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 484 | const VarDecl *Var = I->getCapturedVar(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 485 | QualType VarTy = Var->getType(); |
| 486 | Address ArgAddr = ArgLVal.getAddress(); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 487 | if (ArgLVal.getType()->isLValueReferenceType()) { |
| 488 | ArgAddr = CGF.EmitLoadOfReference(ArgLVal); |
| 489 | } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) { |
| 490 | assert(ArgLVal.getType()->isPointerType()); |
| 491 | ArgAddr = CGF.EmitLoadOfPointer( |
| 492 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 493 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 494 | if (!FO.RegisterCastedArgsOnly) { |
| 495 | LocalAddrs.insert( |
| 496 | {Args[Cnt], |
| 497 | {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); |
| 498 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 499 | } else if (I->capturesVariableByCopy()) { |
| 500 | assert(!FD->getType()->isAnyPointerType() && |
| 501 | "Not expecting a captured pointer."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 502 | const VarDecl *Var = I->getCapturedVar(); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 503 | LocalAddrs.insert({Args[Cnt], |
| 504 | {Var, FO.UIntPtrCastRequired |
| 505 | ? castValueFromUintptr( |
| 506 | CGF, I->getLocation(), FD->getType(), |
| 507 | Args[Cnt]->getName(), ArgLVal) |
| 508 | : ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 509 | } else { |
| 510 | // If 'this' is captured, load it into CXXThisValue. |
| 511 | assert(I->capturesThis()); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 512 | CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 513 | LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 514 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 515 | ++Cnt; |
| 516 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 519 | return F; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | llvm::Function * |
| 523 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { |
| 524 | assert( |
| 525 | CapturedStmtInfo && |
| 526 | "CapturedStmtInfo should be set when generating the captured function"); |
| 527 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 528 | // Build the argument list. |
| 529 | bool NeedWrapperFunction = |
| 530 | getDebugInfo() && |
| 531 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo; |
| 532 | FunctionArgList Args; |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 533 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 534 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 535 | SmallString<256> Buffer; |
| 536 | llvm::raw_svector_ostream Out(Buffer); |
| 537 | Out << CapturedStmtInfo->getHelperName(); |
| 538 | if (NeedWrapperFunction) |
| 539 | Out << "_debug__"; |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 540 | FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 541 | Out.str()); |
| 542 | llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, |
| 543 | VLASizes, CXXThisValue, FO); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 544 | CodeGenFunction::OMPPrivateScope LocalScope(*this); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 545 | for (const auto &LocalAddrPair : LocalAddrs) { |
| 546 | if (LocalAddrPair.second.first) { |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 547 | LocalScope.addPrivate(LocalAddrPair.second.first, [&LocalAddrPair]() { |
| 548 | return LocalAddrPair.second.second; |
| 549 | }); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 550 | } |
| 551 | } |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 552 | (void)LocalScope.Privatize(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 553 | for (const auto &VLASizePair : VLASizes) |
| 554 | VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 555 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 556 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 557 | (void)LocalScope.ForceCleanup(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 558 | FinishFunction(CD->getBodyRBrace()); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 559 | if (!NeedWrapperFunction) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 560 | return F; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 561 | |
Alexey Bataev | efd884d | 2017-08-04 21:26:25 +0000 | [diff] [blame] | 562 | FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 563 | /*RegisterCastedArgsOnly=*/true, |
| 564 | CapturedStmtInfo->getHelperName()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 565 | CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 566 | WrapperCGF.CapturedStmtInfo = CapturedStmtInfo; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 567 | Args.clear(); |
| 568 | LocalAddrs.clear(); |
| 569 | VLASizes.clear(); |
| 570 | llvm::Function *WrapperF = |
| 571 | emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 572 | WrapperCGF.CXXThisValue, WrapperFO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 573 | llvm::SmallVector<llvm::Value *, 4> CallArgs; |
| 574 | for (const auto *Arg : Args) { |
| 575 | llvm::Value *CallArg; |
| 576 | auto I = LocalAddrs.find(Arg); |
| 577 | if (I != LocalAddrs.end()) { |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 578 | LValue LV = WrapperCGF.MakeAddrLValue( |
| 579 | I->second.second, |
| 580 | I->second.first ? I->second.first->getType() : Arg->getType(), |
| 581 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 582 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 583 | } else { |
| 584 | auto EI = VLASizes.find(Arg); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 585 | if (EI != VLASizes.end()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 586 | CallArg = EI->second.second; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 587 | } else { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 588 | LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 589 | Arg->getType(), |
| 590 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 591 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 592 | } |
| 593 | } |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 594 | CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 595 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 596 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 597 | F, CallArgs); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 598 | WrapperCGF.FinishFunction(); |
| 599 | return WrapperF; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 602 | //===----------------------------------------------------------------------===// |
| 603 | // OpenMP Directive Emission |
| 604 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 605 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 606 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 607 | const llvm::function_ref<void(Address, Address)> CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 608 | // Perform element-by-element initialization. |
| 609 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 610 | |
| 611 | // Drill down to the base element type on both arrays. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 612 | const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
| 613 | llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 614 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 615 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 616 | llvm::Value *SrcBegin = SrcAddr.getPointer(); |
| 617 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 618 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 619 | llvm::Value *DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 620 | // The basic structure here is a while-do loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 621 | llvm::BasicBlock *BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 622 | llvm::BasicBlock *DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 623 | llvm::Value *IsEmpty = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 624 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 625 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 626 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 627 | // Enter the loop body, making that address the current address. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 628 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 629 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 630 | |
| 631 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 632 | |
| 633 | llvm::PHINode *SrcElementPHI = |
| 634 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 635 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 636 | Address SrcElementCurrent = |
| 637 | Address(SrcElementPHI, |
| 638 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 639 | |
| 640 | llvm::PHINode *DestElementPHI = |
| 641 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 642 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 643 | Address DestElementCurrent = |
| 644 | Address(DestElementPHI, |
| 645 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 646 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 647 | // Emit copy. |
| 648 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 649 | |
| 650 | // Shift the address forward by one element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 651 | llvm::Value *DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 652 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 653 | llvm::Value *SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 654 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 655 | // Check whether we've reached the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 656 | llvm::Value *Done = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 657 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 658 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 659 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 660 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 661 | |
| 662 | // Done. |
| 663 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 664 | } |
| 665 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 666 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 667 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 668 | const VarDecl *SrcVD, const Expr *Copy) { |
| 669 | if (OriginalType->isArrayType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 670 | const auto *BO = dyn_cast<BinaryOperator>(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 671 | if (BO && BO->getOpcode() == BO_Assign) { |
| 672 | // Perform simple memcpy for simple copying. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 673 | LValue Dest = MakeAddrLValue(DestAddr, OriginalType); |
| 674 | LValue Src = MakeAddrLValue(SrcAddr, OriginalType); |
| 675 | EmitAggregateAssign(Dest, Src, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 676 | } else { |
| 677 | // For arrays with complex element types perform element by element |
| 678 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 679 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 680 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 681 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 682 | // Working with the single array element, so have to remap |
| 683 | // destination and source variables to corresponding array |
| 684 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 685 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 686 | Remap.addPrivate(DestVD, [DestElement]() { return DestElement; }); |
| 687 | Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 688 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 689 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 690 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 691 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 692 | } else { |
| 693 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 694 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 695 | Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; }); |
| 696 | Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 697 | (void)Remap.Privatize(); |
| 698 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 699 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 700 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 703 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 704 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 705 | if (!HaveInsertPoint()) |
| 706 | return false; |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 707 | bool DeviceConstTarget = |
| 708 | getLangOpts().OpenMPIsDevice && |
| 709 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 710 | bool FirstprivateIsLastprivate = false; |
| 711 | llvm::DenseSet<const VarDecl *> Lastprivates; |
| 712 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 713 | for (const auto *D : C->varlists()) |
| 714 | Lastprivates.insert( |
| 715 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 716 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 717 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 718 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 719 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 720 | // Force emission of the firstprivate copy if the directive does not emit |
| 721 | // outlined function, like omp for, omp simd, omp distribute etc. |
| 722 | bool MustEmitFirstprivateCopy = |
| 723 | CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 724 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 725 | auto IRef = C->varlist_begin(); |
| 726 | auto InitsRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 727 | for (const Expr *IInit : C->private_copies()) { |
| 728 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 729 | bool ThisFirstprivateIsLastprivate = |
| 730 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 731 | const FieldDecl *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 732 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 733 | if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 734 | !FD->getType()->isReferenceType() && |
| 735 | (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 736 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 737 | ++IRef; |
| 738 | ++InitsRef; |
| 739 | continue; |
| 740 | } |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 741 | // Do not emit copy for firstprivate constant variables in target regions, |
| 742 | // captured by reference. |
| 743 | if (DeviceConstTarget && OrigVD->getType().isConstant(getContext()) && |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 744 | FD && FD->getType()->isReferenceType() && |
| 745 | (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) { |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 746 | (void)CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(*this, |
| 747 | OrigVD); |
| 748 | ++IRef; |
| 749 | ++InitsRef; |
| 750 | continue; |
| 751 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 752 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 753 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 754 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 755 | const auto *VDInit = |
| 756 | cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 757 | bool IsRegistered; |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 758 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 759 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 760 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Alexey Bataev | e0ef04f | 2019-05-23 22:30:43 +0000 | [diff] [blame] | 761 | LValue OriginalLVal; |
| 762 | if (!FD) { |
| 763 | // Check if the firstprivate variable is just a constant value. |
| 764 | ConstantEmission CE = tryEmitAsConstant(&DRE); |
| 765 | if (CE && !CE.isReference()) { |
| 766 | // Constant value, no need to create a copy. |
| 767 | ++IRef; |
| 768 | ++InitsRef; |
| 769 | continue; |
| 770 | } |
| 771 | if (CE && CE.isReference()) { |
| 772 | OriginalLVal = CE.getReferenceLValue(*this, &DRE); |
| 773 | } else { |
| 774 | assert(!CE && "Expected non-constant firstprivate."); |
| 775 | OriginalLVal = EmitLValue(&DRE); |
| 776 | } |
| 777 | } else { |
| 778 | OriginalLVal = EmitLValue(&DRE); |
| 779 | } |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 780 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 781 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 782 | // Emit VarDecl with copy init for arrays. |
| 783 | // Get the address of the original variable captured in current |
| 784 | // captured region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 785 | IsRegistered = PrivateScope.addPrivate( |
| 786 | OrigVD, [this, VD, Type, OriginalLVal, VDInit]() { |
| 787 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 788 | const Expr *Init = VD->getInit(); |
| 789 | if (!isa<CXXConstructExpr>(Init) || |
| 790 | isTrivialInitializer(Init)) { |
| 791 | // Perform simple memcpy. |
| 792 | LValue Dest = |
| 793 | MakeAddrLValue(Emission.getAllocatedAddress(), Type); |
| 794 | EmitAggregateAssign(Dest, OriginalLVal, Type); |
| 795 | } else { |
| 796 | EmitOMPAggregateAssign( |
| 797 | Emission.getAllocatedAddress(), OriginalLVal.getAddress(), |
| 798 | Type, |
| 799 | [this, VDInit, Init](Address DestElement, |
| 800 | Address SrcElement) { |
| 801 | // Clean up any temporaries needed by the |
| 802 | // initialization. |
| 803 | RunCleanupsScope InitScope(*this); |
| 804 | // Emit initialization for single element. |
| 805 | setAddrOfLocalVar(VDInit, SrcElement); |
| 806 | EmitAnyExprToMem(Init, DestElement, |
| 807 | Init->getType().getQualifiers(), |
| 808 | /*IsInitializer*/ false); |
| 809 | LocalDeclMap.erase(VDInit); |
| 810 | }); |
| 811 | } |
| 812 | EmitAutoVarCleanups(Emission); |
| 813 | return Emission.getAllocatedAddress(); |
| 814 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 815 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 816 | Address OriginalAddr = OriginalLVal.getAddress(); |
| 817 | IsRegistered = PrivateScope.addPrivate( |
| 818 | OrigVD, [this, VDInit, OriginalAddr, VD]() { |
| 819 | // Emit private VarDecl with copy init. |
| 820 | // Remap temp VDInit variable to the address of the original |
| 821 | // variable (for proper handling of captured global variables). |
| 822 | setAddrOfLocalVar(VDInit, OriginalAddr); |
| 823 | EmitDecl(*VD); |
| 824 | LocalDeclMap.erase(VDInit); |
| 825 | return GetAddrOfLocalVar(VD); |
| 826 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 827 | } |
| 828 | assert(IsRegistered && |
| 829 | "firstprivate var already registered as private"); |
| 830 | // Silence the warning about unused variable. |
| 831 | (void)IsRegistered; |
| 832 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 833 | ++IRef; |
| 834 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 837 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 838 | } |
| 839 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 840 | void CodeGenFunction::EmitOMPPrivateClause( |
| 841 | const OMPExecutableDirective &D, |
| 842 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 843 | if (!HaveInsertPoint()) |
| 844 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 845 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 846 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 847 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 848 | for (const Expr *IInit : C->private_copies()) { |
| 849 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 850 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 851 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 852 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
| 853 | // Emit private VarDecl with copy init. |
| 854 | EmitDecl(*VD); |
| 855 | return GetAddrOfLocalVar(VD); |
| 856 | }); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 857 | assert(IsRegistered && "private var already registered as private"); |
| 858 | // Silence the warning about unused variable. |
| 859 | (void)IsRegistered; |
| 860 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 861 | ++IRef; |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 866 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 867 | if (!HaveInsertPoint()) |
| 868 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 869 | // threadprivate_var1 = master_threadprivate_var1; |
| 870 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 871 | // ... |
| 872 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 873 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 874 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 875 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 876 | auto IRef = C->varlist_begin(); |
| 877 | auto ISrcRef = C->source_exprs().begin(); |
| 878 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 879 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 880 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 881 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 882 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 883 | // Get the address of the master variable. If we are emitting code with |
| 884 | // TLS support, the address is passed from the master as field in the |
| 885 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 886 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 887 | if (getLangOpts().OpenMPUseTLS && |
| 888 | getContext().getTargetInfo().isTLSSupported()) { |
| 889 | assert(CapturedStmtInfo->lookup(VD) && |
| 890 | "Copyin threadprivates should have been captured!"); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 891 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), true, |
| 892 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 893 | MasterAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 894 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 895 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 896 | MasterAddr = |
| 897 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 898 | : CGM.GetAddrOfGlobal(VD), |
| 899 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 900 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 901 | // Get the address of the threadprivate variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 902 | Address PrivateAddr = EmitLValue(*IRef).getAddress(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 903 | if (CopiedVars.size() == 1) { |
| 904 | // At first check if current thread is a master thread. If it is, no |
| 905 | // need to copy data. |
| 906 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 907 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 908 | Builder.CreateCondBr( |
| 909 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 910 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 911 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), |
| 912 | CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 913 | CopyBegin, CopyEnd); |
| 914 | EmitBlock(CopyBegin); |
| 915 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 916 | const auto *SrcVD = |
| 917 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 918 | const auto *DestVD = |
| 919 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 920 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 921 | } |
| 922 | ++IRef; |
| 923 | ++ISrcRef; |
| 924 | ++IDestRef; |
| 925 | } |
| 926 | } |
| 927 | if (CopyEnd) { |
| 928 | // Exit out of copying procedure for non-master thread. |
| 929 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 930 | return true; |
| 931 | } |
| 932 | return false; |
| 933 | } |
| 934 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 935 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 936 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 937 | if (!HaveInsertPoint()) |
| 938 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 939 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 940 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 941 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 942 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 943 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 944 | SIMDLCVs.insert( |
| 945 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 946 | } |
| 947 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 948 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 949 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 950 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 951 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 952 | !getLangOpts().OpenMPSimd) |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 953 | break; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 954 | auto IRef = C->varlist_begin(); |
| 955 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 956 | for (const Expr *IInit : C->private_copies()) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 957 | // Keep the address of the original variable for future update at the end |
| 958 | // of the loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 959 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 960 | // Taskloops do not require additional initialization, it is done in |
| 961 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 962 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 963 | const auto *DestVD = |
| 964 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 965 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 966 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
| 967 | /*RefersToEnclosingVariableOrCapture=*/ |
| 968 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 969 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 970 | return EmitLValue(&DRE).getAddress(); |
| 971 | }); |
| 972 | // Check if the variable is also a firstprivate: in this case IInit is |
| 973 | // not generated. Initialization of this variable will happen in codegen |
| 974 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 975 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 976 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 977 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 978 | // Emit private VarDecl with copy init. |
| 979 | EmitDecl(*VD); |
| 980 | return GetAddrOfLocalVar(VD); |
| 981 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 982 | assert(IsRegistered && |
| 983 | "lastprivate var already registered as private"); |
| 984 | (void)IsRegistered; |
| 985 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 986 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 987 | ++IRef; |
| 988 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | return HasAtLeastOneLastprivate; |
| 992 | } |
| 993 | |
| 994 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 995 | const OMPExecutableDirective &D, bool NoFinals, |
| 996 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 997 | if (!HaveInsertPoint()) |
| 998 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 999 | // Emit following code: |
| 1000 | // if (<IsLastIterCond>) { |
| 1001 | // orig_var1 = private_orig_var1; |
| 1002 | // ... |
| 1003 | // orig_varn = private_orig_varn; |
| 1004 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1005 | llvm::BasicBlock *ThenBB = nullptr; |
| 1006 | llvm::BasicBlock *DoneBB = nullptr; |
| 1007 | if (IsLastIterCond) { |
| 1008 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 1009 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 1010 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 1011 | EmitBlock(ThenBB); |
| 1012 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1013 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 1014 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1015 | if (const auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1016 | auto IC = LoopDirective->counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1017 | for (const Expr *F : LoopDirective->finals()) { |
| 1018 | const auto *D = |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1019 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 1020 | if (NoFinals) |
| 1021 | AlreadyEmittedVars.insert(D); |
| 1022 | else |
| 1023 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1024 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1025 | } |
| 1026 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1027 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 1028 | auto IRef = C->varlist_begin(); |
| 1029 | auto ISrcRef = C->source_exprs().begin(); |
| 1030 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1031 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 1032 | const auto *PrivateVD = |
| 1033 | cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1034 | QualType Type = PrivateVD->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1035 | const auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1036 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 1037 | // If lastprivate variable is a loop control variable for loop-based |
| 1038 | // directive, update its value before copyin back to original |
| 1039 | // variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1040 | if (const Expr *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1041 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1042 | const auto *SrcVD = |
| 1043 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 1044 | const auto *DestVD = |
| 1045 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1046 | // Get the address of the original variable. |
| 1047 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 1048 | // Get the address of the private variable. |
| 1049 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1050 | if (const auto *RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1051 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1052 | Address(Builder.CreateLoad(PrivateAddr), |
| 1053 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1054 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1055 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1056 | ++IRef; |
| 1057 | ++ISrcRef; |
| 1058 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1059 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1060 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1061 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1062 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1063 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1064 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1067 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 1068 | const OMPExecutableDirective &D, |
| 1069 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1070 | if (!HaveInsertPoint()) |
| 1071 | return; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1072 | SmallVector<const Expr *, 4> Shareds; |
| 1073 | SmallVector<const Expr *, 4> Privates; |
| 1074 | SmallVector<const Expr *, 4> ReductionOps; |
| 1075 | SmallVector<const Expr *, 4> LHSs; |
| 1076 | SmallVector<const Expr *, 4> RHSs; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1077 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1078 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1079 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1080 | auto ILHS = C->lhs_exprs().begin(); |
| 1081 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1082 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1083 | Shareds.emplace_back(Ref); |
| 1084 | Privates.emplace_back(*IPriv); |
| 1085 | ReductionOps.emplace_back(*IRed); |
| 1086 | LHSs.emplace_back(*ILHS); |
| 1087 | RHSs.emplace_back(*IRHS); |
| 1088 | std::advance(IPriv, 1); |
| 1089 | std::advance(IRed, 1); |
| 1090 | std::advance(ILHS, 1); |
| 1091 | std::advance(IRHS, 1); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1094 | ReductionCodeGen RedCG(Shareds, Privates, ReductionOps); |
| 1095 | unsigned Count = 0; |
| 1096 | auto ILHS = LHSs.begin(); |
| 1097 | auto IRHS = RHSs.begin(); |
| 1098 | auto IPriv = Privates.begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1099 | for (const Expr *IRef : Shareds) { |
| 1100 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1101 | // Emit private VarDecl with reduction init. |
| 1102 | RedCG.emitSharedLValue(*this, Count); |
| 1103 | RedCG.emitAggregateType(*this, Count); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1104 | AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1105 | RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), |
| 1106 | RedCG.getSharedLValue(Count), |
| 1107 | [&Emission](CodeGenFunction &CGF) { |
| 1108 | CGF.EmitAutoVarInit(Emission); |
| 1109 | return true; |
| 1110 | }); |
| 1111 | EmitAutoVarCleanups(Emission); |
| 1112 | Address BaseAddr = RedCG.adjustPrivateAddress( |
| 1113 | *this, Count, Emission.getAllocatedAddress()); |
| 1114 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1115 | RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1116 | assert(IsRegistered && "private var already registered as private"); |
| 1117 | // Silence the warning about unused variable. |
| 1118 | (void)IsRegistered; |
| 1119 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1120 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 1121 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1122 | QualType Type = PrivateVD->getType(); |
| 1123 | bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); |
| 1124 | if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1125 | // Store the address of the original variable associated with the LHS |
| 1126 | // implicit variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1127 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1128 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1129 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1130 | PrivateScope.addPrivate( |
| 1131 | RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); }); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1132 | } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || |
| 1133 | isa<ArraySubscriptExpr>(IRef)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1134 | // Store the address of the original variable associated with the LHS |
| 1135 | // implicit variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1136 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1137 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1138 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1139 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1140 | return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), |
| 1141 | ConvertTypeForMem(RHSVD->getType()), |
| 1142 | "rhs.begin"); |
| 1143 | }); |
| 1144 | } else { |
| 1145 | QualType Type = PrivateVD->getType(); |
| 1146 | bool IsArray = getContext().getAsArrayType(Type) != nullptr; |
| 1147 | Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(); |
| 1148 | // Store the address of the original variable associated with the LHS |
| 1149 | // implicit variable. |
| 1150 | if (IsArray) { |
| 1151 | OriginalAddr = Builder.CreateElementBitCast( |
| 1152 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1153 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1154 | PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1155 | PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1156 | RHSVD, [this, PrivateVD, RHSVD, IsArray]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1157 | return IsArray |
| 1158 | ? Builder.CreateElementBitCast( |
| 1159 | GetAddrOfLocalVar(PrivateVD), |
| 1160 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin") |
| 1161 | : GetAddrOfLocalVar(PrivateVD); |
| 1162 | }); |
| 1163 | } |
| 1164 | ++ILHS; |
| 1165 | ++IRHS; |
| 1166 | ++IPriv; |
| 1167 | ++Count; |
| 1168 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1172 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1173 | if (!HaveInsertPoint()) |
| 1174 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1175 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1176 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1177 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1178 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1179 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1180 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1181 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1182 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1183 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1184 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1185 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1186 | } |
| 1187 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1188 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1189 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1190 | ReductionKind == OMPD_simd; |
| 1191 | bool SimpleReduction = ReductionKind == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1192 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1193 | // parallel directive (it always has implicit barrier). |
| 1194 | CGM.getOpenMPRuntime().emitReduction( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1195 | *this, D.getEndLoc(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1196 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1200 | static void emitPostUpdateForReductionClause( |
| 1201 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1202 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1203 | if (!CGF.HaveInsertPoint()) |
| 1204 | return; |
| 1205 | llvm::BasicBlock *DoneBB = nullptr; |
| 1206 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1207 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1208 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1209 | if (llvm::Value *Cond = CondGen(CGF)) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1210 | // If the first post-update expression is found, emit conditional |
| 1211 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1212 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1213 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1214 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1215 | CGF.EmitBlock(ThenBB); |
| 1216 | } |
| 1217 | } |
| 1218 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1219 | } |
| 1220 | } |
| 1221 | if (DoneBB) |
| 1222 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1223 | } |
| 1224 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1225 | namespace { |
| 1226 | /// Codegen lambda for appending distribute lower and upper bounds to outlined |
| 1227 | /// parallel function. This is necessary for combined constructs such as |
| 1228 | /// 'distribute parallel for' |
| 1229 | typedef llvm::function_ref<void(CodeGenFunction &, |
| 1230 | const OMPExecutableDirective &, |
| 1231 | llvm::SmallVectorImpl<llvm::Value *> &)> |
| 1232 | CodeGenBoundParametersTy; |
| 1233 | } // anonymous namespace |
| 1234 | |
| 1235 | static void emitCommonOMPParallelDirective( |
| 1236 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 1237 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1238 | const CodeGenBoundParametersTy &CodeGenBoundParameters) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1239 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1240 | llvm::Function *OutlinedFn = |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1241 | CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1242 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1243 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1244 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1245 | llvm::Value *NumThreads = |
| 1246 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1247 | /*IgnoreResultAssign=*/true); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1248 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1249 | CGF, NumThreads, NumThreadsClause->getBeginLoc()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1250 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1251 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1252 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1253 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1254 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getBeginLoc()); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1255 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1256 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1257 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1258 | if (C->getNameModifier() == OMPD_unknown || |
| 1259 | C->getNameModifier() == OMPD_parallel) { |
| 1260 | IfCond = C->getCondition(); |
| 1261 | break; |
| 1262 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1263 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1264 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1265 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1266 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1267 | // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk |
| 1268 | // lower and upper bounds with the pragma 'for' chunking mechanism. |
| 1269 | // The following lambda takes care of appending the lower and upper bound |
| 1270 | // parameters when necessary |
| 1271 | CodeGenBoundParameters(CGF, S, CapturedVars); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1272 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1273 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getBeginLoc(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1274 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1277 | static void emitEmptyBoundParameters(CodeGenFunction &, |
| 1278 | const OMPExecutableDirective &, |
| 1279 | llvm::SmallVectorImpl<llvm::Value *> &) {} |
| 1280 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1281 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1282 | // Emit parallel region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1283 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 1284 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1285 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1286 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1287 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1288 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1289 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1290 | // propagation master's thread values of threadprivate variables to local |
| 1291 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1292 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1293 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1294 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1295 | } |
| 1296 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1297 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1298 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1299 | CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1300 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1301 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1302 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, |
| 1303 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1304 | emitPostUpdateForReductionClause(*this, S, |
| 1305 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1306 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1307 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1308 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1309 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1310 | RunCleanupsScope BodyScope(*this); |
| 1311 | // Update counters values on current iteration. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1312 | for (const Expr *UE : D.updates()) |
| 1313 | EmitIgnoredExpr(UE); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1314 | // Update the linear variables. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1315 | // In distribute directives only loop counters may be marked as linear, no |
| 1316 | // need to generate the code for them. |
| 1317 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1318 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1319 | for (const Expr *UE : C->updates()) |
| 1320 | EmitIgnoredExpr(UE); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1321 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1324 | // On a continue in the body, jump to the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1325 | JumpDest Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1326 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexey Bataev | f8be476 | 2019-08-14 19:30:06 +0000 | [diff] [blame] | 1327 | for (const Expr *E : D.finals_conditions()) { |
| 1328 | if (!E) |
| 1329 | continue; |
| 1330 | // Check that loop counter in non-rectangular nest fits into the iteration |
| 1331 | // space. |
| 1332 | llvm::BasicBlock *NextBB = createBasicBlock("omp.body.next"); |
| 1333 | EmitBranchOnBoolExpr(E, NextBB, Continue.getBlock(), |
| 1334 | getProfileCount(D.getBody())); |
| 1335 | EmitBlock(NextBB); |
| 1336 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1337 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1338 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1339 | // The end (updates/cleanups). |
| 1340 | EmitBlock(Continue.getBlock()); |
| 1341 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1342 | } |
| 1343 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1344 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1345 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1346 | const Expr *IncExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1347 | const llvm::function_ref<void(CodeGenFunction &)> BodyGen, |
| 1348 | const llvm::function_ref<void(CodeGenFunction &)> PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1349 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1350 | |
| 1351 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1352 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1353 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1354 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1355 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1356 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1357 | |
| 1358 | // If there are any cleanups between here and the loop-exit scope, |
| 1359 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1360 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1361 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1362 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1363 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1364 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1365 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1366 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1367 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1368 | if (ExitBlock != LoopExit.getBlock()) { |
| 1369 | EmitBlock(ExitBlock); |
| 1370 | EmitBranchThroughCleanup(LoopExit); |
| 1371 | } |
| 1372 | |
| 1373 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1374 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1375 | |
| 1376 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1377 | JumpDest Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1378 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1379 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1380 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1381 | |
| 1382 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1383 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1384 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1385 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1386 | BreakContinueStack.pop_back(); |
| 1387 | EmitBranch(CondBlock); |
| 1388 | LoopStack.pop(); |
| 1389 | // Emit the fall-through block. |
| 1390 | EmitBlock(LoopExit.getBlock()); |
| 1391 | } |
| 1392 | |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1393 | bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1394 | if (!HaveInsertPoint()) |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1395 | return false; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1396 | // Emit inits for the linear variables. |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1397 | bool HasLinears = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1398 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1399 | for (const Expr *Init : C->inits()) { |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1400 | HasLinears = true; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1401 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
| 1402 | if (const auto *Ref = |
| 1403 | dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1404 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1405 | const auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1406 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1407 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1408 | VD->getInit()->getType(), VK_LValue, |
| 1409 | VD->getInit()->getExprLoc()); |
| 1410 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1411 | VD->getType()), |
| 1412 | /*capturedByInit=*/false); |
| 1413 | EmitAutoVarCleanups(Emission); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1414 | } else { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1415 | EmitVarDecl(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1416 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1417 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1418 | // Emit the linear steps for the linear clauses. |
| 1419 | // 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] | 1420 | if (const auto *CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1421 | if (const auto *SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1422 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1423 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1424 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1425 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1426 | } |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1427 | return HasLinears; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1430 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1431 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1432 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1433 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1434 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1435 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1436 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1437 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1438 | auto IC = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1439 | for (const Expr *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1440 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1441 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1442 | // If the first post-update expression is found, emit conditional |
| 1443 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1444 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.linear.pu"); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1445 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1446 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1447 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1448 | } |
| 1449 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1450 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1451 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1452 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1453 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1454 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1455 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1456 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1457 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1458 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1459 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1460 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1461 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1462 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1463 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1464 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1465 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1468 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1469 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1470 | if (!CGF.HaveInsertPoint()) |
| 1471 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1472 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1473 | unsigned ClauseAlignment = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1474 | if (const Expr *AlignmentExpr = Clause->getAlignment()) { |
| 1475 | auto *AlignmentCI = |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1476 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1477 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1478 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1479 | for (const Expr *E : Clause->varlists()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1480 | unsigned Alignment = ClauseAlignment; |
| 1481 | if (Alignment == 0) { |
| 1482 | // OpenMP [2.8.1, Description] |
| 1483 | // If no optional parameter is specified, implementation-defined default |
| 1484 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1485 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1486 | CGF.getContext() |
| 1487 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1488 | E->getType()->getPointeeType())) |
| 1489 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1490 | } |
| 1491 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1492 | "alignment is not power of 2"); |
| 1493 | if (Alignment != 0) { |
| 1494 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
Roman Lebedev | bd1c087 | 2019-01-15 09:44:25 +0000 | [diff] [blame] | 1495 | CGF.EmitAlignmentAssumption( |
| 1496 | PtrValue, E, /*No second loc needed*/ SourceLocation(), Alignment); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1497 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | } |
| 1501 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1502 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1503 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1504 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1505 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1506 | auto I = S.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1507 | for (const Expr *E : S.counters()) { |
| 1508 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1509 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1510 | // Emit var without initialization. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1511 | AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1512 | EmitAutoVarCleanups(VarEmission); |
| 1513 | LocalDeclMap.erase(PrivateVD); |
| 1514 | (void)LoopScope.addPrivate(VD, [&VarEmission]() { |
| 1515 | return VarEmission.getAllocatedAddress(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1516 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1517 | if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || |
| 1518 | VD->hasGlobalStorage()) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1519 | (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1520 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1521 | LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), |
| 1522 | E->getType(), VK_LValue, E->getExprLoc()); |
| 1523 | return EmitLValue(&DRE).getAddress(); |
| 1524 | }); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1525 | } else { |
| 1526 | (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() { |
| 1527 | return VarEmission.getAllocatedAddress(); |
| 1528 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1529 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1530 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1531 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1532 | // Privatize extra loop counters used in loops for ordered(n) clauses. |
| 1533 | for (const auto *C : S.getClausesOfKind<OMPOrderedClause>()) { |
| 1534 | if (!C->getNumForLoops()) |
| 1535 | continue; |
| 1536 | for (unsigned I = S.getCollapsedNumber(), |
| 1537 | E = C->getLoopNumIterations().size(); |
| 1538 | I < E; ++I) { |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 1539 | const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I)); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1540 | const auto *VD = cast<VarDecl>(DRE->getDecl()); |
Alexey Bataev | 0d8fcdf | 2019-03-14 20:36:00 +0000 | [diff] [blame] | 1541 | // Override only those variables that can be captured to avoid re-emission |
| 1542 | // of the variables declared within the loops. |
| 1543 | if (DRE->refersToEnclosingVariableOrCapture()) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1544 | (void)LoopScope.addPrivate(VD, [this, DRE, VD]() { |
| 1545 | return CreateMemTemp(DRE->getType(), VD->getName()); |
| 1546 | }); |
| 1547 | } |
| 1548 | } |
| 1549 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1552 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1553 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1554 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1555 | if (!CGF.HaveInsertPoint()) |
| 1556 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1557 | { |
| 1558 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1559 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1560 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1561 | // Get initial values of real counters. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1562 | for (const Expr *I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1563 | CGF.EmitIgnoredExpr(I); |
| 1564 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1565 | } |
Alexey Bataev | f8be476 | 2019-08-14 19:30:06 +0000 | [diff] [blame] | 1566 | // Create temp loop control variables with their init values to support |
| 1567 | // non-rectangular loops. |
| 1568 | CodeGenFunction::OMPMapVars PreCondVars; |
| 1569 | for (const Expr * E: S.dependent_counters()) { |
| 1570 | if (!E) |
| 1571 | continue; |
| 1572 | assert(!E->getType().getNonReferenceType()->isRecordType() && |
| 1573 | "dependent counter must not be an iterator."); |
| 1574 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1575 | Address CounterAddr = |
| 1576 | CGF.CreateMemTemp(VD->getType().getNonReferenceType()); |
| 1577 | (void)PreCondVars.setVarAddr(CGF, VD, CounterAddr); |
| 1578 | } |
| 1579 | (void)PreCondVars.apply(CGF); |
| 1580 | for (const Expr *E : S.dependent_inits()) { |
| 1581 | if (!E) |
| 1582 | continue; |
| 1583 | CGF.EmitIgnoredExpr(E); |
| 1584 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1585 | // Check that loop is executed at least one time. |
| 1586 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
Alexey Bataev | f8be476 | 2019-08-14 19:30:06 +0000 | [diff] [blame] | 1587 | PreCondVars.restore(CGF); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1590 | void CodeGenFunction::EmitOMPLinearClause( |
| 1591 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1592 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1593 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1594 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1595 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1596 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1597 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1598 | SIMDLCVs.insert( |
| 1599 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1600 | } |
| 1601 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1602 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1603 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1604 | for (const Expr *E : C->varlists()) { |
| 1605 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1606 | const auto *PrivateVD = |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1607 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1608 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1609 | bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1610 | // Emit private VarDecl with copy init. |
| 1611 | EmitVarDecl(*PrivateVD); |
| 1612 | return GetAddrOfLocalVar(PrivateVD); |
| 1613 | }); |
| 1614 | assert(IsRegistered && "linear var already registered as private"); |
| 1615 | // Silence the warning about unused variable. |
| 1616 | (void)IsRegistered; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1617 | } else { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1618 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1619 | } |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1620 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1625 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1626 | const OMPExecutableDirective &D, |
| 1627 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1628 | if (!CGF.HaveInsertPoint()) |
| 1629 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1630 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1631 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1632 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1633 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1634 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1635 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1636 | // the memory instructions parallel, because loop-carried |
| 1637 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1638 | if (!IsMonotonic) |
| 1639 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1640 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1641 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1642 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1643 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1644 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1645 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1646 | // the memory instructions parallel, because loop-carried |
| 1647 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1648 | CGF.LoopStack.setParallel(/*Enable=*/false); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1652 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1653 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1654 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1655 | LoopStack.setParallel(!IsMonotonic); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1656 | LoopStack.setVectorizeEnable(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1657 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1660 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1661 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1662 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1663 | if (!HaveInsertPoint()) |
| 1664 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1665 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1666 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1667 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1668 | for (const Expr *F : D.finals()) { |
| 1669 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
| 1670 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1671 | const auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1672 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1673 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1674 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1675 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1676 | // If the first post-update expression is found, emit conditional |
| 1677 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1678 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.final.then"); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1679 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1680 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1681 | EmitBlock(ThenBB); |
| 1682 | } |
| 1683 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1684 | Address OrigAddr = Address::invalid(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1685 | if (CED) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1686 | OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1687 | } else { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1688 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(PrivateVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1689 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1690 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
| 1691 | OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1692 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1693 | OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1694 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1695 | (void)VarScope.Privatize(); |
| 1696 | EmitIgnoredExpr(F); |
| 1697 | } |
| 1698 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1699 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1700 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1701 | if (DoneBB) |
| 1702 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1705 | static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, |
| 1706 | const OMPLoopDirective &S, |
| 1707 | CodeGenFunction::JumpDest LoopExit) { |
| 1708 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1709 | CGF.EmitStopPoint(&S); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 1710 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1711 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1712 | /// Emit a helper variable and return corresponding lvalue. |
| 1713 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1714 | const DeclRefExpr *Helper) { |
| 1715 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1716 | CGF.EmitVarDecl(*VDecl); |
| 1717 | return CGF.EmitLValue(Helper); |
| 1718 | } |
| 1719 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1720 | static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1721 | PrePostActionTy &Action) { |
| 1722 | Action.Enter(CGF); |
| 1723 | assert(isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 1724 | "Expected simd directive"); |
| 1725 | OMPLoopScope PreInitScope(CGF, S); |
| 1726 | // if (PreCond) { |
| 1727 | // for (IV in 0..LastIteration) BODY; |
| 1728 | // <Final counter/linear vars updates>; |
| 1729 | // } |
| 1730 | // |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1731 | if (isOpenMPDistributeDirective(S.getDirectiveKind()) || |
| 1732 | isOpenMPWorksharingDirective(S.getDirectiveKind()) || |
| 1733 | isOpenMPTaskLoopDirective(S.getDirectiveKind())) { |
| 1734 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1735 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1736 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1737 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1738 | // Emit: if (PreCond) - begin. |
| 1739 | // If the condition constant folds and can be elided, avoid emitting the |
| 1740 | // whole loop. |
| 1741 | bool CondConstant; |
| 1742 | llvm::BasicBlock *ContBlock = nullptr; |
| 1743 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1744 | if (!CondConstant) |
| 1745 | return; |
| 1746 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1747 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1748 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
| 1749 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1750 | CGF.getProfileCount(&S)); |
| 1751 | CGF.EmitBlock(ThenBlock); |
| 1752 | CGF.incrementProfileCounter(&S); |
| 1753 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1754 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1755 | // Emit the loop iteration variable. |
| 1756 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1757 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1758 | CGF.EmitVarDecl(*IVDecl); |
| 1759 | CGF.EmitIgnoredExpr(S.getInit()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1760 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1761 | // Emit the iterations count variable. |
| 1762 | // If it is not a variable, Sema decided to calculate iterations count on |
| 1763 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1764 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1765 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1766 | // Emit calculation of the iterations count. |
| 1767 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1768 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1769 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1770 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1771 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1772 | emitAlignedClause(CGF, S); |
| 1773 | (void)CGF.EmitOMPLinearClauseInit(S); |
| 1774 | { |
| 1775 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1776 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 1777 | CGF.EmitOMPLinearClause(S, LoopScope); |
| 1778 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 1779 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 1780 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 1781 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 1782 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 1783 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1784 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1785 | S.getInc(), |
| 1786 | [&S](CodeGenFunction &CGF) { |
| 1787 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 1788 | CGF.EmitStopPoint(&S); |
| 1789 | }, |
| 1790 | [](CodeGenFunction &) {}); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1791 | CGF.EmitOMPSimdFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1792 | // Emit final copy of the lastprivate variables at the end of loops. |
| 1793 | if (HasLastprivateClause) |
| 1794 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
| 1795 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1796 | emitPostUpdateForReductionClause(CGF, S, |
| 1797 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1798 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1799 | CGF.EmitOMPLinearClauseFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1800 | // Emit: if (PreCond) - end. |
| 1801 | if (ContBlock) { |
| 1802 | CGF.EmitBranch(ContBlock); |
| 1803 | CGF.EmitBlock(ContBlock, true); |
| 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
| 1808 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 1809 | emitOMPSimdRegion(CGF, S, Action); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1810 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1811 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1812 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1815 | void CodeGenFunction::EmitOMPOuterLoop( |
| 1816 | bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, |
| 1817 | CodeGenFunction::OMPPrivateScope &LoopScope, |
| 1818 | const CodeGenFunction::OMPLoopArguments &LoopArgs, |
| 1819 | const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, |
| 1820 | const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1821 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1822 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1823 | const Expr *IVExpr = S.getIterationVariable(); |
| 1824 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1825 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1826 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1827 | JumpDest LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1828 | |
| 1829 | // Start the loop with a block that tests the condition. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1830 | llvm::BasicBlock *CondBlock = createBasicBlock("omp.dispatch.cond"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1831 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1832 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1833 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1834 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1835 | |
| 1836 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1837 | if (!DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1838 | // UB = min(UB, GlobalUB) or |
| 1839 | // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. |
| 1840 | // 'distribute parallel for') |
| 1841 | EmitIgnoredExpr(LoopArgs.EUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1842 | // IV = LB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1843 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1844 | // IV < UB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1845 | BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1846 | } else { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1847 | BoolCondVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1848 | RT.emitForNext(*this, S.getBeginLoc(), IVSize, IVSigned, LoopArgs.IL, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1849 | LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1850 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1851 | |
| 1852 | // If there are any cleanups between here and the loop-exit scope, |
| 1853 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1854 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1855 | if (LoopScope.requiresCleanups()) |
| 1856 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1857 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1858 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.dispatch.body"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1859 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1860 | if (ExitBlock != LoopExit.getBlock()) { |
| 1861 | EmitBlock(ExitBlock); |
| 1862 | EmitBranchThroughCleanup(LoopExit); |
| 1863 | } |
| 1864 | EmitBlock(LoopBody); |
| 1865 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1866 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1867 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1868 | if (DynamicOrOrdered) |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1869 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1870 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1871 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1872 | JumpDest Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1873 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1874 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1875 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1876 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1877 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1878 | LoopStack.setParallel(!IsMonotonic); |
| 1879 | else |
| 1880 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1881 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1882 | SourceLocation Loc = S.getBeginLoc(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1883 | |
| 1884 | // when 'distribute' is not combined with a 'for': |
| 1885 | // while (idx <= UB) { BODY; ++idx; } |
| 1886 | // when 'distribute' is combined with a 'for' |
| 1887 | // (e.g. 'distribute parallel for') |
| 1888 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 1889 | EmitOMPInnerLoop( |
| 1890 | S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, |
| 1891 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 1892 | CodeGenLoop(CGF, S, LoopExit); |
| 1893 | }, |
| 1894 | [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { |
| 1895 | CodeGenOrdered(CGF, Loc, IVSize, IVSigned); |
| 1896 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1897 | |
| 1898 | EmitBlock(Continue.getBlock()); |
| 1899 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1900 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1901 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1902 | EmitIgnoredExpr(LoopArgs.NextLB); |
| 1903 | EmitIgnoredExpr(LoopArgs.NextUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1904 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1905 | |
| 1906 | EmitBranch(CondBlock); |
| 1907 | LoopStack.pop(); |
| 1908 | // Emit the fall-through block. |
| 1909 | EmitBlock(LoopExit.getBlock()); |
| 1910 | |
| 1911 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1912 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 1913 | if (!DynamicOrOrdered) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1914 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 1915 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1916 | }; |
| 1917 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1921 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1922 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1923 | const OMPLoopArguments &LoopArgs, |
| 1924 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1925 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1926 | |
| 1927 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1928 | const bool DynamicOrOrdered = |
| 1929 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1930 | |
| 1931 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1932 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1933 | LoopArgs.Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1934 | "static non-chunked schedule does not need outer loop"); |
| 1935 | |
| 1936 | // Emit outer loop. |
| 1937 | // |
| 1938 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1939 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1940 | // distributed to threads in the team in chunks as the threads request them. |
| 1941 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1942 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1943 | // iterations, except for the last chunk to be distributed, which may have |
| 1944 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1945 | // |
| 1946 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1947 | // to threads in the team in chunks as the executing threads request them. |
| 1948 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1949 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1950 | // each chunk is proportional to the number of unassigned iterations divided |
| 1951 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1952 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1953 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1954 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1955 | // than k iterations). |
| 1956 | // |
| 1957 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1958 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1959 | // implementation the freedom to choose any possible mapping of iterations to |
| 1960 | // threads in the team. |
| 1961 | // |
| 1962 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1963 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1964 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1965 | // implementation defined |
| 1966 | // |
| 1967 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1968 | // idx = LB; |
| 1969 | // while (idx <= UB) { BODY; ++idx; |
| 1970 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1971 | // } // inner loop |
| 1972 | // } |
| 1973 | // |
| 1974 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1975 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1976 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1977 | // the team in a round-robin fashion in the order of the thread number. |
| 1978 | // |
| 1979 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1980 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1981 | // LB = LB + ST; |
| 1982 | // UB = UB + ST; |
| 1983 | // } |
| 1984 | // |
| 1985 | |
| 1986 | const Expr *IVExpr = S.getIterationVariable(); |
| 1987 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1988 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1989 | |
| 1990 | if (DynamicOrOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1991 | const std::pair<llvm::Value *, llvm::Value *> DispatchBounds = |
| 1992 | CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1993 | llvm::Value *LBVal = DispatchBounds.first; |
| 1994 | llvm::Value *UBVal = DispatchBounds.second; |
| 1995 | CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, |
| 1996 | LoopArgs.Chunk}; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1997 | RT.emitForDispatchInit(*this, S.getBeginLoc(), ScheduleKind, IVSize, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1998 | IVSigned, Ordered, DipatchRTInputValues); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1999 | } else { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2000 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2001 | IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, |
| 2002 | LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2003 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2004 | ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2007 | auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, |
| 2008 | const unsigned IVSize, |
| 2009 | const bool IVSigned) { |
| 2010 | if (Ordered) { |
| 2011 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, |
| 2012 | IVSigned); |
| 2013 | } |
| 2014 | }; |
| 2015 | |
| 2016 | OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, |
| 2017 | LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); |
| 2018 | OuterLoopArgs.IncExpr = S.getInc(); |
| 2019 | OuterLoopArgs.Init = S.getInit(); |
| 2020 | OuterLoopArgs.Cond = S.getCond(); |
| 2021 | OuterLoopArgs.NextLB = S.getNextLowerBound(); |
| 2022 | OuterLoopArgs.NextUB = S.getNextUpperBound(); |
| 2023 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, |
| 2024 | emitOMPLoopBodyWithStopPoint, CodeGenOrdered); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2027 | static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, |
| 2028 | const unsigned IVSize, const bool IVSigned) {} |
| 2029 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2030 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2031 | OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, |
| 2032 | OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, |
| 2033 | const CodeGenLoopTy &CodeGenLoopContent) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2034 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2035 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2036 | |
| 2037 | // Emit outer loop. |
| 2038 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 2039 | // dynamic |
| 2040 | // |
| 2041 | |
| 2042 | const Expr *IVExpr = S.getIterationVariable(); |
| 2043 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2044 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2045 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2046 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2047 | IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, |
| 2048 | LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2049 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2050 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2051 | // for combined 'distribute' and 'for' the increment expression of distribute |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2052 | // is stored in DistInc. For 'distribute' alone, it is in Inc. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2053 | Expr *IncExpr; |
| 2054 | if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) |
| 2055 | IncExpr = S.getDistInc(); |
| 2056 | else |
| 2057 | IncExpr = S.getInc(); |
| 2058 | |
| 2059 | // this routine is shared by 'omp distribute parallel for' and |
| 2060 | // 'omp distribute': select the right EUB expression depending on the |
| 2061 | // directive |
| 2062 | OMPLoopArguments OuterLoopArgs; |
| 2063 | OuterLoopArgs.LB = LoopArgs.LB; |
| 2064 | OuterLoopArgs.UB = LoopArgs.UB; |
| 2065 | OuterLoopArgs.ST = LoopArgs.ST; |
| 2066 | OuterLoopArgs.IL = LoopArgs.IL; |
| 2067 | OuterLoopArgs.Chunk = LoopArgs.Chunk; |
| 2068 | OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2069 | ? S.getCombinedEnsureUpperBound() |
| 2070 | : S.getEnsureUpperBound(); |
| 2071 | OuterLoopArgs.IncExpr = IncExpr; |
| 2072 | OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2073 | ? S.getCombinedInit() |
| 2074 | : S.getInit(); |
| 2075 | OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2076 | ? S.getCombinedCond() |
| 2077 | : S.getCond(); |
| 2078 | OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2079 | ? S.getCombinedNextLowerBound() |
| 2080 | : S.getNextLowerBound(); |
| 2081 | OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2082 | ? S.getCombinedNextUpperBound() |
| 2083 | : S.getNextUpperBound(); |
| 2084 | |
| 2085 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, |
| 2086 | LoopScope, OuterLoopArgs, CodeGenLoopContent, |
| 2087 | emitEmptyOrdered); |
| 2088 | } |
| 2089 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2090 | static std::pair<LValue, LValue> |
| 2091 | emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, |
| 2092 | const OMPExecutableDirective &S) { |
| 2093 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2094 | LValue LB = |
| 2095 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2096 | LValue UB = |
| 2097 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2098 | |
| 2099 | // When composing 'distribute' with 'for' (e.g. as in 'distribute |
| 2100 | // parallel for') we need to use the 'distribute' |
| 2101 | // chunk lower and upper bounds rather than the whole loop iteration |
| 2102 | // space. These are parameters to the outlined function for 'parallel' |
| 2103 | // and we copy the bounds of the previous schedule into the |
| 2104 | // the current ones. |
| 2105 | LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); |
| 2106 | LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2107 | llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar( |
| 2108 | PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2109 | PrevLBVal = CGF.EmitScalarConversion( |
| 2110 | PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2111 | LS.getIterationVariable()->getType(), |
| 2112 | LS.getPrevLowerBoundVariable()->getExprLoc()); |
| 2113 | llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar( |
| 2114 | PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2115 | PrevUBVal = CGF.EmitScalarConversion( |
| 2116 | PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2117 | LS.getIterationVariable()->getType(), |
| 2118 | LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2119 | |
| 2120 | CGF.EmitStoreOfScalar(PrevLBVal, LB); |
| 2121 | CGF.EmitStoreOfScalar(PrevUBVal, UB); |
| 2122 | |
| 2123 | return {LB, UB}; |
| 2124 | } |
| 2125 | |
| 2126 | /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then |
| 2127 | /// we need to use the LB and UB expressions generated by the worksharing |
| 2128 | /// code generation support, whereas in non combined situations we would |
| 2129 | /// just emit 0 and the LastIteration expression |
| 2130 | /// This function is necessary due to the difference of the LB and UB |
| 2131 | /// types for the RT emission routines for 'for_static_init' and |
| 2132 | /// 'for_dispatch_init' |
| 2133 | static std::pair<llvm::Value *, llvm::Value *> |
| 2134 | emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, |
| 2135 | const OMPExecutableDirective &S, |
| 2136 | Address LB, Address UB) { |
| 2137 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2138 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2139 | // when implementing a dynamic schedule for a 'for' combined with a |
| 2140 | // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop |
| 2141 | // is not normalized as each team only executes its own assigned |
| 2142 | // distribute chunk |
| 2143 | QualType IteratorTy = IVExpr->getType(); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2144 | llvm::Value *LBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2145 | CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2146 | llvm::Value *UBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2147 | CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2148 | return {LBVal, UBVal}; |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2149 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2150 | |
| 2151 | static void emitDistributeParallelForDistributeInnerBoundParams( |
| 2152 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2153 | llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { |
| 2154 | const auto &Dir = cast<OMPLoopDirective>(S); |
| 2155 | LValue LB = |
| 2156 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2157 | llvm::Value *LBCast = CGF.Builder.CreateIntCast( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2158 | CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2159 | CapturedVars.push_back(LBCast); |
| 2160 | LValue UB = |
| 2161 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); |
| 2162 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2163 | llvm::Value *UBCast = CGF.Builder.CreateIntCast( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2164 | CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2165 | CapturedVars.push_back(UBCast); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2166 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2167 | |
| 2168 | static void |
| 2169 | emitInnerParallelForWhenCombined(CodeGenFunction &CGF, |
| 2170 | const OMPLoopDirective &S, |
| 2171 | CodeGenFunction::JumpDest LoopExit) { |
| 2172 | auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2173 | PrePostActionTy &Action) { |
| 2174 | Action.Enter(CGF); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2175 | bool HasCancel = false; |
| 2176 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2177 | if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) |
| 2178 | HasCancel = D->hasCancel(); |
| 2179 | else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) |
| 2180 | HasCancel = D->hasCancel(); |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 2181 | else if (const auto *D = |
| 2182 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) |
| 2183 | HasCancel = D->hasCancel(); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2184 | } |
| 2185 | CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), |
| 2186 | HasCancel); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2187 | CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), |
| 2188 | emitDistributeParallelForInnerBounds, |
| 2189 | emitDistributeParallelForDispatchBounds); |
| 2190 | }; |
| 2191 | |
| 2192 | emitCommonOMPParallelDirective( |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2193 | CGF, S, |
| 2194 | isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, |
| 2195 | CGInlinedWorksharingLoop, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2196 | emitDistributeParallelForDistributeInnerBoundParams); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2199 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 2200 | const OMPDistributeParallelForDirective &S) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2201 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2202 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2203 | S.getDistInc()); |
| 2204 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2205 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 2206 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2207 | } |
| 2208 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2209 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 2210 | const OMPDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2211 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2212 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2213 | S.getDistInc()); |
| 2214 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2215 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2216 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2217 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2218 | |
| 2219 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 2220 | const OMPDistributeSimdDirective &S) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2221 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2222 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 2223 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2224 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2225 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2226 | } |
| 2227 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2228 | void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| 2229 | CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { |
| 2230 | // Emit SPMD target parallel for region as a standalone region. |
| 2231 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2232 | emitOMPSimdRegion(CGF, S, Action); |
| 2233 | }; |
| 2234 | llvm::Function *Fn; |
| 2235 | llvm::Constant *Addr; |
| 2236 | // Emit target region as a standalone region. |
| 2237 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 2238 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 2239 | assert(Fn && Addr && "Target device function emission failed."); |
| 2240 | } |
| 2241 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2242 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 2243 | const OMPTargetSimdDirective &S) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2244 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2245 | emitOMPSimdRegion(CGF, S, Action); |
| 2246 | }; |
| 2247 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2250 | namespace { |
| 2251 | struct ScheduleKindModifiersTy { |
| 2252 | OpenMPScheduleClauseKind Kind; |
| 2253 | OpenMPScheduleClauseModifier M1; |
| 2254 | OpenMPScheduleClauseModifier M2; |
| 2255 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2256 | OpenMPScheduleClauseModifier M1, |
| 2257 | OpenMPScheduleClauseModifier M2) |
| 2258 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2259 | }; |
| 2260 | } // namespace |
| 2261 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2262 | bool CodeGenFunction::EmitOMPWorksharingLoop( |
| 2263 | const OMPLoopDirective &S, Expr *EUB, |
| 2264 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
| 2265 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2266 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2267 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2268 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2269 | EmitVarDecl(*IVDecl); |
| 2270 | |
| 2271 | // Emit the iterations count variable. |
| 2272 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2273 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2274 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2275 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2276 | // Emit calculation of the iterations count. |
| 2277 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2278 | } |
| 2279 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2280 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2281 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2282 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2283 | // Check pre-condition. |
| 2284 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2285 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2286 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2287 | // If the condition constant folds and can be elided, avoid emitting the |
| 2288 | // whole loop. |
| 2289 | bool CondConstant; |
| 2290 | llvm::BasicBlock *ContBlock = nullptr; |
| 2291 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2292 | if (!CondConstant) |
| 2293 | return false; |
| 2294 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2295 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2296 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2297 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2298 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2299 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2300 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2301 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2302 | |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2303 | RunCleanupsScope DoacrossCleanupScope(*this); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2304 | bool Ordered = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2305 | if (const auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2306 | if (OrderedClause->getNumForLoops()) |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 2307 | RT.emitDoacrossInit(*this, S, OrderedClause->getLoopNumIterations()); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2308 | else |
| 2309 | Ordered = true; |
| 2310 | } |
| 2311 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2312 | llvm::DenseSet<const Expr *> EmittedFinals; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2313 | emitAlignedClause(*this, S); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2314 | bool HasLinears = EmitOMPLinearClauseInit(S); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2315 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2316 | |
| 2317 | std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S); |
| 2318 | LValue LB = Bounds.first; |
| 2319 | LValue UB = Bounds.second; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2320 | LValue ST = |
| 2321 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2322 | LValue IL = |
| 2323 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2324 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2325 | // Emit 'then' code. |
| 2326 | { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2327 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2328 | if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2329 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2330 | // initialization of firstprivate variables and post-update of |
| 2331 | // lastprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2332 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2333 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2334 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2335 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 2336 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2337 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 2338 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2339 | EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2340 | EmitOMPLinearClause(S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 2341 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2342 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2343 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2344 | |
| 2345 | // Detect the loop schedule kind and chunk. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2346 | const Expr *ChunkExpr = nullptr; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2347 | OpenMPScheduleTy ScheduleKind; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2348 | if (const auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2349 | ScheduleKind.Schedule = C->getScheduleKind(); |
| 2350 | ScheduleKind.M1 = C->getFirstScheduleModifier(); |
| 2351 | ScheduleKind.M2 = C->getSecondScheduleModifier(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2352 | ChunkExpr = C->getChunkSize(); |
Gheorghe-Teodor Bercea | 8233af9 | 2018-09-27 20:29:00 +0000 | [diff] [blame] | 2353 | } else { |
| 2354 | // Default behaviour for schedule clause. |
| 2355 | CGM.getOpenMPRuntime().getDefaultScheduleAndChunk( |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2356 | *this, S, ScheduleKind.Schedule, ChunkExpr); |
| 2357 | } |
| 2358 | bool HasChunkSizeOne = false; |
| 2359 | llvm::Value *Chunk = nullptr; |
| 2360 | if (ChunkExpr) { |
| 2361 | Chunk = EmitScalarExpr(ChunkExpr); |
| 2362 | Chunk = EmitScalarConversion(Chunk, ChunkExpr->getType(), |
| 2363 | S.getIterationVariable()->getType(), |
| 2364 | S.getBeginLoc()); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 2365 | Expr::EvalResult Result; |
| 2366 | if (ChunkExpr->EvaluateAsInt(Result, getContext())) { |
| 2367 | llvm::APSInt EvaluatedChunk = Result.Val.getInt(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2368 | HasChunkSizeOne = (EvaluatedChunk.getLimitedValue() == 1); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 2369 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2370 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2371 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2372 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2373 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 2374 | // If the static schedule kind is specified or if the ordered clause is |
| 2375 | // specified, and if no monotonic modifier is specified, the effect will |
| 2376 | // be as if the monotonic modifier was specified. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2377 | bool StaticChunkedOne = RT.isStaticChunked(ScheduleKind.Schedule, |
| 2378 | /* Chunked */ Chunk != nullptr) && HasChunkSizeOne && |
| 2379 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
| 2380 | if ((RT.isStaticNonchunked(ScheduleKind.Schedule, |
| 2381 | /* Chunked */ Chunk != nullptr) || |
| 2382 | StaticChunkedOne) && |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2383 | !Ordered) { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2384 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 2385 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2386 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2387 | // When no chunk_size is specified, the iteration space is divided into |
| 2388 | // chunks that are approximately equal in size, and at most one chunk is |
| 2389 | // distributed to each thread. Note that the size of the chunks is |
| 2390 | // unspecified in this case. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2391 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2392 | IVSize, IVSigned, Ordered, IL.getAddress(), LB.getAddress(), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2393 | UB.getAddress(), ST.getAddress(), |
| 2394 | StaticChunkedOne ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2395 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2396 | ScheduleKind, StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2397 | JumpDest LoopExit = |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2398 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2399 | // UB = min(UB, GlobalUB); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2400 | if (!StaticChunkedOne) |
| 2401 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2402 | // IV = LB; |
| 2403 | EmitIgnoredExpr(S.getInit()); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2404 | // For unchunked static schedule generate: |
| 2405 | // |
| 2406 | // while (idx <= UB) { |
| 2407 | // BODY; |
| 2408 | // ++idx; |
| 2409 | // } |
| 2410 | // |
| 2411 | // For static schedule with chunk one: |
| 2412 | // |
| 2413 | // while (IV <= PrevUB) { |
| 2414 | // BODY; |
| 2415 | // IV += ST; |
| 2416 | // } |
| 2417 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), |
| 2418 | StaticChunkedOne ? S.getCombinedParForInDistCond() : S.getCond(), |
| 2419 | StaticChunkedOne ? S.getDistInc() : S.getInc(), |
| 2420 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2421 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 2422 | CGF.EmitStopPoint(&S); |
| 2423 | }, |
| 2424 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2425 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2426 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2427 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2428 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2429 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2430 | }; |
| 2431 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2432 | } else { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2433 | const bool IsMonotonic = |
| 2434 | Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || |
| 2435 | ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || |
| 2436 | ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 2437 | ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2438 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2439 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2440 | const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(), |
| 2441 | ST.getAddress(), IL.getAddress(), |
| 2442 | Chunk, EUB); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2443 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2444 | LoopArguments, CGDispatchBounds); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2445 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2446 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2447 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
| 2448 | return CGF.Builder.CreateIsNotNull( |
| 2449 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
| 2450 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2451 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2452 | EmitOMPReductionClauseFinal( |
| 2453 | S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) |
| 2454 | ? /*Parallel and Simd*/ OMPD_parallel_for_simd |
| 2455 | : /*Parallel only*/ OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2456 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2457 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2458 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2459 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2460 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2461 | }); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2462 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2463 | if (HasLastprivateClause) |
| 2464 | EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2465 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2466 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2467 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2468 | EmitOMPLinearClauseFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2469 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2470 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2471 | }); |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2472 | DoacrossCleanupScope.ForceCleanup(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2473 | // 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] | 2474 | if (ContBlock) { |
| 2475 | EmitBranch(ContBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2476 | EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2477 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2478 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2479 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2482 | /// The following two functions generate expressions for the loop lower |
| 2483 | /// and upper bounds in case of static and dynamic (dispatch) schedule |
| 2484 | /// of the associated 'for' or 'distribute' loop. |
| 2485 | static std::pair<LValue, LValue> |
| 2486 | emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2487 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2488 | LValue LB = |
| 2489 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2490 | LValue UB = |
| 2491 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2492 | return {LB, UB}; |
| 2493 | } |
| 2494 | |
| 2495 | /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not |
| 2496 | /// consider the lower and upper bound expressions generated by the |
| 2497 | /// worksharing loop support, but we use 0 and the iteration space size as |
| 2498 | /// constants |
| 2499 | static std::pair<llvm::Value *, llvm::Value *> |
| 2500 | emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2501 | Address LB, Address UB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2502 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2503 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2504 | const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); |
| 2505 | llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); |
| 2506 | llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); |
| 2507 | return {LBVal, UBVal}; |
| 2508 | } |
| 2509 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2510 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2511 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2512 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2513 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2514 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2515 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2516 | emitForLoopBounds, |
| 2517 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2518 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2519 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2520 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2521 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2522 | S.hasCancel()); |
| 2523 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2524 | |
| 2525 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2526 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2527 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2528 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2529 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2530 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2531 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2532 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2533 | PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2534 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2535 | emitForLoopBounds, |
| 2536 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2537 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2538 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2539 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2540 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2541 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2542 | |
| 2543 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2544 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2545 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2548 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2549 | const Twine &Name, |
| 2550 | llvm::Value *Init = nullptr) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2551 | LValue LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2552 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2553 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2554 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2557 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2558 | const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt(); |
| 2559 | const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2560 | bool HasLastprivates = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2561 | auto &&CodeGen = [&S, CapturedStmt, CS, |
| 2562 | &HasLastprivates](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2563 | ASTContext &C = CGF.getContext(); |
| 2564 | QualType KmpInt32Ty = |
| 2565 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2566 | // Emit helper vars inits. |
| 2567 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2568 | CGF.Builder.getInt32(0)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2569 | llvm::ConstantInt *GlobalUBVal = CS != nullptr |
| 2570 | ? CGF.Builder.getInt32(CS->size() - 1) |
| 2571 | : CGF.Builder.getInt32(0); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2572 | LValue UB = |
| 2573 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2574 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2575 | CGF.Builder.getInt32(1)); |
| 2576 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2577 | CGF.Builder.getInt32(0)); |
| 2578 | // Loop counter. |
| 2579 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2580 | OpaqueValueExpr IVRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2581 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2582 | OpaqueValueExpr UBRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2583 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2584 | // Generate condition for loop. |
| 2585 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2586 | OK_Ordinary, S.getBeginLoc(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2587 | // Increment for loop counter. |
| 2588 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2589 | S.getBeginLoc(), true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2590 | auto &&BodyGen = [CapturedStmt, CS, &S, &IV](CodeGenFunction &CGF) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2591 | // Iterate through all sections and emit a switch construct: |
| 2592 | // switch (IV) { |
| 2593 | // case 0: |
| 2594 | // <SectionStmt[0]>; |
| 2595 | // break; |
| 2596 | // ... |
| 2597 | // case <NumSection> - 1: |
| 2598 | // <SectionStmt[<NumSection> - 1]>; |
| 2599 | // break; |
| 2600 | // } |
| 2601 | // .omp.sections.exit: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2602 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 2603 | llvm::SwitchInst *SwitchStmt = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2604 | CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getBeginLoc()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2605 | ExitBB, CS == nullptr ? 1 : CS->size()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2606 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2607 | unsigned CaseNumber = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2608 | for (const Stmt *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2609 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2610 | CGF.EmitBlock(CaseBB); |
| 2611 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2612 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2613 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2614 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2615 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2616 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2617 | llvm::BasicBlock *CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2618 | CGF.EmitBlock(CaseBB); |
| 2619 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2620 | CGF.EmitStmt(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2621 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2622 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2623 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2624 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2625 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2626 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2627 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2628 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2629 | // initialization of firstprivate variables and post-update of lastprivate |
| 2630 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2631 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2632 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2633 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2634 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2635 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2636 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2637 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2638 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2639 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2640 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2641 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2642 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2643 | OpenMPScheduleTy ScheduleKind; |
| 2644 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2645 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2646 | /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), |
| 2647 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2648 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2649 | CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, StaticInit); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2650 | // UB = min(UB, GlobalUB); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2651 | llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, S.getBeginLoc()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2652 | llvm::Value *MinUBGlobalUB = CGF.Builder.CreateSelect( |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2653 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 2654 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 2655 | // IV = LB; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2656 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getBeginLoc()), IV); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2657 | // while (idx <= UB) { BODY; ++idx; } |
| 2658 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 2659 | [](CodeGenFunction &) {}); |
| 2660 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2661 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2662 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2663 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2664 | }; |
| 2665 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2666 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2667 | // Emit post-update of the reduction variables if IsLastIter != 0. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2668 | emitPostUpdateForReductionClause(CGF, S, [IL, &S](CodeGenFunction &CGF) { |
| 2669 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2670 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2671 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2672 | |
| 2673 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2674 | if (HasLastprivates) |
| 2675 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2676 | S, /*NoFinals=*/false, |
| 2677 | CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2678 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2679 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2680 | |
| 2681 | bool HasCancel = false; |
| 2682 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 2683 | HasCancel = OSD->hasCancel(); |
| 2684 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 2685 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2686 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2687 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 2688 | HasCancel); |
| 2689 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 2690 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 2691 | // directive. |
| 2692 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2693 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2694 | // initialization of firstprivate variables. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2695 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2696 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2697 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2698 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2699 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2700 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2701 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2702 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2703 | EmitSections(S); |
| 2704 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2705 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2706 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2707 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2708 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2709 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2713 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2714 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2715 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2716 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2717 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 2718 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2719 | } |
| 2720 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2721 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2722 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2723 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2724 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2725 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2726 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2727 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2728 | // Build a list of copyprivate variables along with helper expressions |
| 2729 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2730 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2731 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2732 | DestExprs.append(C->destination_exprs().begin(), |
| 2733 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2734 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2735 | AssignmentOps.append(C->assignment_ops().begin(), |
| 2736 | C->assignment_ops().end()); |
| 2737 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2738 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 2739 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2740 | Action.Enter(CGF); |
| 2741 | OMPPrivateScope SingleScope(CGF); |
| 2742 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 2743 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 2744 | (void)SingleScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2745 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2746 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2747 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2748 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2749 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2750 | CopyprivateVars, DestExprs, |
| 2751 | SrcExprs, AssignmentOps); |
| 2752 | } |
| 2753 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 2754 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2755 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 2756 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2757 | *this, S.getBeginLoc(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2758 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2759 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2760 | } |
| 2761 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2762 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2763 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2764 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2765 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2766 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2767 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2768 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2771 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2772 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2773 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2774 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2775 | }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2776 | const Expr *Hint = nullptr; |
| 2777 | if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2778 | Hint = HintClause->getHint(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2779 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2780 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 2781 | S.getDirectiveName().getAsString(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2782 | CodeGen, S.getBeginLoc(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2783 | } |
| 2784 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2785 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 2786 | const OMPParallelForDirective &S) { |
| 2787 | // Emit directive as a combined directive that consists of two implicit |
| 2788 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2789 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2790 | Action.Enter(CGF); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2791 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2792 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2793 | emitDispatchForLoopBounds); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2794 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2795 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, |
| 2796 | emitEmptyBoundParameters); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2797 | } |
| 2798 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2799 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2800 | const OMPParallelForSimdDirective &S) { |
| 2801 | // Emit directive as a combined directive that consists of two implicit |
| 2802 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2803 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2804 | Action.Enter(CGF); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2805 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2806 | emitDispatchForLoopBounds); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2807 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2808 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, |
| 2809 | emitEmptyBoundParameters); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2810 | } |
| 2811 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2812 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2813 | const OMPParallelSectionsDirective &S) { |
| 2814 | // Emit directive as a combined directive that consists of two implicit |
| 2815 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2816 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2817 | Action.Enter(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2818 | CGF.EmitSections(S); |
| 2819 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2820 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, |
| 2821 | emitEmptyBoundParameters); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2824 | void CodeGenFunction::EmitOMPTaskBasedDirective( |
| 2825 | const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, |
| 2826 | const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, |
| 2827 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2828 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2829 | const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2830 | auto I = CS->getCapturedDecl()->param_begin(); |
| 2831 | auto PartId = std::next(I); |
| 2832 | auto TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2833 | // Check if the task is final |
| 2834 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 2835 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2836 | // the condition and the dead arm of the if/else. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2837 | const Expr *Cond = Clause->getCondition(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2838 | bool CondConstant; |
| 2839 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2840 | Data.Final.setInt(CondConstant); |
| 2841 | else |
| 2842 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2843 | } else { |
| 2844 | // By default the task is not final. |
| 2845 | Data.Final.setInt(/*IntVal=*/false); |
| 2846 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2847 | // Check if the task has 'priority' clause. |
| 2848 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2849 | const Expr *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 2850 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 2851 | Data.Priority.setPointer(EmitScalarConversion( |
| 2852 | EmitScalarExpr(Prio), Prio->getType(), |
| 2853 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2854 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2855 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2856 | // The first function argument for tasks is a thread id, the second one is a |
| 2857 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2858 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 2859 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2860 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2861 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2862 | for (const Expr *IInit : C->private_copies()) { |
| 2863 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2864 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2865 | Data.PrivateVars.push_back(*IRef); |
| 2866 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2867 | } |
| 2868 | ++IRef; |
| 2869 | } |
| 2870 | } |
| 2871 | EmittedAsPrivate.clear(); |
| 2872 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2873 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2874 | auto IRef = C->varlist_begin(); |
| 2875 | auto IElemInitRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2876 | for (const Expr *IInit : C->private_copies()) { |
| 2877 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2878 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2879 | Data.FirstprivateVars.push_back(*IRef); |
| 2880 | Data.FirstprivateCopies.push_back(IInit); |
| 2881 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2882 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 2883 | ++IRef; |
| 2884 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2885 | } |
| 2886 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2887 | // Get list of lastprivate variables (for taskloops). |
| 2888 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 2889 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 2890 | auto IRef = C->varlist_begin(); |
| 2891 | auto ID = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2892 | for (const Expr *IInit : C->private_copies()) { |
| 2893 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2894 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2895 | Data.LastprivateVars.push_back(*IRef); |
| 2896 | Data.LastprivateCopies.push_back(IInit); |
| 2897 | } |
| 2898 | LastprivateDstsOrigs.insert( |
| 2899 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 2900 | cast<DeclRefExpr>(*IRef)}); |
| 2901 | ++IRef; |
| 2902 | ++ID; |
| 2903 | } |
| 2904 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2905 | SmallVector<const Expr *, 4> LHSs; |
| 2906 | SmallVector<const Expr *, 4> RHSs; |
| 2907 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 2908 | auto IPriv = C->privates().begin(); |
| 2909 | auto IRed = C->reduction_ops().begin(); |
| 2910 | auto ILHS = C->lhs_exprs().begin(); |
| 2911 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2912 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2913 | Data.ReductionVars.emplace_back(Ref); |
| 2914 | Data.ReductionCopies.emplace_back(*IPriv); |
| 2915 | Data.ReductionOps.emplace_back(*IRed); |
| 2916 | LHSs.emplace_back(*ILHS); |
| 2917 | RHSs.emplace_back(*IRHS); |
| 2918 | std::advance(IPriv, 1); |
| 2919 | std::advance(IRed, 1); |
| 2920 | std::advance(ILHS, 1); |
| 2921 | std::advance(IRHS, 1); |
| 2922 | } |
| 2923 | } |
| 2924 | Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2925 | *this, S.getBeginLoc(), LHSs, RHSs, Data); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2926 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2927 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2928 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 2929 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2930 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, |
| 2931 | CapturedRegion](CodeGenFunction &CGF, |
| 2932 | PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2933 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2934 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2935 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 2936 | !Data.LastprivateVars.empty()) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2937 | llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( |
| 2938 | CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2939 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2940 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 2941 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 2942 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 2943 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2944 | // Map privates. |
| 2945 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 2946 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2947 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2948 | for (const Expr *E : Data.PrivateVars) { |
| 2949 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2950 | Address PrivatePtr = CGF.CreateMemTemp( |
| 2951 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2952 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2953 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2954 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2955 | for (const Expr *E : Data.FirstprivateVars) { |
| 2956 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2957 | Address PrivatePtr = |
| 2958 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2959 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2960 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2961 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2962 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2963 | for (const Expr *E : Data.LastprivateVars) { |
| 2964 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2965 | Address PrivatePtr = |
| 2966 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2967 | ".lastpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2968 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2969 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 2970 | } |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 2971 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 2972 | CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2973 | for (const auto &Pair : LastprivateDstsOrigs) { |
| 2974 | const auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 2975 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(OrigVD), |
| 2976 | /*RefersToEnclosingVariableOrCapture=*/ |
| 2977 | CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 2978 | Pair.second->getType(), VK_LValue, |
| 2979 | Pair.second->getExprLoc()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2980 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
| 2981 | return CGF.EmitLValue(&DRE).getAddress(); |
| 2982 | }); |
| 2983 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2984 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2985 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2986 | CGF.getContext().getDeclAlign(Pair.first)); |
| 2987 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2988 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2989 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2990 | if (Data.Reductions) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2991 | OMPLexicalScope LexScope(CGF, S, CapturedRegion); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2992 | ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies, |
| 2993 | Data.ReductionOps); |
| 2994 | llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( |
| 2995 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); |
| 2996 | for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { |
| 2997 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2998 | RedCG.emitAggregateType(CGF, Cnt); |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2999 | // FIXME: This must removed once the runtime library is fixed. |
| 3000 | // Emit required threadprivate variables for |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3001 | // initializer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3002 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3003 | RedCG, Cnt); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3004 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3005 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3006 | Replacement = |
| 3007 | Address(CGF.EmitScalarConversion( |
| 3008 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 3009 | CGF.getContext().getPointerType( |
| 3010 | Data.ReductionCopies[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3011 | Data.ReductionCopies[Cnt]->getExprLoc()), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3012 | Replacement.getAlignment()); |
| 3013 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 3014 | Scope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 3015 | [Replacement]() { return Replacement; }); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3016 | } |
| 3017 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3018 | // Privatize all private variables except for in_reduction items. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3019 | (void)Scope.Privatize(); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3020 | SmallVector<const Expr *, 4> InRedVars; |
| 3021 | SmallVector<const Expr *, 4> InRedPrivs; |
| 3022 | SmallVector<const Expr *, 4> InRedOps; |
| 3023 | SmallVector<const Expr *, 4> TaskgroupDescriptors; |
| 3024 | for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { |
| 3025 | auto IPriv = C->privates().begin(); |
| 3026 | auto IRed = C->reduction_ops().begin(); |
| 3027 | auto ITD = C->taskgroup_descriptors().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3028 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3029 | InRedVars.emplace_back(Ref); |
| 3030 | InRedPrivs.emplace_back(*IPriv); |
| 3031 | InRedOps.emplace_back(*IRed); |
| 3032 | TaskgroupDescriptors.emplace_back(*ITD); |
| 3033 | std::advance(IPriv, 1); |
| 3034 | std::advance(IRed, 1); |
| 3035 | std::advance(ITD, 1); |
| 3036 | } |
| 3037 | } |
| 3038 | // Privatize in_reduction items here, because taskgroup descriptors must be |
| 3039 | // privatized earlier. |
| 3040 | OMPPrivateScope InRedScope(CGF); |
| 3041 | if (!InRedVars.empty()) { |
| 3042 | ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps); |
| 3043 | for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { |
| 3044 | RedCG.emitSharedLValue(CGF, Cnt); |
| 3045 | RedCG.emitAggregateType(CGF, Cnt); |
| 3046 | // The taskgroup descriptor variable is always implicit firstprivate and |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3047 | // privatized already during processing of the firstprivates. |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3048 | // FIXME: This must removed once the runtime library is fixed. |
| 3049 | // Emit required threadprivate variables for |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3050 | // initializer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3051 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3052 | RedCG, Cnt); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3053 | llvm::Value *ReductionsPtr = |
| 3054 | CGF.EmitLoadOfScalar(CGF.EmitLValue(TaskgroupDescriptors[Cnt]), |
| 3055 | TaskgroupDescriptors[Cnt]->getExprLoc()); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3056 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3057 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3058 | Replacement = Address( |
| 3059 | CGF.EmitScalarConversion( |
| 3060 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 3061 | CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3062 | InRedPrivs[Cnt]->getExprLoc()), |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3063 | Replacement.getAlignment()); |
| 3064 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 3065 | InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 3066 | [Replacement]() { return Replacement; }); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3067 | } |
| 3068 | } |
| 3069 | (void)InRedScope.Privatize(); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3070 | |
| 3071 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3072 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3073 | }; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3074 | llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3075 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 3076 | Data.NumberOfParts); |
| 3077 | OMPLexicalScope Scope(*this, S); |
| 3078 | TaskGen(*this, OutlinedFn, Data); |
| 3079 | } |
| 3080 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3081 | static ImplicitParamDecl * |
| 3082 | createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3083 | QualType Ty, CapturedDecl *CD, |
| 3084 | SourceLocation Loc) { |
| 3085 | auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3086 | ImplicitParamDecl::Other); |
| 3087 | auto *OrigRef = DeclRefExpr::Create( |
| 3088 | C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, |
| 3089 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
| 3090 | auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3091 | ImplicitParamDecl::Other); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3092 | auto *PrivateRef = DeclRefExpr::Create( |
| 3093 | C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3094 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3095 | QualType ElemType = C.getBaseElementType(Ty); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3096 | auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType, |
| 3097 | ImplicitParamDecl::Other); |
| 3098 | auto *InitRef = DeclRefExpr::Create( |
| 3099 | C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, |
| 3100 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3101 | PrivateVD->setInitStyle(VarDecl::CInit); |
| 3102 | PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, |
| 3103 | InitRef, /*BasePath=*/nullptr, |
| 3104 | VK_RValue)); |
| 3105 | Data.FirstprivateVars.emplace_back(OrigRef); |
| 3106 | Data.FirstprivateCopies.emplace_back(PrivateRef); |
| 3107 | Data.FirstprivateInits.emplace_back(InitRef); |
| 3108 | return OrigVD; |
| 3109 | } |
| 3110 | |
| 3111 | void CodeGenFunction::EmitOMPTargetTaskBasedDirective( |
| 3112 | const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, |
| 3113 | OMPTargetDataInfo &InputInfo) { |
| 3114 | // Emit outlined function for task construct. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3115 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
| 3116 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3117 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3118 | auto I = CS->getCapturedDecl()->param_begin(); |
| 3119 | auto PartId = std::next(I); |
| 3120 | auto TaskT = std::next(I, 4); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3121 | OMPTaskDataTy Data; |
| 3122 | // The task is not final. |
| 3123 | Data.Final.setInt(/*IntVal=*/false); |
| 3124 | // Get list of firstprivate variables. |
| 3125 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 3126 | auto IRef = C->varlist_begin(); |
| 3127 | auto IElemInitRef = C->inits().begin(); |
| 3128 | for (auto *IInit : C->private_copies()) { |
| 3129 | Data.FirstprivateVars.push_back(*IRef); |
| 3130 | Data.FirstprivateCopies.push_back(IInit); |
| 3131 | Data.FirstprivateInits.push_back(*IElemInitRef); |
| 3132 | ++IRef; |
| 3133 | ++IElemInitRef; |
| 3134 | } |
| 3135 | } |
| 3136 | OMPPrivateScope TargetScope(*this); |
| 3137 | VarDecl *BPVD = nullptr; |
| 3138 | VarDecl *PVD = nullptr; |
| 3139 | VarDecl *SVD = nullptr; |
| 3140 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3141 | auto *CD = CapturedDecl::Create( |
| 3142 | getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); |
| 3143 | llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); |
| 3144 | QualType BaseAndPointersType = getContext().getConstantArrayType( |
| 3145 | getContext().VoidPtrTy, ArrSize, ArrayType::Normal, |
| 3146 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3147 | BPVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3148 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3149 | PVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3150 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3151 | QualType SizesType = getContext().getConstantArrayType( |
Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 3152 | getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1), |
| 3153 | ArrSize, ArrayType::Normal, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3154 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3155 | SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3156 | S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3157 | TargetScope.addPrivate( |
| 3158 | BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); |
| 3159 | TargetScope.addPrivate(PVD, |
| 3160 | [&InputInfo]() { return InputInfo.PointersArray; }); |
| 3161 | TargetScope.addPrivate(SVD, |
| 3162 | [&InputInfo]() { return InputInfo.SizesArray; }); |
| 3163 | } |
| 3164 | (void)TargetScope.Privatize(); |
| 3165 | // Build list of dependences. |
| 3166 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3167 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3168 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3169 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, |
| 3170 | &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3171 | // Set proper addresses for generated private copies. |
| 3172 | OMPPrivateScope Scope(CGF); |
| 3173 | if (!Data.FirstprivateVars.empty()) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3174 | llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( |
| 3175 | CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3176 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3177 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 3178 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 3179 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 3180 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3181 | // Map privates. |
| 3182 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3183 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3184 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3185 | for (const Expr *E : Data.FirstprivateVars) { |
| 3186 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3187 | Address PrivatePtr = |
| 3188 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3189 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3190 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3191 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3192 | } |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3193 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 3194 | CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3195 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3196 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3197 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3198 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3199 | } |
| 3200 | } |
| 3201 | // Privatize all private variables except for in_reduction items. |
| 3202 | (void)Scope.Privatize(); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3203 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3204 | InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3205 | CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3206 | InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3207 | CGF.GetAddrOfLocalVar(PVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3208 | InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3209 | CGF.GetAddrOfLocalVar(SVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3210 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3211 | |
| 3212 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3213 | OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3214 | BodyGen(CGF); |
| 3215 | }; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3216 | llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3217 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, |
| 3218 | Data.NumberOfParts); |
| 3219 | llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); |
| 3220 | IntegerLiteral IfCond(getContext(), TrueOrFalse, |
| 3221 | getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3222 | SourceLocation()); |
| 3223 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3224 | CGM.getOpenMPRuntime().emitTaskCall(*this, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3225 | SharedsTy, CapturedStruct, &IfCond, Data); |
| 3226 | } |
| 3227 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3228 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 3229 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3230 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3231 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3232 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3233 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 3234 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3235 | if (C->getNameModifier() == OMPD_unknown || |
| 3236 | C->getNameModifier() == OMPD_task) { |
| 3237 | IfCond = C->getCondition(); |
| 3238 | break; |
| 3239 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3240 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3241 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3242 | OMPTaskDataTy Data; |
| 3243 | // Check if we should emit tied or untied task. |
| 3244 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3245 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3246 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3247 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3248 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3249 | IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3250 | const OMPTaskDataTy &Data) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3251 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3252 | SharedsTy, CapturedStruct, IfCond, |
| 3253 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3254 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3255 | EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3256 | } |
| 3257 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3258 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 3259 | const OMPTaskyieldDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3260 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getBeginLoc()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3263 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3264 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3267 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3268 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getBeginLoc()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3271 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 3272 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3273 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3274 | Action.Enter(CGF); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3275 | if (const Expr *E = S.getReductionRef()) { |
| 3276 | SmallVector<const Expr *, 4> LHSs; |
| 3277 | SmallVector<const Expr *, 4> RHSs; |
| 3278 | OMPTaskDataTy Data; |
| 3279 | for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { |
| 3280 | auto IPriv = C->privates().begin(); |
| 3281 | auto IRed = C->reduction_ops().begin(); |
| 3282 | auto ILHS = C->lhs_exprs().begin(); |
| 3283 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3284 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3285 | Data.ReductionVars.emplace_back(Ref); |
| 3286 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3287 | Data.ReductionOps.emplace_back(*IRed); |
| 3288 | LHSs.emplace_back(*ILHS); |
| 3289 | RHSs.emplace_back(*IRHS); |
| 3290 | std::advance(IPriv, 1); |
| 3291 | std::advance(IRed, 1); |
| 3292 | std::advance(ILHS, 1); |
| 3293 | std::advance(IRHS, 1); |
| 3294 | } |
| 3295 | } |
| 3296 | llvm::Value *ReductionDesc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3297 | CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getBeginLoc(), |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3298 | LHSs, RHSs, Data); |
| 3299 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3300 | CGF.EmitVarDecl(*VD); |
| 3301 | CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), |
| 3302 | /*Volatile=*/false, E->getType()); |
| 3303 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3304 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3305 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3306 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3307 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3310 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3311 | CGM.getOpenMPRuntime().emitFlush( |
| 3312 | *this, |
| 3313 | [&S]() -> ArrayRef<const Expr *> { |
| 3314 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) |
| 3315 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 3316 | FlushClause->varlist_end()); |
| 3317 | return llvm::None; |
| 3318 | }(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3319 | S.getBeginLoc()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3322 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, |
| 3323 | const CodeGenLoopTy &CodeGenLoop, |
| 3324 | Expr *IncExpr) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3325 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3326 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 3327 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3328 | EmitVarDecl(*IVDecl); |
| 3329 | |
| 3330 | // Emit the iterations count variable. |
| 3331 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 3332 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3333 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3334 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3335 | // Emit calculation of the iterations count. |
| 3336 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3337 | } |
| 3338 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3339 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3340 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3341 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3342 | // Check pre-condition. |
| 3343 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3344 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3345 | // Skip the entire loop if we don't meet the precondition. |
| 3346 | // If the condition constant folds and can be elided, avoid emitting the |
| 3347 | // whole loop. |
| 3348 | bool CondConstant; |
| 3349 | llvm::BasicBlock *ContBlock = nullptr; |
| 3350 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3351 | if (!CondConstant) |
| 3352 | return; |
| 3353 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3354 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3355 | ContBlock = createBasicBlock("omp.precond.end"); |
| 3356 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3357 | getProfileCount(&S)); |
| 3358 | EmitBlock(ThenBlock); |
| 3359 | incrementProfileCounter(&S); |
| 3360 | } |
| 3361 | |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3362 | emitAlignedClause(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3363 | // Emit 'then' code. |
| 3364 | { |
| 3365 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3366 | |
| 3367 | LValue LB = EmitOMPHelperVar( |
| 3368 | *this, cast<DeclRefExpr>( |
| 3369 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3370 | ? S.getCombinedLowerBoundVariable() |
| 3371 | : S.getLowerBoundVariable()))); |
| 3372 | LValue UB = EmitOMPHelperVar( |
| 3373 | *this, cast<DeclRefExpr>( |
| 3374 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3375 | ? S.getCombinedUpperBoundVariable() |
| 3376 | : S.getUpperBoundVariable()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3377 | LValue ST = |
| 3378 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 3379 | LValue IL = |
| 3380 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 3381 | |
| 3382 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3383 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3384 | // Emit implicit barrier to synchronize threads and avoid data races |
| 3385 | // on initialization of firstprivate variables and post-update of |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3386 | // lastprivate variables. |
| 3387 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3388 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3389 | /*ForceSimpleCall=*/true); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3390 | } |
| 3391 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3392 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 3393 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3394 | !isOpenMPTeamsDirective(S.getDirectiveKind())) |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3395 | EmitOMPReductionClauseInit(S, LoopScope); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3396 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3397 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3398 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 3399 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 3400 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3401 | |
| 3402 | // Detect the distribute schedule kind and chunk. |
| 3403 | llvm::Value *Chunk = nullptr; |
| 3404 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3405 | if (const auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3406 | ScheduleKind = C->getDistScheduleKind(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3407 | if (const Expr *Ch = C->getChunkSize()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3408 | Chunk = EmitScalarExpr(Ch); |
| 3409 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3410 | S.getIterationVariable()->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3411 | S.getBeginLoc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3412 | } |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 3413 | } else { |
| 3414 | // Default behaviour for dist_schedule clause. |
| 3415 | CGM.getOpenMPRuntime().getDefaultDistScheduleAndChunk( |
| 3416 | *this, S, ScheduleKind, Chunk); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3417 | } |
| 3418 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 3419 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 3420 | |
| 3421 | // OpenMP [2.10.8, distribute Construct, Description] |
| 3422 | // If dist_schedule is specified, kind must be static. If specified, |
| 3423 | // iterations are divided into chunks of size chunk_size, chunks are |
| 3424 | // assigned to the teams of the league in a round-robin fashion in the |
| 3425 | // order of the team number. When no chunk_size is specified, the |
| 3426 | // iteration space is divided into chunks that are approximately equal |
| 3427 | // in size, and at most one chunk is distributed to each team of the |
| 3428 | // league. The size of the chunks is unspecified in this case. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3429 | bool StaticChunked = RT.isStaticChunked( |
| 3430 | ScheduleKind, /* Chunked */ Chunk != nullptr) && |
| 3431 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3432 | if (RT.isStaticNonchunked(ScheduleKind, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3433 | /* Chunked */ Chunk != nullptr) || |
| 3434 | StaticChunked) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3435 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 3436 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3437 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 3438 | IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3439 | LB.getAddress(), UB.getAddress(), ST.getAddress(), |
| 3440 | StaticChunked ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3441 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3442 | StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3443 | JumpDest LoopExit = |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3444 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 3445 | // UB = min(UB, GlobalUB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3446 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3447 | ? S.getCombinedEnsureUpperBound() |
| 3448 | : S.getEnsureUpperBound()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3449 | // IV = LB; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3450 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3451 | ? S.getCombinedInit() |
| 3452 | : S.getInit()); |
| 3453 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3454 | const Expr *Cond = |
| 3455 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3456 | ? S.getCombinedCond() |
| 3457 | : S.getCond(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3458 | |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3459 | if (StaticChunked) |
| 3460 | Cond = S.getCombinedDistCond(); |
| 3461 | |
| 3462 | // For static unchunked schedules generate: |
| 3463 | // |
| 3464 | // 1. For distribute alone, codegen |
| 3465 | // while (idx <= UB) { |
| 3466 | // BODY; |
| 3467 | // ++idx; |
| 3468 | // } |
| 3469 | // |
| 3470 | // 2. When combined with 'for' (e.g. as in 'distribute parallel for') |
| 3471 | // while (idx <= UB) { |
| 3472 | // <CodeGen rest of pragma>(LB, UB); |
| 3473 | // idx += ST; |
| 3474 | // } |
| 3475 | // |
| 3476 | // For static chunk one schedule generate: |
| 3477 | // |
| 3478 | // while (IV <= GlobalUB) { |
| 3479 | // <CodeGen rest of pragma>(LB, UB); |
| 3480 | // LB += ST; |
| 3481 | // UB += ST; |
| 3482 | // UB = min(UB, GlobalUB); |
| 3483 | // IV = LB; |
| 3484 | // } |
| 3485 | // |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3486 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr, |
| 3487 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 3488 | CodeGenLoop(CGF, S, LoopExit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3489 | }, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3490 | [&S, StaticChunked](CodeGenFunction &CGF) { |
| 3491 | if (StaticChunked) { |
| 3492 | CGF.EmitIgnoredExpr(S.getCombinedNextLowerBound()); |
| 3493 | CGF.EmitIgnoredExpr(S.getCombinedNextUpperBound()); |
| 3494 | CGF.EmitIgnoredExpr(S.getCombinedEnsureUpperBound()); |
| 3495 | CGF.EmitIgnoredExpr(S.getCombinedInit()); |
| 3496 | } |
| 3497 | }); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3498 | EmitBlock(LoopExit.getBlock()); |
| 3499 | // Tell the runtime we are done. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3500 | RT.emitForStaticFinish(*this, S.getBeginLoc(), S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3501 | } else { |
| 3502 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 3503 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3504 | const OMPLoopArguments LoopArguments = { |
| 3505 | LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(), |
| 3506 | Chunk}; |
| 3507 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, |
| 3508 | CodeGenLoop); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3509 | } |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3510 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3511 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3512 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3513 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3514 | }); |
| 3515 | } |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3516 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 3517 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3518 | !isOpenMPTeamsDirective(S.getDirectiveKind())) { |
Jonas Hahnfeld | 5aaaece | 2018-10-02 19:12:47 +0000 | [diff] [blame] | 3519 | EmitOMPReductionClauseFinal(S, OMPD_simd); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3520 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 3521 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3522 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3523 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3524 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3525 | }); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3526 | } |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3527 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3528 | if (HasLastprivateClause) { |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3529 | EmitOMPLastprivateClauseFinal( |
| 3530 | S, /*NoFinals=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3531 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3532 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | // We're now done with the loop, so jump to the continuation block. |
| 3536 | if (ContBlock) { |
| 3537 | EmitBranch(ContBlock); |
| 3538 | EmitBlock(ContBlock, true); |
| 3539 | } |
| 3540 | } |
| 3541 | } |
| 3542 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3543 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 3544 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3545 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3546 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3547 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3548 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 3549 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3550 | } |
| 3551 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3552 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 3553 | const CapturedStmt *S) { |
| 3554 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 3555 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 3556 | CGF.CapturedStmtInfo = &CapStmtInfo; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3557 | llvm::Function *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 3558 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3559 | return Fn; |
| 3560 | } |
| 3561 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3562 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3563 | if (S.hasClausesOfKind<OMPDependClause>()) { |
| 3564 | assert(!S.getAssociatedStmt() && |
| 3565 | "No associated statement must be in ordered depend construct."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3566 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 3567 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3568 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3569 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3570 | const auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3571 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 3572 | PrePostActionTy &Action) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3573 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3574 | if (C) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3575 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3576 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3577 | llvm::Function *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3578 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3579 | OutlinedFn, CapturedVars); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3580 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3581 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3582 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3583 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3584 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3585 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3586 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getBeginLoc(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3589 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3590 | QualType SrcType, QualType DestType, |
| 3591 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3592 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 3593 | "DestType must have scalar evaluation kind."); |
| 3594 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3595 | return Val.isScalar() ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 3596 | DestType, Loc) |
| 3597 | : CGF.EmitComplexToScalarConversion( |
| 3598 | Val.getComplexVal(), SrcType, DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | static CodeGenFunction::ComplexPairTy |
| 3602 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3603 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3604 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 3605 | "DestType must have complex evaluation kind."); |
| 3606 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 3607 | if (Val.isScalar()) { |
| 3608 | // Convert the input element to the element type of the complex. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3609 | QualType DestElementType = |
| 3610 | DestType->castAs<ComplexType>()->getElementType(); |
| 3611 | llvm::Value *ScalarVal = CGF.EmitScalarConversion( |
| 3612 | Val.getScalarVal(), SrcType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3613 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 3614 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 3615 | } else { |
| 3616 | assert(Val.isComplex() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3617 | QualType SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 3618 | QualType DestElementType = |
| 3619 | DestType->castAs<ComplexType>()->getElementType(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3620 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3621 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3622 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3623 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3624 | } |
| 3625 | return ComplexVal; |
| 3626 | } |
| 3627 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3628 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 3629 | LValue LVal, RValue RVal) { |
| 3630 | if (LVal.isGlobalReg()) { |
| 3631 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 3632 | } else { |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3633 | CGF.EmitAtomicStore(RVal, LVal, |
| 3634 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3635 | : llvm::AtomicOrdering::Monotonic, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 3636 | LVal.isVolatile(), /*isInit=*/false); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3637 | } |
| 3638 | } |
| 3639 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3640 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 3641 | QualType RValTy, SourceLocation Loc) { |
| 3642 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3643 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3644 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 3645 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 3646 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3647 | break; |
| 3648 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3649 | EmitStoreOfComplex( |
| 3650 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3651 | /*isInit=*/false); |
| 3652 | break; |
| 3653 | case TEK_Aggregate: |
| 3654 | llvm_unreachable("Must be a scalar or complex."); |
| 3655 | } |
| 3656 | } |
| 3657 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3658 | static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3659 | const Expr *X, const Expr *V, |
| 3660 | SourceLocation Loc) { |
| 3661 | // v = x; |
| 3662 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 3663 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 3664 | LValue XLValue = CGF.EmitLValue(X); |
| 3665 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 3666 | RValue Res = XLValue.isGlobalReg() |
| 3667 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3668 | : CGF.EmitAtomicLoad( |
| 3669 | XLValue, Loc, |
| 3670 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3671 | : llvm::AtomicOrdering::Monotonic, |
| 3672 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3673 | // OpenMP, 2.12.6, atomic Construct |
| 3674 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3675 | // performed operation to include an implicit flush operation without a |
| 3676 | // list. |
| 3677 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3678 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3679 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3680 | } |
| 3681 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3682 | static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3683 | const Expr *X, const Expr *E, |
| 3684 | SourceLocation Loc) { |
| 3685 | // x = expr; |
| 3686 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3687 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3688 | // OpenMP, 2.12.6, atomic Construct |
| 3689 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3690 | // performed operation to include an implicit flush operation without a |
| 3691 | // list. |
| 3692 | if (IsSeqCst) |
| 3693 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3694 | } |
| 3695 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3696 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 3697 | RValue Update, |
| 3698 | BinaryOperatorKind BO, |
| 3699 | llvm::AtomicOrdering AO, |
| 3700 | bool IsXLHSInRHSPart) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3701 | ASTContext &Context = CGF.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3702 | // 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] | 3703 | // expression is simple and atomic is allowed for the given type for the |
| 3704 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3705 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 3706 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 3707 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 3708 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3709 | X.getAddress().getElementType())) || |
| 3710 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3711 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 3712 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3713 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3714 | |
| 3715 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 3716 | switch (BO) { |
| 3717 | case BO_Add: |
| 3718 | RMWOp = llvm::AtomicRMWInst::Add; |
| 3719 | break; |
| 3720 | case BO_Sub: |
| 3721 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3722 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3723 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 3724 | break; |
| 3725 | case BO_And: |
| 3726 | RMWOp = llvm::AtomicRMWInst::And; |
| 3727 | break; |
| 3728 | case BO_Or: |
| 3729 | RMWOp = llvm::AtomicRMWInst::Or; |
| 3730 | break; |
| 3731 | case BO_Xor: |
| 3732 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 3733 | break; |
| 3734 | case BO_LT: |
| 3735 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3736 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 3737 | : llvm::AtomicRMWInst::Max) |
| 3738 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 3739 | : llvm::AtomicRMWInst::UMax); |
| 3740 | break; |
| 3741 | case BO_GT: |
| 3742 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3743 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 3744 | : llvm::AtomicRMWInst::Min) |
| 3745 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 3746 | : llvm::AtomicRMWInst::UMin); |
| 3747 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3748 | case BO_Assign: |
| 3749 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 3750 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3751 | case BO_Mul: |
| 3752 | case BO_Div: |
| 3753 | case BO_Rem: |
| 3754 | case BO_Shl: |
| 3755 | case BO_Shr: |
| 3756 | case BO_LAnd: |
| 3757 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3758 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3759 | case BO_PtrMemD: |
| 3760 | case BO_PtrMemI: |
| 3761 | case BO_LE: |
| 3762 | case BO_GE: |
| 3763 | case BO_EQ: |
| 3764 | case BO_NE: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 3765 | case BO_Cmp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3766 | case BO_AddAssign: |
| 3767 | case BO_SubAssign: |
| 3768 | case BO_AndAssign: |
| 3769 | case BO_OrAssign: |
| 3770 | case BO_XorAssign: |
| 3771 | case BO_MulAssign: |
| 3772 | case BO_DivAssign: |
| 3773 | case BO_RemAssign: |
| 3774 | case BO_ShlAssign: |
| 3775 | case BO_ShrAssign: |
| 3776 | case BO_Comma: |
| 3777 | llvm_unreachable("Unsupported atomic update operation"); |
| 3778 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3779 | llvm::Value *UpdateVal = Update.getScalarVal(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3780 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 3781 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3782 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3783 | X.getType()->hasSignedIntegerRepresentation()); |
| 3784 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3785 | llvm::Value *Res = |
| 3786 | CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3787 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3790 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3791 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 3792 | llvm::AtomicOrdering AO, SourceLocation Loc, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3793 | const llvm::function_ref<RValue(RValue)> CommonGen) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3794 | // Update expressions are allowed to have the following forms: |
| 3795 | // x binop= expr; -> xrval + expr; |
| 3796 | // x++, ++x -> xrval + 1; |
| 3797 | // x--, --x -> xrval - 1; |
| 3798 | // x = x binop expr; -> xrval binop expr |
| 3799 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3800 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 3801 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3802 | if (X.isGlobalReg()) { |
| 3803 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 3804 | // 'xrval'. |
| 3805 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 3806 | } else { |
| 3807 | // Perform compare-and-swap procedure. |
| 3808 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3809 | } |
| 3810 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3811 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3814 | static void emitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3815 | const Expr *X, const Expr *E, |
| 3816 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3817 | SourceLocation Loc) { |
| 3818 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3819 | "Update expr in 'atomic update' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3820 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3821 | // Update expressions are allowed to have the following forms: |
| 3822 | // x binop= expr; -> xrval + expr; |
| 3823 | // x++, ++x -> xrval + 1; |
| 3824 | // x--, --x -> xrval - 1; |
| 3825 | // x = x binop expr; -> xrval binop expr |
| 3826 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3827 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3828 | LValue XLValue = CGF.EmitLValue(X); |
| 3829 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3830 | llvm::AtomicOrdering AO = IsSeqCst |
| 3831 | ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3832 | : llvm::AtomicOrdering::Monotonic; |
| 3833 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3834 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3835 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3836 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3837 | auto &&Gen = [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) { |
| 3838 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3839 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3840 | return CGF.EmitAnyExpr(UE); |
| 3841 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3842 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3843 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3844 | // OpenMP, 2.12.6, atomic Construct |
| 3845 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3846 | // performed operation to include an implicit flush operation without a |
| 3847 | // list. |
| 3848 | if (IsSeqCst) |
| 3849 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3850 | } |
| 3851 | |
| 3852 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3853 | QualType SourceType, QualType ResType, |
| 3854 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3855 | switch (CGF.getEvaluationKind(ResType)) { |
| 3856 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3857 | return RValue::get( |
| 3858 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3859 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3860 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3861 | return RValue::getComplex(Res.first, Res.second); |
| 3862 | } |
| 3863 | case TEK_Aggregate: |
| 3864 | break; |
| 3865 | } |
| 3866 | llvm_unreachable("Must be a scalar or complex."); |
| 3867 | } |
| 3868 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3869 | static void emitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3870 | bool IsPostfixUpdate, const Expr *V, |
| 3871 | const Expr *X, const Expr *E, |
| 3872 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3873 | SourceLocation Loc) { |
| 3874 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 3875 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 3876 | RValue NewVVal; |
| 3877 | LValue VLValue = CGF.EmitLValue(V); |
| 3878 | LValue XLValue = CGF.EmitLValue(X); |
| 3879 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3880 | llvm::AtomicOrdering AO = IsSeqCst |
| 3881 | ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3882 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3883 | QualType NewVValType; |
| 3884 | if (UE) { |
| 3885 | // 'x' is updated with some additional value. |
| 3886 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3887 | "Update expr in 'atomic capture' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3888 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3889 | // Update expressions are allowed to have the following forms: |
| 3890 | // x binop= expr; -> xrval + expr; |
| 3891 | // x++, ++x -> xrval + 1; |
| 3892 | // x--, --x -> xrval - 1; |
| 3893 | // x = x binop expr; -> xrval binop expr |
| 3894 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3895 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3896 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3897 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3898 | NewVValType = XRValExpr->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3899 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3900 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3901 | IsPostfixUpdate](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3902 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3903 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3904 | RValue Res = CGF.EmitAnyExpr(UE); |
| 3905 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 3906 | return Res; |
| 3907 | }; |
| 3908 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3909 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3910 | if (Res.first) { |
| 3911 | // 'atomicrmw' instruction was generated. |
| 3912 | if (IsPostfixUpdate) { |
| 3913 | // Use old value from 'atomicrmw'. |
| 3914 | NewVVal = Res.second; |
| 3915 | } else { |
| 3916 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 3917 | // value of 'x'. |
| 3918 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3919 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 3920 | NewVVal = CGF.EmitAnyExpr(UE); |
| 3921 | } |
| 3922 | } |
| 3923 | } else { |
| 3924 | // 'x' is simply rewritten with some 'expr'. |
| 3925 | NewVValType = X->getType().getNonReferenceType(); |
| 3926 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3927 | X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3928 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3929 | NewVVal = XRValue; |
| 3930 | return ExprRValue; |
| 3931 | }; |
| 3932 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 3933 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3934 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 3935 | Loc, Gen); |
| 3936 | if (Res.first) { |
| 3937 | // 'atomicrmw' instruction was generated. |
| 3938 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 3939 | } |
| 3940 | } |
| 3941 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3942 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3943 | // OpenMP, 2.12.6, atomic Construct |
| 3944 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3945 | // performed operation to include an implicit flush operation without a |
| 3946 | // list. |
| 3947 | if (IsSeqCst) |
| 3948 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3949 | } |
| 3950 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3951 | static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3952 | bool IsSeqCst, bool IsPostfixUpdate, |
| 3953 | const Expr *X, const Expr *V, const Expr *E, |
| 3954 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3955 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3956 | switch (Kind) { |
| 3957 | case OMPC_read: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3958 | emitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3959 | break; |
| 3960 | case OMPC_write: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3961 | emitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3962 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3963 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3964 | case OMPC_update: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3965 | emitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3966 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3967 | case OMPC_capture: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3968 | emitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3969 | IsXLHSInRHSPart, Loc); |
| 3970 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3971 | case OMPC_if: |
| 3972 | case OMPC_final: |
| 3973 | case OMPC_num_threads: |
| 3974 | case OMPC_private: |
| 3975 | case OMPC_firstprivate: |
| 3976 | case OMPC_lastprivate: |
| 3977 | case OMPC_reduction: |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 3978 | case OMPC_task_reduction: |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 3979 | case OMPC_in_reduction: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3980 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 3981 | case OMPC_simdlen: |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 3982 | case OMPC_allocator: |
Alexey Bataev | e04483e | 2019-03-27 14:14:31 +0000 | [diff] [blame] | 3983 | case OMPC_allocate: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3984 | case OMPC_collapse: |
| 3985 | case OMPC_default: |
| 3986 | case OMPC_seq_cst: |
| 3987 | case OMPC_shared: |
| 3988 | case OMPC_linear: |
| 3989 | case OMPC_aligned: |
| 3990 | case OMPC_copyin: |
| 3991 | case OMPC_copyprivate: |
| 3992 | case OMPC_flush: |
| 3993 | case OMPC_proc_bind: |
| 3994 | case OMPC_schedule: |
| 3995 | case OMPC_ordered: |
| 3996 | case OMPC_nowait: |
| 3997 | case OMPC_untied: |
| 3998 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 3999 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4000 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 4001 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4002 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4003 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 4004 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 4005 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 4006 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 4007 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 4008 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 4009 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 4010 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4011 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 4012 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 4013 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 4014 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 4015 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 4016 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 4017 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 4018 | case OMPC_is_device_ptr: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 4019 | case OMPC_unified_address: |
Alexey Bataev | 94c5064 | 2018-10-01 14:26:31 +0000 | [diff] [blame] | 4020 | case OMPC_unified_shared_memory: |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 4021 | case OMPC_reverse_offload: |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 4022 | case OMPC_dynamic_allocators: |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 4023 | case OMPC_atomic_default_mem_order: |
Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame^] | 4024 | case OMPC_device_type: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4025 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 4026 | } |
| 4027 | } |
| 4028 | |
| 4029 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 4030 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4031 | OpenMPClauseKind Kind = OMPC_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4032 | for (const OMPClause *C : S.clauses()) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4033 | // Find first clause (skip seq_cst clause, if it is first). |
| 4034 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 4035 | Kind = C->getClauseKind(); |
| 4036 | break; |
| 4037 | } |
| 4038 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 4039 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4040 | const Stmt *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 4041 | if (const auto *FE = dyn_cast<FullExpr>(CS)) |
| 4042 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4043 | // Processing for statements under 'atomic capture'. |
| 4044 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4045 | for (const Stmt *C : Compound->body()) { |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 4046 | if (const auto *FE = dyn_cast<FullExpr>(C)) |
| 4047 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4048 | } |
| 4049 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 4050 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4051 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, |
| 4052 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 4053 | CGF.EmitStopPoint(CS); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4054 | emitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4055 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4056 | S.isXLHSInRHSPart(), S.getBeginLoc()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 4057 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4058 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4059 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4060 | } |
| 4061 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4062 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 4063 | const OMPExecutableDirective &S, |
| 4064 | const RegionCodeGenTy &CodeGen) { |
| 4065 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 4066 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4067 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4068 | // On device emit this construct as inlined code. |
| 4069 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 4070 | OMPLexicalScope Scope(CGF, S, OMPD_target); |
| 4071 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4072 | CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 4ac68a2 | 2018-05-16 15:08:32 +0000 | [diff] [blame] | 4073 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4074 | }); |
| 4075 | return; |
| 4076 | } |
| 4077 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4078 | llvm::Function *Fn = nullptr; |
| 4079 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4080 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4081 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 4082 | // Check for the at most one if clause associated with the target region. |
| 4083 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4084 | if (C->getNameModifier() == OMPD_unknown || |
| 4085 | C->getNameModifier() == OMPD_target) { |
| 4086 | IfCond = C->getCondition(); |
| 4087 | break; |
| 4088 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4089 | } |
| 4090 | |
| 4091 | // Check if we have any device clause associated with the directive. |
| 4092 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4093 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4094 | Device = C->getDevice(); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4095 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4096 | // Check if we have an if clause whose conditional always evaluates to false |
| 4097 | // or if we do not have any targets specified. If so the target region is not |
| 4098 | // an offload entry point. |
| 4099 | bool IsOffloadEntry = true; |
| 4100 | if (IfCond) { |
| 4101 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4102 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4103 | IsOffloadEntry = false; |
| 4104 | } |
| 4105 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4106 | IsOffloadEntry = false; |
| 4107 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4108 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4109 | StringRef ParentName; |
| 4110 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 4111 | // the mangling of the device outlined kernel. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4112 | if (const auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4113 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4114 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4115 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 4116 | else |
| 4117 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4118 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4119 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4120 | // Emit target region as a standalone region. |
| 4121 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 4122 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4123 | OMPLexicalScope Scope(CGF, S, OMPD_task); |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 4124 | auto &&SizeEmitter = [](CodeGenFunction &CGF, const OMPLoopDirective &D) { |
| 4125 | OMPLoopScope(CGF, D); |
| 4126 | // Emit calculation of the iterations count. |
| 4127 | llvm::Value *NumIterations = CGF.EmitScalarExpr(D.getNumIterations()); |
| 4128 | NumIterations = CGF.Builder.CreateIntCast(NumIterations, CGF.Int64Ty, |
Rui Ueyama | 49a3ad2 | 2019-07-16 04:46:31 +0000 | [diff] [blame] | 4129 | /*isSigned=*/false); |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 4130 | return NumIterations; |
| 4131 | }; |
Alexey Bataev | 4920e1a | 2019-01-30 20:49:52 +0000 | [diff] [blame] | 4132 | if (IsOffloadEntry) |
| 4133 | CGM.getOpenMPRuntime().emitTargetNumIterationsCall(CGF, S, Device, |
| 4134 | SizeEmitter); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4135 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4136 | } |
| 4137 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4138 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 4139 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4140 | Action.Enter(CGF); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4141 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4142 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4143 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4144 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4145 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4146 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4147 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4148 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 4152 | StringRef ParentName, |
| 4153 | const OMPTargetDirective &S) { |
| 4154 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4155 | emitTargetRegion(CGF, S, Action); |
| 4156 | }; |
| 4157 | llvm::Function *Fn; |
| 4158 | llvm::Constant *Addr; |
| 4159 | // Emit target region as a standalone region. |
| 4160 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4161 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4162 | assert(Fn && Addr && "Target device function emission failed."); |
| 4163 | } |
| 4164 | |
| 4165 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 4166 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4167 | emitTargetRegion(CGF, S, Action); |
| 4168 | }; |
| 4169 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4170 | } |
| 4171 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4172 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 4173 | const OMPExecutableDirective &S, |
| 4174 | OpenMPDirectiveKind InnermostKind, |
| 4175 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4176 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4177 | llvm::Function *OutlinedFn = |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4178 | CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 4179 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 4180 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4181 | const auto *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 4182 | const auto *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4183 | if (NT || TL) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4184 | const Expr *NumTeams = NT ? NT->getNumTeams() : nullptr; |
| 4185 | const Expr *ThreadLimit = TL ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4186 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4187 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4188 | S.getBeginLoc()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4191 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4192 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4193 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4194 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getBeginLoc(), OutlinedFn, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4195 | CapturedVars); |
| 4196 | } |
| 4197 | |
| 4198 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 4199 | // Emit teams region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4200 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4201 | Action.Enter(CGF); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4202 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 4203 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4204 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4205 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4206 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4207 | CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4208 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4209 | }; |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4210 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4211 | emitPostUpdateForReductionClause(*this, S, |
| 4212 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4213 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4214 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4215 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4216 | const OMPTargetTeamsDirective &S) { |
| 4217 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 4218 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4219 | // Emit teams region as a standalone region. |
| 4220 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4221 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4222 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4223 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4224 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4225 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4226 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4227 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4228 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4229 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4230 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4231 | }; |
| 4232 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4233 | emitPostUpdateForReductionClause(CGF, S, |
| 4234 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 4238 | CodeGenModule &CGM, StringRef ParentName, |
| 4239 | const OMPTargetTeamsDirective &S) { |
| 4240 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4241 | emitTargetTeamsRegion(CGF, Action, S); |
| 4242 | }; |
| 4243 | llvm::Function *Fn; |
| 4244 | llvm::Constant *Addr; |
| 4245 | // Emit target region as a standalone region. |
| 4246 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4247 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4248 | assert(Fn && Addr && "Target device function emission failed."); |
| 4249 | } |
| 4250 | |
| 4251 | void CodeGenFunction::EmitOMPTargetTeamsDirective( |
| 4252 | const OMPTargetTeamsDirective &S) { |
| 4253 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4254 | emitTargetTeamsRegion(CGF, Action, S); |
| 4255 | }; |
| 4256 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4257 | } |
| 4258 | |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4259 | static void |
| 4260 | emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4261 | const OMPTargetTeamsDistributeDirective &S) { |
| 4262 | Action.Enter(CGF); |
| 4263 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4264 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4265 | }; |
| 4266 | |
| 4267 | // Emit teams region as a standalone region. |
| 4268 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4269 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4270 | Action.Enter(CGF); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4271 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4272 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4273 | (void)PrivateScope.Privatize(); |
| 4274 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4275 | CodeGenDistribute); |
| 4276 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4277 | }; |
| 4278 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); |
| 4279 | emitPostUpdateForReductionClause(CGF, S, |
| 4280 | [](CodeGenFunction &) { return nullptr; }); |
| 4281 | } |
| 4282 | |
| 4283 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| 4284 | CodeGenModule &CGM, StringRef ParentName, |
| 4285 | const OMPTargetTeamsDistributeDirective &S) { |
| 4286 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4287 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4288 | }; |
| 4289 | llvm::Function *Fn; |
| 4290 | llvm::Constant *Addr; |
| 4291 | // Emit target region as a standalone region. |
| 4292 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4293 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4294 | assert(Fn && Addr && "Target device function emission failed."); |
| 4295 | } |
| 4296 | |
| 4297 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 4298 | const OMPTargetTeamsDistributeDirective &S) { |
| 4299 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4300 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4301 | }; |
| 4302 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4303 | } |
| 4304 | |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4305 | static void emitTargetTeamsDistributeSimdRegion( |
| 4306 | CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4307 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4308 | Action.Enter(CGF); |
| 4309 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4310 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4311 | }; |
| 4312 | |
| 4313 | // Emit teams region as a standalone region. |
| 4314 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4315 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4316 | Action.Enter(CGF); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4317 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4318 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4319 | (void)PrivateScope.Privatize(); |
| 4320 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4321 | CodeGenDistribute); |
| 4322 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4323 | }; |
| 4324 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); |
| 4325 | emitPostUpdateForReductionClause(CGF, S, |
| 4326 | [](CodeGenFunction &) { return nullptr; }); |
| 4327 | } |
| 4328 | |
| 4329 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| 4330 | CodeGenModule &CGM, StringRef ParentName, |
| 4331 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4332 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4333 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4334 | }; |
| 4335 | llvm::Function *Fn; |
| 4336 | llvm::Constant *Addr; |
| 4337 | // Emit target region as a standalone region. |
| 4338 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4339 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4340 | assert(Fn && Addr && "Target device function emission failed."); |
| 4341 | } |
| 4342 | |
| 4343 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 4344 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4345 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4346 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4347 | }; |
| 4348 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4349 | } |
| 4350 | |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4351 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 4352 | const OMPTeamsDistributeDirective &S) { |
| 4353 | |
| 4354 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4355 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4356 | }; |
| 4357 | |
| 4358 | // Emit teams region as a standalone region. |
| 4359 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4360 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4361 | Action.Enter(CGF); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4362 | OMPPrivateScope PrivateScope(CGF); |
| 4363 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4364 | (void)PrivateScope.Privatize(); |
| 4365 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4366 | CodeGenDistribute); |
| 4367 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4368 | }; |
Alexey Bataev | 95c6dd4 | 2017-11-29 15:14:16 +0000 | [diff] [blame] | 4369 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4370 | emitPostUpdateForReductionClause(*this, S, |
| 4371 | [](CodeGenFunction &) { return nullptr; }); |
| 4372 | } |
| 4373 | |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4374 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 4375 | const OMPTeamsDistributeSimdDirective &S) { |
| 4376 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4377 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4378 | }; |
| 4379 | |
| 4380 | // Emit teams region as a standalone region. |
| 4381 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4382 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4383 | Action.Enter(CGF); |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4384 | OMPPrivateScope PrivateScope(CGF); |
| 4385 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4386 | (void)PrivateScope.Privatize(); |
| 4387 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, |
| 4388 | CodeGenDistribute); |
| 4389 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4390 | }; |
| 4391 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); |
| 4392 | emitPostUpdateForReductionClause(*this, S, |
| 4393 | [](CodeGenFunction &) { return nullptr; }); |
| 4394 | } |
| 4395 | |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4396 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 4397 | const OMPTeamsDistributeParallelForDirective &S) { |
| 4398 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4399 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4400 | S.getDistInc()); |
| 4401 | }; |
| 4402 | |
| 4403 | // Emit teams region as a standalone region. |
| 4404 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4405 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4406 | Action.Enter(CGF); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4407 | OMPPrivateScope PrivateScope(CGF); |
| 4408 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4409 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 4410 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4411 | CodeGenDistribute); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4412 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4413 | }; |
| 4414 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4415 | emitPostUpdateForReductionClause(*this, S, |
| 4416 | [](CodeGenFunction &) { return nullptr; }); |
| 4417 | } |
| 4418 | |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4419 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 4420 | const OMPTeamsDistributeParallelForSimdDirective &S) { |
| 4421 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4422 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4423 | S.getDistInc()); |
| 4424 | }; |
| 4425 | |
| 4426 | // Emit teams region as a standalone region. |
| 4427 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4428 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4429 | Action.Enter(CGF); |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4430 | OMPPrivateScope PrivateScope(CGF); |
| 4431 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4432 | (void)PrivateScope.Privatize(); |
| 4433 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4434 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4435 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4436 | }; |
| 4437 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4438 | emitPostUpdateForReductionClause(*this, S, |
| 4439 | [](CodeGenFunction &) { return nullptr; }); |
| 4440 | } |
| 4441 | |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4442 | static void emitTargetTeamsDistributeParallelForRegion( |
| 4443 | CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, |
| 4444 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4445 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4446 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4447 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4448 | S.getDistInc()); |
| 4449 | }; |
| 4450 | |
| 4451 | // Emit teams region as a standalone region. |
| 4452 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4453 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4454 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4455 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4456 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4457 | (void)PrivateScope.Privatize(); |
| 4458 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4459 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4460 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4461 | }; |
| 4462 | |
| 4463 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, |
| 4464 | CodeGenTeams); |
| 4465 | emitPostUpdateForReductionClause(CGF, S, |
| 4466 | [](CodeGenFunction &) { return nullptr; }); |
| 4467 | } |
| 4468 | |
| 4469 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 4470 | CodeGenModule &CGM, StringRef ParentName, |
| 4471 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4472 | // Emit SPMD target teams distribute parallel for region as a standalone |
| 4473 | // region. |
| 4474 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4475 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4476 | }; |
| 4477 | llvm::Function *Fn; |
| 4478 | llvm::Constant *Addr; |
| 4479 | // Emit target region as a standalone region. |
| 4480 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4481 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4482 | assert(Fn && Addr && "Target device function emission failed."); |
| 4483 | } |
| 4484 | |
| 4485 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 4486 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4487 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4488 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4489 | }; |
| 4490 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4491 | } |
| 4492 | |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4493 | static void emitTargetTeamsDistributeParallelForSimdRegion( |
| 4494 | CodeGenFunction &CGF, |
| 4495 | const OMPTargetTeamsDistributeParallelForSimdDirective &S, |
| 4496 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4497 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4498 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4499 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4500 | S.getDistInc()); |
| 4501 | }; |
| 4502 | |
| 4503 | // Emit teams region as a standalone region. |
| 4504 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4505 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4506 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4507 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4508 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4509 | (void)PrivateScope.Privatize(); |
| 4510 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4511 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4512 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4513 | }; |
| 4514 | |
| 4515 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd, |
| 4516 | CodeGenTeams); |
| 4517 | emitPostUpdateForReductionClause(CGF, S, |
| 4518 | [](CodeGenFunction &) { return nullptr; }); |
| 4519 | } |
| 4520 | |
| 4521 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 4522 | CodeGenModule &CGM, StringRef ParentName, |
| 4523 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4524 | // Emit SPMD target teams distribute parallel for simd region as a standalone |
| 4525 | // region. |
| 4526 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4527 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4528 | }; |
| 4529 | llvm::Function *Fn; |
| 4530 | llvm::Constant *Addr; |
| 4531 | // Emit target region as a standalone region. |
| 4532 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4533 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4534 | assert(Fn && Addr && "Target device function emission failed."); |
| 4535 | } |
| 4536 | |
| 4537 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 4538 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4539 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4540 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4541 | }; |
| 4542 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4543 | } |
| 4544 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4545 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 4546 | const OMPCancellationPointDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4547 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getBeginLoc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 4548 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4549 | } |
| 4550 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4551 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 4552 | const Expr *IfCond = nullptr; |
| 4553 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4554 | if (C->getNameModifier() == OMPD_unknown || |
| 4555 | C->getNameModifier() == OMPD_cancel) { |
| 4556 | IfCond = C->getCondition(); |
| 4557 | break; |
| 4558 | } |
| 4559 | } |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4560 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 4561 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4564 | CodeGenFunction::JumpDest |
| 4565 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4566 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
| 4567 | Kind == OMPD_target_parallel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4568 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4569 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4570 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 4571 | Kind == OMPD_distribute_parallel_for || |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 4572 | Kind == OMPD_target_parallel_for || |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 4573 | Kind == OMPD_teams_distribute_parallel_for || |
| 4574 | Kind == OMPD_target_teams_distribute_parallel_for); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4575 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4576 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4577 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4578 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 4579 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 4580 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 4581 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 4582 | auto OrigVarIt = C.varlist_begin(); |
| 4583 | auto InitIt = C.inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4584 | for (const Expr *PvtVarIt : C.private_copies()) { |
| 4585 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 4586 | const auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 4587 | const auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4588 | |
| 4589 | // In order to identify the right initializer we need to match the |
| 4590 | // declaration used by the mapping logic. In some cases we may get |
| 4591 | // OMPCapturedExprDecl that refers to the original declaration. |
| 4592 | const ValueDecl *MatchingVD = OrigVD; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4593 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4594 | // OMPCapturedExprDecl are used to privative fields of the current |
| 4595 | // structure. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4596 | const auto *ME = cast<MemberExpr>(OED->getInit()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4597 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 4598 | "Base should be the current struct!"); |
| 4599 | MatchingVD = ME->getMemberDecl(); |
| 4600 | } |
| 4601 | |
| 4602 | // If we don't have information about the current list item, move on to |
| 4603 | // the next one. |
| 4604 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 4605 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 4606 | continue; |
| 4607 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4608 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, OrigVD, |
| 4609 | InitAddrIt, InitVD, |
| 4610 | PvtVD]() { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4611 | // Initialize the temporary initialization variable with the address we |
| 4612 | // get from the runtime library. We have to cast the source address |
| 4613 | // because it is always a void *. References are materialized in the |
| 4614 | // privatization scope, so the initialization here disregards the fact |
| 4615 | // the original variable is a reference. |
| 4616 | QualType AddrQTy = |
| 4617 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 4618 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 4619 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 4620 | setAddrOfLocalVar(InitVD, InitAddr); |
| 4621 | |
| 4622 | // Emit private declaration, it will be initialized by the value we |
| 4623 | // declaration we just added to the local declarations map. |
| 4624 | EmitDecl(*PvtVD); |
| 4625 | |
| 4626 | // The initialization variables reached its purpose in the emission |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4627 | // of the previous declaration, so we don't need it anymore. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4628 | LocalDeclMap.erase(InitVD); |
| 4629 | |
| 4630 | // Return the address of the private variable. |
| 4631 | return GetAddrOfLocalVar(PvtVD); |
| 4632 | }); |
| 4633 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 4634 | // Silence the warning about unused variable. |
| 4635 | (void)IsRegistered; |
| 4636 | |
| 4637 | ++OrigVarIt; |
| 4638 | ++InitIt; |
| 4639 | } |
| 4640 | } |
| 4641 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4642 | // Generate the instructions for '#pragma omp target data' directive. |
| 4643 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 4644 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4645 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 4646 | |
| 4647 | // Create a pre/post action to signal the privatization of the device pointer. |
| 4648 | // This action can be replaced by the OpenMP runtime code generation to |
| 4649 | // deactivate privatization. |
| 4650 | bool PrivatizeDevicePointers = false; |
| 4651 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 4652 | bool &PrivatizeDevicePointers; |
| 4653 | |
| 4654 | public: |
| 4655 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 4656 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 4657 | void Enter(CodeGenFunction &CGF) override { |
| 4658 | PrivatizeDevicePointers = true; |
| 4659 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4660 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4661 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 4662 | |
| 4663 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4664 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4665 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4666 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4667 | }; |
| 4668 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4669 | // Codegen that selects whether to generate the privatization code or not. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4670 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 4671 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 4672 | PrePostActionTy &Action) { |
| 4673 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 4674 | PrivatizeDevicePointers = false; |
| 4675 | |
| 4676 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 4677 | // needed. |
| 4678 | Action.Enter(CGF); |
| 4679 | |
| 4680 | if (PrivatizeDevicePointers) { |
| 4681 | OMPPrivateScope PrivateScope(CGF); |
| 4682 | // Emit all instances of the use_device_ptr clause. |
| 4683 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 4684 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 4685 | Info.CaptureDeviceAddrMap); |
| 4686 | (void)PrivateScope.Privatize(); |
| 4687 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4688 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4689 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4690 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4691 | }; |
| 4692 | |
| 4693 | // Forward the provided action to the privatization codegen. |
| 4694 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 4695 | PrivRCG.setAction(Action); |
| 4696 | |
| 4697 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 4698 | // we don't use an inline scope as changes in the references inside the |
| 4699 | // region are expected to be visible outside, so we do not privative them. |
| 4700 | OMPLexicalScope Scope(CGF, S); |
| 4701 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 4702 | PrivRCG); |
| 4703 | }; |
| 4704 | |
| 4705 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4706 | |
| 4707 | // If we don't have target devices, don't bother emitting the data mapping |
| 4708 | // code. |
| 4709 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4710 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4711 | return; |
| 4712 | } |
| 4713 | |
| 4714 | // Check if we have any if clause associated with the directive. |
| 4715 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4716 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4717 | IfCond = C->getCondition(); |
| 4718 | |
| 4719 | // Check if we have any device clause associated with the directive. |
| 4720 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4721 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4722 | Device = C->getDevice(); |
| 4723 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4724 | // Set the action to signal privatization of device pointers. |
| 4725 | RCG.setAction(PrivAction); |
| 4726 | |
| 4727 | // Emit region code. |
| 4728 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 4729 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4730 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4731 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4732 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 4733 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4734 | // If we don't have target devices, don't bother emitting the data mapping |
| 4735 | // code. |
| 4736 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4737 | return; |
| 4738 | |
| 4739 | // Check if we have any if clause associated with the directive. |
| 4740 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4741 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4742 | IfCond = C->getCondition(); |
| 4743 | |
| 4744 | // Check if we have any device clause associated with the directive. |
| 4745 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4746 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4747 | Device = C->getDevice(); |
| 4748 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4749 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4750 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4751 | } |
| 4752 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4753 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 4754 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4755 | // If we don't have target devices, don't bother emitting the data mapping |
| 4756 | // code. |
| 4757 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4758 | return; |
| 4759 | |
| 4760 | // Check if we have any if clause associated with the directive. |
| 4761 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4762 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4763 | IfCond = C->getCondition(); |
| 4764 | |
| 4765 | // Check if we have any device clause associated with the directive. |
| 4766 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4767 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4768 | Device = C->getDevice(); |
| 4769 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4770 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4771 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4774 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 4775 | const OMPTargetParallelDirective &S, |
| 4776 | PrePostActionTy &Action) { |
| 4777 | // Get the captured statement associated with the 'parallel' region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4778 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4779 | Action.Enter(CGF); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4780 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4781 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4782 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4783 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4784 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4785 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4786 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4787 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4788 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4789 | // TODO: Add support for clauses. |
| 4790 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4791 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4792 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4793 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, |
| 4794 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4795 | emitPostUpdateForReductionClause(CGF, S, |
| 4796 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 4800 | CodeGenModule &CGM, StringRef ParentName, |
| 4801 | const OMPTargetParallelDirective &S) { |
| 4802 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4803 | emitTargetParallelRegion(CGF, S, Action); |
| 4804 | }; |
| 4805 | llvm::Function *Fn; |
| 4806 | llvm::Constant *Addr; |
| 4807 | // Emit target region as a standalone region. |
| 4808 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4809 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4810 | assert(Fn && Addr && "Target device function emission failed."); |
| 4811 | } |
| 4812 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4813 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 4814 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4815 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4816 | emitTargetParallelRegion(CGF, S, Action); |
| 4817 | }; |
| 4818 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4819 | } |
| 4820 | |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4821 | static void emitTargetParallelForRegion(CodeGenFunction &CGF, |
| 4822 | const OMPTargetParallelForDirective &S, |
| 4823 | PrePostActionTy &Action) { |
| 4824 | Action.Enter(CGF); |
| 4825 | // Emit directive as a combined directive that consists of two implicit |
| 4826 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4827 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4828 | Action.Enter(CGF); |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4829 | CodeGenFunction::OMPCancelStackRAII CancelRegion( |
| 4830 | CGF, OMPD_target_parallel_for, S.hasCancel()); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4831 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4832 | emitDispatchForLoopBounds); |
| 4833 | }; |
| 4834 | emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, |
| 4835 | emitEmptyBoundParameters); |
| 4836 | } |
| 4837 | |
| 4838 | void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| 4839 | CodeGenModule &CGM, StringRef ParentName, |
| 4840 | const OMPTargetParallelForDirective &S) { |
| 4841 | // Emit SPMD target parallel for region as a standalone region. |
| 4842 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4843 | emitTargetParallelForRegion(CGF, S, Action); |
| 4844 | }; |
| 4845 | llvm::Function *Fn; |
| 4846 | llvm::Constant *Addr; |
| 4847 | // Emit target region as a standalone region. |
| 4848 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4849 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4850 | assert(Fn && Addr && "Target device function emission failed."); |
| 4851 | } |
| 4852 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4853 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 4854 | const OMPTargetParallelForDirective &S) { |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4855 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4856 | emitTargetParallelForRegion(CGF, S, Action); |
| 4857 | }; |
| 4858 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4861 | static void |
| 4862 | emitTargetParallelForSimdRegion(CodeGenFunction &CGF, |
| 4863 | const OMPTargetParallelForSimdDirective &S, |
| 4864 | PrePostActionTy &Action) { |
| 4865 | Action.Enter(CGF); |
| 4866 | // Emit directive as a combined directive that consists of two implicit |
| 4867 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4868 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4869 | Action.Enter(CGF); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4870 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4871 | emitDispatchForLoopBounds); |
| 4872 | }; |
| 4873 | emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, |
| 4874 | emitEmptyBoundParameters); |
| 4875 | } |
| 4876 | |
| 4877 | void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| 4878 | CodeGenModule &CGM, StringRef ParentName, |
| 4879 | const OMPTargetParallelForSimdDirective &S) { |
| 4880 | // Emit SPMD target parallel for region as a standalone region. |
| 4881 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4882 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4883 | }; |
| 4884 | llvm::Function *Fn; |
| 4885 | llvm::Constant *Addr; |
| 4886 | // Emit target region as a standalone region. |
| 4887 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4888 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4889 | assert(Fn && Addr && "Target device function emission failed."); |
| 4890 | } |
| 4891 | |
| 4892 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 4893 | const OMPTargetParallelForSimdDirective &S) { |
| 4894 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4895 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4896 | }; |
| 4897 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4898 | } |
| 4899 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4900 | /// Emit a helper variable and return corresponding lvalue. |
| 4901 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 4902 | const ImplicitParamDecl *PVD, |
| 4903 | CodeGenFunction::OMPPrivateScope &Privates) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4904 | const auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 4905 | Privates.addPrivate(VDecl, |
| 4906 | [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); }); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 4910 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 4911 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4912 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4913 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 4914 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4915 | const Expr *IfCond = nullptr; |
| 4916 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4917 | if (C->getNameModifier() == OMPD_unknown || |
| 4918 | C->getNameModifier() == OMPD_taskloop) { |
| 4919 | IfCond = C->getCondition(); |
| 4920 | break; |
| 4921 | } |
| 4922 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4923 | |
| 4924 | OMPTaskDataTy Data; |
| 4925 | // Check if taskloop must be emitted without taskgroup. |
| 4926 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4927 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4928 | Data.Tied = true; |
| 4929 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4930 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 4931 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4932 | Data.Schedule.setInt(/*IntVal=*/false); |
| 4933 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4934 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 4935 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4936 | Data.Schedule.setInt(/*IntVal=*/true); |
| 4937 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4938 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4939 | |
| 4940 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4941 | // if (PreCond) { |
| 4942 | // for (IV in 0..LastIteration) BODY; |
| 4943 | // <Final counter/linear vars updates>; |
| 4944 | // } |
| 4945 | // |
| 4946 | |
| 4947 | // Emit: if (PreCond) - begin. |
| 4948 | // If the condition constant folds and can be elided, avoid emitting the |
| 4949 | // whole loop. |
| 4950 | bool CondConstant; |
| 4951 | llvm::BasicBlock *ContBlock = nullptr; |
| 4952 | OMPLoopScope PreInitScope(CGF, S); |
| 4953 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 4954 | if (!CondConstant) |
| 4955 | return; |
| 4956 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4957 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4958 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 4959 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 4960 | CGF.getProfileCount(&S)); |
| 4961 | CGF.EmitBlock(ThenBlock); |
| 4962 | CGF.incrementProfileCounter(&S); |
| 4963 | } |
| 4964 | |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4965 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 4966 | CGF.EmitOMPSimdInit(S); |
| 4967 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4968 | OMPPrivateScope LoopScope(CGF); |
| 4969 | // Emit helper vars inits. |
| 4970 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 4971 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 4972 | auto *LBP = std::next(I, LowerBound); |
| 4973 | auto *UBP = std::next(I, UpperBound); |
| 4974 | auto *STP = std::next(I, Stride); |
| 4975 | auto *LIP = std::next(I, LastIter); |
| 4976 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 4977 | LoopScope); |
| 4978 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 4979 | LoopScope); |
| 4980 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 4981 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 4982 | LoopScope); |
| 4983 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4984 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4985 | (void)LoopScope.Privatize(); |
| 4986 | // Emit the loop iteration variable. |
| 4987 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4988 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4989 | CGF.EmitVarDecl(*IVDecl); |
| 4990 | CGF.EmitIgnoredExpr(S.getInit()); |
| 4991 | |
| 4992 | // Emit the iterations count variable. |
| 4993 | // If it is not a variable, Sema decided to calculate iterations count on |
| 4994 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4995 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4996 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 4997 | // Emit calculation of the iterations count. |
| 4998 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 4999 | } |
| 5000 | |
| 5001 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 5002 | S.getInc(), |
| 5003 | [&S](CodeGenFunction &CGF) { |
| 5004 | CGF.EmitOMPLoopBody(S, JumpDest()); |
| 5005 | CGF.EmitStopPoint(&S); |
| 5006 | }, |
| 5007 | [](CodeGenFunction &) {}); |
| 5008 | // Emit: if (PreCond) - end. |
| 5009 | if (ContBlock) { |
| 5010 | CGF.EmitBranch(ContBlock); |
| 5011 | CGF.EmitBlock(ContBlock, true); |
| 5012 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5013 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 5014 | if (HasLastprivateClause) { |
| 5015 | CGF.EmitOMPLastprivateClauseFinal( |
| 5016 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 5017 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 5018 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5019 | (*LIP)->getType(), S.getBeginLoc()))); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5020 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5021 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5022 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5023 | IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5024 | const OMPTaskDataTy &Data) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5025 | auto &&CodeGen = [&S, OutlinedFn, SharedsTy, CapturedStruct, IfCond, |
| 5026 | &Data](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5027 | OMPLoopScope PreInitScope(CGF, S); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5028 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getBeginLoc(), S, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5029 | OutlinedFn, SharedsTy, |
| 5030 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5031 | }; |
| 5032 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 5033 | CodeGen); |
| 5034 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5035 | if (Data.Nogroup) { |
| 5036 | EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); |
| 5037 | } else { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5038 | CGM.getOpenMPRuntime().emitTaskgroupRegion( |
| 5039 | *this, |
| 5040 | [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, |
| 5041 | PrePostActionTy &Action) { |
| 5042 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5043 | CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, |
| 5044 | Data); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5045 | }, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5046 | S.getBeginLoc()); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5047 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5048 | } |
| 5049 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5050 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5051 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5052 | } |
| 5053 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5054 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 5055 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 5056 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5057 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5058 | |
| 5059 | // Generate the instructions for '#pragma omp target update' directive. |
| 5060 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 5061 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5062 | // If we don't have target devices, don't bother emitting the data mapping |
| 5063 | // code. |
| 5064 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 5065 | return; |
| 5066 | |
| 5067 | // Check if we have any if clause associated with the directive. |
| 5068 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5069 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5070 | IfCond = C->getCondition(); |
| 5071 | |
| 5072 | // Check if we have any device clause associated with the directive. |
| 5073 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5074 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5075 | Device = C->getDevice(); |
| 5076 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5077 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5078 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5079 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5080 | |
| 5081 | void CodeGenFunction::EmitSimpleOMPExecutableDirective( |
| 5082 | const OMPExecutableDirective &D) { |
| 5083 | if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) |
| 5084 | return; |
| 5085 | auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5086 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 5087 | emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); |
| 5088 | } else { |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5089 | OMPPrivateScope LoopGlobals(CGF); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5090 | if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5091 | for (const Expr *E : LD->counters()) { |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5092 | const auto *VD = dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 5093 | if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) { |
| 5094 | LValue GlobLVal = CGF.EmitLValue(E); |
| 5095 | LoopGlobals.addPrivate( |
| 5096 | VD, [&GlobLVal]() { return GlobLVal.getAddress(); }); |
| 5097 | } |
Bjorn Pettersson | 6c2d83b | 2018-10-30 08:49:26 +0000 | [diff] [blame] | 5098 | if (isa<OMPCapturedExprDecl>(VD)) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5099 | // Emit only those that were not explicitly referenced in clauses. |
| 5100 | if (!CGF.LocalDeclMap.count(VD)) |
| 5101 | CGF.EmitVarDecl(*VD); |
| 5102 | } |
| 5103 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5104 | for (const auto *C : D.getClausesOfKind<OMPOrderedClause>()) { |
| 5105 | if (!C->getNumForLoops()) |
| 5106 | continue; |
| 5107 | for (unsigned I = LD->getCollapsedNumber(), |
| 5108 | E = C->getLoopNumIterations().size(); |
| 5109 | I < E; ++I) { |
| 5110 | if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 5111 | cast<DeclRefExpr>(C->getLoopCounter(I))->getDecl())) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5112 | // Emit only those that were not explicitly referenced in clauses. |
| 5113 | if (!CGF.LocalDeclMap.count(VD)) |
| 5114 | CGF.EmitVarDecl(*VD); |
| 5115 | } |
| 5116 | } |
| 5117 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5118 | } |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5119 | LoopGlobals.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5120 | CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5121 | } |
| 5122 | }; |
| 5123 | OMPSimdLexicalScope Scope(*this, D); |
| 5124 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5125 | *this, |
| 5126 | isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd |
| 5127 | : D.getDirectiveKind(), |
| 5128 | CodeGen); |
| 5129 | } |