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