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