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