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