Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1 | //===--- CGStmtOpenMP.cpp - Emit LLVM Code from Statements ----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This contains code to emit OpenMP nodes as LLVM code. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 14 | #include "CGCleanup.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 15 | #include "CGOpenMPRuntime.h" |
| 16 | #include "CodeGenFunction.h" |
| 17 | #include "CodeGenModule.h" |
Chandler Carruth | 0d9593d | 2015-01-14 11:29:14 +0000 | [diff] [blame] | 18 | #include "TargetInfo.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 19 | #include "clang/AST/Stmt.h" |
| 20 | #include "clang/AST/StmtOpenMP.h" |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclOpenMP.h" |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 22 | #include "llvm/IR/CallSite.h" |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace CodeGen; |
| 25 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | /// Lexical scope for OpenMP executable constructs, that handles correct codegen |
| 28 | /// for captured expressions. |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 29 | class OMPLexicalScope : public CodeGenFunction::LexicalScope { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 30 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 31 | for (const auto *C : S.clauses()) { |
| 32 | if (auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 33 | if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
Alexey Bataev | 2bbf721 | 2016-03-03 03:52:24 +0000 | [diff] [blame] | 34 | for (const auto *I : PreInit->decls()) { |
| 35 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) |
| 36 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 37 | else { |
| 38 | CodeGenFunction::AutoVarEmission Emission = |
| 39 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 40 | CGF.EmitAutoVarCleanups(Emission); |
| 41 | } |
| 42 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 47 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 48 | |
| 49 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 50 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 51 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 52 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl)); |
| 53 | } |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 54 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 55 | public: |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 56 | OMPLexicalScope( |
| 57 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 58 | const llvm::Optional<OpenMPDirectiveKind> CapturedRegion = llvm::None, |
| 59 | const bool EmitPreInitStmt = true) |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 60 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 61 | InlinedShareds(CGF) { |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 62 | if (EmitPreInitStmt) |
| 63 | emitPreInitStmt(CGF, S); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 64 | if (!CapturedRegion.hasValue()) |
| 65 | return; |
| 66 | assert(S.hasAssociatedStmt() && |
| 67 | "Expected associated statement for inlined directive."); |
| 68 | const CapturedStmt *CS = S.getCapturedStmt(*CapturedRegion); |
| 69 | for (auto &C : CS->captures()) { |
| 70 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 71 | auto *VD = C.getCapturedVar(); |
| 72 | assert(VD == VD->getCanonicalDecl() && |
| 73 | "Canonical decl must be captured."); |
| 74 | DeclRefExpr DRE( |
| 75 | const_cast<VarDecl *>(VD), |
| 76 | isCapturedVar(CGF, VD) || (CGF.CapturedStmtInfo && |
| 77 | InlinedShareds.isGlobalVarCaptured(VD)), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 78 | VD->getType().getNonReferenceType(), VK_LValue, C.getLocation()); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 79 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 80 | return CGF.EmitLValue(&DRE).getAddress(); |
| 81 | }); |
Alexey Bataev | 4ba78a4 | 2016-04-27 07:56:03 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 84 | (void)InlinedShareds.Privatize(); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 85 | } |
| 86 | }; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 87 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 88 | /// Lexical scope for OpenMP parallel construct, that handles correct codegen |
| 89 | /// for captured expressions. |
| 90 | class OMPParallelScope final : public OMPLexicalScope { |
| 91 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 92 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 93 | return !(isOpenMPTargetExecutionDirective(Kind) || |
| 94 | isOpenMPLoopBoundSharingDirective(Kind)) && |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 95 | isOpenMPParallelDirective(Kind); |
| 96 | } |
| 97 | |
| 98 | public: |
| 99 | OMPParallelScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 100 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 101 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 104 | /// Lexical scope for OpenMP teams construct, that handles correct codegen |
| 105 | /// for captured expressions. |
| 106 | class OMPTeamsScope final : public OMPLexicalScope { |
| 107 | bool EmitPreInitStmt(const OMPExecutableDirective &S) { |
| 108 | OpenMPDirectiveKind Kind = S.getDirectiveKind(); |
| 109 | return !isOpenMPTargetExecutionDirective(Kind) && |
| 110 | isOpenMPTeamsDirective(Kind); |
| 111 | } |
| 112 | |
| 113 | public: |
| 114 | OMPTeamsScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 115 | : OMPLexicalScope(CGF, S, /*CapturedRegion=*/llvm::None, |
| 116 | EmitPreInitStmt(S)) {} |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 119 | /// Private scope for OpenMP loop-based directives, that supports capturing |
| 120 | /// of used expression from loop statement. |
| 121 | class OMPLoopScope : public CodeGenFunction::RunCleanupsScope { |
| 122 | void emitPreInitStmt(CodeGenFunction &CGF, const OMPLoopDirective &S) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 123 | CodeGenFunction::OMPMapVars PreCondVars; |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 124 | for (auto *E : S.counters()) { |
| 125 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 126 | (void)PreCondVars.setVarAddr( |
| 127 | CGF, VD, CGF.CreateMemTemp(VD->getType().getNonReferenceType())); |
Alexey Bataev | e83b3e8 | 2017-12-08 20:18:58 +0000 | [diff] [blame] | 128 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 129 | (void)PreCondVars.apply(CGF); |
George Burgess IV | 00f70bd | 2018-03-01 05:43:23 +0000 | [diff] [blame] | 130 | if (auto *PreInits = cast_or_null<DeclStmt>(S.getPreInits())) { |
| 131 | for (const auto *I : PreInits->decls()) |
| 132 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 133 | } |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 134 | PreCondVars.restore(CGF); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | public: |
| 138 | OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) |
| 139 | : CodeGenFunction::RunCleanupsScope(CGF) { |
| 140 | emitPreInitStmt(CGF, S); |
| 141 | } |
| 142 | }; |
| 143 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 144 | class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope { |
| 145 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 146 | |
| 147 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 148 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 149 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 150 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && |
| 151 | cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); |
| 152 | } |
| 153 | |
| 154 | public: |
| 155 | OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 156 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 157 | InlinedShareds(CGF) { |
| 158 | for (const auto *C : S.clauses()) { |
| 159 | if (auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 160 | if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
| 161 | for (const auto *I : PreInit->decls()) { |
| 162 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) |
| 163 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 164 | else { |
| 165 | CodeGenFunction::AutoVarEmission Emission = |
| 166 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 167 | CGF.EmitAutoVarCleanups(Emission); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) { |
| 172 | for (const Expr *E : UDP->varlists()) { |
| 173 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 174 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D)) |
| 175 | CGF.EmitVarDecl(*OED); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 180 | CGF.EmitOMPPrivateClause(S, InlinedShareds); |
| 181 | if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) { |
| 182 | if (const Expr *E = TG->getReductionRef()) |
| 183 | CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())); |
| 184 | } |
| 185 | const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt()); |
| 186 | while (CS) { |
| 187 | for (auto &C : CS->captures()) { |
| 188 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 189 | auto *VD = C.getCapturedVar(); |
| 190 | assert(VD == VD->getCanonicalDecl() && |
| 191 | "Canonical decl must be captured."); |
| 192 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 193 | isCapturedVar(CGF, VD) || |
| 194 | (CGF.CapturedStmtInfo && |
| 195 | InlinedShareds.isGlobalVarCaptured(VD)), |
| 196 | VD->getType().getNonReferenceType(), VK_LValue, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 197 | C.getLocation()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 198 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 199 | return CGF.EmitLValue(&DRE).getAddress(); |
| 200 | }); |
| 201 | } |
| 202 | } |
| 203 | CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt()); |
| 204 | } |
| 205 | (void)InlinedShareds.Privatize(); |
| 206 | } |
| 207 | }; |
| 208 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 209 | } // namespace |
| 210 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 211 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 212 | const OMPExecutableDirective &S, |
| 213 | const RegionCodeGenTy &CodeGen); |
| 214 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 215 | LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) { |
| 216 | if (auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) { |
| 217 | if (auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) { |
| 218 | OrigVD = OrigVD->getCanonicalDecl(); |
| 219 | bool IsCaptured = |
| 220 | LambdaCaptureFields.lookup(OrigVD) || |
| 221 | (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) || |
| 222 | (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)); |
| 223 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), IsCaptured, |
| 224 | OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc()); |
| 225 | return EmitLValue(&DRE); |
| 226 | } |
| 227 | } |
| 228 | return EmitLValue(E); |
| 229 | } |
| 230 | |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 231 | llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { |
| 232 | auto &C = getContext(); |
| 233 | llvm::Value *Size = nullptr; |
| 234 | auto SizeInChars = C.getTypeSizeInChars(Ty); |
| 235 | if (SizeInChars.isZero()) { |
| 236 | // getTypeSizeInChars() returns 0 for a VLA. |
| 237 | while (auto *VAT = C.getAsVariableArrayType(Ty)) { |
Sander de Smalen | 891af03a | 2018-02-03 13:55:59 +0000 | [diff] [blame] | 238 | auto VlaSize = getVLASize(VAT); |
| 239 | Ty = VlaSize.Type; |
| 240 | Size = Size ? Builder.CreateNUWMul(Size, VlaSize.NumElts) |
| 241 | : VlaSize.NumElts; |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 242 | } |
| 243 | SizeInChars = C.getTypeSizeInChars(Ty); |
| 244 | if (SizeInChars.isZero()) |
| 245 | return llvm::ConstantInt::get(SizeTy, /*V=*/0); |
| 246 | Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); |
| 247 | } else |
| 248 | Size = CGM.getSize(SizeInChars); |
| 249 | return Size; |
| 250 | } |
| 251 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 252 | void CodeGenFunction::GenerateOpenMPCapturedVars( |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 253 | const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 254 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 255 | auto CurField = RD->field_begin(); |
| 256 | auto CurCap = S.captures().begin(); |
| 257 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 258 | E = S.capture_init_end(); |
| 259 | I != E; ++I, ++CurField, ++CurCap) { |
| 260 | if (CurField->hasCapturedVLAType()) { |
| 261 | auto VAT = CurField->getCapturedVLAType(); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 262 | auto *Val = VLASizeMap[VAT->getSizeExpr()]; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 263 | CapturedVars.push_back(Val); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 264 | } else if (CurCap->capturesThis()) |
| 265 | CapturedVars.push_back(CXXThisValue); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 266 | else if (CurCap->capturesVariableByCopy()) { |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 267 | llvm::Value *CV = EmitLoadOfScalar(EmitLValue(*I), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 268 | |
| 269 | // If the field is not a pointer, we need to save the actual value |
| 270 | // and load it as a void pointer. |
| 271 | if (!CurField->getType()->isAnyPointerType()) { |
| 272 | auto &Ctx = getContext(); |
| 273 | auto DstAddr = CreateMemTemp( |
| 274 | Ctx.getUIntPtrType(), |
| 275 | Twine(CurCap->getCapturedVar()->getName()) + ".casted"); |
| 276 | LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 277 | |
| 278 | auto *SrcAddrVal = EmitScalarConversion( |
| 279 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 280 | Ctx.getPointerType(CurField->getType()), CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 281 | LValue SrcLV = |
| 282 | MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); |
| 283 | |
| 284 | // Store the value using the source type pointer. |
| 285 | EmitStoreThroughLValue(RValue::get(CV), SrcLV); |
| 286 | |
| 287 | // Load the value using the destination type pointer. |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 288 | CV = EmitLoadOfScalar(DstLV, CurCap->getLocation()); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 289 | } |
| 290 | CapturedVars.push_back(CV); |
| 291 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 292 | assert(CurCap->capturesVariable() && "Expected capture by reference."); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 293 | CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 294 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 298 | static Address castValueFromUintptr(CodeGenFunction &CGF, SourceLocation Loc, |
| 299 | QualType DstType, StringRef Name, |
| 300 | LValue AddrLV, |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 301 | bool isReferenceType = false) { |
| 302 | ASTContext &Ctx = CGF.getContext(); |
| 303 | |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 304 | auto *CastedPtr = CGF.EmitScalarConversion(AddrLV.getAddress().getPointer(), |
| 305 | Ctx.getUIntPtrType(), |
| 306 | Ctx.getPointerType(DstType), Loc); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 307 | auto TmpAddr = |
| 308 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
| 309 | .getAddress(); |
| 310 | |
| 311 | // If we are dealing with references we need to return the address of the |
| 312 | // reference instead of the reference of the value. |
| 313 | if (isReferenceType) { |
| 314 | QualType RefType = Ctx.getLValueReferenceType(DstType); |
| 315 | auto *RefVal = TmpAddr.getPointer(); |
| 316 | TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref"); |
| 317 | auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType); |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 318 | CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit*/ true); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | return TmpAddr; |
| 322 | } |
| 323 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 324 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
| 325 | if (T->isLValueReferenceType()) { |
| 326 | return C.getLValueReferenceType( |
| 327 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 328 | /*SpelledAsLValue=*/false); |
| 329 | } |
| 330 | if (T->isPointerType()) |
| 331 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 332 | if (auto *A = T->getAsArrayTypeUnsafe()) { |
| 333 | if (auto *VLA = dyn_cast<VariableArrayType>(A)) |
| 334 | return getCanonicalParamType(C, VLA->getElementType()); |
| 335 | else if (!A->isVariablyModifiedType()) |
| 336 | return C.getCanonicalType(T); |
| 337 | } |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 338 | return C.getCanonicalParamType(T); |
| 339 | } |
| 340 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 341 | namespace { |
| 342 | /// Contains required data for proper outlined function codegen. |
| 343 | struct FunctionOptions { |
| 344 | /// Captured statement for which the function is generated. |
| 345 | const CapturedStmt *S = nullptr; |
| 346 | /// true if cast to/from UIntPtr is required for variables captured by |
| 347 | /// value. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 348 | const bool UIntPtrCastRequired = true; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 349 | /// true if only casted arguments must be registered as local args or VLA |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 350 | /// sizes. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 351 | const bool RegisterCastedArgsOnly = false; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 352 | /// Name of the generated function. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 353 | const StringRef FunctionName; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 354 | explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, |
| 355 | bool RegisterCastedArgsOnly, |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 356 | StringRef FunctionName) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 357 | : S(S), UIntPtrCastRequired(UIntPtrCastRequired), |
| 358 | RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 359 | FunctionName(FunctionName) {} |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 360 | }; |
| 361 | } |
| 362 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 363 | static llvm::Function *emitOutlinedFunctionPrologue( |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 364 | CodeGenFunction &CGF, FunctionArgList &Args, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 365 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 366 | &LocalAddrs, |
| 367 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> |
| 368 | &VLASizes, |
| 369 | llvm::Value *&CXXThisValue, const FunctionOptions &FO) { |
| 370 | const CapturedDecl *CD = FO.S->getCapturedDecl(); |
| 371 | const RecordDecl *RD = FO.S->getCapturedRecordDecl(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 372 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 373 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 374 | CXXThisValue = nullptr; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 375 | // Build the argument list. |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 376 | CodeGenModule &CGM = CGF.CGM; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 377 | ASTContext &Ctx = CGM.getContext(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 378 | FunctionArgList TargetArgs; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 379 | Args.append(CD->param_begin(), |
| 380 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 381 | TargetArgs.append( |
| 382 | CD->param_begin(), |
| 383 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 384 | auto I = FO.S->captures().begin(); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 385 | FunctionDecl *DebugFunctionDecl = nullptr; |
| 386 | if (!FO.UIntPtrCastRequired) { |
| 387 | FunctionProtoType::ExtProtoInfo EPI; |
| 388 | DebugFunctionDecl = FunctionDecl::Create( |
| 389 | Ctx, Ctx.getTranslationUnitDecl(), FO.S->getLocStart(), |
| 390 | SourceLocation(), DeclarationName(), Ctx.VoidTy, |
| 391 | Ctx.getTrivialTypeSourceInfo( |
| 392 | Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI)), |
| 393 | SC_Static, /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); |
| 394 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 395 | for (auto *FD : RD->fields()) { |
| 396 | QualType ArgType = FD->getType(); |
| 397 | IdentifierInfo *II = nullptr; |
| 398 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 399 | |
| 400 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 401 | // function argument type should be uintptr and the value properly casted to |
| 402 | // uintptr. This is necessary given that the runtime library is only able to |
| 403 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 404 | // outlined function. |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 405 | if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 406 | I->capturesVariableArrayType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 407 | if (FO.UIntPtrCastRequired) |
| 408 | ArgType = Ctx.getUIntPtrType(); |
| 409 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 410 | |
| 411 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 412 | CapVar = I->getCapturedVar(); |
| 413 | II = CapVar->getIdentifier(); |
| 414 | } else if (I->capturesThis()) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 415 | II = &Ctx.Idents.get("this"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 416 | else { |
| 417 | assert(I->capturesVariableArrayType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 418 | II = &Ctx.Idents.get("vla"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 419 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 420 | if (ArgType->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 421 | ArgType = getCanonicalParamType(Ctx, ArgType); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 422 | VarDecl *Arg; |
| 423 | if (DebugFunctionDecl && (CapVar || I->capturesThis())) { |
| 424 | Arg = ParmVarDecl::Create( |
| 425 | Ctx, DebugFunctionDecl, |
| 426 | CapVar ? CapVar->getLocStart() : FD->getLocStart(), |
| 427 | CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, |
| 428 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
| 429 | } else { |
| 430 | Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), |
| 431 | II, ArgType, ImplicitParamDecl::Other); |
| 432 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 433 | Args.emplace_back(Arg); |
| 434 | // Do not cast arguments if we emit function with non-original types. |
| 435 | TargetArgs.emplace_back( |
| 436 | FO.UIntPtrCastRequired |
| 437 | ? Arg |
| 438 | : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 439 | ++I; |
| 440 | } |
| 441 | Args.append( |
| 442 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 443 | CD->param_end()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 444 | TargetArgs.append( |
| 445 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 446 | CD->param_end()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 447 | |
| 448 | // Create the function declaration. |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 449 | const CGFunctionInfo &FuncInfo = |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 450 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 451 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 452 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 453 | llvm::Function *F = |
| 454 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 455 | FO.FunctionName, &CGM.getModule()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 456 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 457 | if (CD->isNothrow()) |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 458 | F->setDoesNotThrow(); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame^] | 459 | F->setDoesNotRecurse(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 460 | |
| 461 | // Generate the function. |
Alexey Bataev | 6e01dc1 | 2017-08-14 16:03:47 +0000 | [diff] [blame] | 462 | CGF.StartFunction(CD, Ctx.VoidTy, F, FuncInfo, TargetArgs, |
| 463 | FO.S->getLocStart(), CD->getBody()->getLocStart()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 464 | unsigned Cnt = CD->getContextParamPosition(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 465 | I = FO.S->captures().begin(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 466 | for (auto *FD : RD->fields()) { |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 467 | // Do not map arguments if we emit function with non-original types. |
| 468 | Address LocalAddr(Address::invalid()); |
| 469 | if (!FO.UIntPtrCastRequired && Args[Cnt] != TargetArgs[Cnt]) { |
| 470 | LocalAddr = CGM.getOpenMPRuntime().getParameterAddress(CGF, Args[Cnt], |
| 471 | TargetArgs[Cnt]); |
| 472 | } else { |
| 473 | LocalAddr = CGF.GetAddrOfLocalVar(Args[Cnt]); |
| 474 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 475 | // If we are capturing a pointer by copy we don't need to do anything, just |
| 476 | // use the value that we get from the arguments. |
| 477 | if (I->capturesVariableByCopy() && FD->getType()->isAnyPointerType()) { |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 478 | const VarDecl *CurVD = I->getCapturedVar(); |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 479 | // If the variable is a reference we need to materialize it here. |
| 480 | if (CurVD->getType()->isReferenceType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 481 | Address RefAddr = CGF.CreateMemTemp( |
| 482 | CurVD->getType(), CGM.getPointerAlign(), ".materialized_ref"); |
| 483 | CGF.EmitStoreOfScalar(LocalAddr.getPointer(), RefAddr, |
| 484 | /*Volatile=*/false, CurVD->getType()); |
Samuel Antao | 403ffd4 | 2016-07-27 22:49:49 +0000 | [diff] [blame] | 485 | LocalAddr = RefAddr; |
| 486 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 487 | if (!FO.RegisterCastedArgsOnly) |
| 488 | LocalAddrs.insert({Args[Cnt], {CurVD, LocalAddr}}); |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 489 | ++Cnt; |
| 490 | ++I; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 491 | continue; |
| 492 | } |
| 493 | |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 494 | LValue ArgLVal = CGF.MakeAddrLValue(LocalAddr, Args[Cnt]->getType(), |
| 495 | AlignmentSource::Decl); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 496 | if (FD->hasCapturedVLAType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 497 | if (FO.UIntPtrCastRequired) { |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 498 | ArgLVal = CGF.MakeAddrLValue( |
| 499 | castValueFromUintptr(CGF, I->getLocation(), FD->getType(), |
| 500 | Args[Cnt]->getName(), ArgLVal), |
| 501 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 502 | } |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 503 | auto *ExprArg = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 504 | auto VAT = FD->getCapturedVLAType(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 505 | VLASizes.insert({Args[Cnt], {VAT->getSizeExpr(), ExprArg}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 506 | } else if (I->capturesVariable()) { |
| 507 | auto *Var = I->getCapturedVar(); |
| 508 | QualType VarTy = Var->getType(); |
| 509 | Address ArgAddr = ArgLVal.getAddress(); |
| 510 | if (!VarTy->isReferenceType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 511 | if (ArgLVal.getType()->isLValueReferenceType()) { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 512 | ArgAddr = CGF.EmitLoadOfReference(ArgLVal); |
Alexey Bataev | ac5eabb | 2016-11-07 11:16:04 +0000 | [diff] [blame] | 513 | } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 514 | assert(ArgLVal.getType()->isPointerType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 515 | ArgAddr = CGF.EmitLoadOfPointer( |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 516 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
| 517 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 518 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 519 | if (!FO.RegisterCastedArgsOnly) { |
| 520 | LocalAddrs.insert( |
| 521 | {Args[Cnt], |
| 522 | {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); |
| 523 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 524 | } else if (I->capturesVariableByCopy()) { |
| 525 | assert(!FD->getType()->isAnyPointerType() && |
| 526 | "Not expecting a captured pointer."); |
| 527 | auto *Var = I->getCapturedVar(); |
| 528 | QualType VarTy = Var->getType(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 529 | LocalAddrs.insert( |
| 530 | {Args[Cnt], |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 531 | {Var, FO.UIntPtrCastRequired |
| 532 | ? castValueFromUintptr(CGF, I->getLocation(), |
| 533 | FD->getType(), Args[Cnt]->getName(), |
| 534 | ArgLVal, VarTy->isReferenceType()) |
| 535 | : ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 536 | } else { |
| 537 | // If 'this' is captured, load it into CXXThisValue. |
| 538 | assert(I->capturesThis()); |
Alexey Bataev | 1e49137 | 2018-01-23 18:44:14 +0000 | [diff] [blame] | 539 | CXXThisValue = CGF.EmitLoadOfScalar(ArgLVal, I->getLocation()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 540 | LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 541 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 542 | ++Cnt; |
| 543 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 546 | return F; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | llvm::Function * |
| 550 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { |
| 551 | assert( |
| 552 | CapturedStmtInfo && |
| 553 | "CapturedStmtInfo should be set when generating the captured function"); |
| 554 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 555 | // Build the argument list. |
| 556 | bool NeedWrapperFunction = |
| 557 | getDebugInfo() && |
| 558 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo; |
| 559 | FunctionArgList Args; |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 560 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 561 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 562 | SmallString<256> Buffer; |
| 563 | llvm::raw_svector_ostream Out(Buffer); |
| 564 | Out << CapturedStmtInfo->getHelperName(); |
| 565 | if (NeedWrapperFunction) |
| 566 | Out << "_debug__"; |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 567 | FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 568 | Out.str()); |
| 569 | llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, |
| 570 | VLASizes, CXXThisValue, FO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 571 | for (const auto &LocalAddrPair : LocalAddrs) { |
| 572 | if (LocalAddrPair.second.first) { |
| 573 | setAddrOfLocalVar(LocalAddrPair.second.first, |
| 574 | LocalAddrPair.second.second); |
| 575 | } |
| 576 | } |
| 577 | for (const auto &VLASizePair : VLASizes) |
| 578 | VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 579 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 580 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 581 | FinishFunction(CD->getBodyRBrace()); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 582 | if (!NeedWrapperFunction) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 583 | return F; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 584 | |
Alexey Bataev | efd884d | 2017-08-04 21:26:25 +0000 | [diff] [blame] | 585 | FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 586 | /*RegisterCastedArgsOnly=*/true, |
| 587 | CapturedStmtInfo->getHelperName()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 588 | CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); |
Gheorghe-Teodor Bercea | d3dcf2f | 2018-03-14 14:17:45 +0000 | [diff] [blame] | 589 | WrapperCGF.CapturedStmtInfo = CapturedStmtInfo; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 590 | Args.clear(); |
| 591 | LocalAddrs.clear(); |
| 592 | VLASizes.clear(); |
| 593 | llvm::Function *WrapperF = |
| 594 | emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 595 | WrapperCGF.CXXThisValue, WrapperFO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 596 | llvm::SmallVector<llvm::Value *, 4> CallArgs; |
| 597 | for (const auto *Arg : Args) { |
| 598 | llvm::Value *CallArg; |
| 599 | auto I = LocalAddrs.find(Arg); |
| 600 | if (I != LocalAddrs.end()) { |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 601 | LValue LV = WrapperCGF.MakeAddrLValue( |
| 602 | I->second.second, |
| 603 | I->second.first ? I->second.first->getType() : Arg->getType(), |
| 604 | AlignmentSource::Decl); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 605 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getLocStart()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 606 | } else { |
| 607 | auto EI = VLASizes.find(Arg); |
| 608 | if (EI != VLASizes.end()) |
| 609 | CallArg = EI->second.second; |
| 610 | else { |
| 611 | LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 612 | Arg->getType(), |
| 613 | AlignmentSource::Decl); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 614 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, S.getLocStart()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 615 | } |
| 616 | } |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 617 | CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 618 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 619 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getLocStart(), |
| 620 | F, CallArgs); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 621 | WrapperCGF.FinishFunction(); |
| 622 | return WrapperF; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 625 | //===----------------------------------------------------------------------===// |
| 626 | // OpenMP Directive Emission |
| 627 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 628 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 629 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
| 630 | const llvm::function_ref<void(Address, Address)> &CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 631 | // Perform element-by-element initialization. |
| 632 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 633 | |
| 634 | // Drill down to the base element type on both arrays. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 635 | auto ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 636 | auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
| 637 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 638 | |
| 639 | auto SrcBegin = SrcAddr.getPointer(); |
| 640 | auto DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 641 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 642 | auto DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
| 643 | // The basic structure here is a while-do loop. |
| 644 | auto BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 645 | auto DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 646 | auto IsEmpty = |
| 647 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 648 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 649 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 650 | // Enter the loop body, making that address the current address. |
| 651 | auto EntryBB = Builder.GetInsertBlock(); |
| 652 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 653 | |
| 654 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 655 | |
| 656 | llvm::PHINode *SrcElementPHI = |
| 657 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 658 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 659 | Address SrcElementCurrent = |
| 660 | Address(SrcElementPHI, |
| 661 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 662 | |
| 663 | llvm::PHINode *DestElementPHI = |
| 664 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 665 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 666 | Address DestElementCurrent = |
| 667 | Address(DestElementPHI, |
| 668 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 669 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 670 | // Emit copy. |
| 671 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 672 | |
| 673 | // Shift the address forward by one element. |
| 674 | auto DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 675 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 676 | auto SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 677 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 678 | // Check whether we've reached the end. |
| 679 | auto Done = |
| 680 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 681 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 682 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 683 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 684 | |
| 685 | // Done. |
| 686 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 687 | } |
| 688 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 689 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 690 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 691 | const VarDecl *SrcVD, const Expr *Copy) { |
| 692 | if (OriginalType->isArrayType()) { |
| 693 | auto *BO = dyn_cast<BinaryOperator>(Copy); |
| 694 | if (BO && BO->getOpcode() == BO_Assign) { |
| 695 | // Perform simple memcpy for simple copying. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 696 | LValue Dest = MakeAddrLValue(DestAddr, OriginalType); |
| 697 | LValue Src = MakeAddrLValue(SrcAddr, OriginalType); |
| 698 | EmitAggregateAssign(Dest, Src, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 699 | } else { |
| 700 | // For arrays with complex element types perform element by element |
| 701 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 702 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 703 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 704 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 705 | // Working with the single array element, so have to remap |
| 706 | // destination and source variables to corresponding array |
| 707 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 708 | CodeGenFunction::OMPPrivateScope Remap(*this); |
| 709 | Remap.addPrivate(DestVD, [DestElement]() -> Address { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 710 | return DestElement; |
| 711 | }); |
| 712 | Remap.addPrivate( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 713 | SrcVD, [SrcElement]() -> Address { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 714 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 715 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 716 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 717 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 718 | } else { |
| 719 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 720 | CodeGenFunction::OMPPrivateScope Remap(*this); |
| 721 | Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; }); |
| 722 | Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 723 | (void)Remap.Privatize(); |
| 724 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 725 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 726 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 729 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 730 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 731 | if (!HaveInsertPoint()) |
| 732 | return false; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 733 | bool FirstprivateIsLastprivate = false; |
| 734 | llvm::DenseSet<const VarDecl *> Lastprivates; |
| 735 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 736 | for (const auto *D : C->varlists()) |
| 737 | Lastprivates.insert( |
| 738 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 739 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 740 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 741 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 742 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 743 | // Force emission of the firstprivate copy if the directive does not emit |
| 744 | // outlined function, like omp for, omp simd, omp distribute etc. |
| 745 | bool MustEmitFirstprivateCopy = |
| 746 | CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 747 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 748 | auto IRef = C->varlist_begin(); |
| 749 | auto InitsRef = C->inits().begin(); |
| 750 | for (auto IInit : C->private_copies()) { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 751 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 752 | bool ThisFirstprivateIsLastprivate = |
| 753 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
| 754 | auto *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 755 | if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 756 | !FD->getType()->isReferenceType()) { |
| 757 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 758 | ++IRef; |
| 759 | ++InitsRef; |
| 760 | continue; |
| 761 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 762 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 763 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 764 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 765 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 766 | auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
| 767 | bool IsRegistered; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 768 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 769 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 770 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 771 | LValue OriginalLVal = EmitLValue(&DRE); |
| 772 | Address OriginalAddr = OriginalLVal.getAddress(); |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 773 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 774 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 775 | // Emit VarDecl with copy init for arrays. |
| 776 | // Get the address of the original variable captured in current |
| 777 | // captured region. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 778 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 779 | auto Emission = EmitAutoVarAlloca(*VD); |
| 780 | auto *Init = VD->getInit(); |
| 781 | if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) { |
| 782 | // Perform simple memcpy. |
Ivan A. Kosarev | 1860b52 | 2018-01-25 14:21:55 +0000 | [diff] [blame] | 783 | LValue Dest = MakeAddrLValue(Emission.getAllocatedAddress(), |
| 784 | Type); |
| 785 | EmitAggregateAssign(Dest, OriginalLVal, Type); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 786 | } else { |
| 787 | EmitOMPAggregateAssign( |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 788 | Emission.getAllocatedAddress(), OriginalAddr, Type, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 789 | [this, VDInit, Init](Address DestElement, |
| 790 | Address SrcElement) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 791 | // Clean up any temporaries needed by the initialization. |
| 792 | RunCleanupsScope InitScope(*this); |
| 793 | // Emit initialization for single element. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 794 | setAddrOfLocalVar(VDInit, SrcElement); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 795 | EmitAnyExprToMem(Init, DestElement, |
| 796 | Init->getType().getQualifiers(), |
| 797 | /*IsInitializer*/ false); |
| 798 | LocalDeclMap.erase(VDInit); |
| 799 | }); |
| 800 | } |
| 801 | EmitAutoVarCleanups(Emission); |
| 802 | return Emission.getAllocatedAddress(); |
| 803 | }); |
| 804 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 805 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 806 | // Emit private VarDecl with copy init. |
| 807 | // Remap temp VDInit variable to the address of the original |
| 808 | // variable |
| 809 | // (for proper handling of captured global variables). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 810 | setAddrOfLocalVar(VDInit, OriginalAddr); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 811 | EmitDecl(*VD); |
| 812 | LocalDeclMap.erase(VDInit); |
| 813 | return GetAddrOfLocalVar(VD); |
| 814 | }); |
| 815 | } |
| 816 | assert(IsRegistered && |
| 817 | "firstprivate var already registered as private"); |
| 818 | // Silence the warning about unused variable. |
| 819 | (void)IsRegistered; |
| 820 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 821 | ++IRef; |
| 822 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 823 | } |
| 824 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 825 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 828 | void CodeGenFunction::EmitOMPPrivateClause( |
| 829 | const OMPExecutableDirective &D, |
| 830 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 831 | if (!HaveInsertPoint()) |
| 832 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 833 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 834 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 835 | auto IRef = C->varlist_begin(); |
| 836 | for (auto IInit : C->private_copies()) { |
| 837 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 838 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 839 | auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 840 | bool IsRegistered = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 841 | PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 842 | // Emit private VarDecl with copy init. |
| 843 | EmitDecl(*VD); |
| 844 | return GetAddrOfLocalVar(VD); |
| 845 | }); |
| 846 | assert(IsRegistered && "private var already registered as private"); |
| 847 | // Silence the warning about unused variable. |
| 848 | (void)IsRegistered; |
| 849 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 850 | ++IRef; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 855 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 856 | if (!HaveInsertPoint()) |
| 857 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 858 | // threadprivate_var1 = master_threadprivate_var1; |
| 859 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 860 | // ... |
| 861 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 862 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 863 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 864 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 865 | auto IRef = C->varlist_begin(); |
| 866 | auto ISrcRef = C->source_exprs().begin(); |
| 867 | auto IDestRef = C->destination_exprs().begin(); |
| 868 | for (auto *AssignOp : C->assignment_ops()) { |
| 869 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 870 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 871 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 872 | // Get the address of the master variable. If we are emitting code with |
| 873 | // TLS support, the address is passed from the master as field in the |
| 874 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 875 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 876 | if (getLangOpts().OpenMPUseTLS && |
| 877 | getContext().getTargetInfo().isTLSSupported()) { |
| 878 | assert(CapturedStmtInfo->lookup(VD) && |
| 879 | "Copyin threadprivates should have been captured!"); |
| 880 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(), |
| 881 | VK_LValue, (*IRef)->getExprLoc()); |
| 882 | MasterAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 883 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 884 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 885 | MasterAddr = |
| 886 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 887 | : CGM.GetAddrOfGlobal(VD), |
| 888 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 889 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 890 | // Get the address of the threadprivate variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 891 | Address PrivateAddr = EmitLValue(*IRef).getAddress(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 892 | if (CopiedVars.size() == 1) { |
| 893 | // At first check if current thread is a master thread. If it is, no |
| 894 | // need to copy data. |
| 895 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 896 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 897 | Builder.CreateCondBr( |
| 898 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 899 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
| 900 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 901 | CopyBegin, CopyEnd); |
| 902 | EmitBlock(CopyBegin); |
| 903 | } |
| 904 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 905 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 906 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 907 | } |
| 908 | ++IRef; |
| 909 | ++ISrcRef; |
| 910 | ++IDestRef; |
| 911 | } |
| 912 | } |
| 913 | if (CopyEnd) { |
| 914 | // Exit out of copying procedure for non-master thread. |
| 915 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 916 | return true; |
| 917 | } |
| 918 | return false; |
| 919 | } |
| 920 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 921 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 922 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 923 | if (!HaveInsertPoint()) |
| 924 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 925 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 926 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 927 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 928 | auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 929 | for (auto *C : LoopDirective->counters()) { |
| 930 | SIMDLCVs.insert( |
| 931 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 932 | } |
| 933 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 934 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 935 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 936 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 937 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 938 | !getLangOpts().OpenMPSimd) |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 939 | break; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 940 | auto IRef = C->varlist_begin(); |
| 941 | auto IDestRef = C->destination_exprs().begin(); |
| 942 | for (auto *IInit : C->private_copies()) { |
| 943 | // Keep the address of the original variable for future update at the end |
| 944 | // of the loop. |
| 945 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 946 | // Taskloops do not require additional initialization, it is done in |
| 947 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 948 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
| 949 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 950 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 951 | DeclRefExpr DRE( |
| 952 | const_cast<VarDecl *>(OrigVD), |
| 953 | /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( |
| 954 | OrigVD) != nullptr, |
| 955 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
| 956 | return EmitLValue(&DRE).getAddress(); |
| 957 | }); |
| 958 | // Check if the variable is also a firstprivate: in this case IInit is |
| 959 | // not generated. Initialization of this variable will happen in codegen |
| 960 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 961 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 962 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 963 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 964 | // Emit private VarDecl with copy init. |
| 965 | EmitDecl(*VD); |
| 966 | return GetAddrOfLocalVar(VD); |
| 967 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 968 | assert(IsRegistered && |
| 969 | "lastprivate var already registered as private"); |
| 970 | (void)IsRegistered; |
| 971 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 972 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 973 | ++IRef; |
| 974 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 975 | } |
| 976 | } |
| 977 | return HasAtLeastOneLastprivate; |
| 978 | } |
| 979 | |
| 980 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 981 | const OMPExecutableDirective &D, bool NoFinals, |
| 982 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 983 | if (!HaveInsertPoint()) |
| 984 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 985 | // Emit following code: |
| 986 | // if (<IsLastIterCond>) { |
| 987 | // orig_var1 = private_orig_var1; |
| 988 | // ... |
| 989 | // orig_varn = private_orig_varn; |
| 990 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 991 | llvm::BasicBlock *ThenBB = nullptr; |
| 992 | llvm::BasicBlock *DoneBB = nullptr; |
| 993 | if (IsLastIterCond) { |
| 994 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 995 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 996 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 997 | EmitBlock(ThenBB); |
| 998 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 999 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 1000 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1001 | if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1002 | auto IC = LoopDirective->counters().begin(); |
| 1003 | for (auto F : LoopDirective->finals()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1004 | auto *D = |
| 1005 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 1006 | if (NoFinals) |
| 1007 | AlreadyEmittedVars.insert(D); |
| 1008 | else |
| 1009 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1010 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1011 | } |
| 1012 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1013 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 1014 | auto IRef = C->varlist_begin(); |
| 1015 | auto ISrcRef = C->source_exprs().begin(); |
| 1016 | auto IDestRef = C->destination_exprs().begin(); |
| 1017 | for (auto *AssignOp : C->assignment_ops()) { |
| 1018 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 1019 | QualType Type = PrivateVD->getType(); |
| 1020 | auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
| 1021 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 1022 | // If lastprivate variable is a loop control variable for loop-based |
| 1023 | // directive, update its value before copyin back to original |
| 1024 | // variable. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1025 | if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
| 1026 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1027 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 1028 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 1029 | // Get the address of the original variable. |
| 1030 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 1031 | // Get the address of the private variable. |
| 1032 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
| 1033 | if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
| 1034 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1035 | Address(Builder.CreateLoad(PrivateAddr), |
| 1036 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1037 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1038 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1039 | ++IRef; |
| 1040 | ++ISrcRef; |
| 1041 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1042 | } |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1043 | if (auto *PostUpdate = C->getPostUpdateExpr()) |
| 1044 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1045 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1046 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1047 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1050 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 1051 | const OMPExecutableDirective &D, |
| 1052 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1053 | if (!HaveInsertPoint()) |
| 1054 | return; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1055 | SmallVector<const Expr *, 4> Shareds; |
| 1056 | SmallVector<const Expr *, 4> Privates; |
| 1057 | SmallVector<const Expr *, 4> ReductionOps; |
| 1058 | SmallVector<const Expr *, 4> LHSs; |
| 1059 | SmallVector<const Expr *, 4> RHSs; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1060 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1061 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1062 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1063 | auto ILHS = C->lhs_exprs().begin(); |
| 1064 | auto IRHS = C->rhs_exprs().begin(); |
| 1065 | for (const auto *Ref : C->varlists()) { |
| 1066 | Shareds.emplace_back(Ref); |
| 1067 | Privates.emplace_back(*IPriv); |
| 1068 | ReductionOps.emplace_back(*IRed); |
| 1069 | LHSs.emplace_back(*ILHS); |
| 1070 | RHSs.emplace_back(*IRHS); |
| 1071 | std::advance(IPriv, 1); |
| 1072 | std::advance(IRed, 1); |
| 1073 | std::advance(ILHS, 1); |
| 1074 | std::advance(IRHS, 1); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1077 | ReductionCodeGen RedCG(Shareds, Privates, ReductionOps); |
| 1078 | unsigned Count = 0; |
| 1079 | auto ILHS = LHSs.begin(); |
| 1080 | auto IRHS = RHSs.begin(); |
| 1081 | auto IPriv = Privates.begin(); |
| 1082 | for (const auto *IRef : Shareds) { |
| 1083 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
| 1084 | // Emit private VarDecl with reduction init. |
| 1085 | RedCG.emitSharedLValue(*this, Count); |
| 1086 | RedCG.emitAggregateType(*this, Count); |
| 1087 | auto Emission = EmitAutoVarAlloca(*PrivateVD); |
| 1088 | RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), |
| 1089 | RedCG.getSharedLValue(Count), |
| 1090 | [&Emission](CodeGenFunction &CGF) { |
| 1091 | CGF.EmitAutoVarInit(Emission); |
| 1092 | return true; |
| 1093 | }); |
| 1094 | EmitAutoVarCleanups(Emission); |
| 1095 | Address BaseAddr = RedCG.adjustPrivateAddress( |
| 1096 | *this, Count, Emission.getAllocatedAddress()); |
| 1097 | bool IsRegistered = PrivateScope.addPrivate( |
| 1098 | RedCG.getBaseDecl(Count), [BaseAddr]() -> Address { return BaseAddr; }); |
| 1099 | assert(IsRegistered && "private var already registered as private"); |
| 1100 | // Silence the warning about unused variable. |
| 1101 | (void)IsRegistered; |
| 1102 | |
| 1103 | auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 1104 | auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1105 | QualType Type = PrivateVD->getType(); |
| 1106 | bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); |
| 1107 | if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1108 | // Store the address of the original variable associated with the LHS |
| 1109 | // implicit variable. |
| 1110 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address { |
| 1111 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1112 | }); |
| 1113 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { |
| 1114 | return GetAddrOfLocalVar(PrivateVD); |
| 1115 | }); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1116 | } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || |
| 1117 | isa<ArraySubscriptExpr>(IRef)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1118 | // Store the address of the original variable associated with the LHS |
| 1119 | // implicit variable. |
| 1120 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address { |
| 1121 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1122 | }); |
| 1123 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { |
| 1124 | return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), |
| 1125 | ConvertTypeForMem(RHSVD->getType()), |
| 1126 | "rhs.begin"); |
| 1127 | }); |
| 1128 | } else { |
| 1129 | QualType Type = PrivateVD->getType(); |
| 1130 | bool IsArray = getContext().getAsArrayType(Type) != nullptr; |
| 1131 | Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(); |
| 1132 | // Store the address of the original variable associated with the LHS |
| 1133 | // implicit variable. |
| 1134 | if (IsArray) { |
| 1135 | OriginalAddr = Builder.CreateElementBitCast( |
| 1136 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1137 | } |
| 1138 | PrivateScope.addPrivate( |
| 1139 | LHSVD, [OriginalAddr]() -> Address { return OriginalAddr; }); |
| 1140 | PrivateScope.addPrivate( |
| 1141 | RHSVD, [this, PrivateVD, RHSVD, IsArray]() -> Address { |
| 1142 | return IsArray |
| 1143 | ? Builder.CreateElementBitCast( |
| 1144 | GetAddrOfLocalVar(PrivateVD), |
| 1145 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin") |
| 1146 | : GetAddrOfLocalVar(PrivateVD); |
| 1147 | }); |
| 1148 | } |
| 1149 | ++ILHS; |
| 1150 | ++IRHS; |
| 1151 | ++IPriv; |
| 1152 | ++Count; |
| 1153 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1157 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1158 | if (!HaveInsertPoint()) |
| 1159 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1160 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1161 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1162 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1163 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1164 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1165 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1166 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1167 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1168 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1169 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1170 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1171 | } |
| 1172 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1173 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1174 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1175 | ReductionKind == OMPD_simd; |
| 1176 | bool SimpleReduction = ReductionKind == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1177 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1178 | // parallel directive (it always has implicit barrier). |
| 1179 | CGM.getOpenMPRuntime().emitReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1180 | *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1181 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1182 | } |
| 1183 | } |
| 1184 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1185 | static void emitPostUpdateForReductionClause( |
| 1186 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 1187 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
| 1188 | if (!CGF.HaveInsertPoint()) |
| 1189 | return; |
| 1190 | llvm::BasicBlock *DoneBB = nullptr; |
| 1191 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
| 1192 | if (auto *PostUpdate = C->getPostUpdateExpr()) { |
| 1193 | if (!DoneBB) { |
| 1194 | if (auto *Cond = CondGen(CGF)) { |
| 1195 | // If the first post-update expression is found, emit conditional |
| 1196 | // block if it was requested. |
| 1197 | auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
| 1198 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1199 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1200 | CGF.EmitBlock(ThenBB); |
| 1201 | } |
| 1202 | } |
| 1203 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1204 | } |
| 1205 | } |
| 1206 | if (DoneBB) |
| 1207 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1208 | } |
| 1209 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1210 | namespace { |
| 1211 | /// Codegen lambda for appending distribute lower and upper bounds to outlined |
| 1212 | /// parallel function. This is necessary for combined constructs such as |
| 1213 | /// 'distribute parallel for' |
| 1214 | typedef llvm::function_ref<void(CodeGenFunction &, |
| 1215 | const OMPExecutableDirective &, |
| 1216 | llvm::SmallVectorImpl<llvm::Value *> &)> |
| 1217 | CodeGenBoundParametersTy; |
| 1218 | } // anonymous namespace |
| 1219 | |
| 1220 | static void emitCommonOMPParallelDirective( |
| 1221 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 1222 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1223 | const CodeGenBoundParametersTy &CodeGenBoundParameters) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1224 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
| 1225 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1226 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1227 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1228 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1229 | auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1230 | /*IgnoreResultAssign*/ true); |
| 1231 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
| 1232 | CGF, NumThreads, NumThreadsClause->getLocStart()); |
| 1233 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1234 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1235 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1236 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
| 1237 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); |
| 1238 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1239 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1240 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1241 | if (C->getNameModifier() == OMPD_unknown || |
| 1242 | C->getNameModifier() == OMPD_parallel) { |
| 1243 | IfCond = C->getCondition(); |
| 1244 | break; |
| 1245 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1246 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1247 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1248 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1249 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1250 | // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk |
| 1251 | // lower and upper bounds with the pragma 'for' chunking mechanism. |
| 1252 | // The following lambda takes care of appending the lower and upper bound |
| 1253 | // parameters when necessary |
| 1254 | CodeGenBoundParameters(CGF, S, CapturedVars); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1255 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1256 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1257 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1260 | static void emitEmptyBoundParameters(CodeGenFunction &, |
| 1261 | const OMPExecutableDirective &, |
| 1262 | llvm::SmallVectorImpl<llvm::Value *> &) {} |
| 1263 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1264 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1265 | // Emit parallel region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 1266 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 1267 | Action.Enter(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1268 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1269 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1270 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1271 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1272 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1273 | // propagation master's thread values of threadprivate variables to local |
| 1274 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1275 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 1276 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 1277 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1278 | } |
| 1279 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1280 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1281 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1282 | CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1283 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1284 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1285 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, |
| 1286 | emitEmptyBoundParameters); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1287 | emitPostUpdateForReductionClause( |
| 1288 | *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1289 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1290 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1291 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1292 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1293 | RunCleanupsScope BodyScope(*this); |
| 1294 | // Update counters values on current iteration. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1295 | for (auto I : D.updates()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1296 | EmitIgnoredExpr(I); |
| 1297 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1298 | // Update the linear variables. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1299 | // In distribute directives only loop counters may be marked as linear, no |
| 1300 | // need to generate the code for them. |
| 1301 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1302 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
| 1303 | for (auto *U : C->updates()) |
| 1304 | EmitIgnoredExpr(U); |
| 1305 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1308 | // On a continue in the body, jump to the end. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1309 | auto Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1310 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1311 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1312 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1313 | // The end (updates/cleanups). |
| 1314 | EmitBlock(Continue.getBlock()); |
| 1315 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1318 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1319 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1320 | const Expr *IncExpr, |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1321 | const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, |
| 1322 | const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1323 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1324 | |
| 1325 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1326 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1327 | EmitBlock(CondBlock); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1328 | const SourceRange &R = S.getSourceRange(); |
| 1329 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1330 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1331 | |
| 1332 | // If there are any cleanups between here and the loop-exit scope, |
| 1333 | // create a block to stage a loop exit along. |
| 1334 | auto ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1335 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1336 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1337 | |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1338 | auto LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1339 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1340 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1341 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1342 | if (ExitBlock != LoopExit.getBlock()) { |
| 1343 | EmitBlock(ExitBlock); |
| 1344 | EmitBranchThroughCleanup(LoopExit); |
| 1345 | } |
| 1346 | |
| 1347 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1348 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1349 | |
| 1350 | // Create a block for the increment. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1351 | auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1352 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1353 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1354 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1355 | |
| 1356 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1357 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1358 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1359 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1360 | BreakContinueStack.pop_back(); |
| 1361 | EmitBranch(CondBlock); |
| 1362 | LoopStack.pop(); |
| 1363 | // Emit the fall-through block. |
| 1364 | EmitBlock(LoopExit.getBlock()); |
| 1365 | } |
| 1366 | |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1367 | bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1368 | if (!HaveInsertPoint()) |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1369 | return false; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1370 | // Emit inits for the linear variables. |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1371 | bool HasLinears = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1372 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1373 | for (auto *Init : C->inits()) { |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1374 | HasLinears = true; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1375 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1376 | if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
| 1377 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 1378 | auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
| 1379 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1380 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1381 | VD->getInit()->getType(), VK_LValue, |
| 1382 | VD->getInit()->getExprLoc()); |
| 1383 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1384 | VD->getType()), |
| 1385 | /*capturedByInit=*/false); |
| 1386 | EmitAutoVarCleanups(Emission); |
| 1387 | } else |
| 1388 | EmitVarDecl(*VD); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1389 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1390 | // Emit the linear steps for the linear clauses. |
| 1391 | // If a step is not constant, it is pre-calculated before the loop. |
| 1392 | if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1393 | if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1394 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1395 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1396 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1397 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1398 | } |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1399 | return HasLinears; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1402 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1403 | const OMPLoopDirective &D, |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1404 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1405 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1406 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1407 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1408 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1409 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1410 | auto IC = C->varlist_begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1411 | for (auto *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1412 | if (!DoneBB) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1413 | if (auto *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1414 | // If the first post-update expression is found, emit conditional |
| 1415 | // block if it was requested. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1416 | auto *ThenBB = createBasicBlock(".omp.linear.pu"); |
| 1417 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1418 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1419 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1420 | } |
| 1421 | } |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1422 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
| 1423 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1424 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1425 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1426 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1427 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
| 1428 | VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1429 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1430 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1431 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1432 | } |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 1433 | if (auto *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1434 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1435 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1436 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1437 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1440 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1441 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1442 | if (!CGF.HaveInsertPoint()) |
| 1443 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1444 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1445 | unsigned ClauseAlignment = 0; |
| 1446 | if (auto AlignmentExpr = Clause->getAlignment()) { |
| 1447 | auto AlignmentCI = |
| 1448 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1449 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1450 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1451 | for (auto E : Clause->varlists()) { |
| 1452 | unsigned Alignment = ClauseAlignment; |
| 1453 | if (Alignment == 0) { |
| 1454 | // OpenMP [2.8.1, Description] |
| 1455 | // If no optional parameter is specified, implementation-defined default |
| 1456 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1457 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1458 | CGF.getContext() |
| 1459 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1460 | E->getType()->getPointeeType())) |
| 1461 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1462 | } |
| 1463 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1464 | "alignment is not power of 2"); |
| 1465 | if (Alignment != 0) { |
| 1466 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
| 1467 | CGF.EmitAlignmentAssumption(PtrValue, Alignment); |
| 1468 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1473 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1474 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1475 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1476 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1477 | auto I = S.private_counters().begin(); |
| 1478 | for (auto *E : S.counters()) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1479 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1480 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1481 | // Emit var without initialization. |
| 1482 | auto VarEmission = EmitAutoVarAlloca(*PrivateVD); |
| 1483 | EmitAutoVarCleanups(VarEmission); |
| 1484 | LocalDeclMap.erase(PrivateVD); |
| 1485 | (void)LoopScope.addPrivate(VD, [&VarEmission]() { |
| 1486 | return VarEmission.getAllocatedAddress(); |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1487 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1488 | if (LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD) || |
| 1489 | VD->hasGlobalStorage()) { |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1490 | (void)LoopScope.addPrivate(PrivateVD, [this, VD, E]() { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1491 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 1492 | LocalDeclMap.count(VD) || CapturedStmtInfo->lookup(VD), |
| 1493 | E->getType(), VK_LValue, E->getExprLoc()); |
| 1494 | return EmitLValue(&DRE).getAddress(); |
| 1495 | }); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1496 | } else { |
| 1497 | (void)LoopScope.addPrivate(PrivateVD, [&VarEmission]() { |
| 1498 | return VarEmission.getAllocatedAddress(); |
| 1499 | }); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1500 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1501 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1502 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1505 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1506 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1507 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1508 | if (!CGF.HaveInsertPoint()) |
| 1509 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1510 | { |
| 1511 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1512 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1513 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1514 | // Get initial values of real counters. |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 1515 | for (auto I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1516 | CGF.EmitIgnoredExpr(I); |
| 1517 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1518 | } |
| 1519 | // Check that loop is executed at least one time. |
| 1520 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 1521 | } |
| 1522 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1523 | void CodeGenFunction::EmitOMPLinearClause( |
| 1524 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1525 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1526 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1527 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1528 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 1529 | auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1530 | for (auto *C : LoopDirective->counters()) { |
| 1531 | SIMDLCVs.insert( |
| 1532 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1533 | } |
| 1534 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1535 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1536 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1537 | for (auto *E : C->varlists()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1538 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1539 | auto *PrivateVD = |
| 1540 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1541 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
| 1542 | bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address { |
| 1543 | // Emit private VarDecl with copy init. |
| 1544 | EmitVarDecl(*PrivateVD); |
| 1545 | return GetAddrOfLocalVar(PrivateVD); |
| 1546 | }); |
| 1547 | assert(IsRegistered && "linear var already registered as private"); |
| 1548 | // Silence the warning about unused variable. |
| 1549 | (void)IsRegistered; |
| 1550 | } else |
| 1551 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1552 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1557 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1558 | const OMPExecutableDirective &D, |
| 1559 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1560 | if (!CGF.HaveInsertPoint()) |
| 1561 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1562 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1563 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1564 | /*ignoreResult=*/true); |
| 1565 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
| 1566 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1567 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1568 | // the memory instructions parallel, because loop-carried |
| 1569 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1570 | if (!IsMonotonic) |
| 1571 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1572 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1573 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1574 | /*ignoreResult=*/true); |
| 1575 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1576 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1577 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1578 | // the memory instructions parallel, because loop-carried |
| 1579 | // dependences of 'safelen' iterations are possible. |
| 1580 | CGF.LoopStack.setParallel(false); |
| 1581 | } |
| 1582 | } |
| 1583 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1584 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1585 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1586 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1587 | LoopStack.setParallel(!IsMonotonic); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1588 | LoopStack.setVectorizeEnable(true); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1589 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1590 | } |
| 1591 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1592 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1593 | const OMPLoopDirective &D, |
| 1594 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1595 | if (!HaveInsertPoint()) |
| 1596 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1597 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1598 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1599 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1600 | for (auto F : D.finals()) { |
| 1601 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1602 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1603 | auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
| 1604 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1605 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1606 | if (!DoneBB) { |
| 1607 | if (auto *Cond = CondGen(*this)) { |
| 1608 | // If the first post-update expression is found, emit conditional |
| 1609 | // block if it was requested. |
| 1610 | auto *ThenBB = createBasicBlock(".omp.final.then"); |
| 1611 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1612 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1613 | EmitBlock(ThenBB); |
| 1614 | } |
| 1615 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1616 | Address OrigAddr = Address::invalid(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1617 | if (CED) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1618 | OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); |
Alexey Bataev | ab4ea22 | 2018-03-07 18:17:06 +0000 | [diff] [blame] | 1619 | } else { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1620 | DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), |
| 1621 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1622 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
| 1623 | OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1624 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1625 | OMPPrivateScope VarScope(*this); |
| 1626 | VarScope.addPrivate(OrigVD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1627 | [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1628 | (void)VarScope.Privatize(); |
| 1629 | EmitIgnoredExpr(F); |
| 1630 | } |
| 1631 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1632 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1633 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1634 | if (DoneBB) |
| 1635 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1638 | static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, |
| 1639 | const OMPLoopDirective &S, |
| 1640 | CodeGenFunction::JumpDest LoopExit) { |
| 1641 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1642 | CGF.EmitStopPoint(&S); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 1643 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1644 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1645 | /// Emit a helper variable and return corresponding lvalue. |
| 1646 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1647 | const DeclRefExpr *Helper) { |
| 1648 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1649 | CGF.EmitVarDecl(*VDecl); |
| 1650 | return CGF.EmitLValue(Helper); |
| 1651 | } |
| 1652 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1653 | static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1654 | PrePostActionTy &Action) { |
| 1655 | Action.Enter(CGF); |
| 1656 | assert(isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 1657 | "Expected simd directive"); |
| 1658 | OMPLoopScope PreInitScope(CGF, S); |
| 1659 | // if (PreCond) { |
| 1660 | // for (IV in 0..LastIteration) BODY; |
| 1661 | // <Final counter/linear vars updates>; |
| 1662 | // } |
| 1663 | // |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1664 | if (isOpenMPDistributeDirective(S.getDirectiveKind()) || |
| 1665 | isOpenMPWorksharingDirective(S.getDirectiveKind()) || |
| 1666 | isOpenMPTaskLoopDirective(S.getDirectiveKind())) { |
| 1667 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1668 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1669 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1670 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1671 | // Emit: if (PreCond) - begin. |
| 1672 | // If the condition constant folds and can be elided, avoid emitting the |
| 1673 | // whole loop. |
| 1674 | bool CondConstant; |
| 1675 | llvm::BasicBlock *ContBlock = nullptr; |
| 1676 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1677 | if (!CondConstant) |
| 1678 | return; |
| 1679 | } else { |
| 1680 | auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
| 1681 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
| 1682 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1683 | CGF.getProfileCount(&S)); |
| 1684 | CGF.EmitBlock(ThenBlock); |
| 1685 | CGF.incrementProfileCounter(&S); |
| 1686 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1687 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1688 | // Emit the loop iteration variable. |
| 1689 | const Expr *IVExpr = S.getIterationVariable(); |
| 1690 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 1691 | CGF.EmitVarDecl(*IVDecl); |
| 1692 | CGF.EmitIgnoredExpr(S.getInit()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1693 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1694 | // Emit the iterations count variable. |
| 1695 | // If it is not a variable, Sema decided to calculate iterations count on |
| 1696 | // each iteration (e.g., it is foldable into a constant). |
| 1697 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 1698 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1699 | // Emit calculation of the iterations count. |
| 1700 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1701 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1702 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1703 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1704 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1705 | emitAlignedClause(CGF, S); |
| 1706 | (void)CGF.EmitOMPLinearClauseInit(S); |
| 1707 | { |
| 1708 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1709 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 1710 | CGF.EmitOMPLinearClause(S, LoopScope); |
| 1711 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 1712 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 1713 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 1714 | (void)LoopScope.Privatize(); |
| 1715 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1716 | S.getInc(), |
| 1717 | [&S](CodeGenFunction &CGF) { |
| 1718 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 1719 | CGF.EmitStopPoint(&S); |
| 1720 | }, |
| 1721 | [](CodeGenFunction &) {}); |
| 1722 | CGF.EmitOMPSimdFinal( |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1723 | S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1724 | // Emit final copy of the lastprivate variables at the end of loops. |
| 1725 | if (HasLastprivateClause) |
| 1726 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
| 1727 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
| 1728 | emitPostUpdateForReductionClause( |
| 1729 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
| 1730 | } |
| 1731 | CGF.EmitOMPLinearClauseFinal( |
| 1732 | S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
| 1733 | // Emit: if (PreCond) - end. |
| 1734 | if (ContBlock) { |
| 1735 | CGF.EmitBranch(ContBlock); |
| 1736 | CGF.EmitBlock(ContBlock, true); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
| 1741 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 1742 | emitOMPSimdRegion(CGF, S, Action); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1743 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1744 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1745 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1746 | } |
| 1747 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1748 | void CodeGenFunction::EmitOMPOuterLoop( |
| 1749 | bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, |
| 1750 | CodeGenFunction::OMPPrivateScope &LoopScope, |
| 1751 | const CodeGenFunction::OMPLoopArguments &LoopArgs, |
| 1752 | const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, |
| 1753 | const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1754 | auto &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1755 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1756 | const Expr *IVExpr = S.getIterationVariable(); |
| 1757 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1758 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1759 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1760 | auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
| 1761 | |
| 1762 | // Start the loop with a block that tests the condition. |
| 1763 | auto CondBlock = createBasicBlock("omp.dispatch.cond"); |
| 1764 | EmitBlock(CondBlock); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1765 | const SourceRange &R = S.getSourceRange(); |
| 1766 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1767 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1768 | |
| 1769 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1770 | if (!DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1771 | // UB = min(UB, GlobalUB) or |
| 1772 | // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. |
| 1773 | // 'distribute parallel for') |
| 1774 | EmitIgnoredExpr(LoopArgs.EUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1775 | // IV = LB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1776 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1777 | // IV < UB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1778 | BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1779 | } else { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1780 | BoolCondVal = |
| 1781 | RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, LoopArgs.IL, |
| 1782 | LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1783 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1784 | |
| 1785 | // If there are any cleanups between here and the loop-exit scope, |
| 1786 | // create a block to stage a loop exit along. |
| 1787 | auto ExitBlock = LoopExit.getBlock(); |
| 1788 | if (LoopScope.requiresCleanups()) |
| 1789 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1790 | |
| 1791 | auto LoopBody = createBasicBlock("omp.dispatch.body"); |
| 1792 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1793 | if (ExitBlock != LoopExit.getBlock()) { |
| 1794 | EmitBlock(ExitBlock); |
| 1795 | EmitBranchThroughCleanup(LoopExit); |
| 1796 | } |
| 1797 | EmitBlock(LoopBody); |
| 1798 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1799 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1800 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1801 | if (DynamicOrOrdered) |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1802 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1803 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1804 | // Create a block for the increment. |
| 1805 | auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
| 1806 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1807 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1808 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1809 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1810 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1811 | LoopStack.setParallel(!IsMonotonic); |
| 1812 | else |
| 1813 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1814 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1815 | SourceLocation Loc = S.getLocStart(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1816 | |
| 1817 | // when 'distribute' is not combined with a 'for': |
| 1818 | // while (idx <= UB) { BODY; ++idx; } |
| 1819 | // when 'distribute' is combined with a 'for' |
| 1820 | // (e.g. 'distribute parallel for') |
| 1821 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 1822 | EmitOMPInnerLoop( |
| 1823 | S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, |
| 1824 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 1825 | CodeGenLoop(CGF, S, LoopExit); |
| 1826 | }, |
| 1827 | [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { |
| 1828 | CodeGenOrdered(CGF, Loc, IVSize, IVSigned); |
| 1829 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1830 | |
| 1831 | EmitBlock(Continue.getBlock()); |
| 1832 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1833 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1834 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1835 | EmitIgnoredExpr(LoopArgs.NextLB); |
| 1836 | EmitIgnoredExpr(LoopArgs.NextUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1837 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1838 | |
| 1839 | EmitBranch(CondBlock); |
| 1840 | LoopStack.pop(); |
| 1841 | // Emit the fall-through block. |
| 1842 | EmitBlock(LoopExit.getBlock()); |
| 1843 | |
| 1844 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1845 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 1846 | if (!DynamicOrOrdered) |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 1847 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(), |
| 1848 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1849 | }; |
| 1850 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1854 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1855 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1856 | const OMPLoopArguments &LoopArgs, |
| 1857 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1858 | auto &RT = CGM.getOpenMPRuntime(); |
| 1859 | |
| 1860 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1861 | const bool DynamicOrOrdered = |
| 1862 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1863 | |
| 1864 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1865 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1866 | LoopArgs.Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1867 | "static non-chunked schedule does not need outer loop"); |
| 1868 | |
| 1869 | // Emit outer loop. |
| 1870 | // |
| 1871 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1872 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1873 | // distributed to threads in the team in chunks as the threads request them. |
| 1874 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1875 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1876 | // iterations, except for the last chunk to be distributed, which may have |
| 1877 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1878 | // |
| 1879 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1880 | // to threads in the team in chunks as the executing threads request them. |
| 1881 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1882 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1883 | // each chunk is proportional to the number of unassigned iterations divided |
| 1884 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1885 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1886 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1887 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1888 | // than k iterations). |
| 1889 | // |
| 1890 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1891 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1892 | // implementation the freedom to choose any possible mapping of iterations to |
| 1893 | // threads in the team. |
| 1894 | // |
| 1895 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1896 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1897 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1898 | // implementation defined |
| 1899 | // |
| 1900 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1901 | // idx = LB; |
| 1902 | // while (idx <= UB) { BODY; ++idx; |
| 1903 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1904 | // } // inner loop |
| 1905 | // } |
| 1906 | // |
| 1907 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1908 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1909 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1910 | // the team in a round-robin fashion in the order of the thread number. |
| 1911 | // |
| 1912 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1913 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1914 | // LB = LB + ST; |
| 1915 | // UB = UB + ST; |
| 1916 | // } |
| 1917 | // |
| 1918 | |
| 1919 | const Expr *IVExpr = S.getIterationVariable(); |
| 1920 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1921 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1922 | |
| 1923 | if (DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1924 | auto DispatchBounds = CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); |
| 1925 | llvm::Value *LBVal = DispatchBounds.first; |
| 1926 | llvm::Value *UBVal = DispatchBounds.second; |
| 1927 | CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, |
| 1928 | LoopArgs.Chunk}; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1929 | RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1930 | IVSigned, Ordered, DipatchRTInputValues); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1931 | } else { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1932 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 1933 | IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, |
| 1934 | LoopArgs.ST, LoopArgs.Chunk); |
| 1935 | RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(), |
| 1936 | ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1937 | } |
| 1938 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1939 | auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, |
| 1940 | const unsigned IVSize, |
| 1941 | const bool IVSigned) { |
| 1942 | if (Ordered) { |
| 1943 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, |
| 1944 | IVSigned); |
| 1945 | } |
| 1946 | }; |
| 1947 | |
| 1948 | OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, |
| 1949 | LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); |
| 1950 | OuterLoopArgs.IncExpr = S.getInc(); |
| 1951 | OuterLoopArgs.Init = S.getInit(); |
| 1952 | OuterLoopArgs.Cond = S.getCond(); |
| 1953 | OuterLoopArgs.NextLB = S.getNextLowerBound(); |
| 1954 | OuterLoopArgs.NextUB = S.getNextUpperBound(); |
| 1955 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, |
| 1956 | emitOMPLoopBodyWithStopPoint, CodeGenOrdered); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1957 | } |
| 1958 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1959 | static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, |
| 1960 | const unsigned IVSize, const bool IVSigned) {} |
| 1961 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1962 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1963 | OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, |
| 1964 | OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, |
| 1965 | const CodeGenLoopTy &CodeGenLoopContent) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1966 | |
| 1967 | auto &RT = CGM.getOpenMPRuntime(); |
| 1968 | |
| 1969 | // Emit outer loop. |
| 1970 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 1971 | // dynamic |
| 1972 | // |
| 1973 | |
| 1974 | const Expr *IVExpr = S.getIterationVariable(); |
| 1975 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1976 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1977 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1978 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 1979 | IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, |
| 1980 | LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); |
| 1981 | RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1982 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1983 | // for combined 'distribute' and 'for' the increment expression of distribute |
| 1984 | // is store in DistInc. For 'distribute' alone, it is in Inc. |
| 1985 | Expr *IncExpr; |
| 1986 | if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) |
| 1987 | IncExpr = S.getDistInc(); |
| 1988 | else |
| 1989 | IncExpr = S.getInc(); |
| 1990 | |
| 1991 | // this routine is shared by 'omp distribute parallel for' and |
| 1992 | // 'omp distribute': select the right EUB expression depending on the |
| 1993 | // directive |
| 1994 | OMPLoopArguments OuterLoopArgs; |
| 1995 | OuterLoopArgs.LB = LoopArgs.LB; |
| 1996 | OuterLoopArgs.UB = LoopArgs.UB; |
| 1997 | OuterLoopArgs.ST = LoopArgs.ST; |
| 1998 | OuterLoopArgs.IL = LoopArgs.IL; |
| 1999 | OuterLoopArgs.Chunk = LoopArgs.Chunk; |
| 2000 | OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2001 | ? S.getCombinedEnsureUpperBound() |
| 2002 | : S.getEnsureUpperBound(); |
| 2003 | OuterLoopArgs.IncExpr = IncExpr; |
| 2004 | OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2005 | ? S.getCombinedInit() |
| 2006 | : S.getInit(); |
| 2007 | OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2008 | ? S.getCombinedCond() |
| 2009 | : S.getCond(); |
| 2010 | OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2011 | ? S.getCombinedNextLowerBound() |
| 2012 | : S.getNextLowerBound(); |
| 2013 | OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2014 | ? S.getCombinedNextUpperBound() |
| 2015 | : S.getNextUpperBound(); |
| 2016 | |
| 2017 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, |
| 2018 | LoopScope, OuterLoopArgs, CodeGenLoopContent, |
| 2019 | emitEmptyOrdered); |
| 2020 | } |
| 2021 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2022 | static std::pair<LValue, LValue> |
| 2023 | emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, |
| 2024 | const OMPExecutableDirective &S) { |
| 2025 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2026 | LValue LB = |
| 2027 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2028 | LValue UB = |
| 2029 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2030 | |
| 2031 | // When composing 'distribute' with 'for' (e.g. as in 'distribute |
| 2032 | // parallel for') we need to use the 'distribute' |
| 2033 | // chunk lower and upper bounds rather than the whole loop iteration |
| 2034 | // space. These are parameters to the outlined function for 'parallel' |
| 2035 | // and we copy the bounds of the previous schedule into the |
| 2036 | // the current ones. |
| 2037 | LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); |
| 2038 | LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2039 | llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar( |
| 2040 | PrevLB, LS.getPrevLowerBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2041 | PrevLBVal = CGF.EmitScalarConversion( |
| 2042 | PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2043 | LS.getIterationVariable()->getType(), |
| 2044 | LS.getPrevLowerBoundVariable()->getExprLoc()); |
| 2045 | llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar( |
| 2046 | PrevUB, LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2047 | PrevUBVal = CGF.EmitScalarConversion( |
| 2048 | PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2049 | LS.getIterationVariable()->getType(), |
| 2050 | LS.getPrevUpperBoundVariable()->getExprLoc()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2051 | |
| 2052 | CGF.EmitStoreOfScalar(PrevLBVal, LB); |
| 2053 | CGF.EmitStoreOfScalar(PrevUBVal, UB); |
| 2054 | |
| 2055 | return {LB, UB}; |
| 2056 | } |
| 2057 | |
| 2058 | /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then |
| 2059 | /// we need to use the LB and UB expressions generated by the worksharing |
| 2060 | /// code generation support, whereas in non combined situations we would |
| 2061 | /// just emit 0 and the LastIteration expression |
| 2062 | /// This function is necessary due to the difference of the LB and UB |
| 2063 | /// types for the RT emission routines for 'for_static_init' and |
| 2064 | /// 'for_dispatch_init' |
| 2065 | static std::pair<llvm::Value *, llvm::Value *> |
| 2066 | emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, |
| 2067 | const OMPExecutableDirective &S, |
| 2068 | Address LB, Address UB) { |
| 2069 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2070 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2071 | // when implementing a dynamic schedule for a 'for' combined with a |
| 2072 | // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop |
| 2073 | // is not normalized as each team only executes its own assigned |
| 2074 | // distribute chunk |
| 2075 | QualType IteratorTy = IVExpr->getType(); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2076 | llvm::Value *LBVal = |
| 2077 | CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, S.getLocStart()); |
| 2078 | llvm::Value *UBVal = |
| 2079 | CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, S.getLocStart()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2080 | return {LBVal, UBVal}; |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2081 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2082 | |
| 2083 | static void emitDistributeParallelForDistributeInnerBoundParams( |
| 2084 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2085 | llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { |
| 2086 | const auto &Dir = cast<OMPLoopDirective>(S); |
| 2087 | LValue LB = |
| 2088 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); |
| 2089 | auto LBCast = CGF.Builder.CreateIntCast( |
| 2090 | CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2091 | CapturedVars.push_back(LBCast); |
| 2092 | LValue UB = |
| 2093 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); |
| 2094 | |
| 2095 | auto UBCast = CGF.Builder.CreateIntCast( |
| 2096 | CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2097 | CapturedVars.push_back(UBCast); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2098 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2099 | |
| 2100 | static void |
| 2101 | emitInnerParallelForWhenCombined(CodeGenFunction &CGF, |
| 2102 | const OMPLoopDirective &S, |
| 2103 | CodeGenFunction::JumpDest LoopExit) { |
| 2104 | auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2105 | PrePostActionTy &Action) { |
| 2106 | Action.Enter(CGF); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2107 | bool HasCancel = false; |
| 2108 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2109 | if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) |
| 2110 | HasCancel = D->hasCancel(); |
| 2111 | else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) |
| 2112 | HasCancel = D->hasCancel(); |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 2113 | else if (const auto *D = |
| 2114 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) |
| 2115 | HasCancel = D->hasCancel(); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2116 | } |
| 2117 | CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), |
| 2118 | HasCancel); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2119 | CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), |
| 2120 | emitDistributeParallelForInnerBounds, |
| 2121 | emitDistributeParallelForDispatchBounds); |
| 2122 | }; |
| 2123 | |
| 2124 | emitCommonOMPParallelDirective( |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2125 | CGF, S, |
| 2126 | isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, |
| 2127 | CGInlinedWorksharingLoop, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2128 | emitDistributeParallelForDistributeInnerBoundParams); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2131 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 2132 | const OMPDistributeParallelForDirective &S) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2133 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2134 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2135 | S.getDistInc()); |
| 2136 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2137 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 2138 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2141 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 2142 | const OMPDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2143 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2144 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2145 | S.getDistInc()); |
| 2146 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2147 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2148 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2149 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2150 | |
| 2151 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 2152 | const OMPDistributeSimdDirective &S) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2153 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2154 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 2155 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2156 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2157 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2160 | void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| 2161 | CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { |
| 2162 | // Emit SPMD target parallel for region as a standalone region. |
| 2163 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2164 | emitOMPSimdRegion(CGF, S, Action); |
| 2165 | }; |
| 2166 | llvm::Function *Fn; |
| 2167 | llvm::Constant *Addr; |
| 2168 | // Emit target region as a standalone region. |
| 2169 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 2170 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 2171 | assert(Fn && Addr && "Target device function emission failed."); |
| 2172 | } |
| 2173 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2174 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 2175 | const OMPTargetSimdDirective &S) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2176 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2177 | emitOMPSimdRegion(CGF, S, Action); |
| 2178 | }; |
| 2179 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2180 | } |
| 2181 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2182 | namespace { |
| 2183 | struct ScheduleKindModifiersTy { |
| 2184 | OpenMPScheduleClauseKind Kind; |
| 2185 | OpenMPScheduleClauseModifier M1; |
| 2186 | OpenMPScheduleClauseModifier M2; |
| 2187 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2188 | OpenMPScheduleClauseModifier M1, |
| 2189 | OpenMPScheduleClauseModifier M2) |
| 2190 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2191 | }; |
| 2192 | } // namespace |
| 2193 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2194 | bool CodeGenFunction::EmitOMPWorksharingLoop( |
| 2195 | const OMPLoopDirective &S, Expr *EUB, |
| 2196 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
| 2197 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2198 | // Emit the loop iteration variable. |
| 2199 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2200 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 2201 | EmitVarDecl(*IVDecl); |
| 2202 | |
| 2203 | // Emit the iterations count variable. |
| 2204 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2205 | // iteration (e.g., it is foldable into a constant). |
| 2206 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 2207 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2208 | // Emit calculation of the iterations count. |
| 2209 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2210 | } |
| 2211 | |
| 2212 | auto &RT = CGM.getOpenMPRuntime(); |
| 2213 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2214 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2215 | // Check pre-condition. |
| 2216 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2217 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2218 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2219 | // If the condition constant folds and can be elided, avoid emitting the |
| 2220 | // whole loop. |
| 2221 | bool CondConstant; |
| 2222 | llvm::BasicBlock *ContBlock = nullptr; |
| 2223 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2224 | if (!CondConstant) |
| 2225 | return false; |
| 2226 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2227 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 2228 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2229 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2230 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2231 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2232 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2233 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2234 | |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2235 | RunCleanupsScope DoacrossCleanupScope(*this); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 2236 | bool Ordered = false; |
| 2237 | if (auto *OrderedClause = S.getSingleClause<OMPOrderedClause>()) { |
| 2238 | if (OrderedClause->getNumForLoops()) |
| 2239 | RT.emitDoacrossInit(*this, S); |
| 2240 | else |
| 2241 | Ordered = true; |
| 2242 | } |
| 2243 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2244 | llvm::DenseSet<const Expr *> EmittedFinals; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2245 | emitAlignedClause(*this, S); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2246 | bool HasLinears = EmitOMPLinearClauseInit(S); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2247 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2248 | |
| 2249 | std::pair<LValue, LValue> Bounds = CodeGenLoopBounds(*this, S); |
| 2250 | LValue LB = Bounds.first; |
| 2251 | LValue UB = Bounds.second; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2252 | LValue ST = |
| 2253 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 2254 | LValue IL = |
| 2255 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 2256 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2257 | // Emit 'then' code. |
| 2258 | { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2259 | OMPPrivateScope LoopScope(*this); |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 2260 | if (EmitOMPFirstprivateClause(S, LoopScope) || HasLinears) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2261 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2262 | // initialization of firstprivate variables and post-update of |
| 2263 | // lastprivate variables. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2264 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 2265 | *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 2266 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 2267 | } |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 2268 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2269 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7ebe5fd | 2015-04-22 13:43:03 +0000 | [diff] [blame] | 2270 | EmitOMPReductionClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2271 | EmitOMPPrivateLoopCounters(S, LoopScope); |
| 2272 | EmitOMPLinearClause(S, LoopScope); |
Alexander Musman | 7931b98 | 2015-03-16 07:14:41 +0000 | [diff] [blame] | 2273 | (void)LoopScope.Privatize(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2274 | |
| 2275 | // Detect the loop schedule kind and chunk. |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2276 | llvm::Value *Chunk = nullptr; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2277 | OpenMPScheduleTy ScheduleKind; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2278 | if (auto *C = S.getSingleClause<OMPScheduleClause>()) { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2279 | ScheduleKind.Schedule = C->getScheduleKind(); |
| 2280 | ScheduleKind.M1 = C->getFirstScheduleModifier(); |
| 2281 | ScheduleKind.M2 = C->getSecondScheduleModifier(); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2282 | if (const auto *Ch = C->getChunkSize()) { |
| 2283 | Chunk = EmitScalarExpr(Ch); |
| 2284 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
| 2285 | S.getIterationVariable()->getType(), |
| 2286 | S.getLocStart()); |
| 2287 | } |
| 2288 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2289 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 2290 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2291 | // OpenMP 4.5, 2.7.1 Loop Construct, Description. |
| 2292 | // If the static schedule kind is specified or if the ordered clause is |
| 2293 | // specified, and if no monotonic modifier is specified, the effect will |
| 2294 | // be as if the monotonic modifier was specified. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2295 | if (RT.isStaticNonchunked(ScheduleKind.Schedule, |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 2296 | /* Chunked */ Chunk != nullptr) && |
| 2297 | !Ordered) { |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2298 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 2299 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2300 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 2301 | // When no chunk_size is specified, the iteration space is divided into |
| 2302 | // chunks that are approximately equal in size, and at most one chunk is |
| 2303 | // distributed to each thread. Note that the size of the chunks is |
| 2304 | // unspecified in this case. |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2305 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2306 | IVSize, IVSigned, Ordered, IL.getAddress(), LB.getAddress(), |
| 2307 | UB.getAddress(), ST.getAddress()); |
| 2308 | RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(), |
| 2309 | ScheduleKind, StaticInit); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2310 | auto LoopExit = |
| 2311 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2312 | // UB = min(UB, GlobalUB); |
| 2313 | EmitIgnoredExpr(S.getEnsureUpperBound()); |
| 2314 | // IV = LB; |
| 2315 | EmitIgnoredExpr(S.getInit()); |
| 2316 | // while (idx <= UB) { BODY; ++idx; } |
Alexey Bataev | ae05c29 | 2015-06-16 11:59:36 +0000 | [diff] [blame] | 2317 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 2318 | S.getInc(), |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2319 | [&S, LoopExit](CodeGenFunction &CGF) { |
| 2320 | CGF.EmitOMPLoopBody(S, LoopExit); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2321 | CGF.EmitStopPoint(&S); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 2322 | }, |
| 2323 | [](CodeGenFunction &) {}); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 2324 | EmitBlock(LoopExit.getBlock()); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2325 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2326 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2327 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(), |
| 2328 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2329 | }; |
| 2330 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2331 | } else { |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2332 | const bool IsMonotonic = |
| 2333 | Ordered || ScheduleKind.Schedule == OMPC_SCHEDULE_static || |
| 2334 | ScheduleKind.Schedule == OMPC_SCHEDULE_unknown || |
| 2335 | ScheduleKind.M1 == OMPC_SCHEDULE_MODIFIER_monotonic || |
| 2336 | ScheduleKind.M2 == OMPC_SCHEDULE_MODIFIER_monotonic; |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2337 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 2338 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2339 | const OMPLoopArguments LoopArguments(LB.getAddress(), UB.getAddress(), |
| 2340 | ST.getAddress(), IL.getAddress(), |
| 2341 | Chunk, EUB); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2342 | EmitOMPForOuterLoop(ScheduleKind, IsMonotonic, S, LoopScope, Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2343 | LoopArguments, CGDispatchBounds); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2344 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2345 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2346 | EmitOMPSimdFinal(S, |
| 2347 | [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2348 | return CGF.Builder.CreateIsNotNull( |
| 2349 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2350 | }); |
| 2351 | } |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2352 | EmitOMPReductionClauseFinal( |
| 2353 | S, /*ReductionKind=*/isOpenMPSimdDirective(S.getDirectiveKind()) |
| 2354 | ? /*Parallel and Simd*/ OMPD_parallel_for_simd |
| 2355 | : /*Parallel only*/ OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2356 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2357 | emitPostUpdateForReductionClause( |
| 2358 | *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2359 | return CGF.Builder.CreateIsNotNull( |
| 2360 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2361 | }); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2362 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2363 | if (HasLastprivateClause) |
| 2364 | EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2365 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 2366 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2367 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2368 | EmitOMPLinearClauseFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 2369 | return CGF.Builder.CreateIsNotNull( |
| 2370 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2371 | }); |
Alexey Bataev | ea33dee | 2018-02-15 23:39:43 +0000 | [diff] [blame] | 2372 | DoacrossCleanupScope.ForceCleanup(); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2373 | // 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] | 2374 | if (ContBlock) { |
| 2375 | EmitBranch(ContBlock); |
| 2376 | EmitBlock(ContBlock, true); |
| 2377 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2378 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2379 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2382 | /// The following two functions generate expressions for the loop lower |
| 2383 | /// and upper bounds in case of static and dynamic (dispatch) schedule |
| 2384 | /// of the associated 'for' or 'distribute' loop. |
| 2385 | static std::pair<LValue, LValue> |
| 2386 | emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 2387 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2388 | LValue LB = |
| 2389 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2390 | LValue UB = |
| 2391 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2392 | return {LB, UB}; |
| 2393 | } |
| 2394 | |
| 2395 | /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not |
| 2396 | /// consider the lower and upper bound expressions generated by the |
| 2397 | /// worksharing loop support, but we use 0 and the iteration space size as |
| 2398 | /// constants |
| 2399 | static std::pair<llvm::Value *, llvm::Value *> |
| 2400 | emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2401 | Address LB, Address UB) { |
| 2402 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2403 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2404 | const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); |
| 2405 | llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); |
| 2406 | llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); |
| 2407 | return {LBVal, UBVal}; |
| 2408 | } |
| 2409 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2410 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2411 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2412 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2413 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2414 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2415 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2416 | emitForLoopBounds, |
| 2417 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2418 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2419 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2420 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2421 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2422 | S.hasCancel()); |
| 2423 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2424 | |
| 2425 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2426 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2427 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 2428 | } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2429 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2430 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2431 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2432 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2433 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2434 | PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2435 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2436 | emitForLoopBounds, |
| 2437 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2438 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2439 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2440 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2441 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2442 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2443 | |
| 2444 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2445 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2446 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 2447 | } |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2450 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2451 | const Twine &Name, |
| 2452 | llvm::Value *Init = nullptr) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2453 | auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2454 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2455 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2456 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2457 | } |
| 2458 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2459 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2460 | const Stmt *Stmt = S.getInnermostCapturedStmt()->getCapturedStmt(); |
| 2461 | const auto *CS = dyn_cast<CompoundStmt>(Stmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2462 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2463 | auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF, |
| 2464 | PrePostActionTy &) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2465 | auto &C = CGF.CGM.getContext(); |
| 2466 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 2467 | // Emit helper vars inits. |
| 2468 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2469 | CGF.Builder.getInt32(0)); |
| 2470 | auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1) |
| 2471 | : CGF.Builder.getInt32(0); |
| 2472 | LValue UB = |
| 2473 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2474 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2475 | CGF.Builder.getInt32(1)); |
| 2476 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2477 | CGF.Builder.getInt32(0)); |
| 2478 | // Loop counter. |
| 2479 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
| 2480 | OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 2481 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
| 2482 | OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 2483 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2484 | // Generate condition for loop. |
| 2485 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 2486 | OK_Ordinary, S.getLocStart(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2487 | // Increment for loop counter. |
| 2488 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 2489 | S.getLocStart(), true); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2490 | auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) { |
| 2491 | // Iterate through all sections and emit a switch construct: |
| 2492 | // switch (IV) { |
| 2493 | // case 0: |
| 2494 | // <SectionStmt[0]>; |
| 2495 | // break; |
| 2496 | // ... |
| 2497 | // case <NumSection> - 1: |
| 2498 | // <SectionStmt[<NumSection> - 1]>; |
| 2499 | // break; |
| 2500 | // } |
| 2501 | // .omp.sections.exit: |
| 2502 | auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2503 | auto *SwitchStmt = |
| 2504 | CGF.Builder.CreateSwitch(CGF.EmitLoadOfScalar(IV, S.getLocStart()), |
| 2505 | ExitBB, CS == nullptr ? 1 : CS->size()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2506 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2507 | unsigned CaseNumber = 0; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2508 | for (auto *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2509 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2510 | CGF.EmitBlock(CaseBB); |
| 2511 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2512 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2513 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2514 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2515 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2516 | } else { |
| 2517 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2518 | CGF.EmitBlock(CaseBB); |
| 2519 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
| 2520 | CGF.EmitStmt(Stmt); |
| 2521 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2522 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2523 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2524 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2525 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2526 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2527 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2528 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2529 | // initialization of firstprivate variables and post-update of lastprivate |
| 2530 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2531 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 2532 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 2533 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2534 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2535 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2536 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2537 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2538 | (void)LoopScope.Privatize(); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2539 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2540 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2541 | OpenMPScheduleTy ScheduleKind; |
| 2542 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2543 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2544 | /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), |
| 2545 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2546 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2547 | CGF, S.getLocStart(), S.getDirectiveKind(), ScheduleKind, StaticInit); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2548 | // UB = min(UB, GlobalUB); |
| 2549 | auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); |
| 2550 | auto *MinUBGlobalUB = CGF.Builder.CreateSelect( |
| 2551 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 2552 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 2553 | // IV = LB; |
| 2554 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); |
| 2555 | // while (idx <= UB) { BODY; ++idx; } |
| 2556 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 2557 | [](CodeGenFunction &) {}); |
| 2558 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2559 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2560 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(), |
| 2561 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2562 | }; |
| 2563 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2564 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2565 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2566 | emitPostUpdateForReductionClause( |
| 2567 | CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2568 | return CGF.Builder.CreateIsNotNull( |
| 2569 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2570 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2571 | |
| 2572 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2573 | if (HasLastprivates) |
| 2574 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2575 | S, /*NoFinals=*/false, |
| 2576 | CGF.Builder.CreateIsNotNull( |
| 2577 | CGF.EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2578 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2579 | |
| 2580 | bool HasCancel = false; |
| 2581 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 2582 | HasCancel = OSD->hasCancel(); |
| 2583 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 2584 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2585 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2586 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 2587 | HasCancel); |
| 2588 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 2589 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 2590 | // directive. |
| 2591 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2592 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2593 | // initialization of firstprivate variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2594 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 2595 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2596 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2597 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2598 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2599 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2600 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2601 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2602 | EmitSections(S); |
| 2603 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2604 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2605 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2606 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 2607 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2608 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2612 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2613 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2614 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2615 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2616 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 2617 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2618 | } |
| 2619 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2620 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2621 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2622 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2623 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2624 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2625 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2626 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2627 | // Build a list of copyprivate variables along with helper expressions |
| 2628 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2629 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2630 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2631 | DestExprs.append(C->destination_exprs().begin(), |
| 2632 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2633 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2634 | AssignmentOps.append(C->assignment_ops().begin(), |
| 2635 | C->assignment_ops().end()); |
| 2636 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2637 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 2638 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2639 | Action.Enter(CGF); |
| 2640 | OMPPrivateScope SingleScope(CGF); |
| 2641 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 2642 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 2643 | (void)SingleScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2644 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2645 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2646 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2647 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2648 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), |
| 2649 | CopyprivateVars, DestExprs, |
| 2650 | SrcExprs, AssignmentOps); |
| 2651 | } |
| 2652 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 2653 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2654 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 2655 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 2656 | *this, S.getLocStart(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2657 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2658 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2659 | } |
| 2660 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2661 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2662 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2663 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2664 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2665 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2666 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2667 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2668 | } |
| 2669 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2670 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2671 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2672 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2673 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2674 | }; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2675 | Expr *Hint = nullptr; |
| 2676 | if (auto *HintClause = S.getSingleClause<OMPHintClause>()) |
| 2677 | Hint = HintClause->getHint(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2678 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2679 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 2680 | S.getDirectiveName().getAsString(), |
| 2681 | CodeGen, S.getLocStart(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2684 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 2685 | const OMPParallelForDirective &S) { |
| 2686 | // Emit directive as a combined directive that consists of two implicit |
| 2687 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2688 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2689 | Action.Enter(CGF); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2690 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2691 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2692 | emitDispatchForLoopBounds); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2693 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2694 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, |
| 2695 | emitEmptyBoundParameters); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2698 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2699 | const OMPParallelForSimdDirective &S) { |
| 2700 | // Emit directive as a combined directive that consists of two implicit |
| 2701 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2702 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2703 | Action.Enter(CGF); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2704 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2705 | emitDispatchForLoopBounds); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2706 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2707 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, |
| 2708 | emitEmptyBoundParameters); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2711 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2712 | const OMPParallelSectionsDirective &S) { |
| 2713 | // Emit directive as a combined directive that consists of two implicit |
| 2714 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 2715 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2716 | Action.Enter(CGF); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2717 | CGF.EmitSections(S); |
| 2718 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2719 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, |
| 2720 | emitEmptyBoundParameters); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2721 | } |
| 2722 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2723 | void CodeGenFunction::EmitOMPTaskBasedDirective( |
| 2724 | const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, |
| 2725 | const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, |
| 2726 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2727 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2728 | const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2729 | auto *I = CS->getCapturedDecl()->param_begin(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2730 | auto *PartId = std::next(I); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2731 | auto *TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2732 | // Check if the task is final |
| 2733 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 2734 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2735 | // the condition and the dead arm of the if/else. |
| 2736 | auto *Cond = Clause->getCondition(); |
| 2737 | bool CondConstant; |
| 2738 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2739 | Data.Final.setInt(CondConstant); |
| 2740 | else |
| 2741 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2742 | } else { |
| 2743 | // By default the task is not final. |
| 2744 | Data.Final.setInt(/*IntVal=*/false); |
| 2745 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2746 | // Check if the task has 'priority' clause. |
| 2747 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2748 | auto *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 2749 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 2750 | Data.Priority.setPointer(EmitScalarConversion( |
| 2751 | EmitScalarExpr(Prio), Prio->getType(), |
| 2752 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2753 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2754 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2755 | // The first function argument for tasks is a thread id, the second one is a |
| 2756 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2757 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 2758 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2759 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2760 | auto IRef = C->varlist_begin(); |
| 2761 | for (auto *IInit : C->private_copies()) { |
| 2762 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2763 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2764 | Data.PrivateVars.push_back(*IRef); |
| 2765 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2766 | } |
| 2767 | ++IRef; |
| 2768 | } |
| 2769 | } |
| 2770 | EmittedAsPrivate.clear(); |
| 2771 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2772 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2773 | auto IRef = C->varlist_begin(); |
| 2774 | auto IElemInitRef = C->inits().begin(); |
| 2775 | for (auto *IInit : C->private_copies()) { |
| 2776 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2777 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2778 | Data.FirstprivateVars.push_back(*IRef); |
| 2779 | Data.FirstprivateCopies.push_back(IInit); |
| 2780 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2781 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 2782 | ++IRef; |
| 2783 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2784 | } |
| 2785 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2786 | // Get list of lastprivate variables (for taskloops). |
| 2787 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 2788 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 2789 | auto IRef = C->varlist_begin(); |
| 2790 | auto ID = C->destination_exprs().begin(); |
| 2791 | for (auto *IInit : C->private_copies()) { |
| 2792 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2793 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2794 | Data.LastprivateVars.push_back(*IRef); |
| 2795 | Data.LastprivateCopies.push_back(IInit); |
| 2796 | } |
| 2797 | LastprivateDstsOrigs.insert( |
| 2798 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 2799 | cast<DeclRefExpr>(*IRef)}); |
| 2800 | ++IRef; |
| 2801 | ++ID; |
| 2802 | } |
| 2803 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2804 | SmallVector<const Expr *, 4> LHSs; |
| 2805 | SmallVector<const Expr *, 4> RHSs; |
| 2806 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 2807 | auto IPriv = C->privates().begin(); |
| 2808 | auto IRed = C->reduction_ops().begin(); |
| 2809 | auto ILHS = C->lhs_exprs().begin(); |
| 2810 | auto IRHS = C->rhs_exprs().begin(); |
| 2811 | for (const auto *Ref : C->varlists()) { |
| 2812 | Data.ReductionVars.emplace_back(Ref); |
| 2813 | Data.ReductionCopies.emplace_back(*IPriv); |
| 2814 | Data.ReductionOps.emplace_back(*IRed); |
| 2815 | LHSs.emplace_back(*ILHS); |
| 2816 | RHSs.emplace_back(*IRHS); |
| 2817 | std::advance(IPriv, 1); |
| 2818 | std::advance(IRed, 1); |
| 2819 | std::advance(ILHS, 1); |
| 2820 | std::advance(IRHS, 1); |
| 2821 | } |
| 2822 | } |
| 2823 | Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( |
| 2824 | *this, S.getLocStart(), LHSs, RHSs, Data); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2825 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2826 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
| 2827 | for (auto *IRef : C->varlists()) |
| 2828 | Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2829 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, |
| 2830 | CapturedRegion](CodeGenFunction &CGF, |
| 2831 | PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2832 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2833 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2834 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 2835 | !Data.LastprivateVars.empty()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2836 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2837 | auto *CopyFn = CGF.Builder.CreateLoad( |
| 2838 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); |
| 2839 | auto *PrivatesPtr = CGF.Builder.CreateLoad( |
| 2840 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); |
| 2841 | // Map privates. |
| 2842 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 2843 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2844 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2845 | for (auto *E : Data.PrivateVars) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2846 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2847 | Address PrivatePtr = CGF.CreateMemTemp( |
| 2848 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
| 2849 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2850 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2851 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2852 | for (auto *E : Data.FirstprivateVars) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2853 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2854 | Address PrivatePtr = |
| 2855 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2856 | ".firstpriv.ptr.addr"); |
| 2857 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2858 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2859 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2860 | for (auto *E : Data.LastprivateVars) { |
| 2861 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2862 | Address PrivatePtr = |
| 2863 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2864 | ".lastpriv.ptr.addr"); |
| 2865 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2866 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 2867 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2868 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(), |
| 2869 | CopyFn, CallArgs); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2870 | for (auto &&Pair : LastprivateDstsOrigs) { |
| 2871 | auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
| 2872 | DeclRefExpr DRE( |
| 2873 | const_cast<VarDecl *>(OrigVD), |
| 2874 | /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup( |
| 2875 | OrigVD) != nullptr, |
| 2876 | Pair.second->getType(), VK_LValue, Pair.second->getExprLoc()); |
| 2877 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
| 2878 | return CGF.EmitLValue(&DRE).getAddress(); |
| 2879 | }); |
| 2880 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2881 | for (auto &&Pair : PrivatePtrs) { |
| 2882 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2883 | CGF.getContext().getDeclAlign(Pair.first)); |
| 2884 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2885 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2886 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2887 | if (Data.Reductions) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2888 | OMPLexicalScope LexScope(CGF, S, CapturedRegion); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2889 | ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies, |
| 2890 | Data.ReductionOps); |
| 2891 | llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( |
| 2892 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); |
| 2893 | for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { |
| 2894 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2895 | RedCG.emitAggregateType(CGF, Cnt); |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2896 | // FIXME: This must removed once the runtime library is fixed. |
| 2897 | // Emit required threadprivate variables for |
| 2898 | // initilizer/combiner/finalizer. |
| 2899 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(), |
| 2900 | RedCG, Cnt); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2901 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
| 2902 | CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
| 2903 | Replacement = |
| 2904 | Address(CGF.EmitScalarConversion( |
| 2905 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 2906 | CGF.getContext().getPointerType( |
| 2907 | Data.ReductionCopies[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2908 | Data.ReductionCopies[Cnt]->getExprLoc()), |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2909 | Replacement.getAlignment()); |
| 2910 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 2911 | Scope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 2912 | [Replacement]() { return Replacement; }); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2913 | } |
| 2914 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2915 | // Privatize all private variables except for in_reduction items. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2916 | (void)Scope.Privatize(); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2917 | SmallVector<const Expr *, 4> InRedVars; |
| 2918 | SmallVector<const Expr *, 4> InRedPrivs; |
| 2919 | SmallVector<const Expr *, 4> InRedOps; |
| 2920 | SmallVector<const Expr *, 4> TaskgroupDescriptors; |
| 2921 | for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { |
| 2922 | auto IPriv = C->privates().begin(); |
| 2923 | auto IRed = C->reduction_ops().begin(); |
| 2924 | auto ITD = C->taskgroup_descriptors().begin(); |
| 2925 | for (const auto *Ref : C->varlists()) { |
| 2926 | InRedVars.emplace_back(Ref); |
| 2927 | InRedPrivs.emplace_back(*IPriv); |
| 2928 | InRedOps.emplace_back(*IRed); |
| 2929 | TaskgroupDescriptors.emplace_back(*ITD); |
| 2930 | std::advance(IPriv, 1); |
| 2931 | std::advance(IRed, 1); |
| 2932 | std::advance(ITD, 1); |
| 2933 | } |
| 2934 | } |
| 2935 | // Privatize in_reduction items here, because taskgroup descriptors must be |
| 2936 | // privatized earlier. |
| 2937 | OMPPrivateScope InRedScope(CGF); |
| 2938 | if (!InRedVars.empty()) { |
| 2939 | ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps); |
| 2940 | for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { |
| 2941 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2942 | RedCG.emitAggregateType(CGF, Cnt); |
| 2943 | // The taskgroup descriptor variable is always implicit firstprivate and |
| 2944 | // privatized already during procoessing of the firstprivates. |
Alexey Bataev | 2e0cbe50 | 2018-03-08 15:24:08 +0000 | [diff] [blame] | 2945 | // FIXME: This must removed once the runtime library is fixed. |
| 2946 | // Emit required threadprivate variables for |
| 2947 | // initilizer/combiner/finalizer. |
| 2948 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(), |
| 2949 | RedCG, Cnt); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2950 | llvm::Value *ReductionsPtr = |
| 2951 | CGF.EmitLoadOfScalar(CGF.EmitLValue(TaskgroupDescriptors[Cnt]), |
| 2952 | TaskgroupDescriptors[Cnt]->getExprLoc()); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2953 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
| 2954 | CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
| 2955 | Replacement = Address( |
| 2956 | CGF.EmitScalarConversion( |
| 2957 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 2958 | CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2959 | InRedPrivs[Cnt]->getExprLoc()), |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2960 | Replacement.getAlignment()); |
| 2961 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 2962 | InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 2963 | [Replacement]() { return Replacement; }); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | (void)InRedScope.Privatize(); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2967 | |
| 2968 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2969 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2970 | }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2971 | auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 2972 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 2973 | Data.NumberOfParts); |
| 2974 | OMPLexicalScope Scope(*this, S); |
| 2975 | TaskGen(*this, OutlinedFn, Data); |
| 2976 | } |
| 2977 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 2978 | static ImplicitParamDecl * |
| 2979 | createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2980 | QualType Ty, CapturedDecl *CD, |
| 2981 | SourceLocation Loc) { |
| 2982 | auto *OrigVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 2983 | ImplicitParamDecl::Other); |
| 2984 | auto *OrigRef = DeclRefExpr::Create( |
| 2985 | C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, |
| 2986 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
| 2987 | auto *PrivateVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, Ty, |
| 2988 | ImplicitParamDecl::Other); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 2989 | auto *PrivateRef = DeclRefExpr::Create( |
| 2990 | C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2991 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, Ty, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 2992 | QualType ElemType = C.getBaseElementType(Ty); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 2993 | auto *InitVD = ImplicitParamDecl::Create(C, CD, Loc, /*Id=*/nullptr, ElemType, |
| 2994 | ImplicitParamDecl::Other); |
| 2995 | auto *InitRef = DeclRefExpr::Create( |
| 2996 | C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, |
| 2997 | /*RefersToEnclosingVariableOrCapture=*/false, Loc, ElemType, VK_LValue); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 2998 | PrivateVD->setInitStyle(VarDecl::CInit); |
| 2999 | PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, |
| 3000 | InitRef, /*BasePath=*/nullptr, |
| 3001 | VK_RValue)); |
| 3002 | Data.FirstprivateVars.emplace_back(OrigRef); |
| 3003 | Data.FirstprivateCopies.emplace_back(PrivateRef); |
| 3004 | Data.FirstprivateInits.emplace_back(InitRef); |
| 3005 | return OrigVD; |
| 3006 | } |
| 3007 | |
| 3008 | void CodeGenFunction::EmitOMPTargetTaskBasedDirective( |
| 3009 | const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, |
| 3010 | OMPTargetDataInfo &InputInfo) { |
| 3011 | // Emit outlined function for task construct. |
| 3012 | auto CS = S.getCapturedStmt(OMPD_task); |
| 3013 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3014 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3015 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 3016 | auto *PartId = std::next(I); |
| 3017 | auto *TaskT = std::next(I, 4); |
| 3018 | OMPTaskDataTy Data; |
| 3019 | // The task is not final. |
| 3020 | Data.Final.setInt(/*IntVal=*/false); |
| 3021 | // Get list of firstprivate variables. |
| 3022 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 3023 | auto IRef = C->varlist_begin(); |
| 3024 | auto IElemInitRef = C->inits().begin(); |
| 3025 | for (auto *IInit : C->private_copies()) { |
| 3026 | Data.FirstprivateVars.push_back(*IRef); |
| 3027 | Data.FirstprivateCopies.push_back(IInit); |
| 3028 | Data.FirstprivateInits.push_back(*IElemInitRef); |
| 3029 | ++IRef; |
| 3030 | ++IElemInitRef; |
| 3031 | } |
| 3032 | } |
| 3033 | OMPPrivateScope TargetScope(*this); |
| 3034 | VarDecl *BPVD = nullptr; |
| 3035 | VarDecl *PVD = nullptr; |
| 3036 | VarDecl *SVD = nullptr; |
| 3037 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3038 | auto *CD = CapturedDecl::Create( |
| 3039 | getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); |
| 3040 | llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); |
| 3041 | QualType BaseAndPointersType = getContext().getConstantArrayType( |
| 3042 | getContext().VoidPtrTy, ArrSize, ArrayType::Normal, |
| 3043 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3044 | BPVD = createImplicitFirstprivateForType( |
| 3045 | getContext(), Data, BaseAndPointersType, CD, S.getLocStart()); |
| 3046 | PVD = createImplicitFirstprivateForType( |
| 3047 | getContext(), Data, BaseAndPointersType, CD, S.getLocStart()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3048 | QualType SizesType = getContext().getConstantArrayType( |
| 3049 | getContext().getSizeType(), ArrSize, ArrayType::Normal, |
| 3050 | /*IndexTypeQuals=*/0); |
Alexey Bataev | a9b9cc0 | 2018-01-23 18:12:38 +0000 | [diff] [blame] | 3051 | SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD, |
| 3052 | S.getLocStart()); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3053 | TargetScope.addPrivate( |
| 3054 | BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); |
| 3055 | TargetScope.addPrivate(PVD, |
| 3056 | [&InputInfo]() { return InputInfo.PointersArray; }); |
| 3057 | TargetScope.addPrivate(SVD, |
| 3058 | [&InputInfo]() { return InputInfo.SizesArray; }); |
| 3059 | } |
| 3060 | (void)TargetScope.Privatize(); |
| 3061 | // Build list of dependences. |
| 3062 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
| 3063 | for (auto *IRef : C->varlists()) |
| 3064 | Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
| 3065 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, |
| 3066 | &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3067 | // Set proper addresses for generated private copies. |
| 3068 | OMPPrivateScope Scope(CGF); |
| 3069 | if (!Data.FirstprivateVars.empty()) { |
| 3070 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
| 3071 | auto *CopyFn = CGF.Builder.CreateLoad( |
| 3072 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); |
| 3073 | auto *PrivatesPtr = CGF.Builder.CreateLoad( |
| 3074 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); |
| 3075 | // Map privates. |
| 3076 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3077 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3078 | CallArgs.push_back(PrivatesPtr); |
| 3079 | for (auto *E : Data.FirstprivateVars) { |
| 3080 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3081 | Address PrivatePtr = |
| 3082 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3083 | ".firstpriv.ptr.addr"); |
| 3084 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 3085 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3086 | } |
| 3087 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(), |
| 3088 | CopyFn, CallArgs); |
| 3089 | for (auto &&Pair : PrivatePtrs) { |
| 3090 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3091 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3092 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3093 | } |
| 3094 | } |
| 3095 | // Privatize all private variables except for in_reduction items. |
| 3096 | (void)Scope.Privatize(); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3097 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3098 | InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( |
| 3099 | CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0, CGF.getPointerSize()); |
| 3100 | InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( |
| 3101 | CGF.GetAddrOfLocalVar(PVD), /*Index=*/0, CGF.getPointerSize()); |
| 3102 | InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( |
| 3103 | CGF.GetAddrOfLocalVar(SVD), /*Index=*/0, CGF.getSizeSize()); |
| 3104 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3105 | |
| 3106 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3107 | OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3108 | BodyGen(CGF); |
| 3109 | }; |
| 3110 | auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 3111 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, |
| 3112 | Data.NumberOfParts); |
| 3113 | llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); |
| 3114 | IntegerLiteral IfCond(getContext(), TrueOrFalse, |
| 3115 | getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3116 | SourceLocation()); |
| 3117 | |
| 3118 | CGM.getOpenMPRuntime().emitTaskCall(*this, S.getLocStart(), S, OutlinedFn, |
| 3119 | SharedsTy, CapturedStruct, &IfCond, Data); |
| 3120 | } |
| 3121 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3122 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 3123 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3124 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3125 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3126 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3127 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 3128 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3129 | if (C->getNameModifier() == OMPD_unknown || |
| 3130 | C->getNameModifier() == OMPD_task) { |
| 3131 | IfCond = C->getCondition(); |
| 3132 | break; |
| 3133 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3134 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3135 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3136 | OMPTaskDataTy Data; |
| 3137 | // Check if we should emit tied or untied task. |
| 3138 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3139 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3140 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3141 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3142 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3143 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3144 | const OMPTaskDataTy &Data) { |
| 3145 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn, |
| 3146 | SharedsTy, CapturedStruct, IfCond, |
| 3147 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3148 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3149 | EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3152 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 3153 | const OMPTaskyieldDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3154 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3155 | } |
| 3156 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3157 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 3158 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3161 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
| 3162 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3163 | } |
| 3164 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3165 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 3166 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3167 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3168 | Action.Enter(CGF); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3169 | if (const Expr *E = S.getReductionRef()) { |
| 3170 | SmallVector<const Expr *, 4> LHSs; |
| 3171 | SmallVector<const Expr *, 4> RHSs; |
| 3172 | OMPTaskDataTy Data; |
| 3173 | for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { |
| 3174 | auto IPriv = C->privates().begin(); |
| 3175 | auto IRed = C->reduction_ops().begin(); |
| 3176 | auto ILHS = C->lhs_exprs().begin(); |
| 3177 | auto IRHS = C->rhs_exprs().begin(); |
| 3178 | for (const auto *Ref : C->varlists()) { |
| 3179 | Data.ReductionVars.emplace_back(Ref); |
| 3180 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3181 | Data.ReductionOps.emplace_back(*IRed); |
| 3182 | LHSs.emplace_back(*ILHS); |
| 3183 | RHSs.emplace_back(*IRHS); |
| 3184 | std::advance(IPriv, 1); |
| 3185 | std::advance(IRed, 1); |
| 3186 | std::advance(ILHS, 1); |
| 3187 | std::advance(IRHS, 1); |
| 3188 | } |
| 3189 | } |
| 3190 | llvm::Value *ReductionDesc = |
| 3191 | CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getLocStart(), |
| 3192 | LHSs, RHSs, Data); |
| 3193 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3194 | CGF.EmitVarDecl(*VD); |
| 3195 | CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), |
| 3196 | /*Volatile=*/false, E->getType()); |
| 3197 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3198 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3199 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3200 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3201 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); |
| 3202 | } |
| 3203 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3204 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3205 | CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3206 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3207 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 3208 | FlushClause->varlist_end()); |
| 3209 | } |
| 3210 | return llvm::None; |
| 3211 | }(), S.getLocStart()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3214 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, |
| 3215 | const CodeGenLoopTy &CodeGenLoop, |
| 3216 | Expr *IncExpr) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3217 | // Emit the loop iteration variable. |
| 3218 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 3219 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 3220 | EmitVarDecl(*IVDecl); |
| 3221 | |
| 3222 | // Emit the iterations count variable. |
| 3223 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 3224 | // iteration (e.g., it is foldable into a constant). |
| 3225 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 3226 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3227 | // Emit calculation of the iterations count. |
| 3228 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3229 | } |
| 3230 | |
| 3231 | auto &RT = CGM.getOpenMPRuntime(); |
| 3232 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3233 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3234 | // Check pre-condition. |
| 3235 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3236 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3237 | // Skip the entire loop if we don't meet the precondition. |
| 3238 | // If the condition constant folds and can be elided, avoid emitting the |
| 3239 | // whole loop. |
| 3240 | bool CondConstant; |
| 3241 | llvm::BasicBlock *ContBlock = nullptr; |
| 3242 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3243 | if (!CondConstant) |
| 3244 | return; |
| 3245 | } else { |
| 3246 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 3247 | ContBlock = createBasicBlock("omp.precond.end"); |
| 3248 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3249 | getProfileCount(&S)); |
| 3250 | EmitBlock(ThenBlock); |
| 3251 | incrementProfileCounter(&S); |
| 3252 | } |
| 3253 | |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3254 | emitAlignedClause(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3255 | // Emit 'then' code. |
| 3256 | { |
| 3257 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3258 | |
| 3259 | LValue LB = EmitOMPHelperVar( |
| 3260 | *this, cast<DeclRefExpr>( |
| 3261 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3262 | ? S.getCombinedLowerBoundVariable() |
| 3263 | : S.getLowerBoundVariable()))); |
| 3264 | LValue UB = EmitOMPHelperVar( |
| 3265 | *this, cast<DeclRefExpr>( |
| 3266 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3267 | ? S.getCombinedUpperBoundVariable() |
| 3268 | : S.getUpperBoundVariable()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3269 | LValue ST = |
| 3270 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 3271 | LValue IL = |
| 3272 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 3273 | |
| 3274 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3275 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3276 | // Emit implicit barrier to synchronize threads and avoid data races |
| 3277 | // on initialization of firstprivate variables and post-update of |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3278 | // lastprivate variables. |
| 3279 | CGM.getOpenMPRuntime().emitBarrierCall( |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3280 | *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 3281 | /*ForceSimpleCall=*/true); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3282 | } |
| 3283 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3284 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 3285 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3286 | !isOpenMPTeamsDirective(S.getDirectiveKind())) |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3287 | EmitOMPReductionClauseInit(S, LoopScope); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3288 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3289 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3290 | (void)LoopScope.Privatize(); |
| 3291 | |
| 3292 | // Detect the distribute schedule kind and chunk. |
| 3293 | llvm::Value *Chunk = nullptr; |
| 3294 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
| 3295 | if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
| 3296 | ScheduleKind = C->getDistScheduleKind(); |
| 3297 | if (const auto *Ch = C->getChunkSize()) { |
| 3298 | Chunk = EmitScalarExpr(Ch); |
| 3299 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3300 | S.getIterationVariable()->getType(), |
| 3301 | S.getLocStart()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3302 | } |
| 3303 | } |
| 3304 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 3305 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 3306 | |
| 3307 | // OpenMP [2.10.8, distribute Construct, Description] |
| 3308 | // If dist_schedule is specified, kind must be static. If specified, |
| 3309 | // iterations are divided into chunks of size chunk_size, chunks are |
| 3310 | // assigned to the teams of the league in a round-robin fashion in the |
| 3311 | // order of the team number. When no chunk_size is specified, the |
| 3312 | // iteration space is divided into chunks that are approximately equal |
| 3313 | // in size, and at most one chunk is distributed to each team of the |
| 3314 | // league. The size of the chunks is unspecified in this case. |
| 3315 | if (RT.isStaticNonchunked(ScheduleKind, |
| 3316 | /* Chunked */ Chunk != nullptr)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3317 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 3318 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3319 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 3320 | IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(), |
| 3321 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3322 | RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3323 | StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3324 | auto LoopExit = |
| 3325 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 3326 | // UB = min(UB, GlobalUB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3327 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3328 | ? S.getCombinedEnsureUpperBound() |
| 3329 | : S.getEnsureUpperBound()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3330 | // IV = LB; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3331 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3332 | ? S.getCombinedInit() |
| 3333 | : S.getInit()); |
| 3334 | |
| 3335 | Expr *Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3336 | ? S.getCombinedCond() |
| 3337 | : S.getCond(); |
| 3338 | |
| 3339 | // for distribute alone, codegen |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3340 | // while (idx <= UB) { BODY; ++idx; } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3341 | // when combined with 'for' (e.g. as in 'distribute parallel for') |
| 3342 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 3343 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr, |
| 3344 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 3345 | CodeGenLoop(CGF, S, LoopExit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3346 | }, |
| 3347 | [](CodeGenFunction &) {}); |
| 3348 | EmitBlock(LoopExit.getBlock()); |
| 3349 | // Tell the runtime we are done. |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3350 | RT.emitForStaticFinish(*this, S.getLocStart(), S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3351 | } else { |
| 3352 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 3353 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3354 | const OMPLoopArguments LoopArguments = { |
| 3355 | LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(), |
| 3356 | Chunk}; |
| 3357 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, |
| 3358 | CodeGenLoop); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3359 | } |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3360 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 3361 | EmitOMPSimdFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 3362 | return CGF.Builder.CreateIsNotNull( |
| 3363 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 3364 | }); |
| 3365 | } |
Carlo Bertolli | beda214 | 2018-02-22 19:38:14 +0000 | [diff] [blame] | 3366 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 3367 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3368 | !isOpenMPTeamsDirective(S.getDirectiveKind())) { |
| 3369 | OpenMPDirectiveKind ReductionKind = OMPD_unknown; |
| 3370 | if (isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3371 | isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 3372 | ReductionKind = OMPD_parallel_for_simd; |
| 3373 | } else if (isOpenMPParallelDirective(S.getDirectiveKind())) { |
| 3374 | ReductionKind = OMPD_parallel_for; |
| 3375 | } else if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 3376 | ReductionKind = OMPD_simd; |
| 3377 | } else if (!isOpenMPTeamsDirective(S.getDirectiveKind()) && |
| 3378 | S.hasClausesOfKind<OMPReductionClause>()) { |
| 3379 | llvm_unreachable( |
| 3380 | "No reduction clauses is allowed in distribute directive."); |
| 3381 | } |
| 3382 | EmitOMPReductionClauseFinal(S, ReductionKind); |
| 3383 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 3384 | emitPostUpdateForReductionClause( |
| 3385 | *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 3386 | return CGF.Builder.CreateIsNotNull( |
| 3387 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 3388 | }); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3389 | } |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3390 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3391 | if (HasLastprivateClause) { |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3392 | EmitOMPLastprivateClauseFinal( |
| 3393 | S, /*NoFinals=*/false, |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3394 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); |
| 3395 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3396 | } |
| 3397 | |
| 3398 | // We're now done with the loop, so jump to the continuation block. |
| 3399 | if (ContBlock) { |
| 3400 | EmitBranch(ContBlock); |
| 3401 | EmitBlock(ContBlock, true); |
| 3402 | } |
| 3403 | } |
| 3404 | } |
| 3405 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3406 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 3407 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3408 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3409 | |
| 3410 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3411 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3412 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 3413 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3416 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 3417 | const CapturedStmt *S) { |
| 3418 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 3419 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 3420 | CGF.CapturedStmtInfo = &CapStmtInfo; |
| 3421 | auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
Alexey Bataev | c0f879b | 2018-04-10 20:10:53 +0000 | [diff] [blame^] | 3422 | Fn->setDoesNotRecurse(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3423 | return Fn; |
| 3424 | } |
| 3425 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3426 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3427 | if (S.hasClausesOfKind<OMPDependClause>()) { |
| 3428 | assert(!S.getAssociatedStmt() && |
| 3429 | "No associated statement must be in ordered depend construct."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3430 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 3431 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3432 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3433 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3434 | auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3435 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 3436 | PrePostActionTy &Action) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3437 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3438 | if (C) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3439 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3440 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
| 3441 | auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3442 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(), |
| 3443 | OutlinedFn, CapturedVars); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3444 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3445 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3446 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3447 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3448 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3449 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3450 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 3451 | } |
| 3452 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3453 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3454 | QualType SrcType, QualType DestType, |
| 3455 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3456 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 3457 | "DestType must have scalar evaluation kind."); |
| 3458 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
| 3459 | return Val.isScalar() |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3460 | ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType, |
| 3461 | Loc) |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3462 | : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3463 | DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | static CodeGenFunction::ComplexPairTy |
| 3467 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3468 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3469 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 3470 | "DestType must have complex evaluation kind."); |
| 3471 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 3472 | if (Val.isScalar()) { |
| 3473 | // Convert the input element to the element type of the complex. |
| 3474 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3475 | auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 3476 | DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3477 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 3478 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 3479 | } else { |
| 3480 | assert(Val.isComplex() && "Must be a scalar or complex."); |
| 3481 | auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 3482 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
| 3483 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3484 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3485 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3486 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3487 | } |
| 3488 | return ComplexVal; |
| 3489 | } |
| 3490 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3491 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 3492 | LValue LVal, RValue RVal) { |
| 3493 | if (LVal.isGlobalReg()) { |
| 3494 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 3495 | } else { |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3496 | CGF.EmitAtomicStore(RVal, LVal, |
| 3497 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3498 | : llvm::AtomicOrdering::Monotonic, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3499 | LVal.isVolatile(), /*IsInit=*/false); |
| 3500 | } |
| 3501 | } |
| 3502 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3503 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 3504 | QualType RValTy, SourceLocation Loc) { |
| 3505 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3506 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3507 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 3508 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 3509 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3510 | break; |
| 3511 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3512 | EmitStoreOfComplex( |
| 3513 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3514 | /*isInit=*/false); |
| 3515 | break; |
| 3516 | case TEK_Aggregate: |
| 3517 | llvm_unreachable("Must be a scalar or complex."); |
| 3518 | } |
| 3519 | } |
| 3520 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3521 | static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3522 | const Expr *X, const Expr *V, |
| 3523 | SourceLocation Loc) { |
| 3524 | // v = x; |
| 3525 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 3526 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 3527 | LValue XLValue = CGF.EmitLValue(X); |
| 3528 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 3529 | RValue Res = XLValue.isGlobalReg() |
| 3530 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3531 | : CGF.EmitAtomicLoad( |
| 3532 | XLValue, Loc, |
| 3533 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3534 | : llvm::AtomicOrdering::Monotonic, |
| 3535 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3536 | // OpenMP, 2.12.6, atomic Construct |
| 3537 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3538 | // performed operation to include an implicit flush operation without a |
| 3539 | // list. |
| 3540 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3541 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3542 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3545 | static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3546 | const Expr *X, const Expr *E, |
| 3547 | SourceLocation Loc) { |
| 3548 | // x = expr; |
| 3549 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3550 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3551 | // OpenMP, 2.12.6, atomic Construct |
| 3552 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3553 | // performed operation to include an implicit flush operation without a |
| 3554 | // list. |
| 3555 | if (IsSeqCst) |
| 3556 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3557 | } |
| 3558 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3559 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 3560 | RValue Update, |
| 3561 | BinaryOperatorKind BO, |
| 3562 | llvm::AtomicOrdering AO, |
| 3563 | bool IsXLHSInRHSPart) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3564 | auto &Context = CGF.CGM.getContext(); |
| 3565 | // 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] | 3566 | // expression is simple and atomic is allowed for the given type for the |
| 3567 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3568 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 3569 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 3570 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 3571 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3572 | X.getAddress().getElementType())) || |
| 3573 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3574 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 3575 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3576 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3577 | |
| 3578 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 3579 | switch (BO) { |
| 3580 | case BO_Add: |
| 3581 | RMWOp = llvm::AtomicRMWInst::Add; |
| 3582 | break; |
| 3583 | case BO_Sub: |
| 3584 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3585 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3586 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 3587 | break; |
| 3588 | case BO_And: |
| 3589 | RMWOp = llvm::AtomicRMWInst::And; |
| 3590 | break; |
| 3591 | case BO_Or: |
| 3592 | RMWOp = llvm::AtomicRMWInst::Or; |
| 3593 | break; |
| 3594 | case BO_Xor: |
| 3595 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 3596 | break; |
| 3597 | case BO_LT: |
| 3598 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3599 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 3600 | : llvm::AtomicRMWInst::Max) |
| 3601 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 3602 | : llvm::AtomicRMWInst::UMax); |
| 3603 | break; |
| 3604 | case BO_GT: |
| 3605 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3606 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 3607 | : llvm::AtomicRMWInst::Min) |
| 3608 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 3609 | : llvm::AtomicRMWInst::UMin); |
| 3610 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3611 | case BO_Assign: |
| 3612 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 3613 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3614 | case BO_Mul: |
| 3615 | case BO_Div: |
| 3616 | case BO_Rem: |
| 3617 | case BO_Shl: |
| 3618 | case BO_Shr: |
| 3619 | case BO_LAnd: |
| 3620 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3621 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3622 | case BO_PtrMemD: |
| 3623 | case BO_PtrMemI: |
| 3624 | case BO_LE: |
| 3625 | case BO_GE: |
| 3626 | case BO_EQ: |
| 3627 | case BO_NE: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 3628 | case BO_Cmp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3629 | case BO_AddAssign: |
| 3630 | case BO_SubAssign: |
| 3631 | case BO_AndAssign: |
| 3632 | case BO_OrAssign: |
| 3633 | case BO_XorAssign: |
| 3634 | case BO_MulAssign: |
| 3635 | case BO_DivAssign: |
| 3636 | case BO_RemAssign: |
| 3637 | case BO_ShlAssign: |
| 3638 | case BO_ShrAssign: |
| 3639 | case BO_Comma: |
| 3640 | llvm_unreachable("Unsupported atomic update operation"); |
| 3641 | } |
| 3642 | auto *UpdateVal = Update.getScalarVal(); |
| 3643 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 3644 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3645 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3646 | X.getType()->hasSignedIntegerRepresentation()); |
| 3647 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3648 | auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3649 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3650 | } |
| 3651 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3652 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3653 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 3654 | llvm::AtomicOrdering AO, SourceLocation Loc, |
| 3655 | const llvm::function_ref<RValue(RValue)> &CommonGen) { |
| 3656 | // Update expressions are allowed to have the following forms: |
| 3657 | // x binop= expr; -> xrval + expr; |
| 3658 | // x++, ++x -> xrval + 1; |
| 3659 | // x--, --x -> xrval - 1; |
| 3660 | // x = x binop expr; -> xrval binop expr |
| 3661 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3662 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 3663 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3664 | if (X.isGlobalReg()) { |
| 3665 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 3666 | // 'xrval'. |
| 3667 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 3668 | } else { |
| 3669 | // Perform compare-and-swap procedure. |
| 3670 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3671 | } |
| 3672 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3673 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3677 | const Expr *X, const Expr *E, |
| 3678 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3679 | SourceLocation Loc) { |
| 3680 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3681 | "Update expr in 'atomic update' must be a binary operator."); |
| 3682 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 3683 | // Update expressions are allowed to have the following forms: |
| 3684 | // x binop= expr; -> xrval + expr; |
| 3685 | // x++, ++x -> xrval + 1; |
| 3686 | // x--, --x -> xrval - 1; |
| 3687 | // x = x binop expr; -> xrval binop expr |
| 3688 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3689 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3690 | LValue XLValue = CGF.EmitLValue(X); |
| 3691 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3692 | auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3693 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3694 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3695 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3696 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3697 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3698 | auto Gen = |
| 3699 | [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { |
| 3700 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3701 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3702 | return CGF.EmitAnyExpr(UE); |
| 3703 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3704 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3705 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3706 | // OpenMP, 2.12.6, atomic Construct |
| 3707 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3708 | // performed operation to include an implicit flush operation without a |
| 3709 | // list. |
| 3710 | if (IsSeqCst) |
| 3711 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3712 | } |
| 3713 | |
| 3714 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3715 | QualType SourceType, QualType ResType, |
| 3716 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3717 | switch (CGF.getEvaluationKind(ResType)) { |
| 3718 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3719 | return RValue::get( |
| 3720 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3721 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3722 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3723 | return RValue::getComplex(Res.first, Res.second); |
| 3724 | } |
| 3725 | case TEK_Aggregate: |
| 3726 | break; |
| 3727 | } |
| 3728 | llvm_unreachable("Must be a scalar or complex."); |
| 3729 | } |
| 3730 | |
| 3731 | static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3732 | bool IsPostfixUpdate, const Expr *V, |
| 3733 | const Expr *X, const Expr *E, |
| 3734 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3735 | SourceLocation Loc) { |
| 3736 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 3737 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 3738 | RValue NewVVal; |
| 3739 | LValue VLValue = CGF.EmitLValue(V); |
| 3740 | LValue XLValue = CGF.EmitLValue(X); |
| 3741 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3742 | auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3743 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3744 | QualType NewVValType; |
| 3745 | if (UE) { |
| 3746 | // 'x' is updated with some additional value. |
| 3747 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3748 | "Update expr in 'atomic capture' must be a binary operator."); |
| 3749 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 3750 | // Update expressions are allowed to have the following forms: |
| 3751 | // x binop= expr; -> xrval + expr; |
| 3752 | // x++, ++x -> xrval + 1; |
| 3753 | // x--, --x -> xrval - 1; |
| 3754 | // x = x binop expr; -> xrval binop expr |
| 3755 | // x = expr Op x; - > expr binop xrval; |
| 3756 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3757 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3758 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3759 | NewVValType = XRValExpr->getType(); |
| 3760 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3761 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 3762 | IsPostfixUpdate](RValue XRValue) -> RValue { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3763 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3764 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3765 | RValue Res = CGF.EmitAnyExpr(UE); |
| 3766 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 3767 | return Res; |
| 3768 | }; |
| 3769 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3770 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3771 | if (Res.first) { |
| 3772 | // 'atomicrmw' instruction was generated. |
| 3773 | if (IsPostfixUpdate) { |
| 3774 | // Use old value from 'atomicrmw'. |
| 3775 | NewVVal = Res.second; |
| 3776 | } else { |
| 3777 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 3778 | // value of 'x'. |
| 3779 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3780 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 3781 | NewVVal = CGF.EmitAnyExpr(UE); |
| 3782 | } |
| 3783 | } |
| 3784 | } else { |
| 3785 | // 'x' is simply rewritten with some 'expr'. |
| 3786 | NewVValType = X->getType().getNonReferenceType(); |
| 3787 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3788 | X->getType().getNonReferenceType(), Loc); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 3789 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) -> RValue { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3790 | NewVVal = XRValue; |
| 3791 | return ExprRValue; |
| 3792 | }; |
| 3793 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 3794 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3795 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 3796 | Loc, Gen); |
| 3797 | if (Res.first) { |
| 3798 | // 'atomicrmw' instruction was generated. |
| 3799 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 3800 | } |
| 3801 | } |
| 3802 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3803 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3804 | // OpenMP, 2.12.6, atomic Construct |
| 3805 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3806 | // performed operation to include an implicit flush operation without a |
| 3807 | // list. |
| 3808 | if (IsSeqCst) |
| 3809 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3810 | } |
| 3811 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3812 | static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3813 | bool IsSeqCst, bool IsPostfixUpdate, |
| 3814 | const Expr *X, const Expr *V, const Expr *E, |
| 3815 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3816 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3817 | switch (Kind) { |
| 3818 | case OMPC_read: |
| 3819 | EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
| 3820 | break; |
| 3821 | case OMPC_write: |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3822 | EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
| 3823 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3824 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3825 | case OMPC_update: |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3826 | EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
| 3827 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3828 | case OMPC_capture: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3829 | EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
| 3830 | IsXLHSInRHSPart, Loc); |
| 3831 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3832 | case OMPC_if: |
| 3833 | case OMPC_final: |
| 3834 | case OMPC_num_threads: |
| 3835 | case OMPC_private: |
| 3836 | case OMPC_firstprivate: |
| 3837 | case OMPC_lastprivate: |
| 3838 | case OMPC_reduction: |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 3839 | case OMPC_task_reduction: |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 3840 | case OMPC_in_reduction: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3841 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 3842 | case OMPC_simdlen: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3843 | case OMPC_collapse: |
| 3844 | case OMPC_default: |
| 3845 | case OMPC_seq_cst: |
| 3846 | case OMPC_shared: |
| 3847 | case OMPC_linear: |
| 3848 | case OMPC_aligned: |
| 3849 | case OMPC_copyin: |
| 3850 | case OMPC_copyprivate: |
| 3851 | case OMPC_flush: |
| 3852 | case OMPC_proc_bind: |
| 3853 | case OMPC_schedule: |
| 3854 | case OMPC_ordered: |
| 3855 | case OMPC_nowait: |
| 3856 | case OMPC_untied: |
| 3857 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 3858 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3859 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 3860 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 3861 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 3862 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 3863 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 3864 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 3865 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 3866 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 3867 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 3868 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 3869 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 3870 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 3871 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 3872 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 3873 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 3874 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 3875 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 3876 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 3877 | case OMPC_is_device_ptr: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3878 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 3879 | } |
| 3880 | } |
| 3881 | |
| 3882 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3883 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3884 | OpenMPClauseKind Kind = OMPC_unknown; |
| 3885 | for (auto *C : S.clauses()) { |
| 3886 | // Find first clause (skip seq_cst clause, if it is first). |
| 3887 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 3888 | Kind = C->getClauseKind(); |
| 3889 | break; |
| 3890 | } |
| 3891 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3892 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3893 | const auto *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3894 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3895 | enterFullExpression(EWC); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3896 | } |
| 3897 | // Processing for statements under 'atomic capture'. |
| 3898 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
| 3899 | for (const auto *C : Compound->body()) { |
| 3900 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { |
| 3901 | enterFullExpression(EWC); |
| 3902 | } |
| 3903 | } |
| 3904 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3905 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3906 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, |
| 3907 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 3908 | CGF.EmitStopPoint(CS); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3909 | EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
| 3910 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
| 3911 | S.isXLHSInRHSPart(), S.getLocStart()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3912 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3913 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3914 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 3915 | } |
| 3916 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3917 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 3918 | const OMPExecutableDirective &S, |
| 3919 | const RegionCodeGenTy &CodeGen) { |
| 3920 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 3921 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3922 | |
Alexey Bataev | 4f4bf7c | 2018-03-15 15:47:20 +0000 | [diff] [blame] | 3923 | // On device emit this construct as inlined code. |
| 3924 | if (CGM.getLangOpts().OpenMPIsDevice) { |
| 3925 | OMPLexicalScope Scope(CGF, S, OMPD_target); |
| 3926 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 3927 | CGF, OMPD_target, [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3928 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
| 3929 | }); |
| 3930 | return; |
| 3931 | } |
| 3932 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3933 | llvm::Function *Fn = nullptr; |
| 3934 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3935 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3936 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 3937 | // Check for the at most one if clause associated with the target region. |
| 3938 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3939 | if (C->getNameModifier() == OMPD_unknown || |
| 3940 | C->getNameModifier() == OMPD_target) { |
| 3941 | IfCond = C->getCondition(); |
| 3942 | break; |
| 3943 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3944 | } |
| 3945 | |
| 3946 | // Check if we have any device clause associated with the directive. |
| 3947 | const Expr *Device = nullptr; |
| 3948 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) { |
| 3949 | Device = C->getDevice(); |
| 3950 | } |
| 3951 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3952 | // Check if we have an if clause whose conditional always evaluates to false |
| 3953 | // or if we do not have any targets specified. If so the target region is not |
| 3954 | // an offload entry point. |
| 3955 | bool IsOffloadEntry = true; |
| 3956 | if (IfCond) { |
| 3957 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3958 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3959 | IsOffloadEntry = false; |
| 3960 | } |
| 3961 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 3962 | IsOffloadEntry = false; |
| 3963 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3964 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3965 | StringRef ParentName; |
| 3966 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 3967 | // the mangling of the device outlined kernel. |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3968 | if (auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3969 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3970 | else if (auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3971 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 3972 | else |
| 3973 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3974 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3975 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3976 | // Emit target region as a standalone region. |
| 3977 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 3978 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame] | 3979 | OMPLexicalScope Scope(CGF, S, OMPD_task); |
| 3980 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3983 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 3984 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 3985 | Action.Enter(CGF); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3986 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 3987 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 3988 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 3989 | (void)PrivateScope.Privatize(); |
| 3990 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3991 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 3995 | StringRef ParentName, |
| 3996 | const OMPTargetDirective &S) { |
| 3997 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3998 | emitTargetRegion(CGF, S, Action); |
| 3999 | }; |
| 4000 | llvm::Function *Fn; |
| 4001 | llvm::Constant *Addr; |
| 4002 | // Emit target region as a standalone region. |
| 4003 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4004 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4005 | assert(Fn && Addr && "Target device function emission failed."); |
| 4006 | } |
| 4007 | |
| 4008 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 4009 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4010 | emitTargetRegion(CGF, S, Action); |
| 4011 | }; |
| 4012 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4013 | } |
| 4014 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4015 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 4016 | const OMPExecutableDirective &S, |
| 4017 | OpenMPDirectiveKind InnermostKind, |
| 4018 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4019 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
| 4020 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 4021 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 4022 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4023 | const OMPNumTeamsClause *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 4024 | const OMPThreadLimitClause *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4025 | if (NT || TL) { |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4026 | Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr; |
| 4027 | Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4028 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4029 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
| 4030 | S.getLocStart()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4031 | } |
| 4032 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4033 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4034 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4035 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4036 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn, |
| 4037 | CapturedVars); |
| 4038 | } |
| 4039 | |
| 4040 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 4041 | // Emit teams region as a standalone region. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4042 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4043 | Action.Enter(CGF); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4044 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 4045 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4046 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4047 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4048 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4049 | CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4050 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4051 | }; |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4052 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4053 | emitPostUpdateForReductionClause( |
| 4054 | *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4055 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4056 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4057 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4058 | const OMPTargetTeamsDirective &S) { |
| 4059 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 4060 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4061 | // Emit teams region as a standalone region. |
| 4062 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4063 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4064 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4065 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4066 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4067 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4068 | (void)PrivateScope.Privatize(); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4069 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4070 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4071 | }; |
| 4072 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4073 | emitPostUpdateForReductionClause( |
| 4074 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
| 4077 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 4078 | CodeGenModule &CGM, StringRef ParentName, |
| 4079 | const OMPTargetTeamsDirective &S) { |
| 4080 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4081 | emitTargetTeamsRegion(CGF, Action, S); |
| 4082 | }; |
| 4083 | llvm::Function *Fn; |
| 4084 | llvm::Constant *Addr; |
| 4085 | // Emit target region as a standalone region. |
| 4086 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4087 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4088 | assert(Fn && Addr && "Target device function emission failed."); |
| 4089 | } |
| 4090 | |
| 4091 | void CodeGenFunction::EmitOMPTargetTeamsDirective( |
| 4092 | const OMPTargetTeamsDirective &S) { |
| 4093 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4094 | emitTargetTeamsRegion(CGF, Action, S); |
| 4095 | }; |
| 4096 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4097 | } |
| 4098 | |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4099 | static void |
| 4100 | emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4101 | const OMPTargetTeamsDistributeDirective &S) { |
| 4102 | Action.Enter(CGF); |
| 4103 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4104 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4105 | }; |
| 4106 | |
| 4107 | // Emit teams region as a standalone region. |
| 4108 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4109 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4110 | Action.Enter(CGF); |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4111 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4112 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4113 | (void)PrivateScope.Privatize(); |
| 4114 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4115 | CodeGenDistribute); |
| 4116 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4117 | }; |
| 4118 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); |
| 4119 | emitPostUpdateForReductionClause(CGF, S, |
| 4120 | [](CodeGenFunction &) { return nullptr; }); |
| 4121 | } |
| 4122 | |
| 4123 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| 4124 | CodeGenModule &CGM, StringRef ParentName, |
| 4125 | const OMPTargetTeamsDistributeDirective &S) { |
| 4126 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4127 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4128 | }; |
| 4129 | llvm::Function *Fn; |
| 4130 | llvm::Constant *Addr; |
| 4131 | // Emit target region as a standalone region. |
| 4132 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4133 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4134 | assert(Fn && Addr && "Target device function emission failed."); |
| 4135 | } |
| 4136 | |
| 4137 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 4138 | const OMPTargetTeamsDistributeDirective &S) { |
| 4139 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4140 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4141 | }; |
| 4142 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4143 | } |
| 4144 | |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4145 | static void emitTargetTeamsDistributeSimdRegion( |
| 4146 | CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4147 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4148 | Action.Enter(CGF); |
| 4149 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4150 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4151 | }; |
| 4152 | |
| 4153 | // Emit teams region as a standalone region. |
| 4154 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4155 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4156 | Action.Enter(CGF); |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4157 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4158 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4159 | (void)PrivateScope.Privatize(); |
| 4160 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4161 | CodeGenDistribute); |
| 4162 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4163 | }; |
| 4164 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); |
| 4165 | emitPostUpdateForReductionClause(CGF, S, |
| 4166 | [](CodeGenFunction &) { return nullptr; }); |
| 4167 | } |
| 4168 | |
| 4169 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| 4170 | CodeGenModule &CGM, StringRef ParentName, |
| 4171 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4172 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4173 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4174 | }; |
| 4175 | llvm::Function *Fn; |
| 4176 | llvm::Constant *Addr; |
| 4177 | // Emit target region as a standalone region. |
| 4178 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4179 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4180 | assert(Fn && Addr && "Target device function emission failed."); |
| 4181 | } |
| 4182 | |
| 4183 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 4184 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4185 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4186 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4187 | }; |
| 4188 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4189 | } |
| 4190 | |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4191 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 4192 | const OMPTeamsDistributeDirective &S) { |
| 4193 | |
| 4194 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4195 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4196 | }; |
| 4197 | |
| 4198 | // Emit teams region as a standalone region. |
| 4199 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4200 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4201 | Action.Enter(CGF); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4202 | OMPPrivateScope PrivateScope(CGF); |
| 4203 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4204 | (void)PrivateScope.Privatize(); |
| 4205 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4206 | CodeGenDistribute); |
| 4207 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4208 | }; |
Alexey Bataev | 95c6dd4 | 2017-11-29 15:14:16 +0000 | [diff] [blame] | 4209 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4210 | emitPostUpdateForReductionClause(*this, S, |
| 4211 | [](CodeGenFunction &) { return nullptr; }); |
| 4212 | } |
| 4213 | |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4214 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 4215 | const OMPTeamsDistributeSimdDirective &S) { |
| 4216 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4217 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4218 | }; |
| 4219 | |
| 4220 | // Emit teams region as a standalone region. |
| 4221 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4222 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4223 | Action.Enter(CGF); |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4224 | OMPPrivateScope PrivateScope(CGF); |
| 4225 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4226 | (void)PrivateScope.Privatize(); |
| 4227 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, |
| 4228 | CodeGenDistribute); |
| 4229 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4230 | }; |
| 4231 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); |
| 4232 | emitPostUpdateForReductionClause(*this, S, |
| 4233 | [](CodeGenFunction &) { return nullptr; }); |
| 4234 | } |
| 4235 | |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4236 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 4237 | const OMPTeamsDistributeParallelForDirective &S) { |
| 4238 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4239 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4240 | S.getDistInc()); |
| 4241 | }; |
| 4242 | |
| 4243 | // Emit teams region as a standalone region. |
| 4244 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4245 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4246 | Action.Enter(CGF); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4247 | OMPPrivateScope PrivateScope(CGF); |
| 4248 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4249 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 4250 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4251 | CodeGenDistribute); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4252 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4253 | }; |
| 4254 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4255 | emitPostUpdateForReductionClause(*this, S, |
| 4256 | [](CodeGenFunction &) { return nullptr; }); |
| 4257 | } |
| 4258 | |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4259 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 4260 | const OMPTeamsDistributeParallelForSimdDirective &S) { |
| 4261 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4262 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4263 | S.getDistInc()); |
| 4264 | }; |
| 4265 | |
| 4266 | // Emit teams region as a standalone region. |
| 4267 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4268 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4269 | Action.Enter(CGF); |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4270 | OMPPrivateScope PrivateScope(CGF); |
| 4271 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4272 | (void)PrivateScope.Privatize(); |
| 4273 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4274 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4275 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4276 | }; |
| 4277 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4278 | emitPostUpdateForReductionClause(*this, S, |
| 4279 | [](CodeGenFunction &) { return nullptr; }); |
| 4280 | } |
| 4281 | |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4282 | static void emitTargetTeamsDistributeParallelForRegion( |
| 4283 | CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, |
| 4284 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4285 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4286 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4287 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4288 | S.getDistInc()); |
| 4289 | }; |
| 4290 | |
| 4291 | // Emit teams region as a standalone region. |
| 4292 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4293 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4294 | Action.Enter(CGF); |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4295 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4296 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4297 | (void)PrivateScope.Privatize(); |
| 4298 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4299 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4300 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4301 | }; |
| 4302 | |
| 4303 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, |
| 4304 | CodeGenTeams); |
| 4305 | emitPostUpdateForReductionClause(CGF, S, |
| 4306 | [](CodeGenFunction &) { return nullptr; }); |
| 4307 | } |
| 4308 | |
| 4309 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 4310 | CodeGenModule &CGM, StringRef ParentName, |
| 4311 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4312 | // Emit SPMD target teams distribute parallel for region as a standalone |
| 4313 | // region. |
| 4314 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4315 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4316 | }; |
| 4317 | llvm::Function *Fn; |
| 4318 | llvm::Constant *Addr; |
| 4319 | // Emit target region as a standalone region. |
| 4320 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4321 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4322 | assert(Fn && Addr && "Target device function emission failed."); |
| 4323 | } |
| 4324 | |
| 4325 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 4326 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4327 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4328 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4329 | }; |
| 4330 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4331 | } |
| 4332 | |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4333 | static void emitTargetTeamsDistributeParallelForSimdRegion( |
| 4334 | CodeGenFunction &CGF, |
| 4335 | const OMPTargetTeamsDistributeParallelForSimdDirective &S, |
| 4336 | PrePostActionTy &Action) { |
Carlo Bertolli | 7971209 | 2018-02-28 20:48:35 +0000 | [diff] [blame] | 4337 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4338 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4339 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4340 | S.getDistInc()); |
| 4341 | }; |
| 4342 | |
| 4343 | // Emit teams region as a standalone region. |
| 4344 | auto &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4345 | PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4346 | Action.Enter(CGF); |
Alexey Bataev | 647dd84 | 2018-01-15 20:59:40 +0000 | [diff] [blame] | 4347 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4348 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4349 | (void)PrivateScope.Privatize(); |
| 4350 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4351 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4352 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4353 | }; |
| 4354 | |
| 4355 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for_simd, |
| 4356 | CodeGenTeams); |
| 4357 | emitPostUpdateForReductionClause(CGF, S, |
| 4358 | [](CodeGenFunction &) { return nullptr; }); |
| 4359 | } |
| 4360 | |
| 4361 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDeviceFunction( |
| 4362 | CodeGenModule &CGM, StringRef ParentName, |
| 4363 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4364 | // Emit SPMD target teams distribute parallel for simd region as a standalone |
| 4365 | // region. |
| 4366 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4367 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4368 | }; |
| 4369 | llvm::Function *Fn; |
| 4370 | llvm::Constant *Addr; |
| 4371 | // Emit target region as a standalone region. |
| 4372 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4373 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4374 | assert(Fn && Addr && "Target device function emission failed."); |
| 4375 | } |
| 4376 | |
| 4377 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 4378 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
| 4379 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4380 | emitTargetTeamsDistributeParallelForSimdRegion(CGF, S, Action); |
| 4381 | }; |
| 4382 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4383 | } |
| 4384 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4385 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 4386 | const OMPCancellationPointDirective &S) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 4387 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), |
| 4388 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4389 | } |
| 4390 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4391 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 4392 | const Expr *IfCond = nullptr; |
| 4393 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4394 | if (C->getNameModifier() == OMPD_unknown || |
| 4395 | C->getNameModifier() == OMPD_cancel) { |
| 4396 | IfCond = C->getCondition(); |
| 4397 | break; |
| 4398 | } |
| 4399 | } |
| 4400 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 4401 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4402 | } |
| 4403 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4404 | CodeGenFunction::JumpDest |
| 4405 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4406 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
| 4407 | Kind == OMPD_target_parallel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4408 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4409 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4410 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 4411 | Kind == OMPD_distribute_parallel_for || |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 4412 | Kind == OMPD_target_parallel_for || |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 4413 | Kind == OMPD_teams_distribute_parallel_for || |
| 4414 | Kind == OMPD_target_teams_distribute_parallel_for); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4415 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4416 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4417 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4418 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 4419 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 4420 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 4421 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 4422 | auto OrigVarIt = C.varlist_begin(); |
| 4423 | auto InitIt = C.inits().begin(); |
| 4424 | for (auto PvtVarIt : C.private_copies()) { |
| 4425 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 4426 | auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 4427 | auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
| 4428 | |
| 4429 | // In order to identify the right initializer we need to match the |
| 4430 | // declaration used by the mapping logic. In some cases we may get |
| 4431 | // OMPCapturedExprDecl that refers to the original declaration. |
| 4432 | const ValueDecl *MatchingVD = OrigVD; |
| 4433 | if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
| 4434 | // OMPCapturedExprDecl are used to privative fields of the current |
| 4435 | // structure. |
| 4436 | auto *ME = cast<MemberExpr>(OED->getInit()); |
| 4437 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 4438 | "Base should be the current struct!"); |
| 4439 | MatchingVD = ME->getMemberDecl(); |
| 4440 | } |
| 4441 | |
| 4442 | // If we don't have information about the current list item, move on to |
| 4443 | // the next one. |
| 4444 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 4445 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 4446 | continue; |
| 4447 | |
| 4448 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 4449 | // Initialize the temporary initialization variable with the address we |
| 4450 | // get from the runtime library. We have to cast the source address |
| 4451 | // because it is always a void *. References are materialized in the |
| 4452 | // privatization scope, so the initialization here disregards the fact |
| 4453 | // the original variable is a reference. |
| 4454 | QualType AddrQTy = |
| 4455 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 4456 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 4457 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 4458 | setAddrOfLocalVar(InitVD, InitAddr); |
| 4459 | |
| 4460 | // Emit private declaration, it will be initialized by the value we |
| 4461 | // declaration we just added to the local declarations map. |
| 4462 | EmitDecl(*PvtVD); |
| 4463 | |
| 4464 | // The initialization variables reached its purpose in the emission |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4465 | // of the previous declaration, so we don't need it anymore. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4466 | LocalDeclMap.erase(InitVD); |
| 4467 | |
| 4468 | // Return the address of the private variable. |
| 4469 | return GetAddrOfLocalVar(PvtVD); |
| 4470 | }); |
| 4471 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 4472 | // Silence the warning about unused variable. |
| 4473 | (void)IsRegistered; |
| 4474 | |
| 4475 | ++OrigVarIt; |
| 4476 | ++InitIt; |
| 4477 | } |
| 4478 | } |
| 4479 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4480 | // Generate the instructions for '#pragma omp target data' directive. |
| 4481 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 4482 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4483 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 4484 | |
| 4485 | // Create a pre/post action to signal the privatization of the device pointer. |
| 4486 | // This action can be replaced by the OpenMP runtime code generation to |
| 4487 | // deactivate privatization. |
| 4488 | bool PrivatizeDevicePointers = false; |
| 4489 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 4490 | bool &PrivatizeDevicePointers; |
| 4491 | |
| 4492 | public: |
| 4493 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 4494 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 4495 | void Enter(CodeGenFunction &CGF) override { |
| 4496 | PrivatizeDevicePointers = true; |
| 4497 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4498 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4499 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 4500 | |
| 4501 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4502 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4503 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4504 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4505 | }; |
| 4506 | |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 4507 | // Codegen that selects whether to generate the privatization code or not. |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4508 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 4509 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 4510 | PrePostActionTy &Action) { |
| 4511 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 4512 | PrivatizeDevicePointers = false; |
| 4513 | |
| 4514 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 4515 | // needed. |
| 4516 | Action.Enter(CGF); |
| 4517 | |
| 4518 | if (PrivatizeDevicePointers) { |
| 4519 | OMPPrivateScope PrivateScope(CGF); |
| 4520 | // Emit all instances of the use_device_ptr clause. |
| 4521 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 4522 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 4523 | Info.CaptureDeviceAddrMap); |
| 4524 | (void)PrivateScope.Privatize(); |
| 4525 | RCG(CGF); |
| 4526 | } else |
| 4527 | RCG(CGF); |
| 4528 | }; |
| 4529 | |
| 4530 | // Forward the provided action to the privatization codegen. |
| 4531 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 4532 | PrivRCG.setAction(Action); |
| 4533 | |
| 4534 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 4535 | // we don't use an inline scope as changes in the references inside the |
| 4536 | // region are expected to be visible outside, so we do not privative them. |
| 4537 | OMPLexicalScope Scope(CGF, S); |
| 4538 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 4539 | PrivRCG); |
| 4540 | }; |
| 4541 | |
| 4542 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4543 | |
| 4544 | // If we don't have target devices, don't bother emitting the data mapping |
| 4545 | // code. |
| 4546 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4547 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4548 | return; |
| 4549 | } |
| 4550 | |
| 4551 | // Check if we have any if clause associated with the directive. |
| 4552 | const Expr *IfCond = nullptr; |
| 4553 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4554 | IfCond = C->getCondition(); |
| 4555 | |
| 4556 | // Check if we have any device clause associated with the directive. |
| 4557 | const Expr *Device = nullptr; |
| 4558 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4559 | Device = C->getDevice(); |
| 4560 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4561 | // Set the action to signal privatization of device pointers. |
| 4562 | RCG.setAction(PrivAction); |
| 4563 | |
| 4564 | // Emit region code. |
| 4565 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 4566 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4567 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4568 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4569 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 4570 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4571 | // If we don't have target devices, don't bother emitting the data mapping |
| 4572 | // code. |
| 4573 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4574 | return; |
| 4575 | |
| 4576 | // Check if we have any if clause associated with the directive. |
| 4577 | const Expr *IfCond = nullptr; |
| 4578 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4579 | IfCond = C->getCondition(); |
| 4580 | |
| 4581 | // Check if we have any device clause associated with the directive. |
| 4582 | const Expr *Device = nullptr; |
| 4583 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4584 | Device = C->getDevice(); |
| 4585 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4586 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4587 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4590 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 4591 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4592 | // If we don't have target devices, don't bother emitting the data mapping |
| 4593 | // code. |
| 4594 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4595 | return; |
| 4596 | |
| 4597 | // Check if we have any if clause associated with the directive. |
| 4598 | const Expr *IfCond = nullptr; |
| 4599 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4600 | IfCond = C->getCondition(); |
| 4601 | |
| 4602 | // Check if we have any device clause associated with the directive. |
| 4603 | const Expr *Device = nullptr; |
| 4604 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4605 | Device = C->getDevice(); |
| 4606 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4607 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4608 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4611 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 4612 | const OMPTargetParallelDirective &S, |
| 4613 | PrePostActionTy &Action) { |
| 4614 | // Get the captured statement associated with the 'parallel' region. |
| 4615 | auto *CS = S.getCapturedStmt(OMPD_parallel); |
| 4616 | Action.Enter(CGF); |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4617 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
Alexey Bataev | 63cc8e9 | 2018-03-20 14:45:59 +0000 | [diff] [blame] | 4618 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4619 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4620 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4621 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4622 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4623 | (void)PrivateScope.Privatize(); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4624 | // TODO: Add support for clauses. |
| 4625 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4626 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4627 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4628 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, |
| 4629 | emitEmptyBoundParameters); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4630 | emitPostUpdateForReductionClause( |
| 4631 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 4635 | CodeGenModule &CGM, StringRef ParentName, |
| 4636 | const OMPTargetParallelDirective &S) { |
| 4637 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4638 | emitTargetParallelRegion(CGF, S, Action); |
| 4639 | }; |
| 4640 | llvm::Function *Fn; |
| 4641 | llvm::Constant *Addr; |
| 4642 | // Emit target region as a standalone region. |
| 4643 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4644 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4645 | assert(Fn && Addr && "Target device function emission failed."); |
| 4646 | } |
| 4647 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4648 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 4649 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4650 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4651 | emitTargetParallelRegion(CGF, S, Action); |
| 4652 | }; |
| 4653 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4656 | static void emitTargetParallelForRegion(CodeGenFunction &CGF, |
| 4657 | const OMPTargetParallelForDirective &S, |
| 4658 | PrePostActionTy &Action) { |
| 4659 | Action.Enter(CGF); |
| 4660 | // Emit directive as a combined directive that consists of two implicit |
| 4661 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4662 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4663 | Action.Enter(CGF); |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4664 | CodeGenFunction::OMPCancelStackRAII CancelRegion( |
| 4665 | CGF, OMPD_target_parallel_for, S.hasCancel()); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4666 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4667 | emitDispatchForLoopBounds); |
| 4668 | }; |
| 4669 | emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, |
| 4670 | emitEmptyBoundParameters); |
| 4671 | } |
| 4672 | |
| 4673 | void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| 4674 | CodeGenModule &CGM, StringRef ParentName, |
| 4675 | const OMPTargetParallelForDirective &S) { |
| 4676 | // Emit SPMD target parallel for region as a standalone region. |
| 4677 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4678 | emitTargetParallelForRegion(CGF, S, Action); |
| 4679 | }; |
| 4680 | llvm::Function *Fn; |
| 4681 | llvm::Constant *Addr; |
| 4682 | // Emit target region as a standalone region. |
| 4683 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4684 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4685 | assert(Fn && Addr && "Target device function emission failed."); |
| 4686 | } |
| 4687 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4688 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 4689 | const OMPTargetParallelForDirective &S) { |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4690 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4691 | emitTargetParallelForRegion(CGF, S, Action); |
| 4692 | }; |
| 4693 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4694 | } |
| 4695 | |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4696 | static void |
| 4697 | emitTargetParallelForSimdRegion(CodeGenFunction &CGF, |
| 4698 | const OMPTargetParallelForSimdDirective &S, |
| 4699 | PrePostActionTy &Action) { |
| 4700 | Action.Enter(CGF); |
| 4701 | // Emit directive as a combined directive that consists of two implicit |
| 4702 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | c99042b | 2018-03-15 18:10:54 +0000 | [diff] [blame] | 4703 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4704 | Action.Enter(CGF); |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4705 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4706 | emitDispatchForLoopBounds); |
| 4707 | }; |
| 4708 | emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, |
| 4709 | emitEmptyBoundParameters); |
| 4710 | } |
| 4711 | |
| 4712 | void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| 4713 | CodeGenModule &CGM, StringRef ParentName, |
| 4714 | const OMPTargetParallelForSimdDirective &S) { |
| 4715 | // Emit SPMD target parallel for region as a standalone region. |
| 4716 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4717 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4718 | }; |
| 4719 | llvm::Function *Fn; |
| 4720 | llvm::Constant *Addr; |
| 4721 | // Emit target region as a standalone region. |
| 4722 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4723 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4724 | assert(Fn && Addr && "Target device function emission failed."); |
| 4725 | } |
| 4726 | |
| 4727 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 4728 | const OMPTargetParallelForSimdDirective &S) { |
| 4729 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4730 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4731 | }; |
| 4732 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4733 | } |
| 4734 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4735 | /// Emit a helper variable and return corresponding lvalue. |
| 4736 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 4737 | const ImplicitParamDecl *PVD, |
| 4738 | CodeGenFunction::OMPPrivateScope &Privates) { |
| 4739 | auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 4740 | Privates.addPrivate( |
| 4741 | VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); }); |
| 4742 | } |
| 4743 | |
| 4744 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 4745 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 4746 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4747 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4748 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 4749 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 4750 | const Expr *IfCond = nullptr; |
| 4751 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4752 | if (C->getNameModifier() == OMPD_unknown || |
| 4753 | C->getNameModifier() == OMPD_taskloop) { |
| 4754 | IfCond = C->getCondition(); |
| 4755 | break; |
| 4756 | } |
| 4757 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4758 | |
| 4759 | OMPTaskDataTy Data; |
| 4760 | // Check if taskloop must be emitted without taskgroup. |
| 4761 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4762 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4763 | Data.Tied = true; |
| 4764 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4765 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 4766 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4767 | Data.Schedule.setInt(/*IntVal=*/false); |
| 4768 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4769 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 4770 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4771 | Data.Schedule.setInt(/*IntVal=*/true); |
| 4772 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4773 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4774 | |
| 4775 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4776 | // if (PreCond) { |
| 4777 | // for (IV in 0..LastIteration) BODY; |
| 4778 | // <Final counter/linear vars updates>; |
| 4779 | // } |
| 4780 | // |
| 4781 | |
| 4782 | // Emit: if (PreCond) - begin. |
| 4783 | // If the condition constant folds and can be elided, avoid emitting the |
| 4784 | // whole loop. |
| 4785 | bool CondConstant; |
| 4786 | llvm::BasicBlock *ContBlock = nullptr; |
| 4787 | OMPLoopScope PreInitScope(CGF, S); |
| 4788 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 4789 | if (!CondConstant) |
| 4790 | return; |
| 4791 | } else { |
| 4792 | auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
| 4793 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 4794 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 4795 | CGF.getProfileCount(&S)); |
| 4796 | CGF.EmitBlock(ThenBlock); |
| 4797 | CGF.incrementProfileCounter(&S); |
| 4798 | } |
| 4799 | |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4800 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 4801 | CGF.EmitOMPSimdInit(S); |
| 4802 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4803 | OMPPrivateScope LoopScope(CGF); |
| 4804 | // Emit helper vars inits. |
| 4805 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 4806 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 4807 | auto *LBP = std::next(I, LowerBound); |
| 4808 | auto *UBP = std::next(I, UpperBound); |
| 4809 | auto *STP = std::next(I, Stride); |
| 4810 | auto *LIP = std::next(I, LastIter); |
| 4811 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 4812 | LoopScope); |
| 4813 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 4814 | LoopScope); |
| 4815 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 4816 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 4817 | LoopScope); |
| 4818 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4819 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4820 | (void)LoopScope.Privatize(); |
| 4821 | // Emit the loop iteration variable. |
| 4822 | const Expr *IVExpr = S.getIterationVariable(); |
| 4823 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 4824 | CGF.EmitVarDecl(*IVDecl); |
| 4825 | CGF.EmitIgnoredExpr(S.getInit()); |
| 4826 | |
| 4827 | // Emit the iterations count variable. |
| 4828 | // If it is not a variable, Sema decided to calculate iterations count on |
| 4829 | // each iteration (e.g., it is foldable into a constant). |
| 4830 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 4831 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 4832 | // Emit calculation of the iterations count. |
| 4833 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 4834 | } |
| 4835 | |
| 4836 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 4837 | S.getInc(), |
| 4838 | [&S](CodeGenFunction &CGF) { |
| 4839 | CGF.EmitOMPLoopBody(S, JumpDest()); |
| 4840 | CGF.EmitStopPoint(&S); |
| 4841 | }, |
| 4842 | [](CodeGenFunction &) {}); |
| 4843 | // Emit: if (PreCond) - end. |
| 4844 | if (ContBlock) { |
| 4845 | CGF.EmitBranch(ContBlock); |
| 4846 | CGF.EmitBlock(ContBlock, true); |
| 4847 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4848 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 4849 | if (HasLastprivateClause) { |
| 4850 | CGF.EmitOMPLastprivateClauseFinal( |
| 4851 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 4852 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 4853 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
| 4854 | (*LIP)->getType(), S.getLocStart()))); |
| 4855 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4856 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4857 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
| 4858 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
| 4859 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4860 | auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4861 | OMPLoopScope PreInitScope(CGF, S); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4862 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S, |
| 4863 | OutlinedFn, SharedsTy, |
| 4864 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4865 | }; |
| 4866 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 4867 | CodeGen); |
| 4868 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4869 | if (Data.Nogroup) { |
| 4870 | EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); |
| 4871 | } else { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4872 | CGM.getOpenMPRuntime().emitTaskgroupRegion( |
| 4873 | *this, |
| 4874 | [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, |
| 4875 | PrePostActionTy &Action) { |
| 4876 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4877 | CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, |
| 4878 | Data); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4879 | }, |
| 4880 | S.getLocStart()); |
| 4881 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4884 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4885 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4886 | } |
| 4887 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4888 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 4889 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4890 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4891 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4892 | |
| 4893 | // Generate the instructions for '#pragma omp target update' directive. |
| 4894 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 4895 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 4896 | // If we don't have target devices, don't bother emitting the data mapping |
| 4897 | // code. |
| 4898 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4899 | return; |
| 4900 | |
| 4901 | // Check if we have any if clause associated with the directive. |
| 4902 | const Expr *IfCond = nullptr; |
| 4903 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4904 | IfCond = C->getCondition(); |
| 4905 | |
| 4906 | // Check if we have any device clause associated with the directive. |
| 4907 | const Expr *Device = nullptr; |
| 4908 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4909 | Device = C->getDevice(); |
| 4910 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4911 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4912 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4913 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 4914 | |
| 4915 | void CodeGenFunction::EmitSimpleOMPExecutableDirective( |
| 4916 | const OMPExecutableDirective &D) { |
| 4917 | if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) |
| 4918 | return; |
| 4919 | auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4920 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 4921 | emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); |
| 4922 | } else { |
| 4923 | if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { |
| 4924 | for (const auto *E : LD->counters()) { |
| 4925 | if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( |
| 4926 | cast<DeclRefExpr>(E)->getDecl())) { |
| 4927 | // Emit only those that were not explicitly referenced in clauses. |
| 4928 | if (!CGF.LocalDeclMap.count(VD)) |
| 4929 | CGF.EmitVarDecl(*VD); |
| 4930 | } |
| 4931 | } |
| 4932 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4933 | CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 4934 | } |
| 4935 | }; |
| 4936 | OMPSimdLexicalScope Scope(*this, D); |
| 4937 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4938 | *this, |
| 4939 | isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd |
| 4940 | : D.getDirectiveKind(), |
| 4941 | CodeGen); |
| 4942 | } |