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 | 6120582 | 2019-12-04 09:50:21 -0500 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Reid Kleckner | 9803178 | 2019-12-09 16:11:56 -0800 | [diff] [blame] | 19 | #include "clang/AST/Attr.h" |
| 20 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | e6d2583 | 2020-01-27 14:10:17 -0500 | [diff] [blame] | 21 | #include "clang/AST/OpenMPClause.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 22 | #include "clang/AST/Stmt.h" |
| 23 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | ea9166b | 2020-02-06 16:30:23 -0500 | [diff] [blame] | 24 | #include "clang/Basic/OpenMPKinds.h" |
Alexey Bataev | 8bbf2e3 | 2019-11-04 09:59:11 -0500 | [diff] [blame] | 25 | #include "clang/Basic/PrettyStackTrace.h" |
Johannes Doerfert | 10fedd9 | 2019-12-26 11:23:38 -0600 | [diff] [blame] | 26 | #include "llvm/Frontend/OpenMP/OMPIRBuilder.h" |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 27 | #include "llvm/IR/Instructions.h" |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 28 | #include "llvm/Support/AtomicOrdering.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 29 | using namespace clang; |
| 30 | using namespace CodeGen; |
Johannes Doerfert | eb3e81f | 2019-11-04 22:00:49 -0600 | [diff] [blame] | 31 | using namespace llvm::omp; |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 32 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 33 | namespace { |
| 34 | /// Lexical scope for OpenMP executable constructs, that handles correct codegen |
| 35 | /// for captured expressions. |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 36 | class OMPLexicalScope : public CodeGenFunction::LexicalScope { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 37 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 38 | for (const auto *C : S.clauses()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 39 | if (const auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 40 | if (const auto *PreInit = |
| 41 | cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 42 | for (const auto *I : PreInit->decls()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 43 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 44 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 45 | } else { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 46 | CodeGenFunction::AutoVarEmission Emission = |
| 47 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 48 | CGF.EmitAutoVarCleanups(Emission); |
| 49 | } |
| 50 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
| 54 | } |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 55 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 56 | |
| 57 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 58 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 59 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
Alexey Bataev | 172f146 | 2020-03-12 12:52:02 -0400 | [diff] [blame] | 60 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && |
| 61 | cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 62 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 63 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 64 | public: |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 65 | OMPLexicalScope( |
| 66 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 67 | const llvm::Optional<OpenMPDirectiveKind> CapturedRegion = llvm::None, |
| 68 | const bool EmitPreInitStmt = true) |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 69 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 70 | InlinedShareds(CGF) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 71 | if (EmitPreInitStmt) |
| 72 | emitPreInitStmt(CGF, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 73 | if (!CapturedRegion.hasValue()) |
| 74 | return; |
| 75 | assert(S.hasAssociatedStmt() && |
| 76 | "Expected associated statement for inlined directive."); |
| 77 | const CapturedStmt *CS = S.getCapturedStmt(*CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 78 | for (const auto &C : CS->captures()) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 79 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 80 | auto *VD = C.getCapturedVar(); |
| 81 | assert(VD == VD->getCanonicalDecl() && |
| 82 | "Canonical decl must be captured."); |
| 83 | DeclRefExpr DRE( |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 84 | CGF.getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 85 | isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo && |
| 86 | InlinedShareds.isGlobalVarCaptured(VD)), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 87 | VD->getType().getNonReferenceType(), VK_LValue, C.getLocation()); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 88 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 89 | return CGF.EmitLValue(&DRE).getAddress(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 90 | }); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 93 | (void)InlinedShareds.Privatize(); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 94 | } |
| 95 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 96 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 97 | /// Lexical scope for OpenMP parallel construct, that handles correct codegen |
| 98 | /// for captured expressions. |
| 99 | class OMPParallelScope final : public OMPLexicalScope { |
| 100 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 101 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 102 | return !(isOpenMPTargetExecutionDirective(Kind) || |
| 103 | isOpenMPLoopBoundSharingDirective(Kind)) && |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 104 | isOpenMPParallelDirective(Kind); |
| 105 | } |
| 106 | |
| 107 | public: |
| 108 | OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 109 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 110 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 113 | /// Lexical scope for OpenMP teams construct, that handles correct codegen |
| 114 | /// for captured expressions. |
| 115 | class OMPTeamsScope final : public OMPLexicalScope { |
| 116 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 117 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
| 118 | return !isOpenMPTargetExecutionDirective(Kind) && |
| 119 | isOpenMPTeamsDirective(Kind); |
| 120 | } |
| 121 | |
| 122 | public: |
| 123 | OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 124 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 125 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 126 | }; |
| 127 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 128 | /// Private scope for OpenMP loop-based directives, that supports capturing |
| 129 | /// of used expression from loop statement. |
| 130 | class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { |
| 131 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 132 | CodeGenFunction::OMPMapVars PreCondVars; |
Alexey Bataev | f71939c | 2019-09-18 19:24:07 +0000 | [diff] [blame] | 133 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 134 | for (const auto *E : S.counters()) { |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 135 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f71939c | 2019-09-18 19:24:07 +0000 | [diff] [blame] | 136 | EmittedAsPrivate.insert(VD->getCanonicalDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 137 | (void)PreCondVars.setVarAddr( |
| 138 | CGF, VD, CGF.CreateMemTemp(VD->getType().getNonReferenceType())); |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 139 | } |
Alexey Bataev | f71939c | 2019-09-18 19:24:07 +0000 | [diff] [blame] | 140 | // Mark private vars as undefs. |
| 141 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
| 142 | for (const Expr *IRef : C->varlists()) { |
| 143 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(IRef)->getDecl()); |
| 144 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 145 | (void)PreCondVars.setVarAddr( |
| 146 | CGF, OrigVD, |
| 147 | Address(llvm::UndefValue::get( |
| 148 | CGF.ConvertTypeForMem(CGF.getContext().getPointerType( |
| 149 | OrigVD->getType().getNonReferenceType()))), |
| 150 | CGF.getContext().getDeclAlign(OrigVD))); |
| 151 | } |
| 152 | } |
| 153 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 154 | (void)PreCondVars.apply(CGF); |
Alexey Bataev | bef93a9 | 2019-10-07 18:54:57 +0000 | [diff] [blame] | 155 | // Emit init, __range and __end variables for C++ range loops. |
| 156 | const Stmt *Body = |
| 157 | S.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers(); |
| 158 | for (unsigned Cnt = 0; Cnt < S.getCollapsedNumber(); ++Cnt) { |
Alexey Bataev | 8bbf2e3 | 2019-11-04 09:59:11 -0500 | [diff] [blame] | 159 | Body = OMPLoopDirective::tryToFindNextInnerLoop( |
| 160 | Body, /*TryImperfectlyNestedLoops=*/true); |
Alexey Bataev | bef93a9 | 2019-10-07 18:54:57 +0000 | [diff] [blame] | 161 | if (auto *For = dyn_cast<ForStmt>(Body)) { |
| 162 | Body = For->getBody(); |
| 163 | } else { |
| 164 | assert(isa<CXXForRangeStmt>(Body) && |
Alexey Bataev | d457f7e | 2019-10-07 19:57:40 +0000 | [diff] [blame] | 165 | "Expected canonical for loop or range-based for loop."); |
Alexey Bataev | bef93a9 | 2019-10-07 18:54:57 +0000 | [diff] [blame] | 166 | auto *CXXFor = cast<CXXForRangeStmt>(Body); |
| 167 | if (const Stmt *Init = CXXFor->getInit()) |
| 168 | CGF.EmitStmt(Init); |
| 169 | CGF.EmitStmt(CXXFor->getRangeStmt()); |
| 170 | CGF.EmitStmt(CXXFor->getEndStmt()); |
| 171 | Body = CXXFor->getBody(); |
| 172 | } |
| 173 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 174 | if (const auto *PreInits = cast_or_null<DeclStmt>(S.getPreInits())) { |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 175 | for (const auto *I : PreInits->decls()) |
| 176 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 177 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 178 | PreCondVars.restore(CGF); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | public: |
| 182 | OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) |
| 183 | : CodeGenFunction::RunCleanupsScope(CGF) { |
| 184 | emitPreInitStmt(CGF, S); |
| 185 | } |
| 186 | }; |
| 187 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 188 | class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope { |
| 189 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 190 | |
| 191 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 192 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 193 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 194 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && |
| 195 | cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); |
| 196 | } |
| 197 | |
| 198 | public: |
| 199 | OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 200 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 201 | InlinedShareds(CGF) { |
| 202 | for (const auto *C : S.clauses()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 203 | if (const auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 204 | if (const auto *PreInit = |
| 205 | cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 206 | for (const auto *I : PreInit->decls()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 207 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 208 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 209 | } else { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 210 | CodeGenFunction::AutoVarEmission Emission = |
| 211 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 212 | CGF.EmitAutoVarCleanups(Emission); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) { |
| 217 | for (const Expr *E : UDP->varlists()) { |
| 218 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 219 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D)) |
| 220 | CGF.EmitVarDecl(*OED); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 225 | CGF.EmitOMPPrivateClause(S, InlinedShareds); |
| 226 | if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) { |
| 227 | if (const Expr *E = TG->getReductionRef()) |
| 228 | CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())); |
| 229 | } |
| 230 | const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt()); |
| 231 | while (CS) { |
| 232 | for (auto &C : CS->captures()) { |
| 233 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 234 | auto *VD = C.getCapturedVar(); |
| 235 | assert(VD == VD->getCanonicalDecl() && |
| 236 | "Canonical decl must be captured."); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 237 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 238 | isCapturedVar(CGF, VD) || |
| 239 | (CGF.CapturedStmtInfo && |
| 240 | InlinedShareds.isGlobalVarCaptured(VD)), |
| 241 | VD->getType().getNonReferenceType(), VK_LValue, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 242 | C.getLocation()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 243 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 244 | return CGF.EmitLValue(&DRE).getAddress(CGF); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 245 | }); |
| 246 | } |
| 247 | } |
| 248 | CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt()); |
| 249 | } |
| 250 | (void)InlinedShareds.Privatize(); |
| 251 | } |
| 252 | }; |
| 253 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 254 | } // namespace |
| 255 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 256 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 257 | const OMPExecutableDirective &S, |
| 258 | const RegionCodeGenTy &CodeGen); |
| 259 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 260 | LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 261 | if (const auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) { |
| 262 | if (const auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) { |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 263 | OrigVD = OrigVD->getCanonicalDecl(); |
| 264 | bool IsCaptured = |
| 265 | LambdaCaptureFields.lookup(OrigVD) || |
| 266 | (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) || |
| 267 | (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 268 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), IsCaptured, |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 269 | OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc()); |
| 270 | return EmitLValue(&DRE); |
| 271 | } |
| 272 | } |
| 273 | return EmitLValue(E); |
| 274 | } |
| 275 | |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 276 | llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 277 | ASTContext &C = getContext(); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 278 | llvm::Value *Size = nullptr; |
| 279 | auto SizeInChars = C.getTypeSizeInChars(Ty); |
| 280 | if (SizeInChars.isZero()) { |
| 281 | // getTypeSizeInChars() returns 0 for a VLA. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 282 | while (const VariableArrayType *VAT = C.getAsVariableArrayType(Ty)) { |
| 283 | VlaSizePair VlaSize = getVLASize(VAT); |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 284 | Ty = VlaSize.Type; |
| 285 | Size = Size ? Builder.CreateNUWMul(Size, VlaSize.NumElts) |
| 286 | : VlaSize.NumElts; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 287 | } |
| 288 | SizeInChars = C.getTypeSizeInChars(Ty); |
| 289 | if (SizeInChars.isZero()) |
| 290 | return llvm::ConstantInt::get(SizeTy, /*V=*/0); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 291 | return Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); |
| 292 | } |
| 293 | return CGM.getSize(SizeInChars); |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 296 | void CodeGenFunction::GenerateOpenMPCapturedVars( |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 297 | const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 298 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 299 | auto CurField = RD->field_begin(); |
| 300 | auto CurCap = S.captures().begin(); |
| 301 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 302 | E = S.capture_init_end(); |
| 303 | I != E; ++I, ++CurField, ++CurCap) { |
| 304 | if (CurField->hasCapturedVLAType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 305 | const VariableArrayType *VAT = CurField->getCapturedVLAType(); |
| 306 | llvm::Value *Val = VLASizeMap[VAT->getSizeExpr()]; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 307 | CapturedVars.push_back(Val); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 308 | } else if (CurCap->capturesThis()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 309 | CapturedVars.push_back(CXXThisValue); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 310 | } else if (CurCap->capturesVariableByCopy()) { |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 311 | llvm::Value *CV = EmitLoadOfScalar(EmitLValue(*I), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 312 | |
| 313 | // If the field is not a pointer, we need to save the actual value |
| 314 | // and load it as a void pointer. |
| 315 | if (!CurField->getType()->isAnyPointerType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 316 | ASTContext &Ctx = getContext(); |
| 317 | Address DstAddr = CreateMemTemp( |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 318 | Ctx.getUIntPtrType(), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 319 | Twine(CurCap->getCapturedVar()->getName(), ".casted")); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 320 | LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 321 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 322 | llvm::Value *SrcAddrVal = EmitScalarConversion( |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 323 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 324 | Ctx.getPointerType(CurField->getType()), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 325 | LValue SrcLV = |
| 326 | MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); |
| 327 | |
| 328 | // Store the value using the source type pointer. |
| 329 | EmitStoreThroughLValue(RValue::get(CV), SrcLV); |
| 330 | |
| 331 | // Load the value using the destination type pointer. |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 332 | CV = EmitLoadOfScalar(DstLV, CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 333 | } |
| 334 | CapturedVars.push_back(CV); |
| 335 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 336 | assert(CurCap->capturesVariable() && "Expected capture by reference."); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 337 | CapturedVars.push_back(EmitLValue(*I).getAddress(*this).getPointer()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 338 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
| 341 | |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 342 | static Address castValueFromUintptr(CodeGenFunction &CGF, SourceLocation Loc, |
| 343 | QualType DstType, StringRef Name, |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 344 | LValue AddrLV) { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 345 | ASTContext &Ctx = CGF.getContext(); |
| 346 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 347 | llvm::Value *CastedPtr = CGF.EmitScalarConversion( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 348 | AddrLV.getAddress(CGF).getPointer(), Ctx.getUIntPtrType(), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 349 | Ctx.getPointerType(DstType), Loc); |
| 350 | Address TmpAddr = |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 351 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 352 | .getAddress(CGF); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 353 | return TmpAddr; |
| 354 | } |
| 355 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 356 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 357 | if (T->isLValueReferenceType()) |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 358 | return C.getLValueReferenceType( |
| 359 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 360 | /*SpelledAsLValue=*/false); |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 361 | if (T->isPointerType()) |
| 362 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 363 | if (const ArrayType *A = T->getAsArrayTypeUnsafe()) { |
| 364 | if (const auto *VLA = dyn_cast<VariableArrayType>(A)) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 365 | return getCanonicalParamType(C, VLA->getElementType()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 366 | if (!A->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 367 | return C.getCanonicalType(T); |
| 368 | } |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 369 | return C.getCanonicalParamType(T); |
| 370 | } |
| 371 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 372 | namespace { |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 373 | /// Contains required data for proper outlined function codegen. |
| 374 | struct FunctionOptions { |
| 375 | /// Captured statement for which the function is generated. |
| 376 | const CapturedStmt *S = nullptr; |
| 377 | /// true if cast to/from UIntPtr is required for variables captured by |
| 378 | /// value. |
| 379 | const bool UIntPtrCastRequired = true; |
| 380 | /// true if only casted arguments must be registered as local args or VLA |
| 381 | /// sizes. |
| 382 | const bool RegisterCastedArgsOnly = false; |
| 383 | /// Name of the generated function. |
| 384 | const StringRef FunctionName; |
| 385 | /// Location of the non-debug version of the outlined function. |
| 386 | SourceLocation Loc; |
| 387 | explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, |
| 388 | bool RegisterCastedArgsOnly, StringRef FunctionName, |
| 389 | SourceLocation Loc) |
| 390 | : S(S), UIntPtrCastRequired(UIntPtrCastRequired), |
| 391 | RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), |
| 392 | FunctionName(FunctionName), Loc(Loc) {} |
| 393 | }; |
| 394 | } // namespace |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 395 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 396 | static llvm::Function *emitOutlinedFunctionPrologue( |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 397 | CodeGenFunction &CGF, FunctionArgList &Args, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 398 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 399 | &LocalAddrs, |
| 400 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> |
| 401 | &VLASizes, |
| 402 | llvm::Value *&CXXThisValue, const FunctionOptions &FO) { |
| 403 | const CapturedDecl *CD = FO.S->getCapturedDecl(); |
| 404 | const RecordDecl *RD = FO.S->getCapturedRecordDecl(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 405 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 406 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 407 | CXXThisValue = nullptr; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 408 | // Build the argument list. |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 409 | CodeGenModule &CGM = CGF.CGM; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 410 | ASTContext &Ctx = CGM.getContext(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 411 | FunctionArgList TargetArgs; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 412 | Args.append(CD->param_begin(), |
| 413 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 414 | TargetArgs.append( |
| 415 | CD->param_begin(), |
| 416 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 417 | auto I = FO.S->captures().begin(); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 418 | FunctionDecl *DebugFunctionDecl = nullptr; |
| 419 | if (!FO.UIntPtrCastRequired) { |
| 420 | FunctionProtoType::ExtProtoInfo EPI; |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 421 | QualType FunctionTy = Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 422 | DebugFunctionDecl = FunctionDecl::Create( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 423 | Ctx, Ctx.getTranslationUnitDecl(), FO.S->getBeginLoc(), |
Jonas Devlieghere | 64a2630 | 2018-11-11 00:56:15 +0000 | [diff] [blame] | 424 | SourceLocation(), DeclarationName(), FunctionTy, |
| 425 | Ctx.getTrivialTypeSourceInfo(FunctionTy), SC_Static, |
| 426 | /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 427 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 428 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 429 | QualType ArgType = FD->getType(); |
| 430 | IdentifierInfo *II = nullptr; |
| 431 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 432 | |
| 433 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 434 | // function argument type should be uintptr and the value properly casted to |
| 435 | // uintptr. This is necessary given that the runtime library is only able to |
| 436 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 437 | // outlined function. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 438 | if (FO.UIntPtrCastRequired && |
| 439 | ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
| 440 | I->capturesVariableArrayType())) |
| 441 | ArgType = Ctx.getUIntPtrType(); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 442 | |
| 443 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 444 | CapVar = I->getCapturedVar(); |
| 445 | II = CapVar->getIdentifier(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 446 | } else if (I->capturesThis()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 447 | II = &Ctx.Idents.get("this"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 448 | } else { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 449 | assert(I->capturesVariableArrayType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 450 | II = &Ctx.Idents.get("vla"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 451 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 452 | if (ArgType->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 453 | ArgType = getCanonicalParamType(Ctx, ArgType); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 454 | VarDecl *Arg; |
| 455 | if (DebugFunctionDecl && (CapVar || I->capturesThis())) { |
| 456 | Arg = ParmVarDecl::Create( |
| 457 | Ctx, DebugFunctionDecl, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 458 | CapVar ? CapVar->getBeginLoc() : FD->getBeginLoc(), |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 459 | CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, |
| 460 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
| 461 | } else { |
| 462 | Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), |
| 463 | II, ArgType, ImplicitParamDecl::Other); |
| 464 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 465 | Args.emplace_back(Arg); |
| 466 | // Do not cast arguments if we emit function with non-original types. |
| 467 | TargetArgs.emplace_back( |
| 468 | FO.UIntPtrCastRequired |
| 469 | ? Arg |
| 470 | : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 471 | ++I; |
| 472 | } |
| 473 | Args.append( |
| 474 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 475 | CD->param_end()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 476 | TargetArgs.append( |
| 477 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 478 | CD->param_end()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 479 | |
| 480 | // Create the function declaration. |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 481 | const CGFunctionInfo &FuncInfo = |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 482 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 483 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 484 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 485 | auto *F = |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 486 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 487 | FO.FunctionName, &CGM.getModule()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 488 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 489 | if (CD->isNothrow()) |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 490 | F->setDoesNotThrow(); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 491 | F->setDoesNotRecurse(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 492 | |
| 493 | // Generate the function. |
Alexey Bataev | 6e01dc1 | 2017-08-14 16:03:47 +0000 | [diff] [blame] | 494 | CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs, |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 495 | FO.UIntPtrCastRequired ? FO.Loc : FO.S->getBeginLoc(), |
| 496 | FO.UIntPtrCastRequired ? FO.Loc |
| 497 | : CD->getBody()->getBeginLoc()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 498 | unsigned Cnt = CD->getContextParamPosition(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 499 | I = FO.S->captures().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 500 | for (const FieldDecl *FD : RD->fields()) { |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 501 | // Do not map arguments if we emit function with non-original types. |
| 502 | Address LocalAddr(Address::invalid()); |
| 503 | if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) { |
| 504 | LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt], |
| 505 | TargetArgs[Cnt]); |
| 506 | } else { |
| 507 | LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]); |
| 508 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 509 | // If we are capturing a pointer by copy we don't need to do anything, just |
| 510 | // use the value that we get from the arguments. |
| 511 | if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 512 | const VarDecl *CurVD = I->getCapturedVar(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 513 | if (!FO.RegisterCastedArgsOnly) |
| 514 | LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}}); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 515 | ++Cnt; |
| 516 | ++I; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 517 | continue; |
| 518 | } |
| 519 | |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 520 | LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), |
| 521 | AlignmentSource::Decl); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 522 | if (FD->hasCapturedVLAType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 523 | if (FO.UIntPtrCastRequired) { |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 524 | ArgLVal = CGF.MakeAddrLValue( |
| 525 | castValueFromUintptr(CGF, I->getLocation(), FD->getType(), |
| 526 | Args[Cnt]->getName(), ArgLVal), |
| 527 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 528 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 529 | llvm::Value *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
| 530 | const VariableArrayType *VAT = FD->getCapturedVLAType(); |
| 531 | VLASizes.try_emplace(Args[Cnt], VAT->getSizeExpr(), ExprArg); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 532 | } else if (I->capturesVariable()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 533 | const VarDecl *Var = I->getCapturedVar(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 534 | QualType VarTy = Var->getType(); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 535 | Address ArgAddr = ArgLVal.getAddress(CGF); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 536 | if (ArgLVal.getType()->isLValueReferenceType()) { |
| 537 | ArgAddr = CGF.EmitLoadOfReference(ArgLVal); |
| 538 | } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) { |
| 539 | assert(ArgLVal.getType()->isPointerType()); |
| 540 | ArgAddr = CGF.EmitLoadOfPointer( |
| 541 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 542 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 543 | if (!FO.RegisterCastedArgsOnly) { |
| 544 | LocalAddrs.insert( |
| 545 | {Args[Cnt], |
| 546 | {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); |
| 547 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 548 | } else if (I->capturesVariableByCopy()) { |
| 549 | assert(!FD->getType()->isAnyPointerType() && |
| 550 | "Not expecting a captured pointer."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 551 | const VarDecl *Var = I->getCapturedVar(); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 552 | LocalAddrs.insert({Args[Cnt], |
| 553 | {Var, FO.UIntPtrCastRequired |
| 554 | ? castValueFromUintptr( |
| 555 | CGF, I->getLocation(), FD->getType(), |
| 556 | Args[Cnt]->getName(), ArgLVal) |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 557 | : ArgLVal.getAddress(CGF)}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 558 | } else { |
| 559 | // If 'this' is captured, load it into CXXThisValue. |
| 560 | assert(I->capturesThis()); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 561 | CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 562 | LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress(CGF)}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 563 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 564 | ++Cnt; |
| 565 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 568 | return F; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | llvm::Function * |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 572 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S, |
| 573 | SourceLocation Loc) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 574 | assert( |
| 575 | CapturedStmtInfo && |
| 576 | "CapturedStmtInfo should be set when generating the captured function"); |
| 577 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 578 | // Build the argument list. |
| 579 | bool NeedWrapperFunction = |
Amy Huang | 53539bb | 2020-01-13 15:54:54 -0800 | [diff] [blame] | 580 | getDebugInfo() && CGM.getCodeGenOpts().hasReducedDebugInfo(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 581 | FunctionArgList Args; |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 582 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 583 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 584 | SmallString<256> Buffer; |
| 585 | llvm::raw_svector_ostream Out(Buffer); |
| 586 | Out << CapturedStmtInfo->getHelperName(); |
| 587 | if (NeedWrapperFunction) |
| 588 | Out << "_debug__"; |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 589 | FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 590 | Out.str(), Loc); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 591 | llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, |
| 592 | VLASizes, CXXThisValue, FO); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 593 | CodeGenFunction::OMPPrivateScope LocalScope(*this); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 594 | for (const auto &LocalAddrPair : LocalAddrs) { |
| 595 | if (LocalAddrPair.second.first) { |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 596 | LocalScope.addPrivate(LocalAddrPair.second.first, [&LocalAddrPair]() { |
| 597 | return LocalAddrPair.second.second; |
| 598 | }); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 599 | } |
| 600 | } |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 601 | (void)LocalScope.Privatize(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 602 | for (const auto &VLASizePair : VLASizes) |
| 603 | VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 604 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 605 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
Alexey Bataev | 06e80f6 | 2019-05-23 18:19:54 +0000 | [diff] [blame] | 606 | (void)LocalScope.ForceCleanup(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 607 | FinishFunction(CD->getBodyRBrace()); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 608 | if (!NeedWrapperFunction) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 609 | return F; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 610 | |
Alexey Bataev | efd884d | 2017-08-04 21:26:25 +0000 | [diff] [blame] | 611 | FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 612 | /*RegisterCastedArgsOnly=*/true, |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 613 | CapturedStmtInfo->getHelperName(), Loc); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 614 | CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 615 | WrapperCGF.CapturedStmtInfo = CapturedStmtInfo; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 616 | Args.clear(); |
| 617 | LocalAddrs.clear(); |
| 618 | VLASizes.clear(); |
| 619 | llvm::Function *WrapperF = |
| 620 | emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 621 | WrapperCGF.CXXThisValue, WrapperFO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 622 | llvm::SmallVector<llvm::Value *, 4> CallArgs; |
| 623 | for (const auto *Arg : Args) { |
| 624 | llvm::Value *CallArg; |
| 625 | auto I = LocalAddrs.find(Arg); |
| 626 | if (I != LocalAddrs.end()) { |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 627 | LValue LV = WrapperCGF.MakeAddrLValue( |
| 628 | I->second.second, |
| 629 | I->second.first ? I->second.first->getType() : Arg->getType(), |
| 630 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 631 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 632 | } else { |
| 633 | auto EI = VLASizes.find(Arg); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 634 | if (EI != VLASizes.end()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 635 | CallArg = EI->second.second; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 636 | } else { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 637 | LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 638 | Arg->getType(), |
| 639 | AlignmentSource::Decl); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 640 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getBeginLoc()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 641 | } |
| 642 | } |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 643 | CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 644 | } |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 645 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, Loc, F, CallArgs); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 646 | WrapperCGF.FinishFunction(); |
| 647 | return WrapperF; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 650 | //===----------------------------------------------------------------------===// |
| 651 | // OpenMP Directive Emission |
| 652 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 653 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 654 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 655 | const llvm::function_ref<void(Address, Address)> CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 656 | // Perform element-by-element initialization. |
| 657 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 658 | |
| 659 | // Drill down to the base element type on both arrays. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 660 | const ArrayType *ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
| 661 | llvm::Value *NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 662 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 663 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 664 | llvm::Value *SrcBegin = SrcAddr.getPointer(); |
| 665 | llvm::Value *DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 666 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 667 | llvm::Value *DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 668 | // The basic structure here is a while-do loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 669 | llvm::BasicBlock *BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 670 | llvm::BasicBlock *DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 671 | llvm::Value *IsEmpty = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 672 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 673 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 674 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 675 | // Enter the loop body, making that address the current address. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 676 | llvm::BasicBlock *EntryBB = Builder.GetInsertBlock(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 677 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 678 | |
| 679 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 680 | |
| 681 | llvm::PHINode *SrcElementPHI = |
| 682 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 683 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 684 | Address SrcElementCurrent = |
| 685 | Address(SrcElementPHI, |
| 686 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 687 | |
| 688 | llvm::PHINode *DestElementPHI = |
| 689 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 690 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 691 | Address DestElementCurrent = |
| 692 | Address(DestElementPHI, |
| 693 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 694 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 695 | // Emit copy. |
| 696 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 697 | |
| 698 | // Shift the address forward by one element. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 699 | llvm::Value *DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 700 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 701 | llvm::Value *SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 702 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 703 | // Check whether we've reached the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 704 | llvm::Value *Done = |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 705 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 706 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 707 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 708 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 709 | |
| 710 | // Done. |
| 711 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 712 | } |
| 713 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 714 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 715 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 716 | const VarDecl *SrcVD, const Expr *Copy) { |
| 717 | if (OriginalType->isArrayType()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 718 | const auto *BO = dyn_cast<BinaryOperator>(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 719 | if (BO && BO->getOpcode() == BO_Assign) { |
| 720 | // Perform simple memcpy for simple copying. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 721 | LValue Dest = MakeAddrLValue(DestAddr, OriginalType); |
| 722 | LValue Src = MakeAddrLValue(SrcAddr, OriginalType); |
| 723 | EmitAggregateAssign(Dest, Src, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 724 | } else { |
| 725 | // For arrays with complex element types perform element by element |
| 726 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 727 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 728 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 729 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 730 | // Working with the single array element, so have to remap |
| 731 | // destination and source variables to corresponding array |
| 732 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 733 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 734 | Remap.addPrivate(DestVD, [DestElement]() { return DestElement; }); |
| 735 | Remap.addPrivate(SrcVD, [SrcElement]() { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 736 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 737 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 738 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 739 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 740 | } else { |
| 741 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 742 | CodeGenFunction::OMPPrivateScope Remap(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 743 | Remap.addPrivate(SrcVD, [SrcAddr]() { return SrcAddr; }); |
| 744 | Remap.addPrivate(DestVD, [DestAddr]() { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 745 | (void)Remap.Privatize(); |
| 746 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 747 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 748 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 749 | } |
| 750 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 751 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 752 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 753 | if (!HaveInsertPoint()) |
| 754 | return false; |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 755 | bool DeviceConstTarget = |
| 756 | getLangOpts().OpenMPIsDevice && |
| 757 | isOpenMPTargetExecutionDirective(D.getDirectiveKind()); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 758 | bool FirstprivateIsLastprivate = false; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 759 | llvm::DenseMap<const VarDecl *, OpenMPLastprivateModifier> Lastprivates; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 760 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 761 | for (const auto *D : C->varlists()) |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 762 | Lastprivates.try_emplace( |
| 763 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl(), |
| 764 | C->getKind()); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 765 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 766 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 767 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 768 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 769 | // Force emission of the firstprivate copy if the directive does not emit |
| 770 | // outlined function, like omp for, omp simd, omp distribute etc. |
| 771 | bool MustEmitFirstprivateCopy = |
| 772 | CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 773 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 774 | const auto *IRef = C->varlist_begin(); |
| 775 | const auto *InitsRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 776 | for (const Expr *IInit : C->private_copies()) { |
| 777 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 778 | bool ThisFirstprivateIsLastprivate = |
| 779 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 780 | const FieldDecl *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 781 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 782 | if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 783 | !FD->getType()->isReferenceType() && |
| 784 | (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) { |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 785 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 786 | ++IRef; |
| 787 | ++InitsRef; |
| 788 | continue; |
| 789 | } |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 790 | // Do not emit copy for firstprivate constant variables in target regions, |
| 791 | // captured by reference. |
| 792 | if (DeviceConstTarget && OrigVD->getType().isConstant(getContext()) && |
Alexey Bataev | 9c39781 | 2019-04-03 17:57:06 +0000 | [diff] [blame] | 793 | FD && FD->getType()->isReferenceType() && |
| 794 | (!VD || !VD->hasAttr<OMPAllocateDeclAttr>())) { |
Alexey Bataev | 1af5bd5 | 2019-03-05 17:47:18 +0000 | [diff] [blame] | 795 | (void)CGM.getOpenMPRuntime().registerTargetFirstprivateCopy(*this, |
| 796 | OrigVD); |
| 797 | ++IRef; |
| 798 | ++InitsRef; |
| 799 | continue; |
| 800 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 801 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 802 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 803 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 804 | const auto *VDInit = |
| 805 | cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 806 | bool IsRegistered; |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 807 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 808 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 809 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Alexey Bataev | e0ef04f | 2019-05-23 22:30:43 +0000 | [diff] [blame] | 810 | LValue OriginalLVal; |
| 811 | if (!FD) { |
| 812 | // Check if the firstprivate variable is just a constant value. |
| 813 | ConstantEmission CE = tryEmitAsConstant(&DRE); |
| 814 | if (CE && !CE.isReference()) { |
| 815 | // Constant value, no need to create a copy. |
| 816 | ++IRef; |
| 817 | ++InitsRef; |
| 818 | continue; |
| 819 | } |
| 820 | if (CE && CE.isReference()) { |
| 821 | OriginalLVal = CE.getReferenceLValue(*this, &DRE); |
| 822 | } else { |
| 823 | assert(!CE && "Expected non-constant firstprivate."); |
| 824 | OriginalLVal = EmitLValue(&DRE); |
| 825 | } |
| 826 | } else { |
| 827 | OriginalLVal = EmitLValue(&DRE); |
| 828 | } |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 829 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 830 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 831 | // Emit VarDecl with copy init for arrays. |
| 832 | // Get the address of the original variable captured in current |
| 833 | // captured region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 834 | IsRegistered = PrivateScope.addPrivate( |
| 835 | OrigVD, [this, VD, Type, OriginalLVal, VDInit]() { |
| 836 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 837 | const Expr *Init = VD->getInit(); |
| 838 | if (!isa<CXXConstructExpr>(Init) || |
| 839 | isTrivialInitializer(Init)) { |
| 840 | // Perform simple memcpy. |
| 841 | LValue Dest = |
| 842 | MakeAddrLValue(Emission.getAllocatedAddress(), Type); |
| 843 | EmitAggregateAssign(Dest, OriginalLVal, Type); |
| 844 | } else { |
| 845 | EmitOMPAggregateAssign( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 846 | Emission.getAllocatedAddress(), |
| 847 | OriginalLVal.getAddress(*this), Type, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 848 | [this, VDInit, Init](Address DestElement, |
| 849 | Address SrcElement) { |
| 850 | // Clean up any temporaries needed by the |
| 851 | // initialization. |
| 852 | RunCleanupsScope InitScope(*this); |
| 853 | // Emit initialization for single element. |
| 854 | setAddrOfLocalVar(VDInit, SrcElement); |
| 855 | EmitAnyExprToMem(Init, DestElement, |
| 856 | Init->getType().getQualifiers(), |
| 857 | /*IsInitializer*/ false); |
| 858 | LocalDeclMap.erase(VDInit); |
| 859 | }); |
| 860 | } |
| 861 | EmitAutoVarCleanups(Emission); |
| 862 | return Emission.getAllocatedAddress(); |
| 863 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 864 | } else { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 865 | Address OriginalAddr = OriginalLVal.getAddress(*this); |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 866 | IsRegistered = |
| 867 | PrivateScope.addPrivate(OrigVD, [this, VDInit, OriginalAddr, VD, |
| 868 | ThisFirstprivateIsLastprivate, |
| 869 | OrigVD, &Lastprivates, IRef]() { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 870 | // Emit private VarDecl with copy init. |
| 871 | // Remap temp VDInit variable to the address of the original |
| 872 | // variable (for proper handling of captured global variables). |
| 873 | setAddrOfLocalVar(VDInit, OriginalAddr); |
| 874 | EmitDecl(*VD); |
| 875 | LocalDeclMap.erase(VDInit); |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 876 | if (ThisFirstprivateIsLastprivate && |
| 877 | Lastprivates[OrigVD->getCanonicalDecl()] == |
| 878 | OMPC_LASTPRIVATE_conditional) { |
| 879 | // Create/init special variable for lastprivate conditionals. |
| 880 | Address VDAddr = |
| 881 | CGM.getOpenMPRuntime().emitLastprivateConditionalInit( |
| 882 | *this, OrigVD); |
| 883 | llvm::Value *V = EmitLoadOfScalar( |
| 884 | MakeAddrLValue(GetAddrOfLocalVar(VD), (*IRef)->getType(), |
| 885 | AlignmentSource::Decl), |
| 886 | (*IRef)->getExprLoc()); |
| 887 | EmitStoreOfScalar(V, |
| 888 | MakeAddrLValue(VDAddr, (*IRef)->getType(), |
| 889 | AlignmentSource::Decl)); |
| 890 | LocalDeclMap.erase(VD); |
| 891 | setAddrOfLocalVar(VD, VDAddr); |
| 892 | return VDAddr; |
| 893 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 894 | return GetAddrOfLocalVar(VD); |
| 895 | }); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 896 | } |
| 897 | assert(IsRegistered && |
| 898 | "firstprivate var already registered as private"); |
| 899 | // Silence the warning about unused variable. |
| 900 | (void)IsRegistered; |
| 901 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 902 | ++IRef; |
| 903 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 904 | } |
| 905 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 906 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 907 | } |
| 908 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 909 | void CodeGenFunction::EmitOMPPrivateClause( |
| 910 | const OMPExecutableDirective &D, |
| 911 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 912 | if (!HaveInsertPoint()) |
| 913 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 914 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 915 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 916 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 917 | for (const Expr *IInit : C->private_copies()) { |
| 918 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 919 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 920 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 921 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD]() { |
| 922 | // Emit private VarDecl with copy init. |
| 923 | EmitDecl(*VD); |
| 924 | return GetAddrOfLocalVar(VD); |
| 925 | }); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 926 | assert(IsRegistered && "private var already registered as private"); |
| 927 | // Silence the warning about unused variable. |
| 928 | (void)IsRegistered; |
| 929 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 930 | ++IRef; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 935 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 936 | if (!HaveInsertPoint()) |
| 937 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 938 | // threadprivate_var1 = master_threadprivate_var1; |
| 939 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 940 | // ... |
| 941 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 942 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 943 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 944 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 945 | auto IRef = C->varlist_begin(); |
| 946 | auto ISrcRef = C->source_exprs().begin(); |
| 947 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 948 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 949 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 950 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 951 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 952 | // Get the address of the master variable. If we are emitting code with |
| 953 | // TLS support, the address is passed from the master as field in the |
| 954 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 955 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 956 | if (getLangOpts().OpenMPUseTLS && |
| 957 | getContext().getTargetInfo().isTLSSupported()) { |
| 958 | assert(CapturedStmtInfo->lookup(VD) && |
| 959 | "Copyin threadprivates should have been captured!"); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 960 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), true, |
| 961 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 962 | MasterAddr = EmitLValue(&DRE).getAddress(*this); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 963 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 964 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 965 | MasterAddr = |
| 966 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 967 | : CGM.GetAddrOfGlobal(VD), |
| 968 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 969 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 970 | // Get the address of the threadprivate variable. |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 971 | Address PrivateAddr = EmitLValue(*IRef).getAddress(*this); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 972 | if (CopiedVars.size() == 1) { |
| 973 | // At first check if current thread is a master thread. If it is, no |
| 974 | // need to copy data. |
| 975 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 976 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 977 | Builder.CreateCondBr( |
| 978 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 979 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 980 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), |
| 981 | CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 982 | CopyBegin, CopyEnd); |
| 983 | EmitBlock(CopyBegin); |
| 984 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 985 | const auto *SrcVD = |
| 986 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 987 | const auto *DestVD = |
| 988 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 989 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 990 | } |
| 991 | ++IRef; |
| 992 | ++ISrcRef; |
| 993 | ++IDestRef; |
| 994 | } |
| 995 | } |
| 996 | if (CopyEnd) { |
| 997 | // Exit out of copying procedure for non-master thread. |
| 998 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 999 | return true; |
| 1000 | } |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1004 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 1005 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1006 | if (!HaveInsertPoint()) |
| 1007 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1008 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1009 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1010 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1011 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1012 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1013 | SIMDLCVs.insert( |
| 1014 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1015 | } |
| 1016 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1017 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1018 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 1019 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1020 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 1021 | !getLangOpts().OpenMPSimd) |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 1022 | break; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1023 | const auto *IRef = C->varlist_begin(); |
| 1024 | const auto *IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1025 | for (const Expr *IInit : C->private_copies()) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1026 | // Keep the address of the original variable for future update at the end |
| 1027 | // of the loop. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1028 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 1029 | // Taskloops do not require additional initialization, it is done in |
| 1030 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1031 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1032 | const auto *DestVD = |
| 1033 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 1034 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1035 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
| 1036 | /*RefersToEnclosingVariableOrCapture=*/ |
| 1037 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1038 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1039 | return EmitLValue(&DRE).getAddress(*this); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1040 | }); |
| 1041 | // Check if the variable is also a firstprivate: in this case IInit is |
| 1042 | // not generated. Initialization of this variable will happen in codegen |
| 1043 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1044 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1045 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1046 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, VD, C, |
| 1047 | OrigVD]() { |
| 1048 | if (C->getKind() == OMPC_LASTPRIVATE_conditional) { |
| 1049 | Address VDAddr = |
| 1050 | CGM.getOpenMPRuntime().emitLastprivateConditionalInit(*this, |
| 1051 | OrigVD); |
| 1052 | setAddrOfLocalVar(VD, VDAddr); |
| 1053 | return VDAddr; |
| 1054 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 1055 | // Emit private VarDecl with copy init. |
| 1056 | EmitDecl(*VD); |
| 1057 | return GetAddrOfLocalVar(VD); |
| 1058 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 1059 | assert(IsRegistered && |
| 1060 | "lastprivate var already registered as private"); |
| 1061 | (void)IsRegistered; |
| 1062 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1063 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 1064 | ++IRef; |
| 1065 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | return HasAtLeastOneLastprivate; |
| 1069 | } |
| 1070 | |
| 1071 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1072 | const OMPExecutableDirective &D, bool NoFinals, |
| 1073 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1074 | if (!HaveInsertPoint()) |
| 1075 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1076 | // Emit following code: |
| 1077 | // if (<IsLastIterCond>) { |
| 1078 | // orig_var1 = private_orig_var1; |
| 1079 | // ... |
| 1080 | // orig_varn = private_orig_varn; |
| 1081 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1082 | llvm::BasicBlock *ThenBB = nullptr; |
| 1083 | llvm::BasicBlock *DoneBB = nullptr; |
| 1084 | if (IsLastIterCond) { |
Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 1085 | // Emit implicit barrier if at least one lastprivate conditional is found |
| 1086 | // and this is not a simd mode. |
| 1087 | if (!getLangOpts().OpenMPSimd && |
| 1088 | llvm::any_of(D.getClausesOfKind<OMPLastprivateClause>(), |
| 1089 | [](const OMPLastprivateClause *C) { |
| 1090 | return C->getKind() == OMPC_LASTPRIVATE_conditional; |
| 1091 | })) { |
| 1092 | CGM.getOpenMPRuntime().emitBarrierCall(*this, D.getBeginLoc(), |
| 1093 | OMPD_unknown, |
| 1094 | /*EmitChecks=*/false, |
| 1095 | /*ForceSimpleCall=*/true); |
| 1096 | } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1097 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 1098 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 1099 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 1100 | EmitBlock(ThenBB); |
| 1101 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1102 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 1103 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1104 | if (const auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1105 | auto IC = LoopDirective->counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1106 | for (const Expr *F : LoopDirective->finals()) { |
| 1107 | const auto *D = |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1108 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 1109 | if (NoFinals) |
| 1110 | AlreadyEmittedVars.insert(D); |
| 1111 | else |
| 1112 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1113 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1116 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 1117 | auto IRef = C->varlist_begin(); |
| 1118 | auto ISrcRef = C->source_exprs().begin(); |
| 1119 | auto IDestRef = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1120 | for (const Expr *AssignOp : C->assignment_ops()) { |
| 1121 | const auto *PrivateVD = |
| 1122 | cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1123 | QualType Type = PrivateVD->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1124 | const auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1125 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 1126 | // If lastprivate variable is a loop control variable for loop-based |
| 1127 | // directive, update its value before copyin back to original |
| 1128 | // variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1129 | if (const Expr *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1130 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1131 | const auto *SrcVD = |
| 1132 | cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 1133 | const auto *DestVD = |
| 1134 | cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1135 | // Get the address of the private variable. |
| 1136 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1137 | if (const auto *RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1138 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1139 | Address(Builder.CreateLoad(PrivateAddr), |
| 1140 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 1141 | // Store the last value to the private copy in the last iteration. |
| 1142 | if (C->getKind() == OMPC_LASTPRIVATE_conditional) |
| 1143 | CGM.getOpenMPRuntime().emitLastprivateConditionalFinalUpdate( |
| 1144 | *this, MakeAddrLValue(PrivateAddr, (*IRef)->getType()), PrivateVD, |
| 1145 | (*IRef)->getExprLoc()); |
| 1146 | // Get the address of the original variable. |
| 1147 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1148 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1149 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1150 | ++IRef; |
| 1151 | ++ISrcRef; |
| 1152 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1153 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1154 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1155 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1156 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1157 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1158 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1161 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 1162 | const OMPExecutableDirective &D, |
| 1163 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1164 | if (!HaveInsertPoint()) |
| 1165 | return; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1166 | SmallVector<const Expr *, 4> Shareds; |
| 1167 | SmallVector<const Expr *, 4> Privates; |
| 1168 | SmallVector<const Expr *, 4> ReductionOps; |
| 1169 | SmallVector<const Expr *, 4> LHSs; |
| 1170 | SmallVector<const Expr *, 4> RHSs; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1171 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1172 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1173 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1174 | auto ILHS = C->lhs_exprs().begin(); |
| 1175 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1176 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1177 | Shareds.emplace_back(Ref); |
| 1178 | Privates.emplace_back(*IPriv); |
| 1179 | ReductionOps.emplace_back(*IRed); |
| 1180 | LHSs.emplace_back(*ILHS); |
| 1181 | RHSs.emplace_back(*IRHS); |
| 1182 | std::advance(IPriv, 1); |
| 1183 | std::advance(IRed, 1); |
| 1184 | std::advance(ILHS, 1); |
| 1185 | std::advance(IRHS, 1); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1186 | } |
| 1187 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1188 | ReductionCodeGen RedCG(Shareds, Privates, ReductionOps); |
| 1189 | unsigned Count = 0; |
| 1190 | auto ILHS = LHSs.begin(); |
| 1191 | auto IRHS = RHSs.begin(); |
| 1192 | auto IPriv = Privates.begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1193 | for (const Expr *IRef : Shareds) { |
| 1194 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1195 | // Emit private VarDecl with reduction init. |
| 1196 | RedCG.emitSharedLValue(*this, Count); |
| 1197 | RedCG.emitAggregateType(*this, Count); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1198 | AutoVarEmission Emission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1199 | RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), |
| 1200 | RedCG.getSharedLValue(Count), |
| 1201 | [&Emission](CodeGenFunction &CGF) { |
| 1202 | CGF.EmitAutoVarInit(Emission); |
| 1203 | return true; |
| 1204 | }); |
| 1205 | EmitAutoVarCleanups(Emission); |
| 1206 | Address BaseAddr = RedCG.adjustPrivateAddress( |
| 1207 | *this, Count, Emission.getAllocatedAddress()); |
| 1208 | bool IsRegistered = PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1209 | RedCG.getBaseDecl(Count), [BaseAddr]() { return BaseAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1210 | assert(IsRegistered && "private var already registered as private"); |
| 1211 | // Silence the warning about unused variable. |
| 1212 | (void)IsRegistered; |
| 1213 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1214 | const auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 1215 | const auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1216 | QualType Type = PrivateVD->getType(); |
| 1217 | bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); |
| 1218 | if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1219 | // Store the address of the original variable associated with the LHS |
| 1220 | // implicit variable. |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1221 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() { |
| 1222 | return RedCG.getSharedLValue(Count).getAddress(*this); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1223 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1224 | PrivateScope.addPrivate( |
| 1225 | RHSVD, [this, PrivateVD]() { return GetAddrOfLocalVar(PrivateVD); }); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1226 | } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || |
| 1227 | isa<ArraySubscriptExpr>(IRef)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1228 | // Store the address of the original variable associated with the LHS |
| 1229 | // implicit variable. |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1230 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count, this]() { |
| 1231 | return RedCG.getSharedLValue(Count).getAddress(*this); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1232 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1233 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1234 | return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), |
| 1235 | ConvertTypeForMem(RHSVD->getType()), |
| 1236 | "rhs.begin"); |
| 1237 | }); |
| 1238 | } else { |
| 1239 | QualType Type = PrivateVD->getType(); |
| 1240 | bool IsArray = getContext().getAsArrayType(Type) != nullptr; |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1241 | Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(*this); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1242 | // Store the address of the original variable associated with the LHS |
| 1243 | // implicit variable. |
| 1244 | if (IsArray) { |
| 1245 | OriginalAddr = Builder.CreateElementBitCast( |
| 1246 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1247 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1248 | PrivateScope.addPrivate(LHSVD, [OriginalAddr]() { return OriginalAddr; }); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1249 | PrivateScope.addPrivate( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1250 | RHSVD, [this, PrivateVD, RHSVD, IsArray]() { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1251 | return IsArray |
| 1252 | ? Builder.CreateElementBitCast( |
| 1253 | GetAddrOfLocalVar(PrivateVD), |
| 1254 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin") |
| 1255 | : GetAddrOfLocalVar(PrivateVD); |
| 1256 | }); |
| 1257 | } |
| 1258 | ++ILHS; |
| 1259 | ++IRHS; |
| 1260 | ++IPriv; |
| 1261 | ++Count; |
| 1262 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1266 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1267 | if (!HaveInsertPoint()) |
| 1268 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1269 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1270 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1271 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1272 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1273 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1274 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1275 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1276 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1277 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1278 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1279 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1280 | } |
| 1281 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1282 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1283 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1284 | ReductionKind == OMPD_simd; |
| 1285 | bool SimpleReduction = ReductionKind == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1286 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1287 | // parallel directive (it always has implicit barrier). |
| 1288 | CGM.getOpenMPRuntime().emitReduction( |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 1289 | *this, D.getEndLoc(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1290 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1294 | static void emitPostUpdateForReductionClause( |
| 1295 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1296 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1297 | if (!CGF.HaveInsertPoint()) |
| 1298 | return; |
| 1299 | llvm::BasicBlock *DoneBB = nullptr; |
| 1300 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1301 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1302 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1303 | if (llvm::Value *Cond = CondGen(CGF)) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1304 | // If the first post-update expression is found, emit conditional |
| 1305 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1306 | llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1307 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1308 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1309 | CGF.EmitBlock(ThenBB); |
| 1310 | } |
| 1311 | } |
| 1312 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1313 | } |
| 1314 | } |
| 1315 | if (DoneBB) |
| 1316 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1317 | } |
| 1318 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1319 | namespace { |
| 1320 | /// Codegen lambda for appending distribute lower and upper bounds to outlined |
| 1321 | /// parallel function. This is necessary for combined constructs such as |
| 1322 | /// 'distribute parallel for' |
| 1323 | typedef llvm::function_ref<void(CodeGenFunction &, |
| 1324 | const OMPExecutableDirective &, |
| 1325 | llvm::SmallVectorImpl<llvm::Value *> &)> |
| 1326 | CodeGenBoundParametersTy; |
| 1327 | } // anonymous namespace |
| 1328 | |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1329 | static void |
| 1330 | checkForLastprivateConditionalUpdate(CodeGenFunction &CGF, |
| 1331 | const OMPExecutableDirective &S) { |
| 1332 | if (CGF.getLangOpts().OpenMP < 50) |
| 1333 | return; |
| 1334 | llvm::DenseSet<CanonicalDeclPtr<const VarDecl>> PrivateDecls; |
| 1335 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 1336 | for (const Expr *Ref : C->varlists()) { |
| 1337 | if (!Ref->getType()->isScalarType()) |
| 1338 | continue; |
| 1339 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 1340 | if (!DRE) |
| 1341 | continue; |
| 1342 | PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); |
| 1343 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref); |
| 1344 | } |
| 1345 | } |
| 1346 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 1347 | for (const Expr *Ref : C->varlists()) { |
| 1348 | if (!Ref->getType()->isScalarType()) |
| 1349 | continue; |
| 1350 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 1351 | if (!DRE) |
| 1352 | continue; |
| 1353 | PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); |
| 1354 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref); |
| 1355 | } |
| 1356 | } |
| 1357 | for (const auto *C : S.getClausesOfKind<OMPLinearClause>()) { |
| 1358 | for (const Expr *Ref : C->varlists()) { |
| 1359 | if (!Ref->getType()->isScalarType()) |
| 1360 | continue; |
| 1361 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 1362 | if (!DRE) |
| 1363 | continue; |
| 1364 | PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); |
| 1365 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, Ref); |
| 1366 | } |
| 1367 | } |
| 1368 | // Privates should ne analyzed since they are not captured at all. |
| 1369 | // Task reductions may be skipped - tasks are ignored. |
| 1370 | // Firstprivates do not return value but may be passed by reference - no need |
| 1371 | // to check for updated lastprivate conditional. |
| 1372 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 1373 | for (const Expr *Ref : C->varlists()) { |
| 1374 | if (!Ref->getType()->isScalarType()) |
| 1375 | continue; |
| 1376 | const auto *DRE = dyn_cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 1377 | if (!DRE) |
| 1378 | continue; |
| 1379 | PrivateDecls.insert(cast<VarDecl>(DRE->getDecl())); |
| 1380 | } |
| 1381 | } |
| 1382 | CGF.CGM.getOpenMPRuntime().checkAndEmitSharedLastprivateConditional( |
| 1383 | CGF, S, PrivateDecls); |
| 1384 | } |
| 1385 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1386 | static void emitCommonOMPParallelDirective( |
| 1387 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 1388 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1389 | const CodeGenBoundParametersTy &CodeGenBoundParameters) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1390 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 1391 | llvm::Function *OutlinedFn = |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1392 | CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1393 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1394 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1395 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1396 | llvm::Value *NumThreads = |
| 1397 | CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1398 | /*IgnoreResultAssign=*/true); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1399 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1400 | CGF, NumThreads, NumThreadsClause->getBeginLoc()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1401 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1402 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1403 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1404 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1405 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getBeginLoc()); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1406 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1407 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1408 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1409 | if (C->getNameModifier() == OMPD_unknown || |
| 1410 | C->getNameModifier() == OMPD_parallel) { |
| 1411 | IfCond = C->getCondition(); |
| 1412 | break; |
| 1413 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1414 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1415 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1416 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1417 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1418 | // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk |
| 1419 | // lower and upper bounds with the pragma 'for' chunking mechanism. |
| 1420 | // The following lambda takes care of appending the lower and upper bound |
| 1421 | // parameters when necessary |
| 1422 | CodeGenBoundParameters(CGF, S, CapturedVars); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1423 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1424 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getBeginLoc(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1425 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1428 | static void emitEmptyBoundParameters(CodeGenFunction &, |
| 1429 | const OMPExecutableDirective &, |
| 1430 | llvm::SmallVectorImpl<llvm::Value *> &) {} |
| 1431 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1432 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Johannes Doerfert | 10fedd9 | 2019-12-26 11:23:38 -0600 | [diff] [blame] | 1433 | if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { |
| 1434 | // Check if we have any if clause associated with the directive. |
| 1435 | llvm::Value *IfCond = nullptr; |
| 1436 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
| 1437 | IfCond = EmitScalarExpr(C->getCondition(), |
| 1438 | /*IgnoreResultAssign=*/true); |
| 1439 | |
| 1440 | llvm::Value *NumThreads = nullptr; |
| 1441 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) |
| 1442 | NumThreads = EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1443 | /*IgnoreResultAssign=*/true); |
| 1444 | |
| 1445 | ProcBindKind ProcBind = OMP_PROC_BIND_default; |
| 1446 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) |
| 1447 | ProcBind = ProcBindClause->getProcBindKind(); |
| 1448 | |
| 1449 | using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; |
| 1450 | |
| 1451 | // The cleanup callback that finalizes all variabels at the given location, |
| 1452 | // thus calls destructors etc. |
| 1453 | auto FiniCB = [this](InsertPointTy IP) { |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 1454 | OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); |
Johannes Doerfert | 10fedd9 | 2019-12-26 11:23:38 -0600 | [diff] [blame] | 1455 | }; |
| 1456 | |
| 1457 | // Privatization callback that performs appropriate action for |
| 1458 | // shared/private/firstprivate/lastprivate/copyin/... variables. |
| 1459 | // |
| 1460 | // TODO: This defaults to shared right now. |
| 1461 | auto PrivCB = [](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, |
| 1462 | llvm::Value &Val, llvm::Value *&ReplVal) { |
| 1463 | // The next line is appropriate only for variables (Val) with the |
| 1464 | // data-sharing attribute "shared". |
| 1465 | ReplVal = &Val; |
| 1466 | |
| 1467 | return CodeGenIP; |
| 1468 | }; |
| 1469 | |
| 1470 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
| 1471 | const Stmt *ParallelRegionBodyStmt = CS->getCapturedStmt(); |
| 1472 | |
| 1473 | auto BodyGenCB = [ParallelRegionBodyStmt, |
| 1474 | this](InsertPointTy AllocaIP, InsertPointTy CodeGenIP, |
| 1475 | llvm::BasicBlock &ContinuationBB) { |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 1476 | OMPBuilderCBHelpers::OutlinedRegionBodyRAII ORB(*this, AllocaIP, |
| 1477 | ContinuationBB); |
| 1478 | OMPBuilderCBHelpers::EmitOMPRegionBody(*this, ParallelRegionBodyStmt, |
| 1479 | CodeGenIP, ContinuationBB); |
Johannes Doerfert | 10fedd9 | 2019-12-26 11:23:38 -0600 | [diff] [blame] | 1480 | }; |
| 1481 | |
| 1482 | CGCapturedStmtInfo CGSI(*CS, CR_OpenMP); |
| 1483 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); |
| 1484 | Builder.restoreIP(OMPBuilder->CreateParallel(Builder, BodyGenCB, PrivCB, |
| 1485 | FiniCB, IfCond, NumThreads, |
| 1486 | ProcBind, S.hasCancel())); |
| 1487 | return; |
| 1488 | } |
| 1489 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1490 | // Emit parallel region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1491 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 1492 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1493 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1494 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1495 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1496 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1497 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1498 | // propagation master's thread values of threadprivate variables to local |
| 1499 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1500 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 1501 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1502 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1503 | } |
| 1504 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1505 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1506 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1507 | CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1508 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1509 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 1510 | { |
| 1511 | auto LPCRegion = |
| 1512 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
| 1513 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, |
| 1514 | emitEmptyBoundParameters); |
| 1515 | emitPostUpdateForReductionClause(*this, S, |
| 1516 | [](CodeGenFunction &) { return nullptr; }); |
| 1517 | } |
| 1518 | // Check for outer lastprivate conditional update. |
| 1519 | checkForLastprivateConditionalUpdate(*this, S); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1520 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1521 | |
Alexey Bataev | 8bbf2e3 | 2019-11-04 09:59:11 -0500 | [diff] [blame] | 1522 | static void emitBody(CodeGenFunction &CGF, const Stmt *S, const Stmt *NextLoop, |
| 1523 | int MaxLevel, int Level = 0) { |
| 1524 | assert(Level < MaxLevel && "Too deep lookup during loop body codegen."); |
| 1525 | const Stmt *SimplifiedS = S->IgnoreContainers(); |
| 1526 | if (const auto *CS = dyn_cast<CompoundStmt>(SimplifiedS)) { |
| 1527 | PrettyStackTraceLoc CrashInfo( |
| 1528 | CGF.getContext().getSourceManager(), CS->getLBracLoc(), |
| 1529 | "LLVM IR generation of compound statement ('{}')"); |
| 1530 | |
| 1531 | // Keep track of the current cleanup stack depth, including debug scopes. |
| 1532 | CodeGenFunction::LexicalScope Scope(CGF, S->getSourceRange()); |
| 1533 | for (const Stmt *CurStmt : CS->body()) |
| 1534 | emitBody(CGF, CurStmt, NextLoop, MaxLevel, Level); |
| 1535 | return; |
| 1536 | } |
| 1537 | if (SimplifiedS == NextLoop) { |
| 1538 | if (const auto *For = dyn_cast<ForStmt>(SimplifiedS)) { |
| 1539 | S = For->getBody(); |
| 1540 | } else { |
| 1541 | assert(isa<CXXForRangeStmt>(SimplifiedS) && |
| 1542 | "Expected canonical for loop or range-based for loop."); |
| 1543 | const auto *CXXFor = cast<CXXForRangeStmt>(SimplifiedS); |
| 1544 | CGF.EmitStmt(CXXFor->getLoopVarStmt()); |
| 1545 | S = CXXFor->getBody(); |
| 1546 | } |
| 1547 | if (Level + 1 < MaxLevel) { |
| 1548 | NextLoop = OMPLoopDirective::tryToFindNextInnerLoop( |
| 1549 | S, /*TryImperfectlyNestedLoops=*/true); |
| 1550 | emitBody(CGF, S, NextLoop, MaxLevel, Level + 1); |
| 1551 | return; |
| 1552 | } |
| 1553 | } |
| 1554 | CGF.EmitStmt(S); |
| 1555 | } |
| 1556 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1557 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1558 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1559 | RunCleanupsScope BodyScope(*this); |
| 1560 | // Update counters values on current iteration. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1561 | for (const Expr *UE : D.updates()) |
| 1562 | EmitIgnoredExpr(UE); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1563 | // Update the linear variables. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1564 | // In distribute directives only loop counters may be marked as linear, no |
| 1565 | // need to generate the code for them. |
| 1566 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1567 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1568 | for (const Expr *UE : C->updates()) |
| 1569 | EmitIgnoredExpr(UE); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1570 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1571 | } |
| 1572 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1573 | // On a continue in the body, jump to the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1574 | JumpDest Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1575 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexey Bataev | f8be476 | 2019-08-14 19:30:06 +0000 | [diff] [blame] | 1576 | for (const Expr *E : D.finals_conditions()) { |
| 1577 | if (!E) |
| 1578 | continue; |
| 1579 | // Check that loop counter in non-rectangular nest fits into the iteration |
| 1580 | // space. |
| 1581 | llvm::BasicBlock *NextBB = createBasicBlock("omp.body.next"); |
| 1582 | EmitBranchOnBoolExpr(E, NextBB, Continue.getBlock(), |
| 1583 | getProfileCount(D.getBody())); |
| 1584 | EmitBlock(NextBB); |
| 1585 | } |
Alexey Bataev | bef93a9 | 2019-10-07 18:54:57 +0000 | [diff] [blame] | 1586 | // Emit loop variables for C++ range loops. |
| 1587 | const Stmt *Body = |
| 1588 | D.getInnermostCapturedStmt()->getCapturedStmt()->IgnoreContainers(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1589 | // Emit loop body. |
Alexey Bataev | 8bbf2e3 | 2019-11-04 09:59:11 -0500 | [diff] [blame] | 1590 | emitBody(*this, Body, |
| 1591 | OMPLoopDirective::tryToFindNextInnerLoop( |
| 1592 | Body, /*TryImperfectlyNestedLoops=*/true), |
| 1593 | D.getCollapsedNumber()); |
| 1594 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1595 | // The end (updates/cleanups). |
| 1596 | EmitBlock(Continue.getBlock()); |
| 1597 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1600 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1601 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1602 | const Expr *IncExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1603 | const llvm::function_ref<void(CodeGenFunction &)> BodyGen, |
| 1604 | const llvm::function_ref<void(CodeGenFunction &)> PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1605 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1606 | |
| 1607 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1608 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1609 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1610 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1611 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1612 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1613 | |
| 1614 | // If there are any cleanups between here and the loop-exit scope, |
| 1615 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1616 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1617 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1618 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1619 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1620 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1621 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1622 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1623 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1624 | if (ExitBlock != LoopExit.getBlock()) { |
| 1625 | EmitBlock(ExitBlock); |
| 1626 | EmitBranchThroughCleanup(LoopExit); |
| 1627 | } |
| 1628 | |
| 1629 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1630 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1631 | |
| 1632 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1633 | JumpDest Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1634 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1635 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1636 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1637 | |
| 1638 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1639 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1640 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1641 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1642 | BreakContinueStack.pop_back(); |
| 1643 | EmitBranch(CondBlock); |
| 1644 | LoopStack.pop(); |
| 1645 | // Emit the fall-through block. |
| 1646 | EmitBlock(LoopExit.getBlock()); |
| 1647 | } |
| 1648 | |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1649 | bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1650 | if (!HaveInsertPoint()) |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1651 | return false; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1652 | // Emit inits for the linear variables. |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1653 | bool HasLinears = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1654 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1655 | for (const Expr *Init : C->inits()) { |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1656 | HasLinears = true; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1657 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
| 1658 | if (const auto *Ref = |
| 1659 | dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1660 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1661 | const auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1662 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1663 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1664 | VD->getInit()->getType(), VK_LValue, |
| 1665 | VD->getInit()->getExprLoc()); |
| 1666 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1667 | VD->getType()), |
| 1668 | /*capturedByInit=*/false); |
| 1669 | EmitAutoVarCleanups(Emission); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1670 | } else { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1671 | EmitVarDecl(*VD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1672 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1673 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1674 | // Emit the linear steps for the linear clauses. |
| 1675 | // 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] | 1676 | if (const auto *CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1677 | if (const auto *SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1678 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1679 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1680 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1681 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1682 | } |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1683 | return HasLinears; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1686 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1687 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1688 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1689 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1690 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1691 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1692 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1693 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1694 | auto IC = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1695 | for (const Expr *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1696 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1697 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1698 | // If the first post-update expression is found, emit conditional |
| 1699 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1700 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.linear.pu"); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1701 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1702 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1703 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1704 | } |
| 1705 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1706 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1707 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1708 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1709 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1710 | Address OrigAddr = EmitLValue(&DRE).getAddress(*this); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1711 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1712 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1713 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1714 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1715 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1716 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1717 | if (const Expr *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1718 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1719 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1720 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1721 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1724 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1725 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1726 | if (!CGF.HaveInsertPoint()) |
| 1727 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1728 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Erich Keane | f759395 | 2019-10-11 14:59:44 +0000 | [diff] [blame] | 1729 | llvm::APInt ClauseAlignment(64, 0); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1730 | if (const Expr *AlignmentExpr = Clause->getAlignment()) { |
| 1731 | auto *AlignmentCI = |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1732 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
Erich Keane | f759395 | 2019-10-11 14:59:44 +0000 | [diff] [blame] | 1733 | ClauseAlignment = AlignmentCI->getValue(); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1734 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1735 | for (const Expr *E : Clause->varlists()) { |
Erich Keane | f759395 | 2019-10-11 14:59:44 +0000 | [diff] [blame] | 1736 | llvm::APInt Alignment(ClauseAlignment); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1737 | if (Alignment == 0) { |
| 1738 | // OpenMP [2.8.1, Description] |
| 1739 | // If no optional parameter is specified, implementation-defined default |
| 1740 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1741 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1742 | CGF.getContext() |
| 1743 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1744 | E->getType()->getPointeeType())) |
| 1745 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1746 | } |
Erich Keane | f759395 | 2019-10-11 14:59:44 +0000 | [diff] [blame] | 1747 | assert((Alignment == 0 || Alignment.isPowerOf2()) && |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1748 | "alignment is not power of 2"); |
| 1749 | if (Alignment != 0) { |
| 1750 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
Fangrui Song | 1d49eb0 | 2020-02-13 16:36:27 -0800 | [diff] [blame] | 1751 | CGF.emitAlignmentAssumption( |
Erich Keane | f759395 | 2019-10-11 14:59:44 +0000 | [diff] [blame] | 1752 | PtrValue, E, /*No second loc needed*/ SourceLocation(), |
| 1753 | llvm::ConstantInt::get(CGF.getLLVMContext(), Alignment)); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1754 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1759 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1760 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1761 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1762 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1763 | auto I = S.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1764 | for (const Expr *E : S.counters()) { |
| 1765 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1766 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1767 | // Emit var without initialization. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1768 | AutoVarEmission VarEmission = EmitAutoVarAlloca(*PrivateVD); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1769 | EmitAutoVarCleanups(VarEmission); |
| 1770 | LocalDeclMap.erase(PrivateVD); |
| 1771 | (void)LoopScope.addPrivate(VD, [&VarEmission]() { |
| 1772 | return VarEmission.getAllocatedAddress(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1773 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1774 | if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || |
| 1775 | VD->hasGlobalStorage()) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1776 | (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1777 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(VD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1778 | LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), |
| 1779 | E->getType(), VK_LValue, E->getExprLoc()); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1780 | return EmitLValue(&DRE).getAddress(*this); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1781 | }); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1782 | } else { |
| 1783 | (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() { |
| 1784 | return VarEmission.getAllocatedAddress(); |
| 1785 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1786 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1787 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1788 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1789 | // Privatize extra loop counters used in loops for ordered(n) clauses. |
| 1790 | for (const auto *C : S.getClausesOfKind<OMPOrderedClause>()) { |
| 1791 | if (!C->getNumForLoops()) |
| 1792 | continue; |
| 1793 | for (unsigned I = S.getCollapsedNumber(), |
| 1794 | E = C->getLoopNumIterations().size(); |
| 1795 | I < E; ++I) { |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 1796 | const auto *DRE = cast<DeclRefExpr>(C->getLoopCounter(I)); |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1797 | const auto *VD = cast<VarDecl>(DRE->getDecl()); |
Alexey Bataev | 0d8fcdf | 2019-03-14 20:36:00 +0000 | [diff] [blame] | 1798 | // Override only those variables that can be captured to avoid re-emission |
| 1799 | // of the variables declared within the loops. |
| 1800 | if (DRE->refersToEnclosingVariableOrCapture()) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 1801 | (void)LoopScope.addPrivate(VD, [this, DRE, VD]() { |
| 1802 | return CreateMemTemp(DRE->getType(), VD->getName()); |
| 1803 | }); |
| 1804 | } |
| 1805 | } |
| 1806 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1809 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1810 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1811 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1812 | if (!CGF.HaveInsertPoint()) |
| 1813 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1814 | { |
| 1815 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1816 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1817 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1818 | // Get initial values of real counters. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1819 | for (const Expr *I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1820 | CGF.EmitIgnoredExpr(I); |
| 1821 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1822 | } |
Alexey Bataev | f8be476 | 2019-08-14 19:30:06 +0000 | [diff] [blame] | 1823 | // Create temp loop control variables with their init values to support |
| 1824 | // non-rectangular loops. |
| 1825 | CodeGenFunction::OMPMapVars PreCondVars; |
| 1826 | for (const Expr * E: S.dependent_counters()) { |
| 1827 | if (!E) |
| 1828 | continue; |
| 1829 | assert(!E->getType().getNonReferenceType()->isRecordType() && |
| 1830 | "dependent counter must not be an iterator."); |
| 1831 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1832 | Address CounterAddr = |
| 1833 | CGF.CreateMemTemp(VD->getType().getNonReferenceType()); |
| 1834 | (void)PreCondVars.setVarAddr(CGF, VD, CounterAddr); |
| 1835 | } |
| 1836 | (void)PreCondVars.apply(CGF); |
| 1837 | for (const Expr *E : S.dependent_inits()) { |
| 1838 | if (!E) |
| 1839 | continue; |
| 1840 | CGF.EmitIgnoredExpr(E); |
| 1841 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1842 | // Check that loop is executed at least one time. |
| 1843 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
Alexey Bataev | f8be476 | 2019-08-14 19:30:06 +0000 | [diff] [blame] | 1844 | PreCondVars.restore(CGF); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1847 | void CodeGenFunction::EmitOMPLinearClause( |
| 1848 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1849 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1850 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1851 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1852 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1853 | const auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1854 | for (const Expr *C : LoopDirective->counters()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1855 | SIMDLCVs.insert( |
| 1856 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1857 | } |
| 1858 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1859 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1860 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1861 | for (const Expr *E : C->varlists()) { |
| 1862 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1863 | const auto *PrivateVD = |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1864 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1865 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1866 | bool IsRegistered = PrivateScope.addPrivate(VD, [this, PrivateVD]() { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1867 | // Emit private VarDecl with copy init. |
| 1868 | EmitVarDecl(*PrivateVD); |
| 1869 | return GetAddrOfLocalVar(PrivateVD); |
| 1870 | }); |
| 1871 | assert(IsRegistered && "linear var already registered as private"); |
| 1872 | // Silence the warning about unused variable. |
| 1873 | (void)IsRegistered; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1874 | } else { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1875 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1876 | } |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1877 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1882 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1883 | const OMPExecutableDirective &D, |
| 1884 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1885 | if (!CGF.HaveInsertPoint()) |
| 1886 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1887 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1888 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1889 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1890 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1891 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1892 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1893 | // the memory instructions parallel, because loop-carried |
| 1894 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1895 | if (!IsMonotonic) |
| 1896 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1897 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1898 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1899 | /*ignoreResult=*/true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1900 | auto *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1901 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1902 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1903 | // the memory instructions parallel, because loop-carried |
| 1904 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1905 | CGF.LoopStack.setParallel(/*Enable=*/false); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1906 | } |
| 1907 | } |
| 1908 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1909 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1910 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1911 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1912 | LoopStack.setParallel(!IsMonotonic); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1913 | LoopStack.setVectorizeEnable(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1914 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | a781521 | 2020-02-03 12:08:16 -0500 | [diff] [blame] | 1915 | if (const auto *C = D.getSingleClause<OMPOrderClause>()) |
| 1916 | if (C->getKind() == OMPC_ORDER_concurrent) |
| 1917 | LoopStack.setParallel(/*Enable=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1920 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1921 | const OMPLoopDirective &D, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1922 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1923 | if (!HaveInsertPoint()) |
| 1924 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1925 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1926 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1927 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1928 | for (const Expr *F : D.finals()) { |
| 1929 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
| 1930 | const auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1931 | const auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1932 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1933 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1934 | if (!DoneBB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1935 | if (llvm::Value *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1936 | // If the first post-update expression is found, emit conditional |
| 1937 | // block if it was requested. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1938 | llvm::BasicBlock *ThenBB = createBasicBlock(".omp.final.then"); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1939 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1940 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1941 | EmitBlock(ThenBB); |
| 1942 | } |
| 1943 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1944 | Address OrigAddr = Address::invalid(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1945 | if (CED) { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1946 | OrigAddr = |
| 1947 | EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(*this); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1948 | } else { |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 1949 | DeclRefExpr DRE(getContext(), const_cast<VarDecl *>(PrivateVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1950 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1951 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 1952 | OrigAddr = EmitLValue(&DRE).getAddress(*this); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1953 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1954 | OMPPrivateScope VarScope(*this); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 1955 | VarScope.addPrivate(OrigVD, [OrigAddr]() { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1956 | (void)VarScope.Privatize(); |
| 1957 | EmitIgnoredExpr(F); |
| 1958 | } |
| 1959 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1960 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1961 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1962 | if (DoneBB) |
| 1963 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1964 | } |
| 1965 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1966 | static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, |
| 1967 | const OMPLoopDirective &S, |
| 1968 | CodeGenFunction::JumpDest LoopExit) { |
| 1969 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1970 | CGF.EmitStopPoint(&S); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 1971 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1972 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1973 | /// Emit a helper variable and return corresponding lvalue. |
| 1974 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1975 | const DeclRefExpr *Helper) { |
| 1976 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1977 | CGF.EmitVarDecl(*VDecl); |
| 1978 | return CGF.EmitLValue(Helper); |
| 1979 | } |
| 1980 | |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 1981 | static void emitCommonSimdLoop(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1982 | const RegionCodeGenTy &SimdInitGen, |
| 1983 | const RegionCodeGenTy &BodyCodeGen) { |
Alexey Bataev | 0860db9 | 2019-12-19 10:01:10 -0500 | [diff] [blame] | 1984 | auto &&ThenGen = [&S, &SimdInitGen, &BodyCodeGen](CodeGenFunction &CGF, |
| 1985 | PrePostActionTy &) { |
| 1986 | CGOpenMPRuntime::NontemporalDeclsRAII NontemporalsRegion(CGF.CGM, S); |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 1987 | CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF); |
| 1988 | SimdInitGen(CGF); |
| 1989 | |
| 1990 | BodyCodeGen(CGF); |
| 1991 | }; |
| 1992 | auto &&ElseGen = [&BodyCodeGen](CodeGenFunction &CGF, PrePostActionTy &) { |
| 1993 | CodeGenFunction::OMPLocalDeclMapRAII Scope(CGF); |
| 1994 | CGF.LoopStack.setVectorizeEnable(/*Enable=*/false); |
| 1995 | |
| 1996 | BodyCodeGen(CGF); |
| 1997 | }; |
| 1998 | const Expr *IfCond = nullptr; |
Alexey Bataev | 18789bf | 2020-02-13 09:21:15 -0500 | [diff] [blame] | 1999 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2000 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 2001 | if (CGF.getLangOpts().OpenMP >= 50 && |
| 2002 | (C->getNameModifier() == OMPD_unknown || |
| 2003 | C->getNameModifier() == OMPD_simd)) { |
| 2004 | IfCond = C->getCondition(); |
| 2005 | break; |
| 2006 | } |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2007 | } |
| 2008 | } |
| 2009 | if (IfCond) { |
| 2010 | CGF.CGM.getOpenMPRuntime().emitIfClause(CGF, IfCond, ThenGen, ElseGen); |
| 2011 | } else { |
| 2012 | RegionCodeGenTy ThenRCG(ThenGen); |
| 2013 | ThenRCG(CGF); |
| 2014 | } |
| 2015 | } |
| 2016 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2017 | static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 2018 | PrePostActionTy &Action) { |
| 2019 | Action.Enter(CGF); |
| 2020 | assert(isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 2021 | "Expected simd directive"); |
| 2022 | OMPLoopScope PreInitScope(CGF, S); |
| 2023 | // if (PreCond) { |
| 2024 | // for (IV in 0..LastIteration) BODY; |
| 2025 | // <Final counter/linear vars updates>; |
| 2026 | // } |
| 2027 | // |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 2028 | if (isOpenMPDistributeDirective(S.getDirectiveKind()) || |
| 2029 | isOpenMPWorksharingDirective(S.getDirectiveKind()) || |
| 2030 | isOpenMPTaskLoopDirective(S.getDirectiveKind())) { |
| 2031 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 2032 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 2033 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 2034 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2035 | // Emit: if (PreCond) - begin. |
| 2036 | // If the condition constant folds and can be elided, avoid emitting the |
| 2037 | // whole loop. |
| 2038 | bool CondConstant; |
| 2039 | llvm::BasicBlock *ContBlock = nullptr; |
| 2040 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2041 | if (!CondConstant) |
| 2042 | return; |
| 2043 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2044 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2045 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
| 2046 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 2047 | CGF.getProfileCount(&S)); |
| 2048 | CGF.EmitBlock(ThenBlock); |
| 2049 | CGF.incrementProfileCounter(&S); |
| 2050 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2051 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2052 | // Emit the loop iteration variable. |
| 2053 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2054 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2055 | CGF.EmitVarDecl(*IVDecl); |
| 2056 | CGF.EmitIgnoredExpr(S.getInit()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2057 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2058 | // Emit the iterations count variable. |
| 2059 | // If it is not a variable, Sema decided to calculate iterations count on |
| 2060 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2061 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2062 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2063 | // Emit calculation of the iterations count. |
| 2064 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2065 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2066 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2067 | emitAlignedClause(CGF, S); |
| 2068 | (void)CGF.EmitOMPLinearClauseInit(S); |
| 2069 | { |
| 2070 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2071 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2072 | CGF.EmitOMPLinearClause(S, LoopScope); |
| 2073 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2074 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 2075 | CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion( |
| 2076 | CGF, S, CGF.EmitLValue(S.getIterationVariable())); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2077 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2078 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2079 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2080 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | d08c056 | 2019-11-19 12:07:54 -0500 | [diff] [blame] | 2081 | |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2082 | emitCommonSimdLoop( |
| 2083 | CGF, S, |
| 2084 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2085 | CGF.EmitOMPSimdInit(S); |
| 2086 | }, |
| 2087 | [&S, &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2088 | CGF.EmitOMPInnerLoop( |
| 2089 | S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), |
| 2090 | [&S](CodeGenFunction &CGF) { |
| 2091 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 2092 | CGF.EmitStopPoint(&S); |
| 2093 | }, |
| 2094 | [](CodeGenFunction &) {}); |
| 2095 | }); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2096 | CGF.EmitOMPSimdFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2097 | // Emit final copy of the lastprivate variables at the end of loops. |
| 2098 | if (HasLastprivateClause) |
| 2099 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
| 2100 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2101 | emitPostUpdateForReductionClause(CGF, S, |
| 2102 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2103 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2104 | CGF.EmitOMPLinearClauseFinal(S, [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2105 | // Emit: if (PreCond) - end. |
| 2106 | if (ContBlock) { |
| 2107 | CGF.EmitBranch(ContBlock); |
| 2108 | CGF.EmitBlock(ContBlock, true); |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
| 2113 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2114 | emitOMPSimdRegion(CGF, S, Action); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2115 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 2116 | { |
| 2117 | auto LPCRegion = |
| 2118 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
| 2119 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
| 2120 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2121 | } |
| 2122 | // Check for outer lastprivate conditional update. |
| 2123 | checkForLastprivateConditionalUpdate(*this, S); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 2124 | } |
| 2125 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2126 | void CodeGenFunction::EmitOMPOuterLoop( |
| 2127 | bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, |
| 2128 | CodeGenFunction::OMPPrivateScope &LoopScope, |
| 2129 | const CodeGenFunction::OMPLoopArguments &LoopArgs, |
| 2130 | const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, |
| 2131 | const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2132 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2133 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2134 | const Expr *IVExpr = S.getIterationVariable(); |
| 2135 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2136 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2137 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2138 | JumpDest LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2139 | |
| 2140 | // Start the loop with a block that tests the condition. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2141 | llvm::BasicBlock *CondBlock = createBasicBlock("omp.dispatch.cond"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2142 | EmitBlock(CondBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2143 | const SourceRange R = S.getSourceRange(); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 2144 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 2145 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2146 | |
| 2147 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2148 | if (!DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2149 | // UB = min(UB, GlobalUB) or |
| 2150 | // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. |
| 2151 | // 'distribute parallel for') |
| 2152 | EmitIgnoredExpr(LoopArgs.EUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2153 | // IV = LB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2154 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2155 | // IV < UB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2156 | BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2157 | } else { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2158 | BoolCondVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2159 | RT.emitForNext(*this, S.getBeginLoc(), IVSize, IVSigned, LoopArgs.IL, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2160 | LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2161 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2162 | |
| 2163 | // If there are any cleanups between here and the loop-exit scope, |
| 2164 | // create a block to stage a loop exit along. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2165 | llvm::BasicBlock *ExitBlock = LoopExit.getBlock(); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2166 | if (LoopScope.requiresCleanups()) |
| 2167 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 2168 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2169 | llvm::BasicBlock *LoopBody = createBasicBlock("omp.dispatch.body"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2170 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 2171 | if (ExitBlock != LoopExit.getBlock()) { |
| 2172 | EmitBlock(ExitBlock); |
| 2173 | EmitBranchThroughCleanup(LoopExit); |
| 2174 | } |
| 2175 | EmitBlock(LoopBody); |
| 2176 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2177 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 2178 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2179 | if (DynamicOrOrdered) |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2180 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2181 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2182 | // Create a block for the increment. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2183 | JumpDest Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2184 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 2185 | |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2186 | emitCommonSimdLoop( |
| 2187 | *this, S, |
| 2188 | [&S, IsMonotonic](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2189 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 2190 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a781521 | 2020-02-03 12:08:16 -0500 | [diff] [blame] | 2191 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2192 | CGF.LoopStack.setParallel(!IsMonotonic); |
Alexey Bataev | a781521 | 2020-02-03 12:08:16 -0500 | [diff] [blame] | 2193 | if (const auto *C = S.getSingleClause<OMPOrderClause>()) |
| 2194 | if (C->getKind() == OMPC_ORDER_concurrent) |
| 2195 | CGF.LoopStack.setParallel(/*Enable=*/true); |
| 2196 | } else { |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2197 | CGF.EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | a781521 | 2020-02-03 12:08:16 -0500 | [diff] [blame] | 2198 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2199 | }, |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2200 | [&S, &LoopArgs, LoopExit, &CodeGenLoop, IVSize, IVSigned, &CodeGenOrdered, |
| 2201 | &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2202 | SourceLocation Loc = S.getBeginLoc(); |
| 2203 | // when 'distribute' is not combined with a 'for': |
| 2204 | // while (idx <= UB) { BODY; ++idx; } |
| 2205 | // when 'distribute' is combined with a 'for' |
| 2206 | // (e.g. 'distribute parallel for') |
| 2207 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 2208 | CGF.EmitOMPInnerLoop( |
| 2209 | S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, |
| 2210 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 2211 | CodeGenLoop(CGF, S, LoopExit); |
| 2212 | }, |
| 2213 | [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { |
| 2214 | CodeGenOrdered(CGF, Loc, IVSize, IVSigned); |
| 2215 | }); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2216 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2217 | |
| 2218 | EmitBlock(Continue.getBlock()); |
| 2219 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2220 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2221 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2222 | EmitIgnoredExpr(LoopArgs.NextLB); |
| 2223 | EmitIgnoredExpr(LoopArgs.NextUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 2224 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2225 | |
| 2226 | EmitBranch(CondBlock); |
| 2227 | LoopStack.pop(); |
| 2228 | // Emit the fall-through block. |
| 2229 | EmitBlock(LoopExit.getBlock()); |
| 2230 | |
| 2231 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2232 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 2233 | if (!DynamicOrOrdered) |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2234 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2235 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2236 | }; |
| 2237 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2241 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2242 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2243 | const OMPLoopArguments &LoopArgs, |
| 2244 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2245 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2246 | |
| 2247 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2248 | const bool DynamicOrOrdered = |
| 2249 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2250 | |
| 2251 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2252 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2253 | LoopArgs.Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2254 | "static non-chunked schedule does not need outer loop"); |
| 2255 | |
| 2256 | // Emit outer loop. |
| 2257 | // |
| 2258 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2259 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 2260 | // distributed to threads in the team in chunks as the threads request them. |
| 2261 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 2262 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 2263 | // iterations, except for the last chunk to be distributed, which may have |
| 2264 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 2265 | // |
| 2266 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 2267 | // to threads in the team in chunks as the executing threads request them. |
| 2268 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 2269 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 2270 | // each chunk is proportional to the number of unassigned iterations divided |
| 2271 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 2272 | // with value k (greater than 1), the size of each chunk is determined in the |
| 2273 | // same way, with the restriction that the chunks do not contain fewer than k |
| 2274 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 2275 | // than k iterations). |
| 2276 | // |
| 2277 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 2278 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 2279 | // implementation the freedom to choose any possible mapping of iterations to |
| 2280 | // threads in the team. |
| 2281 | // |
| 2282 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 2283 | // deferred until run time, and the schedule and chunk size are taken from the |
| 2284 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 2285 | // implementation defined |
| 2286 | // |
| 2287 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 2288 | // idx = LB; |
| 2289 | // while (idx <= UB) { BODY; ++idx; |
| 2290 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 2291 | // } // inner loop |
| 2292 | // } |
| 2293 | // |
| 2294 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2295 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 2296 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 2297 | // the team in a round-robin fashion in the order of the thread number. |
| 2298 | // |
| 2299 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 2300 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 2301 | // LB = LB + ST; |
| 2302 | // UB = UB + ST; |
| 2303 | // } |
| 2304 | // |
| 2305 | |
| 2306 | const Expr *IVExpr = S.getIterationVariable(); |
| 2307 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2308 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2309 | |
| 2310 | if (DynamicOrOrdered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2311 | const std::pair<llvm::Value *, llvm::Value *> DispatchBounds = |
| 2312 | CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2313 | llvm::Value *LBVal = DispatchBounds.first; |
| 2314 | llvm::Value *UBVal = DispatchBounds.second; |
| 2315 | CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, |
| 2316 | LoopArgs.Chunk}; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2317 | RT.emitForDispatchInit(*this, S.getBeginLoc(), ScheduleKind, IVSize, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2318 | IVSigned, Ordered, DipatchRTInputValues); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2319 | } else { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2320 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2321 | IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, |
| 2322 | LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2323 | RT.emitForStaticInit(*this, S.getBeginLoc(), S.getDirectiveKind(), |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2324 | ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2325 | } |
| 2326 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2327 | auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, |
| 2328 | const unsigned IVSize, |
| 2329 | const bool IVSigned) { |
| 2330 | if (Ordered) { |
| 2331 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, |
| 2332 | IVSigned); |
| 2333 | } |
| 2334 | }; |
| 2335 | |
| 2336 | OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, |
| 2337 | LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); |
| 2338 | OuterLoopArgs.IncExpr = S.getInc(); |
| 2339 | OuterLoopArgs.Init = S.getInit(); |
| 2340 | OuterLoopArgs.Cond = S.getCond(); |
| 2341 | OuterLoopArgs.NextLB = S.getNextLowerBound(); |
| 2342 | OuterLoopArgs.NextUB = S.getNextUpperBound(); |
| 2343 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, |
| 2344 | emitOMPLoopBodyWithStopPoint, CodeGenOrdered); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2345 | } |
| 2346 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2347 | static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, |
| 2348 | const unsigned IVSize, const bool IVSigned) {} |
| 2349 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2350 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2351 | OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, |
| 2352 | OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, |
| 2353 | const CodeGenLoopTy &CodeGenLoopContent) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2354 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2355 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2356 | |
| 2357 | // Emit outer loop. |
| 2358 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 2359 | // dynamic |
| 2360 | // |
| 2361 | |
| 2362 | const Expr *IVExpr = S.getIterationVariable(); |
| 2363 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2364 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 2365 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2366 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2367 | IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, |
| 2368 | LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2369 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 2370 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2371 | // for combined 'distribute' and 'for' the increment expression of distribute |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2372 | // is stored in DistInc. For 'distribute' alone, it is in Inc. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2373 | Expr *IncExpr; |
| 2374 | if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) |
| 2375 | IncExpr = S.getDistInc(); |
| 2376 | else |
| 2377 | IncExpr = S.getInc(); |
| 2378 | |
| 2379 | // this routine is shared by 'omp distribute parallel for' and |
| 2380 | // 'omp distribute': select the right EUB expression depending on the |
| 2381 | // directive |
| 2382 | OMPLoopArguments OuterLoopArgs; |
| 2383 | OuterLoopArgs.LB = LoopArgs.LB; |
| 2384 | OuterLoopArgs.UB = LoopArgs.UB; |
| 2385 | OuterLoopArgs.ST = LoopArgs.ST; |
| 2386 | OuterLoopArgs.IL = LoopArgs.IL; |
| 2387 | OuterLoopArgs.Chunk = LoopArgs.Chunk; |
| 2388 | OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2389 | ? S.getCombinedEnsureUpperBound() |
| 2390 | : S.getEnsureUpperBound(); |
| 2391 | OuterLoopArgs.IncExpr = IncExpr; |
| 2392 | OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2393 | ? S.getCombinedInit() |
| 2394 | : S.getInit(); |
| 2395 | OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2396 | ? S.getCombinedCond() |
| 2397 | : S.getCond(); |
| 2398 | OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2399 | ? S.getCombinedNextLowerBound() |
| 2400 | : S.getNextLowerBound(); |
| 2401 | OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2402 | ? S.getCombinedNextUpperBound() |
| 2403 | : S.getNextUpperBound(); |
| 2404 | |
| 2405 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, |
| 2406 | LoopScope, OuterLoopArgs, CodeGenLoopContent, |
| 2407 | emitEmptyOrdered); |
| 2408 | } |
| 2409 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2410 | static std::pair<LValue, LValue> |
| 2411 | emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, |
| 2412 | const OMPExecutableDirective &S) { |
| 2413 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2414 | LValue LB = |
| 2415 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2416 | LValue UB = |
| 2417 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2418 | |
| 2419 | // When composing 'distribute' with 'for' (e.g. as in 'distribute |
| 2420 | // parallel for') we need to use the 'distribute' |
| 2421 | // chunk lower and upper bounds rather than the whole loop iteration |
| 2422 | // space. These are parameters to the outlined function for 'parallel' |
| 2423 | // and we copy the bounds of the previous schedule into the |
| 2424 | // the current ones. |
| 2425 | LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); |
| 2426 | LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2427 | llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar( |
| 2428 | PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2429 | PrevLBVal = CGF.EmitScalarConversion( |
| 2430 | PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2431 | LS.getIterationVariable()->getType(), |
| 2432 | LS.getPrevLowerBoundVariable()->getExprLoc()); |
| 2433 | llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar( |
| 2434 | PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2435 | PrevUBVal = CGF.EmitScalarConversion( |
| 2436 | PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2437 | LS.getIterationVariable()->getType(), |
| 2438 | LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2439 | |
| 2440 | CGF.EmitStoreOfScalar(PrevLBVal, LB); |
| 2441 | CGF.EmitStoreOfScalar(PrevUBVal, UB); |
| 2442 | |
| 2443 | return {LB, UB}; |
| 2444 | } |
| 2445 | |
| 2446 | /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then |
| 2447 | /// we need to use the LB and UB expressions generated by the worksharing |
| 2448 | /// code generation support, whereas in non combined situations we would |
| 2449 | /// just emit 0 and the LastIteration expression |
| 2450 | /// This function is necessary due to the difference of the LB and UB |
| 2451 | /// types for the RT emission routines for 'for_static_init' and |
| 2452 | /// 'for_dispatch_init' |
| 2453 | static std::pair<llvm::Value *, llvm::Value *> |
| 2454 | emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, |
| 2455 | const OMPExecutableDirective &S, |
| 2456 | Address LB, Address UB) { |
| 2457 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2458 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2459 | // when implementing a dynamic schedule for a 'for' combined with a |
| 2460 | // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop |
| 2461 | // is not normalized as each team only executes its own assigned |
| 2462 | // distribute chunk |
| 2463 | QualType IteratorTy = IVExpr->getType(); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2464 | llvm::Value *LBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2465 | CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2466 | llvm::Value *UBVal = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2467 | CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getBeginLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2468 | return {LBVal, UBVal}; |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2469 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2470 | |
| 2471 | static void emitDistributeParallelForDistributeInnerBoundParams( |
| 2472 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2473 | llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { |
| 2474 | const auto &Dir = cast<OMPLoopDirective>(S); |
| 2475 | LValue LB = |
| 2476 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 2477 | llvm::Value *LBCast = |
| 2478 | CGF.Builder.CreateIntCast(CGF.Builder.CreateLoad(LB.getAddress(CGF)), |
| 2479 | CGF.SizeTy, /*isSigned=*/false); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2480 | CapturedVars.push_back(LBCast); |
| 2481 | LValue UB = |
| 2482 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); |
| 2483 | |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 2484 | llvm::Value *UBCast = |
| 2485 | CGF.Builder.CreateIntCast(CGF.Builder.CreateLoad(UB.getAddress(CGF)), |
| 2486 | CGF.SizeTy, /*isSigned=*/false); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2487 | CapturedVars.push_back(UBCast); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2488 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2489 | |
| 2490 | static void |
| 2491 | emitInnerParallelForWhenCombined(CodeGenFunction &CGF, |
| 2492 | const OMPLoopDirective &S, |
| 2493 | CodeGenFunction::JumpDest LoopExit) { |
| 2494 | auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2495 | PrePostActionTy &Action) { |
| 2496 | Action.Enter(CGF); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2497 | bool HasCancel = false; |
| 2498 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2499 | if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) |
| 2500 | HasCancel = D->hasCancel(); |
| 2501 | else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) |
| 2502 | HasCancel = D->hasCancel(); |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 2503 | else if (const auto *D = |
| 2504 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) |
| 2505 | HasCancel = D->hasCancel(); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2506 | } |
| 2507 | CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), |
| 2508 | HasCancel); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2509 | CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), |
| 2510 | emitDistributeParallelForInnerBounds, |
| 2511 | emitDistributeParallelForDispatchBounds); |
| 2512 | }; |
| 2513 | |
| 2514 | emitCommonOMPParallelDirective( |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2515 | CGF, S, |
| 2516 | isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, |
| 2517 | CGInlinedWorksharingLoop, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2518 | emitDistributeParallelForDistributeInnerBoundParams); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2521 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 2522 | const OMPDistributeParallelForDirective &S) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2523 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2524 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2525 | S.getDistInc()); |
| 2526 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2527 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 2528 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2529 | } |
| 2530 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2531 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 2532 | const OMPDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2533 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2534 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2535 | S.getDistInc()); |
| 2536 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2537 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2538 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2539 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2540 | |
| 2541 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 2542 | const OMPDistributeSimdDirective &S) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2543 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2544 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 2545 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2546 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2547 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2548 | } |
| 2549 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2550 | void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| 2551 | CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { |
| 2552 | // Emit SPMD target parallel for region as a standalone region. |
| 2553 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2554 | emitOMPSimdRegion(CGF, S, Action); |
| 2555 | }; |
| 2556 | llvm::Function *Fn; |
| 2557 | llvm::Constant *Addr; |
| 2558 | // Emit target region as a standalone region. |
| 2559 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 2560 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 2561 | assert(Fn && Addr && "Target device function emission failed."); |
| 2562 | } |
| 2563 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2564 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 2565 | const OMPTargetSimdDirective &S) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2566 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2567 | emitOMPSimdRegion(CGF, S, Action); |
| 2568 | }; |
| 2569 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2572 | namespace { |
| 2573 | struct ScheduleKindModifiersTy { |
| 2574 | OpenMPScheduleClauseKind Kind; |
| 2575 | OpenMPScheduleClauseModifier M1; |
| 2576 | OpenMPScheduleClauseModifier M2; |
| 2577 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2578 | OpenMPScheduleClauseModifier M1, |
| 2579 | OpenMPScheduleClauseModifier M2) |
| 2580 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2581 | }; |
| 2582 | } // namespace |
| 2583 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2584 | bool CodeGenFunction::EmitOMPWorksharingLoop( |
| 2585 | const OMPLoopDirective &S, Expr *EUB, |
| 2586 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
| 2587 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2588 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2589 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2590 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2591 | EmitVarDecl(*IVDecl); |
| 2592 | |
| 2593 | // Emit the iterations count variable. |
| 2594 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2595 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2596 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2597 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2598 | // Emit calculation of the iterations count. |
| 2599 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2600 | } |
| 2601 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2602 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2603 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2604 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2605 | // Check pre-condition. |
| 2606 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2607 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2608 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2609 | // If the condition constant folds and can be elided, avoid emitting the |
| 2610 | // whole loop. |
| 2611 | bool CondConstant; |
| 2612 | llvm::BasicBlock *ContBlock = nullptr; |
| 2613 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2614 | if (!CondConstant) |
| 2615 | return false; |
| 2616 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2617 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2618 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2619 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2620 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2621 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2622 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2623 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2624 | |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2625 | RunCleanupsScope DoacrossCleanupScope(*this); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2626 | bool Ordered = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2627 | if (const auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2628 | if (OrderedClause->getNumForLoops()) |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 2629 | RT.emitDoacrossInit(*this, S, OrderedClause->getLoopNumIterations()); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2630 | else |
| 2631 | Ordered = true; |
| 2632 | } |
| 2633 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2634 | llvm::DenseSet<const Expr *> EmittedFinals; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2635 | emitAlignedClause(*this, S); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2636 | bool HasLinears = EmitOMPLinearClauseInit(S); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2637 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2638 | |
| 2639 | std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S); |
| 2640 | LValue LB = Bounds.first; |
| 2641 | LValue UB = Bounds.second; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2642 | LValue ST = |
| 2643 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2644 | LValue IL = |
| 2645 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2646 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2647 | // Emit 'then' code. |
| 2648 | { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2649 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2650 | if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2651 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2652 | // initialization of firstprivate variables and post-update of |
| 2653 | // lastprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2654 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2655 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2656 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2657 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 2658 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 2659 | CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion( |
| 2660 | *this, S, EmitLValue(S.getIterationVariable())); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2661 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 2662 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2663 | EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2664 | EmitOMPLinearClause(S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 2665 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2666 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2667 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Detect the loop schedule kind and chunk. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2670 | const Expr *ChunkExpr = nullptr; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2671 | OpenMPScheduleTy ScheduleKind; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2672 | if (const auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2673 | ScheduleKind.Schedule = C->getScheduleKind(); |
| 2674 | ScheduleKind.M1 = C->getFirstScheduleModifier(); |
| 2675 | ScheduleKind.M2 = C->getSecondScheduleModifier(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2676 | ChunkExpr = C->getChunkSize(); |
Gheorghe-Teodor Bercea | 8233af9 | 2018-09-27 20:29:00 +0000 | [diff] [blame] | 2677 | } else { |
| 2678 | // Default behaviour for schedule clause. |
| 2679 | CGM.getOpenMPRuntime().getDefaultScheduleAndChunk( |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2680 | *this, S, ScheduleKind.Schedule, ChunkExpr); |
| 2681 | } |
| 2682 | bool HasChunkSizeOne = false; |
| 2683 | llvm::Value *Chunk = nullptr; |
| 2684 | if (ChunkExpr) { |
| 2685 | Chunk = EmitScalarExpr(ChunkExpr); |
| 2686 | Chunk = EmitScalarConversion(Chunk, ChunkExpr->getType(), |
| 2687 | S.getIterationVariable()->getType(), |
| 2688 | S.getBeginLoc()); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 2689 | Expr::EvalResult Result; |
| 2690 | if (ChunkExpr->EvaluateAsInt(Result, getContext())) { |
| 2691 | llvm::APSInt EvaluatedChunk = Result.Val.getInt(); |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2692 | HasChunkSizeOne = (EvaluatedChunk.getLimitedValue() == 1); |
Fangrui Song | 407659a | 2018-11-30 23:41:18 +0000 | [diff] [blame] | 2693 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2694 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2695 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2696 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2697 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 2698 | // If the static schedule kind is specified or if the ordered clause is |
| 2699 | // specified, and if no monotonic modifier is specified, the effect will |
| 2700 | // be as if the monotonic modifier was specified. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2701 | bool StaticChunkedOne = RT.isStaticChunked(ScheduleKind.Schedule, |
| 2702 | /* Chunked */ Chunk != nullptr) && HasChunkSizeOne && |
| 2703 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
| 2704 | if ((RT.isStaticNonchunked(ScheduleKind.Schedule, |
| 2705 | /* Chunked */ Chunk != nullptr) || |
| 2706 | StaticChunkedOne) && |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2707 | !Ordered) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2708 | JumpDest LoopExit = |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2709 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2710 | emitCommonSimdLoop( |
| 2711 | *this, S, |
| 2712 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | a781521 | 2020-02-03 12:08:16 -0500 | [diff] [blame] | 2713 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2714 | CGF.EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexey Bataev | a781521 | 2020-02-03 12:08:16 -0500 | [diff] [blame] | 2715 | } else if (const auto *C = S.getSingleClause<OMPOrderClause>()) { |
| 2716 | if (C->getKind() == OMPC_ORDER_concurrent) |
| 2717 | CGF.LoopStack.setParallel(/*Enable=*/true); |
| 2718 | } |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 2719 | }, |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2720 | [IVSize, IVSigned, Ordered, IL, LB, UB, ST, StaticChunkedOne, Chunk, |
| 2721 | &S, ScheduleKind, LoopExit, |
| 2722 | &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2723 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2724 | // When no chunk_size is specified, the iteration space is divided |
| 2725 | // into chunks that are approximately equal in size, and at most |
| 2726 | // one chunk is distributed to each thread. Note that the size of |
| 2727 | // the chunks is unspecified in this case. |
| 2728 | CGOpenMPRuntime::StaticRTInput StaticInit( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 2729 | IVSize, IVSigned, Ordered, IL.getAddress(CGF), |
| 2730 | LB.getAddress(CGF), UB.getAddress(CGF), ST.getAddress(CGF), |
Alexey Bataev | 103f3c9e | 2019-11-20 15:59:03 -0500 | [diff] [blame] | 2731 | StaticChunkedOne ? Chunk : nullptr); |
| 2732 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
| 2733 | CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, |
| 2734 | StaticInit); |
| 2735 | // UB = min(UB, GlobalUB); |
| 2736 | if (!StaticChunkedOne) |
| 2737 | CGF.EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 2738 | // IV = LB; |
| 2739 | CGF.EmitIgnoredExpr(S.getInit()); |
| 2740 | // For unchunked static schedule generate: |
| 2741 | // |
| 2742 | // while (idx <= UB) { |
| 2743 | // BODY; |
| 2744 | // ++idx; |
| 2745 | // } |
| 2746 | // |
| 2747 | // For static schedule with chunk one: |
| 2748 | // |
| 2749 | // while (IV <= PrevUB) { |
| 2750 | // BODY; |
| 2751 | // IV += ST; |
| 2752 | // } |
| 2753 | CGF.EmitOMPInnerLoop( |
| 2754 | S, LoopScope.requiresCleanups(), |
| 2755 | StaticChunkedOne ? S.getCombinedParForInDistCond() |
| 2756 | : S.getCond(), |
| 2757 | StaticChunkedOne ? S.getDistInc() : S.getInc(), |
| 2758 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2759 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 2760 | CGF.EmitStopPoint(&S); |
| 2761 | }, |
| 2762 | [](CodeGenFunction &) {}); |
| 2763 | }); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2764 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2765 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2766 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 2767 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2768 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2769 | }; |
| 2770 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2771 | } else { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2772 | const bool IsMonotonic = |
| 2773 | Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || |
| 2774 | ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || |
| 2775 | ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 2776 | ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2777 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2778 | // runtime and runs the inner loop to process it. |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 2779 | const OMPLoopArguments LoopArguments( |
| 2780 | LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this), |
| 2781 | IL.getAddress(*this), Chunk, EUB); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2782 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2783 | LoopArguments, CGDispatchBounds); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2784 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2785 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2786 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
| 2787 | return CGF.Builder.CreateIsNotNull( |
| 2788 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
| 2789 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2790 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2791 | EmitOMPReductionClauseFinal( |
| 2792 | S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) |
| 2793 | ? /*Parallel and Simd*/ OMPD_parallel_for_simd |
| 2794 | : /*Parallel only*/ OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2795 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2796 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2797 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2798 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2799 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2800 | }); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2801 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2802 | if (HasLastprivateClause) |
| 2803 | EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2804 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2805 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2806 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2807 | EmitOMPLinearClauseFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2808 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2809 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2810 | }); |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2811 | DoacrossCleanupScope.ForceCleanup(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2812 | // 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] | 2813 | if (ContBlock) { |
| 2814 | EmitBranch(ContBlock); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2815 | EmitBlock(ContBlock, /*IsFinished=*/true); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2816 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2817 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2818 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2821 | /// The following two functions generate expressions for the loop lower |
| 2822 | /// and upper bounds in case of static and dynamic (dispatch) schedule |
| 2823 | /// of the associated 'for' or 'distribute' loop. |
| 2824 | static std::pair<LValue, LValue> |
| 2825 | emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2826 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2827 | LValue LB = |
| 2828 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2829 | LValue UB = |
| 2830 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2831 | return {LB, UB}; |
| 2832 | } |
| 2833 | |
| 2834 | /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not |
| 2835 | /// consider the lower and upper bound expressions generated by the |
| 2836 | /// worksharing loop support, but we use 0 and the iteration space size as |
| 2837 | /// constants |
| 2838 | static std::pair<llvm::Value *, llvm::Value *> |
| 2839 | emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2840 | Address LB, Address UB) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2841 | const auto &LS = cast<OMPLoopDirective>(S); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2842 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2843 | const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); |
| 2844 | llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); |
| 2845 | llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); |
| 2846 | return {LBVal, UBVal}; |
| 2847 | } |
| 2848 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2849 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2850 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2851 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2852 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2853 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2854 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2855 | emitForLoopBounds, |
| 2856 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2857 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2858 | { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 2859 | auto LPCRegion = |
| 2860 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2861 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2862 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2863 | S.hasCancel()); |
| 2864 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2865 | |
| 2866 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2867 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2868 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 2869 | // Check for outer lastprivate conditional update. |
| 2870 | checkForLastprivateConditionalUpdate(*this, S); |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2871 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2872 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2873 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2874 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2875 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2876 | PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2877 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2878 | emitForLoopBounds, |
| 2879 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2880 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2881 | { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 2882 | auto LPCRegion = |
| 2883 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2884 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2885 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2886 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2887 | |
| 2888 | // Emit an implicit barrier at the end. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2889 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2890 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_for); |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 2891 | // Check for outer lastprivate conditional update. |
| 2892 | checkForLastprivateConditionalUpdate(*this, S); |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2895 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2896 | const Twine &Name, |
| 2897 | llvm::Value *Init = nullptr) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2898 | LValue LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2899 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2900 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2901 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2904 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2905 | const Stmt *CapturedStmt = S.getInnermostCapturedStmt()->getCapturedStmt(); |
| 2906 | const auto *CS = dyn_cast<CompoundStmt>(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2907 | bool HasLastprivates = false; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2908 | auto &&CodeGen = [&S, CapturedStmt, CS, |
| 2909 | &HasLastprivates](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2910 | ASTContext &C = CGF.getContext(); |
| 2911 | QualType KmpInt32Ty = |
| 2912 | C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2913 | // Emit helper vars inits. |
| 2914 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2915 | CGF.Builder.getInt32(0)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2916 | llvm::ConstantInt *GlobalUBVal = CS != nullptr |
| 2917 | ? CGF.Builder.getInt32(CS->size() - 1) |
| 2918 | : CGF.Builder.getInt32(0); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2919 | LValue UB = |
| 2920 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2921 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2922 | CGF.Builder.getInt32(1)); |
| 2923 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2924 | CGF.Builder.getInt32(0)); |
| 2925 | // Loop counter. |
| 2926 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2927 | OpaqueValueExpr IVRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2928 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2929 | OpaqueValueExpr UBRefExpr(S.getBeginLoc(), KmpInt32Ty, VK_LValue); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2930 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2931 | // Generate condition for loop. |
| 2932 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2933 | OK_Ordinary, S.getBeginLoc(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2934 | // Increment for loop counter. |
| 2935 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2936 | S.getBeginLoc(), true); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2937 | auto &&BodyGen = [CapturedStmt, CS, &S, &IV](CodeGenFunction &CGF) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2938 | // Iterate through all sections and emit a switch construct: |
| 2939 | // switch (IV) { |
| 2940 | // case 0: |
| 2941 | // <SectionStmt[0]>; |
| 2942 | // break; |
| 2943 | // ... |
| 2944 | // case <NumSection> - 1: |
| 2945 | // <SectionStmt[<NumSection> - 1]>; |
| 2946 | // break; |
| 2947 | // } |
| 2948 | // .omp.sections.exit: |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2949 | llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 2950 | llvm::SwitchInst *SwitchStmt = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2951 | CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getBeginLoc()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2952 | ExitBB, CS == nullptr ? 1 : CS->size()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2953 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2954 | unsigned CaseNumber = 0; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2955 | for (const Stmt *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2956 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2957 | CGF.EmitBlock(CaseBB); |
| 2958 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2959 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2960 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2961 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2962 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2963 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2964 | llvm::BasicBlock *CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2965 | CGF.EmitBlock(CaseBB); |
| 2966 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 2967 | CGF.EmitStmt(CapturedStmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2968 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2969 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2970 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2971 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2972 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2973 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2974 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2975 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2976 | // initialization of firstprivate variables and post-update of lastprivate |
| 2977 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2978 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2979 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2980 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2981 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2982 | CGF.EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | a58da1a | 2019-12-27 09:44:43 -0500 | [diff] [blame] | 2983 | CGOpenMPRuntime::LastprivateConditionalRAII LPCRegion(CGF, S, IV); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2984 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2985 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2986 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 2987 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 2988 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2989 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2990 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2991 | OpenMPScheduleTy ScheduleKind; |
| 2992 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2993 | CGOpenMPRuntime::StaticRTInput StaticInit( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 2994 | /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(CGF), |
| 2995 | LB.getAddress(CGF), UB.getAddress(CGF), ST.getAddress(CGF)); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2996 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2997 | CGF, S.getBeginLoc(), S.getDirectiveKind(), ScheduleKind, StaticInit); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2998 | // UB = min(UB, GlobalUB); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 2999 | llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, S.getBeginLoc()); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3000 | llvm::Value *MinUBGlobalUB = CGF.Builder.CreateSelect( |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3001 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 3002 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 3003 | // IV = LB; |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3004 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getBeginLoc()), IV); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3005 | // while (idx <= UB) { BODY; ++idx; } |
| 3006 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 3007 | [](CodeGenFunction &) {}); |
| 3008 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 3009 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Stephen Kelly | 1c301dc | 2018-08-09 21:09:38 +0000 | [diff] [blame] | 3010 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getEndLoc(), |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3011 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 3012 | }; |
| 3013 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 3014 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 3015 | // Emit post-update of the reduction variables if IsLastIter != 0. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3016 | emitPostUpdateForReductionClause(CGF, S, [IL, &S](CodeGenFunction &CGF) { |
| 3017 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3018 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3019 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3020 | |
| 3021 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 3022 | if (HasLastprivates) |
| 3023 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3024 | S, /*NoFinals=*/false, |
| 3025 | CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3026 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 3027 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3028 | |
| 3029 | bool HasCancel = false; |
| 3030 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 3031 | HasCancel = OSD->hasCancel(); |
| 3032 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 3033 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 3034 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3035 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 3036 | HasCancel); |
| 3037 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 3038 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 3039 | // directive. |
| 3040 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 3041 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 3042 | // initialization of firstprivate variables. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3043 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 3044 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 3045 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 3046 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 3047 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 3048 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 3049 | { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3050 | auto LPCRegion = |
| 3051 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3052 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 3053 | EmitSections(S); |
| 3054 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 3055 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3056 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3057 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 3058 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 3059 | } |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3060 | // Check for outer lastprivate conditional update. |
| 3061 | checkForLastprivateConditionalUpdate(*this, S); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3065 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3066 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3067 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3068 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 3069 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 3070 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 3071 | } |
| 3072 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 3073 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3074 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3075 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3076 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3077 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3078 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 3079 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3080 | // Build a list of copyprivate variables along with helper expressions |
| 3081 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3082 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3083 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 3084 | DestExprs.append(C->destination_exprs().begin(), |
| 3085 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3086 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 3087 | AssignmentOps.append(C->assignment_ops().begin(), |
| 3088 | C->assignment_ops().end()); |
| 3089 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3090 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 3091 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3092 | Action.Enter(CGF); |
| 3093 | OMPPrivateScope SingleScope(CGF); |
| 3094 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 3095 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 3096 | (void)SingleScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3097 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3098 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 3099 | { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3100 | auto LPCRegion = |
| 3101 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3102 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3103 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getBeginLoc(), |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 3104 | CopyprivateVars, DestExprs, |
| 3105 | SrcExprs, AssignmentOps); |
| 3106 | } |
| 3107 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 3108 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 3109 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 3110 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3111 | *this, S.getBeginLoc(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3112 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 3113 | } |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3114 | // Check for outer lastprivate conditional update. |
| 3115 | checkForLastprivateConditionalUpdate(*this, S); |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 3118 | static void emitMaster(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3119 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3120 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3121 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3122 | }; |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 3123 | CGF.CGM.getOpenMPRuntime().emitMasterRegion(CGF, CodeGen, S.getBeginLoc()); |
| 3124 | } |
| 3125 | |
| 3126 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3127 | if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { |
| 3128 | using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; |
| 3129 | |
| 3130 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
| 3131 | const Stmt *MasterRegionBodyStmt = CS->getCapturedStmt(); |
| 3132 | |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3133 | auto FiniCB = [this](InsertPointTy IP) { |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 3134 | OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3135 | }; |
| 3136 | |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3137 | auto BodyGenCB = [MasterRegionBodyStmt, this](InsertPointTy AllocaIP, |
| 3138 | InsertPointTy CodeGenIP, |
| 3139 | llvm::BasicBlock &FiniBB) { |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 3140 | OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB); |
| 3141 | OMPBuilderCBHelpers::EmitOMPRegionBody(*this, MasterRegionBodyStmt, |
| 3142 | CodeGenIP, FiniBB); |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3143 | }; |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 3144 | |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3145 | CGCapturedStmtInfo CGSI(*CS, CR_OpenMP); |
| 3146 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); |
| 3147 | Builder.restoreIP(OMPBuilder->CreateMaster(Builder, BodyGenCB, FiniCB)); |
| 3148 | |
| 3149 | return; |
| 3150 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3151 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 3152 | emitMaster(*this, S); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 3155 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3156 | if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { |
| 3157 | using InsertPointTy = llvm::OpenMPIRBuilder::InsertPointTy; |
| 3158 | |
| 3159 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
| 3160 | const Stmt *CriticalRegionBodyStmt = CS->getCapturedStmt(); |
| 3161 | const Expr *Hint = nullptr; |
| 3162 | if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) |
| 3163 | Hint = HintClause->getHint(); |
| 3164 | |
| 3165 | // TODO: This is slightly different from what's currently being done in |
| 3166 | // clang. Fix the Int32Ty to IntPtrTy (pointer width size) when everything |
| 3167 | // about typing is final. |
| 3168 | llvm::Value *HintInst = nullptr; |
| 3169 | if (Hint) |
| 3170 | HintInst = |
| 3171 | Builder.CreateIntCast(EmitScalarExpr(Hint), CGM.Int32Ty, false); |
| 3172 | |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3173 | auto FiniCB = [this](InsertPointTy IP) { |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 3174 | OMPBuilderCBHelpers::FinalizeOMPRegion(*this, IP); |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3175 | }; |
| 3176 | |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3177 | auto BodyGenCB = [CriticalRegionBodyStmt, this](InsertPointTy AllocaIP, |
| 3178 | InsertPointTy CodeGenIP, |
| 3179 | llvm::BasicBlock &FiniBB) { |
Fady Ghanim | ba3f863 | 2020-02-19 13:50:26 -0600 | [diff] [blame] | 3180 | OMPBuilderCBHelpers::InlinedRegionBodyRAII IRB(*this, AllocaIP, FiniBB); |
| 3181 | OMPBuilderCBHelpers::EmitOMPRegionBody(*this, CriticalRegionBodyStmt, |
| 3182 | CodeGenIP, FiniBB); |
Fady Ghanim | 7438059a | 2020-02-15 00:42:23 -0600 | [diff] [blame] | 3183 | }; |
| 3184 | |
| 3185 | CGCapturedStmtInfo CGSI(*CS, CR_OpenMP); |
| 3186 | CodeGenFunction::CGCapturedStmtRAII CapInfoRAII(*this, &CGSI); |
| 3187 | Builder.restoreIP(OMPBuilder->CreateCritical( |
| 3188 | Builder, BodyGenCB, FiniCB, S.getDirectiveName().getAsString(), |
| 3189 | HintInst)); |
| 3190 | |
| 3191 | return; |
| 3192 | } |
| 3193 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3194 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3195 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3196 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3197 | }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3198 | const Expr *Hint = nullptr; |
| 3199 | if (const auto *HintClause = S.getSingleClause<OMPHintClause>()) |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3200 | Hint = HintClause->getHint(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3201 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 3202 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 3203 | S.getDirectiveName().getAsString(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3204 | CodeGen, S.getBeginLoc(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 3205 | } |
| 3206 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 3207 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 3208 | const OMPParallelForDirective &S) { |
| 3209 | // Emit directive as a combined directive that consists of two implicit |
| 3210 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 3211 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3212 | Action.Enter(CGF); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 3213 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3214 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 3215 | emitDispatchForLoopBounds); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 3216 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3217 | { |
| 3218 | auto LPCRegion = |
| 3219 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
| 3220 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, |
| 3221 | emitEmptyBoundParameters); |
| 3222 | } |
| 3223 | // Check for outer lastprivate conditional update. |
| 3224 | checkForLastprivateConditionalUpdate(*this, S); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3227 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 3228 | const OMPParallelForSimdDirective &S) { |
| 3229 | // Emit directive as a combined directive that consists of two implicit |
| 3230 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 3231 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3232 | Action.Enter(CGF); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3233 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 3234 | emitDispatchForLoopBounds); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 3235 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3236 | { |
| 3237 | auto LPCRegion = |
| 3238 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
| 3239 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, |
| 3240 | emitEmptyBoundParameters); |
| 3241 | } |
| 3242 | // Check for outer lastprivate conditional update. |
| 3243 | checkForLastprivateConditionalUpdate(*this, S); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 3246 | void CodeGenFunction::EmitOMPParallelMasterDirective( |
| 3247 | const OMPParallelMasterDirective &S) { |
| 3248 | // Emit directive as a combined directive that consists of two implicit |
| 3249 | // directives: 'parallel' with 'master' directive. |
| 3250 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3251 | Action.Enter(CGF); |
| 3252 | OMPPrivateScope PrivateScope(CGF); |
| 3253 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
| 3254 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 3255 | if (Copyins) { |
| 3256 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 3257 | // propagation master's thread values of threadprivate variables to local |
| 3258 | // instances of that variables of all other implicit threads. |
| 3259 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 3260 | CGF, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
| 3261 | /*ForceSimpleCall=*/true); |
| 3262 | } |
| 3263 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 3264 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 3265 | (void)PrivateScope.Privatize(); |
| 3266 | emitMaster(CGF, S); |
| 3267 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
| 3268 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3269 | { |
| 3270 | auto LPCRegion = |
| 3271 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
| 3272 | emitCommonOMPParallelDirective(*this, S, OMPD_master, CodeGen, |
| 3273 | emitEmptyBoundParameters); |
| 3274 | emitPostUpdateForReductionClause(*this, S, |
| 3275 | [](CodeGenFunction &) { return nullptr; }); |
| 3276 | } |
| 3277 | // Check for outer lastprivate conditional update. |
| 3278 | checkForLastprivateConditionalUpdate(*this, S); |
cchen | 47d6094 | 2019-12-05 13:43:48 -0500 | [diff] [blame] | 3279 | } |
| 3280 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 3281 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 3282 | const OMPParallelSectionsDirective &S) { |
| 3283 | // Emit directive as a combined directive that consists of two implicit |
| 3284 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 3285 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3286 | Action.Enter(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3287 | CGF.EmitSections(S); |
| 3288 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3289 | { |
| 3290 | auto LPCRegion = |
| 3291 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
| 3292 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, |
| 3293 | emitEmptyBoundParameters); |
| 3294 | } |
| 3295 | // Check for outer lastprivate conditional update. |
| 3296 | checkForLastprivateConditionalUpdate(*this, S); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 3297 | } |
| 3298 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3299 | void CodeGenFunction::EmitOMPTaskBasedDirective( |
| 3300 | const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, |
| 3301 | const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, |
| 3302 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3303 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3304 | const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3305 | auto I = CS->getCapturedDecl()->param_begin(); |
| 3306 | auto PartId = std::next(I); |
| 3307 | auto TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3308 | // Check if the task is final |
| 3309 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 3310 | // If the condition constant folds and can be elided, try to avoid emitting |
| 3311 | // the condition and the dead arm of the if/else. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3312 | const Expr *Cond = Clause->getCondition(); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3313 | bool CondConstant; |
| 3314 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 3315 | Data.Final.setInt(CondConstant); |
| 3316 | else |
| 3317 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 3318 | } else { |
| 3319 | // By default the task is not final. |
| 3320 | Data.Final.setInt(/*IntVal=*/false); |
| 3321 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 3322 | // Check if the task has 'priority' clause. |
| 3323 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3324 | const Expr *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 3325 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 3326 | Data.Priority.setPointer(EmitScalarConversion( |
| 3327 | EmitScalarExpr(Prio), Prio->getType(), |
| 3328 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 3329 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 3330 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3331 | // The first function argument for tasks is a thread id, the second one is a |
| 3332 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3333 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 3334 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3335 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3336 | auto IRef = C->varlist_begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3337 | for (const Expr *IInit : C->private_copies()) { |
| 3338 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3339 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3340 | Data.PrivateVars.push_back(*IRef); |
| 3341 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3342 | } |
| 3343 | ++IRef; |
| 3344 | } |
| 3345 | } |
| 3346 | EmittedAsPrivate.clear(); |
| 3347 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3348 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3349 | auto IRef = C->varlist_begin(); |
| 3350 | auto IElemInitRef = C->inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3351 | for (const Expr *IInit : C->private_copies()) { |
| 3352 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3353 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3354 | Data.FirstprivateVars.push_back(*IRef); |
| 3355 | Data.FirstprivateCopies.push_back(IInit); |
| 3356 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3357 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 3358 | ++IRef; |
| 3359 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3360 | } |
| 3361 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3362 | // Get list of lastprivate variables (for taskloops). |
| 3363 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 3364 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 3365 | auto IRef = C->varlist_begin(); |
| 3366 | auto ID = C->destination_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3367 | for (const Expr *IInit : C->private_copies()) { |
| 3368 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3369 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 3370 | Data.LastprivateVars.push_back(*IRef); |
| 3371 | Data.LastprivateCopies.push_back(IInit); |
| 3372 | } |
| 3373 | LastprivateDstsOrigs.insert( |
| 3374 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 3375 | cast<DeclRefExpr>(*IRef)}); |
| 3376 | ++IRef; |
| 3377 | ++ID; |
| 3378 | } |
| 3379 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3380 | SmallVector<const Expr *, 4> LHSs; |
| 3381 | SmallVector<const Expr *, 4> RHSs; |
| 3382 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 3383 | auto IPriv = C->privates().begin(); |
| 3384 | auto IRed = C->reduction_ops().begin(); |
| 3385 | auto ILHS = C->lhs_exprs().begin(); |
| 3386 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3387 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3388 | Data.ReductionVars.emplace_back(Ref); |
| 3389 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3390 | Data.ReductionOps.emplace_back(*IRed); |
| 3391 | LHSs.emplace_back(*ILHS); |
| 3392 | RHSs.emplace_back(*IRHS); |
| 3393 | std::advance(IPriv, 1); |
| 3394 | std::advance(IRed, 1); |
| 3395 | std::advance(ILHS, 1); |
| 3396 | std::advance(IRHS, 1); |
| 3397 | } |
| 3398 | } |
| 3399 | Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3400 | *this, S.getBeginLoc(), LHSs, RHSs, Data); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 3401 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3402 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3403 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3404 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3405 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, |
| 3406 | CapturedRegion](CodeGenFunction &CGF, |
| 3407 | PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3408 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3409 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 3410 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> FirstprivatePtrs; |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3411 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 3412 | !Data.LastprivateVars.empty()) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3413 | llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( |
| 3414 | CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3415 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3416 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 3417 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 3418 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 3419 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3420 | // Map privates. |
| 3421 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3422 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3423 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3424 | for (const Expr *E : Data.PrivateVars) { |
| 3425 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3426 | Address PrivatePtr = CGF.CreateMemTemp( |
| 3427 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3428 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3429 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3430 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3431 | for (const Expr *E : Data.FirstprivateVars) { |
| 3432 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3433 | Address PrivatePtr = |
| 3434 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3435 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3436 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 3437 | FirstprivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3438 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3439 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3440 | for (const Expr *E : Data.LastprivateVars) { |
| 3441 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3442 | Address PrivatePtr = |
| 3443 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3444 | ".lastpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3445 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3446 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3447 | } |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3448 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 3449 | CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3450 | for (const auto &Pair : LastprivateDstsOrigs) { |
| 3451 | const auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
Bruno Ricci | 5fc4db7 | 2018-12-21 14:10:18 +0000 | [diff] [blame] | 3452 | DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(OrigVD), |
| 3453 | /*RefersToEnclosingVariableOrCapture=*/ |
| 3454 | CGF.CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 3455 | Pair.second->getType(), VK_LValue, |
| 3456 | Pair.second->getExprLoc()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3457 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 3458 | return CGF.EmitLValue(&DRE).getAddress(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 3459 | }); |
| 3460 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3461 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3462 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3463 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3464 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3465 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 3466 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3467 | if (Data.Reductions) { |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 3468 | OMPPrivateScope FirstprivateScope(CGF); |
| 3469 | for (const auto &Pair : FirstprivatePtrs) { |
| 3470 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3471 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3472 | FirstprivateScope.addPrivate(Pair.first, |
| 3473 | [Replacement]() { return Replacement; }); |
| 3474 | } |
| 3475 | (void)FirstprivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3476 | OMPLexicalScope LexScope(CGF, S, CapturedRegion); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3477 | ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies, |
| 3478 | Data.ReductionOps); |
| 3479 | llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( |
| 3480 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); |
| 3481 | for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { |
| 3482 | RedCG.emitSharedLValue(CGF, Cnt); |
| 3483 | RedCG.emitAggregateType(CGF, Cnt); |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3484 | // FIXME: This must removed once the runtime library is fixed. |
| 3485 | // Emit required threadprivate variables for |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3486 | // initializer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3487 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3488 | RedCG, Cnt); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3489 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3490 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3491 | Replacement = |
| 3492 | Address(CGF.EmitScalarConversion( |
| 3493 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 3494 | CGF.getContext().getPointerType( |
| 3495 | Data.ReductionCopies[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3496 | Data.ReductionCopies[Cnt]->getExprLoc()), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3497 | Replacement.getAlignment()); |
| 3498 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 3499 | Scope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 3500 | [Replacement]() { return Replacement; }); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 3501 | } |
| 3502 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3503 | // Privatize all private variables except for in_reduction items. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3504 | (void)Scope.Privatize(); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3505 | SmallVector<const Expr *, 4> InRedVars; |
| 3506 | SmallVector<const Expr *, 4> InRedPrivs; |
| 3507 | SmallVector<const Expr *, 4> InRedOps; |
| 3508 | SmallVector<const Expr *, 4> TaskgroupDescriptors; |
| 3509 | for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { |
| 3510 | auto IPriv = C->privates().begin(); |
| 3511 | auto IRed = C->reduction_ops().begin(); |
| 3512 | auto ITD = C->taskgroup_descriptors().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3513 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3514 | InRedVars.emplace_back(Ref); |
| 3515 | InRedPrivs.emplace_back(*IPriv); |
| 3516 | InRedOps.emplace_back(*IRed); |
| 3517 | TaskgroupDescriptors.emplace_back(*ITD); |
| 3518 | std::advance(IPriv, 1); |
| 3519 | std::advance(IRed, 1); |
| 3520 | std::advance(ITD, 1); |
| 3521 | } |
| 3522 | } |
| 3523 | // Privatize in_reduction items here, because taskgroup descriptors must be |
| 3524 | // privatized earlier. |
| 3525 | OMPPrivateScope InRedScope(CGF); |
| 3526 | if (!InRedVars.empty()) { |
| 3527 | ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps); |
| 3528 | for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { |
| 3529 | RedCG.emitSharedLValue(CGF, Cnt); |
| 3530 | RedCG.emitAggregateType(CGF, Cnt); |
| 3531 | // The taskgroup descriptor variable is always implicit firstprivate and |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3532 | // privatized already during processing of the firstprivates. |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3533 | // FIXME: This must removed once the runtime library is fixed. |
| 3534 | // Emit required threadprivate variables for |
Raphael Isemann | b23ccec | 2018-12-10 12:37:46 +0000 | [diff] [blame] | 3535 | // initializer/combiner/finalizer. |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3536 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getBeginLoc(), |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 3537 | RedCG, Cnt); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3538 | llvm::Value *ReductionsPtr = |
| 3539 | CGF.EmitLoadOfScalar(CGF.EmitLValue(TaskgroupDescriptors[Cnt]), |
| 3540 | TaskgroupDescriptors[Cnt]->getExprLoc()); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3541 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3542 | CGF, S.getBeginLoc(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3543 | Replacement = Address( |
| 3544 | CGF.EmitScalarConversion( |
| 3545 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 3546 | CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3547 | InRedPrivs[Cnt]->getExprLoc()), |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3548 | Replacement.getAlignment()); |
| 3549 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 3550 | InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 3551 | [Replacement]() { return Replacement; }); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 3552 | } |
| 3553 | } |
| 3554 | (void)InRedScope.Privatize(); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 3555 | |
| 3556 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3557 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3558 | }; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3559 | llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3560 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 3561 | Data.NumberOfParts); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 3562 | OMPLexicalScope Scope(*this, S, llvm::None, |
Alexey Bataev | 6120582 | 2019-12-04 09:50:21 -0500 | [diff] [blame] | 3563 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3564 | !isOpenMPSimdDirective(S.getDirectiveKind())); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3565 | TaskGen(*this, OutlinedFn, Data); |
| 3566 | } |
| 3567 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3568 | static ImplicitParamDecl * |
| 3569 | createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3570 | QualType Ty, CapturedDecl *CD, |
| 3571 | SourceLocation Loc) { |
| 3572 | auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3573 | ImplicitParamDecl::Other); |
| 3574 | auto *OrigRef = DeclRefExpr::Create( |
| 3575 | C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, |
| 3576 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
| 3577 | auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 3578 | ImplicitParamDecl::Other); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3579 | auto *PrivateRef = DeclRefExpr::Create( |
| 3580 | C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3581 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3582 | QualType ElemType = C.getBaseElementType(Ty); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3583 | auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType, |
| 3584 | ImplicitParamDecl::Other); |
| 3585 | auto *InitRef = DeclRefExpr::Create( |
| 3586 | C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, |
| 3587 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3588 | PrivateVD->setInitStyle(VarDecl::CInit); |
| 3589 | PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, |
| 3590 | InitRef, /*BasePath=*/nullptr, |
| 3591 | VK_RValue)); |
| 3592 | Data.FirstprivateVars.emplace_back(OrigRef); |
| 3593 | Data.FirstprivateCopies.emplace_back(PrivateRef); |
| 3594 | Data.FirstprivateInits.emplace_back(InitRef); |
| 3595 | return OrigVD; |
| 3596 | } |
| 3597 | |
| 3598 | void CodeGenFunction::EmitOMPTargetTaskBasedDirective( |
| 3599 | const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, |
| 3600 | OMPTargetDataInfo &InputInfo) { |
| 3601 | // Emit outlined function for task construct. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3602 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
| 3603 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3604 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3605 | auto I = CS->getCapturedDecl()->param_begin(); |
| 3606 | auto PartId = std::next(I); |
| 3607 | auto TaskT = std::next(I, 4); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3608 | OMPTaskDataTy Data; |
| 3609 | // The task is not final. |
| 3610 | Data.Final.setInt(/*IntVal=*/false); |
| 3611 | // Get list of firstprivate variables. |
| 3612 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 3613 | auto IRef = C->varlist_begin(); |
| 3614 | auto IElemInitRef = C->inits().begin(); |
| 3615 | for (auto *IInit : C->private_copies()) { |
| 3616 | Data.FirstprivateVars.push_back(*IRef); |
| 3617 | Data.FirstprivateCopies.push_back(IInit); |
| 3618 | Data.FirstprivateInits.push_back(*IElemInitRef); |
| 3619 | ++IRef; |
| 3620 | ++IElemInitRef; |
| 3621 | } |
| 3622 | } |
| 3623 | OMPPrivateScope TargetScope(*this); |
| 3624 | VarDecl *BPVD = nullptr; |
| 3625 | VarDecl *PVD = nullptr; |
| 3626 | VarDecl *SVD = nullptr; |
| 3627 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3628 | auto *CD = CapturedDecl::Create( |
| 3629 | getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); |
| 3630 | llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); |
| 3631 | QualType BaseAndPointersType = getContext().getConstantArrayType( |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3632 | getContext().VoidPtrTy, ArrSize, nullptr, ArrayType::Normal, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3633 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3634 | BPVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3635 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3636 | PVD = createImplicitFirstprivateForType( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3637 | getContext(), Data, BaseAndPointersType, CD, S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3638 | QualType SizesType = getContext().getConstantArrayType( |
Alexey Bataev | a90fc66 | 2019-06-25 16:00:43 +0000 | [diff] [blame] | 3639 | getContext().getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1), |
Richard Smith | 772e266 | 2019-10-04 01:25:59 +0000 | [diff] [blame] | 3640 | ArrSize, nullptr, ArrayType::Normal, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3641 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3642 | SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3643 | S.getBeginLoc()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3644 | TargetScope.addPrivate( |
| 3645 | BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); |
| 3646 | TargetScope.addPrivate(PVD, |
| 3647 | [&InputInfo]() { return InputInfo.PointersArray; }); |
| 3648 | TargetScope.addPrivate(SVD, |
| 3649 | [&InputInfo]() { return InputInfo.SizesArray; }); |
| 3650 | } |
| 3651 | (void)TargetScope.Privatize(); |
| 3652 | // Build list of dependences. |
| 3653 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3654 | for (const Expr *IRef : C->varlists()) |
Alexey Bataev | 43a919f | 2018-04-13 17:48:43 +0000 | [diff] [blame] | 3655 | Data.Dependences.emplace_back(C->getDependencyKind(), IRef); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3656 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, |
| 3657 | &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3658 | // Set proper addresses for generated private copies. |
| 3659 | OMPPrivateScope Scope(CGF); |
| 3660 | if (!Data.FirstprivateVars.empty()) { |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3661 | llvm::FunctionType *CopyFnTy = llvm::FunctionType::get( |
| 3662 | CGF.Builder.getVoidTy(), {CGF.Builder.getInt8PtrTy()}, true); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3663 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3664 | llvm::Value *CopyFn = CGF.Builder.CreateLoad( |
| 3665 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(CopyFnParam))); |
| 3666 | llvm::Value *PrivatesPtr = CGF.Builder.CreateLoad(CGF.GetAddrOfLocalVar( |
| 3667 | CS->getCapturedDecl()->getParam(PrivatesParam))); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3668 | // Map privates. |
| 3669 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3670 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3671 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3672 | for (const Expr *E : Data.FirstprivateVars) { |
| 3673 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3674 | Address PrivatePtr = |
| 3675 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3676 | ".firstpriv.ptr.addr"); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3677 | PrivatePtrs.emplace_back(VD, PrivatePtr); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3678 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3679 | } |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3680 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall( |
| 3681 | CGF, S.getBeginLoc(), {CopyFnTy, CopyFn}, CallArgs); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3682 | for (const auto &Pair : PrivatePtrs) { |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3683 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3684 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3685 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3686 | } |
| 3687 | } |
| 3688 | // Privatize all private variables except for in_reduction items. |
| 3689 | (void)Scope.Privatize(); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3690 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3691 | InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3692 | CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3693 | InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3694 | CGF.GetAddrOfLocalVar(PVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3695 | InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( |
James Y Knight | 751fe28 | 2019-02-09 22:22:28 +0000 | [diff] [blame] | 3696 | CGF.GetAddrOfLocalVar(SVD), /*Index=*/0); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3697 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3698 | |
| 3699 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3700 | OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3701 | BodyGen(CGF); |
| 3702 | }; |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3703 | llvm::Function *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3704 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, |
| 3705 | Data.NumberOfParts); |
| 3706 | llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); |
| 3707 | IntegerLiteral IfCond(getContext(), TrueOrFalse, |
| 3708 | getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3709 | SourceLocation()); |
| 3710 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3711 | CGM.getOpenMPRuntime().emitTaskCall(*this, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3712 | SharedsTy, CapturedStruct, &IfCond, Data); |
| 3713 | } |
| 3714 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3715 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 3716 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3717 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3718 | Address CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3719 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3720 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 3721 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3722 | if (C->getNameModifier() == OMPD_unknown || |
| 3723 | C->getNameModifier() == OMPD_task) { |
| 3724 | IfCond = C->getCondition(); |
| 3725 | break; |
| 3726 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3727 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3728 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3729 | OMPTaskDataTy Data; |
| 3730 | // Check if we should emit tied or untied task. |
| 3731 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3732 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3733 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3734 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3735 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 3736 | IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3737 | const OMPTaskDataTy &Data) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3738 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getBeginLoc(), S, OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3739 | SharedsTy, CapturedStruct, IfCond, |
| 3740 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3741 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 3742 | auto LPCRegion = |
| 3743 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3744 | EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3745 | } |
| 3746 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3747 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 3748 | const OMPTaskyieldDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3749 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getBeginLoc()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3752 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3753 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getBeginLoc(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3754 | } |
| 3755 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3756 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3757 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getBeginLoc()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3758 | } |
| 3759 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3760 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 3761 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3762 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3763 | Action.Enter(CGF); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3764 | if (const Expr *E = S.getReductionRef()) { |
| 3765 | SmallVector<const Expr *, 4> LHSs; |
| 3766 | SmallVector<const Expr *, 4> RHSs; |
| 3767 | OMPTaskDataTy Data; |
| 3768 | for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { |
| 3769 | auto IPriv = C->privates().begin(); |
| 3770 | auto IRed = C->reduction_ops().begin(); |
| 3771 | auto ILHS = C->lhs_exprs().begin(); |
| 3772 | auto IRHS = C->rhs_exprs().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3773 | for (const Expr *Ref : C->varlists()) { |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3774 | Data.ReductionVars.emplace_back(Ref); |
| 3775 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3776 | Data.ReductionOps.emplace_back(*IRed); |
| 3777 | LHSs.emplace_back(*ILHS); |
| 3778 | RHSs.emplace_back(*IRHS); |
| 3779 | std::advance(IPriv, 1); |
| 3780 | std::advance(IRed, 1); |
| 3781 | std::advance(ILHS, 1); |
| 3782 | std::advance(IRHS, 1); |
| 3783 | } |
| 3784 | } |
| 3785 | llvm::Value *ReductionDesc = |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3786 | CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getBeginLoc(), |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3787 | LHSs, RHSs, Data); |
| 3788 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3789 | CGF.EmitVarDecl(*VD); |
| 3790 | CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), |
| 3791 | /*Volatile=*/false, E->getType()); |
| 3792 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3793 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3794 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3795 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3796 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getBeginLoc()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3799 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 3800 | llvm::AtomicOrdering AO = S.getSingleClause<OMPFlushClause>() |
| 3801 | ? llvm::AtomicOrdering::NotAtomic |
| 3802 | : llvm::AtomicOrdering::AcquireRelease; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3803 | CGM.getOpenMPRuntime().emitFlush( |
| 3804 | *this, |
| 3805 | [&S]() -> ArrayRef<const Expr *> { |
| 3806 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) |
| 3807 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 3808 | FlushClause->varlist_end()); |
| 3809 | return llvm::None; |
| 3810 | }(), |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 3811 | S.getBeginLoc(), AO); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3812 | } |
| 3813 | |
Alexey Bataev | e46f0fe | 2020-03-04 14:37:51 -0500 | [diff] [blame] | 3814 | void CodeGenFunction::EmitOMPDepobjDirective(const OMPDepobjDirective &S) { |
| 3815 | const auto *DO = S.getSingleClause<OMPDepobjClause>(); |
| 3816 | LValue DOLVal = EmitLValue(DO->getDepobj()); |
| 3817 | if (const auto *DC = S.getSingleClause<OMPDependClause>()) { |
| 3818 | SmallVector<std::pair<OpenMPDependClauseKind, const Expr *>, 4> |
| 3819 | Dependencies; |
| 3820 | for (const Expr *IRef : DC->varlists()) |
| 3821 | Dependencies.emplace_back(DC->getDependencyKind(), IRef); |
| 3822 | Address DepAddr = CGM.getOpenMPRuntime().emitDependClause( |
Alexey Bataev | 6309334 | 2020-03-09 17:18:19 -0400 | [diff] [blame] | 3823 | *this, Dependencies, /*ForDepobj=*/true, DC->getBeginLoc()).second; |
Alexey Bataev | e46f0fe | 2020-03-04 14:37:51 -0500 | [diff] [blame] | 3824 | EmitStoreOfScalar(DepAddr.getPointer(), DOLVal); |
Alexey Bataev | b27ff4d | 2020-03-04 16:15:28 -0500 | [diff] [blame] | 3825 | return; |
| 3826 | } |
| 3827 | if (const auto *DC = S.getSingleClause<OMPDestroyClause>()) { |
| 3828 | CGM.getOpenMPRuntime().emitDestroyClause(*this, DOLVal, DC->getBeginLoc()); |
| 3829 | return; |
Alexey Bataev | e46f0fe | 2020-03-04 14:37:51 -0500 | [diff] [blame] | 3830 | } |
Alexey Bataev | 8d7b118 | 2020-03-05 13:45:28 -0500 | [diff] [blame] | 3831 | if (const auto *UC = S.getSingleClause<OMPUpdateClause>()) { |
| 3832 | CGM.getOpenMPRuntime().emitUpdateClause( |
| 3833 | *this, DOLVal, UC->getDependencyKind(), UC->getBeginLoc()); |
| 3834 | return; |
| 3835 | } |
Alexey Bataev | e46f0fe | 2020-03-04 14:37:51 -0500 | [diff] [blame] | 3836 | } |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 3837 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3838 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, |
| 3839 | const CodeGenLoopTy &CodeGenLoop, |
| 3840 | Expr *IncExpr) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3841 | // Emit the loop iteration variable. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3842 | const auto *IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 3843 | const auto *IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3844 | EmitVarDecl(*IVDecl); |
| 3845 | |
| 3846 | // Emit the iterations count variable. |
| 3847 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 3848 | // iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3849 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3850 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3851 | // Emit calculation of the iterations count. |
| 3852 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3853 | } |
| 3854 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3855 | CGOpenMPRuntime &RT = CGM.getOpenMPRuntime(); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3856 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3857 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3858 | // Check pre-condition. |
| 3859 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3860 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3861 | // Skip the entire loop if we don't meet the precondition. |
| 3862 | // If the condition constant folds and can be elided, avoid emitting the |
| 3863 | // whole loop. |
| 3864 | bool CondConstant; |
| 3865 | llvm::BasicBlock *ContBlock = nullptr; |
| 3866 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3867 | if (!CondConstant) |
| 3868 | return; |
| 3869 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3870 | llvm::BasicBlock *ThenBlock = createBasicBlock("omp.precond.then"); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3871 | ContBlock = createBasicBlock("omp.precond.end"); |
| 3872 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3873 | getProfileCount(&S)); |
| 3874 | EmitBlock(ThenBlock); |
| 3875 | incrementProfileCounter(&S); |
| 3876 | } |
| 3877 | |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3878 | emitAlignedClause(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3879 | // Emit 'then' code. |
| 3880 | { |
| 3881 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3882 | |
| 3883 | LValue LB = EmitOMPHelperVar( |
| 3884 | *this, cast<DeclRefExpr>( |
| 3885 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3886 | ? S.getCombinedLowerBoundVariable() |
| 3887 | : S.getLowerBoundVariable()))); |
| 3888 | LValue UB = EmitOMPHelperVar( |
| 3889 | *this, cast<DeclRefExpr>( |
| 3890 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3891 | ? S.getCombinedUpperBoundVariable() |
| 3892 | : S.getUpperBoundVariable()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3893 | LValue ST = |
| 3894 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 3895 | LValue IL = |
| 3896 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 3897 | |
| 3898 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3899 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3900 | // Emit implicit barrier to synchronize threads and avoid data races |
| 3901 | // on initialization of firstprivate variables and post-update of |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3902 | // lastprivate variables. |
| 3903 | CGM.getOpenMPRuntime().emitBarrierCall( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3904 | *this, S.getBeginLoc(), OMPD_unknown, /*EmitChecks=*/false, |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3905 | /*ForceSimpleCall=*/true); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3906 | } |
| 3907 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3908 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 3909 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3910 | !isOpenMPTeamsDirective(S.getDirectiveKind())) |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3911 | EmitOMPReductionClauseInit(S, LoopScope); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3912 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3913 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3914 | (void)LoopScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 3915 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 3916 | CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3917 | |
| 3918 | // Detect the distribute schedule kind and chunk. |
| 3919 | llvm::Value *Chunk = nullptr; |
| 3920 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3921 | if (const auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3922 | ScheduleKind = C->getDistScheduleKind(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3923 | if (const Expr *Ch = C->getChunkSize()) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3924 | Chunk = EmitScalarExpr(Ch); |
| 3925 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3926 | S.getIterationVariable()->getType(), |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3927 | S.getBeginLoc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3928 | } |
Gheorghe-Teodor Bercea | 02650d4 | 2018-09-27 19:22:56 +0000 | [diff] [blame] | 3929 | } else { |
| 3930 | // Default behaviour for dist_schedule clause. |
| 3931 | CGM.getOpenMPRuntime().getDefaultDistScheduleAndChunk( |
| 3932 | *this, S, ScheduleKind, Chunk); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3933 | } |
| 3934 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 3935 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 3936 | |
| 3937 | // OpenMP [2.10.8, distribute Construct, Description] |
| 3938 | // If dist_schedule is specified, kind must be static. If specified, |
| 3939 | // iterations are divided into chunks of size chunk_size, chunks are |
| 3940 | // assigned to the teams of the league in a round-robin fashion in the |
| 3941 | // order of the team number. When no chunk_size is specified, the |
| 3942 | // iteration space is divided into chunks that are approximately equal |
| 3943 | // in size, and at most one chunk is distributed to each team of the |
| 3944 | // league. The size of the chunks is unspecified in this case. |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3945 | bool StaticChunked = RT.isStaticChunked( |
| 3946 | ScheduleKind, /* Chunked */ Chunk != nullptr) && |
| 3947 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3948 | if (RT.isStaticNonchunked(ScheduleKind, |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3949 | /* Chunked */ Chunk != nullptr) || |
| 3950 | StaticChunked) { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3951 | CGOpenMPRuntime::StaticRTInput StaticInit( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 3952 | IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(*this), |
| 3953 | LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this), |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3954 | StaticChunked ? Chunk : nullptr); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 3955 | RT.emitDistributeStaticInit(*this, S.getBeginLoc(), ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3956 | StaticInit); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3957 | JumpDest LoopExit = |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3958 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 3959 | // UB = min(UB, GlobalUB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3960 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3961 | ? S.getCombinedEnsureUpperBound() |
| 3962 | : S.getEnsureUpperBound()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3963 | // IV = LB; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3964 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3965 | ? S.getCombinedInit() |
| 3966 | : S.getInit()); |
| 3967 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 3968 | const Expr *Cond = |
| 3969 | isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3970 | ? S.getCombinedCond() |
| 3971 | : S.getCond(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3972 | |
Gheorghe-Teodor Bercea | e925676 | 2018-10-29 15:45:47 +0000 | [diff] [blame] | 3973 | if (StaticChunked) |
| 3974 | Cond = S.getCombinedDistCond(); |
| 3975 | |
| 3976 | // For static unchunked schedules generate: |
| 3977 | // |
| 3978 | // 1. For distribute alone, codegen |
| 3979 | // while (idx <= UB) { |
| 3980 | // BODY; |
| 3981 | // ++idx; |
| 3982 | // } |
| 3983 | // |
| 3984 | // 2. When combined with 'for' (e.g. as in 'distribute parallel for') |
| 3985 | // while (idx <= UB) { |
| 3986 | // <CodeGen rest of pragma>(LB, UB); |
| 3987 | // idx += ST; |
| 3988 | // } |
| 3989 | // |
| 3990 | // For static chunk one schedule generate: |
| 3991 | // |
| 3992 | // while (IV <= GlobalUB) { |
| 3993 | // <CodeGen rest of pragma>(LB, UB); |
| 3994 | // LB += ST; |
| 3995 | // UB += ST; |
| 3996 | // UB = min(UB, GlobalUB); |
| 3997 | // IV = LB; |
| 3998 | // } |
| 3999 | // |
Alexey Bataev | 779a180 | 2019-12-06 12:21:31 -0500 | [diff] [blame] | 4000 | emitCommonSimdLoop( |
| 4001 | *this, S, |
| 4002 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4003 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 4004 | CGF.EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
| 4005 | }, |
| 4006 | [&S, &LoopScope, Cond, IncExpr, LoopExit, &CodeGenLoop, |
| 4007 | StaticChunked](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4008 | CGF.EmitOMPInnerLoop( |
| 4009 | S, LoopScope.requiresCleanups(), Cond, IncExpr, |
| 4010 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 4011 | CodeGenLoop(CGF, S, LoopExit); |
| 4012 | }, |
| 4013 | [&S, StaticChunked](CodeGenFunction &CGF) { |
| 4014 | if (StaticChunked) { |
| 4015 | CGF.EmitIgnoredExpr(S.getCombinedNextLowerBound()); |
| 4016 | CGF.EmitIgnoredExpr(S.getCombinedNextUpperBound()); |
| 4017 | CGF.EmitIgnoredExpr(S.getCombinedEnsureUpperBound()); |
| 4018 | CGF.EmitIgnoredExpr(S.getCombinedInit()); |
| 4019 | } |
| 4020 | }); |
| 4021 | }); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 4022 | EmitBlock(LoopExit.getBlock()); |
| 4023 | // Tell the runtime we are done. |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 4024 | RT.emitForStaticFinish(*this, S.getEndLoc(), S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 4025 | } else { |
| 4026 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 4027 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4028 | const OMPLoopArguments LoopArguments = { |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4029 | LB.getAddress(*this), UB.getAddress(*this), ST.getAddress(*this), |
| 4030 | IL.getAddress(*this), Chunk}; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4031 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, |
| 4032 | CodeGenLoop); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 4033 | } |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 4034 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4035 | EmitOMPSimdFinal(S, [IL, &S](CodeGenFunction &CGF) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 4036 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4037 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 4038 | }); |
| 4039 | } |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 4040 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 4041 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 4042 | !isOpenMPTeamsDirective(S.getDirectiveKind())) { |
Jonas Hahnfeld | 5aaaece | 2018-10-02 19:12:47 +0000 | [diff] [blame] | 4043 | EmitOMPReductionClauseFinal(S, OMPD_simd); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 4044 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 4045 | emitPostUpdateForReductionClause( |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4046 | *this, S, [IL, &S](CodeGenFunction &CGF) { |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 4047 | return CGF.Builder.CreateIsNotNull( |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4048 | CGF.EmitLoadOfScalar(IL, S.getBeginLoc())); |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 4049 | }); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 4050 | } |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 4051 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 4052 | if (HasLastprivateClause) { |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 4053 | EmitOMPLastprivateClauseFinal( |
| 4054 | S, /*NoFinals=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4055 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getBeginLoc()))); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 4056 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | // We're now done with the loop, so jump to the continuation block. |
| 4060 | if (ContBlock) { |
| 4061 | EmitBranch(ContBlock); |
| 4062 | EmitBlock(ContBlock, true); |
| 4063 | } |
| 4064 | } |
| 4065 | } |
| 4066 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4067 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 4068 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4069 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4070 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 4071 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4072 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 4073 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 4074 | } |
| 4075 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4076 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 4077 | const CapturedStmt *S, |
| 4078 | SourceLocation Loc) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4079 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 4080 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 4081 | CGF.CapturedStmtInfo = &CapStmtInfo; |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 4082 | llvm::Function *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S, Loc); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame] | 4083 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4084 | return Fn; |
| 4085 | } |
| 4086 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 4087 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4088 | if (S.hasClausesOfKind<OMPDependClause>()) { |
| 4089 | assert(!S.getAssociatedStmt() && |
| 4090 | "No associated statement must be in ordered depend construct."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4091 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 4092 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 4093 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 4094 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4095 | const auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4096 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 4097 | PrePostActionTy &Action) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4098 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4099 | if (C) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4100 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4101 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | c33ba8c | 2020-01-17 14:05:40 -0500 | [diff] [blame] | 4102 | llvm::Function *OutlinedFn = |
| 4103 | emitOutlinedOrderedFunction(CGM, CS, S.getBeginLoc()); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4104 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getBeginLoc(), |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 4105 | OutlinedFn, CapturedVars); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4106 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4107 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4108 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 4109 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 4110 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4111 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4112 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getBeginLoc(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 4113 | } |
| 4114 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4115 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4116 | QualType SrcType, QualType DestType, |
| 4117 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4118 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 4119 | "DestType must have scalar evaluation kind."); |
| 4120 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4121 | return Val.isScalar() ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 4122 | DestType, Loc) |
| 4123 | : CGF.EmitComplexToScalarConversion( |
| 4124 | Val.getComplexVal(), SrcType, DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4125 | } |
| 4126 | |
| 4127 | static CodeGenFunction::ComplexPairTy |
| 4128 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4129 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4130 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 4131 | "DestType must have complex evaluation kind."); |
| 4132 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 4133 | if (Val.isScalar()) { |
| 4134 | // Convert the input element to the element type of the complex. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4135 | QualType DestElementType = |
| 4136 | DestType->castAs<ComplexType>()->getElementType(); |
| 4137 | llvm::Value *ScalarVal = CGF.EmitScalarConversion( |
| 4138 | Val.getScalarVal(), SrcType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4139 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 4140 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 4141 | } else { |
| 4142 | assert(Val.isComplex() && "Must be a scalar or complex."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4143 | QualType SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 4144 | QualType DestElementType = |
| 4145 | DestType->castAs<ComplexType>()->getElementType(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4146 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4147 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4148 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4149 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4150 | } |
| 4151 | return ComplexVal; |
| 4152 | } |
| 4153 | |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4154 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, llvm::AtomicOrdering AO, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4155 | LValue LVal, RValue RVal) { |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4156 | if (LVal.isGlobalReg()) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4157 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4158 | else |
| 4159 | CGF.EmitAtomicStore(RVal, LVal, AO, LVal.isVolatile(), /*isInit=*/false); |
| 4160 | } |
| 4161 | |
| 4162 | static RValue emitSimpleAtomicLoad(CodeGenFunction &CGF, |
| 4163 | llvm::AtomicOrdering AO, LValue LVal, |
| 4164 | SourceLocation Loc) { |
| 4165 | if (LVal.isGlobalReg()) |
| 4166 | return CGF.EmitLoadOfLValue(LVal, Loc); |
| 4167 | return CGF.EmitAtomicLoad( |
| 4168 | LVal, Loc, llvm::AtomicCmpXchgInst::getStrongestFailureOrdering(AO), |
| 4169 | LVal.isVolatile()); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 4172 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 4173 | QualType RValTy, SourceLocation Loc) { |
| 4174 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4175 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 4176 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 4177 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 4178 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4179 | break; |
| 4180 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 4181 | EmitStoreOfComplex( |
| 4182 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4183 | /*isInit=*/false); |
| 4184 | break; |
| 4185 | case TEK_Aggregate: |
| 4186 | llvm_unreachable("Must be a scalar or complex."); |
| 4187 | } |
| 4188 | } |
| 4189 | |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4190 | static void emitOMPAtomicReadExpr(CodeGenFunction &CGF, llvm::AtomicOrdering AO, |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4191 | const Expr *X, const Expr *V, |
| 4192 | SourceLocation Loc) { |
| 4193 | // v = x; |
| 4194 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 4195 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 4196 | LValue XLValue = CGF.EmitLValue(X); |
| 4197 | LValue VLValue = CGF.EmitLValue(V); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4198 | RValue Res = emitSimpleAtomicLoad(CGF, AO, XLValue, Loc); |
| 4199 | // OpenMP, 2.17.7, atomic Construct |
| 4200 | // If the read or capture clause is specified and the acquire, acq_rel, or |
| 4201 | // seq_cst clause is specified then the strong flush on exit from the atomic |
| 4202 | // operation is also an acquire flush. |
| 4203 | switch (AO) { |
| 4204 | case llvm::AtomicOrdering::Acquire: |
| 4205 | case llvm::AtomicOrdering::AcquireRelease: |
| 4206 | case llvm::AtomicOrdering::SequentiallyConsistent: |
| 4207 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, |
| 4208 | llvm::AtomicOrdering::Acquire); |
| 4209 | break; |
| 4210 | case llvm::AtomicOrdering::Monotonic: |
| 4211 | case llvm::AtomicOrdering::Release: |
| 4212 | break; |
| 4213 | case llvm::AtomicOrdering::NotAtomic: |
| 4214 | case llvm::AtomicOrdering::Unordered: |
| 4215 | llvm_unreachable("Unexpected ordering."); |
| 4216 | } |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 4217 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | f117f2c | 2020-01-28 11:19:45 -0500 | [diff] [blame] | 4218 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, V); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4219 | } |
| 4220 | |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4221 | static void emitOMPAtomicWriteExpr(CodeGenFunction &CGF, |
| 4222 | llvm::AtomicOrdering AO, const Expr *X, |
| 4223 | const Expr *E, SourceLocation Loc) { |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 4224 | // x = expr; |
| 4225 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4226 | emitSimpleAtomicStore(CGF, AO, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | f117f2c | 2020-01-28 11:19:45 -0500 | [diff] [blame] | 4227 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4228 | // OpenMP, 2.17.7, atomic Construct |
| 4229 | // If the write, update, or capture clause is specified and the release, |
| 4230 | // acq_rel, or seq_cst clause is specified then the strong flush on entry to |
| 4231 | // the atomic operation is also a release flush. |
| 4232 | switch (AO) { |
| 4233 | case llvm::AtomicOrdering::Release: |
| 4234 | case llvm::AtomicOrdering::AcquireRelease: |
| 4235 | case llvm::AtomicOrdering::SequentiallyConsistent: |
| 4236 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, |
| 4237 | llvm::AtomicOrdering::Release); |
| 4238 | break; |
| 4239 | case llvm::AtomicOrdering::Acquire: |
| 4240 | case llvm::AtomicOrdering::Monotonic: |
| 4241 | break; |
| 4242 | case llvm::AtomicOrdering::NotAtomic: |
| 4243 | case llvm::AtomicOrdering::Unordered: |
| 4244 | llvm_unreachable("Unexpected ordering."); |
| 4245 | } |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 4248 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 4249 | RValue Update, |
| 4250 | BinaryOperatorKind BO, |
| 4251 | llvm::AtomicOrdering AO, |
| 4252 | bool IsXLHSInRHSPart) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4253 | ASTContext &Context = CGF.getContext(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4254 | // 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] | 4255 | // expression is simple and atomic is allowed for the given type for the |
| 4256 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4257 | if (BO == BO_Comma || !Update.isScalar() || |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4258 | !Update.getScalarVal()->getType()->isIntegerTy() || !X.isSimple() || |
| 4259 | (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 4260 | (Update.getScalarVal()->getType() != |
| 4261 | X.getAddress(CGF).getElementType())) || |
| 4262 | !X.getAddress(CGF).getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4263 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 4264 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4265 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4266 | |
| 4267 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 4268 | switch (BO) { |
| 4269 | case BO_Add: |
| 4270 | RMWOp = llvm::AtomicRMWInst::Add; |
| 4271 | break; |
| 4272 | case BO_Sub: |
| 4273 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4274 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4275 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 4276 | break; |
| 4277 | case BO_And: |
| 4278 | RMWOp = llvm::AtomicRMWInst::And; |
| 4279 | break; |
| 4280 | case BO_Or: |
| 4281 | RMWOp = llvm::AtomicRMWInst::Or; |
| 4282 | break; |
| 4283 | case BO_Xor: |
| 4284 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 4285 | break; |
| 4286 | case BO_LT: |
| 4287 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 4288 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 4289 | : llvm::AtomicRMWInst::Max) |
| 4290 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 4291 | : llvm::AtomicRMWInst::UMax); |
| 4292 | break; |
| 4293 | case BO_GT: |
| 4294 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 4295 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 4296 | : llvm::AtomicRMWInst::Min) |
| 4297 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 4298 | : llvm::AtomicRMWInst::UMin); |
| 4299 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4300 | case BO_Assign: |
| 4301 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 4302 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4303 | case BO_Mul: |
| 4304 | case BO_Div: |
| 4305 | case BO_Rem: |
| 4306 | case BO_Shl: |
| 4307 | case BO_Shr: |
| 4308 | case BO_LAnd: |
| 4309 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4310 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4311 | case BO_PtrMemD: |
| 4312 | case BO_PtrMemI: |
| 4313 | case BO_LE: |
| 4314 | case BO_GE: |
| 4315 | case BO_EQ: |
| 4316 | case BO_NE: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 4317 | case BO_Cmp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4318 | case BO_AddAssign: |
| 4319 | case BO_SubAssign: |
| 4320 | case BO_AndAssign: |
| 4321 | case BO_OrAssign: |
| 4322 | case BO_XorAssign: |
| 4323 | case BO_MulAssign: |
| 4324 | case BO_DivAssign: |
| 4325 | case BO_RemAssign: |
| 4326 | case BO_ShlAssign: |
| 4327 | case BO_ShrAssign: |
| 4328 | case BO_Comma: |
| 4329 | llvm_unreachable("Unsupported atomic update operation"); |
| 4330 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4331 | llvm::Value *UpdateVal = Update.getScalarVal(); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4332 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 4333 | UpdateVal = CGF.Builder.CreateIntCast( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4334 | IC, X.getAddress(CGF).getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4335 | X.getType()->hasSignedIntegerRepresentation()); |
| 4336 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4337 | llvm::Value *Res = |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 4338 | CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(CGF), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4339 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4342 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4343 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 4344 | llvm::AtomicOrdering AO, SourceLocation Loc, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4345 | const llvm::function_ref<RValue(RValue)> CommonGen) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4346 | // Update expressions are allowed to have the following forms: |
| 4347 | // x binop= expr; -> xrval + expr; |
| 4348 | // x++, ++x -> xrval + 1; |
| 4349 | // x--, --x -> xrval - 1; |
| 4350 | // x = x binop expr; -> xrval binop expr |
| 4351 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4352 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 4353 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4354 | if (X.isGlobalReg()) { |
| 4355 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 4356 | // 'xrval'. |
| 4357 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 4358 | } else { |
| 4359 | // Perform compare-and-swap procedure. |
| 4360 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4361 | } |
| 4362 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4363 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4366 | static void emitOMPAtomicUpdateExpr(CodeGenFunction &CGF, |
| 4367 | llvm::AtomicOrdering AO, const Expr *X, |
| 4368 | const Expr *E, const Expr *UE, |
| 4369 | bool IsXLHSInRHSPart, SourceLocation Loc) { |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4370 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 4371 | "Update expr in 'atomic update' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4372 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4373 | // Update expressions are allowed to have the following forms: |
| 4374 | // x binop= expr; -> xrval + expr; |
| 4375 | // x++, ++x -> xrval + 1; |
| 4376 | // x--, --x -> xrval - 1; |
| 4377 | // x = x binop expr; -> xrval binop expr |
| 4378 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 4379 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4380 | LValue XLValue = CGF.EmitLValue(X); |
| 4381 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4382 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 4383 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 4384 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 4385 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 4386 | auto &&Gen = [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) { |
| 4387 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 4388 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 4389 | return CGF.EmitAnyExpr(UE); |
| 4390 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4391 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 4392 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
Alexey Bataev | f117f2c | 2020-01-28 11:19:45 -0500 | [diff] [blame] | 4393 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4394 | // OpenMP, 2.17.7, atomic Construct |
| 4395 | // If the write, update, or capture clause is specified and the release, |
| 4396 | // acq_rel, or seq_cst clause is specified then the strong flush on entry to |
| 4397 | // the atomic operation is also a release flush. |
| 4398 | switch (AO) { |
| 4399 | case llvm::AtomicOrdering::Release: |
| 4400 | case llvm::AtomicOrdering::AcquireRelease: |
| 4401 | case llvm::AtomicOrdering::SequentiallyConsistent: |
| 4402 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, |
| 4403 | llvm::AtomicOrdering::Release); |
| 4404 | break; |
| 4405 | case llvm::AtomicOrdering::Acquire: |
| 4406 | case llvm::AtomicOrdering::Monotonic: |
| 4407 | break; |
| 4408 | case llvm::AtomicOrdering::NotAtomic: |
| 4409 | case llvm::AtomicOrdering::Unordered: |
| 4410 | llvm_unreachable("Unexpected ordering."); |
| 4411 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4415 | QualType SourceType, QualType ResType, |
| 4416 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4417 | switch (CGF.getEvaluationKind(ResType)) { |
| 4418 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4419 | return RValue::get( |
| 4420 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4421 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4422 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4423 | return RValue::getComplex(Res.first, Res.second); |
| 4424 | } |
| 4425 | case TEK_Aggregate: |
| 4426 | break; |
| 4427 | } |
| 4428 | llvm_unreachable("Must be a scalar or complex."); |
| 4429 | } |
| 4430 | |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4431 | static void emitOMPAtomicCaptureExpr(CodeGenFunction &CGF, |
| 4432 | llvm::AtomicOrdering AO, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4433 | bool IsPostfixUpdate, const Expr *V, |
| 4434 | const Expr *X, const Expr *E, |
| 4435 | const Expr *UE, bool IsXLHSInRHSPart, |
| 4436 | SourceLocation Loc) { |
| 4437 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 4438 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 4439 | RValue NewVVal; |
| 4440 | LValue VLValue = CGF.EmitLValue(V); |
| 4441 | LValue XLValue = CGF.EmitLValue(X); |
| 4442 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4443 | QualType NewVValType; |
| 4444 | if (UE) { |
| 4445 | // 'x' is updated with some additional value. |
| 4446 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 4447 | "Update expr in 'atomic capture' must be a binary operator."); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4448 | const auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4449 | // Update expressions are allowed to have the following forms: |
| 4450 | // x binop= expr; -> xrval + expr; |
| 4451 | // x++, ++x -> xrval + 1; |
| 4452 | // x--, --x -> xrval - 1; |
| 4453 | // x = x binop expr; -> xrval binop expr |
| 4454 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4455 | const auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 4456 | const auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 4457 | const OpaqueValueExpr *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4458 | NewVValType = XRValExpr->getType(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4459 | const OpaqueValueExpr *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4460 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4461 | IsPostfixUpdate](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4462 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 4463 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 4464 | RValue Res = CGF.EmitAnyExpr(UE); |
| 4465 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 4466 | return Res; |
| 4467 | }; |
| 4468 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 4469 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
Alexey Bataev | f117f2c | 2020-01-28 11:19:45 -0500 | [diff] [blame] | 4470 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4471 | if (Res.first) { |
| 4472 | // 'atomicrmw' instruction was generated. |
| 4473 | if (IsPostfixUpdate) { |
| 4474 | // Use old value from 'atomicrmw'. |
| 4475 | NewVVal = Res.second; |
| 4476 | } else { |
| 4477 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 4478 | // value of 'x'. |
| 4479 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 4480 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 4481 | NewVVal = CGF.EmitAnyExpr(UE); |
| 4482 | } |
| 4483 | } |
| 4484 | } else { |
| 4485 | // 'x' is simply rewritten with some 'expr'. |
| 4486 | NewVValType = X->getType().getNonReferenceType(); |
| 4487 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 4488 | X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4489 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4490 | NewVVal = XRValue; |
| 4491 | return ExprRValue; |
| 4492 | }; |
| 4493 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 4494 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 4495 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 4496 | Loc, Gen); |
Alexey Bataev | f117f2c | 2020-01-28 11:19:45 -0500 | [diff] [blame] | 4497 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, X); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4498 | if (Res.first) { |
| 4499 | // 'atomicrmw' instruction was generated. |
| 4500 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 4501 | } |
| 4502 | } |
| 4503 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 4504 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | f117f2c | 2020-01-28 11:19:45 -0500 | [diff] [blame] | 4505 | CGF.CGM.getOpenMPRuntime().checkAndEmitLastprivateConditional(CGF, V); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4506 | // OpenMP, 2.17.7, atomic Construct |
| 4507 | // If the write, update, or capture clause is specified and the release, |
| 4508 | // acq_rel, or seq_cst clause is specified then the strong flush on entry to |
| 4509 | // the atomic operation is also a release flush. |
| 4510 | // If the read or capture clause is specified and the acquire, acq_rel, or |
| 4511 | // seq_cst clause is specified then the strong flush on exit from the atomic |
| 4512 | // operation is also an acquire flush. |
| 4513 | switch (AO) { |
| 4514 | case llvm::AtomicOrdering::Release: |
| 4515 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, |
| 4516 | llvm::AtomicOrdering::Release); |
| 4517 | break; |
| 4518 | case llvm::AtomicOrdering::Acquire: |
| 4519 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, |
| 4520 | llvm::AtomicOrdering::Acquire); |
| 4521 | break; |
| 4522 | case llvm::AtomicOrdering::AcquireRelease: |
| 4523 | case llvm::AtomicOrdering::SequentiallyConsistent: |
| 4524 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc, |
| 4525 | llvm::AtomicOrdering::AcquireRelease); |
| 4526 | break; |
| 4527 | case llvm::AtomicOrdering::Monotonic: |
| 4528 | break; |
| 4529 | case llvm::AtomicOrdering::NotAtomic: |
| 4530 | case llvm::AtomicOrdering::Unordered: |
| 4531 | llvm_unreachable("Unexpected ordering."); |
| 4532 | } |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4533 | } |
| 4534 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4535 | static void emitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4536 | llvm::AtomicOrdering AO, bool IsPostfixUpdate, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4537 | const Expr *X, const Expr *V, const Expr *E, |
| 4538 | const Expr *UE, bool IsXLHSInRHSPart, |
| 4539 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4540 | switch (Kind) { |
| 4541 | case OMPC_read: |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4542 | emitOMPAtomicReadExpr(CGF, AO, X, V, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4543 | break; |
| 4544 | case OMPC_write: |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4545 | emitOMPAtomicWriteExpr(CGF, AO, X, E, Loc); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 4546 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4547 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4548 | case OMPC_update: |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4549 | emitOMPAtomicUpdateExpr(CGF, AO, X, E, UE, IsXLHSInRHSPart, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 4550 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4551 | case OMPC_capture: |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4552 | emitOMPAtomicCaptureExpr(CGF, AO, IsPostfixUpdate, V, X, E, UE, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4553 | IsXLHSInRHSPart, Loc); |
| 4554 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4555 | case OMPC_if: |
| 4556 | case OMPC_final: |
| 4557 | case OMPC_num_threads: |
| 4558 | case OMPC_private: |
| 4559 | case OMPC_firstprivate: |
| 4560 | case OMPC_lastprivate: |
| 4561 | case OMPC_reduction: |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 4562 | case OMPC_task_reduction: |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 4563 | case OMPC_in_reduction: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4564 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 4565 | case OMPC_simdlen: |
Alexey Bataev | 9cc10fc | 2019-03-12 18:52:33 +0000 | [diff] [blame] | 4566 | case OMPC_allocator: |
Alexey Bataev | e04483e | 2019-03-27 14:14:31 +0000 | [diff] [blame] | 4567 | case OMPC_allocate: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4568 | case OMPC_collapse: |
| 4569 | case OMPC_default: |
| 4570 | case OMPC_seq_cst: |
Alexey Bataev | ea9166b | 2020-02-06 16:30:23 -0500 | [diff] [blame] | 4571 | case OMPC_acq_rel: |
Alexey Bataev | 04a830f | 2020-02-10 14:30:39 -0500 | [diff] [blame] | 4572 | case OMPC_acquire: |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 4573 | case OMPC_release: |
Alexey Bataev | 9a8defc | 2020-02-11 11:10:43 -0500 | [diff] [blame] | 4574 | case OMPC_relaxed: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4575 | case OMPC_shared: |
| 4576 | case OMPC_linear: |
| 4577 | case OMPC_aligned: |
| 4578 | case OMPC_copyin: |
| 4579 | case OMPC_copyprivate: |
| 4580 | case OMPC_flush: |
Alexey Bataev | c112e94 | 2020-02-28 09:52:15 -0500 | [diff] [blame] | 4581 | case OMPC_depobj: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4582 | case OMPC_proc_bind: |
| 4583 | case OMPC_schedule: |
| 4584 | case OMPC_ordered: |
| 4585 | case OMPC_nowait: |
| 4586 | case OMPC_untied: |
| 4587 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 4588 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4589 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 4590 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 4591 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 4592 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 4593 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 4594 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 4595 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 4596 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 4597 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 4598 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 4599 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 4600 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 4601 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 4602 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 4603 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 4604 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 4605 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 4606 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 4607 | case OMPC_is_device_ptr: |
Kelvin Li | 1408f91 | 2018-09-26 04:28:39 +0000 | [diff] [blame] | 4608 | case OMPC_unified_address: |
Alexey Bataev | 94c5064 | 2018-10-01 14:26:31 +0000 | [diff] [blame] | 4609 | case OMPC_unified_shared_memory: |
Patrick Lyster | 6bdf63b | 2018-10-03 20:07:58 +0000 | [diff] [blame] | 4610 | case OMPC_reverse_offload: |
Patrick Lyster | 3fe9e39 | 2018-10-11 14:41:10 +0000 | [diff] [blame] | 4611 | case OMPC_dynamic_allocators: |
Patrick Lyster | 7a2a27c | 2018-11-02 12:18:11 +0000 | [diff] [blame] | 4612 | case OMPC_atomic_default_mem_order: |
Alexey Bataev | 729e242 | 2019-08-23 16:11:14 +0000 | [diff] [blame] | 4613 | case OMPC_device_type: |
Alexey Bataev | dba792c | 2019-09-23 18:13:31 +0000 | [diff] [blame] | 4614 | case OMPC_match: |
Alexey Bataev | b6e7084 | 2019-12-16 15:54:17 -0500 | [diff] [blame] | 4615 | case OMPC_nontemporal: |
Alexey Bataev | cb8e691 | 2020-01-31 16:09:26 -0500 | [diff] [blame] | 4616 | case OMPC_order: |
Alexey Bataev | 375437a | 2020-03-02 14:21:20 -0500 | [diff] [blame] | 4617 | case OMPC_destroy: |
Alexey Bataev | 0f0564b | 2020-03-17 09:17:42 -0400 | [diff] [blame] | 4618 | case OMPC_detach: |
Alexey Bataev | 06dea73 | 2020-03-20 09:41:22 -0400 | [diff] [blame] | 4619 | case OMPC_inclusive: |
Alexey Bataev | 63828a3 | 2020-03-23 10:41:08 -0400 | [diff] [blame] | 4620 | case OMPC_exclusive: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4621 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 4622 | } |
| 4623 | } |
| 4624 | |
| 4625 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4626 | llvm::AtomicOrdering AO = llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4627 | bool MemOrderingSpecified = false; |
| 4628 | if (S.getSingleClause<OMPSeqCstClause>()) { |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4629 | AO = llvm::AtomicOrdering::SequentiallyConsistent; |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4630 | MemOrderingSpecified = true; |
| 4631 | } else if (S.getSingleClause<OMPAcqRelClause>()) { |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4632 | AO = llvm::AtomicOrdering::AcquireRelease; |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4633 | MemOrderingSpecified = true; |
| 4634 | } else if (S.getSingleClause<OMPAcquireClause>()) { |
Alexey Bataev | 04a830f | 2020-02-10 14:30:39 -0500 | [diff] [blame] | 4635 | AO = llvm::AtomicOrdering::Acquire; |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4636 | MemOrderingSpecified = true; |
| 4637 | } else if (S.getSingleClause<OMPReleaseClause>()) { |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 4638 | AO = llvm::AtomicOrdering::Release; |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4639 | MemOrderingSpecified = true; |
| 4640 | } else if (S.getSingleClause<OMPRelaxedClause>()) { |
Alexey Bataev | 9a8defc | 2020-02-11 11:10:43 -0500 | [diff] [blame] | 4641 | AO = llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4642 | MemOrderingSpecified = true; |
| 4643 | } |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4644 | OpenMPClauseKind Kind = OMPC_unknown; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4645 | for (const OMPClause *C : S.clauses()) { |
Alexey Bataev | 9a8defc | 2020-02-11 11:10:43 -0500 | [diff] [blame] | 4646 | // Find first clause (skip seq_cst|acq_rel|aqcuire|release|relaxed clause, |
| 4647 | // if it is first). |
Alexey Bataev | ea9166b | 2020-02-06 16:30:23 -0500 | [diff] [blame] | 4648 | if (C->getClauseKind() != OMPC_seq_cst && |
Alexey Bataev | 04a830f | 2020-02-10 14:30:39 -0500 | [diff] [blame] | 4649 | C->getClauseKind() != OMPC_acq_rel && |
Alexey Bataev | 9559834 | 2020-02-10 15:49:05 -0500 | [diff] [blame] | 4650 | C->getClauseKind() != OMPC_acquire && |
Alexey Bataev | 9a8defc | 2020-02-11 11:10:43 -0500 | [diff] [blame] | 4651 | C->getClauseKind() != OMPC_release && |
| 4652 | C->getClauseKind() != OMPC_relaxed) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 4653 | Kind = C->getClauseKind(); |
| 4654 | break; |
| 4655 | } |
| 4656 | } |
Alexey Bataev | 2d4f80f | 2020-02-11 15:15:21 -0500 | [diff] [blame] | 4657 | if (!MemOrderingSpecified) { |
| 4658 | llvm::AtomicOrdering DefaultOrder = |
| 4659 | CGM.getOpenMPRuntime().getDefaultMemoryOrdering(); |
| 4660 | if (DefaultOrder == llvm::AtomicOrdering::Monotonic || |
| 4661 | DefaultOrder == llvm::AtomicOrdering::SequentiallyConsistent || |
| 4662 | (DefaultOrder == llvm::AtomicOrdering::AcquireRelease && |
| 4663 | Kind == OMPC_capture)) { |
| 4664 | AO = DefaultOrder; |
| 4665 | } else if (DefaultOrder == llvm::AtomicOrdering::AcquireRelease) { |
| 4666 | if (Kind == OMPC_unknown || Kind == OMPC_update || Kind == OMPC_write) { |
| 4667 | AO = llvm::AtomicOrdering::Release; |
| 4668 | } else if (Kind == OMPC_read) { |
| 4669 | assert(Kind == OMPC_read && "Unexpected atomic kind."); |
| 4670 | AO = llvm::AtomicOrdering::Acquire; |
| 4671 | } |
| 4672 | } |
| 4673 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 4674 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4675 | const Stmt *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 4676 | if (const auto *FE = dyn_cast<FullExpr>(CS)) |
| 4677 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4678 | // Processing for statements under 'atomic capture'. |
| 4679 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4680 | for (const Stmt *C : Compound->body()) { |
Bill Wendling | 7c44da2 | 2018-10-31 03:48:47 +0000 | [diff] [blame] | 4681 | if (const auto *FE = dyn_cast<FullExpr>(C)) |
| 4682 | enterFullExpression(FE); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 4683 | } |
| 4684 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 4685 | |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4686 | auto &&CodeGen = [&S, Kind, AO, CS](CodeGenFunction &CGF, |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4687 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 4688 | CGF.EmitStopPoint(CS); |
Alexey Bataev | e8e05de | 2020-02-07 12:22:23 -0500 | [diff] [blame] | 4689 | emitOMPAtomicExpr(CGF, Kind, AO, S.isPostfixUpdate(), S.getX(), S.getV(), |
| 4690 | S.getExpr(), S.getUpdateExpr(), S.isXLHSInRHSPart(), |
| 4691 | S.getBeginLoc()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 4692 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4693 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4694 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4697 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 4698 | const OMPExecutableDirective &S, |
| 4699 | const RegionCodeGenTy &CodeGen) { |
| 4700 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 4701 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4702 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4703 | // On device emit this construct as inlined code. |
| 4704 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 4705 | OMPLexicalScope Scope(CGF, S, OMPD_target); |
| 4706 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4707 | CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 4ac68a2 | 2018-05-16 15:08:32 +0000 | [diff] [blame] | 4708 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 4709 | }); |
| 4710 | return; |
| 4711 | } |
| 4712 | |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 4713 | auto LPCRegion = |
| 4714 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(CGF, S); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4715 | llvm::Function *Fn = nullptr; |
| 4716 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4717 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4718 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 4719 | // Check for the at most one if clause associated with the target region. |
| 4720 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4721 | if (C->getNameModifier() == OMPD_unknown || |
| 4722 | C->getNameModifier() == OMPD_target) { |
| 4723 | IfCond = C->getCondition(); |
| 4724 | break; |
| 4725 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | // Check if we have any device clause associated with the directive. |
Alexey Bataev | f3c857f | 2020-03-18 17:52:41 -0400 | [diff] [blame] | 4729 | llvm::PointerIntPair<const Expr *, 2, OpenMPDeviceClauseModifier> Device( |
| 4730 | nullptr, OMPC_DEVICE_unknown); |
| 4731 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4732 | Device.setPointerAndInt(C->getDevice(), C->getModifier()); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 4733 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4734 | // Check if we have an if clause whose conditional always evaluates to false |
| 4735 | // or if we do not have any targets specified. If so the target region is not |
| 4736 | // an offload entry point. |
| 4737 | bool IsOffloadEntry = true; |
| 4738 | if (IfCond) { |
| 4739 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4740 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4741 | IsOffloadEntry = false; |
| 4742 | } |
| 4743 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4744 | IsOffloadEntry = false; |
| 4745 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4746 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4747 | StringRef ParentName; |
| 4748 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 4749 | // the mangling of the device outlined kernel. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4750 | if (const auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4751 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4752 | else if (const auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4753 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 4754 | else |
| 4755 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4756 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 4757 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4758 | // Emit target region as a standalone region. |
| 4759 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 4760 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 4761 | OMPLexicalScope Scope(CGF, S, OMPD_task); |
Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 4762 | auto &&SizeEmitter = |
| 4763 | [IsOffloadEntry](CodeGenFunction &CGF, |
| 4764 | const OMPLoopDirective &D) -> llvm::Value * { |
| 4765 | if (IsOffloadEntry) { |
| 4766 | OMPLoopScope(CGF, D); |
| 4767 | // Emit calculation of the iterations count. |
| 4768 | llvm::Value *NumIterations = CGF.EmitScalarExpr(D.getNumIterations()); |
| 4769 | NumIterations = CGF.Builder.CreateIntCast(NumIterations, CGF.Int64Ty, |
| 4770 | /*isSigned=*/false); |
| 4771 | return NumIterations; |
| 4772 | } |
| 4773 | return nullptr; |
Alexey Bataev | 7bb3353 | 2019-01-07 21:30:43 +0000 | [diff] [blame] | 4774 | }; |
Alexey Bataev | ec7946e | 2019-09-23 14:06:51 +0000 | [diff] [blame] | 4775 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device, |
| 4776 | SizeEmitter); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 4777 | } |
| 4778 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4779 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 4780 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4781 | Action.Enter(CGF); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4782 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4783 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4784 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4785 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4786 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4787 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4788 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4789 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
| 4792 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 4793 | StringRef ParentName, |
| 4794 | const OMPTargetDirective &S) { |
| 4795 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4796 | emitTargetRegion(CGF, S, Action); |
| 4797 | }; |
| 4798 | llvm::Function *Fn; |
| 4799 | llvm::Constant *Addr; |
| 4800 | // Emit target region as a standalone region. |
| 4801 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4802 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4803 | assert(Fn && Addr && "Target device function emission failed."); |
| 4804 | } |
| 4805 | |
| 4806 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 4807 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4808 | emitTargetRegion(CGF, S, Action); |
| 4809 | }; |
| 4810 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4811 | } |
| 4812 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4813 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 4814 | const OMPExecutableDirective &S, |
| 4815 | OpenMPDirectiveKind InnermostKind, |
| 4816 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4817 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 4818 | llvm::Function *OutlinedFn = |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4819 | CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 4820 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 4821 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4822 | const auto *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 4823 | const auto *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4824 | if (NT || TL) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4825 | const Expr *NumTeams = NT ? NT->getNumTeams() : nullptr; |
| 4826 | const Expr *ThreadLimit = TL ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4827 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4828 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4829 | S.getBeginLoc()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4830 | } |
| 4831 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4832 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4833 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4834 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 4835 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getBeginLoc(), OutlinedFn, |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4836 | CapturedVars); |
| 4837 | } |
| 4838 | |
| 4839 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 4840 | // Emit teams region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4841 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4842 | Action.Enter(CGF); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4843 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 4844 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4845 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4846 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4847 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4848 | CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4849 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4850 | }; |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4851 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4852 | emitPostUpdateForReductionClause(*this, S, |
| 4853 | [](CodeGenFunction &) { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4854 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4855 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4856 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4857 | const OMPTargetTeamsDirective &S) { |
| 4858 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 4859 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4860 | // Emit teams region as a standalone region. |
| 4861 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4862 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4863 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4864 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4865 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4866 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4867 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 4868 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 4869 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4870 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4871 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4872 | }; |
| 4873 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 4874 | emitPostUpdateForReductionClause(CGF, S, |
| 4875 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 4879 | CodeGenModule &CGM, StringRef ParentName, |
| 4880 | const OMPTargetTeamsDirective &S) { |
| 4881 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4882 | emitTargetTeamsRegion(CGF, Action, S); |
| 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::EmitOMPTargetTeamsDirective( |
| 4893 | const OMPTargetTeamsDirective &S) { |
| 4894 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4895 | emitTargetTeamsRegion(CGF, Action, S); |
| 4896 | }; |
| 4897 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4898 | } |
| 4899 | |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4900 | static void |
| 4901 | emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4902 | const OMPTargetTeamsDistributeDirective &S) { |
| 4903 | Action.Enter(CGF); |
| 4904 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4905 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4906 | }; |
| 4907 | |
| 4908 | // Emit teams region as a standalone region. |
| 4909 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4910 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4911 | Action.Enter(CGF); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4912 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4913 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4914 | (void)PrivateScope.Privatize(); |
| 4915 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4916 | CodeGenDistribute); |
| 4917 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4918 | }; |
| 4919 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); |
| 4920 | emitPostUpdateForReductionClause(CGF, S, |
| 4921 | [](CodeGenFunction &) { return nullptr; }); |
| 4922 | } |
| 4923 | |
| 4924 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| 4925 | CodeGenModule &CGM, StringRef ParentName, |
| 4926 | const OMPTargetTeamsDistributeDirective &S) { |
| 4927 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4928 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4929 | }; |
| 4930 | llvm::Function *Fn; |
| 4931 | llvm::Constant *Addr; |
| 4932 | // Emit target region as a standalone region. |
| 4933 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4934 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4935 | assert(Fn && Addr && "Target device function emission failed."); |
| 4936 | } |
| 4937 | |
| 4938 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 4939 | const OMPTargetTeamsDistributeDirective &S) { |
| 4940 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4941 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4942 | }; |
| 4943 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4944 | } |
| 4945 | |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4946 | static void emitTargetTeamsDistributeSimdRegion( |
| 4947 | CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4948 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4949 | Action.Enter(CGF); |
| 4950 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4951 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4952 | }; |
| 4953 | |
| 4954 | // Emit teams region as a standalone region. |
| 4955 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4956 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4957 | Action.Enter(CGF); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4958 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4959 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4960 | (void)PrivateScope.Privatize(); |
| 4961 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4962 | CodeGenDistribute); |
| 4963 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4964 | }; |
| 4965 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); |
| 4966 | emitPostUpdateForReductionClause(CGF, S, |
| 4967 | [](CodeGenFunction &) { return nullptr; }); |
| 4968 | } |
| 4969 | |
| 4970 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| 4971 | CodeGenModule &CGM, StringRef ParentName, |
| 4972 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4973 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4974 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4975 | }; |
| 4976 | llvm::Function *Fn; |
| 4977 | llvm::Constant *Addr; |
| 4978 | // Emit target region as a standalone region. |
| 4979 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4980 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4981 | assert(Fn && Addr && "Target device function emission failed."); |
| 4982 | } |
| 4983 | |
| 4984 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 4985 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4986 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4987 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4988 | }; |
| 4989 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4990 | } |
| 4991 | |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4992 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 4993 | const OMPTeamsDistributeDirective &S) { |
| 4994 | |
| 4995 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4996 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4997 | }; |
| 4998 | |
| 4999 | // Emit teams region as a standalone region. |
| 5000 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5001 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5002 | Action.Enter(CGF); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 5003 | OMPPrivateScope PrivateScope(CGF); |
| 5004 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5005 | (void)PrivateScope.Privatize(); |
| 5006 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 5007 | CodeGenDistribute); |
| 5008 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 5009 | }; |
Alexey Bataev | 95c6dd4 | 2017-11-29 15:14:16 +0000 | [diff] [blame] | 5010 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 5011 | emitPostUpdateForReductionClause(*this, S, |
| 5012 | [](CodeGenFunction &) { return nullptr; }); |
| 5013 | } |
| 5014 | |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 5015 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 5016 | const OMPTeamsDistributeSimdDirective &S) { |
| 5017 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5018 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 5019 | }; |
| 5020 | |
| 5021 | // Emit teams region as a standalone region. |
| 5022 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5023 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5024 | Action.Enter(CGF); |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 5025 | OMPPrivateScope PrivateScope(CGF); |
| 5026 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5027 | (void)PrivateScope.Privatize(); |
| 5028 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, |
| 5029 | CodeGenDistribute); |
| 5030 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 5031 | }; |
| 5032 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); |
| 5033 | emitPostUpdateForReductionClause(*this, S, |
| 5034 | [](CodeGenFunction &) { return nullptr; }); |
| 5035 | } |
| 5036 | |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 5037 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 5038 | const OMPTeamsDistributeParallelForDirective &S) { |
| 5039 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5040 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 5041 | S.getDistInc()); |
| 5042 | }; |
| 5043 | |
| 5044 | // Emit teams region as a standalone region. |
| 5045 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5046 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5047 | Action.Enter(CGF); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 5048 | OMPPrivateScope PrivateScope(CGF); |
| 5049 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5050 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 5051 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 5052 | CodeGenDistribute); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 5053 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 5054 | }; |
| 5055 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 5056 | emitPostUpdateForReductionClause(*this, S, |
| 5057 | [](CodeGenFunction &) { return nullptr; }); |
| 5058 | } |
| 5059 | |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 5060 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 5061 | const OMPTeamsDistributeParallelForSimdDirective &S) { |
| 5062 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5063 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 5064 | S.getDistInc()); |
| 5065 | }; |
| 5066 | |
| 5067 | // Emit teams region as a standalone region. |
| 5068 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5069 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5070 | Action.Enter(CGF); |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 5071 | OMPPrivateScope PrivateScope(CGF); |
| 5072 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5073 | (void)PrivateScope.Privatize(); |
| 5074 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5075 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 5076 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 5077 | }; |
Alexey Bataev | 0b97894 | 2019-12-11 15:26:38 -0500 | [diff] [blame] | 5078 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for_simd, |
| 5079 | CodeGen); |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 5080 | emitPostUpdateForReductionClause(*this, S, |
| 5081 | [](CodeGenFunction &) { return nullptr; }); |
| 5082 | } |
| 5083 | |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 5084 | static void emitTargetTeamsDistributeParallelForRegion( |
| 5085 | CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, |
| 5086 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 5087 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 5088 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5089 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 5090 | S.getDistInc()); |
| 5091 | }; |
| 5092 | |
| 5093 | // Emit teams region as a standalone region. |
| 5094 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5095 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5096 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 5097 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5098 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5099 | (void)PrivateScope.Privatize(); |
| 5100 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5101 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 5102 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 5103 | }; |
| 5104 | |
| 5105 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, |
| 5106 | CodeGenTeams); |
| 5107 | emitPostUpdateForReductionClause(CGF, S, |
| 5108 | [](CodeGenFunction &) { return nullptr; }); |
| 5109 | } |
| 5110 | |
| 5111 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 5112 | CodeGenModule &CGM, StringRef ParentName, |
| 5113 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 5114 | // Emit SPMD target teams distribute parallel for region as a standalone |
| 5115 | // region. |
| 5116 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5117 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 5118 | }; |
| 5119 | llvm::Function *Fn; |
| 5120 | llvm::Constant *Addr; |
| 5121 | // Emit target region as a standalone region. |
| 5122 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 5123 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 5124 | assert(Fn && Addr && "Target device function emission failed."); |
| 5125 | } |
| 5126 | |
| 5127 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 5128 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 5129 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5130 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 5131 | }; |
| 5132 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 5133 | } |
| 5134 | |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 5135 | static void emitTargetTeamsDistributeParallelForSimdRegion( |
| 5136 | CodeGenFunction &CGF, |
| 5137 | const OMPTargetTeamsDistributeParallelForSimdDirective &S, |
| 5138 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 5139 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 5140 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5141 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 5142 | S.getDistInc()); |
| 5143 | }; |
| 5144 | |
| 5145 | // Emit teams region as a standalone region. |
| 5146 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5147 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5148 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 5149 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5150 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5151 | (void)PrivateScope.Privatize(); |
| 5152 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5153 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 5154 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 5155 | }; |
| 5156 | |
| 5157 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd, |
| 5158 | CodeGenTeams); |
| 5159 | emitPostUpdateForReductionClause(CGF, S, |
| 5160 | [](CodeGenFunction &) { return nullptr; }); |
| 5161 | } |
| 5162 | |
| 5163 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 5164 | CodeGenModule &CGM, StringRef ParentName, |
| 5165 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 5166 | // Emit SPMD target teams distribute parallel for simd region as a standalone |
| 5167 | // region. |
| 5168 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5169 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 5170 | }; |
| 5171 | llvm::Function *Fn; |
| 5172 | llvm::Constant *Addr; |
| 5173 | // Emit target region as a standalone region. |
| 5174 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 5175 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 5176 | assert(Fn && Addr && "Target device function emission failed."); |
| 5177 | } |
| 5178 | |
| 5179 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 5180 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 5181 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5182 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 5183 | }; |
| 5184 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 5185 | } |
| 5186 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5187 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 5188 | const OMPCancellationPointDirective &S) { |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5189 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getBeginLoc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 5190 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5193 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 5194 | const Expr *IfCond = nullptr; |
| 5195 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 5196 | if (C->getNameModifier() == OMPD_unknown || |
| 5197 | C->getNameModifier() == OMPD_cancel) { |
| 5198 | IfCond = C->getCondition(); |
| 5199 | break; |
| 5200 | } |
| 5201 | } |
Johannes Doerfert | 10fedd9 | 2019-12-26 11:23:38 -0600 | [diff] [blame] | 5202 | if (llvm::OpenMPIRBuilder *OMPBuilder = CGM.getOpenMPIRBuilder()) { |
| 5203 | // TODO: This check is necessary as we only generate `omp parallel` through |
| 5204 | // the OpenMPIRBuilder for now. |
| 5205 | if (S.getCancelRegion() == OMPD_parallel) { |
| 5206 | llvm::Value *IfCondition = nullptr; |
| 5207 | if (IfCond) |
| 5208 | IfCondition = EmitScalarExpr(IfCond, |
| 5209 | /*IgnoreResultAssign=*/true); |
| 5210 | return Builder.restoreIP( |
| 5211 | OMPBuilder->CreateCancel(Builder, IfCondition, S.getCancelRegion())); |
| 5212 | } |
| 5213 | } |
| 5214 | |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5215 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getBeginLoc(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 5216 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 5219 | CodeGenFunction::JumpDest |
| 5220 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 5221 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
Alexey Bataev | e0ca479 | 2020-02-12 16:12:53 -0500 | [diff] [blame] | 5222 | Kind == OMPD_target_parallel || Kind == OMPD_taskloop || |
| 5223 | Kind == OMPD_master_taskloop || Kind == OMPD_parallel_master_taskloop) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 5224 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 5225 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 5226 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 5227 | Kind == OMPD_distribute_parallel_for || |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 5228 | Kind == OMPD_target_parallel_for || |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 5229 | Kind == OMPD_teams_distribute_parallel_for || |
| 5230 | Kind == OMPD_target_teams_distribute_parallel_for); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 5231 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 5232 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5233 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5234 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 5235 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 5236 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 5237 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 5238 | auto OrigVarIt = C.varlist_begin(); |
| 5239 | auto InitIt = C.inits().begin(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5240 | for (const Expr *PvtVarIt : C.private_copies()) { |
| 5241 | const auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 5242 | const auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 5243 | const auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5244 | |
| 5245 | // In order to identify the right initializer we need to match the |
| 5246 | // declaration used by the mapping logic. In some cases we may get |
| 5247 | // OMPCapturedExprDecl that refers to the original declaration. |
| 5248 | const ValueDecl *MatchingVD = OrigVD; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5249 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5250 | // OMPCapturedExprDecl are used to privative fields of the current |
| 5251 | // structure. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5252 | const auto *ME = cast<MemberExpr>(OED->getInit()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5253 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 5254 | "Base should be the current struct!"); |
| 5255 | MatchingVD = ME->getMemberDecl(); |
| 5256 | } |
| 5257 | |
| 5258 | // If we don't have information about the current list item, move on to |
| 5259 | // the next one. |
| 5260 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 5261 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 5262 | continue; |
| 5263 | |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5264 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [this, OrigVD, |
| 5265 | InitAddrIt, InitVD, |
| 5266 | PvtVD]() { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5267 | // Initialize the temporary initialization variable with the address we |
| 5268 | // get from the runtime library. We have to cast the source address |
| 5269 | // because it is always a void *. References are materialized in the |
| 5270 | // privatization scope, so the initialization here disregards the fact |
| 5271 | // the original variable is a reference. |
| 5272 | QualType AddrQTy = |
| 5273 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 5274 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 5275 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 5276 | setAddrOfLocalVar(InitVD, InitAddr); |
| 5277 | |
| 5278 | // Emit private declaration, it will be initialized by the value we |
| 5279 | // declaration we just added to the local declarations map. |
| 5280 | EmitDecl(*PvtVD); |
| 5281 | |
| 5282 | // The initialization variables reached its purpose in the emission |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5283 | // of the previous declaration, so we don't need it anymore. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5284 | LocalDeclMap.erase(InitVD); |
| 5285 | |
| 5286 | // Return the address of the private variable. |
| 5287 | return GetAddrOfLocalVar(PvtVD); |
| 5288 | }); |
| 5289 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 5290 | // Silence the warning about unused variable. |
| 5291 | (void)IsRegistered; |
| 5292 | |
| 5293 | ++OrigVarIt; |
| 5294 | ++InitIt; |
| 5295 | } |
| 5296 | } |
| 5297 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5298 | // Generate the instructions for '#pragma omp target data' directive. |
| 5299 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 5300 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5301 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 5302 | |
| 5303 | // Create a pre/post action to signal the privatization of the device pointer. |
| 5304 | // This action can be replaced by the OpenMP runtime code generation to |
| 5305 | // deactivate privatization. |
| 5306 | bool PrivatizeDevicePointers = false; |
| 5307 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 5308 | bool &PrivatizeDevicePointers; |
| 5309 | |
| 5310 | public: |
| 5311 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 5312 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 5313 | void Enter(CodeGenFunction &CGF) override { |
| 5314 | PrivatizeDevicePointers = true; |
| 5315 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 5316 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5317 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 5318 | |
| 5319 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5320 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5321 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5322 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5323 | }; |
| 5324 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 5325 | // Codegen that selects whether to generate the privatization code or not. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5326 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 5327 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 5328 | PrePostActionTy &Action) { |
| 5329 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 5330 | PrivatizeDevicePointers = false; |
| 5331 | |
| 5332 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 5333 | // needed. |
| 5334 | Action.Enter(CGF); |
| 5335 | |
| 5336 | if (PrivatizeDevicePointers) { |
| 5337 | OMPPrivateScope PrivateScope(CGF); |
| 5338 | // Emit all instances of the use_device_ptr clause. |
| 5339 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 5340 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 5341 | Info.CaptureDeviceAddrMap); |
| 5342 | (void)PrivateScope.Privatize(); |
| 5343 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5344 | } else { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5345 | RCG(CGF); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5346 | } |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5347 | }; |
| 5348 | |
| 5349 | // Forward the provided action to the privatization codegen. |
| 5350 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 5351 | PrivRCG.setAction(Action); |
| 5352 | |
| 5353 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 5354 | // we don't use an inline scope as changes in the references inside the |
| 5355 | // region are expected to be visible outside, so we do not privative them. |
| 5356 | OMPLexicalScope Scope(CGF, S); |
| 5357 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 5358 | PrivRCG); |
| 5359 | }; |
| 5360 | |
| 5361 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 5362 | |
| 5363 | // If we don't have target devices, don't bother emitting the data mapping |
| 5364 | // code. |
| 5365 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5366 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 5367 | return; |
| 5368 | } |
| 5369 | |
| 5370 | // Check if we have any if clause associated with the directive. |
| 5371 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5372 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 5373 | IfCond = C->getCondition(); |
| 5374 | |
| 5375 | // Check if we have any device clause associated with the directive. |
| 5376 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5377 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 5378 | Device = C->getDevice(); |
| 5379 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 5380 | // Set the action to signal privatization of device pointers. |
| 5381 | RCG.setAction(PrivAction); |
| 5382 | |
| 5383 | // Emit region code. |
| 5384 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 5385 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 5386 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5387 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5388 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 5389 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 5390 | // If we don't have target devices, don't bother emitting the data mapping |
| 5391 | // code. |
| 5392 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 5393 | return; |
| 5394 | |
| 5395 | // Check if we have any if clause associated with the directive. |
| 5396 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5397 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 5398 | IfCond = C->getCondition(); |
| 5399 | |
| 5400 | // Check if we have any device clause associated with the directive. |
| 5401 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5402 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 5403 | Device = C->getDevice(); |
| 5404 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5405 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5406 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 5407 | } |
| 5408 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5409 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 5410 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 5411 | // If we don't have target devices, don't bother emitting the data mapping |
| 5412 | // code. |
| 5413 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 5414 | return; |
| 5415 | |
| 5416 | // Check if we have any if clause associated with the directive. |
| 5417 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5418 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 5419 | IfCond = C->getCondition(); |
| 5420 | |
| 5421 | // Check if we have any device clause associated with the directive. |
| 5422 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5423 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 5424 | Device = C->getDevice(); |
| 5425 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5426 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5427 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 5430 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 5431 | const OMPTargetParallelDirective &S, |
| 5432 | PrePostActionTy &Action) { |
| 5433 | // Get the captured statement associated with the 'parallel' region. |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5434 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 5435 | Action.Enter(CGF); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5436 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 5437 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5438 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 5439 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 5440 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 5441 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 5442 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 6070542 | 2018-10-30 15:50:12 +0000 | [diff] [blame] | 5443 | if (isOpenMPTargetExecutionDirective(S.getDirectiveKind())) |
| 5444 | CGF.CGM.getOpenMPRuntime().adjustTargetSpecificDataForLambdas(CGF, S); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 5445 | // TODO: Add support for clauses. |
| 5446 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 5447 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 5448 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 5449 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, |
| 5450 | emitEmptyBoundParameters); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5451 | emitPostUpdateForReductionClause(CGF, S, |
| 5452 | [](CodeGenFunction &) { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 5453 | } |
| 5454 | |
| 5455 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 5456 | CodeGenModule &CGM, StringRef ParentName, |
| 5457 | const OMPTargetParallelDirective &S) { |
| 5458 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5459 | emitTargetParallelRegion(CGF, S, Action); |
| 5460 | }; |
| 5461 | llvm::Function *Fn; |
| 5462 | llvm::Constant *Addr; |
| 5463 | // Emit target region as a standalone region. |
| 5464 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 5465 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 5466 | assert(Fn && Addr && "Target device function emission failed."); |
| 5467 | } |
| 5468 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5469 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 5470 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 5471 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5472 | emitTargetParallelRegion(CGF, S, Action); |
| 5473 | }; |
| 5474 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 5477 | static void emitTargetParallelForRegion(CodeGenFunction &CGF, |
| 5478 | const OMPTargetParallelForDirective &S, |
| 5479 | PrePostActionTy &Action) { |
| 5480 | Action.Enter(CGF); |
| 5481 | // Emit directive as a combined directive that consists of two implicit |
| 5482 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5483 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5484 | Action.Enter(CGF); |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 5485 | CodeGenFunction::OMPCancelStackRAII CancelRegion( |
| 5486 | CGF, OMPD_target_parallel_for, S.hasCancel()); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 5487 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 5488 | emitDispatchForLoopBounds); |
| 5489 | }; |
| 5490 | emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, |
| 5491 | emitEmptyBoundParameters); |
| 5492 | } |
| 5493 | |
| 5494 | void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| 5495 | CodeGenModule &CGM, StringRef ParentName, |
| 5496 | const OMPTargetParallelForDirective &S) { |
| 5497 | // Emit SPMD target parallel for region as a standalone region. |
| 5498 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5499 | emitTargetParallelForRegion(CGF, S, Action); |
| 5500 | }; |
| 5501 | llvm::Function *Fn; |
| 5502 | llvm::Constant *Addr; |
| 5503 | // Emit target region as a standalone region. |
| 5504 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 5505 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 5506 | assert(Fn && Addr && "Target device function emission failed."); |
| 5507 | } |
| 5508 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5509 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 5510 | const OMPTargetParallelForDirective &S) { |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 5511 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5512 | emitTargetParallelForRegion(CGF, S, Action); |
| 5513 | }; |
| 5514 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 5517 | static void |
| 5518 | emitTargetParallelForSimdRegion(CodeGenFunction &CGF, |
| 5519 | const OMPTargetParallelForSimdDirective &S, |
| 5520 | PrePostActionTy &Action) { |
| 5521 | Action.Enter(CGF); |
| 5522 | // Emit directive as a combined directive that consists of two implicit |
| 5523 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 5524 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5525 | Action.Enter(CGF); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 5526 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 5527 | emitDispatchForLoopBounds); |
| 5528 | }; |
| 5529 | emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, |
| 5530 | emitEmptyBoundParameters); |
| 5531 | } |
| 5532 | |
| 5533 | void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| 5534 | CodeGenModule &CGM, StringRef ParentName, |
| 5535 | const OMPTargetParallelForSimdDirective &S) { |
| 5536 | // Emit SPMD target parallel for region as a standalone region. |
| 5537 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5538 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 5539 | }; |
| 5540 | llvm::Function *Fn; |
| 5541 | llvm::Constant *Addr; |
| 5542 | // Emit target region as a standalone region. |
| 5543 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 5544 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 5545 | assert(Fn && Addr && "Target device function emission failed."); |
| 5546 | } |
| 5547 | |
| 5548 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 5549 | const OMPTargetParallelForSimdDirective &S) { |
| 5550 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5551 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 5552 | }; |
| 5553 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 5554 | } |
| 5555 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5556 | /// Emit a helper variable and return corresponding lvalue. |
| 5557 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 5558 | const ImplicitParamDecl *PVD, |
| 5559 | CodeGenFunction::OMPPrivateScope &Privates) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5560 | const auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 5561 | Privates.addPrivate(VDecl, |
| 5562 | [&CGF, PVD]() { return CGF.GetAddrOfLocalVar(PVD); }); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
| 5565 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 5566 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 5567 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5568 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); |
Alexey Bataev | 172f146 | 2020-03-12 12:52:02 -0400 | [diff] [blame] | 5569 | Address CapturedStruct = Address::invalid(); |
| 5570 | { |
| 5571 | OMPLexicalScope Scope(*this, S, OMPD_taskloop, /*EmitPreInitStmt=*/false); |
| 5572 | CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 5573 | } |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5574 | QualType SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5575 | const Expr *IfCond = nullptr; |
| 5576 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 5577 | if (C->getNameModifier() == OMPD_unknown || |
| 5578 | C->getNameModifier() == OMPD_taskloop) { |
| 5579 | IfCond = C->getCondition(); |
| 5580 | break; |
| 5581 | } |
| 5582 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5583 | |
| 5584 | OMPTaskDataTy Data; |
| 5585 | // Check if taskloop must be emitted without taskgroup. |
| 5586 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5587 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5588 | Data.Tied = true; |
| 5589 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5590 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 5591 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5592 | Data.Schedule.setInt(/*IntVal=*/false); |
| 5593 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5594 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 5595 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5596 | Data.Schedule.setInt(/*IntVal=*/true); |
| 5597 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 5598 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5599 | |
| 5600 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5601 | // if (PreCond) { |
| 5602 | // for (IV in 0..LastIteration) BODY; |
| 5603 | // <Final counter/linear vars updates>; |
| 5604 | // } |
| 5605 | // |
| 5606 | |
| 5607 | // Emit: if (PreCond) - begin. |
| 5608 | // If the condition constant folds and can be elided, avoid emitting the |
| 5609 | // whole loop. |
| 5610 | bool CondConstant; |
| 5611 | llvm::BasicBlock *ContBlock = nullptr; |
| 5612 | OMPLoopScope PreInitScope(CGF, S); |
| 5613 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 5614 | if (!CondConstant) |
| 5615 | return; |
| 5616 | } else { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5617 | llvm::BasicBlock *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5618 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 5619 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 5620 | CGF.getProfileCount(&S)); |
| 5621 | CGF.EmitBlock(ThenBlock); |
| 5622 | CGF.incrementProfileCounter(&S); |
| 5623 | } |
| 5624 | |
Alexey Bataev | 6120582 | 2019-12-04 09:50:21 -0500 | [diff] [blame] | 5625 | (void)CGF.EmitOMPLinearClauseInit(S); |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 5626 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5627 | OMPPrivateScope LoopScope(CGF); |
| 5628 | // Emit helper vars inits. |
| 5629 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 5630 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 5631 | auto *LBP = std::next(I, LowerBound); |
| 5632 | auto *UBP = std::next(I, UpperBound); |
| 5633 | auto *STP = std::next(I, Stride); |
| 5634 | auto *LIP = std::next(I, LastIter); |
| 5635 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 5636 | LoopScope); |
| 5637 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 5638 | LoopScope); |
| 5639 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 5640 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 5641 | LoopScope); |
| 5642 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5643 | CGF.EmitOMPLinearClause(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5644 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5645 | (void)LoopScope.Privatize(); |
| 5646 | // Emit the loop iteration variable. |
| 5647 | const Expr *IVExpr = S.getIterationVariable(); |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5648 | const auto *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5649 | CGF.EmitVarDecl(*IVDecl); |
| 5650 | CGF.EmitIgnoredExpr(S.getInit()); |
| 5651 | |
| 5652 | // Emit the iterations count variable. |
| 5653 | // If it is not a variable, Sema decided to calculate iterations count on |
| 5654 | // each iteration (e.g., it is foldable into a constant). |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5655 | if (const auto *LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5656 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 5657 | // Emit calculation of the iterations count. |
| 5658 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 5659 | } |
| 5660 | |
Alexey Bataev | 6120582 | 2019-12-04 09:50:21 -0500 | [diff] [blame] | 5661 | { |
| 5662 | OMPLexicalScope Scope(CGF, S, OMPD_taskloop, /*EmitPreInitStmt=*/false); |
| 5663 | emitCommonSimdLoop( |
| 5664 | CGF, S, |
| 5665 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5666 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 5667 | CGF.EmitOMPSimdInit(S); |
| 5668 | }, |
| 5669 | [&S, &LoopScope](CodeGenFunction &CGF, PrePostActionTy &) { |
| 5670 | CGF.EmitOMPInnerLoop( |
| 5671 | S, LoopScope.requiresCleanups(), S.getCond(), S.getInc(), |
| 5672 | [&S](CodeGenFunction &CGF) { |
| 5673 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 5674 | CGF.EmitStopPoint(&S); |
| 5675 | }, |
| 5676 | [](CodeGenFunction &) {}); |
| 5677 | }); |
| 5678 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5679 | // Emit: if (PreCond) - end. |
| 5680 | if (ContBlock) { |
| 5681 | CGF.EmitBranch(ContBlock); |
| 5682 | CGF.EmitBlock(ContBlock, true); |
| 5683 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5684 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 5685 | if (HasLastprivateClause) { |
| 5686 | CGF.EmitOMPLastprivateClauseFinal( |
| 5687 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 5688 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 5689 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5690 | (*LIP)->getType(), S.getBeginLoc()))); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 5691 | } |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5692 | CGF.EmitOMPLinearClauseFinal(S, [LIP, &S](CodeGenFunction &CGF) { |
| 5693 | return CGF.Builder.CreateIsNotNull( |
| 5694 | CGF.EmitLoadOfScalar(CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
| 5695 | (*LIP)->getType(), S.getBeginLoc())); |
| 5696 | }); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5697 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5698 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
James Y Knight | 9871db0 | 2019-02-05 16:42:33 +0000 | [diff] [blame] | 5699 | IfCond](CodeGenFunction &CGF, llvm::Function *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5700 | const OMPTaskDataTy &Data) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5701 | auto &&CodeGen = [&S, OutlinedFn, SharedsTy, CapturedStruct, IfCond, |
| 5702 | &Data](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5703 | OMPLoopScope PreInitScope(CGF, S); |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5704 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getBeginLoc(), S, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 5705 | OutlinedFn, SharedsTy, |
| 5706 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5707 | }; |
| 5708 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 5709 | CodeGen); |
| 5710 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5711 | if (Data.Nogroup) { |
| 5712 | EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); |
| 5713 | } else { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5714 | CGM.getOpenMPRuntime().emitTaskgroupRegion( |
| 5715 | *this, |
| 5716 | [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, |
| 5717 | PrePostActionTy &Action) { |
| 5718 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5719 | CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, |
| 5720 | Data); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5721 | }, |
Stephen Kelly | f2ceec4 | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 5722 | S.getBeginLoc()); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 5723 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5724 | } |
| 5725 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5726 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5727 | auto LPCRegion = |
| 5728 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 5729 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 5730 | } |
| 5731 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5732 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 5733 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5734 | auto LPCRegion = |
| 5735 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 6120582 | 2019-12-04 09:50:21 -0500 | [diff] [blame] | 5736 | OMPLexicalScope Scope(*this, S); |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 5737 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 5738 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5739 | |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 5740 | void CodeGenFunction::EmitOMPMasterTaskLoopDirective( |
| 5741 | const OMPMasterTaskLoopDirective &S) { |
| 5742 | auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5743 | Action.Enter(CGF); |
| 5744 | EmitOMPTaskLoopBasedDirective(S); |
| 5745 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5746 | auto LPCRegion = |
| 5747 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 60e51c4 | 2019-10-10 20:13:02 +0000 | [diff] [blame] | 5748 | OMPLexicalScope Scope(*this, S, llvm::None, /*EmitPreInitStmt=*/false); |
| 5749 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); |
| 5750 | } |
| 5751 | |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 5752 | void CodeGenFunction::EmitOMPMasterTaskLoopSimdDirective( |
| 5753 | const OMPMasterTaskLoopSimdDirective &S) { |
| 5754 | auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5755 | Action.Enter(CGF); |
| 5756 | EmitOMPTaskLoopBasedDirective(S); |
| 5757 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5758 | auto LPCRegion = |
| 5759 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 6120582 | 2019-12-04 09:50:21 -0500 | [diff] [blame] | 5760 | OMPLexicalScope Scope(*this, S); |
Alexey Bataev | b8552ab | 2019-10-18 16:47:35 +0000 | [diff] [blame] | 5761 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getBeginLoc()); |
| 5762 | } |
| 5763 | |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 5764 | void CodeGenFunction::EmitOMPParallelMasterTaskLoopDirective( |
| 5765 | const OMPParallelMasterTaskLoopDirective &S) { |
| 5766 | auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5767 | auto &&TaskLoopCodeGen = [&S](CodeGenFunction &CGF, |
| 5768 | PrePostActionTy &Action) { |
| 5769 | Action.Enter(CGF); |
| 5770 | CGF.EmitOMPTaskLoopBasedDirective(S); |
| 5771 | }; |
Alexey Bataev | 18789bf | 2020-02-13 09:21:15 -0500 | [diff] [blame] | 5772 | OMPLexicalScope Scope(CGF, S, OMPD_parallel, /*EmitPreInitStmt=*/false); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 5773 | CGM.getOpenMPRuntime().emitMasterRegion(CGF, TaskLoopCodeGen, |
| 5774 | S.getBeginLoc()); |
| 5775 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5776 | auto LPCRegion = |
| 5777 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 5bbcead | 2019-10-14 17:17:41 +0000 | [diff] [blame] | 5778 | emitCommonOMPParallelDirective(*this, S, OMPD_master_taskloop, CodeGen, |
| 5779 | emitEmptyBoundParameters); |
| 5780 | } |
| 5781 | |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5782 | void CodeGenFunction::EmitOMPParallelMasterTaskLoopSimdDirective( |
| 5783 | const OMPParallelMasterTaskLoopSimdDirective &S) { |
| 5784 | auto &&CodeGen = [this, &S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 5785 | auto &&TaskLoopCodeGen = [&S](CodeGenFunction &CGF, |
| 5786 | PrePostActionTy &Action) { |
| 5787 | Action.Enter(CGF); |
| 5788 | CGF.EmitOMPTaskLoopBasedDirective(S); |
| 5789 | }; |
| 5790 | OMPLexicalScope Scope(CGF, S, OMPD_parallel, /*EmitPreInitStmt=*/false); |
| 5791 | CGM.getOpenMPRuntime().emitMasterRegion(CGF, TaskLoopCodeGen, |
| 5792 | S.getBeginLoc()); |
| 5793 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5794 | auto LPCRegion = |
| 5795 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, S); |
Alexey Bataev | 14a388f | 2019-10-25 10:27:13 -0400 | [diff] [blame] | 5796 | emitCommonOMPParallelDirective(*this, S, OMPD_master_taskloop_simd, CodeGen, |
| 5797 | emitEmptyBoundParameters); |
| 5798 | } |
| 5799 | |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5800 | // Generate the instructions for '#pragma omp target update' directive. |
| 5801 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 5802 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5803 | // If we don't have target devices, don't bother emitting the data mapping |
| 5804 | // code. |
| 5805 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 5806 | return; |
| 5807 | |
| 5808 | // Check if we have any if clause associated with the directive. |
| 5809 | const Expr *IfCond = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5810 | if (const auto *C = S.getSingleClause<OMPIfClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5811 | IfCond = C->getCondition(); |
| 5812 | |
| 5813 | // Check if we have any device clause associated with the directive. |
| 5814 | const Expr *Device = nullptr; |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5815 | if (const auto *C = S.getSingleClause<OMPDeviceClause>()) |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 5816 | Device = C->getDevice(); |
| 5817 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5818 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 5819 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 5820 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5821 | |
| 5822 | void CodeGenFunction::EmitSimpleOMPExecutableDirective( |
| 5823 | const OMPExecutableDirective &D) { |
| 5824 | if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) |
| 5825 | return; |
Akira Hatanaka | 9f37c0e | 2019-12-03 13:07:22 -0800 | [diff] [blame] | 5826 | auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 5827 | OMPPrivateScope GlobalsScope(CGF); |
| 5828 | if (isOpenMPTaskingDirective(D.getDirectiveKind())) { |
| 5829 | // Capture global firstprivates to avoid crash. |
| 5830 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
| 5831 | for (const Expr *Ref : C->varlists()) { |
| 5832 | const auto *DRE = cast<DeclRefExpr>(Ref->IgnoreParenImpCasts()); |
| 5833 | if (!DRE) |
| 5834 | continue; |
| 5835 | const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()); |
| 5836 | if (!VD || VD->hasLocalStorage()) |
| 5837 | continue; |
| 5838 | if (!CGF.LocalDeclMap.count(VD)) { |
| 5839 | LValue GlobLVal = CGF.EmitLValue(Ref); |
| 5840 | GlobalsScope.addPrivate( |
| 5841 | VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); }); |
| 5842 | } |
| 5843 | } |
| 5844 | } |
| 5845 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5846 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 5847 | (void)GlobalsScope.Privatize(); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5848 | emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); |
| 5849 | } else { |
| 5850 | if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | ddf3db9 | 2018-04-13 17:31:06 +0000 | [diff] [blame] | 5851 | for (const Expr *E : LD->counters()) { |
Simon Pilgrim | 93431f9 | 2020-01-11 16:00:17 +0000 | [diff] [blame] | 5852 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5853 | if (!VD->hasLocalStorage() && !CGF.LocalDeclMap.count(VD)) { |
| 5854 | LValue GlobLVal = CGF.EmitLValue(E); |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 5855 | GlobalsScope.addPrivate( |
Akira Hatanaka | f139ae3 | 2019-12-03 15:17:01 -0800 | [diff] [blame] | 5856 | VD, [&GlobLVal, &CGF]() { return GlobLVal.getAddress(CGF); }); |
Alexey Bataev | 6ab5bb1 | 2018-10-29 15:01:58 +0000 | [diff] [blame] | 5857 | } |
Bjorn Pettersson | 6c2d83b | 2018-10-30 08:49:26 +0000 | [diff] [blame] | 5858 | if (isa<OMPCapturedExprDecl>(VD)) { |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5859 | // Emit only those that were not explicitly referenced in clauses. |
| 5860 | if (!CGF.LocalDeclMap.count(VD)) |
| 5861 | CGF.EmitVarDecl(*VD); |
| 5862 | } |
| 5863 | } |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5864 | for (const auto *C : D.getClausesOfKind<OMPOrderedClause>()) { |
| 5865 | if (!C->getNumForLoops()) |
| 5866 | continue; |
| 5867 | for (unsigned I = LD->getCollapsedNumber(), |
| 5868 | E = C->getLoopNumIterations().size(); |
| 5869 | I < E; ++I) { |
| 5870 | if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( |
Mike Rice | 0ed4666 | 2018-09-20 17:19:41 +0000 | [diff] [blame] | 5871 | cast<DeclRefExpr>(C->getLoopCounter(I))->getDecl())) { |
Alexey Bataev | f138fda | 2018-08-13 19:04:24 +0000 | [diff] [blame] | 5872 | // Emit only those that were not explicitly referenced in clauses. |
| 5873 | if (!CGF.LocalDeclMap.count(VD)) |
| 5874 | CGF.EmitVarDecl(*VD); |
| 5875 | } |
| 5876 | } |
| 5877 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5878 | } |
Alexey Bataev | b3998a0 | 2020-03-09 08:55:57 -0400 | [diff] [blame] | 5879 | (void)GlobalsScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 5880 | CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5881 | } |
| 5882 | }; |
Alexey Bataev | 4697874 | 2020-01-30 10:46:11 -0500 | [diff] [blame] | 5883 | { |
| 5884 | auto LPCRegion = |
| 5885 | CGOpenMPRuntime::LastprivateConditionalRAII::disable(*this, D); |
| 5886 | OMPSimdLexicalScope Scope(*this, D); |
| 5887 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 5888 | *this, |
| 5889 | isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd |
| 5890 | : D.getDirectiveKind(), |
| 5891 | CodeGen); |
| 5892 | } |
| 5893 | // Check for outer lastprivate conditional update. |
| 5894 | checkForLastprivateConditionalUpdate(*this, D); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 5895 | } |