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