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