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)), |
| 78 | VD->getType().getNonReferenceType(), VK_LValue, SourceLocation()); |
| 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 | c2e88a8 | 2017-12-04 21:30:42 +0000 | [diff] [blame] | 123 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
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()); |
| 126 | (void)PreCondScope.addPrivate(VD, [&CGF, VD]() { |
| 127 | return CGF.CreateMemTemp(VD->getType().getNonReferenceType()); |
| 128 | }); |
| 129 | } |
Alexey Bataev | c2e88a8 | 2017-12-04 21:30:42 +0000 | [diff] [blame] | 130 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 131 | if (auto *LD = dyn_cast<OMPLoopDirective>(&S)) { |
| 132 | if (auto *PreInits = cast_or_null<DeclStmt>(LD->getPreInits())) { |
| 133 | for (const auto *I : PreInits->decls()) |
| 134 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public: |
| 140 | OMPLoopScope(CodeGenFunction &CGF, const OMPLoopDirective &S) |
| 141 | : CodeGenFunction::RunCleanupsScope(CGF) { |
| 142 | emitPreInitStmt(CGF, S); |
| 143 | } |
| 144 | }; |
| 145 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 146 | class OMPSimdLexicalScope : public CodeGenFunction::LexicalScope { |
| 147 | CodeGenFunction::OMPPrivateScope InlinedShareds; |
| 148 | |
| 149 | static bool isCapturedVar(CodeGenFunction &CGF, const VarDecl *VD) { |
| 150 | return CGF.LambdaCaptureFields.lookup(VD) || |
| 151 | (CGF.CapturedStmtInfo && CGF.CapturedStmtInfo->lookup(VD)) || |
| 152 | (CGF.CurCodeDecl && isa<BlockDecl>(CGF.CurCodeDecl) && |
| 153 | cast<BlockDecl>(CGF.CurCodeDecl)->capturesVariable(VD)); |
| 154 | } |
| 155 | |
| 156 | public: |
| 157 | OMPSimdLexicalScope(CodeGenFunction &CGF, const OMPExecutableDirective &S) |
| 158 | : CodeGenFunction::LexicalScope(CGF, S.getSourceRange()), |
| 159 | InlinedShareds(CGF) { |
| 160 | for (const auto *C : S.clauses()) { |
| 161 | if (auto *CPI = OMPClauseWithPreInit::get(C)) { |
| 162 | if (auto *PreInit = cast_or_null<DeclStmt>(CPI->getPreInitStmt())) { |
| 163 | for (const auto *I : PreInit->decls()) { |
| 164 | if (!I->hasAttr<OMPCaptureNoInitAttr>()) |
| 165 | CGF.EmitVarDecl(cast<VarDecl>(*I)); |
| 166 | else { |
| 167 | CodeGenFunction::AutoVarEmission Emission = |
| 168 | CGF.EmitAutoVarAlloca(cast<VarDecl>(*I)); |
| 169 | CGF.EmitAutoVarCleanups(Emission); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } else if (const auto *UDP = dyn_cast<OMPUseDevicePtrClause>(C)) { |
| 174 | for (const Expr *E : UDP->varlists()) { |
| 175 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
| 176 | if (const auto *OED = dyn_cast<OMPCapturedExprDecl>(D)) |
| 177 | CGF.EmitVarDecl(*OED); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 182 | CGF.EmitOMPPrivateClause(S, InlinedShareds); |
| 183 | if (const auto *TG = dyn_cast<OMPTaskgroupDirective>(&S)) { |
| 184 | if (const Expr *E = TG->getReductionRef()) |
| 185 | CGF.EmitVarDecl(*cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())); |
| 186 | } |
| 187 | const auto *CS = cast_or_null<CapturedStmt>(S.getAssociatedStmt()); |
| 188 | while (CS) { |
| 189 | for (auto &C : CS->captures()) { |
| 190 | if (C.capturesVariable() || C.capturesVariableByCopy()) { |
| 191 | auto *VD = C.getCapturedVar(); |
| 192 | assert(VD == VD->getCanonicalDecl() && |
| 193 | "Canonical decl must be captured."); |
| 194 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), |
| 195 | isCapturedVar(CGF, VD) || |
| 196 | (CGF.CapturedStmtInfo && |
| 197 | InlinedShareds.isGlobalVarCaptured(VD)), |
| 198 | VD->getType().getNonReferenceType(), VK_LValue, |
| 199 | SourceLocation()); |
| 200 | InlinedShareds.addPrivate(VD, [&CGF, &DRE]() -> Address { |
| 201 | return CGF.EmitLValue(&DRE).getAddress(); |
| 202 | }); |
| 203 | } |
| 204 | } |
| 205 | CS = dyn_cast<CapturedStmt>(CS->getCapturedStmt()); |
| 206 | } |
| 207 | (void)InlinedShareds.Privatize(); |
| 208 | } |
| 209 | }; |
| 210 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 211 | } // namespace |
| 212 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 213 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 214 | const OMPExecutableDirective &S, |
| 215 | const RegionCodeGenTy &CodeGen); |
| 216 | |
Alexey Bataev | f47c4b4 | 2017-09-26 13:47:31 +0000 | [diff] [blame] | 217 | LValue CodeGenFunction::EmitOMPSharedLValue(const Expr *E) { |
| 218 | if (auto *OrigDRE = dyn_cast<DeclRefExpr>(E)) { |
| 219 | if (auto *OrigVD = dyn_cast<VarDecl>(OrigDRE->getDecl())) { |
| 220 | OrigVD = OrigVD->getCanonicalDecl(); |
| 221 | bool IsCaptured = |
| 222 | LambdaCaptureFields.lookup(OrigVD) || |
| 223 | (CapturedStmtInfo && CapturedStmtInfo->lookup(OrigVD)) || |
| 224 | (CurCodeDecl && isa<BlockDecl>(CurCodeDecl)); |
| 225 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), IsCaptured, |
| 226 | OrigDRE->getType(), VK_LValue, OrigDRE->getExprLoc()); |
| 227 | return EmitLValue(&DRE); |
| 228 | } |
| 229 | } |
| 230 | return EmitLValue(E); |
| 231 | } |
| 232 | |
Alexey Bataev | 1189bd0 | 2016-01-26 12:20:39 +0000 | [diff] [blame] | 233 | llvm::Value *CodeGenFunction::getTypeSize(QualType Ty) { |
| 234 | auto &C = getContext(); |
| 235 | llvm::Value *Size = nullptr; |
| 236 | auto SizeInChars = C.getTypeSizeInChars(Ty); |
| 237 | if (SizeInChars.isZero()) { |
| 238 | // getTypeSizeInChars() returns 0 for a VLA. |
| 239 | while (auto *VAT = C.getAsVariableArrayType(Ty)) { |
| 240 | llvm::Value *ArraySize; |
| 241 | std::tie(ArraySize, Ty) = getVLASize(VAT); |
| 242 | Size = Size ? Builder.CreateNUWMul(Size, ArraySize) : ArraySize; |
| 243 | } |
| 244 | SizeInChars = C.getTypeSizeInChars(Ty); |
| 245 | if (SizeInChars.isZero()) |
| 246 | return llvm::ConstantInt::get(SizeTy, /*V=*/0); |
| 247 | Size = Builder.CreateNUWMul(Size, CGM.getSize(SizeInChars)); |
| 248 | } else |
| 249 | Size = CGM.getSize(SizeInChars); |
| 250 | return Size; |
| 251 | } |
| 252 | |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 253 | void CodeGenFunction::GenerateOpenMPCapturedVars( |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 254 | const CapturedStmt &S, SmallVectorImpl<llvm::Value *> &CapturedVars) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 255 | const RecordDecl *RD = S.getCapturedRecordDecl(); |
| 256 | auto CurField = RD->field_begin(); |
| 257 | auto CurCap = S.captures().begin(); |
| 258 | for (CapturedStmt::const_capture_init_iterator I = S.capture_init_begin(), |
| 259 | E = S.capture_init_end(); |
| 260 | I != E; ++I, ++CurField, ++CurCap) { |
| 261 | if (CurField->hasCapturedVLAType()) { |
| 262 | auto VAT = CurField->getCapturedVLAType(); |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 263 | auto *Val = VLASizeMap[VAT->getSizeExpr()]; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 264 | CapturedVars.push_back(Val); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 265 | } else if (CurCap->capturesThis()) |
| 266 | CapturedVars.push_back(CXXThisValue); |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 267 | else if (CurCap->capturesVariableByCopy()) { |
| 268 | llvm::Value *CV = |
| 269 | EmitLoadOfLValue(EmitLValue(*I), SourceLocation()).getScalarVal(); |
| 270 | |
| 271 | // If the field is not a pointer, we need to save the actual value |
| 272 | // and load it as a void pointer. |
| 273 | if (!CurField->getType()->isAnyPointerType()) { |
| 274 | auto &Ctx = getContext(); |
| 275 | auto DstAddr = CreateMemTemp( |
| 276 | Ctx.getUIntPtrType(), |
| 277 | Twine(CurCap->getCapturedVar()->getName()) + ".casted"); |
| 278 | LValue DstLV = MakeAddrLValue(DstAddr, Ctx.getUIntPtrType()); |
| 279 | |
| 280 | auto *SrcAddrVal = EmitScalarConversion( |
| 281 | DstAddr.getPointer(), Ctx.getPointerType(Ctx.getUIntPtrType()), |
| 282 | Ctx.getPointerType(CurField->getType()), SourceLocation()); |
| 283 | LValue SrcLV = |
| 284 | MakeNaturalAlignAddrLValue(SrcAddrVal, CurField->getType()); |
| 285 | |
| 286 | // Store the value using the source type pointer. |
| 287 | EmitStoreThroughLValue(RValue::get(CV), SrcLV); |
| 288 | |
| 289 | // Load the value using the destination type pointer. |
| 290 | CV = EmitLoadOfLValue(DstLV, SourceLocation()).getScalarVal(); |
| 291 | } |
| 292 | CapturedVars.push_back(CV); |
| 293 | } else { |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 294 | assert(CurCap->capturesVariable() && "Expected capture by reference."); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 295 | CapturedVars.push_back(EmitLValue(*I).getAddress().getPointer()); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 296 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 300 | static Address castValueFromUintptr(CodeGenFunction &CGF, QualType DstType, |
| 301 | StringRef Name, LValue AddrLV, |
| 302 | bool isReferenceType = false) { |
| 303 | ASTContext &Ctx = CGF.getContext(); |
| 304 | |
| 305 | auto *CastedPtr = CGF.EmitScalarConversion( |
| 306 | AddrLV.getAddress().getPointer(), Ctx.getUIntPtrType(), |
| 307 | Ctx.getPointerType(DstType), SourceLocation()); |
| 308 | auto TmpAddr = |
| 309 | CGF.MakeNaturalAlignAddrLValue(CastedPtr, Ctx.getPointerType(DstType)) |
| 310 | .getAddress(); |
| 311 | |
| 312 | // If we are dealing with references we need to return the address of the |
| 313 | // reference instead of the reference of the value. |
| 314 | if (isReferenceType) { |
| 315 | QualType RefType = Ctx.getLValueReferenceType(DstType); |
| 316 | auto *RefVal = TmpAddr.getPointer(); |
| 317 | TmpAddr = CGF.CreateMemTemp(RefType, Twine(Name) + ".ref"); |
| 318 | auto TmpLVal = CGF.MakeAddrLValue(TmpAddr, RefType); |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 319 | CGF.EmitStoreThroughLValue(RValue::get(RefVal), TmpLVal, /*isInit*/ true); |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | return TmpAddr; |
| 323 | } |
| 324 | |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 325 | static QualType getCanonicalParamType(ASTContext &C, QualType T) { |
| 326 | if (T->isLValueReferenceType()) { |
| 327 | return C.getLValueReferenceType( |
| 328 | getCanonicalParamType(C, T.getNonReferenceType()), |
| 329 | /*SpelledAsLValue=*/false); |
| 330 | } |
| 331 | if (T->isPointerType()) |
| 332 | return C.getPointerType(getCanonicalParamType(C, T->getPointeeType())); |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 333 | if (auto *A = T->getAsArrayTypeUnsafe()) { |
| 334 | if (auto *VLA = dyn_cast<VariableArrayType>(A)) |
| 335 | return getCanonicalParamType(C, VLA->getElementType()); |
| 336 | else if (!A->isVariablyModifiedType()) |
| 337 | return C.getCanonicalType(T); |
| 338 | } |
Alexey Bataev | f7ce166 | 2017-04-10 19:16:45 +0000 | [diff] [blame] | 339 | return C.getCanonicalParamType(T); |
| 340 | } |
| 341 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 342 | namespace { |
| 343 | /// Contains required data for proper outlined function codegen. |
| 344 | struct FunctionOptions { |
| 345 | /// Captured statement for which the function is generated. |
| 346 | const CapturedStmt *S = nullptr; |
| 347 | /// true if cast to/from UIntPtr is required for variables captured by |
| 348 | /// value. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 349 | const bool UIntPtrCastRequired = true; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 350 | /// 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] | 351 | /// sizes. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 352 | const bool RegisterCastedArgsOnly = false; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 353 | /// Name of the generated function. |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 354 | const StringRef FunctionName; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 355 | explicit FunctionOptions(const CapturedStmt *S, bool UIntPtrCastRequired, |
| 356 | bool RegisterCastedArgsOnly, |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 357 | StringRef FunctionName) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 358 | : S(S), UIntPtrCastRequired(UIntPtrCastRequired), |
| 359 | RegisterCastedArgsOnly(UIntPtrCastRequired && RegisterCastedArgsOnly), |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 360 | FunctionName(FunctionName) {} |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 361 | }; |
| 362 | } |
| 363 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 364 | static llvm::Function *emitOutlinedFunctionPrologue( |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 365 | CodeGenFunction &CGF, FunctionArgList &Args, |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 366 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 367 | &LocalAddrs, |
| 368 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> |
| 369 | &VLASizes, |
| 370 | llvm::Value *&CXXThisValue, const FunctionOptions &FO) { |
| 371 | const CapturedDecl *CD = FO.S->getCapturedDecl(); |
| 372 | const RecordDecl *RD = FO.S->getCapturedRecordDecl(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 373 | assert(CD->hasBody() && "missing CapturedDecl body"); |
| 374 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 375 | CXXThisValue = nullptr; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 376 | // Build the argument list. |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 377 | CodeGenModule &CGM = CGF.CGM; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 378 | ASTContext &Ctx = CGM.getContext(); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 379 | FunctionArgList TargetArgs; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 380 | Args.append(CD->param_begin(), |
| 381 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 382 | TargetArgs.append( |
| 383 | CD->param_begin(), |
| 384 | std::next(CD->param_begin(), CD->getContextParamPosition())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 385 | auto I = FO.S->captures().begin(); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 386 | FunctionDecl *DebugFunctionDecl = nullptr; |
| 387 | if (!FO.UIntPtrCastRequired) { |
| 388 | FunctionProtoType::ExtProtoInfo EPI; |
| 389 | DebugFunctionDecl = FunctionDecl::Create( |
| 390 | Ctx, Ctx.getTranslationUnitDecl(), FO.S->getLocStart(), |
| 391 | SourceLocation(), DeclarationName(), Ctx.VoidTy, |
| 392 | Ctx.getTrivialTypeSourceInfo( |
| 393 | Ctx.getFunctionType(Ctx.VoidTy, llvm::None, EPI)), |
| 394 | SC_Static, /*isInlineSpecified=*/false, /*hasWrittenPrototype=*/false); |
| 395 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 396 | for (auto *FD : RD->fields()) { |
| 397 | QualType ArgType = FD->getType(); |
| 398 | IdentifierInfo *II = nullptr; |
| 399 | VarDecl *CapVar = nullptr; |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 400 | |
| 401 | // If this is a capture by copy and the type is not a pointer, the outlined |
| 402 | // function argument type should be uintptr and the value properly casted to |
| 403 | // uintptr. This is necessary given that the runtime library is only able to |
| 404 | // deal with pointers. We can pass in the same way the VLA type sizes to the |
| 405 | // outlined function. |
Samuel Antao | 6d00426 | 2016-06-16 18:39:34 +0000 | [diff] [blame] | 406 | if ((I->capturesVariableByCopy() && !ArgType->isAnyPointerType()) || |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 407 | I->capturesVariableArrayType()) { |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 408 | if (FO.UIntPtrCastRequired) |
| 409 | ArgType = Ctx.getUIntPtrType(); |
| 410 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 411 | |
| 412 | if (I->capturesVariable() || I->capturesVariableByCopy()) { |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 413 | CapVar = I->getCapturedVar(); |
| 414 | II = CapVar->getIdentifier(); |
| 415 | } else if (I->capturesThis()) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 416 | II = &Ctx.Idents.get("this"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 417 | else { |
| 418 | assert(I->capturesVariableArrayType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 419 | II = &Ctx.Idents.get("vla"); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 420 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 421 | if (ArgType->isVariablyModifiedType()) |
Alexey Bataev | 1b48c5e | 2017-10-24 19:52:31 +0000 | [diff] [blame] | 422 | ArgType = getCanonicalParamType(Ctx, ArgType); |
Alexey Bataev | b45d43c | 2017-11-22 16:02:03 +0000 | [diff] [blame] | 423 | VarDecl *Arg; |
| 424 | if (DebugFunctionDecl && (CapVar || I->capturesThis())) { |
| 425 | Arg = ParmVarDecl::Create( |
| 426 | Ctx, DebugFunctionDecl, |
| 427 | CapVar ? CapVar->getLocStart() : FD->getLocStart(), |
| 428 | CapVar ? CapVar->getLocation() : FD->getLocation(), II, ArgType, |
| 429 | /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr); |
| 430 | } else { |
| 431 | Arg = ImplicitParamDecl::Create(Ctx, /*DC=*/nullptr, FD->getLocation(), |
| 432 | II, ArgType, ImplicitParamDecl::Other); |
| 433 | } |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 434 | Args.emplace_back(Arg); |
| 435 | // Do not cast arguments if we emit function with non-original types. |
| 436 | TargetArgs.emplace_back( |
| 437 | FO.UIntPtrCastRequired |
| 438 | ? Arg |
| 439 | : CGM.getOpenMPRuntime().translateParameter(FD, Arg)); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 440 | ++I; |
| 441 | } |
| 442 | Args.append( |
| 443 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 444 | CD->param_end()); |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 445 | TargetArgs.append( |
| 446 | std::next(CD->param_begin(), CD->getContextParamPosition() + 1), |
| 447 | CD->param_end()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 448 | |
| 449 | // Create the function declaration. |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 450 | const CGFunctionInfo &FuncInfo = |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 451 | CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, TargetArgs); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 452 | llvm::FunctionType *FuncLLVMTy = CGM.getTypes().GetFunctionType(FuncInfo); |
| 453 | |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 454 | llvm::Function *F = |
| 455 | llvm::Function::Create(FuncLLVMTy, llvm::GlobalValue::InternalLinkage, |
| 456 | FO.FunctionName, &CGM.getModule()); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 457 | CGM.SetInternalFunctionAttributes(CD, F, FuncInfo); |
| 458 | if (CD->isNothrow()) |
Alexey Bataev | 2c7eee5 | 2017-08-04 19:10:54 +0000 | [diff] [blame] | 459 | F->setDoesNotThrow(); |
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) { |
| 498 | ArgLVal = CGF.MakeAddrLValue(castValueFromUintptr(CGF, FD->getType(), |
| 499 | Args[Cnt]->getName(), |
| 500 | ArgLVal), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 501 | FD->getType(), AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 502 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 503 | auto *ExprArg = |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 504 | CGF.EmitLoadOfLValue(ArgLVal, SourceLocation()).getScalarVal(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 505 | auto VAT = FD->getCapturedVLAType(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 506 | VLASizes.insert({Args[Cnt], {VAT->getSizeExpr(), ExprArg}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 507 | } else if (I->capturesVariable()) { |
| 508 | auto *Var = I->getCapturedVar(); |
| 509 | QualType VarTy = Var->getType(); |
| 510 | Address ArgAddr = ArgLVal.getAddress(); |
| 511 | if (!VarTy->isReferenceType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 512 | if (ArgLVal.getType()->isLValueReferenceType()) { |
Ivan A. Kosarev | 9f9d157 | 2017-10-30 11:49:31 +0000 | [diff] [blame] | 513 | ArgAddr = CGF.EmitLoadOfReference(ArgLVal); |
Alexey Bataev | ac5eabb | 2016-11-07 11:16:04 +0000 | [diff] [blame] | 514 | } else if (!VarTy->isVariablyModifiedType() || !VarTy->isPointerType()) { |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 515 | assert(ArgLVal.getType()->isPointerType()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 516 | ArgAddr = CGF.EmitLoadOfPointer( |
Alexey Bataev | 2f5ed34 | 2016-10-13 09:52:46 +0000 | [diff] [blame] | 517 | ArgAddr, ArgLVal.getType()->castAs<PointerType>()); |
| 518 | } |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 519 | } |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 520 | if (!FO.RegisterCastedArgsOnly) { |
| 521 | LocalAddrs.insert( |
| 522 | {Args[Cnt], |
| 523 | {Var, Address(ArgAddr.getPointer(), Ctx.getDeclAlign(Var))}}); |
| 524 | } |
Samuel Antao | 4af1b7b | 2015-12-02 17:44:43 +0000 | [diff] [blame] | 525 | } else if (I->capturesVariableByCopy()) { |
| 526 | assert(!FD->getType()->isAnyPointerType() && |
| 527 | "Not expecting a captured pointer."); |
| 528 | auto *Var = I->getCapturedVar(); |
| 529 | QualType VarTy = Var->getType(); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 530 | LocalAddrs.insert( |
| 531 | {Args[Cnt], |
| 532 | {Var, |
| 533 | FO.UIntPtrCastRequired |
| 534 | ? castValueFromUintptr(CGF, FD->getType(), Args[Cnt]->getName(), |
| 535 | ArgLVal, VarTy->isReferenceType()) |
| 536 | : ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 537 | } else { |
| 538 | // If 'this' is captured, load it into CXXThisValue. |
| 539 | assert(I->capturesThis()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 540 | CXXThisValue = CGF.EmitLoadOfLValue(ArgLVal, Args[Cnt]->getLocation()) |
| 541 | .getScalarVal(); |
| 542 | LocalAddrs.insert({Args[Cnt], {nullptr, ArgLVal.getAddress()}}); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 543 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 544 | ++Cnt; |
| 545 | ++I; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 548 | return F; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | llvm::Function * |
| 552 | CodeGenFunction::GenerateOpenMPCapturedStmtFunction(const CapturedStmt &S) { |
| 553 | assert( |
| 554 | CapturedStmtInfo && |
| 555 | "CapturedStmtInfo should be set when generating the captured function"); |
| 556 | const CapturedDecl *CD = S.getCapturedDecl(); |
| 557 | // Build the argument list. |
| 558 | bool NeedWrapperFunction = |
| 559 | getDebugInfo() && |
| 560 | CGM.getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo; |
| 561 | FunctionArgList Args; |
Alexey Bataev | 3b8d558 | 2017-08-08 18:04:06 +0000 | [diff] [blame] | 562 | llvm::MapVector<const Decl *, std::pair<const VarDecl *, Address>> LocalAddrs; |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 563 | llvm::DenseMap<const Decl *, std::pair<const Expr *, llvm::Value *>> VLASizes; |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 564 | SmallString<256> Buffer; |
| 565 | llvm::raw_svector_ostream Out(Buffer); |
| 566 | Out << CapturedStmtInfo->getHelperName(); |
| 567 | if (NeedWrapperFunction) |
| 568 | Out << "_debug__"; |
Alexey Bataev | 4aa1905 | 2017-08-08 16:45:36 +0000 | [diff] [blame] | 569 | FunctionOptions FO(&S, !NeedWrapperFunction, /*RegisterCastedArgsOnly=*/false, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 570 | Out.str()); |
| 571 | llvm::Function *F = emitOutlinedFunctionPrologue(*this, Args, LocalAddrs, |
| 572 | VLASizes, CXXThisValue, FO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 573 | for (const auto &LocalAddrPair : LocalAddrs) { |
| 574 | if (LocalAddrPair.second.first) { |
| 575 | setAddrOfLocalVar(LocalAddrPair.second.first, |
| 576 | LocalAddrPair.second.second); |
| 577 | } |
| 578 | } |
| 579 | for (const auto &VLASizePair : VLASizes) |
| 580 | VLASizeMap[VLASizePair.second.first] = VLASizePair.second.second; |
Serge Pavlov | 3a56145 | 2015-12-06 14:32:39 +0000 | [diff] [blame] | 581 | PGO.assignRegionCounters(GlobalDecl(CD), F); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 582 | CapturedStmtInfo->EmitBody(*this, CD->getBody()); |
| 583 | FinishFunction(CD->getBodyRBrace()); |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 584 | if (!NeedWrapperFunction) |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 585 | return F; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 586 | |
Alexey Bataev | efd884d | 2017-08-04 21:26:25 +0000 | [diff] [blame] | 587 | FunctionOptions WrapperFO(&S, /*UIntPtrCastRequired=*/true, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 588 | /*RegisterCastedArgsOnly=*/true, |
| 589 | CapturedStmtInfo->getHelperName()); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 590 | CodeGenFunction WrapperCGF(CGM, /*suppressNewContext=*/true); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 591 | Args.clear(); |
| 592 | LocalAddrs.clear(); |
| 593 | VLASizes.clear(); |
| 594 | llvm::Function *WrapperF = |
| 595 | emitOutlinedFunctionPrologue(WrapperCGF, Args, LocalAddrs, VLASizes, |
Alexey Bataev | e754b18 | 2017-08-09 19:38:53 +0000 | [diff] [blame] | 596 | WrapperCGF.CXXThisValue, WrapperFO); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 597 | llvm::SmallVector<llvm::Value *, 4> CallArgs; |
| 598 | for (const auto *Arg : Args) { |
| 599 | llvm::Value *CallArg; |
| 600 | auto I = LocalAddrs.find(Arg); |
| 601 | if (I != LocalAddrs.end()) { |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 602 | LValue LV = WrapperCGF.MakeAddrLValue( |
| 603 | I->second.second, |
| 604 | I->second.first ? I->second.first->getType() : Arg->getType(), |
| 605 | AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 606 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, SourceLocation()); |
| 607 | } else { |
| 608 | auto EI = VLASizes.find(Arg); |
| 609 | if (EI != VLASizes.end()) |
| 610 | CallArg = EI->second.second; |
| 611 | else { |
| 612 | LValue LV = WrapperCGF.MakeAddrLValue(WrapperCGF.GetAddrOfLocalVar(Arg), |
Ivan A. Kosarev | 5f8c0ca | 2017-10-10 09:39:32 +0000 | [diff] [blame] | 613 | Arg->getType(), |
| 614 | AlignmentSource::Decl); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 615 | CallArg = WrapperCGF.EmitLoadOfScalar(LV, SourceLocation()); |
| 616 | } |
| 617 | } |
Alexey Bataev | 7ba57af | 2017-10-17 16:47:34 +0000 | [diff] [blame] | 618 | CallArgs.emplace_back(WrapperCGF.EmitFromMemory(CallArg, Arg->getType())); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 619 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 620 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(WrapperCGF, S.getLocStart(), |
| 621 | F, CallArgs); |
Alexey Bataev | 1fdfdf7 | 2017-06-29 16:43:05 +0000 | [diff] [blame] | 622 | WrapperCGF.FinishFunction(); |
| 623 | return WrapperF; |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 626 | //===----------------------------------------------------------------------===// |
| 627 | // OpenMP Directive Emission |
| 628 | //===----------------------------------------------------------------------===// |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 629 | void CodeGenFunction::EmitOMPAggregateAssign( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 630 | Address DestAddr, Address SrcAddr, QualType OriginalType, |
| 631 | const llvm::function_ref<void(Address, Address)> &CopyGen) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 632 | // Perform element-by-element initialization. |
| 633 | QualType ElementTy; |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 634 | |
| 635 | // Drill down to the base element type on both arrays. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 636 | auto ArrayTy = OriginalType->getAsArrayTypeUnsafe(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 637 | auto NumElements = emitArrayLength(ArrayTy, ElementTy, DestAddr); |
| 638 | SrcAddr = Builder.CreateElementBitCast(SrcAddr, DestAddr.getElementType()); |
| 639 | |
| 640 | auto SrcBegin = SrcAddr.getPointer(); |
| 641 | auto DestBegin = DestAddr.getPointer(); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 642 | // Cast from pointer to array type to pointer to single element. |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 643 | auto DestEnd = Builder.CreateGEP(DestBegin, NumElements); |
| 644 | // The basic structure here is a while-do loop. |
| 645 | auto BodyBB = createBasicBlock("omp.arraycpy.body"); |
| 646 | auto DoneBB = createBasicBlock("omp.arraycpy.done"); |
| 647 | auto IsEmpty = |
| 648 | Builder.CreateICmpEQ(DestBegin, DestEnd, "omp.arraycpy.isempty"); |
| 649 | Builder.CreateCondBr(IsEmpty, DoneBB, BodyBB); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 650 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 651 | // Enter the loop body, making that address the current address. |
| 652 | auto EntryBB = Builder.GetInsertBlock(); |
| 653 | EmitBlock(BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 654 | |
| 655 | CharUnits ElementSize = getContext().getTypeSizeInChars(ElementTy); |
| 656 | |
| 657 | llvm::PHINode *SrcElementPHI = |
| 658 | Builder.CreatePHI(SrcBegin->getType(), 2, "omp.arraycpy.srcElementPast"); |
| 659 | SrcElementPHI->addIncoming(SrcBegin, EntryBB); |
| 660 | Address SrcElementCurrent = |
| 661 | Address(SrcElementPHI, |
| 662 | SrcAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
| 663 | |
| 664 | llvm::PHINode *DestElementPHI = |
| 665 | Builder.CreatePHI(DestBegin->getType(), 2, "omp.arraycpy.destElementPast"); |
| 666 | DestElementPHI->addIncoming(DestBegin, EntryBB); |
| 667 | Address DestElementCurrent = |
| 668 | Address(DestElementPHI, |
| 669 | DestAddr.getAlignment().alignmentOfArrayElement(ElementSize)); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 670 | |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 671 | // Emit copy. |
| 672 | CopyGen(DestElementCurrent, SrcElementCurrent); |
| 673 | |
| 674 | // Shift the address forward by one element. |
| 675 | auto DestElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 676 | DestElementPHI, /*Idx0=*/1, "omp.arraycpy.dest.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 677 | auto SrcElementNext = Builder.CreateConstGEP1_32( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 678 | SrcElementPHI, /*Idx0=*/1, "omp.arraycpy.src.element"); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 679 | // Check whether we've reached the end. |
| 680 | auto Done = |
| 681 | Builder.CreateICmpEQ(DestElementNext, DestEnd, "omp.arraycpy.done"); |
| 682 | Builder.CreateCondBr(Done, DoneBB, BodyBB); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 683 | DestElementPHI->addIncoming(DestElementNext, Builder.GetInsertBlock()); |
| 684 | SrcElementPHI->addIncoming(SrcElementNext, Builder.GetInsertBlock()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 685 | |
| 686 | // Done. |
| 687 | EmitBlock(DoneBB, /*IsFinished=*/true); |
| 688 | } |
| 689 | |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 690 | void CodeGenFunction::EmitOMPCopy(QualType OriginalType, Address DestAddr, |
| 691 | Address SrcAddr, const VarDecl *DestVD, |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 692 | const VarDecl *SrcVD, const Expr *Copy) { |
| 693 | if (OriginalType->isArrayType()) { |
| 694 | auto *BO = dyn_cast<BinaryOperator>(Copy); |
| 695 | if (BO && BO->getOpcode() == BO_Assign) { |
| 696 | // Perform simple memcpy for simple copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 697 | EmitAggregateAssign(DestAddr, SrcAddr, OriginalType); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 698 | } else { |
| 699 | // For arrays with complex element types perform element by element |
| 700 | // copying. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 701 | EmitOMPAggregateAssign( |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 702 | DestAddr, SrcAddr, OriginalType, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 703 | [this, Copy, SrcVD, DestVD](Address DestElement, Address SrcElement) { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 704 | // Working with the single array element, so have to remap |
| 705 | // destination and source variables to corresponding array |
| 706 | // elements. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 707 | CodeGenFunction::OMPPrivateScope Remap(*this); |
| 708 | Remap.addPrivate(DestVD, [DestElement]() -> Address { |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 709 | return DestElement; |
| 710 | }); |
| 711 | Remap.addPrivate( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 712 | SrcVD, [SrcElement]() -> Address { return SrcElement; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 713 | (void)Remap.Privatize(); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 714 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 715 | }); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 716 | } |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 717 | } else { |
| 718 | // Remap pseudo source variable to private copy. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 719 | CodeGenFunction::OMPPrivateScope Remap(*this); |
| 720 | Remap.addPrivate(SrcVD, [SrcAddr]() -> Address { return SrcAddr; }); |
| 721 | Remap.addPrivate(DestVD, [DestAddr]() -> Address { return DestAddr; }); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 722 | (void)Remap.Privatize(); |
| 723 | // Emit copying of the whole variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 724 | EmitIgnoredExpr(Copy); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 725 | } |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 726 | } |
| 727 | |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 728 | bool CodeGenFunction::EmitOMPFirstprivateClause(const OMPExecutableDirective &D, |
| 729 | OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 730 | if (!HaveInsertPoint()) |
| 731 | return false; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 732 | bool FirstprivateIsLastprivate = false; |
| 733 | llvm::DenseSet<const VarDecl *> Lastprivates; |
| 734 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 735 | for (const auto *D : C->varlists()) |
| 736 | Lastprivates.insert( |
| 737 | cast<VarDecl>(cast<DeclRefExpr>(D)->getDecl())->getCanonicalDecl()); |
| 738 | } |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 739 | llvm::DenseSet<const VarDecl *> EmittedAsFirstprivate; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 740 | llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions; |
| 741 | getOpenMPCaptureRegions(CaptureRegions, D.getDirectiveKind()); |
| 742 | // Force emission of the firstprivate copy if the directive does not emit |
| 743 | // outlined function, like omp for, omp simd, omp distribute etc. |
| 744 | bool MustEmitFirstprivateCopy = |
| 745 | CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 746 | for (const auto *C : D.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 747 | auto IRef = C->varlist_begin(); |
| 748 | auto InitsRef = C->inits().begin(); |
| 749 | for (auto IInit : C->private_copies()) { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 750 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 751 | bool ThisFirstprivateIsLastprivate = |
| 752 | Lastprivates.count(OrigVD->getCanonicalDecl()) > 0; |
| 753 | auto *FD = CapturedStmtInfo->lookup(OrigVD); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 754 | if (!MustEmitFirstprivateCopy && !ThisFirstprivateIsLastprivate && FD && |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 755 | !FD->getType()->isReferenceType()) { |
| 756 | EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()); |
| 757 | ++IRef; |
| 758 | ++InitsRef; |
| 759 | continue; |
| 760 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 761 | FirstprivateIsLastprivate = |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 762 | FirstprivateIsLastprivate || ThisFirstprivateIsLastprivate; |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 763 | if (EmittedAsFirstprivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 764 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 765 | auto *VDInit = cast<VarDecl>(cast<DeclRefExpr>(*InitsRef)->getDecl()); |
| 766 | bool IsRegistered; |
Alexey Bataev | 7ace49d | 2016-05-17 08:55:33 +0000 | [diff] [blame] | 767 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 768 | /*RefersToEnclosingVariableOrCapture=*/FD != nullptr, |
| 769 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 770 | Address OriginalAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | feddd64 | 2016-04-22 09:05:03 +0000 | [diff] [blame] | 771 | QualType Type = VD->getType(); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 772 | if (Type->isArrayType()) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 773 | // Emit VarDecl with copy init for arrays. |
| 774 | // Get the address of the original variable captured in current |
| 775 | // captured region. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 776 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 777 | auto Emission = EmitAutoVarAlloca(*VD); |
| 778 | auto *Init = VD->getInit(); |
| 779 | if (!isa<CXXConstructExpr>(Init) || isTrivialInitializer(Init)) { |
| 780 | // Perform simple memcpy. |
| 781 | EmitAggregateAssign(Emission.getAllocatedAddress(), OriginalAddr, |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 782 | Type); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 783 | } else { |
| 784 | EmitOMPAggregateAssign( |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 785 | Emission.getAllocatedAddress(), OriginalAddr, Type, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 786 | [this, VDInit, Init](Address DestElement, |
| 787 | Address SrcElement) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 788 | // Clean up any temporaries needed by the initialization. |
| 789 | RunCleanupsScope InitScope(*this); |
| 790 | // Emit initialization for single element. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 791 | setAddrOfLocalVar(VDInit, SrcElement); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 792 | EmitAnyExprToMem(Init, DestElement, |
| 793 | Init->getType().getQualifiers(), |
| 794 | /*IsInitializer*/ false); |
| 795 | LocalDeclMap.erase(VDInit); |
| 796 | }); |
| 797 | } |
| 798 | EmitAutoVarCleanups(Emission); |
| 799 | return Emission.getAllocatedAddress(); |
| 800 | }); |
| 801 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 802 | IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 803 | // Emit private VarDecl with copy init. |
| 804 | // Remap temp VDInit variable to the address of the original |
| 805 | // variable |
| 806 | // (for proper handling of captured global variables). |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 807 | setAddrOfLocalVar(VDInit, OriginalAddr); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 808 | EmitDecl(*VD); |
| 809 | LocalDeclMap.erase(VDInit); |
| 810 | return GetAddrOfLocalVar(VD); |
| 811 | }); |
| 812 | } |
| 813 | assert(IsRegistered && |
| 814 | "firstprivate var already registered as private"); |
| 815 | // Silence the warning about unused variable. |
| 816 | (void)IsRegistered; |
| 817 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 818 | ++IRef; |
| 819 | ++InitsRef; |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 820 | } |
| 821 | } |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 822 | return FirstprivateIsLastprivate && !EmittedAsFirstprivate.empty(); |
Alexey Bataev | 4a5bb77 | 2014-10-08 14:01:46 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 825 | void CodeGenFunction::EmitOMPPrivateClause( |
| 826 | const OMPExecutableDirective &D, |
| 827 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 828 | if (!HaveInsertPoint()) |
| 829 | return; |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 830 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 831 | for (const auto *C : D.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 832 | auto IRef = C->varlist_begin(); |
| 833 | for (auto IInit : C->private_copies()) { |
| 834 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 835 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 836 | auto VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
| 837 | bool IsRegistered = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 838 | PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
Alexey Bataev | 50a6458 | 2015-04-22 12:24:45 +0000 | [diff] [blame] | 839 | // Emit private VarDecl with copy init. |
| 840 | EmitDecl(*VD); |
| 841 | return GetAddrOfLocalVar(VD); |
| 842 | }); |
| 843 | assert(IsRegistered && "private var already registered as private"); |
| 844 | // Silence the warning about unused variable. |
| 845 | (void)IsRegistered; |
| 846 | } |
Alexey Bataev | 03b340a | 2014-10-21 03:16:40 +0000 | [diff] [blame] | 847 | ++IRef; |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 852 | bool CodeGenFunction::EmitOMPCopyinClause(const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 853 | if (!HaveInsertPoint()) |
| 854 | return false; |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 855 | // threadprivate_var1 = master_threadprivate_var1; |
| 856 | // operator=(threadprivate_var2, master_threadprivate_var2); |
| 857 | // ... |
| 858 | // __kmpc_barrier(&loc, global_tid); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 859 | llvm::DenseSet<const VarDecl *> CopiedVars; |
| 860 | llvm::BasicBlock *CopyBegin = nullptr, *CopyEnd = nullptr; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 861 | for (const auto *C : D.getClausesOfKind<OMPCopyinClause>()) { |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 862 | auto IRef = C->varlist_begin(); |
| 863 | auto ISrcRef = C->source_exprs().begin(); |
| 864 | auto IDestRef = C->destination_exprs().begin(); |
| 865 | for (auto *AssignOp : C->assignment_ops()) { |
| 866 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | 1d9c15c | 2015-05-19 12:31:28 +0000 | [diff] [blame] | 867 | QualType Type = VD->getType(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 868 | if (CopiedVars.insert(VD->getCanonicalDecl()).second) { |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 869 | // Get the address of the master variable. If we are emitting code with |
| 870 | // TLS support, the address is passed from the master as field in the |
| 871 | // captured declaration. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 872 | Address MasterAddr = Address::invalid(); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 873 | if (getLangOpts().OpenMPUseTLS && |
| 874 | getContext().getTargetInfo().isTLSSupported()) { |
| 875 | assert(CapturedStmtInfo->lookup(VD) && |
| 876 | "Copyin threadprivates should have been captured!"); |
| 877 | DeclRefExpr DRE(const_cast<VarDecl *>(VD), true, (*IRef)->getType(), |
| 878 | VK_LValue, (*IRef)->getExprLoc()); |
| 879 | MasterAddr = EmitLValue(&DRE).getAddress(); |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 880 | LocalDeclMap.erase(VD); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 881 | } else { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 882 | MasterAddr = |
| 883 | Address(VD->isStaticLocal() ? CGM.getStaticLocalDeclAddress(VD) |
| 884 | : CGM.GetAddrOfGlobal(VD), |
| 885 | getContext().getDeclAlign(VD)); |
Samuel Antao | 9c75cfe | 2015-07-27 16:38:06 +0000 | [diff] [blame] | 886 | } |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 887 | // Get the address of the threadprivate variable. |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 888 | Address PrivateAddr = EmitLValue(*IRef).getAddress(); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 889 | if (CopiedVars.size() == 1) { |
| 890 | // At first check if current thread is a master thread. If it is, no |
| 891 | // need to copy data. |
| 892 | CopyBegin = createBasicBlock("copyin.not.master"); |
| 893 | CopyEnd = createBasicBlock("copyin.not.master.end"); |
| 894 | Builder.CreateCondBr( |
| 895 | Builder.CreateICmpNE( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 896 | Builder.CreatePtrToInt(MasterAddr.getPointer(), CGM.IntPtrTy), |
| 897 | Builder.CreatePtrToInt(PrivateAddr.getPointer(), CGM.IntPtrTy)), |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 898 | CopyBegin, CopyEnd); |
| 899 | EmitBlock(CopyBegin); |
| 900 | } |
| 901 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 902 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 903 | EmitOMPCopy(Type, PrivateAddr, MasterAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 904 | } |
| 905 | ++IRef; |
| 906 | ++ISrcRef; |
| 907 | ++IDestRef; |
| 908 | } |
| 909 | } |
| 910 | if (CopyEnd) { |
| 911 | // Exit out of copying procedure for non-master thread. |
| 912 | EmitBlock(CopyEnd, /*IsFinished=*/true); |
| 913 | return true; |
| 914 | } |
| 915 | return false; |
| 916 | } |
| 917 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 918 | bool CodeGenFunction::EmitOMPLastprivateClauseInit( |
| 919 | const OMPExecutableDirective &D, OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 920 | if (!HaveInsertPoint()) |
| 921 | return false; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 922 | bool HasAtLeastOneLastprivate = false; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 923 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 924 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 925 | auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 926 | for (auto *C : LoopDirective->counters()) { |
| 927 | SIMDLCVs.insert( |
| 928 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 929 | } |
| 930 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 931 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 932 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 933 | HasAtLeastOneLastprivate = true; |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 934 | if (isOpenMPTaskLoopDirective(D.getDirectiveKind()) && |
| 935 | !getLangOpts().OpenMPSimd) |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 936 | break; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 937 | auto IRef = C->varlist_begin(); |
| 938 | auto IDestRef = C->destination_exprs().begin(); |
| 939 | for (auto *IInit : C->private_copies()) { |
| 940 | // Keep the address of the original variable for future update at the end |
| 941 | // of the loop. |
| 942 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 943 | // Taskloops do not require additional initialization, it is done in |
| 944 | // runtime support library. |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 945 | if (AlreadyEmittedVars.insert(OrigVD->getCanonicalDecl()).second) { |
| 946 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 947 | PrivateScope.addPrivate(DestVD, [this, OrigVD, IRef]() -> Address { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 948 | DeclRefExpr DRE( |
| 949 | const_cast<VarDecl *>(OrigVD), |
| 950 | /*RefersToEnclosingVariableOrCapture=*/CapturedStmtInfo->lookup( |
| 951 | OrigVD) != nullptr, |
| 952 | (*IRef)->getType(), VK_LValue, (*IRef)->getExprLoc()); |
| 953 | return EmitLValue(&DRE).getAddress(); |
| 954 | }); |
| 955 | // Check if the variable is also a firstprivate: in this case IInit is |
| 956 | // not generated. Initialization of this variable will happen in codegen |
| 957 | // for 'firstprivate' clause. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 958 | if (IInit && !SIMDLCVs.count(OrigVD->getCanonicalDecl())) { |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 959 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IInit)->getDecl()); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 960 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 961 | // Emit private VarDecl with copy init. |
| 962 | EmitDecl(*VD); |
| 963 | return GetAddrOfLocalVar(VD); |
| 964 | }); |
Alexey Bataev | d130fd1 | 2015-05-13 10:23:02 +0000 | [diff] [blame] | 965 | assert(IsRegistered && |
| 966 | "lastprivate var already registered as private"); |
| 967 | (void)IsRegistered; |
| 968 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 969 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 970 | ++IRef; |
| 971 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | return HasAtLeastOneLastprivate; |
| 975 | } |
| 976 | |
| 977 | void CodeGenFunction::EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 978 | const OMPExecutableDirective &D, bool NoFinals, |
| 979 | llvm::Value *IsLastIterCond) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 980 | if (!HaveInsertPoint()) |
| 981 | return; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 982 | // Emit following code: |
| 983 | // if (<IsLastIterCond>) { |
| 984 | // orig_var1 = private_orig_var1; |
| 985 | // ... |
| 986 | // orig_varn = private_orig_varn; |
| 987 | // } |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 988 | llvm::BasicBlock *ThenBB = nullptr; |
| 989 | llvm::BasicBlock *DoneBB = nullptr; |
| 990 | if (IsLastIterCond) { |
| 991 | ThenBB = createBasicBlock(".omp.lastprivate.then"); |
| 992 | DoneBB = createBasicBlock(".omp.lastprivate.done"); |
| 993 | Builder.CreateCondBr(IsLastIterCond, ThenBB, DoneBB); |
| 994 | EmitBlock(ThenBB); |
| 995 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 996 | llvm::DenseSet<const VarDecl *> AlreadyEmittedVars; |
| 997 | llvm::DenseMap<const VarDecl *, const Expr *> LoopCountersAndUpdates; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 998 | if (auto *LoopDirective = dyn_cast<OMPLoopDirective>(&D)) { |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 999 | auto IC = LoopDirective->counters().begin(); |
| 1000 | for (auto F : LoopDirective->finals()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1001 | auto *D = |
| 1002 | cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl())->getCanonicalDecl(); |
| 1003 | if (NoFinals) |
| 1004 | AlreadyEmittedVars.insert(D); |
| 1005 | else |
| 1006 | LoopCountersAndUpdates[D] = F; |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1007 | ++IC; |
Alexey Bataev | 7a228ff | 2015-05-21 07:59:51 +0000 | [diff] [blame] | 1008 | } |
| 1009 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1010 | for (const auto *C : D.getClausesOfKind<OMPLastprivateClause>()) { |
| 1011 | auto IRef = C->varlist_begin(); |
| 1012 | auto ISrcRef = C->source_exprs().begin(); |
| 1013 | auto IDestRef = C->destination_exprs().begin(); |
| 1014 | for (auto *AssignOp : C->assignment_ops()) { |
| 1015 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 1016 | QualType Type = PrivateVD->getType(); |
| 1017 | auto *CanonicalVD = PrivateVD->getCanonicalDecl(); |
| 1018 | if (AlreadyEmittedVars.insert(CanonicalVD).second) { |
| 1019 | // If lastprivate variable is a loop control variable for loop-based |
| 1020 | // directive, update its value before copyin back to original |
| 1021 | // variable. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1022 | if (auto *FinalExpr = LoopCountersAndUpdates.lookup(CanonicalVD)) |
| 1023 | EmitIgnoredExpr(FinalExpr); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1024 | auto *SrcVD = cast<VarDecl>(cast<DeclRefExpr>(*ISrcRef)->getDecl()); |
| 1025 | auto *DestVD = cast<VarDecl>(cast<DeclRefExpr>(*IDestRef)->getDecl()); |
| 1026 | // Get the address of the original variable. |
| 1027 | Address OriginalAddr = GetAddrOfLocalVar(DestVD); |
| 1028 | // Get the address of the private variable. |
| 1029 | Address PrivateAddr = GetAddrOfLocalVar(PrivateVD); |
| 1030 | if (auto RefTy = PrivateVD->getType()->getAs<ReferenceType>()) |
| 1031 | PrivateAddr = |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1032 | Address(Builder.CreateLoad(PrivateAddr), |
| 1033 | getNaturalTypeAlignment(RefTy->getPointeeType())); |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1034 | EmitOMPCopy(Type, OriginalAddr, PrivateAddr, DestVD, SrcVD, AssignOp); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1035 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1036 | ++IRef; |
| 1037 | ++ISrcRef; |
| 1038 | ++IDestRef; |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1039 | } |
Alexey Bataev | 005248a | 2016-02-25 05:25:57 +0000 | [diff] [blame] | 1040 | if (auto *PostUpdate = C->getPostUpdateExpr()) |
| 1041 | EmitIgnoredExpr(PostUpdate); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1042 | } |
Alexey Bataev | 8ffcc94 | 2016-02-18 13:48:15 +0000 | [diff] [blame] | 1043 | if (IsLastIterCond) |
Alexey Bataev | fc087ec | 2015-06-16 13:14:42 +0000 | [diff] [blame] | 1044 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1047 | void CodeGenFunction::EmitOMPReductionClauseInit( |
| 1048 | const OMPExecutableDirective &D, |
| 1049 | CodeGenFunction::OMPPrivateScope &PrivateScope) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1050 | if (!HaveInsertPoint()) |
| 1051 | return; |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1052 | SmallVector<const Expr *, 4> Shareds; |
| 1053 | SmallVector<const Expr *, 4> Privates; |
| 1054 | SmallVector<const Expr *, 4> ReductionOps; |
| 1055 | SmallVector<const Expr *, 4> LHSs; |
| 1056 | SmallVector<const Expr *, 4> RHSs; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1057 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1058 | auto IPriv = C->privates().begin(); |
Alexey Bataev | a839ddd | 2016-03-17 10:19:46 +0000 | [diff] [blame] | 1059 | auto IRed = C->reduction_ops().begin(); |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1060 | auto ILHS = C->lhs_exprs().begin(); |
| 1061 | auto IRHS = C->rhs_exprs().begin(); |
| 1062 | for (const auto *Ref : C->varlists()) { |
| 1063 | Shareds.emplace_back(Ref); |
| 1064 | Privates.emplace_back(*IPriv); |
| 1065 | ReductionOps.emplace_back(*IRed); |
| 1066 | LHSs.emplace_back(*ILHS); |
| 1067 | RHSs.emplace_back(*IRHS); |
| 1068 | std::advance(IPriv, 1); |
| 1069 | std::advance(IRed, 1); |
| 1070 | std::advance(ILHS, 1); |
| 1071 | std::advance(IRHS, 1); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1072 | } |
| 1073 | } |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1074 | ReductionCodeGen RedCG(Shareds, Privates, ReductionOps); |
| 1075 | unsigned Count = 0; |
| 1076 | auto ILHS = LHSs.begin(); |
| 1077 | auto IRHS = RHSs.begin(); |
| 1078 | auto IPriv = Privates.begin(); |
| 1079 | for (const auto *IRef : Shareds) { |
| 1080 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*IPriv)->getDecl()); |
| 1081 | // Emit private VarDecl with reduction init. |
| 1082 | RedCG.emitSharedLValue(*this, Count); |
| 1083 | RedCG.emitAggregateType(*this, Count); |
| 1084 | auto Emission = EmitAutoVarAlloca(*PrivateVD); |
| 1085 | RedCG.emitInitialization(*this, Count, Emission.getAllocatedAddress(), |
| 1086 | RedCG.getSharedLValue(Count), |
| 1087 | [&Emission](CodeGenFunction &CGF) { |
| 1088 | CGF.EmitAutoVarInit(Emission); |
| 1089 | return true; |
| 1090 | }); |
| 1091 | EmitAutoVarCleanups(Emission); |
| 1092 | Address BaseAddr = RedCG.adjustPrivateAddress( |
| 1093 | *this, Count, Emission.getAllocatedAddress()); |
| 1094 | bool IsRegistered = PrivateScope.addPrivate( |
| 1095 | RedCG.getBaseDecl(Count), [BaseAddr]() -> Address { return BaseAddr; }); |
| 1096 | assert(IsRegistered && "private var already registered as private"); |
| 1097 | // Silence the warning about unused variable. |
| 1098 | (void)IsRegistered; |
| 1099 | |
| 1100 | auto *LHSVD = cast<VarDecl>(cast<DeclRefExpr>(*ILHS)->getDecl()); |
| 1101 | auto *RHSVD = cast<VarDecl>(cast<DeclRefExpr>(*IRHS)->getDecl()); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1102 | QualType Type = PrivateVD->getType(); |
| 1103 | bool isaOMPArraySectionExpr = isa<OMPArraySectionExpr>(IRef); |
| 1104 | if (isaOMPArraySectionExpr && Type->isVariablyModifiedType()) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1105 | // Store the address of the original variable associated with the LHS |
| 1106 | // implicit variable. |
| 1107 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address { |
| 1108 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1109 | }); |
| 1110 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD]() -> Address { |
| 1111 | return GetAddrOfLocalVar(PrivateVD); |
| 1112 | }); |
Jonas Hahnfeld | 4525c82 | 2017-10-23 19:01:35 +0000 | [diff] [blame] | 1113 | } else if ((isaOMPArraySectionExpr && Type->isScalarType()) || |
| 1114 | isa<ArraySubscriptExpr>(IRef)) { |
Alexey Bataev | 5c40bec | 2017-07-13 13:36:14 +0000 | [diff] [blame] | 1115 | // Store the address of the original variable associated with the LHS |
| 1116 | // implicit variable. |
| 1117 | PrivateScope.addPrivate(LHSVD, [&RedCG, Count]() -> Address { |
| 1118 | return RedCG.getSharedLValue(Count).getAddress(); |
| 1119 | }); |
| 1120 | PrivateScope.addPrivate(RHSVD, [this, PrivateVD, RHSVD]() -> Address { |
| 1121 | return Builder.CreateElementBitCast(GetAddrOfLocalVar(PrivateVD), |
| 1122 | ConvertTypeForMem(RHSVD->getType()), |
| 1123 | "rhs.begin"); |
| 1124 | }); |
| 1125 | } else { |
| 1126 | QualType Type = PrivateVD->getType(); |
| 1127 | bool IsArray = getContext().getAsArrayType(Type) != nullptr; |
| 1128 | Address OriginalAddr = RedCG.getSharedLValue(Count).getAddress(); |
| 1129 | // Store the address of the original variable associated with the LHS |
| 1130 | // implicit variable. |
| 1131 | if (IsArray) { |
| 1132 | OriginalAddr = Builder.CreateElementBitCast( |
| 1133 | OriginalAddr, ConvertTypeForMem(LHSVD->getType()), "lhs.begin"); |
| 1134 | } |
| 1135 | PrivateScope.addPrivate( |
| 1136 | LHSVD, [OriginalAddr]() -> Address { return OriginalAddr; }); |
| 1137 | PrivateScope.addPrivate( |
| 1138 | RHSVD, [this, PrivateVD, RHSVD, IsArray]() -> Address { |
| 1139 | return IsArray |
| 1140 | ? Builder.CreateElementBitCast( |
| 1141 | GetAddrOfLocalVar(PrivateVD), |
| 1142 | ConvertTypeForMem(RHSVD->getType()), "rhs.begin") |
| 1143 | : GetAddrOfLocalVar(PrivateVD); |
| 1144 | }); |
| 1145 | } |
| 1146 | ++ILHS; |
| 1147 | ++IRHS; |
| 1148 | ++IPriv; |
| 1149 | ++Count; |
| 1150 | } |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | void CodeGenFunction::EmitOMPReductionClauseFinal( |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1154 | const OMPExecutableDirective &D, const OpenMPDirectiveKind ReductionKind) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1155 | if (!HaveInsertPoint()) |
| 1156 | return; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1157 | llvm::SmallVector<const Expr *, 8> Privates; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1158 | llvm::SmallVector<const Expr *, 8> LHSExprs; |
| 1159 | llvm::SmallVector<const Expr *, 8> RHSExprs; |
| 1160 | llvm::SmallVector<const Expr *, 8> ReductionOps; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1161 | bool HasAtLeastOneReduction = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1162 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1163 | HasAtLeastOneReduction = true; |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1164 | Privates.append(C->privates().begin(), C->privates().end()); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1165 | LHSExprs.append(C->lhs_exprs().begin(), C->lhs_exprs().end()); |
| 1166 | RHSExprs.append(C->rhs_exprs().begin(), C->rhs_exprs().end()); |
| 1167 | ReductionOps.append(C->reduction_ops().begin(), C->reduction_ops().end()); |
| 1168 | } |
| 1169 | if (HasAtLeastOneReduction) { |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1170 | bool WithNowait = D.getSingleClause<OMPNowaitClause>() || |
| 1171 | isOpenMPParallelDirective(D.getDirectiveKind()) || |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1172 | ReductionKind == OMPD_simd; |
| 1173 | bool SimpleReduction = ReductionKind == OMPD_simd; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1174 | // Emit nowait reduction if nowait clause is present or directive is a |
| 1175 | // parallel directive (it always has implicit barrier). |
| 1176 | CGM.getOpenMPRuntime().emitReduction( |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 1177 | *this, D.getLocEnd(), Privates, LHSExprs, RHSExprs, ReductionOps, |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1178 | {WithNowait, SimpleReduction, ReductionKind}); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 1179 | } |
| 1180 | } |
| 1181 | |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1182 | static void emitPostUpdateForReductionClause( |
| 1183 | CodeGenFunction &CGF, const OMPExecutableDirective &D, |
| 1184 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
| 1185 | if (!CGF.HaveInsertPoint()) |
| 1186 | return; |
| 1187 | llvm::BasicBlock *DoneBB = nullptr; |
| 1188 | for (const auto *C : D.getClausesOfKind<OMPReductionClause>()) { |
| 1189 | if (auto *PostUpdate = C->getPostUpdateExpr()) { |
| 1190 | if (!DoneBB) { |
| 1191 | if (auto *Cond = CondGen(CGF)) { |
| 1192 | // If the first post-update expression is found, emit conditional |
| 1193 | // block if it was requested. |
| 1194 | auto *ThenBB = CGF.createBasicBlock(".omp.reduction.pu"); |
| 1195 | DoneBB = CGF.createBasicBlock(".omp.reduction.pu.done"); |
| 1196 | CGF.Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1197 | CGF.EmitBlock(ThenBB); |
| 1198 | } |
| 1199 | } |
| 1200 | CGF.EmitIgnoredExpr(PostUpdate); |
| 1201 | } |
| 1202 | } |
| 1203 | if (DoneBB) |
| 1204 | CGF.EmitBlock(DoneBB, /*IsFinished=*/true); |
| 1205 | } |
| 1206 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1207 | namespace { |
| 1208 | /// Codegen lambda for appending distribute lower and upper bounds to outlined |
| 1209 | /// parallel function. This is necessary for combined constructs such as |
| 1210 | /// 'distribute parallel for' |
| 1211 | typedef llvm::function_ref<void(CodeGenFunction &, |
| 1212 | const OMPExecutableDirective &, |
| 1213 | llvm::SmallVectorImpl<llvm::Value *> &)> |
| 1214 | CodeGenBoundParametersTy; |
| 1215 | } // anonymous namespace |
| 1216 | |
| 1217 | static void emitCommonOMPParallelDirective( |
| 1218 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 1219 | OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen, |
| 1220 | const CodeGenBoundParametersTy &CodeGenBoundParameters) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 1221 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_parallel); |
| 1222 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitParallelOutlinedFunction( |
| 1223 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1224 | if (const auto *NumThreadsClause = S.getSingleClause<OMPNumThreadsClause>()) { |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1225 | CodeGenFunction::RunCleanupsScope NumThreadsScope(CGF); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1226 | auto NumThreads = CGF.EmitScalarExpr(NumThreadsClause->getNumThreads(), |
| 1227 | /*IgnoreResultAssign*/ true); |
| 1228 | CGF.CGM.getOpenMPRuntime().emitNumThreadsClause( |
| 1229 | CGF, NumThreads, NumThreadsClause->getLocStart()); |
| 1230 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1231 | if (const auto *ProcBindClause = S.getSingleClause<OMPProcBindClause>()) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1232 | CodeGenFunction::RunCleanupsScope ProcBindScope(CGF); |
Alexey Bataev | 7f210c6 | 2015-06-18 13:40:03 +0000 | [diff] [blame] | 1233 | CGF.CGM.getOpenMPRuntime().emitProcBindClause( |
| 1234 | CGF, ProcBindClause->getProcBindKind(), ProcBindClause->getLocStart()); |
| 1235 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1236 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 1237 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 1238 | if (C->getNameModifier() == OMPD_unknown || |
| 1239 | C->getNameModifier() == OMPD_parallel) { |
| 1240 | IfCond = C->getCondition(); |
| 1241 | break; |
| 1242 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1243 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1244 | |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 1245 | OMPParallelScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1246 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1247 | // Combining 'distribute' with 'for' requires sharing each 'distribute' chunk |
| 1248 | // lower and upper bounds with the pragma 'for' chunking mechanism. |
| 1249 | // The following lambda takes care of appending the lower and upper bound |
| 1250 | // parameters when necessary |
| 1251 | CodeGenBoundParameters(CGF, S, CapturedVars); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1252 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 1253 | CGF.CGM.getOpenMPRuntime().emitParallelCall(CGF, S.getLocStart(), OutlinedFn, |
Alexey Bataev | 2377fe9 | 2015-09-10 08:12:02 +0000 | [diff] [blame] | 1254 | CapturedVars, IfCond); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1255 | } |
| 1256 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1257 | static void emitEmptyBoundParameters(CodeGenFunction &, |
| 1258 | const OMPExecutableDirective &, |
| 1259 | llvm::SmallVectorImpl<llvm::Value *> &) {} |
| 1260 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1261 | void CodeGenFunction::EmitOMPParallelDirective(const OMPParallelDirective &S) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1262 | // Emit parallel region as a standalone region. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 1263 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1264 | OMPPrivateScope PrivateScope(CGF); |
Alexey Bataev | f56f98c | 2015-04-16 05:39:01 +0000 | [diff] [blame] | 1265 | bool Copyins = CGF.EmitOMPCopyinClause(S); |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1266 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 1267 | if (Copyins) { |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1268 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 1269 | // propagation master's thread values of threadprivate variables to local |
| 1270 | // instances of that variables of all other implicit threads. |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 1271 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 1272 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 1273 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 69c62a9 | 2015-04-15 04:52:20 +0000 | [diff] [blame] | 1274 | } |
| 1275 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 1276 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 1277 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1278 | CGF.EmitStmt(S.getCapturedStmt(OMPD_parallel)->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 1279 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1280 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1281 | emitCommonOMPParallelDirective(*this, S, OMPD_parallel, CodeGen, |
| 1282 | emitEmptyBoundParameters); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 1283 | emitPostUpdateForReductionClause( |
| 1284 | *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 9959db5 | 2014-05-06 10:08:46 +0000 | [diff] [blame] | 1285 | } |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1286 | |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1287 | void CodeGenFunction::EmitOMPLoopBody(const OMPLoopDirective &D, |
| 1288 | JumpDest LoopExit) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1289 | RunCleanupsScope BodyScope(*this); |
| 1290 | // Update counters values on current iteration. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1291 | for (auto I : D.updates()) { |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1292 | EmitIgnoredExpr(I); |
| 1293 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1294 | // Update the linear variables. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 1295 | // In distribute directives only loop counters may be marked as linear, no |
| 1296 | // need to generate the code for them. |
| 1297 | if (!isOpenMPDistributeDirective(D.getDirectiveKind())) { |
| 1298 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
| 1299 | for (auto *U : C->updates()) |
| 1300 | EmitIgnoredExpr(U); |
| 1301 | } |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1304 | // On a continue in the body, jump to the end. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1305 | auto Continue = getJumpDestInCurrentScope("omp.body.continue"); |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 1306 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1307 | // Emit loop body. |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1308 | EmitStmt(D.getBody()); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1309 | // The end (updates/cleanups). |
| 1310 | EmitBlock(Continue.getBlock()); |
| 1311 | BreakContinueStack.pop_back(); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1314 | void CodeGenFunction::EmitOMPInnerLoop( |
| 1315 | const Stmt &S, bool RequiresCleanup, const Expr *LoopCond, |
| 1316 | const Expr *IncExpr, |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1317 | const llvm::function_ref<void(CodeGenFunction &)> &BodyGen, |
| 1318 | const llvm::function_ref<void(CodeGenFunction &)> &PostIncGen) { |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1319 | auto LoopExit = getJumpDestInCurrentScope("omp.inner.for.end"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1320 | |
| 1321 | // Start the loop with a block that tests the condition. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1322 | auto CondBlock = createBasicBlock("omp.inner.for.cond"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1323 | EmitBlock(CondBlock); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1324 | const SourceRange &R = S.getSourceRange(); |
| 1325 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1326 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1327 | |
| 1328 | // If there are any cleanups between here and the loop-exit scope, |
| 1329 | // create a block to stage a loop exit along. |
| 1330 | auto ExitBlock = LoopExit.getBlock(); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1331 | if (RequiresCleanup) |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1332 | ExitBlock = createBasicBlock("omp.inner.for.cond.cleanup"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1333 | |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1334 | auto LoopBody = createBasicBlock("omp.inner.for.body"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1335 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1336 | // Emit condition. |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1337 | EmitBranchOnBoolExpr(LoopCond, LoopBody, ExitBlock, getProfileCount(&S)); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1338 | if (ExitBlock != LoopExit.getBlock()) { |
| 1339 | EmitBlock(ExitBlock); |
| 1340 | EmitBranchThroughCleanup(LoopExit); |
| 1341 | } |
| 1342 | |
| 1343 | EmitBlock(LoopBody); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 1344 | incrementProfileCounter(&S); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1345 | |
| 1346 | // Create a block for the increment. |
Alexander Musman | d196ef2 | 2014-10-07 08:57:09 +0000 | [diff] [blame] | 1347 | auto Continue = getJumpDestInCurrentScope("omp.inner.for.inc"); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1348 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1349 | |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1350 | BodyGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1351 | |
| 1352 | // Emit "IV = IV + 1" and a back-edge to the condition block. |
| 1353 | EmitBlock(Continue.getBlock()); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 1354 | EmitIgnoredExpr(IncExpr); |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1355 | PostIncGen(*this); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1356 | BreakContinueStack.pop_back(); |
| 1357 | EmitBranch(CondBlock); |
| 1358 | LoopStack.pop(); |
| 1359 | // Emit the fall-through block. |
| 1360 | EmitBlock(LoopExit.getBlock()); |
| 1361 | } |
| 1362 | |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1363 | bool CodeGenFunction::EmitOMPLinearClauseInit(const OMPLoopDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1364 | if (!HaveInsertPoint()) |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1365 | return false; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1366 | // Emit inits for the linear variables. |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1367 | bool HasLinears = false; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1368 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1369 | for (auto *Init : C->inits()) { |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1370 | HasLinears = true; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1371 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(Init)->getDecl()); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1372 | if (auto *Ref = dyn_cast<DeclRefExpr>(VD->getInit()->IgnoreImpCasts())) { |
| 1373 | AutoVarEmission Emission = EmitAutoVarAlloca(*VD); |
| 1374 | auto *OrigVD = cast<VarDecl>(Ref->getDecl()); |
| 1375 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
| 1376 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
| 1377 | VD->getInit()->getType(), VK_LValue, |
| 1378 | VD->getInit()->getExprLoc()); |
| 1379 | EmitExprAsInit(&DRE, VD, MakeAddrLValue(Emission.getAllocatedAddress(), |
| 1380 | VD->getType()), |
| 1381 | /*capturedByInit=*/false); |
| 1382 | EmitAutoVarCleanups(Emission); |
| 1383 | } else |
| 1384 | EmitVarDecl(*VD); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1385 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1386 | // Emit the linear steps for the linear clauses. |
| 1387 | // If a step is not constant, it is pre-calculated before the loop. |
| 1388 | if (auto CS = cast_or_null<BinaryOperator>(C->getCalcStep())) |
| 1389 | if (auto SaveRef = cast<DeclRefExpr>(CS->getLHS())) { |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1390 | EmitVarDecl(*cast<VarDecl>(SaveRef->getDecl())); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1391 | // Emit calculation of the linear step. |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 1392 | EmitIgnoredExpr(CS); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1393 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1394 | } |
Alexey Bataev | 8c3edfe | 2017-08-16 15:58:46 +0000 | [diff] [blame] | 1395 | return HasLinears; |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1396 | } |
| 1397 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1398 | void CodeGenFunction::EmitOMPLinearClauseFinal( |
| 1399 | const OMPLoopDirective &D, |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1400 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1401 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1402 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1403 | llvm::BasicBlock *DoneBB = nullptr; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1404 | // Emit the final values of the linear variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1405 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1406 | auto IC = C->varlist_begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1407 | for (auto *F : C->finals()) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1408 | if (!DoneBB) { |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1409 | if (auto *Cond = CondGen(*this)) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1410 | // If the first post-update expression is found, emit conditional |
| 1411 | // block if it was requested. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1412 | auto *ThenBB = createBasicBlock(".omp.linear.pu"); |
| 1413 | DoneBB = createBasicBlock(".omp.linear.pu.done"); |
| 1414 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1415 | EmitBlock(ThenBB); |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1416 | } |
| 1417 | } |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1418 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IC)->getDecl()); |
| 1419 | DeclRefExpr DRE(const_cast<VarDecl *>(OrigVD), |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1420 | CapturedStmtInfo->lookup(OrigVD) != nullptr, |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1421 | (*IC)->getType(), VK_LValue, (*IC)->getExprLoc()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1422 | Address OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1423 | CodeGenFunction::OMPPrivateScope VarScope(*this); |
| 1424 | VarScope.addPrivate(OrigVD, [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1425 | (void)VarScope.Privatize(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1426 | EmitIgnoredExpr(F); |
Alexey Bataev | 39f915b8 | 2015-05-08 10:41:21 +0000 | [diff] [blame] | 1427 | ++IC; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1428 | } |
Alexey Bataev | 78849fb | 2016-03-09 09:49:00 +0000 | [diff] [blame] | 1429 | if (auto *PostUpdate = C->getPostUpdateExpr()) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1430 | EmitIgnoredExpr(PostUpdate); |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1431 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1432 | if (DoneBB) |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1433 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1436 | static void emitAlignedClause(CodeGenFunction &CGF, |
| 1437 | const OMPExecutableDirective &D) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1438 | if (!CGF.HaveInsertPoint()) |
| 1439 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1440 | for (const auto *Clause : D.getClausesOfKind<OMPAlignedClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1441 | unsigned ClauseAlignment = 0; |
| 1442 | if (auto AlignmentExpr = Clause->getAlignment()) { |
| 1443 | auto AlignmentCI = |
| 1444 | cast<llvm::ConstantInt>(CGF.EmitScalarExpr(AlignmentExpr)); |
| 1445 | ClauseAlignment = static_cast<unsigned>(AlignmentCI->getZExtValue()); |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1446 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1447 | for (auto E : Clause->varlists()) { |
| 1448 | unsigned Alignment = ClauseAlignment; |
| 1449 | if (Alignment == 0) { |
| 1450 | // OpenMP [2.8.1, Description] |
| 1451 | // If no optional parameter is specified, implementation-defined default |
| 1452 | // alignments for SIMD instructions on the target platforms are assumed. |
| 1453 | Alignment = |
Alexey Bataev | 0039651 | 2015-07-02 03:40:19 +0000 | [diff] [blame] | 1454 | CGF.getContext() |
| 1455 | .toCharUnitsFromBits(CGF.getContext().getOpenMPDefaultSimdAlign( |
| 1456 | E->getType()->getPointeeType())) |
| 1457 | .getQuantity(); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1458 | } |
| 1459 | assert((Alignment == 0 || llvm::isPowerOf2_32(Alignment)) && |
| 1460 | "alignment is not power of 2"); |
| 1461 | if (Alignment != 0) { |
| 1462 | llvm::Value *PtrValue = CGF.EmitScalarExpr(E); |
| 1463 | CGF.EmitAlignmentAssumption(PtrValue, Alignment); |
| 1464 | } |
Alexander Musman | 09184fe | 2014-09-30 05:29:28 +0000 | [diff] [blame] | 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1469 | void CodeGenFunction::EmitOMPPrivateLoopCounters( |
| 1470 | const OMPLoopDirective &S, CodeGenFunction::OMPPrivateScope &LoopScope) { |
| 1471 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1472 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1473 | auto I = S.private_counters().begin(); |
| 1474 | for (auto *E : S.counters()) { |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1475 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1476 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>(*I)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1477 | (void)LoopScope.addPrivate(VD, [&]() -> Address { |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1478 | // Emit var without initialization. |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1479 | if (!LocalDeclMap.count(PrivateVD)) { |
| 1480 | auto VarEmission = EmitAutoVarAlloca(*PrivateVD); |
| 1481 | EmitAutoVarCleanups(VarEmission); |
| 1482 | } |
| 1483 | DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), |
| 1484 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1485 | (*I)->getType(), VK_LValue, (*I)->getExprLoc()); |
| 1486 | return EmitLValue(&DRE).getAddress(); |
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()) { |
| 1490 | (void)LoopScope.addPrivate(PrivateVD, [&]() -> Address { |
| 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 | }); |
| 1496 | } |
Alexey Bataev | a889917 | 2015-08-06 12:30:57 +0000 | [diff] [blame] | 1497 | ++I; |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1498 | } |
Alexey Bataev | 435ad7b | 2014-10-10 09:48:26 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1501 | static void emitPreCond(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1502 | const Expr *Cond, llvm::BasicBlock *TrueBlock, |
| 1503 | llvm::BasicBlock *FalseBlock, uint64_t TrueCount) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1504 | if (!CGF.HaveInsertPoint()) |
| 1505 | return; |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1506 | { |
| 1507 | CodeGenFunction::OMPPrivateScope PreCondScope(CGF); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1508 | CGF.EmitOMPPrivateLoopCounters(S, PreCondScope); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1509 | (void)PreCondScope.Privatize(); |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1510 | // Get initial values of real counters. |
Alexey Bataev | b08f89f | 2015-08-14 12:25:37 +0000 | [diff] [blame] | 1511 | for (auto I : S.inits()) { |
Alexey Bataev | 6e8248f | 2015-06-11 10:53:56 +0000 | [diff] [blame] | 1512 | CGF.EmitIgnoredExpr(I); |
| 1513 | } |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 1514 | } |
| 1515 | // Check that loop is executed at least one time. |
| 1516 | CGF.EmitBranchOnBoolExpr(Cond, TrueBlock, FalseBlock, TrueCount); |
| 1517 | } |
| 1518 | |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1519 | void CodeGenFunction::EmitOMPLinearClause( |
| 1520 | const OMPLoopDirective &D, CodeGenFunction::OMPPrivateScope &PrivateScope) { |
| 1521 | if (!HaveInsertPoint()) |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1522 | return; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1523 | llvm::DenseSet<const VarDecl *> SIMDLCVs; |
| 1524 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 1525 | auto *LoopDirective = cast<OMPLoopDirective>(&D); |
| 1526 | for (auto *C : LoopDirective->counters()) { |
| 1527 | SIMDLCVs.insert( |
| 1528 | cast<VarDecl>(cast<DeclRefExpr>(C)->getDecl())->getCanonicalDecl()); |
| 1529 | } |
| 1530 | } |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1531 | for (const auto *C : D.getClausesOfKind<OMPLinearClause>()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1532 | auto CurPrivate = C->privates().begin(); |
Alexey Bataev | c925aa3 | 2015-04-27 08:00:32 +0000 | [diff] [blame] | 1533 | for (auto *E : C->varlists()) { |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1534 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 1535 | auto *PrivateVD = |
| 1536 | cast<VarDecl>(cast<DeclRefExpr>(*CurPrivate)->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1537 | if (!SIMDLCVs.count(VD->getCanonicalDecl())) { |
| 1538 | bool IsRegistered = PrivateScope.addPrivate(VD, [&]() -> Address { |
| 1539 | // Emit private VarDecl with copy init. |
| 1540 | EmitVarDecl(*PrivateVD); |
| 1541 | return GetAddrOfLocalVar(PrivateVD); |
| 1542 | }); |
| 1543 | assert(IsRegistered && "linear var already registered as private"); |
| 1544 | // Silence the warning about unused variable. |
| 1545 | (void)IsRegistered; |
| 1546 | } else |
| 1547 | EmitVarDecl(*PrivateVD); |
Alexey Bataev | bd9fec1 | 2015-08-18 06:47:21 +0000 | [diff] [blame] | 1548 | ++CurPrivate; |
Alexander Musman | 3276a27 | 2015-03-21 10:12:56 +0000 | [diff] [blame] | 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1553 | static void emitSimdlenSafelenClause(CodeGenFunction &CGF, |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1554 | const OMPExecutableDirective &D, |
| 1555 | bool IsMonotonic) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1556 | if (!CGF.HaveInsertPoint()) |
| 1557 | return; |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1558 | if (const auto *C = D.getSingleClause<OMPSimdlenClause>()) { |
Alexey Bataev | 45bfad5 | 2015-08-21 12:19:04 +0000 | [diff] [blame] | 1559 | RValue Len = CGF.EmitAnyExpr(C->getSimdlen(), AggValueSlot::ignored(), |
| 1560 | /*ignoreResult=*/true); |
| 1561 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
| 1562 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
| 1563 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1564 | // the memory instructions parallel, because loop-carried |
| 1565 | // dependences of 'safelen' iterations are possible. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1566 | if (!IsMonotonic) |
| 1567 | CGF.LoopStack.setParallel(!D.getSingleClause<OMPSafelenClause>()); |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 1568 | } else if (const auto *C = D.getSingleClause<OMPSafelenClause>()) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1569 | RValue Len = CGF.EmitAnyExpr(C->getSafelen(), AggValueSlot::ignored(), |
| 1570 | /*ignoreResult=*/true); |
| 1571 | llvm::ConstantInt *Val = cast<llvm::ConstantInt>(Len.getScalarVal()); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1572 | CGF.LoopStack.setVectorizeWidth(Val->getZExtValue()); |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 1573 | // In presence of finite 'safelen', it may be unsafe to mark all |
| 1574 | // the memory instructions parallel, because loop-carried |
| 1575 | // dependences of 'safelen' iterations are possible. |
| 1576 | CGF.LoopStack.setParallel(false); |
| 1577 | } |
| 1578 | } |
| 1579 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1580 | void CodeGenFunction::EmitOMPSimdInit(const OMPLoopDirective &D, |
| 1581 | bool IsMonotonic) { |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1582 | // Walk clauses and process safelen/lastprivate. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1583 | LoopStack.setParallel(!IsMonotonic); |
Tyler Nowicki | da46d0e | 2015-07-14 23:03:09 +0000 | [diff] [blame] | 1584 | LoopStack.setVectorizeEnable(true); |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1585 | emitSimdlenSafelenClause(*this, D, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1586 | } |
| 1587 | |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1588 | void CodeGenFunction::EmitOMPSimdFinal( |
| 1589 | const OMPLoopDirective &D, |
| 1590 | const llvm::function_ref<llvm::Value *(CodeGenFunction &)> &CondGen) { |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 1591 | if (!HaveInsertPoint()) |
| 1592 | return; |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1593 | llvm::BasicBlock *DoneBB = nullptr; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1594 | auto IC = D.counters().begin(); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1595 | auto IPC = D.private_counters().begin(); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1596 | for (auto F : D.finals()) { |
| 1597 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>((*IC))->getDecl()); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1598 | auto *PrivateVD = cast<VarDecl>(cast<DeclRefExpr>((*IPC))->getDecl()); |
| 1599 | auto *CED = dyn_cast<OMPCapturedExprDecl>(OrigVD); |
| 1600 | if (LocalDeclMap.count(OrigVD) || CapturedStmtInfo->lookup(OrigVD) || |
| 1601 | OrigVD->hasGlobalStorage() || CED) { |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1602 | if (!DoneBB) { |
| 1603 | if (auto *Cond = CondGen(*this)) { |
| 1604 | // If the first post-update expression is found, emit conditional |
| 1605 | // block if it was requested. |
| 1606 | auto *ThenBB = createBasicBlock(".omp.final.then"); |
| 1607 | DoneBB = createBasicBlock(".omp.final.done"); |
| 1608 | Builder.CreateCondBr(Cond, ThenBB, DoneBB); |
| 1609 | EmitBlock(ThenBB); |
| 1610 | } |
| 1611 | } |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1612 | Address OrigAddr = Address::invalid(); |
| 1613 | if (CED) |
| 1614 | OrigAddr = EmitLValue(CED->getInit()->IgnoreImpCasts()).getAddress(); |
| 1615 | else { |
| 1616 | DeclRefExpr DRE(const_cast<VarDecl *>(PrivateVD), |
| 1617 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 1618 | (*IPC)->getType(), VK_LValue, (*IPC)->getExprLoc()); |
| 1619 | OrigAddr = EmitLValue(&DRE).getAddress(); |
| 1620 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1621 | OMPPrivateScope VarScope(*this); |
| 1622 | VarScope.addPrivate(OrigVD, |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 1623 | [OrigAddr]() -> Address { return OrigAddr; }); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1624 | (void)VarScope.Privatize(); |
| 1625 | EmitIgnoredExpr(F); |
| 1626 | } |
| 1627 | ++IC; |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 1628 | ++IPC; |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1629 | } |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1630 | if (DoneBB) |
| 1631 | EmitBlock(DoneBB, /*IsFinished=*/true); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1634 | static void emitOMPLoopBodyWithStopPoint(CodeGenFunction &CGF, |
| 1635 | const OMPLoopDirective &S, |
| 1636 | CodeGenFunction::JumpDest LoopExit) { |
| 1637 | CGF.EmitOMPLoopBody(S, LoopExit); |
| 1638 | CGF.EmitStopPoint(&S); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 1639 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1640 | |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1641 | /// Emit a helper variable and return corresponding lvalue. |
| 1642 | static LValue EmitOMPHelperVar(CodeGenFunction &CGF, |
| 1643 | const DeclRefExpr *Helper) { |
| 1644 | auto VDecl = cast<VarDecl>(Helper->getDecl()); |
| 1645 | CGF.EmitVarDecl(*VDecl); |
| 1646 | return CGF.EmitLValue(Helper); |
| 1647 | } |
| 1648 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1649 | static void emitOMPSimdRegion(CodeGenFunction &CGF, const OMPLoopDirective &S, |
| 1650 | PrePostActionTy &Action) { |
| 1651 | Action.Enter(CGF); |
| 1652 | assert(isOpenMPSimdDirective(S.getDirectiveKind()) && |
| 1653 | "Expected simd directive"); |
| 1654 | OMPLoopScope PreInitScope(CGF, S); |
| 1655 | // if (PreCond) { |
| 1656 | // for (IV in 0..LastIteration) BODY; |
| 1657 | // <Final counter/linear vars updates>; |
| 1658 | // } |
| 1659 | // |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 1660 | if (isOpenMPDistributeDirective(S.getDirectiveKind()) || |
| 1661 | isOpenMPWorksharingDirective(S.getDirectiveKind()) || |
| 1662 | isOpenMPTaskLoopDirective(S.getDirectiveKind())) { |
| 1663 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable())); |
| 1664 | (void)EmitOMPHelperVar(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable())); |
| 1665 | } |
Alexander Musman | a5f070a | 2014-10-01 06:03:56 +0000 | [diff] [blame] | 1666 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1667 | // Emit: if (PreCond) - begin. |
| 1668 | // If the condition constant folds and can be elided, avoid emitting the |
| 1669 | // whole loop. |
| 1670 | bool CondConstant; |
| 1671 | llvm::BasicBlock *ContBlock = nullptr; |
| 1672 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 1673 | if (!CondConstant) |
| 1674 | return; |
| 1675 | } else { |
| 1676 | auto *ThenBlock = CGF.createBasicBlock("simd.if.then"); |
| 1677 | ContBlock = CGF.createBasicBlock("simd.if.end"); |
| 1678 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 1679 | CGF.getProfileCount(&S)); |
| 1680 | CGF.EmitBlock(ThenBlock); |
| 1681 | CGF.incrementProfileCounter(&S); |
| 1682 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1683 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1684 | // Emit the loop iteration variable. |
| 1685 | const Expr *IVExpr = S.getIterationVariable(); |
| 1686 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 1687 | CGF.EmitVarDecl(*IVDecl); |
| 1688 | CGF.EmitIgnoredExpr(S.getInit()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1689 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1690 | // Emit the iterations count variable. |
| 1691 | // If it is not a variable, Sema decided to calculate iterations count on |
| 1692 | // each iteration (e.g., it is foldable into a constant). |
| 1693 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 1694 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 1695 | // Emit calculation of the iterations count. |
| 1696 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 1697 | } |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1698 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1699 | CGF.EmitOMPSimdInit(S); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1700 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1701 | emitAlignedClause(CGF, S); |
| 1702 | (void)CGF.EmitOMPLinearClauseInit(S); |
| 1703 | { |
| 1704 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 1705 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
| 1706 | CGF.EmitOMPLinearClause(S, LoopScope); |
| 1707 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 1708 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 1709 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 1710 | (void)LoopScope.Privatize(); |
| 1711 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 1712 | S.getInc(), |
| 1713 | [&S](CodeGenFunction &CGF) { |
| 1714 | CGF.EmitOMPLoopBody(S, CodeGenFunction::JumpDest()); |
| 1715 | CGF.EmitStopPoint(&S); |
| 1716 | }, |
| 1717 | [](CodeGenFunction &) {}); |
| 1718 | CGF.EmitOMPSimdFinal( |
Alexey Bataev | ef549a8 | 2016-03-09 09:49:09 +0000 | [diff] [blame] | 1719 | S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 1720 | // Emit final copy of the lastprivate variables at the end of loops. |
| 1721 | if (HasLastprivateClause) |
| 1722 | CGF.EmitOMPLastprivateClauseFinal(S, /*NoFinals=*/true); |
| 1723 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_simd); |
| 1724 | emitPostUpdateForReductionClause( |
| 1725 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
| 1726 | } |
| 1727 | CGF.EmitOMPLinearClauseFinal( |
| 1728 | S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
| 1729 | // Emit: if (PreCond) - end. |
| 1730 | if (ContBlock) { |
| 1731 | CGF.EmitBranch(ContBlock); |
| 1732 | CGF.EmitBlock(ContBlock, true); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | void CodeGenFunction::EmitOMPSimdDirective(const OMPSimdDirective &S) { |
| 1737 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 1738 | emitOMPSimdRegion(CGF, S, Action); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 1739 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 1740 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 1741 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Alexander Musman | 515ad8c | 2014-05-22 08:54:05 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1744 | void CodeGenFunction::EmitOMPOuterLoop( |
| 1745 | bool DynamicOrOrdered, bool IsMonotonic, const OMPLoopDirective &S, |
| 1746 | CodeGenFunction::OMPPrivateScope &LoopScope, |
| 1747 | const CodeGenFunction::OMPLoopArguments &LoopArgs, |
| 1748 | const CodeGenFunction::CodeGenLoopTy &CodeGenLoop, |
| 1749 | const CodeGenFunction::CodeGenOrderedTy &CodeGenOrdered) { |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1750 | auto &RT = CGM.getOpenMPRuntime(); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1751 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1752 | const Expr *IVExpr = S.getIterationVariable(); |
| 1753 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1754 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1755 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1756 | auto LoopExit = getJumpDestInCurrentScope("omp.dispatch.end"); |
| 1757 | |
| 1758 | // Start the loop with a block that tests the condition. |
| 1759 | auto CondBlock = createBasicBlock("omp.dispatch.cond"); |
| 1760 | EmitBlock(CondBlock); |
Amara Emerson | 652795d | 2016-11-10 14:44:30 +0000 | [diff] [blame] | 1761 | const SourceRange &R = S.getSourceRange(); |
| 1762 | LoopStack.push(CondBlock, SourceLocToDebugLoc(R.getBegin()), |
| 1763 | SourceLocToDebugLoc(R.getEnd())); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1764 | |
| 1765 | llvm::Value *BoolCondVal = nullptr; |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1766 | if (!DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1767 | // UB = min(UB, GlobalUB) or |
| 1768 | // UB = min(UB, PrevUB) for combined loop sharing constructs (e.g. |
| 1769 | // 'distribute parallel for') |
| 1770 | EmitIgnoredExpr(LoopArgs.EUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1771 | // IV = LB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1772 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1773 | // IV < UB |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1774 | BoolCondVal = EvaluateExprAsBool(LoopArgs.Cond); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1775 | } else { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1776 | BoolCondVal = |
| 1777 | RT.emitForNext(*this, S.getLocStart(), IVSize, IVSigned, LoopArgs.IL, |
| 1778 | LoopArgs.LB, LoopArgs.UB, LoopArgs.ST); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1779 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1780 | |
| 1781 | // If there are any cleanups between here and the loop-exit scope, |
| 1782 | // create a block to stage a loop exit along. |
| 1783 | auto ExitBlock = LoopExit.getBlock(); |
| 1784 | if (LoopScope.requiresCleanups()) |
| 1785 | ExitBlock = createBasicBlock("omp.dispatch.cleanup"); |
| 1786 | |
| 1787 | auto LoopBody = createBasicBlock("omp.dispatch.body"); |
| 1788 | Builder.CreateCondBr(BoolCondVal, LoopBody, ExitBlock); |
| 1789 | if (ExitBlock != LoopExit.getBlock()) { |
| 1790 | EmitBlock(ExitBlock); |
| 1791 | EmitBranchThroughCleanup(LoopExit); |
| 1792 | } |
| 1793 | EmitBlock(LoopBody); |
| 1794 | |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1795 | // Emit "IV = LB" (in case of static schedule, we have already calculated new |
| 1796 | // LB for loop condition and emitted it above). |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1797 | if (DynamicOrOrdered) |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1798 | EmitIgnoredExpr(LoopArgs.Init); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1799 | |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1800 | // Create a block for the increment. |
| 1801 | auto Continue = getJumpDestInCurrentScope("omp.dispatch.inc"); |
| 1802 | BreakContinueStack.push_back(BreakContinue(LoopExit, Continue)); |
| 1803 | |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1804 | // Generate !llvm.loop.parallel metadata for loads and stores for loops |
| 1805 | // with dynamic/guided scheduling and without ordered clause. |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 1806 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) |
| 1807 | LoopStack.setParallel(!IsMonotonic); |
| 1808 | else |
| 1809 | EmitOMPSimdInit(S, IsMonotonic); |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 1810 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 1811 | SourceLocation Loc = S.getLocStart(); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1812 | |
| 1813 | // when 'distribute' is not combined with a 'for': |
| 1814 | // while (idx <= UB) { BODY; ++idx; } |
| 1815 | // when 'distribute' is combined with a 'for' |
| 1816 | // (e.g. 'distribute parallel for') |
| 1817 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 1818 | EmitOMPInnerLoop( |
| 1819 | S, LoopScope.requiresCleanups(), LoopArgs.Cond, LoopArgs.IncExpr, |
| 1820 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 1821 | CodeGenLoop(CGF, S, LoopExit); |
| 1822 | }, |
| 1823 | [IVSize, IVSigned, Loc, &CodeGenOrdered](CodeGenFunction &CGF) { |
| 1824 | CodeGenOrdered(CGF, Loc, IVSize, IVSigned); |
| 1825 | }); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1826 | |
| 1827 | EmitBlock(Continue.getBlock()); |
| 1828 | BreakContinueStack.pop_back(); |
Alexey Bataev | d7589ffe | 2015-05-20 13:12:48 +0000 | [diff] [blame] | 1829 | if (!DynamicOrOrdered) { |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1830 | // Emit "LB = LB + Stride", "UB = UB + Stride". |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1831 | EmitIgnoredExpr(LoopArgs.NextLB); |
| 1832 | EmitIgnoredExpr(LoopArgs.NextUB); |
Alexander Musman | 92bdaab | 2015-03-12 13:37:50 +0000 | [diff] [blame] | 1833 | } |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 1834 | |
| 1835 | EmitBranch(CondBlock); |
| 1836 | LoopStack.pop(); |
| 1837 | // Emit the fall-through block. |
| 1838 | EmitBlock(LoopExit.getBlock()); |
| 1839 | |
| 1840 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1841 | auto &&CodeGen = [DynamicOrOrdered, &S](CodeGenFunction &CGF) { |
| 1842 | if (!DynamicOrOrdered) |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 1843 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(), |
| 1844 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 1845 | }; |
| 1846 | OMPCancelStack.emitExit(*this, S.getDirectiveKind(), CodeGen); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | void CodeGenFunction::EmitOMPForOuterLoop( |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1850 | const OpenMPScheduleTy &ScheduleKind, bool IsMonotonic, |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1851 | const OMPLoopDirective &S, OMPPrivateScope &LoopScope, bool Ordered, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1852 | const OMPLoopArguments &LoopArgs, |
| 1853 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1854 | auto &RT = CGM.getOpenMPRuntime(); |
| 1855 | |
| 1856 | // Dynamic scheduling of the outer loop (dynamic, guided, auto, runtime). |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1857 | const bool DynamicOrOrdered = |
| 1858 | Ordered || RT.isDynamic(ScheduleKind.Schedule); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1859 | |
| 1860 | assert((Ordered || |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1861 | !RT.isStaticNonchunked(ScheduleKind.Schedule, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1862 | LoopArgs.Chunk != nullptr)) && |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1863 | "static non-chunked schedule does not need outer loop"); |
| 1864 | |
| 1865 | // Emit outer loop. |
| 1866 | // |
| 1867 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1868 | // When schedule(dynamic,chunk_size) is specified, the iterations are |
| 1869 | // distributed to threads in the team in chunks as the threads request them. |
| 1870 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1871 | // until no chunks remain to be distributed. Each chunk contains chunk_size |
| 1872 | // iterations, except for the last chunk to be distributed, which may have |
| 1873 | // fewer iterations. When no chunk_size is specified, it defaults to 1. |
| 1874 | // |
| 1875 | // When schedule(guided,chunk_size) is specified, the iterations are assigned |
| 1876 | // to threads in the team in chunks as the executing threads request them. |
| 1877 | // Each thread executes a chunk of iterations, then requests another chunk, |
| 1878 | // until no chunks remain to be assigned. For a chunk_size of 1, the size of |
| 1879 | // each chunk is proportional to the number of unassigned iterations divided |
| 1880 | // by the number of threads in the team, decreasing to 1. For a chunk_size |
| 1881 | // with value k (greater than 1), the size of each chunk is determined in the |
| 1882 | // same way, with the restriction that the chunks do not contain fewer than k |
| 1883 | // iterations (except for the last chunk to be assigned, which may have fewer |
| 1884 | // than k iterations). |
| 1885 | // |
| 1886 | // When schedule(auto) is specified, the decision regarding scheduling is |
| 1887 | // delegated to the compiler and/or runtime system. The programmer gives the |
| 1888 | // implementation the freedom to choose any possible mapping of iterations to |
| 1889 | // threads in the team. |
| 1890 | // |
| 1891 | // When schedule(runtime) is specified, the decision regarding scheduling is |
| 1892 | // deferred until run time, and the schedule and chunk size are taken from the |
| 1893 | // run-sched-var ICV. If the ICV is set to auto, the schedule is |
| 1894 | // implementation defined |
| 1895 | // |
| 1896 | // while(__kmpc_dispatch_next(&LB, &UB)) { |
| 1897 | // idx = LB; |
| 1898 | // while (idx <= UB) { BODY; ++idx; |
| 1899 | // __kmpc_dispatch_fini_(4|8)[u](); // For ordered loops only. |
| 1900 | // } // inner loop |
| 1901 | // } |
| 1902 | // |
| 1903 | // OpenMP [2.7.1, Loop Construct, Description, table 2-1] |
| 1904 | // When schedule(static, chunk_size) is specified, iterations are divided into |
| 1905 | // chunks of size chunk_size, and the chunks are assigned to the threads in |
| 1906 | // the team in a round-robin fashion in the order of the thread number. |
| 1907 | // |
| 1908 | // while(UB = min(UB, GlobalUB), idx = LB, idx < UB) { |
| 1909 | // while (idx <= UB) { BODY; ++idx; } // inner loop |
| 1910 | // LB = LB + ST; |
| 1911 | // UB = UB + ST; |
| 1912 | // } |
| 1913 | // |
| 1914 | |
| 1915 | const Expr *IVExpr = S.getIterationVariable(); |
| 1916 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1917 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1918 | |
| 1919 | if (DynamicOrOrdered) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1920 | auto DispatchBounds = CGDispatchBounds(*this, S, LoopArgs.LB, LoopArgs.UB); |
| 1921 | llvm::Value *LBVal = DispatchBounds.first; |
| 1922 | llvm::Value *UBVal = DispatchBounds.second; |
| 1923 | CGOpenMPRuntime::DispatchRTInput DipatchRTInputValues = {LBVal, UBVal, |
| 1924 | LoopArgs.Chunk}; |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 1925 | RT.emitForDispatchInit(*this, S.getLocStart(), ScheduleKind, IVSize, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1926 | IVSigned, Ordered, DipatchRTInputValues); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1927 | } else { |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1928 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 1929 | IVSize, IVSigned, Ordered, LoopArgs.IL, LoopArgs.LB, LoopArgs.UB, |
| 1930 | LoopArgs.ST, LoopArgs.Chunk); |
| 1931 | RT.emitForStaticInit(*this, S.getLocStart(), S.getDirectiveKind(), |
| 1932 | ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1935 | auto &&CodeGenOrdered = [Ordered](CodeGenFunction &CGF, SourceLocation Loc, |
| 1936 | const unsigned IVSize, |
| 1937 | const bool IVSigned) { |
| 1938 | if (Ordered) { |
| 1939 | CGF.CGM.getOpenMPRuntime().emitForOrderedIterationEnd(CGF, Loc, IVSize, |
| 1940 | IVSigned); |
| 1941 | } |
| 1942 | }; |
| 1943 | |
| 1944 | OMPLoopArguments OuterLoopArgs(LoopArgs.LB, LoopArgs.UB, LoopArgs.ST, |
| 1945 | LoopArgs.IL, LoopArgs.Chunk, LoopArgs.EUB); |
| 1946 | OuterLoopArgs.IncExpr = S.getInc(); |
| 1947 | OuterLoopArgs.Init = S.getInit(); |
| 1948 | OuterLoopArgs.Cond = S.getCond(); |
| 1949 | OuterLoopArgs.NextLB = S.getNextLowerBound(); |
| 1950 | OuterLoopArgs.NextUB = S.getNextUpperBound(); |
| 1951 | EmitOMPOuterLoop(DynamicOrOrdered, IsMonotonic, S, LoopScope, OuterLoopArgs, |
| 1952 | emitOMPLoopBodyWithStopPoint, CodeGenOrdered); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1955 | static void emitEmptyOrdered(CodeGenFunction &, SourceLocation Loc, |
| 1956 | const unsigned IVSize, const bool IVSigned) {} |
| 1957 | |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1958 | void CodeGenFunction::EmitOMPDistributeOuterLoop( |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1959 | OpenMPDistScheduleClauseKind ScheduleKind, const OMPLoopDirective &S, |
| 1960 | OMPPrivateScope &LoopScope, const OMPLoopArguments &LoopArgs, |
| 1961 | const CodeGenLoopTy &CodeGenLoopContent) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1962 | |
| 1963 | auto &RT = CGM.getOpenMPRuntime(); |
| 1964 | |
| 1965 | // Emit outer loop. |
| 1966 | // Same behavior as a OMPForOuterLoop, except that schedule cannot be |
| 1967 | // dynamic |
| 1968 | // |
| 1969 | |
| 1970 | const Expr *IVExpr = S.getIterationVariable(); |
| 1971 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 1972 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 1973 | |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 1974 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 1975 | IVSize, IVSigned, /* Ordered = */ false, LoopArgs.IL, LoopArgs.LB, |
| 1976 | LoopArgs.UB, LoopArgs.ST, LoopArgs.Chunk); |
| 1977 | RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 1978 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 1979 | // for combined 'distribute' and 'for' the increment expression of distribute |
| 1980 | // is store in DistInc. For 'distribute' alone, it is in Inc. |
| 1981 | Expr *IncExpr; |
| 1982 | if (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind())) |
| 1983 | IncExpr = S.getDistInc(); |
| 1984 | else |
| 1985 | IncExpr = S.getInc(); |
| 1986 | |
| 1987 | // this routine is shared by 'omp distribute parallel for' and |
| 1988 | // 'omp distribute': select the right EUB expression depending on the |
| 1989 | // directive |
| 1990 | OMPLoopArguments OuterLoopArgs; |
| 1991 | OuterLoopArgs.LB = LoopArgs.LB; |
| 1992 | OuterLoopArgs.UB = LoopArgs.UB; |
| 1993 | OuterLoopArgs.ST = LoopArgs.ST; |
| 1994 | OuterLoopArgs.IL = LoopArgs.IL; |
| 1995 | OuterLoopArgs.Chunk = LoopArgs.Chunk; |
| 1996 | OuterLoopArgs.EUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 1997 | ? S.getCombinedEnsureUpperBound() |
| 1998 | : S.getEnsureUpperBound(); |
| 1999 | OuterLoopArgs.IncExpr = IncExpr; |
| 2000 | OuterLoopArgs.Init = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2001 | ? S.getCombinedInit() |
| 2002 | : S.getInit(); |
| 2003 | OuterLoopArgs.Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2004 | ? S.getCombinedCond() |
| 2005 | : S.getCond(); |
| 2006 | OuterLoopArgs.NextLB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2007 | ? S.getCombinedNextLowerBound() |
| 2008 | : S.getNextLowerBound(); |
| 2009 | OuterLoopArgs.NextUB = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 2010 | ? S.getCombinedNextUpperBound() |
| 2011 | : S.getNextUpperBound(); |
| 2012 | |
| 2013 | EmitOMPOuterLoop(/* DynamicOrOrdered = */ false, /* IsMonotonic = */ false, S, |
| 2014 | LoopScope, OuterLoopArgs, CodeGenLoopContent, |
| 2015 | emitEmptyOrdered); |
| 2016 | } |
| 2017 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2018 | static std::pair<LValue, LValue> |
| 2019 | emitDistributeParallelForInnerBounds(CodeGenFunction &CGF, |
| 2020 | const OMPExecutableDirective &S) { |
| 2021 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2022 | LValue LB = |
| 2023 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2024 | LValue UB = |
| 2025 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2026 | |
| 2027 | // When composing 'distribute' with 'for' (e.g. as in 'distribute |
| 2028 | // parallel for') we need to use the 'distribute' |
| 2029 | // chunk lower and upper bounds rather than the whole loop iteration |
| 2030 | // space. These are parameters to the outlined function for 'parallel' |
| 2031 | // and we copy the bounds of the previous schedule into the |
| 2032 | // the current ones. |
| 2033 | LValue PrevLB = CGF.EmitLValue(LS.getPrevLowerBoundVariable()); |
| 2034 | LValue PrevUB = CGF.EmitLValue(LS.getPrevUpperBoundVariable()); |
| 2035 | llvm::Value *PrevLBVal = CGF.EmitLoadOfScalar(PrevLB, SourceLocation()); |
| 2036 | PrevLBVal = CGF.EmitScalarConversion( |
| 2037 | PrevLBVal, LS.getPrevLowerBoundVariable()->getType(), |
| 2038 | LS.getIterationVariable()->getType(), SourceLocation()); |
| 2039 | llvm::Value *PrevUBVal = CGF.EmitLoadOfScalar(PrevUB, SourceLocation()); |
| 2040 | PrevUBVal = CGF.EmitScalarConversion( |
| 2041 | PrevUBVal, LS.getPrevUpperBoundVariable()->getType(), |
| 2042 | LS.getIterationVariable()->getType(), SourceLocation()); |
| 2043 | |
| 2044 | CGF.EmitStoreOfScalar(PrevLBVal, LB); |
| 2045 | CGF.EmitStoreOfScalar(PrevUBVal, UB); |
| 2046 | |
| 2047 | return {LB, UB}; |
| 2048 | } |
| 2049 | |
| 2050 | /// if the 'for' loop has a dispatch schedule (e.g. dynamic, guided) then |
| 2051 | /// we need to use the LB and UB expressions generated by the worksharing |
| 2052 | /// code generation support, whereas in non combined situations we would |
| 2053 | /// just emit 0 and the LastIteration expression |
| 2054 | /// This function is necessary due to the difference of the LB and UB |
| 2055 | /// types for the RT emission routines for 'for_static_init' and |
| 2056 | /// 'for_dispatch_init' |
| 2057 | static std::pair<llvm::Value *, llvm::Value *> |
| 2058 | emitDistributeParallelForDispatchBounds(CodeGenFunction &CGF, |
| 2059 | const OMPExecutableDirective &S, |
| 2060 | Address LB, Address UB) { |
| 2061 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2062 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2063 | // when implementing a dynamic schedule for a 'for' combined with a |
| 2064 | // 'distribute' (e.g. 'distribute parallel for'), the 'for' loop |
| 2065 | // is not normalized as each team only executes its own assigned |
| 2066 | // distribute chunk |
| 2067 | QualType IteratorTy = IVExpr->getType(); |
| 2068 | llvm::Value *LBVal = CGF.EmitLoadOfScalar(LB, /*Volatile=*/false, IteratorTy, |
| 2069 | SourceLocation()); |
| 2070 | llvm::Value *UBVal = CGF.EmitLoadOfScalar(UB, /*Volatile=*/false, IteratorTy, |
| 2071 | SourceLocation()); |
| 2072 | return {LBVal, UBVal}; |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2073 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2074 | |
| 2075 | static void emitDistributeParallelForDistributeInnerBoundParams( |
| 2076 | CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2077 | llvm::SmallVectorImpl<llvm::Value *> &CapturedVars) { |
| 2078 | const auto &Dir = cast<OMPLoopDirective>(S); |
| 2079 | LValue LB = |
| 2080 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedLowerBoundVariable())); |
| 2081 | auto LBCast = CGF.Builder.CreateIntCast( |
| 2082 | CGF.Builder.CreateLoad(LB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2083 | CapturedVars.push_back(LBCast); |
| 2084 | LValue UB = |
| 2085 | CGF.EmitLValue(cast<DeclRefExpr>(Dir.getCombinedUpperBoundVariable())); |
| 2086 | |
| 2087 | auto UBCast = CGF.Builder.CreateIntCast( |
| 2088 | CGF.Builder.CreateLoad(UB.getAddress()), CGF.SizeTy, /*isSigned=*/false); |
| 2089 | CapturedVars.push_back(UBCast); |
Hans Wennborg | ed129ae | 2017-04-27 17:02:25 +0000 | [diff] [blame] | 2090 | } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2091 | |
| 2092 | static void |
| 2093 | emitInnerParallelForWhenCombined(CodeGenFunction &CGF, |
| 2094 | const OMPLoopDirective &S, |
| 2095 | CodeGenFunction::JumpDest LoopExit) { |
| 2096 | auto &&CGInlinedWorksharingLoop = [&S](CodeGenFunction &CGF, |
| 2097 | PrePostActionTy &) { |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2098 | bool HasCancel = false; |
| 2099 | if (!isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 2100 | if (const auto *D = dyn_cast<OMPTeamsDistributeParallelForDirective>(&S)) |
| 2101 | HasCancel = D->hasCancel(); |
| 2102 | else if (const auto *D = dyn_cast<OMPDistributeParallelForDirective>(&S)) |
| 2103 | HasCancel = D->hasCancel(); |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 2104 | else if (const auto *D = |
| 2105 | dyn_cast<OMPTargetTeamsDistributeParallelForDirective>(&S)) |
| 2106 | HasCancel = D->hasCancel(); |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2107 | } |
| 2108 | CodeGenFunction::OMPCancelStackRAII CancelRegion(CGF, S.getDirectiveKind(), |
| 2109 | HasCancel); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2110 | CGF.EmitOMPWorksharingLoop(S, S.getPrevEnsureUpperBound(), |
| 2111 | emitDistributeParallelForInnerBounds, |
| 2112 | emitDistributeParallelForDispatchBounds); |
| 2113 | }; |
| 2114 | |
| 2115 | emitCommonOMPParallelDirective( |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 2116 | CGF, S, |
| 2117 | isOpenMPSimdDirective(S.getDirectiveKind()) ? OMPD_for_simd : OMPD_for, |
| 2118 | CGInlinedWorksharingLoop, |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2119 | emitDistributeParallelForDistributeInnerBoundParams); |
Alexander Musman | df7a8e2 | 2015-01-22 08:49:35 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2122 | void CodeGenFunction::EmitOMPDistributeParallelForDirective( |
| 2123 | const OMPDistributeParallelForDirective &S) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2124 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2125 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2126 | S.getDistInc()); |
| 2127 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2128 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 2129 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 9925f15 | 2016-06-27 14:55:37 +0000 | [diff] [blame] | 2130 | } |
| 2131 | |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2132 | void CodeGenFunction::EmitOMPDistributeParallelForSimdDirective( |
| 2133 | const OMPDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2134 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2135 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 2136 | S.getDistInc()); |
| 2137 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2138 | OMPLexicalScope Scope(*this, S, OMPD_parallel); |
Alexey Bataev | 0b49f9e | 2017-11-27 19:38:58 +0000 | [diff] [blame] | 2139 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Kelvin Li | 4a39add | 2016-07-05 05:00:15 +0000 | [diff] [blame] | 2140 | } |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2141 | |
| 2142 | void CodeGenFunction::EmitOMPDistributeSimdDirective( |
| 2143 | const OMPDistributeSimdDirective &S) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2144 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2145 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 2146 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2147 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 2148 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
Kelvin Li | 787f3fc | 2016-07-06 04:45:38 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2151 | void CodeGenFunction::EmitOMPTargetSimdDeviceFunction( |
| 2152 | CodeGenModule &CGM, StringRef ParentName, const OMPTargetSimdDirective &S) { |
| 2153 | // Emit SPMD target parallel for region as a standalone region. |
| 2154 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2155 | emitOMPSimdRegion(CGF, S, Action); |
| 2156 | }; |
| 2157 | llvm::Function *Fn; |
| 2158 | llvm::Constant *Addr; |
| 2159 | // Emit target region as a standalone region. |
| 2160 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 2161 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 2162 | assert(Fn && Addr && "Target device function emission failed."); |
| 2163 | } |
| 2164 | |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2165 | void CodeGenFunction::EmitOMPTargetSimdDirective( |
| 2166 | const OMPTargetSimdDirective &S) { |
Alexey Bataev | f836537 | 2017-11-17 17:57:25 +0000 | [diff] [blame] | 2167 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2168 | emitOMPSimdRegion(CGF, S, Action); |
| 2169 | }; |
| 2170 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Kelvin Li | 986330c | 2016-07-20 22:57:10 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2173 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForSimdDirective( |
| 2174 | const OMPTargetTeamsDistributeParallelForSimdDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2175 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2176 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 2177 | *this, OMPD_target_teams_distribute_parallel_for_simd, |
| 2178 | [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2179 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Kelvin Li | 1851df5 | 2017-01-03 05:23:48 +0000 | [diff] [blame] | 2180 | }); |
| 2181 | } |
| 2182 | |
Alexey Bataev | a6f2a14 | 2015-12-31 06:52:34 +0000 | [diff] [blame] | 2183 | namespace { |
| 2184 | struct ScheduleKindModifiersTy { |
| 2185 | OpenMPScheduleClauseKind Kind; |
| 2186 | OpenMPScheduleClauseModifier M1; |
| 2187 | OpenMPScheduleClauseModifier M2; |
| 2188 | ScheduleKindModifiersTy(OpenMPScheduleClauseKind Kind, |
| 2189 | OpenMPScheduleClauseModifier M1, |
| 2190 | OpenMPScheduleClauseModifier M2) |
| 2191 | : Kind(Kind), M1(M1), M2(M2) {} |
| 2192 | }; |
| 2193 | } // namespace |
| 2194 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2195 | bool CodeGenFunction::EmitOMPWorksharingLoop( |
| 2196 | const OMPLoopDirective &S, Expr *EUB, |
| 2197 | const CodeGenLoopBoundsTy &CodeGenLoopBounds, |
| 2198 | const CodeGenDispatchBoundsTy &CGDispatchBounds) { |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2199 | // Emit the loop iteration variable. |
| 2200 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 2201 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 2202 | EmitVarDecl(*IVDecl); |
| 2203 | |
| 2204 | // Emit the iterations count variable. |
| 2205 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 2206 | // iteration (e.g., it is foldable into a constant). |
| 2207 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 2208 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 2209 | // Emit calculation of the iterations count. |
| 2210 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 2211 | } |
| 2212 | |
| 2213 | auto &RT = CGM.getOpenMPRuntime(); |
| 2214 | |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2215 | bool HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2216 | // Check pre-condition. |
| 2217 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 2218 | OMPLoopScope PreInitScope(*this, S); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2219 | // Skip the entire loop if we don't meet the precondition. |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2220 | // If the condition constant folds and can be elided, avoid emitting the |
| 2221 | // whole loop. |
| 2222 | bool CondConstant; |
| 2223 | llvm::BasicBlock *ContBlock = nullptr; |
| 2224 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 2225 | if (!CondConstant) |
| 2226 | return false; |
| 2227 | } else { |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2228 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 2229 | ContBlock = createBasicBlock("omp.precond.end"); |
| 2230 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2231 | getProfileCount(&S)); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2232 | EmitBlock(ThenBlock); |
Justin Bogner | 66242d6 | 2015-04-23 23:06:47 +0000 | [diff] [blame] | 2233 | incrementProfileCounter(&S); |
Alexey Bataev | 62dbb97 | 2015-04-22 11:59:37 +0000 | [diff] [blame] | 2234 | } |
Alexey Bataev | 58e5bdb | 2015-06-18 04:45:29 +0000 | [diff] [blame] | 2235 | |
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 | }); |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2372 | // 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] | 2373 | if (ContBlock) { |
| 2374 | EmitBranch(ContBlock); |
| 2375 | EmitBlock(ContBlock, true); |
| 2376 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2377 | } |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2378 | return HasLastprivateClause; |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2381 | /// The following two functions generate expressions for the loop lower |
| 2382 | /// and upper bounds in case of static and dynamic (dispatch) schedule |
| 2383 | /// of the associated 'for' or 'distribute' loop. |
| 2384 | static std::pair<LValue, LValue> |
| 2385 | emitForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S) { |
| 2386 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2387 | LValue LB = |
| 2388 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getLowerBoundVariable())); |
| 2389 | LValue UB = |
| 2390 | EmitOMPHelperVar(CGF, cast<DeclRefExpr>(LS.getUpperBoundVariable())); |
| 2391 | return {LB, UB}; |
| 2392 | } |
| 2393 | |
| 2394 | /// When dealing with dispatch schedules (e.g. dynamic, guided) we do not |
| 2395 | /// consider the lower and upper bound expressions generated by the |
| 2396 | /// worksharing loop support, but we use 0 and the iteration space size as |
| 2397 | /// constants |
| 2398 | static std::pair<llvm::Value *, llvm::Value *> |
| 2399 | emitDispatchForLoopBounds(CodeGenFunction &CGF, const OMPExecutableDirective &S, |
| 2400 | Address LB, Address UB) { |
| 2401 | const OMPLoopDirective &LS = cast<OMPLoopDirective>(S); |
| 2402 | const Expr *IVExpr = LS.getIterationVariable(); |
| 2403 | const unsigned IVSize = CGF.getContext().getTypeSize(IVExpr->getType()); |
| 2404 | llvm::Value *LBVal = CGF.Builder.getIntN(IVSize, 0); |
| 2405 | llvm::Value *UBVal = CGF.EmitScalarExpr(LS.getLastIteration()); |
| 2406 | return {LBVal, UBVal}; |
| 2407 | } |
| 2408 | |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2409 | void CodeGenFunction::EmitOMPForDirective(const OMPForDirective &S) { |
Alexey Bataev | 38e8953 | 2015-04-16 04:54:05 +0000 | [diff] [blame] | 2410 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2411 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2412 | PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2413 | OMPCancelStackRAII CancelRegion(CGF, OMPD_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2414 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2415 | emitForLoopBounds, |
| 2416 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2417 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2418 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2419 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2420 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_for, CodeGen, |
| 2421 | S.hasCancel()); |
| 2422 | } |
Alexander Musman | c638868 | 2014-12-15 07:07:06 +0000 | [diff] [blame] | 2423 | |
| 2424 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2425 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2426 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 2427 | } |
Alexey Bataev | f29276e | 2014-06-18 04:14:57 +0000 | [diff] [blame] | 2428 | } |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2429 | |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2430 | void CodeGenFunction::EmitOMPForSimdDirective(const OMPForSimdDirective &S) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2431 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2432 | auto &&CodeGen = [&S, &HasLastprivates](CodeGenFunction &CGF, |
| 2433 | PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2434 | HasLastprivates = CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), |
| 2435 | emitForLoopBounds, |
| 2436 | emitDispatchForLoopBounds); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2437 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2438 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2439 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2440 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_simd, CodeGen); |
| 2441 | } |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2442 | |
| 2443 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2444 | if (!S.getSingleClause<OMPNowaitClause>() || HasLastprivates) { |
Alexey Bataev | cbdcbb7 | 2015-06-17 07:45:51 +0000 | [diff] [blame] | 2445 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_for); |
| 2446 | } |
Alexander Musman | f82886e | 2014-09-18 05:12:34 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2449 | static LValue createSectionLVal(CodeGenFunction &CGF, QualType Ty, |
| 2450 | const Twine &Name, |
| 2451 | llvm::Value *Init = nullptr) { |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 2452 | auto LVal = CGF.MakeAddrLValue(CGF.CreateMemTemp(Ty, Name), Ty); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2453 | if (Init) |
Akira Hatanaka | 642f799 | 2016-10-18 19:05:41 +0000 | [diff] [blame] | 2454 | CGF.EmitStoreThroughLValue(RValue::get(Init), LVal, /*isInit*/ true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2455 | return LVal; |
Alexey Bataev | d3f8dd2 | 2014-06-25 11:44:49 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2458 | void CodeGenFunction::EmitSections(const OMPExecutableDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2459 | const Stmt *Stmt = S.getInnermostCapturedStmt()->getCapturedStmt(); |
| 2460 | const auto *CS = dyn_cast<CompoundStmt>(Stmt); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2461 | bool HasLastprivates = false; |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2462 | auto &&CodeGen = [&S, Stmt, CS, &HasLastprivates](CodeGenFunction &CGF, |
| 2463 | PrePostActionTy &) { |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2464 | auto &C = CGF.CGM.getContext(); |
| 2465 | auto KmpInt32Ty = C.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1); |
| 2466 | // Emit helper vars inits. |
| 2467 | LValue LB = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.lb.", |
| 2468 | CGF.Builder.getInt32(0)); |
| 2469 | auto *GlobalUBVal = CS != nullptr ? CGF.Builder.getInt32(CS->size() - 1) |
| 2470 | : CGF.Builder.getInt32(0); |
| 2471 | LValue UB = |
| 2472 | createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.ub.", GlobalUBVal); |
| 2473 | LValue ST = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.st.", |
| 2474 | CGF.Builder.getInt32(1)); |
| 2475 | LValue IL = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.il.", |
| 2476 | CGF.Builder.getInt32(0)); |
| 2477 | // Loop counter. |
| 2478 | LValue IV = createSectionLVal(CGF, KmpInt32Ty, ".omp.sections.iv."); |
| 2479 | OpaqueValueExpr IVRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 2480 | CodeGenFunction::OpaqueValueMapping OpaqueIV(CGF, &IVRefExpr, IV); |
| 2481 | OpaqueValueExpr UBRefExpr(S.getLocStart(), KmpInt32Ty, VK_LValue); |
| 2482 | CodeGenFunction::OpaqueValueMapping OpaqueUB(CGF, &UBRefExpr, UB); |
| 2483 | // Generate condition for loop. |
| 2484 | BinaryOperator Cond(&IVRefExpr, &UBRefExpr, BO_LE, C.BoolTy, VK_RValue, |
Adam Nemet | 484aa45 | 2017-03-27 19:17:25 +0000 | [diff] [blame] | 2485 | OK_Ordinary, S.getLocStart(), FPOptions()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2486 | // Increment for loop counter. |
| 2487 | UnaryOperator Inc(&IVRefExpr, UO_PreInc, KmpInt32Ty, VK_RValue, OK_Ordinary, |
Aaron Ballman | a503855 | 2018-01-09 13:07:03 +0000 | [diff] [blame] | 2488 | S.getLocStart(), true); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2489 | auto BodyGen = [Stmt, CS, &S, &IV](CodeGenFunction &CGF) { |
| 2490 | // Iterate through all sections and emit a switch construct: |
| 2491 | // switch (IV) { |
| 2492 | // case 0: |
| 2493 | // <SectionStmt[0]>; |
| 2494 | // break; |
| 2495 | // ... |
| 2496 | // case <NumSection> - 1: |
| 2497 | // <SectionStmt[<NumSection> - 1]>; |
| 2498 | // break; |
| 2499 | // } |
| 2500 | // .omp.sections.exit: |
| 2501 | auto *ExitBB = CGF.createBasicBlock(".omp.sections.exit"); |
| 2502 | auto *SwitchStmt = CGF.Builder.CreateSwitch( |
| 2503 | CGF.EmitLoadOfLValue(IV, S.getLocStart()).getScalarVal(), ExitBB, |
| 2504 | CS == nullptr ? 1 : CS->size()); |
| 2505 | if (CS) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2506 | unsigned CaseNumber = 0; |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2507 | for (auto *SubStmt : CS->children()) { |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2508 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2509 | CGF.EmitBlock(CaseBB); |
| 2510 | SwitchStmt->addCase(CGF.Builder.getInt32(CaseNumber), CaseBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2511 | CGF.EmitStmt(SubStmt); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2512 | CGF.EmitBranch(ExitBB); |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 2513 | ++CaseNumber; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2514 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2515 | } else { |
| 2516 | auto CaseBB = CGF.createBasicBlock(".omp.sections.case"); |
| 2517 | CGF.EmitBlock(CaseBB); |
| 2518 | SwitchStmt->addCase(CGF.Builder.getInt32(0), CaseBB); |
| 2519 | CGF.EmitStmt(Stmt); |
| 2520 | CGF.EmitBranch(ExitBB); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2521 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2522 | CGF.EmitBlock(ExitBB, /*IsFinished=*/true); |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2523 | }; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2524 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2525 | CodeGenFunction::OMPPrivateScope LoopScope(CGF); |
| 2526 | if (CGF.EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2527 | // Emit implicit barrier to synchronize threads and avoid data races on |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2528 | // initialization of firstprivate variables and post-update of lastprivate |
| 2529 | // variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2530 | CGF.CGM.getOpenMPRuntime().emitBarrierCall( |
| 2531 | CGF, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 2532 | /*ForceSimpleCall=*/true); |
Alexey Bataev | 9efc03b | 2015-04-27 04:34:03 +0000 | [diff] [blame] | 2533 | } |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2534 | CGF.EmitOMPPrivateClause(S, LoopScope); |
| 2535 | HasLastprivates = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
| 2536 | CGF.EmitOMPReductionClauseInit(S, LoopScope); |
| 2537 | (void)LoopScope.Privatize(); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2538 | |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2539 | // Emit static non-chunked loop. |
Alexey Bataev | 9ebd742 | 2016-05-10 09:57:36 +0000 | [diff] [blame] | 2540 | OpenMPScheduleTy ScheduleKind; |
| 2541 | ScheduleKind.Schedule = OMPC_SCHEDULE_static; |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2542 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 2543 | /*IVSize=*/32, /*IVSigned=*/true, /*Ordered=*/false, IL.getAddress(), |
| 2544 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2545 | CGF.CGM.getOpenMPRuntime().emitForStaticInit( |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 2546 | CGF, S.getLocStart(), S.getDirectiveKind(), ScheduleKind, StaticInit); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2547 | // UB = min(UB, GlobalUB); |
| 2548 | auto *UBVal = CGF.EmitLoadOfScalar(UB, S.getLocStart()); |
| 2549 | auto *MinUBGlobalUB = CGF.Builder.CreateSelect( |
| 2550 | CGF.Builder.CreateICmpSLT(UBVal, GlobalUBVal), UBVal, GlobalUBVal); |
| 2551 | CGF.EmitStoreOfScalar(MinUBGlobalUB, UB); |
| 2552 | // IV = LB; |
| 2553 | CGF.EmitStoreOfScalar(CGF.EmitLoadOfScalar(LB, S.getLocStart()), IV); |
| 2554 | // while (idx <= UB) { BODY; ++idx; } |
| 2555 | CGF.EmitOMPInnerLoop(S, /*RequiresCleanup=*/false, &Cond, &Inc, BodyGen, |
| 2556 | [](CodeGenFunction &) {}); |
| 2557 | // Tell the runtime we are done. |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2558 | auto &&CodeGen = [&S](CodeGenFunction &CGF) { |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 2559 | CGF.CGM.getOpenMPRuntime().emitForStaticFinish(CGF, S.getLocEnd(), |
| 2560 | S.getDirectiveKind()); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2561 | }; |
| 2562 | CGF.OMPCancelStack.emitExit(CGF, S.getDirectiveKind(), CodeGen); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 2563 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Alexey Bataev | 6120507 | 2016-03-02 04:57:40 +0000 | [diff] [blame] | 2564 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 2565 | emitPostUpdateForReductionClause( |
| 2566 | CGF, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 2567 | return CGF.Builder.CreateIsNotNull( |
| 2568 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 2569 | }); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2570 | |
| 2571 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 2572 | if (HasLastprivates) |
| 2573 | CGF.EmitOMPLastprivateClauseFinal( |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 2574 | S, /*NoFinals=*/false, |
| 2575 | CGF.Builder.CreateIsNotNull( |
| 2576 | CGF.EmitLoadOfScalar(IL, S.getLocStart()))); |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2577 | }; |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2578 | |
| 2579 | bool HasCancel = false; |
| 2580 | if (auto *OSD = dyn_cast<OMPSectionsDirective>(&S)) |
| 2581 | HasCancel = OSD->hasCancel(); |
| 2582 | else if (auto *OPSD = dyn_cast<OMPParallelSectionsDirective>(&S)) |
| 2583 | HasCancel = OPSD->hasCancel(); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2584 | OMPCancelStackRAII CancelRegion(*this, S.getDirectiveKind(), HasCancel); |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2585 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_sections, CodeGen, |
| 2586 | HasCancel); |
| 2587 | // Emit barrier for lastprivates only if 'sections' directive has 'nowait' |
| 2588 | // clause. Otherwise the barrier will be generated by the codegen for the |
| 2589 | // directive. |
| 2590 | if (HasLastprivates && S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2591 | // Emit implicit barrier to synchronize threads and avoid data races on |
| 2592 | // initialization of firstprivate variables. |
Alexey Bataev | 3015bcc | 2016-01-22 08:56:50 +0000 | [diff] [blame] | 2593 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 2594 | OMPD_unknown); |
Alexey Bataev | 2cb9b95 | 2015-04-24 03:37:03 +0000 | [diff] [blame] | 2595 | } |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2596 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2597 | |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2598 | void CodeGenFunction::EmitOMPSectionsDirective(const OMPSectionsDirective &S) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2599 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2600 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2601 | EmitSections(S); |
| 2602 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2603 | // Emit an implicit barrier at the end. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2604 | if (!S.getSingleClause<OMPNowaitClause>()) { |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2605 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), |
| 2606 | OMPD_sections); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2607 | } |
Alexey Bataev | 2df54a0 | 2015-03-12 08:53:29 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | void CodeGenFunction::EmitOMPSectionDirective(const OMPSectionDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2611 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2612 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2613 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2614 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 2615 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_section, CodeGen, |
| 2616 | S.hasCancel()); |
Alexey Bataev | 1e0498a | 2014-06-26 08:21:58 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
Alexey Bataev | 6956e2e | 2015-02-05 06:35:41 +0000 | [diff] [blame] | 2619 | void CodeGenFunction::EmitOMPSingleDirective(const OMPSingleDirective &S) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2620 | llvm::SmallVector<const Expr *, 8> CopyprivateVars; |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2621 | llvm::SmallVector<const Expr *, 8> DestExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2622 | llvm::SmallVector<const Expr *, 8> SrcExprs; |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2623 | llvm::SmallVector<const Expr *, 8> AssignmentOps; |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2624 | // Check if there are any 'copyprivate' clauses associated with this |
Alexey Bataev | cd8b6a2 | 2016-02-15 08:07:17 +0000 | [diff] [blame] | 2625 | // 'single' construct. |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2626 | // Build a list of copyprivate variables along with helper expressions |
| 2627 | // (<source>, <destination>, <destination>=<source> expressions) |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2628 | for (const auto *C : S.getClausesOfKind<OMPCopyprivateClause>()) { |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2629 | CopyprivateVars.append(C->varlists().begin(), C->varlists().end()); |
Alexey Bataev | 420d45b | 2015-04-14 05:11:24 +0000 | [diff] [blame] | 2630 | DestExprs.append(C->destination_exprs().begin(), |
| 2631 | C->destination_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2632 | SrcExprs.append(C->source_exprs().begin(), C->source_exprs().end()); |
Alexey Bataev | a63048e | 2015-03-23 06:18:07 +0000 | [diff] [blame] | 2633 | AssignmentOps.append(C->assignment_ops().begin(), |
| 2634 | C->assignment_ops().end()); |
| 2635 | } |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2636 | // Emit code for 'single' region along with 'copyprivate' clauses |
| 2637 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2638 | Action.Enter(CGF); |
| 2639 | OMPPrivateScope SingleScope(CGF); |
| 2640 | (void)CGF.EmitOMPFirstprivateClause(S, SingleScope); |
| 2641 | CGF.EmitOMPPrivateClause(S, SingleScope); |
| 2642 | (void)SingleScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2643 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2644 | }; |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2645 | { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2646 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 3392d76 | 2016-02-16 11:18:12 +0000 | [diff] [blame] | 2647 | CGM.getOpenMPRuntime().emitSingleRegion(*this, CodeGen, S.getLocStart(), |
| 2648 | CopyprivateVars, DestExprs, |
| 2649 | SrcExprs, AssignmentOps); |
| 2650 | } |
| 2651 | // Emit an implicit barrier at the end (to avoid data race on firstprivate |
| 2652 | // init or if no 'nowait' clause was specified and no 'copyprivate' clause). |
Alexey Bataev | 417089f | 2016-02-17 13:19:37 +0000 | [diff] [blame] | 2653 | if (!S.getSingleClause<OMPNowaitClause>() && CopyprivateVars.empty()) { |
Alexey Bataev | 5521d78 | 2015-04-24 04:21:15 +0000 | [diff] [blame] | 2654 | CGM.getOpenMPRuntime().emitBarrierCall( |
| 2655 | *this, S.getLocStart(), |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2656 | S.getSingleClause<OMPNowaitClause>() ? OMPD_unknown : OMPD_single); |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 2657 | } |
Alexey Bataev | d1e40fb | 2014-06-26 12:05:45 +0000 | [diff] [blame] | 2658 | } |
| 2659 | |
Alexey Bataev | 8d69065 | 2014-12-04 07:23:53 +0000 | [diff] [blame] | 2660 | void CodeGenFunction::EmitOMPMasterDirective(const OMPMasterDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2661 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2662 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2663 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2664 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2665 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2666 | CGM.getOpenMPRuntime().emitMasterRegion(*this, CodeGen, S.getLocStart()); |
Alexander Musman | 80c2289 | 2014-07-17 08:54:58 +0000 | [diff] [blame] | 2667 | } |
| 2668 | |
Alexey Bataev | 3a3bf0b | 2014-09-22 10:01:53 +0000 | [diff] [blame] | 2669 | void CodeGenFunction::EmitOMPCriticalDirective(const OMPCriticalDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2670 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 2671 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2672 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2673 | }; |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2674 | Expr *Hint = nullptr; |
| 2675 | if (auto *HintClause = S.getSingleClause<OMPHintClause>()) |
| 2676 | Hint = HintClause->getHint(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2677 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | fc57d16 | 2015-12-15 10:55:09 +0000 | [diff] [blame] | 2678 | CGM.getOpenMPRuntime().emitCriticalRegion(*this, |
| 2679 | S.getDirectiveName().getAsString(), |
| 2680 | CodeGen, S.getLocStart(), Hint); |
Alexander Musman | d9ed09f | 2014-07-21 09:42:05 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2683 | void CodeGenFunction::EmitOMPParallelForDirective( |
| 2684 | const OMPParallelForDirective &S) { |
| 2685 | // Emit directive as a combined directive that consists of two implicit |
| 2686 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2687 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 2688 | OMPCancelStackRAII CancelRegion(CGF, OMPD_parallel_for, S.hasCancel()); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2689 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2690 | emitDispatchForLoopBounds); |
Alexey Bataev | 671605e | 2015-04-13 05:28:11 +0000 | [diff] [blame] | 2691 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2692 | emitCommonOMPParallelDirective(*this, S, OMPD_for, CodeGen, |
| 2693 | emitEmptyBoundParameters); |
Alexey Bataev | 4acb859 | 2014-07-07 13:01:15 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2696 | void CodeGenFunction::EmitOMPParallelForSimdDirective( |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2697 | const OMPParallelForSimdDirective &S) { |
| 2698 | // Emit directive as a combined directive that consists of two implicit |
| 2699 | // directives: 'parallel' with 'for' directive. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2700 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2701 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 2702 | emitDispatchForLoopBounds); |
Alexey Bataev | 3b5b5c4 | 2015-06-18 10:10:12 +0000 | [diff] [blame] | 2703 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2704 | emitCommonOMPParallelDirective(*this, S, OMPD_simd, CodeGen, |
| 2705 | emitEmptyBoundParameters); |
Alexander Musman | e4e893b | 2014-09-23 09:33:00 +0000 | [diff] [blame] | 2706 | } |
| 2707 | |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2708 | void CodeGenFunction::EmitOMPParallelSectionsDirective( |
Alexey Bataev | 68adb7d | 2015-04-14 03:29:22 +0000 | [diff] [blame] | 2709 | const OMPParallelSectionsDirective &S) { |
| 2710 | // Emit directive as a combined directive that consists of two implicit |
| 2711 | // directives: 'parallel' with 'sections' directive. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 2712 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 2713 | CGF.EmitSections(S); |
| 2714 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 2715 | emitCommonOMPParallelDirective(*this, S, OMPD_sections, CodeGen, |
| 2716 | emitEmptyBoundParameters); |
Alexey Bataev | 84d0b3e | 2014-07-08 08:12:03 +0000 | [diff] [blame] | 2717 | } |
| 2718 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2719 | void CodeGenFunction::EmitOMPTaskBasedDirective( |
| 2720 | const OMPExecutableDirective &S, const OpenMPDirectiveKind CapturedRegion, |
| 2721 | const RegionCodeGenTy &BodyGen, const TaskGenTy &TaskGen, |
| 2722 | OMPTaskDataTy &Data) { |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2723 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2724 | const CapturedStmt *CS = S.getCapturedStmt(CapturedRegion); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2725 | auto *I = CS->getCapturedDecl()->param_begin(); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2726 | auto *PartId = std::next(I); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2727 | auto *TaskT = std::next(I, 4); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 2728 | // Check if the task is final |
| 2729 | if (const auto *Clause = S.getSingleClause<OMPFinalClause>()) { |
| 2730 | // If the condition constant folds and can be elided, try to avoid emitting |
| 2731 | // the condition and the dead arm of the if/else. |
| 2732 | auto *Cond = Clause->getCondition(); |
| 2733 | bool CondConstant; |
| 2734 | if (ConstantFoldsToSimpleInteger(Cond, CondConstant)) |
| 2735 | Data.Final.setInt(CondConstant); |
| 2736 | else |
| 2737 | Data.Final.setPointer(EvaluateExprAsBool(Cond)); |
| 2738 | } else { |
| 2739 | // By default the task is not final. |
| 2740 | Data.Final.setInt(/*IntVal=*/false); |
| 2741 | } |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2742 | // Check if the task has 'priority' clause. |
| 2743 | if (const auto *Clause = S.getSingleClause<OMPPriorityClause>()) { |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2744 | auto *Prio = Clause->getPriority(); |
Alexey Bataev | 5140e74 | 2016-07-19 04:21:09 +0000 | [diff] [blame] | 2745 | Data.Priority.setInt(/*IntVal=*/true); |
Alexey Bataev | ad537bb | 2016-05-30 09:06:50 +0000 | [diff] [blame] | 2746 | Data.Priority.setPointer(EmitScalarConversion( |
| 2747 | EmitScalarExpr(Prio), Prio->getType(), |
| 2748 | getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1), |
| 2749 | Prio->getExprLoc())); |
Alexey Bataev | 1e1e286 | 2016-05-10 12:21:02 +0000 | [diff] [blame] | 2750 | } |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 2751 | // The first function argument for tasks is a thread id, the second one is a |
| 2752 | // part id (0 for tied tasks, >=0 for untied task). |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2753 | llvm::DenseSet<const VarDecl *> EmittedAsPrivate; |
| 2754 | // Get list of private variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2755 | for (const auto *C : S.getClausesOfKind<OMPPrivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2756 | auto IRef = C->varlist_begin(); |
| 2757 | for (auto *IInit : C->private_copies()) { |
| 2758 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2759 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2760 | Data.PrivateVars.push_back(*IRef); |
| 2761 | Data.PrivateCopies.push_back(IInit); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2762 | } |
| 2763 | ++IRef; |
| 2764 | } |
| 2765 | } |
| 2766 | EmittedAsPrivate.clear(); |
| 2767 | // Get list of firstprivate variables. |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 2768 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2769 | auto IRef = C->varlist_begin(); |
| 2770 | auto IElemInitRef = C->inits().begin(); |
| 2771 | for (auto *IInit : C->private_copies()) { |
| 2772 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2773 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2774 | Data.FirstprivateVars.push_back(*IRef); |
| 2775 | Data.FirstprivateCopies.push_back(IInit); |
| 2776 | Data.FirstprivateInits.push_back(*IElemInitRef); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2777 | } |
Richard Trieu | cc3949d | 2016-02-18 22:34:54 +0000 | [diff] [blame] | 2778 | ++IRef; |
| 2779 | ++IElemInitRef; |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2780 | } |
| 2781 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2782 | // Get list of lastprivate variables (for taskloops). |
| 2783 | llvm::DenseMap<const VarDecl *, const DeclRefExpr *> LastprivateDstsOrigs; |
| 2784 | for (const auto *C : S.getClausesOfKind<OMPLastprivateClause>()) { |
| 2785 | auto IRef = C->varlist_begin(); |
| 2786 | auto ID = C->destination_exprs().begin(); |
| 2787 | for (auto *IInit : C->private_copies()) { |
| 2788 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*IRef)->getDecl()); |
| 2789 | if (EmittedAsPrivate.insert(OrigVD->getCanonicalDecl()).second) { |
| 2790 | Data.LastprivateVars.push_back(*IRef); |
| 2791 | Data.LastprivateCopies.push_back(IInit); |
| 2792 | } |
| 2793 | LastprivateDstsOrigs.insert( |
| 2794 | {cast<VarDecl>(cast<DeclRefExpr>(*ID)->getDecl()), |
| 2795 | cast<DeclRefExpr>(*IRef)}); |
| 2796 | ++IRef; |
| 2797 | ++ID; |
| 2798 | } |
| 2799 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2800 | SmallVector<const Expr *, 4> LHSs; |
| 2801 | SmallVector<const Expr *, 4> RHSs; |
| 2802 | for (const auto *C : S.getClausesOfKind<OMPReductionClause>()) { |
| 2803 | auto IPriv = C->privates().begin(); |
| 2804 | auto IRed = C->reduction_ops().begin(); |
| 2805 | auto ILHS = C->lhs_exprs().begin(); |
| 2806 | auto IRHS = C->rhs_exprs().begin(); |
| 2807 | for (const auto *Ref : C->varlists()) { |
| 2808 | Data.ReductionVars.emplace_back(Ref); |
| 2809 | Data.ReductionCopies.emplace_back(*IPriv); |
| 2810 | Data.ReductionOps.emplace_back(*IRed); |
| 2811 | LHSs.emplace_back(*ILHS); |
| 2812 | RHSs.emplace_back(*IRHS); |
| 2813 | std::advance(IPriv, 1); |
| 2814 | std::advance(IRed, 1); |
| 2815 | std::advance(ILHS, 1); |
| 2816 | std::advance(IRHS, 1); |
| 2817 | } |
| 2818 | } |
| 2819 | Data.Reductions = CGM.getOpenMPRuntime().emitTaskReductionInit( |
| 2820 | *this, S.getLocStart(), LHSs, RHSs, Data); |
Alexey Bataev | 1d2353d | 2015-06-24 11:01:36 +0000 | [diff] [blame] | 2821 | // Build list of dependences. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2822 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
| 2823 | for (auto *IRef : C->varlists()) |
| 2824 | Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2825 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, &LastprivateDstsOrigs, |
| 2826 | CapturedRegion](CodeGenFunction &CGF, |
| 2827 | PrePostActionTy &Action) { |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2828 | // Set proper addresses for generated private copies. |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2829 | OMPPrivateScope Scope(CGF); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2830 | if (!Data.PrivateVars.empty() || !Data.FirstprivateVars.empty() || |
| 2831 | !Data.LastprivateVars.empty()) { |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2832 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2833 | auto *CopyFn = CGF.Builder.CreateLoad( |
| 2834 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); |
| 2835 | auto *PrivatesPtr = CGF.Builder.CreateLoad( |
| 2836 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); |
| 2837 | // Map privates. |
| 2838 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 2839 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 2840 | CallArgs.push_back(PrivatesPtr); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2841 | for (auto *E : Data.PrivateVars) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2842 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2843 | Address PrivatePtr = CGF.CreateMemTemp( |
| 2844 | CGF.getContext().getPointerType(E->getType()), ".priv.ptr.addr"); |
| 2845 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2846 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2847 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2848 | for (auto *E : Data.FirstprivateVars) { |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2849 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2850 | Address PrivatePtr = |
| 2851 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2852 | ".firstpriv.ptr.addr"); |
| 2853 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2854 | CallArgs.push_back(PrivatePtr.getPointer()); |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2855 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2856 | for (auto *E : Data.LastprivateVars) { |
| 2857 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 2858 | Address PrivatePtr = |
| 2859 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 2860 | ".lastpriv.ptr.addr"); |
| 2861 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 2862 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 2863 | } |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 2864 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(), |
| 2865 | CopyFn, CallArgs); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 2866 | for (auto &&Pair : LastprivateDstsOrigs) { |
| 2867 | auto *OrigVD = cast<VarDecl>(Pair.second->getDecl()); |
| 2868 | DeclRefExpr DRE( |
| 2869 | const_cast<VarDecl *>(OrigVD), |
| 2870 | /*RefersToEnclosingVariableOrCapture=*/CGF.CapturedStmtInfo->lookup( |
| 2871 | OrigVD) != nullptr, |
| 2872 | Pair.second->getType(), VK_LValue, Pair.second->getExprLoc()); |
| 2873 | Scope.addPrivate(Pair.first, [&CGF, &DRE]() { |
| 2874 | return CGF.EmitLValue(&DRE).getAddress(); |
| 2875 | }); |
| 2876 | } |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2877 | for (auto &&Pair : PrivatePtrs) { |
| 2878 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 2879 | CGF.getContext().getDeclAlign(Pair.first)); |
| 2880 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 2881 | } |
Alexey Bataev | 3ae88e2 | 2015-05-22 08:56:35 +0000 | [diff] [blame] | 2882 | } |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2883 | if (Data.Reductions) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 2884 | OMPLexicalScope LexScope(CGF, S, CapturedRegion); |
Alexey Bataev | be5a8b4 | 2017-07-17 13:30:36 +0000 | [diff] [blame] | 2885 | ReductionCodeGen RedCG(Data.ReductionVars, Data.ReductionCopies, |
| 2886 | Data.ReductionOps); |
| 2887 | llvm::Value *ReductionsPtr = CGF.Builder.CreateLoad( |
| 2888 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(9))); |
| 2889 | for (unsigned Cnt = 0, E = Data.ReductionVars.size(); Cnt < E; ++Cnt) { |
| 2890 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2891 | RedCG.emitAggregateType(CGF, Cnt); |
| 2892 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
| 2893 | CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
| 2894 | Replacement = |
| 2895 | Address(CGF.EmitScalarConversion( |
| 2896 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 2897 | CGF.getContext().getPointerType( |
| 2898 | Data.ReductionCopies[Cnt]->getType()), |
| 2899 | SourceLocation()), |
| 2900 | Replacement.getAlignment()); |
| 2901 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 2902 | Scope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 2903 | [Replacement]() { return Replacement; }); |
| 2904 | // FIXME: This must removed once the runtime library is fixed. |
| 2905 | // Emit required threadprivate variables for |
| 2906 | // initilizer/combiner/finalizer. |
| 2907 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(), |
| 2908 | RedCG, Cnt); |
| 2909 | } |
| 2910 | } |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2911 | // Privatize all private variables except for in_reduction items. |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2912 | (void)Scope.Privatize(); |
Alexey Bataev | 88202be | 2017-07-27 13:20:36 +0000 | [diff] [blame] | 2913 | SmallVector<const Expr *, 4> InRedVars; |
| 2914 | SmallVector<const Expr *, 4> InRedPrivs; |
| 2915 | SmallVector<const Expr *, 4> InRedOps; |
| 2916 | SmallVector<const Expr *, 4> TaskgroupDescriptors; |
| 2917 | for (const auto *C : S.getClausesOfKind<OMPInReductionClause>()) { |
| 2918 | auto IPriv = C->privates().begin(); |
| 2919 | auto IRed = C->reduction_ops().begin(); |
| 2920 | auto ITD = C->taskgroup_descriptors().begin(); |
| 2921 | for (const auto *Ref : C->varlists()) { |
| 2922 | InRedVars.emplace_back(Ref); |
| 2923 | InRedPrivs.emplace_back(*IPriv); |
| 2924 | InRedOps.emplace_back(*IRed); |
| 2925 | TaskgroupDescriptors.emplace_back(*ITD); |
| 2926 | std::advance(IPriv, 1); |
| 2927 | std::advance(IRed, 1); |
| 2928 | std::advance(ITD, 1); |
| 2929 | } |
| 2930 | } |
| 2931 | // Privatize in_reduction items here, because taskgroup descriptors must be |
| 2932 | // privatized earlier. |
| 2933 | OMPPrivateScope InRedScope(CGF); |
| 2934 | if (!InRedVars.empty()) { |
| 2935 | ReductionCodeGen RedCG(InRedVars, InRedPrivs, InRedOps); |
| 2936 | for (unsigned Cnt = 0, E = InRedVars.size(); Cnt < E; ++Cnt) { |
| 2937 | RedCG.emitSharedLValue(CGF, Cnt); |
| 2938 | RedCG.emitAggregateType(CGF, Cnt); |
| 2939 | // The taskgroup descriptor variable is always implicit firstprivate and |
| 2940 | // privatized already during procoessing of the firstprivates. |
| 2941 | llvm::Value *ReductionsPtr = CGF.EmitLoadOfScalar( |
| 2942 | CGF.EmitLValue(TaskgroupDescriptors[Cnt]), SourceLocation()); |
| 2943 | Address Replacement = CGF.CGM.getOpenMPRuntime().getTaskReductionItem( |
| 2944 | CGF, S.getLocStart(), ReductionsPtr, RedCG.getSharedLValue(Cnt)); |
| 2945 | Replacement = Address( |
| 2946 | CGF.EmitScalarConversion( |
| 2947 | Replacement.getPointer(), CGF.getContext().VoidPtrTy, |
| 2948 | CGF.getContext().getPointerType(InRedPrivs[Cnt]->getType()), |
| 2949 | SourceLocation()), |
| 2950 | Replacement.getAlignment()); |
| 2951 | Replacement = RedCG.adjustPrivateAddress(CGF, Cnt, Replacement); |
| 2952 | InRedScope.addPrivate(RedCG.getBaseDecl(Cnt), |
| 2953 | [Replacement]() { return Replacement; }); |
| 2954 | // FIXME: This must removed once the runtime library is fixed. |
| 2955 | // Emit required threadprivate variables for |
| 2956 | // initilizer/combiner/finalizer. |
| 2957 | CGF.CGM.getOpenMPRuntime().emitTaskReductionFixups(CGF, S.getLocStart(), |
| 2958 | RedCG, Cnt); |
| 2959 | } |
| 2960 | } |
| 2961 | (void)InRedScope.Privatize(); |
Alexey Bataev | 48591dd | 2016-04-20 04:01:36 +0000 | [diff] [blame] | 2962 | |
| 2963 | Action.Enter(CGF); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2964 | BodyGen(CGF); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 2965 | }; |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 2966 | auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 2967 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, Data.Tied, |
| 2968 | Data.NumberOfParts); |
| 2969 | OMPLexicalScope Scope(*this, S); |
| 2970 | TaskGen(*this, OutlinedFn, Data); |
| 2971 | } |
| 2972 | |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 2973 | static ImplicitParamDecl * |
| 2974 | createImplicitFirstprivateForType(ASTContext &C, OMPTaskDataTy &Data, |
| 2975 | QualType Ty, CapturedDecl *CD) { |
| 2976 | auto *OrigVD = ImplicitParamDecl::Create( |
| 2977 | C, CD, SourceLocation(), /*Id=*/nullptr, Ty, ImplicitParamDecl::Other); |
| 2978 | auto *OrigRef = |
| 2979 | DeclRefExpr::Create(C, NestedNameSpecifierLoc(), SourceLocation(), OrigVD, |
| 2980 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 2981 | SourceLocation(), Ty, VK_LValue); |
| 2982 | auto *PrivateVD = ImplicitParamDecl::Create( |
| 2983 | C, CD, SourceLocation(), /*Id=*/nullptr, Ty, ImplicitParamDecl::Other); |
| 2984 | auto *PrivateRef = DeclRefExpr::Create( |
| 2985 | C, NestedNameSpecifierLoc(), SourceLocation(), PrivateVD, |
| 2986 | /*RefersToEnclosingVariableOrCapture=*/false, SourceLocation(), Ty, |
| 2987 | VK_LValue); |
| 2988 | QualType ElemType = C.getBaseElementType(Ty); |
| 2989 | auto *InitVD = |
| 2990 | ImplicitParamDecl::Create(C, CD, SourceLocation(), /*Id=*/nullptr, |
| 2991 | ElemType, ImplicitParamDecl::Other); |
| 2992 | auto *InitRef = |
| 2993 | DeclRefExpr::Create(C, NestedNameSpecifierLoc(), SourceLocation(), InitVD, |
| 2994 | /*RefersToEnclosingVariableOrCapture=*/false, |
| 2995 | SourceLocation(), ElemType, VK_LValue); |
| 2996 | PrivateVD->setInitStyle(VarDecl::CInit); |
| 2997 | PrivateVD->setInit(ImplicitCastExpr::Create(C, ElemType, CK_LValueToRValue, |
| 2998 | InitRef, /*BasePath=*/nullptr, |
| 2999 | VK_RValue)); |
| 3000 | Data.FirstprivateVars.emplace_back(OrigRef); |
| 3001 | Data.FirstprivateCopies.emplace_back(PrivateRef); |
| 3002 | Data.FirstprivateInits.emplace_back(InitRef); |
| 3003 | return OrigVD; |
| 3004 | } |
| 3005 | |
| 3006 | void CodeGenFunction::EmitOMPTargetTaskBasedDirective( |
| 3007 | const OMPExecutableDirective &S, const RegionCodeGenTy &BodyGen, |
| 3008 | OMPTargetDataInfo &InputInfo) { |
| 3009 | // Emit outlined function for task construct. |
| 3010 | auto CS = S.getCapturedStmt(OMPD_task); |
| 3011 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 3012 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 3013 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 3014 | auto *PartId = std::next(I); |
| 3015 | auto *TaskT = std::next(I, 4); |
| 3016 | OMPTaskDataTy Data; |
| 3017 | // The task is not final. |
| 3018 | Data.Final.setInt(/*IntVal=*/false); |
| 3019 | // Get list of firstprivate variables. |
| 3020 | for (const auto *C : S.getClausesOfKind<OMPFirstprivateClause>()) { |
| 3021 | auto IRef = C->varlist_begin(); |
| 3022 | auto IElemInitRef = C->inits().begin(); |
| 3023 | for (auto *IInit : C->private_copies()) { |
| 3024 | Data.FirstprivateVars.push_back(*IRef); |
| 3025 | Data.FirstprivateCopies.push_back(IInit); |
| 3026 | Data.FirstprivateInits.push_back(*IElemInitRef); |
| 3027 | ++IRef; |
| 3028 | ++IElemInitRef; |
| 3029 | } |
| 3030 | } |
| 3031 | OMPPrivateScope TargetScope(*this); |
| 3032 | VarDecl *BPVD = nullptr; |
| 3033 | VarDecl *PVD = nullptr; |
| 3034 | VarDecl *SVD = nullptr; |
| 3035 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3036 | auto *CD = CapturedDecl::Create( |
| 3037 | getContext(), getContext().getTranslationUnitDecl(), /*NumParams=*/0); |
| 3038 | llvm::APInt ArrSize(/*numBits=*/32, InputInfo.NumberOfTargetItems); |
| 3039 | QualType BaseAndPointersType = getContext().getConstantArrayType( |
| 3040 | getContext().VoidPtrTy, ArrSize, ArrayType::Normal, |
| 3041 | /*IndexTypeQuals=*/0); |
| 3042 | BPVD = createImplicitFirstprivateForType(getContext(), Data, |
| 3043 | BaseAndPointersType, CD); |
| 3044 | PVD = createImplicitFirstprivateForType(getContext(), Data, |
| 3045 | BaseAndPointersType, CD); |
| 3046 | QualType SizesType = getContext().getConstantArrayType( |
| 3047 | getContext().getSizeType(), ArrSize, ArrayType::Normal, |
| 3048 | /*IndexTypeQuals=*/0); |
| 3049 | SVD = createImplicitFirstprivateForType(getContext(), Data, SizesType, CD); |
| 3050 | TargetScope.addPrivate( |
| 3051 | BPVD, [&InputInfo]() { return InputInfo.BasePointersArray; }); |
| 3052 | TargetScope.addPrivate(PVD, |
| 3053 | [&InputInfo]() { return InputInfo.PointersArray; }); |
| 3054 | TargetScope.addPrivate(SVD, |
| 3055 | [&InputInfo]() { return InputInfo.SizesArray; }); |
| 3056 | } |
| 3057 | (void)TargetScope.Privatize(); |
| 3058 | // Build list of dependences. |
| 3059 | for (const auto *C : S.getClausesOfKind<OMPDependClause>()) |
| 3060 | for (auto *IRef : C->varlists()) |
| 3061 | Data.Dependences.push_back(std::make_pair(C->getDependencyKind(), IRef)); |
| 3062 | auto &&CodeGen = [&Data, &S, CS, &BodyGen, BPVD, PVD, SVD, |
| 3063 | &InputInfo](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3064 | // Set proper addresses for generated private copies. |
| 3065 | OMPPrivateScope Scope(CGF); |
| 3066 | if (!Data.FirstprivateVars.empty()) { |
| 3067 | enum { PrivatesParam = 2, CopyFnParam = 3 }; |
| 3068 | auto *CopyFn = CGF.Builder.CreateLoad( |
| 3069 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(3))); |
| 3070 | auto *PrivatesPtr = CGF.Builder.CreateLoad( |
| 3071 | CGF.GetAddrOfLocalVar(CS->getCapturedDecl()->getParam(2))); |
| 3072 | // Map privates. |
| 3073 | llvm::SmallVector<std::pair<const VarDecl *, Address>, 16> PrivatePtrs; |
| 3074 | llvm::SmallVector<llvm::Value *, 16> CallArgs; |
| 3075 | CallArgs.push_back(PrivatesPtr); |
| 3076 | for (auto *E : Data.FirstprivateVars) { |
| 3077 | auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3078 | Address PrivatePtr = |
| 3079 | CGF.CreateMemTemp(CGF.getContext().getPointerType(E->getType()), |
| 3080 | ".firstpriv.ptr.addr"); |
| 3081 | PrivatePtrs.push_back(std::make_pair(VD, PrivatePtr)); |
| 3082 | CallArgs.push_back(PrivatePtr.getPointer()); |
| 3083 | } |
| 3084 | CGF.CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(), |
| 3085 | CopyFn, CallArgs); |
| 3086 | for (auto &&Pair : PrivatePtrs) { |
| 3087 | Address Replacement(CGF.Builder.CreateLoad(Pair.second), |
| 3088 | CGF.getContext().getDeclAlign(Pair.first)); |
| 3089 | Scope.addPrivate(Pair.first, [Replacement]() { return Replacement; }); |
| 3090 | } |
| 3091 | } |
| 3092 | // Privatize all private variables except for in_reduction items. |
| 3093 | (void)Scope.Privatize(); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame^] | 3094 | if (InputInfo.NumberOfTargetItems > 0) { |
| 3095 | InputInfo.BasePointersArray = CGF.Builder.CreateConstArrayGEP( |
| 3096 | CGF.GetAddrOfLocalVar(BPVD), /*Index=*/0, CGF.getPointerSize()); |
| 3097 | InputInfo.PointersArray = CGF.Builder.CreateConstArrayGEP( |
| 3098 | CGF.GetAddrOfLocalVar(PVD), /*Index=*/0, CGF.getPointerSize()); |
| 3099 | InputInfo.SizesArray = CGF.Builder.CreateConstArrayGEP( |
| 3100 | CGF.GetAddrOfLocalVar(SVD), /*Index=*/0, CGF.getSizeSize()); |
| 3101 | } |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3102 | |
| 3103 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3104 | OMPLexicalScope LexScope(CGF, S, OMPD_task, /*EmitPreInitStmt=*/false); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 3105 | BodyGen(CGF); |
| 3106 | }; |
| 3107 | auto *OutlinedFn = CGM.getOpenMPRuntime().emitTaskOutlinedFunction( |
| 3108 | S, *I, *PartId, *TaskT, S.getDirectiveKind(), CodeGen, /*Tied=*/true, |
| 3109 | Data.NumberOfParts); |
| 3110 | llvm::APInt TrueOrFalse(32, S.hasClausesOfKind<OMPNowaitClause>() ? 1 : 0); |
| 3111 | IntegerLiteral IfCond(getContext(), TrueOrFalse, |
| 3112 | getContext().getIntTypeForBitwidth(32, /*Signed=*/0), |
| 3113 | SourceLocation()); |
| 3114 | |
| 3115 | CGM.getOpenMPRuntime().emitTaskCall(*this, S.getLocStart(), S, OutlinedFn, |
| 3116 | SharedsTy, CapturedStruct, &IfCond, Data); |
| 3117 | } |
| 3118 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3119 | void CodeGenFunction::EmitOMPTaskDirective(const OMPTaskDirective &S) { |
| 3120 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3121 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_task); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3122 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
Alexey Bataev | 62b63b1 | 2015-03-10 07:28:44 +0000 | [diff] [blame] | 3123 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3124 | const Expr *IfCond = nullptr; |
Alexey Bataev | 7371aa3 | 2015-09-03 08:45:56 +0000 | [diff] [blame] | 3125 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3126 | if (C->getNameModifier() == OMPD_unknown || |
| 3127 | C->getNameModifier() == OMPD_task) { |
| 3128 | IfCond = C->getCondition(); |
| 3129 | break; |
| 3130 | } |
Alexey Bataev | 1d67713 | 2015-04-22 13:57:31 +0000 | [diff] [blame] | 3131 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3132 | |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3133 | OMPTaskDataTy Data; |
| 3134 | // Check if we should emit tied or untied task. |
| 3135 | Data.Tied = !S.getSingleClause<OMPUntiedClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3136 | auto &&BodyGen = [CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 3137 | CGF.EmitStmt(CS->getCapturedStmt()); |
| 3138 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3139 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3140 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 3141 | const OMPTaskDataTy &Data) { |
| 3142 | CGF.CGM.getOpenMPRuntime().emitTaskCall(CGF, S.getLocStart(), S, OutlinedFn, |
| 3143 | SharedsTy, CapturedStruct, IfCond, |
| 3144 | Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 3145 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3146 | EmitOMPTaskBasedDirective(S, OMPD_task, BodyGen, TaskGen, Data); |
Alexey Bataev | 9c2e8ee | 2014-07-11 11:25:16 +0000 | [diff] [blame] | 3147 | } |
| 3148 | |
Alexey Bataev | 9f797f3 | 2015-02-05 05:57:51 +0000 | [diff] [blame] | 3149 | void CodeGenFunction::EmitOMPTaskyieldDirective( |
| 3150 | const OMPTaskyieldDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3151 | CGM.getOpenMPRuntime().emitTaskyieldCall(*this, S.getLocStart()); |
Alexey Bataev | 68446b7 | 2014-07-18 07:47:19 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
Alexey Bataev | 8f7c1b0 | 2014-12-05 04:09:23 +0000 | [diff] [blame] | 3154 | void CodeGenFunction::EmitOMPBarrierDirective(const OMPBarrierDirective &S) { |
Alexey Bataev | f268568 | 2015-03-30 04:30:22 +0000 | [diff] [blame] | 3155 | CGM.getOpenMPRuntime().emitBarrierCall(*this, S.getLocStart(), OMPD_barrier); |
Alexey Bataev | 4d1dfea | 2014-07-18 09:11:51 +0000 | [diff] [blame] | 3156 | } |
| 3157 | |
Alexey Bataev | 8b8e202 | 2015-04-27 05:22:09 +0000 | [diff] [blame] | 3158 | void CodeGenFunction::EmitOMPTaskwaitDirective(const OMPTaskwaitDirective &S) { |
| 3159 | CGM.getOpenMPRuntime().emitTaskwaitCall(*this, S.getLocStart()); |
Alexey Bataev | 2df347a | 2014-07-18 10:17:07 +0000 | [diff] [blame] | 3160 | } |
| 3161 | |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3162 | void CodeGenFunction::EmitOMPTaskgroupDirective( |
| 3163 | const OMPTaskgroupDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3164 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3165 | Action.Enter(CGF); |
Alexey Bataev | 3b1b895 | 2017-07-25 15:53:26 +0000 | [diff] [blame] | 3166 | if (const Expr *E = S.getReductionRef()) { |
| 3167 | SmallVector<const Expr *, 4> LHSs; |
| 3168 | SmallVector<const Expr *, 4> RHSs; |
| 3169 | OMPTaskDataTy Data; |
| 3170 | for (const auto *C : S.getClausesOfKind<OMPTaskReductionClause>()) { |
| 3171 | auto IPriv = C->privates().begin(); |
| 3172 | auto IRed = C->reduction_ops().begin(); |
| 3173 | auto ILHS = C->lhs_exprs().begin(); |
| 3174 | auto IRHS = C->rhs_exprs().begin(); |
| 3175 | for (const auto *Ref : C->varlists()) { |
| 3176 | Data.ReductionVars.emplace_back(Ref); |
| 3177 | Data.ReductionCopies.emplace_back(*IPriv); |
| 3178 | Data.ReductionOps.emplace_back(*IRed); |
| 3179 | LHSs.emplace_back(*ILHS); |
| 3180 | RHSs.emplace_back(*IRHS); |
| 3181 | std::advance(IPriv, 1); |
| 3182 | std::advance(IRed, 1); |
| 3183 | std::advance(ILHS, 1); |
| 3184 | std::advance(IRHS, 1); |
| 3185 | } |
| 3186 | } |
| 3187 | llvm::Value *ReductionDesc = |
| 3188 | CGF.CGM.getOpenMPRuntime().emitTaskReductionInit(CGF, S.getLocStart(), |
| 3189 | LHSs, RHSs, Data); |
| 3190 | const auto *VD = cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl()); |
| 3191 | CGF.EmitVarDecl(*VD); |
| 3192 | CGF.EmitStoreOfScalar(ReductionDesc, CGF.GetAddrOfLocalVar(VD), |
| 3193 | /*Volatile=*/false, E->getType()); |
| 3194 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3195 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3196 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3197 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | c30dd2d | 2015-06-18 12:14:09 +0000 | [diff] [blame] | 3198 | CGM.getOpenMPRuntime().emitTaskgroupRegion(*this, CodeGen, S.getLocStart()); |
| 3199 | } |
| 3200 | |
Alexey Bataev | cc37cc1 | 2014-11-20 04:34:54 +0000 | [diff] [blame] | 3201 | void CodeGenFunction::EmitOMPFlushDirective(const OMPFlushDirective &S) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3202 | CGM.getOpenMPRuntime().emitFlush(*this, [&]() -> ArrayRef<const Expr *> { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3203 | if (const auto *FlushClause = S.getSingleClause<OMPFlushClause>()) { |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3204 | return llvm::makeArrayRef(FlushClause->varlist_begin(), |
| 3205 | FlushClause->varlist_end()); |
| 3206 | } |
| 3207 | return llvm::None; |
| 3208 | }(), S.getLocStart()); |
Alexey Bataev | 6125da9 | 2014-07-21 11:26:11 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3211 | void CodeGenFunction::EmitOMPDistributeLoop(const OMPLoopDirective &S, |
| 3212 | const CodeGenLoopTy &CodeGenLoop, |
| 3213 | Expr *IncExpr) { |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3214 | // Emit the loop iteration variable. |
| 3215 | auto IVExpr = cast<DeclRefExpr>(S.getIterationVariable()); |
| 3216 | auto IVDecl = cast<VarDecl>(IVExpr->getDecl()); |
| 3217 | EmitVarDecl(*IVDecl); |
| 3218 | |
| 3219 | // Emit the iterations count variable. |
| 3220 | // If it is not a variable, Sema decided to calculate iterations count on each |
| 3221 | // iteration (e.g., it is foldable into a constant). |
| 3222 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 3223 | EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 3224 | // Emit calculation of the iterations count. |
| 3225 | EmitIgnoredExpr(S.getCalcLastIteration()); |
| 3226 | } |
| 3227 | |
| 3228 | auto &RT = CGM.getOpenMPRuntime(); |
| 3229 | |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3230 | bool HasLastprivateClause = false; |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3231 | // Check pre-condition. |
| 3232 | { |
Alexey Bataev | 5a3af13 | 2016-03-29 08:58:54 +0000 | [diff] [blame] | 3233 | OMPLoopScope PreInitScope(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3234 | // Skip the entire loop if we don't meet the precondition. |
| 3235 | // If the condition constant folds and can be elided, avoid emitting the |
| 3236 | // whole loop. |
| 3237 | bool CondConstant; |
| 3238 | llvm::BasicBlock *ContBlock = nullptr; |
| 3239 | if (ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 3240 | if (!CondConstant) |
| 3241 | return; |
| 3242 | } else { |
| 3243 | auto *ThenBlock = createBasicBlock("omp.precond.then"); |
| 3244 | ContBlock = createBasicBlock("omp.precond.end"); |
| 3245 | emitPreCond(*this, S, S.getPreCond(), ThenBlock, ContBlock, |
| 3246 | getProfileCount(&S)); |
| 3247 | EmitBlock(ThenBlock); |
| 3248 | incrementProfileCounter(&S); |
| 3249 | } |
| 3250 | |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3251 | emitAlignedClause(*this, S); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3252 | // Emit 'then' code. |
| 3253 | { |
| 3254 | // Emit helper vars inits. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3255 | |
| 3256 | LValue LB = EmitOMPHelperVar( |
| 3257 | *this, cast<DeclRefExpr>( |
| 3258 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3259 | ? S.getCombinedLowerBoundVariable() |
| 3260 | : S.getLowerBoundVariable()))); |
| 3261 | LValue UB = EmitOMPHelperVar( |
| 3262 | *this, cast<DeclRefExpr>( |
| 3263 | (isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3264 | ? S.getCombinedUpperBoundVariable() |
| 3265 | : S.getUpperBoundVariable()))); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3266 | LValue ST = |
| 3267 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getStrideVariable())); |
| 3268 | LValue IL = |
| 3269 | EmitOMPHelperVar(*this, cast<DeclRefExpr>(S.getIsLastIterVariable())); |
| 3270 | |
| 3271 | OMPPrivateScope LoopScope(*this); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3272 | if (EmitOMPFirstprivateClause(S, LoopScope)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3273 | // Emit implicit barrier to synchronize threads and avoid data races |
| 3274 | // on initialization of firstprivate variables and post-update of |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3275 | // lastprivate variables. |
| 3276 | CGM.getOpenMPRuntime().emitBarrierCall( |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3277 | *this, S.getLocStart(), OMPD_unknown, /*EmitChecks=*/false, |
| 3278 | /*ForceSimpleCall=*/true); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3279 | } |
| 3280 | EmitOMPPrivateClause(S, LoopScope); |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3281 | if (isOpenMPSimdDirective(S.getDirectiveKind()) && |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 3282 | !isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3283 | !isOpenMPTeamsDirective(S.getDirectiveKind())) |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3284 | EmitOMPReductionClauseInit(S, LoopScope); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3285 | HasLastprivateClause = EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 5dff95c | 2016-04-22 03:56:56 +0000 | [diff] [blame] | 3286 | EmitOMPPrivateLoopCounters(S, LoopScope); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3287 | (void)LoopScope.Privatize(); |
| 3288 | |
| 3289 | // Detect the distribute schedule kind and chunk. |
| 3290 | llvm::Value *Chunk = nullptr; |
| 3291 | OpenMPDistScheduleClauseKind ScheduleKind = OMPC_DIST_SCHEDULE_unknown; |
| 3292 | if (auto *C = S.getSingleClause<OMPDistScheduleClause>()) { |
| 3293 | ScheduleKind = C->getDistScheduleKind(); |
| 3294 | if (const auto *Ch = C->getChunkSize()) { |
| 3295 | Chunk = EmitScalarExpr(Ch); |
| 3296 | Chunk = EmitScalarConversion(Chunk, Ch->getType(), |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3297 | S.getIterationVariable()->getType(), |
| 3298 | S.getLocStart()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | const unsigned IVSize = getContext().getTypeSize(IVExpr->getType()); |
| 3302 | const bool IVSigned = IVExpr->getType()->hasSignedIntegerRepresentation(); |
| 3303 | |
| 3304 | // OpenMP [2.10.8, distribute Construct, Description] |
| 3305 | // If dist_schedule is specified, kind must be static. If specified, |
| 3306 | // iterations are divided into chunks of size chunk_size, chunks are |
| 3307 | // assigned to the teams of the league in a round-robin fashion in the |
| 3308 | // order of the team number. When no chunk_size is specified, the |
| 3309 | // iteration space is divided into chunks that are approximately equal |
| 3310 | // in size, and at most one chunk is distributed to each team of the |
| 3311 | // league. The size of the chunks is unspecified in this case. |
| 3312 | if (RT.isStaticNonchunked(ScheduleKind, |
| 3313 | /* Chunked */ Chunk != nullptr)) { |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3314 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 3315 | EmitOMPSimdInit(S, /*IsMonotonic=*/true); |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3316 | CGOpenMPRuntime::StaticRTInput StaticInit( |
| 3317 | IVSize, IVSigned, /* Ordered = */ false, IL.getAddress(), |
| 3318 | LB.getAddress(), UB.getAddress(), ST.getAddress()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3319 | RT.emitDistributeStaticInit(*this, S.getLocStart(), ScheduleKind, |
Alexey Bataev | 0f87dbe | 2017-08-14 17:56:13 +0000 | [diff] [blame] | 3320 | StaticInit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3321 | auto LoopExit = |
| 3322 | getJumpDestInCurrentScope(createBasicBlock("omp.loop.exit")); |
| 3323 | // UB = min(UB, GlobalUB); |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3324 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3325 | ? S.getCombinedEnsureUpperBound() |
| 3326 | : S.getEnsureUpperBound()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3327 | // IV = LB; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3328 | EmitIgnoredExpr(isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3329 | ? S.getCombinedInit() |
| 3330 | : S.getInit()); |
| 3331 | |
| 3332 | Expr *Cond = isOpenMPLoopBoundSharingDirective(S.getDirectiveKind()) |
| 3333 | ? S.getCombinedCond() |
| 3334 | : S.getCond(); |
| 3335 | |
| 3336 | // for distribute alone, codegen |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3337 | // while (idx <= UB) { BODY; ++idx; } |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3338 | // when combined with 'for' (e.g. as in 'distribute parallel for') |
| 3339 | // while (idx <= UB) { <CodeGen rest of pragma>; idx += ST; } |
| 3340 | EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), Cond, IncExpr, |
| 3341 | [&S, LoopExit, &CodeGenLoop](CodeGenFunction &CGF) { |
| 3342 | CodeGenLoop(CGF, S, LoopExit); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3343 | }, |
| 3344 | [](CodeGenFunction &) {}); |
| 3345 | EmitBlock(LoopExit.getBlock()); |
| 3346 | // Tell the runtime we are done. |
Alexey Bataev | f43f714 | 2017-09-06 16:17:35 +0000 | [diff] [blame] | 3347 | RT.emitForStaticFinish(*this, S.getLocStart(), S.getDirectiveKind()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3348 | } else { |
| 3349 | // Emit the outer loop, which requests its work chunk [LB..UB] from |
| 3350 | // runtime and runs the inner loop to process it. |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3351 | const OMPLoopArguments LoopArguments = { |
| 3352 | LB.getAddress(), UB.getAddress(), ST.getAddress(), IL.getAddress(), |
| 3353 | Chunk}; |
| 3354 | EmitOMPDistributeOuterLoop(ScheduleKind, S, LoopScope, LoopArguments, |
| 3355 | CodeGenLoop); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3356 | } |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3357 | if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 3358 | EmitOMPSimdFinal(S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 3359 | return CGF.Builder.CreateIsNotNull( |
| 3360 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 3361 | }); |
| 3362 | } |
| 3363 | OpenMPDirectiveKind ReductionKind = OMPD_unknown; |
| 3364 | if (isOpenMPParallelDirective(S.getDirectiveKind()) && |
| 3365 | isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 3366 | ReductionKind = OMPD_parallel_for_simd; |
| 3367 | } else if (isOpenMPParallelDirective(S.getDirectiveKind())) { |
| 3368 | ReductionKind = OMPD_parallel_for; |
| 3369 | } else if (isOpenMPSimdDirective(S.getDirectiveKind())) { |
| 3370 | ReductionKind = OMPD_simd; |
| 3371 | } else if (!isOpenMPTeamsDirective(S.getDirectiveKind()) && |
| 3372 | S.hasClausesOfKind<OMPReductionClause>()) { |
| 3373 | llvm_unreachable( |
| 3374 | "No reduction clauses is allowed in distribute directive."); |
| 3375 | } |
| 3376 | EmitOMPReductionClauseFinal(S, ReductionKind); |
| 3377 | // Emit post-update of the reduction variables if IsLastIter != 0. |
| 3378 | emitPostUpdateForReductionClause( |
| 3379 | *this, S, [&](CodeGenFunction &CGF) -> llvm::Value * { |
| 3380 | return CGF.Builder.CreateIsNotNull( |
| 3381 | CGF.EmitLoadOfScalar(IL, S.getLocStart())); |
| 3382 | }); |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3383 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3384 | if (HasLastprivateClause) { |
Carlo Bertolli | 962bb80 | 2017-01-03 18:24:42 +0000 | [diff] [blame] | 3385 | EmitOMPLastprivateClauseFinal( |
| 3386 | S, /*NoFinals=*/false, |
Alexey Bataev | 617db5f | 2017-12-04 15:38:33 +0000 | [diff] [blame] | 3387 | Builder.CreateIsNotNull(EmitLoadOfScalar(IL, S.getLocStart()))); |
| 3388 | } |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3389 | } |
| 3390 | |
| 3391 | // We're now done with the loop, so jump to the continuation block. |
| 3392 | if (ContBlock) { |
| 3393 | EmitBranch(ContBlock); |
| 3394 | EmitBlock(ContBlock, true); |
| 3395 | } |
| 3396 | } |
| 3397 | } |
| 3398 | |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3399 | void CodeGenFunction::EmitOMPDistributeDirective( |
| 3400 | const OMPDistributeDirective &S) { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3401 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 3402 | |
| 3403 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
Carlo Bertolli | fc35ad2 | 2016-03-07 16:04:49 +0000 | [diff] [blame] | 3404 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3405 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 3406 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_distribute, CodeGen); |
Carlo Bertolli | 6200a3d | 2015-12-14 14:51:25 +0000 | [diff] [blame] | 3407 | } |
| 3408 | |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3409 | static llvm::Function *emitOutlinedOrderedFunction(CodeGenModule &CGM, |
| 3410 | const CapturedStmt *S) { |
| 3411 | CodeGenFunction CGF(CGM, /*suppressNewContext=*/true); |
| 3412 | CodeGenFunction::CGCapturedStmtInfo CapStmtInfo; |
| 3413 | CGF.CapturedStmtInfo = &CapStmtInfo; |
| 3414 | auto *Fn = CGF.GenerateOpenMPCapturedStmtFunction(*S); |
| 3415 | Fn->addFnAttr(llvm::Attribute::NoInline); |
| 3416 | return Fn; |
| 3417 | } |
| 3418 | |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3419 | void CodeGenFunction::EmitOMPOrderedDirective(const OMPOrderedDirective &S) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3420 | if (S.hasClausesOfKind<OMPDependClause>()) { |
| 3421 | assert(!S.getAssociatedStmt() && |
| 3422 | "No associated statement must be in ordered depend construct."); |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3423 | for (const auto *DC : S.getClausesOfKind<OMPDependClause>()) |
| 3424 | CGM.getOpenMPRuntime().emitDoacrossOrdered(*this, DC); |
Alexey Bataev | 8ef3141 | 2015-12-18 07:58:25 +0000 | [diff] [blame] | 3425 | return; |
Alexey Bataev | 8b42706 | 2016-05-25 12:36:08 +0000 | [diff] [blame] | 3426 | } |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3427 | auto *C = S.getSingleClause<OMPSIMDClause>(); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3428 | auto &&CodeGen = [&S, C, this](CodeGenFunction &CGF, |
| 3429 | PrePostActionTy &Action) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3430 | const CapturedStmt *CS = S.getInnermostCapturedStmt(); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3431 | if (C) { |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3432 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 3433 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
| 3434 | auto *OutlinedFn = emitOutlinedOrderedFunction(CGM, CS); |
Alexey Bataev | 3c595a6 | 2017-08-14 15:01:03 +0000 | [diff] [blame] | 3435 | CGM.getOpenMPRuntime().emitOutlinedFunctionCall(CGF, S.getLocStart(), |
| 3436 | OutlinedFn, CapturedVars); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3437 | } else { |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3438 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3439 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3440 | } |
Alexey Bataev | 98eb6e3 | 2015-04-22 11:15:40 +0000 | [diff] [blame] | 3441 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3442 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 5f600d6 | 2015-09-29 03:48:57 +0000 | [diff] [blame] | 3443 | CGM.getOpenMPRuntime().emitOrderedRegion(*this, CodeGen, S.getLocStart(), !C); |
Alexey Bataev | 9fb6e64 | 2014-07-22 06:45:04 +0000 | [diff] [blame] | 3444 | } |
| 3445 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3446 | static llvm::Value *convertToScalarValue(CodeGenFunction &CGF, RValue Val, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3447 | QualType SrcType, QualType DestType, |
| 3448 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3449 | assert(CGF.hasScalarEvaluationKind(DestType) && |
| 3450 | "DestType must have scalar evaluation kind."); |
| 3451 | assert(!Val.isAggregate() && "Must be a scalar or complex."); |
| 3452 | return Val.isScalar() |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3453 | ? CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, DestType, |
| 3454 | Loc) |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3455 | : CGF.EmitComplexToScalarConversion(Val.getComplexVal(), SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3456 | DestType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3457 | } |
| 3458 | |
| 3459 | static CodeGenFunction::ComplexPairTy |
| 3460 | convertToComplexValue(CodeGenFunction &CGF, RValue Val, QualType SrcType, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3461 | QualType DestType, SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3462 | assert(CGF.getEvaluationKind(DestType) == TEK_Complex && |
| 3463 | "DestType must have complex evaluation kind."); |
| 3464 | CodeGenFunction::ComplexPairTy ComplexVal; |
| 3465 | if (Val.isScalar()) { |
| 3466 | // Convert the input element to the element type of the complex. |
| 3467 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3468 | auto ScalarVal = CGF.EmitScalarConversion(Val.getScalarVal(), SrcType, |
| 3469 | DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3470 | ComplexVal = CodeGenFunction::ComplexPairTy( |
| 3471 | ScalarVal, llvm::Constant::getNullValue(ScalarVal->getType())); |
| 3472 | } else { |
| 3473 | assert(Val.isComplex() && "Must be a scalar or complex."); |
| 3474 | auto SrcElementType = SrcType->castAs<ComplexType>()->getElementType(); |
| 3475 | auto DestElementType = DestType->castAs<ComplexType>()->getElementType(); |
| 3476 | ComplexVal.first = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3477 | Val.getComplexVal().first, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3478 | ComplexVal.second = CGF.EmitScalarConversion( |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3479 | Val.getComplexVal().second, SrcElementType, DestElementType, Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3480 | } |
| 3481 | return ComplexVal; |
| 3482 | } |
| 3483 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3484 | static void emitSimpleAtomicStore(CodeGenFunction &CGF, bool IsSeqCst, |
| 3485 | LValue LVal, RValue RVal) { |
| 3486 | if (LVal.isGlobalReg()) { |
| 3487 | CGF.EmitStoreThroughGlobalRegLValue(RVal, LVal); |
| 3488 | } else { |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3489 | CGF.EmitAtomicStore(RVal, LVal, |
| 3490 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3491 | : llvm::AtomicOrdering::Monotonic, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3492 | LVal.isVolatile(), /*IsInit=*/false); |
| 3493 | } |
| 3494 | } |
| 3495 | |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3496 | void CodeGenFunction::emitOMPSimpleStore(LValue LVal, RValue RVal, |
| 3497 | QualType RValTy, SourceLocation Loc) { |
| 3498 | switch (getEvaluationKind(LVal.getType())) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3499 | case TEK_Scalar: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3500 | EmitStoreThroughLValue(RValue::get(convertToScalarValue( |
| 3501 | *this, RVal, RValTy, LVal.getType(), Loc)), |
| 3502 | LVal); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3503 | break; |
| 3504 | case TEK_Complex: |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3505 | EmitStoreOfComplex( |
| 3506 | convertToComplexValue(*this, RVal, RValTy, LVal.getType(), Loc), LVal, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3507 | /*isInit=*/false); |
| 3508 | break; |
| 3509 | case TEK_Aggregate: |
| 3510 | llvm_unreachable("Must be a scalar or complex."); |
| 3511 | } |
| 3512 | } |
| 3513 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3514 | static void EmitOMPAtomicReadExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3515 | const Expr *X, const Expr *V, |
| 3516 | SourceLocation Loc) { |
| 3517 | // v = x; |
| 3518 | assert(V->isLValue() && "V of 'omp atomic read' is not lvalue"); |
| 3519 | assert(X->isLValue() && "X of 'omp atomic read' is not lvalue"); |
| 3520 | LValue XLValue = CGF.EmitLValue(X); |
| 3521 | LValue VLValue = CGF.EmitLValue(V); |
David Majnemer | a5b195a | 2015-02-14 01:35:12 +0000 | [diff] [blame] | 3522 | RValue Res = XLValue.isGlobalReg() |
| 3523 | ? CGF.EmitLoadOfLValue(XLValue, Loc) |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3524 | : CGF.EmitAtomicLoad( |
| 3525 | XLValue, Loc, |
| 3526 | IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3527 | : llvm::AtomicOrdering::Monotonic, |
| 3528 | XLValue.isVolatile()); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3529 | // OpenMP, 2.12.6, atomic Construct |
| 3530 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3531 | // performed operation to include an implicit flush operation without a |
| 3532 | // list. |
| 3533 | if (IsSeqCst) |
Alexey Bataev | 3eff5f4 | 2015-02-25 08:32:46 +0000 | [diff] [blame] | 3534 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3535 | CGF.emitOMPSimpleStore(VLValue, Res, X->getType().getNonReferenceType(), Loc); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3536 | } |
| 3537 | |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3538 | static void EmitOMPAtomicWriteExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3539 | const Expr *X, const Expr *E, |
| 3540 | SourceLocation Loc) { |
| 3541 | // x = expr; |
| 3542 | assert(X->isLValue() && "X of 'omp atomic write' is not lvalue"); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3543 | emitSimpleAtomicStore(CGF, IsSeqCst, CGF.EmitLValue(X), CGF.EmitAnyExpr(E)); |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3544 | // OpenMP, 2.12.6, atomic Construct |
| 3545 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3546 | // performed operation to include an implicit flush operation without a |
| 3547 | // list. |
| 3548 | if (IsSeqCst) |
| 3549 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3550 | } |
| 3551 | |
Benjamin Kramer | 439ee9d | 2015-05-01 13:59:53 +0000 | [diff] [blame] | 3552 | static std::pair<bool, RValue> emitOMPAtomicRMW(CodeGenFunction &CGF, LValue X, |
| 3553 | RValue Update, |
| 3554 | BinaryOperatorKind BO, |
| 3555 | llvm::AtomicOrdering AO, |
| 3556 | bool IsXLHSInRHSPart) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3557 | auto &Context = CGF.CGM.getContext(); |
| 3558 | // 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] | 3559 | // expression is simple and atomic is allowed for the given type for the |
| 3560 | // target platform. |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3561 | if (BO == BO_Comma || !Update.isScalar() || |
Alexey Bataev | 9d541a7 | 2015-05-08 11:47:16 +0000 | [diff] [blame] | 3562 | !Update.getScalarVal()->getType()->isIntegerTy() || |
| 3563 | !X.isSimple() || (!isa<llvm::ConstantInt>(Update.getScalarVal()) && |
| 3564 | (Update.getScalarVal()->getType() != |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3565 | X.getAddress().getElementType())) || |
| 3566 | !X.getAddress().getElementType()->isIntegerTy() || |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3567 | !Context.getTargetInfo().hasBuiltinAtomic( |
| 3568 | Context.getTypeSize(X.getType()), Context.toBits(X.getAlignment()))) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3569 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3570 | |
| 3571 | llvm::AtomicRMWInst::BinOp RMWOp; |
| 3572 | switch (BO) { |
| 3573 | case BO_Add: |
| 3574 | RMWOp = llvm::AtomicRMWInst::Add; |
| 3575 | break; |
| 3576 | case BO_Sub: |
| 3577 | if (!IsXLHSInRHSPart) |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3578 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3579 | RMWOp = llvm::AtomicRMWInst::Sub; |
| 3580 | break; |
| 3581 | case BO_And: |
| 3582 | RMWOp = llvm::AtomicRMWInst::And; |
| 3583 | break; |
| 3584 | case BO_Or: |
| 3585 | RMWOp = llvm::AtomicRMWInst::Or; |
| 3586 | break; |
| 3587 | case BO_Xor: |
| 3588 | RMWOp = llvm::AtomicRMWInst::Xor; |
| 3589 | break; |
| 3590 | case BO_LT: |
| 3591 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3592 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Min |
| 3593 | : llvm::AtomicRMWInst::Max) |
| 3594 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMin |
| 3595 | : llvm::AtomicRMWInst::UMax); |
| 3596 | break; |
| 3597 | case BO_GT: |
| 3598 | RMWOp = X.getType()->hasSignedIntegerRepresentation() |
| 3599 | ? (IsXLHSInRHSPart ? llvm::AtomicRMWInst::Max |
| 3600 | : llvm::AtomicRMWInst::Min) |
| 3601 | : (IsXLHSInRHSPart ? llvm::AtomicRMWInst::UMax |
| 3602 | : llvm::AtomicRMWInst::UMin); |
| 3603 | break; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3604 | case BO_Assign: |
| 3605 | RMWOp = llvm::AtomicRMWInst::Xchg; |
| 3606 | break; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3607 | case BO_Mul: |
| 3608 | case BO_Div: |
| 3609 | case BO_Rem: |
| 3610 | case BO_Shl: |
| 3611 | case BO_Shr: |
| 3612 | case BO_LAnd: |
| 3613 | case BO_LOr: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3614 | return std::make_pair(false, RValue::get(nullptr)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3615 | case BO_PtrMemD: |
| 3616 | case BO_PtrMemI: |
| 3617 | case BO_LE: |
| 3618 | case BO_GE: |
| 3619 | case BO_EQ: |
| 3620 | case BO_NE: |
Richard Smith | c70f1d6 | 2017-12-14 15:16:18 +0000 | [diff] [blame] | 3621 | case BO_Cmp: |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3622 | case BO_AddAssign: |
| 3623 | case BO_SubAssign: |
| 3624 | case BO_AndAssign: |
| 3625 | case BO_OrAssign: |
| 3626 | case BO_XorAssign: |
| 3627 | case BO_MulAssign: |
| 3628 | case BO_DivAssign: |
| 3629 | case BO_RemAssign: |
| 3630 | case BO_ShlAssign: |
| 3631 | case BO_ShrAssign: |
| 3632 | case BO_Comma: |
| 3633 | llvm_unreachable("Unsupported atomic update operation"); |
| 3634 | } |
| 3635 | auto *UpdateVal = Update.getScalarVal(); |
| 3636 | if (auto *IC = dyn_cast<llvm::ConstantInt>(UpdateVal)) { |
| 3637 | UpdateVal = CGF.Builder.CreateIntCast( |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3638 | IC, X.getAddress().getElementType(), |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3639 | X.getType()->hasSignedIntegerRepresentation()); |
| 3640 | } |
John McCall | 7f416cc | 2015-09-08 08:05:57 +0000 | [diff] [blame] | 3641 | auto *Res = CGF.Builder.CreateAtomicRMW(RMWOp, X.getPointer(), UpdateVal, AO); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3642 | return std::make_pair(true, RValue::get(Res)); |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3645 | std::pair<bool, RValue> CodeGenFunction::EmitOMPAtomicSimpleUpdateExpr( |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3646 | LValue X, RValue E, BinaryOperatorKind BO, bool IsXLHSInRHSPart, |
| 3647 | llvm::AtomicOrdering AO, SourceLocation Loc, |
| 3648 | const llvm::function_ref<RValue(RValue)> &CommonGen) { |
| 3649 | // Update expressions are allowed to have the following forms: |
| 3650 | // x binop= expr; -> xrval + expr; |
| 3651 | // x++, ++x -> xrval + 1; |
| 3652 | // x--, --x -> xrval - 1; |
| 3653 | // x = x binop expr; -> xrval binop expr |
| 3654 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3655 | auto Res = emitOMPAtomicRMW(*this, X, E, BO, AO, IsXLHSInRHSPart); |
| 3656 | if (!Res.first) { |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3657 | if (X.isGlobalReg()) { |
| 3658 | // Emit an update expression: 'xrval' binop 'expr' or 'expr' binop |
| 3659 | // 'xrval'. |
| 3660 | EmitStoreThroughLValue(CommonGen(EmitLoadOfLValue(X, Loc)), X); |
| 3661 | } else { |
| 3662 | // Perform compare-and-swap procedure. |
| 3663 | EmitAtomicUpdate(X, AO, CommonGen, X.getType().isVolatileQualified()); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3664 | } |
| 3665 | } |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3666 | return Res; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | static void EmitOMPAtomicUpdateExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3670 | const Expr *X, const Expr *E, |
| 3671 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3672 | SourceLocation Loc) { |
| 3673 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3674 | "Update expr in 'atomic update' must be a binary operator."); |
| 3675 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 3676 | // Update expressions are allowed to have the following forms: |
| 3677 | // x binop= expr; -> xrval + expr; |
| 3678 | // x++, ++x -> xrval + 1; |
| 3679 | // x--, --x -> xrval - 1; |
| 3680 | // x = x binop expr; -> xrval binop expr |
| 3681 | // x = expr Op x; - > expr binop xrval; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3682 | assert(X->isLValue() && "X of 'omp atomic update' is not lvalue"); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3683 | LValue XLValue = CGF.EmitLValue(X); |
| 3684 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3685 | auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3686 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 794ba0d | 2015-04-10 10:43:45 +0000 | [diff] [blame] | 3687 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3688 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3689 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3690 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3691 | auto Gen = |
| 3692 | [&CGF, UE, ExprRValue, XRValExpr, ERValExpr](RValue XRValue) -> RValue { |
| 3693 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3694 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3695 | return CGF.EmitAnyExpr(UE); |
| 3696 | }; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3697 | (void)CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3698 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3699 | // OpenMP, 2.12.6, atomic Construct |
| 3700 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3701 | // performed operation to include an implicit flush operation without a |
| 3702 | // list. |
| 3703 | if (IsSeqCst) |
| 3704 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3705 | } |
| 3706 | |
| 3707 | static RValue convertToType(CodeGenFunction &CGF, RValue Value, |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3708 | QualType SourceType, QualType ResType, |
| 3709 | SourceLocation Loc) { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3710 | switch (CGF.getEvaluationKind(ResType)) { |
| 3711 | case TEK_Scalar: |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3712 | return RValue::get( |
| 3713 | convertToScalarValue(CGF, Value, SourceType, ResType, Loc)); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3714 | case TEK_Complex: { |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3715 | auto Res = convertToComplexValue(CGF, Value, SourceType, ResType, Loc); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3716 | return RValue::getComplex(Res.first, Res.second); |
| 3717 | } |
| 3718 | case TEK_Aggregate: |
| 3719 | break; |
| 3720 | } |
| 3721 | llvm_unreachable("Must be a scalar or complex."); |
| 3722 | } |
| 3723 | |
| 3724 | static void EmitOMPAtomicCaptureExpr(CodeGenFunction &CGF, bool IsSeqCst, |
| 3725 | bool IsPostfixUpdate, const Expr *V, |
| 3726 | const Expr *X, const Expr *E, |
| 3727 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3728 | SourceLocation Loc) { |
| 3729 | assert(X->isLValue() && "X of 'omp atomic capture' is not lvalue"); |
| 3730 | assert(V->isLValue() && "V of 'omp atomic capture' is not lvalue"); |
| 3731 | RValue NewVVal; |
| 3732 | LValue VLValue = CGF.EmitLValue(V); |
| 3733 | LValue XLValue = CGF.EmitLValue(X); |
| 3734 | RValue ExprRValue = CGF.EmitAnyExpr(E); |
JF Bastien | 92f4ef1 | 2016-04-06 17:26:42 +0000 | [diff] [blame] | 3735 | auto AO = IsSeqCst ? llvm::AtomicOrdering::SequentiallyConsistent |
| 3736 | : llvm::AtomicOrdering::Monotonic; |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3737 | QualType NewVValType; |
| 3738 | if (UE) { |
| 3739 | // 'x' is updated with some additional value. |
| 3740 | assert(isa<BinaryOperator>(UE->IgnoreImpCasts()) && |
| 3741 | "Update expr in 'atomic capture' must be a binary operator."); |
| 3742 | auto *BOUE = cast<BinaryOperator>(UE->IgnoreImpCasts()); |
| 3743 | // Update expressions are allowed to have the following forms: |
| 3744 | // x binop= expr; -> xrval + expr; |
| 3745 | // x++, ++x -> xrval + 1; |
| 3746 | // x--, --x -> xrval - 1; |
| 3747 | // x = x binop expr; -> xrval binop expr |
| 3748 | // x = expr Op x; - > expr binop xrval; |
| 3749 | auto *LHS = cast<OpaqueValueExpr>(BOUE->getLHS()->IgnoreImpCasts()); |
| 3750 | auto *RHS = cast<OpaqueValueExpr>(BOUE->getRHS()->IgnoreImpCasts()); |
| 3751 | auto *XRValExpr = IsXLHSInRHSPart ? LHS : RHS; |
| 3752 | NewVValType = XRValExpr->getType(); |
| 3753 | auto *ERValExpr = IsXLHSInRHSPart ? RHS : LHS; |
| 3754 | auto &&Gen = [&CGF, &NewVVal, UE, ExprRValue, XRValExpr, ERValExpr, |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 3755 | IsPostfixUpdate](RValue XRValue) -> RValue { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3756 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3757 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, XRValue); |
| 3758 | RValue Res = CGF.EmitAnyExpr(UE); |
| 3759 | NewVVal = IsPostfixUpdate ? XRValue : Res; |
| 3760 | return Res; |
| 3761 | }; |
| 3762 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3763 | XLValue, ExprRValue, BOUE->getOpcode(), IsXLHSInRHSPart, AO, Loc, Gen); |
| 3764 | if (Res.first) { |
| 3765 | // 'atomicrmw' instruction was generated. |
| 3766 | if (IsPostfixUpdate) { |
| 3767 | // Use old value from 'atomicrmw'. |
| 3768 | NewVVal = Res.second; |
| 3769 | } else { |
| 3770 | // 'atomicrmw' does not provide new value, so evaluate it using old |
| 3771 | // value of 'x'. |
| 3772 | CodeGenFunction::OpaqueValueMapping MapExpr(CGF, ERValExpr, ExprRValue); |
| 3773 | CodeGenFunction::OpaqueValueMapping MapX(CGF, XRValExpr, Res.second); |
| 3774 | NewVVal = CGF.EmitAnyExpr(UE); |
| 3775 | } |
| 3776 | } |
| 3777 | } else { |
| 3778 | // 'x' is simply rewritten with some 'expr'. |
| 3779 | NewVValType = X->getType().getNonReferenceType(); |
| 3780 | ExprRValue = convertToType(CGF, ExprRValue, E->getType(), |
Filipe Cabecinhas | 7af183d | 2015-08-11 04:19:28 +0000 | [diff] [blame] | 3781 | X->getType().getNonReferenceType(), Loc); |
Malcolm Parsons | c6e4583 | 2017-01-13 18:55:32 +0000 | [diff] [blame] | 3782 | auto &&Gen = [&NewVVal, ExprRValue](RValue XRValue) -> RValue { |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3783 | NewVVal = XRValue; |
| 3784 | return ExprRValue; |
| 3785 | }; |
| 3786 | // Try to perform atomicrmw xchg, otherwise simple exchange. |
| 3787 | auto Res = CGF.EmitOMPAtomicSimpleUpdateExpr( |
| 3788 | XLValue, ExprRValue, /*BO=*/BO_Assign, /*IsXLHSInRHSPart=*/false, AO, |
| 3789 | Loc, Gen); |
| 3790 | if (Res.first) { |
| 3791 | // 'atomicrmw' instruction was generated. |
| 3792 | NewVVal = IsPostfixUpdate ? Res.second : ExprRValue; |
| 3793 | } |
| 3794 | } |
| 3795 | // Emit post-update store to 'v' of old/new 'x' value. |
Alexey Bataev | 8524d15 | 2016-01-21 12:35:58 +0000 | [diff] [blame] | 3796 | CGF.emitOMPSimpleStore(VLValue, NewVVal, NewVValType, Loc); |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3797 | // OpenMP, 2.12.6, atomic Construct |
| 3798 | // Any atomic construct with a seq_cst clause forces the atomically |
| 3799 | // performed operation to include an implicit flush operation without a |
| 3800 | // list. |
| 3801 | if (IsSeqCst) |
| 3802 | CGF.CGM.getOpenMPRuntime().emitFlush(CGF, llvm::None, Loc); |
| 3803 | } |
| 3804 | |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3805 | static void EmitOMPAtomicExpr(CodeGenFunction &CGF, OpenMPClauseKind Kind, |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3806 | bool IsSeqCst, bool IsPostfixUpdate, |
| 3807 | const Expr *X, const Expr *V, const Expr *E, |
| 3808 | const Expr *UE, bool IsXLHSInRHSPart, |
| 3809 | SourceLocation Loc) { |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3810 | switch (Kind) { |
| 3811 | case OMPC_read: |
| 3812 | EmitOMPAtomicReadExpr(CGF, IsSeqCst, X, V, Loc); |
| 3813 | break; |
| 3814 | case OMPC_write: |
Alexey Bataev | b832926 | 2015-02-27 06:33:30 +0000 | [diff] [blame] | 3815 | EmitOMPAtomicWriteExpr(CGF, IsSeqCst, X, E, Loc); |
| 3816 | break; |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3817 | case OMPC_unknown: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3818 | case OMPC_update: |
Alexey Bataev | b4505a7 | 2015-03-30 05:20:59 +0000 | [diff] [blame] | 3819 | EmitOMPAtomicUpdateExpr(CGF, IsSeqCst, X, E, UE, IsXLHSInRHSPart, Loc); |
| 3820 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3821 | case OMPC_capture: |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3822 | EmitOMPAtomicCaptureExpr(CGF, IsSeqCst, IsPostfixUpdate, V, X, E, UE, |
| 3823 | IsXLHSInRHSPart, Loc); |
| 3824 | break; |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3825 | case OMPC_if: |
| 3826 | case OMPC_final: |
| 3827 | case OMPC_num_threads: |
| 3828 | case OMPC_private: |
| 3829 | case OMPC_firstprivate: |
| 3830 | case OMPC_lastprivate: |
| 3831 | case OMPC_reduction: |
Alexey Bataev | 169d96a | 2017-07-18 20:17:46 +0000 | [diff] [blame] | 3832 | case OMPC_task_reduction: |
Alexey Bataev | fa312f3 | 2017-07-21 18:48:21 +0000 | [diff] [blame] | 3833 | case OMPC_in_reduction: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3834 | case OMPC_safelen: |
Alexey Bataev | 66b15b5 | 2015-08-21 11:14:16 +0000 | [diff] [blame] | 3835 | case OMPC_simdlen: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3836 | case OMPC_collapse: |
| 3837 | case OMPC_default: |
| 3838 | case OMPC_seq_cst: |
| 3839 | case OMPC_shared: |
| 3840 | case OMPC_linear: |
| 3841 | case OMPC_aligned: |
| 3842 | case OMPC_copyin: |
| 3843 | case OMPC_copyprivate: |
| 3844 | case OMPC_flush: |
| 3845 | case OMPC_proc_bind: |
| 3846 | case OMPC_schedule: |
| 3847 | case OMPC_ordered: |
| 3848 | case OMPC_nowait: |
| 3849 | case OMPC_untied: |
| 3850 | case OMPC_threadprivate: |
Alexey Bataev | 1c2cfbc | 2015-06-23 14:25:19 +0000 | [diff] [blame] | 3851 | case OMPC_depend: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3852 | case OMPC_mergeable: |
Michael Wong | e710d54 | 2015-08-07 16:16:36 +0000 | [diff] [blame] | 3853 | case OMPC_device: |
Alexey Bataev | 346265e | 2015-09-25 10:37:12 +0000 | [diff] [blame] | 3854 | case OMPC_threads: |
Alexey Bataev | d14d1e6 | 2015-09-28 06:39:35 +0000 | [diff] [blame] | 3855 | case OMPC_simd: |
Kelvin Li | 0bff7af | 2015-11-23 05:32:03 +0000 | [diff] [blame] | 3856 | case OMPC_map: |
Kelvin Li | 099bb8c | 2015-11-24 20:50:12 +0000 | [diff] [blame] | 3857 | case OMPC_num_teams: |
Kelvin Li | a15fb1a | 2015-11-27 18:47:36 +0000 | [diff] [blame] | 3858 | case OMPC_thread_limit: |
Alexey Bataev | a056935 | 2015-12-01 10:17:31 +0000 | [diff] [blame] | 3859 | case OMPC_priority: |
Alexey Bataev | 1fd4aed | 2015-12-07 12:52:51 +0000 | [diff] [blame] | 3860 | case OMPC_grainsize: |
Alexey Bataev | b825de1 | 2015-12-07 10:51:44 +0000 | [diff] [blame] | 3861 | case OMPC_nogroup: |
Alexey Bataev | 382967a | 2015-12-08 12:06:20 +0000 | [diff] [blame] | 3862 | case OMPC_num_tasks: |
Alexey Bataev | 28c7541 | 2015-12-15 08:19:24 +0000 | [diff] [blame] | 3863 | case OMPC_hint: |
Carlo Bertolli | b4adf55 | 2016-01-15 18:50:31 +0000 | [diff] [blame] | 3864 | case OMPC_dist_schedule: |
Arpith Chacko Jacob | 3cf8904 | 2016-01-26 16:37:23 +0000 | [diff] [blame] | 3865 | case OMPC_defaultmap: |
Alexey Bataev | e48a5fc | 2016-04-12 05:28:34 +0000 | [diff] [blame] | 3866 | case OMPC_uniform: |
Samuel Antao | 661c090 | 2016-05-26 17:39:58 +0000 | [diff] [blame] | 3867 | case OMPC_to: |
Samuel Antao | ec172c6 | 2016-05-26 17:49:04 +0000 | [diff] [blame] | 3868 | case OMPC_from: |
Carlo Bertolli | 2404b17 | 2016-07-13 15:37:16 +0000 | [diff] [blame] | 3869 | case OMPC_use_device_ptr: |
Carlo Bertolli | 70594e9 | 2016-07-13 17:16:49 +0000 | [diff] [blame] | 3870 | case OMPC_is_device_ptr: |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3871 | llvm_unreachable("Clause is not allowed in 'omp atomic'."); |
| 3872 | } |
| 3873 | } |
| 3874 | |
| 3875 | void CodeGenFunction::EmitOMPAtomicDirective(const OMPAtomicDirective &S) { |
Benjamin Kramer | fc600dc | 2015-08-30 15:12:28 +0000 | [diff] [blame] | 3876 | bool IsSeqCst = S.getSingleClause<OMPSeqCstClause>(); |
Alexey Bataev | b57056f | 2015-01-22 06:17:56 +0000 | [diff] [blame] | 3877 | OpenMPClauseKind Kind = OMPC_unknown; |
| 3878 | for (auto *C : S.clauses()) { |
| 3879 | // Find first clause (skip seq_cst clause, if it is first). |
| 3880 | if (C->getClauseKind() != OMPC_seq_cst) { |
| 3881 | Kind = C->getClauseKind(); |
| 3882 | break; |
| 3883 | } |
| 3884 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3885 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3886 | const auto *CS = S.getInnermostCapturedStmt()->IgnoreContainers(); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3887 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(CS)) { |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3888 | enterFullExpression(EWC); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3889 | } |
| 3890 | // Processing for statements under 'atomic capture'. |
| 3891 | if (const auto *Compound = dyn_cast<CompoundStmt>(CS)) { |
| 3892 | for (const auto *C : Compound->body()) { |
| 3893 | if (const auto *EWC = dyn_cast<ExprWithCleanups>(C)) { |
| 3894 | enterFullExpression(EWC); |
| 3895 | } |
| 3896 | } |
| 3897 | } |
Alexey Bataev | 10fec57 | 2015-03-11 04:48:56 +0000 | [diff] [blame] | 3898 | |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 3899 | auto &&CodeGen = [&S, Kind, IsSeqCst, CS](CodeGenFunction &CGF, |
| 3900 | PrePostActionTy &) { |
Alexey Bataev | 33c5640 | 2015-12-14 09:26:19 +0000 | [diff] [blame] | 3901 | CGF.EmitStopPoint(CS); |
Alexey Bataev | 5e018f9 | 2015-04-23 06:35:10 +0000 | [diff] [blame] | 3902 | EmitOMPAtomicExpr(CGF, Kind, IsSeqCst, S.isPostfixUpdate(), S.getX(), |
| 3903 | S.getV(), S.getExpr(), S.getUpdateExpr(), |
| 3904 | S.isXLHSInRHSPart(), S.getLocStart()); |
Alexey Bataev | 6f1ffc0 | 2015-04-10 04:50:10 +0000 | [diff] [blame] | 3905 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3906 | OMPLexicalScope Scope(*this, S, OMPD_unknown); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 3907 | CGM.getOpenMPRuntime().emitInlinedDirective(*this, OMPD_atomic, CodeGen); |
Alexey Bataev | 0162e45 | 2014-07-22 10:10:35 +0000 | [diff] [blame] | 3908 | } |
| 3909 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3910 | static void emitCommonOMPTargetDirective(CodeGenFunction &CGF, |
| 3911 | const OMPExecutableDirective &S, |
| 3912 | const RegionCodeGenTy &CodeGen) { |
| 3913 | assert(isOpenMPTargetExecutionDirective(S.getDirectiveKind())); |
| 3914 | CodeGenModule &CGM = CGF.CGM; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3915 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3916 | llvm::Function *Fn = nullptr; |
| 3917 | llvm::Constant *FnID = nullptr; |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3918 | |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3919 | const Expr *IfCond = nullptr; |
Arpith Chacko Jacob | fe4890a | 2017-01-18 20:40:48 +0000 | [diff] [blame] | 3920 | // Check for the at most one if clause associated with the target region. |
| 3921 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 3922 | if (C->getNameModifier() == OMPD_unknown || |
| 3923 | C->getNameModifier() == OMPD_target) { |
| 3924 | IfCond = C->getCondition(); |
| 3925 | break; |
| 3926 | } |
Samuel Antao | bed3c46 | 2015-10-02 16:14:20 +0000 | [diff] [blame] | 3927 | } |
| 3928 | |
| 3929 | // Check if we have any device clause associated with the directive. |
| 3930 | const Expr *Device = nullptr; |
| 3931 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) { |
| 3932 | Device = C->getDevice(); |
| 3933 | } |
| 3934 | |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3935 | // Check if we have an if clause whose conditional always evaluates to false |
| 3936 | // or if we do not have any targets specified. If so the target region is not |
| 3937 | // an offload entry point. |
| 3938 | bool IsOffloadEntry = true; |
| 3939 | if (IfCond) { |
| 3940 | bool Val; |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3941 | if (CGF.ConstantFoldsToSimpleInteger(IfCond, Val) && !Val) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3942 | IsOffloadEntry = false; |
| 3943 | } |
| 3944 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 3945 | IsOffloadEntry = false; |
| 3946 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3947 | assert(CGF.CurFuncDecl && "No parent declaration for target region!"); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3948 | StringRef ParentName; |
| 3949 | // In case we have Ctors/Dtors we use the complete type variant to produce |
| 3950 | // the mangling of the device outlined kernel. |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3951 | if (auto *D = dyn_cast<CXXConstructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3952 | ParentName = CGM.getMangledName(GlobalDecl(D, Ctor_Complete)); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3953 | else if (auto *D = dyn_cast<CXXDestructorDecl>(CGF.CurFuncDecl)) |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3954 | ParentName = CGM.getMangledName(GlobalDecl(D, Dtor_Complete)); |
| 3955 | else |
| 3956 | ParentName = |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3957 | CGM.getMangledName(GlobalDecl(cast<FunctionDecl>(CGF.CurFuncDecl))); |
Samuel Antao | ee8fb30 | 2016-01-06 13:42:12 +0000 | [diff] [blame] | 3958 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3959 | // Emit target region as a standalone region. |
| 3960 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction(S, ParentName, Fn, FnID, |
| 3961 | IsOffloadEntry, CodeGen); |
Alexey Bataev | 8451efa | 2018-01-15 19:06:12 +0000 | [diff] [blame^] | 3962 | OMPLexicalScope Scope(CGF, S, OMPD_task); |
| 3963 | CGM.getOpenMPRuntime().emitTargetCall(CGF, S, Fn, FnID, IfCond, Device); |
Alexey Bataev | 0bd520b | 2014-09-19 08:19:49 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3966 | static void emitTargetRegion(CodeGenFunction &CGF, const OMPTargetDirective &S, |
| 3967 | PrePostActionTy &Action) { |
| 3968 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 3969 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 3970 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 3971 | (void)PrivateScope.Privatize(); |
| 3972 | |
| 3973 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 3974 | CGF.EmitStmt(S.getCapturedStmt(OMPD_target)->getCapturedStmt()); |
Arpith Chacko Jacob | 43a8b7b | 2017-01-16 15:26:02 +0000 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | void CodeGenFunction::EmitOMPTargetDeviceFunction(CodeGenModule &CGM, |
| 3978 | StringRef ParentName, |
| 3979 | const OMPTargetDirective &S) { |
| 3980 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3981 | emitTargetRegion(CGF, S, Action); |
| 3982 | }; |
| 3983 | llvm::Function *Fn; |
| 3984 | llvm::Constant *Addr; |
| 3985 | // Emit target region as a standalone region. |
| 3986 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 3987 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 3988 | assert(Fn && Addr && "Target device function emission failed."); |
| 3989 | } |
| 3990 | |
| 3991 | void CodeGenFunction::EmitOMPTargetDirective(const OMPTargetDirective &S) { |
| 3992 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 3993 | emitTargetRegion(CGF, S, Action); |
| 3994 | }; |
| 3995 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 3996 | } |
| 3997 | |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 3998 | static void emitCommonOMPTeamsDirective(CodeGenFunction &CGF, |
| 3999 | const OMPExecutableDirective &S, |
| 4000 | OpenMPDirectiveKind InnermostKind, |
| 4001 | const RegionCodeGenTy &CodeGen) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4002 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_teams); |
| 4003 | auto OutlinedFn = CGF.CGM.getOpenMPRuntime().emitTeamsOutlinedFunction( |
| 4004 | S, *CS->getCapturedDecl()->param_begin(), InnermostKind, CodeGen); |
Samuel Antao | b68e2db | 2016-03-03 16:20:23 +0000 | [diff] [blame] | 4005 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4006 | const OMPNumTeamsClause *NT = S.getSingleClause<OMPNumTeamsClause>(); |
| 4007 | const OMPThreadLimitClause *TL = S.getSingleClause<OMPThreadLimitClause>(); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4008 | if (NT || TL) { |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4009 | Expr *NumTeams = (NT) ? NT->getNumTeams() : nullptr; |
| 4010 | Expr *ThreadLimit = (TL) ? TL->getThreadLimit() : nullptr; |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4011 | |
Carlo Bertolli | c687225 | 2016-04-04 15:55:02 +0000 | [diff] [blame] | 4012 | CGF.CGM.getOpenMPRuntime().emitNumTeamsClause(CGF, NumTeams, ThreadLimit, |
| 4013 | S.getLocStart()); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4014 | } |
| 4015 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4016 | OMPTeamsScope Scope(CGF, S); |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4017 | llvm::SmallVector<llvm::Value *, 16> CapturedVars; |
| 4018 | CGF.GenerateOpenMPCapturedVars(*CS, CapturedVars); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4019 | CGF.CGM.getOpenMPRuntime().emitTeamsCall(CGF, S, S.getLocStart(), OutlinedFn, |
| 4020 | CapturedVars); |
| 4021 | } |
| 4022 | |
| 4023 | void CodeGenFunction::EmitOMPTeamsDirective(const OMPTeamsDirective &S) { |
Kelvin Li | 51336dd | 2016-12-15 17:55:32 +0000 | [diff] [blame] | 4024 | // Emit teams region as a standalone region. |
Alexey Bataev | 14fa1c6 | 2016-03-29 05:34:15 +0000 | [diff] [blame] | 4025 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4026 | OMPPrivateScope PrivateScope(CGF); |
Carlo Bertolli | 6ad7b5a | 2016-03-03 22:09:40 +0000 | [diff] [blame] | 4027 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4028 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4029 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4030 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4031 | CGF.EmitStmt(S.getCapturedStmt(OMPD_teams)->getCapturedStmt()); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4032 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Carlo Bertolli | 430d8ec | 2016-03-03 20:34:23 +0000 | [diff] [blame] | 4033 | }; |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4034 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Arpith Chacko Jacob | fc711b1 | 2017-02-16 16:48:49 +0000 | [diff] [blame] | 4035 | emitPostUpdateForReductionClause( |
| 4036 | *this, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Alexey Bataev | 13314bf | 2014-10-09 04:18:56 +0000 | [diff] [blame] | 4037 | } |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4038 | |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4039 | static void emitTargetTeamsRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4040 | const OMPTargetTeamsDirective &S) { |
| 4041 | auto *CS = S.getCapturedStmt(OMPD_teams); |
| 4042 | Action.Enter(CGF); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4043 | // Emit teams region as a standalone region. |
| 4044 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4045 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4046 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4047 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4048 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4049 | (void)PrivateScope.Privatize(); |
| 4050 | Action.Enter(CGF); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4051 | CGF.EmitStmt(CS->getCapturedStmt()); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4052 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4053 | }; |
| 4054 | emitCommonOMPTeamsDirective(CGF, S, OMPD_teams, CodeGen); |
Alexey Bataev | f9fc42e | 2017-11-22 14:25:55 +0000 | [diff] [blame] | 4055 | emitPostUpdateForReductionClause( |
| 4056 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Arpith Chacko Jacob | 99a1e0e | 2017-01-25 02:18:43 +0000 | [diff] [blame] | 4057 | } |
| 4058 | |
| 4059 | void CodeGenFunction::EmitOMPTargetTeamsDeviceFunction( |
| 4060 | CodeGenModule &CGM, StringRef ParentName, |
| 4061 | const OMPTargetTeamsDirective &S) { |
| 4062 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4063 | emitTargetTeamsRegion(CGF, Action, S); |
| 4064 | }; |
| 4065 | llvm::Function *Fn; |
| 4066 | llvm::Constant *Addr; |
| 4067 | // Emit target region as a standalone region. |
| 4068 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4069 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4070 | assert(Fn && Addr && "Target device function emission failed."); |
| 4071 | } |
| 4072 | |
| 4073 | void CodeGenFunction::EmitOMPTargetTeamsDirective( |
| 4074 | const OMPTargetTeamsDirective &S) { |
| 4075 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4076 | emitTargetTeamsRegion(CGF, Action, S); |
| 4077 | }; |
| 4078 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4079 | } |
| 4080 | |
Alexey Bataev | dfa430f | 2017-12-08 15:03:50 +0000 | [diff] [blame] | 4081 | static void |
| 4082 | emitTargetTeamsDistributeRegion(CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4083 | const OMPTargetTeamsDistributeDirective &S) { |
| 4084 | Action.Enter(CGF); |
| 4085 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4086 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4087 | }; |
| 4088 | |
| 4089 | // Emit teams region as a standalone region. |
| 4090 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
| 4091 | PrePostActionTy &) { |
| 4092 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4093 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4094 | (void)PrivateScope.Privatize(); |
| 4095 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4096 | CodeGenDistribute); |
| 4097 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4098 | }; |
| 4099 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute, CodeGen); |
| 4100 | emitPostUpdateForReductionClause(CGF, S, |
| 4101 | [](CodeGenFunction &) { return nullptr; }); |
| 4102 | } |
| 4103 | |
| 4104 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDeviceFunction( |
| 4105 | CodeGenModule &CGM, StringRef ParentName, |
| 4106 | const OMPTargetTeamsDistributeDirective &S) { |
| 4107 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4108 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4109 | }; |
| 4110 | llvm::Function *Fn; |
| 4111 | llvm::Constant *Addr; |
| 4112 | // Emit target region as a standalone region. |
| 4113 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4114 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4115 | assert(Fn && Addr && "Target device function emission failed."); |
| 4116 | } |
| 4117 | |
| 4118 | void CodeGenFunction::EmitOMPTargetTeamsDistributeDirective( |
| 4119 | const OMPTargetTeamsDistributeDirective &S) { |
| 4120 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4121 | emitTargetTeamsDistributeRegion(CGF, Action, S); |
| 4122 | }; |
| 4123 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4124 | } |
| 4125 | |
Alexey Bataev | fbe17fb | 2017-12-13 19:45:06 +0000 | [diff] [blame] | 4126 | static void emitTargetTeamsDistributeSimdRegion( |
| 4127 | CodeGenFunction &CGF, PrePostActionTy &Action, |
| 4128 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4129 | Action.Enter(CGF); |
| 4130 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4131 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4132 | }; |
| 4133 | |
| 4134 | // Emit teams region as a standalone region. |
| 4135 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
| 4136 | PrePostActionTy &) { |
| 4137 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4138 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4139 | (void)PrivateScope.Privatize(); |
| 4140 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4141 | CodeGenDistribute); |
| 4142 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4143 | }; |
| 4144 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_simd, CodeGen); |
| 4145 | emitPostUpdateForReductionClause(CGF, S, |
| 4146 | [](CodeGenFunction &) { return nullptr; }); |
| 4147 | } |
| 4148 | |
| 4149 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDeviceFunction( |
| 4150 | CodeGenModule &CGM, StringRef ParentName, |
| 4151 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4152 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4153 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4154 | }; |
| 4155 | llvm::Function *Fn; |
| 4156 | llvm::Constant *Addr; |
| 4157 | // Emit target region as a standalone region. |
| 4158 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4159 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4160 | assert(Fn && Addr && "Target device function emission failed."); |
| 4161 | } |
| 4162 | |
| 4163 | void CodeGenFunction::EmitOMPTargetTeamsDistributeSimdDirective( |
| 4164 | const OMPTargetTeamsDistributeSimdDirective &S) { |
| 4165 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4166 | emitTargetTeamsDistributeSimdRegion(CGF, Action, S); |
| 4167 | }; |
| 4168 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4169 | } |
| 4170 | |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4171 | void CodeGenFunction::EmitOMPTeamsDistributeDirective( |
| 4172 | const OMPTeamsDistributeDirective &S) { |
| 4173 | |
| 4174 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4175 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4176 | }; |
| 4177 | |
| 4178 | // Emit teams region as a standalone region. |
| 4179 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
| 4180 | PrePostActionTy &) { |
| 4181 | OMPPrivateScope PrivateScope(CGF); |
| 4182 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4183 | (void)PrivateScope.Privatize(); |
| 4184 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4185 | CodeGenDistribute); |
| 4186 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4187 | }; |
Alexey Bataev | 95c6dd4 | 2017-11-29 15:14:16 +0000 | [diff] [blame] | 4188 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute, CodeGen); |
Carlo Bertolli | ba1487b | 2017-10-04 14:12:09 +0000 | [diff] [blame] | 4189 | emitPostUpdateForReductionClause(*this, S, |
| 4190 | [](CodeGenFunction &) { return nullptr; }); |
| 4191 | } |
| 4192 | |
Alexey Bataev | 999277a | 2017-12-06 14:31:09 +0000 | [diff] [blame] | 4193 | void CodeGenFunction::EmitOMPTeamsDistributeSimdDirective( |
| 4194 | const OMPTeamsDistributeSimdDirective &S) { |
| 4195 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4196 | CGF.EmitOMPDistributeLoop(S, emitOMPLoopBodyWithStopPoint, S.getInc()); |
| 4197 | }; |
| 4198 | |
| 4199 | // Emit teams region as a standalone region. |
| 4200 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
| 4201 | PrePostActionTy &) { |
| 4202 | OMPPrivateScope PrivateScope(CGF); |
| 4203 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4204 | (void)PrivateScope.Privatize(); |
| 4205 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_simd, |
| 4206 | CodeGenDistribute); |
| 4207 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4208 | }; |
| 4209 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_simd, CodeGen); |
| 4210 | emitPostUpdateForReductionClause(*this, S, |
| 4211 | [](CodeGenFunction &) { return nullptr; }); |
| 4212 | } |
| 4213 | |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4214 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForDirective( |
| 4215 | const OMPTeamsDistributeParallelForDirective &S) { |
| 4216 | auto &&CodeGenDistribute = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4217 | CGF.EmitOMPDistributeLoop(S, emitInnerParallelForWhenCombined, |
| 4218 | S.getDistInc()); |
| 4219 | }; |
| 4220 | |
| 4221 | // Emit teams region as a standalone region. |
| 4222 | auto &&CodeGen = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
| 4223 | PrePostActionTy &) { |
| 4224 | OMPPrivateScope PrivateScope(CGF); |
| 4225 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4226 | (void)PrivateScope.Privatize(); |
Alexey Bataev | 10a5431 | 2017-11-27 16:54:08 +0000 | [diff] [blame] | 4227 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_distribute, |
| 4228 | CodeGenDistribute); |
Carlo Bertolli | 62fae15 | 2017-11-20 20:46:39 +0000 | [diff] [blame] | 4229 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4230 | }; |
| 4231 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4232 | emitPostUpdateForReductionClause(*this, S, |
| 4233 | [](CodeGenFunction &) { return nullptr; }); |
| 4234 | } |
| 4235 | |
Carlo Bertolli | 56a2aa4 | 2017-12-04 20:57:19 +0000 | [diff] [blame] | 4236 | void CodeGenFunction::EmitOMPTeamsDistributeParallelForSimdDirective( |
| 4237 | const OMPTeamsDistributeParallelForSimdDirective &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, |
| 4245 | PrePostActionTy &) { |
| 4246 | OMPPrivateScope PrivateScope(CGF); |
| 4247 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4248 | (void)PrivateScope.Privatize(); |
| 4249 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4250 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4251 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4252 | }; |
| 4253 | emitCommonOMPTeamsDirective(*this, S, OMPD_distribute_parallel_for, CodeGen); |
| 4254 | emitPostUpdateForReductionClause(*this, S, |
| 4255 | [](CodeGenFunction &) { return nullptr; }); |
| 4256 | } |
| 4257 | |
Carlo Bertolli | 52978c3 | 2018-01-03 21:12:44 +0000 | [diff] [blame] | 4258 | static void emitTargetTeamsDistributeParallelForRegion( |
| 4259 | CodeGenFunction &CGF, const OMPTargetTeamsDistributeParallelForDirective &S, |
| 4260 | PrePostActionTy &Action) { |
| 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 &&CodeGenTeams = [&S, &CodeGenDistribute](CodeGenFunction &CGF, |
| 4268 | PrePostActionTy &) { |
| 4269 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4270 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4271 | (void)PrivateScope.Privatize(); |
| 4272 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4273 | CGF, OMPD_distribute, CodeGenDistribute, /*HasCancel=*/false); |
| 4274 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_teams); |
| 4275 | }; |
| 4276 | |
| 4277 | emitCommonOMPTeamsDirective(CGF, S, OMPD_distribute_parallel_for, |
| 4278 | CodeGenTeams); |
| 4279 | emitPostUpdateForReductionClause(CGF, S, |
| 4280 | [](CodeGenFunction &) { return nullptr; }); |
| 4281 | } |
| 4282 | |
| 4283 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDeviceFunction( |
| 4284 | CodeGenModule &CGM, StringRef ParentName, |
| 4285 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4286 | // Emit SPMD target teams distribute parallel for region as a standalone |
| 4287 | // region. |
| 4288 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4289 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4290 | }; |
| 4291 | llvm::Function *Fn; |
| 4292 | llvm::Constant *Addr; |
| 4293 | // Emit target region as a standalone region. |
| 4294 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4295 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4296 | assert(Fn && Addr && "Target device function emission failed."); |
| 4297 | } |
| 4298 | |
| 4299 | void CodeGenFunction::EmitOMPTargetTeamsDistributeParallelForDirective( |
| 4300 | const OMPTargetTeamsDistributeParallelForDirective &S) { |
| 4301 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4302 | emitTargetTeamsDistributeParallelForRegion(CGF, S, Action); |
| 4303 | }; |
| 4304 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4305 | } |
| 4306 | |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4307 | void CodeGenFunction::EmitOMPCancellationPointDirective( |
| 4308 | const OMPCancellationPointDirective &S) { |
Alexey Bataev | 0f34da1 | 2015-07-02 04:17:07 +0000 | [diff] [blame] | 4309 | CGM.getOpenMPRuntime().emitCancellationPointCall(*this, S.getLocStart(), |
| 4310 | S.getCancelRegion()); |
Alexey Bataev | 6d4ed05 | 2015-07-01 06:57:41 +0000 | [diff] [blame] | 4311 | } |
| 4312 | |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4313 | void CodeGenFunction::EmitOMPCancelDirective(const OMPCancelDirective &S) { |
Alexey Bataev | 87933c7 | 2015-09-18 08:07:34 +0000 | [diff] [blame] | 4314 | const Expr *IfCond = nullptr; |
| 4315 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4316 | if (C->getNameModifier() == OMPD_unknown || |
| 4317 | C->getNameModifier() == OMPD_cancel) { |
| 4318 | IfCond = C->getCondition(); |
| 4319 | break; |
| 4320 | } |
| 4321 | } |
| 4322 | CGM.getOpenMPRuntime().emitCancelCall(*this, S.getLocStart(), IfCond, |
Alexey Bataev | 7d5d33e | 2015-07-06 05:50:32 +0000 | [diff] [blame] | 4323 | S.getCancelRegion()); |
Alexey Bataev | 8090987 | 2015-07-02 11:25:17 +0000 | [diff] [blame] | 4324 | } |
| 4325 | |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4326 | CodeGenFunction::JumpDest |
| 4327 | CodeGenFunction::getOMPCancelDestination(OpenMPDirectiveKind Kind) { |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4328 | if (Kind == OMPD_parallel || Kind == OMPD_task || |
| 4329 | Kind == OMPD_target_parallel) |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4330 | return ReturnBlock; |
Alexey Bataev | 25e5b44 | 2015-09-15 12:52:43 +0000 | [diff] [blame] | 4331 | assert(Kind == OMPD_for || Kind == OMPD_section || Kind == OMPD_sections || |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4332 | Kind == OMPD_parallel_sections || Kind == OMPD_parallel_for || |
| 4333 | Kind == OMPD_distribute_parallel_for || |
Alexey Bataev | dcb4b8fb | 2017-11-22 20:19:50 +0000 | [diff] [blame] | 4334 | Kind == OMPD_target_parallel_for || |
Alexey Bataev | 16e7988 | 2017-11-22 21:12:03 +0000 | [diff] [blame] | 4335 | Kind == OMPD_teams_distribute_parallel_for || |
| 4336 | Kind == OMPD_target_teams_distribute_parallel_for); |
Alexey Bataev | 957d856 | 2016-11-17 15:12:05 +0000 | [diff] [blame] | 4337 | return OMPCancelStack.getExitBlock(); |
Alexey Bataev | 81c7ea0 | 2015-07-03 09:56:58 +0000 | [diff] [blame] | 4338 | } |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4339 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4340 | void CodeGenFunction::EmitOMPUseDevicePtrClause( |
| 4341 | const OMPClause &NC, OMPPrivateScope &PrivateScope, |
| 4342 | const llvm::DenseMap<const ValueDecl *, Address> &CaptureDeviceAddrMap) { |
| 4343 | const auto &C = cast<OMPUseDevicePtrClause>(NC); |
| 4344 | auto OrigVarIt = C.varlist_begin(); |
| 4345 | auto InitIt = C.inits().begin(); |
| 4346 | for (auto PvtVarIt : C.private_copies()) { |
| 4347 | auto *OrigVD = cast<VarDecl>(cast<DeclRefExpr>(*OrigVarIt)->getDecl()); |
| 4348 | auto *InitVD = cast<VarDecl>(cast<DeclRefExpr>(*InitIt)->getDecl()); |
| 4349 | auto *PvtVD = cast<VarDecl>(cast<DeclRefExpr>(PvtVarIt)->getDecl()); |
| 4350 | |
| 4351 | // In order to identify the right initializer we need to match the |
| 4352 | // declaration used by the mapping logic. In some cases we may get |
| 4353 | // OMPCapturedExprDecl that refers to the original declaration. |
| 4354 | const ValueDecl *MatchingVD = OrigVD; |
| 4355 | if (auto *OED = dyn_cast<OMPCapturedExprDecl>(MatchingVD)) { |
| 4356 | // OMPCapturedExprDecl are used to privative fields of the current |
| 4357 | // structure. |
| 4358 | auto *ME = cast<MemberExpr>(OED->getInit()); |
| 4359 | assert(isa<CXXThisExpr>(ME->getBase()) && |
| 4360 | "Base should be the current struct!"); |
| 4361 | MatchingVD = ME->getMemberDecl(); |
| 4362 | } |
| 4363 | |
| 4364 | // If we don't have information about the current list item, move on to |
| 4365 | // the next one. |
| 4366 | auto InitAddrIt = CaptureDeviceAddrMap.find(MatchingVD); |
| 4367 | if (InitAddrIt == CaptureDeviceAddrMap.end()) |
| 4368 | continue; |
| 4369 | |
| 4370 | bool IsRegistered = PrivateScope.addPrivate(OrigVD, [&]() -> Address { |
| 4371 | // Initialize the temporary initialization variable with the address we |
| 4372 | // get from the runtime library. We have to cast the source address |
| 4373 | // because it is always a void *. References are materialized in the |
| 4374 | // privatization scope, so the initialization here disregards the fact |
| 4375 | // the original variable is a reference. |
| 4376 | QualType AddrQTy = |
| 4377 | getContext().getPointerType(OrigVD->getType().getNonReferenceType()); |
| 4378 | llvm::Type *AddrTy = ConvertTypeForMem(AddrQTy); |
| 4379 | Address InitAddr = Builder.CreateBitCast(InitAddrIt->second, AddrTy); |
| 4380 | setAddrOfLocalVar(InitVD, InitAddr); |
| 4381 | |
| 4382 | // Emit private declaration, it will be initialized by the value we |
| 4383 | // declaration we just added to the local declarations map. |
| 4384 | EmitDecl(*PvtVD); |
| 4385 | |
| 4386 | // The initialization variables reached its purpose in the emission |
| 4387 | // ofthe previous declaration, so we don't need it anymore. |
| 4388 | LocalDeclMap.erase(InitVD); |
| 4389 | |
| 4390 | // Return the address of the private variable. |
| 4391 | return GetAddrOfLocalVar(PvtVD); |
| 4392 | }); |
| 4393 | assert(IsRegistered && "firstprivate var already registered as private"); |
| 4394 | // Silence the warning about unused variable. |
| 4395 | (void)IsRegistered; |
| 4396 | |
| 4397 | ++OrigVarIt; |
| 4398 | ++InitIt; |
| 4399 | } |
| 4400 | } |
| 4401 | |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4402 | // Generate the instructions for '#pragma omp target data' directive. |
| 4403 | void CodeGenFunction::EmitOMPTargetDataDirective( |
| 4404 | const OMPTargetDataDirective &S) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4405 | CGOpenMPRuntime::TargetDataInfo Info(/*RequiresDevicePointerInfo=*/true); |
| 4406 | |
| 4407 | // Create a pre/post action to signal the privatization of the device pointer. |
| 4408 | // This action can be replaced by the OpenMP runtime code generation to |
| 4409 | // deactivate privatization. |
| 4410 | bool PrivatizeDevicePointers = false; |
| 4411 | class DevicePointerPrivActionTy : public PrePostActionTy { |
| 4412 | bool &PrivatizeDevicePointers; |
| 4413 | |
| 4414 | public: |
| 4415 | explicit DevicePointerPrivActionTy(bool &PrivatizeDevicePointers) |
| 4416 | : PrePostActionTy(), PrivatizeDevicePointers(PrivatizeDevicePointers) {} |
| 4417 | void Enter(CodeGenFunction &CGF) override { |
| 4418 | PrivatizeDevicePointers = true; |
| 4419 | } |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4420 | }; |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4421 | DevicePointerPrivActionTy PrivAction(PrivatizeDevicePointers); |
| 4422 | |
| 4423 | auto &&CodeGen = [&S, &Info, &PrivatizeDevicePointers]( |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4424 | CodeGenFunction &CGF, PrePostActionTy &Action) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4425 | auto &&InnermostCodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4426 | CGF.EmitStmt(S.getInnermostCapturedStmt()->getCapturedStmt()); |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4427 | }; |
| 4428 | |
| 4429 | // Codegen that selects wheather to generate the privatization code or not. |
| 4430 | auto &&PrivCodeGen = [&S, &Info, &PrivatizeDevicePointers, |
| 4431 | &InnermostCodeGen](CodeGenFunction &CGF, |
| 4432 | PrePostActionTy &Action) { |
| 4433 | RegionCodeGenTy RCG(InnermostCodeGen); |
| 4434 | PrivatizeDevicePointers = false; |
| 4435 | |
| 4436 | // Call the pre-action to change the status of PrivatizeDevicePointers if |
| 4437 | // needed. |
| 4438 | Action.Enter(CGF); |
| 4439 | |
| 4440 | if (PrivatizeDevicePointers) { |
| 4441 | OMPPrivateScope PrivateScope(CGF); |
| 4442 | // Emit all instances of the use_device_ptr clause. |
| 4443 | for (const auto *C : S.getClausesOfKind<OMPUseDevicePtrClause>()) |
| 4444 | CGF.EmitOMPUseDevicePtrClause(*C, PrivateScope, |
| 4445 | Info.CaptureDeviceAddrMap); |
| 4446 | (void)PrivateScope.Privatize(); |
| 4447 | RCG(CGF); |
| 4448 | } else |
| 4449 | RCG(CGF); |
| 4450 | }; |
| 4451 | |
| 4452 | // Forward the provided action to the privatization codegen. |
| 4453 | RegionCodeGenTy PrivRCG(PrivCodeGen); |
| 4454 | PrivRCG.setAction(Action); |
| 4455 | |
| 4456 | // Notwithstanding the body of the region is emitted as inlined directive, |
| 4457 | // we don't use an inline scope as changes in the references inside the |
| 4458 | // region are expected to be visible outside, so we do not privative them. |
| 4459 | OMPLexicalScope Scope(CGF, S); |
| 4460 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_target_data, |
| 4461 | PrivRCG); |
| 4462 | }; |
| 4463 | |
| 4464 | RegionCodeGenTy RCG(CodeGen); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4465 | |
| 4466 | // If we don't have target devices, don't bother emitting the data mapping |
| 4467 | // code. |
| 4468 | if (CGM.getLangOpts().OMPTargetTriples.empty()) { |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4469 | RCG(*this); |
Samuel Antao | df158d5 | 2016-04-27 22:58:19 +0000 | [diff] [blame] | 4470 | return; |
| 4471 | } |
| 4472 | |
| 4473 | // Check if we have any if clause associated with the directive. |
| 4474 | const Expr *IfCond = nullptr; |
| 4475 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4476 | IfCond = C->getCondition(); |
| 4477 | |
| 4478 | // Check if we have any device clause associated with the directive. |
| 4479 | const Expr *Device = nullptr; |
| 4480 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4481 | Device = C->getDevice(); |
| 4482 | |
Samuel Antao | cc10b85 | 2016-07-28 14:23:26 +0000 | [diff] [blame] | 4483 | // Set the action to signal privatization of device pointers. |
| 4484 | RCG.setAction(PrivAction); |
| 4485 | |
| 4486 | // Emit region code. |
| 4487 | CGM.getOpenMPRuntime().emitTargetDataCalls(*this, S, IfCond, Device, RCG, |
| 4488 | Info); |
Michael Wong | 65f367f | 2015-07-21 13:44:28 +0000 | [diff] [blame] | 4489 | } |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4490 | |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4491 | void CodeGenFunction::EmitOMPTargetEnterDataDirective( |
| 4492 | const OMPTargetEnterDataDirective &S) { |
Samuel Antao | bd0ae2e | 2016-04-27 23:07:29 +0000 | [diff] [blame] | 4493 | // If we don't have target devices, don't bother emitting the data mapping |
| 4494 | // code. |
| 4495 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4496 | return; |
| 4497 | |
| 4498 | // Check if we have any if clause associated with the directive. |
| 4499 | const Expr *IfCond = nullptr; |
| 4500 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4501 | IfCond = C->getCondition(); |
| 4502 | |
| 4503 | // Check if we have any device clause associated with the directive. |
| 4504 | const Expr *Device = nullptr; |
| 4505 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4506 | Device = C->getDevice(); |
| 4507 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4508 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4509 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | df67fc4 | 2016-01-19 19:15:56 +0000 | [diff] [blame] | 4510 | } |
| 4511 | |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4512 | void CodeGenFunction::EmitOMPTargetExitDataDirective( |
| 4513 | const OMPTargetExitDataDirective &S) { |
Samuel Antao | 8dd6628 | 2016-04-27 23:14:30 +0000 | [diff] [blame] | 4514 | // If we don't have target devices, don't bother emitting the data mapping |
| 4515 | // code. |
| 4516 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4517 | return; |
| 4518 | |
| 4519 | // Check if we have any if clause associated with the directive. |
| 4520 | const Expr *IfCond = nullptr; |
| 4521 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4522 | IfCond = C->getCondition(); |
| 4523 | |
| 4524 | // Check if we have any device clause associated with the directive. |
| 4525 | const Expr *Device = nullptr; |
| 4526 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4527 | Device = C->getDevice(); |
| 4528 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4529 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4530 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 7259076 | 2016-01-19 20:04:50 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4533 | static void emitTargetParallelRegion(CodeGenFunction &CGF, |
| 4534 | const OMPTargetParallelDirective &S, |
| 4535 | PrePostActionTy &Action) { |
| 4536 | // Get the captured statement associated with the 'parallel' region. |
| 4537 | auto *CS = S.getCapturedStmt(OMPD_parallel); |
| 4538 | Action.Enter(CGF); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4539 | auto &&CodeGen = [&S, CS](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4540 | CodeGenFunction::OMPPrivateScope PrivateScope(CGF); |
| 4541 | (void)CGF.EmitOMPFirstprivateClause(S, PrivateScope); |
| 4542 | CGF.EmitOMPPrivateClause(S, PrivateScope); |
| 4543 | CGF.EmitOMPReductionClauseInit(S, PrivateScope); |
| 4544 | (void)PrivateScope.Privatize(); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4545 | // TODO: Add support for clauses. |
| 4546 | CGF.EmitStmt(CS->getCapturedStmt()); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4547 | CGF.EmitOMPReductionClauseFinal(S, /*ReductionKind=*/OMPD_parallel); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4548 | }; |
Carlo Bertolli | b0ff0a6 | 2017-04-25 17:52:12 +0000 | [diff] [blame] | 4549 | emitCommonOMPParallelDirective(CGF, S, OMPD_parallel, CodeGen, |
| 4550 | emitEmptyBoundParameters); |
Arpith Chacko Jacob | 101e8fb | 2017-02-16 16:20:16 +0000 | [diff] [blame] | 4551 | emitPostUpdateForReductionClause( |
| 4552 | CGF, S, [](CodeGenFunction &) -> llvm::Value * { return nullptr; }); |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4553 | } |
| 4554 | |
| 4555 | void CodeGenFunction::EmitOMPTargetParallelDeviceFunction( |
| 4556 | CodeGenModule &CGM, StringRef ParentName, |
| 4557 | const OMPTargetParallelDirective &S) { |
| 4558 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4559 | emitTargetParallelRegion(CGF, S, Action); |
| 4560 | }; |
| 4561 | llvm::Function *Fn; |
| 4562 | llvm::Constant *Addr; |
| 4563 | // Emit target region as a standalone region. |
| 4564 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4565 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4566 | assert(Fn && Addr && "Target device function emission failed."); |
| 4567 | } |
| 4568 | |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4569 | void CodeGenFunction::EmitOMPTargetParallelDirective( |
| 4570 | const OMPTargetParallelDirective &S) { |
Arpith Chacko Jacob | 19b911c | 2017-01-18 18:18:53 +0000 | [diff] [blame] | 4571 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4572 | emitTargetParallelRegion(CGF, S, Action); |
| 4573 | }; |
| 4574 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | e955b3d | 2016-01-26 18:48:41 +0000 | [diff] [blame] | 4575 | } |
| 4576 | |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4577 | static void emitTargetParallelForRegion(CodeGenFunction &CGF, |
| 4578 | const OMPTargetParallelForDirective &S, |
| 4579 | PrePostActionTy &Action) { |
| 4580 | Action.Enter(CGF); |
| 4581 | // Emit directive as a combined directive that consists of two implicit |
| 4582 | // directives: 'parallel' with 'for' directive. |
| 4583 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
Alexey Bataev | 2139ed6 | 2017-11-16 18:20:21 +0000 | [diff] [blame] | 4584 | CodeGenFunction::OMPCancelStackRAII CancelRegion( |
| 4585 | CGF, OMPD_target_parallel_for, S.hasCancel()); |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4586 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4587 | emitDispatchForLoopBounds); |
| 4588 | }; |
| 4589 | emitCommonOMPParallelDirective(CGF, S, OMPD_for, CodeGen, |
| 4590 | emitEmptyBoundParameters); |
| 4591 | } |
| 4592 | |
| 4593 | void CodeGenFunction::EmitOMPTargetParallelForDeviceFunction( |
| 4594 | CodeGenModule &CGM, StringRef ParentName, |
| 4595 | const OMPTargetParallelForDirective &S) { |
| 4596 | // Emit SPMD target parallel for region as a standalone region. |
| 4597 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4598 | emitTargetParallelForRegion(CGF, S, Action); |
| 4599 | }; |
| 4600 | llvm::Function *Fn; |
| 4601 | llvm::Constant *Addr; |
| 4602 | // Emit target region as a standalone region. |
| 4603 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4604 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4605 | assert(Fn && Addr && "Target device function emission failed."); |
| 4606 | } |
| 4607 | |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4608 | void CodeGenFunction::EmitOMPTargetParallelForDirective( |
| 4609 | const OMPTargetParallelForDirective &S) { |
Alexey Bataev | fb0ebec | 2017-11-08 20:16:14 +0000 | [diff] [blame] | 4610 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4611 | emitTargetParallelForRegion(CGF, S, Action); |
| 4612 | }; |
| 4613 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
Arpith Chacko Jacob | 05bebb5 | 2016-02-03 15:46:42 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Alexey Bataev | 5d7edca | 2017-11-09 17:32:15 +0000 | [diff] [blame] | 4616 | static void |
| 4617 | emitTargetParallelForSimdRegion(CodeGenFunction &CGF, |
| 4618 | const OMPTargetParallelForSimdDirective &S, |
| 4619 | PrePostActionTy &Action) { |
| 4620 | Action.Enter(CGF); |
| 4621 | // Emit directive as a combined directive that consists of two implicit |
| 4622 | // directives: 'parallel' with 'for' directive. |
| 4623 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4624 | CGF.EmitOMPWorksharingLoop(S, S.getEnsureUpperBound(), emitForLoopBounds, |
| 4625 | emitDispatchForLoopBounds); |
| 4626 | }; |
| 4627 | emitCommonOMPParallelDirective(CGF, S, OMPD_simd, CodeGen, |
| 4628 | emitEmptyBoundParameters); |
| 4629 | } |
| 4630 | |
| 4631 | void CodeGenFunction::EmitOMPTargetParallelForSimdDeviceFunction( |
| 4632 | CodeGenModule &CGM, StringRef ParentName, |
| 4633 | const OMPTargetParallelForSimdDirective &S) { |
| 4634 | // Emit SPMD target parallel for region as a standalone region. |
| 4635 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4636 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4637 | }; |
| 4638 | llvm::Function *Fn; |
| 4639 | llvm::Constant *Addr; |
| 4640 | // Emit target region as a standalone region. |
| 4641 | CGM.getOpenMPRuntime().emitTargetOutlinedFunction( |
| 4642 | S, ParentName, Fn, Addr, /*IsOffloadEntry=*/true, CodeGen); |
| 4643 | assert(Fn && Addr && "Target device function emission failed."); |
| 4644 | } |
| 4645 | |
| 4646 | void CodeGenFunction::EmitOMPTargetParallelForSimdDirective( |
| 4647 | const OMPTargetParallelForSimdDirective &S) { |
| 4648 | auto &&CodeGen = [&S](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4649 | emitTargetParallelForSimdRegion(CGF, S, Action); |
| 4650 | }; |
| 4651 | emitCommonOMPTargetDirective(*this, S, CodeGen); |
| 4652 | } |
| 4653 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4654 | /// Emit a helper variable and return corresponding lvalue. |
| 4655 | static void mapParam(CodeGenFunction &CGF, const DeclRefExpr *Helper, |
| 4656 | const ImplicitParamDecl *PVD, |
| 4657 | CodeGenFunction::OMPPrivateScope &Privates) { |
| 4658 | auto *VDecl = cast<VarDecl>(Helper->getDecl()); |
| 4659 | Privates.addPrivate( |
| 4660 | VDecl, [&CGF, PVD]() -> Address { return CGF.GetAddrOfLocalVar(PVD); }); |
| 4661 | } |
| 4662 | |
| 4663 | void CodeGenFunction::EmitOMPTaskLoopBasedDirective(const OMPLoopDirective &S) { |
| 4664 | assert(isOpenMPTaskLoopDirective(S.getDirectiveKind())); |
| 4665 | // Emit outlined function for task construct. |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4666 | const CapturedStmt *CS = S.getCapturedStmt(OMPD_taskloop); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4667 | auto CapturedStruct = GenerateCapturedStmtArgument(*CS); |
| 4668 | auto SharedsTy = getContext().getRecordType(CS->getCapturedRecordDecl()); |
| 4669 | const Expr *IfCond = nullptr; |
| 4670 | for (const auto *C : S.getClausesOfKind<OMPIfClause>()) { |
| 4671 | if (C->getNameModifier() == OMPD_unknown || |
| 4672 | C->getNameModifier() == OMPD_taskloop) { |
| 4673 | IfCond = C->getCondition(); |
| 4674 | break; |
| 4675 | } |
| 4676 | } |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4677 | |
| 4678 | OMPTaskDataTy Data; |
| 4679 | // Check if taskloop must be emitted without taskgroup. |
| 4680 | Data.Nogroup = S.getSingleClause<OMPNogroupClause>(); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4681 | // TODO: Check if we should emit tied or untied task. |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4682 | Data.Tied = true; |
| 4683 | // Set scheduling for taskloop |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4684 | if (const auto* Clause = S.getSingleClause<OMPGrainsizeClause>()) { |
| 4685 | // grainsize clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4686 | Data.Schedule.setInt(/*IntVal=*/false); |
| 4687 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getGrainsize())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4688 | } else if (const auto* Clause = S.getSingleClause<OMPNumTasksClause>()) { |
| 4689 | // num_tasks clause |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4690 | Data.Schedule.setInt(/*IntVal=*/true); |
| 4691 | Data.Schedule.setPointer(EmitScalarExpr(Clause->getNumTasks())); |
Alexey Bataev | 2b19a6f | 2016-04-28 09:15:06 +0000 | [diff] [blame] | 4692 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4693 | |
| 4694 | auto &&BodyGen = [CS, &S](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4695 | // if (PreCond) { |
| 4696 | // for (IV in 0..LastIteration) BODY; |
| 4697 | // <Final counter/linear vars updates>; |
| 4698 | // } |
| 4699 | // |
| 4700 | |
| 4701 | // Emit: if (PreCond) - begin. |
| 4702 | // If the condition constant folds and can be elided, avoid emitting the |
| 4703 | // whole loop. |
| 4704 | bool CondConstant; |
| 4705 | llvm::BasicBlock *ContBlock = nullptr; |
| 4706 | OMPLoopScope PreInitScope(CGF, S); |
| 4707 | if (CGF.ConstantFoldsToSimpleInteger(S.getPreCond(), CondConstant)) { |
| 4708 | if (!CondConstant) |
| 4709 | return; |
| 4710 | } else { |
| 4711 | auto *ThenBlock = CGF.createBasicBlock("taskloop.if.then"); |
| 4712 | ContBlock = CGF.createBasicBlock("taskloop.if.end"); |
| 4713 | emitPreCond(CGF, S, S.getPreCond(), ThenBlock, ContBlock, |
| 4714 | CGF.getProfileCount(&S)); |
| 4715 | CGF.EmitBlock(ThenBlock); |
| 4716 | CGF.incrementProfileCounter(&S); |
| 4717 | } |
| 4718 | |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4719 | if (isOpenMPSimdDirective(S.getDirectiveKind())) |
| 4720 | CGF.EmitOMPSimdInit(S); |
| 4721 | |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4722 | OMPPrivateScope LoopScope(CGF); |
| 4723 | // Emit helper vars inits. |
| 4724 | enum { LowerBound = 5, UpperBound, Stride, LastIter }; |
| 4725 | auto *I = CS->getCapturedDecl()->param_begin(); |
| 4726 | auto *LBP = std::next(I, LowerBound); |
| 4727 | auto *UBP = std::next(I, UpperBound); |
| 4728 | auto *STP = std::next(I, Stride); |
| 4729 | auto *LIP = std::next(I, LastIter); |
| 4730 | mapParam(CGF, cast<DeclRefExpr>(S.getLowerBoundVariable()), *LBP, |
| 4731 | LoopScope); |
| 4732 | mapParam(CGF, cast<DeclRefExpr>(S.getUpperBoundVariable()), *UBP, |
| 4733 | LoopScope); |
| 4734 | mapParam(CGF, cast<DeclRefExpr>(S.getStrideVariable()), *STP, LoopScope); |
| 4735 | mapParam(CGF, cast<DeclRefExpr>(S.getIsLastIterVariable()), *LIP, |
| 4736 | LoopScope); |
| 4737 | CGF.EmitOMPPrivateLoopCounters(S, LoopScope); |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4738 | bool HasLastprivateClause = CGF.EmitOMPLastprivateClauseInit(S, LoopScope); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4739 | (void)LoopScope.Privatize(); |
| 4740 | // Emit the loop iteration variable. |
| 4741 | const Expr *IVExpr = S.getIterationVariable(); |
| 4742 | const VarDecl *IVDecl = cast<VarDecl>(cast<DeclRefExpr>(IVExpr)->getDecl()); |
| 4743 | CGF.EmitVarDecl(*IVDecl); |
| 4744 | CGF.EmitIgnoredExpr(S.getInit()); |
| 4745 | |
| 4746 | // Emit the iterations count variable. |
| 4747 | // If it is not a variable, Sema decided to calculate iterations count on |
| 4748 | // each iteration (e.g., it is foldable into a constant). |
| 4749 | if (auto LIExpr = dyn_cast<DeclRefExpr>(S.getLastIteration())) { |
| 4750 | CGF.EmitVarDecl(*cast<VarDecl>(LIExpr->getDecl())); |
| 4751 | // Emit calculation of the iterations count. |
| 4752 | CGF.EmitIgnoredExpr(S.getCalcLastIteration()); |
| 4753 | } |
| 4754 | |
| 4755 | CGF.EmitOMPInnerLoop(S, LoopScope.requiresCleanups(), S.getCond(), |
| 4756 | S.getInc(), |
| 4757 | [&S](CodeGenFunction &CGF) { |
| 4758 | CGF.EmitOMPLoopBody(S, JumpDest()); |
| 4759 | CGF.EmitStopPoint(&S); |
| 4760 | }, |
| 4761 | [](CodeGenFunction &) {}); |
| 4762 | // Emit: if (PreCond) - end. |
| 4763 | if (ContBlock) { |
| 4764 | CGF.EmitBranch(ContBlock); |
| 4765 | CGF.EmitBlock(ContBlock, true); |
| 4766 | } |
Alexey Bataev | f93095a | 2016-05-05 08:46:22 +0000 | [diff] [blame] | 4767 | // Emit final copy of the lastprivate variables if IsLastIter != 0. |
| 4768 | if (HasLastprivateClause) { |
| 4769 | CGF.EmitOMPLastprivateClauseFinal( |
| 4770 | S, isOpenMPSimdDirective(S.getDirectiveKind()), |
| 4771 | CGF.Builder.CreateIsNotNull(CGF.EmitLoadOfScalar( |
| 4772 | CGF.GetAddrOfLocalVar(*LIP), /*Volatile=*/false, |
| 4773 | (*LIP)->getType(), S.getLocStart()))); |
| 4774 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4775 | }; |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4776 | auto &&TaskGen = [&S, SharedsTy, CapturedStruct, |
| 4777 | IfCond](CodeGenFunction &CGF, llvm::Value *OutlinedFn, |
| 4778 | const OMPTaskDataTy &Data) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4779 | auto &&CodeGen = [&](CodeGenFunction &CGF, PrePostActionTy &) { |
| 4780 | OMPLoopScope PreInitScope(CGF, S); |
Alexey Bataev | 24b5bae | 2016-04-28 09:23:51 +0000 | [diff] [blame] | 4781 | CGF.CGM.getOpenMPRuntime().emitTaskLoopCall(CGF, S.getLocStart(), S, |
| 4782 | OutlinedFn, SharedsTy, |
| 4783 | CapturedStruct, IfCond, Data); |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4784 | }; |
| 4785 | CGF.CGM.getOpenMPRuntime().emitInlinedDirective(CGF, OMPD_taskloop, |
| 4786 | CodeGen); |
| 4787 | }; |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4788 | if (Data.Nogroup) { |
| 4789 | EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, Data); |
| 4790 | } else { |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4791 | CGM.getOpenMPRuntime().emitTaskgroupRegion( |
| 4792 | *this, |
| 4793 | [&S, &BodyGen, &TaskGen, &Data](CodeGenFunction &CGF, |
| 4794 | PrePostActionTy &Action) { |
| 4795 | Action.Enter(CGF); |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4796 | CGF.EmitOMPTaskBasedDirective(S, OMPD_taskloop, BodyGen, TaskGen, |
| 4797 | Data); |
Alexey Bataev | 3344603 | 2017-07-12 18:09:32 +0000 | [diff] [blame] | 4798 | }, |
| 4799 | S.getLocStart()); |
| 4800 | } |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4801 | } |
| 4802 | |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4803 | void CodeGenFunction::EmitOMPTaskLoopDirective(const OMPTaskLoopDirective &S) { |
Alexey Bataev | 7292c29 | 2016-04-25 12:22:29 +0000 | [diff] [blame] | 4804 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 49f6e78 | 2015-12-01 04:18:41 +0000 | [diff] [blame] | 4805 | } |
| 4806 | |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4807 | void CodeGenFunction::EmitOMPTaskLoopSimdDirective( |
| 4808 | const OMPTaskLoopSimdDirective &S) { |
Alexey Bataev | 1e73ef3 | 2016-04-28 12:14:51 +0000 | [diff] [blame] | 4809 | EmitOMPTaskLoopBasedDirective(S); |
Alexey Bataev | 0a6ed84 | 2015-12-03 09:40:15 +0000 | [diff] [blame] | 4810 | } |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4811 | |
| 4812 | // Generate the instructions for '#pragma omp target update' directive. |
| 4813 | void CodeGenFunction::EmitOMPTargetUpdateDirective( |
| 4814 | const OMPTargetUpdateDirective &S) { |
Samuel Antao | 8d2d730 | 2016-05-26 18:30:22 +0000 | [diff] [blame] | 4815 | // If we don't have target devices, don't bother emitting the data mapping |
| 4816 | // code. |
| 4817 | if (CGM.getLangOpts().OMPTargetTriples.empty()) |
| 4818 | return; |
| 4819 | |
| 4820 | // Check if we have any if clause associated with the directive. |
| 4821 | const Expr *IfCond = nullptr; |
| 4822 | if (auto *C = S.getSingleClause<OMPIfClause>()) |
| 4823 | IfCond = C->getCondition(); |
| 4824 | |
| 4825 | // Check if we have any device clause associated with the directive. |
| 4826 | const Expr *Device = nullptr; |
| 4827 | if (auto *C = S.getSingleClause<OMPDeviceClause>()) |
| 4828 | Device = C->getDevice(); |
| 4829 | |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4830 | OMPLexicalScope Scope(*this, S, OMPD_task); |
Alexey Bataev | d2202ca | 2017-12-27 17:58:32 +0000 | [diff] [blame] | 4831 | CGM.getOpenMPRuntime().emitTargetDataStandAloneCall(*this, S, IfCond, Device); |
Samuel Antao | 686c70c | 2016-05-26 17:30:50 +0000 | [diff] [blame] | 4832 | } |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 4833 | |
| 4834 | void CodeGenFunction::EmitSimpleOMPExecutableDirective( |
| 4835 | const OMPExecutableDirective &D) { |
| 4836 | if (!D.hasAssociatedStmt() || !D.getAssociatedStmt()) |
| 4837 | return; |
| 4838 | auto &&CodeGen = [&D](CodeGenFunction &CGF, PrePostActionTy &Action) { |
| 4839 | if (isOpenMPSimdDirective(D.getDirectiveKind())) { |
| 4840 | emitOMPSimdRegion(CGF, cast<OMPLoopDirective>(D), Action); |
| 4841 | } else { |
| 4842 | if (const auto *LD = dyn_cast<OMPLoopDirective>(&D)) { |
| 4843 | for (const auto *E : LD->counters()) { |
| 4844 | if (const auto *VD = dyn_cast<OMPCapturedExprDecl>( |
| 4845 | cast<DeclRefExpr>(E)->getDecl())) { |
| 4846 | // Emit only those that were not explicitly referenced in clauses. |
| 4847 | if (!CGF.LocalDeclMap.count(VD)) |
| 4848 | CGF.EmitVarDecl(*VD); |
| 4849 | } |
| 4850 | } |
| 4851 | } |
Alexey Bataev | 475a744 | 2018-01-12 19:39:11 +0000 | [diff] [blame] | 4852 | CGF.EmitStmt(D.getInnermostCapturedStmt()->getCapturedStmt()); |
Alexey Bataev | a8a9153a | 2017-12-29 18:07:07 +0000 | [diff] [blame] | 4853 | } |
| 4854 | }; |
| 4855 | OMPSimdLexicalScope Scope(*this, D); |
| 4856 | CGM.getOpenMPRuntime().emitInlinedDirective( |
| 4857 | *this, |
| 4858 | isOpenMPSimdDirective(D.getDirectiveKind()) ? OMPD_simd |
| 4859 | : D.getDirectiveKind(), |
| 4860 | CodeGen); |
| 4861 | } |