blob: 2c577904626f0bde38cdc8febacdeef2fe81c787 [file] [log] [blame]
Alexey Bataev9959db52014-05-06 10:08:46 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ---------===//
Alexey Bataeva769e072013-03-22 06:34:35 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alexey Bataeva769e072013-03-22 06:34:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00009/// This file implements semantic analysis for OpenMP directives and
Alexey Bataev6f6f3b42013-05-13 04:18:18 +000010/// clauses.
Alexey Bataeva769e072013-03-22 06:34:35 +000011///
12//===----------------------------------------------------------------------===//
13
Alexey Bataevb08f89f2015-08-14 12:25:37 +000014#include "TreeTransform.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000015#include "clang/AST/ASTContext.h"
Alexey Bataev97720002014-11-11 04:05:39 +000016#include "clang/AST/ASTMutationListener.h"
Alexey Bataeva839ddd2016-03-17 10:19:46 +000017#include "clang/AST/CXXInheritance.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000018#include "clang/AST/Decl.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000019#include "clang/AST/DeclCXX.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000020#include "clang/AST/DeclOpenMP.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000021#include "clang/AST/StmtCXX.h"
22#include "clang/AST/StmtOpenMP.h"
23#include "clang/AST/StmtVisitor.h"
Patrick Lystere13b1e32019-01-02 19:28:48 +000024#include "clang/AST/TypeOrdering.h"
Alexey Bataev9959db52014-05-06 10:08:46 +000025#include "clang/Basic/OpenMPKinds.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000026#include "clang/Sema/Initialization.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000027#include "clang/Sema/Lookup.h"
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000028#include "clang/Sema/Scope.h"
29#include "clang/Sema/ScopeInfo.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000030#include "clang/Sema/SemaInternal.h"
Alexey Bataevfa312f32017-07-21 18:48:21 +000031#include "llvm/ADT/PointerEmbeddedInt.h"
Alexey Bataeva769e072013-03-22 06:34:35 +000032using namespace clang;
33
Alexey Bataev758e55e2013-09-06 18:03:48 +000034//===----------------------------------------------------------------------===//
35// Stack of data-sharing attributes for variables
36//===----------------------------------------------------------------------===//
37
Alexey Bataeve3727102018-04-18 15:57:46 +000038static const Expr *checkMapClauseExpressionBase(
Alexey Bataevf47c4b42017-09-26 13:47:31 +000039 Sema &SemaRef, Expr *E,
40 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000041 OpenMPClauseKind CKind, bool NoDiagnose);
Alexey Bataevf47c4b42017-09-26 13:47:31 +000042
Alexey Bataev758e55e2013-09-06 18:03:48 +000043namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044/// Default data sharing attributes, which can be applied to directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +000045enum DefaultDataSharingAttributes {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000046 DSA_unspecified = 0, /// Data sharing attribute not specified.
47 DSA_none = 1 << 0, /// Default data sharing attribute 'none'.
48 DSA_shared = 1 << 1, /// Default data sharing attribute 'shared'.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000049};
50
51/// Attributes of the defaultmap clause.
52enum DefaultMapAttributes {
53 DMA_unspecified, /// Default mapping is not specified.
54 DMA_tofrom_scalar, /// Default mapping is 'tofrom:scalar'.
Alexey Bataev758e55e2013-09-06 18:03:48 +000055};
Alexey Bataev7ff55242014-06-19 09:13:45 +000056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000057/// Stack for tracking declarations used in OpenMP directives and
Alexey Bataev758e55e2013-09-06 18:03:48 +000058/// clauses and their data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000059class DSAStackTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +000060public:
Alexey Bataeve3727102018-04-18 15:57:46 +000061 struct DSAVarData {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000062 OpenMPDirectiveKind DKind = OMPD_unknown;
63 OpenMPClauseKind CKind = OMPC_unknown;
Alexey Bataeve3727102018-04-18 15:57:46 +000064 const Expr *RefExpr = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000065 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +000066 SourceLocation ImplicitDSALoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +000067 DSAVarData() = default;
Alexey Bataeve3727102018-04-18 15:57:46 +000068 DSAVarData(OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
69 const Expr *RefExpr, DeclRefExpr *PrivateCopy,
70 SourceLocation ImplicitDSALoc)
Alexey Bataevf189cb72017-07-24 14:52:13 +000071 : DKind(DKind), CKind(CKind), RefExpr(RefExpr),
72 PrivateCopy(PrivateCopy), ImplicitDSALoc(ImplicitDSALoc) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +000073 };
Alexey Bataeve3727102018-04-18 15:57:46 +000074 using OperatorOffsetTy =
75 llvm::SmallVector<std::pair<Expr *, OverloadedOperatorKind>, 4>;
Alexey Bataevf138fda2018-08-13 19:04:24 +000076 using DoacrossDependMapTy =
77 llvm::DenseMap<OMPDependClause *, OperatorOffsetTy>;
Alexey Bataeved09d242014-05-28 05:53:51 +000078
Alexey Bataev758e55e2013-09-06 18:03:48 +000079private:
Alexey Bataeve3727102018-04-18 15:57:46 +000080 struct DSAInfo {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000081 OpenMPClauseKind Attributes = OMPC_unknown;
82 /// Pointer to a reference expression and a flag which shows that the
83 /// variable is marked as lastprivate(true) or not (false).
Alexey Bataeve3727102018-04-18 15:57:46 +000084 llvm::PointerIntPair<const Expr *, 1, bool> RefExpr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000085 DeclRefExpr *PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000086 };
Alexey Bataeve3727102018-04-18 15:57:46 +000087 using DeclSAMapTy = llvm::SmallDenseMap<const ValueDecl *, DSAInfo, 8>;
88 using AlignedMapTy = llvm::SmallDenseMap<const ValueDecl *, const Expr *, 8>;
89 using LCDeclInfo = std::pair<unsigned, VarDecl *>;
90 using LoopControlVariablesMapTy =
91 llvm::SmallDenseMap<const ValueDecl *, LCDeclInfo, 8>;
Samuel Antao6890b092016-07-28 14:25:09 +000092 /// Struct that associates a component with the clause kind where they are
93 /// found.
94 struct MappedExprComponentTy {
95 OMPClauseMappableExprCommon::MappableExprComponentLists Components;
96 OpenMPClauseKind Kind = OMPC_unknown;
97 };
Alexey Bataeve3727102018-04-18 15:57:46 +000098 using MappedExprComponentsTy =
99 llvm::DenseMap<const ValueDecl *, MappedExprComponentTy>;
100 using CriticalsWithHintsTy =
101 llvm::StringMap<std::pair<const OMPCriticalDirective *, llvm::APSInt>>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000102 struct ReductionData {
Alexey Bataeve3727102018-04-18 15:57:46 +0000103 using BOKPtrType = llvm::PointerEmbeddedInt<BinaryOperatorKind, 16>;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000104 SourceRange ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +0000105 llvm::PointerUnion<const Expr *, BOKPtrType> ReductionOp;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000106 ReductionData() = default;
107 void set(BinaryOperatorKind BO, SourceRange RR) {
108 ReductionRange = RR;
109 ReductionOp = BO;
110 }
111 void set(const Expr *RefExpr, SourceRange RR) {
112 ReductionRange = RR;
113 ReductionOp = RefExpr;
114 }
115 };
Alexey Bataeve3727102018-04-18 15:57:46 +0000116 using DeclReductionMapTy =
117 llvm::SmallDenseMap<const ValueDecl *, ReductionData, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000118
Alexey Bataeve3727102018-04-18 15:57:46 +0000119 struct SharingMapTy {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000120 DeclSAMapTy SharingMap;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000121 DeclReductionMapTy ReductionMap;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000122 AlignedMapTy AlignedMap;
Samuel Antao90927002016-04-26 14:54:23 +0000123 MappedExprComponentsTy MappedExprComponents;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000124 LoopControlVariablesMapTy LCVMap;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000125 DefaultDataSharingAttributes DefaultAttr = DSA_unspecified;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000126 SourceLocation DefaultAttrLoc;
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000127 DefaultMapAttributes DefaultMapAttr = DMA_unspecified;
128 SourceLocation DefaultMapAttrLoc;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000129 OpenMPDirectiveKind Directive = OMPD_unknown;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000130 DeclarationNameInfo DirectiveName;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000131 Scope *CurScope = nullptr;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000132 SourceLocation ConstructLoc;
Alexey Bataev8b427062016-05-25 12:36:08 +0000133 /// Set of 'depend' clauses with 'sink|source' dependence kind. Required to
134 /// get the data (loop counters etc.) about enclosing loop-based construct.
135 /// This data is required during codegen.
136 DoacrossDependMapTy DoacrossDepends;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000137 /// first argument (Expr *) contains optional argument of the
Alexey Bataev346265e2015-09-25 10:37:12 +0000138 /// 'ordered' clause, the second one is true if the regions has 'ordered'
139 /// clause, false otherwise.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000140 llvm::Optional<std::pair<const Expr *, OMPOrderedClause *>> OrderedRegion;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000141 unsigned AssociatedLoops = 1;
142 const Decl *PossiblyLoopCounter = nullptr;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000143 bool NowaitRegion = false;
144 bool CancelRegion = false;
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000145 bool LoopStart = false;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000146 SourceLocation InnerTeamsRegionLoc;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000147 /// Reference to the taskgroup task_reduction reference expression.
148 Expr *TaskgroupReductionRef = nullptr;
Patrick Lystere13b1e32019-01-02 19:28:48 +0000149 llvm::DenseSet<QualType> MappedClassesQualTypes;
Alexey Bataeved09d242014-05-28 05:53:51 +0000150 SharingMapTy(OpenMPDirectiveKind DKind, DeclarationNameInfo Name,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000151 Scope *CurScope, SourceLocation Loc)
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000152 : Directive(DKind), DirectiveName(Name), CurScope(CurScope),
153 ConstructLoc(Loc) {}
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000154 SharingMapTy() = default;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000155 };
156
Alexey Bataeve3727102018-04-18 15:57:46 +0000157 using StackTy = SmallVector<SharingMapTy, 4>;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000158
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000159 /// Stack of used declaration and their data-sharing attributes.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000160 DeclSAMapTy Threadprivates;
Alexey Bataev4b465392017-04-26 15:06:24 +0000161 const FunctionScopeInfo *CurrentNonCapturingFunctionScope = nullptr;
162 SmallVector<std::pair<StackTy, const FunctionScopeInfo *>, 4> Stack;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000163 /// true, if check for DSA must be from parent directive, false, if
Alexey Bataev39f915b82015-05-08 10:41:21 +0000164 /// from current directive.
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000165 OpenMPClauseKind ClauseKindMode = OMPC_unknown;
Alexey Bataev7ff55242014-06-19 09:13:45 +0000166 Sema &SemaRef;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000167 bool ForceCapturing = false;
Alexey Bataev60705422018-10-30 15:50:12 +0000168 /// true if all the vaiables in the target executable directives must be
169 /// captured by reference.
170 bool ForceCaptureByReferenceInTargetExecutable = false;
Alexey Bataev28c75412015-12-15 08:19:24 +0000171 CriticalsWithHintsTy Criticals;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000172
Alexey Bataeve3727102018-04-18 15:57:46 +0000173 using iterator = StackTy::const_reverse_iterator;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000174
Alexey Bataeve3727102018-04-18 15:57:46 +0000175 DSAVarData getDSA(iterator &Iter, ValueDecl *D) const;
Alexey Bataevec3da872014-01-31 05:15:34 +0000176
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000177 /// Checks if the variable is a local for OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000178 bool isOpenMPLocal(VarDecl *D, iterator Iter) const;
Alexey Bataeved09d242014-05-28 05:53:51 +0000179
Alexey Bataev4b465392017-04-26 15:06:24 +0000180 bool isStackEmpty() const {
181 return Stack.empty() ||
182 Stack.back().second != CurrentNonCapturingFunctionScope ||
183 Stack.back().first.empty();
184 }
185
Kelvin Li1408f912018-09-26 04:28:39 +0000186 /// Vector of previously declared requires directives
187 SmallVector<const OMPRequiresDecl *, 2> RequiresDecls;
188
Alexey Bataev758e55e2013-09-06 18:03:48 +0000189public:
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000190 explicit DSAStackTy(Sema &S) : SemaRef(S) {}
Alexey Bataev39f915b82015-05-08 10:41:21 +0000191
Alexey Bataevaac108a2015-06-23 04:51:00 +0000192 bool isClauseParsingMode() const { return ClauseKindMode != OMPC_unknown; }
Alexey Bataev3f82cfc2017-12-13 15:28:44 +0000193 OpenMPClauseKind getClauseParsingMode() const {
194 assert(isClauseParsingMode() && "Must be in clause parsing mode.");
195 return ClauseKindMode;
196 }
Alexey Bataevaac108a2015-06-23 04:51:00 +0000197 void setClauseParsingMode(OpenMPClauseKind K) { ClauseKindMode = K; }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000198
Samuel Antao9c75cfe2015-07-27 16:38:06 +0000199 bool isForceVarCapturing() const { return ForceCapturing; }
200 void setForceVarCapturing(bool V) { ForceCapturing = V; }
201
Alexey Bataev60705422018-10-30 15:50:12 +0000202 void setForceCaptureByReferenceInTargetExecutable(bool V) {
203 ForceCaptureByReferenceInTargetExecutable = V;
204 }
205 bool isForceCaptureByReferenceInTargetExecutable() const {
206 return ForceCaptureByReferenceInTargetExecutable;
207 }
208
Alexey Bataev758e55e2013-09-06 18:03:48 +0000209 void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +0000210 Scope *CurScope, SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000211 if (Stack.empty() ||
212 Stack.back().second != CurrentNonCapturingFunctionScope)
213 Stack.emplace_back(StackTy(), CurrentNonCapturingFunctionScope);
214 Stack.back().first.emplace_back(DKind, DirName, CurScope, Loc);
215 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000216 }
217
218 void pop() {
Alexey Bataev4b465392017-04-26 15:06:24 +0000219 assert(!Stack.back().first.empty() &&
220 "Data-sharing attributes stack is empty!");
221 Stack.back().first.pop_back();
222 }
223
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000224 /// Marks that we're started loop parsing.
225 void loopInit() {
226 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
227 "Expected loop-based directive.");
228 Stack.back().first.back().LoopStart = true;
229 }
230 /// Start capturing of the variables in the loop context.
231 void loopStart() {
232 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
233 "Expected loop-based directive.");
234 Stack.back().first.back().LoopStart = false;
235 }
236 /// true, if variables are captured, false otherwise.
237 bool isLoopStarted() const {
238 assert(isOpenMPLoopDirective(getCurrentDirective()) &&
239 "Expected loop-based directive.");
240 return !Stack.back().first.back().LoopStart;
241 }
242 /// Marks (or clears) declaration as possibly loop counter.
243 void resetPossibleLoopCounter(const Decl *D = nullptr) {
244 Stack.back().first.back().PossiblyLoopCounter =
245 D ? D->getCanonicalDecl() : D;
246 }
247 /// Gets the possible loop counter decl.
248 const Decl *getPossiblyLoopCunter() const {
249 return Stack.back().first.back().PossiblyLoopCounter;
250 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000251 /// Start new OpenMP region stack in new non-capturing function.
252 void pushFunction() {
253 const FunctionScopeInfo *CurFnScope = SemaRef.getCurFunction();
254 assert(!isa<CapturingScopeInfo>(CurFnScope));
255 CurrentNonCapturingFunctionScope = CurFnScope;
256 }
257 /// Pop region stack for non-capturing function.
258 void popFunction(const FunctionScopeInfo *OldFSI) {
259 if (!Stack.empty() && Stack.back().second == OldFSI) {
260 assert(Stack.back().first.empty());
261 Stack.pop_back();
262 }
263 CurrentNonCapturingFunctionScope = nullptr;
264 for (const FunctionScopeInfo *FSI : llvm::reverse(SemaRef.FunctionScopes)) {
265 if (!isa<CapturingScopeInfo>(FSI)) {
266 CurrentNonCapturingFunctionScope = FSI;
267 break;
268 }
269 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000270 }
271
Alexey Bataeve3727102018-04-18 15:57:46 +0000272 void addCriticalWithHint(const OMPCriticalDirective *D, llvm::APSInt Hint) {
Alexey Bataev43a919f2018-04-13 17:48:43 +0000273 Criticals.try_emplace(D->getDirectiveName().getAsString(), D, Hint);
Alexey Bataev28c75412015-12-15 08:19:24 +0000274 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000275 const std::pair<const OMPCriticalDirective *, llvm::APSInt>
Alexey Bataev28c75412015-12-15 08:19:24 +0000276 getCriticalWithHint(const DeclarationNameInfo &Name) const {
277 auto I = Criticals.find(Name.getAsString());
278 if (I != Criticals.end())
279 return I->second;
280 return std::make_pair(nullptr, llvm::APSInt());
281 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000282 /// If 'aligned' declaration for given variable \a D was not seen yet,
Alp Toker15e62a32014-06-06 12:02:07 +0000283 /// add it and return NULL; otherwise return previous occurrence's expression
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000284 /// for diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +0000285 const Expr *addUniqueAligned(const ValueDecl *D, const Expr *NewDE);
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000286
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000287 /// Register specified variable as loop control variable.
Alexey Bataeve3727102018-04-18 15:57:46 +0000288 void addLoopControlVariable(const ValueDecl *D, VarDecl *Capture);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000289 /// Check if the specified variable is a loop control variable for
Alexey Bataev9c821032015-04-30 04:23:23 +0000290 /// current region.
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000291 /// \return The index of the loop control variable in the list of associated
292 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000293 const LCDeclInfo isLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000294 /// Check if the specified variable is a loop control variable for
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000295 /// parent region.
296 /// \return The index of the loop control variable in the list of associated
297 /// for-loops (from outer to inner).
Alexey Bataeve3727102018-04-18 15:57:46 +0000298 const LCDeclInfo isParentLoopControlVariable(const ValueDecl *D) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000299 /// Get the loop control variable for the I-th loop (or nullptr) in
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000300 /// parent directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000301 const ValueDecl *getParentLoopControlVariable(unsigned I) const;
Alexey Bataev9c821032015-04-30 04:23:23 +0000302
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000303 /// Adds explicit data sharing attribute to the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000304 void addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000305 DeclRefExpr *PrivateCopy = nullptr);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000306
Alexey Bataevfa312f32017-07-21 18:48:21 +0000307 /// Adds additional information for the reduction items with the reduction id
308 /// represented as an operator.
Alexey Bataeve3727102018-04-18 15:57:46 +0000309 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000310 BinaryOperatorKind BOK);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000311 /// Adds additional information for the reduction items with the reduction id
312 /// represented as reduction identifier.
Alexey Bataeve3727102018-04-18 15:57:46 +0000313 void addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000314 const Expr *ReductionRef);
Alexey Bataevfa312f32017-07-21 18:48:21 +0000315 /// Returns the location and reduction operation from the innermost parent
316 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000317 const DSAVarData
318 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
319 BinaryOperatorKind &BOK,
320 Expr *&TaskgroupDescriptor) const;
Alexey Bataevfa312f32017-07-21 18:48:21 +0000321 /// Returns the location and reduction operation from the innermost parent
322 /// region for the given \p D.
Alexey Bataeve3727102018-04-18 15:57:46 +0000323 const DSAVarData
324 getTopMostTaskgroupReductionData(const ValueDecl *D, SourceRange &SR,
325 const Expr *&ReductionRef,
326 Expr *&TaskgroupDescriptor) const;
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000327 /// Return reduction reference expression for the current taskgroup.
328 Expr *getTaskgroupReductionRef() const {
329 assert(Stack.back().first.back().Directive == OMPD_taskgroup &&
330 "taskgroup reference expression requested for non taskgroup "
331 "directive.");
332 return Stack.back().first.back().TaskgroupReductionRef;
333 }
Alexey Bataev88202be2017-07-27 13:20:36 +0000334 /// Checks if the given \p VD declaration is actually a taskgroup reduction
335 /// descriptor variable at the \p Level of OpenMP regions.
Alexey Bataeve3727102018-04-18 15:57:46 +0000336 bool isTaskgroupReductionRef(const ValueDecl *VD, unsigned Level) const {
Alexey Bataev88202be2017-07-27 13:20:36 +0000337 return Stack.back().first[Level].TaskgroupReductionRef &&
338 cast<DeclRefExpr>(Stack.back().first[Level].TaskgroupReductionRef)
339 ->getDecl() == VD;
340 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000341
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000342 /// Returns data sharing attributes from top of the stack for the
Alexey Bataev758e55e2013-09-06 18:03:48 +0000343 /// specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000344 const DSAVarData getTopDSA(ValueDecl *D, bool FromParent);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000345 /// Returns data-sharing attributes for the specified declaration.
Alexey Bataeve3727102018-04-18 15:57:46 +0000346 const DSAVarData getImplicitDSA(ValueDecl *D, bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000347 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000348 /// match specified \a CPred predicate in any directive which matches \a DPred
349 /// predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000350 const DSAVarData
351 hasDSA(ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
352 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
353 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000354 /// Checks if the specified variables has data-sharing attributes which
Alexey Bataevf29276e2014-06-18 04:14:57 +0000355 /// match specified \a CPred predicate in any innermost directive which
356 /// matches \a DPred predicate.
Alexey Bataeve3727102018-04-18 15:57:46 +0000357 const DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000358 hasInnermostDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000359 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
360 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000361 bool FromParent) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000362 /// Checks if the specified variables has explicit data-sharing
Alexey Bataevaac108a2015-06-23 04:51:00 +0000363 /// attributes which match specified \a CPred predicate at the specified
364 /// OpenMP region.
Alexey Bataeve3727102018-04-18 15:57:46 +0000365 bool hasExplicitDSA(const ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000366 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000367 unsigned Level, bool NotLastprivate = false) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000368
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000369 /// Returns true if the directive at level \Level matches in the
Samuel Antao4be30e92015-10-02 17:14:03 +0000370 /// specified \a DPred predicate.
371 bool hasExplicitDirective(
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000372 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000373 unsigned Level) const;
Samuel Antao4be30e92015-10-02 17:14:03 +0000374
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000375 /// Finds a directive which matches specified \a DPred predicate.
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000376 bool hasDirective(
377 const llvm::function_ref<bool(
378 OpenMPDirectiveKind, const DeclarationNameInfo &, SourceLocation)>
379 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +0000380 bool FromParent) const;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000381
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000382 /// Returns currently analyzed directive.
Alexey Bataev758e55e2013-09-06 18:03:48 +0000383 OpenMPDirectiveKind getCurrentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000384 return isStackEmpty() ? OMPD_unknown : Stack.back().first.back().Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000385 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000386 /// Returns directive kind at specified level.
Alexey Bataevdfa430f2017-12-08 15:03:50 +0000387 OpenMPDirectiveKind getDirective(unsigned Level) const {
388 assert(!isStackEmpty() && "No directive at specified level.");
389 return Stack.back().first[Level].Directive;
390 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000391 /// Returns parent directive.
Alexey Bataev549210e2014-06-24 04:39:47 +0000392 OpenMPDirectiveKind getParentDirective() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000393 if (isStackEmpty() || Stack.back().first.size() == 1)
394 return OMPD_unknown;
395 return std::next(Stack.back().first.rbegin())->Directive;
Alexey Bataev549210e2014-06-24 04:39:47 +0000396 }
Alexey Bataev6ab5bb12018-10-29 15:01:58 +0000397
Kelvin Li1408f912018-09-26 04:28:39 +0000398 /// Add requires decl to internal vector
399 void addRequiresDecl(OMPRequiresDecl *RD) {
400 RequiresDecls.push_back(RD);
401 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000402
Kelvin Li1408f912018-09-26 04:28:39 +0000403 /// Checks for a duplicate clause amongst previously declared requires
404 /// directives
405 bool hasDuplicateRequiresClause(ArrayRef<OMPClause *> ClauseList) const {
406 bool IsDuplicate = false;
407 for (OMPClause *CNew : ClauseList) {
408 for (const OMPRequiresDecl *D : RequiresDecls) {
409 for (const OMPClause *CPrev : D->clauselists()) {
410 if (CNew->getClauseKind() == CPrev->getClauseKind()) {
411 SemaRef.Diag(CNew->getBeginLoc(),
412 diag::err_omp_requires_clause_redeclaration)
413 << getOpenMPClauseName(CNew->getClauseKind());
414 SemaRef.Diag(CPrev->getBeginLoc(),
415 diag::note_omp_requires_previous_clause)
416 << getOpenMPClauseName(CPrev->getClauseKind());
417 IsDuplicate = true;
418 }
419 }
420 }
421 }
422 return IsDuplicate;
423 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +0000424
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000425 /// Set default data sharing attribute to none.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000426 void setDefaultDSANone(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000427 assert(!isStackEmpty());
428 Stack.back().first.back().DefaultAttr = DSA_none;
429 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000430 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000431 /// Set default data sharing attribute to shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000432 void setDefaultDSAShared(SourceLocation Loc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000433 assert(!isStackEmpty());
434 Stack.back().first.back().DefaultAttr = DSA_shared;
435 Stack.back().first.back().DefaultAttrLoc = Loc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000436 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000437 /// Set default data mapping attribute to 'tofrom:scalar'.
438 void setDefaultDMAToFromScalar(SourceLocation Loc) {
439 assert(!isStackEmpty());
440 Stack.back().first.back().DefaultMapAttr = DMA_tofrom_scalar;
441 Stack.back().first.back().DefaultMapAttrLoc = Loc;
442 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000443
444 DefaultDataSharingAttributes getDefaultDSA() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000445 return isStackEmpty() ? DSA_unspecified
446 : Stack.back().first.back().DefaultAttr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000447 }
Alexey Bataevbae9a792014-06-27 10:37:06 +0000448 SourceLocation getDefaultDSALocation() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000449 return isStackEmpty() ? SourceLocation()
450 : Stack.back().first.back().DefaultAttrLoc;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000451 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +0000452 DefaultMapAttributes getDefaultDMA() const {
453 return isStackEmpty() ? DMA_unspecified
454 : Stack.back().first.back().DefaultMapAttr;
455 }
456 DefaultMapAttributes getDefaultDMAAtLevel(unsigned Level) const {
457 return Stack.back().first[Level].DefaultMapAttr;
458 }
459 SourceLocation getDefaultDMALocation() const {
460 return isStackEmpty() ? SourceLocation()
461 : Stack.back().first.back().DefaultMapAttrLoc;
462 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000463
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000464 /// Checks if the specified variable is a threadprivate.
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000465 bool isThreadPrivate(VarDecl *D) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000466 const DSAVarData DVar = getTopDSA(D, false);
Alexey Bataevf29276e2014-06-18 04:14:57 +0000467 return isOpenMPThreadPrivate(DVar.CKind);
Alexey Bataevd48bcd82014-03-31 03:36:38 +0000468 }
469
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000470 /// Marks current region as ordered (it has an 'ordered' clause).
Alexey Bataevf138fda2018-08-13 19:04:24 +0000471 void setOrderedRegion(bool IsOrdered, const Expr *Param,
472 OMPOrderedClause *Clause) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000473 assert(!isStackEmpty());
Alexey Bataevf138fda2018-08-13 19:04:24 +0000474 if (IsOrdered)
475 Stack.back().first.back().OrderedRegion.emplace(Param, Clause);
476 else
477 Stack.back().first.back().OrderedRegion.reset();
478 }
479 /// Returns true, if region is ordered (has associated 'ordered' clause),
480 /// false - otherwise.
481 bool isOrderedRegion() const {
482 if (isStackEmpty())
483 return false;
484 return Stack.back().first.rbegin()->OrderedRegion.hasValue();
485 }
486 /// Returns optional parameter for the ordered region.
487 std::pair<const Expr *, OMPOrderedClause *> getOrderedRegionParam() const {
488 if (isStackEmpty() ||
489 !Stack.back().first.rbegin()->OrderedRegion.hasValue())
490 return std::make_pair(nullptr, nullptr);
491 return Stack.back().first.rbegin()->OrderedRegion.getValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000492 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000493 /// Returns true, if parent region is ordered (has associated
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000494 /// 'ordered' clause), false - otherwise.
495 bool isParentOrderedRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000496 if (isStackEmpty() || Stack.back().first.size() == 1)
497 return false;
Alexey Bataevf138fda2018-08-13 19:04:24 +0000498 return std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue();
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000499 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000500 /// Returns optional parameter for the ordered region.
Alexey Bataevf138fda2018-08-13 19:04:24 +0000501 std::pair<const Expr *, OMPOrderedClause *>
502 getParentOrderedRegionParam() const {
503 if (isStackEmpty() || Stack.back().first.size() == 1 ||
504 !std::next(Stack.back().first.rbegin())->OrderedRegion.hasValue())
505 return std::make_pair(nullptr, nullptr);
506 return std::next(Stack.back().first.rbegin())->OrderedRegion.getValue();
Alexey Bataev346265e2015-09-25 10:37:12 +0000507 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000508 /// Marks current region as nowait (it has a 'nowait' clause).
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000509 void setNowaitRegion(bool IsNowait = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000510 assert(!isStackEmpty());
511 Stack.back().first.back().NowaitRegion = IsNowait;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000512 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000513 /// Returns true, if parent region is nowait (has associated
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000514 /// 'nowait' clause), false - otherwise.
515 bool isParentNowaitRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000516 if (isStackEmpty() || Stack.back().first.size() == 1)
517 return false;
518 return std::next(Stack.back().first.rbegin())->NowaitRegion;
Alexey Bataev6d4ed052015-07-01 06:57:41 +0000519 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000520 /// Marks parent region as cancel region.
Alexey Bataev25e5b442015-09-15 12:52:43 +0000521 void setParentCancelRegion(bool Cancel = true) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000522 if (!isStackEmpty() && Stack.back().first.size() > 1) {
523 auto &StackElemRef = *std::next(Stack.back().first.rbegin());
524 StackElemRef.CancelRegion |= StackElemRef.CancelRegion || Cancel;
525 }
Alexey Bataev25e5b442015-09-15 12:52:43 +0000526 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000527 /// Return true if current region has inner cancel construct.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000528 bool isCancelRegion() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000529 return isStackEmpty() ? false : Stack.back().first.back().CancelRegion;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000530 }
Alexey Bataev9fb6e642014-07-22 06:45:04 +0000531
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000532 /// Set collapse value for the region.
Alexey Bataev4b465392017-04-26 15:06:24 +0000533 void setAssociatedLoops(unsigned Val) {
534 assert(!isStackEmpty());
535 Stack.back().first.back().AssociatedLoops = Val;
536 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000537 /// Return collapse value for region.
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000538 unsigned getAssociatedLoops() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000539 return isStackEmpty() ? 0 : Stack.back().first.back().AssociatedLoops;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000540 }
Alexey Bataev9c821032015-04-30 04:23:23 +0000541
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000542 /// Marks current target region as one with closely nested teams
Alexey Bataev13314bf2014-10-09 04:18:56 +0000543 /// region.
544 void setParentTeamsRegionLoc(SourceLocation TeamsRegionLoc) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000545 if (!isStackEmpty() && Stack.back().first.size() > 1) {
546 std::next(Stack.back().first.rbegin())->InnerTeamsRegionLoc =
547 TeamsRegionLoc;
548 }
Alexey Bataev13314bf2014-10-09 04:18:56 +0000549 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000550 /// Returns true, if current region has closely nested teams region.
Alexey Bataev13314bf2014-10-09 04:18:56 +0000551 bool hasInnerTeamsRegion() const {
552 return getInnerTeamsRegionLoc().isValid();
553 }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000554 /// Returns location of the nested teams region (if any).
Alexey Bataev13314bf2014-10-09 04:18:56 +0000555 SourceLocation getInnerTeamsRegionLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000556 return isStackEmpty() ? SourceLocation()
557 : Stack.back().first.back().InnerTeamsRegionLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000558 }
559
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000560 Scope *getCurScope() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000561 return isStackEmpty() ? nullptr : Stack.back().first.back().CurScope;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000562 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000563 SourceLocation getConstructLoc() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000564 return isStackEmpty() ? SourceLocation()
565 : Stack.back().first.back().ConstructLoc;
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000566 }
Kelvin Li0bff7af2015-11-23 05:32:03 +0000567
Samuel Antao4c8035b2016-12-12 18:00:20 +0000568 /// Do the check specified in \a Check to all component lists and return true
569 /// if any issue is found.
Samuel Antao90927002016-04-26 14:54:23 +0000570 bool checkMappableExprComponentListsForDecl(
Alexey Bataeve3727102018-04-18 15:57:46 +0000571 const ValueDecl *VD, bool CurrentRegionOnly,
Samuel Antao6890b092016-07-28 14:25:09 +0000572 const llvm::function_ref<
573 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000574 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000575 Check) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000576 if (isStackEmpty())
577 return false;
578 auto SI = Stack.back().first.rbegin();
579 auto SE = Stack.back().first.rend();
Samuel Antao5de996e2016-01-22 20:21:36 +0000580
581 if (SI == SE)
582 return false;
583
Alexey Bataeve3727102018-04-18 15:57:46 +0000584 if (CurrentRegionOnly)
Samuel Antao5de996e2016-01-22 20:21:36 +0000585 SE = std::next(SI);
Alexey Bataeve3727102018-04-18 15:57:46 +0000586 else
587 std::advance(SI, 1);
Samuel Antao5de996e2016-01-22 20:21:36 +0000588
589 for (; SI != SE; ++SI) {
Samuel Antao90927002016-04-26 14:54:23 +0000590 auto MI = SI->MappedExprComponents.find(VD);
591 if (MI != SI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000592 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
593 MI->second.Components)
Samuel Antao6890b092016-07-28 14:25:09 +0000594 if (Check(L, MI->second.Kind))
Samuel Antao5de996e2016-01-22 20:21:36 +0000595 return true;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000596 }
Samuel Antao5de996e2016-01-22 20:21:36 +0000597 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000598 }
599
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000600 /// Do the check specified in \a Check to all component lists at a given level
601 /// and return true if any issue is found.
602 bool checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +0000603 const ValueDecl *VD, unsigned Level,
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000604 const llvm::function_ref<
605 bool(OMPClauseMappableExprCommon::MappableExprComponentListRef,
Alexey Bataev97d18bf2018-04-11 19:21:00 +0000606 OpenMPClauseKind)>
Alexey Bataeve3727102018-04-18 15:57:46 +0000607 Check) const {
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000608 if (isStackEmpty())
609 return false;
610
611 auto StartI = Stack.back().first.begin();
612 auto EndI = Stack.back().first.end();
613 if (std::distance(StartI, EndI) <= (int)Level)
614 return false;
615 std::advance(StartI, Level);
616
617 auto MI = StartI->MappedExprComponents.find(VD);
618 if (MI != StartI->MappedExprComponents.end())
Alexey Bataeve3727102018-04-18 15:57:46 +0000619 for (OMPClauseMappableExprCommon::MappableExprComponentListRef L :
620 MI->second.Components)
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +0000621 if (Check(L, MI->second.Kind))
622 return true;
623 return false;
624 }
625
Samuel Antao4c8035b2016-12-12 18:00:20 +0000626 /// Create a new mappable expression component list associated with a given
627 /// declaration and initialize it with the provided list of components.
Samuel Antao90927002016-04-26 14:54:23 +0000628 void addMappableExpressionComponents(
Alexey Bataeve3727102018-04-18 15:57:46 +0000629 const ValueDecl *VD,
Samuel Antao6890b092016-07-28 14:25:09 +0000630 OMPClauseMappableExprCommon::MappableExprComponentListRef Components,
631 OpenMPClauseKind WhereFoundClauseKind) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000632 assert(!isStackEmpty() &&
Samuel Antao90927002016-04-26 14:54:23 +0000633 "Not expecting to retrieve components from a empty stack!");
Alexey Bataeve3727102018-04-18 15:57:46 +0000634 MappedExprComponentTy &MEC =
635 Stack.back().first.back().MappedExprComponents[VD];
Samuel Antao90927002016-04-26 14:54:23 +0000636 // Create new entry and append the new components there.
Samuel Antao6890b092016-07-28 14:25:09 +0000637 MEC.Components.resize(MEC.Components.size() + 1);
638 MEC.Components.back().append(Components.begin(), Components.end());
639 MEC.Kind = WhereFoundClauseKind;
Kelvin Li0bff7af2015-11-23 05:32:03 +0000640 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000641
642 unsigned getNestingLevel() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000643 assert(!isStackEmpty());
644 return Stack.back().first.size() - 1;
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000645 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000646 void addDoacrossDependClause(OMPDependClause *C,
647 const OperatorOffsetTy &OpsOffs) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000648 assert(!isStackEmpty() && Stack.back().first.size() > 1);
Alexey Bataeve3727102018-04-18 15:57:46 +0000649 SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000650 assert(isOpenMPWorksharingDirective(StackElem.Directive));
Alexey Bataeve3727102018-04-18 15:57:46 +0000651 StackElem.DoacrossDepends.try_emplace(C, OpsOffs);
Alexey Bataev8b427062016-05-25 12:36:08 +0000652 }
653 llvm::iterator_range<DoacrossDependMapTy::const_iterator>
654 getDoacrossDependClauses() const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000655 assert(!isStackEmpty());
Alexey Bataeve3727102018-04-18 15:57:46 +0000656 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000657 if (isOpenMPWorksharingDirective(StackElem.Directive)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000658 const DoacrossDependMapTy &Ref = StackElem.DoacrossDepends;
Alexey Bataev8b427062016-05-25 12:36:08 +0000659 return llvm::make_range(Ref.begin(), Ref.end());
660 }
Alexey Bataev4b465392017-04-26 15:06:24 +0000661 return llvm::make_range(StackElem.DoacrossDepends.end(),
662 StackElem.DoacrossDepends.end());
Alexey Bataev8b427062016-05-25 12:36:08 +0000663 }
Patrick Lystere13b1e32019-01-02 19:28:48 +0000664
665 // Store types of classes which have been explicitly mapped
666 void addMappedClassesQualTypes(QualType QT) {
667 SharingMapTy &StackElem = Stack.back().first.back();
668 StackElem.MappedClassesQualTypes.insert(QT);
669 }
670
671 // Return set of mapped classes types
672 bool isClassPreviouslyMapped(QualType QT) const {
673 const SharingMapTy &StackElem = Stack.back().first.back();
674 return StackElem.MappedClassesQualTypes.count(QT) != 0;
675 }
676
Alexey Bataev758e55e2013-09-06 18:03:48 +0000677};
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000678
679bool isImplicitTaskingRegion(OpenMPDirectiveKind DKind) {
680 return isOpenMPParallelDirective(DKind) || isOpenMPTeamsDirective(DKind);
681}
682
683bool isImplicitOrExplicitTaskingRegion(OpenMPDirectiveKind DKind) {
684 return isImplicitTaskingRegion(DKind) || isOpenMPTaskingDirective(DKind) || DKind == OMPD_unknown;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000685}
Alexey Bataeve3727102018-04-18 15:57:46 +0000686
Alexey Bataeved09d242014-05-28 05:53:51 +0000687} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +0000688
Alexey Bataeve3727102018-04-18 15:57:46 +0000689static const Expr *getExprAsWritten(const Expr *E) {
Bill Wendling7c44da22018-10-31 03:48:47 +0000690 if (const auto *FE = dyn_cast<FullExpr>(E))
691 E = FE->getSubExpr();
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000692
Alexey Bataeve3727102018-04-18 15:57:46 +0000693 if (const auto *MTE = dyn_cast<MaterializeTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000694 E = MTE->GetTemporaryExpr();
695
Alexey Bataeve3727102018-04-18 15:57:46 +0000696 while (const auto *Binder = dyn_cast<CXXBindTemporaryExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000697 E = Binder->getSubExpr();
698
Alexey Bataeve3727102018-04-18 15:57:46 +0000699 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000700 E = ICE->getSubExprAsWritten();
701 return E->IgnoreParens();
702}
703
Alexey Bataeve3727102018-04-18 15:57:46 +0000704static Expr *getExprAsWritten(Expr *E) {
705 return const_cast<Expr *>(getExprAsWritten(const_cast<const Expr *>(E)));
706}
707
708static const ValueDecl *getCanonicalDecl(const ValueDecl *D) {
709 if (const auto *CED = dyn_cast<OMPCapturedExprDecl>(D))
710 if (const auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataev4d4624c2017-07-20 16:47:47 +0000711 D = ME->getMemberDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +0000712 const auto *VD = dyn_cast<VarDecl>(D);
713 const auto *FD = dyn_cast<FieldDecl>(D);
David Majnemer9d168222016-08-05 17:44:54 +0000714 if (VD != nullptr) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000715 VD = VD->getCanonicalDecl();
716 D = VD;
717 } else {
718 assert(FD);
719 FD = FD->getCanonicalDecl();
720 D = FD;
721 }
722 return D;
723}
724
Alexey Bataeve3727102018-04-18 15:57:46 +0000725static ValueDecl *getCanonicalDecl(ValueDecl *D) {
726 return const_cast<ValueDecl *>(
727 getCanonicalDecl(const_cast<const ValueDecl *>(D)));
728}
729
730DSAStackTy::DSAVarData DSAStackTy::getDSA(iterator &Iter,
731 ValueDecl *D) const {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000732 D = getCanonicalDecl(D);
733 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000734 const auto *FD = dyn_cast<FieldDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000735 DSAVarData DVar;
Alexey Bataev4b465392017-04-26 15:06:24 +0000736 if (isStackEmpty() || Iter == Stack.back().first.rend()) {
Alexey Bataev750a58b2014-03-18 12:19:12 +0000737 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
738 // in a region but not in construct]
739 // File-scope or namespace-scope variables referenced in called routines
740 // in the region are shared unless they appear in a threadprivate
741 // directive.
Alexey Bataeve3727102018-04-18 15:57:46 +0000742 if (VD && !VD->isFunctionOrMethodVarDecl() && !isa<ParmVarDecl>(VD))
Alexey Bataev750a58b2014-03-18 12:19:12 +0000743 DVar.CKind = OMPC_shared;
744
745 // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
746 // in a region but not in construct]
747 // Variables with static storage duration that are declared in called
748 // routines in the region are shared.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000749 if (VD && VD->hasGlobalStorage())
750 DVar.CKind = OMPC_shared;
751
752 // Non-static data members are shared by default.
753 if (FD)
Alexey Bataev750a58b2014-03-18 12:19:12 +0000754 DVar.CKind = OMPC_shared;
755
Alexey Bataev758e55e2013-09-06 18:03:48 +0000756 return DVar;
757 }
Alexey Bataevec3da872014-01-31 05:15:34 +0000758
Alexey Bataevec3da872014-01-31 05:15:34 +0000759 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
760 // in a Construct, C/C++, predetermined, p.1]
761 // Variables with automatic storage duration that are declared in a scope
762 // inside the construct are private.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000763 if (VD && isOpenMPLocal(VD, Iter) && VD->isLocalVarDecl() &&
764 (VD->getStorageClass() == SC_Auto || VD->getStorageClass() == SC_None)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +0000765 DVar.CKind = OMPC_private;
766 return DVar;
Alexey Bataevec3da872014-01-31 05:15:34 +0000767 }
768
Alexey Bataeveffbdf12017-07-21 17:24:30 +0000769 DVar.DKind = Iter->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000770 // Explicitly specified attributes and local variables with predetermined
771 // attributes.
772 if (Iter->SharingMap.count(D)) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000773 const DSAInfo &Data = Iter->SharingMap.lookup(D);
774 DVar.RefExpr = Data.RefExpr.getPointer();
775 DVar.PrivateCopy = Data.PrivateCopy;
776 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +0000777 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000778 return DVar;
779 }
780
781 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
782 // in a Construct, C/C++, implicitly determined, p.1]
783 // In a parallel or task construct, the data-sharing attributes of these
784 // variables are determined by the default clause, if present.
785 switch (Iter->DefaultAttr) {
786 case DSA_shared:
787 DVar.CKind = OMPC_shared;
Alexey Bataevbae9a792014-06-27 10:37:06 +0000788 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000789 return DVar;
790 case DSA_none:
791 return DVar;
792 case DSA_unspecified:
793 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
794 // in a Construct, implicitly determined, p.2]
795 // In a parallel construct, if no default clause is present, these
796 // variables are shared.
Alexey Bataevbae9a792014-06-27 10:37:06 +0000797 DVar.ImplicitDSALoc = Iter->DefaultAttrLoc;
Alexey Bataev13314bf2014-10-09 04:18:56 +0000798 if (isOpenMPParallelDirective(DVar.DKind) ||
799 isOpenMPTeamsDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000800 DVar.CKind = OMPC_shared;
801 return DVar;
802 }
803
804 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
805 // in a Construct, implicitly determined, p.4]
806 // In a task construct, if no default clause is present, a variable that in
807 // the enclosing context is determined to be shared by all implicit tasks
808 // bound to the current team is shared.
Alexey Bataev35aaee62016-04-13 13:36:48 +0000809 if (isOpenMPTaskingDirective(DVar.DKind)) {
Alexey Bataev758e55e2013-09-06 18:03:48 +0000810 DSAVarData DVarTemp;
Alexey Bataeve3727102018-04-18 15:57:46 +0000811 iterator I = Iter, E = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +0000812 do {
813 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +0000814 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables
Alexey Bataev35aaee62016-04-13 13:36:48 +0000815 // Referenced in a Construct, implicitly determined, p.6]
Alexey Bataev758e55e2013-09-06 18:03:48 +0000816 // In a task construct, if no default clause is present, a variable
817 // whose data-sharing attribute is not determined by the rules above is
818 // firstprivate.
819 DVarTemp = getDSA(I, D);
820 if (DVarTemp.CKind != OMPC_shared) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +0000821 DVar.RefExpr = nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +0000822 DVar.CKind = OMPC_firstprivate;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000823 return DVar;
824 }
Alexey Bataev7e6803e2019-01-09 15:58:05 +0000825 } while (I != E && !isImplicitTaskingRegion(I->Directive));
Alexey Bataev758e55e2013-09-06 18:03:48 +0000826 DVar.CKind =
Alexey Bataeved09d242014-05-28 05:53:51 +0000827 (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000828 return DVar;
829 }
830 }
831 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
832 // in a Construct, implicitly determined, p.3]
833 // For constructs other than task, if no default clause is present, these
834 // variables inherit their data-sharing attributes from the enclosing
835 // context.
Dmitry Polukhindc78bc822016-04-01 09:52:30 +0000836 return getDSA(++Iter, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000837}
838
Alexey Bataeve3727102018-04-18 15:57:46 +0000839const Expr *DSAStackTy::addUniqueAligned(const ValueDecl *D,
840 const Expr *NewDE) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000841 assert(!isStackEmpty() && "Data sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000842 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000843 SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000844 auto It = StackElem.AlignedMap.find(D);
845 if (It == StackElem.AlignedMap.end()) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000846 assert(NewDE && "Unexpected nullptr expr to be added into aligned map");
Alexey Bataev4b465392017-04-26 15:06:24 +0000847 StackElem.AlignedMap[D] = NewDE;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000848 return nullptr;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000849 }
Alexey Bataeve3727102018-04-18 15:57:46 +0000850 assert(It->second && "Unexpected nullptr expr in the aligned map");
851 return It->second;
Alexander Musmanf0d76e72014-05-29 14:36:25 +0000852}
853
Alexey Bataeve3727102018-04-18 15:57:46 +0000854void DSAStackTy::addLoopControlVariable(const ValueDecl *D, VarDecl *Capture) {
Alexey Bataev4b465392017-04-26 15:06:24 +0000855 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000856 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000857 SharingMapTy &StackElem = Stack.back().first.back();
858 StackElem.LCVMap.try_emplace(
859 D, LCDeclInfo(StackElem.LCVMap.size() + 1, Capture));
Alexey Bataev9c821032015-04-30 04:23:23 +0000860}
861
Alexey Bataeve3727102018-04-18 15:57:46 +0000862const DSAStackTy::LCDeclInfo
863DSAStackTy::isLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000864 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000865 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000866 const SharingMapTy &StackElem = Stack.back().first.back();
Alexey Bataev4b465392017-04-26 15:06:24 +0000867 auto It = StackElem.LCVMap.find(D);
868 if (It != StackElem.LCVMap.end())
869 return It->second;
870 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000871}
872
Alexey Bataeve3727102018-04-18 15:57:46 +0000873const DSAStackTy::LCDeclInfo
874DSAStackTy::isParentLoopControlVariable(const ValueDecl *D) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000875 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
876 "Data-sharing attributes stack is empty");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000877 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +0000878 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000879 auto It = StackElem.LCVMap.find(D);
880 if (It != StackElem.LCVMap.end())
881 return It->second;
882 return {0, nullptr};
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000883}
884
Alexey Bataeve3727102018-04-18 15:57:46 +0000885const ValueDecl *DSAStackTy::getParentLoopControlVariable(unsigned I) const {
Alexey Bataev4b465392017-04-26 15:06:24 +0000886 assert(!isStackEmpty() && Stack.back().first.size() > 1 &&
887 "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000888 const SharingMapTy &StackElem = *std::next(Stack.back().first.rbegin());
Alexey Bataev4b465392017-04-26 15:06:24 +0000889 if (StackElem.LCVMap.size() < I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000890 return nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +0000891 for (const auto &Pair : StackElem.LCVMap)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +0000892 if (Pair.second.first == I)
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000893 return Pair.first;
Alexey Bataeva636c7f2015-12-23 10:27:45 +0000894 return nullptr;
Alexey Bataev9c821032015-04-30 04:23:23 +0000895}
896
Alexey Bataeve3727102018-04-18 15:57:46 +0000897void DSAStackTy::addDSA(const ValueDecl *D, const Expr *E, OpenMPClauseKind A,
Alexey Bataev90c228f2016-02-08 09:29:13 +0000898 DeclRefExpr *PrivateCopy) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +0000899 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +0000900 if (A == OMPC_threadprivate) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000901 DSAInfo &Data = Threadprivates[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000902 Data.Attributes = A;
903 Data.RefExpr.setPointer(E);
904 Data.PrivateCopy = nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +0000905 } else {
Alexey Bataev4b465392017-04-26 15:06:24 +0000906 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataeve3727102018-04-18 15:57:46 +0000907 DSAInfo &Data = Stack.back().first.back().SharingMap[D];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000908 assert(Data.Attributes == OMPC_unknown || (A == Data.Attributes) ||
909 (A == OMPC_firstprivate && Data.Attributes == OMPC_lastprivate) ||
910 (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) ||
911 (isLoopControlVariable(D).first && A == OMPC_private));
912 if (A == OMPC_lastprivate && Data.Attributes == OMPC_firstprivate) {
913 Data.RefExpr.setInt(/*IntVal=*/true);
914 return;
915 }
916 const bool IsLastprivate =
917 A == OMPC_lastprivate || Data.Attributes == OMPC_lastprivate;
918 Data.Attributes = A;
919 Data.RefExpr.setPointerAndInt(E, IsLastprivate);
920 Data.PrivateCopy = PrivateCopy;
921 if (PrivateCopy) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000922 DSAInfo &Data =
923 Stack.back().first.back().SharingMap[PrivateCopy->getDecl()];
Alexey Bataev7ace49d2016-05-17 08:55:33 +0000924 Data.Attributes = A;
925 Data.RefExpr.setPointerAndInt(PrivateCopy, IsLastprivate);
926 Data.PrivateCopy = nullptr;
927 }
Alexey Bataev758e55e2013-09-06 18:03:48 +0000928 }
929}
930
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000931/// Build a variable declaration for OpenMP loop iteration variable.
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000932static VarDecl *buildVarDecl(Sema &SemaRef, SourceLocation Loc, QualType Type,
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000933 StringRef Name, const AttrVec *Attrs = nullptr,
934 DeclRefExpr *OrigRef = nullptr) {
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000935 DeclContext *DC = SemaRef.CurContext;
936 IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name);
937 TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc);
Alexey Bataeve3727102018-04-18 15:57:46 +0000938 auto *Decl =
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000939 VarDecl::Create(SemaRef.Context, DC, Loc, Loc, II, Type, TInfo, SC_None);
940 if (Attrs) {
941 for (specific_attr_iterator<AlignedAttr> I(Attrs->begin()), E(Attrs->end());
942 I != E; ++I)
943 Decl->addAttr(*I);
944 }
945 Decl->setImplicit();
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000946 if (OrigRef) {
947 Decl->addAttr(
948 OMPReferencedVarAttr::CreateImplicit(SemaRef.Context, OrigRef));
949 }
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000950 return Decl;
951}
952
953static DeclRefExpr *buildDeclRefExpr(Sema &S, VarDecl *D, QualType Ty,
954 SourceLocation Loc,
955 bool RefersToCapture = false) {
956 D->setReferenced();
957 D->markUsed(S.Context);
958 return DeclRefExpr::Create(S.getASTContext(), NestedNameSpecifierLoc(),
959 SourceLocation(), D, RefersToCapture, Loc, Ty,
960 VK_LValue);
961}
962
Alexey Bataeve3727102018-04-18 15:57:46 +0000963void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000964 BinaryOperatorKind BOK) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000965 D = getCanonicalDecl(D);
966 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000967 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000968 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000969 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000970 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000971 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000972 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000973 "Additional reduction info may be specified only once for reduction "
974 "items.");
975 ReductionData.set(BOK, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000976 Expr *&TaskgroupReductionRef =
977 Stack.back().first.back().TaskgroupReductionRef;
978 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +0000979 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
980 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +0000981 TaskgroupReductionRef =
982 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000983 }
Alexey Bataevfa312f32017-07-21 18:48:21 +0000984}
985
Alexey Bataeve3727102018-04-18 15:57:46 +0000986void DSAStackTy::addTaskgroupReductionData(const ValueDecl *D, SourceRange SR,
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000987 const Expr *ReductionRef) {
Alexey Bataevfa312f32017-07-21 18:48:21 +0000988 D = getCanonicalDecl(D);
989 assert(!isStackEmpty() && "Data-sharing attributes stack is empty");
Alexey Bataevfa312f32017-07-21 18:48:21 +0000990 assert(
Richard Trieu09f14112017-07-21 21:29:35 +0000991 Stack.back().first.back().SharingMap[D].Attributes == OMPC_reduction &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000992 "Additional reduction info may be specified only for reduction items.");
Alexey Bataeve3727102018-04-18 15:57:46 +0000993 ReductionData &ReductionData = Stack.back().first.back().ReductionMap[D];
Alexey Bataevfa312f32017-07-21 18:48:21 +0000994 assert(ReductionData.ReductionRange.isInvalid() &&
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000995 Stack.back().first.back().Directive == OMPD_taskgroup &&
Alexey Bataevfa312f32017-07-21 18:48:21 +0000996 "Additional reduction info may be specified only once for reduction "
997 "items.");
998 ReductionData.set(ReductionRef, SR);
Alexey Bataev3b1b8952017-07-25 15:53:26 +0000999 Expr *&TaskgroupReductionRef =
1000 Stack.back().first.back().TaskgroupReductionRef;
1001 if (!TaskgroupReductionRef) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001002 VarDecl *VD = buildVarDecl(SemaRef, SR.getBegin(),
1003 SemaRef.Context.VoidPtrTy, ".task_red.");
Alexey Bataevd070a582017-10-25 15:54:04 +00001004 TaskgroupReductionRef =
1005 buildDeclRefExpr(SemaRef, VD, SemaRef.Context.VoidPtrTy, SR.getBegin());
Alexey Bataev3b1b8952017-07-25 15:53:26 +00001006 }
Alexey Bataevfa312f32017-07-21 18:48:21 +00001007}
1008
Alexey Bataeve3727102018-04-18 15:57:46 +00001009const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1010 const ValueDecl *D, SourceRange &SR, BinaryOperatorKind &BOK,
1011 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001012 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001013 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1014 if (Stack.back().first.empty())
1015 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001016 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1017 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001018 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001019 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001020 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001021 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001022 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001023 if (!ReductionData.ReductionOp ||
1024 ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001025 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001026 SR = ReductionData.ReductionRange;
Alexey Bataevf87fa882017-07-21 19:26:22 +00001027 BOK = ReductionData.ReductionOp.get<ReductionData::BOKPtrType>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001028 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1029 "expression for the descriptor is not "
1030 "set.");
1031 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001032 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1033 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001034 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001035 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001036}
1037
Alexey Bataeve3727102018-04-18 15:57:46 +00001038const DSAStackTy::DSAVarData DSAStackTy::getTopMostTaskgroupReductionData(
1039 const ValueDecl *D, SourceRange &SR, const Expr *&ReductionRef,
1040 Expr *&TaskgroupDescriptor) const {
Alexey Bataevfa312f32017-07-21 18:48:21 +00001041 D = getCanonicalDecl(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001042 assert(!isStackEmpty() && "Data-sharing attributes stack is empty.");
1043 if (Stack.back().first.empty())
1044 return DSAVarData();
Alexey Bataeve3727102018-04-18 15:57:46 +00001045 for (iterator I = std::next(Stack.back().first.rbegin(), 1),
1046 E = Stack.back().first.rend();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001047 I != E; std::advance(I, 1)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001048 const DSAInfo &Data = I->SharingMap.lookup(D);
Alexey Bataevf189cb72017-07-24 14:52:13 +00001049 if (Data.Attributes != OMPC_reduction || I->Directive != OMPD_taskgroup)
Alexey Bataevfa312f32017-07-21 18:48:21 +00001050 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001051 const ReductionData &ReductionData = I->ReductionMap.lookup(D);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001052 if (!ReductionData.ReductionOp ||
1053 !ReductionData.ReductionOp.is<const Expr *>())
Alexey Bataevf189cb72017-07-24 14:52:13 +00001054 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001055 SR = ReductionData.ReductionRange;
1056 ReductionRef = ReductionData.ReductionOp.get<const Expr *>();
Alexey Bataev88202be2017-07-27 13:20:36 +00001057 assert(I->TaskgroupReductionRef && "taskgroup reduction reference "
1058 "expression for the descriptor is not "
1059 "set.");
1060 TaskgroupDescriptor = I->TaskgroupReductionRef;
Alexey Bataevf189cb72017-07-24 14:52:13 +00001061 return DSAVarData(OMPD_taskgroup, OMPC_reduction, Data.RefExpr.getPointer(),
1062 Data.PrivateCopy, I->DefaultAttrLoc);
Alexey Bataevfa312f32017-07-21 18:48:21 +00001063 }
Alexey Bataevf189cb72017-07-24 14:52:13 +00001064 return DSAVarData();
Alexey Bataevfa312f32017-07-21 18:48:21 +00001065}
1066
Alexey Bataeve3727102018-04-18 15:57:46 +00001067bool DSAStackTy::isOpenMPLocal(VarDecl *D, iterator Iter) const {
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001068 D = D->getCanonicalDecl();
Alexey Bataev852525d2018-03-02 17:17:12 +00001069 if (!isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001070 iterator I = Iter, E = Stack.back().first.rend();
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001071 Scope *TopScope = nullptr;
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001072 while (I != E && !isImplicitOrExplicitTaskingRegion(I->Directive) &&
Alexey Bataev852525d2018-03-02 17:17:12 +00001073 !isOpenMPTargetExecutionDirective(I->Directive))
Alexey Bataevec3da872014-01-31 05:15:34 +00001074 ++I;
Alexey Bataeved09d242014-05-28 05:53:51 +00001075 if (I == E)
1076 return false;
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001077 TopScope = I->CurScope ? I->CurScope->getParent() : nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00001078 Scope *CurScope = getCurScope();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001079 while (CurScope != TopScope && !CurScope->isDeclScope(D))
Alexey Bataev758e55e2013-09-06 18:03:48 +00001080 CurScope = CurScope->getParent();
Alexey Bataevec3da872014-01-31 05:15:34 +00001081 return CurScope != TopScope;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001082 }
Alexey Bataevec3da872014-01-31 05:15:34 +00001083 return false;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001084}
1085
Joel E. Dennyd2649292019-01-04 22:11:56 +00001086static bool isConstNotMutableType(Sema &SemaRef, QualType Type,
1087 bool AcceptIfMutable = true,
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001088 bool *IsClassType = nullptr) {
1089 ASTContext &Context = SemaRef.getASTContext();
Joel E. Dennyd2649292019-01-04 22:11:56 +00001090 Type = Type.getNonReferenceType().getCanonicalType();
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001091 bool IsConstant = Type.isConstant(Context);
1092 Type = Context.getBaseElementType(Type);
Joel E. Dennyd2649292019-01-04 22:11:56 +00001093 const CXXRecordDecl *RD = AcceptIfMutable && SemaRef.getLangOpts().CPlusPlus
1094 ? Type->getAsCXXRecordDecl()
1095 : nullptr;
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001096 if (const auto *CTSD = dyn_cast_or_null<ClassTemplateSpecializationDecl>(RD))
1097 if (const ClassTemplateDecl *CTD = CTSD->getSpecializedTemplate())
1098 RD = CTD->getTemplatedDecl();
1099 if (IsClassType)
1100 *IsClassType = RD;
1101 return IsConstant && !(SemaRef.getLangOpts().CPlusPlus && RD &&
1102 RD->hasDefinition() && RD->hasMutableFields());
1103}
1104
Joel E. Dennyd2649292019-01-04 22:11:56 +00001105static bool rejectConstNotMutableType(Sema &SemaRef, const ValueDecl *D,
1106 QualType Type, OpenMPClauseKind CKind,
1107 SourceLocation ELoc,
1108 bool AcceptIfMutable = true,
1109 bool ListItemNotVar = false) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001110 ASTContext &Context = SemaRef.getASTContext();
1111 bool IsClassType;
Joel E. Dennyd2649292019-01-04 22:11:56 +00001112 if (isConstNotMutableType(SemaRef, Type, AcceptIfMutable, &IsClassType)) {
1113 unsigned Diag = ListItemNotVar
1114 ? diag::err_omp_const_list_item
1115 : IsClassType ? diag::err_omp_const_not_mutable_variable
1116 : diag::err_omp_const_variable;
1117 SemaRef.Diag(ELoc, Diag) << getOpenMPClauseName(CKind);
1118 if (!ListItemNotVar && D) {
1119 const VarDecl *VD = dyn_cast<VarDecl>(D);
1120 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
1121 VarDecl::DeclarationOnly;
1122 SemaRef.Diag(D->getLocation(),
1123 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1124 << D;
1125 }
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001126 return true;
1127 }
1128 return false;
1129}
1130
Alexey Bataeve3727102018-04-18 15:57:46 +00001131const DSAStackTy::DSAVarData DSAStackTy::getTopDSA(ValueDecl *D,
1132 bool FromParent) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001133 D = getCanonicalDecl(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001134 DSAVarData DVar;
1135
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001136 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001137 auto TI = Threadprivates.find(D);
1138 if (TI != Threadprivates.end()) {
1139 DVar.RefExpr = TI->getSecond().RefExpr.getPointer();
Alexey Bataev758e55e2013-09-06 18:03:48 +00001140 DVar.CKind = OMPC_threadprivate;
1141 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001142 }
1143 if (VD && VD->hasAttr<OMPThreadPrivateDeclAttr>()) {
Alexey Bataev817d7f32017-11-14 21:01:01 +00001144 DVar.RefExpr = buildDeclRefExpr(
1145 SemaRef, VD, D->getType().getNonReferenceType(),
1146 VD->getAttr<OMPThreadPrivateDeclAttr>()->getLocation());
1147 DVar.CKind = OMPC_threadprivate;
1148 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
Alexey Bataev852525d2018-03-02 17:17:12 +00001149 return DVar;
1150 }
1151 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1152 // in a Construct, C/C++, predetermined, p.1]
1153 // Variables appearing in threadprivate directives are threadprivate.
1154 if ((VD && VD->getTLSKind() != VarDecl::TLS_None &&
1155 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
1156 SemaRef.getLangOpts().OpenMPUseTLS &&
1157 SemaRef.getASTContext().getTargetInfo().isTLSSupported())) ||
1158 (VD && VD->getStorageClass() == SC_Register &&
1159 VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl())) {
1160 DVar.RefExpr = buildDeclRefExpr(
1161 SemaRef, VD, D->getType().getNonReferenceType(), D->getLocation());
1162 DVar.CKind = OMPC_threadprivate;
1163 addDSA(D, DVar.RefExpr, OMPC_threadprivate);
1164 return DVar;
1165 }
1166 if (SemaRef.getLangOpts().OpenMPCUDAMode && VD &&
1167 VD->isLocalVarDeclOrParm() && !isStackEmpty() &&
1168 !isLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001169 iterator IterTarget =
Alexey Bataev852525d2018-03-02 17:17:12 +00001170 std::find_if(Stack.back().first.rbegin(), Stack.back().first.rend(),
1171 [](const SharingMapTy &Data) {
1172 return isOpenMPTargetExecutionDirective(Data.Directive);
1173 });
1174 if (IterTarget != Stack.back().first.rend()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001175 iterator ParentIterTarget = std::next(IterTarget, 1);
1176 for (iterator Iter = Stack.back().first.rbegin();
1177 Iter != ParentIterTarget; std::advance(Iter, 1)) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001178 if (isOpenMPLocal(VD, Iter)) {
1179 DVar.RefExpr =
1180 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1181 D->getLocation());
1182 DVar.CKind = OMPC_threadprivate;
1183 return DVar;
1184 }
Alexey Bataev852525d2018-03-02 17:17:12 +00001185 }
1186 if (!isClauseParsingMode() || IterTarget != Stack.back().first.rbegin()) {
1187 auto DSAIter = IterTarget->SharingMap.find(D);
1188 if (DSAIter != IterTarget->SharingMap.end() &&
1189 isOpenMPPrivate(DSAIter->getSecond().Attributes)) {
1190 DVar.RefExpr = DSAIter->getSecond().RefExpr.getPointer();
1191 DVar.CKind = OMPC_threadprivate;
1192 return DVar;
Alexey Bataeve3727102018-04-18 15:57:46 +00001193 }
1194 iterator End = Stack.back().first.rend();
1195 if (!SemaRef.isOpenMPCapturedByRef(
1196 D, std::distance(ParentIterTarget, End))) {
Alexey Bataev852525d2018-03-02 17:17:12 +00001197 DVar.RefExpr =
1198 buildDeclRefExpr(SemaRef, VD, D->getType().getNonReferenceType(),
1199 IterTarget->ConstructLoc);
1200 DVar.CKind = OMPC_threadprivate;
1201 return DVar;
1202 }
1203 }
1204 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001205 }
1206
Alexey Bataev4b465392017-04-26 15:06:24 +00001207 if (isStackEmpty())
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001208 // Not in OpenMP execution region and top scope was already checked.
1209 return DVar;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00001210
Alexey Bataev758e55e2013-09-06 18:03:48 +00001211 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001212 // in a Construct, C/C++, predetermined, p.4]
1213 // Static data members are shared.
1214 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1215 // in a Construct, C/C++, predetermined, p.7]
1216 // Variables with static storage duration that are declared in a scope
1217 // inside the construct are shared.
Alexey Bataeve3727102018-04-18 15:57:46 +00001218 auto &&MatchesAlways = [](OpenMPDirectiveKind) { return true; };
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001219 if (VD && VD->isStaticDataMember()) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001220 DSAVarData DVarTemp = hasDSA(D, isOpenMPPrivate, MatchesAlways, FromParent);
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001221 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
Alexey Bataevec3da872014-01-31 05:15:34 +00001222 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001223
Alexey Bataevdffa93a2015-12-10 08:20:58 +00001224 DVar.CKind = OMPC_shared;
1225 return DVar;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001226 }
1227
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001228 // The predetermined shared attribute for const-qualified types having no
1229 // mutable members was removed after OpenMP 3.1.
1230 if (SemaRef.LangOpts.OpenMP <= 31) {
1231 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1232 // in a Construct, C/C++, predetermined, p.6]
1233 // Variables with const qualified type having no mutable member are
1234 // shared.
Joel E. Dennyd2649292019-01-04 22:11:56 +00001235 if (isConstNotMutableType(SemaRef, D->getType())) {
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001236 // Variables with const-qualified type having no mutable member may be
1237 // listed in a firstprivate clause, even if they are static data members.
1238 DSAVarData DVarTemp = hasInnermostDSA(
1239 D,
1240 [](OpenMPClauseKind C) {
1241 return C == OMPC_firstprivate || C == OMPC_shared;
1242 },
1243 MatchesAlways, FromParent);
1244 if (DVarTemp.CKind != OMPC_unknown && DVarTemp.RefExpr)
1245 return DVarTemp;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001246
Joel E. Dennye6234d1422019-01-04 22:11:31 +00001247 DVar.CKind = OMPC_shared;
1248 return DVar;
1249 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001250 }
1251
Alexey Bataev758e55e2013-09-06 18:03:48 +00001252 // Explicitly specified attributes and local variables with predetermined
1253 // attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00001254 iterator I = Stack.back().first.rbegin();
1255 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001256 if (FromParent && I != EndI)
1257 std::advance(I, 1);
Alexey Bataeve3727102018-04-18 15:57:46 +00001258 auto It = I->SharingMap.find(D);
1259 if (It != I->SharingMap.end()) {
1260 const DSAInfo &Data = It->getSecond();
1261 DVar.RefExpr = Data.RefExpr.getPointer();
1262 DVar.PrivateCopy = Data.PrivateCopy;
1263 DVar.CKind = Data.Attributes;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001264 DVar.ImplicitDSALoc = I->DefaultAttrLoc;
Alexey Bataev4d4624c2017-07-20 16:47:47 +00001265 DVar.DKind = I->Directive;
Alexey Bataev758e55e2013-09-06 18:03:48 +00001266 }
1267
1268 return DVar;
1269}
1270
Alexey Bataeve3727102018-04-18 15:57:46 +00001271const DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(ValueDecl *D,
1272 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001273 if (isStackEmpty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00001274 iterator I;
Alexey Bataev4b465392017-04-26 15:06:24 +00001275 return getDSA(I, D);
1276 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001277 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001278 iterator StartI = Stack.back().first.rbegin();
1279 iterator EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001280 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001281 std::advance(StartI, 1);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00001282 return getDSA(StartI, D);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001283}
1284
Alexey Bataeve3727102018-04-18 15:57:46 +00001285const DSAStackTy::DSAVarData
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001286DSAStackTy::hasDSA(ValueDecl *D,
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001287 const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1288 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001289 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001290 if (isStackEmpty())
1291 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001292 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001293 iterator I = Stack.back().first.rbegin();
1294 iterator EndI = Stack.back().first.rend();
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001295 if (FromParent && I != EndI)
Alexey Bataev0e6fc1c2017-04-27 14:46:26 +00001296 std::advance(I, 1);
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001297 for (; I != EndI; std::advance(I, 1)) {
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001298 if (!DPred(I->Directive) && !isImplicitOrExplicitTaskingRegion(I->Directive))
Alexey Bataeved09d242014-05-28 05:53:51 +00001299 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00001300 iterator NewI = I;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001301 DSAVarData DVar = getDSA(NewI, D);
1302 if (I == NewI && CPred(DVar.CKind))
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001303 return DVar;
Alexey Bataev60859c02017-04-27 15:10:33 +00001304 }
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001305 return {};
Alexey Bataevd5af8e42013-10-01 05:32:34 +00001306}
1307
Alexey Bataeve3727102018-04-18 15:57:46 +00001308const DSAStackTy::DSAVarData DSAStackTy::hasInnermostDSA(
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001309 ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1310 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001311 bool FromParent) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001312 if (isStackEmpty())
1313 return {};
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001314 D = getCanonicalDecl(D);
Alexey Bataeve3727102018-04-18 15:57:46 +00001315 iterator StartI = Stack.back().first.rbegin();
1316 iterator EndI = Stack.back().first.rend();
Alexey Bataeve3978122016-07-19 05:06:39 +00001317 if (FromParent && StartI != EndI)
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001318 std::advance(StartI, 1);
Alexey Bataeve3978122016-07-19 05:06:39 +00001319 if (StartI == EndI || !DPred(StartI->Directive))
Alexey Bataev4b465392017-04-26 15:06:24 +00001320 return {};
Alexey Bataeve3727102018-04-18 15:57:46 +00001321 iterator NewI = StartI;
Alexey Bataeveffbdf12017-07-21 17:24:30 +00001322 DSAVarData DVar = getDSA(NewI, D);
1323 return (NewI == StartI && CPred(DVar.CKind)) ? DVar : DSAVarData();
Alexey Bataevc5e02582014-06-16 07:08:35 +00001324}
1325
Alexey Bataevaac108a2015-06-23 04:51:00 +00001326bool DSAStackTy::hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001327 const ValueDecl *D, const llvm::function_ref<bool(OpenMPClauseKind)> CPred,
1328 unsigned Level, bool NotLastprivate) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001329 if (isStackEmpty())
1330 return false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001331 D = getCanonicalDecl(D);
Alexey Bataev4b465392017-04-26 15:06:24 +00001332 auto StartI = Stack.back().first.begin();
1333 auto EndI = Stack.back().first.end();
NAKAMURA Takumi0332eda2015-06-23 10:01:20 +00001334 if (std::distance(StartI, EndI) <= (int)Level)
Alexey Bataevaac108a2015-06-23 04:51:00 +00001335 return false;
1336 std::advance(StartI, Level);
Alexey Bataeve3727102018-04-18 15:57:46 +00001337 auto I = StartI->SharingMap.find(D);
Alexey Bataev92b33652018-11-21 19:41:10 +00001338 if ((I != StartI->SharingMap.end()) &&
Alexey Bataeve3727102018-04-18 15:57:46 +00001339 I->getSecond().RefExpr.getPointer() &&
1340 CPred(I->getSecond().Attributes) &&
Alexey Bataev92b33652018-11-21 19:41:10 +00001341 (!NotLastprivate || !I->getSecond().RefExpr.getInt()))
1342 return true;
1343 // Check predetermined rules for the loop control variables.
1344 auto LI = StartI->LCVMap.find(D);
1345 if (LI != StartI->LCVMap.end())
1346 return CPred(OMPC_private);
1347 return false;
Alexey Bataevaac108a2015-06-23 04:51:00 +00001348}
1349
Samuel Antao4be30e92015-10-02 17:14:03 +00001350bool DSAStackTy::hasExplicitDirective(
Alexey Bataeve3727102018-04-18 15:57:46 +00001351 const llvm::function_ref<bool(OpenMPDirectiveKind)> DPred,
1352 unsigned Level) const {
Alexey Bataev4b465392017-04-26 15:06:24 +00001353 if (isStackEmpty())
1354 return false;
1355 auto StartI = Stack.back().first.begin();
1356 auto EndI = Stack.back().first.end();
Samuel Antao4be30e92015-10-02 17:14:03 +00001357 if (std::distance(StartI, EndI) <= (int)Level)
1358 return false;
1359 std::advance(StartI, Level);
1360 return DPred(StartI->Directive);
1361}
1362
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001363bool DSAStackTy::hasDirective(
1364 const llvm::function_ref<bool(OpenMPDirectiveKind,
1365 const DeclarationNameInfo &, SourceLocation)>
Alexey Bataev97d18bf2018-04-11 19:21:00 +00001366 DPred,
Alexey Bataeve3727102018-04-18 15:57:46 +00001367 bool FromParent) const {
Samuel Antaof0d79752016-05-27 15:21:27 +00001368 // We look only in the enclosing region.
Alexey Bataev4b465392017-04-26 15:06:24 +00001369 if (isStackEmpty())
Samuel Antaof0d79752016-05-27 15:21:27 +00001370 return false;
Alexey Bataev4b465392017-04-26 15:06:24 +00001371 auto StartI = std::next(Stack.back().first.rbegin());
1372 auto EndI = Stack.back().first.rend();
Alexey Bataevccaddfb2017-04-26 14:24:21 +00001373 if (FromParent && StartI != EndI)
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001374 StartI = std::next(StartI);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00001375 for (auto I = StartI, EE = EndI; I != EE; ++I) {
1376 if (DPred(I->Directive, I->DirectiveName, I->ConstructLoc))
1377 return true;
1378 }
1379 return false;
1380}
1381
Alexey Bataev758e55e2013-09-06 18:03:48 +00001382void Sema::InitDataSharingAttributesStack() {
1383 VarDataSharingAttributesStack = new DSAStackTy(*this);
1384}
1385
1386#define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
1387
Alexey Bataev4b465392017-04-26 15:06:24 +00001388void Sema::pushOpenMPFunctionRegion() {
1389 DSAStack->pushFunction();
1390}
1391
1392void Sema::popOpenMPFunctionRegion(const FunctionScopeInfo *OldFSI) {
1393 DSAStack->popFunction(OldFSI);
1394}
1395
Alexey Bataeve3727102018-04-18 15:57:46 +00001396bool Sema::isOpenMPCapturedByRef(const ValueDecl *D, unsigned Level) const {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001397 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1398
Alexey Bataeve3727102018-04-18 15:57:46 +00001399 ASTContext &Ctx = getASTContext();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001400 bool IsByRef = true;
1401
1402 // Find the directive that is associated with the provided scope.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00001403 D = cast<ValueDecl>(D->getCanonicalDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001404 QualType Ty = D->getType();
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001405
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001406 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective, Level)) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001407 // This table summarizes how a given variable should be passed to the device
1408 // given its type and the clauses where it appears. This table is based on
1409 // the description in OpenMP 4.5 [2.10.4, target Construct] and
1410 // OpenMP 4.5 [2.15.5, Data-mapping Attribute Rules and Clauses].
1411 //
1412 // =========================================================================
1413 // | type | defaultmap | pvt | first | is_device_ptr | map | res. |
1414 // | |(tofrom:scalar)| | pvt | | | |
1415 // =========================================================================
1416 // | scl | | | | - | | bycopy|
1417 // | scl | | - | x | - | - | bycopy|
1418 // | scl | | x | - | - | - | null |
1419 // | scl | x | | | - | | byref |
1420 // | scl | x | - | x | - | - | bycopy|
1421 // | scl | x | x | - | - | - | null |
1422 // | scl | | - | - | - | x | byref |
1423 // | scl | x | - | - | - | x | byref |
1424 //
1425 // | agg | n.a. | | | - | | byref |
1426 // | agg | n.a. | - | x | - | - | byref |
1427 // | agg | n.a. | x | - | - | - | null |
1428 // | agg | n.a. | - | - | - | x | byref |
1429 // | agg | n.a. | - | - | - | x[] | byref |
1430 //
1431 // | ptr | n.a. | | | - | | bycopy|
1432 // | ptr | n.a. | - | x | - | - | bycopy|
1433 // | ptr | n.a. | x | - | - | - | null |
1434 // | ptr | n.a. | - | - | - | x | byref |
1435 // | ptr | n.a. | - | - | - | x[] | bycopy|
1436 // | ptr | n.a. | - | - | x | | bycopy|
1437 // | ptr | n.a. | - | - | x | x | bycopy|
1438 // | ptr | n.a. | - | - | x | x[] | bycopy|
1439 // =========================================================================
1440 // Legend:
1441 // scl - scalar
1442 // ptr - pointer
1443 // agg - aggregate
1444 // x - applies
1445 // - - invalid in this combination
1446 // [] - mapped with an array section
1447 // byref - should be mapped by reference
1448 // byval - should be mapped by value
1449 // null - initialize a local variable to null on the device
1450 //
1451 // Observations:
1452 // - All scalar declarations that show up in a map clause have to be passed
1453 // by reference, because they may have been mapped in the enclosing data
1454 // environment.
1455 // - If the scalar value does not fit the size of uintptr, it has to be
1456 // passed by reference, regardless the result in the table above.
1457 // - For pointers mapped by value that have either an implicit map or an
1458 // array section, the runtime library may pass the NULL value to the
1459 // device instead of the value passed to it by the compiler.
1460
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001461 if (Ty->isReferenceType())
1462 Ty = Ty->castAs<ReferenceType>()->getPointeeType();
Samuel Antao86ace552016-04-27 22:40:57 +00001463
1464 // Locate map clauses and see if the variable being captured is referred to
1465 // in any of those clauses. Here we only care about variables, not fields,
1466 // because fields are part of aggregates.
1467 bool IsVariableUsedInMapClause = false;
1468 bool IsVariableAssociatedWithSection = false;
1469
Jonas Hahnfeldf7c4d7b2017-07-01 10:40:50 +00001470 DSAStack->checkMappableExprComponentListsForDeclAtLevel(
Alexey Bataeve3727102018-04-18 15:57:46 +00001471 D, Level,
1472 [&IsVariableUsedInMapClause, &IsVariableAssociatedWithSection, D](
1473 OMPClauseMappableExprCommon::MappableExprComponentListRef
Samuel Antao6890b092016-07-28 14:25:09 +00001474 MapExprComponents,
1475 OpenMPClauseKind WhereFoundClauseKind) {
1476 // Only the map clause information influences how a variable is
1477 // captured. E.g. is_device_ptr does not require changing the default
Samuel Antao4c8035b2016-12-12 18:00:20 +00001478 // behavior.
Samuel Antao6890b092016-07-28 14:25:09 +00001479 if (WhereFoundClauseKind != OMPC_map)
1480 return false;
Samuel Antao86ace552016-04-27 22:40:57 +00001481
1482 auto EI = MapExprComponents.rbegin();
1483 auto EE = MapExprComponents.rend();
1484
1485 assert(EI != EE && "Invalid map expression!");
1486
1487 if (isa<DeclRefExpr>(EI->getAssociatedExpression()))
1488 IsVariableUsedInMapClause |= EI->getAssociatedDeclaration() == D;
1489
1490 ++EI;
1491 if (EI == EE)
1492 return false;
1493
1494 if (isa<ArraySubscriptExpr>(EI->getAssociatedExpression()) ||
1495 isa<OMPArraySectionExpr>(EI->getAssociatedExpression()) ||
1496 isa<MemberExpr>(EI->getAssociatedExpression())) {
1497 IsVariableAssociatedWithSection = true;
1498 // There is nothing more we need to know about this variable.
1499 return true;
1500 }
1501
1502 // Keep looking for more map info.
1503 return false;
1504 });
1505
1506 if (IsVariableUsedInMapClause) {
1507 // If variable is identified in a map clause it is always captured by
1508 // reference except if it is a pointer that is dereferenced somehow.
1509 IsByRef = !(Ty->isPointerType() && IsVariableAssociatedWithSection);
1510 } else {
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001511 // By default, all the data that has a scalar type is mapped by copy
1512 // (except for reduction variables).
1513 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001514 (DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1515 !Ty->isAnyPointerType()) ||
Alexey Bataev3f96fe62017-12-13 17:31:39 +00001516 !Ty->isScalarType() ||
1517 DSAStack->getDefaultDMAAtLevel(Level) == DMA_tofrom_scalar ||
1518 DSAStack->hasExplicitDSA(
1519 D, [](OpenMPClauseKind K) { return K == OMPC_reduction; }, Level);
Samuel Antao86ace552016-04-27 22:40:57 +00001520 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001521 }
1522
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001523 if (IsByRef && Ty.getNonReferenceType()->isScalarType()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001524 IsByRef =
Alexey Bataev60705422018-10-30 15:50:12 +00001525 ((DSAStack->isForceCaptureByReferenceInTargetExecutable() &&
1526 !Ty->isAnyPointerType()) ||
1527 !DSAStack->hasExplicitDSA(
1528 D,
1529 [](OpenMPClauseKind K) -> bool { return K == OMPC_firstprivate; },
1530 Level, /*NotLastprivate=*/true)) &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00001531 // If the variable is artificial and must be captured by value - try to
1532 // capture by value.
Alexey Bataevd2202ca2017-12-27 17:58:32 +00001533 !(isa<OMPCapturedExprDecl>(D) && !D->hasAttr<OMPCaptureNoInitAttr>() &&
1534 !cast<OMPCapturedExprDecl>(D)->getInit()->isGLValue());
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001535 }
1536
Samuel Antao86ace552016-04-27 22:40:57 +00001537 // When passing data by copy, we need to make sure it fits the uintptr size
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001538 // and alignment, because the runtime library only deals with uintptr types.
1539 // If it does not fit the uintptr size, we need to pass the data by reference
1540 // instead.
1541 if (!IsByRef &&
1542 (Ctx.getTypeSizeInChars(Ty) >
1543 Ctx.getTypeSizeInChars(Ctx.getUIntPtrType()) ||
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001544 Ctx.getDeclAlign(D) > Ctx.getTypeAlignInChars(Ctx.getUIntPtrType()))) {
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001545 IsByRef = true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001546 }
Samuel Antao4af1b7b2015-12-02 17:44:43 +00001547
1548 return IsByRef;
1549}
1550
Alexey Bataev7ace49d2016-05-17 08:55:33 +00001551unsigned Sema::getOpenMPNestingLevel() const {
1552 assert(getLangOpts().OpenMP);
1553 return DSAStack->getNestingLevel();
1554}
1555
Jonas Hahnfeld87d44262017-11-18 21:00:46 +00001556bool Sema::isInOpenMPTargetExecutionDirective() const {
1557 return (isOpenMPTargetExecutionDirective(DSAStack->getCurrentDirective()) &&
1558 !DSAStack->isClauseParsingMode()) ||
1559 DSAStack->hasDirective(
1560 [](OpenMPDirectiveKind K, const DeclarationNameInfo &,
1561 SourceLocation) -> bool {
1562 return isOpenMPTargetExecutionDirective(K);
1563 },
1564 false);
1565}
1566
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001567VarDecl *Sema::isOpenMPCapturedDecl(ValueDecl *D) {
Alexey Bataevf841bd92014-12-16 07:00:22 +00001568 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001569 D = getCanonicalDecl(D);
Samuel Antao4be30e92015-10-02 17:14:03 +00001570
1571 // If we are attempting to capture a global variable in a directive with
1572 // 'target' we return true so that this global is also mapped to the device.
1573 //
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001574 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001575 if (VD && !VD->hasLocalStorage()) {
1576 if (isInOpenMPDeclareTargetContext() &&
1577 (getCurCapturedRegion() || getCurBlock() || getCurLambda())) {
1578 // Try to mark variable as declare target if it is used in capturing
1579 // regions.
Alexey Bataev97b72212018-08-14 18:31:20 +00001580 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001581 checkDeclIsAllowedInOpenMPTarget(nullptr, VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00001582 return nullptr;
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001583 } else if (isInOpenMPTargetExecutionDirective()) {
1584 // If the declaration is enclosed in a 'declare target' directive,
1585 // then it should not be captured.
1586 //
Alexey Bataev97b72212018-08-14 18:31:20 +00001587 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataevbf8fe712018-08-07 16:14:36 +00001588 return nullptr;
1589 return VD;
1590 }
Alexey Bataev4f4bf7c2018-03-15 15:47:20 +00001591 }
Alexey Bataev60705422018-10-30 15:50:12 +00001592 // Capture variables captured by reference in lambdas for target-based
1593 // directives.
1594 if (VD && !DSAStack->isClauseParsingMode()) {
1595 if (const auto *RD = VD->getType()
1596 .getCanonicalType()
1597 .getNonReferenceType()
1598 ->getAsCXXRecordDecl()) {
1599 bool SavedForceCaptureByReferenceInTargetExecutable =
1600 DSAStack->isForceCaptureByReferenceInTargetExecutable();
1601 DSAStack->setForceCaptureByReferenceInTargetExecutable(/*V=*/true);
Alexey Bataevd1840e52018-11-16 21:13:33 +00001602 if (RD->isLambda()) {
1603 llvm::DenseMap<const VarDecl *, FieldDecl *> Captures;
1604 FieldDecl *ThisCapture;
1605 RD->getCaptureFields(Captures, ThisCapture);
Alexey Bataev60705422018-10-30 15:50:12 +00001606 for (const LambdaCapture &LC : RD->captures()) {
1607 if (LC.getCaptureKind() == LCK_ByRef) {
1608 VarDecl *VD = LC.getCapturedVar();
1609 DeclContext *VDC = VD->getDeclContext();
1610 if (!VDC->Encloses(CurContext))
1611 continue;
1612 DSAStackTy::DSAVarData DVarPrivate =
1613 DSAStack->getTopDSA(VD, /*FromParent=*/false);
1614 // Do not capture already captured variables.
1615 if (!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD) &&
1616 DVarPrivate.CKind == OMPC_unknown &&
1617 !DSAStack->checkMappableExprComponentListsForDecl(
1618 D, /*CurrentRegionOnly=*/true,
1619 [](OMPClauseMappableExprCommon::
1620 MappableExprComponentListRef,
1621 OpenMPClauseKind) { return true; }))
1622 MarkVariableReferenced(LC.getLocation(), LC.getCapturedVar());
1623 } else if (LC.getCaptureKind() == LCK_This) {
Alexey Bataevd1840e52018-11-16 21:13:33 +00001624 QualType ThisTy = getCurrentThisType();
1625 if (!ThisTy.isNull() &&
1626 Context.typesAreCompatible(ThisTy, ThisCapture->getType()))
1627 CheckCXXThisCapture(LC.getLocation());
Alexey Bataev60705422018-10-30 15:50:12 +00001628 }
1629 }
Alexey Bataevd1840e52018-11-16 21:13:33 +00001630 }
Alexey Bataev60705422018-10-30 15:50:12 +00001631 DSAStack->setForceCaptureByReferenceInTargetExecutable(
1632 SavedForceCaptureByReferenceInTargetExecutable);
1633 }
1634 }
Samuel Antao4be30e92015-10-02 17:14:03 +00001635
Alexey Bataev48977c32015-08-04 08:10:48 +00001636 if (DSAStack->getCurrentDirective() != OMPD_unknown &&
1637 (!DSAStack->isClauseParsingMode() ||
1638 DSAStack->getParentDirective() != OMPD_unknown)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001639 auto &&Info = DSAStack->isLoopControlVariable(D);
1640 if (Info.first ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001641 (VD && VD->hasLocalStorage() &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00001642 isImplicitOrExplicitTaskingRegion(DSAStack->getCurrentDirective())) ||
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001643 (VD && DSAStack->isForceVarCapturing()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00001644 return VD ? VD : Info.second;
Alexey Bataeve3727102018-04-18 15:57:46 +00001645 DSAStackTy::DSAVarData DVarPrivate =
1646 DSAStack->getTopDSA(D, DSAStack->isClauseParsingMode());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001647 if (DVarPrivate.CKind != OMPC_unknown && isOpenMPPrivate(DVarPrivate.CKind))
Alexey Bataev90c228f2016-02-08 09:29:13 +00001648 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00001649 DVarPrivate = DSAStack->hasDSA(D, isOpenMPPrivate,
1650 [](OpenMPDirectiveKind) { return true; },
1651 DSAStack->isClauseParsingMode());
Alexey Bataev90c228f2016-02-08 09:29:13 +00001652 if (DVarPrivate.CKind != OMPC_unknown)
1653 return VD ? VD : cast<VarDecl>(DVarPrivate.PrivateCopy->getDecl());
Alexey Bataevf841bd92014-12-16 07:00:22 +00001654 }
Alexey Bataev90c228f2016-02-08 09:29:13 +00001655 return nullptr;
Alexey Bataevf841bd92014-12-16 07:00:22 +00001656}
1657
Alexey Bataevdfa430f2017-12-08 15:03:50 +00001658void Sema::adjustOpenMPTargetScopeIndex(unsigned &FunctionScopesIndex,
1659 unsigned Level) const {
1660 SmallVector<OpenMPDirectiveKind, 4> Regions;
1661 getOpenMPCaptureRegions(Regions, DSAStack->getDirective(Level));
1662 FunctionScopesIndex -= Regions.size();
1663}
1664
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001665void Sema::startOpenMPLoop() {
1666 assert(LangOpts.OpenMP && "OpenMP must be enabled.");
1667 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective()))
1668 DSAStack->loopInit();
1669}
1670
Alexey Bataeve3727102018-04-18 15:57:46 +00001671bool Sema::isOpenMPPrivateDecl(const ValueDecl *D, unsigned Level) const {
Alexey Bataevaac108a2015-06-23 04:51:00 +00001672 assert(LangOpts.OpenMP && "OpenMP is not allowed");
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00001673 if (isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
1674 if (DSAStack->getAssociatedLoops() > 0 &&
1675 !DSAStack->isLoopStarted()) {
1676 DSAStack->resetPossibleLoopCounter(D);
1677 DSAStack->loopStart();
1678 return true;
1679 }
1680 if ((DSAStack->getPossiblyLoopCunter() == D->getCanonicalDecl() ||
1681 DSAStack->isLoopControlVariable(D).first) &&
1682 !DSAStack->hasExplicitDSA(
1683 D, [](OpenMPClauseKind K) { return K != OMPC_private; }, Level) &&
1684 !isOpenMPSimdDirective(DSAStack->getCurrentDirective()))
1685 return true;
1686 }
Alexey Bataevaac108a2015-06-23 04:51:00 +00001687 return DSAStack->hasExplicitDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00001688 D, [](OpenMPClauseKind K) { return K == OMPC_private; }, Level) ||
Alexey Bataev3f82cfc2017-12-13 15:28:44 +00001689 (DSAStack->isClauseParsingMode() &&
1690 DSAStack->getClauseParsingMode() == OMPC_private) ||
Alexey Bataev88202be2017-07-27 13:20:36 +00001691 // Consider taskgroup reduction descriptor variable a private to avoid
1692 // possible capture in the region.
1693 (DSAStack->hasExplicitDirective(
1694 [](OpenMPDirectiveKind K) { return K == OMPD_taskgroup; },
1695 Level) &&
1696 DSAStack->isTaskgroupReductionRef(D, Level));
Alexey Bataevaac108a2015-06-23 04:51:00 +00001697}
1698
Alexey Bataeve3727102018-04-18 15:57:46 +00001699void Sema::setOpenMPCaptureKind(FieldDecl *FD, const ValueDecl *D,
1700 unsigned Level) {
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001701 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1702 D = getCanonicalDecl(D);
1703 OpenMPClauseKind OMPC = OMPC_unknown;
1704 for (unsigned I = DSAStack->getNestingLevel() + 1; I > Level; --I) {
1705 const unsigned NewLevel = I - 1;
1706 if (DSAStack->hasExplicitDSA(D,
1707 [&OMPC](const OpenMPClauseKind K) {
1708 if (isOpenMPPrivate(K)) {
1709 OMPC = K;
1710 return true;
1711 }
1712 return false;
1713 },
1714 NewLevel))
1715 break;
1716 if (DSAStack->checkMappableExprComponentListsForDeclAtLevel(
1717 D, NewLevel,
1718 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
1719 OpenMPClauseKind) { return true; })) {
1720 OMPC = OMPC_map;
1721 break;
1722 }
1723 if (DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1724 NewLevel)) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001725 OMPC = OMPC_map;
1726 if (D->getType()->isScalarType() &&
1727 DSAStack->getDefaultDMAAtLevel(NewLevel) !=
1728 DefaultMapAttributes::DMA_tofrom_scalar)
1729 OMPC = OMPC_firstprivate;
Alexey Bataev3b8d5582017-08-08 18:04:06 +00001730 break;
1731 }
1732 }
1733 if (OMPC != OMPC_unknown)
1734 FD->addAttr(OMPCaptureKindAttr::CreateImplicit(Context, OMPC));
1735}
1736
Alexey Bataeve3727102018-04-18 15:57:46 +00001737bool Sema::isOpenMPTargetCapturedDecl(const ValueDecl *D,
1738 unsigned Level) const {
Samuel Antao4be30e92015-10-02 17:14:03 +00001739 assert(LangOpts.OpenMP && "OpenMP is not allowed");
1740 // Return true if the current level is no longer enclosed in a target region.
1741
Alexey Bataeve3727102018-04-18 15:57:46 +00001742 const auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001743 return VD && !VD->hasLocalStorage() &&
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00001744 DSAStack->hasExplicitDirective(isOpenMPTargetExecutionDirective,
1745 Level);
Samuel Antao4be30e92015-10-02 17:14:03 +00001746}
1747
Alexey Bataeved09d242014-05-28 05:53:51 +00001748void Sema::DestroyDataSharingAttributesStack() { delete DSAStack; }
Alexey Bataev758e55e2013-09-06 18:03:48 +00001749
1750void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
1751 const DeclarationNameInfo &DirName,
Alexey Bataevbae9a792014-06-27 10:37:06 +00001752 Scope *CurScope, SourceLocation Loc) {
1753 DSAStack->push(DKind, DirName, CurScope, Loc);
Faisal Valid143a0c2017-04-01 21:30:49 +00001754 PushExpressionEvaluationContext(
1755 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev758e55e2013-09-06 18:03:48 +00001756}
1757
Alexey Bataevaac108a2015-06-23 04:51:00 +00001758void Sema::StartOpenMPClause(OpenMPClauseKind K) {
1759 DSAStack->setClauseParsingMode(K);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001760}
1761
Alexey Bataevaac108a2015-06-23 04:51:00 +00001762void Sema::EndOpenMPClause() {
1763 DSAStack->setClauseParsingMode(/*K=*/OMPC_unknown);
Alexey Bataev39f915b82015-05-08 10:41:21 +00001764}
1765
Alexey Bataev758e55e2013-09-06 18:03:48 +00001766void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
Alexey Bataevf29276e2014-06-18 04:14:57 +00001767 // OpenMP [2.14.3.5, Restrictions, C/C++, p.1]
1768 // A variable of class type (or array thereof) that appears in a lastprivate
1769 // clause requires an accessible, unambiguous default constructor for the
1770 // class type, unless the list item is also specified in a firstprivate
1771 // clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00001772 if (const auto *D = dyn_cast_or_null<OMPExecutableDirective>(CurDirective)) {
1773 for (OMPClause *C : D->clauses()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001774 if (auto *Clause = dyn_cast<OMPLastprivateClause>(C)) {
1775 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00001776 for (Expr *DE : Clause->varlists()) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001777 if (DE->isValueDependent() || DE->isTypeDependent()) {
1778 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001779 continue;
Alexey Bataev38e89532015-04-16 04:54:05 +00001780 }
Alexey Bataev74caaf22016-02-20 04:09:36 +00001781 auto *DRE = cast<DeclRefExpr>(DE->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +00001782 auto *VD = cast<VarDecl>(DRE->getDecl());
Alexey Bataev005248a2016-02-25 05:25:57 +00001783 QualType Type = VD->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +00001784 const DSAStackTy::DSAVarData DVar =
1785 DSAStack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001786 if (DVar.CKind == OMPC_lastprivate) {
Alexey Bataev38e89532015-04-16 04:54:05 +00001787 // Generate helper private variable and initialize it with the
1788 // default value. The address of the original variable is replaced
1789 // by the address of the new private variable in CodeGen. This new
1790 // variable is not added to IdResolver, so the code in the OpenMP
1791 // region uses original variable for proper diagnostics.
Alexey Bataeve3727102018-04-18 15:57:46 +00001792 VarDecl *VDPrivate = buildVarDecl(
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +00001793 *this, DE->getExprLoc(), Type.getUnqualifiedType(),
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001794 VD->getName(), VD->hasAttrs() ? &VD->getAttrs() : nullptr, DRE);
Richard Smith3beb7c62017-01-12 02:27:38 +00001795 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev38e89532015-04-16 04:54:05 +00001796 if (VDPrivate->isInvalidDecl())
1797 continue;
Alexey Bataev39f915b82015-05-08 10:41:21 +00001798 PrivateCopies.push_back(buildDeclRefExpr(
1799 *this, VDPrivate, DE->getType(), DE->getExprLoc()));
Alexey Bataev38e89532015-04-16 04:54:05 +00001800 } else {
1801 // The variable is also a firstprivate, so initialization sequence
1802 // for private copy is generated already.
1803 PrivateCopies.push_back(nullptr);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001804 }
1805 }
Alexey Bataev38e89532015-04-16 04:54:05 +00001806 // Set initializers to private copies if no errors were found.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00001807 if (PrivateCopies.size() == Clause->varlist_size())
Alexey Bataev38e89532015-04-16 04:54:05 +00001808 Clause->setPrivateCopies(PrivateCopies);
Alexey Bataevf29276e2014-06-18 04:14:57 +00001809 }
1810 }
1811 }
1812
Alexey Bataev758e55e2013-09-06 18:03:48 +00001813 DSAStack->pop();
1814 DiscardCleanupsInEvaluationContext();
1815 PopExpressionEvaluationContext();
1816}
1817
Alexey Bataev5dff95c2016-04-22 03:56:56 +00001818static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
1819 Expr *NumIterations, Sema &SemaRef,
1820 Scope *S, DSAStackTy *Stack);
Alexander Musman3276a272015-03-21 10:12:56 +00001821
Alexey Bataeva769e072013-03-22 06:34:35 +00001822namespace {
1823
Alexey Bataeve3727102018-04-18 15:57:46 +00001824class VarDeclFilterCCC final : public CorrectionCandidateCallback {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001825private:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001826 Sema &SemaRef;
Alexey Bataeved09d242014-05-28 05:53:51 +00001827
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001828public:
Alexey Bataev7ff55242014-06-19 09:13:45 +00001829 explicit VarDeclFilterCCC(Sema &S) : SemaRef(S) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00001830 bool ValidateCandidate(const TypoCorrection &Candidate) override {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001831 NamedDecl *ND = Candidate.getCorrectionDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +00001832 if (const auto *VD = dyn_cast_or_null<VarDecl>(ND)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001833 return VD->hasGlobalStorage() &&
Alexey Bataev7ff55242014-06-19 09:13:45 +00001834 SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1835 SemaRef.getCurScope());
Alexey Bataeva769e072013-03-22 06:34:35 +00001836 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001837 return false;
Alexey Bataeva769e072013-03-22 06:34:35 +00001838 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001839};
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001840
Alexey Bataeve3727102018-04-18 15:57:46 +00001841class VarOrFuncDeclFilterCCC final : public CorrectionCandidateCallback {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001842private:
1843 Sema &SemaRef;
1844
1845public:
1846 explicit VarOrFuncDeclFilterCCC(Sema &S) : SemaRef(S) {}
1847 bool ValidateCandidate(const TypoCorrection &Candidate) override {
1848 NamedDecl *ND = Candidate.getCorrectionDecl();
Kelvin Li59e3d192017-11-30 18:52:06 +00001849 if (ND && (isa<VarDecl>(ND) || isa<FunctionDecl>(ND))) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +00001850 return SemaRef.isDeclInScope(ND, SemaRef.getCurLexicalContext(),
1851 SemaRef.getCurScope());
1852 }
1853 return false;
1854 }
1855};
1856
Alexey Bataeved09d242014-05-28 05:53:51 +00001857} // namespace
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001858
1859ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
1860 CXXScopeSpec &ScopeSpec,
1861 const DeclarationNameInfo &Id) {
1862 LookupResult Lookup(*this, Id, LookupOrdinaryName);
1863 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
1864
1865 if (Lookup.isAmbiguous())
1866 return ExprError();
1867
1868 VarDecl *VD;
1869 if (!Lookup.isSingleResult()) {
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001870 if (TypoCorrection Corrected = CorrectTypo(
1871 Id, LookupOrdinaryName, CurScope, nullptr,
1872 llvm::make_unique<VarDeclFilterCCC>(*this), CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +00001873 diagnoseTypo(Corrected,
Alexander Musmancb7f9c42014-05-15 13:04:49 +00001874 PDiag(Lookup.empty()
1875 ? diag::err_undeclared_var_use_suggest
1876 : diag::err_omp_expected_var_arg_suggest)
1877 << Id.getName());
Richard Smithf9b15102013-08-17 00:46:16 +00001878 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001879 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001880 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
1881 : diag::err_omp_expected_var_arg)
1882 << Id.getName();
1883 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001884 }
Alexey Bataeve3727102018-04-18 15:57:46 +00001885 } else if (!(VD = Lookup.getAsSingle<VarDecl>())) {
1886 Diag(Id.getLoc(), diag::err_omp_expected_var_arg) << Id.getName();
1887 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
1888 return ExprError();
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001889 }
1890 Lookup.suppressDiagnostics();
1891
1892 // OpenMP [2.9.2, Syntax, C/C++]
1893 // Variables must be file-scope, namespace-scope, or static block-scope.
1894 if (!VD->hasGlobalStorage()) {
1895 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00001896 << getOpenMPDirectiveName(OMPD_threadprivate) << !VD->isStaticLocal();
1897 bool IsDecl =
1898 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001899 Diag(VD->getLocation(),
Alexey Bataeved09d242014-05-28 05:53:51 +00001900 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1901 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001902 return ExprError();
1903 }
1904
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001905 VarDecl *CanonicalVD = VD->getCanonicalDecl();
George Burgess IV00f70bd2018-03-01 05:43:23 +00001906 NamedDecl *ND = CanonicalVD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001907 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
1908 // A threadprivate directive for file-scope variables must appear outside
1909 // any definition or declaration.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001910 if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
1911 !getCurLexicalContext()->isTranslationUnit()) {
1912 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001913 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1914 bool IsDecl =
1915 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1916 Diag(VD->getLocation(),
1917 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1918 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001919 return ExprError();
1920 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001921 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
1922 // A threadprivate directive for static class member variables must appear
1923 // in the class definition, in the same scope in which the member
1924 // variables are declared.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001925 if (CanonicalVD->isStaticDataMember() &&
1926 !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
1927 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001928 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1929 bool IsDecl =
1930 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1931 Diag(VD->getLocation(),
1932 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1933 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001934 return ExprError();
1935 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001936 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
1937 // A threadprivate directive for namespace-scope variables must appear
1938 // outside any definition or declaration other than the namespace
1939 // definition itself.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001940 if (CanonicalVD->getDeclContext()->isNamespace() &&
1941 (!getCurLexicalContext()->isFileContext() ||
1942 !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
1943 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001944 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1945 bool IsDecl =
1946 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1947 Diag(VD->getLocation(),
1948 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1949 << VD;
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001950 return ExprError();
1951 }
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001952 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
1953 // A threadprivate directive for static block-scope variables must appear
1954 // in the scope of the variable and not in a nested scope.
Alexey Bataev7d2960b2013-09-26 03:24:06 +00001955 if (CanonicalVD->isStaticLocal() && CurScope &&
1956 !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001957 Diag(Id.getLoc(), diag::err_omp_var_scope)
Alexey Bataeved09d242014-05-28 05:53:51 +00001958 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
1959 bool IsDecl =
1960 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
1961 Diag(VD->getLocation(),
1962 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
1963 << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001964 return ExprError();
1965 }
1966
1967 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
1968 // A threadprivate directive must lexically precede all references to any
1969 // of the variables in its list.
Alexey Bataev6ddfe1a2015-04-16 13:49:42 +00001970 if (VD->isUsed() && !DSAStack->isThreadPrivate(VD)) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001971 Diag(Id.getLoc(), diag::err_omp_var_used)
Alexey Bataeved09d242014-05-28 05:53:51 +00001972 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001973 return ExprError();
1974 }
1975
1976 QualType ExprType = VD->getType().getNonReferenceType();
Alexey Bataev376b4a42016-02-09 09:41:09 +00001977 return DeclRefExpr::Create(Context, NestedNameSpecifierLoc(),
1978 SourceLocation(), VD,
1979 /*RefersToEnclosingVariableOrCapture=*/false,
1980 Id.getLoc(), ExprType, VK_LValue);
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001981}
1982
Alexey Bataeved09d242014-05-28 05:53:51 +00001983Sema::DeclGroupPtrTy
1984Sema::ActOnOpenMPThreadprivateDirective(SourceLocation Loc,
1985 ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00001986 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00001987 CurContext->addDecl(D);
1988 return DeclGroupPtrTy::make(DeclGroupRef(D));
1989 }
David Blaikie0403cb12016-01-15 23:43:25 +00001990 return nullptr;
Alexey Bataeva769e072013-03-22 06:34:35 +00001991}
1992
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001993namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00001994class LocalVarRefChecker final
1995 : public ConstStmtVisitor<LocalVarRefChecker, bool> {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00001996 Sema &SemaRef;
1997
1998public:
1999 bool VisitDeclRefExpr(const DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002000 if (const auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002001 if (VD->hasLocalStorage()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002002 SemaRef.Diag(E->getBeginLoc(),
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002003 diag::err_omp_local_var_in_threadprivate_init)
2004 << E->getSourceRange();
2005 SemaRef.Diag(VD->getLocation(), diag::note_defined_here)
2006 << VD << VD->getSourceRange();
2007 return true;
2008 }
2009 }
2010 return false;
2011 }
2012 bool VisitStmt(const Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002013 for (const Stmt *Child : S->children()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002014 if (Child && Visit(Child))
2015 return true;
2016 }
2017 return false;
2018 }
Alexey Bataev23b69422014-06-18 07:08:49 +00002019 explicit LocalVarRefChecker(Sema &SemaRef) : SemaRef(SemaRef) {}
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002020};
2021} // namespace
2022
Alexey Bataeved09d242014-05-28 05:53:51 +00002023OMPThreadPrivateDecl *
2024Sema::CheckOMPThreadPrivateDecl(SourceLocation Loc, ArrayRef<Expr *> VarList) {
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002025 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +00002026 for (Expr *RefExpr : VarList) {
2027 auto *DE = cast<DeclRefExpr>(RefExpr);
2028 auto *VD = cast<VarDecl>(DE->getDecl());
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002029 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataeva769e072013-03-22 06:34:35 +00002030
Alexey Bataev376b4a42016-02-09 09:41:09 +00002031 // Mark variable as used.
2032 VD->setReferenced();
2033 VD->markUsed(Context);
2034
Alexey Bataevf56f98c2015-04-16 05:39:01 +00002035 QualType QType = VD->getType();
2036 if (QType->isDependentType() || QType->isInstantiationDependentType()) {
2037 // It will be analyzed later.
2038 Vars.push_back(DE);
2039 continue;
2040 }
2041
Alexey Bataeva769e072013-03-22 06:34:35 +00002042 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2043 // A threadprivate variable must not have an incomplete type.
2044 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002045 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataeva769e072013-03-22 06:34:35 +00002046 continue;
2047 }
2048
2049 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
2050 // A threadprivate variable must not have a reference type.
2051 if (VD->getType()->isReferenceType()) {
2052 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataeved09d242014-05-28 05:53:51 +00002053 << getOpenMPDirectiveName(OMPD_threadprivate) << VD->getType();
2054 bool IsDecl =
2055 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2056 Diag(VD->getLocation(),
2057 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2058 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002059 continue;
2060 }
2061
Samuel Antaof8b50122015-07-13 22:54:53 +00002062 // Check if this is a TLS variable. If TLS is not being supported, produce
2063 // the corresponding diagnostic.
2064 if ((VD->getTLSKind() != VarDecl::TLS_None &&
2065 !(VD->hasAttr<OMPThreadPrivateDeclAttr>() &&
2066 getLangOpts().OpenMPUseTLS &&
2067 getASTContext().getTargetInfo().isTLSSupported())) ||
Alexey Bataev1a8b3f12015-05-06 06:34:55 +00002068 (VD->getStorageClass() == SC_Register && VD->hasAttr<AsmLabelAttr>() &&
2069 !VD->isLocalVarDecl())) {
Alexey Bataev26a39242015-01-13 03:35:30 +00002070 Diag(ILoc, diag::err_omp_var_thread_local)
2071 << VD << ((VD->getTLSKind() != VarDecl::TLS_None) ? 0 : 1);
Alexey Bataeved09d242014-05-28 05:53:51 +00002072 bool IsDecl =
2073 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
2074 Diag(VD->getLocation(),
2075 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
2076 << VD;
Alexey Bataeva769e072013-03-22 06:34:35 +00002077 continue;
2078 }
2079
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002080 // Check if initial value of threadprivate variable reference variable with
2081 // local storage (it is not supported by runtime).
Alexey Bataeve3727102018-04-18 15:57:46 +00002082 if (const Expr *Init = VD->getAnyInitializer()) {
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002083 LocalVarRefChecker Checker(*this);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002084 if (Checker.Visit(Init))
2085 continue;
Alexey Bataev18b92ee2014-05-28 07:40:25 +00002086 }
2087
Alexey Bataeved09d242014-05-28 05:53:51 +00002088 Vars.push_back(RefExpr);
Alexey Bataevd178ad42014-03-07 08:03:37 +00002089 DSAStack->addDSA(VD, DE, OMPC_threadprivate);
Alexey Bataev97720002014-11-11 04:05:39 +00002090 VD->addAttr(OMPThreadPrivateDeclAttr::CreateImplicit(
2091 Context, SourceRange(Loc, Loc)));
Alexey Bataeve3727102018-04-18 15:57:46 +00002092 if (ASTMutationListener *ML = Context.getASTMutationListener())
Alexey Bataev97720002014-11-11 04:05:39 +00002093 ML->DeclarationMarkedOpenMPThreadPrivate(VD);
Alexey Bataeva769e072013-03-22 06:34:35 +00002094 }
Alexander Musmancb7f9c42014-05-15 13:04:49 +00002095 OMPThreadPrivateDecl *D = nullptr;
Alexey Bataevec3da872014-01-31 05:15:34 +00002096 if (!Vars.empty()) {
2097 D = OMPThreadPrivateDecl::Create(Context, getCurLexicalContext(), Loc,
2098 Vars);
2099 D->setAccess(AS_public);
2100 }
2101 return D;
Alexey Bataeva769e072013-03-22 06:34:35 +00002102}
Alexey Bataev6f6f3b42013-05-13 04:18:18 +00002103
Kelvin Li1408f912018-09-26 04:28:39 +00002104Sema::DeclGroupPtrTy
2105Sema::ActOnOpenMPRequiresDirective(SourceLocation Loc,
2106 ArrayRef<OMPClause *> ClauseList) {
2107 OMPRequiresDecl *D = nullptr;
2108 if (!CurContext->isFileContext()) {
2109 Diag(Loc, diag::err_omp_invalid_scope) << "requires";
2110 } else {
2111 D = CheckOMPRequiresDecl(Loc, ClauseList);
2112 if (D) {
2113 CurContext->addDecl(D);
2114 DSAStack->addRequiresDecl(D);
2115 }
2116 }
2117 return DeclGroupPtrTy::make(DeclGroupRef(D));
2118}
2119
2120OMPRequiresDecl *Sema::CheckOMPRequiresDecl(SourceLocation Loc,
2121 ArrayRef<OMPClause *> ClauseList) {
2122 if (!DSAStack->hasDuplicateRequiresClause(ClauseList))
2123 return OMPRequiresDecl::Create(Context, getCurLexicalContext(), Loc,
2124 ClauseList);
2125 return nullptr;
2126}
2127
Alexey Bataeve3727102018-04-18 15:57:46 +00002128static void reportOriginalDsa(Sema &SemaRef, const DSAStackTy *Stack,
2129 const ValueDecl *D,
2130 const DSAStackTy::DSAVarData &DVar,
Alexey Bataev7ff55242014-06-19 09:13:45 +00002131 bool IsLoopIterVar = false) {
2132 if (DVar.RefExpr) {
2133 SemaRef.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
2134 << getOpenMPClauseName(DVar.CKind);
2135 return;
2136 }
2137 enum {
2138 PDSA_StaticMemberShared,
2139 PDSA_StaticLocalVarShared,
2140 PDSA_LoopIterVarPrivate,
2141 PDSA_LoopIterVarLinear,
2142 PDSA_LoopIterVarLastprivate,
2143 PDSA_ConstVarShared,
2144 PDSA_GlobalVarShared,
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002145 PDSA_TaskVarFirstprivate,
Alexey Bataevbae9a792014-06-27 10:37:06 +00002146 PDSA_LocalVarPrivate,
2147 PDSA_Implicit
2148 } Reason = PDSA_Implicit;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002149 bool ReportHint = false;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002150 auto ReportLoc = D->getLocation();
2151 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev7ff55242014-06-19 09:13:45 +00002152 if (IsLoopIterVar) {
2153 if (DVar.CKind == OMPC_private)
2154 Reason = PDSA_LoopIterVarPrivate;
2155 else if (DVar.CKind == OMPC_lastprivate)
2156 Reason = PDSA_LoopIterVarLastprivate;
2157 else
2158 Reason = PDSA_LoopIterVarLinear;
Alexey Bataev35aaee62016-04-13 13:36:48 +00002159 } else if (isOpenMPTaskingDirective(DVar.DKind) &&
2160 DVar.CKind == OMPC_firstprivate) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002161 Reason = PDSA_TaskVarFirstprivate;
2162 ReportLoc = DVar.ImplicitDSALoc;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002163 } else if (VD && VD->isStaticLocal())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002164 Reason = PDSA_StaticLocalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002165 else if (VD && VD->isStaticDataMember())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002166 Reason = PDSA_StaticMemberShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002167 else if (VD && VD->isFileVarDecl())
Alexey Bataev7ff55242014-06-19 09:13:45 +00002168 Reason = PDSA_GlobalVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002169 else if (D->getType().isConstant(SemaRef.getASTContext()))
Alexey Bataev7ff55242014-06-19 09:13:45 +00002170 Reason = PDSA_ConstVarShared;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002171 else if (VD && VD->isLocalVarDecl() && DVar.CKind == OMPC_private) {
Alexey Bataev7ff55242014-06-19 09:13:45 +00002172 ReportHint = true;
2173 Reason = PDSA_LocalVarPrivate;
2174 }
Alexey Bataevbae9a792014-06-27 10:37:06 +00002175 if (Reason != PDSA_Implicit) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002176 SemaRef.Diag(ReportLoc, diag::note_omp_predetermined_dsa)
Alexey Bataevbae9a792014-06-27 10:37:06 +00002177 << Reason << ReportHint
2178 << getOpenMPDirectiveName(Stack->getCurrentDirective());
2179 } else if (DVar.ImplicitDSALoc.isValid()) {
2180 SemaRef.Diag(DVar.ImplicitDSALoc, diag::note_omp_implicit_dsa)
2181 << getOpenMPClauseName(DVar.CKind);
2182 }
Alexey Bataev7ff55242014-06-19 09:13:45 +00002183}
2184
Alexey Bataev758e55e2013-09-06 18:03:48 +00002185namespace {
Alexey Bataeve3727102018-04-18 15:57:46 +00002186class DSAAttrChecker final : public StmtVisitor<DSAAttrChecker, void> {
Alexey Bataev758e55e2013-09-06 18:03:48 +00002187 DSAStackTy *Stack;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002188 Sema &SemaRef;
Alexey Bataeve3727102018-04-18 15:57:46 +00002189 bool ErrorFound = false;
2190 CapturedStmt *CS = nullptr;
2191 llvm::SmallVector<Expr *, 4> ImplicitFirstprivate;
2192 llvm::SmallVector<Expr *, 4> ImplicitMap;
2193 Sema::VarsWithInheritedDSAType VarsWithInheritedDSA;
2194 llvm::SmallDenseSet<const ValueDecl *, 4> ImplicitDeclarations;
Alexey Bataeved09d242014-05-28 05:53:51 +00002195
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00002196 void VisitSubCaptures(OMPExecutableDirective *S) {
2197 // Check implicitly captured variables.
2198 if (!S->hasAssociatedStmt() || !S->getAssociatedStmt())
2199 return;
2200 for (const CapturedStmt::Capture &Cap :
2201 S->getInnermostCapturedStmt()->captures()) {
2202 if (!Cap.capturesVariable())
2203 continue;
2204 VarDecl *VD = Cap.getCapturedVar();
2205 // Do not try to map the variable if it or its sub-component was mapped
2206 // already.
2207 if (isOpenMPTargetExecutionDirective(Stack->getCurrentDirective()) &&
2208 Stack->checkMappableExprComponentListsForDecl(
2209 VD, /*CurrentRegionOnly=*/true,
2210 [](OMPClauseMappableExprCommon::MappableExprComponentListRef,
2211 OpenMPClauseKind) { return true; }))
2212 continue;
2213 DeclRefExpr *DRE = buildDeclRefExpr(
2214 SemaRef, VD, VD->getType().getNonLValueExprType(SemaRef.Context),
2215 Cap.getLocation(), /*RefersToCapture=*/true);
2216 Visit(DRE);
2217 }
2218 }
2219
Alexey Bataev758e55e2013-09-06 18:03:48 +00002220public:
2221 void VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002222 if (E->isTypeDependent() || E->isValueDependent() ||
2223 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2224 return;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002225 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002226 VD = VD->getCanonicalDecl();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002227 // Skip internally declared variables.
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002228 if (VD->hasLocalStorage() && !CS->capturesVariable(VD))
Alexey Bataeved09d242014-05-28 05:53:51 +00002229 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002230
Alexey Bataeve3727102018-04-18 15:57:46 +00002231 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002232 // Check if the variable has explicit DSA set and stop analysis if it so.
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00002233 if (DVar.RefExpr || !ImplicitDeclarations.insert(VD).second)
David Majnemer9d168222016-08-05 17:44:54 +00002234 return;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002235
Alexey Bataevafe50572017-10-06 17:00:28 +00002236 // Skip internally declared static variables.
Alexey Bataev92327c52018-03-26 16:40:55 +00002237 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
Alexey Bataev97b72212018-08-14 18:31:20 +00002238 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD);
Alexey Bataev92327c52018-03-26 16:40:55 +00002239 if (VD->hasGlobalStorage() && !CS->capturesVariable(VD) &&
2240 (!Res || *Res != OMPDeclareTargetDeclAttr::MT_Link))
Alexey Bataevafe50572017-10-06 17:00:28 +00002241 return;
2242
Alexey Bataeve3727102018-04-18 15:57:46 +00002243 SourceLocation ELoc = E->getExprLoc();
2244 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Alexey Bataev758e55e2013-09-06 18:03:48 +00002245 // The default(none) clause requires that each variable that is referenced
2246 // in the construct, and does not have a predetermined data-sharing
2247 // attribute, must have its data-sharing attribute explicitly determined
2248 // by being listed in a data-sharing attribute clause.
2249 if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
Alexey Bataev7e6803e2019-01-09 15:58:05 +00002250 isImplicitOrExplicitTaskingRegion(DKind) &&
Alexey Bataev4acb8592014-07-07 13:01:15 +00002251 VarsWithInheritedDSA.count(VD) == 0) {
2252 VarsWithInheritedDSA[VD] = E;
Alexey Bataev758e55e2013-09-06 18:03:48 +00002253 return;
2254 }
2255
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002256 if (isOpenMPTargetExecutionDirective(DKind) &&
2257 !Stack->isLoopControlVariable(VD).first) {
2258 if (!Stack->checkMappableExprComponentListsForDecl(
2259 VD, /*CurrentRegionOnly=*/true,
2260 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2261 StackComponents,
2262 OpenMPClauseKind) {
2263 // Variable is used if it has been marked as an array, array
2264 // section or the variable iself.
2265 return StackComponents.size() == 1 ||
2266 std::all_of(
2267 std::next(StackComponents.rbegin()),
2268 StackComponents.rend(),
2269 [](const OMPClauseMappableExprCommon::
2270 MappableComponent &MC) {
2271 return MC.getAssociatedDeclaration() ==
2272 nullptr &&
2273 (isa<OMPArraySectionExpr>(
2274 MC.getAssociatedExpression()) ||
2275 isa<ArraySubscriptExpr>(
2276 MC.getAssociatedExpression()));
2277 });
2278 })) {
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002279 bool IsFirstprivate = false;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002280 // By default lambdas are captured as firstprivates.
2281 if (const auto *RD =
2282 VD->getType().getNonReferenceType()->getAsCXXRecordDecl())
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002283 IsFirstprivate = RD->isLambda();
2284 IsFirstprivate =
2285 IsFirstprivate ||
2286 (VD->getType().getNonReferenceType()->isScalarType() &&
Alexey Bataev92327c52018-03-26 16:40:55 +00002287 Stack->getDefaultDMA() != DMA_tofrom_scalar && !Res);
Alexey Bataev2fd0cb22017-10-05 17:51:39 +00002288 if (IsFirstprivate)
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002289 ImplicitFirstprivate.emplace_back(E);
2290 else
2291 ImplicitMap.emplace_back(E);
2292 return;
2293 }
2294 }
2295
Alexey Bataev758e55e2013-09-06 18:03:48 +00002296 // OpenMP [2.9.3.6, Restrictions, p.2]
2297 // A list item that appears in a reduction clause of the innermost
2298 // enclosing worksharing or parallel construct may not be accessed in an
2299 // explicit task.
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002300 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002301 VD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2302 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +00002303 return isOpenMPParallelDirective(K) ||
2304 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2305 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +00002306 /*FromParent=*/true);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002307 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
Alexey Bataevc5e02582014-06-16 07:08:35 +00002308 ErrorFound = true;
Alexey Bataev7ff55242014-06-19 09:13:45 +00002309 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002310 reportOriginalDsa(SemaRef, Stack, VD, DVar);
Alexey Bataevc5e02582014-06-16 07:08:35 +00002311 return;
2312 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002313
2314 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002315 DVar = Stack->getImplicitDSA(VD, /*FromParent=*/false);
Alexey Bataev35aaee62016-04-13 13:36:48 +00002316 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
2317 !Stack->isLoopControlVariable(VD).first)
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002318 ImplicitFirstprivate.push_back(E);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002319 }
2320 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002321 void VisitMemberExpr(MemberExpr *E) {
Alexey Bataev07b79c22016-04-29 09:56:11 +00002322 if (E->isTypeDependent() || E->isValueDependent() ||
2323 E->containsUnexpandedParameterPack() || E->isInstantiationDependent())
2324 return;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002325 auto *FD = dyn_cast<FieldDecl>(E->getMemberDecl());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002326 OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
Patrick Lystere13b1e32019-01-02 19:28:48 +00002327 if (auto *TE = dyn_cast<CXXThisExpr>(E->getBase()->IgnoreParens())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002328 if (!FD)
2329 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002330 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002331 // Check if the variable has explicit DSA set and stop analysis if it
2332 // so.
2333 if (DVar.RefExpr || !ImplicitDeclarations.insert(FD).second)
2334 return;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002335
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002336 if (isOpenMPTargetExecutionDirective(DKind) &&
2337 !Stack->isLoopControlVariable(FD).first &&
2338 !Stack->checkMappableExprComponentListsForDecl(
2339 FD, /*CurrentRegionOnly=*/true,
2340 [](OMPClauseMappableExprCommon::MappableExprComponentListRef
2341 StackComponents,
2342 OpenMPClauseKind) {
2343 return isa<CXXThisExpr>(
2344 cast<MemberExpr>(
2345 StackComponents.back().getAssociatedExpression())
2346 ->getBase()
2347 ->IgnoreParens());
2348 })) {
2349 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
2350 // A bit-field cannot appear in a map clause.
2351 //
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002352 if (FD->isBitField())
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002353 return;
Patrick Lystere13b1e32019-01-02 19:28:48 +00002354
2355 // Check to see if the member expression is referencing a class that
2356 // has already been explicitly mapped
2357 if (Stack->isClassPreviouslyMapped(TE->getType()))
2358 return;
2359
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002360 ImplicitMap.emplace_back(E);
2361 return;
2362 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002363
Alexey Bataeve3727102018-04-18 15:57:46 +00002364 SourceLocation ELoc = E->getExprLoc();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002365 // OpenMP [2.9.3.6, Restrictions, p.2]
2366 // A list item that appears in a reduction clause of the innermost
2367 // enclosing worksharing or parallel construct may not be accessed in
2368 // an explicit task.
2369 DVar = Stack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +00002370 FD, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
2371 [](OpenMPDirectiveKind K) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002372 return isOpenMPParallelDirective(K) ||
2373 isOpenMPWorksharingDirective(K) || isOpenMPTeamsDirective(K);
2374 },
2375 /*FromParent=*/true);
2376 if (isOpenMPTaskingDirective(DKind) && DVar.CKind == OMPC_reduction) {
2377 ErrorFound = true;
2378 SemaRef.Diag(ELoc, diag::err_omp_reduction_in_task);
Alexey Bataeve3727102018-04-18 15:57:46 +00002379 reportOriginalDsa(SemaRef, Stack, FD, DVar);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002380 return;
2381 }
2382
2383 // Define implicit data-sharing attributes for task.
Alexey Bataeve3727102018-04-18 15:57:46 +00002384 DVar = Stack->getImplicitDSA(FD, /*FromParent=*/false);
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002385 if (isOpenMPTaskingDirective(DKind) && DVar.CKind != OMPC_shared &&
Alexey Bataevb40e05202018-10-24 18:53:12 +00002386 !Stack->isLoopControlVariable(FD).first) {
2387 // Check if there is a captured expression for the current field in the
2388 // region. Do not mark it as firstprivate unless there is no captured
2389 // expression.
2390 // TODO: try to make it firstprivate.
2391 if (DVar.CKind != OMPC_unknown)
2392 ImplicitFirstprivate.push_back(E);
2393 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002394 return;
2395 }
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002396 if (isOpenMPTargetExecutionDirective(DKind)) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002397 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
Alexey Bataeve3727102018-04-18 15:57:46 +00002398 if (!checkMapClauseExpressionBase(SemaRef, E, CurComponents, OMPC_map,
Alexey Bataevb7a9b742017-12-05 19:20:09 +00002399 /*NoDiagnose=*/true))
Alexey Bataev27041fa2017-12-05 15:22:49 +00002400 return;
Alexey Bataeve3727102018-04-18 15:57:46 +00002401 const auto *VD = cast<ValueDecl>(
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002402 CurComponents.back().getAssociatedDeclaration()->getCanonicalDecl());
2403 if (!Stack->checkMappableExprComponentListsForDecl(
2404 VD, /*CurrentRegionOnly=*/true,
2405 [&CurComponents](
2406 OMPClauseMappableExprCommon::MappableExprComponentListRef
2407 StackComponents,
2408 OpenMPClauseKind) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002409 auto CCI = CurComponents.rbegin();
Alexey Bataev5ec38932017-09-26 16:19:04 +00002410 auto CCE = CurComponents.rend();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002411 for (const auto &SC : llvm::reverse(StackComponents)) {
2412 // Do both expressions have the same kind?
2413 if (CCI->getAssociatedExpression()->getStmtClass() !=
2414 SC.getAssociatedExpression()->getStmtClass())
2415 if (!(isa<OMPArraySectionExpr>(
2416 SC.getAssociatedExpression()) &&
2417 isa<ArraySubscriptExpr>(
2418 CCI->getAssociatedExpression())))
2419 return false;
2420
Alexey Bataeve3727102018-04-18 15:57:46 +00002421 const Decl *CCD = CCI->getAssociatedDeclaration();
2422 const Decl *SCD = SC.getAssociatedDeclaration();
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002423 CCD = CCD ? CCD->getCanonicalDecl() : nullptr;
2424 SCD = SCD ? SCD->getCanonicalDecl() : nullptr;
2425 if (SCD != CCD)
2426 return false;
2427 std::advance(CCI, 1);
Alexey Bataev5ec38932017-09-26 16:19:04 +00002428 if (CCI == CCE)
2429 break;
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002430 }
2431 return true;
2432 })) {
2433 Visit(E->getBase());
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002434 }
Alexey Bataeve3727102018-04-18 15:57:46 +00002435 } else {
Alexey Bataev7fcacd82016-11-28 15:55:15 +00002436 Visit(E->getBase());
Alexey Bataeve3727102018-04-18 15:57:46 +00002437 }
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00002438 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002439 void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002440 for (OMPClause *C : S->clauses()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002441 // Skip analysis of arguments of implicitly defined firstprivate clause
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002442 // for task|target directives.
2443 // Skip analysis of arguments of implicitly defined map clause for target
2444 // directives.
2445 if (C && !((isa<OMPFirstprivateClause>(C) || isa<OMPMapClause>(C)) &&
2446 C->isImplicit())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002447 for (Stmt *CC : C->children()) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002448 if (CC)
2449 Visit(CC);
2450 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002451 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002452 }
Alexey Bataevf07946e2018-10-29 20:17:42 +00002453 // Check implicitly captured variables.
2454 VisitSubCaptures(S);
Alexey Bataev758e55e2013-09-06 18:03:48 +00002455 }
2456 void VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002457 for (Stmt *C : S->children()) {
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002458 if (C) {
Joel E. Denny0fdf5a92018-12-19 15:59:47 +00002459 // Check implicitly captured variables in the task-based directives to
2460 // check if they must be firstprivatized.
2461 Visit(C);
Alexey Bataev8fc7b5f2018-10-25 15:35:27 +00002462 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002463 }
Alexey Bataeved09d242014-05-28 05:53:51 +00002464 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002465
Alexey Bataeve3727102018-04-18 15:57:46 +00002466 bool isErrorFound() const { return ErrorFound; }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00002467 ArrayRef<Expr *> getImplicitFirstprivate() const {
2468 return ImplicitFirstprivate;
2469 }
2470 ArrayRef<Expr *> getImplicitMap() const { return ImplicitMap; }
Alexey Bataeve3727102018-04-18 15:57:46 +00002471 const Sema::VarsWithInheritedDSAType &getVarsWithInheritedDSA() const {
Alexey Bataev4acb8592014-07-07 13:01:15 +00002472 return VarsWithInheritedDSA;
2473 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00002474
Alexey Bataev7ff55242014-06-19 09:13:45 +00002475 DSAAttrChecker(DSAStackTy *S, Sema &SemaRef, CapturedStmt *CS)
2476 : Stack(S), SemaRef(SemaRef), ErrorFound(false), CS(CS) {}
Alexey Bataev758e55e2013-09-06 18:03:48 +00002477};
Alexey Bataeved09d242014-05-28 05:53:51 +00002478} // namespace
Alexey Bataev758e55e2013-09-06 18:03:48 +00002479
Alexey Bataevbae9a792014-06-27 10:37:06 +00002480void Sema::ActOnOpenMPRegionStart(OpenMPDirectiveKind DKind, Scope *CurScope) {
Alexey Bataev9959db52014-05-06 10:08:46 +00002481 switch (DKind) {
Kelvin Li70a12c52016-07-13 21:51:49 +00002482 case OMPD_parallel:
2483 case OMPD_parallel_for:
2484 case OMPD_parallel_for_simd:
2485 case OMPD_parallel_sections:
Carlo Bertolliba1487b2017-10-04 14:12:09 +00002486 case OMPD_teams:
Alexey Bataev999277a2017-12-06 14:31:09 +00002487 case OMPD_teams_distribute:
2488 case OMPD_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002489 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Alexey Bataev2377fe92015-09-10 08:12:02 +00002490 QualType KmpInt32PtrTy =
2491 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002492 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002493 std::make_pair(".global_tid.", KmpInt32PtrTy),
2494 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2495 std::make_pair(StringRef(), QualType()) // __context with shared vars
Alexey Bataev9959db52014-05-06 10:08:46 +00002496 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002497 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2498 Params);
Alexey Bataev9959db52014-05-06 10:08:46 +00002499 break;
2500 }
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002501 case OMPD_target_teams:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00002502 case OMPD_target_parallel:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00002503 case OMPD_target_parallel_for:
Alexey Bataevdfa430f2017-12-08 15:03:50 +00002504 case OMPD_target_parallel_for_simd:
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00002505 case OMPD_target_teams_distribute:
2506 case OMPD_target_teams_distribute_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002507 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2508 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2509 QualType KmpInt32PtrTy =
2510 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2511 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002512 FunctionProtoType::ExtProtoInfo EPI;
2513 EPI.Variadic = true;
2514 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2515 Sema::CapturedParamNameType Params[] = {
2516 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002517 std::make_pair(".part_id.", KmpInt32PtrTy),
2518 std::make_pair(".privates.", VoidPtrTy),
2519 std::make_pair(
2520 ".copy_fn.",
2521 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002522 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2523 std::make_pair(StringRef(), QualType()) // __context with shared vars
2524 };
2525 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2526 Params);
Alexey Bataev0c869ef2018-01-16 15:57:07 +00002527 // Mark this captured region as inlined, because we don't use outlined
2528 // function directly.
2529 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2530 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002531 Context, AlwaysInlineAttr::Keyword_forceinline));
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002532 Sema::CapturedParamNameType ParamsTarget[] = {
2533 std::make_pair(StringRef(), QualType()) // __context with shared vars
2534 };
2535 // Start a captured region for 'target' with no implicit parameters.
2536 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2537 ParamsTarget);
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002538 Sema::CapturedParamNameType ParamsTeamsOrParallel[] = {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002539 std::make_pair(".global_tid.", KmpInt32PtrTy),
2540 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2541 std::make_pair(StringRef(), QualType()) // __context with shared vars
2542 };
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002543 // Start a captured region for 'teams' or 'parallel'. Both regions have
2544 // the same implicit parameters.
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002545 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
Arpith Chacko Jacob99a1e0e2017-01-25 02:18:43 +00002546 ParamsTeamsOrParallel);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002547 break;
2548 }
Alexey Bataev8451efa2018-01-15 19:06:12 +00002549 case OMPD_target:
2550 case OMPD_target_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002551 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2552 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2553 QualType KmpInt32PtrTy =
2554 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2555 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002556 FunctionProtoType::ExtProtoInfo EPI;
2557 EPI.Variadic = true;
2558 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2559 Sema::CapturedParamNameType Params[] = {
2560 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002561 std::make_pair(".part_id.", KmpInt32PtrTy),
2562 std::make_pair(".privates.", VoidPtrTy),
2563 std::make_pair(
2564 ".copy_fn.",
2565 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002566 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2567 std::make_pair(StringRef(), QualType()) // __context with shared vars
2568 };
2569 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2570 Params);
2571 // Mark this captured region as inlined, because we don't use outlined
2572 // function directly.
2573 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2574 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002575 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev8451efa2018-01-15 19:06:12 +00002576 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2577 std::make_pair(StringRef(), QualType()));
2578 break;
2579 }
Kelvin Li70a12c52016-07-13 21:51:49 +00002580 case OMPD_simd:
2581 case OMPD_for:
2582 case OMPD_for_simd:
2583 case OMPD_sections:
2584 case OMPD_section:
2585 case OMPD_single:
2586 case OMPD_master:
2587 case OMPD_critical:
Kelvin Lia579b912016-07-14 02:54:56 +00002588 case OMPD_taskgroup:
2589 case OMPD_distribute:
Alexey Bataev46506272017-12-05 17:41:34 +00002590 case OMPD_distribute_simd:
Kelvin Li70a12c52016-07-13 21:51:49 +00002591 case OMPD_ordered:
2592 case OMPD_atomic:
Alexey Bataev8451efa2018-01-15 19:06:12 +00002593 case OMPD_target_data: {
Alexey Bataevdf9b1592014-06-25 04:09:13 +00002594 Sema::CapturedParamNameType Params[] = {
Alexey Bataevf29276e2014-06-18 04:14:57 +00002595 std::make_pair(StringRef(), QualType()) // __context with shared vars
2596 };
Alexey Bataevbae9a792014-06-27 10:37:06 +00002597 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2598 Params);
Alexey Bataevf29276e2014-06-18 04:14:57 +00002599 break;
2600 }
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002601 case OMPD_task: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002602 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2603 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2604 QualType KmpInt32PtrTy =
2605 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2606 QualType Args[] = {VoidPtrTy};
Alexey Bataev3ae88e22015-05-22 08:56:35 +00002607 FunctionProtoType::ExtProtoInfo EPI;
2608 EPI.Variadic = true;
2609 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002610 Sema::CapturedParamNameType Params[] = {
Alexey Bataev62b63b12015-03-10 07:28:44 +00002611 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002612 std::make_pair(".part_id.", KmpInt32PtrTy),
2613 std::make_pair(".privates.", VoidPtrTy),
2614 std::make_pair(
2615 ".copy_fn.",
2616 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev48591dd2016-04-20 04:01:36 +00002617 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002618 std::make_pair(StringRef(), QualType()) // __context with shared vars
2619 };
2620 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2621 Params);
Alexey Bataev62b63b12015-03-10 07:28:44 +00002622 // Mark this captured region as inlined, because we don't use outlined
2623 // function directly.
2624 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2625 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002626 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00002627 break;
2628 }
Alexey Bataev1e73ef32016-04-28 12:14:51 +00002629 case OMPD_taskloop:
2630 case OMPD_taskloop_simd: {
Alexey Bataev7292c292016-04-25 12:22:29 +00002631 QualType KmpInt32Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002632 Context.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1)
2633 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002634 QualType KmpUInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002635 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0)
2636 .withConst();
Alexey Bataev7292c292016-04-25 12:22:29 +00002637 QualType KmpInt64Ty =
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002638 Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1)
2639 .withConst();
2640 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2641 QualType KmpInt32PtrTy =
2642 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2643 QualType Args[] = {VoidPtrTy};
Alexey Bataev7292c292016-04-25 12:22:29 +00002644 FunctionProtoType::ExtProtoInfo EPI;
2645 EPI.Variadic = true;
2646 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
Alexey Bataev49f6e782015-12-01 04:18:41 +00002647 Sema::CapturedParamNameType Params[] = {
Alexey Bataev7292c292016-04-25 12:22:29 +00002648 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002649 std::make_pair(".part_id.", KmpInt32PtrTy),
2650 std::make_pair(".privates.", VoidPtrTy),
Alexey Bataev7292c292016-04-25 12:22:29 +00002651 std::make_pair(
2652 ".copy_fn.",
2653 Context.getPointerType(CopyFnType).withConst().withRestrict()),
2654 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2655 std::make_pair(".lb.", KmpUInt64Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002656 std::make_pair(".ub.", KmpUInt64Ty),
2657 std::make_pair(".st.", KmpInt64Ty),
Alexey Bataev7292c292016-04-25 12:22:29 +00002658 std::make_pair(".liter.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002659 std::make_pair(".reductions.", VoidPtrTy),
Alexey Bataev49f6e782015-12-01 04:18:41 +00002660 std::make_pair(StringRef(), QualType()) // __context with shared vars
2661 };
2662 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2663 Params);
Alexey Bataev7292c292016-04-25 12:22:29 +00002664 // Mark this captured region as inlined, because we don't use outlined
2665 // function directly.
2666 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2667 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002668 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev49f6e782015-12-01 04:18:41 +00002669 break;
2670 }
Kelvin Li4a39add2016-07-05 05:00:15 +00002671 case OMPD_distribute_parallel_for_simd:
Alexey Bataev647dd842018-01-15 20:59:40 +00002672 case OMPD_distribute_parallel_for: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002673 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli9925f152016-06-27 14:55:37 +00002674 QualType KmpInt32PtrTy =
2675 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2676 Sema::CapturedParamNameType Params[] = {
2677 std::make_pair(".global_tid.", KmpInt32PtrTy),
2678 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002679 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2680 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli9925f152016-06-27 14:55:37 +00002681 std::make_pair(StringRef(), QualType()) // __context with shared vars
2682 };
2683 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2684 Params);
2685 break;
2686 }
Alexey Bataev647dd842018-01-15 20:59:40 +00002687 case OMPD_target_teams_distribute_parallel_for:
2688 case OMPD_target_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002689 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002690 QualType KmpInt32PtrTy =
2691 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002692 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
Carlo Bertolli52978c32018-01-03 21:12:44 +00002693
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002694 QualType Args[] = {VoidPtrTy};
Alexey Bataev8451efa2018-01-15 19:06:12 +00002695 FunctionProtoType::ExtProtoInfo EPI;
2696 EPI.Variadic = true;
2697 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2698 Sema::CapturedParamNameType Params[] = {
2699 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002700 std::make_pair(".part_id.", KmpInt32PtrTy),
2701 std::make_pair(".privates.", VoidPtrTy),
2702 std::make_pair(
2703 ".copy_fn.",
2704 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev8451efa2018-01-15 19:06:12 +00002705 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2706 std::make_pair(StringRef(), QualType()) // __context with shared vars
2707 };
2708 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2709 Params);
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00002710 // Mark this captured region as inlined, because we don't use outlined
2711 // function directly.
2712 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2713 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002714 Context, AlwaysInlineAttr::Keyword_forceinline));
Carlo Bertolli52978c32018-01-03 21:12:44 +00002715 Sema::CapturedParamNameType ParamsTarget[] = {
2716 std::make_pair(StringRef(), QualType()) // __context with shared vars
2717 };
2718 // Start a captured region for 'target' with no implicit parameters.
2719 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2720 ParamsTarget);
2721
2722 Sema::CapturedParamNameType ParamsTeams[] = {
2723 std::make_pair(".global_tid.", KmpInt32PtrTy),
2724 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2725 std::make_pair(StringRef(), QualType()) // __context with shared vars
2726 };
2727 // Start a captured region for 'target' with no implicit parameters.
2728 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2729 ParamsTeams);
2730
2731 Sema::CapturedParamNameType ParamsParallel[] = {
2732 std::make_pair(".global_tid.", KmpInt32PtrTy),
2733 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002734 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2735 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli52978c32018-01-03 21:12:44 +00002736 std::make_pair(StringRef(), QualType()) // __context with shared vars
2737 };
2738 // Start a captured region for 'teams' or 'parallel'. Both regions have
2739 // the same implicit parameters.
2740 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2741 ParamsParallel);
2742 break;
2743 }
2744
Alexey Bataev46506272017-12-05 17:41:34 +00002745 case OMPD_teams_distribute_parallel_for:
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00002746 case OMPD_teams_distribute_parallel_for_simd: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002747 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
Carlo Bertolli62fae152017-11-20 20:46:39 +00002748 QualType KmpInt32PtrTy =
2749 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2750
2751 Sema::CapturedParamNameType ParamsTeams[] = {
2752 std::make_pair(".global_tid.", KmpInt32PtrTy),
2753 std::make_pair(".bound_tid.", KmpInt32PtrTy),
2754 std::make_pair(StringRef(), QualType()) // __context with shared vars
2755 };
2756 // Start a captured region for 'target' with no implicit parameters.
2757 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2758 ParamsTeams);
2759
2760 Sema::CapturedParamNameType ParamsParallel[] = {
2761 std::make_pair(".global_tid.", KmpInt32PtrTy),
2762 std::make_pair(".bound_tid.", KmpInt32PtrTy),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002763 std::make_pair(".previous.lb.", Context.getSizeType().withConst()),
2764 std::make_pair(".previous.ub.", Context.getSizeType().withConst()),
Carlo Bertolli62fae152017-11-20 20:46:39 +00002765 std::make_pair(StringRef(), QualType()) // __context with shared vars
2766 };
2767 // Start a captured region for 'teams' or 'parallel'. Both regions have
2768 // the same implicit parameters.
2769 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2770 ParamsParallel);
2771 break;
2772 }
Alexey Bataev7828b252017-11-21 17:08:48 +00002773 case OMPD_target_update:
2774 case OMPD_target_enter_data:
2775 case OMPD_target_exit_data: {
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002776 QualType KmpInt32Ty = Context.getIntTypeForBitwidth(32, 1).withConst();
2777 QualType VoidPtrTy = Context.VoidPtrTy.withConst().withRestrict();
2778 QualType KmpInt32PtrTy =
2779 Context.getPointerType(KmpInt32Ty).withConst().withRestrict();
2780 QualType Args[] = {VoidPtrTy};
Alexey Bataev7828b252017-11-21 17:08:48 +00002781 FunctionProtoType::ExtProtoInfo EPI;
2782 EPI.Variadic = true;
2783 QualType CopyFnType = Context.getFunctionType(Context.VoidTy, Args, EPI);
2784 Sema::CapturedParamNameType Params[] = {
2785 std::make_pair(".global_tid.", KmpInt32Ty),
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002786 std::make_pair(".part_id.", KmpInt32PtrTy),
2787 std::make_pair(".privates.", VoidPtrTy),
2788 std::make_pair(
2789 ".copy_fn.",
2790 Context.getPointerType(CopyFnType).withConst().withRestrict()),
Alexey Bataev7828b252017-11-21 17:08:48 +00002791 std::make_pair(".task_t.", Context.VoidPtrTy.withConst()),
2792 std::make_pair(StringRef(), QualType()) // __context with shared vars
2793 };
2794 ActOnCapturedRegionStart(DSAStack->getConstructLoc(), CurScope, CR_OpenMP,
2795 Params);
2796 // Mark this captured region as inlined, because we don't use outlined
2797 // function directly.
2798 getCurCapturedRegion()->TheCapturedDecl->addAttr(
2799 AlwaysInlineAttr::CreateImplicit(
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002800 Context, AlwaysInlineAttr::Keyword_forceinline));
Alexey Bataev7828b252017-11-21 17:08:48 +00002801 break;
2802 }
Alexey Bataev9959db52014-05-06 10:08:46 +00002803 case OMPD_threadprivate:
Alexey Bataevee9af452014-11-21 11:33:46 +00002804 case OMPD_taskyield:
2805 case OMPD_barrier:
2806 case OMPD_taskwait:
Alexey Bataev6d4ed052015-07-01 06:57:41 +00002807 case OMPD_cancellation_point:
Alexey Bataev80909872015-07-02 11:25:17 +00002808 case OMPD_cancel:
Alexey Bataevee9af452014-11-21 11:33:46 +00002809 case OMPD_flush:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00002810 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00002811 case OMPD_declare_simd:
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00002812 case OMPD_declare_target:
2813 case OMPD_end_declare_target:
Kelvin Li1408f912018-09-26 04:28:39 +00002814 case OMPD_requires:
Alexey Bataev9959db52014-05-06 10:08:46 +00002815 llvm_unreachable("OpenMP Directive is not allowed");
2816 case OMPD_unknown:
Alexey Bataev9959db52014-05-06 10:08:46 +00002817 llvm_unreachable("Unknown OpenMP directive");
2818 }
2819}
2820
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002821int Sema::getOpenMPCaptureLevels(OpenMPDirectiveKind DKind) {
2822 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2823 getOpenMPCaptureRegions(CaptureRegions, DKind);
2824 return CaptureRegions.size();
2825}
2826
Alexey Bataev3392d762016-02-16 11:18:12 +00002827static OMPCapturedExprDecl *buildCaptureDecl(Sema &S, IdentifierInfo *Id,
Alexey Bataev5a3af132016-03-29 08:58:54 +00002828 Expr *CaptureExpr, bool WithInit,
2829 bool AsExpression) {
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002830 assert(CaptureExpr);
Alexey Bataev4244be22016-02-11 05:35:55 +00002831 ASTContext &C = S.getASTContext();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002832 Expr *Init = AsExpression ? CaptureExpr : CaptureExpr->IgnoreImpCasts();
Alexey Bataev4244be22016-02-11 05:35:55 +00002833 QualType Ty = Init->getType();
2834 if (CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002835 if (S.getLangOpts().CPlusPlus) {
Alexey Bataev4244be22016-02-11 05:35:55 +00002836 Ty = C.getLValueReferenceType(Ty);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002837 } else {
Alexey Bataev4244be22016-02-11 05:35:55 +00002838 Ty = C.getPointerType(Ty);
2839 ExprResult Res =
2840 S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_AddrOf, Init);
2841 if (!Res.isUsable())
2842 return nullptr;
2843 Init = Res.get();
2844 }
Alexey Bataev61205072016-03-02 04:57:40 +00002845 WithInit = true;
Alexey Bataev4244be22016-02-11 05:35:55 +00002846 }
Alexey Bataeva7206b92016-12-20 16:51:02 +00002847 auto *CED = OMPCapturedExprDecl::Create(C, S.CurContext, Id, Ty,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002848 CaptureExpr->getBeginLoc());
Alexey Bataev2bbf7212016-03-03 03:52:24 +00002849 if (!WithInit)
Alexey Bataeve3727102018-04-18 15:57:46 +00002850 CED->addAttr(OMPCaptureNoInitAttr::CreateImplicit(C));
Alexey Bataev4244be22016-02-11 05:35:55 +00002851 S.CurContext->addHiddenDecl(CED);
Richard Smith3beb7c62017-01-12 02:27:38 +00002852 S.AddInitializerToDecl(CED, Init, /*DirectInit=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002853 return CED;
2854}
2855
Alexey Bataev61205072016-03-02 04:57:40 +00002856static DeclRefExpr *buildCapture(Sema &S, ValueDecl *D, Expr *CaptureExpr,
2857 bool WithInit) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002858 OMPCapturedExprDecl *CD;
Alexey Bataeve3727102018-04-18 15:57:46 +00002859 if (VarDecl *VD = S.isOpenMPCapturedDecl(D))
Alexey Bataevb7a34b62016-02-25 03:59:29 +00002860 CD = cast<OMPCapturedExprDecl>(VD);
Alexey Bataeve3727102018-04-18 15:57:46 +00002861 else
Alexey Bataev5a3af132016-03-29 08:58:54 +00002862 CD = buildCaptureDecl(S, D->getIdentifier(), CaptureExpr, WithInit,
2863 /*AsExpression=*/false);
Alexey Bataev3392d762016-02-16 11:18:12 +00002864 return buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
Alexey Bataev1efd1662016-03-29 10:59:56 +00002865 CaptureExpr->getExprLoc());
Alexey Bataev3392d762016-02-16 11:18:12 +00002866}
2867
Alexey Bataev5a3af132016-03-29 08:58:54 +00002868static ExprResult buildCapture(Sema &S, Expr *CaptureExpr, DeclRefExpr *&Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002869 CaptureExpr = S.DefaultLvalueConversion(CaptureExpr).get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00002870 if (!Ref) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002871 OMPCapturedExprDecl *CD = buildCaptureDecl(
2872 S, &S.getASTContext().Idents.get(".capture_expr."), CaptureExpr,
2873 /*WithInit=*/true, /*AsExpression=*/true);
Alexey Bataev5a3af132016-03-29 08:58:54 +00002874 Ref = buildDeclRefExpr(S, CD, CD->getType().getNonReferenceType(),
2875 CaptureExpr->getExprLoc());
2876 }
2877 ExprResult Res = Ref;
2878 if (!S.getLangOpts().CPlusPlus &&
2879 CaptureExpr->getObjectKind() == OK_Ordinary && CaptureExpr->isGLValue() &&
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002880 Ref->getType()->isPointerType()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00002881 Res = S.CreateBuiltinUnaryOp(CaptureExpr->getExprLoc(), UO_Deref, Ref);
Alexey Bataev8e769ee2017-12-22 21:01:52 +00002882 if (!Res.isUsable())
2883 return ExprError();
2884 }
2885 return S.DefaultLvalueConversion(Res.get());
Alexey Bataev4244be22016-02-11 05:35:55 +00002886}
2887
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002888namespace {
2889// OpenMP directives parsed in this section are represented as a
2890// CapturedStatement with an associated statement. If a syntax error
2891// is detected during the parsing of the associated statement, the
2892// compiler must abort processing and close the CapturedStatement.
2893//
2894// Combined directives such as 'target parallel' have more than one
2895// nested CapturedStatements. This RAII ensures that we unwind out
2896// of all the nested CapturedStatements when an error is found.
2897class CaptureRegionUnwinderRAII {
2898private:
2899 Sema &S;
2900 bool &ErrorFound;
Alexey Bataeve3727102018-04-18 15:57:46 +00002901 OpenMPDirectiveKind DKind = OMPD_unknown;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002902
2903public:
2904 CaptureRegionUnwinderRAII(Sema &S, bool &ErrorFound,
2905 OpenMPDirectiveKind DKind)
2906 : S(S), ErrorFound(ErrorFound), DKind(DKind) {}
2907 ~CaptureRegionUnwinderRAII() {
2908 if (ErrorFound) {
2909 int ThisCaptureLevel = S.getOpenMPCaptureLevels(DKind);
2910 while (--ThisCaptureLevel >= 0)
2911 S.ActOnCapturedRegionError();
2912 }
2913 }
2914};
2915} // namespace
2916
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002917StmtResult Sema::ActOnOpenMPRegionEnd(StmtResult S,
2918 ArrayRef<OMPClause *> Clauses) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002919 bool ErrorFound = false;
2920 CaptureRegionUnwinderRAII CaptureRegionUnwinder(
2921 *this, ErrorFound, DSAStack->getCurrentDirective());
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002922 if (!S.isUsable()) {
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00002923 ErrorFound = true;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002924 return StmtError();
2925 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002926
Alexey Bataev2ba67042017-11-28 21:11:44 +00002927 SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
2928 getOpenMPCaptureRegions(CaptureRegions, DSAStack->getCurrentDirective());
Alexey Bataev993d2802015-12-28 06:23:08 +00002929 OMPOrderedClause *OC = nullptr;
Alexey Bataev6402bca2015-12-28 07:25:51 +00002930 OMPScheduleClause *SC = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00002931 SmallVector<const OMPLinearClause *, 4> LCs;
2932 SmallVector<const OMPClauseWithPreInit *, 4> PICs;
Alexey Bataev040d5402015-05-12 08:35:28 +00002933 // This is required for proper codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00002934 for (OMPClause *Clause : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00002935 if (isOpenMPTaskingDirective(DSAStack->getCurrentDirective()) &&
2936 Clause->getClauseKind() == OMPC_in_reduction) {
2937 // Capture taskgroup task_reduction descriptors inside the tasking regions
2938 // with the corresponding in_reduction items.
2939 auto *IRC = cast<OMPInReductionClause>(Clause);
Alexey Bataeve3727102018-04-18 15:57:46 +00002940 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00002941 if (E)
2942 MarkDeclarationsReferencedInExpr(E);
2943 }
Alexey Bataev16dc7b62015-05-20 03:46:04 +00002944 if (isOpenMPPrivate(Clause->getClauseKind()) ||
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002945 Clause->getClauseKind() == OMPC_copyprivate ||
2946 (getLangOpts().OpenMPUseTLS &&
2947 getASTContext().getTargetInfo().isTLSSupported() &&
2948 Clause->getClauseKind() == OMPC_copyin)) {
2949 DSAStack->setForceVarCapturing(Clause->getClauseKind() == OMPC_copyin);
Alexey Bataev040d5402015-05-12 08:35:28 +00002950 // Mark all variables in private list clauses as used in inner region.
Alexey Bataeve3727102018-04-18 15:57:46 +00002951 for (Stmt *VarRef : Clause->children()) {
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002952 if (auto *E = cast_or_null<Expr>(VarRef)) {
Alexey Bataev8bf6b3e2015-04-02 13:07:08 +00002953 MarkDeclarationsReferencedInExpr(E);
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002954 }
2955 }
Samuel Antao9c75cfe2015-07-27 16:38:06 +00002956 DSAStack->setForceVarCapturing(/*V=*/false);
Alexey Bataev2ba67042017-11-28 21:11:44 +00002957 } else if (CaptureRegions.size() > 1 ||
2958 CaptureRegions.back() != OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00002959 if (auto *C = OMPClauseWithPreInit::get(Clause))
2960 PICs.push_back(C);
Alexey Bataev005248a2016-02-25 05:25:57 +00002961 if (auto *C = OMPClauseWithPostUpdate::get(Clause)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002962 if (Expr *E = C->getPostUpdateExpr())
Alexey Bataev005248a2016-02-25 05:25:57 +00002963 MarkDeclarationsReferencedInExpr(E);
2964 }
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00002965 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002966 if (Clause->getClauseKind() == OMPC_schedule)
2967 SC = cast<OMPScheduleClause>(Clause);
2968 else if (Clause->getClauseKind() == OMPC_ordered)
Alexey Bataev993d2802015-12-28 06:23:08 +00002969 OC = cast<OMPOrderedClause>(Clause);
2970 else if (Clause->getClauseKind() == OMPC_linear)
2971 LCs.push_back(cast<OMPLinearClause>(Clause));
2972 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002973 // OpenMP, 2.7.1 Loop Construct, Restrictions
2974 // The nonmonotonic modifier cannot be specified if an ordered clause is
2975 // specified.
2976 if (SC &&
2977 (SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
2978 SC->getSecondScheduleModifier() ==
2979 OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
2980 OC) {
2981 Diag(SC->getFirstScheduleModifier() == OMPC_SCHEDULE_MODIFIER_nonmonotonic
2982 ? SC->getFirstScheduleModifierLoc()
2983 : SC->getSecondScheduleModifierLoc(),
2984 diag::err_omp_schedule_nonmonotonic_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002985 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev6402bca2015-12-28 07:25:51 +00002986 ErrorFound = true;
2987 }
Alexey Bataev993d2802015-12-28 06:23:08 +00002988 if (!LCs.empty() && OC && OC->getNumForLoops()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00002989 for (const OMPLinearClause *C : LCs) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002990 Diag(C->getBeginLoc(), diag::err_omp_linear_ordered)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00002991 << SourceRange(OC->getBeginLoc(), OC->getEndLoc());
Alexey Bataev993d2802015-12-28 06:23:08 +00002992 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00002993 ErrorFound = true;
2994 }
Alexey Bataev113438c2015-12-30 12:06:23 +00002995 if (isOpenMPWorksharingDirective(DSAStack->getCurrentDirective()) &&
2996 isOpenMPSimdDirective(DSAStack->getCurrentDirective()) && OC &&
2997 OC->getNumForLoops()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00002998 Diag(OC->getBeginLoc(), diag::err_omp_ordered_simd)
Alexey Bataev113438c2015-12-30 12:06:23 +00002999 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
3000 ErrorFound = true;
3001 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00003002 if (ErrorFound) {
Alexey Bataev993d2802015-12-28 06:23:08 +00003003 return StmtError();
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003004 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003005 StmtResult SR = S;
Alexey Bataev2ba67042017-11-28 21:11:44 +00003006 for (OpenMPDirectiveKind ThisCaptureRegion : llvm::reverse(CaptureRegions)) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003007 // Mark all variables in private list clauses as used in inner region.
3008 // Required for proper codegen of combined directives.
3009 // TODO: add processing for other clauses.
Alexey Bataev2ba67042017-11-28 21:11:44 +00003010 if (ThisCaptureRegion != OMPD_unknown) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003011 for (const clang::OMPClauseWithPreInit *C : PICs) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003012 OpenMPDirectiveKind CaptureRegion = C->getCaptureRegion();
3013 // Find the particular capture region for the clause if the
3014 // directive is a combined one with multiple capture regions.
3015 // If the directive is not a combined one, the capture region
3016 // associated with the clause is OMPD_unknown and is generated
3017 // only once.
3018 if (CaptureRegion == ThisCaptureRegion ||
3019 CaptureRegion == OMPD_unknown) {
3020 if (auto *DS = cast_or_null<DeclStmt>(C->getPreInitStmt())) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003021 for (Decl *D : DS->decls())
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003022 MarkVariableReferenced(D->getLocation(), cast<VarDecl>(D));
3023 }
3024 }
3025 }
3026 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003027 SR = ActOnCapturedRegionEnd(SR.get());
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00003028 }
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00003029 return SR;
Alexey Bataeva8d4a5432015-04-02 07:48:16 +00003030}
3031
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003032static bool checkCancelRegion(Sema &SemaRef, OpenMPDirectiveKind CurrentRegion,
3033 OpenMPDirectiveKind CancelRegion,
3034 SourceLocation StartLoc) {
3035 // CancelRegion is only needed for cancel and cancellation_point.
3036 if (CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_cancellation_point)
3037 return false;
3038
3039 if (CancelRegion == OMPD_parallel || CancelRegion == OMPD_for ||
3040 CancelRegion == OMPD_sections || CancelRegion == OMPD_taskgroup)
3041 return false;
3042
3043 SemaRef.Diag(StartLoc, diag::err_omp_wrong_cancel_region)
3044 << getOpenMPDirectiveName(CancelRegion);
3045 return true;
3046}
3047
Alexey Bataeve3727102018-04-18 15:57:46 +00003048static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003049 OpenMPDirectiveKind CurrentRegion,
3050 const DeclarationNameInfo &CurrentName,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003051 OpenMPDirectiveKind CancelRegion,
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003052 SourceLocation StartLoc) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003053 if (Stack->getCurScope()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003054 OpenMPDirectiveKind ParentRegion = Stack->getParentDirective();
3055 OpenMPDirectiveKind OffendingRegion = ParentRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003056 bool NestingProhibited = false;
3057 bool CloseNesting = true;
David Majnemer9d168222016-08-05 17:44:54 +00003058 bool OrphanSeen = false;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003059 enum {
3060 NoRecommend,
3061 ShouldBeInParallelRegion,
Alexey Bataev13314bf2014-10-09 04:18:56 +00003062 ShouldBeInOrderedRegion,
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003063 ShouldBeInTargetRegion,
3064 ShouldBeInTeamsRegion
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003065 } Recommend = NoRecommend;
Kelvin Lifd8b5742016-07-01 14:30:25 +00003066 if (isOpenMPSimdDirective(ParentRegion) && CurrentRegion != OMPD_ordered) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003067 // OpenMP [2.16, Nesting of Regions]
3068 // OpenMP constructs may not be nested inside a simd region.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003069 // OpenMP [2.8.1,simd Construct, Restrictions]
Kelvin Lifd8b5742016-07-01 14:30:25 +00003070 // An ordered construct with the simd clause is the only OpenMP
3071 // construct that can appear in the simd region.
David Majnemer9d168222016-08-05 17:44:54 +00003072 // Allowing a SIMD construct nested in another SIMD construct is an
Kelvin Lifd8b5742016-07-01 14:30:25 +00003073 // extension. The OpenMP 4.5 spec does not allow it. Issue a warning
3074 // message.
3075 SemaRef.Diag(StartLoc, (CurrentRegion != OMPD_simd)
3076 ? diag::err_omp_prohibited_region_simd
3077 : diag::warn_omp_nesting_simd);
3078 return CurrentRegion != OMPD_simd;
Alexey Bataev549210e2014-06-24 04:39:47 +00003079 }
Alexey Bataev0162e452014-07-22 10:10:35 +00003080 if (ParentRegion == OMPD_atomic) {
3081 // OpenMP [2.16, Nesting of Regions]
3082 // OpenMP constructs may not be nested inside an atomic region.
3083 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region_atomic);
3084 return true;
3085 }
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003086 if (CurrentRegion == OMPD_section) {
3087 // OpenMP [2.7.2, sections Construct, Restrictions]
3088 // Orphaned section directives are prohibited. That is, the section
3089 // directives must appear within the sections construct and must not be
3090 // encountered elsewhere in the sections region.
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003091 if (ParentRegion != OMPD_sections &&
3092 ParentRegion != OMPD_parallel_sections) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003093 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_section_directive)
3094 << (ParentRegion != OMPD_unknown)
3095 << getOpenMPDirectiveName(ParentRegion);
3096 return true;
3097 }
3098 return false;
3099 }
Alexey Bataev185e88d2019-01-08 15:53:42 +00003100 // Allow some constructs (except teams and cancellation constructs) to be
3101 // orphaned (they could be used in functions, called from OpenMP regions
3102 // with the required preconditions).
Kelvin Libf594a52016-12-17 05:48:59 +00003103 if (ParentRegion == OMPD_unknown &&
Alexey Bataev185e88d2019-01-08 15:53:42 +00003104 !isOpenMPNestingTeamsDirective(CurrentRegion) &&
3105 CurrentRegion != OMPD_cancellation_point &&
3106 CurrentRegion != OMPD_cancel)
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003107 return false;
Alexey Bataev80909872015-07-02 11:25:17 +00003108 if (CurrentRegion == OMPD_cancellation_point ||
3109 CurrentRegion == OMPD_cancel) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003110 // OpenMP [2.16, Nesting of Regions]
3111 // A cancellation point construct for which construct-type-clause is
3112 // taskgroup must be nested inside a task construct. A cancellation
3113 // point construct for which construct-type-clause is not taskgroup must
3114 // be closely nested inside an OpenMP construct that matches the type
3115 // specified in construct-type-clause.
Alexey Bataev80909872015-07-02 11:25:17 +00003116 // A cancel construct for which construct-type-clause is taskgroup must be
3117 // nested inside a task construct. A cancel construct for which
3118 // construct-type-clause is not taskgroup must be closely nested inside an
3119 // OpenMP construct that matches the type specified in
3120 // construct-type-clause.
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003121 NestingProhibited =
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003122 !((CancelRegion == OMPD_parallel &&
3123 (ParentRegion == OMPD_parallel ||
3124 ParentRegion == OMPD_target_parallel)) ||
Alexey Bataev25e5b442015-09-15 12:52:43 +00003125 (CancelRegion == OMPD_for &&
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003126 (ParentRegion == OMPD_for || ParentRegion == OMPD_parallel_for ||
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00003127 ParentRegion == OMPD_target_parallel_for ||
3128 ParentRegion == OMPD_distribute_parallel_for ||
Alexey Bataev16e79882017-11-22 21:12:03 +00003129 ParentRegion == OMPD_teams_distribute_parallel_for ||
3130 ParentRegion == OMPD_target_teams_distribute_parallel_for)) ||
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003131 (CancelRegion == OMPD_taskgroup && ParentRegion == OMPD_task) ||
3132 (CancelRegion == OMPD_sections &&
Alexey Bataev25e5b442015-09-15 12:52:43 +00003133 (ParentRegion == OMPD_section || ParentRegion == OMPD_sections ||
3134 ParentRegion == OMPD_parallel_sections)));
Alexey Bataev185e88d2019-01-08 15:53:42 +00003135 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003136 } else if (CurrentRegion == OMPD_master) {
Alexander Musman80c22892014-07-17 08:54:58 +00003137 // OpenMP [2.16, Nesting of Regions]
3138 // A master region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003139 // atomic, or explicit task region.
Alexander Musman80c22892014-07-17 08:54:58 +00003140 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003141 isOpenMPTaskingDirective(ParentRegion);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003142 } else if (CurrentRegion == OMPD_critical && CurrentName.getName()) {
3143 // OpenMP [2.16, Nesting of Regions]
3144 // A critical region may not be nested (closely or otherwise) inside a
3145 // critical region with the same name. Note that this restriction is not
3146 // sufficient to prevent deadlock.
3147 SourceLocation PreviousCriticalLoc;
David Majnemer9d168222016-08-05 17:44:54 +00003148 bool DeadLock = Stack->hasDirective(
3149 [CurrentName, &PreviousCriticalLoc](OpenMPDirectiveKind K,
3150 const DeclarationNameInfo &DNI,
Alexey Bataeve3727102018-04-18 15:57:46 +00003151 SourceLocation Loc) {
David Majnemer9d168222016-08-05 17:44:54 +00003152 if (K == OMPD_critical && DNI.getName() == CurrentName.getName()) {
3153 PreviousCriticalLoc = Loc;
3154 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003155 }
3156 return false;
David Majnemer9d168222016-08-05 17:44:54 +00003157 },
3158 false /* skip top directive */);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003159 if (DeadLock) {
3160 SemaRef.Diag(StartLoc,
3161 diag::err_omp_prohibited_region_critical_same_name)
3162 << CurrentName.getName();
3163 if (PreviousCriticalLoc.isValid())
3164 SemaRef.Diag(PreviousCriticalLoc,
3165 diag::note_omp_previous_critical_region);
3166 return true;
3167 }
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003168 } else if (CurrentRegion == OMPD_barrier) {
3169 // OpenMP [2.16, Nesting of Regions]
3170 // A barrier region may not be closely nested inside a worksharing,
Alexey Bataev0162e452014-07-22 10:10:35 +00003171 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003172 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3173 isOpenMPTaskingDirective(ParentRegion) ||
3174 ParentRegion == OMPD_master ||
3175 ParentRegion == OMPD_critical ||
3176 ParentRegion == OMPD_ordered;
Alexander Musman80c22892014-07-17 08:54:58 +00003177 } else if (isOpenMPWorksharingDirective(CurrentRegion) &&
Kelvin Li579e41c2016-11-30 23:51:03 +00003178 !isOpenMPParallelDirective(CurrentRegion) &&
3179 !isOpenMPTeamsDirective(CurrentRegion)) {
Alexey Bataev549210e2014-06-24 04:39:47 +00003180 // OpenMP [2.16, Nesting of Regions]
3181 // A worksharing region may not be closely nested inside a worksharing,
3182 // explicit task, critical, ordered, atomic, or master region.
Alexey Bataev35aaee62016-04-13 13:36:48 +00003183 NestingProhibited = isOpenMPWorksharingDirective(ParentRegion) ||
3184 isOpenMPTaskingDirective(ParentRegion) ||
3185 ParentRegion == OMPD_master ||
3186 ParentRegion == OMPD_critical ||
3187 ParentRegion == OMPD_ordered;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003188 Recommend = ShouldBeInParallelRegion;
3189 } else if (CurrentRegion == OMPD_ordered) {
3190 // OpenMP [2.16, Nesting of Regions]
3191 // An ordered region may not be closely nested inside a critical,
Alexey Bataev0162e452014-07-22 10:10:35 +00003192 // atomic, or explicit task region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003193 // An ordered region must be closely nested inside a loop region (or
3194 // parallel loop region) with an ordered clause.
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003195 // OpenMP [2.8.1,simd Construct, Restrictions]
3196 // An ordered construct with the simd clause is the only OpenMP construct
3197 // that can appear in the simd region.
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003198 NestingProhibited = ParentRegion == OMPD_critical ||
Alexey Bataev35aaee62016-04-13 13:36:48 +00003199 isOpenMPTaskingDirective(ParentRegion) ||
Alexey Bataevd14d1e62015-09-28 06:39:35 +00003200 !(isOpenMPSimdDirective(ParentRegion) ||
3201 Stack->isParentOrderedRegion());
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003202 Recommend = ShouldBeInOrderedRegion;
Kelvin Libf594a52016-12-17 05:48:59 +00003203 } else if (isOpenMPNestingTeamsDirective(CurrentRegion)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003204 // OpenMP [2.16, Nesting of Regions]
3205 // If specified, a teams construct must be contained within a target
3206 // construct.
3207 NestingProhibited = ParentRegion != OMPD_target;
Kelvin Li2b51f722016-07-26 04:32:50 +00003208 OrphanSeen = ParentRegion == OMPD_unknown;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003209 Recommend = ShouldBeInTargetRegion;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003210 }
Kelvin Libf594a52016-12-17 05:48:59 +00003211 if (!NestingProhibited &&
3212 !isOpenMPTargetExecutionDirective(CurrentRegion) &&
3213 !isOpenMPTargetDataManagementDirective(CurrentRegion) &&
3214 (ParentRegion == OMPD_teams || ParentRegion == OMPD_target_teams)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00003215 // OpenMP [2.16, Nesting of Regions]
3216 // distribute, parallel, parallel sections, parallel workshare, and the
3217 // parallel loop and parallel loop SIMD constructs are the only OpenMP
3218 // constructs that can be closely nested in the teams region.
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003219 NestingProhibited = !isOpenMPParallelDirective(CurrentRegion) &&
3220 !isOpenMPDistributeDirective(CurrentRegion);
Alexey Bataev13314bf2014-10-09 04:18:56 +00003221 Recommend = ShouldBeInParallelRegion;
Alexey Bataev549210e2014-06-24 04:39:47 +00003222 }
David Majnemer9d168222016-08-05 17:44:54 +00003223 if (!NestingProhibited &&
Kelvin Li02532872016-08-05 14:37:37 +00003224 isOpenMPNestingDistributeDirective(CurrentRegion)) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003225 // OpenMP 4.5 [2.17 Nesting of Regions]
3226 // The region associated with the distribute construct must be strictly
3227 // nested inside a teams region
Kelvin Libf594a52016-12-17 05:48:59 +00003228 NestingProhibited =
3229 (ParentRegion != OMPD_teams && ParentRegion != OMPD_target_teams);
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003230 Recommend = ShouldBeInTeamsRegion;
3231 }
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003232 if (!NestingProhibited &&
3233 (isOpenMPTargetExecutionDirective(CurrentRegion) ||
3234 isOpenMPTargetDataManagementDirective(CurrentRegion))) {
3235 // OpenMP 4.5 [2.17 Nesting of Regions]
3236 // If a target, target update, target data, target enter data, or
3237 // target exit data construct is encountered during execution of a
3238 // target region, the behavior is unspecified.
3239 NestingProhibited = Stack->hasDirective(
Alexey Bataev7ace49d2016-05-17 08:55:33 +00003240 [&OffendingRegion](OpenMPDirectiveKind K, const DeclarationNameInfo &,
Alexey Bataeve3727102018-04-18 15:57:46 +00003241 SourceLocation) {
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003242 if (isOpenMPTargetExecutionDirective(K)) {
3243 OffendingRegion = K;
3244 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003245 }
3246 return false;
Arpith Chacko Jacob3d58f262016-02-02 04:00:47 +00003247 },
3248 false /* don't skip top directive */);
3249 CloseNesting = false;
3250 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003251 if (NestingProhibited) {
Kelvin Li2b51f722016-07-26 04:32:50 +00003252 if (OrphanSeen) {
3253 SemaRef.Diag(StartLoc, diag::err_omp_orphaned_device_directive)
3254 << getOpenMPDirectiveName(CurrentRegion) << Recommend;
3255 } else {
3256 SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
3257 << CloseNesting << getOpenMPDirectiveName(OffendingRegion)
3258 << Recommend << getOpenMPDirectiveName(CurrentRegion);
3259 }
Alexey Bataev549210e2014-06-24 04:39:47 +00003260 return true;
3261 }
3262 }
3263 return false;
3264}
3265
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003266static bool checkIfClauses(Sema &S, OpenMPDirectiveKind Kind,
3267 ArrayRef<OMPClause *> Clauses,
3268 ArrayRef<OpenMPDirectiveKind> AllowedNameModifiers) {
3269 bool ErrorFound = false;
3270 unsigned NamedModifiersNumber = 0;
3271 SmallVector<const OMPIfClause *, OMPC_unknown + 1> FoundNameModifiers(
3272 OMPD_unknown + 1);
Alexey Bataevecb156a2015-09-15 17:23:56 +00003273 SmallVector<SourceLocation, 4> NameModifierLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00003274 for (const OMPClause *C : Clauses) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003275 if (const auto *IC = dyn_cast_or_null<OMPIfClause>(C)) {
3276 // At most one if clause without a directive-name-modifier can appear on
3277 // the directive.
3278 OpenMPDirectiveKind CurNM = IC->getNameModifier();
3279 if (FoundNameModifiers[CurNM]) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003280 S.Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003281 << getOpenMPDirectiveName(Kind) << getOpenMPClauseName(OMPC_if)
3282 << (CurNM != OMPD_unknown) << getOpenMPDirectiveName(CurNM);
3283 ErrorFound = true;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003284 } else if (CurNM != OMPD_unknown) {
3285 NameModifierLoc.push_back(IC->getNameModifierLoc());
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003286 ++NamedModifiersNumber;
Alexey Bataevecb156a2015-09-15 17:23:56 +00003287 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003288 FoundNameModifiers[CurNM] = IC;
3289 if (CurNM == OMPD_unknown)
3290 continue;
3291 // Check if the specified name modifier is allowed for the current
3292 // directive.
3293 // At most one if clause with the particular directive-name-modifier can
3294 // appear on the directive.
3295 bool MatchFound = false;
3296 for (auto NM : AllowedNameModifiers) {
3297 if (CurNM == NM) {
3298 MatchFound = true;
3299 break;
3300 }
3301 }
3302 if (!MatchFound) {
3303 S.Diag(IC->getNameModifierLoc(),
3304 diag::err_omp_wrong_if_directive_name_modifier)
3305 << getOpenMPDirectiveName(CurNM) << getOpenMPDirectiveName(Kind);
3306 ErrorFound = true;
3307 }
3308 }
3309 }
3310 // If any if clause on the directive includes a directive-name-modifier then
3311 // all if clauses on the directive must include a directive-name-modifier.
3312 if (FoundNameModifiers[OMPD_unknown] && NamedModifiersNumber > 0) {
3313 if (NamedModifiersNumber == AllowedNameModifiers.size()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003314 S.Diag(FoundNameModifiers[OMPD_unknown]->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003315 diag::err_omp_no_more_if_clause);
3316 } else {
3317 std::string Values;
3318 std::string Sep(", ");
3319 unsigned AllowedCnt = 0;
3320 unsigned TotalAllowedNum =
3321 AllowedNameModifiers.size() - NamedModifiersNumber;
3322 for (unsigned Cnt = 0, End = AllowedNameModifiers.size(); Cnt < End;
3323 ++Cnt) {
3324 OpenMPDirectiveKind NM = AllowedNameModifiers[Cnt];
3325 if (!FoundNameModifiers[NM]) {
3326 Values += "'";
3327 Values += getOpenMPDirectiveName(NM);
3328 Values += "'";
3329 if (AllowedCnt + 2 == TotalAllowedNum)
3330 Values += " or ";
3331 else if (AllowedCnt + 1 != TotalAllowedNum)
3332 Values += Sep;
3333 ++AllowedCnt;
3334 }
3335 }
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003336 S.Diag(FoundNameModifiers[OMPD_unknown]->getCondition()->getBeginLoc(),
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003337 diag::err_omp_unnamed_if_clause)
3338 << (TotalAllowedNum > 1) << Values;
3339 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003340 for (SourceLocation Loc : NameModifierLoc) {
Alexey Bataevecb156a2015-09-15 17:23:56 +00003341 S.Diag(Loc, diag::note_omp_previous_named_if_clause);
3342 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003343 ErrorFound = true;
3344 }
3345 return ErrorFound;
3346}
3347
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003348StmtResult Sema::ActOnOpenMPExecutableDirective(
3349 OpenMPDirectiveKind Kind, const DeclarationNameInfo &DirName,
3350 OpenMPDirectiveKind CancelRegion, ArrayRef<OMPClause *> Clauses,
3351 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003352 StmtResult Res = StmtError();
Jonas Hahnfeld64a9e3c2017-02-22 06:49:10 +00003353 // First check CancelRegion which is then used in checkNestingOfRegions.
3354 if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
3355 checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003356 StartLoc))
Alexey Bataev549210e2014-06-24 04:39:47 +00003357 return StmtError();
Alexey Bataev758e55e2013-09-06 18:03:48 +00003358
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003359 llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
Alexey Bataeve3727102018-04-18 15:57:46 +00003360 VarsWithInheritedDSAType VarsWithInheritedDSA;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003361 bool ErrorFound = false;
Alexey Bataev6125da92014-07-21 11:26:11 +00003362 ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
Alexey Bataev0dce2ea2017-09-21 14:06:59 +00003363 if (AStmt && !CurContext->isDependentContext()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003364 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
3365
3366 // Check default data sharing attributes for referenced variables.
3367 DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
Arpith Chacko Jacob1f46b702017-01-23 15:38:49 +00003368 int ThisCaptureLevel = getOpenMPCaptureLevels(Kind);
3369 Stmt *S = AStmt;
3370 while (--ThisCaptureLevel >= 0)
3371 S = cast<CapturedStmt>(S)->getCapturedStmt();
3372 DSAChecker.Visit(S);
Alexey Bataev68446b72014-07-18 07:47:19 +00003373 if (DSAChecker.isErrorFound())
3374 return StmtError();
3375 // Generate list of implicitly defined firstprivate variables.
3376 VarsWithInheritedDSA = DSAChecker.getVarsWithInheritedDSA();
Alexey Bataev68446b72014-07-18 07:47:19 +00003377
Alexey Bataev88202be2017-07-27 13:20:36 +00003378 SmallVector<Expr *, 4> ImplicitFirstprivates(
3379 DSAChecker.getImplicitFirstprivate().begin(),
3380 DSAChecker.getImplicitFirstprivate().end());
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003381 SmallVector<Expr *, 4> ImplicitMaps(DSAChecker.getImplicitMap().begin(),
3382 DSAChecker.getImplicitMap().end());
Alexey Bataev88202be2017-07-27 13:20:36 +00003383 // Mark taskgroup task_reduction descriptors as implicitly firstprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +00003384 for (OMPClause *C : Clauses) {
Alexey Bataev88202be2017-07-27 13:20:36 +00003385 if (auto *IRC = dyn_cast<OMPInReductionClause>(C)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00003386 for (Expr *E : IRC->taskgroup_descriptors())
Alexey Bataev88202be2017-07-27 13:20:36 +00003387 if (E)
3388 ImplicitFirstprivates.emplace_back(E);
3389 }
3390 }
3391 if (!ImplicitFirstprivates.empty()) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003392 if (OMPClause *Implicit = ActOnOpenMPFirstprivateClause(
Alexey Bataev88202be2017-07-27 13:20:36 +00003393 ImplicitFirstprivates, SourceLocation(), SourceLocation(),
3394 SourceLocation())) {
Alexey Bataev68446b72014-07-18 07:47:19 +00003395 ClausesWithImplicit.push_back(Implicit);
3396 ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
Alexey Bataev88202be2017-07-27 13:20:36 +00003397 ImplicitFirstprivates.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003398 } else {
Alexey Bataev68446b72014-07-18 07:47:19 +00003399 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003400 }
Alexey Bataev68446b72014-07-18 07:47:19 +00003401 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003402 if (!ImplicitMaps.empty()) {
3403 if (OMPClause *Implicit = ActOnOpenMPMapClause(
Kelvin Lief579432018-12-18 22:18:41 +00003404 llvm::None, llvm::None, OMPC_MAP_tofrom,
3405 /*IsMapTypeImplicit=*/true, SourceLocation(), SourceLocation(),
3406 ImplicitMaps, SourceLocation(), SourceLocation(),
3407 SourceLocation())) {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003408 ClausesWithImplicit.emplace_back(Implicit);
3409 ErrorFound |=
3410 cast<OMPMapClause>(Implicit)->varlist_size() != ImplicitMaps.size();
Alexey Bataeve3727102018-04-18 15:57:46 +00003411 } else {
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003412 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00003413 }
Alexey Bataevf47c4b42017-09-26 13:47:31 +00003414 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003415 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00003416
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003417 llvm::SmallVector<OpenMPDirectiveKind, 4> AllowedNameModifiers;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003418 switch (Kind) {
3419 case OMPD_parallel:
Alexey Bataeved09d242014-05-28 05:53:51 +00003420 Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt, StartLoc,
3421 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003422 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003423 break;
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003424 case OMPD_simd:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003425 Res = ActOnOpenMPSimdDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3426 VarsWithInheritedDSA);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00003427 break;
Alexey Bataevf29276e2014-06-18 04:14:57 +00003428 case OMPD_for:
Alexey Bataev4acb8592014-07-07 13:01:15 +00003429 Res = ActOnOpenMPForDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc,
3430 VarsWithInheritedDSA);
Alexey Bataevf29276e2014-06-18 04:14:57 +00003431 break;
Alexander Musmanf82886e2014-09-18 05:12:34 +00003432 case OMPD_for_simd:
3433 Res = ActOnOpenMPForSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3434 EndLoc, VarsWithInheritedDSA);
3435 break;
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00003436 case OMPD_sections:
3437 Res = ActOnOpenMPSectionsDirective(ClausesWithImplicit, AStmt, StartLoc,
3438 EndLoc);
3439 break;
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003440 case OMPD_section:
3441 assert(ClausesWithImplicit.empty() &&
Alexander Musman80c22892014-07-17 08:54:58 +00003442 "No clauses are allowed for 'omp section' directive");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00003443 Res = ActOnOpenMPSectionDirective(AStmt, StartLoc, EndLoc);
3444 break;
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00003445 case OMPD_single:
3446 Res = ActOnOpenMPSingleDirective(ClausesWithImplicit, AStmt, StartLoc,
3447 EndLoc);
3448 break;
Alexander Musman80c22892014-07-17 08:54:58 +00003449 case OMPD_master:
3450 assert(ClausesWithImplicit.empty() &&
3451 "No clauses are allowed for 'omp master' directive");
3452 Res = ActOnOpenMPMasterDirective(AStmt, StartLoc, EndLoc);
3453 break;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003454 case OMPD_critical:
Alexey Bataev28c75412015-12-15 08:19:24 +00003455 Res = ActOnOpenMPCriticalDirective(DirName, ClausesWithImplicit, AStmt,
3456 StartLoc, EndLoc);
Alexander Musmand9ed09f2014-07-21 09:42:05 +00003457 break;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003458 case OMPD_parallel_for:
3459 Res = ActOnOpenMPParallelForDirective(ClausesWithImplicit, AStmt, StartLoc,
3460 EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003461 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev4acb8592014-07-07 13:01:15 +00003462 break;
Alexander Musmane4e893b2014-09-23 09:33:00 +00003463 case OMPD_parallel_for_simd:
3464 Res = ActOnOpenMPParallelForSimdDirective(
3465 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003466 AllowedNameModifiers.push_back(OMPD_parallel);
Alexander Musmane4e893b2014-09-23 09:33:00 +00003467 break;
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003468 case OMPD_parallel_sections:
3469 Res = ActOnOpenMPParallelSectionsDirective(ClausesWithImplicit, AStmt,
3470 StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003471 AllowedNameModifiers.push_back(OMPD_parallel);
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00003472 break;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003473 case OMPD_task:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003474 Res =
3475 ActOnOpenMPTaskDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003476 AllowedNameModifiers.push_back(OMPD_task);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003477 break;
Alexey Bataev68446b72014-07-18 07:47:19 +00003478 case OMPD_taskyield:
3479 assert(ClausesWithImplicit.empty() &&
3480 "No clauses are allowed for 'omp taskyield' directive");
3481 assert(AStmt == nullptr &&
3482 "No associated statement allowed for 'omp taskyield' directive");
3483 Res = ActOnOpenMPTaskyieldDirective(StartLoc, EndLoc);
3484 break;
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00003485 case OMPD_barrier:
3486 assert(ClausesWithImplicit.empty() &&
3487 "No clauses are allowed for 'omp barrier' directive");
3488 assert(AStmt == nullptr &&
3489 "No associated statement allowed for 'omp barrier' directive");
3490 Res = ActOnOpenMPBarrierDirective(StartLoc, EndLoc);
3491 break;
Alexey Bataev2df347a2014-07-18 10:17:07 +00003492 case OMPD_taskwait:
3493 assert(ClausesWithImplicit.empty() &&
3494 "No clauses are allowed for 'omp taskwait' directive");
3495 assert(AStmt == nullptr &&
3496 "No associated statement allowed for 'omp taskwait' directive");
3497 Res = ActOnOpenMPTaskwaitDirective(StartLoc, EndLoc);
3498 break;
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003499 case OMPD_taskgroup:
Alexey Bataev169d96a2017-07-18 20:17:46 +00003500 Res = ActOnOpenMPTaskgroupDirective(ClausesWithImplicit, AStmt, StartLoc,
3501 EndLoc);
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00003502 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00003503 case OMPD_flush:
3504 assert(AStmt == nullptr &&
3505 "No associated statement allowed for 'omp flush' directive");
3506 Res = ActOnOpenMPFlushDirective(ClausesWithImplicit, StartLoc, EndLoc);
3507 break;
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003508 case OMPD_ordered:
Alexey Bataev346265e2015-09-25 10:37:12 +00003509 Res = ActOnOpenMPOrderedDirective(ClausesWithImplicit, AStmt, StartLoc,
3510 EndLoc);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00003511 break;
Alexey Bataev0162e452014-07-22 10:10:35 +00003512 case OMPD_atomic:
3513 Res = ActOnOpenMPAtomicDirective(ClausesWithImplicit, AStmt, StartLoc,
3514 EndLoc);
3515 break;
Alexey Bataev13314bf2014-10-09 04:18:56 +00003516 case OMPD_teams:
3517 Res =
3518 ActOnOpenMPTeamsDirective(ClausesWithImplicit, AStmt, StartLoc, EndLoc);
3519 break;
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003520 case OMPD_target:
3521 Res = ActOnOpenMPTargetDirective(ClausesWithImplicit, AStmt, StartLoc,
3522 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003523 AllowedNameModifiers.push_back(OMPD_target);
Alexey Bataev0bd520b2014-09-19 08:19:49 +00003524 break;
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00003525 case OMPD_target_parallel:
3526 Res = ActOnOpenMPTargetParallelDirective(ClausesWithImplicit, AStmt,
3527 StartLoc, EndLoc);
3528 AllowedNameModifiers.push_back(OMPD_target);
3529 AllowedNameModifiers.push_back(OMPD_parallel);
3530 break;
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00003531 case OMPD_target_parallel_for:
3532 Res = ActOnOpenMPTargetParallelForDirective(
3533 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3534 AllowedNameModifiers.push_back(OMPD_target);
3535 AllowedNameModifiers.push_back(OMPD_parallel);
3536 break;
Alexey Bataev6d4ed052015-07-01 06:57:41 +00003537 case OMPD_cancellation_point:
3538 assert(ClausesWithImplicit.empty() &&
3539 "No clauses are allowed for 'omp cancellation point' directive");
3540 assert(AStmt == nullptr && "No associated statement allowed for 'omp "
3541 "cancellation point' directive");
3542 Res = ActOnOpenMPCancellationPointDirective(StartLoc, EndLoc, CancelRegion);
3543 break;
Alexey Bataev80909872015-07-02 11:25:17 +00003544 case OMPD_cancel:
Alexey Bataev80909872015-07-02 11:25:17 +00003545 assert(AStmt == nullptr &&
3546 "No associated statement allowed for 'omp cancel' directive");
Alexey Bataev87933c72015-09-18 08:07:34 +00003547 Res = ActOnOpenMPCancelDirective(ClausesWithImplicit, StartLoc, EndLoc,
3548 CancelRegion);
3549 AllowedNameModifiers.push_back(OMPD_cancel);
Alexey Bataev80909872015-07-02 11:25:17 +00003550 break;
Michael Wong65f367f2015-07-21 13:44:28 +00003551 case OMPD_target_data:
3552 Res = ActOnOpenMPTargetDataDirective(ClausesWithImplicit, AStmt, StartLoc,
3553 EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003554 AllowedNameModifiers.push_back(OMPD_target_data);
Michael Wong65f367f2015-07-21 13:44:28 +00003555 break;
Samuel Antaodf67fc42016-01-19 19:15:56 +00003556 case OMPD_target_enter_data:
3557 Res = ActOnOpenMPTargetEnterDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003558 EndLoc, AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00003559 AllowedNameModifiers.push_back(OMPD_target_enter_data);
3560 break;
Samuel Antao72590762016-01-19 20:04:50 +00003561 case OMPD_target_exit_data:
3562 Res = ActOnOpenMPTargetExitDataDirective(ClausesWithImplicit, StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00003563 EndLoc, AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00003564 AllowedNameModifiers.push_back(OMPD_target_exit_data);
3565 break;
Alexey Bataev49f6e782015-12-01 04:18:41 +00003566 case OMPD_taskloop:
3567 Res = ActOnOpenMPTaskLoopDirective(ClausesWithImplicit, AStmt, StartLoc,
3568 EndLoc, VarsWithInheritedDSA);
3569 AllowedNameModifiers.push_back(OMPD_taskloop);
3570 break;
Alexey Bataev0a6ed842015-12-03 09:40:15 +00003571 case OMPD_taskloop_simd:
3572 Res = ActOnOpenMPTaskLoopSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3573 EndLoc, VarsWithInheritedDSA);
3574 AllowedNameModifiers.push_back(OMPD_taskloop);
3575 break;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00003576 case OMPD_distribute:
3577 Res = ActOnOpenMPDistributeDirective(ClausesWithImplicit, AStmt, StartLoc,
3578 EndLoc, VarsWithInheritedDSA);
3579 break;
Samuel Antao686c70c2016-05-26 17:30:50 +00003580 case OMPD_target_update:
Alexey Bataev7828b252017-11-21 17:08:48 +00003581 Res = ActOnOpenMPTargetUpdateDirective(ClausesWithImplicit, StartLoc,
3582 EndLoc, AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00003583 AllowedNameModifiers.push_back(OMPD_target_update);
3584 break;
Carlo Bertolli9925f152016-06-27 14:55:37 +00003585 case OMPD_distribute_parallel_for:
3586 Res = ActOnOpenMPDistributeParallelForDirective(
3587 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3588 AllowedNameModifiers.push_back(OMPD_parallel);
3589 break;
Kelvin Li4a39add2016-07-05 05:00:15 +00003590 case OMPD_distribute_parallel_for_simd:
3591 Res = ActOnOpenMPDistributeParallelForSimdDirective(
3592 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3593 AllowedNameModifiers.push_back(OMPD_parallel);
3594 break;
Kelvin Li787f3fc2016-07-06 04:45:38 +00003595 case OMPD_distribute_simd:
3596 Res = ActOnOpenMPDistributeSimdDirective(
3597 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3598 break;
Kelvin Lia579b912016-07-14 02:54:56 +00003599 case OMPD_target_parallel_for_simd:
3600 Res = ActOnOpenMPTargetParallelForSimdDirective(
3601 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3602 AllowedNameModifiers.push_back(OMPD_target);
3603 AllowedNameModifiers.push_back(OMPD_parallel);
3604 break;
Kelvin Li986330c2016-07-20 22:57:10 +00003605 case OMPD_target_simd:
3606 Res = ActOnOpenMPTargetSimdDirective(ClausesWithImplicit, AStmt, StartLoc,
3607 EndLoc, VarsWithInheritedDSA);
3608 AllowedNameModifiers.push_back(OMPD_target);
3609 break;
Kelvin Li02532872016-08-05 14:37:37 +00003610 case OMPD_teams_distribute:
David Majnemer9d168222016-08-05 17:44:54 +00003611 Res = ActOnOpenMPTeamsDistributeDirective(
3612 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
Kelvin Li02532872016-08-05 14:37:37 +00003613 break;
Kelvin Li4e325f72016-10-25 12:50:55 +00003614 case OMPD_teams_distribute_simd:
3615 Res = ActOnOpenMPTeamsDistributeSimdDirective(
3616 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3617 break;
Kelvin Li579e41c2016-11-30 23:51:03 +00003618 case OMPD_teams_distribute_parallel_for_simd:
3619 Res = ActOnOpenMPTeamsDistributeParallelForSimdDirective(
3620 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3621 AllowedNameModifiers.push_back(OMPD_parallel);
3622 break;
Kelvin Li7ade93f2016-12-09 03:24:30 +00003623 case OMPD_teams_distribute_parallel_for:
3624 Res = ActOnOpenMPTeamsDistributeParallelForDirective(
3625 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3626 AllowedNameModifiers.push_back(OMPD_parallel);
3627 break;
Kelvin Libf594a52016-12-17 05:48:59 +00003628 case OMPD_target_teams:
3629 Res = ActOnOpenMPTargetTeamsDirective(ClausesWithImplicit, AStmt, StartLoc,
3630 EndLoc);
3631 AllowedNameModifiers.push_back(OMPD_target);
3632 break;
Kelvin Li83c451e2016-12-25 04:52:54 +00003633 case OMPD_target_teams_distribute:
3634 Res = ActOnOpenMPTargetTeamsDistributeDirective(
3635 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3636 AllowedNameModifiers.push_back(OMPD_target);
3637 break;
Kelvin Li80e8f562016-12-29 22:16:30 +00003638 case OMPD_target_teams_distribute_parallel_for:
3639 Res = ActOnOpenMPTargetTeamsDistributeParallelForDirective(
3640 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3641 AllowedNameModifiers.push_back(OMPD_target);
3642 AllowedNameModifiers.push_back(OMPD_parallel);
3643 break;
Kelvin Li1851df52017-01-03 05:23:48 +00003644 case OMPD_target_teams_distribute_parallel_for_simd:
3645 Res = ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
3646 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3647 AllowedNameModifiers.push_back(OMPD_target);
3648 AllowedNameModifiers.push_back(OMPD_parallel);
3649 break;
Kelvin Lida681182017-01-10 18:08:18 +00003650 case OMPD_target_teams_distribute_simd:
3651 Res = ActOnOpenMPTargetTeamsDistributeSimdDirective(
3652 ClausesWithImplicit, AStmt, StartLoc, EndLoc, VarsWithInheritedDSA);
3653 AllowedNameModifiers.push_back(OMPD_target);
3654 break;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +00003655 case OMPD_declare_target:
3656 case OMPD_end_declare_target:
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00003657 case OMPD_threadprivate:
Alexey Bataev94a4f0c2016-03-03 05:21:39 +00003658 case OMPD_declare_reduction:
Alexey Bataev587e1de2016-03-30 10:43:55 +00003659 case OMPD_declare_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00003660 case OMPD_requires:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003661 llvm_unreachable("OpenMP Directive is not allowed");
3662 case OMPD_unknown:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003663 llvm_unreachable("Unknown OpenMP directive");
3664 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +00003665
Alexey Bataeve3727102018-04-18 15:57:46 +00003666 for (const auto &P : VarsWithInheritedDSA) {
Alexey Bataev4acb8592014-07-07 13:01:15 +00003667 Diag(P.second->getExprLoc(), diag::err_omp_no_dsa_for_variable)
3668 << P.first << P.second->getSourceRange();
3669 }
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003670 ErrorFound = !VarsWithInheritedDSA.empty() || ErrorFound;
3671
3672 if (!AllowedNameModifiers.empty())
3673 ErrorFound = checkIfClauses(*this, Kind, Clauses, AllowedNameModifiers) ||
3674 ErrorFound;
Alexey Bataev4acb8592014-07-07 13:01:15 +00003675
Alexey Bataeved09d242014-05-28 05:53:51 +00003676 if (ErrorFound)
3677 return StmtError();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003678 return Res;
3679}
3680
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003681Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareSimdDirective(
3682 DeclGroupPtrTy DG, OMPDeclareSimdDeclAttr::BranchStateTy BS, Expr *Simdlen,
Alexey Bataevd93d3762016-04-12 09:35:56 +00003683 ArrayRef<Expr *> Uniforms, ArrayRef<Expr *> Aligneds,
Alexey Bataevecba70f2016-04-12 11:02:11 +00003684 ArrayRef<Expr *> Alignments, ArrayRef<Expr *> Linears,
3685 ArrayRef<unsigned> LinModifiers, ArrayRef<Expr *> Steps, SourceRange SR) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003686 assert(Aligneds.size() == Alignments.size());
Alexey Bataevecba70f2016-04-12 11:02:11 +00003687 assert(Linears.size() == LinModifiers.size());
3688 assert(Linears.size() == Steps.size());
Alexey Bataev587e1de2016-03-30 10:43:55 +00003689 if (!DG || DG.get().isNull())
3690 return DeclGroupPtrTy();
3691
3692 if (!DG.get().isSingleDecl()) {
Alexey Bataev20dfd772016-04-04 10:12:15 +00003693 Diag(SR.getBegin(), diag::err_omp_single_decl_in_declare_simd);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003694 return DG;
3695 }
Alexey Bataeve3727102018-04-18 15:57:46 +00003696 Decl *ADecl = DG.get().getSingleDecl();
Alexey Bataev587e1de2016-03-30 10:43:55 +00003697 if (auto *FTD = dyn_cast<FunctionTemplateDecl>(ADecl))
3698 ADecl = FTD->getTemplatedDecl();
3699
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003700 auto *FD = dyn_cast<FunctionDecl>(ADecl);
3701 if (!FD) {
3702 Diag(ADecl->getLocation(), diag::err_omp_function_expected);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003703 return DeclGroupPtrTy();
3704 }
3705
Alexey Bataev2af33e32016-04-07 12:45:37 +00003706 // OpenMP [2.8.2, declare simd construct, Description]
3707 // The parameter of the simdlen clause must be a constant positive integer
3708 // expression.
3709 ExprResult SL;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003710 if (Simdlen)
Alexey Bataev2af33e32016-04-07 12:45:37 +00003711 SL = VerifyPositiveIntegerConstantInClause(Simdlen, OMPC_simdlen);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003712 // OpenMP [2.8.2, declare simd construct, Description]
3713 // The special this pointer can be used as if was one of the arguments to the
3714 // function in any of the linear, aligned, or uniform clauses.
3715 // The uniform clause declares one or more arguments to have an invariant
3716 // value for all concurrent invocations of the function in the execution of a
3717 // single SIMD loop.
Alexey Bataeve3727102018-04-18 15:57:46 +00003718 llvm::DenseMap<const Decl *, const Expr *> UniformedArgs;
3719 const Expr *UniformedLinearThis = nullptr;
3720 for (const Expr *E : Uniforms) {
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003721 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003722 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3723 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl()))
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003724 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3725 FD->getParamDecl(PVD->getFunctionScopeIndex())
Alexey Bataevecba70f2016-04-12 11:02:11 +00003726 ->getCanonicalDecl() == PVD->getCanonicalDecl()) {
Alexey Bataev43a919f2018-04-13 17:48:43 +00003727 UniformedArgs.try_emplace(PVD->getCanonicalDecl(), E);
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003728 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003729 }
3730 if (isa<CXXThisExpr>(E)) {
3731 UniformedLinearThis = E;
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003732 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003733 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003734 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3735 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
Alexey Bataev2af33e32016-04-07 12:45:37 +00003736 }
Alexey Bataevd93d3762016-04-12 09:35:56 +00003737 // OpenMP [2.8.2, declare simd construct, Description]
3738 // The aligned clause declares that the object to which each list item points
3739 // is aligned to the number of bytes expressed in the optional parameter of
3740 // the aligned clause.
3741 // The special this pointer can be used as if was one of the arguments to the
3742 // function in any of the linear, aligned, or uniform clauses.
3743 // The type of list items appearing in the aligned clause must be array,
3744 // pointer, reference to array, or reference to pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +00003745 llvm::DenseMap<const Decl *, const Expr *> AlignedArgs;
3746 const Expr *AlignedThis = nullptr;
3747 for (const Expr *E : Aligneds) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003748 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003749 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3750 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3751 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevd93d3762016-04-12 09:35:56 +00003752 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3753 FD->getParamDecl(PVD->getFunctionScopeIndex())
3754 ->getCanonicalDecl() == CanonPVD) {
3755 // OpenMP [2.8.1, simd construct, Restrictions]
3756 // A list-item cannot appear in more than one aligned clause.
3757 if (AlignedArgs.count(CanonPVD) > 0) {
3758 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3759 << 1 << E->getSourceRange();
3760 Diag(AlignedArgs[CanonPVD]->getExprLoc(),
3761 diag::note_omp_explicit_dsa)
3762 << getOpenMPClauseName(OMPC_aligned);
3763 continue;
3764 }
3765 AlignedArgs[CanonPVD] = E;
3766 QualType QTy = PVD->getType()
3767 .getNonReferenceType()
3768 .getUnqualifiedType()
3769 .getCanonicalType();
3770 const Type *Ty = QTy.getTypePtrOrNull();
3771 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
3772 Diag(E->getExprLoc(), diag::err_omp_aligned_expected_array_or_ptr)
3773 << QTy << getLangOpts().CPlusPlus << E->getSourceRange();
3774 Diag(PVD->getLocation(), diag::note_previous_decl) << PVD;
3775 }
3776 continue;
3777 }
3778 }
3779 if (isa<CXXThisExpr>(E)) {
3780 if (AlignedThis) {
3781 Diag(E->getExprLoc(), diag::err_omp_aligned_twice)
3782 << 2 << E->getSourceRange();
3783 Diag(AlignedThis->getExprLoc(), diag::note_omp_explicit_dsa)
3784 << getOpenMPClauseName(OMPC_aligned);
3785 }
3786 AlignedThis = E;
3787 continue;
3788 }
3789 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3790 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3791 }
3792 // The optional parameter of the aligned clause, alignment, must be a constant
3793 // positive integer expression. If no optional parameter is specified,
3794 // implementation-defined default alignments for SIMD instructions on the
3795 // target platforms are assumed.
Alexey Bataeve3727102018-04-18 15:57:46 +00003796 SmallVector<const Expr *, 4> NewAligns;
3797 for (Expr *E : Alignments) {
Alexey Bataevd93d3762016-04-12 09:35:56 +00003798 ExprResult Align;
3799 if (E)
3800 Align = VerifyPositiveIntegerConstantInClause(E, OMPC_aligned);
3801 NewAligns.push_back(Align.get());
3802 }
Alexey Bataevecba70f2016-04-12 11:02:11 +00003803 // OpenMP [2.8.2, declare simd construct, Description]
3804 // The linear clause declares one or more list items to be private to a SIMD
3805 // lane and to have a linear relationship with respect to the iteration space
3806 // of a loop.
3807 // The special this pointer can be used as if was one of the arguments to the
3808 // function in any of the linear, aligned, or uniform clauses.
3809 // When a linear-step expression is specified in a linear clause it must be
3810 // either a constant integer expression or an integer-typed parameter that is
3811 // specified in a uniform clause on the directive.
Alexey Bataeve3727102018-04-18 15:57:46 +00003812 llvm::DenseMap<const Decl *, const Expr *> LinearArgs;
Alexey Bataevecba70f2016-04-12 11:02:11 +00003813 const bool IsUniformedThis = UniformedLinearThis != nullptr;
3814 auto MI = LinModifiers.begin();
Alexey Bataeve3727102018-04-18 15:57:46 +00003815 for (const Expr *E : Linears) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003816 auto LinKind = static_cast<OpenMPLinearClauseKind>(*MI);
3817 ++MI;
3818 E = E->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00003819 if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
3820 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3821 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003822 if (FD->getNumParams() > PVD->getFunctionScopeIndex() &&
3823 FD->getParamDecl(PVD->getFunctionScopeIndex())
3824 ->getCanonicalDecl() == CanonPVD) {
3825 // OpenMP [2.15.3.7, linear Clause, Restrictions]
3826 // A list-item cannot appear in more than one linear clause.
3827 if (LinearArgs.count(CanonPVD) > 0) {
3828 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3829 << getOpenMPClauseName(OMPC_linear)
3830 << getOpenMPClauseName(OMPC_linear) << E->getSourceRange();
3831 Diag(LinearArgs[CanonPVD]->getExprLoc(),
3832 diag::note_omp_explicit_dsa)
3833 << getOpenMPClauseName(OMPC_linear);
3834 continue;
3835 }
3836 // Each argument can appear in at most one uniform or linear clause.
3837 if (UniformedArgs.count(CanonPVD) > 0) {
3838 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3839 << getOpenMPClauseName(OMPC_linear)
3840 << getOpenMPClauseName(OMPC_uniform) << E->getSourceRange();
3841 Diag(UniformedArgs[CanonPVD]->getExprLoc(),
3842 diag::note_omp_explicit_dsa)
3843 << getOpenMPClauseName(OMPC_uniform);
3844 continue;
3845 }
3846 LinearArgs[CanonPVD] = E;
3847 if (E->isValueDependent() || E->isTypeDependent() ||
3848 E->isInstantiationDependent() ||
3849 E->containsUnexpandedParameterPack())
3850 continue;
3851 (void)CheckOpenMPLinearDecl(CanonPVD, E->getExprLoc(), LinKind,
3852 PVD->getOriginalType());
3853 continue;
3854 }
3855 }
3856 if (isa<CXXThisExpr>(E)) {
3857 if (UniformedLinearThis) {
3858 Diag(E->getExprLoc(), diag::err_omp_wrong_dsa)
3859 << getOpenMPClauseName(OMPC_linear)
3860 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform : OMPC_linear)
3861 << E->getSourceRange();
3862 Diag(UniformedLinearThis->getExprLoc(), diag::note_omp_explicit_dsa)
3863 << getOpenMPClauseName(IsUniformedThis ? OMPC_uniform
3864 : OMPC_linear);
3865 continue;
3866 }
3867 UniformedLinearThis = E;
3868 if (E->isValueDependent() || E->isTypeDependent() ||
3869 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
3870 continue;
3871 (void)CheckOpenMPLinearDecl(/*D=*/nullptr, E->getExprLoc(), LinKind,
3872 E->getType());
3873 continue;
3874 }
3875 Diag(E->getExprLoc(), diag::err_omp_param_or_this_in_clause)
3876 << FD->getDeclName() << (isa<CXXMethodDecl>(ADecl) ? 1 : 0);
3877 }
3878 Expr *Step = nullptr;
3879 Expr *NewStep = nullptr;
3880 SmallVector<Expr *, 4> NewSteps;
Alexey Bataeve3727102018-04-18 15:57:46 +00003881 for (Expr *E : Steps) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003882 // Skip the same step expression, it was checked already.
3883 if (Step == E || !E) {
3884 NewSteps.push_back(E ? NewStep : nullptr);
3885 continue;
3886 }
3887 Step = E;
Alexey Bataeve3727102018-04-18 15:57:46 +00003888 if (const auto *DRE = dyn_cast<DeclRefExpr>(Step))
3889 if (const auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) {
3890 const VarDecl *CanonPVD = PVD->getCanonicalDecl();
Alexey Bataevecba70f2016-04-12 11:02:11 +00003891 if (UniformedArgs.count(CanonPVD) == 0) {
3892 Diag(Step->getExprLoc(), diag::err_omp_expected_uniform_param)
3893 << Step->getSourceRange();
3894 } else if (E->isValueDependent() || E->isTypeDependent() ||
3895 E->isInstantiationDependent() ||
3896 E->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00003897 CanonPVD->getType()->hasIntegerRepresentation()) {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003898 NewSteps.push_back(Step);
Alexey Bataeve3727102018-04-18 15:57:46 +00003899 } else {
Alexey Bataevecba70f2016-04-12 11:02:11 +00003900 Diag(Step->getExprLoc(), diag::err_omp_expected_int_param)
3901 << Step->getSourceRange();
3902 }
3903 continue;
3904 }
3905 NewStep = Step;
3906 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
3907 !Step->isInstantiationDependent() &&
3908 !Step->containsUnexpandedParameterPack()) {
3909 NewStep = PerformOpenMPImplicitIntegerConversion(Step->getExprLoc(), Step)
3910 .get();
3911 if (NewStep)
3912 NewStep = VerifyIntegerConstantExpression(NewStep).get();
3913 }
3914 NewSteps.push_back(NewStep);
3915 }
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00003916 auto *NewAttr = OMPDeclareSimdDeclAttr::CreateImplicit(
3917 Context, BS, SL.get(), const_cast<Expr **>(Uniforms.data()),
Alexey Bataevd93d3762016-04-12 09:35:56 +00003918 Uniforms.size(), const_cast<Expr **>(Aligneds.data()), Aligneds.size(),
Alexey Bataevecba70f2016-04-12 11:02:11 +00003919 const_cast<Expr **>(NewAligns.data()), NewAligns.size(),
3920 const_cast<Expr **>(Linears.data()), Linears.size(),
3921 const_cast<unsigned *>(LinModifiers.data()), LinModifiers.size(),
3922 NewSteps.data(), NewSteps.size(), SR);
Alexey Bataev587e1de2016-03-30 10:43:55 +00003923 ADecl->addAttr(NewAttr);
3924 return ConvertDeclToDeclGroup(ADecl);
3925}
3926
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003927StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
3928 Stmt *AStmt,
3929 SourceLocation StartLoc,
3930 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00003931 if (!AStmt)
3932 return StmtError();
3933
Alexey Bataeve3727102018-04-18 15:57:46 +00003934 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9959db52014-05-06 10:08:46 +00003935 // 1.2.2 OpenMP Language Terminology
3936 // Structured block - An executable statement with a single entry at the
3937 // top and a single exit at the bottom.
3938 // The point of exit cannot be a branch out of the structured block.
3939 // longjmp() and throw() must not violate the entry/exit criteria.
3940 CS->getCapturedDecl()->setNothrow();
3941
Reid Kleckner87a31802018-03-12 21:43:02 +00003942 setFunctionHasBranchProtectedScope();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003943
Alexey Bataev25e5b442015-09-15 12:52:43 +00003944 return OMPParallelDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
3945 DSAStack->isCancelRegion());
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00003946}
3947
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003948namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003949/// Helper class for checking canonical form of the OpenMP loops and
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003950/// extracting iteration space of each loop in the loop nest, that will be used
3951/// for IR generation.
3952class OpenMPIterationSpaceChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003953 /// Reference to Sema.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003954 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003955 /// A location for diagnostics (when there is no some better location).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003956 SourceLocation DefaultLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003957 /// A location for diagnostics (when increment is not compatible).
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003958 SourceLocation ConditionLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003959 /// A source location for referring to loop init later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003960 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003961 /// A source location for referring to condition later.
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003962 SourceRange ConditionSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003963 /// A source location for referring to increment later.
Alexander Musmana5f070a2014-10-01 06:03:56 +00003964 SourceRange IncrementSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003965 /// Loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003966 ValueDecl *LCDecl = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003967 /// Reference to loop variable.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003968 Expr *LCRef = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003969 /// Lower bound (initializer for the var).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003970 Expr *LB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003971 /// Upper bound.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003972 Expr *UB = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003973 /// Loop step (increment).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003974 Expr *Step = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003975 /// This flag is true when condition is one of:
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003976 /// Var < UB
3977 /// Var <= UB
3978 /// UB > Var
3979 /// UB >= Var
Kelvin Liefbe4af2018-11-21 19:10:48 +00003980 /// This will have no value when the condition is !=
3981 llvm::Optional<bool> TestIsLessOp;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003982 /// This flag is true when condition is strict ( < or > ).
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003983 bool TestIsStrictOp = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003984 /// This flag is true when step is subtracted on each iteration.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003985 bool SubtractStep = false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003986
3987public:
3988 OpenMPIterationSpaceChecker(Sema &SemaRef, SourceLocation DefaultLoc)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00003989 : SemaRef(SemaRef), DefaultLoc(DefaultLoc), ConditionLoc(DefaultLoc) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003990 /// Check init-expr for canonical loop form and save loop counter
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003991 /// variable - #Var and its initialization value - #LB.
Alexey Bataeve3727102018-04-18 15:57:46 +00003992 bool checkAndSetInit(Stmt *S, bool EmitDiags = true);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003993 /// Check test-expr for canonical form, save upper-bound (#UB), flags
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003994 /// for less/greater and for strict/non-strict comparison.
Alexey Bataeve3727102018-04-18 15:57:46 +00003995 bool checkAndSetCond(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003996 /// Check incr-expr for canonical loop form and return true if it
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00003997 /// does not conform, otherwise save loop step (#Step).
Alexey Bataeve3727102018-04-18 15:57:46 +00003998 bool checkAndSetInc(Expr *S);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00003999 /// Return the loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00004000 ValueDecl *getLoopDecl() const { return LCDecl; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004001 /// Return the reference expression to loop counter variable.
Alexey Bataeve3727102018-04-18 15:57:46 +00004002 Expr *getLoopDeclRefExpr() const { return LCRef; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004003 /// Source range of the loop init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004004 SourceRange getInitSrcRange() const { return InitSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004005 /// Source range of the loop condition.
Alexey Bataeve3727102018-04-18 15:57:46 +00004006 SourceRange getConditionSrcRange() const { return ConditionSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004007 /// Source range of the loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004008 SourceRange getIncrementSrcRange() const { return IncrementSrcRange; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004009 /// True if the step should be subtracted.
Alexey Bataeve3727102018-04-18 15:57:46 +00004010 bool shouldSubtractStep() const { return SubtractStep; }
Alexey Bataev316ccf62019-01-29 18:51:58 +00004011 /// True, if the compare operator is strict (<, > or !=).
4012 bool isStrictTestOp() const { return TestIsStrictOp; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004013 /// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004014 Expr *buildNumIterations(
4015 Scope *S, const bool LimitedType,
4016 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004017 /// Build the precondition expression for the loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00004018 Expr *
4019 buildPreCond(Scope *S, Expr *Cond,
4020 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004021 /// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004022 DeclRefExpr *
4023 buildCounterVar(llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4024 DSAStackTy &DSA) const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004025 /// Build reference expression to the private counter be used for
Alexey Bataeva8899172015-08-06 12:30:57 +00004026 /// codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004027 Expr *buildPrivateCounterVar() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004028 /// Build initialization of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004029 Expr *buildCounterInit() const;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004030 /// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004031 Expr *buildCounterStep() const;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004032 /// Build loop data with counter value for depend clauses in ordered
4033 /// directives.
4034 Expr *
4035 buildOrderedLoopData(Scope *S, Expr *Counter,
4036 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4037 SourceLocation Loc, Expr *Inc = nullptr,
4038 OverloadedOperatorKind OOK = OO_Amp);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004039 /// Return true if any expression is dependent.
Alexey Bataeve3727102018-04-18 15:57:46 +00004040 bool dependent() const;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004041
4042private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004043 /// Check the right-hand side of an assignment in the increment
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004044 /// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +00004045 bool checkAndSetIncRHS(Expr *RHS);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004046 /// Helper to set loop counter variable and its initializer.
Alexey Bataeve3727102018-04-18 15:57:46 +00004047 bool setLCDeclAndLB(ValueDecl *NewLCDecl, Expr *NewDeclRefExpr, Expr *NewLB);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004048 /// Helper to set upper bound.
Kelvin Liefbe4af2018-11-21 19:10:48 +00004049 bool setUB(Expr *NewUB, llvm::Optional<bool> LessOp, bool StrictOp,
4050 SourceRange SR, SourceLocation SL);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004051 /// Helper to set loop increment.
Alexey Bataeve3727102018-04-18 15:57:46 +00004052 bool setStep(Expr *NewStep, bool Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004053};
4054
Alexey Bataeve3727102018-04-18 15:57:46 +00004055bool OpenMPIterationSpaceChecker::dependent() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004056 if (!LCDecl) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004057 assert(!LB && !UB && !Step);
4058 return false;
4059 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004060 return LCDecl->getType()->isDependentType() ||
4061 (LB && LB->isValueDependent()) || (UB && UB->isValueDependent()) ||
4062 (Step && Step->isValueDependent());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004063}
4064
Alexey Bataeve3727102018-04-18 15:57:46 +00004065bool OpenMPIterationSpaceChecker::setLCDeclAndLB(ValueDecl *NewLCDecl,
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004066 Expr *NewLCRefExpr,
4067 Expr *NewLB) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004068 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004069 assert(LCDecl == nullptr && LB == nullptr && LCRef == nullptr &&
Alexey Bataevcaf09b02014-07-25 06:27:47 +00004070 UB == nullptr && Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004071 if (!NewLCDecl || !NewLB)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004072 return true;
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004073 LCDecl = getCanonicalDecl(NewLCDecl);
4074 LCRef = NewLCRefExpr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004075 if (auto *CE = dyn_cast_or_null<CXXConstructExpr>(NewLB))
4076 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004077 if ((Ctor->isCopyOrMoveConstructor() ||
4078 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4079 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004080 NewLB = CE->getArg(0)->IgnoreParenImpCasts();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004081 LB = NewLB;
4082 return false;
4083}
4084
Alexey Bataev316ccf62019-01-29 18:51:58 +00004085bool OpenMPIterationSpaceChecker::setUB(Expr *NewUB,
4086 llvm::Optional<bool> LessOp,
Kelvin Liefbe4af2018-11-21 19:10:48 +00004087 bool StrictOp, SourceRange SR,
4088 SourceLocation SL) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004089 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004090 assert(LCDecl != nullptr && LB != nullptr && UB == nullptr &&
4091 Step == nullptr && !TestIsLessOp && !TestIsStrictOp);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004092 if (!NewUB)
4093 return true;
4094 UB = NewUB;
Kelvin Liefbe4af2018-11-21 19:10:48 +00004095 if (LessOp)
4096 TestIsLessOp = LessOp;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004097 TestIsStrictOp = StrictOp;
4098 ConditionSrcRange = SR;
4099 ConditionLoc = SL;
4100 return false;
4101}
4102
Alexey Bataeve3727102018-04-18 15:57:46 +00004103bool OpenMPIterationSpaceChecker::setStep(Expr *NewStep, bool Subtract) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004104 // State consistency checking to ensure correct usage.
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004105 assert(LCDecl != nullptr && LB != nullptr && Step == nullptr);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004106 if (!NewStep)
4107 return true;
4108 if (!NewStep->isValueDependent()) {
4109 // Check that the step is integer expression.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004110 SourceLocation StepLoc = NewStep->getBeginLoc();
Alexey Bataev5372fb82017-08-31 23:06:52 +00004111 ExprResult Val = SemaRef.PerformOpenMPImplicitIntegerConversion(
4112 StepLoc, getExprAsWritten(NewStep));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004113 if (Val.isInvalid())
4114 return true;
4115 NewStep = Val.get();
4116
4117 // OpenMP [2.6, Canonical Loop Form, Restrictions]
4118 // If test-expr is of form var relational-op b and relational-op is < or
4119 // <= then incr-expr must cause var to increase on each iteration of the
4120 // loop. If test-expr is of form var relational-op b and relational-op is
4121 // > or >= then incr-expr must cause var to decrease on each iteration of
4122 // the loop.
4123 // If test-expr is of form b relational-op var and relational-op is < or
4124 // <= then incr-expr must cause var to decrease on each iteration of the
4125 // loop. If test-expr is of form b relational-op var and relational-op is
4126 // > or >= then incr-expr must cause var to increase on each iteration of
4127 // the loop.
4128 llvm::APSInt Result;
4129 bool IsConstant = NewStep->isIntegerConstantExpr(Result, SemaRef.Context);
4130 bool IsUnsigned = !NewStep->getType()->hasSignedIntegerRepresentation();
4131 bool IsConstNeg =
4132 IsConstant && Result.isSigned() && (Subtract != Result.isNegative());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004133 bool IsConstPos =
4134 IsConstant && Result.isSigned() && (Subtract == Result.isNegative());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004135 bool IsConstZero = IsConstant && !Result.getBoolValue();
Kelvin Liefbe4af2018-11-21 19:10:48 +00004136
4137 // != with increment is treated as <; != with decrement is treated as >
4138 if (!TestIsLessOp.hasValue())
4139 TestIsLessOp = IsConstPos || (IsUnsigned && !Subtract);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004140 if (UB && (IsConstZero ||
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004141 (TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00004142 (IsConstNeg || (IsUnsigned && Subtract)) :
4143 (IsConstPos || (IsUnsigned && !Subtract))))) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004144 SemaRef.Diag(NewStep->getExprLoc(),
4145 diag::err_omp_loop_incr_not_compatible)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004146 << LCDecl << TestIsLessOp.getValue() << NewStep->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004147 SemaRef.Diag(ConditionLoc,
4148 diag::note_omp_loop_cond_requres_compatible_incr)
Kelvin Liefbe4af2018-11-21 19:10:48 +00004149 << TestIsLessOp.getValue() << ConditionSrcRange;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004150 return true;
4151 }
Kelvin Liefbe4af2018-11-21 19:10:48 +00004152 if (TestIsLessOp.getValue() == Subtract) {
David Majnemer9d168222016-08-05 17:44:54 +00004153 NewStep =
4154 SemaRef.CreateBuiltinUnaryOp(NewStep->getExprLoc(), UO_Minus, NewStep)
4155 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004156 Subtract = !Subtract;
4157 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004158 }
4159
4160 Step = NewStep;
4161 SubtractStep = Subtract;
4162 return false;
4163}
4164
Alexey Bataeve3727102018-04-18 15:57:46 +00004165bool OpenMPIterationSpaceChecker::checkAndSetInit(Stmt *S, bool EmitDiags) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004166 // Check init-expr for canonical loop form and save loop counter
4167 // variable - #Var and its initialization value - #LB.
4168 // OpenMP [2.6] Canonical loop form. init-expr may be one of the following:
4169 // var = lb
4170 // integer-type var = lb
4171 // random-access-iterator-type var = lb
4172 // pointer-type var = lb
4173 //
4174 if (!S) {
Alexey Bataev9c821032015-04-30 04:23:23 +00004175 if (EmitDiags) {
4176 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_init);
4177 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004178 return true;
4179 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004180 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4181 if (!ExprTemp->cleanupsHaveSideEffects())
4182 S = ExprTemp->getSubExpr();
4183
Alexander Musmana5f070a2014-10-01 06:03:56 +00004184 InitSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004185 if (Expr *E = dyn_cast<Expr>(S))
4186 S = E->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004187 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004188 if (BO->getOpcode() == BO_Assign) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004189 Expr *LHS = BO->getLHS()->IgnoreParens();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004190 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
4191 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4192 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004193 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4194 return setLCDeclAndLB(DRE->getDecl(), DRE, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004195 }
4196 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4197 if (ME->isArrow() &&
4198 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004199 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004200 }
4201 }
David Majnemer9d168222016-08-05 17:44:54 +00004202 } else if (auto *DS = dyn_cast<DeclStmt>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004203 if (DS->isSingleDecl()) {
David Majnemer9d168222016-08-05 17:44:54 +00004204 if (auto *Var = dyn_cast_or_null<VarDecl>(DS->getSingleDecl())) {
Alexey Bataeva8899172015-08-06 12:30:57 +00004205 if (Var->hasInit() && !Var->getType()->isReferenceType()) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004206 // Accept non-canonical init form here but emit ext. warning.
Alexey Bataev9c821032015-04-30 04:23:23 +00004207 if (Var->getInitStyle() != VarDecl::CInit && EmitDiags)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004208 SemaRef.Diag(S->getBeginLoc(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004209 diag::ext_omp_loop_not_canonical_init)
4210 << S->getSourceRange();
Alexey Bataevf138fda2018-08-13 19:04:24 +00004211 return setLCDeclAndLB(
4212 Var,
4213 buildDeclRefExpr(SemaRef, Var,
4214 Var->getType().getNonReferenceType(),
4215 DS->getBeginLoc()),
4216 Var->getInit());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004217 }
4218 }
4219 }
David Majnemer9d168222016-08-05 17:44:54 +00004220 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004221 if (CE->getOperator() == OO_Equal) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004222 Expr *LHS = CE->getArg(0);
David Majnemer9d168222016-08-05 17:44:54 +00004223 if (auto *DRE = dyn_cast<DeclRefExpr>(LHS)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004224 if (auto *CED = dyn_cast<OMPCapturedExprDecl>(DRE->getDecl()))
4225 if (auto *ME = dyn_cast<MemberExpr>(getExprAsWritten(CED->getInit())))
Alexey Bataeve3727102018-04-18 15:57:46 +00004226 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
4227 return setLCDeclAndLB(DRE->getDecl(), DRE, CE->getArg(1));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004228 }
4229 if (auto *ME = dyn_cast<MemberExpr>(LHS)) {
4230 if (ME->isArrow() &&
4231 isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
Alexey Bataeve3727102018-04-18 15:57:46 +00004232 return setLCDeclAndLB(ME->getMemberDecl(), ME, BO->getRHS());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004233 }
4234 }
4235 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004236
Alexey Bataeve3727102018-04-18 15:57:46 +00004237 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004238 return false;
Alexey Bataev9c821032015-04-30 04:23:23 +00004239 if (EmitDiags) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004240 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_init)
Alexey Bataev9c821032015-04-30 04:23:23 +00004241 << S->getSourceRange();
4242 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004243 return true;
4244}
4245
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004246/// Ignore parenthesizes, implicit casts, copy constructor and return the
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004247/// variable (which may be the loop variable) if possible.
Alexey Bataeve3727102018-04-18 15:57:46 +00004248static const ValueDecl *getInitLCDecl(const Expr *E) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004249 if (!E)
Craig Topper4b566922014-06-09 02:04:02 +00004250 return nullptr;
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004251 E = getExprAsWritten(E);
Alexey Bataeve3727102018-04-18 15:57:46 +00004252 if (const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004253 if (const CXXConstructorDecl *Ctor = CE->getConstructor())
Alexey Bataev0d08a7f2015-07-16 04:19:43 +00004254 if ((Ctor->isCopyOrMoveConstructor() ||
4255 Ctor->isConvertingConstructor(/*AllowExplicit=*/false)) &&
4256 CE->getNumArgs() > 0 && CE->getArg(0) != nullptr)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004257 E = CE->getArg(0)->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00004258 if (const auto *DRE = dyn_cast_or_null<DeclRefExpr>(E)) {
4259 if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl()))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004260 return getCanonicalDecl(VD);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004261 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004262 if (const auto *ME = dyn_cast_or_null<MemberExpr>(E))
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004263 if (ME->isArrow() && isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()))
4264 return getCanonicalDecl(ME->getMemberDecl());
4265 return nullptr;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004266}
4267
Alexey Bataeve3727102018-04-18 15:57:46 +00004268bool OpenMPIterationSpaceChecker::checkAndSetCond(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004269 // Check test-expr for canonical form, save upper-bound UB, flags for
4270 // less/greater and for strict/non-strict comparison.
4271 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4272 // var relational-op b
4273 // b relational-op var
4274 //
4275 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004276 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_cond) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004277 return true;
4278 }
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004279 S = getExprAsWritten(S);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004280 SourceLocation CondLoc = S->getBeginLoc();
David Majnemer9d168222016-08-05 17:44:54 +00004281 if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004282 if (BO->isRelationalOp()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004283 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4284 return setUB(BO->getRHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004285 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_LE),
4286 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4287 BO->getSourceRange(), BO->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004288 if (getInitLCDecl(BO->getRHS()) == LCDecl)
4289 return setUB(BO->getLHS(),
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004290 (BO->getOpcode() == BO_GT || BO->getOpcode() == BO_GE),
4291 (BO->getOpcode() == BO_LT || BO->getOpcode() == BO_GT),
4292 BO->getSourceRange(), BO->getOperatorLoc());
Kelvin Liefbe4af2018-11-21 19:10:48 +00004293 } else if (BO->getOpcode() == BO_NE)
4294 return setUB(getInitLCDecl(BO->getLHS()) == LCDecl ?
4295 BO->getRHS() : BO->getLHS(),
4296 /*LessOp=*/llvm::None,
4297 /*StrictOp=*/true,
4298 BO->getSourceRange(), BO->getOperatorLoc());
David Majnemer9d168222016-08-05 17:44:54 +00004299 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004300 if (CE->getNumArgs() == 2) {
4301 auto Op = CE->getOperator();
4302 switch (Op) {
4303 case OO_Greater:
4304 case OO_GreaterEqual:
4305 case OO_Less:
4306 case OO_LessEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004307 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4308 return setUB(CE->getArg(1), Op == OO_Less || Op == OO_LessEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004309 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4310 CE->getOperatorLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +00004311 if (getInitLCDecl(CE->getArg(1)) == LCDecl)
4312 return setUB(CE->getArg(0), Op == OO_Greater || Op == OO_GreaterEqual,
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004313 Op == OO_Less || Op == OO_Greater, CE->getSourceRange(),
4314 CE->getOperatorLoc());
4315 break;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004316 case OO_ExclaimEqual:
Kelvin Liefbe4af2018-11-21 19:10:48 +00004317 return setUB(getInitLCDecl(CE->getArg(0)) == LCDecl ?
4318 CE->getArg(1) : CE->getArg(0),
4319 /*LessOp=*/llvm::None,
4320 /*StrictOp=*/true,
4321 CE->getSourceRange(),
4322 CE->getOperatorLoc());
4323 break;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004324 default:
4325 break;
4326 }
4327 }
4328 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004329 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004330 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004331 SemaRef.Diag(CondLoc, diag::err_omp_loop_not_canonical_cond)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004332 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004333 return true;
4334}
4335
Alexey Bataeve3727102018-04-18 15:57:46 +00004336bool OpenMPIterationSpaceChecker::checkAndSetIncRHS(Expr *RHS) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004337 // RHS of canonical loop form increment can be:
4338 // var + incr
4339 // incr + var
4340 // var - incr
4341 //
4342 RHS = RHS->IgnoreParenImpCasts();
David Majnemer9d168222016-08-05 17:44:54 +00004343 if (auto *BO = dyn_cast<BinaryOperator>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004344 if (BO->isAdditiveOp()) {
4345 bool IsAdd = BO->getOpcode() == BO_Add;
Alexey Bataeve3727102018-04-18 15:57:46 +00004346 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4347 return setStep(BO->getRHS(), !IsAdd);
4348 if (IsAdd && getInitLCDecl(BO->getRHS()) == LCDecl)
4349 return setStep(BO->getLHS(), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004350 }
David Majnemer9d168222016-08-05 17:44:54 +00004351 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(RHS)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004352 bool IsAdd = CE->getOperator() == OO_Plus;
4353 if ((IsAdd || CE->getOperator() == OO_Minus) && CE->getNumArgs() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004354 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4355 return setStep(CE->getArg(1), !IsAdd);
4356 if (IsAdd && getInitLCDecl(CE->getArg(1)) == LCDecl)
4357 return setStep(CE->getArg(0), /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004358 }
4359 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004360 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004361 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004362 SemaRef.Diag(RHS->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004363 << RHS->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004364 return true;
4365}
4366
Alexey Bataeve3727102018-04-18 15:57:46 +00004367bool OpenMPIterationSpaceChecker::checkAndSetInc(Expr *S) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004368 // Check incr-expr for canonical loop form and return true if it
4369 // does not conform.
4370 // OpenMP [2.6] Canonical loop form. Test-expr may be one of the following:
4371 // ++var
4372 // var++
4373 // --var
4374 // var--
4375 // var += incr
4376 // var -= incr
4377 // var = var + incr
4378 // var = incr + var
4379 // var = var - incr
4380 //
4381 if (!S) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004382 SemaRef.Diag(DefaultLoc, diag::err_omp_loop_not_canonical_incr) << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004383 return true;
4384 }
Tim Shen4a05bb82016-06-21 20:29:17 +00004385 if (auto *ExprTemp = dyn_cast<ExprWithCleanups>(S))
4386 if (!ExprTemp->cleanupsHaveSideEffects())
4387 S = ExprTemp->getSubExpr();
4388
Alexander Musmana5f070a2014-10-01 06:03:56 +00004389 IncrementSrcRange = S->getSourceRange();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004390 S = S->IgnoreParens();
David Majnemer9d168222016-08-05 17:44:54 +00004391 if (auto *UO = dyn_cast<UnaryOperator>(S)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004392 if (UO->isIncrementDecrementOp() &&
Alexey Bataeve3727102018-04-18 15:57:46 +00004393 getInitLCDecl(UO->getSubExpr()) == LCDecl)
4394 return setStep(SemaRef
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004395 .ActOnIntegerConstant(UO->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004396 (UO->isDecrementOp() ? -1 : 1))
4397 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004398 /*Subtract=*/false);
David Majnemer9d168222016-08-05 17:44:54 +00004399 } else if (auto *BO = dyn_cast<BinaryOperator>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004400 switch (BO->getOpcode()) {
4401 case BO_AddAssign:
4402 case BO_SubAssign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004403 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4404 return setStep(BO->getRHS(), BO->getOpcode() == BO_SubAssign);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004405 break;
4406 case BO_Assign:
Alexey Bataeve3727102018-04-18 15:57:46 +00004407 if (getInitLCDecl(BO->getLHS()) == LCDecl)
4408 return checkAndSetIncRHS(BO->getRHS());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004409 break;
4410 default:
4411 break;
4412 }
David Majnemer9d168222016-08-05 17:44:54 +00004413 } else if (auto *CE = dyn_cast<CXXOperatorCallExpr>(S)) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004414 switch (CE->getOperator()) {
4415 case OO_PlusPlus:
4416 case OO_MinusMinus:
Alexey Bataeve3727102018-04-18 15:57:46 +00004417 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4418 return setStep(SemaRef
David Majnemer9d168222016-08-05 17:44:54 +00004419 .ActOnIntegerConstant(
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004420 CE->getBeginLoc(),
David Majnemer9d168222016-08-05 17:44:54 +00004421 ((CE->getOperator() == OO_MinusMinus) ? -1 : 1))
4422 .get(),
Alexey Bataeve3727102018-04-18 15:57:46 +00004423 /*Subtract=*/false);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004424 break;
4425 case OO_PlusEqual:
4426 case OO_MinusEqual:
Alexey Bataeve3727102018-04-18 15:57:46 +00004427 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4428 return setStep(CE->getArg(1), CE->getOperator() == OO_MinusEqual);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004429 break;
4430 case OO_Equal:
Alexey Bataeve3727102018-04-18 15:57:46 +00004431 if (getInitLCDecl(CE->getArg(0)) == LCDecl)
4432 return checkAndSetIncRHS(CE->getArg(1));
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004433 break;
4434 default:
4435 break;
4436 }
4437 }
Alexey Bataeve3727102018-04-18 15:57:46 +00004438 if (dependent() || SemaRef.CurContext->isDependentContext())
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004439 return false;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004440 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_loop_not_canonical_incr)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004441 << S->getSourceRange() << LCDecl;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004442 return true;
4443}
Alexander Musmana5f070a2014-10-01 06:03:56 +00004444
Alexey Bataev5a3af132016-03-29 08:58:54 +00004445static ExprResult
4446tryBuildCapture(Sema &SemaRef, Expr *Capture,
Alexey Bataeve3727102018-04-18 15:57:46 +00004447 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00004448 if (SemaRef.CurContext->isDependentContext())
4449 return ExprResult(Capture);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004450 if (Capture->isEvaluatable(SemaRef.Context, Expr::SE_AllowSideEffects))
4451 return SemaRef.PerformImplicitConversion(
4452 Capture->IgnoreImpCasts(), Capture->getType(), Sema::AA_Converting,
4453 /*AllowExplicit=*/true);
4454 auto I = Captures.find(Capture);
4455 if (I != Captures.end())
4456 return buildCapture(SemaRef, Capture, I->second);
4457 DeclRefExpr *Ref = nullptr;
4458 ExprResult Res = buildCapture(SemaRef, Capture, Ref);
4459 Captures[Capture] = Ref;
4460 return Res;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004461}
4462
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004463/// Build the expression to calculate the number of iterations.
Alexey Bataeve3727102018-04-18 15:57:46 +00004464Expr *OpenMPIterationSpaceChecker::buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004465 Scope *S, const bool LimitedType,
Alexey Bataeve3727102018-04-18 15:57:46 +00004466 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004467 ExprResult Diff;
Alexey Bataeve3727102018-04-18 15:57:46 +00004468 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004469 if (VarType->isIntegerType() || VarType->isPointerType() ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004470 SemaRef.getLangOpts().CPlusPlus) {
4471 // Upper - Lower
Kelvin Liefbe4af2018-11-21 19:10:48 +00004472 Expr *UBExpr = TestIsLessOp.getValue() ? UB : LB;
4473 Expr *LBExpr = TestIsLessOp.getValue() ? LB : UB;
Alexey Bataev5a3af132016-03-29 08:58:54 +00004474 Expr *Upper = tryBuildCapture(SemaRef, UBExpr, Captures).get();
4475 Expr *Lower = tryBuildCapture(SemaRef, LBExpr, Captures).get();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004476 if (!Upper || !Lower)
4477 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004478
4479 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4480
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004481 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004482 // BuildBinOp already emitted error, this one is to point user to upper
4483 // and lower bound, and to tell what is passed to 'operator-'.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004484 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
Alexander Musmana5f070a2014-10-01 06:03:56 +00004485 << Upper->getSourceRange() << Lower->getSourceRange();
4486 return nullptr;
4487 }
4488 }
4489
4490 if (!Diff.isUsable())
4491 return nullptr;
4492
4493 // Upper - Lower [- 1]
4494 if (TestIsStrictOp)
4495 Diff = SemaRef.BuildBinOp(
4496 S, DefaultLoc, BO_Sub, Diff.get(),
4497 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
4498 if (!Diff.isUsable())
4499 return nullptr;
4500
4501 // Upper - Lower [- 1] + Step
Alexey Bataeve3727102018-04-18 15:57:46 +00004502 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004503 if (!NewStep.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004504 return nullptr;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004505 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Add, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004506 if (!Diff.isUsable())
4507 return nullptr;
4508
4509 // Parentheses (for dumping/debugging purposes only).
4510 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4511 if (!Diff.isUsable())
4512 return nullptr;
4513
4514 // (Upper - Lower [- 1] + Step) / Step
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004515 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004516 if (!Diff.isUsable())
4517 return nullptr;
4518
Alexander Musman174b3ca2014-10-06 11:16:29 +00004519 // OpenMP runtime requires 32-bit or 64-bit loop variables.
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004520 QualType Type = Diff.get()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +00004521 ASTContext &C = SemaRef.Context;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004522 bool UseVarType = VarType->hasIntegerRepresentation() &&
4523 C.getTypeSize(Type) > C.getTypeSize(VarType);
4524 if (!Type->isIntegerType() || UseVarType) {
4525 unsigned NewSize =
4526 UseVarType ? C.getTypeSize(VarType) : C.getTypeSize(Type);
4527 bool IsSigned = UseVarType ? VarType->hasSignedIntegerRepresentation()
4528 : Type->hasSignedIntegerRepresentation();
4529 Type = C.getIntTypeForBitwidth(NewSize, IsSigned);
Alexey Bataev11481f52016-02-17 10:29:05 +00004530 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), Type)) {
4531 Diff = SemaRef.PerformImplicitConversion(
4532 Diff.get(), Type, Sema::AA_Converting, /*AllowExplicit=*/true);
4533 if (!Diff.isUsable())
4534 return nullptr;
4535 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004536 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004537 if (LimitedType) {
Alexander Musman174b3ca2014-10-06 11:16:29 +00004538 unsigned NewSize = (C.getTypeSize(Type) > 32) ? 64 : 32;
4539 if (NewSize != C.getTypeSize(Type)) {
4540 if (NewSize < C.getTypeSize(Type)) {
4541 assert(NewSize == 64 && "incorrect loop var size");
4542 SemaRef.Diag(DefaultLoc, diag::warn_omp_loop_64_bit_var)
4543 << InitSrcRange << ConditionSrcRange;
4544 }
4545 QualType NewType = C.getIntTypeForBitwidth(
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004546 NewSize, Type->hasSignedIntegerRepresentation() ||
4547 C.getTypeSize(Type) < NewSize);
Alexey Bataev11481f52016-02-17 10:29:05 +00004548 if (!SemaRef.Context.hasSameType(Diff.get()->getType(), NewType)) {
4549 Diff = SemaRef.PerformImplicitConversion(Diff.get(), NewType,
4550 Sema::AA_Converting, true);
4551 if (!Diff.isUsable())
4552 return nullptr;
4553 }
Alexander Musman174b3ca2014-10-06 11:16:29 +00004554 }
4555 }
4556
Alexander Musmana5f070a2014-10-01 06:03:56 +00004557 return Diff.get();
4558}
4559
Alexey Bataeve3727102018-04-18 15:57:46 +00004560Expr *OpenMPIterationSpaceChecker::buildPreCond(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004561 Scope *S, Expr *Cond,
Alexey Bataeve3727102018-04-18 15:57:46 +00004562 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) const {
Alexey Bataev62dbb972015-04-22 11:59:37 +00004563 // Try to build LB <op> UB, where <op> is <, >, <=, or >=.
4564 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
4565 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004566
Alexey Bataeve3727102018-04-18 15:57:46 +00004567 ExprResult NewLB = tryBuildCapture(SemaRef, LB, Captures);
4568 ExprResult NewUB = tryBuildCapture(SemaRef, UB, Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004569 if (!NewLB.isUsable() || !NewUB.isUsable())
4570 return nullptr;
4571
Alexey Bataeve3727102018-04-18 15:57:46 +00004572 ExprResult CondExpr =
4573 SemaRef.BuildBinOp(S, DefaultLoc,
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00004574 TestIsLessOp.getValue() ?
Kelvin Liefbe4af2018-11-21 19:10:48 +00004575 (TestIsStrictOp ? BO_LT : BO_LE) :
4576 (TestIsStrictOp ? BO_GT : BO_GE),
Alexey Bataeve3727102018-04-18 15:57:46 +00004577 NewLB.get(), NewUB.get());
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004578 if (CondExpr.isUsable()) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00004579 if (!SemaRef.Context.hasSameUnqualifiedType(CondExpr.get()->getType(),
4580 SemaRef.Context.BoolTy))
Alexey Bataev11481f52016-02-17 10:29:05 +00004581 CondExpr = SemaRef.PerformImplicitConversion(
4582 CondExpr.get(), SemaRef.Context.BoolTy, /*Action=*/Sema::AA_Casting,
4583 /*AllowExplicit=*/true);
Alexey Bataev3bed68c2015-07-15 12:14:07 +00004584 }
Alexey Bataev62dbb972015-04-22 11:59:37 +00004585 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
4586 // Otherwise use original loop conditon and evaluate it in runtime.
4587 return CondExpr.isUsable() ? CondExpr.get() : Cond;
4588}
4589
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004590/// Build reference expression to the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004591DeclRefExpr *OpenMPIterationSpaceChecker::buildCounterVar(
Alexey Bataevf138fda2018-08-13 19:04:24 +00004592 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures,
4593 DSAStackTy &DSA) const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004594 auto *VD = dyn_cast<VarDecl>(LCDecl);
4595 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004596 VD = SemaRef.isOpenMPCapturedDecl(LCDecl);
4597 DeclRefExpr *Ref = buildDeclRefExpr(
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004598 SemaRef, VD, VD->getType().getNonReferenceType(), DefaultLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004599 const DSAStackTy::DSAVarData Data =
4600 DSA.getTopDSA(LCDecl, /*FromParent=*/false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +00004601 // If the loop control decl is explicitly marked as private, do not mark it
4602 // as captured again.
4603 if (!isOpenMPPrivate(Data.CKind) || !Data.RefExpr)
4604 Captures.insert(std::make_pair(LCRef, Ref));
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004605 return Ref;
4606 }
4607 return buildDeclRefExpr(SemaRef, VD, VD->getType().getNonReferenceType(),
Alexey Bataeva8899172015-08-06 12:30:57 +00004608 DefaultLoc);
4609}
4610
Alexey Bataeve3727102018-04-18 15:57:46 +00004611Expr *OpenMPIterationSpaceChecker::buildPrivateCounterVar() const {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004612 if (LCDecl && !LCDecl->isInvalidDecl()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004613 QualType Type = LCDecl->getType().getNonReferenceType();
4614 VarDecl *PrivateVar = buildVarDecl(
Alexey Bataev63cc8e92018-03-20 14:45:59 +00004615 SemaRef, DefaultLoc, Type, LCDecl->getName(),
4616 LCDecl->hasAttrs() ? &LCDecl->getAttrs() : nullptr,
4617 isa<VarDecl>(LCDecl)
4618 ? buildDeclRefExpr(SemaRef, cast<VarDecl>(LCDecl), Type, DefaultLoc)
4619 : nullptr);
Alexey Bataeva8899172015-08-06 12:30:57 +00004620 if (PrivateVar->isInvalidDecl())
4621 return nullptr;
4622 return buildDeclRefExpr(SemaRef, PrivateVar, Type, DefaultLoc);
4623 }
4624 return nullptr;
Alexander Musmana5f070a2014-10-01 06:03:56 +00004625}
4626
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004627/// Build initialization of the counter to be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004628Expr *OpenMPIterationSpaceChecker::buildCounterInit() const { return LB; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004629
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004630/// Build step of the counter be used for codegen.
Alexey Bataeve3727102018-04-18 15:57:46 +00004631Expr *OpenMPIterationSpaceChecker::buildCounterStep() const { return Step; }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004632
Alexey Bataevf138fda2018-08-13 19:04:24 +00004633Expr *OpenMPIterationSpaceChecker::buildOrderedLoopData(
4634 Scope *S, Expr *Counter,
4635 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures, SourceLocation Loc,
4636 Expr *Inc, OverloadedOperatorKind OOK) {
4637 Expr *Cnt = SemaRef.DefaultLvalueConversion(Counter).get();
4638 if (!Cnt)
4639 return nullptr;
4640 if (Inc) {
4641 assert((OOK == OO_Plus || OOK == OO_Minus) &&
4642 "Expected only + or - operations for depend clauses.");
4643 BinaryOperatorKind BOK = (OOK == OO_Plus) ? BO_Add : BO_Sub;
4644 Cnt = SemaRef.BuildBinOp(S, Loc, BOK, Cnt, Inc).get();
4645 if (!Cnt)
4646 return nullptr;
4647 }
4648 ExprResult Diff;
4649 QualType VarType = LCDecl->getType().getNonReferenceType();
4650 if (VarType->isIntegerType() || VarType->isPointerType() ||
4651 SemaRef.getLangOpts().CPlusPlus) {
4652 // Upper - Lower
Alexey Bataev316ccf62019-01-29 18:51:58 +00004653 Expr *Upper = TestIsLessOp.getValue()
4654 ? Cnt
4655 : tryBuildCapture(SemaRef, UB, Captures).get();
4656 Expr *Lower = TestIsLessOp.getValue()
4657 ? tryBuildCapture(SemaRef, LB, Captures).get()
4658 : Cnt;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004659 if (!Upper || !Lower)
4660 return nullptr;
4661
4662 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Sub, Upper, Lower);
4663
4664 if (!Diff.isUsable() && VarType->getAsCXXRecordDecl()) {
4665 // BuildBinOp already emitted error, this one is to point user to upper
4666 // and lower bound, and to tell what is passed to 'operator-'.
4667 SemaRef.Diag(Upper->getBeginLoc(), diag::err_omp_loop_diff_cxx)
4668 << Upper->getSourceRange() << Lower->getSourceRange();
4669 return nullptr;
4670 }
4671 }
4672
4673 if (!Diff.isUsable())
4674 return nullptr;
4675
4676 // Parentheses (for dumping/debugging purposes only).
4677 Diff = SemaRef.ActOnParenExpr(DefaultLoc, DefaultLoc, Diff.get());
4678 if (!Diff.isUsable())
4679 return nullptr;
4680
4681 ExprResult NewStep = tryBuildCapture(SemaRef, Step, Captures);
4682 if (!NewStep.isUsable())
4683 return nullptr;
4684 // (Upper - Lower) / Step
4685 Diff = SemaRef.BuildBinOp(S, DefaultLoc, BO_Div, Diff.get(), NewStep.get());
4686 if (!Diff.isUsable())
4687 return nullptr;
4688
4689 return Diff.get();
4690}
4691
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004692/// Iteration space of a single for loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004693struct LoopIterationSpace final {
Alexey Bataev316ccf62019-01-29 18:51:58 +00004694 /// True if the condition operator is the strict compare operator (<, > or
4695 /// !=).
4696 bool IsStrictCompare = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004697 /// Condition of the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004698 Expr *PreCond = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004699 /// This expression calculates the number of iterations in the loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004700 /// It is always possible to calculate it before starting the loop.
Alexey Bataev8b427062016-05-25 12:36:08 +00004701 Expr *NumIterations = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004702 /// The loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004703 Expr *CounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004704 /// Private loop counter variable.
Alexey Bataev8b427062016-05-25 12:36:08 +00004705 Expr *PrivateCounterVar = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004706 /// This is initializer for the initial value of #CounterVar.
Alexey Bataev8b427062016-05-25 12:36:08 +00004707 Expr *CounterInit = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004708 /// This is step for the #CounterVar used to generate its update:
Alexander Musmana5f070a2014-10-01 06:03:56 +00004709 /// #CounterVar = #CounterInit + #CounterStep * CurrentIteration.
Alexey Bataev8b427062016-05-25 12:36:08 +00004710 Expr *CounterStep = nullptr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004711 /// Should step be subtracted?
Alexey Bataev8b427062016-05-25 12:36:08 +00004712 bool Subtract = false;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004713 /// Source range of the loop init.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004714 SourceRange InitSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004715 /// Source range of the loop condition.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004716 SourceRange CondSrcRange;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004717 /// Source range of the loop increment.
Alexander Musmana5f070a2014-10-01 06:03:56 +00004718 SourceRange IncSrcRange;
4719};
4720
Alexey Bataev23b69422014-06-18 07:08:49 +00004721} // namespace
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004722
Alexey Bataev9c821032015-04-30 04:23:23 +00004723void Sema::ActOnOpenMPLoopInitialization(SourceLocation ForLoc, Stmt *Init) {
4724 assert(getLangOpts().OpenMP && "OpenMP is not active.");
4725 assert(Init && "Expected loop in canonical form.");
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004726 unsigned AssociatedLoops = DSAStack->getAssociatedLoops();
4727 if (AssociatedLoops > 0 &&
Alexey Bataev9c821032015-04-30 04:23:23 +00004728 isOpenMPLoopDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevce901812018-12-19 18:16:37 +00004729 DSAStack->loopStart();
Alexey Bataev9c821032015-04-30 04:23:23 +00004730 OpenMPIterationSpaceChecker ISC(*this, ForLoc);
Alexey Bataeve3727102018-04-18 15:57:46 +00004731 if (!ISC.checkAndSetInit(Init, /*EmitDiags=*/false)) {
4732 if (ValueDecl *D = ISC.getLoopDecl()) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004733 auto *VD = dyn_cast<VarDecl>(D);
4734 if (!VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +00004735 if (VarDecl *Private = isOpenMPCapturedDecl(D)) {
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004736 VD = Private;
Alexey Bataeve3727102018-04-18 15:57:46 +00004737 } else {
4738 DeclRefExpr *Ref = buildCapture(*this, D, ISC.getLoopDeclRefExpr(),
4739 /*WithInit=*/false);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004740 VD = cast<VarDecl>(Ref->getDecl());
4741 }
4742 }
4743 DSAStack->addLoopControlVariable(D, VD);
Alexey Bataev6ab5bb12018-10-29 15:01:58 +00004744 const Decl *LD = DSAStack->getPossiblyLoopCunter();
4745 if (LD != D->getCanonicalDecl()) {
4746 DSAStack->resetPossibleLoopCounter();
4747 if (auto *Var = dyn_cast_or_null<VarDecl>(LD))
4748 MarkDeclarationsReferencedInExpr(
4749 buildDeclRefExpr(*this, const_cast<VarDecl *>(Var),
4750 Var->getType().getNonLValueExprType(Context),
4751 ForLoc, /*RefersToCapture=*/true));
4752 }
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004753 }
4754 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00004755 DSAStack->setAssociatedLoops(AssociatedLoops - 1);
Alexey Bataev9c821032015-04-30 04:23:23 +00004756 }
4757}
4758
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004759/// Called on a for stmt to check and extract its iteration space
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004760/// for further processing (such as collapsing).
Alexey Bataeve3727102018-04-18 15:57:46 +00004761static bool checkOpenMPIterationSpace(
Alexey Bataev4acb8592014-07-07 13:01:15 +00004762 OpenMPDirectiveKind DKind, Stmt *S, Sema &SemaRef, DSAStackTy &DSA,
4763 unsigned CurrentNestedLoopCount, unsigned NestedLoopCount,
Alexey Bataevf138fda2018-08-13 19:04:24 +00004764 unsigned TotalNestedLoopCount, Expr *CollapseLoopCountExpr,
4765 Expr *OrderedLoopCountExpr,
Alexey Bataeve3727102018-04-18 15:57:46 +00004766 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004767 LoopIterationSpace &ResultIterSpace,
Alexey Bataeve3727102018-04-18 15:57:46 +00004768 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004769 // OpenMP [2.6, Canonical Loop Form]
4770 // for (init-expr; test-expr; incr-expr) structured-block
David Majnemer9d168222016-08-05 17:44:54 +00004771 auto *For = dyn_cast_or_null<ForStmt>(S);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004772 if (!For) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004773 SemaRef.Diag(S->getBeginLoc(), diag::err_omp_not_for)
Alexey Bataev10e775f2015-07-30 11:36:16 +00004774 << (CollapseLoopCountExpr != nullptr || OrderedLoopCountExpr != nullptr)
Alexey Bataevf138fda2018-08-13 19:04:24 +00004775 << getOpenMPDirectiveName(DKind) << TotalNestedLoopCount
Alexey Bataev10e775f2015-07-30 11:36:16 +00004776 << (CurrentNestedLoopCount > 0) << CurrentNestedLoopCount;
Alexey Bataevf138fda2018-08-13 19:04:24 +00004777 if (TotalNestedLoopCount > 1) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00004778 if (CollapseLoopCountExpr && OrderedLoopCountExpr)
4779 SemaRef.Diag(DSA.getConstructLoc(),
4780 diag::note_omp_collapse_ordered_expr)
4781 << 2 << CollapseLoopCountExpr->getSourceRange()
4782 << OrderedLoopCountExpr->getSourceRange();
4783 else if (CollapseLoopCountExpr)
4784 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
4785 diag::note_omp_collapse_ordered_expr)
4786 << 0 << CollapseLoopCountExpr->getSourceRange();
4787 else
4788 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
4789 diag::note_omp_collapse_ordered_expr)
4790 << 1 << OrderedLoopCountExpr->getSourceRange();
4791 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004792 return true;
4793 }
4794 assert(For->getBody());
4795
4796 OpenMPIterationSpaceChecker ISC(SemaRef, For->getForLoc());
4797
4798 // Check init.
Alexey Bataeve3727102018-04-18 15:57:46 +00004799 Stmt *Init = For->getInit();
4800 if (ISC.checkAndSetInit(Init))
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004801 return true;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004802
4803 bool HasErrors = false;
4804
4805 // Check loop variable's type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004806 if (ValueDecl *LCDecl = ISC.getLoopDecl()) {
4807 Expr *LoopDeclRefExpr = ISC.getLoopDeclRefExpr();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004808
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004809 // OpenMP [2.6, Canonical Loop Form]
4810 // Var is one of the following:
4811 // A variable of signed or unsigned integer type.
4812 // For C++, a variable of a random access iterator type.
4813 // For C, a variable of a pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +00004814 QualType VarType = LCDecl->getType().getNonReferenceType();
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004815 if (!VarType->isDependentType() && !VarType->isIntegerType() &&
4816 !VarType->isPointerType() &&
4817 !(SemaRef.getLangOpts().CPlusPlus && VarType->isOverloadableType())) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004818 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_variable_type)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004819 << SemaRef.getLangOpts().CPlusPlus;
4820 HasErrors = true;
4821 }
4822
4823 // OpenMP, 2.14.1.1 Data-sharing Attribute Rules for Variables Referenced in
4824 // a Construct
4825 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4826 // parallel for construct is (are) private.
4827 // The loop iteration variable in the associated for-loop of a simd
4828 // construct with just one associated for-loop is linear with a
4829 // constant-linear-step that is the increment of the associated for-loop.
4830 // Exclude loop var from the list of variables with implicitly defined data
4831 // sharing attributes.
4832 VarsWithImplicitDSA.erase(LCDecl);
4833
4834 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
4835 // in a Construct, C/C++].
4836 // The loop iteration variable in the associated for-loop of a simd
4837 // construct with just one associated for-loop may be listed in a linear
4838 // clause with a constant-linear-step that is the increment of the
4839 // associated for-loop.
4840 // The loop iteration variable(s) in the associated for-loop(s) of a for or
4841 // parallel for construct may be listed in a private or lastprivate clause.
4842 DSAStackTy::DSAVarData DVar = DSA.getTopDSA(LCDecl, false);
4843 // If LoopVarRefExpr is nullptr it means the corresponding loop variable is
4844 // declared in the loop and it is predetermined as a private.
Alexey Bataeve3727102018-04-18 15:57:46 +00004845 OpenMPClauseKind PredeterminedCKind =
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004846 isOpenMPSimdDirective(DKind)
4847 ? ((NestedLoopCount == 1) ? OMPC_linear : OMPC_lastprivate)
4848 : OMPC_private;
4849 if (((isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4850 DVar.CKind != PredeterminedCKind) ||
4851 ((isOpenMPWorksharingDirective(DKind) || DKind == OMPD_taskloop ||
4852 isOpenMPDistributeDirective(DKind)) &&
4853 !isOpenMPSimdDirective(DKind) && DVar.CKind != OMPC_unknown &&
4854 DVar.CKind != OMPC_private && DVar.CKind != OMPC_lastprivate)) &&
4855 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00004856 SemaRef.Diag(Init->getBeginLoc(), diag::err_omp_loop_var_dsa)
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004857 << getOpenMPClauseName(DVar.CKind) << getOpenMPDirectiveName(DKind)
4858 << getOpenMPClauseName(PredeterminedCKind);
4859 if (DVar.RefExpr == nullptr)
4860 DVar.CKind = PredeterminedCKind;
Alexey Bataeve3727102018-04-18 15:57:46 +00004861 reportOriginalDsa(SemaRef, &DSA, LCDecl, DVar, /*IsLoopIterVar=*/true);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004862 HasErrors = true;
4863 } else if (LoopDeclRefExpr != nullptr) {
4864 // Make the loop iteration variable private (for worksharing constructs),
4865 // linear (for simd directives with the only one associated loop) or
4866 // lastprivate (for simd directives with several collapsed or ordered
4867 // loops).
4868 if (DVar.CKind == OMPC_unknown)
Alexey Bataevc2cdff62019-01-29 21:12:28 +00004869 DSA.addDSA(LCDecl, LoopDeclRefExpr, PredeterminedCKind);
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004870 }
4871
4872 assert(isOpenMPLoopDirective(DKind) && "DSA for non-loop vars");
4873
4874 // Check test-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004875 HasErrors |= ISC.checkAndSetCond(For->getCond());
Alexey Bataevc6ad97a2016-04-01 09:23:34 +00004876
4877 // Check incr-expr.
Alexey Bataeve3727102018-04-18 15:57:46 +00004878 HasErrors |= ISC.checkAndSetInc(For->getInc());
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004879 }
4880
Alexey Bataeve3727102018-04-18 15:57:46 +00004881 if (ISC.dependent() || SemaRef.CurContext->isDependentContext() || HasErrors)
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004882 return HasErrors;
4883
Alexander Musmana5f070a2014-10-01 06:03:56 +00004884 // Build the loop's iteration space representation.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004885 ResultIterSpace.PreCond =
Alexey Bataeve3727102018-04-18 15:57:46 +00004886 ISC.buildPreCond(DSA.getCurScope(), For->getCond(), Captures);
4887 ResultIterSpace.NumIterations = ISC.buildNumIterations(
Alexey Bataev5a3af132016-03-29 08:58:54 +00004888 DSA.getCurScope(),
4889 (isOpenMPWorksharingDirective(DKind) ||
4890 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind)),
4891 Captures);
Alexey Bataeve3727102018-04-18 15:57:46 +00004892 ResultIterSpace.CounterVar = ISC.buildCounterVar(Captures, DSA);
4893 ResultIterSpace.PrivateCounterVar = ISC.buildPrivateCounterVar();
4894 ResultIterSpace.CounterInit = ISC.buildCounterInit();
4895 ResultIterSpace.CounterStep = ISC.buildCounterStep();
4896 ResultIterSpace.InitSrcRange = ISC.getInitSrcRange();
4897 ResultIterSpace.CondSrcRange = ISC.getConditionSrcRange();
4898 ResultIterSpace.IncSrcRange = ISC.getIncrementSrcRange();
4899 ResultIterSpace.Subtract = ISC.shouldSubtractStep();
Alexey Bataev316ccf62019-01-29 18:51:58 +00004900 ResultIterSpace.IsStrictCompare = ISC.isStrictTestOp();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004901
Alexey Bataev62dbb972015-04-22 11:59:37 +00004902 HasErrors |= (ResultIterSpace.PreCond == nullptr ||
4903 ResultIterSpace.NumIterations == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004904 ResultIterSpace.CounterVar == nullptr ||
Alexey Bataeva8899172015-08-06 12:30:57 +00004905 ResultIterSpace.PrivateCounterVar == nullptr ||
Alexander Musmana5f070a2014-10-01 06:03:56 +00004906 ResultIterSpace.CounterInit == nullptr ||
4907 ResultIterSpace.CounterStep == nullptr);
Alexey Bataevf138fda2018-08-13 19:04:24 +00004908 if (!HasErrors && DSA.isOrderedRegion()) {
4909 if (DSA.getOrderedRegionParam().second->getNumForLoops()) {
4910 if (CurrentNestedLoopCount <
4911 DSA.getOrderedRegionParam().second->getLoopNumIterations().size()) {
4912 DSA.getOrderedRegionParam().second->setLoopNumIterations(
4913 CurrentNestedLoopCount, ResultIterSpace.NumIterations);
4914 DSA.getOrderedRegionParam().second->setLoopCounter(
4915 CurrentNestedLoopCount, ResultIterSpace.CounterVar);
4916 }
4917 }
4918 for (auto &Pair : DSA.getDoacrossDependClauses()) {
4919 if (CurrentNestedLoopCount >= Pair.first->getNumLoops()) {
4920 // Erroneous case - clause has some problems.
4921 continue;
4922 }
4923 if (Pair.first->getDependencyKind() == OMPC_DEPEND_sink &&
4924 Pair.second.size() <= CurrentNestedLoopCount) {
4925 // Erroneous case - clause has some problems.
4926 Pair.first->setLoopData(CurrentNestedLoopCount, nullptr);
4927 continue;
4928 }
4929 Expr *CntValue;
4930 if (Pair.first->getDependencyKind() == OMPC_DEPEND_source)
4931 CntValue = ISC.buildOrderedLoopData(
4932 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4933 Pair.first->getDependencyLoc());
4934 else
4935 CntValue = ISC.buildOrderedLoopData(
4936 DSA.getCurScope(), ResultIterSpace.CounterVar, Captures,
4937 Pair.first->getDependencyLoc(),
4938 Pair.second[CurrentNestedLoopCount].first,
4939 Pair.second[CurrentNestedLoopCount].second);
4940 Pair.first->setLoopData(CurrentNestedLoopCount, CntValue);
4941 }
4942 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00004943
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00004944 return HasErrors;
4945}
4946
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004947/// Build 'VarRef = Start.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004948static ExprResult
Alexey Bataeve3727102018-04-18 15:57:46 +00004949buildCounterInit(Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
Alexey Bataev5a3af132016-03-29 08:58:54 +00004950 ExprResult Start,
Alexey Bataeve3727102018-04-18 15:57:46 +00004951 llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004952 // Build 'VarRef = Start.
Alexey Bataeve3727102018-04-18 15:57:46 +00004953 ExprResult NewStart = tryBuildCapture(SemaRef, Start.get(), Captures);
Alexey Bataev5a3af132016-03-29 08:58:54 +00004954 if (!NewStart.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004955 return ExprError();
Alexey Bataev11481f52016-02-17 10:29:05 +00004956 if (!SemaRef.Context.hasSameType(NewStart.get()->getType(),
Alexey Bataev11481f52016-02-17 10:29:05 +00004957 VarRef.get()->getType())) {
4958 NewStart = SemaRef.PerformImplicitConversion(
4959 NewStart.get(), VarRef.get()->getType(), Sema::AA_Converting,
4960 /*AllowExplicit=*/true);
4961 if (!NewStart.isUsable())
4962 return ExprError();
4963 }
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004964
Alexey Bataeve3727102018-04-18 15:57:46 +00004965 ExprResult Init =
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004966 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
4967 return Init;
4968}
4969
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00004970/// Build 'VarRef = Start + Iter * Step'.
Alexey Bataeve3727102018-04-18 15:57:46 +00004971static ExprResult buildCounterUpdate(
4972 Sema &SemaRef, Scope *S, SourceLocation Loc, ExprResult VarRef,
4973 ExprResult Start, ExprResult Iter, ExprResult Step, bool Subtract,
4974 llvm::MapVector<const Expr *, DeclRefExpr *> *Captures = nullptr) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00004975 // Add parentheses (for debugging purposes only).
4976 Iter = SemaRef.ActOnParenExpr(Loc, Loc, Iter.get());
4977 if (!VarRef.isUsable() || !Start.isUsable() || !Iter.isUsable() ||
4978 !Step.isUsable())
4979 return ExprError();
4980
Alexey Bataev5a3af132016-03-29 08:58:54 +00004981 ExprResult NewStep = Step;
4982 if (Captures)
4983 NewStep = tryBuildCapture(SemaRef, Step.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004984 if (NewStep.isInvalid())
4985 return ExprError();
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004986 ExprResult Update =
4987 SemaRef.BuildBinOp(S, Loc, BO_Mul, Iter.get(), NewStep.get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00004988 if (!Update.isUsable())
4989 return ExprError();
4990
Alexey Bataevc0214e02016-02-16 12:13:49 +00004991 // Try to build 'VarRef = Start, VarRef (+|-)= Iter * Step' or
4992 // 'VarRef = Start (+|-) Iter * Step'.
Alexey Bataev5a3af132016-03-29 08:58:54 +00004993 ExprResult NewStart = Start;
4994 if (Captures)
4995 NewStart = tryBuildCapture(SemaRef, Start.get(), *Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00004996 if (NewStart.isInvalid())
4997 return ExprError();
Alexander Musmana5f070a2014-10-01 06:03:56 +00004998
Alexey Bataevc0214e02016-02-16 12:13:49 +00004999 // First attempt: try to build 'VarRef = Start, VarRef += Iter * Step'.
5000 ExprResult SavedUpdate = Update;
5001 ExprResult UpdateVal;
5002 if (VarRef.get()->getType()->isOverloadableType() ||
5003 NewStart.get()->getType()->isOverloadableType() ||
5004 Update.get()->getType()->isOverloadableType()) {
5005 bool Suppress = SemaRef.getDiagnostics().getSuppressAllDiagnostics();
5006 SemaRef.getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
5007 Update =
5008 SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), NewStart.get());
5009 if (Update.isUsable()) {
5010 UpdateVal =
5011 SemaRef.BuildBinOp(S, Loc, Subtract ? BO_SubAssign : BO_AddAssign,
5012 VarRef.get(), SavedUpdate.get());
5013 if (UpdateVal.isUsable()) {
5014 Update = SemaRef.CreateBuiltinBinOp(Loc, BO_Comma, Update.get(),
5015 UpdateVal.get());
5016 }
5017 }
5018 SemaRef.getDiagnostics().setSuppressAllDiagnostics(Suppress);
5019 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005020
Alexey Bataevc0214e02016-02-16 12:13:49 +00005021 // Second attempt: try to build 'VarRef = Start (+|-) Iter * Step'.
5022 if (!Update.isUsable() || !UpdateVal.isUsable()) {
5023 Update = SemaRef.BuildBinOp(S, Loc, Subtract ? BO_Sub : BO_Add,
5024 NewStart.get(), SavedUpdate.get());
5025 if (!Update.isUsable())
5026 return ExprError();
5027
Alexey Bataev11481f52016-02-17 10:29:05 +00005028 if (!SemaRef.Context.hasSameType(Update.get()->getType(),
5029 VarRef.get()->getType())) {
5030 Update = SemaRef.PerformImplicitConversion(
5031 Update.get(), VarRef.get()->getType(), Sema::AA_Converting, true);
5032 if (!Update.isUsable())
5033 return ExprError();
5034 }
Alexey Bataevc0214e02016-02-16 12:13:49 +00005035
5036 Update = SemaRef.BuildBinOp(S, Loc, BO_Assign, VarRef.get(), Update.get());
5037 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005038 return Update;
5039}
5040
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005041/// Convert integer expression \a E to make it have at least \a Bits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005042/// bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005043static ExprResult widenIterationCount(unsigned Bits, Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005044 if (E == nullptr)
5045 return ExprError();
Alexey Bataeve3727102018-04-18 15:57:46 +00005046 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005047 QualType OldType = E->getType();
5048 unsigned HasBits = C.getTypeSize(OldType);
5049 if (HasBits >= Bits)
5050 return ExprResult(E);
5051 // OK to convert to signed, because new type has more bits than old.
5052 QualType NewType = C.getIntTypeForBitwidth(Bits, /* Signed */ true);
5053 return SemaRef.PerformImplicitConversion(E, NewType, Sema::AA_Converting,
5054 true);
5055}
5056
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005057/// Check if the given expression \a E is a constant integer that fits
Alexander Musmana5f070a2014-10-01 06:03:56 +00005058/// into \a Bits bits.
Alexey Bataeve3727102018-04-18 15:57:46 +00005059static bool fitsInto(unsigned Bits, bool Signed, const Expr *E, Sema &SemaRef) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005060 if (E == nullptr)
5061 return false;
5062 llvm::APSInt Result;
5063 if (E->isIntegerConstantExpr(Result, SemaRef.Context))
5064 return Signed ? Result.isSignedIntN(Bits) : Result.isIntN(Bits);
5065 return false;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005066}
5067
Alexey Bataev5a3af132016-03-29 08:58:54 +00005068/// Build preinits statement for the given declarations.
5069static Stmt *buildPreInits(ASTContext &Context,
Alexey Bataevc5514062017-10-25 15:44:52 +00005070 MutableArrayRef<Decl *> PreInits) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005071 if (!PreInits.empty()) {
5072 return new (Context) DeclStmt(
5073 DeclGroupRef::Create(Context, PreInits.begin(), PreInits.size()),
5074 SourceLocation(), SourceLocation());
5075 }
5076 return nullptr;
5077}
5078
5079/// Build preinits statement for the given declarations.
Alexey Bataevc5514062017-10-25 15:44:52 +00005080static Stmt *
5081buildPreInits(ASTContext &Context,
Alexey Bataeve3727102018-04-18 15:57:46 +00005082 const llvm::MapVector<const Expr *, DeclRefExpr *> &Captures) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005083 if (!Captures.empty()) {
5084 SmallVector<Decl *, 16> PreInits;
Alexey Bataeve3727102018-04-18 15:57:46 +00005085 for (const auto &Pair : Captures)
Alexey Bataev5a3af132016-03-29 08:58:54 +00005086 PreInits.push_back(Pair.second->getDecl());
5087 return buildPreInits(Context, PreInits);
5088 }
5089 return nullptr;
5090}
5091
5092/// Build postupdate expression for the given list of postupdates expressions.
5093static Expr *buildPostUpdate(Sema &S, ArrayRef<Expr *> PostUpdates) {
5094 Expr *PostUpdate = nullptr;
5095 if (!PostUpdates.empty()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005096 for (Expr *E : PostUpdates) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005097 Expr *ConvE = S.BuildCStyleCastExpr(
5098 E->getExprLoc(),
5099 S.Context.getTrivialTypeSourceInfo(S.Context.VoidTy),
5100 E->getExprLoc(), E)
5101 .get();
5102 PostUpdate = PostUpdate
5103 ? S.CreateBuiltinBinOp(ConvE->getExprLoc(), BO_Comma,
5104 PostUpdate, ConvE)
5105 .get()
5106 : ConvE;
5107 }
5108 }
5109 return PostUpdate;
5110}
5111
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00005112/// Called on a for stmt to check itself and nested loops (if any).
Alexey Bataevabfc0692014-06-25 06:52:00 +00005113/// \return Returns 0 if one of the collapsed stmts is not canonical for loop,
5114/// number of collapsed loops otherwise.
Alexey Bataev4acb8592014-07-07 13:01:15 +00005115static unsigned
Alexey Bataeve3727102018-04-18 15:57:46 +00005116checkOpenMPLoop(OpenMPDirectiveKind DKind, Expr *CollapseLoopCountExpr,
Alexey Bataev10e775f2015-07-30 11:36:16 +00005117 Expr *OrderedLoopCountExpr, Stmt *AStmt, Sema &SemaRef,
5118 DSAStackTy &DSA,
Alexey Bataeve3727102018-04-18 15:57:46 +00005119 Sema::VarsWithInheritedDSAType &VarsWithImplicitDSA,
Alexander Musmanc6388682014-12-15 07:07:06 +00005120 OMPLoopDirective::HelperExprs &Built) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005121 unsigned NestedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005122 if (CollapseLoopCountExpr) {
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005123 // Found 'collapse' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005124 Expr::EvalResult Result;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005125 if (CollapseLoopCountExpr->EvaluateAsInt(Result, SemaRef.getASTContext()))
Fangrui Song407659a2018-11-30 23:41:18 +00005126 NestedLoopCount = Result.Val.getInt().getLimitedValue();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005127 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005128 unsigned OrderedLoopCount = 1;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005129 if (OrderedLoopCountExpr) {
5130 // Found 'ordered' clause - calculate collapse number.
Fangrui Song407659a2018-11-30 23:41:18 +00005131 Expr::EvalResult EVResult;
5132 if (OrderedLoopCountExpr->EvaluateAsInt(EVResult, SemaRef.getASTContext())) {
5133 llvm::APSInt Result = EVResult.Val.getInt();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005134 if (Result.getLimitedValue() < NestedLoopCount) {
5135 SemaRef.Diag(OrderedLoopCountExpr->getExprLoc(),
5136 diag::err_omp_wrong_ordered_loop_count)
5137 << OrderedLoopCountExpr->getSourceRange();
5138 SemaRef.Diag(CollapseLoopCountExpr->getExprLoc(),
5139 diag::note_collapse_loop_count)
5140 << CollapseLoopCountExpr->getSourceRange();
5141 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005142 OrderedLoopCount = Result.getLimitedValue();
Alexey Bataev7b6bc882015-11-26 07:50:39 +00005143 }
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005144 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005145 // This is helper routine for loop directives (e.g., 'for', 'simd',
5146 // 'for simd', etc.).
Alexey Bataeve3727102018-04-18 15:57:46 +00005147 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev316ccf62019-01-29 18:51:58 +00005148 SmallVector<LoopIterationSpace, 4> IterSpaces(
5149 std::max(OrderedLoopCount, NestedLoopCount));
Alexander Musmana5f070a2014-10-01 06:03:56 +00005150 Stmt *CurStmt = AStmt->IgnoreContainers(/* IgnoreCaptured */ true);
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005151 for (unsigned Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00005152 if (checkOpenMPIterationSpace(
5153 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5154 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5155 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5156 Captures))
Alexey Bataevabfc0692014-06-25 06:52:00 +00005157 return 0;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005158 // Move on to the next nested for loop, or to the loop body.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005159 // OpenMP [2.8.1, simd construct, Restrictions]
5160 // All loops associated with the construct must be perfectly nested; that
5161 // is, there must be no intervening code nor any OpenMP directive between
5162 // any two loops.
5163 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005164 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00005165 for (unsigned Cnt = NestedLoopCount; Cnt < OrderedLoopCount; ++Cnt) {
5166 if (checkOpenMPIterationSpace(
5167 DKind, CurStmt, SemaRef, DSA, Cnt, NestedLoopCount,
5168 std::max(OrderedLoopCount, NestedLoopCount), CollapseLoopCountExpr,
5169 OrderedLoopCountExpr, VarsWithImplicitDSA, IterSpaces[Cnt],
5170 Captures))
5171 return 0;
5172 if (Cnt > 0 && IterSpaces[Cnt].CounterVar) {
5173 // Handle initialization of captured loop iterator variables.
5174 auto *DRE = cast<DeclRefExpr>(IterSpaces[Cnt].CounterVar);
5175 if (isa<OMPCapturedExprDecl>(DRE->getDecl())) {
5176 Captures[DRE] = DRE;
5177 }
5178 }
5179 // Move on to the next nested for loop, or to the loop body.
5180 // OpenMP [2.8.1, simd construct, Restrictions]
5181 // All loops associated with the construct must be perfectly nested; that
5182 // is, there must be no intervening code nor any OpenMP directive between
5183 // any two loops.
5184 CurStmt = cast<ForStmt>(CurStmt)->getBody()->IgnoreContainers();
5185 }
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005186
Alexander Musmana5f070a2014-10-01 06:03:56 +00005187 Built.clear(/* size */ NestedLoopCount);
5188
5189 if (SemaRef.CurContext->isDependentContext())
5190 return NestedLoopCount;
5191
5192 // An example of what is generated for the following code:
5193 //
Alexey Bataev10e775f2015-07-30 11:36:16 +00005194 // #pragma omp simd collapse(2) ordered(2)
Alexander Musmana5f070a2014-10-01 06:03:56 +00005195 // for (i = 0; i < NI; ++i)
Alexey Bataev10e775f2015-07-30 11:36:16 +00005196 // for (k = 0; k < NK; ++k)
5197 // for (j = J0; j < NJ; j+=2) {
5198 // <loop body>
5199 // }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005200 //
5201 // We generate the code below.
5202 // Note: the loop body may be outlined in CodeGen.
5203 // Note: some counters may be C++ classes, operator- is used to find number of
5204 // iterations and operator+= to calculate counter value.
5205 // Note: decltype(NumIterations) must be integer type (in 'omp for', only i32
5206 // or i64 is currently supported).
5207 //
5208 // #define NumIterations (NI * ((NJ - J0 - 1 + 2) / 2))
5209 // for (int[32|64]_t IV = 0; IV < NumIterations; ++IV ) {
5210 // .local.i = IV / ((NJ - J0 - 1 + 2) / 2);
5211 // .local.j = J0 + (IV % ((NJ - J0 - 1 + 2) / 2)) * 2;
5212 // // similar updates for vars in clauses (e.g. 'linear')
5213 // <loop body (using local i and j)>
5214 // }
5215 // i = NI; // assign final values of counters
5216 // j = NJ;
5217 //
5218
5219 // Last iteration number is (I1 * I2 * ... In) - 1, where I1, I2 ... In are
5220 // the iteration counts of the collapsed for loops.
Alexey Bataev62dbb972015-04-22 11:59:37 +00005221 // Precondition tests if there is at least one iteration (all conditions are
5222 // true).
5223 auto PreCond = ExprResult(IterSpaces[0].PreCond);
Alexey Bataeve3727102018-04-18 15:57:46 +00005224 Expr *N0 = IterSpaces[0].NumIterations;
5225 ExprResult LastIteration32 =
5226 widenIterationCount(/*Bits=*/32,
5227 SemaRef
5228 .PerformImplicitConversion(
5229 N0->IgnoreImpCasts(), N0->getType(),
5230 Sema::AA_Converting, /*AllowExplicit=*/true)
5231 .get(),
5232 SemaRef);
5233 ExprResult LastIteration64 = widenIterationCount(
5234 /*Bits=*/64,
5235 SemaRef
5236 .PerformImplicitConversion(N0->IgnoreImpCasts(), N0->getType(),
5237 Sema::AA_Converting,
5238 /*AllowExplicit=*/true)
5239 .get(),
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005240 SemaRef);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005241
5242 if (!LastIteration32.isUsable() || !LastIteration64.isUsable())
5243 return NestedLoopCount;
5244
Alexey Bataeve3727102018-04-18 15:57:46 +00005245 ASTContext &C = SemaRef.Context;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005246 bool AllCountsNeedLessThan32Bits = C.getTypeSize(N0->getType()) < 32;
5247
5248 Scope *CurScope = DSA.getCurScope();
5249 for (unsigned Cnt = 1; Cnt < NestedLoopCount; ++Cnt) {
Alexey Bataev62dbb972015-04-22 11:59:37 +00005250 if (PreCond.isUsable()) {
Alexey Bataeva7206b92016-12-20 16:51:02 +00005251 PreCond =
5252 SemaRef.BuildBinOp(CurScope, PreCond.get()->getExprLoc(), BO_LAnd,
5253 PreCond.get(), IterSpaces[Cnt].PreCond);
Alexey Bataev62dbb972015-04-22 11:59:37 +00005254 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005255 Expr *N = IterSpaces[Cnt].NumIterations;
Alexey Bataeva7206b92016-12-20 16:51:02 +00005256 SourceLocation Loc = N->getExprLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005257 AllCountsNeedLessThan32Bits &= C.getTypeSize(N->getType()) < 32;
5258 if (LastIteration32.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005259 LastIteration32 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005260 CurScope, Loc, BO_Mul, LastIteration32.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005261 SemaRef
5262 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5263 Sema::AA_Converting,
5264 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005265 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005266 if (LastIteration64.isUsable())
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005267 LastIteration64 = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005268 CurScope, Loc, BO_Mul, LastIteration64.get(),
David Majnemer9d168222016-08-05 17:44:54 +00005269 SemaRef
5270 .PerformImplicitConversion(N->IgnoreImpCasts(), N->getType(),
5271 Sema::AA_Converting,
5272 /*AllowExplicit=*/true)
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005273 .get());
Alexander Musmana5f070a2014-10-01 06:03:56 +00005274 }
5275
5276 // Choose either the 32-bit or 64-bit version.
5277 ExprResult LastIteration = LastIteration64;
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +00005278 if (SemaRef.getLangOpts().OpenMPOptimisticCollapse ||
5279 (LastIteration32.isUsable() &&
5280 C.getTypeSize(LastIteration32.get()->getType()) == 32 &&
5281 (AllCountsNeedLessThan32Bits || NestedLoopCount == 1 ||
5282 fitsInto(
5283 /*Bits=*/32,
5284 LastIteration32.get()->getType()->hasSignedIntegerRepresentation(),
5285 LastIteration64.get(), SemaRef))))
Alexander Musmana5f070a2014-10-01 06:03:56 +00005286 LastIteration = LastIteration32;
Alexey Bataev7292c292016-04-25 12:22:29 +00005287 QualType VType = LastIteration.get()->getType();
5288 QualType RealVType = VType;
5289 QualType StrideVType = VType;
5290 if (isOpenMPTaskLoopDirective(DKind)) {
5291 VType =
5292 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/0);
5293 StrideVType =
5294 SemaRef.Context.getIntTypeForBitwidth(/*DestWidth=*/64, /*Signed=*/1);
5295 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005296
5297 if (!LastIteration.isUsable())
5298 return 0;
5299
5300 // Save the number of iterations.
5301 ExprResult NumIterations = LastIteration;
5302 {
5303 LastIteration = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005304 CurScope, LastIteration.get()->getExprLoc(), BO_Sub,
5305 LastIteration.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005306 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5307 if (!LastIteration.isUsable())
5308 return 0;
5309 }
5310
5311 // Calculate the last iteration number beforehand instead of doing this on
5312 // each iteration. Do not do this if the number of iterations may be kfold-ed.
5313 llvm::APSInt Result;
5314 bool IsConstant =
5315 LastIteration.get()->isIntegerConstantExpr(Result, SemaRef.Context);
5316 ExprResult CalcLastIteration;
5317 if (!IsConstant) {
Alexey Bataev5a3af132016-03-29 08:58:54 +00005318 ExprResult SaveRef =
5319 tryBuildCapture(SemaRef, LastIteration.get(), Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005320 LastIteration = SaveRef;
5321
5322 // Prepare SaveRef + 1.
5323 NumIterations = SemaRef.BuildBinOp(
Alexey Bataeva7206b92016-12-20 16:51:02 +00005324 CurScope, SaveRef.get()->getExprLoc(), BO_Add, SaveRef.get(),
Alexander Musmana5f070a2014-10-01 06:03:56 +00005325 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get());
5326 if (!NumIterations.isUsable())
5327 return 0;
5328 }
5329
5330 SourceLocation InitLoc = IterSpaces[0].InitSrcRange.getBegin();
5331
David Majnemer9d168222016-08-05 17:44:54 +00005332 // Build variables passed into runtime, necessary for worksharing directives.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005333 ExprResult LB, UB, IL, ST, EUB, CombLB, CombUB, PrevLB, PrevUB, CombEUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005334 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5335 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005336 // Lower bound variable, initialized with zero.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005337 VarDecl *LBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.lb");
5338 LB = buildDeclRefExpr(SemaRef, LBDecl, VType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005339 SemaRef.AddInitializerToDecl(LBDecl,
5340 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5341 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005342
5343 // Upper bound variable, initialized with last iteration number.
Alexey Bataev39f915b82015-05-08 10:41:21 +00005344 VarDecl *UBDecl = buildVarDecl(SemaRef, InitLoc, VType, ".omp.ub");
5345 UB = buildDeclRefExpr(SemaRef, UBDecl, VType, InitLoc);
Alexander Musmanc6388682014-12-15 07:07:06 +00005346 SemaRef.AddInitializerToDecl(UBDecl, LastIteration.get(),
Richard Smith3beb7c62017-01-12 02:27:38 +00005347 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005348
5349 // A 32-bit variable-flag where runtime returns 1 for the last iteration.
5350 // This will be used to implement clause 'lastprivate'.
5351 QualType Int32Ty = SemaRef.Context.getIntTypeForBitwidth(32, true);
Alexey Bataev39f915b82015-05-08 10:41:21 +00005352 VarDecl *ILDecl = buildVarDecl(SemaRef, InitLoc, Int32Ty, ".omp.is_last");
5353 IL = buildDeclRefExpr(SemaRef, ILDecl, Int32Ty, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005354 SemaRef.AddInitializerToDecl(ILDecl,
5355 SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5356 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005357
5358 // Stride variable returned by runtime (we initialize it to 1 by default).
Alexey Bataev7292c292016-04-25 12:22:29 +00005359 VarDecl *STDecl =
5360 buildVarDecl(SemaRef, InitLoc, StrideVType, ".omp.stride");
5361 ST = buildDeclRefExpr(SemaRef, STDecl, StrideVType, InitLoc);
Richard Smith3beb7c62017-01-12 02:27:38 +00005362 SemaRef.AddInitializerToDecl(STDecl,
5363 SemaRef.ActOnIntegerConstant(InitLoc, 1).get(),
5364 /*DirectInit*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005365
5366 // Build expression: UB = min(UB, LastIteration)
David Majnemer9d168222016-08-05 17:44:54 +00005367 // It is necessary for CodeGen of directives with static scheduling.
Alexander Musmanc6388682014-12-15 07:07:06 +00005368 ExprResult IsUBGreater = SemaRef.BuildBinOp(CurScope, InitLoc, BO_GT,
5369 UB.get(), LastIteration.get());
5370 ExprResult CondOp = SemaRef.ActOnConditionalOp(
Alexey Bataev86ec3fe2018-07-25 14:40:26 +00005371 LastIteration.get()->getExprLoc(), InitLoc, IsUBGreater.get(),
5372 LastIteration.get(), UB.get());
Alexander Musmanc6388682014-12-15 07:07:06 +00005373 EUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, UB.get(),
5374 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005375 EUB = SemaRef.ActOnFinishFullExpr(EUB.get(), /*DiscardedValue*/ false);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005376
5377 // If we have a combined directive that combines 'distribute', 'for' or
5378 // 'simd' we need to be able to access the bounds of the schedule of the
5379 // enclosing region. E.g. in 'distribute parallel for' the bounds obtained
5380 // by scheduling 'distribute' have to be passed to the schedule of 'for'.
5381 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Carlo Bertolliffafe102017-04-20 00:39:39 +00005382 // Lower bound variable, initialized with zero.
5383 VarDecl *CombLBDecl =
5384 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.lb");
5385 CombLB = buildDeclRefExpr(SemaRef, CombLBDecl, VType, InitLoc);
5386 SemaRef.AddInitializerToDecl(
5387 CombLBDecl, SemaRef.ActOnIntegerConstant(InitLoc, 0).get(),
5388 /*DirectInit*/ false);
5389
5390 // Upper bound variable, initialized with last iteration number.
5391 VarDecl *CombUBDecl =
5392 buildVarDecl(SemaRef, InitLoc, VType, ".omp.comb.ub");
5393 CombUB = buildDeclRefExpr(SemaRef, CombUBDecl, VType, InitLoc);
5394 SemaRef.AddInitializerToDecl(CombUBDecl, LastIteration.get(),
5395 /*DirectInit*/ false);
5396
5397 ExprResult CombIsUBGreater = SemaRef.BuildBinOp(
5398 CurScope, InitLoc, BO_GT, CombUB.get(), LastIteration.get());
5399 ExprResult CombCondOp =
5400 SemaRef.ActOnConditionalOp(InitLoc, InitLoc, CombIsUBGreater.get(),
5401 LastIteration.get(), CombUB.get());
5402 CombEUB = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, CombUB.get(),
5403 CombCondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005404 CombEUB =
5405 SemaRef.ActOnFinishFullExpr(CombEUB.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005406
Alexey Bataeve3727102018-04-18 15:57:46 +00005407 const CapturedDecl *CD = cast<CapturedStmt>(AStmt)->getCapturedDecl();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005408 // We expect to have at least 2 more parameters than the 'parallel'
5409 // directive does - the lower and upper bounds of the previous schedule.
5410 assert(CD->getNumParams() >= 4 &&
5411 "Unexpected number of parameters in loop combined directive");
5412
5413 // Set the proper type for the bounds given what we learned from the
5414 // enclosed loops.
Alexey Bataeve3727102018-04-18 15:57:46 +00005415 ImplicitParamDecl *PrevLBDecl = CD->getParam(/*PrevLB=*/2);
5416 ImplicitParamDecl *PrevUBDecl = CD->getParam(/*PrevUB=*/3);
Carlo Bertolli9925f152016-06-27 14:55:37 +00005417
5418 // Previous lower and upper bounds are obtained from the region
5419 // parameters.
5420 PrevLB =
5421 buildDeclRefExpr(SemaRef, PrevLBDecl, PrevLBDecl->getType(), InitLoc);
5422 PrevUB =
5423 buildDeclRefExpr(SemaRef, PrevUBDecl, PrevUBDecl->getType(), InitLoc);
5424 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005425 }
5426
5427 // Build the iteration variable and its initialization before loop.
Alexander Musmana5f070a2014-10-01 06:03:56 +00005428 ExprResult IV;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005429 ExprResult Init, CombInit;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005430 {
Alexey Bataev7292c292016-04-25 12:22:29 +00005431 VarDecl *IVDecl = buildVarDecl(SemaRef, InitLoc, RealVType, ".omp.iv");
5432 IV = buildDeclRefExpr(SemaRef, IVDecl, RealVType, InitLoc);
David Majnemer9d168222016-08-05 17:44:54 +00005433 Expr *RHS =
5434 (isOpenMPWorksharingDirective(DKind) ||
5435 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
5436 ? LB.get()
5437 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005438 Init = SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), RHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005439 Init = SemaRef.ActOnFinishFullExpr(Init.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005440
5441 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5442 Expr *CombRHS =
5443 (isOpenMPWorksharingDirective(DKind) ||
5444 isOpenMPTaskLoopDirective(DKind) ||
5445 isOpenMPDistributeDirective(DKind))
5446 ? CombLB.get()
5447 : SemaRef.ActOnIntegerConstant(SourceLocation(), 0).get();
5448 CombInit =
5449 SemaRef.BuildBinOp(CurScope, InitLoc, BO_Assign, IV.get(), CombRHS);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005450 CombInit =
5451 SemaRef.ActOnFinishFullExpr(CombInit.get(), /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005452 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005453 }
5454
Alexey Bataev316ccf62019-01-29 18:51:58 +00005455 bool UseStrictCompare =
5456 RealVType->hasUnsignedIntegerRepresentation() &&
5457 llvm::all_of(IterSpaces, [](const LoopIterationSpace &LIS) {
5458 return LIS.IsStrictCompare;
5459 });
5460 // Loop condition (IV < NumIterations) or (IV <= UB or IV < UB + 1 (for
5461 // unsigned IV)) for worksharing loops.
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005462 SourceLocation CondLoc = AStmt->getBeginLoc();
Alexey Bataev316ccf62019-01-29 18:51:58 +00005463 Expr *BoundUB = UB.get();
5464 if (UseStrictCompare) {
5465 BoundUB =
5466 SemaRef
5467 .BuildBinOp(CurScope, CondLoc, BO_Add, BoundUB,
5468 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5469 .get();
5470 BoundUB =
5471 SemaRef.ActOnFinishFullExpr(BoundUB, /*DiscardedValue*/ false).get();
5472 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005473 ExprResult Cond =
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005474 (isOpenMPWorksharingDirective(DKind) ||
5475 isOpenMPTaskLoopDirective(DKind) || isOpenMPDistributeDirective(DKind))
Alexey Bataev316ccf62019-01-29 18:51:58 +00005476 ? SemaRef.BuildBinOp(CurScope, CondLoc,
5477 UseStrictCompare ? BO_LT : BO_LE, IV.get(),
5478 BoundUB)
Alexander Musmanc6388682014-12-15 07:07:06 +00005479 : SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5480 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005481 ExprResult CombDistCond;
5482 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005483 CombDistCond = SemaRef.BuildBinOp(CurScope, CondLoc, BO_LT, IV.get(),
5484 NumIterations.get());
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005485 }
5486
Carlo Bertolliffafe102017-04-20 00:39:39 +00005487 ExprResult CombCond;
5488 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005489 Expr *BoundCombUB = CombUB.get();
5490 if (UseStrictCompare) {
5491 BoundCombUB =
5492 SemaRef
5493 .BuildBinOp(
5494 CurScope, CondLoc, BO_Add, BoundCombUB,
5495 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5496 .get();
5497 BoundCombUB =
5498 SemaRef.ActOnFinishFullExpr(BoundCombUB, /*DiscardedValue*/ false)
5499 .get();
5500 }
Carlo Bertolliffafe102017-04-20 00:39:39 +00005501 CombCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00005502 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
5503 IV.get(), BoundCombUB);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005504 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005505 // Loop increment (IV = IV + 1)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005506 SourceLocation IncLoc = AStmt->getBeginLoc();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005507 ExprResult Inc =
5508 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, IV.get(),
5509 SemaRef.ActOnIntegerConstant(IncLoc, 1).get());
5510 if (!Inc.isUsable())
5511 return 0;
5512 Inc = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, IV.get(), Inc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005513 Inc = SemaRef.ActOnFinishFullExpr(Inc.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005514 if (!Inc.isUsable())
5515 return 0;
5516
5517 // Increments for worksharing loops (LB = LB + ST; UB = UB + ST).
5518 // Used for directives with static scheduling.
Carlo Bertolliffafe102017-04-20 00:39:39 +00005519 // In combined construct, add combined version that use CombLB and CombUB
5520 // base variables for the update
5521 ExprResult NextLB, NextUB, CombNextLB, CombNextUB;
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00005522 if (isOpenMPWorksharingDirective(DKind) || isOpenMPTaskLoopDirective(DKind) ||
5523 isOpenMPDistributeDirective(DKind)) {
Alexander Musmanc6388682014-12-15 07:07:06 +00005524 // LB + ST
5525 NextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, LB.get(), ST.get());
5526 if (!NextLB.isUsable())
5527 return 0;
5528 // LB = LB + ST
5529 NextLB =
5530 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, LB.get(), NextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005531 NextLB =
5532 SemaRef.ActOnFinishFullExpr(NextLB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005533 if (!NextLB.isUsable())
5534 return 0;
5535 // UB + ST
5536 NextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, UB.get(), ST.get());
5537 if (!NextUB.isUsable())
5538 return 0;
5539 // UB = UB + ST
5540 NextUB =
5541 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, UB.get(), NextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005542 NextUB =
5543 SemaRef.ActOnFinishFullExpr(NextUB.get(), /*DiscardedValue*/ false);
Alexander Musmanc6388682014-12-15 07:07:06 +00005544 if (!NextUB.isUsable())
5545 return 0;
Carlo Bertolliffafe102017-04-20 00:39:39 +00005546 if (isOpenMPLoopBoundSharingDirective(DKind)) {
5547 CombNextLB =
5548 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombLB.get(), ST.get());
5549 if (!NextLB.isUsable())
5550 return 0;
5551 // LB = LB + ST
5552 CombNextLB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombLB.get(),
5553 CombNextLB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005554 CombNextLB = SemaRef.ActOnFinishFullExpr(CombNextLB.get(),
5555 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005556 if (!CombNextLB.isUsable())
5557 return 0;
5558 // UB + ST
5559 CombNextUB =
5560 SemaRef.BuildBinOp(CurScope, IncLoc, BO_Add, CombUB.get(), ST.get());
5561 if (!CombNextUB.isUsable())
5562 return 0;
5563 // UB = UB + ST
5564 CombNextUB = SemaRef.BuildBinOp(CurScope, IncLoc, BO_Assign, CombUB.get(),
5565 CombNextUB.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005566 CombNextUB = SemaRef.ActOnFinishFullExpr(CombNextUB.get(),
5567 /*DiscardedValue*/ false);
Carlo Bertolliffafe102017-04-20 00:39:39 +00005568 if (!CombNextUB.isUsable())
5569 return 0;
5570 }
Alexander Musmanc6388682014-12-15 07:07:06 +00005571 }
Alexander Musmana5f070a2014-10-01 06:03:56 +00005572
Carlo Bertolliffafe102017-04-20 00:39:39 +00005573 // Create increment expression for distribute loop when combined in a same
Carlo Bertolli8429d812017-02-17 21:29:13 +00005574 // directive with for as IV = IV + ST; ensure upper bound expression based
5575 // on PrevUB instead of NumIterations - used to implement 'for' when found
5576 // in combination with 'distribute', like in 'distribute parallel for'
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005577 SourceLocation DistIncLoc = AStmt->getBeginLoc();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005578 ExprResult DistCond, DistInc, PrevEUB, ParForInDistCond;
Carlo Bertolli8429d812017-02-17 21:29:13 +00005579 if (isOpenMPLoopBoundSharingDirective(DKind)) {
Alexey Bataev316ccf62019-01-29 18:51:58 +00005580 DistCond = SemaRef.BuildBinOp(
5581 CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE, IV.get(), BoundUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005582 assert(DistCond.isUsable() && "distribute cond expr was not built");
5583
5584 DistInc =
5585 SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Add, IV.get(), ST.get());
5586 assert(DistInc.isUsable() && "distribute inc expr was not built");
5587 DistInc = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, IV.get(),
5588 DistInc.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005589 DistInc =
5590 SemaRef.ActOnFinishFullExpr(DistInc.get(), /*DiscardedValue*/ false);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005591 assert(DistInc.isUsable() && "distribute inc expr was not built");
5592
5593 // Build expression: UB = min(UB, prevUB) for #for in composite or combined
5594 // construct
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005595 SourceLocation DistEUBLoc = AStmt->getBeginLoc();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005596 ExprResult IsUBGreater =
5597 SemaRef.BuildBinOp(CurScope, DistEUBLoc, BO_GT, UB.get(), PrevUB.get());
5598 ExprResult CondOp = SemaRef.ActOnConditionalOp(
5599 DistEUBLoc, DistEUBLoc, IsUBGreater.get(), PrevUB.get(), UB.get());
5600 PrevEUB = SemaRef.BuildBinOp(CurScope, DistIncLoc, BO_Assign, UB.get(),
5601 CondOp.get());
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005602 PrevEUB =
5603 SemaRef.ActOnFinishFullExpr(PrevEUB.get(), /*DiscardedValue*/ false);
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005604
Alexey Bataev316ccf62019-01-29 18:51:58 +00005605 // Build IV <= PrevUB or IV < PrevUB + 1 for unsigned IV to be used in
5606 // parallel for is in combination with a distribute directive with
5607 // schedule(static, 1)
5608 Expr *BoundPrevUB = PrevUB.get();
5609 if (UseStrictCompare) {
5610 BoundPrevUB =
5611 SemaRef
5612 .BuildBinOp(
5613 CurScope, CondLoc, BO_Add, BoundPrevUB,
5614 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get())
5615 .get();
5616 BoundPrevUB =
5617 SemaRef.ActOnFinishFullExpr(BoundPrevUB, /*DiscardedValue*/ false)
5618 .get();
5619 }
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005620 ParForInDistCond =
Alexey Bataev316ccf62019-01-29 18:51:58 +00005621 SemaRef.BuildBinOp(CurScope, CondLoc, UseStrictCompare ? BO_LT : BO_LE,
5622 IV.get(), BoundPrevUB);
Carlo Bertolli8429d812017-02-17 21:29:13 +00005623 }
5624
Alexander Musmana5f070a2014-10-01 06:03:56 +00005625 // Build updates and final values of the loop counters.
5626 bool HasErrors = false;
5627 Built.Counters.resize(NestedLoopCount);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005628 Built.Inits.resize(NestedLoopCount);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005629 Built.Updates.resize(NestedLoopCount);
5630 Built.Finals.resize(NestedLoopCount);
5631 {
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005632 // We implement the following algorithm for obtaining the
5633 // original loop iteration variable values based on the
5634 // value of the collapsed loop iteration variable IV.
5635 //
5636 // Let n+1 be the number of collapsed loops in the nest.
5637 // Iteration variables (I0, I1, .... In)
5638 // Iteration counts (N0, N1, ... Nn)
5639 //
5640 // Acc = IV;
5641 //
5642 // To compute Ik for loop k, 0 <= k <= n, generate:
5643 // Prod = N(k+1) * N(k+2) * ... * Nn;
5644 // Ik = Acc / Prod;
5645 // Acc -= Ik * Prod;
5646 //
5647 ExprResult Acc = IV;
5648 for (unsigned int Cnt = 0; Cnt < NestedLoopCount; ++Cnt) {
Alexander Musmana5f070a2014-10-01 06:03:56 +00005649 LoopIterationSpace &IS = IterSpaces[Cnt];
5650 SourceLocation UpdLoc = IS.IncSrcRange.getBegin();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005651 ExprResult Iter;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005652
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005653 // Compute prod
5654 ExprResult Prod =
5655 SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
5656 for (unsigned int K = Cnt+1; K < NestedLoopCount; ++K)
5657 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul, Prod.get(),
5658 IterSpaces[K].NumIterations);
5659
5660 // Iter = Acc / Prod
5661 // If there is at least one more inner loop to avoid
5662 // multiplication by 1.
5663 if (Cnt + 1 < NestedLoopCount)
5664 Iter = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Div,
5665 Acc.get(), Prod.get());
5666 else
5667 Iter = Acc;
Alexander Musmana5f070a2014-10-01 06:03:56 +00005668 if (!Iter.isUsable()) {
5669 HasErrors = true;
5670 break;
5671 }
5672
Gheorghe-Teodor Bercea677960642019-01-09 20:45:26 +00005673 // Update Acc:
5674 // Acc -= Iter * Prod
5675 // Check if there is at least one more inner loop to avoid
5676 // multiplication by 1.
5677 if (Cnt + 1 < NestedLoopCount)
5678 Prod = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Mul,
5679 Iter.get(), Prod.get());
5680 else
5681 Prod = Iter;
5682 Acc = SemaRef.BuildBinOp(CurScope, UpdLoc, BO_Sub,
5683 Acc.get(), Prod.get());
5684
Alexey Bataev39f915b82015-05-08 10:41:21 +00005685 // Build update: IS.CounterVar(Private) = IS.Start + Iter * IS.Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005686 auto *VD = cast<VarDecl>(cast<DeclRefExpr>(IS.CounterVar)->getDecl());
Alexey Bataeve3727102018-04-18 15:57:46 +00005687 DeclRefExpr *CounterVar = buildDeclRefExpr(
5688 SemaRef, VD, IS.CounterVar->getType(), IS.CounterVar->getExprLoc(),
5689 /*RefersToCapture=*/true);
5690 ExprResult Init = buildCounterInit(SemaRef, CurScope, UpdLoc, CounterVar,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005691 IS.CounterInit, Captures);
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005692 if (!Init.isUsable()) {
5693 HasErrors = true;
5694 break;
5695 }
Alexey Bataeve3727102018-04-18 15:57:46 +00005696 ExprResult Update = buildCounterUpdate(
Alexey Bataev5a3af132016-03-29 08:58:54 +00005697 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit, Iter,
5698 IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005699 if (!Update.isUsable()) {
5700 HasErrors = true;
5701 break;
5702 }
5703
5704 // Build final: IS.CounterVar = IS.Start + IS.NumIters * IS.Step
Alexey Bataeve3727102018-04-18 15:57:46 +00005705 ExprResult Final = buildCounterUpdate(
Alexey Bataev39f915b82015-05-08 10:41:21 +00005706 SemaRef, CurScope, UpdLoc, CounterVar, IS.CounterInit,
Alexey Bataev5a3af132016-03-29 08:58:54 +00005707 IS.NumIterations, IS.CounterStep, IS.Subtract, &Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005708 if (!Final.isUsable()) {
5709 HasErrors = true;
5710 break;
5711 }
5712
Alexander Musmana5f070a2014-10-01 06:03:56 +00005713 if (!Update.isUsable() || !Final.isUsable()) {
5714 HasErrors = true;
5715 break;
5716 }
5717 // Save results
5718 Built.Counters[Cnt] = IS.CounterVar;
Alexey Bataeva8899172015-08-06 12:30:57 +00005719 Built.PrivateCounters[Cnt] = IS.PrivateCounterVar;
Alexey Bataevb08f89f2015-08-14 12:25:37 +00005720 Built.Inits[Cnt] = Init.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005721 Built.Updates[Cnt] = Update.get();
5722 Built.Finals[Cnt] = Final.get();
5723 }
5724 }
5725
5726 if (HasErrors)
5727 return 0;
5728
5729 // Save results
5730 Built.IterationVarRef = IV.get();
5731 Built.LastIteration = LastIteration.get();
Alexander Musman3276a272015-03-21 10:12:56 +00005732 Built.NumIterations = NumIterations.get();
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +00005733 Built.CalcLastIteration = SemaRef
5734 .ActOnFinishFullExpr(CalcLastIteration.get(),
5735 /*DiscardedValue*/ false)
5736 .get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005737 Built.PreCond = PreCond.get();
Alexey Bataev5a3af132016-03-29 08:58:54 +00005738 Built.PreInits = buildPreInits(C, Captures);
Alexander Musmana5f070a2014-10-01 06:03:56 +00005739 Built.Cond = Cond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005740 Built.Init = Init.get();
5741 Built.Inc = Inc.get();
Alexander Musmanc6388682014-12-15 07:07:06 +00005742 Built.LB = LB.get();
5743 Built.UB = UB.get();
5744 Built.IL = IL.get();
5745 Built.ST = ST.get();
5746 Built.EUB = EUB.get();
5747 Built.NLB = NextLB.get();
5748 Built.NUB = NextUB.get();
Carlo Bertolli9925f152016-06-27 14:55:37 +00005749 Built.PrevLB = PrevLB.get();
5750 Built.PrevUB = PrevUB.get();
Carlo Bertolli8429d812017-02-17 21:29:13 +00005751 Built.DistInc = DistInc.get();
5752 Built.PrevEUB = PrevEUB.get();
Carlo Bertolliffafe102017-04-20 00:39:39 +00005753 Built.DistCombinedFields.LB = CombLB.get();
5754 Built.DistCombinedFields.UB = CombUB.get();
5755 Built.DistCombinedFields.EUB = CombEUB.get();
5756 Built.DistCombinedFields.Init = CombInit.get();
5757 Built.DistCombinedFields.Cond = CombCond.get();
5758 Built.DistCombinedFields.NLB = CombNextLB.get();
5759 Built.DistCombinedFields.NUB = CombNextUB.get();
Gheorghe-Teodor Berceae9256762018-10-29 15:45:47 +00005760 Built.DistCombinedFields.DistCond = CombDistCond.get();
5761 Built.DistCombinedFields.ParForInDistCond = ParForInDistCond.get();
Alexander Musmana5f070a2014-10-01 06:03:56 +00005762
Alexey Bataevabfc0692014-06-25 06:52:00 +00005763 return NestedLoopCount;
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00005764}
5765
Alexey Bataev10e775f2015-07-30 11:36:16 +00005766static Expr *getCollapseNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005767 auto CollapseClauses =
5768 OMPExecutableDirective::getClausesOfKind<OMPCollapseClause>(Clauses);
5769 if (CollapseClauses.begin() != CollapseClauses.end())
5770 return (*CollapseClauses.begin())->getNumForLoops();
Alexey Bataeve2f07d42014-06-24 12:55:56 +00005771 return nullptr;
5772}
5773
Alexey Bataev10e775f2015-07-30 11:36:16 +00005774static Expr *getOrderedNumberExpr(ArrayRef<OMPClause *> Clauses) {
Benjamin Kramerfc600dc2015-08-30 15:12:28 +00005775 auto OrderedClauses =
5776 OMPExecutableDirective::getClausesOfKind<OMPOrderedClause>(Clauses);
5777 if (OrderedClauses.begin() != OrderedClauses.end())
5778 return (*OrderedClauses.begin())->getNumForLoops();
Alexey Bataev10e775f2015-07-30 11:36:16 +00005779 return nullptr;
5780}
5781
Kelvin Lic5609492016-07-15 04:39:07 +00005782static bool checkSimdlenSafelenSpecified(Sema &S,
5783 const ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005784 const OMPSafelenClause *Safelen = nullptr;
5785 const OMPSimdlenClause *Simdlen = nullptr;
Kelvin Lic5609492016-07-15 04:39:07 +00005786
Alexey Bataeve3727102018-04-18 15:57:46 +00005787 for (const OMPClause *Clause : Clauses) {
Kelvin Lic5609492016-07-15 04:39:07 +00005788 if (Clause->getClauseKind() == OMPC_safelen)
5789 Safelen = cast<OMPSafelenClause>(Clause);
5790 else if (Clause->getClauseKind() == OMPC_simdlen)
5791 Simdlen = cast<OMPSimdlenClause>(Clause);
5792 if (Safelen && Simdlen)
5793 break;
5794 }
5795
5796 if (Simdlen && Safelen) {
Alexey Bataeve3727102018-04-18 15:57:46 +00005797 const Expr *SimdlenLength = Simdlen->getSimdlen();
5798 const Expr *SafelenLength = Safelen->getSafelen();
Kelvin Lic5609492016-07-15 04:39:07 +00005799 if (SimdlenLength->isValueDependent() || SimdlenLength->isTypeDependent() ||
5800 SimdlenLength->isInstantiationDependent() ||
5801 SimdlenLength->containsUnexpandedParameterPack())
5802 return false;
5803 if (SafelenLength->isValueDependent() || SafelenLength->isTypeDependent() ||
5804 SafelenLength->isInstantiationDependent() ||
5805 SafelenLength->containsUnexpandedParameterPack())
5806 return false;
Fangrui Song407659a2018-11-30 23:41:18 +00005807 Expr::EvalResult SimdlenResult, SafelenResult;
5808 SimdlenLength->EvaluateAsInt(SimdlenResult, S.Context);
5809 SafelenLength->EvaluateAsInt(SafelenResult, S.Context);
5810 llvm::APSInt SimdlenRes = SimdlenResult.Val.getInt();
5811 llvm::APSInt SafelenRes = SafelenResult.Val.getInt();
Kelvin Lic5609492016-07-15 04:39:07 +00005812 // OpenMP 4.5 [2.8.1, simd Construct, Restrictions]
5813 // If both simdlen and safelen clauses are specified, the value of the
5814 // simdlen parameter must be less than or equal to the value of the safelen
5815 // parameter.
5816 if (SimdlenRes > SafelenRes) {
5817 S.Diag(SimdlenLength->getExprLoc(),
5818 diag::err_omp_wrong_simdlen_safelen_values)
5819 << SimdlenLength->getSourceRange() << SafelenLength->getSourceRange();
5820 return true;
5821 }
Alexey Bataev66b15b52015-08-21 11:14:16 +00005822 }
5823 return false;
5824}
5825
Alexey Bataeve3727102018-04-18 15:57:46 +00005826StmtResult
5827Sema::ActOnOpenMPSimdDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5828 SourceLocation StartLoc, SourceLocation EndLoc,
5829 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005830 if (!AStmt)
5831 return StmtError();
5832
5833 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005834 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005835 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5836 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005837 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005838 OMPD_simd, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5839 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005840 if (NestedLoopCount == 0)
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005841 return StmtError();
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005842
Alexander Musmana5f070a2014-10-01 06:03:56 +00005843 assert((CurContext->isDependentContext() || B.builtAll()) &&
5844 "omp simd loop exprs were not built");
5845
Alexander Musman3276a272015-03-21 10:12:56 +00005846 if (!CurContext->isDependentContext()) {
5847 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005848 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005849 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexander Musman3276a272015-03-21 10:12:56 +00005850 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005851 B.NumIterations, *this, CurScope,
5852 DSAStack))
Alexander Musman3276a272015-03-21 10:12:56 +00005853 return StmtError();
5854 }
5855 }
5856
Kelvin Lic5609492016-07-15 04:39:07 +00005857 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005858 return StmtError();
5859
Reid Kleckner87a31802018-03-12 21:43:02 +00005860 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005861 return OMPSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5862 Clauses, AStmt, B);
Alexey Bataev1b59ab52014-02-27 08:29:12 +00005863}
5864
Alexey Bataeve3727102018-04-18 15:57:46 +00005865StmtResult
5866Sema::ActOnOpenMPForDirective(ArrayRef<OMPClause *> Clauses, Stmt *AStmt,
5867 SourceLocation StartLoc, SourceLocation EndLoc,
5868 VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005869 if (!AStmt)
5870 return StmtError();
5871
5872 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005873 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005874 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5875 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00005876 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataev10e775f2015-07-30 11:36:16 +00005877 OMPD_for, getCollapseNumberExpr(Clauses), getOrderedNumberExpr(Clauses),
5878 AStmt, *this, *DSAStack, VarsWithImplicitDSA, B);
Alexey Bataevabfc0692014-06-25 06:52:00 +00005879 if (NestedLoopCount == 0)
Alexey Bataevf29276e2014-06-18 04:14:57 +00005880 return StmtError();
5881
Alexander Musmana5f070a2014-10-01 06:03:56 +00005882 assert((CurContext->isDependentContext() || B.builtAll()) &&
5883 "omp for loop exprs were not built");
5884
Alexey Bataev54acd402015-08-04 11:18:19 +00005885 if (!CurContext->isDependentContext()) {
5886 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005887 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005888 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00005889 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005890 B.NumIterations, *this, CurScope,
5891 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00005892 return StmtError();
5893 }
5894 }
5895
Reid Kleckner87a31802018-03-12 21:43:02 +00005896 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005897 return OMPForDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
Alexey Bataev25e5b442015-09-15 12:52:43 +00005898 Clauses, AStmt, B, DSAStack->isCancelRegion());
Alexey Bataevf29276e2014-06-18 04:14:57 +00005899}
5900
Alexander Musmanf82886e2014-09-18 05:12:34 +00005901StmtResult Sema::ActOnOpenMPForSimdDirective(
5902 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00005903 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005904 if (!AStmt)
5905 return StmtError();
5906
5907 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmanc6388682014-12-15 07:07:06 +00005908 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00005909 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
5910 // define the nested loops number.
Alexander Musmanf82886e2014-09-18 05:12:34 +00005911 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00005912 checkOpenMPLoop(OMPD_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00005913 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
5914 VarsWithImplicitDSA, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005915 if (NestedLoopCount == 0)
5916 return StmtError();
5917
Alexander Musmanc6388682014-12-15 07:07:06 +00005918 assert((CurContext->isDependentContext() || B.builtAll()) &&
5919 "omp for simd loop exprs were not built");
5920
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005921 if (!CurContext->isDependentContext()) {
5922 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00005923 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00005924 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005925 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00005926 B.NumIterations, *this, CurScope,
5927 DSAStack))
Alexey Bataev58e5bdb2015-06-18 04:45:29 +00005928 return StmtError();
5929 }
5930 }
5931
Kelvin Lic5609492016-07-15 04:39:07 +00005932 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00005933 return StmtError();
5934
Reid Kleckner87a31802018-03-12 21:43:02 +00005935 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00005936 return OMPForSimdDirective::Create(Context, StartLoc, EndLoc, NestedLoopCount,
5937 Clauses, AStmt, B);
Alexander Musmanf82886e2014-09-18 05:12:34 +00005938}
5939
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005940StmtResult Sema::ActOnOpenMPSectionsDirective(ArrayRef<OMPClause *> Clauses,
5941 Stmt *AStmt,
5942 SourceLocation StartLoc,
5943 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005944 if (!AStmt)
5945 return StmtError();
5946
5947 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005948 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00005949 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005950 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00005951 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005952 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00005953 if (S.begin() == S.end())
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005954 return StmtError();
5955 // All associated statements must be '#pragma omp section' except for
5956 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00005957 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005958 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
5959 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005960 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005961 diag::err_omp_sections_substmt_not_section);
5962 return StmtError();
5963 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00005964 cast<OMPSectionDirective>(SectionStmt)
5965 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005966 }
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005967 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00005968 Diag(AStmt->getBeginLoc(), diag::err_omp_sections_not_compound_stmt);
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005969 return StmtError();
5970 }
5971
Reid Kleckner87a31802018-03-12 21:43:02 +00005972 setFunctionHasBranchProtectedScope();
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005973
Alexey Bataev25e5b442015-09-15 12:52:43 +00005974 return OMPSectionsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
5975 DSAStack->isCancelRegion());
Alexey Bataevd3f8dd22014-06-25 11:44:49 +00005976}
5977
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005978StmtResult Sema::ActOnOpenMPSectionDirective(Stmt *AStmt,
5979 SourceLocation StartLoc,
5980 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005981 if (!AStmt)
5982 return StmtError();
5983
5984 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005985
Reid Kleckner87a31802018-03-12 21:43:02 +00005986 setFunctionHasBranchProtectedScope();
Alexey Bataev25e5b442015-09-15 12:52:43 +00005987 DSAStack->setParentCancelRegion(DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005988
Alexey Bataev25e5b442015-09-15 12:52:43 +00005989 return OMPSectionDirective::Create(Context, StartLoc, EndLoc, AStmt,
5990 DSAStack->isCancelRegion());
Alexey Bataev1e0498a2014-06-26 08:21:58 +00005991}
5992
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00005993StmtResult Sema::ActOnOpenMPSingleDirective(ArrayRef<OMPClause *> Clauses,
5994 Stmt *AStmt,
5995 SourceLocation StartLoc,
5996 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00005997 if (!AStmt)
5998 return StmtError();
5999
6000 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev74a05c92014-07-15 02:55:09 +00006001
Reid Kleckner87a31802018-03-12 21:43:02 +00006002 setFunctionHasBranchProtectedScope();
Alexey Bataev74a05c92014-07-15 02:55:09 +00006003
Alexey Bataev3255bf32015-01-19 05:20:46 +00006004 // OpenMP [2.7.3, single Construct, Restrictions]
6005 // The copyprivate clause must not be used with the nowait clause.
Alexey Bataeve3727102018-04-18 15:57:46 +00006006 const OMPClause *Nowait = nullptr;
6007 const OMPClause *Copyprivate = nullptr;
6008 for (const OMPClause *Clause : Clauses) {
Alexey Bataev3255bf32015-01-19 05:20:46 +00006009 if (Clause->getClauseKind() == OMPC_nowait)
6010 Nowait = Clause;
6011 else if (Clause->getClauseKind() == OMPC_copyprivate)
6012 Copyprivate = Clause;
6013 if (Copyprivate && Nowait) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006014 Diag(Copyprivate->getBeginLoc(),
Alexey Bataev3255bf32015-01-19 05:20:46 +00006015 diag::err_omp_single_copyprivate_with_nowait);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006016 Diag(Nowait->getBeginLoc(), diag::note_omp_nowait_clause_here);
Alexey Bataev3255bf32015-01-19 05:20:46 +00006017 return StmtError();
6018 }
6019 }
6020
Alexey Bataevd1e40fb2014-06-26 12:05:45 +00006021 return OMPSingleDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
6022}
6023
Alexander Musman80c22892014-07-17 08:54:58 +00006024StmtResult Sema::ActOnOpenMPMasterDirective(Stmt *AStmt,
6025 SourceLocation StartLoc,
6026 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006027 if (!AStmt)
6028 return StmtError();
6029
6030 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musman80c22892014-07-17 08:54:58 +00006031
Reid Kleckner87a31802018-03-12 21:43:02 +00006032 setFunctionHasBranchProtectedScope();
Alexander Musman80c22892014-07-17 08:54:58 +00006033
6034 return OMPMasterDirective::Create(Context, StartLoc, EndLoc, AStmt);
6035}
6036
Alexey Bataev28c75412015-12-15 08:19:24 +00006037StmtResult Sema::ActOnOpenMPCriticalDirective(
6038 const DeclarationNameInfo &DirName, ArrayRef<OMPClause *> Clauses,
6039 Stmt *AStmt, SourceLocation StartLoc, SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006040 if (!AStmt)
6041 return StmtError();
6042
6043 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006044
Alexey Bataev28c75412015-12-15 08:19:24 +00006045 bool ErrorFound = false;
6046 llvm::APSInt Hint;
6047 SourceLocation HintLoc;
6048 bool DependentHint = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006049 for (const OMPClause *C : Clauses) {
Alexey Bataev28c75412015-12-15 08:19:24 +00006050 if (C->getClauseKind() == OMPC_hint) {
6051 if (!DirName.getName()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006052 Diag(C->getBeginLoc(), diag::err_omp_hint_clause_no_name);
Alexey Bataev28c75412015-12-15 08:19:24 +00006053 ErrorFound = true;
6054 }
6055 Expr *E = cast<OMPHintClause>(C)->getHint();
6056 if (E->isTypeDependent() || E->isValueDependent() ||
Alexey Bataeve3727102018-04-18 15:57:46 +00006057 E->isInstantiationDependent()) {
Alexey Bataev28c75412015-12-15 08:19:24 +00006058 DependentHint = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006059 } else {
Alexey Bataev28c75412015-12-15 08:19:24 +00006060 Hint = E->EvaluateKnownConstInt(Context);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006061 HintLoc = C->getBeginLoc();
Alexey Bataev28c75412015-12-15 08:19:24 +00006062 }
6063 }
6064 }
6065 if (ErrorFound)
6066 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006067 const auto Pair = DSAStack->getCriticalWithHint(DirName);
Alexey Bataev28c75412015-12-15 08:19:24 +00006068 if (Pair.first && DirName.getName() && !DependentHint) {
6069 if (llvm::APSInt::compareValues(Hint, Pair.second) != 0) {
6070 Diag(StartLoc, diag::err_omp_critical_with_hint);
Alexey Bataeve3727102018-04-18 15:57:46 +00006071 if (HintLoc.isValid())
Alexey Bataev28c75412015-12-15 08:19:24 +00006072 Diag(HintLoc, diag::note_omp_critical_hint_here)
6073 << 0 << Hint.toString(/*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006074 else
Alexey Bataev28c75412015-12-15 08:19:24 +00006075 Diag(StartLoc, diag::note_omp_critical_no_hint) << 0;
Alexey Bataeve3727102018-04-18 15:57:46 +00006076 if (const auto *C = Pair.first->getSingleClause<OMPHintClause>()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006077 Diag(C->getBeginLoc(), diag::note_omp_critical_hint_here)
Alexey Bataev28c75412015-12-15 08:19:24 +00006078 << 1
6079 << C->getHint()->EvaluateKnownConstInt(Context).toString(
6080 /*Radix=*/10, /*Signed=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +00006081 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006082 Diag(Pair.first->getBeginLoc(), diag::note_omp_critical_no_hint) << 1;
Alexey Bataeve3727102018-04-18 15:57:46 +00006083 }
Alexey Bataev28c75412015-12-15 08:19:24 +00006084 }
6085 }
6086
Reid Kleckner87a31802018-03-12 21:43:02 +00006087 setFunctionHasBranchProtectedScope();
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006088
Alexey Bataev28c75412015-12-15 08:19:24 +00006089 auto *Dir = OMPCriticalDirective::Create(Context, DirName, StartLoc, EndLoc,
6090 Clauses, AStmt);
6091 if (!Pair.first && DirName.getName() && !DependentHint)
6092 DSAStack->addCriticalWithHint(Dir, Hint);
6093 return Dir;
Alexander Musmand9ed09f2014-07-21 09:42:05 +00006094}
6095
Alexey Bataev4acb8592014-07-07 13:01:15 +00006096StmtResult Sema::ActOnOpenMPParallelForDirective(
6097 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006098 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006099 if (!AStmt)
6100 return StmtError();
6101
Alexey Bataeve3727102018-04-18 15:57:46 +00006102 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006103 // 1.2.2 OpenMP Language Terminology
6104 // Structured block - An executable statement with a single entry at the
6105 // top and a single exit at the bottom.
6106 // The point of exit cannot be a branch out of the structured block.
6107 // longjmp() and throw() must not violate the entry/exit criteria.
6108 CS->getCapturedDecl()->setNothrow();
6109
Alexander Musmanc6388682014-12-15 07:07:06 +00006110 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006111 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6112 // define the nested loops number.
Alexey Bataev4acb8592014-07-07 13:01:15 +00006113 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006114 checkOpenMPLoop(OMPD_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006115 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6116 VarsWithImplicitDSA, B);
Alexey Bataev4acb8592014-07-07 13:01:15 +00006117 if (NestedLoopCount == 0)
6118 return StmtError();
6119
Alexander Musmana5f070a2014-10-01 06:03:56 +00006120 assert((CurContext->isDependentContext() || B.builtAll()) &&
6121 "omp parallel for loop exprs were not built");
6122
Alexey Bataev54acd402015-08-04 11:18:19 +00006123 if (!CurContext->isDependentContext()) {
6124 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006125 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006126 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev54acd402015-08-04 11:18:19 +00006127 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006128 B.NumIterations, *this, CurScope,
6129 DSAStack))
Alexey Bataev54acd402015-08-04 11:18:19 +00006130 return StmtError();
6131 }
6132 }
6133
Reid Kleckner87a31802018-03-12 21:43:02 +00006134 setFunctionHasBranchProtectedScope();
Alexander Musmanc6388682014-12-15 07:07:06 +00006135 return OMPParallelForDirective::Create(Context, StartLoc, EndLoc,
Alexey Bataev25e5b442015-09-15 12:52:43 +00006136 NestedLoopCount, Clauses, AStmt, B,
6137 DSAStack->isCancelRegion());
Alexey Bataev4acb8592014-07-07 13:01:15 +00006138}
6139
Alexander Musmane4e893b2014-09-23 09:33:00 +00006140StmtResult Sema::ActOnOpenMPParallelForSimdDirective(
6141 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00006142 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006143 if (!AStmt)
6144 return StmtError();
6145
Alexey Bataeve3727102018-04-18 15:57:46 +00006146 auto *CS = cast<CapturedStmt>(AStmt);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006147 // 1.2.2 OpenMP Language Terminology
6148 // Structured block - An executable statement with a single entry at the
6149 // top and a single exit at the bottom.
6150 // The point of exit cannot be a branch out of the structured block.
6151 // longjmp() and throw() must not violate the entry/exit criteria.
6152 CS->getCapturedDecl()->setNothrow();
6153
Alexander Musmanc6388682014-12-15 07:07:06 +00006154 OMPLoopDirective::HelperExprs B;
Alexey Bataev10e775f2015-07-30 11:36:16 +00006155 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
6156 // define the nested loops number.
Alexander Musmane4e893b2014-09-23 09:33:00 +00006157 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00006158 checkOpenMPLoop(OMPD_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev10e775f2015-07-30 11:36:16 +00006159 getOrderedNumberExpr(Clauses), AStmt, *this, *DSAStack,
6160 VarsWithImplicitDSA, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006161 if (NestedLoopCount == 0)
6162 return StmtError();
6163
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006164 if (!CurContext->isDependentContext()) {
6165 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00006166 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00006167 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006168 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00006169 B.NumIterations, *this, CurScope,
6170 DSAStack))
Alexey Bataev3b5b5c42015-06-18 10:10:12 +00006171 return StmtError();
6172 }
6173 }
6174
Kelvin Lic5609492016-07-15 04:39:07 +00006175 if (checkSimdlenSafelenSpecified(*this, Clauses))
Alexey Bataev66b15b52015-08-21 11:14:16 +00006176 return StmtError();
6177
Reid Kleckner87a31802018-03-12 21:43:02 +00006178 setFunctionHasBranchProtectedScope();
Alexander Musmana5f070a2014-10-01 06:03:56 +00006179 return OMPParallelForSimdDirective::Create(
Alexander Musmanc6388682014-12-15 07:07:06 +00006180 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Alexander Musmane4e893b2014-09-23 09:33:00 +00006181}
6182
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006183StmtResult
6184Sema::ActOnOpenMPParallelSectionsDirective(ArrayRef<OMPClause *> Clauses,
6185 Stmt *AStmt, SourceLocation StartLoc,
6186 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006187 if (!AStmt)
6188 return StmtError();
6189
6190 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006191 auto BaseStmt = AStmt;
David Majnemer9d168222016-08-05 17:44:54 +00006192 while (auto *CS = dyn_cast_or_null<CapturedStmt>(BaseStmt))
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006193 BaseStmt = CS->getCapturedStmt();
David Majnemer9d168222016-08-05 17:44:54 +00006194 if (auto *C = dyn_cast_or_null<CompoundStmt>(BaseStmt)) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006195 auto S = C->children();
Benjamin Kramer5733e352015-07-18 17:09:36 +00006196 if (S.begin() == S.end())
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006197 return StmtError();
6198 // All associated statements must be '#pragma omp section' except for
6199 // the first one.
Benjamin Kramer5733e352015-07-18 17:09:36 +00006200 for (Stmt *SectionStmt : llvm::make_range(std::next(S.begin()), S.end())) {
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006201 if (!SectionStmt || !isa<OMPSectionDirective>(SectionStmt)) {
6202 if (SectionStmt)
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006203 Diag(SectionStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006204 diag::err_omp_parallel_sections_substmt_not_section);
6205 return StmtError();
6206 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00006207 cast<OMPSectionDirective>(SectionStmt)
6208 ->setHasCancel(DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006209 }
6210 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006211 Diag(AStmt->getBeginLoc(),
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006212 diag::err_omp_parallel_sections_not_compound_stmt);
6213 return StmtError();
6214 }
6215
Reid Kleckner87a31802018-03-12 21:43:02 +00006216 setFunctionHasBranchProtectedScope();
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006217
Alexey Bataev25e5b442015-09-15 12:52:43 +00006218 return OMPParallelSectionsDirective::Create(
6219 Context, StartLoc, EndLoc, Clauses, AStmt, DSAStack->isCancelRegion());
Alexey Bataev84d0b3e2014-07-08 08:12:03 +00006220}
6221
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006222StmtResult Sema::ActOnOpenMPTaskDirective(ArrayRef<OMPClause *> Clauses,
6223 Stmt *AStmt, SourceLocation StartLoc,
6224 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006225 if (!AStmt)
6226 return StmtError();
6227
David Majnemer9d168222016-08-05 17:44:54 +00006228 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006229 // 1.2.2 OpenMP Language Terminology
6230 // Structured block - An executable statement with a single entry at the
6231 // top and a single exit at the bottom.
6232 // The point of exit cannot be a branch out of the structured block.
6233 // longjmp() and throw() must not violate the entry/exit criteria.
6234 CS->getCapturedDecl()->setNothrow();
6235
Reid Kleckner87a31802018-03-12 21:43:02 +00006236 setFunctionHasBranchProtectedScope();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006237
Alexey Bataev25e5b442015-09-15 12:52:43 +00006238 return OMPTaskDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
6239 DSAStack->isCancelRegion());
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +00006240}
6241
Alexey Bataev68446b72014-07-18 07:47:19 +00006242StmtResult Sema::ActOnOpenMPTaskyieldDirective(SourceLocation StartLoc,
6243 SourceLocation EndLoc) {
6244 return OMPTaskyieldDirective::Create(Context, StartLoc, EndLoc);
6245}
6246
Alexey Bataev4d1dfea2014-07-18 09:11:51 +00006247StmtResult Sema::ActOnOpenMPBarrierDirective(SourceLocation StartLoc,
6248 SourceLocation EndLoc) {
6249 return OMPBarrierDirective::Create(Context, StartLoc, EndLoc);
6250}
6251
Alexey Bataev2df347a2014-07-18 10:17:07 +00006252StmtResult Sema::ActOnOpenMPTaskwaitDirective(SourceLocation StartLoc,
6253 SourceLocation EndLoc) {
6254 return OMPTaskwaitDirective::Create(Context, StartLoc, EndLoc);
6255}
6256
Alexey Bataev169d96a2017-07-18 20:17:46 +00006257StmtResult Sema::ActOnOpenMPTaskgroupDirective(ArrayRef<OMPClause *> Clauses,
6258 Stmt *AStmt,
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006259 SourceLocation StartLoc,
6260 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006261 if (!AStmt)
6262 return StmtError();
6263
6264 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006265
Reid Kleckner87a31802018-03-12 21:43:02 +00006266 setFunctionHasBranchProtectedScope();
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006267
Alexey Bataev169d96a2017-07-18 20:17:46 +00006268 return OMPTaskgroupDirective::Create(Context, StartLoc, EndLoc, Clauses,
Alexey Bataev3b1b8952017-07-25 15:53:26 +00006269 AStmt,
6270 DSAStack->getTaskgroupReductionRef());
Alexey Bataevc30dd2d2015-06-18 12:14:09 +00006271}
6272
Alexey Bataev6125da92014-07-21 11:26:11 +00006273StmtResult Sema::ActOnOpenMPFlushDirective(ArrayRef<OMPClause *> Clauses,
6274 SourceLocation StartLoc,
6275 SourceLocation EndLoc) {
6276 assert(Clauses.size() <= 1 && "Extra clauses in flush directive");
6277 return OMPFlushDirective::Create(Context, StartLoc, EndLoc, Clauses);
6278}
6279
Alexey Bataev346265e2015-09-25 10:37:12 +00006280StmtResult Sema::ActOnOpenMPOrderedDirective(ArrayRef<OMPClause *> Clauses,
6281 Stmt *AStmt,
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006282 SourceLocation StartLoc,
6283 SourceLocation EndLoc) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006284 const OMPClause *DependFound = nullptr;
6285 const OMPClause *DependSourceClause = nullptr;
6286 const OMPClause *DependSinkClause = nullptr;
Alexey Bataeveb482352015-12-18 05:05:56 +00006287 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00006288 const OMPThreadsClause *TC = nullptr;
6289 const OMPSIMDClause *SC = nullptr;
6290 for (const OMPClause *C : Clauses) {
Alexey Bataeveb482352015-12-18 05:05:56 +00006291 if (auto *DC = dyn_cast<OMPDependClause>(C)) {
6292 DependFound = C;
6293 if (DC->getDependencyKind() == OMPC_DEPEND_source) {
6294 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006295 Diag(C->getBeginLoc(), diag::err_omp_more_one_clause)
Alexey Bataeveb482352015-12-18 05:05:56 +00006296 << getOpenMPDirectiveName(OMPD_ordered)
6297 << getOpenMPClauseName(OMPC_depend) << 2;
6298 ErrorFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006299 } else {
Alexey Bataeveb482352015-12-18 05:05:56 +00006300 DependSourceClause = C;
Alexey Bataeve3727102018-04-18 15:57:46 +00006301 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006302 if (DependSinkClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006303 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006304 << 0;
6305 ErrorFound = true;
6306 }
6307 } else if (DC->getDependencyKind() == OMPC_DEPEND_sink) {
6308 if (DependSourceClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006309 Diag(C->getBeginLoc(), diag::err_omp_depend_sink_source_not_allowed)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00006310 << 1;
6311 ErrorFound = true;
6312 }
6313 DependSinkClause = C;
Alexey Bataeveb482352015-12-18 05:05:56 +00006314 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006315 } else if (C->getClauseKind() == OMPC_threads) {
Alexey Bataev346265e2015-09-25 10:37:12 +00006316 TC = cast<OMPThreadsClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006317 } else if (C->getClauseKind() == OMPC_simd) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006318 SC = cast<OMPSIMDClause>(C);
Alexey Bataeve3727102018-04-18 15:57:46 +00006319 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006320 }
Alexey Bataeveb482352015-12-18 05:05:56 +00006321 if (!ErrorFound && !SC &&
6322 isOpenMPSimdDirective(DSAStack->getParentDirective())) {
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006323 // OpenMP [2.8.1,simd Construct, Restrictions]
6324 // An ordered construct with the simd clause is the only OpenMP construct
6325 // that can appear in the simd region.
6326 Diag(StartLoc, diag::err_omp_prohibited_region_simd);
Alexey Bataeveb482352015-12-18 05:05:56 +00006327 ErrorFound = true;
6328 } else if (DependFound && (TC || SC)) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006329 Diag(DependFound->getBeginLoc(), diag::err_omp_depend_clause_thread_simd)
Alexey Bataeveb482352015-12-18 05:05:56 +00006330 << getOpenMPClauseName(TC ? TC->getClauseKind() : SC->getClauseKind());
6331 ErrorFound = true;
Alexey Bataevf138fda2018-08-13 19:04:24 +00006332 } else if (DependFound && !DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006333 Diag(DependFound->getBeginLoc(),
Alexey Bataeveb482352015-12-18 05:05:56 +00006334 diag::err_omp_ordered_directive_without_param);
6335 ErrorFound = true;
6336 } else if (TC || Clauses.empty()) {
Alexey Bataevf138fda2018-08-13 19:04:24 +00006337 if (const Expr *Param = DSAStack->getParentOrderedRegionParam().first) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006338 SourceLocation ErrLoc = TC ? TC->getBeginLoc() : StartLoc;
Alexey Bataeveb482352015-12-18 05:05:56 +00006339 Diag(ErrLoc, diag::err_omp_ordered_directive_with_param)
6340 << (TC != nullptr);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006341 Diag(Param->getBeginLoc(), diag::note_omp_ordered_param);
Alexey Bataeveb482352015-12-18 05:05:56 +00006342 ErrorFound = true;
6343 }
6344 }
6345 if ((!AStmt && !DependFound) || ErrorFound)
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006346 return StmtError();
Alexey Bataeveb482352015-12-18 05:05:56 +00006347
6348 if (AStmt) {
6349 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
6350
Reid Kleckner87a31802018-03-12 21:43:02 +00006351 setFunctionHasBranchProtectedScope();
Alexey Bataevd14d1e62015-09-28 06:39:35 +00006352 }
Alexey Bataev346265e2015-09-25 10:37:12 +00006353
6354 return OMPOrderedDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
Alexey Bataev9fb6e642014-07-22 06:45:04 +00006355}
6356
Alexey Bataev1d160b12015-03-13 12:27:31 +00006357namespace {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006358/// Helper class for checking expression in 'omp atomic [update]'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006359/// construct.
6360class OpenMPAtomicUpdateChecker {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006361 /// Error results for atomic update expressions.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006362 enum ExprAnalysisErrorCode {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006363 /// A statement is not an expression statement.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006364 NotAnExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006365 /// Expression is not builtin binary or unary operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006366 NotABinaryOrUnaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006367 /// Unary operation is not post-/pre- increment/decrement operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006368 NotAnUnaryIncDecExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006369 /// An expression is not of scalar type.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006370 NotAScalarType,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006371 /// A binary operation is not an assignment operation.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006372 NotAnAssignmentOp,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006373 /// RHS part of the binary operation is not a binary expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006374 NotABinaryExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006375 /// RHS part is not additive/multiplicative/shift/biwise binary
Alexey Bataev1d160b12015-03-13 12:27:31 +00006376 /// expression.
6377 NotABinaryOperator,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006378 /// RHS binary operation does not have reference to the updated LHS
Alexey Bataev1d160b12015-03-13 12:27:31 +00006379 /// part.
6380 NotAnUpdateExpression,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006381 /// No errors is found.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006382 NoError
6383 };
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006384 /// Reference to Sema.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006385 Sema &SemaRef;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006386 /// A location for note diagnostics (when error is found).
Alexey Bataev1d160b12015-03-13 12:27:31 +00006387 SourceLocation NoteLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006388 /// 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006389 Expr *X;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006390 /// 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006391 Expr *E;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006392 /// Helper expression of the form
Alexey Bataevb4505a72015-03-30 05:20:59 +00006393 /// 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6394 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6395 Expr *UpdateExpr;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006396 /// Is 'x' a LHS in a RHS part of full update expression. It is
Alexey Bataevb4505a72015-03-30 05:20:59 +00006397 /// important for non-associative operations.
6398 bool IsXLHSInRHSPart;
6399 BinaryOperatorKind Op;
6400 SourceLocation OpLoc;
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006401 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006402 /// if it is a prefix unary operation.
6403 bool IsPostfixUpdate;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006404
6405public:
6406 OpenMPAtomicUpdateChecker(Sema &SemaRef)
Alexey Bataevb4505a72015-03-30 05:20:59 +00006407 : SemaRef(SemaRef), X(nullptr), E(nullptr), UpdateExpr(nullptr),
Alexey Bataevb78ca832015-04-01 03:33:17 +00006408 IsXLHSInRHSPart(false), Op(BO_PtrMemD), IsPostfixUpdate(false) {}
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006409 /// Check specified statement that it is suitable for 'atomic update'
Alexey Bataev1d160b12015-03-13 12:27:31 +00006410 /// constructs and extract 'x', 'expr' and Operation from the original
Alexey Bataevb78ca832015-04-01 03:33:17 +00006411 /// expression. If DiagId and NoteId == 0, then only check is performed
6412 /// without error notification.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006413 /// \param DiagId Diagnostic which should be emitted if error is found.
6414 /// \param NoteId Diagnostic note for the main error message.
6415 /// \return true if statement is not an update expression, false otherwise.
Alexey Bataevb78ca832015-04-01 03:33:17 +00006416 bool checkStatement(Stmt *S, unsigned DiagId = 0, unsigned NoteId = 0);
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006417 /// Return the 'x' lvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006418 Expr *getX() const { return X; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006419 /// Return the 'expr' rvalue part of the source atomic expression.
Alexey Bataev1d160b12015-03-13 12:27:31 +00006420 Expr *getExpr() const { return E; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006421 /// Return the update expression used in calculation of the updated
Alexey Bataevb4505a72015-03-30 05:20:59 +00006422 /// value. Always has form 'OpaqueValueExpr(x) binop OpaqueValueExpr(expr)' or
6423 /// 'OpaqueValueExpr(expr) binop OpaqueValueExpr(x)'.
6424 Expr *getUpdateExpr() const { return UpdateExpr; }
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006425 /// Return true if 'x' is LHS in RHS part of full update expression,
Alexey Bataevb4505a72015-03-30 05:20:59 +00006426 /// false otherwise.
6427 bool isXLHSInRHSPart() const { return IsXLHSInRHSPart; }
6428
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00006429 /// true if the source expression is a postfix unary operation, false
Alexey Bataevb78ca832015-04-01 03:33:17 +00006430 /// if it is a prefix unary operation.
6431 bool isPostfixUpdate() const { return IsPostfixUpdate; }
6432
Alexey Bataev1d160b12015-03-13 12:27:31 +00006433private:
Alexey Bataevb78ca832015-04-01 03:33:17 +00006434 bool checkBinaryOperation(BinaryOperator *AtomicBinOp, unsigned DiagId = 0,
6435 unsigned NoteId = 0);
Alexey Bataev1d160b12015-03-13 12:27:31 +00006436};
6437} // namespace
6438
6439bool OpenMPAtomicUpdateChecker::checkBinaryOperation(
6440 BinaryOperator *AtomicBinOp, unsigned DiagId, unsigned NoteId) {
6441 ExprAnalysisErrorCode ErrorFound = NoError;
6442 SourceLocation ErrorLoc, NoteLoc;
6443 SourceRange ErrorRange, NoteRange;
6444 // Allowed constructs are:
6445 // x = x binop expr;
6446 // x = expr binop x;
6447 if (AtomicBinOp->getOpcode() == BO_Assign) {
6448 X = AtomicBinOp->getLHS();
Alexey Bataeve3727102018-04-18 15:57:46 +00006449 if (const auto *AtomicInnerBinOp = dyn_cast<BinaryOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006450 AtomicBinOp->getRHS()->IgnoreParenImpCasts())) {
6451 if (AtomicInnerBinOp->isMultiplicativeOp() ||
6452 AtomicInnerBinOp->isAdditiveOp() || AtomicInnerBinOp->isShiftOp() ||
6453 AtomicInnerBinOp->isBitwiseOp()) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006454 Op = AtomicInnerBinOp->getOpcode();
6455 OpLoc = AtomicInnerBinOp->getOperatorLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +00006456 Expr *LHS = AtomicInnerBinOp->getLHS();
6457 Expr *RHS = AtomicInnerBinOp->getRHS();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006458 llvm::FoldingSetNodeID XId, LHSId, RHSId;
6459 X->IgnoreParenImpCasts()->Profile(XId, SemaRef.getASTContext(),
6460 /*Canonical=*/true);
6461 LHS->IgnoreParenImpCasts()->Profile(LHSId, SemaRef.getASTContext(),
6462 /*Canonical=*/true);
6463 RHS->IgnoreParenImpCasts()->Profile(RHSId, SemaRef.getASTContext(),
6464 /*Canonical=*/true);
6465 if (XId == LHSId) {
6466 E = RHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006467 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006468 } else if (XId == RHSId) {
6469 E = LHS;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006470 IsXLHSInRHSPart = false;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006471 } else {
6472 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6473 ErrorRange = AtomicInnerBinOp->getSourceRange();
6474 NoteLoc = X->getExprLoc();
6475 NoteRange = X->getSourceRange();
6476 ErrorFound = NotAnUpdateExpression;
6477 }
6478 } else {
6479 ErrorLoc = AtomicInnerBinOp->getExprLoc();
6480 ErrorRange = AtomicInnerBinOp->getSourceRange();
6481 NoteLoc = AtomicInnerBinOp->getOperatorLoc();
6482 NoteRange = SourceRange(NoteLoc, NoteLoc);
6483 ErrorFound = NotABinaryOperator;
6484 }
6485 } else {
6486 NoteLoc = ErrorLoc = AtomicBinOp->getRHS()->getExprLoc();
6487 NoteRange = ErrorRange = AtomicBinOp->getRHS()->getSourceRange();
6488 ErrorFound = NotABinaryExpression;
6489 }
6490 } else {
6491 ErrorLoc = AtomicBinOp->getExprLoc();
6492 ErrorRange = AtomicBinOp->getSourceRange();
6493 NoteLoc = AtomicBinOp->getOperatorLoc();
6494 NoteRange = SourceRange(NoteLoc, NoteLoc);
6495 ErrorFound = NotAnAssignmentOp;
6496 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006497 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006498 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6499 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6500 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006501 }
6502 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006503 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006504 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006505}
6506
6507bool OpenMPAtomicUpdateChecker::checkStatement(Stmt *S, unsigned DiagId,
6508 unsigned NoteId) {
6509 ExprAnalysisErrorCode ErrorFound = NoError;
6510 SourceLocation ErrorLoc, NoteLoc;
6511 SourceRange ErrorRange, NoteRange;
6512 // Allowed constructs are:
6513 // x++;
6514 // x--;
6515 // ++x;
6516 // --x;
6517 // x binop= expr;
6518 // x = x binop expr;
6519 // x = expr binop x;
6520 if (auto *AtomicBody = dyn_cast<Expr>(S)) {
6521 AtomicBody = AtomicBody->IgnoreParenImpCasts();
6522 if (AtomicBody->getType()->isScalarType() ||
6523 AtomicBody->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006524 if (const auto *AtomicCompAssignOp = dyn_cast<CompoundAssignOperator>(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006525 AtomicBody->IgnoreParenImpCasts())) {
6526 // Check for Compound Assignment Operation
Alexey Bataevb4505a72015-03-30 05:20:59 +00006527 Op = BinaryOperator::getOpForCompoundAssignment(
Alexey Bataev1d160b12015-03-13 12:27:31 +00006528 AtomicCompAssignOp->getOpcode());
Alexey Bataevb4505a72015-03-30 05:20:59 +00006529 OpLoc = AtomicCompAssignOp->getOperatorLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006530 E = AtomicCompAssignOp->getRHS();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006531 X = AtomicCompAssignOp->getLHS()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006532 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006533 } else if (auto *AtomicBinOp = dyn_cast<BinaryOperator>(
6534 AtomicBody->IgnoreParenImpCasts())) {
6535 // Check for Binary Operation
David Majnemer9d168222016-08-05 17:44:54 +00006536 if (checkBinaryOperation(AtomicBinOp, DiagId, NoteId))
Alexey Bataevb4505a72015-03-30 05:20:59 +00006537 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006538 } else if (const auto *AtomicUnaryOp = dyn_cast<UnaryOperator>(
David Majnemer9d168222016-08-05 17:44:54 +00006539 AtomicBody->IgnoreParenImpCasts())) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006540 // Check for Unary Operation
6541 if (AtomicUnaryOp->isIncrementDecrementOp()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006542 IsPostfixUpdate = AtomicUnaryOp->isPostfix();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006543 Op = AtomicUnaryOp->isIncrementOp() ? BO_Add : BO_Sub;
6544 OpLoc = AtomicUnaryOp->getOperatorLoc();
Kelvin Li4f161cf2016-07-20 19:41:17 +00006545 X = AtomicUnaryOp->getSubExpr()->IgnoreParens();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006546 E = SemaRef.ActOnIntegerConstant(OpLoc, /*uint64_t Val=*/1).get();
6547 IsXLHSInRHSPart = true;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006548 } else {
6549 ErrorFound = NotAnUnaryIncDecExpression;
6550 ErrorLoc = AtomicUnaryOp->getExprLoc();
6551 ErrorRange = AtomicUnaryOp->getSourceRange();
6552 NoteLoc = AtomicUnaryOp->getOperatorLoc();
6553 NoteRange = SourceRange(NoteLoc, NoteLoc);
6554 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006555 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006556 ErrorFound = NotABinaryOrUnaryExpression;
6557 NoteLoc = ErrorLoc = AtomicBody->getExprLoc();
6558 NoteRange = ErrorRange = AtomicBody->getSourceRange();
6559 }
6560 } else {
6561 ErrorFound = NotAScalarType;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006562 NoteLoc = ErrorLoc = AtomicBody->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006563 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6564 }
6565 } else {
6566 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006567 NoteLoc = ErrorLoc = S->getBeginLoc();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006568 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
6569 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006570 if (ErrorFound != NoError && DiagId != 0 && NoteId != 0) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006571 SemaRef.Diag(ErrorLoc, DiagId) << ErrorRange;
6572 SemaRef.Diag(NoteLoc, NoteId) << ErrorFound << NoteRange;
6573 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +00006574 }
6575 if (SemaRef.CurContext->isDependentContext())
Alexey Bataevb4505a72015-03-30 05:20:59 +00006576 E = X = UpdateExpr = nullptr;
Alexey Bataev5e018f92015-04-23 06:35:10 +00006577 if (ErrorFound == NoError && E && X) {
Alexey Bataevb4505a72015-03-30 05:20:59 +00006578 // Build an update expression of form 'OpaqueValueExpr(x) binop
6579 // OpaqueValueExpr(expr)' or 'OpaqueValueExpr(expr) binop
6580 // OpaqueValueExpr(x)' and then cast it to the type of the 'x' expression.
6581 auto *OVEX = new (SemaRef.getASTContext())
6582 OpaqueValueExpr(X->getExprLoc(), X->getType(), VK_RValue);
6583 auto *OVEExpr = new (SemaRef.getASTContext())
6584 OpaqueValueExpr(E->getExprLoc(), E->getType(), VK_RValue);
Alexey Bataeve3727102018-04-18 15:57:46 +00006585 ExprResult Update =
Alexey Bataevb4505a72015-03-30 05:20:59 +00006586 SemaRef.CreateBuiltinBinOp(OpLoc, Op, IsXLHSInRHSPart ? OVEX : OVEExpr,
6587 IsXLHSInRHSPart ? OVEExpr : OVEX);
6588 if (Update.isInvalid())
6589 return true;
6590 Update = SemaRef.PerformImplicitConversion(Update.get(), X->getType(),
6591 Sema::AA_Casting);
6592 if (Update.isInvalid())
6593 return true;
6594 UpdateExpr = Update.get();
6595 }
Alexey Bataev5e018f92015-04-23 06:35:10 +00006596 return ErrorFound != NoError;
Alexey Bataev1d160b12015-03-13 12:27:31 +00006597}
6598
Alexey Bataev0162e452014-07-22 10:10:35 +00006599StmtResult Sema::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
6600 Stmt *AStmt,
6601 SourceLocation StartLoc,
6602 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00006603 if (!AStmt)
6604 return StmtError();
6605
David Majnemer9d168222016-08-05 17:44:54 +00006606 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev0162e452014-07-22 10:10:35 +00006607 // 1.2.2 OpenMP Language Terminology
6608 // Structured block - An executable statement with a single entry at the
6609 // top and a single exit at the bottom.
6610 // The point of exit cannot be a branch out of the structured block.
6611 // longjmp() and throw() must not violate the entry/exit criteria.
Alexey Bataevdea47612014-07-23 07:46:59 +00006612 OpenMPClauseKind AtomicKind = OMPC_unknown;
6613 SourceLocation AtomicKindLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +00006614 for (const OMPClause *C : Clauses) {
Alexey Bataev67a4f222014-07-23 10:25:33 +00006615 if (C->getClauseKind() == OMPC_read || C->getClauseKind() == OMPC_write ||
Alexey Bataev459dec02014-07-24 06:46:57 +00006616 C->getClauseKind() == OMPC_update ||
6617 C->getClauseKind() == OMPC_capture) {
Alexey Bataevdea47612014-07-23 07:46:59 +00006618 if (AtomicKind != OMPC_unknown) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006619 Diag(C->getBeginLoc(), diag::err_omp_atomic_several_clauses)
Stephen Kelly1c301dc2018-08-09 21:09:38 +00006620 << SourceRange(C->getBeginLoc(), C->getEndLoc());
Alexey Bataevdea47612014-07-23 07:46:59 +00006621 Diag(AtomicKindLoc, diag::note_omp_atomic_previous_clause)
6622 << getOpenMPClauseName(AtomicKind);
6623 } else {
6624 AtomicKind = C->getClauseKind();
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006625 AtomicKindLoc = C->getBeginLoc();
Alexey Bataevf98b00c2014-07-23 02:27:21 +00006626 }
6627 }
6628 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006629
Alexey Bataeve3727102018-04-18 15:57:46 +00006630 Stmt *Body = CS->getCapturedStmt();
Alexey Bataev10fec572015-03-11 04:48:56 +00006631 if (auto *EWC = dyn_cast<ExprWithCleanups>(Body))
6632 Body = EWC->getSubExpr();
6633
Alexey Bataev62cec442014-11-18 10:14:22 +00006634 Expr *X = nullptr;
6635 Expr *V = nullptr;
6636 Expr *E = nullptr;
Alexey Bataevb4505a72015-03-30 05:20:59 +00006637 Expr *UE = nullptr;
6638 bool IsXLHSInRHSPart = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006639 bool IsPostfixUpdate = false;
Alexey Bataev62cec442014-11-18 10:14:22 +00006640 // OpenMP [2.12.6, atomic Construct]
6641 // In the next expressions:
6642 // * x and v (as applicable) are both l-value expressions with scalar type.
6643 // * During the execution of an atomic region, multiple syntactic
6644 // occurrences of x must designate the same storage location.
6645 // * Neither of v and expr (as applicable) may access the storage location
6646 // designated by x.
6647 // * Neither of x and expr (as applicable) may access the storage location
6648 // designated by v.
6649 // * expr is an expression with scalar type.
6650 // * binop is one of +, *, -, /, &, ^, |, <<, or >>.
6651 // * binop, binop=, ++, and -- are not overloaded operators.
6652 // * The expression x binop expr must be numerically equivalent to x binop
6653 // (expr). This requirement is satisfied if the operators in expr have
6654 // precedence greater than binop, or by using parentheses around expr or
6655 // subexpressions of expr.
6656 // * The expression expr binop x must be numerically equivalent to (expr)
6657 // binop x. This requirement is satisfied if the operators in expr have
6658 // precedence equal to or greater than binop, or by using parentheses around
6659 // expr or subexpressions of expr.
6660 // * For forms that allow multiple occurrences of x, the number of times
6661 // that x is evaluated is unspecified.
Alexey Bataevdea47612014-07-23 07:46:59 +00006662 if (AtomicKind == OMPC_read) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006663 enum {
6664 NotAnExpression,
6665 NotAnAssignmentOp,
6666 NotAScalarType,
6667 NotAnLValue,
6668 NoError
6669 } ErrorFound = NoError;
Alexey Bataev62cec442014-11-18 10:14:22 +00006670 SourceLocation ErrorLoc, NoteLoc;
6671 SourceRange ErrorRange, NoteRange;
6672 // If clause is read:
6673 // v = x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006674 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6675 const auto *AtomicBinOp =
Alexey Bataev62cec442014-11-18 10:14:22 +00006676 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6677 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6678 X = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6679 V = AtomicBinOp->getLHS()->IgnoreParenImpCasts();
6680 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6681 (V->isInstantiationDependent() || V->getType()->isScalarType())) {
6682 if (!X->isLValue() || !V->isLValue()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006683 const Expr *NotLValueExpr = X->isLValue() ? V : X;
Alexey Bataev62cec442014-11-18 10:14:22 +00006684 ErrorFound = NotAnLValue;
6685 ErrorLoc = AtomicBinOp->getExprLoc();
6686 ErrorRange = AtomicBinOp->getSourceRange();
6687 NoteLoc = NotLValueExpr->getExprLoc();
6688 NoteRange = NotLValueExpr->getSourceRange();
6689 }
6690 } else if (!X->isInstantiationDependent() ||
6691 !V->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006692 const Expr *NotScalarExpr =
Alexey Bataev62cec442014-11-18 10:14:22 +00006693 (X->isInstantiationDependent() || X->getType()->isScalarType())
6694 ? V
6695 : X;
6696 ErrorFound = NotAScalarType;
6697 ErrorLoc = AtomicBinOp->getExprLoc();
6698 ErrorRange = AtomicBinOp->getSourceRange();
6699 NoteLoc = NotScalarExpr->getExprLoc();
6700 NoteRange = NotScalarExpr->getSourceRange();
6701 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006702 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataev62cec442014-11-18 10:14:22 +00006703 ErrorFound = NotAnAssignmentOp;
6704 ErrorLoc = AtomicBody->getExprLoc();
6705 ErrorRange = AtomicBody->getSourceRange();
6706 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6707 : AtomicBody->getExprLoc();
6708 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6709 : AtomicBody->getSourceRange();
6710 }
6711 } else {
6712 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006713 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataev62cec442014-11-18 10:14:22 +00006714 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006715 }
Alexey Bataev62cec442014-11-18 10:14:22 +00006716 if (ErrorFound != NoError) {
6717 Diag(ErrorLoc, diag::err_omp_atomic_read_not_expression_statement)
6718 << ErrorRange;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006719 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6720 << NoteRange;
Alexey Bataev62cec442014-11-18 10:14:22 +00006721 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006722 }
6723 if (CurContext->isDependentContext())
Alexey Bataev62cec442014-11-18 10:14:22 +00006724 V = X = nullptr;
Alexey Bataevdea47612014-07-23 07:46:59 +00006725 } else if (AtomicKind == OMPC_write) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006726 enum {
6727 NotAnExpression,
6728 NotAnAssignmentOp,
6729 NotAScalarType,
6730 NotAnLValue,
6731 NoError
6732 } ErrorFound = NoError;
Alexey Bataevf33eba62014-11-28 07:21:40 +00006733 SourceLocation ErrorLoc, NoteLoc;
6734 SourceRange ErrorRange, NoteRange;
6735 // If clause is write:
6736 // x = expr;
Alexey Bataeve3727102018-04-18 15:57:46 +00006737 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
6738 const auto *AtomicBinOp =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006739 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6740 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
Alexey Bataevb8329262015-02-27 06:33:30 +00006741 X = AtomicBinOp->getLHS();
6742 E = AtomicBinOp->getRHS();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006743 if ((X->isInstantiationDependent() || X->getType()->isScalarType()) &&
6744 (E->isInstantiationDependent() || E->getType()->isScalarType())) {
6745 if (!X->isLValue()) {
6746 ErrorFound = NotAnLValue;
6747 ErrorLoc = AtomicBinOp->getExprLoc();
6748 ErrorRange = AtomicBinOp->getSourceRange();
6749 NoteLoc = X->getExprLoc();
6750 NoteRange = X->getSourceRange();
6751 }
6752 } else if (!X->isInstantiationDependent() ||
6753 !E->isInstantiationDependent()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006754 const Expr *NotScalarExpr =
Alexey Bataevf33eba62014-11-28 07:21:40 +00006755 (X->isInstantiationDependent() || X->getType()->isScalarType())
6756 ? E
6757 : X;
6758 ErrorFound = NotAScalarType;
6759 ErrorLoc = AtomicBinOp->getExprLoc();
6760 ErrorRange = AtomicBinOp->getSourceRange();
6761 NoteLoc = NotScalarExpr->getExprLoc();
6762 NoteRange = NotScalarExpr->getSourceRange();
6763 }
Alexey Bataev5a195472015-09-04 12:55:50 +00006764 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevf33eba62014-11-28 07:21:40 +00006765 ErrorFound = NotAnAssignmentOp;
6766 ErrorLoc = AtomicBody->getExprLoc();
6767 ErrorRange = AtomicBody->getSourceRange();
6768 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6769 : AtomicBody->getExprLoc();
6770 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6771 : AtomicBody->getSourceRange();
6772 }
6773 } else {
6774 ErrorFound = NotAnExpression;
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006775 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevf33eba62014-11-28 07:21:40 +00006776 NoteRange = ErrorRange = SourceRange(NoteLoc, NoteLoc);
Alexey Bataevdea47612014-07-23 07:46:59 +00006777 }
Alexey Bataevf33eba62014-11-28 07:21:40 +00006778 if (ErrorFound != NoError) {
6779 Diag(ErrorLoc, diag::err_omp_atomic_write_not_expression_statement)
6780 << ErrorRange;
6781 Diag(NoteLoc, diag::note_omp_atomic_read_write) << ErrorFound
6782 << NoteRange;
6783 return StmtError();
Alexey Bataeve3727102018-04-18 15:57:46 +00006784 }
6785 if (CurContext->isDependentContext())
Alexey Bataevf33eba62014-11-28 07:21:40 +00006786 E = X = nullptr;
Alexey Bataev67a4f222014-07-23 10:25:33 +00006787 } else if (AtomicKind == OMPC_update || AtomicKind == OMPC_unknown) {
Alexey Bataev1d160b12015-03-13 12:27:31 +00006788 // If clause is update:
6789 // x++;
6790 // x--;
6791 // ++x;
6792 // --x;
6793 // x binop= expr;
6794 // x = x binop expr;
6795 // x = expr binop x;
6796 OpenMPAtomicUpdateChecker Checker(*this);
6797 if (Checker.checkStatement(
6798 Body, (AtomicKind == OMPC_update)
6799 ? diag::err_omp_atomic_update_not_expression_statement
6800 : diag::err_omp_atomic_not_expression_statement,
6801 diag::note_omp_atomic_update))
Alexey Bataev67a4f222014-07-23 10:25:33 +00006802 return StmtError();
Alexey Bataev1d160b12015-03-13 12:27:31 +00006803 if (!CurContext->isDependentContext()) {
6804 E = Checker.getExpr();
6805 X = Checker.getX();
Alexey Bataevb4505a72015-03-30 05:20:59 +00006806 UE = Checker.getUpdateExpr();
6807 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev67a4f222014-07-23 10:25:33 +00006808 }
Alexey Bataev459dec02014-07-24 06:46:57 +00006809 } else if (AtomicKind == OMPC_capture) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006810 enum {
6811 NotAnAssignmentOp,
6812 NotACompoundStatement,
6813 NotTwoSubstatements,
6814 NotASpecificExpression,
6815 NoError
6816 } ErrorFound = NoError;
6817 SourceLocation ErrorLoc, NoteLoc;
6818 SourceRange ErrorRange, NoteRange;
Alexey Bataeve3727102018-04-18 15:57:46 +00006819 if (const auto *AtomicBody = dyn_cast<Expr>(Body)) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006820 // If clause is a capture:
6821 // v = x++;
6822 // v = x--;
6823 // v = ++x;
6824 // v = --x;
6825 // v = x binop= expr;
6826 // v = x = x binop expr;
6827 // v = x = expr binop x;
Alexey Bataeve3727102018-04-18 15:57:46 +00006828 const auto *AtomicBinOp =
Alexey Bataevb78ca832015-04-01 03:33:17 +00006829 dyn_cast<BinaryOperator>(AtomicBody->IgnoreParenImpCasts());
6830 if (AtomicBinOp && AtomicBinOp->getOpcode() == BO_Assign) {
6831 V = AtomicBinOp->getLHS();
6832 Body = AtomicBinOp->getRHS()->IgnoreParenImpCasts();
6833 OpenMPAtomicUpdateChecker Checker(*this);
6834 if (Checker.checkStatement(
6835 Body, diag::err_omp_atomic_capture_not_expression_statement,
6836 diag::note_omp_atomic_update))
6837 return StmtError();
6838 E = Checker.getExpr();
6839 X = Checker.getX();
6840 UE = Checker.getUpdateExpr();
6841 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
6842 IsPostfixUpdate = Checker.isPostfixUpdate();
Alexey Bataev5a195472015-09-04 12:55:50 +00006843 } else if (!AtomicBody->isInstantiationDependent()) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006844 ErrorLoc = AtomicBody->getExprLoc();
6845 ErrorRange = AtomicBody->getSourceRange();
6846 NoteLoc = AtomicBinOp ? AtomicBinOp->getOperatorLoc()
6847 : AtomicBody->getExprLoc();
6848 NoteRange = AtomicBinOp ? AtomicBinOp->getSourceRange()
6849 : AtomicBody->getSourceRange();
6850 ErrorFound = NotAnAssignmentOp;
6851 }
6852 if (ErrorFound != NoError) {
6853 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_expression_statement)
6854 << ErrorRange;
6855 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
6856 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006857 }
Alexey Bataeve3727102018-04-18 15:57:46 +00006858 if (CurContext->isDependentContext())
6859 UE = V = E = X = nullptr;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006860 } else {
6861 // If clause is a capture:
6862 // { v = x; x = expr; }
6863 // { v = x; x++; }
6864 // { v = x; x--; }
6865 // { v = x; ++x; }
6866 // { v = x; --x; }
6867 // { v = x; x binop= expr; }
6868 // { v = x; x = x binop expr; }
6869 // { v = x; x = expr binop x; }
6870 // { x++; v = x; }
6871 // { x--; v = x; }
6872 // { ++x; v = x; }
6873 // { --x; v = x; }
6874 // { x binop= expr; v = x; }
6875 // { x = x binop expr; v = x; }
6876 // { x = expr binop x; v = x; }
6877 if (auto *CS = dyn_cast<CompoundStmt>(Body)) {
6878 // Check that this is { expr1; expr2; }
6879 if (CS->size() == 2) {
Alexey Bataeve3727102018-04-18 15:57:46 +00006880 Stmt *First = CS->body_front();
6881 Stmt *Second = CS->body_back();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006882 if (auto *EWC = dyn_cast<ExprWithCleanups>(First))
6883 First = EWC->getSubExpr()->IgnoreParenImpCasts();
6884 if (auto *EWC = dyn_cast<ExprWithCleanups>(Second))
6885 Second = EWC->getSubExpr()->IgnoreParenImpCasts();
6886 // Need to find what subexpression is 'v' and what is 'x'.
6887 OpenMPAtomicUpdateChecker Checker(*this);
6888 bool IsUpdateExprFound = !Checker.checkStatement(Second);
6889 BinaryOperator *BinOp = nullptr;
6890 if (IsUpdateExprFound) {
6891 BinOp = dyn_cast<BinaryOperator>(First);
6892 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6893 }
6894 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6895 // { v = x; x++; }
6896 // { v = x; x--; }
6897 // { v = x; ++x; }
6898 // { v = x; --x; }
6899 // { v = x; x binop= expr; }
6900 // { v = x; x = x binop expr; }
6901 // { v = x; x = expr binop x; }
6902 // Check that the first expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006903 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006904 llvm::FoldingSetNodeID XId, PossibleXId;
6905 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6906 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6907 IsUpdateExprFound = XId == PossibleXId;
6908 if (IsUpdateExprFound) {
6909 V = BinOp->getLHS();
6910 X = Checker.getX();
6911 E = Checker.getExpr();
6912 UE = Checker.getUpdateExpr();
6913 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006914 IsPostfixUpdate = true;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006915 }
6916 }
6917 if (!IsUpdateExprFound) {
6918 IsUpdateExprFound = !Checker.checkStatement(First);
6919 BinOp = nullptr;
6920 if (IsUpdateExprFound) {
6921 BinOp = dyn_cast<BinaryOperator>(Second);
6922 IsUpdateExprFound = BinOp && BinOp->getOpcode() == BO_Assign;
6923 }
6924 if (IsUpdateExprFound && !CurContext->isDependentContext()) {
6925 // { x++; v = x; }
6926 // { x--; v = x; }
6927 // { ++x; v = x; }
6928 // { --x; v = x; }
6929 // { x binop= expr; v = x; }
6930 // { x = x binop expr; v = x; }
6931 // { x = expr binop x; v = x; }
6932 // Check that the second expression has form v = x.
Alexey Bataeve3727102018-04-18 15:57:46 +00006933 Expr *PossibleX = BinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataevb78ca832015-04-01 03:33:17 +00006934 llvm::FoldingSetNodeID XId, PossibleXId;
6935 Checker.getX()->Profile(XId, Context, /*Canonical=*/true);
6936 PossibleX->Profile(PossibleXId, Context, /*Canonical=*/true);
6937 IsUpdateExprFound = XId == PossibleXId;
6938 if (IsUpdateExprFound) {
6939 V = BinOp->getLHS();
6940 X = Checker.getX();
6941 E = Checker.getExpr();
6942 UE = Checker.getUpdateExpr();
6943 IsXLHSInRHSPart = Checker.isXLHSInRHSPart();
Alexey Bataev5e018f92015-04-23 06:35:10 +00006944 IsPostfixUpdate = false;
Alexey Bataevb78ca832015-04-01 03:33:17 +00006945 }
6946 }
6947 }
6948 if (!IsUpdateExprFound) {
6949 // { v = x; x = expr; }
Alexey Bataev5a195472015-09-04 12:55:50 +00006950 auto *FirstExpr = dyn_cast<Expr>(First);
6951 auto *SecondExpr = dyn_cast<Expr>(Second);
6952 if (!FirstExpr || !SecondExpr ||
6953 !(FirstExpr->isInstantiationDependent() ||
6954 SecondExpr->isInstantiationDependent())) {
6955 auto *FirstBinOp = dyn_cast<BinaryOperator>(First);
6956 if (!FirstBinOp || FirstBinOp->getOpcode() != BO_Assign) {
Alexey Bataevb78ca832015-04-01 03:33:17 +00006957 ErrorFound = NotAnAssignmentOp;
Alexey Bataev5a195472015-09-04 12:55:50 +00006958 NoteLoc = ErrorLoc = FirstBinOp ? FirstBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006959 : First->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006960 NoteRange = ErrorRange = FirstBinOp
6961 ? FirstBinOp->getSourceRange()
Alexey Bataevb78ca832015-04-01 03:33:17 +00006962 : SourceRange(ErrorLoc, ErrorLoc);
6963 } else {
Alexey Bataev5a195472015-09-04 12:55:50 +00006964 auto *SecondBinOp = dyn_cast<BinaryOperator>(Second);
6965 if (!SecondBinOp || SecondBinOp->getOpcode() != BO_Assign) {
6966 ErrorFound = NotAnAssignmentOp;
6967 NoteLoc = ErrorLoc = SecondBinOp
6968 ? SecondBinOp->getOperatorLoc()
Stephen Kellyf2ceec42018-08-09 21:08:08 +00006969 : Second->getBeginLoc();
Alexey Bataev5a195472015-09-04 12:55:50 +00006970 NoteRange = ErrorRange =
6971 SecondBinOp ? SecondBinOp->getSourceRange()
6972 : SourceRange(ErrorLoc, ErrorLoc);
Alexey Bataevb78ca832015-04-01 03:33:17 +00006973 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00006974 Expr *PossibleXRHSInFirst =
Alexey Bataev5a195472015-09-04 12:55:50 +00006975 FirstBinOp->getRHS()->IgnoreParenImpCasts();
Alexey Bataeve3727102018-04-18 15:57:46 +00006976 Expr *PossibleXLHSInSecond =
Alexey Bataev5a195472015-09-04 12:55:50 +00006977 SecondBinOp->getLHS()->IgnoreParenImpCasts();
6978 llvm::FoldingSetNodeID X1Id, X2Id;
6979 PossibleXRHSInFirst->Profile(X1Id, Context,
6980 /*Canonical=*/true);
6981 PossibleXLHSInSecond->Profile(X2Id, Context,
6982 /*Canonical=*/true);
6983 IsUpdateExprFound = X1Id == X2Id;
6984 if (IsUpdateExprFound) {
6985 V = FirstBinOp->getLHS();
6986 X = SecondBinOp->getLHS();
6987 E = SecondBinOp->getRHS();
6988 UE = nullptr;
6989 IsXLHSInRHSPart = false;
6990 IsPostfixUpdate = true;
6991 } else {
6992 ErrorFound = NotASpecificExpression;
6993 ErrorLoc = FirstBinOp->getExprLoc();
6994 ErrorRange = FirstBinOp->getSourceRange();
6995 NoteLoc = SecondBinOp->getLHS()->getExprLoc();
6996 NoteRange = SecondBinOp->getRHS()->getSourceRange();
6997 }
Alexey Bataevb78ca832015-04-01 03:33:17 +00006998 }
6999 }
7000 }
7001 }
7002 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007003 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007004 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007005 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00007006 ErrorFound = NotTwoSubstatements;
7007 }
7008 } else {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007009 NoteLoc = ErrorLoc = Body->getBeginLoc();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007010 NoteRange = ErrorRange =
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007011 SourceRange(Body->getBeginLoc(), Body->getBeginLoc());
Alexey Bataevb78ca832015-04-01 03:33:17 +00007012 ErrorFound = NotACompoundStatement;
7013 }
7014 if (ErrorFound != NoError) {
7015 Diag(ErrorLoc, diag::err_omp_atomic_capture_not_compound_statement)
7016 << ErrorRange;
7017 Diag(NoteLoc, diag::note_omp_atomic_capture) << ErrorFound << NoteRange;
7018 return StmtError();
Alexey Bataevb78ca832015-04-01 03:33:17 +00007019 }
Alexey Bataeve3727102018-04-18 15:57:46 +00007020 if (CurContext->isDependentContext())
7021 UE = V = E = X = nullptr;
Alexey Bataev459dec02014-07-24 06:46:57 +00007022 }
Alexey Bataevdea47612014-07-23 07:46:59 +00007023 }
Alexey Bataev0162e452014-07-22 10:10:35 +00007024
Reid Kleckner87a31802018-03-12 21:43:02 +00007025 setFunctionHasBranchProtectedScope();
Alexey Bataev0162e452014-07-22 10:10:35 +00007026
Alexey Bataev62cec442014-11-18 10:14:22 +00007027 return OMPAtomicDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt,
Alexey Bataevb78ca832015-04-01 03:33:17 +00007028 X, V, E, UE, IsXLHSInRHSPart,
7029 IsPostfixUpdate);
Alexey Bataev0162e452014-07-22 10:10:35 +00007030}
7031
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007032StmtResult Sema::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
7033 Stmt *AStmt,
7034 SourceLocation StartLoc,
7035 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007036 if (!AStmt)
7037 return StmtError();
7038
Alexey Bataeve3727102018-04-18 15:57:46 +00007039 auto *CS = cast<CapturedStmt>(AStmt);
Samuel Antao4af1b7b2015-12-02 17:44:43 +00007040 // 1.2.2 OpenMP Language Terminology
7041 // Structured block - An executable statement with a single entry at the
7042 // top and a single exit at the bottom.
7043 // The point of exit cannot be a branch out of the structured block.
7044 // longjmp() and throw() must not violate the entry/exit criteria.
7045 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007046 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target);
7047 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7048 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7049 // 1.2.2 OpenMP Language Terminology
7050 // Structured block - An executable statement with a single entry at the
7051 // top and a single exit at the bottom.
7052 // The point of exit cannot be a branch out of the structured block.
7053 // longjmp() and throw() must not violate the entry/exit criteria.
7054 CS->getCapturedDecl()->setNothrow();
7055 }
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007056
Alexey Bataev13314bf2014-10-09 04:18:56 +00007057 // OpenMP [2.16, Nesting of Regions]
7058 // If specified, a teams construct must be contained within a target
7059 // construct. That target construct must contain no statements or directives
7060 // outside of the teams construct.
7061 if (DSAStack->hasInnerTeamsRegion()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007062 const Stmt *S = CS->IgnoreContainers(/*IgnoreCaptured=*/true);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007063 bool OMPTeamsFound = true;
Alexey Bataeve3727102018-04-18 15:57:46 +00007064 if (const auto *CS = dyn_cast<CompoundStmt>(S)) {
Alexey Bataev13314bf2014-10-09 04:18:56 +00007065 auto I = CS->body_begin();
7066 while (I != CS->body_end()) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007067 const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007068 if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind())) {
7069 OMPTeamsFound = false;
7070 break;
7071 }
7072 ++I;
7073 }
7074 assert(I != CS->body_end() && "Not found statement");
7075 S = *I;
Kelvin Li3834dce2016-06-27 19:15:43 +00007076 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +00007077 const auto *OED = dyn_cast<OMPExecutableDirective>(S);
Kelvin Li3834dce2016-06-27 19:15:43 +00007078 OMPTeamsFound = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
Alexey Bataev13314bf2014-10-09 04:18:56 +00007079 }
7080 if (!OMPTeamsFound) {
7081 Diag(StartLoc, diag::err_omp_target_contains_not_only_teams);
7082 Diag(DSAStack->getInnerTeamsRegionLoc(),
7083 diag::note_omp_nested_teams_construct_here);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007084 Diag(S->getBeginLoc(), diag::note_omp_nested_statement_here)
Alexey Bataev13314bf2014-10-09 04:18:56 +00007085 << isa<OMPExecutableDirective>(S);
7086 return StmtError();
7087 }
7088 }
7089
Reid Kleckner87a31802018-03-12 21:43:02 +00007090 setFunctionHasBranchProtectedScope();
Alexey Bataev0bd520b2014-09-19 08:19:49 +00007091
7092 return OMPTargetDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7093}
7094
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007095StmtResult
7096Sema::ActOnOpenMPTargetParallelDirective(ArrayRef<OMPClause *> Clauses,
7097 Stmt *AStmt, SourceLocation StartLoc,
7098 SourceLocation EndLoc) {
7099 if (!AStmt)
7100 return StmtError();
7101
Alexey Bataeve3727102018-04-18 15:57:46 +00007102 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007103 // 1.2.2 OpenMP Language Terminology
7104 // Structured block - An executable statement with a single entry at the
7105 // top and a single exit at the bottom.
7106 // The point of exit cannot be a branch out of the structured block.
7107 // longjmp() and throw() must not violate the entry/exit criteria.
7108 CS->getCapturedDecl()->setNothrow();
Alexey Bataev8451efa2018-01-15 19:06:12 +00007109 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel);
7110 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7111 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7112 // 1.2.2 OpenMP Language Terminology
7113 // Structured block - An executable statement with a single entry at the
7114 // top and a single exit at the bottom.
7115 // The point of exit cannot be a branch out of the structured block.
7116 // longjmp() and throw() must not violate the entry/exit criteria.
7117 CS->getCapturedDecl()->setNothrow();
7118 }
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007119
Reid Kleckner87a31802018-03-12 21:43:02 +00007120 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacobe955b3d2016-01-26 18:48:41 +00007121
7122 return OMPTargetParallelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7123 AStmt);
7124}
7125
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007126StmtResult Sema::ActOnOpenMPTargetParallelForDirective(
7127 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007128 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007129 if (!AStmt)
7130 return StmtError();
7131
Alexey Bataeve3727102018-04-18 15:57:46 +00007132 auto *CS = cast<CapturedStmt>(AStmt);
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007133 // 1.2.2 OpenMP Language Terminology
7134 // Structured block - An executable statement with a single entry at the
7135 // top and a single exit at the bottom.
7136 // The point of exit cannot be a branch out of the structured block.
7137 // longjmp() and throw() must not violate the entry/exit criteria.
7138 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007139 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7140 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7141 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7142 // 1.2.2 OpenMP Language Terminology
7143 // Structured block - An executable statement with a single entry at the
7144 // top and a single exit at the bottom.
7145 // The point of exit cannot be a branch out of the structured block.
7146 // longjmp() and throw() must not violate the entry/exit criteria.
7147 CS->getCapturedDecl()->setNothrow();
7148 }
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007149
7150 OMPLoopDirective::HelperExprs B;
7151 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7152 // define the nested loops number.
7153 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007154 checkOpenMPLoop(OMPD_target_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00007155 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007156 VarsWithImplicitDSA, B);
7157 if (NestedLoopCount == 0)
7158 return StmtError();
7159
7160 assert((CurContext->isDependentContext() || B.builtAll()) &&
7161 "omp target parallel for loop exprs were not built");
7162
7163 if (!CurContext->isDependentContext()) {
7164 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007165 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007166 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007167 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007168 B.NumIterations, *this, CurScope,
7169 DSAStack))
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007170 return StmtError();
7171 }
7172 }
7173
Reid Kleckner87a31802018-03-12 21:43:02 +00007174 setFunctionHasBranchProtectedScope();
Arpith Chacko Jacob05bebb52016-02-03 15:46:42 +00007175 return OMPTargetParallelForDirective::Create(Context, StartLoc, EndLoc,
7176 NestedLoopCount, Clauses, AStmt,
7177 B, DSAStack->isCancelRegion());
7178}
7179
Alexey Bataev95b64a92017-05-30 16:00:04 +00007180/// Check for existence of a map clause in the list of clauses.
7181static bool hasClauses(ArrayRef<OMPClause *> Clauses,
7182 const OpenMPClauseKind K) {
7183 return llvm::any_of(
7184 Clauses, [K](const OMPClause *C) { return C->getClauseKind() == K; });
7185}
Samuel Antaodf67fc42016-01-19 19:15:56 +00007186
Alexey Bataev95b64a92017-05-30 16:00:04 +00007187template <typename... Params>
7188static bool hasClauses(ArrayRef<OMPClause *> Clauses, const OpenMPClauseKind K,
7189 const Params... ClauseTypes) {
7190 return hasClauses(Clauses, K) || hasClauses(Clauses, ClauseTypes...);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007191}
7192
Michael Wong65f367f2015-07-21 13:44:28 +00007193StmtResult Sema::ActOnOpenMPTargetDataDirective(ArrayRef<OMPClause *> Clauses,
7194 Stmt *AStmt,
7195 SourceLocation StartLoc,
7196 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007197 if (!AStmt)
7198 return StmtError();
7199
7200 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7201
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007202 // OpenMP [2.10.1, Restrictions, p. 97]
7203 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007204 if (!hasClauses(Clauses, OMPC_map, OMPC_use_device_ptr)) {
7205 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7206 << "'map' or 'use_device_ptr'"
David Majnemer9d168222016-08-05 17:44:54 +00007207 << getOpenMPDirectiveName(OMPD_target_data);
Arpith Chacko Jacob46a04bb2016-01-21 19:57:55 +00007208 return StmtError();
7209 }
7210
Reid Kleckner87a31802018-03-12 21:43:02 +00007211 setFunctionHasBranchProtectedScope();
Michael Wong65f367f2015-07-21 13:44:28 +00007212
7213 return OMPTargetDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7214 AStmt);
7215}
7216
Samuel Antaodf67fc42016-01-19 19:15:56 +00007217StmtResult
7218Sema::ActOnOpenMPTargetEnterDataDirective(ArrayRef<OMPClause *> Clauses,
7219 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007220 SourceLocation EndLoc, Stmt *AStmt) {
7221 if (!AStmt)
7222 return StmtError();
7223
Alexey Bataeve3727102018-04-18 15:57:46 +00007224 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007225 // 1.2.2 OpenMP Language Terminology
7226 // Structured block - An executable statement with a single entry at the
7227 // top and a single exit at the bottom.
7228 // The point of exit cannot be a branch out of the structured block.
7229 // longjmp() and throw() must not violate the entry/exit criteria.
7230 CS->getCapturedDecl()->setNothrow();
7231 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_enter_data);
7232 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7233 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7234 // 1.2.2 OpenMP Language Terminology
7235 // Structured block - An executable statement with a single entry at the
7236 // top and a single exit at the bottom.
7237 // The point of exit cannot be a branch out of the structured block.
7238 // longjmp() and throw() must not violate the entry/exit criteria.
7239 CS->getCapturedDecl()->setNothrow();
7240 }
7241
Samuel Antaodf67fc42016-01-19 19:15:56 +00007242 // OpenMP [2.10.2, Restrictions, p. 99]
7243 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007244 if (!hasClauses(Clauses, OMPC_map)) {
7245 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7246 << "'map'" << getOpenMPDirectiveName(OMPD_target_enter_data);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007247 return StmtError();
7248 }
7249
Alexey Bataev7828b252017-11-21 17:08:48 +00007250 return OMPTargetEnterDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7251 AStmt);
Samuel Antaodf67fc42016-01-19 19:15:56 +00007252}
7253
Samuel Antao72590762016-01-19 20:04:50 +00007254StmtResult
7255Sema::ActOnOpenMPTargetExitDataDirective(ArrayRef<OMPClause *> Clauses,
7256 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007257 SourceLocation EndLoc, Stmt *AStmt) {
7258 if (!AStmt)
7259 return StmtError();
7260
Alexey Bataeve3727102018-04-18 15:57:46 +00007261 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007262 // 1.2.2 OpenMP Language Terminology
7263 // Structured block - An executable statement with a single entry at the
7264 // top and a single exit at the bottom.
7265 // The point of exit cannot be a branch out of the structured block.
7266 // longjmp() and throw() must not violate the entry/exit criteria.
7267 CS->getCapturedDecl()->setNothrow();
7268 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_exit_data);
7269 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7270 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7271 // 1.2.2 OpenMP Language Terminology
7272 // Structured block - An executable statement with a single entry at the
7273 // top and a single exit at the bottom.
7274 // The point of exit cannot be a branch out of the structured block.
7275 // longjmp() and throw() must not violate the entry/exit criteria.
7276 CS->getCapturedDecl()->setNothrow();
7277 }
7278
Samuel Antao72590762016-01-19 20:04:50 +00007279 // OpenMP [2.10.3, Restrictions, p. 102]
7280 // At least one map clause must appear on the directive.
Alexey Bataev95b64a92017-05-30 16:00:04 +00007281 if (!hasClauses(Clauses, OMPC_map)) {
7282 Diag(StartLoc, diag::err_omp_no_clause_for_directive)
7283 << "'map'" << getOpenMPDirectiveName(OMPD_target_exit_data);
Samuel Antao72590762016-01-19 20:04:50 +00007284 return StmtError();
7285 }
7286
Alexey Bataev7828b252017-11-21 17:08:48 +00007287 return OMPTargetExitDataDirective::Create(Context, StartLoc, EndLoc, Clauses,
7288 AStmt);
Samuel Antao72590762016-01-19 20:04:50 +00007289}
7290
Samuel Antao686c70c2016-05-26 17:30:50 +00007291StmtResult Sema::ActOnOpenMPTargetUpdateDirective(ArrayRef<OMPClause *> Clauses,
7292 SourceLocation StartLoc,
Alexey Bataev7828b252017-11-21 17:08:48 +00007293 SourceLocation EndLoc,
7294 Stmt *AStmt) {
7295 if (!AStmt)
7296 return StmtError();
7297
Alexey Bataeve3727102018-04-18 15:57:46 +00007298 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev7828b252017-11-21 17:08:48 +00007299 // 1.2.2 OpenMP Language Terminology
7300 // Structured block - An executable statement with a single entry at the
7301 // top and a single exit at the bottom.
7302 // The point of exit cannot be a branch out of the structured block.
7303 // longjmp() and throw() must not violate the entry/exit criteria.
7304 CS->getCapturedDecl()->setNothrow();
7305 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_update);
7306 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7307 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7308 // 1.2.2 OpenMP Language Terminology
7309 // Structured block - An executable statement with a single entry at the
7310 // top and a single exit at the bottom.
7311 // The point of exit cannot be a branch out of the structured block.
7312 // longjmp() and throw() must not violate the entry/exit criteria.
7313 CS->getCapturedDecl()->setNothrow();
7314 }
7315
Alexey Bataev95b64a92017-05-30 16:00:04 +00007316 if (!hasClauses(Clauses, OMPC_to, OMPC_from)) {
Samuel Antao686c70c2016-05-26 17:30:50 +00007317 Diag(StartLoc, diag::err_omp_at_least_one_motion_clause_required);
7318 return StmtError();
7319 }
Alexey Bataev7828b252017-11-21 17:08:48 +00007320 return OMPTargetUpdateDirective::Create(Context, StartLoc, EndLoc, Clauses,
7321 AStmt);
Samuel Antao686c70c2016-05-26 17:30:50 +00007322}
7323
Alexey Bataev13314bf2014-10-09 04:18:56 +00007324StmtResult Sema::ActOnOpenMPTeamsDirective(ArrayRef<OMPClause *> Clauses,
7325 Stmt *AStmt, SourceLocation StartLoc,
7326 SourceLocation EndLoc) {
Alexey Bataev6b8046a2015-09-03 07:23:48 +00007327 if (!AStmt)
7328 return StmtError();
7329
Alexey Bataeve3727102018-04-18 15:57:46 +00007330 auto *CS = cast<CapturedStmt>(AStmt);
Alexey Bataev13314bf2014-10-09 04:18:56 +00007331 // 1.2.2 OpenMP Language Terminology
7332 // Structured block - An executable statement with a single entry at the
7333 // top and a single exit at the bottom.
7334 // The point of exit cannot be a branch out of the structured block.
7335 // longjmp() and throw() must not violate the entry/exit criteria.
7336 CS->getCapturedDecl()->setNothrow();
7337
Reid Kleckner87a31802018-03-12 21:43:02 +00007338 setFunctionHasBranchProtectedScope();
Alexey Bataev13314bf2014-10-09 04:18:56 +00007339
Alexey Bataevceabd412017-11-30 18:01:54 +00007340 DSAStack->setParentTeamsRegionLoc(StartLoc);
7341
Alexey Bataev13314bf2014-10-09 04:18:56 +00007342 return OMPTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses, AStmt);
7343}
7344
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007345StmtResult
7346Sema::ActOnOpenMPCancellationPointDirective(SourceLocation StartLoc,
7347 SourceLocation EndLoc,
7348 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00007349 if (DSAStack->isParentNowaitRegion()) {
7350 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 0;
7351 return StmtError();
7352 }
7353 if (DSAStack->isParentOrderedRegion()) {
7354 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 0;
7355 return StmtError();
7356 }
7357 return OMPCancellationPointDirective::Create(Context, StartLoc, EndLoc,
7358 CancelRegion);
7359}
7360
Alexey Bataev87933c72015-09-18 08:07:34 +00007361StmtResult Sema::ActOnOpenMPCancelDirective(ArrayRef<OMPClause *> Clauses,
7362 SourceLocation StartLoc,
Alexey Bataev80909872015-07-02 11:25:17 +00007363 SourceLocation EndLoc,
7364 OpenMPDirectiveKind CancelRegion) {
Alexey Bataev80909872015-07-02 11:25:17 +00007365 if (DSAStack->isParentNowaitRegion()) {
7366 Diag(StartLoc, diag::err_omp_parent_cancel_region_nowait) << 1;
7367 return StmtError();
7368 }
7369 if (DSAStack->isParentOrderedRegion()) {
7370 Diag(StartLoc, diag::err_omp_parent_cancel_region_ordered) << 1;
7371 return StmtError();
7372 }
Alexey Bataev25e5b442015-09-15 12:52:43 +00007373 DSAStack->setParentCancelRegion(/*Cancel=*/true);
Alexey Bataev87933c72015-09-18 08:07:34 +00007374 return OMPCancelDirective::Create(Context, StartLoc, EndLoc, Clauses,
7375 CancelRegion);
Alexey Bataev80909872015-07-02 11:25:17 +00007376}
7377
Alexey Bataev382967a2015-12-08 12:06:20 +00007378static bool checkGrainsizeNumTasksClauses(Sema &S,
7379 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007380 const OMPClause *PrevClause = nullptr;
Alexey Bataev382967a2015-12-08 12:06:20 +00007381 bool ErrorFound = false;
Alexey Bataeve3727102018-04-18 15:57:46 +00007382 for (const OMPClause *C : Clauses) {
Alexey Bataev382967a2015-12-08 12:06:20 +00007383 if (C->getClauseKind() == OMPC_grainsize ||
7384 C->getClauseKind() == OMPC_num_tasks) {
7385 if (!PrevClause)
7386 PrevClause = C;
7387 else if (PrevClause->getClauseKind() != C->getClauseKind()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007388 S.Diag(C->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007389 diag::err_omp_grainsize_num_tasks_mutually_exclusive)
7390 << getOpenMPClauseName(C->getClauseKind())
7391 << getOpenMPClauseName(PrevClause->getClauseKind());
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007392 S.Diag(PrevClause->getBeginLoc(),
Alexey Bataev382967a2015-12-08 12:06:20 +00007393 diag::note_omp_previous_grainsize_num_tasks)
7394 << getOpenMPClauseName(PrevClause->getClauseKind());
7395 ErrorFound = true;
7396 }
7397 }
7398 }
7399 return ErrorFound;
7400}
7401
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007402static bool checkReductionClauseWithNogroup(Sema &S,
7403 ArrayRef<OMPClause *> Clauses) {
Alexey Bataeve3727102018-04-18 15:57:46 +00007404 const OMPClause *ReductionClause = nullptr;
7405 const OMPClause *NogroupClause = nullptr;
7406 for (const OMPClause *C : Clauses) {
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007407 if (C->getClauseKind() == OMPC_reduction) {
7408 ReductionClause = C;
7409 if (NogroupClause)
7410 break;
7411 continue;
7412 }
7413 if (C->getClauseKind() == OMPC_nogroup) {
7414 NogroupClause = C;
7415 if (ReductionClause)
7416 break;
7417 continue;
7418 }
7419 }
7420 if (ReductionClause && NogroupClause) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00007421 S.Diag(ReductionClause->getBeginLoc(), diag::err_omp_reduction_with_nogroup)
7422 << SourceRange(NogroupClause->getBeginLoc(),
Stephen Kelly1c301dc2018-08-09 21:09:38 +00007423 NogroupClause->getEndLoc());
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007424 return true;
7425 }
7426 return false;
7427}
7428
Alexey Bataev49f6e782015-12-01 04:18:41 +00007429StmtResult Sema::ActOnOpenMPTaskLoopDirective(
7430 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007431 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev49f6e782015-12-01 04:18:41 +00007432 if (!AStmt)
7433 return StmtError();
7434
7435 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7436 OMPLoopDirective::HelperExprs B;
7437 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7438 // define the nested loops number.
7439 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007440 checkOpenMPLoop(OMPD_taskloop, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007441 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
Alexey Bataev49f6e782015-12-01 04:18:41 +00007442 VarsWithImplicitDSA, B);
7443 if (NestedLoopCount == 0)
7444 return StmtError();
7445
7446 assert((CurContext->isDependentContext() || B.builtAll()) &&
7447 "omp for loop exprs were not built");
7448
Alexey Bataev382967a2015-12-08 12:06:20 +00007449 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7450 // The grainsize clause and num_tasks clause are mutually exclusive and may
7451 // not appear on the same taskloop directive.
7452 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7453 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007454 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7455 // If a reduction clause is present on the taskloop directive, the nogroup
7456 // clause must not be specified.
7457 if (checkReductionClauseWithNogroup(*this, Clauses))
7458 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007459
Reid Kleckner87a31802018-03-12 21:43:02 +00007460 setFunctionHasBranchProtectedScope();
Alexey Bataev49f6e782015-12-01 04:18:41 +00007461 return OMPTaskLoopDirective::Create(Context, StartLoc, EndLoc,
7462 NestedLoopCount, Clauses, AStmt, B);
7463}
7464
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007465StmtResult Sema::ActOnOpenMPTaskLoopSimdDirective(
7466 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007467 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007468 if (!AStmt)
7469 return StmtError();
7470
7471 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7472 OMPLoopDirective::HelperExprs B;
7473 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7474 // define the nested loops number.
7475 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007476 checkOpenMPLoop(OMPD_taskloop_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007477 /*OrderedLoopCountExpr=*/nullptr, AStmt, *this, *DSAStack,
7478 VarsWithImplicitDSA, B);
7479 if (NestedLoopCount == 0)
7480 return StmtError();
7481
7482 assert((CurContext->isDependentContext() || B.builtAll()) &&
7483 "omp for loop exprs were not built");
7484
Alexey Bataev5a3af132016-03-29 08:58:54 +00007485 if (!CurContext->isDependentContext()) {
7486 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007487 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007488 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007489 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
Alexey Bataev5dff95c2016-04-22 03:56:56 +00007490 B.NumIterations, *this, CurScope,
7491 DSAStack))
Alexey Bataev5a3af132016-03-29 08:58:54 +00007492 return StmtError();
7493 }
7494 }
7495
Alexey Bataev382967a2015-12-08 12:06:20 +00007496 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7497 // The grainsize clause and num_tasks clause are mutually exclusive and may
7498 // not appear on the same taskloop directive.
7499 if (checkGrainsizeNumTasksClauses(*this, Clauses))
7500 return StmtError();
Alexey Bataevbcd0ae02017-07-11 19:16:44 +00007501 // OpenMP, [2.9.2 taskloop Construct, Restrictions]
7502 // If a reduction clause is present on the taskloop directive, the nogroup
7503 // clause must not be specified.
7504 if (checkReductionClauseWithNogroup(*this, Clauses))
7505 return StmtError();
Alexey Bataev438388c2017-11-22 18:34:02 +00007506 if (checkSimdlenSafelenSpecified(*this, Clauses))
7507 return StmtError();
Alexey Bataev382967a2015-12-08 12:06:20 +00007508
Reid Kleckner87a31802018-03-12 21:43:02 +00007509 setFunctionHasBranchProtectedScope();
Alexey Bataev0a6ed842015-12-03 09:40:15 +00007510 return OMPTaskLoopSimdDirective::Create(Context, StartLoc, EndLoc,
7511 NestedLoopCount, Clauses, AStmt, B);
7512}
7513
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007514StmtResult Sema::ActOnOpenMPDistributeDirective(
7515 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007516 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007517 if (!AStmt)
7518 return StmtError();
7519
7520 assert(isa<CapturedStmt>(AStmt) && "Captured statement expected");
7521 OMPLoopDirective::HelperExprs B;
7522 // In presence of clause 'collapse' with number of loops, it will
7523 // define the nested loops number.
7524 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007525 checkOpenMPLoop(OMPD_distribute, getCollapseNumberExpr(Clauses),
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007526 nullptr /*ordered not a clause on distribute*/, AStmt,
7527 *this, *DSAStack, VarsWithImplicitDSA, B);
7528 if (NestedLoopCount == 0)
7529 return StmtError();
7530
7531 assert((CurContext->isDependentContext() || B.builtAll()) &&
7532 "omp for loop exprs were not built");
7533
Reid Kleckner87a31802018-03-12 21:43:02 +00007534 setFunctionHasBranchProtectedScope();
Carlo Bertolli6200a3d2015-12-14 14:51:25 +00007535 return OMPDistributeDirective::Create(Context, StartLoc, EndLoc,
7536 NestedLoopCount, Clauses, AStmt, B);
7537}
7538
Carlo Bertolli9925f152016-06-27 14:55:37 +00007539StmtResult Sema::ActOnOpenMPDistributeParallelForDirective(
7540 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007541 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Carlo Bertolli9925f152016-06-27 14:55:37 +00007542 if (!AStmt)
7543 return StmtError();
7544
Alexey Bataeve3727102018-04-18 15:57:46 +00007545 auto *CS = cast<CapturedStmt>(AStmt);
Carlo Bertolli9925f152016-06-27 14:55:37 +00007546 // 1.2.2 OpenMP Language Terminology
7547 // Structured block - An executable statement with a single entry at the
7548 // top and a single exit at the bottom.
7549 // The point of exit cannot be a branch out of the structured block.
7550 // longjmp() and throw() must not violate the entry/exit criteria.
7551 CS->getCapturedDecl()->setNothrow();
Alexey Bataev7f96c372017-11-22 17:19:31 +00007552 for (int ThisCaptureLevel =
7553 getOpenMPCaptureLevels(OMPD_distribute_parallel_for);
7554 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7555 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7556 // 1.2.2 OpenMP Language Terminology
7557 // Structured block - An executable statement with a single entry at the
7558 // top and a single exit at the bottom.
7559 // The point of exit cannot be a branch out of the structured block.
7560 // longjmp() and throw() must not violate the entry/exit criteria.
7561 CS->getCapturedDecl()->setNothrow();
7562 }
Carlo Bertolli9925f152016-06-27 14:55:37 +00007563
7564 OMPLoopDirective::HelperExprs B;
7565 // In presence of clause 'collapse' with number of loops, it will
7566 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007567 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli9925f152016-06-27 14:55:37 +00007568 OMPD_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Alexey Bataev7f96c372017-11-22 17:19:31 +00007569 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Carlo Bertolli9925f152016-06-27 14:55:37 +00007570 VarsWithImplicitDSA, B);
7571 if (NestedLoopCount == 0)
7572 return StmtError();
7573
7574 assert((CurContext->isDependentContext() || B.builtAll()) &&
7575 "omp for loop exprs were not built");
7576
Reid Kleckner87a31802018-03-12 21:43:02 +00007577 setFunctionHasBranchProtectedScope();
Carlo Bertolli9925f152016-06-27 14:55:37 +00007578 return OMPDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00007579 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
7580 DSAStack->isCancelRegion());
Carlo Bertolli9925f152016-06-27 14:55:37 +00007581}
7582
Kelvin Li4a39add2016-07-05 05:00:15 +00007583StmtResult Sema::ActOnOpenMPDistributeParallelForSimdDirective(
7584 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007585 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4a39add2016-07-05 05:00:15 +00007586 if (!AStmt)
7587 return StmtError();
7588
Alexey Bataeve3727102018-04-18 15:57:46 +00007589 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4a39add2016-07-05 05:00:15 +00007590 // 1.2.2 OpenMP Language Terminology
7591 // Structured block - An executable statement with a single entry at the
7592 // top and a single exit at the bottom.
7593 // The point of exit cannot be a branch out of the structured block.
7594 // longjmp() and throw() must not violate the entry/exit criteria.
7595 CS->getCapturedDecl()->setNothrow();
Alexey Bataev974acd62017-11-27 19:38:52 +00007596 for (int ThisCaptureLevel =
7597 getOpenMPCaptureLevels(OMPD_distribute_parallel_for_simd);
7598 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7599 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7600 // 1.2.2 OpenMP Language Terminology
7601 // Structured block - An executable statement with a single entry at the
7602 // top and a single exit at the bottom.
7603 // The point of exit cannot be a branch out of the structured block.
7604 // longjmp() and throw() must not violate the entry/exit criteria.
7605 CS->getCapturedDecl()->setNothrow();
7606 }
Kelvin Li4a39add2016-07-05 05:00:15 +00007607
7608 OMPLoopDirective::HelperExprs B;
7609 // In presence of clause 'collapse' with number of loops, it will
7610 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007611 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li4a39add2016-07-05 05:00:15 +00007612 OMPD_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev974acd62017-11-27 19:38:52 +00007613 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li4a39add2016-07-05 05:00:15 +00007614 VarsWithImplicitDSA, B);
7615 if (NestedLoopCount == 0)
7616 return StmtError();
7617
7618 assert((CurContext->isDependentContext() || B.builtAll()) &&
7619 "omp for loop exprs were not built");
7620
Alexey Bataev438388c2017-11-22 18:34:02 +00007621 if (!CurContext->isDependentContext()) {
7622 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007623 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007624 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7625 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7626 B.NumIterations, *this, CurScope,
7627 DSAStack))
7628 return StmtError();
7629 }
7630 }
7631
Kelvin Lic5609492016-07-15 04:39:07 +00007632 if (checkSimdlenSafelenSpecified(*this, Clauses))
7633 return StmtError();
7634
Reid Kleckner87a31802018-03-12 21:43:02 +00007635 setFunctionHasBranchProtectedScope();
Kelvin Li4a39add2016-07-05 05:00:15 +00007636 return OMPDistributeParallelForSimdDirective::Create(
7637 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7638}
7639
Kelvin Li787f3fc2016-07-06 04:45:38 +00007640StmtResult Sema::ActOnOpenMPDistributeSimdDirective(
7641 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007642 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li787f3fc2016-07-06 04:45:38 +00007643 if (!AStmt)
7644 return StmtError();
7645
Alexey Bataeve3727102018-04-18 15:57:46 +00007646 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007647 // 1.2.2 OpenMP Language Terminology
7648 // Structured block - An executable statement with a single entry at the
7649 // top and a single exit at the bottom.
7650 // The point of exit cannot be a branch out of the structured block.
7651 // longjmp() and throw() must not violate the entry/exit criteria.
7652 CS->getCapturedDecl()->setNothrow();
Alexey Bataev617db5f2017-12-04 15:38:33 +00007653 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_distribute_simd);
7654 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7655 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7656 // 1.2.2 OpenMP Language Terminology
7657 // Structured block - An executable statement with a single entry at the
7658 // top and a single exit at the bottom.
7659 // The point of exit cannot be a branch out of the structured block.
7660 // longjmp() and throw() must not violate the entry/exit criteria.
7661 CS->getCapturedDecl()->setNothrow();
7662 }
Kelvin Li787f3fc2016-07-06 04:45:38 +00007663
7664 OMPLoopDirective::HelperExprs B;
7665 // In presence of clause 'collapse' with number of loops, it will
7666 // define the nested loops number.
7667 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007668 checkOpenMPLoop(OMPD_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev617db5f2017-12-04 15:38:33 +00007669 nullptr /*ordered not a clause on distribute*/, CS, *this,
7670 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li787f3fc2016-07-06 04:45:38 +00007671 if (NestedLoopCount == 0)
7672 return StmtError();
7673
7674 assert((CurContext->isDependentContext() || B.builtAll()) &&
7675 "omp for loop exprs were not built");
7676
Alexey Bataev438388c2017-11-22 18:34:02 +00007677 if (!CurContext->isDependentContext()) {
7678 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007679 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00007680 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7681 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7682 B.NumIterations, *this, CurScope,
7683 DSAStack))
7684 return StmtError();
7685 }
7686 }
7687
Kelvin Lic5609492016-07-15 04:39:07 +00007688 if (checkSimdlenSafelenSpecified(*this, Clauses))
7689 return StmtError();
7690
Reid Kleckner87a31802018-03-12 21:43:02 +00007691 setFunctionHasBranchProtectedScope();
Kelvin Li787f3fc2016-07-06 04:45:38 +00007692 return OMPDistributeSimdDirective::Create(Context, StartLoc, EndLoc,
7693 NestedLoopCount, Clauses, AStmt, B);
7694}
7695
Kelvin Lia579b912016-07-14 02:54:56 +00007696StmtResult Sema::ActOnOpenMPTargetParallelForSimdDirective(
7697 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007698 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lia579b912016-07-14 02:54:56 +00007699 if (!AStmt)
7700 return StmtError();
7701
Alexey Bataeve3727102018-04-18 15:57:46 +00007702 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Lia579b912016-07-14 02:54:56 +00007703 // 1.2.2 OpenMP Language Terminology
7704 // Structured block - An executable statement with a single entry at the
7705 // top and a single exit at the bottom.
7706 // The point of exit cannot be a branch out of the structured block.
7707 // longjmp() and throw() must not violate the entry/exit criteria.
7708 CS->getCapturedDecl()->setNothrow();
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007709 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_parallel_for);
7710 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7711 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7712 // 1.2.2 OpenMP Language Terminology
7713 // Structured block - An executable statement with a single entry at the
7714 // top and a single exit at the bottom.
7715 // The point of exit cannot be a branch out of the structured block.
7716 // longjmp() and throw() must not violate the entry/exit criteria.
7717 CS->getCapturedDecl()->setNothrow();
7718 }
Kelvin Lia579b912016-07-14 02:54:56 +00007719
7720 OMPLoopDirective::HelperExprs B;
7721 // In presence of clause 'collapse' or 'ordered' with number of loops, it will
7722 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007723 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lia579b912016-07-14 02:54:56 +00007724 OMPD_target_parallel_for_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev5d7edca2017-11-09 17:32:15 +00007725 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Lia579b912016-07-14 02:54:56 +00007726 VarsWithImplicitDSA, B);
7727 if (NestedLoopCount == 0)
7728 return StmtError();
7729
7730 assert((CurContext->isDependentContext() || B.builtAll()) &&
7731 "omp target parallel for simd loop exprs were not built");
7732
7733 if (!CurContext->isDependentContext()) {
7734 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007735 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007736 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Lia579b912016-07-14 02:54:56 +00007737 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7738 B.NumIterations, *this, CurScope,
7739 DSAStack))
7740 return StmtError();
7741 }
7742 }
Kelvin Lic5609492016-07-15 04:39:07 +00007743 if (checkSimdlenSafelenSpecified(*this, Clauses))
Kelvin Lia579b912016-07-14 02:54:56 +00007744 return StmtError();
7745
Reid Kleckner87a31802018-03-12 21:43:02 +00007746 setFunctionHasBranchProtectedScope();
Kelvin Lia579b912016-07-14 02:54:56 +00007747 return OMPTargetParallelForSimdDirective::Create(
7748 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7749}
7750
Kelvin Li986330c2016-07-20 22:57:10 +00007751StmtResult Sema::ActOnOpenMPTargetSimdDirective(
7752 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007753 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li986330c2016-07-20 22:57:10 +00007754 if (!AStmt)
7755 return StmtError();
7756
Alexey Bataeve3727102018-04-18 15:57:46 +00007757 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li986330c2016-07-20 22:57:10 +00007758 // 1.2.2 OpenMP Language Terminology
7759 // Structured block - An executable statement with a single entry at the
7760 // top and a single exit at the bottom.
7761 // The point of exit cannot be a branch out of the structured block.
7762 // longjmp() and throw() must not violate the entry/exit criteria.
7763 CS->getCapturedDecl()->setNothrow();
Alexey Bataevf8365372017-11-17 17:57:25 +00007764 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_simd);
7765 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7766 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7767 // 1.2.2 OpenMP Language Terminology
7768 // Structured block - An executable statement with a single entry at the
7769 // top and a single exit at the bottom.
7770 // The point of exit cannot be a branch out of the structured block.
7771 // longjmp() and throw() must not violate the entry/exit criteria.
7772 CS->getCapturedDecl()->setNothrow();
7773 }
7774
Kelvin Li986330c2016-07-20 22:57:10 +00007775 OMPLoopDirective::HelperExprs B;
7776 // In presence of clause 'collapse' with number of loops, it will define the
7777 // nested loops number.
David Majnemer9d168222016-08-05 17:44:54 +00007778 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007779 checkOpenMPLoop(OMPD_target_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevf8365372017-11-17 17:57:25 +00007780 getOrderedNumberExpr(Clauses), CS, *this, *DSAStack,
Kelvin Li986330c2016-07-20 22:57:10 +00007781 VarsWithImplicitDSA, B);
7782 if (NestedLoopCount == 0)
7783 return StmtError();
7784
7785 assert((CurContext->isDependentContext() || B.builtAll()) &&
7786 "omp target simd loop exprs were not built");
7787
7788 if (!CurContext->isDependentContext()) {
7789 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007790 for (OMPClause *C : Clauses) {
David Majnemer9d168222016-08-05 17:44:54 +00007791 if (auto *LC = dyn_cast<OMPLinearClause>(C))
Kelvin Li986330c2016-07-20 22:57:10 +00007792 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7793 B.NumIterations, *this, CurScope,
7794 DSAStack))
7795 return StmtError();
7796 }
7797 }
7798
7799 if (checkSimdlenSafelenSpecified(*this, Clauses))
7800 return StmtError();
7801
Reid Kleckner87a31802018-03-12 21:43:02 +00007802 setFunctionHasBranchProtectedScope();
Kelvin Li986330c2016-07-20 22:57:10 +00007803 return OMPTargetSimdDirective::Create(Context, StartLoc, EndLoc,
7804 NestedLoopCount, Clauses, AStmt, B);
7805}
7806
Kelvin Li02532872016-08-05 14:37:37 +00007807StmtResult Sema::ActOnOpenMPTeamsDistributeDirective(
7808 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007809 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li02532872016-08-05 14:37:37 +00007810 if (!AStmt)
7811 return StmtError();
7812
Alexey Bataeve3727102018-04-18 15:57:46 +00007813 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li02532872016-08-05 14:37:37 +00007814 // 1.2.2 OpenMP Language Terminology
7815 // Structured block - An executable statement with a single entry at the
7816 // top and a single exit at the bottom.
7817 // The point of exit cannot be a branch out of the structured block.
7818 // longjmp() and throw() must not violate the entry/exit criteria.
7819 CS->getCapturedDecl()->setNothrow();
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007820 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_teams_distribute);
7821 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7822 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7823 // 1.2.2 OpenMP Language Terminology
7824 // Structured block - An executable statement with a single entry at the
7825 // top and a single exit at the bottom.
7826 // The point of exit cannot be a branch out of the structured block.
7827 // longjmp() and throw() must not violate the entry/exit criteria.
7828 CS->getCapturedDecl()->setNothrow();
7829 }
Kelvin Li02532872016-08-05 14:37:37 +00007830
7831 OMPLoopDirective::HelperExprs B;
7832 // In presence of clause 'collapse' with number of loops, it will
7833 // define the nested loops number.
7834 unsigned NestedLoopCount =
Alexey Bataeve3727102018-04-18 15:57:46 +00007835 checkOpenMPLoop(OMPD_teams_distribute, getCollapseNumberExpr(Clauses),
Alexey Bataev95c6dd42017-11-29 15:14:16 +00007836 nullptr /*ordered not a clause on distribute*/, CS, *this,
7837 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li02532872016-08-05 14:37:37 +00007838 if (NestedLoopCount == 0)
7839 return StmtError();
7840
7841 assert((CurContext->isDependentContext() || B.builtAll()) &&
7842 "omp teams distribute loop exprs were not built");
7843
Reid Kleckner87a31802018-03-12 21:43:02 +00007844 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007845
7846 DSAStack->setParentTeamsRegionLoc(StartLoc);
7847
David Majnemer9d168222016-08-05 17:44:54 +00007848 return OMPTeamsDistributeDirective::Create(
7849 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
Kelvin Li02532872016-08-05 14:37:37 +00007850}
7851
Kelvin Li4e325f72016-10-25 12:50:55 +00007852StmtResult Sema::ActOnOpenMPTeamsDistributeSimdDirective(
7853 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007854 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007855 if (!AStmt)
7856 return StmtError();
7857
Alexey Bataeve3727102018-04-18 15:57:46 +00007858 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li4e325f72016-10-25 12:50:55 +00007859 // 1.2.2 OpenMP Language Terminology
7860 // Structured block - An executable statement with a single entry at the
7861 // top and a single exit at the bottom.
7862 // The point of exit cannot be a branch out of the structured block.
7863 // longjmp() and throw() must not violate the entry/exit criteria.
7864 CS->getCapturedDecl()->setNothrow();
Alexey Bataev999277a2017-12-06 14:31:09 +00007865 for (int ThisCaptureLevel =
7866 getOpenMPCaptureLevels(OMPD_teams_distribute_simd);
7867 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7868 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7869 // 1.2.2 OpenMP Language Terminology
7870 // Structured block - An executable statement with a single entry at the
7871 // top and a single exit at the bottom.
7872 // The point of exit cannot be a branch out of the structured block.
7873 // longjmp() and throw() must not violate the entry/exit criteria.
7874 CS->getCapturedDecl()->setNothrow();
7875 }
7876
Kelvin Li4e325f72016-10-25 12:50:55 +00007877
7878 OMPLoopDirective::HelperExprs B;
7879 // In presence of clause 'collapse' with number of loops, it will
7880 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007881 unsigned NestedLoopCount = checkOpenMPLoop(
Samuel Antao4c8035b2016-12-12 18:00:20 +00007882 OMPD_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataev999277a2017-12-06 14:31:09 +00007883 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Samuel Antao4c8035b2016-12-12 18:00:20 +00007884 VarsWithImplicitDSA, B);
Kelvin Li4e325f72016-10-25 12:50:55 +00007885
7886 if (NestedLoopCount == 0)
7887 return StmtError();
7888
7889 assert((CurContext->isDependentContext() || B.builtAll()) &&
7890 "omp teams distribute simd loop exprs were not built");
7891
7892 if (!CurContext->isDependentContext()) {
7893 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007894 for (OMPClause *C : Clauses) {
Kelvin Li4e325f72016-10-25 12:50:55 +00007895 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7896 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7897 B.NumIterations, *this, CurScope,
7898 DSAStack))
7899 return StmtError();
7900 }
7901 }
7902
7903 if (checkSimdlenSafelenSpecified(*this, Clauses))
7904 return StmtError();
7905
Reid Kleckner87a31802018-03-12 21:43:02 +00007906 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007907
7908 DSAStack->setParentTeamsRegionLoc(StartLoc);
7909
Kelvin Li4e325f72016-10-25 12:50:55 +00007910 return OMPTeamsDistributeSimdDirective::Create(
7911 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7912}
7913
Kelvin Li579e41c2016-11-30 23:51:03 +00007914StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForSimdDirective(
7915 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007916 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007917 if (!AStmt)
7918 return StmtError();
7919
Alexey Bataeve3727102018-04-18 15:57:46 +00007920 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li579e41c2016-11-30 23:51:03 +00007921 // 1.2.2 OpenMP Language Terminology
7922 // Structured block - An executable statement with a single entry at the
7923 // top and a single exit at the bottom.
7924 // The point of exit cannot be a branch out of the structured block.
7925 // longjmp() and throw() must not violate the entry/exit criteria.
7926 CS->getCapturedDecl()->setNothrow();
7927
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007928 for (int ThisCaptureLevel =
7929 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for_simd);
7930 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7931 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7932 // 1.2.2 OpenMP Language Terminology
7933 // Structured block - An executable statement with a single entry at the
7934 // top and a single exit at the bottom.
7935 // The point of exit cannot be a branch out of the structured block.
7936 // longjmp() and throw() must not violate the entry/exit criteria.
7937 CS->getCapturedDecl()->setNothrow();
7938 }
7939
Kelvin Li579e41c2016-11-30 23:51:03 +00007940 OMPLoopDirective::HelperExprs B;
7941 // In presence of clause 'collapse' with number of loops, it will
7942 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00007943 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li579e41c2016-11-30 23:51:03 +00007944 OMPD_teams_distribute_parallel_for_simd, getCollapseNumberExpr(Clauses),
Carlo Bertolli56a2aa42017-12-04 20:57:19 +00007945 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li579e41c2016-11-30 23:51:03 +00007946 VarsWithImplicitDSA, B);
7947
7948 if (NestedLoopCount == 0)
7949 return StmtError();
7950
7951 assert((CurContext->isDependentContext() || B.builtAll()) &&
7952 "omp for loop exprs were not built");
7953
7954 if (!CurContext->isDependentContext()) {
7955 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00007956 for (OMPClause *C : Clauses) {
Kelvin Li579e41c2016-11-30 23:51:03 +00007957 if (auto *LC = dyn_cast<OMPLinearClause>(C))
7958 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
7959 B.NumIterations, *this, CurScope,
7960 DSAStack))
7961 return StmtError();
7962 }
7963 }
7964
7965 if (checkSimdlenSafelenSpecified(*this, Clauses))
7966 return StmtError();
7967
Reid Kleckner87a31802018-03-12 21:43:02 +00007968 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00007969
7970 DSAStack->setParentTeamsRegionLoc(StartLoc);
7971
Kelvin Li579e41c2016-11-30 23:51:03 +00007972 return OMPTeamsDistributeParallelForSimdDirective::Create(
7973 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
7974}
7975
Kelvin Li7ade93f2016-12-09 03:24:30 +00007976StmtResult Sema::ActOnOpenMPTeamsDistributeParallelForDirective(
7977 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00007978 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li7ade93f2016-12-09 03:24:30 +00007979 if (!AStmt)
7980 return StmtError();
7981
Alexey Bataeve3727102018-04-18 15:57:46 +00007982 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li7ade93f2016-12-09 03:24:30 +00007983 // 1.2.2 OpenMP Language Terminology
7984 // Structured block - An executable statement with a single entry at the
7985 // top and a single exit at the bottom.
7986 // The point of exit cannot be a branch out of the structured block.
7987 // longjmp() and throw() must not violate the entry/exit criteria.
7988 CS->getCapturedDecl()->setNothrow();
7989
Carlo Bertolli62fae152017-11-20 20:46:39 +00007990 for (int ThisCaptureLevel =
7991 getOpenMPCaptureLevels(OMPD_teams_distribute_parallel_for);
7992 ThisCaptureLevel > 1; --ThisCaptureLevel) {
7993 CS = cast<CapturedStmt>(CS->getCapturedStmt());
7994 // 1.2.2 OpenMP Language Terminology
7995 // Structured block - An executable statement with a single entry at the
7996 // top and a single exit at the bottom.
7997 // The point of exit cannot be a branch out of the structured block.
7998 // longjmp() and throw() must not violate the entry/exit criteria.
7999 CS->getCapturedDecl()->setNothrow();
8000 }
8001
Kelvin Li7ade93f2016-12-09 03:24:30 +00008002 OMPLoopDirective::HelperExprs B;
8003 // In presence of clause 'collapse' with number of loops, it will
8004 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008005 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Li7ade93f2016-12-09 03:24:30 +00008006 OMPD_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
Carlo Bertolli62fae152017-11-20 20:46:39 +00008007 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li7ade93f2016-12-09 03:24:30 +00008008 VarsWithImplicitDSA, B);
8009
8010 if (NestedLoopCount == 0)
8011 return StmtError();
8012
8013 assert((CurContext->isDependentContext() || B.builtAll()) &&
8014 "omp for loop exprs were not built");
8015
Reid Kleckner87a31802018-03-12 21:43:02 +00008016 setFunctionHasBranchProtectedScope();
Alexey Bataevceabd412017-11-30 18:01:54 +00008017
8018 DSAStack->setParentTeamsRegionLoc(StartLoc);
8019
Kelvin Li7ade93f2016-12-09 03:24:30 +00008020 return OMPTeamsDistributeParallelForDirective::Create(
Alexey Bataevdcb4b8fb2017-11-22 20:19:50 +00008021 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8022 DSAStack->isCancelRegion());
Kelvin Li7ade93f2016-12-09 03:24:30 +00008023}
8024
Kelvin Libf594a52016-12-17 05:48:59 +00008025StmtResult Sema::ActOnOpenMPTargetTeamsDirective(ArrayRef<OMPClause *> Clauses,
8026 Stmt *AStmt,
8027 SourceLocation StartLoc,
8028 SourceLocation EndLoc) {
8029 if (!AStmt)
8030 return StmtError();
8031
Alexey Bataeve3727102018-04-18 15:57:46 +00008032 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Libf594a52016-12-17 05:48:59 +00008033 // 1.2.2 OpenMP Language Terminology
8034 // Structured block - An executable statement with a single entry at the
8035 // top and a single exit at the bottom.
8036 // The point of exit cannot be a branch out of the structured block.
8037 // longjmp() and throw() must not violate the entry/exit criteria.
8038 CS->getCapturedDecl()->setNothrow();
8039
Alexey Bataevf9fc42e2017-11-22 14:25:55 +00008040 for (int ThisCaptureLevel = getOpenMPCaptureLevels(OMPD_target_teams);
8041 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8042 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8043 // 1.2.2 OpenMP Language Terminology
8044 // Structured block - An executable statement with a single entry at the
8045 // top and a single exit at the bottom.
8046 // The point of exit cannot be a branch out of the structured block.
8047 // longjmp() and throw() must not violate the entry/exit criteria.
8048 CS->getCapturedDecl()->setNothrow();
8049 }
Reid Kleckner87a31802018-03-12 21:43:02 +00008050 setFunctionHasBranchProtectedScope();
Kelvin Libf594a52016-12-17 05:48:59 +00008051
8052 return OMPTargetTeamsDirective::Create(Context, StartLoc, EndLoc, Clauses,
8053 AStmt);
8054}
8055
Kelvin Li83c451e2016-12-25 04:52:54 +00008056StmtResult Sema::ActOnOpenMPTargetTeamsDistributeDirective(
8057 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008058 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li83c451e2016-12-25 04:52:54 +00008059 if (!AStmt)
8060 return StmtError();
8061
Alexey Bataeve3727102018-04-18 15:57:46 +00008062 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li83c451e2016-12-25 04:52:54 +00008063 // 1.2.2 OpenMP Language Terminology
8064 // Structured block - An executable statement with a single entry at the
8065 // top and a single exit at the bottom.
8066 // The point of exit cannot be a branch out of the structured block.
8067 // longjmp() and throw() must not violate the entry/exit criteria.
8068 CS->getCapturedDecl()->setNothrow();
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008069 for (int ThisCaptureLevel =
8070 getOpenMPCaptureLevels(OMPD_target_teams_distribute);
8071 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8072 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8073 // 1.2.2 OpenMP Language Terminology
8074 // Structured block - An executable statement with a single entry at the
8075 // top and a single exit at the bottom.
8076 // The point of exit cannot be a branch out of the structured block.
8077 // longjmp() and throw() must not violate the entry/exit criteria.
8078 CS->getCapturedDecl()->setNothrow();
8079 }
Kelvin Li83c451e2016-12-25 04:52:54 +00008080
8081 OMPLoopDirective::HelperExprs B;
8082 // In presence of clause 'collapse' with number of loops, it will
8083 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008084 unsigned NestedLoopCount = checkOpenMPLoop(
Alexey Bataevdfa430f2017-12-08 15:03:50 +00008085 OMPD_target_teams_distribute, getCollapseNumberExpr(Clauses),
8086 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li83c451e2016-12-25 04:52:54 +00008087 VarsWithImplicitDSA, B);
8088 if (NestedLoopCount == 0)
8089 return StmtError();
8090
8091 assert((CurContext->isDependentContext() || B.builtAll()) &&
8092 "omp target teams distribute loop exprs were not built");
8093
Reid Kleckner87a31802018-03-12 21:43:02 +00008094 setFunctionHasBranchProtectedScope();
Kelvin Li83c451e2016-12-25 04:52:54 +00008095 return OMPTargetTeamsDistributeDirective::Create(
8096 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8097}
8098
Kelvin Li80e8f562016-12-29 22:16:30 +00008099StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForDirective(
8100 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008101 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li80e8f562016-12-29 22:16:30 +00008102 if (!AStmt)
8103 return StmtError();
8104
Alexey Bataeve3727102018-04-18 15:57:46 +00008105 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li80e8f562016-12-29 22:16:30 +00008106 // 1.2.2 OpenMP Language Terminology
8107 // Structured block - An executable statement with a single entry at the
8108 // top and a single exit at the bottom.
8109 // The point of exit cannot be a branch out of the structured block.
8110 // longjmp() and throw() must not violate the entry/exit criteria.
8111 CS->getCapturedDecl()->setNothrow();
Carlo Bertolli52978c32018-01-03 21:12:44 +00008112 for (int ThisCaptureLevel =
8113 getOpenMPCaptureLevels(OMPD_target_teams_distribute_parallel_for);
8114 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8115 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8116 // 1.2.2 OpenMP Language Terminology
8117 // Structured block - An executable statement with a single entry at the
8118 // top and a single exit at the bottom.
8119 // The point of exit cannot be a branch out of the structured block.
8120 // longjmp() and throw() must not violate the entry/exit criteria.
8121 CS->getCapturedDecl()->setNothrow();
8122 }
8123
Kelvin Li80e8f562016-12-29 22:16:30 +00008124 OMPLoopDirective::HelperExprs B;
8125 // In presence of clause 'collapse' with number of loops, it will
8126 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008127 unsigned NestedLoopCount = checkOpenMPLoop(
Carlo Bertolli52978c32018-01-03 21:12:44 +00008128 OMPD_target_teams_distribute_parallel_for, getCollapseNumberExpr(Clauses),
8129 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Li80e8f562016-12-29 22:16:30 +00008130 VarsWithImplicitDSA, B);
8131 if (NestedLoopCount == 0)
8132 return StmtError();
8133
8134 assert((CurContext->isDependentContext() || B.builtAll()) &&
8135 "omp target teams distribute parallel for loop exprs were not built");
8136
Alexey Bataev647dd842018-01-15 20:59:40 +00008137 if (!CurContext->isDependentContext()) {
8138 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008139 for (OMPClause *C : Clauses) {
Alexey Bataev647dd842018-01-15 20:59:40 +00008140 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8141 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8142 B.NumIterations, *this, CurScope,
8143 DSAStack))
8144 return StmtError();
8145 }
8146 }
8147
Reid Kleckner87a31802018-03-12 21:43:02 +00008148 setFunctionHasBranchProtectedScope();
Kelvin Li80e8f562016-12-29 22:16:30 +00008149 return OMPTargetTeamsDistributeParallelForDirective::Create(
Alexey Bataev16e79882017-11-22 21:12:03 +00008150 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B,
8151 DSAStack->isCancelRegion());
Kelvin Li80e8f562016-12-29 22:16:30 +00008152}
8153
Kelvin Li1851df52017-01-03 05:23:48 +00008154StmtResult Sema::ActOnOpenMPTargetTeamsDistributeParallelForSimdDirective(
8155 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008156 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Li1851df52017-01-03 05:23:48 +00008157 if (!AStmt)
8158 return StmtError();
8159
Alexey Bataeve3727102018-04-18 15:57:46 +00008160 auto *CS = cast<CapturedStmt>(AStmt);
Kelvin Li1851df52017-01-03 05:23:48 +00008161 // 1.2.2 OpenMP Language Terminology
8162 // Structured block - An executable statement with a single entry at the
8163 // top and a single exit at the bottom.
8164 // The point of exit cannot be a branch out of the structured block.
8165 // longjmp() and throw() must not violate the entry/exit criteria.
8166 CS->getCapturedDecl()->setNothrow();
Alexey Bataev647dd842018-01-15 20:59:40 +00008167 for (int ThisCaptureLevel = getOpenMPCaptureLevels(
8168 OMPD_target_teams_distribute_parallel_for_simd);
8169 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8170 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8171 // 1.2.2 OpenMP Language Terminology
8172 // Structured block - An executable statement with a single entry at the
8173 // top and a single exit at the bottom.
8174 // The point of exit cannot be a branch out of the structured block.
8175 // longjmp() and throw() must not violate the entry/exit criteria.
8176 CS->getCapturedDecl()->setNothrow();
8177 }
Kelvin Li1851df52017-01-03 05:23:48 +00008178
8179 OMPLoopDirective::HelperExprs B;
8180 // In presence of clause 'collapse' with number of loops, it will
8181 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008182 unsigned NestedLoopCount =
8183 checkOpenMPLoop(OMPD_target_teams_distribute_parallel_for_simd,
Alexey Bataev647dd842018-01-15 20:59:40 +00008184 getCollapseNumberExpr(Clauses),
8185 nullptr /*ordered not a clause on distribute*/, CS, *this,
8186 *DSAStack, VarsWithImplicitDSA, B);
Kelvin Li1851df52017-01-03 05:23:48 +00008187 if (NestedLoopCount == 0)
8188 return StmtError();
8189
8190 assert((CurContext->isDependentContext() || B.builtAll()) &&
8191 "omp target teams distribute parallel for simd loop exprs were not "
8192 "built");
8193
8194 if (!CurContext->isDependentContext()) {
8195 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008196 for (OMPClause *C : Clauses) {
Kelvin Li1851df52017-01-03 05:23:48 +00008197 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8198 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8199 B.NumIterations, *this, CurScope,
8200 DSAStack))
8201 return StmtError();
8202 }
8203 }
8204
Alexey Bataev438388c2017-11-22 18:34:02 +00008205 if (checkSimdlenSafelenSpecified(*this, Clauses))
8206 return StmtError();
8207
Reid Kleckner87a31802018-03-12 21:43:02 +00008208 setFunctionHasBranchProtectedScope();
Kelvin Li1851df52017-01-03 05:23:48 +00008209 return OMPTargetTeamsDistributeParallelForSimdDirective::Create(
8210 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8211}
8212
Kelvin Lida681182017-01-10 18:08:18 +00008213StmtResult Sema::ActOnOpenMPTargetTeamsDistributeSimdDirective(
8214 ArrayRef<OMPClause *> Clauses, Stmt *AStmt, SourceLocation StartLoc,
Alexey Bataeve3727102018-04-18 15:57:46 +00008215 SourceLocation EndLoc, VarsWithInheritedDSAType &VarsWithImplicitDSA) {
Kelvin Lida681182017-01-10 18:08:18 +00008216 if (!AStmt)
8217 return StmtError();
8218
8219 auto *CS = cast<CapturedStmt>(AStmt);
8220 // 1.2.2 OpenMP Language Terminology
8221 // Structured block - An executable statement with a single entry at the
8222 // top and a single exit at the bottom.
8223 // The point of exit cannot be a branch out of the structured block.
8224 // longjmp() and throw() must not violate the entry/exit criteria.
8225 CS->getCapturedDecl()->setNothrow();
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008226 for (int ThisCaptureLevel =
8227 getOpenMPCaptureLevels(OMPD_target_teams_distribute_simd);
8228 ThisCaptureLevel > 1; --ThisCaptureLevel) {
8229 CS = cast<CapturedStmt>(CS->getCapturedStmt());
8230 // 1.2.2 OpenMP Language Terminology
8231 // Structured block - An executable statement with a single entry at the
8232 // top and a single exit at the bottom.
8233 // The point of exit cannot be a branch out of the structured block.
8234 // longjmp() and throw() must not violate the entry/exit criteria.
8235 CS->getCapturedDecl()->setNothrow();
8236 }
Kelvin Lida681182017-01-10 18:08:18 +00008237
8238 OMPLoopDirective::HelperExprs B;
8239 // In presence of clause 'collapse' with number of loops, it will
8240 // define the nested loops number.
Alexey Bataeve3727102018-04-18 15:57:46 +00008241 unsigned NestedLoopCount = checkOpenMPLoop(
Kelvin Lida681182017-01-10 18:08:18 +00008242 OMPD_target_teams_distribute_simd, getCollapseNumberExpr(Clauses),
Alexey Bataevfbe17fb2017-12-13 19:45:06 +00008243 nullptr /*ordered not a clause on distribute*/, CS, *this, *DSAStack,
Kelvin Lida681182017-01-10 18:08:18 +00008244 VarsWithImplicitDSA, B);
8245 if (NestedLoopCount == 0)
8246 return StmtError();
8247
8248 assert((CurContext->isDependentContext() || B.builtAll()) &&
8249 "omp target teams distribute simd loop exprs were not built");
8250
Alexey Bataev438388c2017-11-22 18:34:02 +00008251 if (!CurContext->isDependentContext()) {
8252 // Finalize the clauses that need pre-built expressions for CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +00008253 for (OMPClause *C : Clauses) {
Alexey Bataev438388c2017-11-22 18:34:02 +00008254 if (auto *LC = dyn_cast<OMPLinearClause>(C))
8255 if (FinishOpenMPLinearClause(*LC, cast<DeclRefExpr>(B.IterationVarRef),
8256 B.NumIterations, *this, CurScope,
8257 DSAStack))
8258 return StmtError();
8259 }
8260 }
8261
8262 if (checkSimdlenSafelenSpecified(*this, Clauses))
8263 return StmtError();
8264
Reid Kleckner87a31802018-03-12 21:43:02 +00008265 setFunctionHasBranchProtectedScope();
Kelvin Lida681182017-01-10 18:08:18 +00008266 return OMPTargetTeamsDistributeSimdDirective::Create(
8267 Context, StartLoc, EndLoc, NestedLoopCount, Clauses, AStmt, B);
8268}
8269
Alexey Bataeved09d242014-05-28 05:53:51 +00008270OMPClause *Sema::ActOnOpenMPSingleExprClause(OpenMPClauseKind Kind, Expr *Expr,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008271 SourceLocation StartLoc,
8272 SourceLocation LParenLoc,
8273 SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008274 OMPClause *Res = nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008275 switch (Kind) {
Alexey Bataev3778b602014-07-17 07:32:53 +00008276 case OMPC_final:
8277 Res = ActOnOpenMPFinalClause(Expr, StartLoc, LParenLoc, EndLoc);
8278 break;
Alexey Bataev568a8332014-03-06 06:15:19 +00008279 case OMPC_num_threads:
8280 Res = ActOnOpenMPNumThreadsClause(Expr, StartLoc, LParenLoc, EndLoc);
8281 break;
Alexey Bataev62c87d22014-03-21 04:51:18 +00008282 case OMPC_safelen:
8283 Res = ActOnOpenMPSafelenClause(Expr, StartLoc, LParenLoc, EndLoc);
8284 break;
Alexey Bataev66b15b52015-08-21 11:14:16 +00008285 case OMPC_simdlen:
8286 Res = ActOnOpenMPSimdlenClause(Expr, StartLoc, LParenLoc, EndLoc);
8287 break;
Alexander Musman8bd31e62014-05-27 15:12:19 +00008288 case OMPC_collapse:
8289 Res = ActOnOpenMPCollapseClause(Expr, StartLoc, LParenLoc, EndLoc);
8290 break;
Alexey Bataev10e775f2015-07-30 11:36:16 +00008291 case OMPC_ordered:
8292 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc, LParenLoc, Expr);
8293 break;
Michael Wonge710d542015-08-07 16:16:36 +00008294 case OMPC_device:
8295 Res = ActOnOpenMPDeviceClause(Expr, StartLoc, LParenLoc, EndLoc);
8296 break;
Kelvin Li099bb8c2015-11-24 20:50:12 +00008297 case OMPC_num_teams:
8298 Res = ActOnOpenMPNumTeamsClause(Expr, StartLoc, LParenLoc, EndLoc);
8299 break;
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008300 case OMPC_thread_limit:
8301 Res = ActOnOpenMPThreadLimitClause(Expr, StartLoc, LParenLoc, EndLoc);
8302 break;
Alexey Bataeva0569352015-12-01 10:17:31 +00008303 case OMPC_priority:
8304 Res = ActOnOpenMPPriorityClause(Expr, StartLoc, LParenLoc, EndLoc);
8305 break;
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00008306 case OMPC_grainsize:
8307 Res = ActOnOpenMPGrainsizeClause(Expr, StartLoc, LParenLoc, EndLoc);
8308 break;
Alexey Bataev382967a2015-12-08 12:06:20 +00008309 case OMPC_num_tasks:
8310 Res = ActOnOpenMPNumTasksClause(Expr, StartLoc, LParenLoc, EndLoc);
8311 break;
Alexey Bataev28c75412015-12-15 08:19:24 +00008312 case OMPC_hint:
8313 Res = ActOnOpenMPHintClause(Expr, StartLoc, LParenLoc, EndLoc);
8314 break;
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008315 case OMPC_if:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008316 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00008317 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00008318 case OMPC_schedule:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008319 case OMPC_private:
8320 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00008321 case OMPC_lastprivate:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008322 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00008323 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008324 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008325 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00008326 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00008327 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00008328 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00008329 case OMPC_copyprivate:
Alexey Bataev236070f2014-06-20 11:19:47 +00008330 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00008331 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00008332 case OMPC_mergeable:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008333 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00008334 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00008335 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00008336 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00008337 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00008338 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00008339 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00008340 case OMPC_depend:
Alexey Bataev346265e2015-09-25 10:37:12 +00008341 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00008342 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00008343 case OMPC_map:
Alexey Bataevb825de12015-12-07 10:51:44 +00008344 case OMPC_nogroup:
Carlo Bertollib4adf552016-01-15 18:50:31 +00008345 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00008346 case OMPC_defaultmap:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008347 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00008348 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00008349 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00008350 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00008351 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00008352 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008353 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008354 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008355 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008356 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008357 case OMPC_atomic_default_mem_order:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008358 llvm_unreachable("Clause is not allowed.");
8359 }
8360 return Res;
8361}
8362
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008363// An OpenMP directive such as 'target parallel' has two captured regions:
8364// for the 'target' and 'parallel' respectively. This function returns
8365// the region in which to capture expressions associated with a clause.
8366// A return value of OMPD_unknown signifies that the expression should not
8367// be captured.
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008368static OpenMPDirectiveKind getOpenMPCaptureRegionForClause(
8369 OpenMPDirectiveKind DKind, OpenMPClauseKind CKind,
8370 OpenMPDirectiveKind NameModifier = OMPD_unknown) {
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008371 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008372 switch (CKind) {
8373 case OMPC_if:
8374 switch (DKind) {
8375 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008376 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008377 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008378 // If this clause applies to the nested 'parallel' region, capture within
8379 // the 'target' region, otherwise do not capture.
8380 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8381 CaptureRegion = OMPD_target;
8382 break;
Carlo Bertolli52978c32018-01-03 21:12:44 +00008383 case OMPD_target_teams_distribute_parallel_for:
8384 case OMPD_target_teams_distribute_parallel_for_simd:
8385 // If this clause applies to the nested 'parallel' region, capture within
8386 // the 'teams' region, otherwise do not capture.
8387 if (NameModifier == OMPD_unknown || NameModifier == OMPD_parallel)
8388 CaptureRegion = OMPD_teams;
8389 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008390 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008391 case OMPD_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008392 CaptureRegion = OMPD_teams;
8393 break;
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008394 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008395 case OMPD_target_enter_data:
8396 case OMPD_target_exit_data:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008397 CaptureRegion = OMPD_task;
8398 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008399 case OMPD_cancel:
8400 case OMPD_parallel:
8401 case OMPD_parallel_sections:
8402 case OMPD_parallel_for:
8403 case OMPD_parallel_for_simd:
8404 case OMPD_target:
8405 case OMPD_target_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008406 case OMPD_target_teams:
8407 case OMPD_target_teams_distribute:
8408 case OMPD_target_teams_distribute_simd:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008409 case OMPD_distribute_parallel_for:
8410 case OMPD_distribute_parallel_for_simd:
8411 case OMPD_task:
8412 case OMPD_taskloop:
8413 case OMPD_taskloop_simd:
8414 case OMPD_target_data:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008415 // Do not capture if-clause expressions.
8416 break;
8417 case OMPD_threadprivate:
8418 case OMPD_taskyield:
8419 case OMPD_barrier:
8420 case OMPD_taskwait:
8421 case OMPD_cancellation_point:
8422 case OMPD_flush:
8423 case OMPD_declare_reduction:
8424 case OMPD_declare_simd:
8425 case OMPD_declare_target:
8426 case OMPD_end_declare_target:
8427 case OMPD_teams:
8428 case OMPD_simd:
8429 case OMPD_for:
8430 case OMPD_for_simd:
8431 case OMPD_sections:
8432 case OMPD_section:
8433 case OMPD_single:
8434 case OMPD_master:
8435 case OMPD_critical:
8436 case OMPD_taskgroup:
8437 case OMPD_distribute:
8438 case OMPD_ordered:
8439 case OMPD_atomic:
8440 case OMPD_distribute_simd:
8441 case OMPD_teams_distribute:
8442 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008443 case OMPD_requires:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008444 llvm_unreachable("Unexpected OpenMP directive with if-clause");
8445 case OMPD_unknown:
8446 llvm_unreachable("Unknown OpenMP directive");
8447 }
8448 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008449 case OMPC_num_threads:
8450 switch (DKind) {
8451 case OMPD_target_parallel:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008452 case OMPD_target_parallel_for:
Alexey Bataev5d7edca2017-11-09 17:32:15 +00008453 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008454 CaptureRegion = OMPD_target;
8455 break;
Carlo Bertolli62fae152017-11-20 20:46:39 +00008456 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008457 case OMPD_teams_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008458 case OMPD_target_teams_distribute_parallel_for:
8459 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008460 CaptureRegion = OMPD_teams;
8461 break;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008462 case OMPD_parallel:
8463 case OMPD_parallel_sections:
8464 case OMPD_parallel_for:
8465 case OMPD_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008466 case OMPD_distribute_parallel_for:
8467 case OMPD_distribute_parallel_for_simd:
8468 // Do not capture num_threads-clause expressions.
8469 break;
8470 case OMPD_target_data:
8471 case OMPD_target_enter_data:
8472 case OMPD_target_exit_data:
8473 case OMPD_target_update:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008474 case OMPD_target:
8475 case OMPD_target_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008476 case OMPD_target_teams:
8477 case OMPD_target_teams_distribute:
8478 case OMPD_target_teams_distribute_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008479 case OMPD_cancel:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008480 case OMPD_task:
8481 case OMPD_taskloop:
8482 case OMPD_taskloop_simd:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008483 case OMPD_threadprivate:
8484 case OMPD_taskyield:
8485 case OMPD_barrier:
8486 case OMPD_taskwait:
8487 case OMPD_cancellation_point:
8488 case OMPD_flush:
8489 case OMPD_declare_reduction:
8490 case OMPD_declare_simd:
8491 case OMPD_declare_target:
8492 case OMPD_end_declare_target:
8493 case OMPD_teams:
8494 case OMPD_simd:
8495 case OMPD_for:
8496 case OMPD_for_simd:
8497 case OMPD_sections:
8498 case OMPD_section:
8499 case OMPD_single:
8500 case OMPD_master:
8501 case OMPD_critical:
8502 case OMPD_taskgroup:
8503 case OMPD_distribute:
8504 case OMPD_ordered:
8505 case OMPD_atomic:
8506 case OMPD_distribute_simd:
8507 case OMPD_teams_distribute:
8508 case OMPD_teams_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008509 case OMPD_requires:
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00008510 llvm_unreachable("Unexpected OpenMP directive with num_threads-clause");
8511 case OMPD_unknown:
8512 llvm_unreachable("Unknown OpenMP directive");
8513 }
8514 break;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008515 case OMPC_num_teams:
8516 switch (DKind) {
8517 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008518 case OMPD_target_teams_distribute:
8519 case OMPD_target_teams_distribute_simd:
8520 case OMPD_target_teams_distribute_parallel_for:
8521 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008522 CaptureRegion = OMPD_target;
8523 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008524 case OMPD_teams_distribute_parallel_for:
8525 case OMPD_teams_distribute_parallel_for_simd:
8526 case OMPD_teams:
8527 case OMPD_teams_distribute:
8528 case OMPD_teams_distribute_simd:
8529 // Do not capture num_teams-clause expressions.
8530 break;
8531 case OMPD_distribute_parallel_for:
8532 case OMPD_distribute_parallel_for_simd:
8533 case OMPD_task:
8534 case OMPD_taskloop:
8535 case OMPD_taskloop_simd:
8536 case OMPD_target_data:
8537 case OMPD_target_enter_data:
8538 case OMPD_target_exit_data:
8539 case OMPD_target_update:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008540 case OMPD_cancel:
8541 case OMPD_parallel:
8542 case OMPD_parallel_sections:
8543 case OMPD_parallel_for:
8544 case OMPD_parallel_for_simd:
8545 case OMPD_target:
8546 case OMPD_target_simd:
8547 case OMPD_target_parallel:
8548 case OMPD_target_parallel_for:
8549 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008550 case OMPD_threadprivate:
8551 case OMPD_taskyield:
8552 case OMPD_barrier:
8553 case OMPD_taskwait:
8554 case OMPD_cancellation_point:
8555 case OMPD_flush:
8556 case OMPD_declare_reduction:
8557 case OMPD_declare_simd:
8558 case OMPD_declare_target:
8559 case OMPD_end_declare_target:
8560 case OMPD_simd:
8561 case OMPD_for:
8562 case OMPD_for_simd:
8563 case OMPD_sections:
8564 case OMPD_section:
8565 case OMPD_single:
8566 case OMPD_master:
8567 case OMPD_critical:
8568 case OMPD_taskgroup:
8569 case OMPD_distribute:
8570 case OMPD_ordered:
8571 case OMPD_atomic:
8572 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008573 case OMPD_requires:
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +00008574 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8575 case OMPD_unknown:
8576 llvm_unreachable("Unknown OpenMP directive");
8577 }
8578 break;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008579 case OMPC_thread_limit:
8580 switch (DKind) {
8581 case OMPD_target_teams:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008582 case OMPD_target_teams_distribute:
8583 case OMPD_target_teams_distribute_simd:
8584 case OMPD_target_teams_distribute_parallel_for:
8585 case OMPD_target_teams_distribute_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008586 CaptureRegion = OMPD_target;
8587 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008588 case OMPD_teams_distribute_parallel_for:
8589 case OMPD_teams_distribute_parallel_for_simd:
8590 case OMPD_teams:
8591 case OMPD_teams_distribute:
8592 case OMPD_teams_distribute_simd:
8593 // Do not capture thread_limit-clause expressions.
8594 break;
8595 case OMPD_distribute_parallel_for:
8596 case OMPD_distribute_parallel_for_simd:
8597 case OMPD_task:
8598 case OMPD_taskloop:
8599 case OMPD_taskloop_simd:
8600 case OMPD_target_data:
8601 case OMPD_target_enter_data:
8602 case OMPD_target_exit_data:
8603 case OMPD_target_update:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008604 case OMPD_cancel:
8605 case OMPD_parallel:
8606 case OMPD_parallel_sections:
8607 case OMPD_parallel_for:
8608 case OMPD_parallel_for_simd:
8609 case OMPD_target:
8610 case OMPD_target_simd:
8611 case OMPD_target_parallel:
8612 case OMPD_target_parallel_for:
8613 case OMPD_target_parallel_for_simd:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008614 case OMPD_threadprivate:
8615 case OMPD_taskyield:
8616 case OMPD_barrier:
8617 case OMPD_taskwait:
8618 case OMPD_cancellation_point:
8619 case OMPD_flush:
8620 case OMPD_declare_reduction:
8621 case OMPD_declare_simd:
8622 case OMPD_declare_target:
8623 case OMPD_end_declare_target:
8624 case OMPD_simd:
8625 case OMPD_for:
8626 case OMPD_for_simd:
8627 case OMPD_sections:
8628 case OMPD_section:
8629 case OMPD_single:
8630 case OMPD_master:
8631 case OMPD_critical:
8632 case OMPD_taskgroup:
8633 case OMPD_distribute:
8634 case OMPD_ordered:
8635 case OMPD_atomic:
8636 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008637 case OMPD_requires:
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +00008638 llvm_unreachable("Unexpected OpenMP directive with thread_limit-clause");
8639 case OMPD_unknown:
8640 llvm_unreachable("Unknown OpenMP directive");
8641 }
8642 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008643 case OMPC_schedule:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008644 switch (DKind) {
Alexey Bataev2ba67042017-11-28 21:11:44 +00008645 case OMPD_parallel_for:
8646 case OMPD_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008647 case OMPD_distribute_parallel_for:
Alexey Bataev974acd62017-11-27 19:38:52 +00008648 case OMPD_distribute_parallel_for_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008649 case OMPD_teams_distribute_parallel_for:
8650 case OMPD_teams_distribute_parallel_for_simd:
8651 case OMPD_target_parallel_for:
8652 case OMPD_target_parallel_for_simd:
8653 case OMPD_target_teams_distribute_parallel_for:
8654 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev7f96c372017-11-22 17:19:31 +00008655 CaptureRegion = OMPD_parallel;
8656 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008657 case OMPD_for:
8658 case OMPD_for_simd:
8659 // Do not capture schedule-clause expressions.
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008660 break;
8661 case OMPD_task:
8662 case OMPD_taskloop:
8663 case OMPD_taskloop_simd:
8664 case OMPD_target_data:
8665 case OMPD_target_enter_data:
8666 case OMPD_target_exit_data:
8667 case OMPD_target_update:
8668 case OMPD_teams:
8669 case OMPD_teams_distribute:
8670 case OMPD_teams_distribute_simd:
8671 case OMPD_target_teams_distribute:
8672 case OMPD_target_teams_distribute_simd:
8673 case OMPD_target:
8674 case OMPD_target_simd:
8675 case OMPD_target_parallel:
8676 case OMPD_cancel:
8677 case OMPD_parallel:
8678 case OMPD_parallel_sections:
8679 case OMPD_threadprivate:
8680 case OMPD_taskyield:
8681 case OMPD_barrier:
8682 case OMPD_taskwait:
8683 case OMPD_cancellation_point:
8684 case OMPD_flush:
8685 case OMPD_declare_reduction:
8686 case OMPD_declare_simd:
8687 case OMPD_declare_target:
8688 case OMPD_end_declare_target:
8689 case OMPD_simd:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008690 case OMPD_sections:
8691 case OMPD_section:
8692 case OMPD_single:
8693 case OMPD_master:
8694 case OMPD_critical:
8695 case OMPD_taskgroup:
8696 case OMPD_distribute:
8697 case OMPD_ordered:
8698 case OMPD_atomic:
8699 case OMPD_distribute_simd:
8700 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008701 case OMPD_requires:
Alexey Bataevfb0ebec2017-11-08 20:16:14 +00008702 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8703 case OMPD_unknown:
8704 llvm_unreachable("Unknown OpenMP directive");
8705 }
8706 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008707 case OMPC_dist_schedule:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008708 switch (DKind) {
8709 case OMPD_teams_distribute_parallel_for:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008710 case OMPD_teams_distribute_parallel_for_simd:
8711 case OMPD_teams_distribute:
8712 case OMPD_teams_distribute_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008713 case OMPD_target_teams_distribute_parallel_for:
8714 case OMPD_target_teams_distribute_parallel_for_simd:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008715 case OMPD_target_teams_distribute:
8716 case OMPD_target_teams_distribute_simd:
Alexey Bataevfd9b2af2018-01-04 20:50:08 +00008717 CaptureRegion = OMPD_teams;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008718 break;
8719 case OMPD_distribute_parallel_for:
8720 case OMPD_distribute_parallel_for_simd:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008721 case OMPD_distribute:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008722 case OMPD_distribute_simd:
8723 // Do not capture thread_limit-clause expressions.
8724 break;
8725 case OMPD_parallel_for:
8726 case OMPD_parallel_for_simd:
8727 case OMPD_target_parallel_for_simd:
8728 case OMPD_target_parallel_for:
8729 case OMPD_task:
8730 case OMPD_taskloop:
8731 case OMPD_taskloop_simd:
8732 case OMPD_target_data:
8733 case OMPD_target_enter_data:
8734 case OMPD_target_exit_data:
8735 case OMPD_target_update:
8736 case OMPD_teams:
8737 case OMPD_target:
8738 case OMPD_target_simd:
8739 case OMPD_target_parallel:
8740 case OMPD_cancel:
8741 case OMPD_parallel:
8742 case OMPD_parallel_sections:
8743 case OMPD_threadprivate:
8744 case OMPD_taskyield:
8745 case OMPD_barrier:
8746 case OMPD_taskwait:
8747 case OMPD_cancellation_point:
8748 case OMPD_flush:
8749 case OMPD_declare_reduction:
8750 case OMPD_declare_simd:
8751 case OMPD_declare_target:
8752 case OMPD_end_declare_target:
8753 case OMPD_simd:
8754 case OMPD_for:
8755 case OMPD_for_simd:
8756 case OMPD_sections:
8757 case OMPD_section:
8758 case OMPD_single:
8759 case OMPD_master:
8760 case OMPD_critical:
8761 case OMPD_taskgroup:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008762 case OMPD_ordered:
8763 case OMPD_atomic:
8764 case OMPD_target_teams:
Kelvin Li1408f912018-09-26 04:28:39 +00008765 case OMPD_requires:
Carlo Bertolli62fae152017-11-20 20:46:39 +00008766 llvm_unreachable("Unexpected OpenMP directive with schedule clause");
8767 case OMPD_unknown:
8768 llvm_unreachable("Unknown OpenMP directive");
8769 }
8770 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008771 case OMPC_device:
8772 switch (DKind) {
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008773 case OMPD_target_update:
Alexey Bataevfab20e42017-12-27 18:49:38 +00008774 case OMPD_target_enter_data:
8775 case OMPD_target_exit_data:
Alexey Bataev8451efa2018-01-15 19:06:12 +00008776 case OMPD_target:
Alexey Bataevf41c88f2018-01-16 15:05:16 +00008777 case OMPD_target_simd:
Alexey Bataev0c869ef2018-01-16 15:57:07 +00008778 case OMPD_target_teams:
Alexey Bataev54d5c7d2018-01-16 16:27:49 +00008779 case OMPD_target_parallel:
Alexey Bataev79df7562018-01-16 16:46:46 +00008780 case OMPD_target_teams_distribute:
Alexey Bataev8d16a432018-01-16 17:22:50 +00008781 case OMPD_target_teams_distribute_simd:
Alexey Bataev8ed895512018-01-16 17:41:04 +00008782 case OMPD_target_parallel_for:
Alexey Bataevd60d1ba2018-01-16 17:55:15 +00008783 case OMPD_target_parallel_for_simd:
Alexey Bataev9f9fb0b2018-01-16 19:02:33 +00008784 case OMPD_target_teams_distribute_parallel_for:
Alexey Bataev9350fc32018-01-16 19:18:24 +00008785 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataevd2202ca2017-12-27 17:58:32 +00008786 CaptureRegion = OMPD_task;
8787 break;
Alexey Bataev2ba67042017-11-28 21:11:44 +00008788 case OMPD_target_data:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008789 // Do not capture device-clause expressions.
8790 break;
8791 case OMPD_teams_distribute_parallel_for:
8792 case OMPD_teams_distribute_parallel_for_simd:
8793 case OMPD_teams:
8794 case OMPD_teams_distribute:
8795 case OMPD_teams_distribute_simd:
8796 case OMPD_distribute_parallel_for:
8797 case OMPD_distribute_parallel_for_simd:
8798 case OMPD_task:
8799 case OMPD_taskloop:
8800 case OMPD_taskloop_simd:
8801 case OMPD_cancel:
8802 case OMPD_parallel:
8803 case OMPD_parallel_sections:
8804 case OMPD_parallel_for:
8805 case OMPD_parallel_for_simd:
8806 case OMPD_threadprivate:
8807 case OMPD_taskyield:
8808 case OMPD_barrier:
8809 case OMPD_taskwait:
8810 case OMPD_cancellation_point:
8811 case OMPD_flush:
8812 case OMPD_declare_reduction:
8813 case OMPD_declare_simd:
8814 case OMPD_declare_target:
8815 case OMPD_end_declare_target:
8816 case OMPD_simd:
8817 case OMPD_for:
8818 case OMPD_for_simd:
8819 case OMPD_sections:
8820 case OMPD_section:
8821 case OMPD_single:
8822 case OMPD_master:
8823 case OMPD_critical:
8824 case OMPD_taskgroup:
8825 case OMPD_distribute:
8826 case OMPD_ordered:
8827 case OMPD_atomic:
8828 case OMPD_distribute_simd:
Kelvin Li1408f912018-09-26 04:28:39 +00008829 case OMPD_requires:
Alexey Bataev2ba67042017-11-28 21:11:44 +00008830 llvm_unreachable("Unexpected OpenMP directive with num_teams-clause");
8831 case OMPD_unknown:
8832 llvm_unreachable("Unknown OpenMP directive");
8833 }
8834 break;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008835 case OMPC_firstprivate:
8836 case OMPC_lastprivate:
8837 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00008838 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00008839 case OMPC_in_reduction:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008840 case OMPC_linear:
8841 case OMPC_default:
8842 case OMPC_proc_bind:
8843 case OMPC_final:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008844 case OMPC_safelen:
8845 case OMPC_simdlen:
8846 case OMPC_collapse:
8847 case OMPC_private:
8848 case OMPC_shared:
8849 case OMPC_aligned:
8850 case OMPC_copyin:
8851 case OMPC_copyprivate:
8852 case OMPC_ordered:
8853 case OMPC_nowait:
8854 case OMPC_untied:
8855 case OMPC_mergeable:
8856 case OMPC_threadprivate:
8857 case OMPC_flush:
8858 case OMPC_read:
8859 case OMPC_write:
8860 case OMPC_update:
8861 case OMPC_capture:
8862 case OMPC_seq_cst:
8863 case OMPC_depend:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008864 case OMPC_threads:
8865 case OMPC_simd:
8866 case OMPC_map:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008867 case OMPC_priority:
8868 case OMPC_grainsize:
8869 case OMPC_nogroup:
8870 case OMPC_num_tasks:
8871 case OMPC_hint:
8872 case OMPC_defaultmap:
8873 case OMPC_unknown:
8874 case OMPC_uniform:
8875 case OMPC_to:
8876 case OMPC_from:
8877 case OMPC_use_device_ptr:
8878 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00008879 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00008880 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00008881 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00008882 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00008883 case OMPC_atomic_default_mem_order:
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008884 llvm_unreachable("Unexpected OpenMP clause.");
8885 }
8886 return CaptureRegion;
8887}
8888
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008889OMPClause *Sema::ActOnOpenMPIfClause(OpenMPDirectiveKind NameModifier,
8890 Expr *Condition, SourceLocation StartLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008891 SourceLocation LParenLoc,
Alexey Bataev6b8046a2015-09-03 07:23:48 +00008892 SourceLocation NameModifierLoc,
8893 SourceLocation ColonLoc,
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008894 SourceLocation EndLoc) {
8895 Expr *ValExpr = Condition;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008896 Stmt *HelperValStmt = nullptr;
8897 OpenMPDirectiveKind CaptureRegion = OMPD_unknown;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008898 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8899 !Condition->isInstantiationDependent() &&
8900 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008901 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008902 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00008903 return nullptr;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008904
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008905 ValExpr = Val.get();
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008906
8907 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
8908 CaptureRegion =
8909 getOpenMPCaptureRegionForClause(DKind, OMPC_if, NameModifier);
Alexey Bataev2ba67042017-11-28 21:11:44 +00008910 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00008911 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00008912 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008913 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
8914 HelperValStmt = buildPreInits(Context, Captures);
8915 }
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008916 }
8917
Arpith Chacko Jacobfe4890a2017-01-18 20:40:48 +00008918 return new (Context)
8919 OMPIfClause(NameModifier, ValExpr, HelperValStmt, CaptureRegion, StartLoc,
8920 LParenLoc, NameModifierLoc, ColonLoc, EndLoc);
Alexey Bataevaadd52e2014-02-13 05:29:23 +00008921}
8922
Alexey Bataev3778b602014-07-17 07:32:53 +00008923OMPClause *Sema::ActOnOpenMPFinalClause(Expr *Condition,
8924 SourceLocation StartLoc,
8925 SourceLocation LParenLoc,
8926 SourceLocation EndLoc) {
8927 Expr *ValExpr = Condition;
8928 if (!Condition->isValueDependent() && !Condition->isTypeDependent() &&
8929 !Condition->isInstantiationDependent() &&
8930 !Condition->containsUnexpandedParameterPack()) {
Richard Smith03a4aa32016-06-23 19:02:52 +00008931 ExprResult Val = CheckBooleanCondition(StartLoc, Condition);
Alexey Bataev3778b602014-07-17 07:32:53 +00008932 if (Val.isInvalid())
8933 return nullptr;
8934
Richard Smith03a4aa32016-06-23 19:02:52 +00008935 ValExpr = MakeFullExpr(Val.get()).get();
Alexey Bataev3778b602014-07-17 07:32:53 +00008936 }
8937
8938 return new (Context) OMPFinalClause(ValExpr, StartLoc, LParenLoc, EndLoc);
8939}
Alexander Musmana8e9d2e2014-06-03 10:16:47 +00008940ExprResult Sema::PerformOpenMPImplicitIntegerConversion(SourceLocation Loc,
8941 Expr *Op) {
Alexey Bataev568a8332014-03-06 06:15:19 +00008942 if (!Op)
8943 return ExprError();
8944
8945 class IntConvertDiagnoser : public ICEConvertDiagnoser {
8946 public:
8947 IntConvertDiagnoser()
Alexey Bataeved09d242014-05-28 05:53:51 +00008948 : ICEConvertDiagnoser(/*AllowScopedEnumerations*/ false, false, true) {}
Craig Toppere14c0f82014-03-12 04:55:44 +00008949 SemaDiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc,
8950 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008951 return S.Diag(Loc, diag::err_omp_not_integral) << T;
8952 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008953 SemaDiagnosticBuilder diagnoseIncomplete(Sema &S, SourceLocation Loc,
8954 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008955 return S.Diag(Loc, diag::err_omp_incomplete_type) << T;
8956 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008957 SemaDiagnosticBuilder diagnoseExplicitConv(Sema &S, SourceLocation Loc,
8958 QualType T,
8959 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008960 return S.Diag(Loc, diag::err_omp_explicit_conversion) << T << ConvTy;
8961 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008962 SemaDiagnosticBuilder noteExplicitConv(Sema &S, CXXConversionDecl *Conv,
8963 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008964 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008965 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008966 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008967 SemaDiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc,
8968 QualType T) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008969 return S.Diag(Loc, diag::err_omp_ambiguous_conversion) << T;
8970 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008971 SemaDiagnosticBuilder noteAmbiguous(Sema &S, CXXConversionDecl *Conv,
8972 QualType ConvTy) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008973 return S.Diag(Conv->getLocation(), diag::note_omp_conversion_here)
Alexey Bataeved09d242014-05-28 05:53:51 +00008974 << ConvTy->isEnumeralType() << ConvTy;
Alexey Bataev568a8332014-03-06 06:15:19 +00008975 }
Alexey Bataeved09d242014-05-28 05:53:51 +00008976 SemaDiagnosticBuilder diagnoseConversion(Sema &, SourceLocation, QualType,
8977 QualType) override {
Alexey Bataev568a8332014-03-06 06:15:19 +00008978 llvm_unreachable("conversion functions are permitted");
8979 }
8980 } ConvertDiagnoser;
8981 return PerformContextualImplicitConversion(Loc, Op, ConvertDiagnoser);
8982}
8983
Alexey Bataeve3727102018-04-18 15:57:46 +00008984static bool isNonNegativeIntegerValue(Expr *&ValExpr, Sema &SemaRef,
Alexey Bataeva0569352015-12-01 10:17:31 +00008985 OpenMPClauseKind CKind,
8986 bool StrictlyPositive) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00008987 if (!ValExpr->isTypeDependent() && !ValExpr->isValueDependent() &&
8988 !ValExpr->isInstantiationDependent()) {
8989 SourceLocation Loc = ValExpr->getExprLoc();
8990 ExprResult Value =
8991 SemaRef.PerformOpenMPImplicitIntegerConversion(Loc, ValExpr);
8992 if (Value.isInvalid())
8993 return false;
8994
8995 ValExpr = Value.get();
8996 // The expression must evaluate to a non-negative integer value.
8997 llvm::APSInt Result;
8998 if (ValExpr->isIntegerConstantExpr(Result, SemaRef.Context) &&
Alexey Bataeva0569352015-12-01 10:17:31 +00008999 Result.isSigned() &&
9000 !((!StrictlyPositive && Result.isNonNegative()) ||
9001 (StrictlyPositive && Result.isStrictlyPositive()))) {
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009002 SemaRef.Diag(Loc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009003 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9004 << ValExpr->getSourceRange();
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009005 return false;
9006 }
9007 }
9008 return true;
9009}
9010
Alexey Bataev568a8332014-03-06 06:15:19 +00009011OMPClause *Sema::ActOnOpenMPNumThreadsClause(Expr *NumThreads,
9012 SourceLocation StartLoc,
9013 SourceLocation LParenLoc,
9014 SourceLocation EndLoc) {
9015 Expr *ValExpr = NumThreads;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009016 Stmt *HelperValStmt = nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00009017
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009018 // OpenMP [2.5, Restrictions]
9019 // The num_threads expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +00009020 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_threads,
Alexey Bataeva0569352015-12-01 10:17:31 +00009021 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009022 return nullptr;
Alexey Bataev568a8332014-03-06 06:15:19 +00009023
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009024 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +00009025 OpenMPDirectiveKind CaptureRegion =
9026 getOpenMPCaptureRegionForClause(DKind, OMPC_num_threads);
9027 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009028 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009029 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob33c849a2017-01-25 00:57:16 +00009030 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9031 HelperValStmt = buildPreInits(Context, Captures);
9032 }
9033
9034 return new (Context) OMPNumThreadsClause(
9035 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Alexey Bataev568a8332014-03-06 06:15:19 +00009036}
9037
Alexey Bataev62c87d22014-03-21 04:51:18 +00009038ExprResult Sema::VerifyPositiveIntegerConstantInClause(Expr *E,
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009039 OpenMPClauseKind CKind,
9040 bool StrictlyPositive) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00009041 if (!E)
9042 return ExprError();
9043 if (E->isValueDependent() || E->isTypeDependent() ||
9044 E->isInstantiationDependent() || E->containsUnexpandedParameterPack())
Nikola Smiljanic03ff2592014-05-29 14:05:12 +00009045 return E;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009046 llvm::APSInt Result;
9047 ExprResult ICE = VerifyIntegerConstantExpression(E, &Result);
9048 if (ICE.isInvalid())
9049 return ExprError();
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009050 if ((StrictlyPositive && !Result.isStrictlyPositive()) ||
9051 (!StrictlyPositive && !Result.isNonNegative())) {
Alexey Bataev62c87d22014-03-21 04:51:18 +00009052 Diag(E->getExprLoc(), diag::err_omp_negative_expression_in_clause)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009053 << getOpenMPClauseName(CKind) << (StrictlyPositive ? 1 : 0)
9054 << E->getSourceRange();
Alexey Bataev62c87d22014-03-21 04:51:18 +00009055 return ExprError();
9056 }
Alexander Musman09184fe2014-09-30 05:29:28 +00009057 if (CKind == OMPC_aligned && !Result.isPowerOf2()) {
9058 Diag(E->getExprLoc(), diag::warn_omp_alignment_not_power_of_two)
9059 << E->getSourceRange();
9060 return ExprError();
9061 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009062 if (CKind == OMPC_collapse && DSAStack->getAssociatedLoops() == 1)
9063 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev7b6bc882015-11-26 07:50:39 +00009064 else if (CKind == OMPC_ordered)
Alexey Bataeva636c7f2015-12-23 10:27:45 +00009065 DSAStack->setAssociatedLoops(Result.getExtValue());
Alexey Bataev62c87d22014-03-21 04:51:18 +00009066 return ICE;
9067}
9068
9069OMPClause *Sema::ActOnOpenMPSafelenClause(Expr *Len, SourceLocation StartLoc,
9070 SourceLocation LParenLoc,
9071 SourceLocation EndLoc) {
9072 // OpenMP [2.8.1, simd construct, Description]
9073 // The parameter of the safelen clause must be a constant
9074 // positive integer expression.
9075 ExprResult Safelen = VerifyPositiveIntegerConstantInClause(Len, OMPC_safelen);
9076 if (Safelen.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009077 return nullptr;
Alexey Bataev62c87d22014-03-21 04:51:18 +00009078 return new (Context)
Nikola Smiljanic01a75982014-05-29 10:55:11 +00009079 OMPSafelenClause(Safelen.get(), StartLoc, LParenLoc, EndLoc);
Alexey Bataev62c87d22014-03-21 04:51:18 +00009080}
9081
Alexey Bataev66b15b52015-08-21 11:14:16 +00009082OMPClause *Sema::ActOnOpenMPSimdlenClause(Expr *Len, SourceLocation StartLoc,
9083 SourceLocation LParenLoc,
9084 SourceLocation EndLoc) {
9085 // OpenMP [2.8.1, simd construct, Description]
9086 // The parameter of the simdlen clause must be a constant
9087 // positive integer expression.
9088 ExprResult Simdlen = VerifyPositiveIntegerConstantInClause(Len, OMPC_simdlen);
9089 if (Simdlen.isInvalid())
9090 return nullptr;
9091 return new (Context)
9092 OMPSimdlenClause(Simdlen.get(), StartLoc, LParenLoc, EndLoc);
9093}
9094
Alexander Musman64d33f12014-06-04 07:53:32 +00009095OMPClause *Sema::ActOnOpenMPCollapseClause(Expr *NumForLoops,
9096 SourceLocation StartLoc,
Alexander Musman8bd31e62014-05-27 15:12:19 +00009097 SourceLocation LParenLoc,
9098 SourceLocation EndLoc) {
Alexander Musman64d33f12014-06-04 07:53:32 +00009099 // OpenMP [2.7.1, loop construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009100 // OpenMP [2.8.1, simd construct, Description]
Alexander Musman64d33f12014-06-04 07:53:32 +00009101 // OpenMP [2.9.6, distribute construct, Description]
Alexander Musman8bd31e62014-05-27 15:12:19 +00009102 // The parameter of the collapse clause must be a constant
9103 // positive integer expression.
Alexander Musman64d33f12014-06-04 07:53:32 +00009104 ExprResult NumForLoopsResult =
9105 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_collapse);
9106 if (NumForLoopsResult.isInvalid())
Alexander Musman8bd31e62014-05-27 15:12:19 +00009107 return nullptr;
9108 return new (Context)
Alexander Musman64d33f12014-06-04 07:53:32 +00009109 OMPCollapseClause(NumForLoopsResult.get(), StartLoc, LParenLoc, EndLoc);
Alexander Musman8bd31e62014-05-27 15:12:19 +00009110}
9111
Alexey Bataev10e775f2015-07-30 11:36:16 +00009112OMPClause *Sema::ActOnOpenMPOrderedClause(SourceLocation StartLoc,
9113 SourceLocation EndLoc,
9114 SourceLocation LParenLoc,
9115 Expr *NumForLoops) {
Alexey Bataev10e775f2015-07-30 11:36:16 +00009116 // OpenMP [2.7.1, loop construct, Description]
9117 // OpenMP [2.8.1, simd construct, Description]
9118 // OpenMP [2.9.6, distribute construct, Description]
9119 // The parameter of the ordered clause must be a constant
9120 // positive integer expression if any.
9121 if (NumForLoops && LParenLoc.isValid()) {
9122 ExprResult NumForLoopsResult =
9123 VerifyPositiveIntegerConstantInClause(NumForLoops, OMPC_ordered);
9124 if (NumForLoopsResult.isInvalid())
9125 return nullptr;
9126 NumForLoops = NumForLoopsResult.get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009127 } else {
Alexey Bataev346265e2015-09-25 10:37:12 +00009128 NumForLoops = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +00009129 }
Alexey Bataevf138fda2018-08-13 19:04:24 +00009130 auto *Clause = OMPOrderedClause::Create(
9131 Context, NumForLoops, NumForLoops ? DSAStack->getAssociatedLoops() : 0,
9132 StartLoc, LParenLoc, EndLoc);
9133 DSAStack->setOrderedRegion(/*IsOrdered=*/true, NumForLoops, Clause);
9134 return Clause;
Alexey Bataev10e775f2015-07-30 11:36:16 +00009135}
9136
Alexey Bataeved09d242014-05-28 05:53:51 +00009137OMPClause *Sema::ActOnOpenMPSimpleClause(
9138 OpenMPClauseKind Kind, unsigned Argument, SourceLocation ArgumentLoc,
9139 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009140 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009141 switch (Kind) {
9142 case OMPC_default:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009143 Res =
Alexey Bataeved09d242014-05-28 05:53:51 +00009144 ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
9145 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009146 break;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009147 case OMPC_proc_bind:
Alexey Bataeved09d242014-05-28 05:53:51 +00009148 Res = ActOnOpenMPProcBindClause(
9149 static_cast<OpenMPProcBindClauseKind>(Argument), ArgumentLoc, StartLoc,
9150 LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009151 break;
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009152 case OMPC_atomic_default_mem_order:
9153 Res = ActOnOpenMPAtomicDefaultMemOrderClause(
9154 static_cast<OpenMPAtomicDefaultMemOrderClauseKind>(Argument),
9155 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
9156 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009157 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009158 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009159 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009160 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009161 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009162 case OMPC_collapse:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009163 case OMPC_schedule:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009164 case OMPC_private:
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009165 case OMPC_firstprivate:
Alexander Musman1bb328c2014-06-04 13:06:39 +00009166 case OMPC_lastprivate:
Alexey Bataev758e55e2013-09-06 18:03:48 +00009167 case OMPC_shared:
Alexey Bataevc5e02582014-06-16 07:08:35 +00009168 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009169 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009170 case OMPC_in_reduction:
Alexander Musman8dba6642014-04-22 13:09:42 +00009171 case OMPC_linear:
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009172 case OMPC_aligned:
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009173 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009174 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009175 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009176 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009177 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009178 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009179 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009180 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009181 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009182 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009183 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009184 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009185 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009186 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009187 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009188 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009189 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009190 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009191 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009192 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009193 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009194 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009195 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009196 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009197 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009198 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009199 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009200 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009201 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009202 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009203 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009204 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009205 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009206 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009207 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009208 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009209 case OMPC_dynamic_allocators:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009210 llvm_unreachable("Clause is not allowed.");
9211 }
9212 return Res;
9213}
9214
Alexey Bataev6402bca2015-12-28 07:25:51 +00009215static std::string
9216getListOfPossibleValues(OpenMPClauseKind K, unsigned First, unsigned Last,
9217 ArrayRef<unsigned> Exclude = llvm::None) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009218 SmallString<256> Buffer;
9219 llvm::raw_svector_ostream Out(Buffer);
Alexey Bataev6402bca2015-12-28 07:25:51 +00009220 unsigned Bound = Last >= 2 ? Last - 2 : 0;
9221 unsigned Skipped = Exclude.size();
9222 auto S = Exclude.begin(), E = Exclude.end();
Alexey Bataeve3727102018-04-18 15:57:46 +00009223 for (unsigned I = First; I < Last; ++I) {
9224 if (std::find(S, E, I) != E) {
Alexey Bataev6402bca2015-12-28 07:25:51 +00009225 --Skipped;
9226 continue;
9227 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009228 Out << "'" << getOpenMPSimpleClauseTypeName(K, I) << "'";
9229 if (I == Bound - Skipped)
9230 Out << " or ";
9231 else if (I != Bound + 1 - Skipped)
9232 Out << ", ";
Alexey Bataev6402bca2015-12-28 07:25:51 +00009233 }
Alexey Bataeve3727102018-04-18 15:57:46 +00009234 return Out.str();
Alexey Bataev6402bca2015-12-28 07:25:51 +00009235}
9236
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009237OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
9238 SourceLocation KindKwLoc,
9239 SourceLocation StartLoc,
9240 SourceLocation LParenLoc,
9241 SourceLocation EndLoc) {
9242 if (Kind == OMPC_DEFAULT_unknown) {
Alexey Bataev4ca40ed2014-05-12 04:23:46 +00009243 static_assert(OMPC_DEFAULT_unknown > 0,
9244 "OMPC_DEFAULT_unknown not greater than 0");
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009245 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009246 << getListOfPossibleValues(OMPC_default, /*First=*/0,
9247 /*Last=*/OMPC_DEFAULT_unknown)
9248 << getOpenMPClauseName(OMPC_default);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009249 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009250 }
Alexey Bataev758e55e2013-09-06 18:03:48 +00009251 switch (Kind) {
9252 case OMPC_DEFAULT_none:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009253 DSAStack->setDefaultDSANone(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009254 break;
9255 case OMPC_DEFAULT_shared:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009256 DSAStack->setDefaultDSAShared(KindKwLoc);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009257 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009258 case OMPC_DEFAULT_unknown:
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009259 llvm_unreachable("Clause kind is not allowed.");
Alexey Bataev758e55e2013-09-06 18:03:48 +00009260 break;
9261 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009262 return new (Context)
9263 OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009264}
9265
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009266OMPClause *Sema::ActOnOpenMPProcBindClause(OpenMPProcBindClauseKind Kind,
9267 SourceLocation KindKwLoc,
9268 SourceLocation StartLoc,
9269 SourceLocation LParenLoc,
9270 SourceLocation EndLoc) {
9271 if (Kind == OMPC_PROC_BIND_unknown) {
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009272 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +00009273 << getListOfPossibleValues(OMPC_proc_bind, /*First=*/0,
9274 /*Last=*/OMPC_PROC_BIND_unknown)
9275 << getOpenMPClauseName(OMPC_proc_bind);
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009276 return nullptr;
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009277 }
Alexey Bataeved09d242014-05-28 05:53:51 +00009278 return new (Context)
9279 OMPProcBindClause(Kind, KindKwLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009280}
9281
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009282OMPClause *Sema::ActOnOpenMPAtomicDefaultMemOrderClause(
9283 OpenMPAtomicDefaultMemOrderClauseKind Kind, SourceLocation KindKwLoc,
9284 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation EndLoc) {
9285 if (Kind == OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown) {
9286 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
9287 << getListOfPossibleValues(
9288 OMPC_atomic_default_mem_order, /*First=*/0,
9289 /*Last=*/OMPC_ATOMIC_DEFAULT_MEM_ORDER_unknown)
9290 << getOpenMPClauseName(OMPC_atomic_default_mem_order);
9291 return nullptr;
9292 }
9293 return new (Context) OMPAtomicDefaultMemOrderClause(Kind, KindKwLoc, StartLoc,
9294 LParenLoc, EndLoc);
9295}
9296
Alexey Bataev56dafe82014-06-20 07:16:17 +00009297OMPClause *Sema::ActOnOpenMPSingleExprWithArgClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009298 OpenMPClauseKind Kind, ArrayRef<unsigned> Argument, Expr *Expr,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009299 SourceLocation StartLoc, SourceLocation LParenLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009300 ArrayRef<SourceLocation> ArgumentLoc, SourceLocation DelimLoc,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009301 SourceLocation EndLoc) {
9302 OMPClause *Res = nullptr;
9303 switch (Kind) {
9304 case OMPC_schedule:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009305 enum { Modifier1, Modifier2, ScheduleKind, NumberOfElements };
9306 assert(Argument.size() == NumberOfElements &&
9307 ArgumentLoc.size() == NumberOfElements);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009308 Res = ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009309 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier1]),
9310 static_cast<OpenMPScheduleClauseModifier>(Argument[Modifier2]),
9311 static_cast<OpenMPScheduleClauseKind>(Argument[ScheduleKind]), Expr,
9312 StartLoc, LParenLoc, ArgumentLoc[Modifier1], ArgumentLoc[Modifier2],
9313 ArgumentLoc[ScheduleKind], DelimLoc, EndLoc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009314 break;
9315 case OMPC_if:
Alexey Bataev6402bca2015-12-28 07:25:51 +00009316 assert(Argument.size() == 1 && ArgumentLoc.size() == 1);
9317 Res = ActOnOpenMPIfClause(static_cast<OpenMPDirectiveKind>(Argument.back()),
9318 Expr, StartLoc, LParenLoc, ArgumentLoc.back(),
9319 DelimLoc, EndLoc);
Alexey Bataev6b8046a2015-09-03 07:23:48 +00009320 break;
Carlo Bertollib4adf552016-01-15 18:50:31 +00009321 case OMPC_dist_schedule:
9322 Res = ActOnOpenMPDistScheduleClause(
9323 static_cast<OpenMPDistScheduleClauseKind>(Argument.back()), Expr,
9324 StartLoc, LParenLoc, ArgumentLoc.back(), DelimLoc, EndLoc);
9325 break;
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009326 case OMPC_defaultmap:
9327 enum { Modifier, DefaultmapKind };
9328 Res = ActOnOpenMPDefaultmapClause(
9329 static_cast<OpenMPDefaultmapClauseModifier>(Argument[Modifier]),
9330 static_cast<OpenMPDefaultmapClauseKind>(Argument[DefaultmapKind]),
David Majnemer9d168222016-08-05 17:44:54 +00009331 StartLoc, LParenLoc, ArgumentLoc[Modifier], ArgumentLoc[DefaultmapKind],
9332 EndLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009333 break;
Alexey Bataev3778b602014-07-17 07:32:53 +00009334 case OMPC_final:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009335 case OMPC_num_threads:
9336 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009337 case OMPC_simdlen:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009338 case OMPC_collapse:
9339 case OMPC_default:
9340 case OMPC_proc_bind:
9341 case OMPC_private:
9342 case OMPC_firstprivate:
9343 case OMPC_lastprivate:
9344 case OMPC_shared:
9345 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009346 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009347 case OMPC_in_reduction:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009348 case OMPC_linear:
9349 case OMPC_aligned:
9350 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009351 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009352 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009353 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009354 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009355 case OMPC_mergeable:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009356 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009357 case OMPC_flush:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009358 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009359 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009360 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009361 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009362 case OMPC_seq_cst:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009363 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009364 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009365 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009366 case OMPC_simd:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009367 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009368 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009369 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009370 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009371 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009372 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009373 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009374 case OMPC_hint:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009375 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009376 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009377 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009378 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009379 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009380 case OMPC_is_device_ptr:
Kelvin Li1408f912018-09-26 04:28:39 +00009381 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009382 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009383 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009384 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009385 case OMPC_atomic_default_mem_order:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009386 llvm_unreachable("Clause is not allowed.");
9387 }
9388 return Res;
9389}
9390
Alexey Bataev6402bca2015-12-28 07:25:51 +00009391static bool checkScheduleModifiers(Sema &S, OpenMPScheduleClauseModifier M1,
9392 OpenMPScheduleClauseModifier M2,
9393 SourceLocation M1Loc, SourceLocation M2Loc) {
9394 if (M1 == OMPC_SCHEDULE_MODIFIER_unknown && M1Loc.isValid()) {
9395 SmallVector<unsigned, 2> Excluded;
9396 if (M2 != OMPC_SCHEDULE_MODIFIER_unknown)
9397 Excluded.push_back(M2);
9398 if (M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic)
9399 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_monotonic);
9400 if (M2 == OMPC_SCHEDULE_MODIFIER_monotonic)
9401 Excluded.push_back(OMPC_SCHEDULE_MODIFIER_nonmonotonic);
9402 S.Diag(M1Loc, diag::err_omp_unexpected_clause_value)
9403 << getListOfPossibleValues(OMPC_schedule,
9404 /*First=*/OMPC_SCHEDULE_MODIFIER_unknown + 1,
9405 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9406 Excluded)
9407 << getOpenMPClauseName(OMPC_schedule);
9408 return true;
9409 }
9410 return false;
9411}
9412
Alexey Bataev56dafe82014-06-20 07:16:17 +00009413OMPClause *Sema::ActOnOpenMPScheduleClause(
Alexey Bataev6402bca2015-12-28 07:25:51 +00009414 OpenMPScheduleClauseModifier M1, OpenMPScheduleClauseModifier M2,
Alexey Bataev56dafe82014-06-20 07:16:17 +00009415 OpenMPScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
Alexey Bataev6402bca2015-12-28 07:25:51 +00009416 SourceLocation LParenLoc, SourceLocation M1Loc, SourceLocation M2Loc,
9417 SourceLocation KindLoc, SourceLocation CommaLoc, SourceLocation EndLoc) {
9418 if (checkScheduleModifiers(*this, M1, M2, M1Loc, M2Loc) ||
9419 checkScheduleModifiers(*this, M2, M1, M2Loc, M1Loc))
9420 return nullptr;
9421 // OpenMP, 2.7.1, Loop Construct, Restrictions
9422 // Either the monotonic modifier or the nonmonotonic modifier can be specified
9423 // but not both.
9424 if ((M1 == M2 && M1 != OMPC_SCHEDULE_MODIFIER_unknown) ||
9425 (M1 == OMPC_SCHEDULE_MODIFIER_monotonic &&
9426 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) ||
9427 (M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic &&
9428 M2 == OMPC_SCHEDULE_MODIFIER_monotonic)) {
9429 Diag(M2Loc, diag::err_omp_unexpected_schedule_modifier)
9430 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M2)
9431 << getOpenMPSimpleClauseTypeName(OMPC_schedule, M1);
9432 return nullptr;
9433 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009434 if (Kind == OMPC_SCHEDULE_unknown) {
9435 std::string Values;
Alexey Bataev6402bca2015-12-28 07:25:51 +00009436 if (M1Loc.isInvalid() && M2Loc.isInvalid()) {
9437 unsigned Exclude[] = {OMPC_SCHEDULE_unknown};
9438 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9439 /*Last=*/OMPC_SCHEDULE_MODIFIER_last,
9440 Exclude);
9441 } else {
9442 Values = getListOfPossibleValues(OMPC_schedule, /*First=*/0,
9443 /*Last=*/OMPC_SCHEDULE_unknown);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009444 }
9445 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
9446 << Values << getOpenMPClauseName(OMPC_schedule);
9447 return nullptr;
9448 }
Alexey Bataev6402bca2015-12-28 07:25:51 +00009449 // OpenMP, 2.7.1, Loop Construct, Restrictions
9450 // The nonmonotonic modifier can only be specified with schedule(dynamic) or
9451 // schedule(guided).
9452 if ((M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ||
9453 M2 == OMPC_SCHEDULE_MODIFIER_nonmonotonic) &&
9454 Kind != OMPC_SCHEDULE_dynamic && Kind != OMPC_SCHEDULE_guided) {
9455 Diag(M1 == OMPC_SCHEDULE_MODIFIER_nonmonotonic ? M1Loc : M2Loc,
9456 diag::err_omp_schedule_nonmonotonic_static);
9457 return nullptr;
9458 }
Alexey Bataev56dafe82014-06-20 07:16:17 +00009459 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +00009460 Stmt *HelperValStmt = nullptr;
Alexey Bataev56dafe82014-06-20 07:16:17 +00009461 if (ChunkSize) {
9462 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
9463 !ChunkSize->isInstantiationDependent() &&
9464 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00009465 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Alexey Bataev56dafe82014-06-20 07:16:17 +00009466 ExprResult Val =
9467 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
9468 if (Val.isInvalid())
9469 return nullptr;
9470
9471 ValExpr = Val.get();
9472
9473 // OpenMP [2.7.1, Restrictions]
9474 // chunk_size must be a loop invariant integer expression with a positive
9475 // value.
9476 llvm::APSInt Result;
Alexey Bataev040d5402015-05-12 08:35:28 +00009477 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
9478 if (Result.isSigned() && !Result.isStrictlyPositive()) {
9479 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
Alexey Bataeva0569352015-12-01 10:17:31 +00009480 << "schedule" << 1 << ChunkSize->getSourceRange();
Alexey Bataev040d5402015-05-12 08:35:28 +00009481 return nullptr;
9482 }
Alexey Bataev2ba67042017-11-28 21:11:44 +00009483 } else if (getOpenMPCaptureRegionForClause(
9484 DSAStack->getCurrentDirective(), OMPC_schedule) !=
9485 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +00009486 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +00009487 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +00009488 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +00009489 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
9490 HelperValStmt = buildPreInits(Context, Captures);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009491 }
9492 }
9493 }
9494
Alexey Bataev6402bca2015-12-28 07:25:51 +00009495 return new (Context)
9496 OMPScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc, Kind,
Alexey Bataev3392d762016-02-16 11:18:12 +00009497 ValExpr, HelperValStmt, M1, M1Loc, M2, M2Loc);
Alexey Bataev56dafe82014-06-20 07:16:17 +00009498}
9499
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009500OMPClause *Sema::ActOnOpenMPClause(OpenMPClauseKind Kind,
9501 SourceLocation StartLoc,
9502 SourceLocation EndLoc) {
9503 OMPClause *Res = nullptr;
9504 switch (Kind) {
9505 case OMPC_ordered:
9506 Res = ActOnOpenMPOrderedClause(StartLoc, EndLoc);
9507 break;
Alexey Bataev236070f2014-06-20 11:19:47 +00009508 case OMPC_nowait:
9509 Res = ActOnOpenMPNowaitClause(StartLoc, EndLoc);
9510 break;
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009511 case OMPC_untied:
9512 Res = ActOnOpenMPUntiedClause(StartLoc, EndLoc);
9513 break;
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009514 case OMPC_mergeable:
9515 Res = ActOnOpenMPMergeableClause(StartLoc, EndLoc);
9516 break;
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009517 case OMPC_read:
9518 Res = ActOnOpenMPReadClause(StartLoc, EndLoc);
9519 break;
Alexey Bataevdea47612014-07-23 07:46:59 +00009520 case OMPC_write:
9521 Res = ActOnOpenMPWriteClause(StartLoc, EndLoc);
9522 break;
Alexey Bataev67a4f222014-07-23 10:25:33 +00009523 case OMPC_update:
9524 Res = ActOnOpenMPUpdateClause(StartLoc, EndLoc);
9525 break;
Alexey Bataev459dec02014-07-24 06:46:57 +00009526 case OMPC_capture:
9527 Res = ActOnOpenMPCaptureClause(StartLoc, EndLoc);
9528 break;
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009529 case OMPC_seq_cst:
9530 Res = ActOnOpenMPSeqCstClause(StartLoc, EndLoc);
9531 break;
Alexey Bataev346265e2015-09-25 10:37:12 +00009532 case OMPC_threads:
9533 Res = ActOnOpenMPThreadsClause(StartLoc, EndLoc);
9534 break;
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009535 case OMPC_simd:
9536 Res = ActOnOpenMPSIMDClause(StartLoc, EndLoc);
9537 break;
Alexey Bataevb825de12015-12-07 10:51:44 +00009538 case OMPC_nogroup:
9539 Res = ActOnOpenMPNogroupClause(StartLoc, EndLoc);
9540 break;
Kelvin Li1408f912018-09-26 04:28:39 +00009541 case OMPC_unified_address:
9542 Res = ActOnOpenMPUnifiedAddressClause(StartLoc, EndLoc);
9543 break;
Patrick Lyster4a370b92018-10-01 13:47:43 +00009544 case OMPC_unified_shared_memory:
9545 Res = ActOnOpenMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9546 break;
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009547 case OMPC_reverse_offload:
9548 Res = ActOnOpenMPReverseOffloadClause(StartLoc, EndLoc);
9549 break;
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009550 case OMPC_dynamic_allocators:
9551 Res = ActOnOpenMPDynamicAllocatorsClause(StartLoc, EndLoc);
9552 break;
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009553 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009554 case OMPC_final:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009555 case OMPC_num_threads:
9556 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009557 case OMPC_simdlen:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009558 case OMPC_collapse:
9559 case OMPC_schedule:
9560 case OMPC_private:
9561 case OMPC_firstprivate:
9562 case OMPC_lastprivate:
9563 case OMPC_shared:
9564 case OMPC_reduction:
Alexey Bataev169d96a2017-07-18 20:17:46 +00009565 case OMPC_task_reduction:
Alexey Bataevfa312f32017-07-21 18:48:21 +00009566 case OMPC_in_reduction:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009567 case OMPC_linear:
9568 case OMPC_aligned:
9569 case OMPC_copyin:
Alexey Bataevbae9a792014-06-27 10:37:06 +00009570 case OMPC_copyprivate:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009571 case OMPC_default:
9572 case OMPC_proc_bind:
9573 case OMPC_threadprivate:
Alexey Bataev6125da92014-07-21 11:26:11 +00009574 case OMPC_flush:
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009575 case OMPC_depend:
Michael Wonge710d542015-08-07 16:16:36 +00009576 case OMPC_device:
Kelvin Li0bff7af2015-11-23 05:32:03 +00009577 case OMPC_map:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009578 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009579 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009580 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009581 case OMPC_grainsize:
Alexey Bataev382967a2015-12-08 12:06:20 +00009582 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009583 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009584 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009585 case OMPC_defaultmap:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009586 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009587 case OMPC_uniform:
Samuel Antao661c0902016-05-26 17:39:58 +00009588 case OMPC_to:
Samuel Antaoec172c62016-05-26 17:49:04 +00009589 case OMPC_from:
Carlo Bertolli2404b172016-07-13 15:37:16 +00009590 case OMPC_use_device_ptr:
Carlo Bertolli70594e92016-07-13 17:16:49 +00009591 case OMPC_is_device_ptr:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009592 case OMPC_atomic_default_mem_order:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009593 llvm_unreachable("Clause is not allowed.");
9594 }
9595 return Res;
9596}
9597
Alexey Bataev236070f2014-06-20 11:19:47 +00009598OMPClause *Sema::ActOnOpenMPNowaitClause(SourceLocation StartLoc,
9599 SourceLocation EndLoc) {
Alexey Bataev6d4ed052015-07-01 06:57:41 +00009600 DSAStack->setNowaitRegion();
Alexey Bataev236070f2014-06-20 11:19:47 +00009601 return new (Context) OMPNowaitClause(StartLoc, EndLoc);
9602}
9603
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009604OMPClause *Sema::ActOnOpenMPUntiedClause(SourceLocation StartLoc,
9605 SourceLocation EndLoc) {
9606 return new (Context) OMPUntiedClause(StartLoc, EndLoc);
9607}
9608
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009609OMPClause *Sema::ActOnOpenMPMergeableClause(SourceLocation StartLoc,
9610 SourceLocation EndLoc) {
9611 return new (Context) OMPMergeableClause(StartLoc, EndLoc);
9612}
9613
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009614OMPClause *Sema::ActOnOpenMPReadClause(SourceLocation StartLoc,
9615 SourceLocation EndLoc) {
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009616 return new (Context) OMPReadClause(StartLoc, EndLoc);
9617}
9618
Alexey Bataevdea47612014-07-23 07:46:59 +00009619OMPClause *Sema::ActOnOpenMPWriteClause(SourceLocation StartLoc,
9620 SourceLocation EndLoc) {
9621 return new (Context) OMPWriteClause(StartLoc, EndLoc);
9622}
9623
Alexey Bataev67a4f222014-07-23 10:25:33 +00009624OMPClause *Sema::ActOnOpenMPUpdateClause(SourceLocation StartLoc,
9625 SourceLocation EndLoc) {
9626 return new (Context) OMPUpdateClause(StartLoc, EndLoc);
9627}
9628
Alexey Bataev459dec02014-07-24 06:46:57 +00009629OMPClause *Sema::ActOnOpenMPCaptureClause(SourceLocation StartLoc,
9630 SourceLocation EndLoc) {
9631 return new (Context) OMPCaptureClause(StartLoc, EndLoc);
9632}
9633
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009634OMPClause *Sema::ActOnOpenMPSeqCstClause(SourceLocation StartLoc,
9635 SourceLocation EndLoc) {
9636 return new (Context) OMPSeqCstClause(StartLoc, EndLoc);
9637}
9638
Alexey Bataev346265e2015-09-25 10:37:12 +00009639OMPClause *Sema::ActOnOpenMPThreadsClause(SourceLocation StartLoc,
9640 SourceLocation EndLoc) {
9641 return new (Context) OMPThreadsClause(StartLoc, EndLoc);
9642}
9643
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009644OMPClause *Sema::ActOnOpenMPSIMDClause(SourceLocation StartLoc,
9645 SourceLocation EndLoc) {
9646 return new (Context) OMPSIMDClause(StartLoc, EndLoc);
9647}
9648
Alexey Bataevb825de12015-12-07 10:51:44 +00009649OMPClause *Sema::ActOnOpenMPNogroupClause(SourceLocation StartLoc,
9650 SourceLocation EndLoc) {
9651 return new (Context) OMPNogroupClause(StartLoc, EndLoc);
9652}
9653
Kelvin Li1408f912018-09-26 04:28:39 +00009654OMPClause *Sema::ActOnOpenMPUnifiedAddressClause(SourceLocation StartLoc,
9655 SourceLocation EndLoc) {
9656 return new (Context) OMPUnifiedAddressClause(StartLoc, EndLoc);
9657}
9658
Patrick Lyster4a370b92018-10-01 13:47:43 +00009659OMPClause *Sema::ActOnOpenMPUnifiedSharedMemoryClause(SourceLocation StartLoc,
9660 SourceLocation EndLoc) {
9661 return new (Context) OMPUnifiedSharedMemoryClause(StartLoc, EndLoc);
9662}
9663
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009664OMPClause *Sema::ActOnOpenMPReverseOffloadClause(SourceLocation StartLoc,
9665 SourceLocation EndLoc) {
9666 return new (Context) OMPReverseOffloadClause(StartLoc, EndLoc);
9667}
9668
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009669OMPClause *Sema::ActOnOpenMPDynamicAllocatorsClause(SourceLocation StartLoc,
9670 SourceLocation EndLoc) {
9671 return new (Context) OMPDynamicAllocatorsClause(StartLoc, EndLoc);
9672}
9673
Alexey Bataevc5e02582014-06-16 07:08:35 +00009674OMPClause *Sema::ActOnOpenMPVarListClause(
9675 OpenMPClauseKind Kind, ArrayRef<Expr *> VarList, Expr *TailExpr,
9676 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation ColonLoc,
9677 SourceLocation EndLoc, CXXScopeSpec &ReductionIdScopeSpec,
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009678 const DeclarationNameInfo &ReductionId, OpenMPDependClauseKind DepKind,
Kelvin Lief579432018-12-18 22:18:41 +00009679 OpenMPLinearClauseKind LinKind,
9680 ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
9681 ArrayRef<SourceLocation> MapTypeModifiersLoc,
Samuel Antao23abd722016-01-19 20:40:49 +00009682 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
9683 SourceLocation DepLinMapLoc) {
Alexander Musmancb7f9c42014-05-15 13:04:49 +00009684 OMPClause *Res = nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009685 switch (Kind) {
9686 case OMPC_private:
9687 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9688 break;
Alexey Bataevd5af8e42013-10-01 05:32:34 +00009689 case OMPC_firstprivate:
9690 Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9691 break;
Alexander Musman1bb328c2014-06-04 13:06:39 +00009692 case OMPC_lastprivate:
9693 Res = ActOnOpenMPLastprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9694 break;
Alexey Bataev758e55e2013-09-06 18:03:48 +00009695 case OMPC_shared:
9696 Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
9697 break;
Alexey Bataevc5e02582014-06-16 07:08:35 +00009698 case OMPC_reduction:
Alexey Bataev23b69422014-06-18 07:08:49 +00009699 Res = ActOnOpenMPReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9700 EndLoc, ReductionIdScopeSpec, ReductionId);
Alexey Bataevc5e02582014-06-16 07:08:35 +00009701 break;
Alexey Bataev169d96a2017-07-18 20:17:46 +00009702 case OMPC_task_reduction:
9703 Res = ActOnOpenMPTaskReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9704 EndLoc, ReductionIdScopeSpec,
9705 ReductionId);
9706 break;
Alexey Bataevfa312f32017-07-21 18:48:21 +00009707 case OMPC_in_reduction:
9708 Res =
9709 ActOnOpenMPInReductionClause(VarList, StartLoc, LParenLoc, ColonLoc,
9710 EndLoc, ReductionIdScopeSpec, ReductionId);
9711 break;
Alexander Musman8dba6642014-04-22 13:09:42 +00009712 case OMPC_linear:
9713 Res = ActOnOpenMPLinearClause(VarList, TailExpr, StartLoc, LParenLoc,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009714 LinKind, DepLinMapLoc, ColonLoc, EndLoc);
Alexander Musman8dba6642014-04-22 13:09:42 +00009715 break;
Alexander Musmanf0d76e72014-05-29 14:36:25 +00009716 case OMPC_aligned:
9717 Res = ActOnOpenMPAlignedClause(VarList, TailExpr, StartLoc, LParenLoc,
9718 ColonLoc, EndLoc);
9719 break;
Alexey Bataevd48bcd82014-03-31 03:36:38 +00009720 case OMPC_copyin:
9721 Res = ActOnOpenMPCopyinClause(VarList, StartLoc, LParenLoc, EndLoc);
9722 break;
Alexey Bataevbae9a792014-06-27 10:37:06 +00009723 case OMPC_copyprivate:
9724 Res = ActOnOpenMPCopyprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
9725 break;
Alexey Bataev6125da92014-07-21 11:26:11 +00009726 case OMPC_flush:
9727 Res = ActOnOpenMPFlushClause(VarList, StartLoc, LParenLoc, EndLoc);
9728 break;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009729 case OMPC_depend:
David Majnemer9d168222016-08-05 17:44:54 +00009730 Res = ActOnOpenMPDependClause(DepKind, DepLinMapLoc, ColonLoc, VarList,
Kelvin Li0bff7af2015-11-23 05:32:03 +00009731 StartLoc, LParenLoc, EndLoc);
9732 break;
9733 case OMPC_map:
Kelvin Lief579432018-12-18 22:18:41 +00009734 Res = ActOnOpenMPMapClause(MapTypeModifiers, MapTypeModifiersLoc, MapType,
9735 IsMapTypeImplicit, DepLinMapLoc, ColonLoc,
9736 VarList, StartLoc, LParenLoc, EndLoc);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +00009737 break;
Samuel Antao661c0902016-05-26 17:39:58 +00009738 case OMPC_to:
9739 Res = ActOnOpenMPToClause(VarList, StartLoc, LParenLoc, EndLoc);
9740 break;
Samuel Antaoec172c62016-05-26 17:49:04 +00009741 case OMPC_from:
9742 Res = ActOnOpenMPFromClause(VarList, StartLoc, LParenLoc, EndLoc);
9743 break;
Carlo Bertolli2404b172016-07-13 15:37:16 +00009744 case OMPC_use_device_ptr:
9745 Res = ActOnOpenMPUseDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9746 break;
Carlo Bertolli70594e92016-07-13 17:16:49 +00009747 case OMPC_is_device_ptr:
9748 Res = ActOnOpenMPIsDevicePtrClause(VarList, StartLoc, LParenLoc, EndLoc);
9749 break;
Alexey Bataevaadd52e2014-02-13 05:29:23 +00009750 case OMPC_if:
Alexey Bataev3778b602014-07-17 07:32:53 +00009751 case OMPC_final:
Alexey Bataev568a8332014-03-06 06:15:19 +00009752 case OMPC_num_threads:
Alexey Bataev62c87d22014-03-21 04:51:18 +00009753 case OMPC_safelen:
Alexey Bataev66b15b52015-08-21 11:14:16 +00009754 case OMPC_simdlen:
Alexander Musman8bd31e62014-05-27 15:12:19 +00009755 case OMPC_collapse:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009756 case OMPC_default:
Alexey Bataevbcbadb62014-05-06 06:04:14 +00009757 case OMPC_proc_bind:
Alexey Bataev56dafe82014-06-20 07:16:17 +00009758 case OMPC_schedule:
Alexey Bataev142e1fc2014-06-20 09:44:06 +00009759 case OMPC_ordered:
Alexey Bataev236070f2014-06-20 11:19:47 +00009760 case OMPC_nowait:
Alexey Bataev7aea99a2014-07-17 12:19:31 +00009761 case OMPC_untied:
Alexey Bataev74ba3a52014-07-17 12:47:03 +00009762 case OMPC_mergeable:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009763 case OMPC_threadprivate:
Alexey Bataevf98b00c2014-07-23 02:27:21 +00009764 case OMPC_read:
Alexey Bataevdea47612014-07-23 07:46:59 +00009765 case OMPC_write:
Alexey Bataev67a4f222014-07-23 10:25:33 +00009766 case OMPC_update:
Alexey Bataev459dec02014-07-24 06:46:57 +00009767 case OMPC_capture:
Alexey Bataev82bad8b2014-07-24 08:55:34 +00009768 case OMPC_seq_cst:
Michael Wonge710d542015-08-07 16:16:36 +00009769 case OMPC_device:
Alexey Bataev346265e2015-09-25 10:37:12 +00009770 case OMPC_threads:
Alexey Bataevd14d1e62015-09-28 06:39:35 +00009771 case OMPC_simd:
Kelvin Li099bb8c2015-11-24 20:50:12 +00009772 case OMPC_num_teams:
Kelvin Lia15fb1a2015-11-27 18:47:36 +00009773 case OMPC_thread_limit:
Alexey Bataeva0569352015-12-01 10:17:31 +00009774 case OMPC_priority:
Alexey Bataev1fd4aed2015-12-07 12:52:51 +00009775 case OMPC_grainsize:
Alexey Bataevb825de12015-12-07 10:51:44 +00009776 case OMPC_nogroup:
Alexey Bataev382967a2015-12-08 12:06:20 +00009777 case OMPC_num_tasks:
Alexey Bataev28c75412015-12-15 08:19:24 +00009778 case OMPC_hint:
Carlo Bertollib4adf552016-01-15 18:50:31 +00009779 case OMPC_dist_schedule:
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +00009780 case OMPC_defaultmap:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009781 case OMPC_unknown:
Alexey Bataeve48a5fc2016-04-12 05:28:34 +00009782 case OMPC_uniform:
Kelvin Li1408f912018-09-26 04:28:39 +00009783 case OMPC_unified_address:
Patrick Lyster4a370b92018-10-01 13:47:43 +00009784 case OMPC_unified_shared_memory:
Patrick Lyster6bdf63b2018-10-03 20:07:58 +00009785 case OMPC_reverse_offload:
Patrick Lyster3fe9e392018-10-11 14:41:10 +00009786 case OMPC_dynamic_allocators:
Patrick Lyster7a2a27c2018-11-02 12:18:11 +00009787 case OMPC_atomic_default_mem_order:
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009788 llvm_unreachable("Clause is not allowed.");
9789 }
9790 return Res;
9791}
9792
Alexey Bataev90c228f2016-02-08 09:29:13 +00009793ExprResult Sema::getOpenMPCapturedExpr(VarDecl *Capture, ExprValueKind VK,
Alexey Bataev61205072016-03-02 04:57:40 +00009794 ExprObjectKind OK, SourceLocation Loc) {
Alexey Bataev90c228f2016-02-08 09:29:13 +00009795 ExprResult Res = BuildDeclRefExpr(
9796 Capture, Capture->getType().getNonReferenceType(), VK_LValue, Loc);
9797 if (!Res.isUsable())
9798 return ExprError();
9799 if (OK == OK_Ordinary && !getLangOpts().CPlusPlus) {
9800 Res = CreateBuiltinUnaryOp(Loc, UO_Deref, Res.get());
9801 if (!Res.isUsable())
9802 return ExprError();
9803 }
9804 if (VK != VK_LValue && Res.get()->isGLValue()) {
9805 Res = DefaultLvalueConversion(Res.get());
9806 if (!Res.isUsable())
9807 return ExprError();
9808 }
9809 return Res;
9810}
9811
Alexey Bataev60da77e2016-02-29 05:54:20 +00009812static std::pair<ValueDecl *, bool>
9813getPrivateItem(Sema &S, Expr *&RefExpr, SourceLocation &ELoc,
9814 SourceRange &ERange, bool AllowArraySection = false) {
Alexey Bataevd985eda2016-02-10 11:29:16 +00009815 if (RefExpr->isTypeDependent() || RefExpr->isValueDependent() ||
9816 RefExpr->containsUnexpandedParameterPack())
9817 return std::make_pair(nullptr, true);
9818
Alexey Bataevd985eda2016-02-10 11:29:16 +00009819 // OpenMP [3.1, C/C++]
9820 // A list item is a variable name.
9821 // OpenMP [2.9.3.3, Restrictions, p.1]
9822 // A variable that is part of another variable (as an array or
9823 // structure element) cannot appear in a private clause.
9824 RefExpr = RefExpr->IgnoreParens();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009825 enum {
9826 NoArrayExpr = -1,
9827 ArraySubscript = 0,
9828 OMPArraySection = 1
9829 } IsArrayExpr = NoArrayExpr;
9830 if (AllowArraySection) {
9831 if (auto *ASE = dyn_cast_or_null<ArraySubscriptExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009832 Expr *Base = ASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009833 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9834 Base = TempASE->getBase()->IgnoreParenImpCasts();
9835 RefExpr = Base;
9836 IsArrayExpr = ArraySubscript;
9837 } else if (auto *OASE = dyn_cast_or_null<OMPArraySectionExpr>(RefExpr)) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009838 Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
Alexey Bataev60da77e2016-02-29 05:54:20 +00009839 while (auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base))
9840 Base = TempOASE->getBase()->IgnoreParenImpCasts();
9841 while (auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base))
9842 Base = TempASE->getBase()->IgnoreParenImpCasts();
9843 RefExpr = Base;
9844 IsArrayExpr = OMPArraySection;
9845 }
9846 }
9847 ELoc = RefExpr->getExprLoc();
9848 ERange = RefExpr->getSourceRange();
9849 RefExpr = RefExpr->IgnoreParenImpCasts();
Alexey Bataevd985eda2016-02-10 11:29:16 +00009850 auto *DE = dyn_cast_or_null<DeclRefExpr>(RefExpr);
9851 auto *ME = dyn_cast_or_null<MemberExpr>(RefExpr);
9852 if ((!DE || !isa<VarDecl>(DE->getDecl())) &&
9853 (S.getCurrentThisType().isNull() || !ME ||
9854 !isa<CXXThisExpr>(ME->getBase()->IgnoreParenImpCasts()) ||
9855 !isa<FieldDecl>(ME->getMemberDecl()))) {
Alexey Bataeve3727102018-04-18 15:57:46 +00009856 if (IsArrayExpr != NoArrayExpr) {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009857 S.Diag(ELoc, diag::err_omp_expected_base_var_name) << IsArrayExpr
9858 << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +00009859 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +00009860 S.Diag(ELoc,
9861 AllowArraySection
9862 ? diag::err_omp_expected_var_name_member_expr_or_array_item
9863 : diag::err_omp_expected_var_name_member_expr)
9864 << (S.getCurrentThisType().isNull() ? 0 : 1) << ERange;
9865 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009866 return std::make_pair(nullptr, false);
9867 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +00009868 return std::make_pair(
9869 getCanonicalDecl(DE ? DE->getDecl() : ME->getMemberDecl()), false);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009870}
9871
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009872OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
9873 SourceLocation StartLoc,
9874 SourceLocation LParenLoc,
9875 SourceLocation EndLoc) {
9876 SmallVector<Expr *, 8> Vars;
Alexey Bataev03b340a2014-10-21 03:16:40 +00009877 SmallVector<Expr *, 8> PrivateCopies;
Alexey Bataeve3727102018-04-18 15:57:46 +00009878 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009879 assert(RefExpr && "NULL expr in OpenMP private clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +00009880 SourceLocation ELoc;
9881 SourceRange ERange;
9882 Expr *SimpleRefExpr = RefExpr;
9883 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +00009884 if (Res.second) {
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009885 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +00009886 Vars.push_back(RefExpr);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009887 PrivateCopies.push_back(nullptr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009888 }
Alexey Bataevd985eda2016-02-10 11:29:16 +00009889 ValueDecl *D = Res.first;
9890 if (!D)
9891 continue;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009892
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009893 QualType Type = D->getType();
9894 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009895
9896 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
9897 // A variable that appears in a private clause must not have an incomplete
9898 // type or a reference type.
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009899 if (RequireCompleteType(ELoc, Type, diag::err_omp_private_incomplete_type))
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009900 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +00009901 Type = Type.getNonReferenceType();
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009902
Joel E. Dennye6234d1422019-01-04 22:11:31 +00009903 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
9904 // A variable that is privatized must not have a const-qualified type
9905 // unless it is of class type with a mutable member. This restriction does
9906 // not apply to the firstprivate clause.
9907 //
9908 // OpenMP 3.1 [2.9.3.3, private clause, Restrictions]
9909 // A variable that appears in a private clause must not have a
9910 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +00009911 if (rejectConstNotMutableType(*this, D, Type, OMPC_private, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +00009912 continue;
9913
Alexey Bataev758e55e2013-09-06 18:03:48 +00009914 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
9915 // in a Construct]
9916 // Variables with the predetermined data-sharing attributes may not be
9917 // listed in data-sharing attributes clauses, except for the cases
9918 // listed below. For these exceptions only, listing a predetermined
9919 // variable in a data-sharing attribute clause is allowed and overrides
9920 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +00009921 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009922 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
Alexey Bataeved09d242014-05-28 05:53:51 +00009923 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
9924 << getOpenMPClauseName(OMPC_private);
Alexey Bataeve3727102018-04-18 15:57:46 +00009925 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +00009926 continue;
9927 }
9928
Alexey Bataeve3727102018-04-18 15:57:46 +00009929 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009930 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +00009931 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Kelvin Libf594a52016-12-17 05:48:59 +00009932 isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009933 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
9934 << getOpenMPClauseName(OMPC_private) << Type
Kelvin Libf594a52016-12-17 05:48:59 +00009935 << getOpenMPDirectiveName(CurrDir);
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009936 bool IsDecl =
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009937 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009938 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009939 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009940 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009941 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +00009942 continue;
9943 }
9944
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009945 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
9946 // A list item cannot appear in both a map clause and a data-sharing
9947 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +00009948 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +00009949 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +00009950 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +00009951 VD, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +00009952 [&](OMPClauseMappableExprCommon::MappableExprComponentListRef,
9953 OpenMPClauseKind WhereFoundClauseKind) -> bool {
9954 ConflictKind = WhereFoundClauseKind;
9955 return true;
9956 })) {
9957 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009958 << getOpenMPClauseName(OMPC_private)
Samuel Antao6890b092016-07-28 14:25:09 +00009959 << getOpenMPClauseName(ConflictKind)
Kelvin Libf594a52016-12-17 05:48:59 +00009960 << getOpenMPDirectiveName(CurrDir);
Alexey Bataeve3727102018-04-18 15:57:46 +00009961 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +00009962 continue;
9963 }
9964 }
9965
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009966 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
9967 // A variable of class type (or array thereof) that appears in a private
9968 // clause requires an accessible, unambiguous default constructor for the
9969 // class type.
Alexey Bataev03b340a2014-10-21 03:16:40 +00009970 // Generate helper private variable and initialize it with the default
9971 // value. The address of the original variable is replaced by the address of
9972 // the new private variable in CodeGen. This new variable is not added to
9973 // IdResolver, so the code in the OpenMP region uses original variable for
9974 // proper diagnostics.
Alexey Bataevf120c0d2015-05-19 07:46:42 +00009975 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +00009976 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +00009977 buildVarDecl(*this, ELoc, Type, D->getName(),
9978 D->hasAttrs() ? &D->getAttrs() : nullptr,
9979 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Richard Smith3beb7c62017-01-12 02:27:38 +00009980 ActOnUninitializedDecl(VDPrivate);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009981 if (VDPrivate->isInvalidDecl())
9982 continue;
Alexey Bataeve3727102018-04-18 15:57:46 +00009983 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataev48c0bfb2016-01-20 09:07:54 +00009984 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009985
Alexey Bataev90c228f2016-02-08 09:29:13 +00009986 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009987 if (!VD && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +00009988 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev90c228f2016-02-08 09:29:13 +00009989 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_private, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +00009990 Vars.push_back((VD || CurContext->isDependentContext())
9991 ? RefExpr->IgnoreParens()
9992 : Ref);
Alexey Bataev03b340a2014-10-21 03:16:40 +00009993 PrivateCopies.push_back(VDPrivateRefExpr);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009994 }
9995
Alexey Bataeved09d242014-05-28 05:53:51 +00009996 if (Vars.empty())
9997 return nullptr;
Alexey Bataev5ec3eb12013-07-19 03:13:43 +00009998
Alexey Bataev03b340a2014-10-21 03:16:40 +00009999 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
10000 PrivateCopies);
Alexey Bataev5ec3eb12013-07-19 03:13:43 +000010001}
10002
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010003namespace {
10004class DiagsUninitializedSeveretyRAII {
10005private:
10006 DiagnosticsEngine &Diags;
10007 SourceLocation SavedLoc;
Alexey Bataeve3727102018-04-18 15:57:46 +000010008 bool IsIgnored = false;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010009
10010public:
10011 DiagsUninitializedSeveretyRAII(DiagnosticsEngine &Diags, SourceLocation Loc,
10012 bool IsIgnored)
10013 : Diags(Diags), SavedLoc(Loc), IsIgnored(IsIgnored) {
10014 if (!IsIgnored) {
10015 Diags.setSeverity(/*Diag*/ diag::warn_uninit_self_reference_in_init,
10016 /*Map*/ diag::Severity::Ignored, Loc);
10017 }
10018 }
10019 ~DiagsUninitializedSeveretyRAII() {
10020 if (!IsIgnored)
10021 Diags.popMappings(SavedLoc);
10022 }
10023};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000010024}
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010025
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010026OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
10027 SourceLocation StartLoc,
10028 SourceLocation LParenLoc,
10029 SourceLocation EndLoc) {
10030 SmallVector<Expr *, 8> Vars;
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010031 SmallVector<Expr *, 8> PrivateCopies;
10032 SmallVector<Expr *, 8> Inits;
Alexey Bataev417089f2016-02-17 13:19:37 +000010033 SmallVector<Decl *, 4> ExprCaptures;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010034 bool IsImplicitClause =
10035 StartLoc.isInvalid() && LParenLoc.isInvalid() && EndLoc.isInvalid();
Alexey Bataeve3727102018-04-18 15:57:46 +000010036 SourceLocation ImplicitClauseLoc = DSAStack->getConstructLoc();
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010037
Alexey Bataeve3727102018-04-18 15:57:46 +000010038 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000010039 assert(RefExpr && "NULL expr in OpenMP firstprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010040 SourceLocation ELoc;
10041 SourceRange ERange;
10042 Expr *SimpleRefExpr = RefExpr;
10043 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevd985eda2016-02-10 11:29:16 +000010044 if (Res.second) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010045 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010046 Vars.push_back(RefExpr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010047 PrivateCopies.push_back(nullptr);
10048 Inits.push_back(nullptr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010049 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010050 ValueDecl *D = Res.first;
10051 if (!D)
10052 continue;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010053
Alexey Bataev60da77e2016-02-29 05:54:20 +000010054 ELoc = IsImplicitClause ? ImplicitClauseLoc : ELoc;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010055 QualType Type = D->getType();
10056 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010057
10058 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
10059 // A variable that appears in a private clause must not have an incomplete
10060 // type or a reference type.
10061 if (RequireCompleteType(ELoc, Type,
Alexey Bataevd985eda2016-02-10 11:29:16 +000010062 diag::err_omp_firstprivate_incomplete_type))
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010063 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010064 Type = Type.getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010065
10066 // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
10067 // A variable of class type (or array thereof) that appears in a private
Alexey Bataev23b69422014-06-18 07:08:49 +000010068 // clause requires an accessible, unambiguous copy constructor for the
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010069 // class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000010070 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010071
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010072 // If an implicit firstprivate variable found it was checked already.
Alexey Bataev005248a2016-02-25 05:25:57 +000010073 DSAStackTy::DSAVarData TopDVar;
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010074 if (!IsImplicitClause) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010075 DSAStackTy::DSAVarData DVar =
10076 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataev005248a2016-02-25 05:25:57 +000010077 TopDVar = DVar;
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010078 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010079 bool IsConstant = ElemType.isConstant(Context);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010080 // OpenMP [2.4.13, Data-sharing Attribute Clauses]
10081 // A list item that specifies a given variable may not appear in more
10082 // than one clause on the same directive, except that a variable may be
10083 // specified in both firstprivate and lastprivate clauses.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010084 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10085 // A list item may appear in a firstprivate or lastprivate clause but not
10086 // both.
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010087 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010088 (isOpenMPDistributeDirective(CurrDir) ||
10089 DVar.CKind != OMPC_lastprivate) &&
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010090 DVar.RefExpr) {
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010091 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010092 << getOpenMPClauseName(DVar.CKind)
10093 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010094 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010095 continue;
10096 }
10097
10098 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10099 // in a Construct]
10100 // Variables with the predetermined data-sharing attributes may not be
10101 // listed in data-sharing attributes clauses, except for the cases
10102 // listed below. For these exceptions only, listing a predetermined
10103 // variable in a data-sharing attribute clause is allowed and overrides
10104 // the variable's predetermined data-sharing attributes.
10105 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10106 // in a Construct, C/C++, p.2]
10107 // Variables with const-qualified type having no mutable member may be
10108 // listed in a firstprivate clause, even if they are static data members.
Alexey Bataevd985eda2016-02-10 11:29:16 +000010109 if (!(IsConstant || (VD && VD->isStaticDataMember())) && !DVar.RefExpr &&
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010110 DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
10111 Diag(ELoc, diag::err_omp_wrong_dsa)
Alexey Bataeved09d242014-05-28 05:53:51 +000010112 << getOpenMPClauseName(DVar.CKind)
10113 << getOpenMPClauseName(OMPC_firstprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010114 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010115 continue;
10116 }
10117
10118 // OpenMP [2.9.3.4, Restrictions, p.2]
10119 // A list item that is private within a parallel region must not appear
10120 // in a firstprivate clause on a worksharing construct if any of the
10121 // worksharing regions arising from the worksharing construct ever bind
10122 // to any of the parallel regions arising from the parallel construct.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010123 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10124 // A list item that is private within a teams region must not appear in a
10125 // firstprivate clause on a distribute construct if any of the distribute
10126 // regions arising from the distribute construct ever bind to any of the
10127 // teams regions arising from the teams construct.
10128 // OpenMP 4.5 [2.15.3.4, Restrictions, p.3]
10129 // A list item that appears in a reduction clause of a teams construct
10130 // must not appear in a firstprivate clause on a distribute construct if
10131 // any of the distribute regions arising from the distribute construct
10132 // ever bind to any of the teams regions arising from the teams construct.
10133 if ((isOpenMPWorksharingDirective(CurrDir) ||
10134 isOpenMPDistributeDirective(CurrDir)) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010135 !isOpenMPParallelDirective(CurrDir) &&
10136 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010137 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010138 if (DVar.CKind != OMPC_shared &&
10139 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010140 isOpenMPTeamsDirective(DVar.DKind) ||
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010141 DVar.DKind == OMPD_unknown)) {
Alexey Bataevf29276e2014-06-18 04:14:57 +000010142 Diag(ELoc, diag::err_omp_required_access)
10143 << getOpenMPClauseName(OMPC_firstprivate)
10144 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010145 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010146 continue;
10147 }
10148 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010149 // OpenMP [2.9.3.4, Restrictions, p.3]
10150 // A list item that appears in a reduction clause of a parallel construct
10151 // must not appear in a firstprivate clause on a worksharing or task
10152 // construct if any of the worksharing or task regions arising from the
10153 // worksharing or task construct ever bind to any of the parallel regions
10154 // arising from the parallel construct.
10155 // OpenMP [2.9.3.4, Restrictions, p.4]
10156 // A list item that appears in a reduction clause in worksharing
10157 // construct must not appear in a firstprivate clause in a task construct
10158 // encountered during execution of any of the worksharing regions arising
10159 // from the worksharing construct.
Alexey Bataev35aaee62016-04-13 13:36:48 +000010160 if (isOpenMPTaskingDirective(CurrDir)) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010161 DVar = DSAStack->hasInnermostDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010162 D, [](OpenMPClauseKind C) { return C == OMPC_reduction; },
10163 [](OpenMPDirectiveKind K) {
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010164 return isOpenMPParallelDirective(K) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010165 isOpenMPWorksharingDirective(K) ||
10166 isOpenMPTeamsDirective(K);
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010167 },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010168 /*FromParent=*/true);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010169 if (DVar.CKind == OMPC_reduction &&
10170 (isOpenMPParallelDirective(DVar.DKind) ||
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010171 isOpenMPWorksharingDirective(DVar.DKind) ||
10172 isOpenMPTeamsDirective(DVar.DKind))) {
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010173 Diag(ELoc, diag::err_omp_parallel_reduction_in_task_firstprivate)
10174 << getOpenMPDirectiveName(DVar.DKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000010175 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev9c2e8ee2014-07-11 11:25:16 +000010176 continue;
10177 }
10178 }
Carlo Bertolli6200a3d2015-12-14 14:51:25 +000010179
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010180 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
10181 // A list item cannot appear in both a map clause and a data-sharing
10182 // attribute clause on the same construct
Alexey Bataevb358f992017-12-01 17:40:15 +000010183 if (isOpenMPTargetExecutionDirective(CurrDir)) {
Samuel Antao6890b092016-07-28 14:25:09 +000010184 OpenMPClauseKind ConflictKind;
Samuel Antao90927002016-04-26 14:54:23 +000010185 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000010186 VD, /*CurrentRegionOnly=*/true,
Alexey Bataeve3727102018-04-18 15:57:46 +000010187 [&ConflictKind](
10188 OMPClauseMappableExprCommon::MappableExprComponentListRef,
10189 OpenMPClauseKind WhereFoundClauseKind) {
Samuel Antao6890b092016-07-28 14:25:09 +000010190 ConflictKind = WhereFoundClauseKind;
10191 return true;
10192 })) {
10193 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010194 << getOpenMPClauseName(OMPC_firstprivate)
Samuel Antao6890b092016-07-28 14:25:09 +000010195 << getOpenMPClauseName(ConflictKind)
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010196 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000010197 reportOriginalDsa(*this, DSAStack, D, DVar);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000010198 continue;
10199 }
10200 }
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010201 }
10202
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010203 // Variably modified types are not supported for tasks.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000010204 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType() &&
Alexey Bataev35aaee62016-04-13 13:36:48 +000010205 isOpenMPTaskingDirective(DSAStack->getCurrentDirective())) {
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010206 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
10207 << getOpenMPClauseName(OMPC_firstprivate) << Type
10208 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
10209 bool IsDecl =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010210 !VD ||
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010211 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataevd985eda2016-02-10 11:29:16 +000010212 Diag(D->getLocation(),
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010213 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataevd985eda2016-02-10 11:29:16 +000010214 << D;
Alexey Bataevccb59ec2015-05-19 08:44:56 +000010215 continue;
10216 }
10217
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010218 Type = Type.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010219 VarDecl *VDPrivate =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000010220 buildVarDecl(*this, ELoc, Type, D->getName(),
10221 D->hasAttrs() ? &D->getAttrs() : nullptr,
10222 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010223 // Generate helper private variable and initialize it with the value of the
10224 // original variable. The address of the original variable is replaced by
10225 // the address of the new private variable in the CodeGen. This new variable
10226 // is not added to IdResolver, so the code in the OpenMP region uses
10227 // original variable for proper diagnostics and variable capturing.
10228 Expr *VDInitRefExpr = nullptr;
10229 // For arrays generate initializer for single element and replace it by the
10230 // original array element in CodeGen.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010231 if (Type->isArrayType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010232 VarDecl *VDInit =
Alexey Bataevd985eda2016-02-10 11:29:16 +000010233 buildVarDecl(*this, RefExpr->getExprLoc(), ElemType, D->getName());
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010234 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, ElemType, ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010235 Expr *Init = DefaultLvalueConversion(VDInitRefExpr).get();
Alexey Bataevf120c0d2015-05-19 07:46:42 +000010236 ElemType = ElemType.getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010237 VarDecl *VDInitTemp = buildVarDecl(*this, RefExpr->getExprLoc(), ElemType,
10238 ".firstprivate.temp");
Alexey Bataev69c62a92015-04-15 04:52:20 +000010239 InitializedEntity Entity =
10240 InitializedEntity::InitializeVariable(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010241 InitializationKind Kind = InitializationKind::CreateCopy(ELoc, ELoc);
10242
10243 InitializationSequence InitSeq(*this, Entity, Kind, Init);
10244 ExprResult Result = InitSeq.Perform(*this, Entity, Kind, Init);
10245 if (Result.isInvalid())
10246 VDPrivate->setInvalidDecl();
10247 else
10248 VDPrivate->setInit(Result.getAs<Expr>());
Alexey Bataevf24e7b12015-10-08 09:10:53 +000010249 // Remove temp variable declaration.
10250 Context.Deallocate(VDInitTemp);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010251 } else {
Alexey Bataeve3727102018-04-18 15:57:46 +000010252 VarDecl *VDInit = buildVarDecl(*this, RefExpr->getExprLoc(), Type,
10253 ".firstprivate.temp");
Alexey Bataevd985eda2016-02-10 11:29:16 +000010254 VDInitRefExpr = buildDeclRefExpr(*this, VDInit, RefExpr->getType(),
10255 RefExpr->getExprLoc());
Alexey Bataev69c62a92015-04-15 04:52:20 +000010256 AddInitializerToDecl(VDPrivate,
10257 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000010258 /*DirectInit=*/false);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010259 }
10260 if (VDPrivate->isInvalidDecl()) {
10261 if (IsImplicitClause) {
Alexey Bataevd985eda2016-02-10 11:29:16 +000010262 Diag(RefExpr->getExprLoc(),
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010263 diag::note_omp_task_predetermined_firstprivate_here);
10264 }
10265 continue;
10266 }
10267 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010268 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Alexey Bataevd985eda2016-02-10 11:29:16 +000010269 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(),
10270 RefExpr->getExprLoc());
10271 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010272 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010273 if (TopDVar.CKind == OMPC_lastprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010274 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010275 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010276 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010277 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010278 ExprCaptures.push_back(Ref->getDecl());
10279 }
Alexey Bataev417089f2016-02-17 13:19:37 +000010280 }
Alexey Bataevd985eda2016-02-10 11:29:16 +000010281 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010282 Vars.push_back((VD || CurContext->isDependentContext())
10283 ? RefExpr->IgnoreParens()
10284 : Ref);
Alexey Bataev4a5bb772014-10-08 14:01:46 +000010285 PrivateCopies.push_back(VDPrivateRefExpr);
10286 Inits.push_back(VDInitRefExpr);
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010287 }
10288
Alexey Bataeved09d242014-05-28 05:53:51 +000010289 if (Vars.empty())
10290 return nullptr;
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010291
10292 return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010293 Vars, PrivateCopies, Inits,
10294 buildPreInits(Context, ExprCaptures));
Alexey Bataevd5af8e42013-10-01 05:32:34 +000010295}
10296
Alexander Musman1bb328c2014-06-04 13:06:39 +000010297OMPClause *Sema::ActOnOpenMPLastprivateClause(ArrayRef<Expr *> VarList,
10298 SourceLocation StartLoc,
10299 SourceLocation LParenLoc,
10300 SourceLocation EndLoc) {
10301 SmallVector<Expr *, 8> Vars;
Alexey Bataev38e89532015-04-16 04:54:05 +000010302 SmallVector<Expr *, 8> SrcExprs;
10303 SmallVector<Expr *, 8> DstExprs;
10304 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataev005248a2016-02-25 05:25:57 +000010305 SmallVector<Decl *, 4> ExprCaptures;
10306 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataeve3727102018-04-18 15:57:46 +000010307 for (Expr *RefExpr : VarList) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010308 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010309 SourceLocation ELoc;
10310 SourceRange ERange;
10311 Expr *SimpleRefExpr = RefExpr;
10312 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev74caaf22016-02-20 04:09:36 +000010313 if (Res.second) {
Alexander Musman1bb328c2014-06-04 13:06:39 +000010314 // It will be analyzed later.
10315 Vars.push_back(RefExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010316 SrcExprs.push_back(nullptr);
10317 DstExprs.push_back(nullptr);
10318 AssignmentOps.push_back(nullptr);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010319 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010320 ValueDecl *D = Res.first;
10321 if (!D)
10322 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010323
Alexey Bataev74caaf22016-02-20 04:09:36 +000010324 QualType Type = D->getType();
10325 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010326
10327 // OpenMP [2.14.3.5, Restrictions, C/C++, p.2]
10328 // A variable that appears in a lastprivate clause must not have an
10329 // incomplete type or a reference type.
10330 if (RequireCompleteType(ELoc, Type,
Alexey Bataev74caaf22016-02-20 04:09:36 +000010331 diag::err_omp_lastprivate_incomplete_type))
Alexander Musman1bb328c2014-06-04 13:06:39 +000010332 continue;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000010333 Type = Type.getNonReferenceType();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010334
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010335 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
10336 // A variable that is privatized must not have a const-qualified type
10337 // unless it is of class type with a mutable member. This restriction does
10338 // not apply to the firstprivate clause.
10339 //
10340 // OpenMP 3.1 [2.9.3.5, lastprivate clause, Restrictions]
10341 // A variable that appears in a lastprivate clause must not have a
10342 // const-qualified type unless it is of class type with a mutable member.
Joel E. Dennyd2649292019-01-04 22:11:56 +000010343 if (rejectConstNotMutableType(*this, D, Type, OMPC_lastprivate, ELoc))
Joel E. Dennye6234d1422019-01-04 22:11:31 +000010344 continue;
10345
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010346 OpenMPDirectiveKind CurrDir = DSAStack->getCurrentDirective();
Alexander Musman1bb328c2014-06-04 13:06:39 +000010347 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
10348 // in a Construct]
10349 // Variables with the predetermined data-sharing attributes may not be
10350 // listed in data-sharing attributes clauses, except for the cases
10351 // listed below.
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010352 // OpenMP 4.5 [2.10.8, Distribute Construct, p.3]
10353 // A list item may appear in a firstprivate or lastprivate clause but not
10354 // both.
Alexey Bataeve3727102018-04-18 15:57:46 +000010355 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010356 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_lastprivate &&
Alexey Bataevb358f992017-12-01 17:40:15 +000010357 (isOpenMPDistributeDirective(CurrDir) ||
10358 DVar.CKind != OMPC_firstprivate) &&
Alexander Musman1bb328c2014-06-04 13:06:39 +000010359 (DVar.CKind != OMPC_private || DVar.RefExpr != nullptr)) {
10360 Diag(ELoc, diag::err_omp_wrong_dsa)
10361 << getOpenMPClauseName(DVar.CKind)
10362 << getOpenMPClauseName(OMPC_lastprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000010363 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman1bb328c2014-06-04 13:06:39 +000010364 continue;
10365 }
10366
Alexey Bataevf29276e2014-06-18 04:14:57 +000010367 // OpenMP [2.14.3.5, Restrictions, p.2]
10368 // A list item that is private within a parallel region, or that appears in
10369 // the reduction clause of a parallel construct, must not appear in a
10370 // lastprivate clause on a worksharing construct if any of the corresponding
10371 // worksharing regions ever binds to any of the corresponding parallel
10372 // regions.
Alexey Bataev39f915b82015-05-08 10:41:21 +000010373 DSAStackTy::DSAVarData TopDVar = DVar;
Alexey Bataev549210e2014-06-24 04:39:47 +000010374 if (isOpenMPWorksharingDirective(CurrDir) &&
Kelvin Li579e41c2016-11-30 23:51:03 +000010375 !isOpenMPParallelDirective(CurrDir) &&
10376 !isOpenMPTeamsDirective(CurrDir)) {
Alexey Bataev74caaf22016-02-20 04:09:36 +000010377 DVar = DSAStack->getImplicitDSA(D, true);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010378 if (DVar.CKind != OMPC_shared) {
10379 Diag(ELoc, diag::err_omp_required_access)
10380 << getOpenMPClauseName(OMPC_lastprivate)
10381 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010382 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevf29276e2014-06-18 04:14:57 +000010383 continue;
10384 }
10385 }
Alexey Bataev74caaf22016-02-20 04:09:36 +000010386
Alexander Musman1bb328c2014-06-04 13:06:39 +000010387 // OpenMP [2.14.3.5, Restrictions, C++, p.1,2]
Alexey Bataevf29276e2014-06-18 04:14:57 +000010388 // A variable of class type (or array thereof) that appears in a
10389 // lastprivate clause requires an accessible, unambiguous default
10390 // constructor for the class type, unless the list item is also specified
10391 // in a firstprivate clause.
Alexander Musman1bb328c2014-06-04 13:06:39 +000010392 // A variable of class type (or array thereof) that appears in a
10393 // lastprivate clause requires an accessible, unambiguous copy assignment
10394 // operator for the class type.
Alexey Bataev38e89532015-04-16 04:54:05 +000010395 Type = Context.getBaseElementType(Type).getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000010396 VarDecl *SrcVD = buildVarDecl(*this, ERange.getBegin(),
10397 Type.getUnqualifiedType(), ".lastprivate.src",
10398 D->hasAttrs() ? &D->getAttrs() : nullptr);
10399 DeclRefExpr *PseudoSrcExpr =
Alexey Bataev74caaf22016-02-20 04:09:36 +000010400 buildDeclRefExpr(*this, SrcVD, Type.getUnqualifiedType(), ELoc);
Alexey Bataeve3727102018-04-18 15:57:46 +000010401 VarDecl *DstVD =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010402 buildVarDecl(*this, ERange.getBegin(), Type, ".lastprivate.dst",
Alexey Bataev74caaf22016-02-20 04:09:36 +000010403 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000010404 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
Alexey Bataev38e89532015-04-16 04:54:05 +000010405 // For arrays generate assignment operation for single element and replace
10406 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000010407 ExprResult AssignmentOp = BuildBinOp(/*S=*/nullptr, ELoc, BO_Assign,
10408 PseudoDstExpr, PseudoSrcExpr);
Alexey Bataev38e89532015-04-16 04:54:05 +000010409 if (AssignmentOp.isInvalid())
10410 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000010411 AssignmentOp =
10412 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataev38e89532015-04-16 04:54:05 +000010413 if (AssignmentOp.isInvalid())
10414 continue;
Alexander Musman1bb328c2014-06-04 13:06:39 +000010415
Alexey Bataev74caaf22016-02-20 04:09:36 +000010416 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010417 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010418 if (TopDVar.CKind == OMPC_firstprivate) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010419 Ref = TopDVar.PrivateCopy;
Alexey Bataeve3727102018-04-18 15:57:46 +000010420 } else {
Alexey Bataev61205072016-03-02 04:57:40 +000010421 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000010422 if (!isOpenMPCapturedDecl(D))
Alexey Bataev005248a2016-02-25 05:25:57 +000010423 ExprCaptures.push_back(Ref->getDecl());
10424 }
10425 if (TopDVar.CKind == OMPC_firstprivate ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010426 (!isOpenMPCapturedDecl(D) &&
Alexey Bataev2bbf7212016-03-03 03:52:24 +000010427 Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>())) {
Alexey Bataev005248a2016-02-25 05:25:57 +000010428 ExprResult RefRes = DefaultLvalueConversion(Ref);
10429 if (!RefRes.isUsable())
10430 continue;
10431 ExprResult PostUpdateRes =
Alexey Bataev60da77e2016-02-29 05:54:20 +000010432 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
10433 RefRes.get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010434 if (!PostUpdateRes.isUsable())
10435 continue;
Alexey Bataev78849fb2016-03-09 09:49:00 +000010436 ExprPostUpdates.push_back(
10437 IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev005248a2016-02-25 05:25:57 +000010438 }
10439 }
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010440 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_lastprivate, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010441 Vars.push_back((VD || CurContext->isDependentContext())
10442 ? RefExpr->IgnoreParens()
10443 : Ref);
Alexey Bataev38e89532015-04-16 04:54:05 +000010444 SrcExprs.push_back(PseudoSrcExpr);
10445 DstExprs.push_back(PseudoDstExpr);
10446 AssignmentOps.push_back(AssignmentOp.get());
Alexander Musman1bb328c2014-06-04 13:06:39 +000010447 }
10448
10449 if (Vars.empty())
10450 return nullptr;
10451
10452 return OMPLastprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataev005248a2016-02-25 05:25:57 +000010453 Vars, SrcExprs, DstExprs, AssignmentOps,
Alexey Bataev5a3af132016-03-29 08:58:54 +000010454 buildPreInits(Context, ExprCaptures),
10455 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman1bb328c2014-06-04 13:06:39 +000010456}
10457
Alexey Bataev758e55e2013-09-06 18:03:48 +000010458OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
10459 SourceLocation StartLoc,
10460 SourceLocation LParenLoc,
10461 SourceLocation EndLoc) {
10462 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000010463 for (Expr *RefExpr : VarList) {
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010464 assert(RefExpr && "NULL expr in OpenMP lastprivate clause.");
Alexey Bataev60da77e2016-02-29 05:54:20 +000010465 SourceLocation ELoc;
10466 SourceRange ERange;
10467 Expr *SimpleRefExpr = RefExpr;
10468 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010469 if (Res.second) {
Alexey Bataev758e55e2013-09-06 18:03:48 +000010470 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000010471 Vars.push_back(RefExpr);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010472 }
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010473 ValueDecl *D = Res.first;
10474 if (!D)
10475 continue;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010476
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010477 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010478 // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
10479 // in a Construct]
10480 // Variables with the predetermined data-sharing attributes may not be
10481 // listed in data-sharing attributes clauses, except for the cases
10482 // listed below. For these exceptions only, listing a predetermined
10483 // variable in a data-sharing attribute clause is allowed and overrides
10484 // the variable's predetermined data-sharing attributes.
Alexey Bataeve3727102018-04-18 15:57:46 +000010485 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeved09d242014-05-28 05:53:51 +000010486 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared &&
10487 DVar.RefExpr) {
10488 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
10489 << getOpenMPClauseName(OMPC_shared);
Alexey Bataeve3727102018-04-18 15:57:46 +000010490 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010491 continue;
10492 }
10493
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010494 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010495 if (!VD && isOpenMPCapturedDecl(D) && !CurContext->isDependentContext())
Alexey Bataev61205072016-03-02 04:57:40 +000010496 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
Alexey Bataevb7a34b62016-02-25 03:59:29 +000010497 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_shared, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000010498 Vars.push_back((VD || !Ref || CurContext->isDependentContext())
10499 ? RefExpr->IgnoreParens()
10500 : Ref);
Alexey Bataev758e55e2013-09-06 18:03:48 +000010501 }
10502
Alexey Bataeved09d242014-05-28 05:53:51 +000010503 if (Vars.empty())
10504 return nullptr;
Alexey Bataev758e55e2013-09-06 18:03:48 +000010505
10506 return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
10507}
10508
Alexey Bataevc5e02582014-06-16 07:08:35 +000010509namespace {
10510class DSARefChecker : public StmtVisitor<DSARefChecker, bool> {
10511 DSAStackTy *Stack;
10512
10513public:
10514 bool VisitDeclRefExpr(DeclRefExpr *E) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010515 if (auto *VD = dyn_cast<VarDecl>(E->getDecl())) {
10516 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD, /*FromParent=*/false);
Alexey Bataevc5e02582014-06-16 07:08:35 +000010517 if (DVar.CKind == OMPC_shared && !DVar.RefExpr)
10518 return false;
10519 if (DVar.CKind != OMPC_unknown)
10520 return true;
Alexey Bataev7ace49d2016-05-17 08:55:33 +000010521 DSAStackTy::DSAVarData DVarPrivate = Stack->hasDSA(
Alexey Bataeve3727102018-04-18 15:57:46 +000010522 VD, isOpenMPPrivate, [](OpenMPDirectiveKind) { return true; },
Alexey Bataeveffbdf12017-07-21 17:24:30 +000010523 /*FromParent=*/true);
Alexey Bataeve3727102018-04-18 15:57:46 +000010524 return DVarPrivate.CKind != OMPC_unknown;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010525 }
10526 return false;
10527 }
10528 bool VisitStmt(Stmt *S) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010529 for (Stmt *Child : S->children()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010530 if (Child && Visit(Child))
10531 return true;
10532 }
10533 return false;
10534 }
Alexey Bataev23b69422014-06-18 07:08:49 +000010535 explicit DSARefChecker(DSAStackTy *S) : Stack(S) {}
Alexey Bataevc5e02582014-06-16 07:08:35 +000010536};
Alexey Bataev23b69422014-06-18 07:08:49 +000010537} // namespace
Alexey Bataevc5e02582014-06-16 07:08:35 +000010538
Alexey Bataev60da77e2016-02-29 05:54:20 +000010539namespace {
10540// Transform MemberExpression for specified FieldDecl of current class to
10541// DeclRefExpr to specified OMPCapturedExprDecl.
10542class TransformExprToCaptures : public TreeTransform<TransformExprToCaptures> {
10543 typedef TreeTransform<TransformExprToCaptures> BaseTransform;
Alexey Bataeve3727102018-04-18 15:57:46 +000010544 ValueDecl *Field = nullptr;
10545 DeclRefExpr *CapturedExpr = nullptr;
Alexey Bataev60da77e2016-02-29 05:54:20 +000010546
10547public:
10548 TransformExprToCaptures(Sema &SemaRef, ValueDecl *FieldDecl)
10549 : BaseTransform(SemaRef), Field(FieldDecl), CapturedExpr(nullptr) {}
10550
10551 ExprResult TransformMemberExpr(MemberExpr *E) {
10552 if (isa<CXXThisExpr>(E->getBase()->IgnoreParenImpCasts()) &&
10553 E->getMemberDecl() == Field) {
Alexey Bataev61205072016-03-02 04:57:40 +000010554 CapturedExpr = buildCapture(SemaRef, Field, E, /*WithInit=*/false);
Alexey Bataev60da77e2016-02-29 05:54:20 +000010555 return CapturedExpr;
10556 }
10557 return BaseTransform::TransformMemberExpr(E);
10558 }
10559 DeclRefExpr *getCapturedExpr() { return CapturedExpr; }
10560};
10561} // namespace
10562
Alexey Bataev97d18bf2018-04-11 19:21:00 +000010563template <typename T, typename U>
10564static T filterLookupForUDR(SmallVectorImpl<U> &Lookups,
10565 const llvm::function_ref<T(ValueDecl *)> Gen) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010566 for (U &Set : Lookups) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010567 for (auto *D : Set) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010568 if (T Res = Gen(cast<ValueDecl>(D)))
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010569 return Res;
10570 }
10571 }
10572 return T();
10573}
10574
Alexey Bataev43b90b72018-09-12 16:31:59 +000010575static NamedDecl *findAcceptableDecl(Sema &SemaRef, NamedDecl *D) {
10576 assert(!LookupResult::isVisible(SemaRef, D) && "not in slow case");
10577
10578 for (auto RD : D->redecls()) {
10579 // Don't bother with extra checks if we already know this one isn't visible.
10580 if (RD == D)
10581 continue;
10582
10583 auto ND = cast<NamedDecl>(RD);
10584 if (LookupResult::isVisible(SemaRef, ND))
10585 return ND;
10586 }
10587
10588 return nullptr;
10589}
10590
10591static void
10592argumentDependentLookup(Sema &SemaRef, const DeclarationNameInfo &ReductionId,
10593 SourceLocation Loc, QualType Ty,
10594 SmallVectorImpl<UnresolvedSet<8>> &Lookups) {
10595 // Find all of the associated namespaces and classes based on the
10596 // arguments we have.
10597 Sema::AssociatedNamespaceSet AssociatedNamespaces;
10598 Sema::AssociatedClassSet AssociatedClasses;
10599 OpaqueValueExpr OVE(Loc, Ty, VK_LValue);
10600 SemaRef.FindAssociatedClassesAndNamespaces(Loc, &OVE, AssociatedNamespaces,
10601 AssociatedClasses);
10602
10603 // C++ [basic.lookup.argdep]p3:
10604 // Let X be the lookup set produced by unqualified lookup (3.4.1)
10605 // and let Y be the lookup set produced by argument dependent
10606 // lookup (defined as follows). If X contains [...] then Y is
10607 // empty. Otherwise Y is the set of declarations found in the
10608 // namespaces associated with the argument types as described
10609 // below. The set of declarations found by the lookup of the name
10610 // is the union of X and Y.
10611 //
10612 // Here, we compute Y and add its members to the overloaded
10613 // candidate set.
10614 for (auto *NS : AssociatedNamespaces) {
10615 // When considering an associated namespace, the lookup is the
10616 // same as the lookup performed when the associated namespace is
10617 // used as a qualifier (3.4.3.2) except that:
10618 //
10619 // -- Any using-directives in the associated namespace are
10620 // ignored.
10621 //
10622 // -- Any namespace-scope friend functions declared in
10623 // associated classes are visible within their respective
10624 // namespaces even if they are not visible during an ordinary
10625 // lookup (11.4).
10626 DeclContext::lookup_result R = NS->lookup(ReductionId.getName());
10627 for (auto *D : R) {
10628 auto *Underlying = D;
10629 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10630 Underlying = USD->getTargetDecl();
10631
10632 if (!isa<OMPDeclareReductionDecl>(Underlying))
10633 continue;
10634
10635 if (!SemaRef.isVisible(D)) {
10636 D = findAcceptableDecl(SemaRef, D);
10637 if (!D)
10638 continue;
10639 if (auto *USD = dyn_cast<UsingShadowDecl>(D))
10640 Underlying = USD->getTargetDecl();
10641 }
10642 Lookups.emplace_back();
10643 Lookups.back().addDecl(Underlying);
10644 }
10645 }
10646}
10647
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010648static ExprResult
10649buildDeclareReductionRef(Sema &SemaRef, SourceLocation Loc, SourceRange Range,
10650 Scope *S, CXXScopeSpec &ReductionIdScopeSpec,
10651 const DeclarationNameInfo &ReductionId, QualType Ty,
10652 CXXCastPath &BasePath, Expr *UnresolvedReduction) {
10653 if (ReductionIdScopeSpec.isInvalid())
10654 return ExprError();
10655 SmallVector<UnresolvedSet<8>, 4> Lookups;
10656 if (S) {
10657 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10658 Lookup.suppressDiagnostics();
10659 while (S && SemaRef.LookupParsedName(Lookup, S, &ReductionIdScopeSpec)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010660 NamedDecl *D = Lookup.getRepresentativeDecl();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010661 do {
10662 S = S->getParent();
10663 } while (S && !S->isDeclScope(D));
10664 if (S)
10665 S = S->getParent();
Alexey Bataev43b90b72018-09-12 16:31:59 +000010666 Lookups.emplace_back();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010667 Lookups.back().append(Lookup.begin(), Lookup.end());
10668 Lookup.clear();
10669 }
10670 } else if (auto *ULE =
10671 cast_or_null<UnresolvedLookupExpr>(UnresolvedReduction)) {
10672 Lookups.push_back(UnresolvedSet<8>());
10673 Decl *PrevD = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000010674 for (NamedDecl *D : ULE->decls()) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010675 if (D == PrevD)
10676 Lookups.push_back(UnresolvedSet<8>());
10677 else if (auto *DRD = cast<OMPDeclareReductionDecl>(D))
10678 Lookups.back().addDecl(DRD);
10679 PrevD = D;
10680 }
10681 }
Alexey Bataevfdc20352017-08-25 15:43:55 +000010682 if (SemaRef.CurContext->isDependentContext() || Ty->isDependentType() ||
10683 Ty->isInstantiationDependentType() ||
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010684 Ty->containsUnexpandedParameterPack() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000010685 filterLookupForUDR<bool>(Lookups, [](ValueDecl *D) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010686 return !D->isInvalidDecl() &&
10687 (D->getType()->isDependentType() ||
10688 D->getType()->isInstantiationDependentType() ||
10689 D->getType()->containsUnexpandedParameterPack());
10690 })) {
10691 UnresolvedSet<8> ResSet;
Alexey Bataeve3727102018-04-18 15:57:46 +000010692 for (const UnresolvedSet<8> &Set : Lookups) {
Alexey Bataev43b90b72018-09-12 16:31:59 +000010693 if (Set.empty())
10694 continue;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010695 ResSet.append(Set.begin(), Set.end());
10696 // The last item marks the end of all declarations at the specified scope.
10697 ResSet.addDecl(Set[Set.size() - 1]);
10698 }
10699 return UnresolvedLookupExpr::Create(
10700 SemaRef.Context, /*NamingClass=*/nullptr,
10701 ReductionIdScopeSpec.getWithLocInContext(SemaRef.Context), ReductionId,
10702 /*ADL=*/true, /*Overloaded=*/true, ResSet.begin(), ResSet.end());
10703 }
Alexey Bataev43b90b72018-09-12 16:31:59 +000010704 // Lookup inside the classes.
10705 // C++ [over.match.oper]p3:
10706 // For a unary operator @ with an operand of a type whose
10707 // cv-unqualified version is T1, and for a binary operator @ with
10708 // a left operand of a type whose cv-unqualified version is T1 and
10709 // a right operand of a type whose cv-unqualified version is T2,
10710 // three sets of candidate functions, designated member
10711 // candidates, non-member candidates and built-in candidates, are
10712 // constructed as follows:
10713 // -- If T1 is a complete class type or a class currently being
10714 // defined, the set of member candidates is the result of the
10715 // qualified lookup of T1::operator@ (13.3.1.1.1); otherwise,
10716 // the set of member candidates is empty.
10717 LookupResult Lookup(SemaRef, ReductionId, Sema::LookupOMPReductionName);
10718 Lookup.suppressDiagnostics();
10719 if (const auto *TyRec = Ty->getAs<RecordType>()) {
10720 // Complete the type if it can be completed.
10721 // If the type is neither complete nor being defined, bail out now.
10722 if (SemaRef.isCompleteType(Loc, Ty) || TyRec->isBeingDefined() ||
10723 TyRec->getDecl()->getDefinition()) {
10724 Lookup.clear();
10725 SemaRef.LookupQualifiedName(Lookup, TyRec->getDecl());
10726 if (Lookup.empty()) {
10727 Lookups.emplace_back();
10728 Lookups.back().append(Lookup.begin(), Lookup.end());
10729 }
10730 }
10731 }
10732 // Perform ADL.
10733 argumentDependentLookup(SemaRef, ReductionId, Loc, Ty, Lookups);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010734 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10735 Lookups, [&SemaRef, Ty](ValueDecl *D) -> ValueDecl * {
10736 if (!D->isInvalidDecl() &&
10737 SemaRef.Context.hasSameType(D->getType(), Ty))
10738 return D;
10739 return nullptr;
10740 }))
10741 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10742 if (auto *VD = filterLookupForUDR<ValueDecl *>(
10743 Lookups, [&SemaRef, Ty, Loc](ValueDecl *D) -> ValueDecl * {
10744 if (!D->isInvalidDecl() &&
10745 SemaRef.IsDerivedFrom(Loc, Ty, D->getType()) &&
10746 !Ty.isMoreQualifiedThan(D->getType()))
10747 return D;
10748 return nullptr;
10749 })) {
10750 CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
10751 /*DetectVirtual=*/false);
10752 if (SemaRef.IsDerivedFrom(Loc, Ty, VD->getType(), Paths)) {
10753 if (!Paths.isAmbiguous(SemaRef.Context.getCanonicalType(
10754 VD->getType().getUnqualifiedType()))) {
10755 if (SemaRef.CheckBaseClassAccess(Loc, VD->getType(), Ty, Paths.front(),
10756 /*DiagID=*/0) !=
10757 Sema::AR_inaccessible) {
10758 SemaRef.BuildBasePathArray(Paths, BasePath);
10759 return SemaRef.BuildDeclRefExpr(VD, Ty, VK_LValue, Loc);
10760 }
10761 }
10762 }
10763 }
10764 if (ReductionIdScopeSpec.isSet()) {
10765 SemaRef.Diag(Loc, diag::err_omp_not_resolved_reduction_identifier) << Range;
10766 return ExprError();
10767 }
10768 return ExprEmpty();
10769}
10770
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010771namespace {
10772/// Data for the reduction-based clauses.
10773struct ReductionData {
10774 /// List of original reduction items.
10775 SmallVector<Expr *, 8> Vars;
10776 /// List of private copies of the reduction items.
10777 SmallVector<Expr *, 8> Privates;
10778 /// LHS expressions for the reduction_op expressions.
10779 SmallVector<Expr *, 8> LHSs;
10780 /// RHS expressions for the reduction_op expressions.
10781 SmallVector<Expr *, 8> RHSs;
10782 /// Reduction operation expression.
10783 SmallVector<Expr *, 8> ReductionOps;
Alexey Bataev88202be2017-07-27 13:20:36 +000010784 /// Taskgroup descriptors for the corresponding reduction items in
10785 /// in_reduction clauses.
10786 SmallVector<Expr *, 8> TaskgroupDescriptors;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010787 /// List of captures for clause.
10788 SmallVector<Decl *, 4> ExprCaptures;
10789 /// List of postupdate expressions.
10790 SmallVector<Expr *, 4> ExprPostUpdates;
10791 ReductionData() = delete;
10792 /// Reserves required memory for the reduction data.
10793 ReductionData(unsigned Size) {
10794 Vars.reserve(Size);
10795 Privates.reserve(Size);
10796 LHSs.reserve(Size);
10797 RHSs.reserve(Size);
10798 ReductionOps.reserve(Size);
Alexey Bataev88202be2017-07-27 13:20:36 +000010799 TaskgroupDescriptors.reserve(Size);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010800 ExprCaptures.reserve(Size);
10801 ExprPostUpdates.reserve(Size);
10802 }
10803 /// Stores reduction item and reduction operation only (required for dependent
10804 /// reduction item).
10805 void push(Expr *Item, Expr *ReductionOp) {
10806 Vars.emplace_back(Item);
10807 Privates.emplace_back(nullptr);
10808 LHSs.emplace_back(nullptr);
10809 RHSs.emplace_back(nullptr);
10810 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010811 TaskgroupDescriptors.emplace_back(nullptr);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010812 }
10813 /// Stores reduction data.
Alexey Bataev88202be2017-07-27 13:20:36 +000010814 void push(Expr *Item, Expr *Private, Expr *LHS, Expr *RHS, Expr *ReductionOp,
10815 Expr *TaskgroupDescriptor) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010816 Vars.emplace_back(Item);
10817 Privates.emplace_back(Private);
10818 LHSs.emplace_back(LHS);
10819 RHSs.emplace_back(RHS);
10820 ReductionOps.emplace_back(ReductionOp);
Alexey Bataev88202be2017-07-27 13:20:36 +000010821 TaskgroupDescriptors.emplace_back(TaskgroupDescriptor);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010822 }
10823};
10824} // namespace
10825
Alexey Bataeve3727102018-04-18 15:57:46 +000010826static bool checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010827 ASTContext &Context, const OMPArraySectionExpr *OASE, bool &SingleElement,
10828 SmallVectorImpl<llvm::APSInt> &ArraySizes) {
10829 const Expr *Length = OASE->getLength();
10830 if (Length == nullptr) {
10831 // For array sections of the form [1:] or [:], we would need to analyze
10832 // the lower bound...
10833 if (OASE->getColonLoc().isValid())
10834 return false;
10835
10836 // This is an array subscript which has implicit length 1!
10837 SingleElement = true;
10838 ArraySizes.push_back(llvm::APSInt::get(1));
10839 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010840 Expr::EvalResult Result;
10841 if (!Length->EvaluateAsInt(Result, Context))
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010842 return false;
10843
Fangrui Song407659a2018-11-30 23:41:18 +000010844 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010845 SingleElement = (ConstantLengthValue.getSExtValue() == 1);
10846 ArraySizes.push_back(ConstantLengthValue);
10847 }
10848
10849 // Get the base of this array section and walk up from there.
10850 const Expr *Base = OASE->getBase()->IgnoreParenImpCasts();
10851
10852 // We require length = 1 for all array sections except the right-most to
10853 // guarantee that the memory region is contiguous and has no holes in it.
10854 while (const auto *TempOASE = dyn_cast<OMPArraySectionExpr>(Base)) {
10855 Length = TempOASE->getLength();
10856 if (Length == nullptr) {
10857 // For array sections of the form [1:] or [:], we would need to analyze
10858 // the lower bound...
10859 if (OASE->getColonLoc().isValid())
10860 return false;
10861
10862 // This is an array subscript which has implicit length 1!
10863 ArraySizes.push_back(llvm::APSInt::get(1));
10864 } else {
Fangrui Song407659a2018-11-30 23:41:18 +000010865 Expr::EvalResult Result;
10866 if (!Length->EvaluateAsInt(Result, Context))
10867 return false;
10868
10869 llvm::APSInt ConstantLengthValue = Result.Val.getInt();
10870 if (ConstantLengthValue.getSExtValue() != 1)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000010871 return false;
10872
10873 ArraySizes.push_back(ConstantLengthValue);
10874 }
10875 Base = TempOASE->getBase()->IgnoreParenImpCasts();
10876 }
10877
10878 // If we have a single element, we don't need to add the implicit lengths.
10879 if (!SingleElement) {
10880 while (const auto *TempASE = dyn_cast<ArraySubscriptExpr>(Base)) {
10881 // Has implicit length 1!
10882 ArraySizes.push_back(llvm::APSInt::get(1));
10883 Base = TempASE->getBase()->IgnoreParenImpCasts();
10884 }
10885 }
10886
10887 // This array section can be privatized as a single value or as a constant
10888 // sized array.
10889 return true;
10890}
10891
Alexey Bataeve3727102018-04-18 15:57:46 +000010892static bool actOnOMPReductionKindClause(
Alexey Bataev169d96a2017-07-18 20:17:46 +000010893 Sema &S, DSAStackTy *Stack, OpenMPClauseKind ClauseKind,
10894 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
10895 SourceLocation ColonLoc, SourceLocation EndLoc,
10896 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010897 ArrayRef<Expr *> UnresolvedReductions, ReductionData &RD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000010898 DeclarationName DN = ReductionId.getName();
10899 OverloadedOperatorKind OOK = DN.getCXXOverloadedOperator();
Alexey Bataevc5e02582014-06-16 07:08:35 +000010900 BinaryOperatorKind BOK = BO_Comma;
10901
Alexey Bataevfad872fc2017-07-18 15:32:58 +000010902 ASTContext &Context = S.Context;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010903 // OpenMP [2.14.3.6, reduction clause]
10904 // C
10905 // reduction-identifier is either an identifier or one of the following
10906 // operators: +, -, *, &, |, ^, && and ||
10907 // C++
10908 // reduction-identifier is either an id-expression or one of the following
10909 // operators: +, -, *, &, |, ^, && and ||
Alexey Bataevc5e02582014-06-16 07:08:35 +000010910 switch (OOK) {
10911 case OO_Plus:
10912 case OO_Minus:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010913 BOK = BO_Add;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010914 break;
10915 case OO_Star:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010916 BOK = BO_Mul;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010917 break;
10918 case OO_Amp:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010919 BOK = BO_And;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010920 break;
10921 case OO_Pipe:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010922 BOK = BO_Or;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010923 break;
10924 case OO_Caret:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010925 BOK = BO_Xor;
Alexey Bataevc5e02582014-06-16 07:08:35 +000010926 break;
10927 case OO_AmpAmp:
10928 BOK = BO_LAnd;
10929 break;
10930 case OO_PipePipe:
10931 BOK = BO_LOr;
10932 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010933 case OO_New:
10934 case OO_Delete:
10935 case OO_Array_New:
10936 case OO_Array_Delete:
10937 case OO_Slash:
10938 case OO_Percent:
10939 case OO_Tilde:
10940 case OO_Exclaim:
10941 case OO_Equal:
10942 case OO_Less:
10943 case OO_Greater:
10944 case OO_LessEqual:
10945 case OO_GreaterEqual:
10946 case OO_PlusEqual:
10947 case OO_MinusEqual:
10948 case OO_StarEqual:
10949 case OO_SlashEqual:
10950 case OO_PercentEqual:
10951 case OO_CaretEqual:
10952 case OO_AmpEqual:
10953 case OO_PipeEqual:
10954 case OO_LessLess:
10955 case OO_GreaterGreater:
10956 case OO_LessLessEqual:
10957 case OO_GreaterGreaterEqual:
10958 case OO_EqualEqual:
10959 case OO_ExclaimEqual:
Richard Smithd30b23d2017-12-01 02:13:10 +000010960 case OO_Spaceship:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010961 case OO_PlusPlus:
10962 case OO_MinusMinus:
10963 case OO_Comma:
10964 case OO_ArrowStar:
10965 case OO_Arrow:
10966 case OO_Call:
10967 case OO_Subscript:
10968 case OO_Conditional:
Richard Smith9be594e2015-10-22 05:12:22 +000010969 case OO_Coawait:
Alexey Bataev794ba0d2015-04-10 10:43:45 +000010970 case NUM_OVERLOADED_OPERATORS:
10971 llvm_unreachable("Unexpected reduction identifier");
10972 case OO_None:
Alexey Bataeve3727102018-04-18 15:57:46 +000010973 if (IdentifierInfo *II = DN.getAsIdentifierInfo()) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010974 if (II->isStr("max"))
10975 BOK = BO_GT;
10976 else if (II->isStr("min"))
10977 BOK = BO_LT;
10978 }
10979 break;
10980 }
10981 SourceRange ReductionIdRange;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010982 if (ReductionIdScopeSpec.isValid())
Alexey Bataevc5e02582014-06-16 07:08:35 +000010983 ReductionIdRange.setBegin(ReductionIdScopeSpec.getBeginLoc());
Alexey Bataev4d4624c2017-07-20 16:47:47 +000010984 else
10985 ReductionIdRange.setBegin(ReductionId.getBeginLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010986 ReductionIdRange.setEnd(ReductionId.getEndLoc());
Alexey Bataevc5e02582014-06-16 07:08:35 +000010987
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010988 auto IR = UnresolvedReductions.begin(), ER = UnresolvedReductions.end();
10989 bool FirstIter = true;
Alexey Bataeve3727102018-04-18 15:57:46 +000010990 for (Expr *RefExpr : VarList) {
Alexey Bataevc5e02582014-06-16 07:08:35 +000010991 assert(RefExpr && "nullptr expr in OpenMP reduction clause.");
Alexey Bataevc5e02582014-06-16 07:08:35 +000010992 // OpenMP [2.1, C/C++]
10993 // A list item is a variable or array section, subject to the restrictions
10994 // specified in Section 2.4 on page 42 and in each of the sections
10995 // describing clauses and directives for which a list appears.
10996 // OpenMP [2.14.3.3, Restrictions, p.1]
10997 // A variable that is part of another variable (as an array or
10998 // structure element) cannot appear in a private clause.
Alexey Bataeva839ddd2016-03-17 10:19:46 +000010999 if (!FirstIter && IR != ER)
11000 ++IR;
11001 FirstIter = false;
Alexey Bataev60da77e2016-02-29 05:54:20 +000011002 SourceLocation ELoc;
11003 SourceRange ERange;
11004 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011005 auto Res = getPrivateItem(S, SimpleRefExpr, ELoc, ERange,
Alexey Bataev60da77e2016-02-29 05:54:20 +000011006 /*AllowArraySection=*/true);
11007 if (Res.second) {
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011008 // Try to find 'declare reduction' corresponding construct before using
11009 // builtin/overloaded operators.
11010 QualType Type = Context.DependentTy;
11011 CXXCastPath BasePath;
11012 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011013 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011014 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011015 Expr *ReductionOp = nullptr;
11016 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011017 (DeclareReductionRef.isUnset() ||
11018 isa<UnresolvedLookupExpr>(DeclareReductionRef.get())))
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011019 ReductionOp = DeclareReductionRef.get();
11020 // It will be analyzed later.
11021 RD.push(RefExpr, ReductionOp);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011022 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011023 ValueDecl *D = Res.first;
11024 if (!D)
11025 continue;
11026
Alexey Bataev88202be2017-07-27 13:20:36 +000011027 Expr *TaskgroupDescriptor = nullptr;
Alexey Bataeva1764212015-09-30 09:22:36 +000011028 QualType Type;
Alexey Bataev60da77e2016-02-29 05:54:20 +000011029 auto *ASE = dyn_cast<ArraySubscriptExpr>(RefExpr->IgnoreParens());
11030 auto *OASE = dyn_cast<OMPArraySectionExpr>(RefExpr->IgnoreParens());
Alexey Bataeve3727102018-04-18 15:57:46 +000011031 if (ASE) {
Alexey Bataev31300ed2016-02-04 11:27:03 +000011032 Type = ASE->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011033 } else if (OASE) {
11034 QualType BaseType =
11035 OMPArraySectionExpr::getBaseOriginalType(OASE->getBase());
11036 if (const auto *ATy = BaseType->getAsArrayTypeUnsafe())
Alexey Bataeva1764212015-09-30 09:22:36 +000011037 Type = ATy->getElementType();
11038 else
11039 Type = BaseType->getPointeeType();
Alexey Bataev31300ed2016-02-04 11:27:03 +000011040 Type = Type.getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011041 } else {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011042 Type = Context.getBaseElementType(D->getType().getNonReferenceType());
Alexey Bataeve3727102018-04-18 15:57:46 +000011043 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011044 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataeva1764212015-09-30 09:22:36 +000011045
Alexey Bataevc5e02582014-06-16 07:08:35 +000011046 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
11047 // A variable that appears in a private clause must not have an incomplete
11048 // type or a reference type.
Joel E. Denny3cabf732018-06-28 19:54:49 +000011049 if (S.RequireCompleteType(ELoc, D->getType(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011050 diag::err_omp_reduction_incomplete_type))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011051 continue;
11052 // OpenMP [2.14.3.6, reduction clause, Restrictions]
Alexey Bataevc5e02582014-06-16 07:08:35 +000011053 // A list item that appears in a reduction clause must not be
11054 // const-qualified.
Joel E. Dennyd2649292019-01-04 22:11:56 +000011055 if (rejectConstNotMutableType(S, D, Type, ClauseKind, ELoc,
11056 /*AcceptIfMutable*/ false, ASE || OASE))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011057 continue;
Alexey Bataevbc529672018-09-28 19:33:14 +000011058
11059 OpenMPDirectiveKind CurrDir = Stack->getCurrentDirective();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011060 // OpenMP [2.9.3.6, Restrictions, C/C++, p.4]
11061 // If a list-item is a reference type then it must bind to the same object
11062 // for all threads of the team.
Alexey Bataevbc529672018-09-28 19:33:14 +000011063 if (!ASE && !OASE) {
11064 if (VD) {
11065 VarDecl *VDDef = VD->getDefinition();
11066 if (VD->getType()->isReferenceType() && VDDef && VDDef->hasInit()) {
11067 DSARefChecker Check(Stack);
11068 if (Check.Visit(VDDef->getInit())) {
11069 S.Diag(ELoc, diag::err_omp_reduction_ref_type_arg)
11070 << getOpenMPClauseName(ClauseKind) << ERange;
11071 S.Diag(VDDef->getLocation(), diag::note_defined_here) << VDDef;
11072 continue;
11073 }
Alexey Bataeva1764212015-09-30 09:22:36 +000011074 }
Alexey Bataevc5e02582014-06-16 07:08:35 +000011075 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011076
Alexey Bataevbc529672018-09-28 19:33:14 +000011077 // OpenMP [2.14.1.1, Data-sharing Attribute Rules for Variables Referenced
11078 // in a Construct]
11079 // Variables with the predetermined data-sharing attributes may not be
11080 // listed in data-sharing attributes clauses, except for the cases
11081 // listed below. For these exceptions only, listing a predetermined
11082 // variable in a data-sharing attribute clause is allowed and overrides
11083 // the variable's predetermined data-sharing attributes.
11084 // OpenMP [2.14.3.6, Restrictions, p.3]
11085 // Any number of reduction clauses can be specified on the directive,
11086 // but a list item can appear only once in the reduction clauses for that
11087 // directive.
11088 DSAStackTy::DSAVarData DVar = Stack->getTopDSA(D, /*FromParent=*/false);
11089 if (DVar.CKind == OMPC_reduction) {
11090 S.Diag(ELoc, diag::err_omp_once_referenced)
11091 << getOpenMPClauseName(ClauseKind);
11092 if (DVar.RefExpr)
11093 S.Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_referenced);
11094 continue;
11095 }
11096 if (DVar.CKind != OMPC_unknown) {
11097 S.Diag(ELoc, diag::err_omp_wrong_dsa)
11098 << getOpenMPClauseName(DVar.CKind)
11099 << getOpenMPClauseName(OMPC_reduction);
Alexey Bataeve3727102018-04-18 15:57:46 +000011100 reportOriginalDsa(S, Stack, D, DVar);
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011101 continue;
Alexey Bataevf29276e2014-06-18 04:14:57 +000011102 }
Alexey Bataevbc529672018-09-28 19:33:14 +000011103
11104 // OpenMP [2.14.3.6, Restrictions, p.1]
11105 // A list item that appears in a reduction clause of a worksharing
11106 // construct must be shared in the parallel regions to which any of the
11107 // worksharing regions arising from the worksharing construct bind.
11108 if (isOpenMPWorksharingDirective(CurrDir) &&
11109 !isOpenMPParallelDirective(CurrDir) &&
11110 !isOpenMPTeamsDirective(CurrDir)) {
11111 DVar = Stack->getImplicitDSA(D, true);
11112 if (DVar.CKind != OMPC_shared) {
11113 S.Diag(ELoc, diag::err_omp_required_access)
11114 << getOpenMPClauseName(OMPC_reduction)
11115 << getOpenMPClauseName(OMPC_shared);
11116 reportOriginalDsa(S, Stack, D, DVar);
11117 continue;
11118 }
11119 }
Alexey Bataevf29276e2014-06-18 04:14:57 +000011120 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011121
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011122 // Try to find 'declare reduction' corresponding construct before using
11123 // builtin/overloaded operators.
11124 CXXCastPath BasePath;
11125 ExprResult DeclareReductionRef = buildDeclareReductionRef(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011126 S, ELoc, ERange, Stack->getCurScope(), ReductionIdScopeSpec,
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011127 ReductionId, Type, BasePath, IR == ER ? nullptr : *IR);
11128 if (DeclareReductionRef.isInvalid())
11129 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011130 if (S.CurContext->isDependentContext() &&
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011131 (DeclareReductionRef.isUnset() ||
11132 isa<UnresolvedLookupExpr>(DeclareReductionRef.get()))) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011133 RD.push(RefExpr, DeclareReductionRef.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011134 continue;
11135 }
11136 if (BOK == BO_Comma && DeclareReductionRef.isUnset()) {
11137 // Not allowed reduction identifier is found.
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011138 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011139 diag::err_omp_unknown_reduction_identifier)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011140 << Type << ReductionIdRange;
11141 continue;
11142 }
11143
11144 // OpenMP [2.14.3.6, reduction clause, Restrictions]
11145 // The type of a list item that appears in a reduction clause must be valid
11146 // for the reduction-identifier. For a max or min reduction in C, the type
11147 // of the list item must be an allowed arithmetic data type: char, int,
11148 // float, double, or _Bool, possibly modified with long, short, signed, or
11149 // unsigned. For a max or min reduction in C++, the type of the list item
11150 // must be an allowed arithmetic data type: char, wchar_t, int, float,
11151 // double, or bool, possibly modified with long, short, signed, or unsigned.
11152 if (DeclareReductionRef.isUnset()) {
11153 if ((BOK == BO_GT || BOK == BO_LT) &&
11154 !(Type->isScalarType() ||
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011155 (S.getLangOpts().CPlusPlus && Type->isArithmeticType()))) {
11156 S.Diag(ELoc, diag::err_omp_clause_not_arithmetic_type_arg)
Alexey Bataev169d96a2017-07-18 20:17:46 +000011157 << getOpenMPClauseName(ClauseKind) << S.getLangOpts().CPlusPlus;
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011158 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011159 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11160 VarDecl::DeclarationOnly;
11161 S.Diag(D->getLocation(),
11162 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011163 << D;
11164 }
11165 continue;
11166 }
11167 if ((BOK == BO_OrAssign || BOK == BO_AndAssign || BOK == BO_XorAssign) &&
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011168 !S.getLangOpts().CPlusPlus && Type->isFloatingType()) {
Alexey Bataev169d96a2017-07-18 20:17:46 +000011169 S.Diag(ELoc, diag::err_omp_clause_floating_type_arg)
11170 << getOpenMPClauseName(ClauseKind);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011171 if (!ASE && !OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011172 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11173 VarDecl::DeclarationOnly;
11174 S.Diag(D->getLocation(),
11175 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011176 << D;
11177 }
11178 continue;
11179 }
11180 }
11181
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011182 Type = Type.getNonLValueExprType(Context).getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011183 VarDecl *LHSVD = buildVarDecl(S, ELoc, Type, ".reduction.lhs",
11184 D->hasAttrs() ? &D->getAttrs() : nullptr);
11185 VarDecl *RHSVD = buildVarDecl(S, ELoc, Type, D->getName(),
11186 D->hasAttrs() ? &D->getAttrs() : nullptr);
11187 QualType PrivateTy = Type;
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011188
11189 // Try if we can determine constant lengths for all array sections and avoid
11190 // the VLA.
11191 bool ConstantLengthOASE = false;
11192 if (OASE) {
11193 bool SingleElement;
11194 llvm::SmallVector<llvm::APSInt, 4> ArraySizes;
Alexey Bataeve3727102018-04-18 15:57:46 +000011195 ConstantLengthOASE = checkOMPArraySectionConstantForReduction(
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011196 Context, OASE, SingleElement, ArraySizes);
11197
11198 // If we don't have a single element, we must emit a constant array type.
11199 if (ConstantLengthOASE && !SingleElement) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011200 for (llvm::APSInt &Size : ArraySizes)
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011201 PrivateTy = Context.getConstantArrayType(
11202 PrivateTy, Size, ArrayType::Normal, /*IndexTypeQuals=*/0);
Jonas Hahnfeld4525c822017-10-23 19:01:35 +000011203 }
11204 }
11205
11206 if ((OASE && !ConstantLengthOASE) ||
Jonas Hahnfeld96087f32017-11-02 13:30:42 +000011207 (!OASE && !ASE &&
Alexey Bataev60da77e2016-02-29 05:54:20 +000011208 D->getType().getNonReferenceType()->isVariablyModifiedType())) {
Jonas Hahnfeld87d44262017-11-18 21:00:46 +000011209 if (!Context.getTargetInfo().isVLASupported() &&
11210 S.shouldDiagnoseTargetSupportFromOpenMP()) {
11211 S.Diag(ELoc, diag::err_omp_reduction_vla_unsupported) << !!OASE;
11212 S.Diag(ELoc, diag::note_vla_unsupported);
11213 continue;
11214 }
David Majnemer9d168222016-08-05 17:44:54 +000011215 // For arrays/array sections only:
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011216 // Create pseudo array type for private copy. The size for this array will
11217 // be generated during codegen.
11218 // For array subscripts or single variables Private Ty is the same as Type
11219 // (type of the variable or single array element).
11220 PrivateTy = Context.getVariableArrayType(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011221 Type,
Alexey Bataevd070a582017-10-25 15:54:04 +000011222 new (Context) OpaqueValueExpr(ELoc, Context.getSizeType(), VK_RValue),
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011223 ArrayType::Normal, /*IndexTypeQuals=*/0, SourceRange());
Alexey Bataev60da77e2016-02-29 05:54:20 +000011224 } else if (!ASE && !OASE &&
Alexey Bataeve3727102018-04-18 15:57:46 +000011225 Context.getAsArrayType(D->getType().getNonReferenceType())) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011226 PrivateTy = D->getType().getNonReferenceType();
Alexey Bataeve3727102018-04-18 15:57:46 +000011227 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011228 // Private copy.
Alexey Bataeve3727102018-04-18 15:57:46 +000011229 VarDecl *PrivateVD =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011230 buildVarDecl(S, ELoc, PrivateTy, D->getName(),
11231 D->hasAttrs() ? &D->getAttrs() : nullptr,
11232 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011233 // Add initializer for private variable.
11234 Expr *Init = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011235 DeclRefExpr *LHSDRE = buildDeclRefExpr(S, LHSVD, Type, ELoc);
11236 DeclRefExpr *RHSDRE = buildDeclRefExpr(S, RHSVD, Type, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011237 if (DeclareReductionRef.isUsable()) {
11238 auto *DRDRef = DeclareReductionRef.getAs<DeclRefExpr>();
11239 auto *DRD = cast<OMPDeclareReductionDecl>(DRDRef->getDecl());
11240 if (DRD->getInitializer()) {
11241 Init = DRDRef;
11242 RHSVD->setInit(DRDRef);
11243 RHSVD->setInitStyle(VarDecl::CallInit);
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011244 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011245 } else {
11246 switch (BOK) {
11247 case BO_Add:
11248 case BO_Xor:
11249 case BO_Or:
11250 case BO_LOr:
11251 // '+', '-', '^', '|', '||' reduction ops - initializer is '0'.
11252 if (Type->isScalarType() || Type->isAnyComplexType())
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011253 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/0).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011254 break;
11255 case BO_Mul:
11256 case BO_LAnd:
11257 if (Type->isScalarType() || Type->isAnyComplexType()) {
11258 // '*' and '&&' reduction ops - initializer is '1'.
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011259 Init = S.ActOnIntegerConstant(ELoc, /*Val=*/1).get();
Alexey Bataevc5e02582014-06-16 07:08:35 +000011260 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011261 break;
11262 case BO_And: {
11263 // '&' reduction op - initializer is '~0'.
11264 QualType OrigType = Type;
11265 if (auto *ComplexTy = OrigType->getAs<ComplexType>())
11266 Type = ComplexTy->getElementType();
11267 if (Type->isRealFloatingType()) {
11268 llvm::APFloat InitValue =
11269 llvm::APFloat::getAllOnesValue(Context.getTypeSize(Type),
11270 /*isIEEE=*/true);
11271 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11272 Type, ELoc);
11273 } else if (Type->isScalarType()) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011274 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011275 QualType IntTy = Context.getIntTypeForBitwidth(Size, /*Signed=*/0);
11276 llvm::APInt InitValue = llvm::APInt::getAllOnesValue(Size);
11277 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11278 }
11279 if (Init && OrigType->isAnyComplexType()) {
11280 // Init = 0xFFFF + 0xFFFFi;
11281 auto *Im = new (Context) ImaginaryLiteral(Init, OrigType);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011282 Init = S.CreateBuiltinBinOp(ELoc, BO_Add, Init, Im).get();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011283 }
11284 Type = OrigType;
11285 break;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011286 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011287 case BO_LT:
11288 case BO_GT: {
11289 // 'min' reduction op - initializer is 'Largest representable number in
11290 // the reduction list item type'.
11291 // 'max' reduction op - initializer is 'Least representable number in
11292 // the reduction list item type'.
11293 if (Type->isIntegerType() || Type->isPointerType()) {
11294 bool IsSigned = Type->hasSignedIntegerRepresentation();
Alexey Bataeve3727102018-04-18 15:57:46 +000011295 uint64_t Size = Context.getTypeSize(Type);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011296 QualType IntTy =
11297 Context.getIntTypeForBitwidth(Size, /*Signed=*/IsSigned);
11298 llvm::APInt InitValue =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011299 (BOK != BO_LT) ? IsSigned ? llvm::APInt::getSignedMinValue(Size)
11300 : llvm::APInt::getMinValue(Size)
11301 : IsSigned ? llvm::APInt::getSignedMaxValue(Size)
11302 : llvm::APInt::getMaxValue(Size);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011303 Init = IntegerLiteral::Create(Context, InitValue, IntTy, ELoc);
11304 if (Type->isPointerType()) {
11305 // Cast to pointer type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011306 ExprResult CastExpr = S.BuildCStyleCastExpr(
Alexey Bataevd070a582017-10-25 15:54:04 +000011307 ELoc, Context.getTrivialTypeSourceInfo(Type, ELoc), ELoc, Init);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011308 if (CastExpr.isInvalid())
11309 continue;
11310 Init = CastExpr.get();
11311 }
11312 } else if (Type->isRealFloatingType()) {
11313 llvm::APFloat InitValue = llvm::APFloat::getLargest(
11314 Context.getFloatTypeSemantics(Type), BOK != BO_LT);
11315 Init = FloatingLiteral::Create(Context, InitValue, /*isexact=*/true,
11316 Type, ELoc);
11317 }
11318 break;
11319 }
11320 case BO_PtrMemD:
11321 case BO_PtrMemI:
11322 case BO_MulAssign:
11323 case BO_Div:
11324 case BO_Rem:
11325 case BO_Sub:
11326 case BO_Shl:
11327 case BO_Shr:
11328 case BO_LE:
11329 case BO_GE:
11330 case BO_EQ:
11331 case BO_NE:
Richard Smithc70f1d62017-12-14 15:16:18 +000011332 case BO_Cmp:
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011333 case BO_AndAssign:
11334 case BO_XorAssign:
11335 case BO_OrAssign:
11336 case BO_Assign:
11337 case BO_AddAssign:
11338 case BO_SubAssign:
11339 case BO_DivAssign:
11340 case BO_RemAssign:
11341 case BO_ShlAssign:
11342 case BO_ShrAssign:
11343 case BO_Comma:
11344 llvm_unreachable("Unexpected reduction operation");
11345 }
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011346 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011347 if (Init && DeclareReductionRef.isUnset())
11348 S.AddInitializerToDecl(RHSVD, Init, /*DirectInit=*/false);
11349 else if (!Init)
11350 S.ActOnUninitializedDecl(RHSVD);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011351 if (RHSVD->isInvalidDecl())
11352 continue;
11353 if (!RHSVD->hasInit() && DeclareReductionRef.isUnset()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011354 S.Diag(ELoc, diag::err_omp_reduction_id_not_compatible)
11355 << Type << ReductionIdRange;
11356 bool IsDecl = !VD || VD->isThisDeclarationADefinition(Context) ==
11357 VarDecl::DeclarationOnly;
11358 S.Diag(D->getLocation(),
11359 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev60da77e2016-02-29 05:54:20 +000011360 << D;
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011361 continue;
11362 }
Alexey Bataevf24e7b12015-10-08 09:10:53 +000011363 // Store initializer for single element in private copy. Will be used during
11364 // codegen.
11365 PrivateVD->setInit(RHSVD->getInit());
11366 PrivateVD->setInitStyle(RHSVD->getInitStyle());
Alexey Bataeve3727102018-04-18 15:57:46 +000011367 DeclRefExpr *PrivateDRE = buildDeclRefExpr(S, PrivateVD, PrivateTy, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011368 ExprResult ReductionOp;
11369 if (DeclareReductionRef.isUsable()) {
11370 QualType RedTy = DeclareReductionRef.get()->getType();
11371 QualType PtrRedTy = Context.getPointerType(RedTy);
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011372 ExprResult LHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, LHSDRE);
11373 ExprResult RHS = S.CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011374 if (!BasePath.empty()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011375 LHS = S.DefaultLvalueConversion(LHS.get());
11376 RHS = S.DefaultLvalueConversion(RHS.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011377 LHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11378 CK_UncheckedDerivedToBase, LHS.get(),
11379 &BasePath, LHS.get()->getValueKind());
11380 RHS = ImplicitCastExpr::Create(Context, PtrRedTy,
11381 CK_UncheckedDerivedToBase, RHS.get(),
11382 &BasePath, RHS.get()->getValueKind());
Alexey Bataev794ba0d2015-04-10 10:43:45 +000011383 }
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011384 FunctionProtoType::ExtProtoInfo EPI;
11385 QualType Params[] = {PtrRedTy, PtrRedTy};
11386 QualType FnTy = Context.getFunctionType(Context.VoidTy, Params, EPI);
11387 auto *OVE = new (Context) OpaqueValueExpr(
11388 ELoc, Context.getPointerType(FnTy), VK_RValue, OK_Ordinary,
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011389 S.DefaultLvalueConversion(DeclareReductionRef.get()).get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011390 Expr *Args[] = {LHS.get(), RHS.get()};
Bruno Riccic5885cf2018-12-21 15:20:32 +000011391 ReductionOp =
11392 CallExpr::Create(Context, OVE, Args, Context.VoidTy, VK_RValue, ELoc);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011393 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011394 ReductionOp = S.BuildBinOp(
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011395 Stack->getCurScope(), ReductionId.getBeginLoc(), BOK, LHSDRE, RHSDRE);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011396 if (ReductionOp.isUsable()) {
11397 if (BOK != BO_LT && BOK != BO_GT) {
11398 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011399 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011400 BO_Assign, LHSDRE, ReductionOp.get());
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011401 } else {
Alexey Bataevd070a582017-10-25 15:54:04 +000011402 auto *ConditionalOp = new (Context)
11403 ConditionalOperator(ReductionOp.get(), ELoc, LHSDRE, ELoc, RHSDRE,
11404 Type, VK_LValue, OK_Ordinary);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011405 ReductionOp =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011406 S.BuildBinOp(Stack->getCurScope(), ReductionId.getBeginLoc(),
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011407 BO_Assign, LHSDRE, ConditionalOp);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011408 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011409 if (ReductionOp.isUsable())
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011410 ReductionOp = S.ActOnFinishFullExpr(ReductionOp.get(),
11411 /*DiscardedValue*/ false);
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011412 }
Alexey Bataev4d4624c2017-07-20 16:47:47 +000011413 if (!ReductionOp.isUsable())
Alexey Bataeva839ddd2016-03-17 10:19:46 +000011414 continue;
Alexey Bataevc5e02582014-06-16 07:08:35 +000011415 }
11416
Alexey Bataevfa312f32017-07-21 18:48:21 +000011417 // OpenMP [2.15.4.6, Restrictions, p.2]
11418 // A list item that appears in an in_reduction clause of a task construct
11419 // must appear in a task_reduction clause of a construct associated with a
11420 // taskgroup region that includes the participating task in its taskgroup
11421 // set. The construct associated with the innermost region that meets this
11422 // condition must specify the same reduction-identifier as the in_reduction
11423 // clause.
11424 if (ClauseKind == OMPC_in_reduction) {
Alexey Bataevfa312f32017-07-21 18:48:21 +000011425 SourceRange ParentSR;
11426 BinaryOperatorKind ParentBOK;
11427 const Expr *ParentReductionOp;
Alexey Bataev88202be2017-07-27 13:20:36 +000011428 Expr *ParentBOKTD, *ParentReductionOpTD;
Alexey Bataevf189cb72017-07-24 14:52:13 +000011429 DSAStackTy::DSAVarData ParentBOKDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011430 Stack->getTopMostTaskgroupReductionData(D, ParentSR, ParentBOK,
11431 ParentBOKTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011432 DSAStackTy::DSAVarData ParentReductionOpDSA =
Alexey Bataev88202be2017-07-27 13:20:36 +000011433 Stack->getTopMostTaskgroupReductionData(
11434 D, ParentSR, ParentReductionOp, ParentReductionOpTD);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011435 bool IsParentBOK = ParentBOKDSA.DKind != OMPD_unknown;
11436 bool IsParentReductionOp = ParentReductionOpDSA.DKind != OMPD_unknown;
11437 if (!IsParentBOK && !IsParentReductionOp) {
11438 S.Diag(ELoc, diag::err_omp_in_reduction_not_task_reduction);
11439 continue;
11440 }
Alexey Bataevfa312f32017-07-21 18:48:21 +000011441 if ((DeclareReductionRef.isUnset() && IsParentReductionOp) ||
11442 (DeclareReductionRef.isUsable() && IsParentBOK) || BOK != ParentBOK ||
11443 IsParentReductionOp) {
11444 bool EmitError = true;
11445 if (IsParentReductionOp && DeclareReductionRef.isUsable()) {
11446 llvm::FoldingSetNodeID RedId, ParentRedId;
11447 ParentReductionOp->Profile(ParentRedId, Context, /*Canonical=*/true);
11448 DeclareReductionRef.get()->Profile(RedId, Context,
11449 /*Canonical=*/true);
11450 EmitError = RedId != ParentRedId;
11451 }
11452 if (EmitError) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011453 S.Diag(ReductionId.getBeginLoc(),
Alexey Bataevfa312f32017-07-21 18:48:21 +000011454 diag::err_omp_reduction_identifier_mismatch)
11455 << ReductionIdRange << RefExpr->getSourceRange();
11456 S.Diag(ParentSR.getBegin(),
11457 diag::note_omp_previous_reduction_identifier)
Alexey Bataevf189cb72017-07-24 14:52:13 +000011458 << ParentSR
11459 << (IsParentBOK ? ParentBOKDSA.RefExpr
11460 : ParentReductionOpDSA.RefExpr)
11461 ->getSourceRange();
Alexey Bataevfa312f32017-07-21 18:48:21 +000011462 continue;
11463 }
11464 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011465 TaskgroupDescriptor = IsParentBOK ? ParentBOKTD : ParentReductionOpTD;
11466 assert(TaskgroupDescriptor && "Taskgroup descriptor must be defined.");
Alexey Bataevfa312f32017-07-21 18:48:21 +000011467 }
11468
Alexey Bataev60da77e2016-02-29 05:54:20 +000011469 DeclRefExpr *Ref = nullptr;
11470 Expr *VarsExpr = RefExpr->IgnoreParens();
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011471 if (!VD && !S.CurContext->isDependentContext()) {
Alexey Bataev60da77e2016-02-29 05:54:20 +000011472 if (ASE || OASE) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011473 TransformExprToCaptures RebuildToCapture(S, D);
Alexey Bataev60da77e2016-02-29 05:54:20 +000011474 VarsExpr =
11475 RebuildToCapture.TransformExpr(RefExpr->IgnoreParens()).get();
11476 Ref = RebuildToCapture.getCapturedExpr();
Alexey Bataev61205072016-03-02 04:57:40 +000011477 } else {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011478 VarsExpr = Ref = buildCapture(S, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011479 }
Alexey Bataeve3727102018-04-18 15:57:46 +000011480 if (!S.isOpenMPCapturedDecl(D)) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011481 RD.ExprCaptures.emplace_back(Ref->getDecl());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011482 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011483 ExprResult RefRes = S.DefaultLvalueConversion(Ref);
Alexey Bataev5a3af132016-03-29 08:58:54 +000011484 if (!RefRes.isUsable())
11485 continue;
11486 ExprResult PostUpdateRes =
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011487 S.BuildBinOp(Stack->getCurScope(), ELoc, BO_Assign, SimpleRefExpr,
11488 RefRes.get());
Alexey Bataev5a3af132016-03-29 08:58:54 +000011489 if (!PostUpdateRes.isUsable())
11490 continue;
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011491 if (isOpenMPTaskingDirective(Stack->getCurrentDirective()) ||
11492 Stack->getCurrentDirective() == OMPD_taskgroup) {
11493 S.Diag(RefExpr->getExprLoc(),
11494 diag::err_omp_reduction_non_addressable_expression)
Alexey Bataevbcd0ae02017-07-11 19:16:44 +000011495 << RefExpr->getSourceRange();
11496 continue;
11497 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011498 RD.ExprPostUpdates.emplace_back(
11499 S.IgnoredValueConversions(PostUpdateRes.get()).get());
Alexey Bataev61205072016-03-02 04:57:40 +000011500 }
11501 }
Alexey Bataev60da77e2016-02-29 05:54:20 +000011502 }
Alexey Bataev169d96a2017-07-18 20:17:46 +000011503 // All reduction items are still marked as reduction (to do not increase
11504 // code base size).
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011505 Stack->addDSA(D, RefExpr->IgnoreParens(), OMPC_reduction, Ref);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011506 if (CurrDir == OMPD_taskgroup) {
11507 if (DeclareReductionRef.isUsable())
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011508 Stack->addTaskgroupReductionData(D, ReductionIdRange,
11509 DeclareReductionRef.get());
Alexey Bataevf189cb72017-07-24 14:52:13 +000011510 else
Alexey Bataev3b1b8952017-07-25 15:53:26 +000011511 Stack->addTaskgroupReductionData(D, ReductionIdRange, BOK);
Alexey Bataevf189cb72017-07-24 14:52:13 +000011512 }
Alexey Bataev88202be2017-07-27 13:20:36 +000011513 RD.push(VarsExpr, PrivateDRE, LHSDRE, RHSDRE, ReductionOp.get(),
11514 TaskgroupDescriptor);
Alexey Bataevc5e02582014-06-16 07:08:35 +000011515 }
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011516 return RD.Vars.empty();
11517}
Alexey Bataevc5e02582014-06-16 07:08:35 +000011518
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011519OMPClause *Sema::ActOnOpenMPReductionClause(
11520 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11521 SourceLocation ColonLoc, SourceLocation EndLoc,
11522 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11523 ArrayRef<Expr *> UnresolvedReductions) {
11524 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011525 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_reduction, VarList,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011526 StartLoc, LParenLoc, ColonLoc, EndLoc,
11527 ReductionIdScopeSpec, ReductionId,
11528 UnresolvedReductions, RD))
Alexey Bataevc5e02582014-06-16 07:08:35 +000011529 return nullptr;
Alexey Bataev61205072016-03-02 04:57:40 +000011530
Alexey Bataevc5e02582014-06-16 07:08:35 +000011531 return OMPReductionClause::Create(
Alexey Bataevfad872fc2017-07-18 15:32:58 +000011532 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11533 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11534 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11535 buildPreInits(Context, RD.ExprCaptures),
11536 buildPostUpdate(*this, RD.ExprPostUpdates));
Alexey Bataevc5e02582014-06-16 07:08:35 +000011537}
11538
Alexey Bataev169d96a2017-07-18 20:17:46 +000011539OMPClause *Sema::ActOnOpenMPTaskReductionClause(
11540 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11541 SourceLocation ColonLoc, SourceLocation EndLoc,
11542 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11543 ArrayRef<Expr *> UnresolvedReductions) {
11544 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011545 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_task_reduction, VarList,
11546 StartLoc, LParenLoc, ColonLoc, EndLoc,
11547 ReductionIdScopeSpec, ReductionId,
Alexey Bataev169d96a2017-07-18 20:17:46 +000011548 UnresolvedReductions, RD))
11549 return nullptr;
11550
11551 return OMPTaskReductionClause::Create(
11552 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11553 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
11554 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps,
11555 buildPreInits(Context, RD.ExprCaptures),
11556 buildPostUpdate(*this, RD.ExprPostUpdates));
11557}
11558
Alexey Bataevfa312f32017-07-21 18:48:21 +000011559OMPClause *Sema::ActOnOpenMPInReductionClause(
11560 ArrayRef<Expr *> VarList, SourceLocation StartLoc, SourceLocation LParenLoc,
11561 SourceLocation ColonLoc, SourceLocation EndLoc,
11562 CXXScopeSpec &ReductionIdScopeSpec, const DeclarationNameInfo &ReductionId,
11563 ArrayRef<Expr *> UnresolvedReductions) {
11564 ReductionData RD(VarList.size());
Alexey Bataeve3727102018-04-18 15:57:46 +000011565 if (actOnOMPReductionKindClause(*this, DSAStack, OMPC_in_reduction, VarList,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011566 StartLoc, LParenLoc, ColonLoc, EndLoc,
11567 ReductionIdScopeSpec, ReductionId,
11568 UnresolvedReductions, RD))
11569 return nullptr;
11570
11571 return OMPInReductionClause::Create(
11572 Context, StartLoc, LParenLoc, ColonLoc, EndLoc, RD.Vars,
11573 ReductionIdScopeSpec.getWithLocInContext(Context), ReductionId,
Alexey Bataev88202be2017-07-27 13:20:36 +000011574 RD.Privates, RD.LHSs, RD.RHSs, RD.ReductionOps, RD.TaskgroupDescriptors,
Alexey Bataevfa312f32017-07-21 18:48:21 +000011575 buildPreInits(Context, RD.ExprCaptures),
11576 buildPostUpdate(*this, RD.ExprPostUpdates));
11577}
11578
Alexey Bataevecba70f2016-04-12 11:02:11 +000011579bool Sema::CheckOpenMPLinearModifier(OpenMPLinearClauseKind LinKind,
11580 SourceLocation LinLoc) {
11581 if ((!LangOpts.CPlusPlus && LinKind != OMPC_LINEAR_val) ||
11582 LinKind == OMPC_LINEAR_unknown) {
11583 Diag(LinLoc, diag::err_omp_wrong_linear_modifier) << LangOpts.CPlusPlus;
11584 return true;
11585 }
11586 return false;
11587}
11588
Alexey Bataeve3727102018-04-18 15:57:46 +000011589bool Sema::CheckOpenMPLinearDecl(const ValueDecl *D, SourceLocation ELoc,
Alexey Bataevecba70f2016-04-12 11:02:11 +000011590 OpenMPLinearClauseKind LinKind,
11591 QualType Type) {
Alexey Bataeve3727102018-04-18 15:57:46 +000011592 const auto *VD = dyn_cast_or_null<VarDecl>(D);
Alexey Bataevecba70f2016-04-12 11:02:11 +000011593 // A variable must not have an incomplete type or a reference type.
11594 if (RequireCompleteType(ELoc, Type, diag::err_omp_linear_incomplete_type))
11595 return true;
11596 if ((LinKind == OMPC_LINEAR_uval || LinKind == OMPC_LINEAR_ref) &&
11597 !Type->isReferenceType()) {
11598 Diag(ELoc, diag::err_omp_wrong_linear_modifier_non_reference)
11599 << Type << getOpenMPSimpleClauseTypeName(OMPC_linear, LinKind);
11600 return true;
11601 }
11602 Type = Type.getNonReferenceType();
11603
Joel E. Dennybae586f2019-01-04 22:12:13 +000011604 // OpenMP 5.0 [2.19.3, List Item Privatization, Restrictions]
11605 // A variable that is privatized must not have a const-qualified type
11606 // unless it is of class type with a mutable member. This restriction does
11607 // not apply to the firstprivate clause.
11608 if (rejectConstNotMutableType(*this, D, Type, OMPC_linear, ELoc))
Alexey Bataevecba70f2016-04-12 11:02:11 +000011609 return true;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011610
11611 // A list item must be of integral or pointer type.
11612 Type = Type.getUnqualifiedType().getCanonicalType();
11613 const auto *Ty = Type.getTypePtrOrNull();
11614 if (!Ty || (!Ty->isDependentType() && !Ty->isIntegralType(Context) &&
11615 !Ty->isPointerType())) {
11616 Diag(ELoc, diag::err_omp_linear_expected_int_or_ptr) << Type;
11617 if (D) {
11618 bool IsDecl =
11619 !VD ||
11620 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
11621 Diag(D->getLocation(),
11622 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
11623 << D;
11624 }
11625 return true;
11626 }
11627 return false;
11628}
11629
Alexey Bataev182227b2015-08-20 10:54:39 +000011630OMPClause *Sema::ActOnOpenMPLinearClause(
11631 ArrayRef<Expr *> VarList, Expr *Step, SourceLocation StartLoc,
11632 SourceLocation LParenLoc, OpenMPLinearClauseKind LinKind,
11633 SourceLocation LinLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011634 SmallVector<Expr *, 8> Vars;
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011635 SmallVector<Expr *, 8> Privates;
Alexander Musman3276a272015-03-21 10:12:56 +000011636 SmallVector<Expr *, 8> Inits;
Alexey Bataev78849fb2016-03-09 09:49:00 +000011637 SmallVector<Decl *, 4> ExprCaptures;
11638 SmallVector<Expr *, 4> ExprPostUpdates;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011639 if (CheckOpenMPLinearModifier(LinKind, LinLoc))
Alexey Bataev182227b2015-08-20 10:54:39 +000011640 LinKind = OMPC_LINEAR_val;
Alexey Bataeve3727102018-04-18 15:57:46 +000011641 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011642 assert(RefExpr && "NULL expr in OpenMP linear clause.");
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011643 SourceLocation ELoc;
11644 SourceRange ERange;
11645 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011646 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011647 if (Res.second) {
Alexander Musman8dba6642014-04-22 13:09:42 +000011648 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011649 Vars.push_back(RefExpr);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011650 Privates.push_back(nullptr);
Alexander Musman3276a272015-03-21 10:12:56 +000011651 Inits.push_back(nullptr);
Alexander Musman8dba6642014-04-22 13:09:42 +000011652 }
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011653 ValueDecl *D = Res.first;
11654 if (!D)
Alexander Musman8dba6642014-04-22 13:09:42 +000011655 continue;
Alexander Musman8dba6642014-04-22 13:09:42 +000011656
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011657 QualType Type = D->getType();
11658 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musman8dba6642014-04-22 13:09:42 +000011659
11660 // OpenMP [2.14.3.7, linear clause]
11661 // A list-item cannot appear in more than one linear clause.
11662 // A list-item that appears in a linear clause cannot appear in any
11663 // other data-sharing attribute clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011664 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexander Musman8dba6642014-04-22 13:09:42 +000011665 if (DVar.RefExpr) {
11666 Diag(ELoc, diag::err_omp_wrong_dsa) << getOpenMPClauseName(DVar.CKind)
11667 << getOpenMPClauseName(OMPC_linear);
Alexey Bataeve3727102018-04-18 15:57:46 +000011668 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexander Musman8dba6642014-04-22 13:09:42 +000011669 continue;
11670 }
11671
Alexey Bataevecba70f2016-04-12 11:02:11 +000011672 if (CheckOpenMPLinearDecl(D, ELoc, LinKind, Type))
Alexander Musman8dba6642014-04-22 13:09:42 +000011673 continue;
Alexey Bataevecba70f2016-04-12 11:02:11 +000011674 Type = Type.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musman8dba6642014-04-22 13:09:42 +000011675
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011676 // Build private copy of original var.
Alexey Bataeve3727102018-04-18 15:57:46 +000011677 VarDecl *Private =
Alexey Bataev63cc8e92018-03-20 14:45:59 +000011678 buildVarDecl(*this, ELoc, Type, D->getName(),
11679 D->hasAttrs() ? &D->getAttrs() : nullptr,
11680 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011681 DeclRefExpr *PrivateRef = buildDeclRefExpr(*this, Private, Type, ELoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011682 // Build var to save initial value.
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011683 VarDecl *Init = buildVarDecl(*this, ELoc, Type, ".linear.start");
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011684 Expr *InitExpr;
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011685 DeclRefExpr *Ref = nullptr;
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011686 if (!VD && !CurContext->isDependentContext()) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011687 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011688 if (!isOpenMPCapturedDecl(D)) {
Alexey Bataev78849fb2016-03-09 09:49:00 +000011689 ExprCaptures.push_back(Ref->getDecl());
11690 if (Ref->getDecl()->hasAttr<OMPCaptureNoInitAttr>()) {
11691 ExprResult RefRes = DefaultLvalueConversion(Ref);
11692 if (!RefRes.isUsable())
11693 continue;
11694 ExprResult PostUpdateRes =
11695 BuildBinOp(DSAStack->getCurScope(), ELoc, BO_Assign,
11696 SimpleRefExpr, RefRes.get());
11697 if (!PostUpdateRes.isUsable())
11698 continue;
11699 ExprPostUpdates.push_back(
11700 IgnoredValueConversions(PostUpdateRes.get()).get());
11701 }
11702 }
11703 }
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011704 if (LinKind == OMPC_LINEAR_uval)
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011705 InitExpr = VD ? VD->getInit() : SimpleRefExpr;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011706 else
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011707 InitExpr = VD ? SimpleRefExpr : Ref;
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011708 AddInitializerToDecl(Init, DefaultLvalueConversion(InitExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000011709 /*DirectInit=*/false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011710 DeclRefExpr *InitRef = buildDeclRefExpr(*this, Init, Type, ELoc);
Alexey Bataev2bbf7212016-03-03 03:52:24 +000011711
11712 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_linear, Ref);
Alexey Bataevbe8b8b52016-07-07 11:04:06 +000011713 Vars.push_back((VD || CurContext->isDependentContext())
11714 ? RefExpr->IgnoreParens()
11715 : Ref);
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011716 Privates.push_back(PrivateRef);
Alexander Musman3276a272015-03-21 10:12:56 +000011717 Inits.push_back(InitRef);
Alexander Musman8dba6642014-04-22 13:09:42 +000011718 }
11719
11720 if (Vars.empty())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011721 return nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011722
11723 Expr *StepExpr = Step;
Alexander Musman3276a272015-03-21 10:12:56 +000011724 Expr *CalcStepExpr = nullptr;
Alexander Musman8dba6642014-04-22 13:09:42 +000011725 if (Step && !Step->isValueDependent() && !Step->isTypeDependent() &&
11726 !Step->isInstantiationDependent() &&
11727 !Step->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011728 SourceLocation StepLoc = Step->getBeginLoc();
Alexander Musmana8e9d2e2014-06-03 10:16:47 +000011729 ExprResult Val = PerformOpenMPImplicitIntegerConversion(StepLoc, Step);
Alexander Musman8dba6642014-04-22 13:09:42 +000011730 if (Val.isInvalid())
Alexander Musmancb7f9c42014-05-15 13:04:49 +000011731 return nullptr;
Nikola Smiljanic01a75982014-05-29 10:55:11 +000011732 StepExpr = Val.get();
Alexander Musman8dba6642014-04-22 13:09:42 +000011733
Alexander Musman3276a272015-03-21 10:12:56 +000011734 // Build var to save the step value.
11735 VarDecl *SaveVar =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011736 buildVarDecl(*this, StepLoc, StepExpr->getType(), ".linear.step");
Alexander Musman3276a272015-03-21 10:12:56 +000011737 ExprResult SaveRef =
Alexey Bataev39f915b82015-05-08 10:41:21 +000011738 buildDeclRefExpr(*this, SaveVar, StepExpr->getType(), StepLoc);
Alexander Musman3276a272015-03-21 10:12:56 +000011739 ExprResult CalcStep =
11740 BuildBinOp(CurScope, StepLoc, BO_Assign, SaveRef.get(), StepExpr);
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011741 CalcStep = ActOnFinishFullExpr(CalcStep.get(), /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011742
Alexander Musman8dba6642014-04-22 13:09:42 +000011743 // Warn about zero linear step (it would be probably better specified as
11744 // making corresponding variables 'const').
11745 llvm::APSInt Result;
Alexander Musman3276a272015-03-21 10:12:56 +000011746 bool IsConstant = StepExpr->isIntegerConstantExpr(Result, Context);
11747 if (IsConstant && !Result.isNegative() && !Result.isStrictlyPositive())
Alexander Musman8dba6642014-04-22 13:09:42 +000011748 Diag(StepLoc, diag::warn_omp_linear_step_zero) << Vars[0]
11749 << (Vars.size() > 1);
Alexander Musman3276a272015-03-21 10:12:56 +000011750 if (!IsConstant && CalcStep.isUsable()) {
11751 // Calculate the step beforehand instead of doing this on each iteration.
11752 // (This is not used if the number of iterations may be kfold-ed).
11753 CalcStepExpr = CalcStep.get();
11754 }
Alexander Musman8dba6642014-04-22 13:09:42 +000011755 }
11756
Alexey Bataev182227b2015-08-20 10:54:39 +000011757 return OMPLinearClause::Create(Context, StartLoc, LParenLoc, LinKind, LinLoc,
11758 ColonLoc, EndLoc, Vars, Privates, Inits,
Alexey Bataev5a3af132016-03-29 08:58:54 +000011759 StepExpr, CalcStepExpr,
11760 buildPreInits(Context, ExprCaptures),
11761 buildPostUpdate(*this, ExprPostUpdates));
Alexander Musman3276a272015-03-21 10:12:56 +000011762}
11763
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011764static bool FinishOpenMPLinearClause(OMPLinearClause &Clause, DeclRefExpr *IV,
11765 Expr *NumIterations, Sema &SemaRef,
11766 Scope *S, DSAStackTy *Stack) {
Alexander Musman3276a272015-03-21 10:12:56 +000011767 // Walk the vars and build update/final expressions for the CodeGen.
11768 SmallVector<Expr *, 8> Updates;
11769 SmallVector<Expr *, 8> Finals;
11770 Expr *Step = Clause.getStep();
11771 Expr *CalcStep = Clause.getCalcStep();
11772 // OpenMP [2.14.3.7, linear clause]
11773 // If linear-step is not specified it is assumed to be 1.
Alexey Bataeve3727102018-04-18 15:57:46 +000011774 if (!Step)
Alexander Musman3276a272015-03-21 10:12:56 +000011775 Step = SemaRef.ActOnIntegerConstant(SourceLocation(), 1).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000011776 else if (CalcStep)
Alexander Musman3276a272015-03-21 10:12:56 +000011777 Step = cast<BinaryOperator>(CalcStep)->getLHS();
11778 bool HasErrors = false;
11779 auto CurInit = Clause.inits().begin();
Alexey Bataevbd9fec12015-08-18 06:47:21 +000011780 auto CurPrivate = Clause.privates().begin();
Alexey Bataeve3727102018-04-18 15:57:46 +000011781 OpenMPLinearClauseKind LinKind = Clause.getModifier();
11782 for (Expr *RefExpr : Clause.varlists()) {
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011783 SourceLocation ELoc;
11784 SourceRange ERange;
11785 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011786 auto Res = getPrivateItem(SemaRef, SimpleRefExpr, ELoc, ERange);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011787 ValueDecl *D = Res.first;
11788 if (Res.second || !D) {
11789 Updates.push_back(nullptr);
11790 Finals.push_back(nullptr);
11791 HasErrors = true;
11792 continue;
11793 }
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011794 auto &&Info = Stack->isLoopControlVariable(D);
Alexey Bataev2b86f212017-11-29 21:31:48 +000011795 // OpenMP [2.15.11, distribute simd Construct]
11796 // A list item may not appear in a linear clause, unless it is the loop
11797 // iteration variable.
11798 if (isOpenMPDistributeDirective(Stack->getCurrentDirective()) &&
11799 isOpenMPSimdDirective(Stack->getCurrentDirective()) && !Info.first) {
11800 SemaRef.Diag(ELoc,
11801 diag::err_omp_linear_distribute_var_non_loop_iteration);
11802 Updates.push_back(nullptr);
11803 Finals.push_back(nullptr);
11804 HasErrors = true;
11805 continue;
11806 }
Alexander Musman3276a272015-03-21 10:12:56 +000011807 Expr *InitExpr = *CurInit;
11808
11809 // Build privatized reference to the current linear var.
David Majnemer9d168222016-08-05 17:44:54 +000011810 auto *DE = cast<DeclRefExpr>(SimpleRefExpr);
Alexey Bataev84cfb1d2015-08-21 06:41:23 +000011811 Expr *CapturedRef;
11812 if (LinKind == OMPC_LINEAR_uval)
11813 CapturedRef = cast<VarDecl>(DE->getDecl())->getInit();
11814 else
11815 CapturedRef =
11816 buildDeclRefExpr(SemaRef, cast<VarDecl>(DE->getDecl()),
11817 DE->getType().getUnqualifiedType(), DE->getExprLoc(),
11818 /*RefersToCapture=*/true);
Alexander Musman3276a272015-03-21 10:12:56 +000011819
11820 // Build update: Var = InitExpr + IV * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011821 ExprResult Update;
Alexey Bataeve3727102018-04-18 15:57:46 +000011822 if (!Info.first)
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011823 Update =
Alexey Bataeve3727102018-04-18 15:57:46 +000011824 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), *CurPrivate,
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011825 InitExpr, IV, Step, /* Subtract */ false);
Alexey Bataeve3727102018-04-18 15:57:46 +000011826 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011827 Update = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011828 Update = SemaRef.ActOnFinishFullExpr(Update.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011829 /*DiscardedValue*/ false);
Alexander Musman3276a272015-03-21 10:12:56 +000011830
11831 // Build final: Var = InitExpr + NumIterations * Step
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011832 ExprResult Final;
Alexey Bataeve3727102018-04-18 15:57:46 +000011833 if (!Info.first)
11834 Final =
11835 buildCounterUpdate(SemaRef, S, RefExpr->getExprLoc(), CapturedRef,
11836 InitExpr, NumIterations, Step, /*Subtract=*/false);
11837 else
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011838 Final = *CurPrivate;
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011839 Final = SemaRef.ActOnFinishFullExpr(Final.get(), DE->getBeginLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000011840 /*DiscardedValue*/ false);
Alexey Bataev5dff95c2016-04-22 03:56:56 +000011841
Alexander Musman3276a272015-03-21 10:12:56 +000011842 if (!Update.isUsable() || !Final.isUsable()) {
11843 Updates.push_back(nullptr);
11844 Finals.push_back(nullptr);
11845 HasErrors = true;
11846 } else {
11847 Updates.push_back(Update.get());
11848 Finals.push_back(Final.get());
11849 }
Richard Trieucc3949d2016-02-18 22:34:54 +000011850 ++CurInit;
11851 ++CurPrivate;
Alexander Musman3276a272015-03-21 10:12:56 +000011852 }
11853 Clause.setUpdates(Updates);
11854 Clause.setFinals(Finals);
11855 return HasErrors;
Alexander Musman8dba6642014-04-22 13:09:42 +000011856}
11857
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011858OMPClause *Sema::ActOnOpenMPAlignedClause(
11859 ArrayRef<Expr *> VarList, Expr *Alignment, SourceLocation StartLoc,
11860 SourceLocation LParenLoc, SourceLocation ColonLoc, SourceLocation EndLoc) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011861 SmallVector<Expr *, 8> Vars;
Alexey Bataeve3727102018-04-18 15:57:46 +000011862 for (Expr *RefExpr : VarList) {
Alexey Bataev1efd1662016-03-29 10:59:56 +000011863 assert(RefExpr && "NULL expr in OpenMP linear clause.");
11864 SourceLocation ELoc;
11865 SourceRange ERange;
11866 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000011867 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataev1efd1662016-03-29 10:59:56 +000011868 if (Res.second) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011869 // It will be analyzed later.
11870 Vars.push_back(RefExpr);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011871 }
Alexey Bataev1efd1662016-03-29 10:59:56 +000011872 ValueDecl *D = Res.first;
11873 if (!D)
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011874 continue;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011875
Alexey Bataev1efd1662016-03-29 10:59:56 +000011876 QualType QType = D->getType();
11877 auto *VD = dyn_cast<VarDecl>(D);
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011878
11879 // OpenMP [2.8.1, simd construct, Restrictions]
11880 // The type of list items appearing in the aligned clause must be
11881 // array, pointer, reference to array, or reference to pointer.
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011882 QType = QType.getNonReferenceType().getUnqualifiedType().getCanonicalType();
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011883 const Type *Ty = QType.getTypePtrOrNull();
Alexey Bataev1efd1662016-03-29 10:59:56 +000011884 if (!Ty || (!Ty->isArrayType() && !Ty->isPointerType())) {
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011885 Diag(ELoc, diag::err_omp_aligned_expected_array_or_ptr)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011886 << QType << getLangOpts().CPlusPlus << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011887 bool IsDecl =
Alexey Bataev1efd1662016-03-29 10:59:56 +000011888 !VD ||
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011889 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataev1efd1662016-03-29 10:59:56 +000011890 Diag(D->getLocation(),
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011891 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataev1efd1662016-03-29 10:59:56 +000011892 << D;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011893 continue;
11894 }
11895
11896 // OpenMP [2.8.1, simd construct, Restrictions]
11897 // A list-item cannot appear in more than one aligned clause.
Alexey Bataeve3727102018-04-18 15:57:46 +000011898 if (const Expr *PrevRef = DSAStack->addUniqueAligned(D, SimpleRefExpr)) {
Alexey Bataevd93d3762016-04-12 09:35:56 +000011899 Diag(ELoc, diag::err_omp_aligned_twice) << 0 << ERange;
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011900 Diag(PrevRef->getExprLoc(), diag::note_omp_explicit_dsa)
11901 << getOpenMPClauseName(OMPC_aligned);
11902 continue;
11903 }
11904
Alexey Bataev1efd1662016-03-29 10:59:56 +000011905 DeclRefExpr *Ref = nullptr;
Alexey Bataeve3727102018-04-18 15:57:46 +000011906 if (!VD && isOpenMPCapturedDecl(D))
Alexey Bataev1efd1662016-03-29 10:59:56 +000011907 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
11908 Vars.push_back(DefaultFunctionArrayConversion(
11909 (VD || !Ref) ? RefExpr->IgnoreParens() : Ref)
11910 .get());
Alexander Musmanf0d76e72014-05-29 14:36:25 +000011911 }
11912
11913 // OpenMP [2.8.1, simd construct, Description]
11914 // The parameter of the aligned clause, alignment, must be a constant
11915 // positive integer expression.
11916 // If no optional parameter is specified, implementation-defined default
11917 // alignments for SIMD instructions on the target platforms are assumed.
11918 if (Alignment != nullptr) {
11919 ExprResult AlignResult =
11920 VerifyPositiveIntegerConstantInClause(Alignment, OMPC_aligned);
11921 if (AlignResult.isInvalid())
11922 return nullptr;
11923 Alignment = AlignResult.get();
11924 }
11925 if (Vars.empty())
11926 return nullptr;
11927
11928 return OMPAlignedClause::Create(Context, StartLoc, LParenLoc, ColonLoc,
11929 EndLoc, Vars, Alignment);
11930}
11931
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011932OMPClause *Sema::ActOnOpenMPCopyinClause(ArrayRef<Expr *> VarList,
11933 SourceLocation StartLoc,
11934 SourceLocation LParenLoc,
11935 SourceLocation EndLoc) {
11936 SmallVector<Expr *, 8> Vars;
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011937 SmallVector<Expr *, 8> SrcExprs;
11938 SmallVector<Expr *, 8> DstExprs;
11939 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000011940 for (Expr *RefExpr : VarList) {
Alexey Bataeved09d242014-05-28 05:53:51 +000011941 assert(RefExpr && "NULL expr in OpenMP copyin clause.");
11942 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011943 // It will be analyzed later.
Alexey Bataeved09d242014-05-28 05:53:51 +000011944 Vars.push_back(RefExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011945 SrcExprs.push_back(nullptr);
11946 DstExprs.push_back(nullptr);
11947 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011948 continue;
11949 }
11950
Alexey Bataeved09d242014-05-28 05:53:51 +000011951 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011952 // OpenMP [2.1, C/C++]
11953 // A list item is a variable name.
11954 // OpenMP [2.14.4.1, Restrictions, p.1]
11955 // A list item that appears in a copyin clause must be threadprivate.
Alexey Bataeve3727102018-04-18 15:57:46 +000011956 auto *DE = dyn_cast<DeclRefExpr>(RefExpr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011957 if (!DE || !isa<VarDecl>(DE->getDecl())) {
Alexey Bataev48c0bfb2016-01-20 09:07:54 +000011958 Diag(ELoc, diag::err_omp_expected_var_name_member_expr)
11959 << 0 << RefExpr->getSourceRange();
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011960 continue;
11961 }
11962
11963 Decl *D = DE->getDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000011964 auto *VD = cast<VarDecl>(D);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011965
11966 QualType Type = VD->getType();
11967 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
11968 // It will be analyzed later.
11969 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000011970 SrcExprs.push_back(nullptr);
11971 DstExprs.push_back(nullptr);
11972 AssignmentOps.push_back(nullptr);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011973 continue;
11974 }
11975
11976 // OpenMP [2.14.4.1, Restrictions, C/C++, p.1]
11977 // A list item that appears in a copyin clause must be threadprivate.
11978 if (!DSAStack->isThreadPrivate(VD)) {
11979 Diag(ELoc, diag::err_omp_required_access)
Alexey Bataeved09d242014-05-28 05:53:51 +000011980 << getOpenMPClauseName(OMPC_copyin)
11981 << getOpenMPDirectiveName(OMPD_threadprivate);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011982 continue;
11983 }
11984
11985 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
11986 // A variable of class type (or array thereof) that appears in a
Alexey Bataev23b69422014-06-18 07:08:49 +000011987 // copyin clause requires an accessible, unambiguous copy assignment
Alexey Bataevd48bcd82014-03-31 03:36:38 +000011988 // operator for the class type.
Alexey Bataeve3727102018-04-18 15:57:46 +000011989 QualType ElemType = Context.getBaseElementType(Type).getNonReferenceType();
11990 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011991 buildVarDecl(*this, DE->getBeginLoc(), ElemType.getUnqualifiedType(),
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011992 ".copyin.src", VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011993 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011994 *this, SrcVD, ElemType.getUnqualifiedType(), DE->getExprLoc());
Alexey Bataeve3727102018-04-18 15:57:46 +000011995 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000011996 buildVarDecl(*this, DE->getBeginLoc(), ElemType, ".copyin.dst",
Alexey Bataev1d7f0fa2015-09-10 09:48:30 +000011997 VD->hasAttrs() ? &VD->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000011998 DeclRefExpr *PseudoDstExpr =
Alexey Bataevf120c0d2015-05-19 07:46:42 +000011999 buildDeclRefExpr(*this, DstVD, ElemType, DE->getExprLoc());
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012000 // For arrays generate assignment operation for single element and replace
12001 // it by the original array element in CodeGen.
Alexey Bataeve3727102018-04-18 15:57:46 +000012002 ExprResult AssignmentOp =
12003 BuildBinOp(/*S=*/nullptr, DE->getExprLoc(), BO_Assign, PseudoDstExpr,
12004 PseudoSrcExpr);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012005 if (AssignmentOp.isInvalid())
12006 continue;
12007 AssignmentOp = ActOnFinishFullExpr(AssignmentOp.get(), DE->getExprLoc(),
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012008 /*DiscardedValue*/ false);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012009 if (AssignmentOp.isInvalid())
12010 continue;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012011
12012 DSAStack->addDSA(VD, DE, OMPC_copyin);
12013 Vars.push_back(DE);
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012014 SrcExprs.push_back(PseudoSrcExpr);
12015 DstExprs.push_back(PseudoDstExpr);
12016 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012017 }
12018
Alexey Bataeved09d242014-05-28 05:53:51 +000012019 if (Vars.empty())
12020 return nullptr;
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012021
Alexey Bataevf56f98c2015-04-16 05:39:01 +000012022 return OMPCopyinClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars,
12023 SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevd48bcd82014-03-31 03:36:38 +000012024}
12025
Alexey Bataevbae9a792014-06-27 10:37:06 +000012026OMPClause *Sema::ActOnOpenMPCopyprivateClause(ArrayRef<Expr *> VarList,
12027 SourceLocation StartLoc,
12028 SourceLocation LParenLoc,
12029 SourceLocation EndLoc) {
12030 SmallVector<Expr *, 8> Vars;
Alexey Bataeva63048e2015-03-23 06:18:07 +000012031 SmallVector<Expr *, 8> SrcExprs;
12032 SmallVector<Expr *, 8> DstExprs;
12033 SmallVector<Expr *, 8> AssignmentOps;
Alexey Bataeve3727102018-04-18 15:57:46 +000012034 for (Expr *RefExpr : VarList) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012035 assert(RefExpr && "NULL expr in OpenMP linear clause.");
12036 SourceLocation ELoc;
12037 SourceRange ERange;
12038 Expr *SimpleRefExpr = RefExpr;
Alexey Bataevbc529672018-09-28 19:33:14 +000012039 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
Alexey Bataeve122da12016-03-17 10:50:17 +000012040 if (Res.second) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012041 // It will be analyzed later.
12042 Vars.push_back(RefExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012043 SrcExprs.push_back(nullptr);
12044 DstExprs.push_back(nullptr);
12045 AssignmentOps.push_back(nullptr);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012046 }
Alexey Bataeve122da12016-03-17 10:50:17 +000012047 ValueDecl *D = Res.first;
12048 if (!D)
Alexey Bataevbae9a792014-06-27 10:37:06 +000012049 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012050
Alexey Bataeve122da12016-03-17 10:50:17 +000012051 QualType Type = D->getType();
12052 auto *VD = dyn_cast<VarDecl>(D);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012053
12054 // OpenMP [2.14.4.2, Restrictions, p.2]
12055 // A list item that appears in a copyprivate clause may not appear in a
12056 // private or firstprivate clause on the single construct.
Alexey Bataeve122da12016-03-17 10:50:17 +000012057 if (!VD || !DSAStack->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012058 DSAStackTy::DSAVarData DVar =
12059 DSAStack->getTopDSA(D, /*FromParent=*/false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012060 if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_copyprivate &&
12061 DVar.RefExpr) {
Alexey Bataevbae9a792014-06-27 10:37:06 +000012062 Diag(ELoc, diag::err_omp_wrong_dsa)
12063 << getOpenMPClauseName(DVar.CKind)
12064 << getOpenMPClauseName(OMPC_copyprivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000012065 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012066 continue;
12067 }
12068
12069 // OpenMP [2.11.4.2, Restrictions, p.1]
12070 // All list items that appear in a copyprivate clause must be either
12071 // threadprivate or private in the enclosing context.
12072 if (DVar.CKind == OMPC_unknown) {
Alexey Bataeve122da12016-03-17 10:50:17 +000012073 DVar = DSAStack->getImplicitDSA(D, false);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012074 if (DVar.CKind == OMPC_shared) {
12075 Diag(ELoc, diag::err_omp_required_access)
12076 << getOpenMPClauseName(OMPC_copyprivate)
12077 << "threadprivate or private in the enclosing context";
Alexey Bataeve3727102018-04-18 15:57:46 +000012078 reportOriginalDsa(*this, DSAStack, D, DVar);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012079 continue;
12080 }
12081 }
12082 }
12083
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012084 // Variably modified types are not supported.
Alexey Bataev5129d3a2015-05-21 09:47:46 +000012085 if (!Type->isAnyPointerType() && Type->isVariablyModifiedType()) {
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012086 Diag(ELoc, diag::err_omp_variably_modified_type_not_supported)
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012087 << getOpenMPClauseName(OMPC_copyprivate) << Type
12088 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012089 bool IsDecl =
Alexey Bataeve122da12016-03-17 10:50:17 +000012090 !VD ||
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012091 VD->isThisDeclarationADefinition(Context) == VarDecl::DeclarationOnly;
Alexey Bataeve122da12016-03-17 10:50:17 +000012092 Diag(D->getLocation(),
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012093 IsDecl ? diag::note_previous_decl : diag::note_defined_here)
Alexey Bataeve122da12016-03-17 10:50:17 +000012094 << D;
Alexey Bataev7a3e5852015-05-19 08:19:24 +000012095 continue;
12096 }
Alexey Bataevccb59ec2015-05-19 08:44:56 +000012097
Alexey Bataevbae9a792014-06-27 10:37:06 +000012098 // OpenMP [2.14.4.1, Restrictions, C/C++, p.2]
12099 // A variable of class type (or array thereof) that appears in a
12100 // copyin clause requires an accessible, unambiguous copy assignment
12101 // operator for the class type.
Alexey Bataevbd9fec12015-08-18 06:47:21 +000012102 Type = Context.getBaseElementType(Type.getNonReferenceType())
12103 .getUnqualifiedType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012104 VarDecl *SrcVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012105 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.src",
Alexey Bataeve122da12016-03-17 10:50:17 +000012106 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012107 DeclRefExpr *PseudoSrcExpr = buildDeclRefExpr(*this, SrcVD, Type, ELoc);
12108 VarDecl *DstVD =
Stephen Kellyf2ceec42018-08-09 21:08:08 +000012109 buildVarDecl(*this, RefExpr->getBeginLoc(), Type, ".copyprivate.dst",
Alexey Bataeve122da12016-03-17 10:50:17 +000012110 D->hasAttrs() ? &D->getAttrs() : nullptr);
Alexey Bataeve3727102018-04-18 15:57:46 +000012111 DeclRefExpr *PseudoDstExpr = buildDeclRefExpr(*this, DstVD, Type, ELoc);
12112 ExprResult AssignmentOp = BuildBinOp(
12113 DSAStack->getCurScope(), ELoc, BO_Assign, PseudoDstExpr, PseudoSrcExpr);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012114 if (AssignmentOp.isInvalid())
12115 continue;
Aaron Ballmanfb6deeb2019-01-04 16:58:14 +000012116 AssignmentOp =
12117 ActOnFinishFullExpr(AssignmentOp.get(), ELoc, /*DiscardedValue*/ false);
Alexey Bataeva63048e2015-03-23 06:18:07 +000012118 if (AssignmentOp.isInvalid())
12119 continue;
Alexey Bataevbae9a792014-06-27 10:37:06 +000012120
12121 // No need to mark vars as copyprivate, they are already threadprivate or
12122 // implicitly private.
Alexey Bataeve3727102018-04-18 15:57:46 +000012123 assert(VD || isOpenMPCapturedDecl(D));
Alexey Bataeve122da12016-03-17 10:50:17 +000012124 Vars.push_back(
12125 VD ? RefExpr->IgnoreParens()
12126 : buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/false));
Alexey Bataeva63048e2015-03-23 06:18:07 +000012127 SrcExprs.push_back(PseudoSrcExpr);
12128 DstExprs.push_back(PseudoDstExpr);
12129 AssignmentOps.push_back(AssignmentOp.get());
Alexey Bataevbae9a792014-06-27 10:37:06 +000012130 }
12131
12132 if (Vars.empty())
12133 return nullptr;
12134
Alexey Bataeva63048e2015-03-23 06:18:07 +000012135 return OMPCopyprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
12136 Vars, SrcExprs, DstExprs, AssignmentOps);
Alexey Bataevbae9a792014-06-27 10:37:06 +000012137}
12138
Alexey Bataev6125da92014-07-21 11:26:11 +000012139OMPClause *Sema::ActOnOpenMPFlushClause(ArrayRef<Expr *> VarList,
12140 SourceLocation StartLoc,
12141 SourceLocation LParenLoc,
12142 SourceLocation EndLoc) {
12143 if (VarList.empty())
12144 return nullptr;
12145
12146 return OMPFlushClause::Create(Context, StartLoc, LParenLoc, EndLoc, VarList);
12147}
Alexey Bataevdea47612014-07-23 07:46:59 +000012148
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012149OMPClause *
12150Sema::ActOnOpenMPDependClause(OpenMPDependClauseKind DepKind,
12151 SourceLocation DepLoc, SourceLocation ColonLoc,
12152 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
12153 SourceLocation LParenLoc, SourceLocation EndLoc) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012154 if (DSAStack->getCurrentDirective() == OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012155 DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink) {
Alexey Bataeveb482352015-12-18 05:05:56 +000012156 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012157 << "'source' or 'sink'" << getOpenMPClauseName(OMPC_depend);
Alexey Bataeveb482352015-12-18 05:05:56 +000012158 return nullptr;
12159 }
12160 if (DSAStack->getCurrentDirective() != OMPD_ordered &&
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012161 (DepKind == OMPC_DEPEND_unknown || DepKind == OMPC_DEPEND_source ||
12162 DepKind == OMPC_DEPEND_sink)) {
Alexey Bataev6402bca2015-12-28 07:25:51 +000012163 unsigned Except[] = {OMPC_DEPEND_source, OMPC_DEPEND_sink};
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012164 Diag(DepLoc, diag::err_omp_unexpected_clause_value)
Alexey Bataev6402bca2015-12-28 07:25:51 +000012165 << getListOfPossibleValues(OMPC_depend, /*First=*/0,
12166 /*Last=*/OMPC_DEPEND_unknown, Except)
12167 << getOpenMPClauseName(OMPC_depend);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012168 return nullptr;
12169 }
12170 SmallVector<Expr *, 8> Vars;
Alexey Bataev8b427062016-05-25 12:36:08 +000012171 DSAStackTy::OperatorOffsetTy OpsOffs;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012172 llvm::APSInt DepCounter(/*BitWidth=*/32);
12173 llvm::APSInt TotalDepCount(/*BitWidth=*/32);
Alexey Bataevf138fda2018-08-13 19:04:24 +000012174 if (DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) {
12175 if (const Expr *OrderedCountExpr =
12176 DSAStack->getParentOrderedRegionParam().first) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012177 TotalDepCount = OrderedCountExpr->EvaluateKnownConstInt(Context);
12178 TotalDepCount.setIsUnsigned(/*Val=*/true);
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012179 }
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012180 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012181 for (Expr *RefExpr : VarList) {
Alexey Bataev17daedf2018-02-15 22:42:57 +000012182 assert(RefExpr && "NULL expr in OpenMP shared clause.");
12183 if (isa<DependentScopeDeclRefExpr>(RefExpr)) {
12184 // It will be analyzed later.
12185 Vars.push_back(RefExpr);
12186 continue;
12187 }
12188
12189 SourceLocation ELoc = RefExpr->getExprLoc();
Alexey Bataeve3727102018-04-18 15:57:46 +000012190 Expr *SimpleExpr = RefExpr->IgnoreParenCasts();
Alexey Bataev17daedf2018-02-15 22:42:57 +000012191 if (DepKind == OMPC_DEPEND_sink) {
Alexey Bataevf138fda2018-08-13 19:04:24 +000012192 if (DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012193 DepCounter >= TotalDepCount) {
12194 Diag(ELoc, diag::err_omp_depend_sink_unexpected_expr);
12195 continue;
12196 }
12197 ++DepCounter;
12198 // OpenMP [2.13.9, Summary]
12199 // depend(dependence-type : vec), where dependence-type is:
12200 // 'sink' and where vec is the iteration vector, which has the form:
12201 // x1 [+- d1], x2 [+- d2 ], . . . , xn [+- dn]
12202 // where n is the value specified by the ordered clause in the loop
12203 // directive, xi denotes the loop iteration variable of the i-th nested
12204 // loop associated with the loop directive, and di is a constant
12205 // non-negative integer.
12206 if (CurContext->isDependentContext()) {
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012207 // It will be analyzed later.
12208 Vars.push_back(RefExpr);
12209 continue;
12210 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012211 SimpleExpr = SimpleExpr->IgnoreImplicit();
12212 OverloadedOperatorKind OOK = OO_None;
12213 SourceLocation OOLoc;
12214 Expr *LHS = SimpleExpr;
12215 Expr *RHS = nullptr;
12216 if (auto *BO = dyn_cast<BinaryOperator>(SimpleExpr)) {
12217 OOK = BinaryOperator::getOverloadedOperator(BO->getOpcode());
12218 OOLoc = BO->getOperatorLoc();
12219 LHS = BO->getLHS()->IgnoreParenImpCasts();
12220 RHS = BO->getRHS()->IgnoreParenImpCasts();
12221 } else if (auto *OCE = dyn_cast<CXXOperatorCallExpr>(SimpleExpr)) {
12222 OOK = OCE->getOperator();
12223 OOLoc = OCE->getOperatorLoc();
12224 LHS = OCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
12225 RHS = OCE->getArg(/*Arg=*/1)->IgnoreParenImpCasts();
12226 } else if (auto *MCE = dyn_cast<CXXMemberCallExpr>(SimpleExpr)) {
12227 OOK = MCE->getMethodDecl()
12228 ->getNameInfo()
12229 .getName()
12230 .getCXXOverloadedOperator();
12231 OOLoc = MCE->getCallee()->getExprLoc();
12232 LHS = MCE->getImplicitObjectArgument()->IgnoreParenImpCasts();
12233 RHS = MCE->getArg(/*Arg=*/0)->IgnoreParenImpCasts();
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012234 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012235 SourceLocation ELoc;
12236 SourceRange ERange;
Alexey Bataevbc529672018-09-28 19:33:14 +000012237 auto Res = getPrivateItem(*this, LHS, ELoc, ERange);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012238 if (Res.second) {
12239 // It will be analyzed later.
12240 Vars.push_back(RefExpr);
12241 }
12242 ValueDecl *D = Res.first;
12243 if (!D)
12244 continue;
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012245
Alexey Bataev17daedf2018-02-15 22:42:57 +000012246 if (OOK != OO_Plus && OOK != OO_Minus && (RHS || OOK != OO_None)) {
12247 Diag(OOLoc, diag::err_omp_depend_sink_expected_plus_minus);
12248 continue;
12249 }
12250 if (RHS) {
12251 ExprResult RHSRes = VerifyPositiveIntegerConstantInClause(
12252 RHS, OMPC_depend, /*StrictlyPositive=*/false);
12253 if (RHSRes.isInvalid())
12254 continue;
12255 }
12256 if (!CurContext->isDependentContext() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012257 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012258 DepCounter != DSAStack->isParentLoopControlVariable(D).first) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012259 const ValueDecl *VD =
Alexey Bataev17daedf2018-02-15 22:42:57 +000012260 DSAStack->getParentLoopControlVariable(DepCounter.getZExtValue());
Alexey Bataeve3727102018-04-18 15:57:46 +000012261 if (VD)
Alexey Bataev17daedf2018-02-15 22:42:57 +000012262 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration)
12263 << 1 << VD;
Alexey Bataeve3727102018-04-18 15:57:46 +000012264 else
Alexey Bataev17daedf2018-02-15 22:42:57 +000012265 Diag(ELoc, diag::err_omp_depend_sink_expected_loop_iteration) << 0;
Alexey Bataev17daedf2018-02-15 22:42:57 +000012266 continue;
12267 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012268 OpsOffs.emplace_back(RHS, OOK);
Alexey Bataev17daedf2018-02-15 22:42:57 +000012269 } else {
12270 auto *ASE = dyn_cast<ArraySubscriptExpr>(SimpleExpr);
12271 if (!RefExpr->IgnoreParenImpCasts()->isLValue() ||
12272 (ASE &&
12273 !ASE->getBase()->getType().getNonReferenceType()->isPointerType() &&
12274 !ASE->getBase()->getType().getNonReferenceType()->isArrayType())) {
12275 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12276 << RefExpr->getSourceRange();
12277 continue;
12278 }
12279 bool Suppress = getDiagnostics().getSuppressAllDiagnostics();
12280 getDiagnostics().setSuppressAllDiagnostics(/*Val=*/true);
12281 ExprResult Res =
12282 CreateBuiltinUnaryOp(ELoc, UO_AddrOf, RefExpr->IgnoreParenImpCasts());
12283 getDiagnostics().setSuppressAllDiagnostics(Suppress);
12284 if (!Res.isUsable() && !isa<OMPArraySectionExpr>(SimpleExpr)) {
12285 Diag(ELoc, diag::err_omp_expected_addressable_lvalue_or_array_item)
12286 << RefExpr->getSourceRange();
12287 continue;
12288 }
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012289 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012290 Vars.push_back(RefExpr->IgnoreParenImpCasts());
Alexey Bataeva636c7f2015-12-23 10:27:45 +000012291 }
Alexey Bataev17daedf2018-02-15 22:42:57 +000012292
12293 if (!CurContext->isDependentContext() && DepKind == OMPC_DEPEND_sink &&
12294 TotalDepCount > VarList.size() &&
Alexey Bataevf138fda2018-08-13 19:04:24 +000012295 DSAStack->getParentOrderedRegionParam().first &&
Alexey Bataev17daedf2018-02-15 22:42:57 +000012296 DSAStack->getParentLoopControlVariable(VarList.size() + 1)) {
12297 Diag(EndLoc, diag::err_omp_depend_sink_expected_loop_iteration)
12298 << 1 << DSAStack->getParentLoopControlVariable(VarList.size() + 1);
12299 }
12300 if (DepKind != OMPC_DEPEND_source && DepKind != OMPC_DEPEND_sink &&
12301 Vars.empty())
12302 return nullptr;
12303
Alexey Bataev8b427062016-05-25 12:36:08 +000012304 auto *C = OMPDependClause::Create(Context, StartLoc, LParenLoc, EndLoc,
Alexey Bataevf138fda2018-08-13 19:04:24 +000012305 DepKind, DepLoc, ColonLoc, Vars,
12306 TotalDepCount.getZExtValue());
Alexey Bataev17daedf2018-02-15 22:42:57 +000012307 if ((DepKind == OMPC_DEPEND_sink || DepKind == OMPC_DEPEND_source) &&
12308 DSAStack->isParentOrderedRegion())
Alexey Bataev8b427062016-05-25 12:36:08 +000012309 DSAStack->addDoacrossDependClause(C, OpsOffs);
12310 return C;
Alexey Bataev1c2cfbc2015-06-23 14:25:19 +000012311}
Michael Wonge710d542015-08-07 16:16:36 +000012312
12313OMPClause *Sema::ActOnOpenMPDeviceClause(Expr *Device, SourceLocation StartLoc,
12314 SourceLocation LParenLoc,
12315 SourceLocation EndLoc) {
12316 Expr *ValExpr = Device;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012317 Stmt *HelperValStmt = nullptr;
Michael Wonge710d542015-08-07 16:16:36 +000012318
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012319 // OpenMP [2.9.1, Restrictions]
12320 // The device expression must evaluate to a non-negative integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000012321 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_device,
Alexey Bataeva0569352015-12-01 10:17:31 +000012322 /*StrictlyPositive=*/false))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000012323 return nullptr;
12324
Alexey Bataev931e19b2017-10-02 16:32:39 +000012325 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000012326 OpenMPDirectiveKind CaptureRegion =
12327 getOpenMPCaptureRegionForClause(DKind, OMPC_device);
12328 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000012329 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000012330 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev931e19b2017-10-02 16:32:39 +000012331 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
12332 HelperValStmt = buildPreInits(Context, Captures);
12333 }
12334
Alexey Bataev8451efa2018-01-15 19:06:12 +000012335 return new (Context) OMPDeviceClause(ValExpr, HelperValStmt, CaptureRegion,
12336 StartLoc, LParenLoc, EndLoc);
Michael Wonge710d542015-08-07 16:16:36 +000012337}
Kelvin Li0bff7af2015-11-23 05:32:03 +000012338
Alexey Bataeve3727102018-04-18 15:57:46 +000012339static bool checkTypeMappable(SourceLocation SL, SourceRange SR, Sema &SemaRef,
Alexey Bataev95c23e72018-02-27 21:31:11 +000012340 DSAStackTy *Stack, QualType QTy,
12341 bool FullCheck = true) {
Kelvin Li0bff7af2015-11-23 05:32:03 +000012342 NamedDecl *ND;
12343 if (QTy->isIncompleteType(&ND)) {
12344 SemaRef.Diag(SL, diag::err_incomplete_type) << QTy << SR;
12345 return false;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012346 }
Alexey Bataev95c23e72018-02-27 21:31:11 +000012347 if (FullCheck && !SemaRef.CurContext->isDependentContext() &&
12348 !QTy.isTrivialType(SemaRef.Context))
12349 SemaRef.Diag(SL, diag::warn_omp_non_trivial_type_mapped) << QTy << SR;
Kelvin Li0bff7af2015-11-23 05:32:03 +000012350 return true;
12351}
12352
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000012353/// Return true if it can be proven that the provided array expression
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012354/// (array section or array subscript) does NOT specify the whole size of the
12355/// array whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012356static bool checkArrayExpressionDoesNotReferToWholeSize(Sema &SemaRef,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012357 const Expr *E,
12358 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012359 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012360
12361 // If this is an array subscript, it refers to the whole size if the size of
12362 // the dimension is constant and equals 1. Also, an array section assumes the
12363 // format of an array subscript if no colon is used.
12364 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012365 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012366 return ATy->getSize().getSExtValue() != 1;
12367 // Size can't be evaluated statically.
12368 return false;
12369 }
12370
12371 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012372 const Expr *LowerBound = OASE->getLowerBound();
12373 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012374
12375 // If there is a lower bound that does not evaluates to zero, we are not
David Majnemer9d168222016-08-05 17:44:54 +000012376 // covering the whole dimension.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012377 if (LowerBound) {
Fangrui Song407659a2018-11-30 23:41:18 +000012378 Expr::EvalResult Result;
12379 if (!LowerBound->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012380 return false; // Can't get the integer value as a constant.
Fangrui Song407659a2018-11-30 23:41:18 +000012381
12382 llvm::APSInt ConstLowerBound = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012383 if (ConstLowerBound.getSExtValue())
12384 return true;
12385 }
12386
12387 // If we don't have a length we covering the whole dimension.
12388 if (!Length)
12389 return false;
12390
12391 // If the base is a pointer, we don't have a way to get the size of the
12392 // pointee.
12393 if (BaseQTy->isPointerType())
12394 return false;
12395
12396 // We can only check if the length is the same as the size of the dimension
12397 // if we have a constant array.
Alexey Bataeve3727102018-04-18 15:57:46 +000012398 const auto *CATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr());
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012399 if (!CATy)
12400 return false;
12401
Fangrui Song407659a2018-11-30 23:41:18 +000012402 Expr::EvalResult Result;
12403 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012404 return false; // Can't get the integer value as a constant.
12405
Fangrui Song407659a2018-11-30 23:41:18 +000012406 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012407 return CATy->getSize().getSExtValue() != ConstLength.getSExtValue();
12408}
12409
12410// Return true if it can be proven that the provided array expression (array
12411// section or array subscript) does NOT specify a single element of the array
12412// whose base type is \a BaseQTy.
Alexey Bataeve3727102018-04-18 15:57:46 +000012413static bool checkArrayExpressionDoesNotReferToUnitySize(Sema &SemaRef,
David Majnemer9d168222016-08-05 17:44:54 +000012414 const Expr *E,
12415 QualType BaseQTy) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012416 const auto *OASE = dyn_cast<OMPArraySectionExpr>(E);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012417
12418 // An array subscript always refer to a single element. Also, an array section
12419 // assumes the format of an array subscript if no colon is used.
12420 if (isa<ArraySubscriptExpr>(E) || (OASE && OASE->getColonLoc().isInvalid()))
12421 return false;
12422
12423 assert(OASE && "Expecting array section if not an array subscript.");
Alexey Bataeve3727102018-04-18 15:57:46 +000012424 const Expr *Length = OASE->getLength();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012425
12426 // If we don't have a length we have to check if the array has unitary size
12427 // for this dimension. Also, we should always expect a length if the base type
12428 // is pointer.
12429 if (!Length) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012430 if (const auto *ATy = dyn_cast<ConstantArrayType>(BaseQTy.getTypePtr()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012431 return ATy->getSize().getSExtValue() != 1;
12432 // We cannot assume anything.
12433 return false;
12434 }
12435
12436 // Check if the length evaluates to 1.
Fangrui Song407659a2018-11-30 23:41:18 +000012437 Expr::EvalResult Result;
12438 if (!Length->EvaluateAsInt(Result, SemaRef.getASTContext()))
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012439 return false; // Can't get the integer value as a constant.
12440
Fangrui Song407659a2018-11-30 23:41:18 +000012441 llvm::APSInt ConstLength = Result.Val.getInt();
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012442 return ConstLength.getSExtValue() != 1;
12443}
12444
Samuel Antao661c0902016-05-26 17:39:58 +000012445// Return the expression of the base of the mappable expression or null if it
12446// cannot be determined and do all the necessary checks to see if the expression
12447// is valid as a standalone mappable expression. In the process, record all the
Samuel Antao90927002016-04-26 14:54:23 +000012448// components of the expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012449static const Expr *checkMapClauseExpressionBase(
Samuel Antao90927002016-04-26 14:54:23 +000012450 Sema &SemaRef, Expr *E,
Samuel Antao661c0902016-05-26 17:39:58 +000012451 OMPClauseMappableExprCommon::MappableExprComponentList &CurComponents,
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012452 OpenMPClauseKind CKind, bool NoDiagnose) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012453 SourceLocation ELoc = E->getExprLoc();
12454 SourceRange ERange = E->getSourceRange();
12455
12456 // The base of elements of list in a map clause have to be either:
12457 // - a reference to variable or field.
12458 // - a member expression.
12459 // - an array expression.
12460 //
12461 // E.g. if we have the expression 'r.S.Arr[:12]', we want to retrieve the
12462 // reference to 'r'.
12463 //
12464 // If we have:
12465 //
12466 // struct SS {
12467 // Bla S;
12468 // foo() {
12469 // #pragma omp target map (S.Arr[:12]);
12470 // }
12471 // }
12472 //
12473 // We want to retrieve the member expression 'this->S';
12474
Alexey Bataeve3727102018-04-18 15:57:46 +000012475 const Expr *RelevantExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012476
Samuel Antao5de996e2016-01-22 20:21:36 +000012477 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.2]
12478 // If a list item is an array section, it must specify contiguous storage.
12479 //
12480 // For this restriction it is sufficient that we make sure only references
12481 // to variables or fields and array expressions, and that no array sections
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012482 // exist except in the rightmost expression (unless they cover the whole
12483 // dimension of the array). E.g. these would be invalid:
Samuel Antao5de996e2016-01-22 20:21:36 +000012484 //
12485 // r.ArrS[3:5].Arr[6:7]
12486 //
12487 // r.ArrS[3:5].x
12488 //
12489 // but these would be valid:
12490 // r.ArrS[3].Arr[6:7]
12491 //
12492 // r.ArrS[3].x
12493
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012494 bool AllowUnitySizeArraySection = true;
12495 bool AllowWholeSizeArraySection = true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012496
Dmitry Polukhin644a9252016-03-11 07:58:34 +000012497 while (!RelevantExpr) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012498 E = E->IgnoreParenImpCasts();
12499
12500 if (auto *CurE = dyn_cast<DeclRefExpr>(E)) {
12501 if (!isa<VarDecl>(CurE->getDecl()))
Alexey Bataev27041fa2017-12-05 15:22:49 +000012502 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012503
12504 RelevantExpr = CurE;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012505
12506 // If we got a reference to a declaration, we should not expect any array
12507 // section before that.
12508 AllowUnitySizeArraySection = false;
12509 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012510
12511 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012512 CurComponents.emplace_back(CurE, CurE->getDecl());
12513 } else if (auto *CurE = dyn_cast<MemberExpr>(E)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012514 Expr *BaseE = CurE->getBase()->IgnoreParenImpCasts();
Samuel Antao5de996e2016-01-22 20:21:36 +000012515
12516 if (isa<CXXThisExpr>(BaseE))
12517 // We found a base expression: this->Val.
12518 RelevantExpr = CurE;
12519 else
12520 E = BaseE;
12521
12522 if (!isa<FieldDecl>(CurE->getMemberDecl())) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012523 if (!NoDiagnose) {
12524 SemaRef.Diag(ELoc, diag::err_omp_expected_access_to_data_field)
12525 << CurE->getSourceRange();
12526 return nullptr;
12527 }
12528 if (RelevantExpr)
12529 return nullptr;
12530 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012531 }
12532
12533 auto *FD = cast<FieldDecl>(CurE->getMemberDecl());
12534
12535 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.3]
12536 // A bit-field cannot appear in a map clause.
12537 //
12538 if (FD->isBitField()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012539 if (!NoDiagnose) {
12540 SemaRef.Diag(ELoc, diag::err_omp_bit_fields_forbidden_in_clause)
12541 << CurE->getSourceRange() << getOpenMPClauseName(CKind);
12542 return nullptr;
12543 }
12544 if (RelevantExpr)
12545 return nullptr;
12546 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012547 }
12548
12549 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12550 // If the type of a list item is a reference to a type T then the type
12551 // will be considered to be T for all purposes of this clause.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012552 QualType CurType = BaseE->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012553
12554 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.2]
12555 // A list item cannot be a variable that is a member of a structure with
12556 // a union type.
12557 //
Alexey Bataeve3727102018-04-18 15:57:46 +000012558 if (CurType->isUnionType()) {
12559 if (!NoDiagnose) {
12560 SemaRef.Diag(ELoc, diag::err_omp_union_type_not_allowed)
12561 << CurE->getSourceRange();
12562 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012563 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012564 continue;
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012565 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012566
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012567 // If we got a member expression, we should not expect any array section
12568 // before that:
12569 //
12570 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.7]
12571 // If a list item is an element of a structure, only the rightmost symbol
12572 // of the variable reference can be an array section.
12573 //
12574 AllowUnitySizeArraySection = false;
12575 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012576
12577 // Record the component.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012578 CurComponents.emplace_back(CurE, FD);
12579 } else if (auto *CurE = dyn_cast<ArraySubscriptExpr>(E)) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012580 E = CurE->getBase()->IgnoreParenImpCasts();
12581
12582 if (!E->getType()->isAnyPointerType() && !E->getType()->isArrayType()) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012583 if (!NoDiagnose) {
12584 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12585 << 0 << CurE->getSourceRange();
12586 return nullptr;
12587 }
12588 continue;
Samuel Antao5de996e2016-01-22 20:21:36 +000012589 }
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012590
12591 // If we got an array subscript that express the whole dimension we
12592 // can have any array expressions before. If it only expressing part of
12593 // the dimension, we can only have unitary-size array expressions.
Alexey Bataeve3727102018-04-18 15:57:46 +000012594 if (checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE,
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012595 E->getType()))
12596 AllowWholeSizeArraySection = false;
Samuel Antao90927002016-04-26 14:54:23 +000012597
Patrick Lystere13b1e32019-01-02 19:28:48 +000012598 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12599 Expr::EvalResult Result;
12600 if (CurE->getIdx()->EvaluateAsInt(Result, SemaRef.getASTContext())) {
12601 if (!Result.Val.getInt().isNullValue()) {
12602 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12603 diag::err_omp_invalid_map_this_expr);
12604 SemaRef.Diag(CurE->getIdx()->getExprLoc(),
12605 diag::note_omp_invalid_subscript_on_this_ptr_map);
12606 }
12607 }
12608 RelevantExpr = TE;
12609 }
12610
Samuel Antao90927002016-04-26 14:54:23 +000012611 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012612 CurComponents.emplace_back(CurE, nullptr);
12613 } else if (auto *CurE = dyn_cast<OMPArraySectionExpr>(E)) {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012614 assert(!NoDiagnose && "Array sections cannot be implicitly mapped.");
Samuel Antao5de996e2016-01-22 20:21:36 +000012615 E = CurE->getBase()->IgnoreParenImpCasts();
12616
Alexey Bataev27041fa2017-12-05 15:22:49 +000012617 QualType CurType =
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012618 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12619
Samuel Antao5de996e2016-01-22 20:21:36 +000012620 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12621 // If the type of a list item is a reference to a type T then the type
12622 // will be considered to be T for all purposes of this clause.
Samuel Antao5de996e2016-01-22 20:21:36 +000012623 if (CurType->isReferenceType())
12624 CurType = CurType->getPointeeType();
12625
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012626 bool IsPointer = CurType->isAnyPointerType();
12627
12628 if (!IsPointer && !CurType->isArrayType()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012629 SemaRef.Diag(ELoc, diag::err_omp_expected_base_var_name)
12630 << 0 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012631 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012632 }
12633
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012634 bool NotWhole =
Alexey Bataeve3727102018-04-18 15:57:46 +000012635 checkArrayExpressionDoesNotReferToWholeSize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012636 bool NotUnity =
Alexey Bataeve3727102018-04-18 15:57:46 +000012637 checkArrayExpressionDoesNotReferToUnitySize(SemaRef, CurE, CurType);
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012638
Samuel Antaodab51bb2016-07-18 23:22:11 +000012639 if (AllowWholeSizeArraySection) {
12640 // Any array section is currently allowed. Allowing a whole size array
12641 // section implies allowing a unity array section as well.
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012642 //
12643 // If this array section refers to the whole dimension we can still
12644 // accept other array sections before this one, except if the base is a
12645 // pointer. Otherwise, only unitary sections are accepted.
12646 if (NotWhole || IsPointer)
12647 AllowWholeSizeArraySection = false;
Samuel Antaodab51bb2016-07-18 23:22:11 +000012648 } else if (AllowUnitySizeArraySection && NotUnity) {
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012649 // A unity or whole array section is not allowed and that is not
12650 // compatible with the properties of the current array section.
12651 SemaRef.Diag(
12652 ELoc, diag::err_array_section_does_not_specify_contiguous_storage)
12653 << CurE->getSourceRange();
Alexey Bataev27041fa2017-12-05 15:22:49 +000012654 return nullptr;
Samuel Antaoa9f35cb2016-03-09 15:46:05 +000012655 }
Samuel Antao90927002016-04-26 14:54:23 +000012656
Patrick Lystere13b1e32019-01-02 19:28:48 +000012657 if (const auto *TE = dyn_cast<CXXThisExpr>(E)) {
12658 Expr::EvalResult ResultR;
12659 Expr::EvalResult ResultL;
12660 if (CurE->getLength()->EvaluateAsInt(ResultR,
12661 SemaRef.getASTContext())) {
12662 if (!ResultR.Val.getInt().isOneValue()) {
12663 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12664 diag::err_omp_invalid_map_this_expr);
12665 SemaRef.Diag(CurE->getLength()->getExprLoc(),
12666 diag::note_omp_invalid_length_on_this_ptr_mapping);
12667 }
12668 }
12669 if (CurE->getLowerBound() && CurE->getLowerBound()->EvaluateAsInt(
12670 ResultL, SemaRef.getASTContext())) {
12671 if (!ResultL.Val.getInt().isNullValue()) {
12672 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12673 diag::err_omp_invalid_map_this_expr);
12674 SemaRef.Diag(CurE->getLowerBound()->getExprLoc(),
12675 diag::note_omp_invalid_lower_bound_on_this_ptr_mapping);
12676 }
12677 }
12678 RelevantExpr = TE;
12679 }
12680
Samuel Antao90927002016-04-26 14:54:23 +000012681 // Record the component - we don't have any declaration associated.
Alexey Bataev27041fa2017-12-05 15:22:49 +000012682 CurComponents.emplace_back(CurE, nullptr);
12683 } else {
Alexey Bataevb7a9b742017-12-05 19:20:09 +000012684 if (!NoDiagnose) {
12685 // If nothing else worked, this is not a valid map clause expression.
12686 SemaRef.Diag(
12687 ELoc, diag::err_omp_expected_named_var_member_or_array_expression)
12688 << ERange;
12689 }
Alexey Bataev27041fa2017-12-05 15:22:49 +000012690 return nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012691 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012692 }
12693
12694 return RelevantExpr;
12695}
12696
12697// Return true if expression E associated with value VD has conflicts with other
12698// map information.
Alexey Bataeve3727102018-04-18 15:57:46 +000012699static bool checkMapConflicts(
12700 Sema &SemaRef, DSAStackTy *DSAS, const ValueDecl *VD, const Expr *E,
Samuel Antao90927002016-04-26 14:54:23 +000012701 bool CurrentRegionOnly,
Samuel Antao661c0902016-05-26 17:39:58 +000012702 OMPClauseMappableExprCommon::MappableExprComponentListRef CurComponents,
12703 OpenMPClauseKind CKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012704 assert(VD && E);
Samuel Antao5de996e2016-01-22 20:21:36 +000012705 SourceLocation ELoc = E->getExprLoc();
12706 SourceRange ERange = E->getSourceRange();
12707
12708 // In order to easily check the conflicts we need to match each component of
12709 // the expression under test with the components of the expressions that are
12710 // already in the stack.
12711
Samuel Antao5de996e2016-01-22 20:21:36 +000012712 assert(!CurComponents.empty() && "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012713 assert(CurComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012714 "Map clause expression with unexpected base!");
12715
12716 // Variables to help detecting enclosing problems in data environment nests.
12717 bool IsEnclosedByDataEnvironmentExpr = false;
Samuel Antao90927002016-04-26 14:54:23 +000012718 const Expr *EnclosingExpr = nullptr;
Samuel Antao5de996e2016-01-22 20:21:36 +000012719
Samuel Antao90927002016-04-26 14:54:23 +000012720 bool FoundError = DSAS->checkMappableExprComponentListsForDecl(
12721 VD, CurrentRegionOnly,
Alexey Bataeve3727102018-04-18 15:57:46 +000012722 [&IsEnclosedByDataEnvironmentExpr, &SemaRef, VD, CurrentRegionOnly, ELoc,
12723 ERange, CKind, &EnclosingExpr,
12724 CurComponents](OMPClauseMappableExprCommon::MappableExprComponentListRef
12725 StackComponents,
12726 OpenMPClauseKind) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012727 assert(!StackComponents.empty() &&
12728 "Map clause expression with no components!");
Samuel Antao90927002016-04-26 14:54:23 +000012729 assert(StackComponents.back().getAssociatedDeclaration() == VD &&
Samuel Antao5de996e2016-01-22 20:21:36 +000012730 "Map clause expression with unexpected base!");
Fangrui Song16fe49a2018-04-18 19:32:01 +000012731 (void)VD;
Samuel Antao5de996e2016-01-22 20:21:36 +000012732
Samuel Antao90927002016-04-26 14:54:23 +000012733 // The whole expression in the stack.
Alexey Bataeve3727102018-04-18 15:57:46 +000012734 const Expr *RE = StackComponents.front().getAssociatedExpression();
Samuel Antao90927002016-04-26 14:54:23 +000012735
Samuel Antao5de996e2016-01-22 20:21:36 +000012736 // Expressions must start from the same base. Here we detect at which
12737 // point both expressions diverge from each other and see if we can
12738 // detect if the memory referred to both expressions is contiguous and
12739 // do not overlap.
12740 auto CI = CurComponents.rbegin();
12741 auto CE = CurComponents.rend();
12742 auto SI = StackComponents.rbegin();
12743 auto SE = StackComponents.rend();
12744 for (; CI != CE && SI != SE; ++CI, ++SI) {
12745
12746 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.3]
12747 // At most one list item can be an array item derived from a given
12748 // variable in map clauses of the same construct.
Samuel Antao90927002016-04-26 14:54:23 +000012749 if (CurrentRegionOnly &&
12750 (isa<ArraySubscriptExpr>(CI->getAssociatedExpression()) ||
12751 isa<OMPArraySectionExpr>(CI->getAssociatedExpression())) &&
12752 (isa<ArraySubscriptExpr>(SI->getAssociatedExpression()) ||
12753 isa<OMPArraySectionExpr>(SI->getAssociatedExpression()))) {
12754 SemaRef.Diag(CI->getAssociatedExpression()->getExprLoc(),
Samuel Antao5de996e2016-01-22 20:21:36 +000012755 diag::err_omp_multiple_array_items_in_map_clause)
Samuel Antao90927002016-04-26 14:54:23 +000012756 << CI->getAssociatedExpression()->getSourceRange();
12757 SemaRef.Diag(SI->getAssociatedExpression()->getExprLoc(),
12758 diag::note_used_here)
12759 << SI->getAssociatedExpression()->getSourceRange();
Samuel Antao5de996e2016-01-22 20:21:36 +000012760 return true;
12761 }
12762
12763 // Do both expressions have the same kind?
Samuel Antao90927002016-04-26 14:54:23 +000012764 if (CI->getAssociatedExpression()->getStmtClass() !=
12765 SI->getAssociatedExpression()->getStmtClass())
Samuel Antao5de996e2016-01-22 20:21:36 +000012766 break;
12767
12768 // Are we dealing with different variables/fields?
Samuel Antao90927002016-04-26 14:54:23 +000012769 if (CI->getAssociatedDeclaration() != SI->getAssociatedDeclaration())
Samuel Antao5de996e2016-01-22 20:21:36 +000012770 break;
12771 }
Kelvin Li9f645ae2016-07-18 22:49:16 +000012772 // Check if the extra components of the expressions in the enclosing
12773 // data environment are redundant for the current base declaration.
12774 // If they are, the maps completely overlap, which is legal.
12775 for (; SI != SE; ++SI) {
12776 QualType Type;
Alexey Bataeve3727102018-04-18 15:57:46 +000012777 if (const auto *ASE =
David Majnemer9d168222016-08-05 17:44:54 +000012778 dyn_cast<ArraySubscriptExpr>(SI->getAssociatedExpression())) {
Kelvin Li9f645ae2016-07-18 22:49:16 +000012779 Type = ASE->getBase()->IgnoreParenImpCasts()->getType();
Alexey Bataeve3727102018-04-18 15:57:46 +000012780 } else if (const auto *OASE = dyn_cast<OMPArraySectionExpr>(
David Majnemer9d168222016-08-05 17:44:54 +000012781 SI->getAssociatedExpression())) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012782 const Expr *E = OASE->getBase()->IgnoreParenImpCasts();
Kelvin Li9f645ae2016-07-18 22:49:16 +000012783 Type =
12784 OMPArraySectionExpr::getBaseOriginalType(E).getCanonicalType();
12785 }
12786 if (Type.isNull() || Type->isAnyPointerType() ||
Alexey Bataeve3727102018-04-18 15:57:46 +000012787 checkArrayExpressionDoesNotReferToWholeSize(
Kelvin Li9f645ae2016-07-18 22:49:16 +000012788 SemaRef, SI->getAssociatedExpression(), Type))
12789 break;
12790 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012791
12792 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12793 // List items of map clauses in the same construct must not share
12794 // original storage.
12795 //
12796 // If the expressions are exactly the same or one is a subset of the
12797 // other, it means they are sharing storage.
12798 if (CI == CE && SI == SE) {
12799 if (CurrentRegionOnly) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012800 if (CKind == OMPC_map) {
Samuel Antao661c0902016-05-26 17:39:58 +000012801 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012802 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012803 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012804 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12805 << ERange;
12806 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012807 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12808 << RE->getSourceRange();
12809 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012810 }
Alexey Bataeve3727102018-04-18 15:57:46 +000012811 // If we find the same expression in the enclosing data environment,
12812 // that is legal.
12813 IsEnclosedByDataEnvironmentExpr = true;
12814 return false;
Samuel Antao5de996e2016-01-22 20:21:36 +000012815 }
12816
Samuel Antao90927002016-04-26 14:54:23 +000012817 QualType DerivedType =
12818 std::prev(CI)->getAssociatedDeclaration()->getType();
12819 SourceLocation DerivedLoc =
12820 std::prev(CI)->getAssociatedExpression()->getExprLoc();
Samuel Antao5de996e2016-01-22 20:21:36 +000012821
12822 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
12823 // If the type of a list item is a reference to a type T then the type
12824 // will be considered to be T for all purposes of this clause.
Samuel Antao90927002016-04-26 14:54:23 +000012825 DerivedType = DerivedType.getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000012826
12827 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C/C++, p.1]
12828 // A variable for which the type is pointer and an array section
12829 // derived from that variable must not appear as list items of map
12830 // clauses of the same construct.
12831 //
12832 // Also, cover one of the cases in:
12833 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12834 // If any part of the original storage of a list item has corresponding
12835 // storage in the device data environment, all of the original storage
12836 // must have corresponding storage in the device data environment.
12837 //
12838 if (DerivedType->isAnyPointerType()) {
12839 if (CI == CE || SI == SE) {
12840 SemaRef.Diag(
12841 DerivedLoc,
12842 diag::err_omp_pointer_mapped_along_with_derived_section)
12843 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012844 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12845 << RE->getSourceRange();
12846 return true;
Alexey Bataeve3727102018-04-18 15:57:46 +000012847 }
12848 if (CI->getAssociatedExpression()->getStmtClass() !=
Alexey Bataev2819260b2018-02-27 17:42:00 +000012849 SI->getAssociatedExpression()->getStmtClass() ||
12850 CI->getAssociatedDeclaration()->getCanonicalDecl() ==
12851 SI->getAssociatedDeclaration()->getCanonicalDecl()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012852 assert(CI != CE && SI != SE);
Alexey Bataev2819260b2018-02-27 17:42:00 +000012853 SemaRef.Diag(DerivedLoc, diag::err_omp_same_pointer_dereferenced)
Samuel Antao5de996e2016-01-22 20:21:36 +000012854 << DerivedLoc;
Alexey Bataev2819260b2018-02-27 17:42:00 +000012855 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12856 << RE->getSourceRange();
12857 return true;
Samuel Antao5de996e2016-01-22 20:21:36 +000012858 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012859 }
12860
12861 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.4]
12862 // List items of map clauses in the same construct must not share
12863 // original storage.
12864 //
12865 // An expression is a subset of the other.
12866 if (CurrentRegionOnly && (CI == CE || SI == SE)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000012867 if (CKind == OMPC_map) {
Alexey Bataeve82445f2018-09-20 13:54:02 +000012868 if (CI != CE || SI != SE) {
12869 // Allow constructs like this: map(s, s.ptr[0:1]), where s.ptr is
12870 // a pointer.
12871 auto Begin =
12872 CI != CE ? CurComponents.begin() : StackComponents.begin();
12873 auto End = CI != CE ? CurComponents.end() : StackComponents.end();
12874 auto It = Begin;
12875 while (It != End && !It->getAssociatedDeclaration())
12876 std::advance(It, 1);
12877 assert(It != End &&
12878 "Expected at least one component with the declaration.");
12879 if (It != Begin && It->getAssociatedDeclaration()
12880 ->getType()
12881 .getCanonicalType()
12882 ->isAnyPointerType()) {
12883 IsEnclosedByDataEnvironmentExpr = false;
12884 EnclosingExpr = nullptr;
12885 return false;
12886 }
12887 }
Samuel Antao661c0902016-05-26 17:39:58 +000012888 SemaRef.Diag(ELoc, diag::err_omp_map_shared_storage) << ERange;
Alexey Bataeve3727102018-04-18 15:57:46 +000012889 } else {
Samuel Antaoec172c62016-05-26 17:49:04 +000012890 assert(CKind == OMPC_to || CKind == OMPC_from);
Samuel Antao661c0902016-05-26 17:39:58 +000012891 SemaRef.Diag(ELoc, diag::err_omp_once_referenced_in_target_update)
12892 << ERange;
12893 }
Samuel Antao5de996e2016-01-22 20:21:36 +000012894 SemaRef.Diag(RE->getExprLoc(), diag::note_used_here)
12895 << RE->getSourceRange();
12896 return true;
12897 }
12898
12899 // The current expression uses the same base as other expression in the
Samuel Antao90927002016-04-26 14:54:23 +000012900 // data environment but does not contain it completely.
Samuel Antao5de996e2016-01-22 20:21:36 +000012901 if (!CurrentRegionOnly && SI != SE)
12902 EnclosingExpr = RE;
12903
12904 // The current expression is a subset of the expression in the data
12905 // environment.
12906 IsEnclosedByDataEnvironmentExpr |=
12907 (!CurrentRegionOnly && CI != CE && SI == SE);
12908
12909 return false;
12910 });
12911
12912 if (CurrentRegionOnly)
12913 return FoundError;
12914
12915 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.5]
12916 // If any part of the original storage of a list item has corresponding
12917 // storage in the device data environment, all of the original storage must
12918 // have corresponding storage in the device data environment.
12919 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.6]
12920 // If a list item is an element of a structure, and a different element of
12921 // the structure has a corresponding list item in the device data environment
12922 // prior to a task encountering the construct associated with the map clause,
Samuel Antao90927002016-04-26 14:54:23 +000012923 // then the list item must also have a corresponding list item in the device
Samuel Antao5de996e2016-01-22 20:21:36 +000012924 // data environment prior to the task encountering the construct.
12925 //
12926 if (EnclosingExpr && !IsEnclosedByDataEnvironmentExpr) {
12927 SemaRef.Diag(ELoc,
12928 diag::err_omp_original_storage_is_shared_and_does_not_contain)
12929 << ERange;
12930 SemaRef.Diag(EnclosingExpr->getExprLoc(), diag::note_used_here)
12931 << EnclosingExpr->getSourceRange();
12932 return true;
12933 }
12934
12935 return FoundError;
12936}
12937
Samuel Antao661c0902016-05-26 17:39:58 +000012938namespace {
12939// Utility struct that gathers all the related lists associated with a mappable
12940// expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000012941struct MappableVarListInfo {
Samuel Antao661c0902016-05-26 17:39:58 +000012942 // The list of expressions.
12943 ArrayRef<Expr *> VarList;
12944 // The list of processed expressions.
12945 SmallVector<Expr *, 16> ProcessedVarList;
12946 // The mappble components for each expression.
12947 OMPClauseMappableExprCommon::MappableExprComponentLists VarComponents;
12948 // The base declaration of the variable.
12949 SmallVector<ValueDecl *, 16> VarBaseDeclarations;
12950
12951 MappableVarListInfo(ArrayRef<Expr *> VarList) : VarList(VarList) {
12952 // We have a list of components and base declarations for each entry in the
12953 // variable list.
12954 VarComponents.reserve(VarList.size());
12955 VarBaseDeclarations.reserve(VarList.size());
12956 }
12957};
12958}
12959
12960// Check the validity of the provided variable list for the provided clause kind
12961// \a CKind. In the check process the valid expressions, and mappable expression
12962// components and variables are extracted and used to fill \a Vars,
12963// \a ClauseComponents, and \a ClauseBaseDeclarations. \a MapType and
12964// \a IsMapTypeImplicit are expected to be valid if the clause kind is 'map'.
12965static void
12966checkMappableExpressionList(Sema &SemaRef, DSAStackTy *DSAS,
12967 OpenMPClauseKind CKind, MappableVarListInfo &MVLI,
12968 SourceLocation StartLoc,
12969 OpenMPMapClauseKind MapType = OMPC_MAP_unknown,
12970 bool IsMapTypeImplicit = false) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012971 // We only expect mappable expressions in 'to', 'from', and 'map' clauses.
12972 assert((CKind == OMPC_map || CKind == OMPC_to || CKind == OMPC_from) &&
Samuel Antao661c0902016-05-26 17:39:58 +000012973 "Unexpected clause kind with mappable expressions!");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012974
Samuel Antao90927002016-04-26 14:54:23 +000012975 // Keep track of the mappable components and base declarations in this clause.
12976 // Each entry in the list is going to have a list of components associated. We
12977 // record each set of the components so that we can build the clause later on.
12978 // In the end we should have the same amount of declarations and component
12979 // lists.
Samuel Antao90927002016-04-26 14:54:23 +000012980
Alexey Bataeve3727102018-04-18 15:57:46 +000012981 for (Expr *RE : MVLI.VarList) {
Samuel Antaoec172c62016-05-26 17:49:04 +000012982 assert(RE && "Null expr in omp to/from/map clause");
Kelvin Li0bff7af2015-11-23 05:32:03 +000012983 SourceLocation ELoc = RE->getExprLoc();
12984
Alexey Bataeve3727102018-04-18 15:57:46 +000012985 const Expr *VE = RE->IgnoreParenLValueCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012986
12987 if (VE->isValueDependent() || VE->isTypeDependent() ||
12988 VE->isInstantiationDependent() ||
12989 VE->containsUnexpandedParameterPack()) {
Samuel Antao5de996e2016-01-22 20:21:36 +000012990 // We can only analyze this information once the missing information is
12991 // resolved.
Samuel Antao661c0902016-05-26 17:39:58 +000012992 MVLI.ProcessedVarList.push_back(RE);
Kelvin Li0bff7af2015-11-23 05:32:03 +000012993 continue;
12994 }
12995
Alexey Bataeve3727102018-04-18 15:57:46 +000012996 Expr *SimpleExpr = RE->IgnoreParenCasts();
Kelvin Li0bff7af2015-11-23 05:32:03 +000012997
Samuel Antao5de996e2016-01-22 20:21:36 +000012998 if (!RE->IgnoreParenImpCasts()->isLValue()) {
Samuel Antao661c0902016-05-26 17:39:58 +000012999 SemaRef.Diag(ELoc,
13000 diag::err_omp_expected_named_var_member_or_array_expression)
Samuel Antao5de996e2016-01-22 20:21:36 +000013001 << RE->getSourceRange();
Kelvin Li0bff7af2015-11-23 05:32:03 +000013002 continue;
13003 }
13004
Samuel Antao90927002016-04-26 14:54:23 +000013005 OMPClauseMappableExprCommon::MappableExprComponentList CurComponents;
13006 ValueDecl *CurDeclaration = nullptr;
13007
13008 // Obtain the array or member expression bases if required. Also, fill the
13009 // components array with all the components identified in the process.
Alexey Bataeve3727102018-04-18 15:57:46 +000013010 const Expr *BE = checkMapClauseExpressionBase(
13011 SemaRef, SimpleExpr, CurComponents, CKind, /*NoDiagnose=*/false);
Samuel Antao5de996e2016-01-22 20:21:36 +000013012 if (!BE)
13013 continue;
13014
Samuel Antao90927002016-04-26 14:54:23 +000013015 assert(!CurComponents.empty() &&
13016 "Invalid mappable expression information.");
Kelvin Li0bff7af2015-11-23 05:32:03 +000013017
Patrick Lystere13b1e32019-01-02 19:28:48 +000013018 if (const auto *TE = dyn_cast<CXXThisExpr>(BE)) {
13019 // Add store "this" pointer to class in DSAStackTy for future checking
13020 DSAS->addMappedClassesQualTypes(TE->getType());
13021 // Skip restriction checking for variable or field declarations
13022 MVLI.ProcessedVarList.push_back(RE);
13023 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13024 MVLI.VarComponents.back().append(CurComponents.begin(),
13025 CurComponents.end());
13026 MVLI.VarBaseDeclarations.push_back(nullptr);
13027 continue;
13028 }
13029
Samuel Antao90927002016-04-26 14:54:23 +000013030 // For the following checks, we rely on the base declaration which is
13031 // expected to be associated with the last component. The declaration is
13032 // expected to be a variable or a field (if 'this' is being mapped).
13033 CurDeclaration = CurComponents.back().getAssociatedDeclaration();
13034 assert(CurDeclaration && "Null decl on map clause.");
13035 assert(
13036 CurDeclaration->isCanonicalDecl() &&
13037 "Expecting components to have associated only canonical declarations.");
13038
13039 auto *VD = dyn_cast<VarDecl>(CurDeclaration);
Alexey Bataeve3727102018-04-18 15:57:46 +000013040 const auto *FD = dyn_cast<FieldDecl>(CurDeclaration);
Samuel Antao5de996e2016-01-22 20:21:36 +000013041
13042 assert((VD || FD) && "Only variables or fields are expected here!");
NAKAMURA Takumi6dcb8142016-01-23 01:38:20 +000013043 (void)FD;
Samuel Antao5de996e2016-01-22 20:21:36 +000013044
13045 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.10]
Samuel Antao661c0902016-05-26 17:39:58 +000013046 // threadprivate variables cannot appear in a map clause.
13047 // OpenMP 4.5 [2.10.5, target update Construct]
13048 // threadprivate variables cannot appear in a from clause.
13049 if (VD && DSAS->isThreadPrivate(VD)) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013050 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013051 SemaRef.Diag(ELoc, diag::err_omp_threadprivate_in_clause)
13052 << getOpenMPClauseName(CKind);
Alexey Bataeve3727102018-04-18 15:57:46 +000013053 reportOriginalDsa(SemaRef, DSAS, VD, DVar);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013054 continue;
13055 }
13056
Samuel Antao5de996e2016-01-22 20:21:36 +000013057 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
13058 // A list item cannot appear in both a map clause and a data-sharing
13059 // attribute clause on the same construct.
Kelvin Li0bff7af2015-11-23 05:32:03 +000013060
Samuel Antao5de996e2016-01-22 20:21:36 +000013061 // Check conflicts with other map clause expressions. We check the conflicts
13062 // with the current construct separately from the enclosing data
Samuel Antao661c0902016-05-26 17:39:58 +000013063 // environment, because the restrictions are different. We only have to
13064 // check conflicts across regions for the map clauses.
Alexey Bataeve3727102018-04-18 15:57:46 +000013065 if (checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013066 /*CurrentRegionOnly=*/true, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013067 break;
Samuel Antao661c0902016-05-26 17:39:58 +000013068 if (CKind == OMPC_map &&
Alexey Bataeve3727102018-04-18 15:57:46 +000013069 checkMapConflicts(SemaRef, DSAS, CurDeclaration, SimpleExpr,
Samuel Antao661c0902016-05-26 17:39:58 +000013070 /*CurrentRegionOnly=*/false, CurComponents, CKind))
Samuel Antao5de996e2016-01-22 20:21:36 +000013071 break;
Kelvin Li0bff7af2015-11-23 05:32:03 +000013072
Samuel Antao661c0902016-05-26 17:39:58 +000013073 // OpenMP 4.5 [2.10.5, target update Construct]
Samuel Antao5de996e2016-01-22 20:21:36 +000013074 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, C++, p.1]
13075 // If the type of a list item is a reference to a type T then the type will
13076 // be considered to be T for all purposes of this clause.
Alexey Bataev354df2e2018-05-02 18:44:10 +000013077 auto I = llvm::find_if(
13078 CurComponents,
13079 [](const OMPClauseMappableExprCommon::MappableComponent &MC) {
13080 return MC.getAssociatedDeclaration();
13081 });
13082 assert(I != CurComponents.end() && "Null decl on map clause.");
13083 QualType Type =
13084 I->getAssociatedDeclaration()->getType().getNonReferenceType();
Samuel Antao5de996e2016-01-22 20:21:36 +000013085
Samuel Antao661c0902016-05-26 17:39:58 +000013086 // OpenMP 4.5 [2.10.5, target update Construct, Restrictions, p.4]
13087 // A list item in a to or from clause must have a mappable type.
Samuel Antao5de996e2016-01-22 20:21:36 +000013088 // OpenMP 4.5 [2.15.5.1, map Clause, Restrictions, p.9]
Kelvin Li0bff7af2015-11-23 05:32:03 +000013089 // A list item must have a mappable type.
Alexey Bataeve3727102018-04-18 15:57:46 +000013090 if (!checkTypeMappable(VE->getExprLoc(), VE->getSourceRange(), SemaRef,
Samuel Antao661c0902016-05-26 17:39:58 +000013091 DSAS, Type))
Kelvin Li0bff7af2015-11-23 05:32:03 +000013092 continue;
13093
Samuel Antao661c0902016-05-26 17:39:58 +000013094 if (CKind == OMPC_map) {
13095 // target enter data
13096 // OpenMP [2.10.2, Restrictions, p. 99]
13097 // A map-type must be specified in all map clauses and must be either
13098 // to or alloc.
13099 OpenMPDirectiveKind DKind = DSAS->getCurrentDirective();
13100 if (DKind == OMPD_target_enter_data &&
13101 !(MapType == OMPC_MAP_to || MapType == OMPC_MAP_alloc)) {
13102 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13103 << (IsMapTypeImplicit ? 1 : 0)
13104 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13105 << getOpenMPDirectiveName(DKind);
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013106 continue;
13107 }
Samuel Antao661c0902016-05-26 17:39:58 +000013108
13109 // target exit_data
13110 // OpenMP [2.10.3, Restrictions, p. 102]
13111 // A map-type must be specified in all map clauses and must be either
13112 // from, release, or delete.
13113 if (DKind == OMPD_target_exit_data &&
13114 !(MapType == OMPC_MAP_from || MapType == OMPC_MAP_release ||
13115 MapType == OMPC_MAP_delete)) {
13116 SemaRef.Diag(StartLoc, diag::err_omp_invalid_map_type_for_directive)
13117 << (IsMapTypeImplicit ? 1 : 0)
13118 << getOpenMPSimpleClauseTypeName(OMPC_map, MapType)
13119 << getOpenMPDirectiveName(DKind);
13120 continue;
13121 }
13122
13123 // OpenMP 4.5 [2.15.5.1, Restrictions, p.3]
13124 // A list item cannot appear in both a map clause and a data-sharing
13125 // attribute clause on the same construct
Alexey Bataeve3727102018-04-18 15:57:46 +000013126 if (VD && isOpenMPTargetExecutionDirective(DKind)) {
13127 DSAStackTy::DSAVarData DVar = DSAS->getTopDSA(VD, /*FromParent=*/false);
Samuel Antao661c0902016-05-26 17:39:58 +000013128 if (isOpenMPPrivate(DVar.CKind)) {
Samuel Antao6890b092016-07-28 14:25:09 +000013129 SemaRef.Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
Samuel Antao661c0902016-05-26 17:39:58 +000013130 << getOpenMPClauseName(DVar.CKind)
Samuel Antao6890b092016-07-28 14:25:09 +000013131 << getOpenMPClauseName(OMPC_map)
Samuel Antao661c0902016-05-26 17:39:58 +000013132 << getOpenMPDirectiveName(DSAS->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013133 reportOriginalDsa(SemaRef, DSAS, CurDeclaration, DVar);
Samuel Antao661c0902016-05-26 17:39:58 +000013134 continue;
13135 }
13136 }
Carlo Bertollib74bfc82016-03-18 21:43:32 +000013137 }
13138
Samuel Antao90927002016-04-26 14:54:23 +000013139 // Save the current expression.
Samuel Antao661c0902016-05-26 17:39:58 +000013140 MVLI.ProcessedVarList.push_back(RE);
Samuel Antao90927002016-04-26 14:54:23 +000013141
13142 // Store the components in the stack so that they can be used to check
13143 // against other clauses later on.
Samuel Antao6890b092016-07-28 14:25:09 +000013144 DSAS->addMappableExpressionComponents(CurDeclaration, CurComponents,
13145 /*WhereFoundClauseKind=*/OMPC_map);
Samuel Antao90927002016-04-26 14:54:23 +000013146
13147 // Save the components and declaration to create the clause. For purposes of
13148 // the clause creation, any component list that has has base 'this' uses
Samuel Antao686c70c2016-05-26 17:30:50 +000013149 // null as base declaration.
Samuel Antao661c0902016-05-26 17:39:58 +000013150 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13151 MVLI.VarComponents.back().append(CurComponents.begin(),
13152 CurComponents.end());
13153 MVLI.VarBaseDeclarations.push_back(isa<MemberExpr>(BE) ? nullptr
13154 : CurDeclaration);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013155 }
Samuel Antao661c0902016-05-26 17:39:58 +000013156}
13157
13158OMPClause *
Kelvin Lief579432018-12-18 22:18:41 +000013159Sema::ActOnOpenMPMapClause(ArrayRef<OpenMPMapModifierKind> MapTypeModifiers,
13160 ArrayRef<SourceLocation> MapTypeModifiersLoc,
Samuel Antao661c0902016-05-26 17:39:58 +000013161 OpenMPMapClauseKind MapType, bool IsMapTypeImplicit,
13162 SourceLocation MapLoc, SourceLocation ColonLoc,
13163 ArrayRef<Expr *> VarList, SourceLocation StartLoc,
13164 SourceLocation LParenLoc, SourceLocation EndLoc) {
13165 MappableVarListInfo MVLI(VarList);
13166 checkMappableExpressionList(*this, DSAStack, OMPC_map, MVLI, StartLoc,
13167 MapType, IsMapTypeImplicit);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013168
Kelvin Lief579432018-12-18 22:18:41 +000013169 OpenMPMapModifierKind Modifiers[] = { OMPC_MAP_MODIFIER_unknown,
13170 OMPC_MAP_MODIFIER_unknown };
13171 SourceLocation ModifiersLoc[OMPMapClause::NumberOfModifiers];
13172
13173 // Process map-type-modifiers, flag errors for duplicate modifiers.
13174 unsigned Count = 0;
13175 for (unsigned I = 0, E = MapTypeModifiers.size(); I < E; ++I) {
13176 if (MapTypeModifiers[I] != OMPC_MAP_MODIFIER_unknown &&
13177 llvm::find(Modifiers, MapTypeModifiers[I]) != std::end(Modifiers)) {
13178 Diag(MapTypeModifiersLoc[I], diag::err_omp_duplicate_map_type_modifier);
13179 continue;
13180 }
13181 assert(Count < OMPMapClause::NumberOfModifiers &&
Gheorghe-Teodor Berceaa3afcf22019-01-09 20:38:35 +000013182 "Modifiers exceed the allowed number of map type modifiers");
Kelvin Lief579432018-12-18 22:18:41 +000013183 Modifiers[Count] = MapTypeModifiers[I];
13184 ModifiersLoc[Count] = MapTypeModifiersLoc[I];
13185 ++Count;
13186 }
13187
Samuel Antao5de996e2016-01-22 20:21:36 +000013188 // We need to produce a map clause even if we don't have variables so that
13189 // other diagnostics related with non-existing map clauses are accurate.
Samuel Antao661c0902016-05-26 17:39:58 +000013190 return OMPMapClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13191 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
Kelvin Lief579432018-12-18 22:18:41 +000013192 MVLI.VarComponents, Modifiers, ModifiersLoc,
13193 MapType, IsMapTypeImplicit, MapLoc);
Kelvin Li0bff7af2015-11-23 05:32:03 +000013194}
Kelvin Li099bb8c2015-11-24 20:50:12 +000013195
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013196QualType Sema::ActOnOpenMPDeclareReductionType(SourceLocation TyLoc,
13197 TypeResult ParsedType) {
13198 assert(ParsedType.isUsable());
13199
13200 QualType ReductionType = GetTypeFromParser(ParsedType.get());
13201 if (ReductionType.isNull())
13202 return QualType();
13203
13204 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions, C\C++
13205 // A type name in a declare reduction directive cannot be a function type, an
13206 // array type, a reference type, or a type qualified with const, volatile or
13207 // restrict.
13208 if (ReductionType.hasQualifiers()) {
13209 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 0;
13210 return QualType();
13211 }
13212
13213 if (ReductionType->isFunctionType()) {
13214 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 1;
13215 return QualType();
13216 }
13217 if (ReductionType->isReferenceType()) {
13218 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 2;
13219 return QualType();
13220 }
13221 if (ReductionType->isArrayType()) {
13222 Diag(TyLoc, diag::err_omp_reduction_wrong_type) << 3;
13223 return QualType();
13224 }
13225 return ReductionType;
13226}
13227
13228Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveStart(
13229 Scope *S, DeclContext *DC, DeclarationName Name,
13230 ArrayRef<std::pair<QualType, SourceLocation>> ReductionTypes,
13231 AccessSpecifier AS, Decl *PrevDeclInScope) {
13232 SmallVector<Decl *, 8> Decls;
13233 Decls.reserve(ReductionTypes.size());
13234
13235 LookupResult Lookup(*this, Name, SourceLocation(), LookupOMPReductionName,
Richard Smithbecb92d2017-10-10 22:33:17 +000013236 forRedeclarationInCurContext());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013237 // [OpenMP 4.0], 2.15 declare reduction Directive, Restrictions
13238 // A reduction-identifier may not be re-declared in the current scope for the
13239 // same type or for a type that is compatible according to the base language
13240 // rules.
13241 llvm::DenseMap<QualType, SourceLocation> PreviousRedeclTypes;
13242 OMPDeclareReductionDecl *PrevDRD = nullptr;
13243 bool InCompoundScope = true;
13244 if (S != nullptr) {
13245 // Find previous declaration with the same name not referenced in other
13246 // declarations.
13247 FunctionScopeInfo *ParentFn = getEnclosingFunction();
13248 InCompoundScope =
13249 (ParentFn != nullptr) && !ParentFn->CompoundScopes.empty();
13250 LookupName(Lookup, S);
13251 FilterLookupForScope(Lookup, DC, S, /*ConsiderLinkage=*/false,
13252 /*AllowInlineNamespace=*/false);
13253 llvm::DenseMap<OMPDeclareReductionDecl *, bool> UsedAsPrevious;
Alexey Bataeve3727102018-04-18 15:57:46 +000013254 LookupResult::Filter Filter = Lookup.makeFilter();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013255 while (Filter.hasNext()) {
13256 auto *PrevDecl = cast<OMPDeclareReductionDecl>(Filter.next());
13257 if (InCompoundScope) {
13258 auto I = UsedAsPrevious.find(PrevDecl);
13259 if (I == UsedAsPrevious.end())
13260 UsedAsPrevious[PrevDecl] = false;
Alexey Bataeve3727102018-04-18 15:57:46 +000013261 if (OMPDeclareReductionDecl *D = PrevDecl->getPrevDeclInScope())
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013262 UsedAsPrevious[D] = true;
13263 }
13264 PreviousRedeclTypes[PrevDecl->getType().getCanonicalType()] =
13265 PrevDecl->getLocation();
13266 }
13267 Filter.done();
13268 if (InCompoundScope) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013269 for (const auto &PrevData : UsedAsPrevious) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013270 if (!PrevData.second) {
13271 PrevDRD = PrevData.first;
13272 break;
13273 }
13274 }
13275 }
13276 } else if (PrevDeclInScope != nullptr) {
13277 auto *PrevDRDInScope = PrevDRD =
13278 cast<OMPDeclareReductionDecl>(PrevDeclInScope);
13279 do {
13280 PreviousRedeclTypes[PrevDRDInScope->getType().getCanonicalType()] =
13281 PrevDRDInScope->getLocation();
13282 PrevDRDInScope = PrevDRDInScope->getPrevDeclInScope();
13283 } while (PrevDRDInScope != nullptr);
13284 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013285 for (const auto &TyData : ReductionTypes) {
13286 const auto I = PreviousRedeclTypes.find(TyData.first.getCanonicalType());
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013287 bool Invalid = false;
13288 if (I != PreviousRedeclTypes.end()) {
13289 Diag(TyData.second, diag::err_omp_declare_reduction_redefinition)
13290 << TyData.first;
13291 Diag(I->second, diag::note_previous_definition);
13292 Invalid = true;
13293 }
13294 PreviousRedeclTypes[TyData.first.getCanonicalType()] = TyData.second;
13295 auto *DRD = OMPDeclareReductionDecl::Create(Context, DC, TyData.second,
13296 Name, TyData.first, PrevDRD);
13297 DC->addDecl(DRD);
13298 DRD->setAccess(AS);
13299 Decls.push_back(DRD);
13300 if (Invalid)
13301 DRD->setInvalidDecl();
13302 else
13303 PrevDRD = DRD;
13304 }
13305
13306 return DeclGroupPtrTy::make(
13307 DeclGroupRef::Create(Context, Decls.begin(), Decls.size()));
13308}
13309
13310void Sema::ActOnOpenMPDeclareReductionCombinerStart(Scope *S, Decl *D) {
13311 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13312
13313 // Enter new function scope.
13314 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013315 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013316 getCurFunction()->setHasOMPDeclareReductionCombiner();
13317
13318 if (S != nullptr)
13319 PushDeclContext(S, DRD);
13320 else
13321 CurContext = DRD;
13322
Faisal Valid143a0c2017-04-01 21:30:49 +000013323 PushExpressionEvaluationContext(
13324 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013325
13326 QualType ReductionType = DRD->getType();
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013327 // Create 'T* omp_parm;T omp_in;'. All references to 'omp_in' will
13328 // be replaced by '*omp_parm' during codegen. This required because 'omp_in'
13329 // uses semantics of argument handles by value, but it should be passed by
13330 // reference. C lang does not support references, so pass all parameters as
13331 // pointers.
13332 // Create 'T omp_in;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013333 VarDecl *OmpInParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013334 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_in");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013335 // Create 'T* omp_parm;T omp_out;'. All references to 'omp_out' will
13336 // be replaced by '*omp_parm' during codegen. This required because 'omp_out'
13337 // uses semantics of argument handles by value, but it should be passed by
13338 // reference. C lang does not support references, so pass all parameters as
13339 // pointers.
13340 // Create 'T omp_out;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013341 VarDecl *OmpOutParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013342 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_out");
13343 if (S != nullptr) {
13344 PushOnScopeChains(OmpInParm, S);
13345 PushOnScopeChains(OmpOutParm, S);
13346 } else {
13347 DRD->addDecl(OmpInParm);
13348 DRD->addDecl(OmpOutParm);
13349 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013350 Expr *InE =
13351 ::buildDeclRefExpr(*this, OmpInParm, ReductionType, D->getLocation());
13352 Expr *OutE =
13353 ::buildDeclRefExpr(*this, OmpOutParm, ReductionType, D->getLocation());
13354 DRD->setCombinerData(InE, OutE);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013355}
13356
13357void Sema::ActOnOpenMPDeclareReductionCombinerEnd(Decl *D, Expr *Combiner) {
13358 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13359 DiscardCleanupsInEvaluationContext();
13360 PopExpressionEvaluationContext();
13361
13362 PopDeclContext();
13363 PopFunctionScopeInfo();
13364
13365 if (Combiner != nullptr)
13366 DRD->setCombiner(Combiner);
13367 else
13368 DRD->setInvalidDecl();
13369}
13370
Alexey Bataev070f43a2017-09-06 14:49:58 +000013371VarDecl *Sema::ActOnOpenMPDeclareReductionInitializerStart(Scope *S, Decl *D) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013372 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13373
13374 // Enter new function scope.
13375 PushFunctionScope();
Reid Kleckner87a31802018-03-12 21:43:02 +000013376 setFunctionHasBranchProtectedScope();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013377
13378 if (S != nullptr)
13379 PushDeclContext(S, DRD);
13380 else
13381 CurContext = DRD;
13382
Faisal Valid143a0c2017-04-01 21:30:49 +000013383 PushExpressionEvaluationContext(
13384 ExpressionEvaluationContext::PotentiallyEvaluated);
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013385
13386 QualType ReductionType = DRD->getType();
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013387 // Create 'T* omp_parm;T omp_priv;'. All references to 'omp_priv' will
13388 // be replaced by '*omp_parm' during codegen. This required because 'omp_priv'
13389 // uses semantics of argument handles by value, but it should be passed by
13390 // reference. C lang does not support references, so pass all parameters as
13391 // pointers.
13392 // Create 'T omp_priv;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013393 VarDecl *OmpPrivParm =
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013394 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_priv");
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013395 // Create 'T* omp_parm;T omp_orig;'. All references to 'omp_orig' will
13396 // be replaced by '*omp_parm' during codegen. This required because 'omp_orig'
13397 // uses semantics of argument handles by value, but it should be passed by
13398 // reference. C lang does not support references, so pass all parameters as
13399 // pointers.
13400 // Create 'T omp_orig;' variable.
Alexey Bataeve3727102018-04-18 15:57:46 +000013401 VarDecl *OmpOrigParm =
Alexey Bataeva839ddd2016-03-17 10:19:46 +000013402 buildVarDecl(*this, D->getLocation(), ReductionType, "omp_orig");
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013403 if (S != nullptr) {
13404 PushOnScopeChains(OmpPrivParm, S);
13405 PushOnScopeChains(OmpOrigParm, S);
13406 } else {
13407 DRD->addDecl(OmpPrivParm);
13408 DRD->addDecl(OmpOrigParm);
13409 }
Alexey Bataeve6aa4692018-09-13 16:54:05 +000013410 Expr *OrigE =
13411 ::buildDeclRefExpr(*this, OmpOrigParm, ReductionType, D->getLocation());
13412 Expr *PrivE =
13413 ::buildDeclRefExpr(*this, OmpPrivParm, ReductionType, D->getLocation());
13414 DRD->setInitializerData(OrigE, PrivE);
Alexey Bataev070f43a2017-09-06 14:49:58 +000013415 return OmpPrivParm;
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013416}
13417
Alexey Bataev070f43a2017-09-06 14:49:58 +000013418void Sema::ActOnOpenMPDeclareReductionInitializerEnd(Decl *D, Expr *Initializer,
13419 VarDecl *OmpPrivParm) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013420 auto *DRD = cast<OMPDeclareReductionDecl>(D);
13421 DiscardCleanupsInEvaluationContext();
13422 PopExpressionEvaluationContext();
13423
13424 PopDeclContext();
13425 PopFunctionScopeInfo();
13426
Alexey Bataev070f43a2017-09-06 14:49:58 +000013427 if (Initializer != nullptr) {
13428 DRD->setInitializer(Initializer, OMPDeclareReductionDecl::CallInit);
13429 } else if (OmpPrivParm->hasInit()) {
13430 DRD->setInitializer(OmpPrivParm->getInit(),
13431 OmpPrivParm->isDirectInit()
13432 ? OMPDeclareReductionDecl::DirectInit
13433 : OMPDeclareReductionDecl::CopyInit);
13434 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013435 DRD->setInvalidDecl();
Alexey Bataev070f43a2017-09-06 14:49:58 +000013436 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013437}
13438
13439Sema::DeclGroupPtrTy Sema::ActOnOpenMPDeclareReductionDirectiveEnd(
13440 Scope *S, DeclGroupPtrTy DeclReductions, bool IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013441 for (Decl *D : DeclReductions.get()) {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013442 if (IsValid) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013443 if (S)
13444 PushOnScopeChains(cast<OMPDeclareReductionDecl>(D), S,
13445 /*AddToContext=*/false);
13446 } else {
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013447 D->setInvalidDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013448 }
Alexey Bataev94a4f0c2016-03-03 05:21:39 +000013449 }
13450 return DeclReductions;
13451}
13452
David Majnemer9d168222016-08-05 17:44:54 +000013453OMPClause *Sema::ActOnOpenMPNumTeamsClause(Expr *NumTeams,
Kelvin Li099bb8c2015-11-24 20:50:12 +000013454 SourceLocation StartLoc,
13455 SourceLocation LParenLoc,
13456 SourceLocation EndLoc) {
13457 Expr *ValExpr = NumTeams;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013458 Stmt *HelperValStmt = nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013459
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013460 // OpenMP [teams Constrcut, Restrictions]
13461 // The num_teams expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013462 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_teams,
Alexey Bataeva0569352015-12-01 10:17:31 +000013463 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013464 return nullptr;
Kelvin Li099bb8c2015-11-24 20:50:12 +000013465
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013466 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013467 OpenMPDirectiveKind CaptureRegion =
13468 getOpenMPCaptureRegionForClause(DKind, OMPC_num_teams);
13469 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013470 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013471 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacobbc126342017-01-25 11:28:18 +000013472 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13473 HelperValStmt = buildPreInits(Context, Captures);
13474 }
13475
13476 return new (Context) OMPNumTeamsClause(ValExpr, HelperValStmt, CaptureRegion,
13477 StartLoc, LParenLoc, EndLoc);
Kelvin Li099bb8c2015-11-24 20:50:12 +000013478}
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013479
13480OMPClause *Sema::ActOnOpenMPThreadLimitClause(Expr *ThreadLimit,
13481 SourceLocation StartLoc,
13482 SourceLocation LParenLoc,
13483 SourceLocation EndLoc) {
13484 Expr *ValExpr = ThreadLimit;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013485 Stmt *HelperValStmt = nullptr;
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013486
13487 // OpenMP [teams Constrcut, Restrictions]
13488 // The thread_limit expression must evaluate to a positive integer value.
Alexey Bataeve3727102018-04-18 15:57:46 +000013489 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_thread_limit,
Alexey Bataeva0569352015-12-01 10:17:31 +000013490 /*StrictlyPositive=*/true))
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013491 return nullptr;
13492
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013493 OpenMPDirectiveKind DKind = DSAStack->getCurrentDirective();
Alexey Bataev2ba67042017-11-28 21:11:44 +000013494 OpenMPDirectiveKind CaptureRegion =
13495 getOpenMPCaptureRegionForClause(DKind, OMPC_thread_limit);
13496 if (CaptureRegion != OMPD_unknown && !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013497 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013498 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Arpith Chacko Jacob7ecc0b72017-01-25 11:44:35 +000013499 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13500 HelperValStmt = buildPreInits(Context, Captures);
13501 }
13502
13503 return new (Context) OMPThreadLimitClause(
13504 ValExpr, HelperValStmt, CaptureRegion, StartLoc, LParenLoc, EndLoc);
Kelvin Lia15fb1a2015-11-27 18:47:36 +000013505}
Alexey Bataeva0569352015-12-01 10:17:31 +000013506
13507OMPClause *Sema::ActOnOpenMPPriorityClause(Expr *Priority,
13508 SourceLocation StartLoc,
13509 SourceLocation LParenLoc,
13510 SourceLocation EndLoc) {
13511 Expr *ValExpr = Priority;
13512
13513 // OpenMP [2.9.1, task Constrcut]
13514 // The priority-value is a non-negative numerical scalar expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013515 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_priority,
Alexey Bataeva0569352015-12-01 10:17:31 +000013516 /*StrictlyPositive=*/false))
13517 return nullptr;
13518
13519 return new (Context) OMPPriorityClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13520}
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013521
13522OMPClause *Sema::ActOnOpenMPGrainsizeClause(Expr *Grainsize,
13523 SourceLocation StartLoc,
13524 SourceLocation LParenLoc,
13525 SourceLocation EndLoc) {
13526 Expr *ValExpr = Grainsize;
13527
13528 // OpenMP [2.9.2, taskloop Constrcut]
13529 // The parameter of the grainsize clause must be a positive integer
13530 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013531 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_grainsize,
Alexey Bataev1fd4aed2015-12-07 12:52:51 +000013532 /*StrictlyPositive=*/true))
13533 return nullptr;
13534
13535 return new (Context) OMPGrainsizeClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13536}
Alexey Bataev382967a2015-12-08 12:06:20 +000013537
13538OMPClause *Sema::ActOnOpenMPNumTasksClause(Expr *NumTasks,
13539 SourceLocation StartLoc,
13540 SourceLocation LParenLoc,
13541 SourceLocation EndLoc) {
13542 Expr *ValExpr = NumTasks;
13543
13544 // OpenMP [2.9.2, taskloop Constrcut]
13545 // The parameter of the num_tasks clause must be a positive integer
13546 // expression.
Alexey Bataeve3727102018-04-18 15:57:46 +000013547 if (!isNonNegativeIntegerValue(ValExpr, *this, OMPC_num_tasks,
Alexey Bataev382967a2015-12-08 12:06:20 +000013548 /*StrictlyPositive=*/true))
13549 return nullptr;
13550
13551 return new (Context) OMPNumTasksClause(ValExpr, StartLoc, LParenLoc, EndLoc);
13552}
13553
Alexey Bataev28c75412015-12-15 08:19:24 +000013554OMPClause *Sema::ActOnOpenMPHintClause(Expr *Hint, SourceLocation StartLoc,
13555 SourceLocation LParenLoc,
13556 SourceLocation EndLoc) {
13557 // OpenMP [2.13.2, critical construct, Description]
13558 // ... where hint-expression is an integer constant expression that evaluates
13559 // to a valid lock hint.
13560 ExprResult HintExpr = VerifyPositiveIntegerConstantInClause(Hint, OMPC_hint);
13561 if (HintExpr.isInvalid())
13562 return nullptr;
13563 return new (Context)
13564 OMPHintClause(HintExpr.get(), StartLoc, LParenLoc, EndLoc);
13565}
13566
Carlo Bertollib4adf552016-01-15 18:50:31 +000013567OMPClause *Sema::ActOnOpenMPDistScheduleClause(
13568 OpenMPDistScheduleClauseKind Kind, Expr *ChunkSize, SourceLocation StartLoc,
13569 SourceLocation LParenLoc, SourceLocation KindLoc, SourceLocation CommaLoc,
13570 SourceLocation EndLoc) {
13571 if (Kind == OMPC_DIST_SCHEDULE_unknown) {
13572 std::string Values;
13573 Values += "'";
13574 Values += getOpenMPSimpleClauseTypeName(OMPC_dist_schedule, 0);
13575 Values += "'";
13576 Diag(KindLoc, diag::err_omp_unexpected_clause_value)
13577 << Values << getOpenMPClauseName(OMPC_dist_schedule);
13578 return nullptr;
13579 }
13580 Expr *ValExpr = ChunkSize;
Alexey Bataev3392d762016-02-16 11:18:12 +000013581 Stmt *HelperValStmt = nullptr;
Carlo Bertollib4adf552016-01-15 18:50:31 +000013582 if (ChunkSize) {
13583 if (!ChunkSize->isValueDependent() && !ChunkSize->isTypeDependent() &&
13584 !ChunkSize->isInstantiationDependent() &&
13585 !ChunkSize->containsUnexpandedParameterPack()) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013586 SourceLocation ChunkSizeLoc = ChunkSize->getBeginLoc();
Carlo Bertollib4adf552016-01-15 18:50:31 +000013587 ExprResult Val =
13588 PerformOpenMPImplicitIntegerConversion(ChunkSizeLoc, ChunkSize);
13589 if (Val.isInvalid())
13590 return nullptr;
13591
13592 ValExpr = Val.get();
13593
13594 // OpenMP [2.7.1, Restrictions]
13595 // chunk_size must be a loop invariant integer expression with a positive
13596 // value.
13597 llvm::APSInt Result;
13598 if (ValExpr->isIntegerConstantExpr(Result, Context)) {
13599 if (Result.isSigned() && !Result.isStrictlyPositive()) {
13600 Diag(ChunkSizeLoc, diag::err_omp_negative_expression_in_clause)
13601 << "dist_schedule" << ChunkSize->getSourceRange();
13602 return nullptr;
13603 }
Alexey Bataev2ba67042017-11-28 21:11:44 +000013604 } else if (getOpenMPCaptureRegionForClause(
13605 DSAStack->getCurrentDirective(), OMPC_dist_schedule) !=
13606 OMPD_unknown &&
Alexey Bataevb46cdea2016-06-15 11:20:48 +000013607 !CurContext->isDependentContext()) {
Alexey Bataev8e769ee2017-12-22 21:01:52 +000013608 ValExpr = MakeFullExpr(ValExpr).get();
Alexey Bataeve3727102018-04-18 15:57:46 +000013609 llvm::MapVector<const Expr *, DeclRefExpr *> Captures;
Alexey Bataev5a3af132016-03-29 08:58:54 +000013610 ValExpr = tryBuildCapture(*this, ValExpr, Captures).get();
13611 HelperValStmt = buildPreInits(Context, Captures);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013612 }
13613 }
13614 }
13615
13616 return new (Context)
13617 OMPDistScheduleClause(StartLoc, LParenLoc, KindLoc, CommaLoc, EndLoc,
Alexey Bataev3392d762016-02-16 11:18:12 +000013618 Kind, ValExpr, HelperValStmt);
Carlo Bertollib4adf552016-01-15 18:50:31 +000013619}
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013620
13621OMPClause *Sema::ActOnOpenMPDefaultmapClause(
13622 OpenMPDefaultmapClauseModifier M, OpenMPDefaultmapClauseKind Kind,
13623 SourceLocation StartLoc, SourceLocation LParenLoc, SourceLocation MLoc,
13624 SourceLocation KindLoc, SourceLocation EndLoc) {
13625 // OpenMP 4.5 only supports 'defaultmap(tofrom: scalar)'
David Majnemer9d168222016-08-05 17:44:54 +000013626 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom || Kind != OMPC_DEFAULTMAP_scalar) {
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013627 std::string Value;
13628 SourceLocation Loc;
13629 Value += "'";
13630 if (M != OMPC_DEFAULTMAP_MODIFIER_tofrom) {
13631 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013632 OMPC_DEFAULTMAP_MODIFIER_tofrom);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013633 Loc = MLoc;
13634 } else {
13635 Value += getOpenMPSimpleClauseTypeName(OMPC_defaultmap,
David Majnemer9d168222016-08-05 17:44:54 +000013636 OMPC_DEFAULTMAP_scalar);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013637 Loc = KindLoc;
13638 }
13639 Value += "'";
13640 Diag(Loc, diag::err_omp_unexpected_clause_value)
13641 << Value << getOpenMPClauseName(OMPC_defaultmap);
13642 return nullptr;
13643 }
Alexey Bataev2fd0cb22017-10-05 17:51:39 +000013644 DSAStack->setDefaultDMAToFromScalar(StartLoc);
Arpith Chacko Jacob3cf89042016-01-26 16:37:23 +000013645
13646 return new (Context)
13647 OMPDefaultmapClause(StartLoc, LParenLoc, MLoc, KindLoc, EndLoc, Kind, M);
13648}
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013649
13650bool Sema::ActOnStartOpenMPDeclareTargetDirective(SourceLocation Loc) {
13651 DeclContext *CurLexicalContext = getCurLexicalContext();
13652 if (!CurLexicalContext->isFileContext() &&
13653 !CurLexicalContext->isExternCContext() &&
Alexey Bataev502ec492017-10-03 20:00:00 +000013654 !CurLexicalContext->isExternCXXContext() &&
13655 !isa<CXXRecordDecl>(CurLexicalContext) &&
13656 !isa<ClassTemplateDecl>(CurLexicalContext) &&
13657 !isa<ClassTemplatePartialSpecializationDecl>(CurLexicalContext) &&
13658 !isa<ClassTemplateSpecializationDecl>(CurLexicalContext)) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013659 Diag(Loc, diag::err_omp_region_not_file_context);
13660 return false;
13661 }
Kelvin Libc38e632018-09-10 02:07:09 +000013662 ++DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013663 return true;
13664}
13665
13666void Sema::ActOnFinishOpenMPDeclareTargetDirective() {
Kelvin Libc38e632018-09-10 02:07:09 +000013667 assert(DeclareTargetNestingLevel > 0 &&
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013668 "Unexpected ActOnFinishOpenMPDeclareTargetDirective");
Kelvin Libc38e632018-09-10 02:07:09 +000013669 --DeclareTargetNestingLevel;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013670}
13671
David Majnemer9d168222016-08-05 17:44:54 +000013672void Sema::ActOnOpenMPDeclareTargetName(Scope *CurScope,
13673 CXXScopeSpec &ScopeSpec,
13674 const DeclarationNameInfo &Id,
13675 OMPDeclareTargetDeclAttr::MapTypeTy MT,
13676 NamedDeclSetType &SameDirectiveDecls) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013677 LookupResult Lookup(*this, Id, LookupOrdinaryName);
13678 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
13679
13680 if (Lookup.isAmbiguous())
13681 return;
13682 Lookup.suppressDiagnostics();
13683
13684 if (!Lookup.isSingleResult()) {
13685 if (TypoCorrection Corrected =
13686 CorrectTypo(Id, LookupOrdinaryName, CurScope, nullptr,
13687 llvm::make_unique<VarOrFuncDeclFilterCCC>(*this),
13688 CTK_ErrorRecovery)) {
13689 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_var_use_suggest)
13690 << Id.getName());
13691 checkDeclIsAllowedInOpenMPTarget(nullptr, Corrected.getCorrectionDecl());
13692 return;
13693 }
13694
13695 Diag(Id.getLoc(), diag::err_undeclared_var_use) << Id.getName();
13696 return;
13697 }
13698
13699 NamedDecl *ND = Lookup.getAsSingle<NamedDecl>();
Alexey Bataev30a78212018-09-11 13:59:10 +000013700 if (isa<VarDecl>(ND) || isa<FunctionDecl>(ND) ||
13701 isa<FunctionTemplateDecl>(ND)) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013702 if (!SameDirectiveDecls.insert(cast<NamedDecl>(ND->getCanonicalDecl())))
13703 Diag(Id.getLoc(), diag::err_omp_declare_target_multiple) << Id.getName();
Alexey Bataev30a78212018-09-11 13:59:10 +000013704 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13705 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(
13706 cast<ValueDecl>(ND));
13707 if (!Res) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013708 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(Context, MT);
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013709 ND->addAttr(A);
13710 if (ASTMutationListener *ML = Context.getASTMutationListener())
13711 ML->DeclarationMarkedOpenMPDeclareTarget(ND, A);
Kelvin Li1ce87c72017-12-12 20:08:12 +000013712 checkDeclIsAllowedInOpenMPTarget(nullptr, ND, Id.getLoc());
Alexey Bataev30a78212018-09-11 13:59:10 +000013713 } else if (*Res != MT) {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013714 Diag(Id.getLoc(), diag::err_omp_declare_target_to_and_link)
13715 << Id.getName();
13716 }
Alexey Bataeve3727102018-04-18 15:57:46 +000013717 } else {
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013718 Diag(Id.getLoc(), diag::err_omp_invalid_target_decl) << Id.getName();
Alexey Bataeve3727102018-04-18 15:57:46 +000013719 }
Dmitry Polukhind69b5052016-05-09 14:59:13 +000013720}
13721
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013722static void checkDeclInTargetContext(SourceLocation SL, SourceRange SR,
13723 Sema &SemaRef, Decl *D) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013724 if (!D || !isa<VarDecl>(D))
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013725 return;
Alexey Bataev30a78212018-09-11 13:59:10 +000013726 auto *VD = cast<VarDecl>(D);
13727 if (OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
13728 return;
13729 SemaRef.Diag(VD->getLocation(), diag::warn_omp_not_in_target_context);
13730 SemaRef.Diag(SL, diag::note_used_here) << SR;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013731}
13732
13733static bool checkValueDeclInTarget(SourceLocation SL, SourceRange SR,
13734 Sema &SemaRef, DSAStackTy *Stack,
13735 ValueDecl *VD) {
Alexey Bataeve3727102018-04-18 15:57:46 +000013736 return VD->hasAttr<OMPDeclareTargetDeclAttr>() ||
13737 checkTypeMappable(SL, SR, SemaRef, Stack, VD->getType(),
13738 /*FullCheck=*/false);
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013739}
13740
Kelvin Li1ce87c72017-12-12 20:08:12 +000013741void Sema::checkDeclIsAllowedInOpenMPTarget(Expr *E, Decl *D,
13742 SourceLocation IdLoc) {
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013743 if (!D || D->isInvalidDecl())
13744 return;
13745 SourceRange SR = E ? E->getSourceRange() : D->getSourceRange();
Stephen Kellyf2ceec42018-08-09 21:08:08 +000013746 SourceLocation SL = E ? E->getBeginLoc() : D->getLocation();
Alexey Bataeve3727102018-04-18 15:57:46 +000013747 if (auto *VD = dyn_cast<VarDecl>(D)) {
Alexey Bataevc1943e72018-07-09 19:58:08 +000013748 // Only global variables can be marked as declare target.
Alexey Bataev30a78212018-09-11 13:59:10 +000013749 if (!VD->isFileVarDecl() && !VD->isStaticLocal() &&
13750 !VD->isStaticDataMember())
Alexey Bataevc1943e72018-07-09 19:58:08 +000013751 return;
13752 // 2.10.6: threadprivate variable cannot appear in a declare target
13753 // directive.
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013754 if (DSAStack->isThreadPrivate(VD)) {
13755 Diag(SL, diag::err_omp_threadprivate_in_target);
Alexey Bataeve3727102018-04-18 15:57:46 +000013756 reportOriginalDsa(*this, DSAStack, VD, DSAStack->getTopDSA(VD, false));
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013757 return;
13758 }
13759 }
Alexey Bataev97b72212018-08-14 18:31:20 +000013760 if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
13761 D = FTD->getTemplatedDecl();
Alexey Bataeve3727102018-04-18 15:57:46 +000013762 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
Alexey Bataev30a78212018-09-11 13:59:10 +000013763 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> Res =
13764 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(FD);
13765 if (Res && *Res == OMPDeclareTargetDeclAttr::MT_Link) {
Kelvin Li1ce87c72017-12-12 20:08:12 +000013766 assert(IdLoc.isValid() && "Source location is expected");
13767 Diag(IdLoc, diag::err_omp_function_in_link_clause);
13768 Diag(FD->getLocation(), diag::note_defined_here) << FD;
13769 return;
13770 }
13771 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013772 if (auto *VD = dyn_cast<ValueDecl>(D)) {
13773 // Problem if any with var declared with incomplete type will be reported
13774 // as normal, so no need to check it here.
13775 if ((E || !VD->getType()->isIncompleteType()) &&
13776 !checkValueDeclInTarget(SL, SR, *this, DSAStack, VD))
13777 return;
13778 if (!E && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
13779 // Checking declaration inside declare target region.
13780 if (isa<VarDecl>(D) || isa<FunctionDecl>(D) ||
13781 isa<FunctionTemplateDecl>(D)) {
13782 auto *A = OMPDeclareTargetDeclAttr::CreateImplicit(
13783 Context, OMPDeclareTargetDeclAttr::MT_To);
13784 D->addAttr(A);
13785 if (ASTMutationListener *ML = Context.getASTMutationListener())
13786 ML->DeclarationMarkedOpenMPDeclareTarget(D, A);
13787 }
13788 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013789 }
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013790 }
Alexey Bataev30a78212018-09-11 13:59:10 +000013791 if (!E)
13792 return;
Dmitry Polukhin0b0da292016-04-06 11:38:59 +000013793 checkDeclInTargetContext(E->getExprLoc(), E->getSourceRange(), *this, D);
13794}
Samuel Antao661c0902016-05-26 17:39:58 +000013795
13796OMPClause *Sema::ActOnOpenMPToClause(ArrayRef<Expr *> VarList,
13797 SourceLocation StartLoc,
13798 SourceLocation LParenLoc,
13799 SourceLocation EndLoc) {
13800 MappableVarListInfo MVLI(VarList);
13801 checkMappableExpressionList(*this, DSAStack, OMPC_to, MVLI, StartLoc);
13802 if (MVLI.ProcessedVarList.empty())
13803 return nullptr;
13804
13805 return OMPToClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13806 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13807 MVLI.VarComponents);
13808}
Samuel Antaoec172c62016-05-26 17:49:04 +000013809
13810OMPClause *Sema::ActOnOpenMPFromClause(ArrayRef<Expr *> VarList,
13811 SourceLocation StartLoc,
13812 SourceLocation LParenLoc,
13813 SourceLocation EndLoc) {
13814 MappableVarListInfo MVLI(VarList);
13815 checkMappableExpressionList(*this, DSAStack, OMPC_from, MVLI, StartLoc);
13816 if (MVLI.ProcessedVarList.empty())
13817 return nullptr;
13818
13819 return OMPFromClause::Create(Context, StartLoc, LParenLoc, EndLoc,
13820 MVLI.ProcessedVarList, MVLI.VarBaseDeclarations,
13821 MVLI.VarComponents);
13822}
Carlo Bertolli2404b172016-07-13 15:37:16 +000013823
13824OMPClause *Sema::ActOnOpenMPUseDevicePtrClause(ArrayRef<Expr *> VarList,
13825 SourceLocation StartLoc,
13826 SourceLocation LParenLoc,
13827 SourceLocation EndLoc) {
Samuel Antaocc10b852016-07-28 14:23:26 +000013828 MappableVarListInfo MVLI(VarList);
13829 SmallVector<Expr *, 8> PrivateCopies;
13830 SmallVector<Expr *, 8> Inits;
13831
Alexey Bataeve3727102018-04-18 15:57:46 +000013832 for (Expr *RefExpr : VarList) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013833 assert(RefExpr && "NULL expr in OpenMP use_device_ptr clause.");
13834 SourceLocation ELoc;
13835 SourceRange ERange;
13836 Expr *SimpleRefExpr = RefExpr;
13837 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13838 if (Res.second) {
13839 // It will be analyzed later.
Samuel Antaocc10b852016-07-28 14:23:26 +000013840 MVLI.ProcessedVarList.push_back(RefExpr);
13841 PrivateCopies.push_back(nullptr);
13842 Inits.push_back(nullptr);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013843 }
13844 ValueDecl *D = Res.first;
13845 if (!D)
13846 continue;
13847
13848 QualType Type = D->getType();
Samuel Antaocc10b852016-07-28 14:23:26 +000013849 Type = Type.getNonReferenceType().getUnqualifiedType();
13850
13851 auto *VD = dyn_cast<VarDecl>(D);
13852
13853 // Item should be a pointer or reference to pointer.
13854 if (!Type->isPointerType()) {
Carlo Bertolli2404b172016-07-13 15:37:16 +000013855 Diag(ELoc, diag::err_omp_usedeviceptr_not_a_pointer)
13856 << 0 << RefExpr->getSourceRange();
13857 continue;
13858 }
Samuel Antaocc10b852016-07-28 14:23:26 +000013859
13860 // Build the private variable and the expression that refers to it.
Alexey Bataev63cc8e92018-03-20 14:45:59 +000013861 auto VDPrivate =
13862 buildVarDecl(*this, ELoc, Type, D->getName(),
13863 D->hasAttrs() ? &D->getAttrs() : nullptr,
13864 VD ? cast<DeclRefExpr>(SimpleRefExpr) : nullptr);
Samuel Antaocc10b852016-07-28 14:23:26 +000013865 if (VDPrivate->isInvalidDecl())
13866 continue;
13867
13868 CurContext->addDecl(VDPrivate);
Alexey Bataeve3727102018-04-18 15:57:46 +000013869 DeclRefExpr *VDPrivateRefExpr = buildDeclRefExpr(
Samuel Antaocc10b852016-07-28 14:23:26 +000013870 *this, VDPrivate, RefExpr->getType().getUnqualifiedType(), ELoc);
13871
13872 // Add temporary variable to initialize the private copy of the pointer.
Alexey Bataeve3727102018-04-18 15:57:46 +000013873 VarDecl *VDInit =
Samuel Antaocc10b852016-07-28 14:23:26 +000013874 buildVarDecl(*this, RefExpr->getExprLoc(), Type, ".devptr.temp");
Alexey Bataeve3727102018-04-18 15:57:46 +000013875 DeclRefExpr *VDInitRefExpr = buildDeclRefExpr(
13876 *this, VDInit, RefExpr->getType(), RefExpr->getExprLoc());
Samuel Antaocc10b852016-07-28 14:23:26 +000013877 AddInitializerToDecl(VDPrivate,
13878 DefaultLvalueConversion(VDInitRefExpr).get(),
Richard Smith3beb7c62017-01-12 02:27:38 +000013879 /*DirectInit=*/false);
Samuel Antaocc10b852016-07-28 14:23:26 +000013880
13881 // If required, build a capture to implement the privatization initialized
13882 // with the current list item value.
13883 DeclRefExpr *Ref = nullptr;
13884 if (!VD)
13885 Ref = buildCapture(*this, D, SimpleRefExpr, /*WithInit=*/true);
13886 MVLI.ProcessedVarList.push_back(VD ? RefExpr->IgnoreParens() : Ref);
13887 PrivateCopies.push_back(VDPrivateRefExpr);
13888 Inits.push_back(VDInitRefExpr);
13889
13890 // We need to add a data sharing attribute for this variable to make sure it
13891 // is correctly captured. A variable that shows up in a use_device_ptr has
13892 // similar properties of a first private variable.
13893 DSAStack->addDSA(D, RefExpr->IgnoreParens(), OMPC_firstprivate, Ref);
13894
13895 // Create a mappable component for the list item. List items in this clause
13896 // only need a component.
13897 MVLI.VarBaseDeclarations.push_back(D);
13898 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13899 MVLI.VarComponents.back().push_back(
13900 OMPClauseMappableExprCommon::MappableComponent(SimpleRefExpr, D));
Carlo Bertolli2404b172016-07-13 15:37:16 +000013901 }
13902
Samuel Antaocc10b852016-07-28 14:23:26 +000013903 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli2404b172016-07-13 15:37:16 +000013904 return nullptr;
13905
Samuel Antaocc10b852016-07-28 14:23:26 +000013906 return OMPUseDevicePtrClause::Create(
13907 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13908 PrivateCopies, Inits, MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli2404b172016-07-13 15:37:16 +000013909}
Carlo Bertolli70594e92016-07-13 17:16:49 +000013910
13911OMPClause *Sema::ActOnOpenMPIsDevicePtrClause(ArrayRef<Expr *> VarList,
13912 SourceLocation StartLoc,
13913 SourceLocation LParenLoc,
13914 SourceLocation EndLoc) {
Samuel Antao6890b092016-07-28 14:25:09 +000013915 MappableVarListInfo MVLI(VarList);
Alexey Bataeve3727102018-04-18 15:57:46 +000013916 for (Expr *RefExpr : VarList) {
Kelvin Li84376252016-12-14 15:39:58 +000013917 assert(RefExpr && "NULL expr in OpenMP is_device_ptr clause.");
Carlo Bertolli70594e92016-07-13 17:16:49 +000013918 SourceLocation ELoc;
13919 SourceRange ERange;
13920 Expr *SimpleRefExpr = RefExpr;
13921 auto Res = getPrivateItem(*this, SimpleRefExpr, ELoc, ERange);
13922 if (Res.second) {
13923 // It will be analyzed later.
Samuel Antao6890b092016-07-28 14:25:09 +000013924 MVLI.ProcessedVarList.push_back(RefExpr);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013925 }
13926 ValueDecl *D = Res.first;
13927 if (!D)
13928 continue;
13929
13930 QualType Type = D->getType();
13931 // item should be a pointer or array or reference to pointer or array
13932 if (!Type.getNonReferenceType()->isPointerType() &&
13933 !Type.getNonReferenceType()->isArrayType()) {
13934 Diag(ELoc, diag::err_omp_argument_type_isdeviceptr)
13935 << 0 << RefExpr->getSourceRange();
13936 continue;
13937 }
Samuel Antao6890b092016-07-28 14:25:09 +000013938
13939 // Check if the declaration in the clause does not show up in any data
13940 // sharing attribute.
Alexey Bataeve3727102018-04-18 15:57:46 +000013941 DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(D, /*FromParent=*/false);
Samuel Antao6890b092016-07-28 14:25:09 +000013942 if (isOpenMPPrivate(DVar.CKind)) {
13943 Diag(ELoc, diag::err_omp_variable_in_given_clause_and_dsa)
13944 << getOpenMPClauseName(DVar.CKind)
13945 << getOpenMPClauseName(OMPC_is_device_ptr)
13946 << getOpenMPDirectiveName(DSAStack->getCurrentDirective());
Alexey Bataeve3727102018-04-18 15:57:46 +000013947 reportOriginalDsa(*this, DSAStack, D, DVar);
Samuel Antao6890b092016-07-28 14:25:09 +000013948 continue;
13949 }
13950
Alexey Bataeve3727102018-04-18 15:57:46 +000013951 const Expr *ConflictExpr;
Samuel Antao6890b092016-07-28 14:25:09 +000013952 if (DSAStack->checkMappableExprComponentListsForDecl(
David Majnemer9d168222016-08-05 17:44:54 +000013953 D, /*CurrentRegionOnly=*/true,
Samuel Antao6890b092016-07-28 14:25:09 +000013954 [&ConflictExpr](
13955 OMPClauseMappableExprCommon::MappableExprComponentListRef R,
13956 OpenMPClauseKind) -> bool {
13957 ConflictExpr = R.front().getAssociatedExpression();
13958 return true;
13959 })) {
13960 Diag(ELoc, diag::err_omp_map_shared_storage) << RefExpr->getSourceRange();
13961 Diag(ConflictExpr->getExprLoc(), diag::note_used_here)
13962 << ConflictExpr->getSourceRange();
13963 continue;
13964 }
13965
13966 // Store the components in the stack so that they can be used to check
13967 // against other clauses later on.
13968 OMPClauseMappableExprCommon::MappableComponent MC(SimpleRefExpr, D);
13969 DSAStack->addMappableExpressionComponents(
13970 D, MC, /*WhereFoundClauseKind=*/OMPC_is_device_ptr);
13971
13972 // Record the expression we've just processed.
13973 MVLI.ProcessedVarList.push_back(SimpleRefExpr);
13974
13975 // Create a mappable component for the list item. List items in this clause
13976 // only need a component. We use a null declaration to signal fields in
13977 // 'this'.
13978 assert((isa<DeclRefExpr>(SimpleRefExpr) ||
13979 isa<CXXThisExpr>(cast<MemberExpr>(SimpleRefExpr)->getBase())) &&
13980 "Unexpected device pointer expression!");
13981 MVLI.VarBaseDeclarations.push_back(
13982 isa<DeclRefExpr>(SimpleRefExpr) ? D : nullptr);
13983 MVLI.VarComponents.resize(MVLI.VarComponents.size() + 1);
13984 MVLI.VarComponents.back().push_back(MC);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013985 }
13986
Samuel Antao6890b092016-07-28 14:25:09 +000013987 if (MVLI.ProcessedVarList.empty())
Carlo Bertolli70594e92016-07-13 17:16:49 +000013988 return nullptr;
13989
Samuel Antao6890b092016-07-28 14:25:09 +000013990 return OMPIsDevicePtrClause::Create(
13991 Context, StartLoc, LParenLoc, EndLoc, MVLI.ProcessedVarList,
13992 MVLI.VarBaseDeclarations, MVLI.VarComponents);
Carlo Bertolli70594e92016-07-13 17:16:49 +000013993}