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