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